blob: 064ff715bec3818e4cc7837ac15d6bea8d020fa8 [file] [log] [blame]
Yin-Chia Yehe074a932015-01-30 10:29:02 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "CameraModule"
18//#define LOG_NDEBUG 0
19
20#include "CameraModule.h"
21
22namespace android {
23
24void CameraModule::deriveCameraCharacteristicsKeys(
25 uint32_t deviceVersion, CameraMetadata &chars) {
26 // HAL1 devices should not reach here
27 if (deviceVersion < CAMERA_DEVICE_API_VERSION_2_0) {
28 ALOGV("%s: Cannot derive keys for HAL version < 2.0");
29 return;
30 }
31
32 // Keys added in HAL3.3
33 if (deviceVersion < CAMERA_DEVICE_API_VERSION_3_3) {
34 Vector<uint8_t> controlModes;
35 uint8_t data = ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE;
36 chars.update(ANDROID_CONTROL_AE_LOCK_AVAILABLE, &data, /*count*/1);
37 data = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_TRUE;
38 chars.update(ANDROID_CONTROL_AWB_LOCK_AVAILABLE, &data, /*count*/1);
Yin-Chia Yehe074a932015-01-30 10:29:02 -080039 controlModes.push(ANDROID_CONTROL_MODE_AUTO);
40 camera_metadata_entry entry = chars.find(ANDROID_CONTROL_AVAILABLE_SCENE_MODES);
41 if (entry.count > 1 || entry.data.u8[0] != ANDROID_CONTROL_SCENE_MODE_DISABLED) {
42 controlModes.push(ANDROID_CONTROL_MODE_USE_SCENE_MODE);
43 }
Zhijun He9c5af612015-05-04 16:34:37 -070044
45 // Only advertise CONTROL_OFF mode if 3A manual controls are supported.
46 bool isManualAeSupported = false;
47 bool isManualAfSupported = false;
48 bool isManualAwbSupported = false;
49 entry = chars.find(ANDROID_CONTROL_AE_AVAILABLE_MODES);
50 if (entry.count > 0) {
51 for (size_t i = 0; i < entry.count; i++) {
52 if (entry.data.u8[i] == ANDROID_CONTROL_AE_MODE_OFF) {
53 isManualAeSupported = true;
54 break;
55 }
56 }
57 }
58 entry = chars.find(ANDROID_CONTROL_AF_AVAILABLE_MODES);
59 if (entry.count > 0) {
60 for (size_t i = 0; i < entry.count; i++) {
61 if (entry.data.u8[i] == ANDROID_CONTROL_AF_MODE_OFF) {
62 isManualAfSupported = true;
63 break;
64 }
65 }
66 }
67 entry = chars.find(ANDROID_CONTROL_AWB_AVAILABLE_MODES);
68 if (entry.count > 0) {
69 for (size_t i = 0; i < entry.count; i++) {
70 if (entry.data.u8[i] == ANDROID_CONTROL_AWB_MODE_OFF) {
71 isManualAwbSupported = true;
72 break;
73 }
74 }
75 }
76 if (isManualAeSupported && isManualAfSupported && isManualAwbSupported) {
77 controlModes.push(ANDROID_CONTROL_MODE_OFF);
78 }
79
Yin-Chia Yehe074a932015-01-30 10:29:02 -080080 chars.update(ANDROID_CONTROL_AVAILABLE_MODES, controlModes);
81 }
82 return;
83}
84
85CameraModule::CameraModule(camera_module_t *module) {
86 if (module == NULL) {
87 ALOGE("%s: camera hardware module must not be null", __FUNCTION__);
88 assert(0);
89 }
90
91 mModule = module;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -080092 mCameraInfoMap.setCapacity(getNumberOfCameras());
Yin-Chia Yehe074a932015-01-30 10:29:02 -080093}
94
Eino-Ville Talvala1527f072015-04-07 15:55:31 -070095int CameraModule::init() {
96 if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 &&
97 mModule->init != NULL) {
98 return mModule->init();
99 }
100 return OK;
101}
102
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800103int CameraModule::getCameraInfo(int cameraId, struct camera_info *info) {
104 Mutex::Autolock lock(mCameraInfoLock);
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800105 if (cameraId < 0) {
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800106 ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
107 return -EINVAL;
108 }
109
Yin-Chia Yehb6dc0bf2015-02-18 14:42:16 -0800110 // Only override static_camera_characteristics for API2 devices
111 int apiVersion = mModule->common.module_api_version;
112 if (apiVersion < CAMERA_MODULE_API_VERSION_2_0) {
113 return mModule->get_camera_info(cameraId, info);
114 }
115
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800116 ssize_t index = mCameraInfoMap.indexOfKey(cameraId);
117 if (index == NAME_NOT_FOUND) {
118 // Get camera info from raw module and cache it
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700119 camera_info rawInfo, cameraInfo;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800120 int ret = mModule->get_camera_info(cameraId, &rawInfo);
121 if (ret != 0) {
122 return ret;
123 }
Zhijun He9c5af612015-05-04 16:34:37 -0700124 int deviceVersion = rawInfo.device_version;
Yin-Chia Yeh7768ded2015-04-15 12:16:02 -0700125 if (deviceVersion < CAMERA_DEVICE_API_VERSION_2_0) {
126 // static_camera_characteristics is invalid
127 *info = rawInfo;
128 return ret;
129 }
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700130 CameraMetadata m;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800131 m = rawInfo.static_camera_characteristics;
Yin-Chia Yehb6dc0bf2015-02-18 14:42:16 -0800132 deriveCameraCharacteristicsKeys(rawInfo.device_version, m);
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700133 mCameraCharacteristicsMap.add(cameraId, m);
134 cameraInfo = rawInfo;
135 cameraInfo.static_camera_characteristics =
136 mCameraCharacteristicsMap.valueFor(cameraId).getAndLock();
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800137 mCameraInfoMap.add(cameraId, cameraInfo);
138 index = mCameraInfoMap.indexOfKey(cameraId);
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800139 }
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800140
141 assert(index != NAME_NOT_FOUND);
142 // return the cached camera info
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700143 *info = mCameraInfoMap[index];
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700144 return OK;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800145}
146
147int CameraModule::open(const char* id, struct hw_device_t** device) {
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800148 return filterOpenErrorCode(mModule->common.methods->open(&mModule->common, id, device));
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800149}
150
151int CameraModule::openLegacy(
152 const char* id, uint32_t halVersion, struct hw_device_t** device) {
153 return mModule->open_legacy(&mModule->common, id, halVersion, device);
154}
155
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800156int CameraModule::getNumberOfCameras() {
157 return mModule->get_number_of_cameras();
158}
159
160int CameraModule::setCallbacks(const camera_module_callbacks_t *callbacks) {
161 return mModule->set_callbacks(callbacks);
162}
163
164bool CameraModule::isVendorTagDefined() {
165 return mModule->get_vendor_tag_ops != NULL;
166}
167
168void CameraModule::getVendorTagOps(vendor_tag_ops_t* ops) {
169 if (mModule->get_vendor_tag_ops) {
170 mModule->get_vendor_tag_ops(ops);
171 }
172}
173
174int CameraModule::setTorchMode(const char* camera_id, bool enable) {
175 return mModule->set_torch_mode(camera_id, enable);
176}
177
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800178status_t CameraModule::filterOpenErrorCode(status_t err) {
179 switch(err) {
180 case NO_ERROR:
181 case -EBUSY:
182 case -EINVAL:
183 case -EUSERS:
184 return err;
185 default:
186 break;
187 }
188 return -ENODEV;
189}
190
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800191uint16_t CameraModule::getModuleApiVersion() {
192 return mModule->common.module_api_version;
193}
194
195const char* CameraModule::getModuleName() {
196 return mModule->common.name;
197}
198
199uint16_t CameraModule::getHalApiVersion() {
200 return mModule->common.hal_api_version;
201}
202
203const char* CameraModule::getModuleAuthor() {
204 return mModule->common.author;
205}
206
207void* CameraModule::getDso() {
208 return mModule->common.dso;
209}
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800210
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800211}; // namespace android