1 /*
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"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 //#define LOG_NDEBUG 0
20
21 #include <utils/Trace.h>
22
23 #include "CameraModule.h"
24
25 namespace android {
26
deriveCameraCharacteristicsKeys(uint32_t deviceVersion,CameraMetadata & chars)27 void CameraModule::deriveCameraCharacteristicsKeys(
28 uint32_t deviceVersion, CameraMetadata &chars) {
29 ATRACE_CALL();
30 // 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) {
38 const size_t NUM_DERIVED_KEYS_HAL3_3 = 5;
39 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);
44 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 }
49
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
85 chars.update(ANDROID_CONTROL_AVAILABLE_MODES, controlModes);
86
87 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
110 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);
117 availableCharsKeys.push(ANDROID_CONTROL_AWB_LOCK_AVAILABLE);
118 availableCharsKeys.push(ANDROID_CONTROL_AVAILABLE_MODES);
119 availableCharsKeys.push(ANDROID_SHADING_AVAILABLE_MODES);
120 availableCharsKeys.push(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES);
121 chars.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, availableCharsKeys);
122
123 // 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 }
138 }
139
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) {
143 Vector<int32_t> preCorrectionArray;
144 entry = chars.find(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE);
145 preCorrectionArray.appendArray(entry.data.i32, entry.count);
146 chars.update(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE, preCorrectionArray);
147 }
148
149 return;
150 }
151
CameraModule(camera_module_t * module)152 CameraModule::CameraModule(camera_module_t *module) {
153 if (module == NULL) {
154 ALOGE("%s: camera hardware module must not be null", __FUNCTION__);
155 assert(0);
156 }
157 mModule = module;
158 }
159
~CameraModule()160 CameraModule::~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
init()172 int CameraModule::init() {
173 ATRACE_CALL();
174 int res = OK;
175 if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 &&
176 mModule->init != NULL) {
177 ATRACE_BEGIN("camera_module->init");
178 res = mModule->init();
179 ATRACE_END();
180 }
181 mCameraInfoMap.setCapacity(getNumberOfCameras());
182 return res;
183 }
184
getCameraInfo(int cameraId,struct camera_info * info)185 int CameraModule::getCameraInfo(int cameraId, struct camera_info *info) {
186 ATRACE_CALL();
187 Mutex::Autolock lock(mCameraInfoLock);
188 if (cameraId < 0) {
189 ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
190 return -EINVAL;
191 }
192
193 // 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) {
196 int ret;
197 ATRACE_BEGIN("camera_module->get_camera_info");
198 ret = mModule->get_camera_info(cameraId, info);
199 ATRACE_END();
200 return ret;
201 }
202
203 ssize_t index = mCameraInfoMap.indexOfKey(cameraId);
204 if (index == NAME_NOT_FOUND) {
205 // Get camera info from raw module and cache it
206 camera_info rawInfo, cameraInfo;
207 ATRACE_BEGIN("camera_module->get_camera_info");
208 int ret = mModule->get_camera_info(cameraId, &rawInfo);
209 ATRACE_END();
210 if (ret != 0) {
211 return ret;
212 }
213 int deviceVersion = rawInfo.device_version;
214 if (deviceVersion < CAMERA_DEVICE_API_VERSION_2_0) {
215 // static_camera_characteristics is invalid
216 *info = rawInfo;
217 return ret;
218 }
219 CameraMetadata m;
220 m = rawInfo.static_camera_characteristics;
221 deriveCameraCharacteristicsKeys(rawInfo.device_version, m);
222 cameraInfo = rawInfo;
223 cameraInfo.static_camera_characteristics = m.release();
224 index = mCameraInfoMap.add(cameraId, cameraInfo);
225 }
226
227 assert(index != NAME_NOT_FOUND);
228 // return the cached camera info
229 *info = mCameraInfoMap[index];
230 return OK;
231 }
232
open(const char * id,struct hw_device_t ** device)233 int CameraModule::open(const char* id, struct hw_device_t** device) {
234 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;
239 }
240
openLegacy(const char * id,uint32_t halVersion,struct hw_device_t ** device)241 int CameraModule::openLegacy(
242 const char* id, uint32_t halVersion, struct hw_device_t** device) {
243 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;
248 }
249
getNumberOfCameras()250 int CameraModule::getNumberOfCameras() {
251 int numCameras;
252 ATRACE_BEGIN("camera_module->get_number_of_cameras");
253 numCameras = mModule->get_number_of_cameras();
254 ATRACE_END();
255 return numCameras;
256 }
257
setCallbacks(const camera_module_callbacks_t * callbacks)258 int CameraModule::setCallbacks(const camera_module_callbacks_t *callbacks) {
259 int res;
260 ATRACE_BEGIN("camera_module->set_callbacks");
261 res = mModule->set_callbacks(callbacks);
262 ATRACE_END();
263 return res;
264 }
265
isVendorTagDefined()266 bool CameraModule::isVendorTagDefined() {
267 return mModule->get_vendor_tag_ops != NULL;
268 }
269
getVendorTagOps(vendor_tag_ops_t * ops)270 void CameraModule::getVendorTagOps(vendor_tag_ops_t* ops) {
271 if (mModule->get_vendor_tag_ops) {
272 ATRACE_BEGIN("camera_module->get_vendor_tag_ops");
273 mModule->get_vendor_tag_ops(ops);
274 ATRACE_END();
275 }
276 }
277
setTorchMode(const char * camera_id,bool enable)278 int CameraModule::setTorchMode(const char* camera_id, bool enable) {
279 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;
284 }
285
filterOpenErrorCode(status_t err)286 status_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
getModuleApiVersion()299 uint16_t CameraModule::getModuleApiVersion() {
300 return mModule->common.module_api_version;
301 }
302
getModuleName()303 const char* CameraModule::getModuleName() {
304 return mModule->common.name;
305 }
306
getHalApiVersion()307 uint16_t CameraModule::getHalApiVersion() {
308 return mModule->common.hal_api_version;
309 }
310
getModuleAuthor()311 const char* CameraModule::getModuleAuthor() {
312 return mModule->common.author;
313 }
314
getDso()315 void* CameraModule::getDso() {
316 return mModule->common.dso;
317 }
318
319 }; // namespace android
320