blob: d1c79d0fa0524a817ab9a8df43a325c1821e936a [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;
133 const int STREAM_CONFIGURATION_SIZE = 4;
134 const int STREAM_FORMAT_OFFSET = 0;
135 const int STREAM_WIDTH_OFFSET = 1;
136 const int STREAM_HEIGHT_OFFSET = 2;
137 const int STREAM_IS_INPUT_OFFSET = 3;
138 Vector<int32_t> rawOpaqueSizes;
139
140 for (size_t i=0; i < entry.count; i += STREAM_CONFIGURATION_SIZE) {
141 int32_t format = entry.data.i32[i + STREAM_FORMAT_OFFSET];
142 int32_t width = entry.data.i32[i + STREAM_WIDTH_OFFSET];
143 int32_t height = entry.data.i32[i + STREAM_HEIGHT_OFFSET];
144 int32_t isInput = entry.data.i32[i + STREAM_IS_INPUT_OFFSET];
145 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT &&
146 format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
147 supportRawOpaque = true;
148 rawOpaqueSizes.push(width);
149 rawOpaqueSizes.push(height);
150 // 2 bytes per pixel. This rough estimation is only used when
151 // HAL does not fill in the opaque raw size
152 rawOpaqueSizes.push(width * height *2);
153 }
154 }
155
156 if (supportRawOpaque) {
157 entry = chars.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
158 if (entry.count == 0) {
159 // Fill in estimated value if HAL does not list it
160 chars.update(ANDROID_SENSOR_OPAQUE_RAW_SIZE, rawOpaqueSizes);
161 derivedCharKeys.push(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
162 }
163 }
164 }
165
Ruben Brunka3b3caa2015-06-22 14:26:51 -0700166 // Always add a default for the pre-correction active array if the vendor chooses to omit this
167 camera_metadata_entry entry = chars.find(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE);
168 if (entry.count == 0) {
Ruben Brunkeff134a2015-07-17 15:22:01 -0700169 Vector<int32_t> preCorrectionArray;
Ruben Brunka3b3caa2015-06-22 14:26:51 -0700170 entry = chars.find(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE);
Ruben Brunkeff134a2015-07-17 15:22:01 -0700171 preCorrectionArray.appendArray(entry.data.i32, entry.count);
172 chars.update(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE, preCorrectionArray);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800173 derivedCharKeys.push(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE);
Ruben Brunka3b3caa2015-06-22 14:26:51 -0700174 }
175
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800176 // Add those newly added keys to AVAILABLE_CHARACTERISTICS_KEYS
177 // This has to be done at this end of this function.
178 entry = chars.find(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
179 Vector<int32_t> availableCharsKeys;
180 availableCharsKeys.setCapacity(entry.count + derivedCharKeys.size());
181 for (size_t i = 0; i < entry.count; i++) {
182 availableCharsKeys.push(entry.data.i32[i]);
183 }
184 for (size_t i = 0; i < derivedCharKeys.size(); i++) {
185 availableCharsKeys.push(derivedCharKeys[i]);
186 }
187 chars.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, availableCharsKeys);
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800188 return;
189}
190
191CameraModule::CameraModule(camera_module_t *module) {
192 if (module == NULL) {
193 ALOGE("%s: camera hardware module must not be null", __FUNCTION__);
194 assert(0);
195 }
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800196 mModule = module;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800197}
198
Chien-Yu Chen944f8432015-07-01 17:08:03 -0700199CameraModule::~CameraModule()
200{
201 while (mCameraInfoMap.size() > 0) {
202 camera_info cameraInfo = mCameraInfoMap.editValueAt(0);
203 if (cameraInfo.static_camera_characteristics != NULL) {
204 free_camera_metadata(
205 const_cast<camera_metadata_t*>(cameraInfo.static_camera_characteristics));
206 }
207 mCameraInfoMap.removeItemsAt(0);
208 }
209}
210
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700211int CameraModule::init() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700212 ATRACE_CALL();
213 int res = OK;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700214 if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 &&
215 mModule->init != NULL) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700216 ATRACE_BEGIN("camera_module->init");
217 res = mModule->init();
218 ATRACE_END();
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700219 }
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700220 mCameraInfoMap.setCapacity(getNumberOfCameras());
221 return res;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700222}
223
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800224int CameraModule::getCameraInfo(int cameraId, struct camera_info *info) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700225 ATRACE_CALL();
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800226 Mutex::Autolock lock(mCameraInfoLock);
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800227 if (cameraId < 0) {
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800228 ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
229 return -EINVAL;
230 }
231
Yin-Chia Yehb6dc0bf2015-02-18 14:42:16 -0800232 // Only override static_camera_characteristics for API2 devices
233 int apiVersion = mModule->common.module_api_version;
234 if (apiVersion < CAMERA_MODULE_API_VERSION_2_0) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700235 int ret;
236 ATRACE_BEGIN("camera_module->get_camera_info");
237 ret = mModule->get_camera_info(cameraId, info);
Yin-Chia Yeh654b4bf2015-12-08 12:19:31 -0800238 // Fill in this so CameraService won't be confused by
239 // possibly 0 device_version
240 info->device_version = CAMERA_DEVICE_API_VERSION_1_0;
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700241 ATRACE_END();
242 return ret;
Yin-Chia Yehb6dc0bf2015-02-18 14:42:16 -0800243 }
244
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800245 ssize_t index = mCameraInfoMap.indexOfKey(cameraId);
246 if (index == NAME_NOT_FOUND) {
247 // Get camera info from raw module and cache it
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700248 camera_info rawInfo, cameraInfo;
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700249 ATRACE_BEGIN("camera_module->get_camera_info");
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800250 int ret = mModule->get_camera_info(cameraId, &rawInfo);
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700251 ATRACE_END();
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800252 if (ret != 0) {
253 return ret;
254 }
Zhijun He9c5af612015-05-04 16:34:37 -0700255 int deviceVersion = rawInfo.device_version;
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800256 if (deviceVersion < CAMERA_DEVICE_API_VERSION_3_0) {
Yin-Chia Yeh7768ded2015-04-15 12:16:02 -0700257 // static_camera_characteristics is invalid
258 *info = rawInfo;
259 return ret;
260 }
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700261 CameraMetadata m;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800262 m = rawInfo.static_camera_characteristics;
Yin-Chia Yehb6dc0bf2015-02-18 14:42:16 -0800263 deriveCameraCharacteristicsKeys(rawInfo.device_version, m);
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700264 cameraInfo = rawInfo;
Chien-Yu Chen944f8432015-07-01 17:08:03 -0700265 cameraInfo.static_camera_characteristics = m.release();
266 index = mCameraInfoMap.add(cameraId, cameraInfo);
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800267 }
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800268
269 assert(index != NAME_NOT_FOUND);
270 // return the cached camera info
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700271 *info = mCameraInfoMap[index];
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700272 return OK;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800273}
274
275int CameraModule::open(const char* id, struct hw_device_t** device) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700276 int res;
277 ATRACE_BEGIN("camera_module->open");
278 res = filterOpenErrorCode(mModule->common.methods->open(&mModule->common, id, device));
279 ATRACE_END();
280 return res;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800281}
282
283int CameraModule::openLegacy(
284 const char* id, uint32_t halVersion, struct hw_device_t** device) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700285 int res;
286 ATRACE_BEGIN("camera_module->open_legacy");
287 res = mModule->open_legacy(&mModule->common, id, halVersion, device);
288 ATRACE_END();
289 return res;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800290}
291
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800292int CameraModule::getNumberOfCameras() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700293 int numCameras;
294 ATRACE_BEGIN("camera_module->get_number_of_cameras");
295 numCameras = mModule->get_number_of_cameras();
296 ATRACE_END();
297 return numCameras;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800298}
299
300int CameraModule::setCallbacks(const camera_module_callbacks_t *callbacks) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700301 int res;
302 ATRACE_BEGIN("camera_module->set_callbacks");
303 res = mModule->set_callbacks(callbacks);
304 ATRACE_END();
305 return res;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800306}
307
308bool CameraModule::isVendorTagDefined() {
309 return mModule->get_vendor_tag_ops != NULL;
310}
311
312void CameraModule::getVendorTagOps(vendor_tag_ops_t* ops) {
313 if (mModule->get_vendor_tag_ops) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700314 ATRACE_BEGIN("camera_module->get_vendor_tag_ops");
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800315 mModule->get_vendor_tag_ops(ops);
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700316 ATRACE_END();
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800317 }
318}
319
320int CameraModule::setTorchMode(const char* camera_id, bool enable) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700321 int res;
322 ATRACE_BEGIN("camera_module->set_torch_mode");
323 res = mModule->set_torch_mode(camera_id, enable);
324 ATRACE_END();
325 return res;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800326}
327
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800328status_t CameraModule::filterOpenErrorCode(status_t err) {
329 switch(err) {
330 case NO_ERROR:
331 case -EBUSY:
332 case -EINVAL:
333 case -EUSERS:
334 return err;
335 default:
336 break;
337 }
338 return -ENODEV;
339}
340
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800341uint16_t CameraModule::getModuleApiVersion() {
342 return mModule->common.module_api_version;
343}
344
345const char* CameraModule::getModuleName() {
346 return mModule->common.name;
347}
348
349uint16_t CameraModule::getHalApiVersion() {
350 return mModule->common.hal_api_version;
351}
352
353const char* CameraModule::getModuleAuthor() {
354 return mModule->common.author;
355}
356
357void* CameraModule::getDso() {
358 return mModule->common.dso;
359}
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800360
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800361}; // namespace android