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 27 void CameraModule::deriveCameraCharacteristicsKeys( 28 uint32_t deviceVersion, CameraMetadata &chars) { 29 ATRACE_CALL(); 30 31 Vector<int32_t> derivedCharKeys; 32 Vector<int32_t> derivedRequestKeys; 33 Vector<int32_t> derivedResultKeys; 34 // Keys added in HAL3.3 35 if (deviceVersion < CAMERA_DEVICE_API_VERSION_3_3) { 36 Vector<uint8_t> controlModes; 37 uint8_t data = ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE; 38 chars.update(ANDROID_CONTROL_AE_LOCK_AVAILABLE, &data, /*count*/1); 39 data = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_TRUE; 40 chars.update(ANDROID_CONTROL_AWB_LOCK_AVAILABLE, &data, /*count*/1); 41 controlModes.push(ANDROID_CONTROL_MODE_AUTO); 42 camera_metadata_entry entry = chars.find(ANDROID_CONTROL_AVAILABLE_SCENE_MODES); 43 if (entry.count > 1 || entry.data.u8[0] != ANDROID_CONTROL_SCENE_MODE_DISABLED) { 44 controlModes.push(ANDROID_CONTROL_MODE_USE_SCENE_MODE); 45 } 46 47 // Only advertise CONTROL_OFF mode if 3A manual controls are supported. 48 bool isManualAeSupported = false; 49 bool isManualAfSupported = false; 50 bool isManualAwbSupported = false; 51 entry = chars.find(ANDROID_CONTROL_AE_AVAILABLE_MODES); 52 if (entry.count > 0) { 53 for (size_t i = 0; i < entry.count; i++) { 54 if (entry.data.u8[i] == ANDROID_CONTROL_AE_MODE_OFF) { 55 isManualAeSupported = true; 56 break; 57 } 58 } 59 } 60 entry = chars.find(ANDROID_CONTROL_AF_AVAILABLE_MODES); 61 if (entry.count > 0) { 62 for (size_t i = 0; i < entry.count; i++) { 63 if (entry.data.u8[i] == ANDROID_CONTROL_AF_MODE_OFF) { 64 isManualAfSupported = true; 65 break; 66 } 67 } 68 } 69 entry = chars.find(ANDROID_CONTROL_AWB_AVAILABLE_MODES); 70 if (entry.count > 0) { 71 for (size_t i = 0; i < entry.count; i++) { 72 if (entry.data.u8[i] == ANDROID_CONTROL_AWB_MODE_OFF) { 73 isManualAwbSupported = true; 74 break; 75 } 76 } 77 } 78 if (isManualAeSupported && isManualAfSupported && isManualAwbSupported) { 79 controlModes.push(ANDROID_CONTROL_MODE_OFF); 80 } 81 82 chars.update(ANDROID_CONTROL_AVAILABLE_MODES, controlModes); 83 84 entry = chars.find(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS); 85 // HAL3.2 devices passing existing CTS test should all support all LSC modes and LSC map 86 bool lensShadingModeSupported = false; 87 if (entry.count > 0) { 88 for (size_t i = 0; i < entry.count; i++) { 89 if (entry.data.i32[i] == ANDROID_SHADING_MODE) { 90 lensShadingModeSupported = true; 91 break; 92 } 93 } 94 } 95 Vector<uint8_t> lscModes; 96 Vector<uint8_t> lscMapModes; 97 lscModes.push(ANDROID_SHADING_MODE_FAST); 98 lscModes.push(ANDROID_SHADING_MODE_HIGH_QUALITY); 99 lscMapModes.push(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF); 100 if (lensShadingModeSupported) { 101 lscModes.push(ANDROID_SHADING_MODE_OFF); 102 lscMapModes.push(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_ON); 103 } 104 chars.update(ANDROID_SHADING_AVAILABLE_MODES, lscModes); 105 chars.update(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES, lscMapModes); 106 107 derivedCharKeys.push(ANDROID_CONTROL_AE_LOCK_AVAILABLE); 108 derivedCharKeys.push(ANDROID_CONTROL_AWB_LOCK_AVAILABLE); 109 derivedCharKeys.push(ANDROID_CONTROL_AVAILABLE_MODES); 110 derivedCharKeys.push(ANDROID_SHADING_AVAILABLE_MODES); 111 derivedCharKeys.push(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES); 112 113 // Need update android.control.availableHighSpeedVideoConfigurations since HAL3.3 114 // adds batch size to this array. 115 entry = chars.find(ANDROID_CONTROL_AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS); 116 if (entry.count > 0) { 117 Vector<int32_t> highSpeedConfig; 118 for (size_t i = 0; i < entry.count; i += 4) { 119 highSpeedConfig.add(entry.data.i32[i]); // width 120 highSpeedConfig.add(entry.data.i32[i + 1]); // height 121 highSpeedConfig.add(entry.data.i32[i + 2]); // fps_min 122 highSpeedConfig.add(entry.data.i32[i + 3]); // fps_max 123 highSpeedConfig.add(1); // batchSize_max. default to 1 for HAL3.2 124 } 125 chars.update(ANDROID_CONTROL_AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS, 126 highSpeedConfig); 127 } 128 } 129 130 // Keys added in HAL3.4 131 if (deviceVersion < CAMERA_DEVICE_API_VERSION_3_4) { 132 // Check if HAL supports RAW_OPAQUE output 133 camera_metadata_entry entry = chars.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS); 134 bool supportRawOpaque = false; 135 bool supportAnyRaw = false; 136 const int STREAM_CONFIGURATION_SIZE = 4; 137 const int STREAM_FORMAT_OFFSET = 0; 138 const int STREAM_WIDTH_OFFSET = 1; 139 const int STREAM_HEIGHT_OFFSET = 2; 140 const int STREAM_IS_INPUT_OFFSET = 3; 141 Vector<int32_t> rawOpaqueSizes; 142 143 for (size_t i=0; i < entry.count; i += STREAM_CONFIGURATION_SIZE) { 144 int32_t format = entry.data.i32[i + STREAM_FORMAT_OFFSET]; 145 int32_t width = entry.data.i32[i + STREAM_WIDTH_OFFSET]; 146 int32_t height = entry.data.i32[i + STREAM_HEIGHT_OFFSET]; 147 int32_t isInput = entry.data.i32[i + STREAM_IS_INPUT_OFFSET]; 148 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT && 149 format == HAL_PIXEL_FORMAT_RAW_OPAQUE) { 150 supportRawOpaque = true; 151 rawOpaqueSizes.push(width); 152 rawOpaqueSizes.push(height); 153 // 2 bytes per pixel. This rough estimation is only used when 154 // HAL does not fill in the opaque raw size 155 rawOpaqueSizes.push(width * height *2); 156 } 157 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT && 158 (format == HAL_PIXEL_FORMAT_RAW16 || 159 format == HAL_PIXEL_FORMAT_RAW10 || 160 format == HAL_PIXEL_FORMAT_RAW12 || 161 format == HAL_PIXEL_FORMAT_RAW_OPAQUE)) { 162 supportAnyRaw = true; 163 } 164 } 165 166 if (supportRawOpaque) { 167 entry = chars.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE); 168 if (entry.count == 0) { 169 // Fill in estimated value if HAL does not list it 170 chars.update(ANDROID_SENSOR_OPAQUE_RAW_SIZE, rawOpaqueSizes); 171 derivedCharKeys.push(ANDROID_SENSOR_OPAQUE_RAW_SIZE); 172 } 173 } 174 175 // Check if HAL supports any RAW output, if so, fill in postRawSensitivityBoost range 176 if (supportAnyRaw) { 177 int32_t defaultRange[2] = {100, 100}; 178 entry = chars.find(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE); 179 if (entry.count == 0) { 180 // Fill in default value (100, 100) 181 chars.update( 182 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE, 183 defaultRange, 2); 184 derivedCharKeys.push(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE); 185 // Actual request/results will be derived by camera device. 186 derivedRequestKeys.push(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST); 187 derivedResultKeys.push(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST); 188 } 189 } 190 } 191 192 // Always add a default for the pre-correction active array if the vendor chooses to omit this 193 camera_metadata_entry entry = chars.find(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE); 194 if (entry.count == 0) { 195 Vector<int32_t> preCorrectionArray; 196 entry = chars.find(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE); 197 preCorrectionArray.appendArray(entry.data.i32, entry.count); 198 chars.update(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE, preCorrectionArray); 199 derivedCharKeys.push(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE); 200 } 201 202 // Add those newly added keys to AVAILABLE_CHARACTERISTICS_KEYS 203 // This has to be done at this end of this function. 204 if (derivedCharKeys.size() > 0) { 205 appendAvailableKeys( 206 chars, ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, derivedCharKeys); 207 } 208 if (derivedRequestKeys.size() > 0) { 209 appendAvailableKeys( 210 chars, ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS, derivedRequestKeys); 211 } 212 if (derivedResultKeys.size() > 0) { 213 appendAvailableKeys( 214 chars, ANDROID_REQUEST_AVAILABLE_RESULT_KEYS, derivedResultKeys); 215 } 216 return; 217 } 218 219 void CameraModule::appendAvailableKeys(CameraMetadata &chars, 220 int32_t keyTag, const Vector<int32_t>& appendKeys) { 221 camera_metadata_entry entry = chars.find(keyTag); 222 Vector<int32_t> availableKeys; 223 availableKeys.setCapacity(entry.count + appendKeys.size()); 224 for (size_t i = 0; i < entry.count; i++) { 225 availableKeys.push(entry.data.i32[i]); 226 } 227 for (size_t i = 0; i < appendKeys.size(); i++) { 228 availableKeys.push(appendKeys[i]); 229 } 230 chars.update(keyTag, availableKeys); 231 } 232 233 CameraModule::CameraModule(camera_module_t *module) { 234 if (module == NULL) { 235 ALOGE("%s: camera hardware module must not be null", __FUNCTION__); 236 assert(0); 237 } 238 mModule = module; 239 } 240 241 CameraModule::~CameraModule() 242 { 243 while (mCameraInfoMap.size() > 0) { 244 camera_info cameraInfo = mCameraInfoMap.editValueAt(0); 245 if (cameraInfo.static_camera_characteristics != NULL) { 246 free_camera_metadata( 247 const_cast<camera_metadata_t*>(cameraInfo.static_camera_characteristics)); 248 } 249 mCameraInfoMap.removeItemsAt(0); 250 } 251 } 252 253 int CameraModule::init() { 254 ATRACE_CALL(); 255 int res = OK; 256 if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 && 257 mModule->init != NULL) { 258 ATRACE_BEGIN("camera_module->init"); 259 res = mModule->init(); 260 ATRACE_END(); 261 } 262 mCameraInfoMap.setCapacity(getNumberOfCameras()); 263 return res; 264 } 265 266 int CameraModule::getCameraInfo(int cameraId, struct camera_info *info) { 267 ATRACE_CALL(); 268 Mutex::Autolock lock(mCameraInfoLock); 269 if (cameraId < 0) { 270 ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId); 271 return -EINVAL; 272 } 273 274 // Only override static_camera_characteristics for API2 devices 275 int apiVersion = mModule->common.module_api_version; 276 if (apiVersion < CAMERA_MODULE_API_VERSION_2_0) { 277 int ret; 278 ATRACE_BEGIN("camera_module->get_camera_info"); 279 ret = mModule->get_camera_info(cameraId, info); 280 // Fill in this so CameraService won't be confused by 281 // possibly 0 device_version 282 info->device_version = CAMERA_DEVICE_API_VERSION_1_0; 283 ATRACE_END(); 284 return ret; 285 } 286 287 ssize_t index = mCameraInfoMap.indexOfKey(cameraId); 288 if (index == NAME_NOT_FOUND) { 289 // Get camera info from raw module and cache it 290 camera_info rawInfo, cameraInfo; 291 ATRACE_BEGIN("camera_module->get_camera_info"); 292 int ret = mModule->get_camera_info(cameraId, &rawInfo); 293 ATRACE_END(); 294 if (ret != 0) { 295 return ret; 296 } 297 int deviceVersion = rawInfo.device_version; 298 if (deviceVersion < CAMERA_DEVICE_API_VERSION_3_0) { 299 // static_camera_characteristics is invalid 300 *info = rawInfo; 301 return ret; 302 } 303 CameraMetadata m; 304 m = rawInfo.static_camera_characteristics; 305 deriveCameraCharacteristicsKeys(rawInfo.device_version, m); 306 cameraInfo = rawInfo; 307 cameraInfo.static_camera_characteristics = m.release(); 308 index = mCameraInfoMap.add(cameraId, cameraInfo); 309 } 310 311 assert(index != NAME_NOT_FOUND); 312 // return the cached camera info 313 *info = mCameraInfoMap[index]; 314 return OK; 315 } 316 317 int CameraModule::open(const char* id, struct hw_device_t** device) { 318 int res; 319 ATRACE_BEGIN("camera_module->open"); 320 res = filterOpenErrorCode(mModule->common.methods->open(&mModule->common, id, device)); 321 ATRACE_END(); 322 return res; 323 } 324 325 int CameraModule::openLegacy( 326 const char* id, uint32_t halVersion, struct hw_device_t** device) { 327 int res; 328 ATRACE_BEGIN("camera_module->open_legacy"); 329 res = mModule->open_legacy(&mModule->common, id, halVersion, device); 330 ATRACE_END(); 331 return res; 332 } 333 334 int CameraModule::getNumberOfCameras() { 335 int numCameras; 336 ATRACE_BEGIN("camera_module->get_number_of_cameras"); 337 numCameras = mModule->get_number_of_cameras(); 338 ATRACE_END(); 339 return numCameras; 340 } 341 342 int CameraModule::setCallbacks(const camera_module_callbacks_t *callbacks) { 343 int res; 344 ATRACE_BEGIN("camera_module->set_callbacks"); 345 res = mModule->set_callbacks(callbacks); 346 ATRACE_END(); 347 return res; 348 } 349 350 bool CameraModule::isVendorTagDefined() { 351 return mModule->get_vendor_tag_ops != NULL; 352 } 353 354 void CameraModule::getVendorTagOps(vendor_tag_ops_t* ops) { 355 if (mModule->get_vendor_tag_ops) { 356 ATRACE_BEGIN("camera_module->get_vendor_tag_ops"); 357 mModule->get_vendor_tag_ops(ops); 358 ATRACE_END(); 359 } 360 } 361 362 int CameraModule::setTorchMode(const char* camera_id, bool enable) { 363 int res; 364 ATRACE_BEGIN("camera_module->set_torch_mode"); 365 res = mModule->set_torch_mode(camera_id, enable); 366 ATRACE_END(); 367 return res; 368 } 369 370 status_t CameraModule::filterOpenErrorCode(status_t err) { 371 switch(err) { 372 case NO_ERROR: 373 case -EBUSY: 374 case -EINVAL: 375 case -EUSERS: 376 return err; 377 default: 378 break; 379 } 380 return -ENODEV; 381 } 382 383 uint16_t CameraModule::getModuleApiVersion() { 384 return mModule->common.module_api_version; 385 } 386 387 const char* CameraModule::getModuleName() { 388 return mModule->common.name; 389 } 390 391 uint16_t CameraModule::getHalApiVersion() { 392 return mModule->common.hal_api_version; 393 } 394 395 const char* CameraModule::getModuleAuthor() { 396 return mModule->common.author; 397 } 398 399 void* CameraModule::getDso() { 400 return mModule->common.dso; 401 } 402 403 }; // namespace android 404