blob: 9c2b02ba0cf65a75ce0b04ebbcb04620d6533e9b [file] [log] [blame]
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -07001/*
2 * Copyright (C) 2016 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 "CamComm1.0-CamModule"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20
21#include <utils/Trace.h>
22
23#include "CameraModule.h"
24
25namespace android {
26namespace hardware {
27namespace camera {
28namespace common {
29namespace V1_0 {
30namespace helper {
31
32void CameraModule::deriveCameraCharacteristicsKeys(
33 uint32_t deviceVersion, CameraMetadata &chars) {
34 ATRACE_CALL();
35
36 Vector<int32_t> derivedCharKeys;
37 Vector<int32_t> derivedRequestKeys;
38 Vector<int32_t> derivedResultKeys;
39 // Keys added in HAL3.3
40 if (deviceVersion < CAMERA_DEVICE_API_VERSION_3_3) {
41 Vector<uint8_t> controlModes;
42 uint8_t data = ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE;
43 chars.update(ANDROID_CONTROL_AE_LOCK_AVAILABLE, &data, /*count*/1);
44 data = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_TRUE;
45 chars.update(ANDROID_CONTROL_AWB_LOCK_AVAILABLE, &data, /*count*/1);
46 controlModes.push(ANDROID_CONTROL_MODE_AUTO);
47 camera_metadata_entry entry = chars.find(ANDROID_CONTROL_AVAILABLE_SCENE_MODES);
48 if (entry.count > 1 || entry.data.u8[0] != ANDROID_CONTROL_SCENE_MODE_DISABLED) {
49 controlModes.push(ANDROID_CONTROL_MODE_USE_SCENE_MODE);
50 }
51
52 // Only advertise CONTROL_OFF mode if 3A manual controls are supported.
53 bool isManualAeSupported = false;
54 bool isManualAfSupported = false;
55 bool isManualAwbSupported = false;
56 entry = chars.find(ANDROID_CONTROL_AE_AVAILABLE_MODES);
57 if (entry.count > 0) {
58 for (size_t i = 0; i < entry.count; i++) {
59 if (entry.data.u8[i] == ANDROID_CONTROL_AE_MODE_OFF) {
60 isManualAeSupported = true;
61 break;
62 }
63 }
64 }
65 entry = chars.find(ANDROID_CONTROL_AF_AVAILABLE_MODES);
66 if (entry.count > 0) {
67 for (size_t i = 0; i < entry.count; i++) {
68 if (entry.data.u8[i] == ANDROID_CONTROL_AF_MODE_OFF) {
69 isManualAfSupported = true;
70 break;
71 }
72 }
73 }
74 entry = chars.find(ANDROID_CONTROL_AWB_AVAILABLE_MODES);
75 if (entry.count > 0) {
76 for (size_t i = 0; i < entry.count; i++) {
77 if (entry.data.u8[i] == ANDROID_CONTROL_AWB_MODE_OFF) {
78 isManualAwbSupported = true;
79 break;
80 }
81 }
82 }
83 if (isManualAeSupported && isManualAfSupported && isManualAwbSupported) {
84 controlModes.push(ANDROID_CONTROL_MODE_OFF);
85 }
86
87 chars.update(ANDROID_CONTROL_AVAILABLE_MODES, controlModes);
88
89 entry = chars.find(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS);
90 // HAL3.2 devices passing existing CTS test should all support all LSC modes and LSC map
91 bool lensShadingModeSupported = false;
92 if (entry.count > 0) {
93 for (size_t i = 0; i < entry.count; i++) {
94 if (entry.data.i32[i] == ANDROID_SHADING_MODE) {
95 lensShadingModeSupported = true;
96 break;
97 }
98 }
99 }
100 Vector<uint8_t> lscModes;
101 Vector<uint8_t> lscMapModes;
102 lscModes.push(ANDROID_SHADING_MODE_FAST);
103 lscModes.push(ANDROID_SHADING_MODE_HIGH_QUALITY);
104 lscMapModes.push(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF);
105 if (lensShadingModeSupported) {
106 lscModes.push(ANDROID_SHADING_MODE_OFF);
107 lscMapModes.push(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_ON);
108 }
109 chars.update(ANDROID_SHADING_AVAILABLE_MODES, lscModes);
110 chars.update(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES, lscMapModes);
111
112 derivedCharKeys.push(ANDROID_CONTROL_AE_LOCK_AVAILABLE);
113 derivedCharKeys.push(ANDROID_CONTROL_AWB_LOCK_AVAILABLE);
114 derivedCharKeys.push(ANDROID_CONTROL_AVAILABLE_MODES);
115 derivedCharKeys.push(ANDROID_SHADING_AVAILABLE_MODES);
116 derivedCharKeys.push(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES);
117
118 // 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 }
133 }
134
135 // Keys added in HAL3.4
136 if (deviceVersion < CAMERA_DEVICE_API_VERSION_3_4) {
137 // Check if HAL supports RAW_OPAQUE output
138 camera_metadata_entry entry = chars.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
139 bool supportRawOpaque = false;
140 bool supportAnyRaw = false;
141 const int STREAM_CONFIGURATION_SIZE = 4;
142 const int STREAM_FORMAT_OFFSET = 0;
143 const int STREAM_WIDTH_OFFSET = 1;
144 const int STREAM_HEIGHT_OFFSET = 2;
145 const int STREAM_IS_INPUT_OFFSET = 3;
146 Vector<int32_t> rawOpaqueSizes;
147
148 for (size_t i=0; i < entry.count; i += STREAM_CONFIGURATION_SIZE) {
149 int32_t format = entry.data.i32[i + STREAM_FORMAT_OFFSET];
150 int32_t width = entry.data.i32[i + STREAM_WIDTH_OFFSET];
151 int32_t height = entry.data.i32[i + STREAM_HEIGHT_OFFSET];
152 int32_t isInput = entry.data.i32[i + STREAM_IS_INPUT_OFFSET];
153 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT &&
154 format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
155 supportRawOpaque = true;
156 rawOpaqueSizes.push(width);
157 rawOpaqueSizes.push(height);
158 // 2 bytes per pixel. This rough estimation is only used when
159 // HAL does not fill in the opaque raw size
160 rawOpaqueSizes.push(width * height *2);
161 }
162 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT &&
163 (format == HAL_PIXEL_FORMAT_RAW16 ||
164 format == HAL_PIXEL_FORMAT_RAW10 ||
165 format == HAL_PIXEL_FORMAT_RAW12 ||
166 format == HAL_PIXEL_FORMAT_RAW_OPAQUE)) {
167 supportAnyRaw = true;
168 }
169 }
170
171 if (supportRawOpaque) {
172 entry = chars.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
173 if (entry.count == 0) {
174 // Fill in estimated value if HAL does not list it
175 chars.update(ANDROID_SENSOR_OPAQUE_RAW_SIZE, rawOpaqueSizes);
176 derivedCharKeys.push(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
177 }
178 }
179
180 // Check if HAL supports any RAW output, if so, fill in postRawSensitivityBoost range
181 if (supportAnyRaw) {
182 int32_t defaultRange[2] = {100, 100};
183 entry = chars.find(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE);
184 if (entry.count == 0) {
185 // Fill in default value (100, 100)
186 chars.update(
187 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE,
188 defaultRange, 2);
189 derivedCharKeys.push(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE);
190 // Actual request/results will be derived by camera device.
191 derivedRequestKeys.push(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST);
192 derivedResultKeys.push(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST);
193 }
194 }
195 }
196
197 // Always add a default for the pre-correction active array if the vendor chooses to omit this
198 camera_metadata_entry entry = chars.find(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE);
199 if (entry.count == 0) {
200 Vector<int32_t> preCorrectionArray;
201 entry = chars.find(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE);
202 preCorrectionArray.appendArray(entry.data.i32, entry.count);
203 chars.update(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE, preCorrectionArray);
204 derivedCharKeys.push(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE);
205 }
206
207 // Add those newly added keys to AVAILABLE_CHARACTERISTICS_KEYS
208 // This has to be done at this end of this function.
209 if (derivedCharKeys.size() > 0) {
210 appendAvailableKeys(
211 chars, ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, derivedCharKeys);
212 }
213 if (derivedRequestKeys.size() > 0) {
214 appendAvailableKeys(
215 chars, ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS, derivedRequestKeys);
216 }
217 if (derivedResultKeys.size() > 0) {
218 appendAvailableKeys(
219 chars, ANDROID_REQUEST_AVAILABLE_RESULT_KEYS, derivedResultKeys);
220 }
221 return;
222}
223
224void CameraModule::appendAvailableKeys(CameraMetadata &chars,
225 int32_t keyTag, const Vector<int32_t>& appendKeys) {
226 camera_metadata_entry entry = chars.find(keyTag);
227 Vector<int32_t> availableKeys;
228 availableKeys.setCapacity(entry.count + appendKeys.size());
229 for (size_t i = 0; i < entry.count; i++) {
230 availableKeys.push(entry.data.i32[i]);
231 }
232 for (size_t i = 0; i < appendKeys.size(); i++) {
233 availableKeys.push(appendKeys[i]);
234 }
235 chars.update(keyTag, availableKeys);
236}
237
Shuzhen Wang6bdeaf52018-09-05 09:40:00 -0700238CameraModule::CameraModule(camera_module_t *module) : mNumberOfCameras(0) {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700239 if (module == NULL) {
240 ALOGE("%s: camera hardware module must not be null", __FUNCTION__);
241 assert(0);
242 }
243 mModule = module;
244}
245
246CameraModule::~CameraModule()
247{
248 while (mCameraInfoMap.size() > 0) {
249 camera_info cameraInfo = mCameraInfoMap.editValueAt(0);
250 if (cameraInfo.static_camera_characteristics != NULL) {
251 free_camera_metadata(
252 const_cast<camera_metadata_t*>(cameraInfo.static_camera_characteristics));
253 }
254 mCameraInfoMap.removeItemsAt(0);
255 }
256}
257
258int CameraModule::init() {
259 ATRACE_CALL();
260 int res = OK;
261 if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 &&
262 mModule->init != NULL) {
263 ATRACE_BEGIN("camera_module->init");
264 res = mModule->init();
265 ATRACE_END();
266 }
Shuzhen Wang6bdeaf52018-09-05 09:40:00 -0700267 mNumberOfCameras = getNumberOfCameras();
268 mCameraInfoMap.setCapacity(mNumberOfCameras);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700269 return res;
270}
271
272int CameraModule::getCameraInfo(int cameraId, struct camera_info *info) {
273 ATRACE_CALL();
274 Mutex::Autolock lock(mCameraInfoLock);
275 if (cameraId < 0) {
276 ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
277 return -EINVAL;
278 }
279
280 // Only override static_camera_characteristics for API2 devices
281 int apiVersion = mModule->common.module_api_version;
282 if (apiVersion < CAMERA_MODULE_API_VERSION_2_0) {
283 int ret;
284 ATRACE_BEGIN("camera_module->get_camera_info");
285 ret = mModule->get_camera_info(cameraId, info);
286 // Fill in this so CameraService won't be confused by
287 // possibly 0 device_version
288 info->device_version = CAMERA_DEVICE_API_VERSION_1_0;
289 ATRACE_END();
290 return ret;
291 }
292
293 ssize_t index = mCameraInfoMap.indexOfKey(cameraId);
294 if (index == NAME_NOT_FOUND) {
295 // Get camera info from raw module and cache it
296 camera_info rawInfo, cameraInfo;
297 ATRACE_BEGIN("camera_module->get_camera_info");
298 int ret = mModule->get_camera_info(cameraId, &rawInfo);
299 ATRACE_END();
300 if (ret != 0) {
301 return ret;
302 }
303 int deviceVersion = rawInfo.device_version;
304 if (deviceVersion < CAMERA_DEVICE_API_VERSION_3_0) {
305 // static_camera_characteristics is invalid
306 *info = rawInfo;
307 return ret;
308 }
309 CameraMetadata m;
Yin-Chia Yeh090872a2018-05-17 15:53:30 -0700310 m.append(rawInfo.static_camera_characteristics);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700311 deriveCameraCharacteristicsKeys(rawInfo.device_version, m);
312 cameraInfo = rawInfo;
313 cameraInfo.static_camera_characteristics = m.release();
314 index = mCameraInfoMap.add(cameraId, cameraInfo);
315 }
316
317 assert(index != NAME_NOT_FOUND);
318 // return the cached camera info
319 *info = mCameraInfoMap[index];
320 return OK;
321}
322
Shuzhen Wangd3feb3d2018-08-17 13:52:40 -0700323int CameraModule::getPhysicalCameraInfo(int physicalCameraId, camera_metadata_t **physicalInfo) {
324 ATRACE_CALL();
325 Mutex::Autolock lock(mCameraInfoLock);
Shuzhen Wang6bdeaf52018-09-05 09:40:00 -0700326 if (physicalCameraId < mNumberOfCameras) {
Shuzhen Wangd3feb3d2018-08-17 13:52:40 -0700327 ALOGE("%s: Invalid physical camera ID %d", __FUNCTION__, physicalCameraId);
328 return -EINVAL;
329 }
330
331 // Only query physical camera info for 2.5 version for newer
332 int apiVersion = mModule->common.module_api_version;
333 if (apiVersion < CAMERA_MODULE_API_VERSION_2_5) {
334 ALOGE("%s: Module version must be at least 2.5 to handle getPhysicalCameraInfo",
335 __FUNCTION__);
336 return -ENODEV;
337 }
Shuzhen Wang6bdeaf52018-09-05 09:40:00 -0700338 if (mModule->get_physical_camera_info == nullptr) {
339 ALOGE("%s: get_physical_camera is NULL for module version 2.5", __FUNCTION__);
340 return -EINVAL;
341 }
Shuzhen Wangd3feb3d2018-08-17 13:52:40 -0700342
343 ssize_t index = mPhysicalCameraInfoMap.indexOfKey(physicalCameraId);
344 if (index == NAME_NOT_FOUND) {
345 // Get physical camera characteristics, and cache it
346 camera_metadata_t *info = nullptr;
347 ATRACE_BEGIN("camera_module->get_physical_camera_info");
348 int ret = mModule->get_physical_camera_info(physicalCameraId, &info);
349 ATRACE_END();
350 if (ret != 0) {
351 return ret;
352 }
353
354 index = mPhysicalCameraInfoMap.add(physicalCameraId, info);
355 }
356
357 assert(index != NAME_NOT_FOUND);
358 *physicalInfo = mPhysicalCameraInfoMap[index];
359 return OK;
360}
361
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700362int CameraModule::getDeviceVersion(int cameraId) {
363 ssize_t index = mDeviceVersionMap.indexOfKey(cameraId);
364 if (index == NAME_NOT_FOUND) {
365 int deviceVersion;
366 if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
367 struct camera_info info;
368 getCameraInfo(cameraId, &info);
369 deviceVersion = info.device_version;
370 } else {
371 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
372 }
373 index = mDeviceVersionMap.add(cameraId, deviceVersion);
374 }
375 assert(index != NAME_NOT_FOUND);
376 return mDeviceVersionMap[index];
377}
378
379int CameraModule::open(const char* id, struct hw_device_t** device) {
380 int res;
381 ATRACE_BEGIN("camera_module->open");
382 res = filterOpenErrorCode(mModule->common.methods->open(&mModule->common, id, device));
383 ATRACE_END();
384 return res;
385}
386
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800387bool CameraModule::isOpenLegacyDefined() const {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700388 if (getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_3) {
389 return false;
390 }
391 return mModule->open_legacy != NULL;
392}
393
394int CameraModule::openLegacy(
395 const char* id, uint32_t halVersion, struct hw_device_t** device) {
396 int res;
397 ATRACE_BEGIN("camera_module->open_legacy");
398 res = mModule->open_legacy(&mModule->common, id, halVersion, device);
399 ATRACE_END();
400 return res;
401}
402
403int CameraModule::getNumberOfCameras() {
404 int numCameras;
405 ATRACE_BEGIN("camera_module->get_number_of_cameras");
406 numCameras = mModule->get_number_of_cameras();
407 ATRACE_END();
408 return numCameras;
409}
410
411int CameraModule::setCallbacks(const camera_module_callbacks_t *callbacks) {
Yin-Chia Yehfca2e742017-01-31 16:00:20 -0800412 int res = OK;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700413 ATRACE_BEGIN("camera_module->set_callbacks");
Yin-Chia Yehfca2e742017-01-31 16:00:20 -0800414 if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_1) {
415 res = mModule->set_callbacks(callbacks);
416 }
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700417 ATRACE_END();
418 return res;
419}
420
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800421bool CameraModule::isVendorTagDefined() const {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700422 return mModule->get_vendor_tag_ops != NULL;
423}
424
425void CameraModule::getVendorTagOps(vendor_tag_ops_t* ops) {
426 if (mModule->get_vendor_tag_ops) {
427 ATRACE_BEGIN("camera_module->get_vendor_tag_ops");
428 mModule->get_vendor_tag_ops(ops);
429 ATRACE_END();
430 }
431}
432
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800433bool CameraModule::isSetTorchModeSupported() const {
434 if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4) {
435 if (mModule->set_torch_mode == NULL) {
436 ALOGE("%s: Module 2.4 device must support set torch API!",
437 __FUNCTION__);
438 return false;
439 }
440 return true;
441 }
442 return false;
443}
444
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700445int CameraModule::setTorchMode(const char* camera_id, bool enable) {
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800446 int res = INVALID_OPERATION;
447 if (mModule->set_torch_mode != NULL) {
448 ATRACE_BEGIN("camera_module->set_torch_mode");
449 res = mModule->set_torch_mode(camera_id, enable);
450 ATRACE_END();
451 }
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700452 return res;
453}
454
455status_t CameraModule::filterOpenErrorCode(status_t err) {
456 switch(err) {
457 case NO_ERROR:
458 case -EBUSY:
459 case -EINVAL:
460 case -EUSERS:
461 return err;
462 default:
463 break;
464 }
465 return -ENODEV;
466}
467
Guennadi Liakhovetskieca1d452017-12-07 10:59:35 +0100468void CameraModule::removeCamera(int cameraId) {
Shik Chend4b8d212018-10-25 18:00:27 +0800469 free_camera_metadata(const_cast<camera_metadata_t*>(
470 mCameraInfoMap.valueFor(cameraId).static_camera_characteristics));
Guennadi Liakhovetskieca1d452017-12-07 10:59:35 +0100471 mCameraInfoMap.removeItem(cameraId);
472 mDeviceVersionMap.removeItem(cameraId);
473}
474
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800475uint16_t CameraModule::getModuleApiVersion() const {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700476 return mModule->common.module_api_version;
477}
478
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800479const char* CameraModule::getModuleName() const {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700480 return mModule->common.name;
481}
482
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800483uint16_t CameraModule::getHalApiVersion() const {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700484 return mModule->common.hal_api_version;
485}
486
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800487const char* CameraModule::getModuleAuthor() const {
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700488 return mModule->common.author;
489}
490
491void* CameraModule::getDso() {
492 return mModule->common.dso;
493}
494
495} // namespace helper
496} // namespace V1_0
497} // namespace common
498} // namespace camera
499} // namespace hardware
500} // namespace android