blob: d7a15684f013cf14b762606436584714c83bb9fa [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
31 // Keys added in HAL3.3
32 if (deviceVersion < CAMERA_DEVICE_API_VERSION_3_3) {
Yin-Chia Yeh3e28a1a2015-05-22 15:03:38 -070033 const size_t NUM_DERIVED_KEYS_HAL3_3 = 5;
Yin-Chia Yehe074a932015-01-30 10:29:02 -080034 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);
Yin-Chia Yehb28c3442015-05-07 14:43:19 -070081
Yin-Chia Yeh3e28a1a2015-05-22 15:03:38 -070082 entry = chars.find(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS);
83 // HAL3.2 devices passing existing CTS test should all support all LSC modes and LSC map
84 bool lensShadingModeSupported = false;
85 if (entry.count > 0) {
86 for (size_t i = 0; i < entry.count; i++) {
87 if (entry.data.i32[i] == ANDROID_SHADING_MODE) {
88 lensShadingModeSupported = true;
89 break;
90 }
91 }
92 }
93 Vector<uint8_t> lscModes;
94 Vector<uint8_t> lscMapModes;
95 lscModes.push(ANDROID_SHADING_MODE_FAST);
96 lscModes.push(ANDROID_SHADING_MODE_HIGH_QUALITY);
97 lscMapModes.push(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF);
98 if (lensShadingModeSupported) {
99 lscModes.push(ANDROID_SHADING_MODE_OFF);
100 lscMapModes.push(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_ON);
101 }
102 chars.update(ANDROID_SHADING_AVAILABLE_MODES, lscModes);
103 chars.update(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES, lscMapModes);
104
Yin-Chia Yehb28c3442015-05-07 14:43:19 -0700105 entry = chars.find(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
106 Vector<int32_t> availableCharsKeys;
107 availableCharsKeys.setCapacity(entry.count + NUM_DERIVED_KEYS_HAL3_3);
108 for (size_t i = 0; i < entry.count; i++) {
109 availableCharsKeys.push(entry.data.i32[i]);
110 }
111 availableCharsKeys.push(ANDROID_CONTROL_AE_LOCK_AVAILABLE);
Yin-Chia Yeh3e28a1a2015-05-22 15:03:38 -0700112 availableCharsKeys.push(ANDROID_CONTROL_AWB_LOCK_AVAILABLE);
Yin-Chia Yehb28c3442015-05-07 14:43:19 -0700113 availableCharsKeys.push(ANDROID_CONTROL_AVAILABLE_MODES);
Yin-Chia Yeh3e28a1a2015-05-22 15:03:38 -0700114 availableCharsKeys.push(ANDROID_SHADING_AVAILABLE_MODES);
115 availableCharsKeys.push(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES);
Yin-Chia Yehb28c3442015-05-07 14:43:19 -0700116 chars.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, availableCharsKeys);
Yin-Chia Yeh3e28a1a2015-05-22 15:03:38 -0700117
Zhijun He1fa89992015-06-01 15:44:31 -0700118 // Need update android.control.availableHighSpeedVideoConfigurations since HAL3.3
119 // adds batch size to this array.
120 entry = chars.find(ANDROID_CONTROL_AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS);
121 if (entry.count > 0) {
122 Vector<int32_t> highSpeedConfig;
123 for (size_t i = 0; i < entry.count; i += 4) {
124 highSpeedConfig.add(entry.data.i32[i]); // width
125 highSpeedConfig.add(entry.data.i32[i + 1]); // height
126 highSpeedConfig.add(entry.data.i32[i + 2]); // fps_min
127 highSpeedConfig.add(entry.data.i32[i + 3]); // fps_max
128 highSpeedConfig.add(1); // batchSize_max. default to 1 for HAL3.2
129 }
130 chars.update(ANDROID_CONTROL_AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS,
131 highSpeedConfig);
132 }
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800133 }
Ruben Brunka3b3caa2015-06-22 14:26:51 -0700134
135 // Always add a default for the pre-correction active array if the vendor chooses to omit this
136 camera_metadata_entry entry = chars.find(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE);
137 if (entry.count == 0) {
Ruben Brunkeff134a2015-07-17 15:22:01 -0700138 Vector<int32_t> preCorrectionArray;
Ruben Brunka3b3caa2015-06-22 14:26:51 -0700139 entry = chars.find(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE);
Ruben Brunkeff134a2015-07-17 15:22:01 -0700140 preCorrectionArray.appendArray(entry.data.i32, entry.count);
141 chars.update(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE, preCorrectionArray);
Ruben Brunka3b3caa2015-06-22 14:26:51 -0700142 }
143
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800144 return;
145}
146
147CameraModule::CameraModule(camera_module_t *module) {
148 if (module == NULL) {
149 ALOGE("%s: camera hardware module must not be null", __FUNCTION__);
150 assert(0);
151 }
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800152 mModule = module;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800153}
154
Chien-Yu Chen944f8432015-07-01 17:08:03 -0700155CameraModule::~CameraModule()
156{
157 while (mCameraInfoMap.size() > 0) {
158 camera_info cameraInfo = mCameraInfoMap.editValueAt(0);
159 if (cameraInfo.static_camera_characteristics != NULL) {
160 free_camera_metadata(
161 const_cast<camera_metadata_t*>(cameraInfo.static_camera_characteristics));
162 }
163 mCameraInfoMap.removeItemsAt(0);
164 }
165}
166
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700167int CameraModule::init() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700168 ATRACE_CALL();
169 int res = OK;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700170 if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 &&
171 mModule->init != NULL) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700172 ATRACE_BEGIN("camera_module->init");
173 res = mModule->init();
174 ATRACE_END();
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700175 }
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700176 mCameraInfoMap.setCapacity(getNumberOfCameras());
177 return res;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700178}
179
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800180int CameraModule::getCameraInfo(int cameraId, struct camera_info *info) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700181 ATRACE_CALL();
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800182 Mutex::Autolock lock(mCameraInfoLock);
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800183 if (cameraId < 0) {
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800184 ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
185 return -EINVAL;
186 }
187
Yin-Chia Yehb6dc0bf2015-02-18 14:42:16 -0800188 // Only override static_camera_characteristics for API2 devices
189 int apiVersion = mModule->common.module_api_version;
190 if (apiVersion < CAMERA_MODULE_API_VERSION_2_0) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700191 int ret;
192 ATRACE_BEGIN("camera_module->get_camera_info");
193 ret = mModule->get_camera_info(cameraId, info);
194 ATRACE_END();
195 return ret;
Yin-Chia Yehb6dc0bf2015-02-18 14:42:16 -0800196 }
197
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800198 ssize_t index = mCameraInfoMap.indexOfKey(cameraId);
199 if (index == NAME_NOT_FOUND) {
200 // Get camera info from raw module and cache it
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700201 camera_info rawInfo, cameraInfo;
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700202 ATRACE_BEGIN("camera_module->get_camera_info");
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800203 int ret = mModule->get_camera_info(cameraId, &rawInfo);
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700204 ATRACE_END();
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800205 if (ret != 0) {
206 return ret;
207 }
Zhijun He9c5af612015-05-04 16:34:37 -0700208 int deviceVersion = rawInfo.device_version;
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800209 if (deviceVersion < CAMERA_DEVICE_API_VERSION_3_0) {
Yin-Chia Yeh7768ded2015-04-15 12:16:02 -0700210 // static_camera_characteristics is invalid
211 *info = rawInfo;
212 return ret;
213 }
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700214 CameraMetadata m;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800215 m = rawInfo.static_camera_characteristics;
Yin-Chia Yehb6dc0bf2015-02-18 14:42:16 -0800216 deriveCameraCharacteristicsKeys(rawInfo.device_version, m);
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700217 cameraInfo = rawInfo;
Chien-Yu Chen944f8432015-07-01 17:08:03 -0700218 cameraInfo.static_camera_characteristics = m.release();
219 index = mCameraInfoMap.add(cameraId, cameraInfo);
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800220 }
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800221
222 assert(index != NAME_NOT_FOUND);
223 // return the cached camera info
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700224 *info = mCameraInfoMap[index];
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700225 return OK;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800226}
227
228int CameraModule::open(const char* id, struct hw_device_t** device) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700229 int res;
230 ATRACE_BEGIN("camera_module->open");
231 res = filterOpenErrorCode(mModule->common.methods->open(&mModule->common, id, device));
232 ATRACE_END();
233 return res;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800234}
235
236int CameraModule::openLegacy(
237 const char* id, uint32_t halVersion, struct hw_device_t** device) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700238 int res;
239 ATRACE_BEGIN("camera_module->open_legacy");
240 res = mModule->open_legacy(&mModule->common, id, halVersion, device);
241 ATRACE_END();
242 return res;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800243}
244
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800245int CameraModule::getNumberOfCameras() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700246 int numCameras;
247 ATRACE_BEGIN("camera_module->get_number_of_cameras");
248 numCameras = mModule->get_number_of_cameras();
249 ATRACE_END();
250 return numCameras;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800251}
252
253int CameraModule::setCallbacks(const camera_module_callbacks_t *callbacks) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700254 int res;
255 ATRACE_BEGIN("camera_module->set_callbacks");
256 res = mModule->set_callbacks(callbacks);
257 ATRACE_END();
258 return res;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800259}
260
261bool CameraModule::isVendorTagDefined() {
262 return mModule->get_vendor_tag_ops != NULL;
263}
264
265void CameraModule::getVendorTagOps(vendor_tag_ops_t* ops) {
266 if (mModule->get_vendor_tag_ops) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700267 ATRACE_BEGIN("camera_module->get_vendor_tag_ops");
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800268 mModule->get_vendor_tag_ops(ops);
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700269 ATRACE_END();
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800270 }
271}
272
273int CameraModule::setTorchMode(const char* camera_id, bool enable) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700274 int res;
275 ATRACE_BEGIN("camera_module->set_torch_mode");
276 res = mModule->set_torch_mode(camera_id, enable);
277 ATRACE_END();
278 return res;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800279}
280
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800281status_t CameraModule::filterOpenErrorCode(status_t err) {
282 switch(err) {
283 case NO_ERROR:
284 case -EBUSY:
285 case -EINVAL:
286 case -EUSERS:
287 return err;
288 default:
289 break;
290 }
291 return -ENODEV;
292}
293
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800294uint16_t CameraModule::getModuleApiVersion() {
295 return mModule->common.module_api_version;
296}
297
298const char* CameraModule::getModuleName() {
299 return mModule->common.name;
300}
301
302uint16_t CameraModule::getHalApiVersion() {
303 return mModule->common.hal_api_version;
304}
305
306const char* CameraModule::getModuleAuthor() {
307 return mModule->common.author;
308}
309
310void* CameraModule::getDso() {
311 return mModule->common.dso;
312}
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800313
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800314}; // namespace android