Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 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 LOG_NDEBUG 0 |
| 19 | |
| 20 | #include "CameraModule.h" |
| 21 | |
| 22 | namespace android { |
| 23 | |
| 24 | void CameraModule::deriveCameraCharacteristicsKeys( |
| 25 | uint32_t deviceVersion, CameraMetadata &chars) { |
| 26 | // HAL1 devices should not reach here |
| 27 | if (deviceVersion < CAMERA_DEVICE_API_VERSION_2_0) { |
| 28 | ALOGV("%s: Cannot derive keys for HAL version < 2.0"); |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | // Keys added in HAL3.3 |
| 33 | if (deviceVersion < CAMERA_DEVICE_API_VERSION_3_3) { |
Yin-Chia Yeh | b28c344 | 2015-05-07 14:43:19 -0700 | [diff] [blame^] | 34 | const size_t NUM_DERIVED_KEYS_HAL3_3 = 3; |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 35 | Vector<uint8_t> controlModes; |
| 36 | uint8_t data = ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE; |
| 37 | chars.update(ANDROID_CONTROL_AE_LOCK_AVAILABLE, &data, /*count*/1); |
| 38 | data = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_TRUE; |
| 39 | chars.update(ANDROID_CONTROL_AWB_LOCK_AVAILABLE, &data, /*count*/1); |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 40 | controlModes.push(ANDROID_CONTROL_MODE_AUTO); |
| 41 | camera_metadata_entry entry = chars.find(ANDROID_CONTROL_AVAILABLE_SCENE_MODES); |
| 42 | if (entry.count > 1 || entry.data.u8[0] != ANDROID_CONTROL_SCENE_MODE_DISABLED) { |
| 43 | controlModes.push(ANDROID_CONTROL_MODE_USE_SCENE_MODE); |
| 44 | } |
Zhijun He | 9c5af61 | 2015-05-04 16:34:37 -0700 | [diff] [blame] | 45 | |
| 46 | // Only advertise CONTROL_OFF mode if 3A manual controls are supported. |
| 47 | bool isManualAeSupported = false; |
| 48 | bool isManualAfSupported = false; |
| 49 | bool isManualAwbSupported = false; |
| 50 | entry = chars.find(ANDROID_CONTROL_AE_AVAILABLE_MODES); |
| 51 | if (entry.count > 0) { |
| 52 | for (size_t i = 0; i < entry.count; i++) { |
| 53 | if (entry.data.u8[i] == ANDROID_CONTROL_AE_MODE_OFF) { |
| 54 | isManualAeSupported = true; |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | entry = chars.find(ANDROID_CONTROL_AF_AVAILABLE_MODES); |
| 60 | if (entry.count > 0) { |
| 61 | for (size_t i = 0; i < entry.count; i++) { |
| 62 | if (entry.data.u8[i] == ANDROID_CONTROL_AF_MODE_OFF) { |
| 63 | isManualAfSupported = true; |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | entry = chars.find(ANDROID_CONTROL_AWB_AVAILABLE_MODES); |
| 69 | if (entry.count > 0) { |
| 70 | for (size_t i = 0; i < entry.count; i++) { |
| 71 | if (entry.data.u8[i] == ANDROID_CONTROL_AWB_MODE_OFF) { |
| 72 | isManualAwbSupported = true; |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | if (isManualAeSupported && isManualAfSupported && isManualAwbSupported) { |
| 78 | controlModes.push(ANDROID_CONTROL_MODE_OFF); |
| 79 | } |
| 80 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 81 | chars.update(ANDROID_CONTROL_AVAILABLE_MODES, controlModes); |
Yin-Chia Yeh | b28c344 | 2015-05-07 14:43:19 -0700 | [diff] [blame^] | 82 | |
| 83 | entry = chars.find(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS); |
| 84 | Vector<int32_t> availableCharsKeys; |
| 85 | availableCharsKeys.setCapacity(entry.count + NUM_DERIVED_KEYS_HAL3_3); |
| 86 | for (size_t i = 0; i < entry.count; i++) { |
| 87 | availableCharsKeys.push(entry.data.i32[i]); |
| 88 | } |
| 89 | availableCharsKeys.push(ANDROID_CONTROL_AE_LOCK_AVAILABLE); |
| 90 | availableCharsKeys.push(ANDROID_CONTROL_AWB_LOCK_AVAILABLE_TRUE); |
| 91 | availableCharsKeys.push(ANDROID_CONTROL_AVAILABLE_MODES); |
| 92 | chars.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, availableCharsKeys); |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 93 | } |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | CameraModule::CameraModule(camera_module_t *module) { |
| 98 | if (module == NULL) { |
| 99 | ALOGE("%s: camera hardware module must not be null", __FUNCTION__); |
| 100 | assert(0); |
| 101 | } |
| 102 | |
| 103 | mModule = module; |
Chien-Yu Chen | 676b21b | 2015-02-24 10:28:19 -0800 | [diff] [blame] | 104 | mCameraInfoMap.setCapacity(getNumberOfCameras()); |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 107 | int CameraModule::init() { |
| 108 | if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 && |
| 109 | mModule->init != NULL) { |
| 110 | return mModule->init(); |
| 111 | } |
| 112 | return OK; |
| 113 | } |
| 114 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 115 | int CameraModule::getCameraInfo(int cameraId, struct camera_info *info) { |
| 116 | Mutex::Autolock lock(mCameraInfoLock); |
Chien-Yu Chen | 676b21b | 2015-02-24 10:28:19 -0800 | [diff] [blame] | 117 | if (cameraId < 0) { |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 118 | ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId); |
| 119 | return -EINVAL; |
| 120 | } |
| 121 | |
Yin-Chia Yeh | b6dc0bf | 2015-02-18 14:42:16 -0800 | [diff] [blame] | 122 | // Only override static_camera_characteristics for API2 devices |
| 123 | int apiVersion = mModule->common.module_api_version; |
| 124 | if (apiVersion < CAMERA_MODULE_API_VERSION_2_0) { |
| 125 | return mModule->get_camera_info(cameraId, info); |
| 126 | } |
| 127 | |
Chien-Yu Chen | 676b21b | 2015-02-24 10:28:19 -0800 | [diff] [blame] | 128 | ssize_t index = mCameraInfoMap.indexOfKey(cameraId); |
| 129 | if (index == NAME_NOT_FOUND) { |
| 130 | // Get camera info from raw module and cache it |
Yin-Chia Yeh | 54298b3 | 2015-03-24 16:51:41 -0700 | [diff] [blame] | 131 | camera_info rawInfo, cameraInfo; |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 132 | int ret = mModule->get_camera_info(cameraId, &rawInfo); |
| 133 | if (ret != 0) { |
| 134 | return ret; |
| 135 | } |
Zhijun He | 9c5af61 | 2015-05-04 16:34:37 -0700 | [diff] [blame] | 136 | int deviceVersion = rawInfo.device_version; |
Yin-Chia Yeh | 7768ded | 2015-04-15 12:16:02 -0700 | [diff] [blame] | 137 | if (deviceVersion < CAMERA_DEVICE_API_VERSION_2_0) { |
| 138 | // static_camera_characteristics is invalid |
| 139 | *info = rawInfo; |
| 140 | return ret; |
| 141 | } |
Yin-Chia Yeh | 54298b3 | 2015-03-24 16:51:41 -0700 | [diff] [blame] | 142 | CameraMetadata m; |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 143 | m = rawInfo.static_camera_characteristics; |
Yin-Chia Yeh | b6dc0bf | 2015-02-18 14:42:16 -0800 | [diff] [blame] | 144 | deriveCameraCharacteristicsKeys(rawInfo.device_version, m); |
Yin-Chia Yeh | 54298b3 | 2015-03-24 16:51:41 -0700 | [diff] [blame] | 145 | mCameraCharacteristicsMap.add(cameraId, m); |
| 146 | cameraInfo = rawInfo; |
| 147 | cameraInfo.static_camera_characteristics = |
| 148 | mCameraCharacteristicsMap.valueFor(cameraId).getAndLock(); |
Chien-Yu Chen | 676b21b | 2015-02-24 10:28:19 -0800 | [diff] [blame] | 149 | mCameraInfoMap.add(cameraId, cameraInfo); |
| 150 | index = mCameraInfoMap.indexOfKey(cameraId); |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 151 | } |
Chien-Yu Chen | 676b21b | 2015-02-24 10:28:19 -0800 | [diff] [blame] | 152 | |
| 153 | assert(index != NAME_NOT_FOUND); |
| 154 | // return the cached camera info |
Yin-Chia Yeh | 54298b3 | 2015-03-24 16:51:41 -0700 | [diff] [blame] | 155 | *info = mCameraInfoMap[index]; |
Eino-Ville Talvala | 1527f07 | 2015-04-07 15:55:31 -0700 | [diff] [blame] | 156 | return OK; |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | int CameraModule::open(const char* id, struct hw_device_t** device) { |
Chien-Yu Chen | d231fd6 | 2015-02-25 16:04:22 -0800 | [diff] [blame] | 160 | return filterOpenErrorCode(mModule->common.methods->open(&mModule->common, id, device)); |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | int CameraModule::openLegacy( |
| 164 | const char* id, uint32_t halVersion, struct hw_device_t** device) { |
| 165 | return mModule->open_legacy(&mModule->common, id, halVersion, device); |
| 166 | } |
| 167 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 168 | int CameraModule::getNumberOfCameras() { |
| 169 | return mModule->get_number_of_cameras(); |
| 170 | } |
| 171 | |
| 172 | int CameraModule::setCallbacks(const camera_module_callbacks_t *callbacks) { |
| 173 | return mModule->set_callbacks(callbacks); |
| 174 | } |
| 175 | |
| 176 | bool CameraModule::isVendorTagDefined() { |
| 177 | return mModule->get_vendor_tag_ops != NULL; |
| 178 | } |
| 179 | |
| 180 | void CameraModule::getVendorTagOps(vendor_tag_ops_t* ops) { |
| 181 | if (mModule->get_vendor_tag_ops) { |
| 182 | mModule->get_vendor_tag_ops(ops); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | int CameraModule::setTorchMode(const char* camera_id, bool enable) { |
| 187 | return mModule->set_torch_mode(camera_id, enable); |
| 188 | } |
| 189 | |
Chien-Yu Chen | d231fd6 | 2015-02-25 16:04:22 -0800 | [diff] [blame] | 190 | status_t CameraModule::filterOpenErrorCode(status_t err) { |
| 191 | switch(err) { |
| 192 | case NO_ERROR: |
| 193 | case -EBUSY: |
| 194 | case -EINVAL: |
| 195 | case -EUSERS: |
| 196 | return err; |
| 197 | default: |
| 198 | break; |
| 199 | } |
| 200 | return -ENODEV; |
| 201 | } |
| 202 | |
Chien-Yu Chen | 676b21b | 2015-02-24 10:28:19 -0800 | [diff] [blame] | 203 | uint16_t CameraModule::getModuleApiVersion() { |
| 204 | return mModule->common.module_api_version; |
| 205 | } |
| 206 | |
| 207 | const char* CameraModule::getModuleName() { |
| 208 | return mModule->common.name; |
| 209 | } |
| 210 | |
| 211 | uint16_t CameraModule::getHalApiVersion() { |
| 212 | return mModule->common.hal_api_version; |
| 213 | } |
| 214 | |
| 215 | const char* CameraModule::getModuleAuthor() { |
| 216 | return mModule->common.author; |
| 217 | } |
| 218 | |
| 219 | void* CameraModule::getDso() { |
| 220 | return mModule->common.dso; |
| 221 | } |
Chien-Yu Chen | d231fd6 | 2015-02-25 16:04:22 -0800 | [diff] [blame] | 222 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 223 | }; // namespace android |