Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "CamProvider@2.4-impl" |
Eino-Ville Talvala | 50fe430 | 2017-08-22 16:15:09 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 19 | #include <android/log.h> |
| 20 | |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 21 | #include "CameraProvider.h" |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 22 | #include "ExternalCameraProvider.h" |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 23 | #include "CameraDevice_1_0.h" |
Eino-Ville Talvala | 50fe430 | 2017-08-22 16:15:09 -0700 | [diff] [blame] | 24 | #include "CameraDevice_3_3.h" |
Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 25 | #include "CameraDevice_3_4.h" |
Eino-Ville Talvala | 50fe430 | 2017-08-22 16:15:09 -0700 | [diff] [blame] | 26 | #include <cutils/properties.h> |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 27 | #include <string.h> |
| 28 | #include <utils/Trace.h> |
| 29 | |
| 30 | |
| 31 | namespace android { |
| 32 | namespace hardware { |
| 33 | namespace camera { |
| 34 | namespace provider { |
| 35 | namespace V2_4 { |
| 36 | namespace implementation { |
| 37 | |
| 38 | namespace { |
| 39 | const char *kLegacyProviderName = "legacy/0"; |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 40 | const char *kExternalProviderName = "external/0"; |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 41 | // "device@<version>/legacy/<id>" |
| 42 | const std::regex kDeviceNameRE("device@([0-9]+\\.[0-9]+)/legacy/(.+)"); |
| 43 | const char *kHAL3_2 = "3.2"; |
Eino-Ville Talvala | 50fe430 | 2017-08-22 16:15:09 -0700 | [diff] [blame] | 44 | const char *kHAL3_3 = "3.3"; |
Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 45 | const char *kHAL3_4 = "3.4"; |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 46 | const char *kHAL1_0 = "1.0"; |
| 47 | const int kMaxCameraDeviceNameLen = 128; |
| 48 | const int kMaxCameraIdLen = 16; |
| 49 | |
Andreas Gampe | 0b171f1 | 2017-04-04 20:02:25 -0700 | [diff] [blame] | 50 | bool matchDeviceName(const hidl_string& deviceName, std::string* deviceVersion, |
| 51 | std::string* cameraId) { |
| 52 | std::string deviceNameStd(deviceName.c_str()); |
| 53 | std::smatch sm; |
| 54 | if (std::regex_match(deviceNameStd, sm, kDeviceNameRE)) { |
| 55 | if (deviceVersion != nullptr) { |
| 56 | *deviceVersion = sm[1]; |
| 57 | } |
| 58 | if (cameraId != nullptr) { |
| 59 | *cameraId = sm[2]; |
| 60 | } |
| 61 | return true; |
| 62 | } |
| 63 | return false; |
| 64 | } |
| 65 | |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 66 | } // anonymous namespace |
| 67 | |
| 68 | using ::android::hardware::camera::common::V1_0::CameraMetadataType; |
| 69 | using ::android::hardware::camera::common::V1_0::Status; |
| 70 | |
Guennadi Liakhovetski | 7b7ede7 | 2017-11-28 09:28:56 +0100 | [diff] [blame] | 71 | void CameraProvider::addDeviceNames(int camera_id, CameraDeviceStatus status, bool cam_new) |
| 72 | { |
| 73 | char cameraId[kMaxCameraIdLen]; |
| 74 | snprintf(cameraId, sizeof(cameraId), "%d", camera_id); |
| 75 | std::string cameraIdStr(cameraId); |
| 76 | |
| 77 | mCameraIds.add(cameraIdStr); |
| 78 | |
| 79 | // initialize mCameraDeviceNames and mOpenLegacySupported |
| 80 | mOpenLegacySupported[cameraIdStr] = false; |
| 81 | int deviceVersion = mModule->getDeviceVersion(camera_id); |
| 82 | auto deviceNamePair = std::make_pair(cameraIdStr, |
| 83 | getHidlDeviceName(cameraIdStr, deviceVersion)); |
| 84 | mCameraDeviceNames.add(deviceNamePair); |
| 85 | if (cam_new) { |
| 86 | mCallbacks->cameraDeviceStatusChange(deviceNamePair.second, status); |
| 87 | } |
| 88 | if (deviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 && |
| 89 | mModule->isOpenLegacyDefined()) { |
| 90 | // try open_legacy to see if it actually works |
| 91 | struct hw_device_t* halDev = nullptr; |
| 92 | int ret = mModule->openLegacy(cameraId, CAMERA_DEVICE_API_VERSION_1_0, &halDev); |
| 93 | if (ret == 0) { |
| 94 | mOpenLegacySupported[cameraIdStr] = true; |
| 95 | halDev->close(halDev); |
| 96 | deviceNamePair = std::make_pair(cameraIdStr, |
| 97 | getHidlDeviceName(cameraIdStr, CAMERA_DEVICE_API_VERSION_1_0)); |
| 98 | mCameraDeviceNames.add(deviceNamePair); |
| 99 | if (cam_new) { |
| 100 | mCallbacks->cameraDeviceStatusChange(deviceNamePair.second, status); |
| 101 | } |
| 102 | } else if (ret == -EBUSY || ret == -EUSERS) { |
| 103 | // Looks like this provider instance is not initialized during |
| 104 | // system startup and there are other camera users already. |
| 105 | // Not a good sign but not fatal. |
| 106 | ALOGW("%s: open_legacy try failed!", __FUNCTION__); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
Guennadi Liakhovetski | eca1d45 | 2017-12-07 10:59:35 +0100 | [diff] [blame] | 111 | void CameraProvider::removeDeviceNames(int camera_id) |
| 112 | { |
| 113 | std::string cameraIdStr = std::to_string(camera_id); |
| 114 | |
| 115 | mCameraIds.remove(cameraIdStr); |
| 116 | |
| 117 | int deviceVersion = mModule->getDeviceVersion(camera_id); |
| 118 | auto deviceNamePair = std::make_pair(cameraIdStr, |
| 119 | getHidlDeviceName(cameraIdStr, deviceVersion)); |
| 120 | mCameraDeviceNames.remove(deviceNamePair); |
| 121 | mCallbacks->cameraDeviceStatusChange(deviceNamePair.second, CameraDeviceStatus::NOT_PRESENT); |
| 122 | if (deviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 && |
| 123 | mModule->isOpenLegacyDefined() && mOpenLegacySupported[cameraIdStr]) { |
| 124 | |
| 125 | deviceNamePair = std::make_pair(cameraIdStr, |
| 126 | getHidlDeviceName(cameraIdStr, CAMERA_DEVICE_API_VERSION_1_0)); |
| 127 | mCameraDeviceNames.remove(deviceNamePair); |
| 128 | mCallbacks->cameraDeviceStatusChange(deviceNamePair.second, |
| 129 | CameraDeviceStatus::NOT_PRESENT); |
| 130 | } |
| 131 | |
| 132 | mModule->removeCamera(camera_id); |
| 133 | } |
| 134 | |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 135 | /** |
| 136 | * static callback forwarding methods from HAL to instance |
| 137 | */ |
| 138 | void CameraProvider::sCameraDeviceStatusChange( |
| 139 | const struct camera_module_callbacks* callbacks, |
| 140 | int camera_id, |
| 141 | int new_status) { |
Yin-Chia Yeh | 6dc9b53 | 2017-02-09 18:43:35 -0800 | [diff] [blame] | 142 | CameraProvider* cp = const_cast<CameraProvider*>( |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 143 | static_cast<const CameraProvider*>(callbacks)); |
Guennadi Liakhovetski | 7b7ede7 | 2017-11-28 09:28:56 +0100 | [diff] [blame] | 144 | bool found = false; |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 145 | |
| 146 | if (cp == nullptr) { |
| 147 | ALOGE("%s: callback ops is null", __FUNCTION__); |
| 148 | return; |
| 149 | } |
| 150 | |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 151 | Mutex::Autolock _l(cp->mCbLock); |
| 152 | char cameraId[kMaxCameraIdLen]; |
| 153 | snprintf(cameraId, sizeof(cameraId), "%d", camera_id); |
| 154 | std::string cameraIdStr(cameraId); |
| 155 | cp->mCameraStatusMap[cameraIdStr] = (camera_device_status_t) new_status; |
| 156 | if (cp->mCallbacks != nullptr) { |
| 157 | CameraDeviceStatus status = (CameraDeviceStatus) new_status; |
| 158 | for (auto const& deviceNamePair : cp->mCameraDeviceNames) { |
| 159 | if (cameraIdStr.compare(deviceNamePair.first) == 0) { |
| 160 | cp->mCallbacks->cameraDeviceStatusChange( |
| 161 | deviceNamePair.second, status); |
Guennadi Liakhovetski | 7b7ede7 | 2017-11-28 09:28:56 +0100 | [diff] [blame] | 162 | found = true; |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 163 | } |
| 164 | } |
Guennadi Liakhovetski | 7b7ede7 | 2017-11-28 09:28:56 +0100 | [diff] [blame] | 165 | |
Guennadi Liakhovetski | eca1d45 | 2017-12-07 10:59:35 +0100 | [diff] [blame] | 166 | switch (status) { |
| 167 | case CameraDeviceStatus::PRESENT: |
| 168 | case CameraDeviceStatus::ENUMERATING: |
| 169 | if (!found) { |
| 170 | cp->addDeviceNames(camera_id, status, true); |
| 171 | } |
| 172 | break; |
| 173 | case CameraDeviceStatus::NOT_PRESENT: |
| 174 | if (found) { |
| 175 | cp->removeDeviceNames(camera_id); |
| 176 | } |
Guennadi Liakhovetski | 7b7ede7 | 2017-11-28 09:28:56 +0100 | [diff] [blame] | 177 | } |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 178 | } |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void CameraProvider::sTorchModeStatusChange( |
| 182 | const struct camera_module_callbacks* callbacks, |
| 183 | const char* camera_id, |
| 184 | int new_status) { |
Yin-Chia Yeh | 6dc9b53 | 2017-02-09 18:43:35 -0800 | [diff] [blame] | 185 | CameraProvider* cp = const_cast<CameraProvider*>( |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 186 | static_cast<const CameraProvider*>(callbacks)); |
| 187 | |
| 188 | if (cp == nullptr) { |
| 189 | ALOGE("%s: callback ops is null", __FUNCTION__); |
| 190 | return; |
| 191 | } |
| 192 | |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 193 | Mutex::Autolock _l(cp->mCbLock); |
| 194 | if (cp->mCallbacks != nullptr) { |
| 195 | std::string cameraIdStr(camera_id); |
| 196 | TorchModeStatus status = (TorchModeStatus) new_status; |
| 197 | for (auto const& deviceNamePair : cp->mCameraDeviceNames) { |
Yin-Chia Yeh | 9c6dbd5 | 2016-12-22 14:55:02 -0800 | [diff] [blame] | 198 | if (cameraIdStr.compare(deviceNamePair.first) == 0) { |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 199 | cp->mCallbacks->torchModeStatusChange( |
| 200 | deviceNamePair.second, status); |
| 201 | } |
| 202 | } |
| 203 | } |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | Status CameraProvider::getHidlStatus(int status) { |
| 207 | switch (status) { |
| 208 | case 0: return Status::OK; |
| 209 | case -ENODEV: return Status::INTERNAL_ERROR; |
| 210 | case -EINVAL: return Status::ILLEGAL_ARGUMENT; |
| 211 | default: |
| 212 | ALOGE("%s: unknown HAL status code %d", __FUNCTION__, status); |
| 213 | return Status::INTERNAL_ERROR; |
| 214 | } |
| 215 | } |
| 216 | |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 217 | std::string CameraProvider::getLegacyCameraId(const hidl_string& deviceName) { |
Andreas Gampe | 0b171f1 | 2017-04-04 20:02:25 -0700 | [diff] [blame] | 218 | std::string cameraId; |
| 219 | matchDeviceName(deviceName, nullptr, &cameraId); |
| 220 | return cameraId; |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | int CameraProvider::getCameraDeviceVersion(const hidl_string& deviceName) { |
Andreas Gampe | 0b171f1 | 2017-04-04 20:02:25 -0700 | [diff] [blame] | 224 | std::string deviceVersion; |
| 225 | bool match = matchDeviceName(deviceName, &deviceVersion, nullptr); |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 226 | if (!match) { |
| 227 | return -1; |
| 228 | } |
Eino-Ville Talvala | 50fe430 | 2017-08-22 16:15:09 -0700 | [diff] [blame] | 229 | if (deviceVersion == kHAL3_3) { |
| 230 | return CAMERA_DEVICE_API_VERSION_3_3; |
| 231 | } else if (deviceVersion == kHAL3_2) { |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 232 | return CAMERA_DEVICE_API_VERSION_3_2; |
Andreas Gampe | 0b171f1 | 2017-04-04 20:02:25 -0700 | [diff] [blame] | 233 | } else if (deviceVersion == kHAL1_0) { |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 234 | return CAMERA_DEVICE_API_VERSION_1_0; |
| 235 | } |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | std::string CameraProvider::getHidlDeviceName( |
| 240 | std::string cameraId, int deviceVersion) { |
| 241 | // Maybe consider create a version check method and SortedVec to speed up? |
| 242 | if (deviceVersion != CAMERA_DEVICE_API_VERSION_1_0 && |
| 243 | deviceVersion != CAMERA_DEVICE_API_VERSION_3_2 && |
| 244 | deviceVersion != CAMERA_DEVICE_API_VERSION_3_3 && |
Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 245 | deviceVersion != CAMERA_DEVICE_API_VERSION_3_4 && |
| 246 | deviceVersion != CAMERA_DEVICE_API_VERSION_3_5) { |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 247 | return hidl_string(""); |
| 248 | } |
Eino-Ville Talvala | 50fe430 | 2017-08-22 16:15:09 -0700 | [diff] [blame] | 249 | bool isV1 = deviceVersion == CAMERA_DEVICE_API_VERSION_1_0; |
| 250 | int versionMajor = isV1 ? 1 : 3; |
| 251 | int versionMinor = isV1 ? 0 : mPreferredHal3MinorVersion; |
Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 252 | if (deviceVersion == CAMERA_DEVICE_API_VERSION_3_5) { |
| 253 | versionMinor = 4; |
| 254 | } |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 255 | char deviceName[kMaxCameraDeviceNameLen]; |
Eino-Ville Talvala | 50fe430 | 2017-08-22 16:15:09 -0700 | [diff] [blame] | 256 | snprintf(deviceName, sizeof(deviceName), "device@%d.%d/legacy/%s", |
| 257 | versionMajor, versionMinor, cameraId.c_str()); |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 258 | return deviceName; |
| 259 | } |
| 260 | |
| 261 | CameraProvider::CameraProvider() : |
| 262 | camera_module_callbacks_t({sCameraDeviceStatusChange, |
| 263 | sTorchModeStatusChange}) { |
| 264 | mInitFailed = initialize(); |
| 265 | } |
| 266 | |
| 267 | CameraProvider::~CameraProvider() {} |
| 268 | |
| 269 | bool CameraProvider::initialize() { |
| 270 | camera_module_t *rawModule; |
| 271 | int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID, |
| 272 | (const hw_module_t **)&rawModule); |
| 273 | if (err < 0) { |
| 274 | ALOGE("Could not load camera HAL module: %d (%s)", err, strerror(-err)); |
| 275 | return true; |
| 276 | } |
| 277 | |
| 278 | mModule = new CameraModule(rawModule); |
| 279 | err = mModule->init(); |
| 280 | if (err != OK) { |
Yin-Chia Yeh | fca2e74 | 2017-01-31 16:00:20 -0800 | [diff] [blame] | 281 | ALOGE("Could not initialize camera HAL module: %d (%s)", err, strerror(-err)); |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 282 | mModule.clear(); |
| 283 | return true; |
| 284 | } |
| 285 | ALOGI("Loaded \"%s\" camera module", mModule->getModuleName()); |
| 286 | |
Shuzhen Wang | efb7bfa | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 287 | // Setup vendor tags here so HAL can setup vendor keys in camera characteristics |
| 288 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
| 289 | if (!setUpVendorTags()) { |
| 290 | ALOGE("%s: Vendor tag setup failed, will not be available.", __FUNCTION__); |
| 291 | } |
| 292 | |
Yin-Chia Yeh | fca2e74 | 2017-01-31 16:00:20 -0800 | [diff] [blame] | 293 | // Setup callback now because we are going to try openLegacy next |
| 294 | err = mModule->setCallbacks(this); |
| 295 | if (err != OK) { |
| 296 | ALOGE("Could not set camera module callback: %d (%s)", err, strerror(-err)); |
| 297 | mModule.clear(); |
| 298 | return true; |
| 299 | } |
| 300 | |
Eino-Ville Talvala | 50fe430 | 2017-08-22 16:15:09 -0700 | [diff] [blame] | 301 | mPreferredHal3MinorVersion = property_get_int32("ro.camera.wrapper.hal3TrebleMinorVersion", 3); |
| 302 | ALOGV("Preferred HAL 3 minor version is %d", mPreferredHal3MinorVersion); |
| 303 | switch(mPreferredHal3MinorVersion) { |
| 304 | case 2: |
| 305 | case 3: |
| 306 | // OK |
| 307 | break; |
| 308 | default: |
| 309 | ALOGW("Unknown minor camera device HAL version %d in property " |
Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 310 | "'camera.wrapper.hal3TrebleMinorVersion', defaulting to 3", |
| 311 | mPreferredHal3MinorVersion); |
Eino-Ville Talvala | 50fe430 | 2017-08-22 16:15:09 -0700 | [diff] [blame] | 312 | mPreferredHal3MinorVersion = 3; |
| 313 | } |
| 314 | |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 315 | mNumberOfLegacyCameras = mModule->getNumberOfCameras(); |
| 316 | for (int i = 0; i < mNumberOfLegacyCameras; i++) { |
Emilian Peev | c9ded51 | 2017-04-10 16:12:55 +0100 | [diff] [blame] | 317 | struct camera_info info; |
| 318 | auto rc = mModule->getCameraInfo(i, &info); |
| 319 | if (rc != NO_ERROR) { |
| 320 | ALOGE("%s: Camera info query failed!", __func__); |
| 321 | mModule.clear(); |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | if (checkCameraVersion(i, info) != OK) { |
| 326 | ALOGE("%s: Camera version check failed!", __func__); |
| 327 | mModule.clear(); |
| 328 | return true; |
| 329 | } |
| 330 | |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 331 | char cameraId[kMaxCameraIdLen]; |
| 332 | snprintf(cameraId, sizeof(cameraId), "%d", i); |
| 333 | std::string cameraIdStr(cameraId); |
| 334 | mCameraStatusMap[cameraIdStr] = CAMERA_DEVICE_STATUS_PRESENT; |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 335 | |
Guennadi Liakhovetski | 7b7ede7 | 2017-11-28 09:28:56 +0100 | [diff] [blame] | 336 | addDeviceNames(i); |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 337 | } |
| 338 | |
Eino-Ville Talvala | 0f5eb83 | 2017-02-09 19:45:31 -0800 | [diff] [blame] | 339 | return false; // mInitFailed |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Emilian Peev | c9ded51 | 2017-04-10 16:12:55 +0100 | [diff] [blame] | 342 | /** |
| 343 | * Check that the device HAL version is still in supported. |
| 344 | */ |
| 345 | int CameraProvider::checkCameraVersion(int id, camera_info info) { |
| 346 | if (mModule == nullptr) { |
| 347 | return NO_INIT; |
| 348 | } |
| 349 | |
| 350 | // device_version undefined in CAMERA_MODULE_API_VERSION_1_0, |
| 351 | // All CAMERA_MODULE_API_VERSION_1_0 devices are backward-compatible |
| 352 | if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) { |
| 353 | // Verify the device version is in the supported range |
| 354 | switch (info.device_version) { |
| 355 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 356 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 357 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 358 | case CAMERA_DEVICE_API_VERSION_3_4: |
Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 359 | case CAMERA_DEVICE_API_VERSION_3_5: |
Emilian Peev | c9ded51 | 2017-04-10 16:12:55 +0100 | [diff] [blame] | 360 | // in support |
| 361 | break; |
| 362 | case CAMERA_DEVICE_API_VERSION_2_0: |
| 363 | case CAMERA_DEVICE_API_VERSION_2_1: |
| 364 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 365 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 366 | // no longer supported |
| 367 | default: |
| 368 | ALOGE("%s: Device %d has HAL version %x, which is not supported", |
| 369 | __FUNCTION__, id, info.device_version); |
| 370 | return NO_INIT; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | return OK; |
| 375 | } |
| 376 | |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 377 | bool CameraProvider::setUpVendorTags() { |
| 378 | ATRACE_CALL(); |
| 379 | vendor_tag_ops_t vOps = vendor_tag_ops_t(); |
| 380 | |
| 381 | // Check if vendor operations have been implemented |
| 382 | if (!mModule->isVendorTagDefined()) { |
| 383 | ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__); |
Eino-Ville Talvala | 0f5eb83 | 2017-02-09 19:45:31 -0800 | [diff] [blame] | 384 | return true; |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | mModule->getVendorTagOps(&vOps); |
| 388 | |
| 389 | // Ensure all vendor operations are present |
| 390 | if (vOps.get_tag_count == nullptr || vOps.get_all_tags == nullptr || |
| 391 | vOps.get_section_name == nullptr || vOps.get_tag_name == nullptr || |
| 392 | vOps.get_tag_type == nullptr) { |
| 393 | ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions." |
| 394 | , __FUNCTION__); |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | // Read all vendor tag definitions into a descriptor |
| 399 | sp<VendorTagDescriptor> desc; |
| 400 | status_t res; |
| 401 | if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc)) |
| 402 | != OK) { |
| 403 | ALOGE("%s: Could not generate descriptor from vendor tag operations," |
| 404 | "received error %s (%d). Camera clients will not be able to use" |
| 405 | "vendor tags", __FUNCTION__, strerror(res), res); |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | // Set the global descriptor to use with camera metadata |
| 410 | VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc); |
| 411 | const SortedVector<String8>* sectionNames = desc->getAllSectionNames(); |
| 412 | size_t numSections = sectionNames->size(); |
| 413 | std::vector<std::vector<VendorTag>> tagsBySection(numSections); |
| 414 | int tagCount = desc->getTagCount(); |
| 415 | std::vector<uint32_t> tags(tagCount); |
| 416 | desc->getTagArray(tags.data()); |
| 417 | for (int i = 0; i < tagCount; i++) { |
| 418 | VendorTag vt; |
| 419 | vt.tagId = tags[i]; |
| 420 | vt.tagName = desc->getTagName(tags[i]); |
| 421 | vt.tagType = (CameraMetadataType) desc->getTagType(tags[i]); |
| 422 | ssize_t sectionIdx = desc->getSectionIndex(tags[i]); |
| 423 | tagsBySection[sectionIdx].push_back(vt); |
| 424 | } |
| 425 | mVendorTagSections.resize(numSections); |
| 426 | for (size_t s = 0; s < numSections; s++) { |
| 427 | mVendorTagSections[s].sectionName = (*sectionNames)[s].string(); |
| 428 | mVendorTagSections[s].tags = tagsBySection[s]; |
| 429 | } |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | // Methods from ::android::hardware::camera::provider::V2_4::ICameraProvider follow. |
| 434 | Return<Status> CameraProvider::setCallback(const sp<ICameraProviderCallback>& callback) { |
Yin-Chia Yeh | fca2e74 | 2017-01-31 16:00:20 -0800 | [diff] [blame] | 435 | Mutex::Autolock _l(mCbLock); |
| 436 | mCallbacks = callback; |
| 437 | return Status::OK; |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | Return<void> CameraProvider::getVendorTags(getVendorTags_cb _hidl_cb) { |
| 441 | _hidl_cb(Status::OK, mVendorTagSections); |
| 442 | return Void(); |
| 443 | } |
| 444 | |
| 445 | Return<void> CameraProvider::getCameraIdList(getCameraIdList_cb _hidl_cb) { |
| 446 | std::vector<hidl_string> deviceNameList; |
| 447 | for (auto const& deviceNamePair : mCameraDeviceNames) { |
| 448 | if (mCameraStatusMap[deviceNamePair.first] == CAMERA_DEVICE_STATUS_PRESENT) { |
| 449 | deviceNameList.push_back(deviceNamePair.second); |
| 450 | } |
| 451 | } |
| 452 | hidl_vec<hidl_string> hidlDeviceNameList(deviceNameList); |
Yin-Chia Yeh | 9c6dbd5 | 2016-12-22 14:55:02 -0800 | [diff] [blame] | 453 | _hidl_cb(Status::OK, hidlDeviceNameList); |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 454 | return Void(); |
| 455 | } |
| 456 | |
| 457 | Return<void> CameraProvider::isSetTorchModeSupported(isSetTorchModeSupported_cb _hidl_cb) { |
| 458 | bool support = mModule->isSetTorchModeSupported(); |
| 459 | _hidl_cb (Status::OK, support); |
| 460 | return Void(); |
| 461 | } |
| 462 | |
Yin-Chia Yeh | 9c6dbd5 | 2016-12-22 14:55:02 -0800 | [diff] [blame] | 463 | Return<void> CameraProvider::getCameraDeviceInterface_V1_x( |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 464 | const hidl_string& cameraDeviceName, getCameraDeviceInterface_V1_x_cb _hidl_cb) { |
Andreas Gampe | 0b171f1 | 2017-04-04 20:02:25 -0700 | [diff] [blame] | 465 | std::string cameraId, deviceVersion; |
| 466 | bool match = matchDeviceName(cameraDeviceName, &deviceVersion, &cameraId); |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 467 | if (!match) { |
| 468 | _hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr); |
| 469 | return Void(); |
| 470 | } |
| 471 | |
Yin-Chia Yeh | 248ed70 | 2017-01-23 17:27:26 -0800 | [diff] [blame] | 472 | std::string deviceName(cameraDeviceName.c_str()); |
| 473 | ssize_t index = mCameraDeviceNames.indexOf(std::make_pair(cameraId, deviceName)); |
| 474 | if (index == NAME_NOT_FOUND) { // Either an illegal name or a device version mismatch |
| 475 | Status status = Status::OK; |
| 476 | ssize_t idx = mCameraIds.indexOf(cameraId); |
| 477 | if (idx == NAME_NOT_FOUND) { |
| 478 | ALOGE("%s: cannot find camera %s!", __FUNCTION__, cameraId.c_str()); |
| 479 | status = Status::ILLEGAL_ARGUMENT; |
| 480 | } else { // invalid version |
| 481 | ALOGE("%s: camera device %s does not support version %s!", |
| 482 | __FUNCTION__, cameraId.c_str(), deviceVersion.c_str()); |
| 483 | status = Status::OPERATION_NOT_SUPPORTED; |
| 484 | } |
| 485 | _hidl_cb(status, nullptr); |
| 486 | return Void(); |
| 487 | } |
| 488 | |
| 489 | if (mCameraStatusMap.count(cameraId) == 0 || |
| 490 | mCameraStatusMap[cameraId] != CAMERA_DEVICE_STATUS_PRESENT) { |
| 491 | _hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr); |
| 492 | return Void(); |
| 493 | } |
| 494 | |
| 495 | sp<android::hardware::camera::device::V1_0::implementation::CameraDevice> device = |
| 496 | new android::hardware::camera::device::V1_0::implementation::CameraDevice( |
| 497 | mModule, cameraId, mCameraDeviceNames); |
| 498 | |
| 499 | if (device == nullptr) { |
| 500 | ALOGE("%s: cannot allocate camera device for id %s", __FUNCTION__, cameraId.c_str()); |
| 501 | _hidl_cb(Status::INTERNAL_ERROR, nullptr); |
| 502 | return Void(); |
| 503 | } |
| 504 | |
| 505 | if (device->isInitFailed()) { |
| 506 | ALOGE("%s: camera device %s init failed!", __FUNCTION__, cameraId.c_str()); |
| 507 | device = nullptr; |
| 508 | _hidl_cb(Status::INTERNAL_ERROR, nullptr); |
| 509 | return Void(); |
| 510 | } |
| 511 | |
| 512 | _hidl_cb (Status::OK, device); |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 513 | return Void(); |
| 514 | } |
| 515 | |
Yin-Chia Yeh | 9c6dbd5 | 2016-12-22 14:55:02 -0800 | [diff] [blame] | 516 | Return<void> CameraProvider::getCameraDeviceInterface_V3_x( |
| 517 | const hidl_string& cameraDeviceName, getCameraDeviceInterface_V3_x_cb _hidl_cb) { |
Andreas Gampe | 0b171f1 | 2017-04-04 20:02:25 -0700 | [diff] [blame] | 518 | std::string cameraId, deviceVersion; |
| 519 | bool match = matchDeviceName(cameraDeviceName, &deviceVersion, &cameraId); |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 520 | if (!match) { |
| 521 | _hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr); |
| 522 | return Void(); |
| 523 | } |
| 524 | |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 525 | std::string deviceName(cameraDeviceName.c_str()); |
| 526 | ssize_t index = mCameraDeviceNames.indexOf(std::make_pair(cameraId, deviceName)); |
| 527 | if (index == NAME_NOT_FOUND) { // Either an illegal name or a device version mismatch |
| 528 | Status status = Status::OK; |
| 529 | ssize_t idx = mCameraIds.indexOf(cameraId); |
| 530 | if (idx == NAME_NOT_FOUND) { |
Yin-Chia Yeh | 9c6dbd5 | 2016-12-22 14:55:02 -0800 | [diff] [blame] | 531 | ALOGE("%s: cannot find camera %s!", __FUNCTION__, cameraId.c_str()); |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 532 | status = Status::ILLEGAL_ARGUMENT; |
| 533 | } else { // invalid version |
Yin-Chia Yeh | 9c6dbd5 | 2016-12-22 14:55:02 -0800 | [diff] [blame] | 534 | ALOGE("%s: camera device %s does not support version %s!", |
| 535 | __FUNCTION__, cameraId.c_str(), deviceVersion.c_str()); |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 536 | status = Status::OPERATION_NOT_SUPPORTED; |
| 537 | } |
| 538 | _hidl_cb(status, nullptr); |
| 539 | return Void(); |
| 540 | } |
| 541 | |
| 542 | if (mCameraStatusMap.count(cameraId) == 0 || |
| 543 | mCameraStatusMap[cameraId] != CAMERA_DEVICE_STATUS_PRESENT) { |
| 544 | _hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr); |
| 545 | return Void(); |
| 546 | } |
| 547 | |
Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 548 | sp<android::hardware::camera::device::V3_2::ICameraDevice> device; |
| 549 | if (deviceVersion == kHAL3_4) { |
| 550 | ALOGV("Constructing v3.4 camera device"); |
| 551 | sp<android::hardware::camera::device::V3_2::implementation::CameraDevice> deviceImpl = |
| 552 | new android::hardware::camera::device::V3_4::implementation::CameraDevice( |
| 553 | mModule, cameraId, mCameraDeviceNames); |
| 554 | if (deviceImpl == nullptr || deviceImpl->isInitFailed()) { |
| 555 | ALOGE("%s: camera device %s init failed!", __FUNCTION__, cameraId.c_str()); |
| 556 | device = nullptr; |
| 557 | _hidl_cb(Status::INTERNAL_ERROR, nullptr); |
| 558 | return Void(); |
| 559 | } |
| 560 | |
| 561 | device = deviceImpl; |
| 562 | _hidl_cb (Status::OK, device); |
| 563 | return Void(); |
| 564 | } |
| 565 | |
Eino-Ville Talvala | 50fe430 | 2017-08-22 16:15:09 -0700 | [diff] [blame] | 566 | // Since some Treble HAL revisions can map to the same legacy HAL version(s), we default |
| 567 | // to the newest possible Treble HAL revision, but allow for override if needed via |
| 568 | // system property. |
Eino-Ville Talvala | 50fe430 | 2017-08-22 16:15:09 -0700 | [diff] [blame] | 569 | switch (mPreferredHal3MinorVersion) { |
| 570 | case 2: { // Map legacy camera device v3 HAL to Treble camera device HAL v3.2 |
| 571 | ALOGV("Constructing v3.2 camera device"); |
| 572 | sp<android::hardware::camera::device::V3_2::implementation::CameraDevice> deviceImpl = |
| 573 | new android::hardware::camera::device::V3_2::implementation::CameraDevice( |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 574 | mModule, cameraId, mCameraDeviceNames); |
Eino-Ville Talvala | 50fe430 | 2017-08-22 16:15:09 -0700 | [diff] [blame] | 575 | if (deviceImpl == nullptr || deviceImpl->isInitFailed()) { |
| 576 | ALOGE("%s: camera device %s init failed!", __FUNCTION__, cameraId.c_str()); |
| 577 | device = nullptr; |
| 578 | _hidl_cb(Status::INTERNAL_ERROR, nullptr); |
| 579 | return Void(); |
| 580 | } |
| 581 | device = deviceImpl; |
| 582 | break; |
| 583 | } |
| 584 | case 3: { // Map legacy camera device v3 HAL to Treble camera device HAL v3.3 |
| 585 | ALOGV("Constructing v3.3 camera device"); |
| 586 | sp<android::hardware::camera::device::V3_2::implementation::CameraDevice> deviceImpl = |
| 587 | new android::hardware::camera::device::V3_3::implementation::CameraDevice( |
| 588 | mModule, cameraId, mCameraDeviceNames); |
| 589 | if (deviceImpl == nullptr || deviceImpl->isInitFailed()) { |
| 590 | ALOGE("%s: camera device %s init failed!", __FUNCTION__, cameraId.c_str()); |
| 591 | device = nullptr; |
| 592 | _hidl_cb(Status::INTERNAL_ERROR, nullptr); |
| 593 | return Void(); |
| 594 | } |
| 595 | device = deviceImpl; |
| 596 | break; |
| 597 | } |
| 598 | default: |
| 599 | ALOGE("%s: Unknown HAL minor version %d!", __FUNCTION__, mPreferredHal3MinorVersion); |
| 600 | device = nullptr; |
| 601 | _hidl_cb(Status::INTERNAL_ERROR, nullptr); |
| 602 | return Void(); |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 603 | } |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 604 | _hidl_cb (Status::OK, device); |
| 605 | return Void(); |
| 606 | } |
| 607 | |
| 608 | ICameraProvider* HIDL_FETCH_ICameraProvider(const char* name) { |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 609 | if (strcmp(name, kLegacyProviderName) == 0) { |
| 610 | CameraProvider* provider = new CameraProvider(); |
| 611 | if (provider == nullptr) { |
| 612 | ALOGE("%s: cannot allocate camera provider!", __FUNCTION__); |
| 613 | return nullptr; |
| 614 | } |
| 615 | if (provider->isInitFailed()) { |
| 616 | ALOGE("%s: camera provider init failed!", __FUNCTION__); |
| 617 | delete provider; |
| 618 | return nullptr; |
| 619 | } |
| 620 | return provider; |
| 621 | } else if (strcmp(name, kExternalProviderName) == 0) { |
| 622 | ExternalCameraProvider* provider = new ExternalCameraProvider(); |
| 623 | return provider; |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 624 | } |
Yin-Chia Yeh | 1903059 | 2017-10-19 17:30:11 -0700 | [diff] [blame] | 625 | ALOGE("%s: unknown instance name: %s", __FUNCTION__, name); |
| 626 | return nullptr; |
Yin-Chia Yeh | f906b3b | 2016-12-14 19:13:15 -0800 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | } // namespace implementation |
| 630 | } // namespace V2_4 |
| 631 | } // namespace provider |
| 632 | } // namespace camera |
| 633 | } // namespace hardware |
| 634 | } // namespace android |