Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -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 "CameraProviderManager" |
| 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | #include "CameraProviderManager.h" |
| 22 | |
Yin-Chia Yeh | 6540509 | 2017-01-13 15:42:28 -0800 | [diff] [blame] | 23 | #include <chrono> |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 24 | #include <inttypes.h> |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 25 | #include <hidl/ServiceManagement.h> |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | using namespace ::android::hardware::camera; |
| 30 | using namespace ::android::hardware::camera::common::V1_0; |
| 31 | |
| 32 | namespace { |
| 33 | // Hardcoded name for the passthrough HAL implementation, since it can't be discovered via the |
| 34 | // service manager |
| 35 | const std::string kLegacyProviderName("legacy/0"); |
| 36 | |
| 37 | // Slash-separated list of provider types to consider for use via the old camera API |
| 38 | const std::string kStandardProviderTypes("internal/legacy"); |
| 39 | |
| 40 | } // anonymous namespace |
| 41 | |
| 42 | CameraProviderManager::HardwareServiceInteractionProxy |
| 43 | CameraProviderManager::sHardwareServiceInteractionProxy{}; |
| 44 | |
| 45 | CameraProviderManager::~CameraProviderManager() { |
| 46 | } |
| 47 | |
| 48 | status_t CameraProviderManager::initialize(wp<CameraProviderManager::StatusListener> listener, |
| 49 | ServiceInteractionProxy* proxy) { |
Yin-Chia Yeh | 4c5b1c7 | 2017-01-31 13:20:56 -0800 | [diff] [blame] | 50 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 51 | if (proxy == nullptr) { |
| 52 | ALOGE("%s: No valid service interaction proxy provided", __FUNCTION__); |
| 53 | return BAD_VALUE; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 54 | } |
Yin-Chia Yeh | 4c5b1c7 | 2017-01-31 13:20:56 -0800 | [diff] [blame] | 55 | mListener = listener; |
| 56 | mServiceProxy = proxy; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 57 | |
Yin-Chia Yeh | 4c5b1c7 | 2017-01-31 13:20:56 -0800 | [diff] [blame] | 58 | // Registering will trigger notifications for all already-known providers |
| 59 | bool success = mServiceProxy->registerForNotifications( |
| 60 | /* instance name, empty means no filter */ "", |
| 61 | this); |
| 62 | if (!success) { |
| 63 | ALOGE("%s: Unable to register with hardware service manager for notifications " |
| 64 | "about camera providers", __FUNCTION__); |
| 65 | return INVALID_OPERATION; |
Yin-Chia Yeh | 6540509 | 2017-01-13 15:42:28 -0800 | [diff] [blame] | 66 | } |
Emilian Peev | 5d7e515 | 2017-02-22 15:37:48 +0000 | [diff] [blame] | 67 | |
Eino-Ville Talvala | 6566536 | 2017-02-24 13:07:56 -0800 | [diff] [blame] | 68 | // See if there's a passthrough HAL, but let's not complain if there's not |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 69 | addProviderLocked(kLegacyProviderName, /*expected*/ false); |
Eino-Ville Talvala | 6566536 | 2017-02-24 13:07:56 -0800 | [diff] [blame] | 70 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 71 | return OK; |
| 72 | } |
| 73 | |
| 74 | int CameraProviderManager::getCameraCount() const { |
| 75 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 76 | int count = 0; |
| 77 | for (auto& provider : mProviders) { |
Yin-Chia Yeh | e8e9e19 | 2017-03-16 15:23:51 -0700 | [diff] [blame] | 78 | count += provider->mUniqueDeviceCount; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 79 | } |
| 80 | return count; |
| 81 | } |
| 82 | |
| 83 | int CameraProviderManager::getStandardCameraCount() const { |
| 84 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 85 | int count = 0; |
| 86 | for (auto& provider : mProviders) { |
| 87 | if (kStandardProviderTypes.find(provider->getType()) != std::string::npos) { |
Yin-Chia Yeh | e8e9e19 | 2017-03-16 15:23:51 -0700 | [diff] [blame] | 88 | count += provider->mUniqueDeviceCount; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | return count; |
| 92 | } |
| 93 | |
| 94 | std::vector<std::string> CameraProviderManager::getCameraDeviceIds() const { |
| 95 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 96 | std::vector<std::string> deviceIds; |
| 97 | for (auto& provider : mProviders) { |
Yin-Chia Yeh | dc3134e | 2017-03-23 15:26:59 -0700 | [diff] [blame] | 98 | for (auto& id : provider->mUniqueCameraIds) { |
| 99 | deviceIds.push_back(id); |
| 100 | } |
| 101 | } |
| 102 | return deviceIds; |
| 103 | } |
| 104 | |
| 105 | std::vector<std::string> CameraProviderManager::getStandardCameraDeviceIds() const { |
| 106 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 107 | std::vector<std::string> deviceIds; |
| 108 | for (auto& provider : mProviders) { |
| 109 | if (kStandardProviderTypes.find(provider->getType()) != std::string::npos) { |
| 110 | for (auto& id : provider->mUniqueCameraIds) { |
| 111 | deviceIds.push_back(id); |
| 112 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | return deviceIds; |
| 116 | } |
| 117 | |
| 118 | bool CameraProviderManager::isValidDevice(const std::string &id, uint16_t majorVersion) const { |
| 119 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 120 | return isValidDeviceLocked(id, majorVersion); |
| 121 | } |
| 122 | |
| 123 | bool CameraProviderManager::isValidDeviceLocked(const std::string &id, uint16_t majorVersion) const { |
| 124 | for (auto& provider : mProviders) { |
| 125 | for (auto& deviceInfo : provider->mDevices) { |
| 126 | if (deviceInfo->mId == id && deviceInfo->mVersion.get_major() == majorVersion) { |
| 127 | return true; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | bool CameraProviderManager::hasFlashUnit(const std::string &id) const { |
| 135 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 136 | |
| 137 | auto deviceInfo = findDeviceInfoLocked(id); |
| 138 | if (deviceInfo == nullptr) return false; |
| 139 | |
| 140 | return deviceInfo->hasFlashUnit(); |
| 141 | } |
| 142 | |
| 143 | status_t CameraProviderManager::getResourceCost(const std::string &id, |
| 144 | CameraResourceCost* cost) const { |
| 145 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 146 | |
| 147 | auto deviceInfo = findDeviceInfoLocked(id); |
| 148 | if (deviceInfo == nullptr) return NAME_NOT_FOUND; |
| 149 | |
| 150 | *cost = deviceInfo->mResourceCost; |
| 151 | return OK; |
| 152 | } |
| 153 | |
| 154 | status_t CameraProviderManager::getCameraInfo(const std::string &id, |
| 155 | hardware::CameraInfo* info) const { |
| 156 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 157 | |
| 158 | auto deviceInfo = findDeviceInfoLocked(id); |
| 159 | if (deviceInfo == nullptr) return NAME_NOT_FOUND; |
| 160 | |
| 161 | return deviceInfo->getCameraInfo(info); |
| 162 | } |
| 163 | |
| 164 | status_t CameraProviderManager::getCameraCharacteristics(const std::string &id, |
| 165 | CameraMetadata* characteristics) const { |
| 166 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 167 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 168 | auto deviceInfo = findDeviceInfoLocked(id, /*minVersion*/ {3,0}, /*maxVersion*/ {4,0}); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 169 | if (deviceInfo == nullptr) return NAME_NOT_FOUND; |
| 170 | |
| 171 | return deviceInfo->getCameraCharacteristics(characteristics); |
| 172 | } |
| 173 | |
| 174 | status_t CameraProviderManager::getHighestSupportedVersion(const std::string &id, |
| 175 | hardware::hidl_version *v) { |
| 176 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 177 | |
| 178 | hardware::hidl_version maxVersion{0,0}; |
| 179 | bool found = false; |
| 180 | for (auto& provider : mProviders) { |
| 181 | for (auto& deviceInfo : provider->mDevices) { |
| 182 | if (deviceInfo->mId == id) { |
| 183 | if (deviceInfo->mVersion > maxVersion) { |
| 184 | maxVersion = deviceInfo->mVersion; |
| 185 | found = true; |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | if (!found) { |
| 191 | return NAME_NOT_FOUND; |
| 192 | } |
| 193 | *v = maxVersion; |
| 194 | return OK; |
| 195 | } |
| 196 | |
Yin-Chia Yeh | dc3134e | 2017-03-23 15:26:59 -0700 | [diff] [blame] | 197 | bool CameraProviderManager::supportSetTorchMode(const std::string &id) { |
| 198 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 199 | bool support = false; |
| 200 | for (auto& provider : mProviders) { |
| 201 | auto deviceInfo = findDeviceInfoLocked(id); |
| 202 | if (deviceInfo != nullptr) { |
| 203 | provider->mInterface->isSetTorchModeSupported( |
| 204 | [&support](auto status, bool supported) { |
| 205 | if (status == Status::OK) { |
| 206 | support = supported; |
| 207 | } |
| 208 | }); |
| 209 | } |
| 210 | } |
| 211 | return support; |
| 212 | } |
| 213 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 214 | status_t CameraProviderManager::setTorchMode(const std::string &id, bool enabled) { |
| 215 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 216 | |
| 217 | auto deviceInfo = findDeviceInfoLocked(id); |
| 218 | if (deviceInfo == nullptr) return NAME_NOT_FOUND; |
| 219 | |
| 220 | return deviceInfo->setTorchMode(enabled); |
| 221 | } |
| 222 | |
Yin-Chia Yeh | 067428c | 2017-01-13 15:19:24 -0800 | [diff] [blame] | 223 | status_t CameraProviderManager::setUpVendorTags() { |
| 224 | // TODO (b/34275821): support aggregating vendor tags for more than one provider |
| 225 | for (auto& provider : mProviders) { |
| 226 | hardware::hidl_vec<VendorTagSection> vts; |
| 227 | Status status; |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 228 | hardware::Return<void> ret; |
| 229 | ret = provider->mInterface->getVendorTags( |
Yin-Chia Yeh | 067428c | 2017-01-13 15:19:24 -0800 | [diff] [blame] | 230 | [&](auto s, const auto& vendorTagSecs) { |
| 231 | status = s; |
| 232 | if (s == Status::OK) { |
| 233 | vts = vendorTagSecs; |
| 234 | } |
| 235 | }); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 236 | if (!ret.isOk()) { |
| 237 | ALOGE("%s: Transaction error getting vendor tags from provider '%s': %s", |
| 238 | __FUNCTION__, provider->mProviderName.c_str(), ret.description().c_str()); |
| 239 | return DEAD_OBJECT; |
| 240 | } |
Yin-Chia Yeh | 067428c | 2017-01-13 15:19:24 -0800 | [diff] [blame] | 241 | if (status != Status::OK) { |
| 242 | return mapToStatusT(status); |
| 243 | } |
| 244 | |
| 245 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
| 246 | |
| 247 | // Read all vendor tag definitions into a descriptor |
| 248 | sp<VendorTagDescriptor> desc; |
| 249 | status_t res; |
| 250 | if ((res = HidlVendorTagDescriptor::createDescriptorFromHidl(vts, /*out*/desc)) |
| 251 | != OK) { |
| 252 | ALOGE("%s: Could not generate descriptor from vendor tag operations," |
| 253 | "received error %s (%d). Camera clients will not be able to use" |
| 254 | "vendor tags", __FUNCTION__, strerror(res), res); |
| 255 | return res; |
| 256 | } |
| 257 | |
| 258 | // Set the global descriptor to use with camera metadata |
| 259 | VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc); |
| 260 | } |
| 261 | return OK; |
| 262 | } |
| 263 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 264 | status_t CameraProviderManager::openSession(const std::string &id, |
| 265 | const sp<hardware::camera::device::V3_2::ICameraDeviceCallback>& callback, |
| 266 | /*out*/ |
| 267 | sp<hardware::camera::device::V3_2::ICameraDeviceSession> *session) { |
| 268 | |
| 269 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 270 | |
| 271 | auto deviceInfo = findDeviceInfoLocked(id, |
| 272 | /*minVersion*/ {3,0}, /*maxVersion*/ {4,0}); |
| 273 | if (deviceInfo == nullptr) return NAME_NOT_FOUND; |
| 274 | |
| 275 | auto *deviceInfo3 = static_cast<ProviderInfo::DeviceInfo3*>(deviceInfo); |
| 276 | |
| 277 | Status status; |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 278 | hardware::Return<void> ret; |
| 279 | ret = deviceInfo3->mInterface->open(callback, [&status, &session] |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 280 | (Status s, const sp<device::V3_2::ICameraDeviceSession>& cameraSession) { |
| 281 | status = s; |
| 282 | if (status == Status::OK) { |
| 283 | *session = cameraSession; |
| 284 | } |
| 285 | }); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 286 | if (!ret.isOk()) { |
| 287 | ALOGE("%s: Transaction error opening a session for camera device %s: %s", |
| 288 | __FUNCTION__, id.c_str(), ret.description().c_str()); |
| 289 | return DEAD_OBJECT; |
| 290 | } |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 291 | return mapToStatusT(status); |
| 292 | } |
| 293 | |
| 294 | status_t CameraProviderManager::openSession(const std::string &id, |
| 295 | const sp<hardware::camera::device::V1_0::ICameraDeviceCallback>& callback, |
| 296 | /*out*/ |
| 297 | sp<hardware::camera::device::V1_0::ICameraDevice> *session) { |
| 298 | |
| 299 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 300 | |
| 301 | auto deviceInfo = findDeviceInfoLocked(id, |
| 302 | /*minVersion*/ {1,0}, /*maxVersion*/ {2,0}); |
| 303 | if (deviceInfo == nullptr) return NAME_NOT_FOUND; |
| 304 | |
| 305 | auto *deviceInfo1 = static_cast<ProviderInfo::DeviceInfo1*>(deviceInfo); |
| 306 | |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 307 | hardware::Return<Status> status = deviceInfo1->mInterface->open(callback); |
| 308 | if (!status.isOk()) { |
| 309 | ALOGE("%s: Transaction error opening a session for camera device %s: %s", |
| 310 | __FUNCTION__, id.c_str(), status.description().c_str()); |
| 311 | return DEAD_OBJECT; |
| 312 | } |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 313 | if (status == Status::OK) { |
| 314 | *session = deviceInfo1->mInterface; |
| 315 | } |
| 316 | return mapToStatusT(status); |
| 317 | } |
| 318 | |
| 319 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 320 | hardware::Return<void> CameraProviderManager::onRegistration( |
| 321 | const hardware::hidl_string& /*fqName*/, |
| 322 | const hardware::hidl_string& name, |
| 323 | bool /*preexisting*/) { |
| 324 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 325 | |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 326 | addProviderLocked(name); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 327 | return hardware::Return<void>(); |
| 328 | } |
| 329 | |
| 330 | status_t CameraProviderManager::dump(int fd, const Vector<String16>& args) { |
| 331 | std::lock_guard<std::mutex> lock(mInterfaceMutex); |
| 332 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 333 | for (auto& provider : mProviders) { |
| 334 | provider->dump(fd, args); |
| 335 | } |
| 336 | return OK; |
| 337 | } |
| 338 | |
| 339 | CameraProviderManager::ProviderInfo::DeviceInfo* CameraProviderManager::findDeviceInfoLocked( |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 340 | const std::string& id, |
| 341 | hardware::hidl_version minVersion, hardware::hidl_version maxVersion) const { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 342 | for (auto& provider : mProviders) { |
| 343 | for (auto& deviceInfo : provider->mDevices) { |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 344 | if (deviceInfo->mId == id && |
| 345 | minVersion <= deviceInfo->mVersion && maxVersion >= deviceInfo->mVersion) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 346 | return deviceInfo.get(); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | return nullptr; |
| 351 | } |
| 352 | |
| 353 | |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 354 | status_t CameraProviderManager::addProviderLocked(const std::string& newProvider, bool expected) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 355 | for (const auto& providerInfo : mProviders) { |
| 356 | if (providerInfo->mProviderName == newProvider) { |
| 357 | ALOGW("%s: Camera provider HAL with name '%s' already registered", __FUNCTION__, |
| 358 | newProvider.c_str()); |
| 359 | return ALREADY_EXISTS; |
| 360 | } |
| 361 | } |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 362 | |
| 363 | sp<provider::V2_4::ICameraProvider> interface; |
| 364 | interface = mServiceProxy->getService(newProvider); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 365 | |
| 366 | if (interface == nullptr) { |
| 367 | if (expected) { |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 368 | ALOGE("%s: Camera provider HAL '%s' is not actually available", __FUNCTION__, |
| 369 | newProvider.c_str()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 370 | return BAD_VALUE; |
| 371 | } else { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 372 | return OK; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | sp<ProviderInfo> providerInfo = |
| 377 | new ProviderInfo(newProvider, interface, this); |
| 378 | status_t res = providerInfo->initialize(); |
| 379 | if (res != OK) { |
| 380 | return res; |
| 381 | } |
| 382 | |
| 383 | mProviders.push_back(providerInfo); |
| 384 | |
| 385 | return OK; |
| 386 | } |
| 387 | |
| 388 | status_t CameraProviderManager::removeProvider(const std::string& provider) { |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 389 | std::unique_lock<std::mutex> lock(mInterfaceMutex); |
| 390 | std::vector<String8> removedDeviceIds; |
| 391 | status_t res = NAME_NOT_FOUND; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 392 | for (auto it = mProviders.begin(); it != mProviders.end(); it++) { |
| 393 | if ((*it)->mProviderName == provider) { |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 394 | removedDeviceIds.reserve((*it)->mDevices.size()); |
| 395 | for (auto& deviceInfo : (*it)->mDevices) { |
| 396 | removedDeviceIds.push_back(String8(deviceInfo->mId.c_str())); |
| 397 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 398 | mProviders.erase(it); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 399 | res = OK; |
| 400 | break; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 401 | } |
| 402 | } |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 403 | if (res != OK) { |
| 404 | ALOGW("%s: Camera provider HAL with name '%s' is not registered", __FUNCTION__, |
| 405 | provider.c_str()); |
| 406 | } else { |
| 407 | // Inform camera service of loss of presence for all the devices from this provider, |
| 408 | // without lock held for reentrancy |
| 409 | sp<StatusListener> listener = getStatusListener(); |
| 410 | if (listener != nullptr) { |
| 411 | lock.unlock(); |
| 412 | for (auto& id : removedDeviceIds) { |
| 413 | listener->onDeviceStatusChanged(id, CameraDeviceStatus::NOT_PRESENT); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | return res; |
| 418 | } |
| 419 | |
| 420 | sp<CameraProviderManager::StatusListener> CameraProviderManager::getStatusListener() const { |
| 421 | return mListener.promote(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /**** Methods for ProviderInfo ****/ |
| 425 | |
| 426 | |
| 427 | CameraProviderManager::ProviderInfo::ProviderInfo( |
| 428 | const std::string &providerName, |
| 429 | sp<provider::V2_4::ICameraProvider>& interface, |
| 430 | CameraProviderManager *manager) : |
| 431 | mProviderName(providerName), |
| 432 | mInterface(interface), |
| 433 | mManager(manager) { |
| 434 | (void) mManager; |
| 435 | } |
| 436 | |
| 437 | status_t CameraProviderManager::ProviderInfo::initialize() { |
| 438 | status_t res = parseProviderName(mProviderName, &mType, &mId); |
| 439 | if (res != OK) { |
| 440 | ALOGE("%s: Invalid provider name, ignoring", __FUNCTION__); |
| 441 | return BAD_VALUE; |
| 442 | } |
Yin-Chia Yeh | 6540509 | 2017-01-13 15:42:28 -0800 | [diff] [blame] | 443 | ALOGI("Connecting to new camera provider: %s, isRemote? %d", |
| 444 | mProviderName.c_str(), mInterface->isRemote()); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 445 | hardware::Return<Status> status = mInterface->setCallback(this); |
| 446 | if (!status.isOk()) { |
| 447 | ALOGE("%s: Transaction error setting up callbacks with camera provider '%s': %s", |
| 448 | __FUNCTION__, mProviderName.c_str(), status.description().c_str()); |
| 449 | return DEAD_OBJECT; |
| 450 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 451 | if (status != Status::OK) { |
| 452 | ALOGE("%s: Unable to register callbacks with camera provider '%s'", |
| 453 | __FUNCTION__, mProviderName.c_str()); |
| 454 | return mapToStatusT(status); |
| 455 | } |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 456 | |
| 457 | hardware::Return<bool> linked = mInterface->linkToDeath(this, /*cookie*/ mId); |
| 458 | if (!linked.isOk()) { |
| 459 | ALOGE("%s: Transaction error in linking to camera provider '%s' death: %s", |
| 460 | __FUNCTION__, mProviderName.c_str(), linked.description().c_str()); |
| 461 | return DEAD_OBJECT; |
| 462 | } else if (!linked) { |
| 463 | ALOGW("%s: Unable to link to provider '%s' death notifications", |
| 464 | __FUNCTION__, mProviderName.c_str()); |
| 465 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 466 | |
| 467 | // Get initial list of camera devices, if any |
| 468 | std::vector<std::string> devices; |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 469 | hardware::Return<void> ret = mInterface->getCameraIdList([&status, &devices]( |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 470 | Status idStatus, |
| 471 | const hardware::hidl_vec<hardware::hidl_string>& cameraDeviceNames) { |
| 472 | status = idStatus; |
| 473 | if (status == Status::OK) { |
| 474 | for (size_t i = 0; i < cameraDeviceNames.size(); i++) { |
| 475 | devices.push_back(cameraDeviceNames[i]); |
| 476 | } |
| 477 | } }); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 478 | if (!ret.isOk()) { |
| 479 | ALOGE("%s: Transaction error in getting camera ID list from provider '%s': %s", |
| 480 | __FUNCTION__, mProviderName.c_str(), linked.description().c_str()); |
| 481 | return DEAD_OBJECT; |
| 482 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 483 | if (status != Status::OK) { |
| 484 | ALOGE("%s: Unable to query for camera devices from provider '%s'", |
| 485 | __FUNCTION__, mProviderName.c_str()); |
| 486 | return mapToStatusT(status); |
| 487 | } |
| 488 | |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 489 | sp<StatusListener> listener = mManager->getStatusListener(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 490 | for (auto& device : devices) { |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 491 | std::string id; |
| 492 | status_t res = addDevice(device, |
| 493 | hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT, &id); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 494 | if (res != OK) { |
| 495 | ALOGE("%s: Unable to enumerate camera device '%s': %s (%d)", |
| 496 | __FUNCTION__, device.c_str(), strerror(-res), res); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 497 | continue; |
| 498 | } |
| 499 | if (listener != nullptr) { |
| 500 | listener->onDeviceStatusChanged(String8(id.c_str()), CameraDeviceStatus::PRESENT); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
Yin-Chia Yeh | e8e9e19 | 2017-03-16 15:23:51 -0700 | [diff] [blame] | 504 | for (auto& device : mDevices) { |
Yin-Chia Yeh | dc3134e | 2017-03-23 15:26:59 -0700 | [diff] [blame] | 505 | mUniqueCameraIds.insert(device->mId); |
Yin-Chia Yeh | e8e9e19 | 2017-03-16 15:23:51 -0700 | [diff] [blame] | 506 | } |
Yin-Chia Yeh | dc3134e | 2017-03-23 15:26:59 -0700 | [diff] [blame] | 507 | mUniqueDeviceCount = mUniqueCameraIds.size(); |
Yin-Chia Yeh | e8e9e19 | 2017-03-16 15:23:51 -0700 | [diff] [blame] | 508 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 509 | ALOGI("Camera provider %s ready with %zu camera devices", |
| 510 | mProviderName.c_str(), mDevices.size()); |
| 511 | |
| 512 | return OK; |
| 513 | } |
| 514 | |
| 515 | const std::string& CameraProviderManager::ProviderInfo::getType() const { |
| 516 | return mType; |
| 517 | } |
| 518 | |
| 519 | status_t CameraProviderManager::ProviderInfo::addDevice(const std::string& name, |
| 520 | CameraDeviceStatus initialStatus, /*out*/ std::string* parsedId) { |
| 521 | |
| 522 | ALOGI("Enumerating new camera device: %s", name.c_str()); |
| 523 | |
| 524 | uint16_t major, minor; |
| 525 | std::string type, id; |
| 526 | |
| 527 | status_t res = parseDeviceName(name, &major, &minor, &type, &id); |
| 528 | if (res != OK) { |
| 529 | return res; |
| 530 | } |
| 531 | if (type != mType) { |
| 532 | ALOGE("%s: Device type %s does not match provider type %s", __FUNCTION__, |
| 533 | type.c_str(), mType.c_str()); |
| 534 | return BAD_VALUE; |
| 535 | } |
| 536 | if (mManager->isValidDeviceLocked(id, major)) { |
| 537 | ALOGE("%s: Device %s: ID %s is already in use for device major version %d", __FUNCTION__, |
| 538 | name.c_str(), id.c_str(), major); |
| 539 | return BAD_VALUE; |
| 540 | } |
| 541 | |
| 542 | std::unique_ptr<DeviceInfo> deviceInfo; |
| 543 | switch (major) { |
| 544 | case 1: |
| 545 | deviceInfo = initializeDeviceInfo<DeviceInfo1>(name, id, minor); |
| 546 | break; |
| 547 | case 3: |
| 548 | deviceInfo = initializeDeviceInfo<DeviceInfo3>(name, id, minor); |
| 549 | break; |
| 550 | default: |
| 551 | ALOGE("%s: Device %s: Unknown HIDL device HAL major version %d:", __FUNCTION__, |
| 552 | name.c_str(), major); |
| 553 | return BAD_VALUE; |
| 554 | } |
| 555 | if (deviceInfo == nullptr) return BAD_VALUE; |
| 556 | deviceInfo->mStatus = initialStatus; |
| 557 | |
| 558 | mDevices.push_back(std::move(deviceInfo)); |
| 559 | |
| 560 | if (parsedId != nullptr) { |
| 561 | *parsedId = id; |
| 562 | } |
| 563 | return OK; |
| 564 | } |
| 565 | |
| 566 | status_t CameraProviderManager::ProviderInfo::dump(int fd, const Vector<String16>&) const { |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 567 | dprintf(fd, "== Camera Provider HAL %s (v2.4, %s) static info: %zu devices: ==\n", |
| 568 | mProviderName.c_str(), mInterface->isRemote() ? "remote" : "passthrough", |
| 569 | mDevices.size()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 570 | |
| 571 | for (auto& device : mDevices) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 572 | dprintf(fd, "== Camera HAL device %s (v%d.%d) static information: ==\n", device->mName.c_str(), |
| 573 | device->mVersion.get_major(), device->mVersion.get_minor()); |
| 574 | dprintf(fd, " Resource cost: %d\n", device->mResourceCost.resourceCost); |
| 575 | if (device->mResourceCost.conflictingDevices.size() == 0) { |
| 576 | dprintf(fd, " Conflicting devices: None\n"); |
| 577 | } else { |
| 578 | dprintf(fd, " Conflicting devices:\n"); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 579 | for (size_t i = 0; i < device->mResourceCost.conflictingDevices.size(); i++) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 580 | dprintf(fd, " %s\n", |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 581 | device->mResourceCost.conflictingDevices[i].c_str()); |
| 582 | } |
| 583 | } |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 584 | dprintf(fd, " API1 info:\n"); |
| 585 | dprintf(fd, " Has a flash unit: %s\n", |
| 586 | device->hasFlashUnit() ? "true" : "false"); |
| 587 | hardware::CameraInfo info; |
| 588 | status_t res = device->getCameraInfo(&info); |
| 589 | if (res != OK) { |
| 590 | dprintf(fd, " <Error reading camera info: %s (%d)>\n", |
| 591 | strerror(-res), res); |
| 592 | } else { |
| 593 | dprintf(fd, " Facing: %s\n", |
| 594 | info.facing == hardware::CAMERA_FACING_BACK ? "Back" : "Front"); |
| 595 | dprintf(fd, " Orientation: %d\n", info.orientation); |
| 596 | } |
| 597 | CameraMetadata info2; |
| 598 | res = device->getCameraCharacteristics(&info2); |
| 599 | if (res == INVALID_OPERATION) { |
| 600 | dprintf(fd, " API2 not directly supported\n"); |
| 601 | } else if (res != OK) { |
| 602 | dprintf(fd, " <Error reading camera characteristics: %s (%d)>\n", |
| 603 | strerror(-res), res); |
| 604 | } else { |
| 605 | dprintf(fd, " API2 camera characteristics:\n"); |
| 606 | info2.dump(fd, /*verbosity*/ 2, /*indentation*/ 4); |
| 607 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 608 | } |
| 609 | return OK; |
| 610 | } |
| 611 | |
| 612 | hardware::Return<void> CameraProviderManager::ProviderInfo::cameraDeviceStatusChange( |
| 613 | const hardware::hidl_string& cameraDeviceName, |
| 614 | CameraDeviceStatus newStatus) { |
| 615 | sp<StatusListener> listener; |
| 616 | std::string id; |
| 617 | { |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 618 | std::lock_guard<std::mutex> lock(mLock); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 619 | bool known = false; |
| 620 | for (auto& deviceInfo : mDevices) { |
| 621 | if (deviceInfo->mName == cameraDeviceName) { |
| 622 | ALOGI("Camera device %s status is now %s, was %s", cameraDeviceName.c_str(), |
| 623 | deviceStatusToString(newStatus), deviceStatusToString(deviceInfo->mStatus)); |
| 624 | deviceInfo->mStatus = newStatus; |
| 625 | // TODO: Handle device removal (NOT_PRESENT) |
| 626 | id = deviceInfo->mId; |
| 627 | known = true; |
| 628 | break; |
| 629 | } |
| 630 | } |
| 631 | // Previously unseen device; status must not be NOT_PRESENT |
| 632 | if (!known) { |
| 633 | if (newStatus == CameraDeviceStatus::NOT_PRESENT) { |
| 634 | ALOGW("Camera provider %s says an unknown camera device %s is not present. Curious.", |
| 635 | mProviderName.c_str(), cameraDeviceName.c_str()); |
| 636 | return hardware::Void(); |
| 637 | } |
| 638 | addDevice(cameraDeviceName, newStatus, &id); |
| 639 | } |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 640 | listener = mManager->getStatusListener(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 641 | } |
| 642 | // Call without lock held to allow reentrancy into provider manager |
| 643 | if (listener != nullptr) { |
| 644 | listener->onDeviceStatusChanged(String8(id.c_str()), newStatus); |
| 645 | } |
| 646 | return hardware::Void(); |
| 647 | } |
| 648 | |
| 649 | hardware::Return<void> CameraProviderManager::ProviderInfo::torchModeStatusChange( |
| 650 | const hardware::hidl_string& cameraDeviceName, |
| 651 | TorchModeStatus newStatus) { |
| 652 | sp<StatusListener> listener; |
| 653 | std::string id; |
| 654 | { |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 655 | std::lock_guard<std::mutex> lock(mManager->mStatusListenerMutex); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 656 | bool known = false; |
| 657 | for (auto& deviceInfo : mDevices) { |
| 658 | if (deviceInfo->mName == cameraDeviceName) { |
| 659 | ALOGI("Camera device %s torch status is now %s", cameraDeviceName.c_str(), |
| 660 | torchStatusToString(newStatus)); |
| 661 | id = deviceInfo->mId; |
| 662 | known = true; |
| 663 | break; |
| 664 | } |
| 665 | } |
| 666 | if (!known) { |
| 667 | ALOGW("Camera provider %s says an unknown camera %s now has torch status %d. Curious.", |
| 668 | mProviderName.c_str(), cameraDeviceName.c_str(), newStatus); |
| 669 | return hardware::Void(); |
| 670 | } |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 671 | listener = mManager->getStatusListener(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 672 | } |
| 673 | // Call without lock held to allow reentrancy into provider manager |
| 674 | if (listener != nullptr) { |
| 675 | listener->onTorchStatusChanged(String8(id.c_str()), newStatus); |
| 676 | } |
| 677 | return hardware::Void(); |
| 678 | } |
| 679 | |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 680 | void CameraProviderManager::ProviderInfo::serviceDied(uint64_t cookie, |
| 681 | const wp<hidl::base::V1_0::IBase>& who) { |
| 682 | (void) who; |
| 683 | ALOGI("Camera provider '%s' has died; removing it", mProviderName.c_str()); |
| 684 | if (cookie != mId) { |
| 685 | ALOGW("%s: Unexpected serviceDied cookie %" PRIu64 ", expected %" PRIu32, |
| 686 | __FUNCTION__, cookie, mId); |
| 687 | } |
| 688 | mManager->removeProvider(mProviderName); |
| 689 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 690 | |
| 691 | template<class DeviceInfoT> |
| 692 | std::unique_ptr<CameraProviderManager::ProviderInfo::DeviceInfo> |
| 693 | CameraProviderManager::ProviderInfo::initializeDeviceInfo( |
| 694 | const std::string &name, |
| 695 | const std::string &id, uint16_t minorVersion) const { |
| 696 | Status status; |
| 697 | |
| 698 | auto cameraInterface = |
| 699 | getDeviceInterface<typename DeviceInfoT::InterfaceT>(name); |
| 700 | if (cameraInterface == nullptr) return nullptr; |
| 701 | |
| 702 | CameraResourceCost resourceCost; |
| 703 | cameraInterface->getResourceCost([&status, &resourceCost]( |
| 704 | Status s, CameraResourceCost cost) { |
| 705 | status = s; |
| 706 | resourceCost = cost; |
| 707 | }); |
| 708 | if (status != Status::OK) { |
| 709 | ALOGE("%s: Unable to obtain resource costs for camera device %s: %s", __FUNCTION__, |
| 710 | name.c_str(), statusToString(status)); |
| 711 | return nullptr; |
| 712 | } |
| 713 | return std::unique_ptr<DeviceInfo>( |
| 714 | new DeviceInfoT(name, id, minorVersion, resourceCost, cameraInterface)); |
| 715 | } |
| 716 | |
| 717 | template<class InterfaceT> |
| 718 | sp<InterfaceT> |
| 719 | CameraProviderManager::ProviderInfo::getDeviceInterface(const std::string &name) const { |
| 720 | ALOGE("%s: Device %s: Unknown HIDL device HAL major version %d:", __FUNCTION__, |
| 721 | name.c_str(), InterfaceT::version.get_major()); |
| 722 | return nullptr; |
| 723 | } |
| 724 | |
| 725 | template<> |
| 726 | sp<device::V1_0::ICameraDevice> |
| 727 | CameraProviderManager::ProviderInfo::getDeviceInterface |
| 728 | <device::V1_0::ICameraDevice>(const std::string &name) const { |
| 729 | Status status; |
| 730 | sp<device::V1_0::ICameraDevice> cameraInterface; |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 731 | hardware::Return<void> ret; |
| 732 | ret = mInterface->getCameraDeviceInterface_V1_x(name, [&status, &cameraInterface]( |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 733 | Status s, sp<device::V1_0::ICameraDevice> interface) { |
| 734 | status = s; |
| 735 | cameraInterface = interface; |
| 736 | }); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 737 | if (!ret.isOk()) { |
| 738 | ALOGE("%s: Transaction error trying to obtain interface for camera device %s: %s", |
| 739 | __FUNCTION__, name.c_str(), ret.description().c_str()); |
| 740 | return nullptr; |
| 741 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 742 | if (status != Status::OK) { |
| 743 | ALOGE("%s: Unable to obtain interface for camera device %s: %s", __FUNCTION__, |
| 744 | name.c_str(), statusToString(status)); |
| 745 | return nullptr; |
| 746 | } |
| 747 | return cameraInterface; |
| 748 | } |
| 749 | |
| 750 | template<> |
| 751 | sp<device::V3_2::ICameraDevice> |
| 752 | CameraProviderManager::ProviderInfo::getDeviceInterface |
| 753 | <device::V3_2::ICameraDevice>(const std::string &name) const { |
| 754 | Status status; |
| 755 | sp<device::V3_2::ICameraDevice> cameraInterface; |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 756 | hardware::Return<void> ret; |
| 757 | ret = mInterface->getCameraDeviceInterface_V3_x(name, [&status, &cameraInterface]( |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 758 | Status s, sp<device::V3_2::ICameraDevice> interface) { |
| 759 | status = s; |
| 760 | cameraInterface = interface; |
| 761 | }); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 762 | if (!ret.isOk()) { |
| 763 | ALOGE("%s: Transaction error trying to obtain interface for camera device %s: %s", |
| 764 | __FUNCTION__, name.c_str(), ret.description().c_str()); |
| 765 | return nullptr; |
| 766 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 767 | if (status != Status::OK) { |
| 768 | ALOGE("%s: Unable to obtain interface for camera device %s: %s", __FUNCTION__, |
| 769 | name.c_str(), statusToString(status)); |
| 770 | return nullptr; |
| 771 | } |
| 772 | return cameraInterface; |
| 773 | } |
| 774 | |
| 775 | CameraProviderManager::ProviderInfo::DeviceInfo::~DeviceInfo() {} |
| 776 | |
| 777 | template<class InterfaceT> |
| 778 | status_t CameraProviderManager::ProviderInfo::DeviceInfo::setTorchMode(InterfaceT& interface, |
| 779 | bool enabled) { |
| 780 | Status s = interface->setTorchMode(enabled ? TorchMode::ON : TorchMode::OFF); |
| 781 | return mapToStatusT(s); |
| 782 | } |
| 783 | |
| 784 | CameraProviderManager::ProviderInfo::DeviceInfo1::DeviceInfo1(const std::string& name, |
| 785 | const std::string &id, |
| 786 | uint16_t minorVersion, |
| 787 | const CameraResourceCost& resourceCost, |
| 788 | sp<InterfaceT> interface) : |
| 789 | DeviceInfo(name, id, hardware::hidl_version{1, minorVersion}, resourceCost), |
| 790 | mInterface(interface) { |
| 791 | // Get default parameters and initialize flash unit availability |
| 792 | // Requires powering on the camera device |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 793 | hardware::Return<Status> status = mInterface->open(nullptr); |
| 794 | if (!status.isOk()) { |
| 795 | ALOGE("%s: Transaction error opening camera device %s to check for a flash unit: %s", |
| 796 | __FUNCTION__, mId.c_str(), status.description().c_str()); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 797 | return; |
| 798 | } |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 799 | if (status != Status::OK) { |
| 800 | ALOGE("%s: Unable to open camera device %s to check for a flash unit: %s", __FUNCTION__, |
| 801 | mId.c_str(), CameraProviderManager::statusToString(status)); |
| 802 | return; |
| 803 | } |
| 804 | hardware::Return<void> ret; |
| 805 | ret = mInterface->getParameters([this](const hardware::hidl_string& parms) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 806 | mDefaultParameters.unflatten(String8(parms.c_str())); |
| 807 | }); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 808 | if (!ret.isOk()) { |
| 809 | ALOGE("%s: Transaction error reading camera device %s params to check for a flash unit: %s", |
| 810 | __FUNCTION__, mId.c_str(), status.description().c_str()); |
| 811 | return; |
| 812 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 813 | const char *flashMode = |
| 814 | mDefaultParameters.get(CameraParameters::KEY_SUPPORTED_FLASH_MODES); |
| 815 | if (flashMode && strstr(flashMode, CameraParameters::FLASH_MODE_TORCH)) { |
| 816 | mHasFlashUnit = true; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 817 | } |
| 818 | |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 819 | ret = mInterface->close(); |
| 820 | if (!ret.isOk()) { |
| 821 | ALOGE("%s: Transaction error closing camera device %s after check for a flash unit: %s", |
| 822 | __FUNCTION__, mId.c_str(), status.description().c_str()); |
| 823 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | CameraProviderManager::ProviderInfo::DeviceInfo1::~DeviceInfo1() {} |
| 827 | |
| 828 | status_t CameraProviderManager::ProviderInfo::DeviceInfo1::setTorchMode(bool enabled) { |
| 829 | return DeviceInfo::setTorchMode(mInterface, enabled); |
| 830 | } |
| 831 | |
| 832 | status_t CameraProviderManager::ProviderInfo::DeviceInfo1::getCameraInfo( |
| 833 | hardware::CameraInfo *info) const { |
| 834 | if (info == nullptr) return BAD_VALUE; |
| 835 | |
| 836 | Status status; |
| 837 | device::V1_0::CameraInfo cInfo; |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 838 | hardware::Return<void> ret; |
| 839 | ret = mInterface->getCameraInfo([&status, &cInfo](Status s, device::V1_0::CameraInfo camInfo) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 840 | status = s; |
| 841 | cInfo = camInfo; |
| 842 | }); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 843 | if (!ret.isOk()) { |
| 844 | ALOGE("%s: Transaction error reading camera info from device %s: %s", |
| 845 | __FUNCTION__, mId.c_str(), ret.description().c_str()); |
| 846 | return DEAD_OBJECT; |
| 847 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 848 | if (status != Status::OK) { |
| 849 | return mapToStatusT(status); |
| 850 | } |
| 851 | |
| 852 | switch(cInfo.facing) { |
| 853 | case device::V1_0::CameraFacing::BACK: |
| 854 | info->facing = hardware::CAMERA_FACING_BACK; |
| 855 | break; |
| 856 | case device::V1_0::CameraFacing::EXTERNAL: |
| 857 | // Map external to front for legacy API |
| 858 | case device::V1_0::CameraFacing::FRONT: |
| 859 | info->facing = hardware::CAMERA_FACING_FRONT; |
| 860 | break; |
| 861 | default: |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 862 | ALOGW("%s: Device %s: Unknown camera facing: %d", |
| 863 | __FUNCTION__, mId.c_str(), cInfo.facing); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 864 | info->facing = hardware::CAMERA_FACING_BACK; |
| 865 | } |
| 866 | info->orientation = cInfo.orientation; |
| 867 | |
| 868 | return OK; |
| 869 | } |
| 870 | |
| 871 | CameraProviderManager::ProviderInfo::DeviceInfo3::DeviceInfo3(const std::string& name, |
| 872 | const std::string &id, |
| 873 | uint16_t minorVersion, |
| 874 | const CameraResourceCost& resourceCost, |
| 875 | sp<InterfaceT> interface) : |
| 876 | DeviceInfo(name, id, hardware::hidl_version{3, minorVersion}, resourceCost), |
| 877 | mInterface(interface) { |
| 878 | // Get camera characteristics and initialize flash unit availability |
| 879 | Status status; |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 880 | hardware::Return<void> ret; |
| 881 | ret = mInterface->getCameraCharacteristics([&status, this](Status s, |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 882 | device::V3_2::CameraMetadata metadata) { |
| 883 | status = s; |
| 884 | if (s == Status::OK) { |
| 885 | camera_metadata_t *buffer = |
| 886 | reinterpret_cast<camera_metadata_t*>(metadata.data()); |
| 887 | mCameraCharacteristics = buffer; |
| 888 | } |
| 889 | }); |
Eino-Ville Talvala | 8d942f9 | 2017-03-13 10:09:51 -0700 | [diff] [blame] | 890 | if (!ret.isOk()) { |
| 891 | ALOGE("%s: Transaction error getting camera characteristics for device %s" |
| 892 | " to check for a flash unit: %s", __FUNCTION__, mId.c_str(), |
| 893 | ret.description().c_str()); |
| 894 | return; |
| 895 | } |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 896 | if (status != Status::OK) { |
| 897 | ALOGE("%s: Unable to get camera characteristics for device %s: %s (%d)", |
| 898 | __FUNCTION__, mId.c_str(), CameraProviderManager::statusToString(status), status); |
| 899 | return; |
| 900 | } |
| 901 | camera_metadata_entry flashAvailable = |
| 902 | mCameraCharacteristics.find(ANDROID_FLASH_INFO_AVAILABLE); |
| 903 | if (flashAvailable.count == 1 && |
| 904 | flashAvailable.data.u8[0] == ANDROID_FLASH_INFO_AVAILABLE_TRUE) { |
| 905 | mHasFlashUnit = true; |
| 906 | } else { |
| 907 | mHasFlashUnit = false; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | CameraProviderManager::ProviderInfo::DeviceInfo3::~DeviceInfo3() {} |
| 912 | |
| 913 | status_t CameraProviderManager::ProviderInfo::DeviceInfo3::setTorchMode(bool enabled) { |
| 914 | return DeviceInfo::setTorchMode(mInterface, enabled); |
| 915 | } |
| 916 | |
| 917 | status_t CameraProviderManager::ProviderInfo::DeviceInfo3::getCameraInfo( |
| 918 | hardware::CameraInfo *info) const { |
| 919 | if (info == nullptr) return BAD_VALUE; |
| 920 | |
| 921 | camera_metadata_ro_entry facing = |
| 922 | mCameraCharacteristics.find(ANDROID_LENS_FACING); |
| 923 | if (facing.count == 1) { |
| 924 | switch (facing.data.u8[0]) { |
| 925 | case ANDROID_LENS_FACING_BACK: |
| 926 | info->facing = hardware::CAMERA_FACING_BACK; |
| 927 | break; |
| 928 | case ANDROID_LENS_FACING_EXTERNAL: |
| 929 | // Map external to front for legacy API |
| 930 | case ANDROID_LENS_FACING_FRONT: |
| 931 | info->facing = hardware::CAMERA_FACING_FRONT; |
| 932 | break; |
| 933 | } |
| 934 | } else { |
| 935 | ALOGE("%s: Unable to find android.lens.facing static metadata", __FUNCTION__); |
| 936 | return NAME_NOT_FOUND; |
| 937 | } |
| 938 | |
| 939 | camera_metadata_ro_entry orientation = |
| 940 | mCameraCharacteristics.find(ANDROID_SENSOR_ORIENTATION); |
| 941 | if (orientation.count == 1) { |
| 942 | info->orientation = orientation.data.i32[0]; |
| 943 | } else { |
| 944 | ALOGE("%s: Unable to find android.sensor.orientation static metadata", __FUNCTION__); |
| 945 | return NAME_NOT_FOUND; |
| 946 | } |
| 947 | |
| 948 | return OK; |
| 949 | } |
| 950 | |
| 951 | status_t CameraProviderManager::ProviderInfo::DeviceInfo3::getCameraCharacteristics( |
| 952 | CameraMetadata *characteristics) const { |
| 953 | if (characteristics == nullptr) return BAD_VALUE; |
| 954 | |
| 955 | *characteristics = mCameraCharacteristics; |
| 956 | return OK; |
| 957 | } |
| 958 | |
| 959 | status_t CameraProviderManager::ProviderInfo::parseProviderName(const std::string& name, |
| 960 | std::string *type, uint32_t *id) { |
| 961 | // Format must be "<type>/<id>" |
| 962 | #define ERROR_MSG_PREFIX "%s: Invalid provider name '%s'. " \ |
| 963 | "Should match '<type>/<id>' - " |
| 964 | |
| 965 | if (!type || !id) return INVALID_OPERATION; |
| 966 | |
| 967 | std::string::size_type slashIdx = name.find('/'); |
| 968 | if (slashIdx == std::string::npos || slashIdx == name.size() - 1) { |
| 969 | ALOGE(ERROR_MSG_PREFIX |
| 970 | "does not have / separator between type and id", |
| 971 | __FUNCTION__, name.c_str()); |
| 972 | return BAD_VALUE; |
| 973 | } |
| 974 | |
| 975 | std::string typeVal = name.substr(0, slashIdx); |
| 976 | |
| 977 | char *endPtr; |
| 978 | errno = 0; |
| 979 | long idVal = strtol(name.c_str() + slashIdx + 1, &endPtr, 10); |
| 980 | if (errno != 0) { |
| 981 | ALOGE(ERROR_MSG_PREFIX |
| 982 | "cannot parse provider id as an integer: %s (%d)", |
| 983 | __FUNCTION__, name.c_str(), strerror(errno), errno); |
| 984 | return BAD_VALUE; |
| 985 | } |
| 986 | if (endPtr != name.c_str() + name.size()) { |
| 987 | ALOGE(ERROR_MSG_PREFIX |
| 988 | "provider id has unexpected length", |
| 989 | __FUNCTION__, name.c_str()); |
| 990 | return BAD_VALUE; |
| 991 | } |
| 992 | if (idVal < 0) { |
| 993 | ALOGE(ERROR_MSG_PREFIX |
| 994 | "id is negative: %ld", |
| 995 | __FUNCTION__, name.c_str(), idVal); |
| 996 | return BAD_VALUE; |
| 997 | } |
| 998 | |
| 999 | #undef ERROR_MSG_PREFIX |
| 1000 | |
| 1001 | *type = typeVal; |
| 1002 | *id = static_cast<uint32_t>(idVal); |
| 1003 | |
| 1004 | return OK; |
| 1005 | } |
| 1006 | |
| 1007 | status_t CameraProviderManager::ProviderInfo::parseDeviceName(const std::string& name, |
| 1008 | uint16_t *major, uint16_t *minor, std::string *type, std::string *id) { |
| 1009 | |
| 1010 | // Format must be "device@<major>.<minor>/<type>/<id>" |
| 1011 | |
| 1012 | #define ERROR_MSG_PREFIX "%s: Invalid device name '%s'. " \ |
| 1013 | "Should match 'device@<major>.<minor>/<type>/<id>' - " |
| 1014 | |
| 1015 | if (!major || !minor || !type || !id) return INVALID_OPERATION; |
| 1016 | |
| 1017 | // Verify starting prefix |
| 1018 | const char expectedPrefix[] = "device@"; |
| 1019 | |
| 1020 | if (name.find(expectedPrefix) != 0) { |
| 1021 | ALOGE(ERROR_MSG_PREFIX |
| 1022 | "does not start with '%s'", |
| 1023 | __FUNCTION__, name.c_str(), expectedPrefix); |
| 1024 | return BAD_VALUE; |
| 1025 | } |
| 1026 | |
| 1027 | // Extract major/minor versions |
| 1028 | constexpr std::string::size_type atIdx = sizeof(expectedPrefix) - 2; |
| 1029 | std::string::size_type dotIdx = name.find('.', atIdx); |
| 1030 | if (dotIdx == std::string::npos) { |
| 1031 | ALOGE(ERROR_MSG_PREFIX |
| 1032 | "does not have @<major>. version section", |
| 1033 | __FUNCTION__, name.c_str()); |
| 1034 | return BAD_VALUE; |
| 1035 | } |
| 1036 | std::string::size_type typeSlashIdx = name.find('/', dotIdx); |
| 1037 | if (typeSlashIdx == std::string::npos) { |
| 1038 | ALOGE(ERROR_MSG_PREFIX |
| 1039 | "does not have .<minor>/ version section", |
| 1040 | __FUNCTION__, name.c_str()); |
| 1041 | return BAD_VALUE; |
| 1042 | } |
| 1043 | |
| 1044 | char *endPtr; |
| 1045 | errno = 0; |
| 1046 | long majorVal = strtol(name.c_str() + atIdx + 1, &endPtr, 10); |
| 1047 | if (errno != 0) { |
| 1048 | ALOGE(ERROR_MSG_PREFIX |
| 1049 | "cannot parse major version: %s (%d)", |
| 1050 | __FUNCTION__, name.c_str(), strerror(errno), errno); |
| 1051 | return BAD_VALUE; |
| 1052 | } |
| 1053 | if (endPtr != name.c_str() + dotIdx) { |
| 1054 | ALOGE(ERROR_MSG_PREFIX |
| 1055 | "major version has unexpected length", |
| 1056 | __FUNCTION__, name.c_str()); |
| 1057 | return BAD_VALUE; |
| 1058 | } |
| 1059 | long minorVal = strtol(name.c_str() + dotIdx + 1, &endPtr, 10); |
| 1060 | if (errno != 0) { |
| 1061 | ALOGE(ERROR_MSG_PREFIX |
| 1062 | "cannot parse minor version: %s (%d)", |
| 1063 | __FUNCTION__, name.c_str(), strerror(errno), errno); |
| 1064 | return BAD_VALUE; |
| 1065 | } |
| 1066 | if (endPtr != name.c_str() + typeSlashIdx) { |
| 1067 | ALOGE(ERROR_MSG_PREFIX |
| 1068 | "minor version has unexpected length", |
| 1069 | __FUNCTION__, name.c_str()); |
| 1070 | return BAD_VALUE; |
| 1071 | } |
| 1072 | if (majorVal < 0 || majorVal > UINT16_MAX || minorVal < 0 || minorVal > UINT16_MAX) { |
| 1073 | ALOGE(ERROR_MSG_PREFIX |
| 1074 | "major/minor version is out of range of uint16_t: %ld.%ld", |
| 1075 | __FUNCTION__, name.c_str(), majorVal, minorVal); |
| 1076 | return BAD_VALUE; |
| 1077 | } |
| 1078 | |
| 1079 | // Extract type and id |
| 1080 | |
| 1081 | std::string::size_type instanceSlashIdx = name.find('/', typeSlashIdx + 1); |
| 1082 | if (instanceSlashIdx == std::string::npos) { |
| 1083 | ALOGE(ERROR_MSG_PREFIX |
| 1084 | "does not have /<type>/ component", |
| 1085 | __FUNCTION__, name.c_str()); |
| 1086 | return BAD_VALUE; |
| 1087 | } |
| 1088 | std::string typeVal = name.substr(typeSlashIdx + 1, instanceSlashIdx - typeSlashIdx - 1); |
| 1089 | |
| 1090 | if (instanceSlashIdx == name.size() - 1) { |
| 1091 | ALOGE(ERROR_MSG_PREFIX |
| 1092 | "does not have an /<id> component", |
| 1093 | __FUNCTION__, name.c_str()); |
| 1094 | return BAD_VALUE; |
| 1095 | } |
| 1096 | std::string idVal = name.substr(instanceSlashIdx + 1); |
| 1097 | |
| 1098 | #undef ERROR_MSG_PREFIX |
| 1099 | |
| 1100 | *major = static_cast<uint16_t>(majorVal); |
| 1101 | *minor = static_cast<uint16_t>(minorVal); |
| 1102 | *type = typeVal; |
| 1103 | *id = idVal; |
| 1104 | |
| 1105 | return OK; |
| 1106 | } |
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 | CameraProviderManager::ProviderInfo::~ProviderInfo() { |
| 1111 | // Destruction of ProviderInfo is only supposed to happen when the respective |
| 1112 | // CameraProvider interface dies, so do not unregister callbacks. |
| 1113 | |
| 1114 | } |
| 1115 | |
| 1116 | status_t CameraProviderManager::mapToStatusT(const Status& s) { |
| 1117 | switch(s) { |
| 1118 | case Status::OK: |
| 1119 | return OK; |
| 1120 | case Status::ILLEGAL_ARGUMENT: |
| 1121 | return BAD_VALUE; |
| 1122 | case Status::CAMERA_IN_USE: |
| 1123 | return -EBUSY; |
| 1124 | case Status::MAX_CAMERAS_IN_USE: |
| 1125 | return -EUSERS; |
| 1126 | case Status::METHOD_NOT_SUPPORTED: |
| 1127 | return UNKNOWN_TRANSACTION; |
| 1128 | case Status::OPERATION_NOT_SUPPORTED: |
| 1129 | return INVALID_OPERATION; |
| 1130 | case Status::CAMERA_DISCONNECTED: |
| 1131 | return DEAD_OBJECT; |
| 1132 | case Status::INTERNAL_ERROR: |
| 1133 | return INVALID_OPERATION; |
| 1134 | } |
| 1135 | ALOGW("Unexpected HAL status code %d", s); |
| 1136 | return INVALID_OPERATION; |
| 1137 | } |
| 1138 | |
| 1139 | const char* CameraProviderManager::statusToString(const Status& s) { |
| 1140 | switch(s) { |
| 1141 | case Status::OK: |
| 1142 | return "OK"; |
| 1143 | case Status::ILLEGAL_ARGUMENT: |
| 1144 | return "ILLEGAL_ARGUMENT"; |
| 1145 | case Status::CAMERA_IN_USE: |
| 1146 | return "CAMERA_IN_USE"; |
| 1147 | case Status::MAX_CAMERAS_IN_USE: |
| 1148 | return "MAX_CAMERAS_IN_USE"; |
| 1149 | case Status::METHOD_NOT_SUPPORTED: |
| 1150 | return "METHOD_NOT_SUPPORTED"; |
| 1151 | case Status::OPERATION_NOT_SUPPORTED: |
| 1152 | return "OPERATION_NOT_SUPPORTED"; |
| 1153 | case Status::CAMERA_DISCONNECTED: |
| 1154 | return "CAMERA_DISCONNECTED"; |
| 1155 | case Status::INTERNAL_ERROR: |
| 1156 | return "INTERNAL_ERROR"; |
| 1157 | } |
| 1158 | ALOGW("Unexpected HAL status code %d", s); |
| 1159 | return "UNKNOWN_ERROR"; |
| 1160 | } |
| 1161 | |
| 1162 | const char* CameraProviderManager::deviceStatusToString(const CameraDeviceStatus& s) { |
| 1163 | switch(s) { |
| 1164 | case CameraDeviceStatus::NOT_PRESENT: |
| 1165 | return "NOT_PRESENT"; |
| 1166 | case CameraDeviceStatus::PRESENT: |
| 1167 | return "PRESENT"; |
| 1168 | case CameraDeviceStatus::ENUMERATING: |
| 1169 | return "ENUMERATING"; |
| 1170 | } |
| 1171 | ALOGW("Unexpected HAL device status code %d", s); |
| 1172 | return "UNKNOWN_STATUS"; |
| 1173 | } |
| 1174 | |
| 1175 | const char* CameraProviderManager::torchStatusToString(const TorchModeStatus& s) { |
| 1176 | switch(s) { |
| 1177 | case TorchModeStatus::NOT_AVAILABLE: |
| 1178 | return "NOT_AVAILABLE"; |
| 1179 | case TorchModeStatus::AVAILABLE_OFF: |
| 1180 | return "AVAILABLE_OFF"; |
| 1181 | case TorchModeStatus::AVAILABLE_ON: |
| 1182 | return "AVAILABLE_ON"; |
| 1183 | } |
| 1184 | ALOGW("Unexpected HAL torch mode status code %d", s); |
| 1185 | return "UNKNOWN_STATUS"; |
| 1186 | } |
| 1187 | |
Yin-Chia Yeh | 067428c | 2017-01-13 15:19:24 -0800 | [diff] [blame] | 1188 | |
| 1189 | status_t HidlVendorTagDescriptor::createDescriptorFromHidl( |
| 1190 | const hardware::hidl_vec<hardware::camera::common::V1_0::VendorTagSection>& vts, |
| 1191 | /*out*/ |
| 1192 | sp<VendorTagDescriptor>& descriptor) { |
| 1193 | |
| 1194 | int tagCount = 0; |
| 1195 | |
| 1196 | for (size_t s = 0; s < vts.size(); s++) { |
| 1197 | tagCount += vts[s].tags.size(); |
| 1198 | } |
| 1199 | |
| 1200 | if (tagCount < 0 || tagCount > INT32_MAX) { |
| 1201 | ALOGE("%s: tag count %d from vendor tag sections is invalid.", __FUNCTION__, tagCount); |
| 1202 | return BAD_VALUE; |
| 1203 | } |
| 1204 | |
| 1205 | Vector<uint32_t> tagArray; |
| 1206 | LOG_ALWAYS_FATAL_IF(tagArray.resize(tagCount) != tagCount, |
| 1207 | "%s: too many (%u) vendor tags defined.", __FUNCTION__, tagCount); |
| 1208 | |
| 1209 | |
| 1210 | sp<HidlVendorTagDescriptor> desc = new HidlVendorTagDescriptor(); |
| 1211 | desc->mTagCount = tagCount; |
| 1212 | |
| 1213 | SortedVector<String8> sections; |
| 1214 | KeyedVector<uint32_t, String8> tagToSectionMap; |
| 1215 | |
| 1216 | int idx = 0; |
| 1217 | for (size_t s = 0; s < vts.size(); s++) { |
| 1218 | const hardware::camera::common::V1_0::VendorTagSection& section = vts[s]; |
| 1219 | const char *sectionName = section.sectionName.c_str(); |
| 1220 | if (sectionName == NULL) { |
| 1221 | ALOGE("%s: no section name defined for vendor tag section %zu.", __FUNCTION__, s); |
| 1222 | return BAD_VALUE; |
| 1223 | } |
| 1224 | String8 sectionString(sectionName); |
| 1225 | sections.add(sectionString); |
| 1226 | |
| 1227 | for (size_t j = 0; j < section.tags.size(); j++) { |
| 1228 | uint32_t tag = section.tags[j].tagId; |
| 1229 | if (tag < CAMERA_METADATA_VENDOR_TAG_BOUNDARY) { |
| 1230 | ALOGE("%s: vendor tag %d not in vendor tag section.", __FUNCTION__, tag); |
| 1231 | return BAD_VALUE; |
| 1232 | } |
| 1233 | |
| 1234 | tagArray.editItemAt(idx++) = section.tags[j].tagId; |
| 1235 | |
| 1236 | const char *tagName = section.tags[j].tagName.c_str(); |
| 1237 | if (tagName == NULL) { |
| 1238 | ALOGE("%s: no tag name defined for vendor tag %d.", __FUNCTION__, tag); |
| 1239 | return BAD_VALUE; |
| 1240 | } |
| 1241 | desc->mTagToNameMap.add(tag, String8(tagName)); |
| 1242 | tagToSectionMap.add(tag, sectionString); |
| 1243 | |
| 1244 | int tagType = (int) section.tags[j].tagType; |
| 1245 | if (tagType < 0 || tagType >= NUM_TYPES) { |
| 1246 | ALOGE("%s: tag type %d from vendor ops does not exist.", __FUNCTION__, tagType); |
| 1247 | return BAD_VALUE; |
| 1248 | } |
| 1249 | desc->mTagToTypeMap.add(tag, tagType); |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | desc->mSections = sections; |
| 1254 | |
| 1255 | for (size_t i = 0; i < tagArray.size(); ++i) { |
| 1256 | uint32_t tag = tagArray[i]; |
| 1257 | String8 sectionString = tagToSectionMap.valueFor(tag); |
| 1258 | |
| 1259 | // Set up tag to section index map |
| 1260 | ssize_t index = sections.indexOf(sectionString); |
| 1261 | LOG_ALWAYS_FATAL_IF(index < 0, "index %zd must be non-negative", index); |
| 1262 | desc->mTagToSectionMap.add(tag, static_cast<uint32_t>(index)); |
| 1263 | |
| 1264 | // Set up reverse mapping |
| 1265 | ssize_t reverseIndex = -1; |
| 1266 | if ((reverseIndex = desc->mReverseMapping.indexOfKey(sectionString)) < 0) { |
| 1267 | KeyedVector<String8, uint32_t>* nameMapper = new KeyedVector<String8, uint32_t>(); |
| 1268 | reverseIndex = desc->mReverseMapping.add(sectionString, nameMapper); |
| 1269 | } |
| 1270 | desc->mReverseMapping[reverseIndex]->add(desc->mTagToNameMap.valueFor(tag), tag); |
| 1271 | } |
| 1272 | |
| 1273 | descriptor = desc; |
| 1274 | return OK; |
| 1275 | } |
| 1276 | |
| 1277 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 1278 | } // namespace android |