blob: 16b8abae491bcbe5d178b93664e06ab50c4d5a07 [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"
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -070018#define ATRACE_TAG ATRACE_TAG_CAMERA
Yin-Chia Yehe074a932015-01-30 10:29:02 -080019//#define LOG_NDEBUG 0
20
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -070021#include <utils/Trace.h>
22
Yin-Chia Yehe074a932015-01-30 10:29:02 -080023#include "CameraModule.h"
24
25namespace android {
26
27void CameraModule::deriveCameraCharacteristicsKeys(
28 uint32_t deviceVersion, CameraMetadata &chars) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -070029 ATRACE_CALL();
Yin-Chia Yehe074a932015-01-30 10:29:02 -080030 // HAL1 devices should not reach here
31 if (deviceVersion < CAMERA_DEVICE_API_VERSION_2_0) {
32 ALOGV("%s: Cannot derive keys for HAL version < 2.0");
33 return;
34 }
35
36 // Keys added in HAL3.3
37 if (deviceVersion < CAMERA_DEVICE_API_VERSION_3_3) {
Yin-Chia Yeh3e28a1a2015-05-22 15:03:38 -070038 const size_t NUM_DERIVED_KEYS_HAL3_3 = 5;
Yin-Chia Yehe074a932015-01-30 10:29:02 -080039 Vector<uint8_t> controlModes;
40 uint8_t data = ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE;
41 chars.update(ANDROID_CONTROL_AE_LOCK_AVAILABLE, &data, /*count*/1);
42 data = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_TRUE;
43 chars.update(ANDROID_CONTROL_AWB_LOCK_AVAILABLE, &data, /*count*/1);
Yin-Chia Yehe074a932015-01-30 10:29:02 -080044 controlModes.push(ANDROID_CONTROL_MODE_AUTO);
45 camera_metadata_entry entry = chars.find(ANDROID_CONTROL_AVAILABLE_SCENE_MODES);
46 if (entry.count > 1 || entry.data.u8[0] != ANDROID_CONTROL_SCENE_MODE_DISABLED) {
47 controlModes.push(ANDROID_CONTROL_MODE_USE_SCENE_MODE);
48 }
Zhijun He9c5af612015-05-04 16:34:37 -070049
50 // Only advertise CONTROL_OFF mode if 3A manual controls are supported.
51 bool isManualAeSupported = false;
52 bool isManualAfSupported = false;
53 bool isManualAwbSupported = false;
54 entry = chars.find(ANDROID_CONTROL_AE_AVAILABLE_MODES);
55 if (entry.count > 0) {
56 for (size_t i = 0; i < entry.count; i++) {
57 if (entry.data.u8[i] == ANDROID_CONTROL_AE_MODE_OFF) {
58 isManualAeSupported = true;
59 break;
60 }
61 }
62 }
63 entry = chars.find(ANDROID_CONTROL_AF_AVAILABLE_MODES);
64 if (entry.count > 0) {
65 for (size_t i = 0; i < entry.count; i++) {
66 if (entry.data.u8[i] == ANDROID_CONTROL_AF_MODE_OFF) {
67 isManualAfSupported = true;
68 break;
69 }
70 }
71 }
72 entry = chars.find(ANDROID_CONTROL_AWB_AVAILABLE_MODES);
73 if (entry.count > 0) {
74 for (size_t i = 0; i < entry.count; i++) {
75 if (entry.data.u8[i] == ANDROID_CONTROL_AWB_MODE_OFF) {
76 isManualAwbSupported = true;
77 break;
78 }
79 }
80 }
81 if (isManualAeSupported && isManualAfSupported && isManualAwbSupported) {
82 controlModes.push(ANDROID_CONTROL_MODE_OFF);
83 }
84
Yin-Chia Yehe074a932015-01-30 10:29:02 -080085 chars.update(ANDROID_CONTROL_AVAILABLE_MODES, controlModes);
Yin-Chia Yehb28c3442015-05-07 14:43:19 -070086
Yin-Chia Yeh3e28a1a2015-05-22 15:03:38 -070087 entry = chars.find(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS);
88 // HAL3.2 devices passing existing CTS test should all support all LSC modes and LSC map
89 bool lensShadingModeSupported = false;
90 if (entry.count > 0) {
91 for (size_t i = 0; i < entry.count; i++) {
92 if (entry.data.i32[i] == ANDROID_SHADING_MODE) {
93 lensShadingModeSupported = true;
94 break;
95 }
96 }
97 }
98 Vector<uint8_t> lscModes;
99 Vector<uint8_t> lscMapModes;
100 lscModes.push(ANDROID_SHADING_MODE_FAST);
101 lscModes.push(ANDROID_SHADING_MODE_HIGH_QUALITY);
102 lscMapModes.push(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF);
103 if (lensShadingModeSupported) {
104 lscModes.push(ANDROID_SHADING_MODE_OFF);
105 lscMapModes.push(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_ON);
106 }
107 chars.update(ANDROID_SHADING_AVAILABLE_MODES, lscModes);
108 chars.update(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES, lscMapModes);
109
Yin-Chia Yehb28c3442015-05-07 14:43:19 -0700110 entry = chars.find(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
111 Vector<int32_t> availableCharsKeys;
112 availableCharsKeys.setCapacity(entry.count + NUM_DERIVED_KEYS_HAL3_3);
113 for (size_t i = 0; i < entry.count; i++) {
114 availableCharsKeys.push(entry.data.i32[i]);
115 }
116 availableCharsKeys.push(ANDROID_CONTROL_AE_LOCK_AVAILABLE);
Yin-Chia Yeh3e28a1a2015-05-22 15:03:38 -0700117 availableCharsKeys.push(ANDROID_CONTROL_AWB_LOCK_AVAILABLE);
Yin-Chia Yehb28c3442015-05-07 14:43:19 -0700118 availableCharsKeys.push(ANDROID_CONTROL_AVAILABLE_MODES);
Yin-Chia Yeh3e28a1a2015-05-22 15:03:38 -0700119 availableCharsKeys.push(ANDROID_SHADING_AVAILABLE_MODES);
120 availableCharsKeys.push(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES);
Yin-Chia Yehb28c3442015-05-07 14:43:19 -0700121 chars.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, availableCharsKeys);
Yin-Chia Yeh3e28a1a2015-05-22 15:03:38 -0700122
Zhijun He1fa89992015-06-01 15:44:31 -0700123 // Need update android.control.availableHighSpeedVideoConfigurations since HAL3.3
124 // adds batch size to this array.
125 entry = chars.find(ANDROID_CONTROL_AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS);
126 if (entry.count > 0) {
127 Vector<int32_t> highSpeedConfig;
128 for (size_t i = 0; i < entry.count; i += 4) {
129 highSpeedConfig.add(entry.data.i32[i]); // width
130 highSpeedConfig.add(entry.data.i32[i + 1]); // height
131 highSpeedConfig.add(entry.data.i32[i + 2]); // fps_min
132 highSpeedConfig.add(entry.data.i32[i + 3]); // fps_max
133 highSpeedConfig.add(1); // batchSize_max. default to 1 for HAL3.2
134 }
135 chars.update(ANDROID_CONTROL_AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS,
136 highSpeedConfig);
137 }
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800138 }
Ruben Brunka3b3caa2015-06-22 14:26:51 -0700139
140 // Always add a default for the pre-correction active array if the vendor chooses to omit this
141 camera_metadata_entry entry = chars.find(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE);
142 if (entry.count == 0) {
Ruben Brunkeff134a2015-07-17 15:22:01 -0700143 Vector<int32_t> preCorrectionArray;
Ruben Brunka3b3caa2015-06-22 14:26:51 -0700144 entry = chars.find(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE);
Ruben Brunkeff134a2015-07-17 15:22:01 -0700145 preCorrectionArray.appendArray(entry.data.i32, entry.count);
146 chars.update(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE, preCorrectionArray);
Ruben Brunka3b3caa2015-06-22 14:26:51 -0700147 }
148
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800149 return;
150}
151
152CameraModule::CameraModule(camera_module_t *module) {
153 if (module == NULL) {
154 ALOGE("%s: camera hardware module must not be null", __FUNCTION__);
155 assert(0);
156 }
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800157 mModule = module;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800158}
159
Chien-Yu Chen944f8432015-07-01 17:08:03 -0700160CameraModule::~CameraModule()
161{
162 while (mCameraInfoMap.size() > 0) {
163 camera_info cameraInfo = mCameraInfoMap.editValueAt(0);
164 if (cameraInfo.static_camera_characteristics != NULL) {
165 free_camera_metadata(
166 const_cast<camera_metadata_t*>(cameraInfo.static_camera_characteristics));
167 }
168 mCameraInfoMap.removeItemsAt(0);
169 }
170}
171
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700172int CameraModule::init() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700173 ATRACE_CALL();
174 int res = OK;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700175 if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 &&
176 mModule->init != NULL) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700177 ATRACE_BEGIN("camera_module->init");
178 res = mModule->init();
179 ATRACE_END();
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700180 }
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700181 mCameraInfoMap.setCapacity(getNumberOfCameras());
182 return res;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700183}
184
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800185int CameraModule::getCameraInfo(int cameraId, struct camera_info *info) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700186 ATRACE_CALL();
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800187 Mutex::Autolock lock(mCameraInfoLock);
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800188 if (cameraId < 0) {
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800189 ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
190 return -EINVAL;
191 }
192
Yin-Chia Yehb6dc0bf2015-02-18 14:42:16 -0800193 // Only override static_camera_characteristics for API2 devices
194 int apiVersion = mModule->common.module_api_version;
195 if (apiVersion < CAMERA_MODULE_API_VERSION_2_0) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700196 int ret;
197 ATRACE_BEGIN("camera_module->get_camera_info");
198 ret = mModule->get_camera_info(cameraId, info);
199 ATRACE_END();
200 return ret;
Yin-Chia Yehb6dc0bf2015-02-18 14:42:16 -0800201 }
202
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800203 ssize_t index = mCameraInfoMap.indexOfKey(cameraId);
204 if (index == NAME_NOT_FOUND) {
205 // Get camera info from raw module and cache it
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700206 camera_info rawInfo, cameraInfo;
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700207 ATRACE_BEGIN("camera_module->get_camera_info");
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800208 int ret = mModule->get_camera_info(cameraId, &rawInfo);
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700209 ATRACE_END();
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800210 if (ret != 0) {
211 return ret;
212 }
Zhijun He9c5af612015-05-04 16:34:37 -0700213 int deviceVersion = rawInfo.device_version;
Yin-Chia Yeh7768ded2015-04-15 12:16:02 -0700214 if (deviceVersion < CAMERA_DEVICE_API_VERSION_2_0) {
215 // static_camera_characteristics is invalid
216 *info = rawInfo;
217 return ret;
218 }
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700219 CameraMetadata m;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800220 m = rawInfo.static_camera_characteristics;
Yin-Chia Yehb6dc0bf2015-02-18 14:42:16 -0800221 deriveCameraCharacteristicsKeys(rawInfo.device_version, m);
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700222 cameraInfo = rawInfo;
Chien-Yu Chen944f8432015-07-01 17:08:03 -0700223 cameraInfo.static_camera_characteristics = m.release();
224 index = mCameraInfoMap.add(cameraId, cameraInfo);
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800225 }
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800226
227 assert(index != NAME_NOT_FOUND);
228 // return the cached camera info
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700229 *info = mCameraInfoMap[index];
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700230 return OK;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800231}
232
233int CameraModule::open(const char* id, struct hw_device_t** device) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700234 int res;
235 ATRACE_BEGIN("camera_module->open");
236 res = filterOpenErrorCode(mModule->common.methods->open(&mModule->common, id, device));
237 ATRACE_END();
238 return res;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800239}
240
241int CameraModule::openLegacy(
242 const char* id, uint32_t halVersion, struct hw_device_t** device) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700243 int res;
244 ATRACE_BEGIN("camera_module->open_legacy");
245 res = mModule->open_legacy(&mModule->common, id, halVersion, device);
246 ATRACE_END();
247 return res;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800248}
249
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800250int CameraModule::getNumberOfCameras() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700251 int numCameras;
252 ATRACE_BEGIN("camera_module->get_number_of_cameras");
253 numCameras = mModule->get_number_of_cameras();
254 ATRACE_END();
255 return numCameras;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800256}
257
258int CameraModule::setCallbacks(const camera_module_callbacks_t *callbacks) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700259 int res;
260 ATRACE_BEGIN("camera_module->set_callbacks");
261 res = mModule->set_callbacks(callbacks);
262 ATRACE_END();
263 return res;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800264}
265
266bool CameraModule::isVendorTagDefined() {
267 return mModule->get_vendor_tag_ops != NULL;
268}
269
270void CameraModule::getVendorTagOps(vendor_tag_ops_t* ops) {
271 if (mModule->get_vendor_tag_ops) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700272 ATRACE_BEGIN("camera_module->get_vendor_tag_ops");
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800273 mModule->get_vendor_tag_ops(ops);
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700274 ATRACE_END();
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800275 }
276}
277
278int CameraModule::setTorchMode(const char* camera_id, bool enable) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700279 int res;
280 ATRACE_BEGIN("camera_module->set_torch_mode");
281 res = mModule->set_torch_mode(camera_id, enable);
282 ATRACE_END();
283 return res;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800284}
285
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800286status_t CameraModule::filterOpenErrorCode(status_t err) {
287 switch(err) {
288 case NO_ERROR:
289 case -EBUSY:
290 case -EINVAL:
291 case -EUSERS:
292 return err;
293 default:
294 break;
295 }
296 return -ENODEV;
297}
298
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800299uint16_t CameraModule::getModuleApiVersion() {
300 return mModule->common.module_api_version;
301}
302
303const char* CameraModule::getModuleName() {
304 return mModule->common.name;
305}
306
307uint16_t CameraModule::getHalApiVersion() {
308 return mModule->common.hal_api_version;
309}
310
311const char* CameraModule::getModuleAuthor() {
312 return mModule->common.author;
313}
314
315void* CameraModule::getDso() {
316 return mModule->common.dso;
317}
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800318
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800319}; // namespace android