blob: f33d1badbd5cce3ff4d07c9ab13071edbbbcb524 [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
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -080031 Vector<int32_t> derivedCharKeys;
Yin-Chia Yehe074a932015-01-30 10:29:02 -080032 // 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);
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 Yehe9154ce2015-12-07 14:38:04 -0800105 derivedCharKeys.push(ANDROID_CONTROL_AE_LOCK_AVAILABLE);
106 derivedCharKeys.push(ANDROID_CONTROL_AWB_LOCK_AVAILABLE);
107 derivedCharKeys.push(ANDROID_CONTROL_AVAILABLE_MODES);
108 derivedCharKeys.push(ANDROID_SHADING_AVAILABLE_MODES);
109 derivedCharKeys.push(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES);
Yin-Chia Yeh3e28a1a2015-05-22 15:03:38 -0700110
Zhijun He1fa89992015-06-01 15:44:31 -0700111 // Need update android.control.availableHighSpeedVideoConfigurations since HAL3.3
112 // adds batch size to this array.
113 entry = chars.find(ANDROID_CONTROL_AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS);
114 if (entry.count > 0) {
115 Vector<int32_t> highSpeedConfig;
116 for (size_t i = 0; i < entry.count; i += 4) {
117 highSpeedConfig.add(entry.data.i32[i]); // width
118 highSpeedConfig.add(entry.data.i32[i + 1]); // height
119 highSpeedConfig.add(entry.data.i32[i + 2]); // fps_min
120 highSpeedConfig.add(entry.data.i32[i + 3]); // fps_max
121 highSpeedConfig.add(1); // batchSize_max. default to 1 for HAL3.2
122 }
123 chars.update(ANDROID_CONTROL_AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS,
124 highSpeedConfig);
125 }
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800126 }
Ruben Brunka3b3caa2015-06-22 14:26:51 -0700127
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800128 // Keys added in HAL3.4
129 if (deviceVersion < CAMERA_DEVICE_API_VERSION_3_4) {
130 // Check if HAL supports RAW_OPAQUE output
131 camera_metadata_entry entry = chars.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
132 bool supportRawOpaque = false;
Yin-Chia Yehf1d124e2016-01-28 16:32:15 -0800133 bool supportAnyRaw = false;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800134 const int STREAM_CONFIGURATION_SIZE = 4;
135 const int STREAM_FORMAT_OFFSET = 0;
136 const int STREAM_WIDTH_OFFSET = 1;
137 const int STREAM_HEIGHT_OFFSET = 2;
138 const int STREAM_IS_INPUT_OFFSET = 3;
139 Vector<int32_t> rawOpaqueSizes;
140
141 for (size_t i=0; i < entry.count; i += STREAM_CONFIGURATION_SIZE) {
142 int32_t format = entry.data.i32[i + STREAM_FORMAT_OFFSET];
143 int32_t width = entry.data.i32[i + STREAM_WIDTH_OFFSET];
144 int32_t height = entry.data.i32[i + STREAM_HEIGHT_OFFSET];
145 int32_t isInput = entry.data.i32[i + STREAM_IS_INPUT_OFFSET];
146 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT &&
147 format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
148 supportRawOpaque = true;
149 rawOpaqueSizes.push(width);
150 rawOpaqueSizes.push(height);
151 // 2 bytes per pixel. This rough estimation is only used when
152 // HAL does not fill in the opaque raw size
153 rawOpaqueSizes.push(width * height *2);
154 }
Yin-Chia Yehf1d124e2016-01-28 16:32:15 -0800155 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT &&
156 (format == HAL_PIXEL_FORMAT_RAW16 ||
157 format == HAL_PIXEL_FORMAT_RAW10 ||
158 format == HAL_PIXEL_FORMAT_RAW12 ||
159 format == HAL_PIXEL_FORMAT_RAW_OPAQUE)) {
160 supportAnyRaw = true;
161 }
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800162 }
163
164 if (supportRawOpaque) {
165 entry = chars.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
166 if (entry.count == 0) {
167 // Fill in estimated value if HAL does not list it
168 chars.update(ANDROID_SENSOR_OPAQUE_RAW_SIZE, rawOpaqueSizes);
169 derivedCharKeys.push(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
170 }
171 }
Yin-Chia Yehf1d124e2016-01-28 16:32:15 -0800172
173 // Check if HAL supports any RAW output, if so, fill in postRawSensitivityBoost range
174 if (supportAnyRaw) {
175 int32_t defaultRange[2] = {100, 100};
176 entry = chars.find(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE);
177 if (entry.count == 0) {
178 // Fill in default value (100, 100)
179 chars.update(
180 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE,
181 defaultRange, 2);
182 derivedCharKeys.push(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE);
183 }
184 }
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800185 }
186
Ruben Brunka3b3caa2015-06-22 14:26:51 -0700187 // Always add a default for the pre-correction active array if the vendor chooses to omit this
188 camera_metadata_entry entry = chars.find(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE);
189 if (entry.count == 0) {
Ruben Brunkeff134a2015-07-17 15:22:01 -0700190 Vector<int32_t> preCorrectionArray;
Ruben Brunka3b3caa2015-06-22 14:26:51 -0700191 entry = chars.find(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE);
Ruben Brunkeff134a2015-07-17 15:22:01 -0700192 preCorrectionArray.appendArray(entry.data.i32, entry.count);
193 chars.update(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE, preCorrectionArray);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800194 derivedCharKeys.push(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE);
Ruben Brunka3b3caa2015-06-22 14:26:51 -0700195 }
196
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800197 // Add those newly added keys to AVAILABLE_CHARACTERISTICS_KEYS
198 // This has to be done at this end of this function.
199 entry = chars.find(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
200 Vector<int32_t> availableCharsKeys;
201 availableCharsKeys.setCapacity(entry.count + derivedCharKeys.size());
202 for (size_t i = 0; i < entry.count; i++) {
203 availableCharsKeys.push(entry.data.i32[i]);
204 }
205 for (size_t i = 0; i < derivedCharKeys.size(); i++) {
206 availableCharsKeys.push(derivedCharKeys[i]);
207 }
208 chars.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, availableCharsKeys);
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800209 return;
210}
211
212CameraModule::CameraModule(camera_module_t *module) {
213 if (module == NULL) {
214 ALOGE("%s: camera hardware module must not be null", __FUNCTION__);
215 assert(0);
216 }
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800217 mModule = module;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800218}
219
Chien-Yu Chen944f8432015-07-01 17:08:03 -0700220CameraModule::~CameraModule()
221{
222 while (mCameraInfoMap.size() > 0) {
223 camera_info cameraInfo = mCameraInfoMap.editValueAt(0);
224 if (cameraInfo.static_camera_characteristics != NULL) {
225 free_camera_metadata(
226 const_cast<camera_metadata_t*>(cameraInfo.static_camera_characteristics));
227 }
228 mCameraInfoMap.removeItemsAt(0);
229 }
230}
231
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700232int CameraModule::init() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700233 ATRACE_CALL();
234 int res = OK;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700235 if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 &&
236 mModule->init != NULL) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700237 ATRACE_BEGIN("camera_module->init");
238 res = mModule->init();
239 ATRACE_END();
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700240 }
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700241 mCameraInfoMap.setCapacity(getNumberOfCameras());
242 return res;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700243}
244
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800245int CameraModule::getCameraInfo(int cameraId, struct camera_info *info) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700246 ATRACE_CALL();
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800247 Mutex::Autolock lock(mCameraInfoLock);
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800248 if (cameraId < 0) {
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800249 ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
250 return -EINVAL;
251 }
252
Yin-Chia Yehb6dc0bf2015-02-18 14:42:16 -0800253 // Only override static_camera_characteristics for API2 devices
254 int apiVersion = mModule->common.module_api_version;
255 if (apiVersion < CAMERA_MODULE_API_VERSION_2_0) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700256 int ret;
257 ATRACE_BEGIN("camera_module->get_camera_info");
258 ret = mModule->get_camera_info(cameraId, info);
Yin-Chia Yeh654b4bf2015-12-08 12:19:31 -0800259 // Fill in this so CameraService won't be confused by
260 // possibly 0 device_version
261 info->device_version = CAMERA_DEVICE_API_VERSION_1_0;
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700262 ATRACE_END();
263 return ret;
Yin-Chia Yehb6dc0bf2015-02-18 14:42:16 -0800264 }
265
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800266 ssize_t index = mCameraInfoMap.indexOfKey(cameraId);
267 if (index == NAME_NOT_FOUND) {
268 // Get camera info from raw module and cache it
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700269 camera_info rawInfo, cameraInfo;
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700270 ATRACE_BEGIN("camera_module->get_camera_info");
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800271 int ret = mModule->get_camera_info(cameraId, &rawInfo);
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700272 ATRACE_END();
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800273 if (ret != 0) {
274 return ret;
275 }
Zhijun He9c5af612015-05-04 16:34:37 -0700276 int deviceVersion = rawInfo.device_version;
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800277 if (deviceVersion < CAMERA_DEVICE_API_VERSION_3_0) {
Yin-Chia Yeh7768ded2015-04-15 12:16:02 -0700278 // static_camera_characteristics is invalid
279 *info = rawInfo;
280 return ret;
281 }
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700282 CameraMetadata m;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800283 m = rawInfo.static_camera_characteristics;
Yin-Chia Yehb6dc0bf2015-02-18 14:42:16 -0800284 deriveCameraCharacteristicsKeys(rawInfo.device_version, m);
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700285 cameraInfo = rawInfo;
Chien-Yu Chen944f8432015-07-01 17:08:03 -0700286 cameraInfo.static_camera_characteristics = m.release();
287 index = mCameraInfoMap.add(cameraId, cameraInfo);
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800288 }
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800289
290 assert(index != NAME_NOT_FOUND);
291 // return the cached camera info
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700292 *info = mCameraInfoMap[index];
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700293 return OK;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800294}
295
296int CameraModule::open(const char* id, struct hw_device_t** device) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700297 int res;
298 ATRACE_BEGIN("camera_module->open");
299 res = filterOpenErrorCode(mModule->common.methods->open(&mModule->common, id, device));
300 ATRACE_END();
301 return res;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800302}
303
304int CameraModule::openLegacy(
305 const char* id, uint32_t halVersion, struct hw_device_t** device) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700306 int res;
307 ATRACE_BEGIN("camera_module->open_legacy");
308 res = mModule->open_legacy(&mModule->common, id, halVersion, device);
309 ATRACE_END();
310 return res;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800311}
312
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800313int CameraModule::getNumberOfCameras() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700314 int numCameras;
315 ATRACE_BEGIN("camera_module->get_number_of_cameras");
316 numCameras = mModule->get_number_of_cameras();
317 ATRACE_END();
318 return numCameras;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800319}
320
321int CameraModule::setCallbacks(const camera_module_callbacks_t *callbacks) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700322 int res;
323 ATRACE_BEGIN("camera_module->set_callbacks");
324 res = mModule->set_callbacks(callbacks);
325 ATRACE_END();
326 return res;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800327}
328
329bool CameraModule::isVendorTagDefined() {
330 return mModule->get_vendor_tag_ops != NULL;
331}
332
333void CameraModule::getVendorTagOps(vendor_tag_ops_t* ops) {
334 if (mModule->get_vendor_tag_ops) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700335 ATRACE_BEGIN("camera_module->get_vendor_tag_ops");
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800336 mModule->get_vendor_tag_ops(ops);
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700337 ATRACE_END();
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800338 }
339}
340
341int CameraModule::setTorchMode(const char* camera_id, bool enable) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700342 int res;
343 ATRACE_BEGIN("camera_module->set_torch_mode");
344 res = mModule->set_torch_mode(camera_id, enable);
345 ATRACE_END();
346 return res;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800347}
348
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800349status_t CameraModule::filterOpenErrorCode(status_t err) {
350 switch(err) {
351 case NO_ERROR:
352 case -EBUSY:
353 case -EINVAL:
354 case -EUSERS:
355 return err;
356 default:
357 break;
358 }
359 return -ENODEV;
360}
361
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800362uint16_t CameraModule::getModuleApiVersion() {
363 return mModule->common.module_api_version;
364}
365
366const char* CameraModule::getModuleName() {
367 return mModule->common.name;
368}
369
370uint16_t CameraModule::getHalApiVersion() {
371 return mModule->common.hal_api_version;
372}
373
374const char* CameraModule::getModuleAuthor() {
375 return mModule->common.author;
376}
377
378void* CameraModule::getDso() {
379 return mModule->common.dso;
380}
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800381
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800382}; // namespace android