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
23 #include <android/hardware/camera/device/3.5/ICameraDevice.h>
24
25 #include <algorithm>
26 #include <chrono>
27 #include "common/DepthPhotoProcessor.h"
28 #include <dlfcn.h>
29 #include <future>
30 #include <inttypes.h>
31 #include <hardware/camera_common.h>
32 #include <hidl/ServiceManagement.h>
33 #include <functional>
34 #include <camera_metadata_hidden.h>
35 #include <android-base/parseint.h>
36 #include <android-base/logging.h>
37 #include <cutils/properties.h>
38 #include <hwbinder/IPCThreadState.h>
39 #include <utils/Trace.h>
40
41 #include "api2/HeicCompositeStream.h"
42
43 namespace android {
44
45 using namespace ::android::hardware::camera;
46 using namespace ::android::hardware::camera::common::V1_0;
47 using std::literals::chrono_literals::operator""s;
48
49 namespace {
50 // Hardcoded name for the passthrough HAL implementation, since it can't be discovered via the
51 // service manager
52 const std::string kLegacyProviderName("legacy/0");
53 const std::string kExternalProviderName("external/0");
54 const bool kEnableLazyHal(property_get_bool("ro.camera.enableLazyHal", false));
55 } // anonymous namespace
56
57 const float CameraProviderManager::kDepthARTolerance = .1f;
58
59 CameraProviderManager::HardwareServiceInteractionProxy
60 CameraProviderManager::sHardwareServiceInteractionProxy{};
61
~CameraProviderManager()62 CameraProviderManager::~CameraProviderManager() {
63 }
64
initialize(wp<CameraProviderManager::StatusListener> listener,ServiceInteractionProxy * proxy)65 status_t CameraProviderManager::initialize(wp<CameraProviderManager::StatusListener> listener,
66 ServiceInteractionProxy* proxy) {
67 std::lock_guard<std::mutex> lock(mInterfaceMutex);
68 if (proxy == nullptr) {
69 ALOGE("%s: No valid service interaction proxy provided", __FUNCTION__);
70 return BAD_VALUE;
71 }
72 mListener = listener;
73 mServiceProxy = proxy;
74 mDeviceState = static_cast<hardware::hidl_bitfield<provider::V2_5::DeviceState>>(
75 provider::V2_5::DeviceState::NORMAL);
76
77 // Registering will trigger notifications for all already-known providers
78 bool success = mServiceProxy->registerForNotifications(
79 /* instance name, empty means no filter */ "",
80 this);
81 if (!success) {
82 ALOGE("%s: Unable to register with hardware service manager for notifications "
83 "about camera providers", __FUNCTION__);
84 return INVALID_OPERATION;
85 }
86
87 // See if there's a passthrough HAL, but let's not complain if there's not
88 addProviderLocked(kLegacyProviderName, /*expected*/ false);
89 addProviderLocked(kExternalProviderName, /*expected*/ false);
90
91 IPCThreadState::self()->flushCommands();
92
93 return OK;
94 }
95
getCameraCount() const96 int CameraProviderManager::getCameraCount() const {
97 std::lock_guard<std::mutex> lock(mInterfaceMutex);
98 int count = 0;
99 for (auto& provider : mProviders) {
100 count += provider->mUniqueCameraIds.size();
101 }
102 return count;
103 }
104
getCameraDeviceIds() const105 std::vector<std::string> CameraProviderManager::getCameraDeviceIds() const {
106 std::lock_guard<std::mutex> lock(mInterfaceMutex);
107 std::vector<std::string> deviceIds;
108 for (auto& provider : mProviders) {
109 for (auto& id : provider->mUniqueCameraIds) {
110 deviceIds.push_back(id);
111 }
112 }
113 return deviceIds;
114 }
115
getAPI1CompatibleCameraDeviceIds() const116 std::vector<std::string> CameraProviderManager::getAPI1CompatibleCameraDeviceIds() const {
117 std::lock_guard<std::mutex> lock(mInterfaceMutex);
118 std::vector<std::string> deviceIds;
119 for (auto& provider : mProviders) {
120 std::vector<std::string> providerDeviceIds = provider->mUniqueAPI1CompatibleCameraIds;
121
122 // API1 app doesn't handle logical and physical camera devices well. So
123 // for each camera facing, only take the first id advertised by HAL in
124 // all [logical, physical1, physical2, ...] id combos, and filter out the rest.
125 filterLogicalCameraIdsLocked(providerDeviceIds);
126
127 deviceIds.insert(deviceIds.end(), providerDeviceIds.begin(), providerDeviceIds.end());
128 }
129
130 std::sort(deviceIds.begin(), deviceIds.end(),
131 [](const std::string& a, const std::string& b) -> bool {
132 uint32_t aUint = 0, bUint = 0;
133 bool aIsUint = base::ParseUint(a, &aUint);
134 bool bIsUint = base::ParseUint(b, &bUint);
135
136 // Uint device IDs first
137 if (aIsUint && bIsUint) {
138 return aUint < bUint;
139 } else if (aIsUint) {
140 return true;
141 } else if (bIsUint) {
142 return false;
143 }
144 // Simple string compare if both id are not uint
145 return a < b;
146 });
147 return deviceIds;
148 }
149
isValidDevice(const std::string & id,uint16_t majorVersion) const150 bool CameraProviderManager::isValidDevice(const std::string &id, uint16_t majorVersion) const {
151 std::lock_guard<std::mutex> lock(mInterfaceMutex);
152 return isValidDeviceLocked(id, majorVersion);
153 }
154
isValidDeviceLocked(const std::string & id,uint16_t majorVersion) const155 bool CameraProviderManager::isValidDeviceLocked(const std::string &id, uint16_t majorVersion) const {
156 for (auto& provider : mProviders) {
157 for (auto& deviceInfo : provider->mDevices) {
158 if (deviceInfo->mId == id && deviceInfo->mVersion.get_major() == majorVersion) {
159 return true;
160 }
161 }
162 }
163 return false;
164 }
165
hasFlashUnit(const std::string & id) const166 bool CameraProviderManager::hasFlashUnit(const std::string &id) const {
167 std::lock_guard<std::mutex> lock(mInterfaceMutex);
168
169 auto deviceInfo = findDeviceInfoLocked(id);
170 if (deviceInfo == nullptr) return false;
171
172 return deviceInfo->hasFlashUnit();
173 }
174
getResourceCost(const std::string & id,CameraResourceCost * cost) const175 status_t CameraProviderManager::getResourceCost(const std::string &id,
176 CameraResourceCost* cost) const {
177 std::lock_guard<std::mutex> lock(mInterfaceMutex);
178
179 auto deviceInfo = findDeviceInfoLocked(id);
180 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
181
182 *cost = deviceInfo->mResourceCost;
183 return OK;
184 }
185
getCameraInfo(const std::string & id,hardware::CameraInfo * info) const186 status_t CameraProviderManager::getCameraInfo(const std::string &id,
187 hardware::CameraInfo* info) const {
188 std::lock_guard<std::mutex> lock(mInterfaceMutex);
189
190 auto deviceInfo = findDeviceInfoLocked(id);
191 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
192
193 return deviceInfo->getCameraInfo(info);
194 }
195
isSessionConfigurationSupported(const std::string & id,const hardware::camera::device::V3_4::StreamConfiguration & configuration,bool * status) const196 status_t CameraProviderManager::isSessionConfigurationSupported(const std::string& id,
197 const hardware::camera::device::V3_4::StreamConfiguration &configuration,
198 bool *status /*out*/) const {
199 std::lock_guard<std::mutex> lock(mInterfaceMutex);
200
201 auto deviceInfo = findDeviceInfoLocked(id);
202 if (deviceInfo == nullptr) {
203 return NAME_NOT_FOUND;
204 }
205
206 return deviceInfo->isSessionConfigurationSupported(configuration, status);
207 }
208
getCameraCharacteristics(const std::string & id,CameraMetadata * characteristics) const209 status_t CameraProviderManager::getCameraCharacteristics(const std::string &id,
210 CameraMetadata* characteristics) const {
211 std::lock_guard<std::mutex> lock(mInterfaceMutex);
212 return getCameraCharacteristicsLocked(id, characteristics);
213 }
214
getHighestSupportedVersion(const std::string & id,hardware::hidl_version * v)215 status_t CameraProviderManager::getHighestSupportedVersion(const std::string &id,
216 hardware::hidl_version *v) {
217 std::lock_guard<std::mutex> lock(mInterfaceMutex);
218
219 hardware::hidl_version maxVersion{0,0};
220 bool found = false;
221 for (auto& provider : mProviders) {
222 for (auto& deviceInfo : provider->mDevices) {
223 if (deviceInfo->mId == id) {
224 if (deviceInfo->mVersion > maxVersion) {
225 maxVersion = deviceInfo->mVersion;
226 found = true;
227 }
228 }
229 }
230 }
231 if (!found) {
232 return NAME_NOT_FOUND;
233 }
234 *v = maxVersion;
235 return OK;
236 }
237
supportSetTorchMode(const std::string & id) const238 bool CameraProviderManager::supportSetTorchMode(const std::string &id) const {
239 std::lock_guard<std::mutex> lock(mInterfaceMutex);
240 for (auto& provider : mProviders) {
241 auto deviceInfo = findDeviceInfoLocked(id);
242 if (deviceInfo != nullptr) {
243 return provider->mSetTorchModeSupported;
244 }
245 }
246 return false;
247 }
248
setTorchMode(const std::string & id,bool enabled)249 status_t CameraProviderManager::setTorchMode(const std::string &id, bool enabled) {
250 std::lock_guard<std::mutex> lock(mInterfaceMutex);
251
252 auto deviceInfo = findDeviceInfoLocked(id);
253 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
254
255 // Pass the camera ID to start interface so that it will save it to the map of ICameraProviders
256 // that are currently in use.
257 const sp<provider::V2_4::ICameraProvider> interface =
258 deviceInfo->mParentProvider->startProviderInterface();
259 if (interface == nullptr) {
260 return DEAD_OBJECT;
261 }
262 saveRef(DeviceMode::TORCH, deviceInfo->mId, interface);
263
264 return deviceInfo->setTorchMode(enabled);
265 }
266
setUpVendorTags()267 status_t CameraProviderManager::setUpVendorTags() {
268 sp<VendorTagDescriptorCache> tagCache = new VendorTagDescriptorCache();
269
270 for (auto& provider : mProviders) {
271 tagCache->addVendorDescriptor(provider->mProviderTagid, provider->mVendorTagDescriptor);
272 }
273
274 VendorTagDescriptorCache::setAsGlobalVendorTagCache(tagCache);
275
276 return OK;
277 }
278
notifyDeviceStateChange(hardware::hidl_bitfield<provider::V2_5::DeviceState> newState)279 status_t CameraProviderManager::notifyDeviceStateChange(
280 hardware::hidl_bitfield<provider::V2_5::DeviceState> newState) {
281 std::lock_guard<std::mutex> lock(mInterfaceMutex);
282 mDeviceState = newState;
283 status_t res = OK;
284 for (auto& provider : mProviders) {
285 ALOGV("%s: Notifying %s for new state 0x%" PRIx64,
286 __FUNCTION__, provider->mProviderName.c_str(), newState);
287 status_t singleRes = provider->notifyDeviceStateChange(mDeviceState);
288 if (singleRes != OK) {
289 ALOGE("%s: Unable to notify provider %s about device state change",
290 __FUNCTION__,
291 provider->mProviderName.c_str());
292 res = singleRes;
293 // continue to do the rest of the providers instead of returning now
294 }
295 }
296 return res;
297 }
298
openSession(const std::string & id,const sp<device::V3_2::ICameraDeviceCallback> & callback,sp<device::V3_2::ICameraDeviceSession> * session)299 status_t CameraProviderManager::openSession(const std::string &id,
300 const sp<device::V3_2::ICameraDeviceCallback>& callback,
301 /*out*/
302 sp<device::V3_2::ICameraDeviceSession> *session) {
303
304 std::lock_guard<std::mutex> lock(mInterfaceMutex);
305
306 auto deviceInfo = findDeviceInfoLocked(id,
307 /*minVersion*/ {3,0}, /*maxVersion*/ {4,0});
308 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
309
310 auto *deviceInfo3 = static_cast<ProviderInfo::DeviceInfo3*>(deviceInfo);
311 const sp<provider::V2_4::ICameraProvider> provider =
312 deviceInfo->mParentProvider->startProviderInterface();
313 if (provider == nullptr) {
314 return DEAD_OBJECT;
315 }
316 saveRef(DeviceMode::CAMERA, id, provider);
317
318 Status status;
319 hardware::Return<void> ret;
320 auto interface = deviceInfo3->startDeviceInterface<
321 CameraProviderManager::ProviderInfo::DeviceInfo3::InterfaceT>();
322 if (interface == nullptr) {
323 return DEAD_OBJECT;
324 }
325
326 ret = interface->open(callback, [&status, &session]
327 (Status s, const sp<device::V3_2::ICameraDeviceSession>& cameraSession) {
328 status = s;
329 if (status == Status::OK) {
330 *session = cameraSession;
331 }
332 });
333 if (!ret.isOk()) {
334 removeRef(DeviceMode::CAMERA, id);
335 ALOGE("%s: Transaction error opening a session for camera device %s: %s",
336 __FUNCTION__, id.c_str(), ret.description().c_str());
337 return DEAD_OBJECT;
338 }
339 return mapToStatusT(status);
340 }
341
openSession(const std::string & id,const sp<device::V1_0::ICameraDeviceCallback> & callback,sp<device::V1_0::ICameraDevice> * session)342 status_t CameraProviderManager::openSession(const std::string &id,
343 const sp<device::V1_0::ICameraDeviceCallback>& callback,
344 /*out*/
345 sp<device::V1_0::ICameraDevice> *session) {
346
347 std::lock_guard<std::mutex> lock(mInterfaceMutex);
348
349 auto deviceInfo = findDeviceInfoLocked(id,
350 /*minVersion*/ {1,0}, /*maxVersion*/ {2,0});
351 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
352
353 auto *deviceInfo1 = static_cast<ProviderInfo::DeviceInfo1*>(deviceInfo);
354 const sp<provider::V2_4::ICameraProvider> provider =
355 deviceInfo->mParentProvider->startProviderInterface();
356 if (provider == nullptr) {
357 return DEAD_OBJECT;
358 }
359 saveRef(DeviceMode::CAMERA, id, provider);
360
361 auto interface = deviceInfo1->startDeviceInterface<
362 CameraProviderManager::ProviderInfo::DeviceInfo1::InterfaceT>();
363 if (interface == nullptr) {
364 return DEAD_OBJECT;
365 }
366 hardware::Return<Status> status = interface->open(callback);
367 if (!status.isOk()) {
368 removeRef(DeviceMode::CAMERA, id);
369 ALOGE("%s: Transaction error opening a session for camera device %s: %s",
370 __FUNCTION__, id.c_str(), status.description().c_str());
371 return DEAD_OBJECT;
372 }
373 if (status == Status::OK) {
374 *session = interface;
375 }
376 return mapToStatusT(status);
377 }
378
saveRef(DeviceMode usageType,const std::string & cameraId,sp<provider::V2_4::ICameraProvider> provider)379 void CameraProviderManager::saveRef(DeviceMode usageType, const std::string &cameraId,
380 sp<provider::V2_4::ICameraProvider> provider) {
381 if (!kEnableLazyHal) {
382 return;
383 }
384 ALOGV("Saving camera provider %s for camera device %s", provider->descriptor, cameraId.c_str());
385 std::lock_guard<std::mutex> lock(mProviderInterfaceMapLock);
386 std::unordered_map<std::string, sp<provider::V2_4::ICameraProvider>> *primaryMap, *alternateMap;
387 if (usageType == DeviceMode::TORCH) {
388 primaryMap = &mTorchProviderByCameraId;
389 alternateMap = &mCameraProviderByCameraId;
390 } else {
391 primaryMap = &mCameraProviderByCameraId;
392 alternateMap = &mTorchProviderByCameraId;
393 }
394 auto id = cameraId.c_str();
395 (*primaryMap)[id] = provider;
396 auto search = alternateMap->find(id);
397 if (search != alternateMap->end()) {
398 ALOGW("%s: Camera device %s is using both torch mode and camera mode simultaneously. "
399 "That should not be possible", __FUNCTION__, id);
400 }
401 ALOGV("%s: Camera device %s connected", __FUNCTION__, id);
402 }
403
removeRef(DeviceMode usageType,const std::string & cameraId)404 void CameraProviderManager::removeRef(DeviceMode usageType, const std::string &cameraId) {
405 if (!kEnableLazyHal) {
406 return;
407 }
408 ALOGV("Removing camera device %s", cameraId.c_str());
409 std::unordered_map<std::string, sp<provider::V2_4::ICameraProvider>> *providerMap;
410 if (usageType == DeviceMode::TORCH) {
411 providerMap = &mTorchProviderByCameraId;
412 } else {
413 providerMap = &mCameraProviderByCameraId;
414 }
415 std::lock_guard<std::mutex> lock(mProviderInterfaceMapLock);
416 auto search = providerMap->find(cameraId.c_str());
417 if (search != providerMap->end()) {
418 // Drop the reference to this ICameraProvider. This is safe to do immediately (without an
419 // added delay) because hwservicemanager guarantees to hold the reference for at least five
420 // more seconds. We depend on this behavior so that if the provider is unreferenced and
421 // then referenced again quickly, we do not let the HAL exit and then need to immediately
422 // restart it. An example when this could happen is switching from a front-facing to a
423 // rear-facing camera. If the HAL were to exit during the camera switch, the camera could
424 // appear janky to the user.
425 providerMap->erase(cameraId.c_str());
426 IPCThreadState::self()->flushCommands();
427 } else {
428 ALOGE("%s: Asked to remove reference for camera %s, but no reference to it was found. This "
429 "could mean removeRef was called twice for the same camera ID.", __FUNCTION__,
430 cameraId.c_str());
431 }
432 }
433
onRegistration(const hardware::hidl_string &,const hardware::hidl_string & name,bool)434 hardware::Return<void> CameraProviderManager::onRegistration(
435 const hardware::hidl_string& /*fqName*/,
436 const hardware::hidl_string& name,
437 bool /*preexisting*/) {
438 std::lock_guard<std::mutex> providerLock(mProviderLifecycleLock);
439 {
440 std::lock_guard<std::mutex> lock(mInterfaceMutex);
441
442 addProviderLocked(name);
443 }
444
445 sp<StatusListener> listener = getStatusListener();
446 if (nullptr != listener.get()) {
447 listener->onNewProviderRegistered();
448 }
449
450 IPCThreadState::self()->flushCommands();
451
452 return hardware::Return<void>();
453 }
454
dump(int fd,const Vector<String16> & args)455 status_t CameraProviderManager::dump(int fd, const Vector<String16>& args) {
456 std::lock_guard<std::mutex> lock(mInterfaceMutex);
457
458 for (auto& provider : mProviders) {
459 provider->dump(fd, args);
460 }
461 return OK;
462 }
463
findDeviceInfoLocked(const std::string & id,hardware::hidl_version minVersion,hardware::hidl_version maxVersion) const464 CameraProviderManager::ProviderInfo::DeviceInfo* CameraProviderManager::findDeviceInfoLocked(
465 const std::string& id,
466 hardware::hidl_version minVersion, hardware::hidl_version maxVersion) const {
467 for (auto& provider : mProviders) {
468 for (auto& deviceInfo : provider->mDevices) {
469 if (deviceInfo->mId == id &&
470 minVersion <= deviceInfo->mVersion && maxVersion >= deviceInfo->mVersion) {
471 return deviceInfo.get();
472 }
473 }
474 }
475 return nullptr;
476 }
477
getProviderTagIdLocked(const std::string & id,hardware::hidl_version minVersion,hardware::hidl_version maxVersion) const478 metadata_vendor_id_t CameraProviderManager::getProviderTagIdLocked(
479 const std::string& id, hardware::hidl_version minVersion,
480 hardware::hidl_version maxVersion) const {
481 metadata_vendor_id_t ret = CAMERA_METADATA_INVALID_VENDOR_ID;
482
483 std::lock_guard<std::mutex> lock(mInterfaceMutex);
484 for (auto& provider : mProviders) {
485 for (auto& deviceInfo : provider->mDevices) {
486 if (deviceInfo->mId == id &&
487 minVersion <= deviceInfo->mVersion &&
488 maxVersion >= deviceInfo->mVersion) {
489 return provider->mProviderTagid;
490 }
491 }
492 }
493
494 return ret;
495 }
496
queryPhysicalCameraIds()497 void CameraProviderManager::ProviderInfo::DeviceInfo3::queryPhysicalCameraIds() {
498 camera_metadata_entry_t entryCap;
499
500 entryCap = mCameraCharacteristics.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
501 for (size_t i = 0; i < entryCap.count; ++i) {
502 uint8_t capability = entryCap.data.u8[i];
503 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA) {
504 mIsLogicalCamera = true;
505 break;
506 }
507 }
508 if (!mIsLogicalCamera) {
509 return;
510 }
511
512 camera_metadata_entry_t entryIds = mCameraCharacteristics.find(
513 ANDROID_LOGICAL_MULTI_CAMERA_PHYSICAL_IDS);
514 const uint8_t* ids = entryIds.data.u8;
515 size_t start = 0;
516 for (size_t i = 0; i < entryIds.count; ++i) {
517 if (ids[i] == '\0') {
518 if (start != i) {
519 mPhysicalIds.push_back((const char*)ids+start);
520 }
521 start = i+1;
522 }
523 }
524 }
525
isPublicallyHiddenSecureCamera()526 bool CameraProviderManager::ProviderInfo::DeviceInfo3::isPublicallyHiddenSecureCamera() {
527 camera_metadata_entry_t entryCap;
528 entryCap = mCameraCharacteristics.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
529 if (entryCap.count != 1) {
530 // Do NOT hide this camera device if the capabilities specify anything more
531 // than ANDROID_REQUEST_AVAILABLE_CAPABILITIES_SECURE_IMAGE_DATA.
532 return false;
533 }
534 return entryCap.data.u8[0] == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_SECURE_IMAGE_DATA;
535 }
536
getSupportedSizes(const CameraMetadata & ch,uint32_t tag,android_pixel_format_t format,std::vector<std::tuple<size_t,size_t>> * sizes)537 void CameraProviderManager::ProviderInfo::DeviceInfo3::getSupportedSizes(
538 const CameraMetadata& ch, uint32_t tag, android_pixel_format_t format,
539 std::vector<std::tuple<size_t, size_t>> *sizes/*out*/) {
540 if (sizes == nullptr) {
541 return;
542 }
543
544 auto scalerDims = ch.find(tag);
545 if (scalerDims.count > 0) {
546 // Scaler entry contains 4 elements (format, width, height, type)
547 for (size_t i = 0; i < scalerDims.count; i += 4) {
548 if ((scalerDims.data.i32[i] == format) &&
549 (scalerDims.data.i32[i+3] ==
550 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT)) {
551 sizes->push_back(std::make_tuple(scalerDims.data.i32[i+1],
552 scalerDims.data.i32[i+2]));
553 }
554 }
555 }
556 }
557
getSupportedDurations(const CameraMetadata & ch,uint32_t tag,android_pixel_format_t format,const std::vector<std::tuple<size_t,size_t>> & sizes,std::vector<int64_t> * durations)558 void CameraProviderManager::ProviderInfo::DeviceInfo3::getSupportedDurations(
559 const CameraMetadata& ch, uint32_t tag, android_pixel_format_t format,
560 const std::vector<std::tuple<size_t, size_t>>& sizes,
561 std::vector<int64_t> *durations/*out*/) {
562 if (durations == nullptr) {
563 return;
564 }
565
566 auto availableDurations = ch.find(tag);
567 if (availableDurations.count > 0) {
568 // Duration entry contains 4 elements (format, width, height, duration)
569 for (size_t i = 0; i < availableDurations.count; i += 4) {
570 for (const auto& size : sizes) {
571 int64_t width = std::get<0>(size);
572 int64_t height = std::get<1>(size);
573 if ((availableDurations.data.i64[i] == format) &&
574 (availableDurations.data.i64[i+1] == width) &&
575 (availableDurations.data.i64[i+2] == height)) {
576 durations->push_back(availableDurations.data.i64[i+3]);
577 }
578 }
579 }
580 }
581 }
getSupportedDynamicDepthDurations(const std::vector<int64_t> & depthDurations,const std::vector<int64_t> & blobDurations,std::vector<int64_t> * dynamicDepthDurations)582 void CameraProviderManager::ProviderInfo::DeviceInfo3::getSupportedDynamicDepthDurations(
583 const std::vector<int64_t>& depthDurations, const std::vector<int64_t>& blobDurations,
584 std::vector<int64_t> *dynamicDepthDurations /*out*/) {
585 if ((dynamicDepthDurations == nullptr) || (depthDurations.size() != blobDurations.size())) {
586 return;
587 }
588
589 // Unfortunately there is no direct way to calculate the dynamic depth stream duration.
590 // Processing time on camera service side can vary greatly depending on multiple
591 // variables which are not under our control. Make a guesstimate by taking the maximum
592 // corresponding duration value from depth and blob.
593 auto depthDuration = depthDurations.begin();
594 auto blobDuration = blobDurations.begin();
595 dynamicDepthDurations->reserve(depthDurations.size());
596 while ((depthDuration != depthDurations.end()) && (blobDuration != blobDurations.end())) {
597 dynamicDepthDurations->push_back(std::max(*depthDuration, *blobDuration));
598 depthDuration++; blobDuration++;
599 }
600 }
601
getSupportedDynamicDepthSizes(const std::vector<std::tuple<size_t,size_t>> & blobSizes,const std::vector<std::tuple<size_t,size_t>> & depthSizes,std::vector<std::tuple<size_t,size_t>> * dynamicDepthSizes,std::vector<std::tuple<size_t,size_t>> * internalDepthSizes)602 void CameraProviderManager::ProviderInfo::DeviceInfo3::getSupportedDynamicDepthSizes(
603 const std::vector<std::tuple<size_t, size_t>>& blobSizes,
604 const std::vector<std::tuple<size_t, size_t>>& depthSizes,
605 std::vector<std::tuple<size_t, size_t>> *dynamicDepthSizes /*out*/,
606 std::vector<std::tuple<size_t, size_t>> *internalDepthSizes /*out*/) {
607 if (dynamicDepthSizes == nullptr || internalDepthSizes == nullptr) {
608 return;
609 }
610
611 // The dynamic depth spec. does not mention how close the AR ratio should be.
612 // Try using something appropriate.
613 float ARTolerance = kDepthARTolerance;
614
615 for (const auto& blobSize : blobSizes) {
616 float jpegAR = static_cast<float> (std::get<0>(blobSize)) /
617 static_cast<float>(std::get<1>(blobSize));
618 bool found = false;
619 for (const auto& depthSize : depthSizes) {
620 if (depthSize == blobSize) {
621 internalDepthSizes->push_back(depthSize);
622 found = true;
623 break;
624 } else {
625 float depthAR = static_cast<float> (std::get<0>(depthSize)) /
626 static_cast<float>(std::get<1>(depthSize));
627 if (std::fabs(jpegAR - depthAR) <= ARTolerance) {
628 internalDepthSizes->push_back(depthSize);
629 found = true;
630 break;
631 }
632 }
633 }
634
635 if (found) {
636 dynamicDepthSizes->push_back(blobSize);
637 }
638 }
639 }
640
isDepthPhotoLibraryPresent()641 bool CameraProviderManager::ProviderInfo::DeviceInfo3::isDepthPhotoLibraryPresent() {
642 static bool libraryPresent = false;
643 static bool initialized = false;
644 if (initialized) {
645 return libraryPresent;
646 } else {
647 initialized = true;
648 }
649
650 void* depthLibHandle = dlopen(camera3::kDepthPhotoLibrary, RTLD_NOW | RTLD_LOCAL);
651 if (depthLibHandle == nullptr) {
652 return false;
653 }
654
655 auto processFunc = dlsym(depthLibHandle, camera3::kDepthPhotoProcessFunction);
656 if (processFunc != nullptr) {
657 libraryPresent = true;
658 } else {
659 libraryPresent = false;
660 }
661 dlclose(depthLibHandle);
662
663 return libraryPresent;
664 }
665
addDynamicDepthTags()666 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::addDynamicDepthTags() {
667 uint32_t depthExclTag = ANDROID_DEPTH_DEPTH_IS_EXCLUSIVE;
668 uint32_t depthSizesTag = ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS;
669 auto& c = mCameraCharacteristics;
670 std::vector<std::tuple<size_t, size_t>> supportedBlobSizes, supportedDepthSizes,
671 supportedDynamicDepthSizes, internalDepthSizes;
672 auto chTags = c.find(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
673 if (chTags.count == 0) {
674 ALOGE("%s: Supported camera characteristics is empty!", __FUNCTION__);
675 return BAD_VALUE;
676 }
677
678 bool isDepthExclusivePresent = std::find(chTags.data.i32, chTags.data.i32 + chTags.count,
679 depthExclTag) != (chTags.data.i32 + chTags.count);
680 bool isDepthSizePresent = std::find(chTags.data.i32, chTags.data.i32 + chTags.count,
681 depthSizesTag) != (chTags.data.i32 + chTags.count);
682 if (!(isDepthExclusivePresent && isDepthSizePresent)) {
683 // No depth support, nothing more to do.
684 return OK;
685 }
686
687 auto depthExclusiveEntry = c.find(depthExclTag);
688 if (depthExclusiveEntry.count > 0) {
689 if (depthExclusiveEntry.data.u8[0] != ANDROID_DEPTH_DEPTH_IS_EXCLUSIVE_FALSE) {
690 // Depth support is exclusive, nothing more to do.
691 return OK;
692 }
693 } else {
694 ALOGE("%s: Advertised depth exclusive tag but value is not present!", __FUNCTION__);
695 return BAD_VALUE;
696 }
697
698 getSupportedSizes(c, ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, HAL_PIXEL_FORMAT_BLOB,
699 &supportedBlobSizes);
700 getSupportedSizes(c, depthSizesTag, HAL_PIXEL_FORMAT_Y16, &supportedDepthSizes);
701 if (supportedBlobSizes.empty() || supportedDepthSizes.empty()) {
702 // Nothing to do in this case.
703 return OK;
704 }
705
706 getSupportedDynamicDepthSizes(supportedBlobSizes, supportedDepthSizes,
707 &supportedDynamicDepthSizes, &internalDepthSizes);
708 if (supportedDynamicDepthSizes.empty()) {
709 // Nothing more to do.
710 return OK;
711 }
712
713 if(!isDepthPhotoLibraryPresent()) {
714 // Depth photo processing library is not present, nothing more to do.
715 return OK;
716 }
717
718 std::vector<int32_t> dynamicDepthEntries;
719 for (const auto& it : supportedDynamicDepthSizes) {
720 int32_t entry[4] = {HAL_PIXEL_FORMAT_BLOB, static_cast<int32_t> (std::get<0>(it)),
721 static_cast<int32_t> (std::get<1>(it)),
722 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT };
723 dynamicDepthEntries.insert(dynamicDepthEntries.end(), entry, entry + 4);
724 }
725
726 std::vector<int64_t> depthMinDurations, depthStallDurations;
727 std::vector<int64_t> blobMinDurations, blobStallDurations;
728 std::vector<int64_t> dynamicDepthMinDurations, dynamicDepthStallDurations;
729
730 getSupportedDurations(c, ANDROID_DEPTH_AVAILABLE_DEPTH_MIN_FRAME_DURATIONS,
731 HAL_PIXEL_FORMAT_Y16, internalDepthSizes, &depthMinDurations);
732 getSupportedDurations(c, ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
733 HAL_PIXEL_FORMAT_BLOB, supportedDynamicDepthSizes, &blobMinDurations);
734 if (blobMinDurations.empty() || depthMinDurations.empty() ||
735 (depthMinDurations.size() != blobMinDurations.size())) {
736 ALOGE("%s: Unexpected number of available depth min durations! %zu vs. %zu",
737 __FUNCTION__, depthMinDurations.size(), blobMinDurations.size());
738 return BAD_VALUE;
739 }
740
741 getSupportedDurations(c, ANDROID_DEPTH_AVAILABLE_DEPTH_STALL_DURATIONS,
742 HAL_PIXEL_FORMAT_Y16, internalDepthSizes, &depthStallDurations);
743 getSupportedDurations(c, ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
744 HAL_PIXEL_FORMAT_BLOB, supportedDynamicDepthSizes, &blobStallDurations);
745 if (blobStallDurations.empty() || depthStallDurations.empty() ||
746 (depthStallDurations.size() != blobStallDurations.size())) {
747 ALOGE("%s: Unexpected number of available depth stall durations! %zu vs. %zu",
748 __FUNCTION__, depthStallDurations.size(), blobStallDurations.size());
749 return BAD_VALUE;
750 }
751
752 getSupportedDynamicDepthDurations(depthMinDurations, blobMinDurations,
753 &dynamicDepthMinDurations);
754 getSupportedDynamicDepthDurations(depthStallDurations, blobStallDurations,
755 &dynamicDepthStallDurations);
756 if (dynamicDepthMinDurations.empty() || dynamicDepthStallDurations.empty() ||
757 (dynamicDepthMinDurations.size() != dynamicDepthStallDurations.size())) {
758 ALOGE("%s: Unexpected number of dynamic depth stall/min durations! %zu vs. %zu",
759 __FUNCTION__, dynamicDepthMinDurations.size(), dynamicDepthStallDurations.size());
760 return BAD_VALUE;
761 }
762
763 std::vector<int64_t> dynamicDepthMinDurationEntries;
764 auto itDuration = dynamicDepthMinDurations.begin();
765 auto itSize = supportedDynamicDepthSizes.begin();
766 while (itDuration != dynamicDepthMinDurations.end()) {
767 int64_t entry[4] = {HAL_PIXEL_FORMAT_BLOB, static_cast<int32_t> (std::get<0>(*itSize)),
768 static_cast<int32_t> (std::get<1>(*itSize)), *itDuration};
769 dynamicDepthMinDurationEntries.insert(dynamicDepthMinDurationEntries.end(), entry,
770 entry + 4);
771 itDuration++; itSize++;
772 }
773
774 std::vector<int64_t> dynamicDepthStallDurationEntries;
775 itDuration = dynamicDepthStallDurations.begin();
776 itSize = supportedDynamicDepthSizes.begin();
777 while (itDuration != dynamicDepthStallDurations.end()) {
778 int64_t entry[4] = {HAL_PIXEL_FORMAT_BLOB, static_cast<int32_t> (std::get<0>(*itSize)),
779 static_cast<int32_t> (std::get<1>(*itSize)), *itDuration};
780 dynamicDepthStallDurationEntries.insert(dynamicDepthStallDurationEntries.end(), entry,
781 entry + 4);
782 itDuration++; itSize++;
783 }
784
785 c.update(ANDROID_DEPTH_AVAILABLE_DYNAMIC_DEPTH_STREAM_CONFIGURATIONS,
786 dynamicDepthEntries.data(), dynamicDepthEntries.size());
787 c.update(ANDROID_DEPTH_AVAILABLE_DYNAMIC_DEPTH_MIN_FRAME_DURATIONS,
788 dynamicDepthMinDurationEntries.data(), dynamicDepthMinDurationEntries.size());
789 c.update(ANDROID_DEPTH_AVAILABLE_DYNAMIC_DEPTH_STALL_DURATIONS,
790 dynamicDepthStallDurationEntries.data(), dynamicDepthStallDurationEntries.size());
791
792 std::vector<int32_t> supportedChTags;
793 supportedChTags.reserve(chTags.count + 3);
794 supportedChTags.insert(supportedChTags.end(), chTags.data.i32,
795 chTags.data.i32 + chTags.count);
796 supportedChTags.push_back(ANDROID_DEPTH_AVAILABLE_DYNAMIC_DEPTH_STREAM_CONFIGURATIONS);
797 supportedChTags.push_back(ANDROID_DEPTH_AVAILABLE_DYNAMIC_DEPTH_MIN_FRAME_DURATIONS);
798 supportedChTags.push_back(ANDROID_DEPTH_AVAILABLE_DYNAMIC_DEPTH_STALL_DURATIONS);
799 c.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, supportedChTags.data(),
800 supportedChTags.size());
801
802 return OK;
803 }
804
fixupMonochromeTags()805 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::fixupMonochromeTags() {
806 status_t res = OK;
807 auto& c = mCameraCharacteristics;
808
809 // Override static metadata for MONOCHROME camera with older device version
810 if (mVersion.get_major() == 3 && mVersion.get_minor() < 5) {
811 camera_metadata_entry cap = c.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
812 for (size_t i = 0; i < cap.count; i++) {
813 if (cap.data.u8[i] == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_MONOCHROME) {
814 // ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT
815 uint8_t cfa = ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT_MONO;
816 res = c.update(ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT, &cfa, 1);
817 if (res != OK) {
818 ALOGE("%s: Failed to update COLOR_FILTER_ARRANGEMENT: %s (%d)",
819 __FUNCTION__, strerror(-res), res);
820 return res;
821 }
822
823 // ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS
824 const std::vector<uint32_t> sKeys = {
825 ANDROID_SENSOR_REFERENCE_ILLUMINANT1,
826 ANDROID_SENSOR_REFERENCE_ILLUMINANT2,
827 ANDROID_SENSOR_CALIBRATION_TRANSFORM1,
828 ANDROID_SENSOR_CALIBRATION_TRANSFORM2,
829 ANDROID_SENSOR_COLOR_TRANSFORM1,
830 ANDROID_SENSOR_COLOR_TRANSFORM2,
831 ANDROID_SENSOR_FORWARD_MATRIX1,
832 ANDROID_SENSOR_FORWARD_MATRIX2,
833 };
834 res = removeAvailableKeys(c, sKeys,
835 ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
836 if (res != OK) {
837 ALOGE("%s: Failed to update REQUEST_AVAILABLE_CHARACTERISTICS_KEYS: %s (%d)",
838 __FUNCTION__, strerror(-res), res);
839 return res;
840 }
841
842 // ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS
843 const std::vector<uint32_t> reqKeys = {
844 ANDROID_COLOR_CORRECTION_MODE,
845 ANDROID_COLOR_CORRECTION_TRANSFORM,
846 ANDROID_COLOR_CORRECTION_GAINS,
847 };
848 res = removeAvailableKeys(c, reqKeys, ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS);
849 if (res != OK) {
850 ALOGE("%s: Failed to update REQUEST_AVAILABLE_REQUEST_KEYS: %s (%d)",
851 __FUNCTION__, strerror(-res), res);
852 return res;
853 }
854
855 // ANDROID_REQUEST_AVAILABLE_RESULT_KEYS
856 const std::vector<uint32_t> resKeys = {
857 ANDROID_SENSOR_GREEN_SPLIT,
858 ANDROID_SENSOR_NEUTRAL_COLOR_POINT,
859 ANDROID_COLOR_CORRECTION_MODE,
860 ANDROID_COLOR_CORRECTION_TRANSFORM,
861 ANDROID_COLOR_CORRECTION_GAINS,
862 };
863 res = removeAvailableKeys(c, resKeys, ANDROID_REQUEST_AVAILABLE_RESULT_KEYS);
864 if (res != OK) {
865 ALOGE("%s: Failed to update REQUEST_AVAILABLE_RESULT_KEYS: %s (%d)",
866 __FUNCTION__, strerror(-res), res);
867 return res;
868 }
869
870 // ANDROID_SENSOR_BLACK_LEVEL_PATTERN
871 camera_metadata_entry blEntry = c.find(ANDROID_SENSOR_BLACK_LEVEL_PATTERN);
872 for (size_t j = 1; j < blEntry.count; j++) {
873 blEntry.data.i32[j] = blEntry.data.i32[0];
874 }
875 }
876 }
877 }
878 return res;
879 }
880
removeAvailableKeys(CameraMetadata & c,const std::vector<uint32_t> & keys,uint32_t keyTag)881 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::removeAvailableKeys(
882 CameraMetadata& c, const std::vector<uint32_t>& keys, uint32_t keyTag) {
883 status_t res = OK;
884
885 camera_metadata_entry keysEntry = c.find(keyTag);
886 if (keysEntry.count == 0) {
887 ALOGE("%s: Failed to find tag %u: %s (%d)", __FUNCTION__, keyTag, strerror(-res), res);
888 return res;
889 }
890 std::vector<int32_t> vKeys;
891 vKeys.reserve(keysEntry.count);
892 for (size_t i = 0; i < keysEntry.count; i++) {
893 if (std::find(keys.begin(), keys.end(), keysEntry.data.i32[i]) == keys.end()) {
894 vKeys.push_back(keysEntry.data.i32[i]);
895 }
896 }
897 res = c.update(keyTag, vKeys.data(), vKeys.size());
898 return res;
899 }
900
fillHeicStreamCombinations(std::vector<int32_t> * outputs,std::vector<int64_t> * durations,std::vector<int64_t> * stallDurations,const camera_metadata_entry & halStreamConfigs,const camera_metadata_entry & halStreamDurations)901 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::fillHeicStreamCombinations(
902 std::vector<int32_t>* outputs,
903 std::vector<int64_t>* durations,
904 std::vector<int64_t>* stallDurations,
905 const camera_metadata_entry& halStreamConfigs,
906 const camera_metadata_entry& halStreamDurations) {
907 if (outputs == nullptr || durations == nullptr || stallDurations == nullptr) {
908 return BAD_VALUE;
909 }
910
911 static bool supportInMemoryTempFile =
912 camera3::HeicCompositeStream::isInMemoryTempFileSupported();
913 if (!supportInMemoryTempFile) {
914 ALOGI("%s: No HEIC support due to absence of in memory temp file support",
915 __FUNCTION__);
916 return OK;
917 }
918
919 for (size_t i = 0; i < halStreamConfigs.count; i += 4) {
920 int32_t format = halStreamConfigs.data.i32[i];
921 // Only IMPLEMENTATION_DEFINED and YUV_888 can be used to generate HEIC
922 // image.
923 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
924 format != HAL_PIXEL_FORMAT_YCBCR_420_888) {
925 continue;
926 }
927
928 bool sizeAvail = false;
929 for (size_t j = 0; j < outputs->size(); j+= 4) {
930 if ((*outputs)[j+1] == halStreamConfigs.data.i32[i+1] &&
931 (*outputs)[j+2] == halStreamConfigs.data.i32[i+2]) {
932 sizeAvail = true;
933 break;
934 }
935 }
936 if (sizeAvail) continue;
937
938 int64_t stall = 0;
939 bool useHeic, useGrid;
940 if (camera3::HeicCompositeStream::isSizeSupportedByHeifEncoder(
941 halStreamConfigs.data.i32[i+1], halStreamConfigs.data.i32[i+2],
942 &useHeic, &useGrid, &stall)) {
943 if (useGrid != (format == HAL_PIXEL_FORMAT_YCBCR_420_888)) {
944 continue;
945 }
946
947 // HEIC configuration
948 int32_t config[] = {HAL_PIXEL_FORMAT_BLOB, halStreamConfigs.data.i32[i+1],
949 halStreamConfigs.data.i32[i+2], 0 /*isInput*/};
950 outputs->insert(outputs->end(), config, config + 4);
951
952 // HEIC minFrameDuration
953 for (size_t j = 0; j < halStreamDurations.count; j += 4) {
954 if (halStreamDurations.data.i64[j] == format &&
955 halStreamDurations.data.i64[j+1] == halStreamConfigs.data.i32[i+1] &&
956 halStreamDurations.data.i64[j+2] == halStreamConfigs.data.i32[i+2]) {
957 int64_t duration[] = {HAL_PIXEL_FORMAT_BLOB, halStreamConfigs.data.i32[i+1],
958 halStreamConfigs.data.i32[i+2], halStreamDurations.data.i64[j+3]};
959 durations->insert(durations->end(), duration, duration+4);
960 break;
961 }
962 }
963
964 // HEIC stallDuration
965 int64_t stallDuration[] = {HAL_PIXEL_FORMAT_BLOB, halStreamConfigs.data.i32[i+1],
966 halStreamConfigs.data.i32[i+2], stall};
967 stallDurations->insert(stallDurations->end(), stallDuration, stallDuration+4);
968 }
969 }
970 return OK;
971 }
972
deriveHeicTags()973 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::deriveHeicTags() {
974 auto& c = mCameraCharacteristics;
975
976 camera_metadata_entry halHeicSupport = c.find(ANDROID_HEIC_INFO_SUPPORTED);
977 if (halHeicSupport.count > 1) {
978 ALOGE("%s: Invalid entry count %zu for ANDROID_HEIC_INFO_SUPPORTED",
979 __FUNCTION__, halHeicSupport.count);
980 return BAD_VALUE;
981 } else if (halHeicSupport.count == 0 ||
982 halHeicSupport.data.u8[0] == ANDROID_HEIC_INFO_SUPPORTED_FALSE) {
983 // Camera HAL doesn't support mandatory stream combinations for HEIC.
984 return OK;
985 }
986
987 camera_metadata_entry maxJpegAppsSegments =
988 c.find(ANDROID_HEIC_INFO_MAX_JPEG_APP_SEGMENTS_COUNT);
989 if (maxJpegAppsSegments.count != 1 || maxJpegAppsSegments.data.u8[0] == 0 ||
990 maxJpegAppsSegments.data.u8[0] > 16) {
991 ALOGE("%s: ANDROID_HEIC_INFO_MAX_JPEG_APP_SEGMENTS_COUNT must be within [1, 16]",
992 __FUNCTION__);
993 return BAD_VALUE;
994 }
995
996 // Populate HEIC output configurations and its related min frame duration
997 // and stall duration.
998 std::vector<int32_t> heicOutputs;
999 std::vector<int64_t> heicDurations;
1000 std::vector<int64_t> heicStallDurations;
1001
1002 camera_metadata_entry halStreamConfigs =
1003 c.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
1004 camera_metadata_entry minFrameDurations =
1005 c.find(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS);
1006
1007 status_t res = fillHeicStreamCombinations(&heicOutputs, &heicDurations, &heicStallDurations,
1008 halStreamConfigs, minFrameDurations);
1009 if (res != OK) {
1010 ALOGE("%s: Failed to fill HEIC stream combinations: %s (%d)", __FUNCTION__,
1011 strerror(-res), res);
1012 return res;
1013 }
1014
1015 c.update(ANDROID_HEIC_AVAILABLE_HEIC_STREAM_CONFIGURATIONS,
1016 heicOutputs.data(), heicOutputs.size());
1017 c.update(ANDROID_HEIC_AVAILABLE_HEIC_MIN_FRAME_DURATIONS,
1018 heicDurations.data(), heicDurations.size());
1019 c.update(ANDROID_HEIC_AVAILABLE_HEIC_STALL_DURATIONS,
1020 heicStallDurations.data(), heicStallDurations.size());
1021
1022 return OK;
1023 }
1024
isLogicalCamera(const std::string & id,std::vector<std::string> * physicalCameraIds)1025 bool CameraProviderManager::isLogicalCamera(const std::string& id,
1026 std::vector<std::string>* physicalCameraIds) {
1027 std::lock_guard<std::mutex> lock(mInterfaceMutex);
1028
1029 auto deviceInfo = findDeviceInfoLocked(id);
1030 if (deviceInfo == nullptr) return false;
1031
1032 if (deviceInfo->mIsLogicalCamera && physicalCameraIds != nullptr) {
1033 *physicalCameraIds = deviceInfo->mPhysicalIds;
1034 }
1035 return deviceInfo->mIsLogicalCamera;
1036 }
1037
isPublicallyHiddenSecureCamera(const std::string & id)1038 bool CameraProviderManager::isPublicallyHiddenSecureCamera(const std::string& id) {
1039 std::lock_guard<std::mutex> lock(mInterfaceMutex);
1040
1041 auto deviceInfo = findDeviceInfoLocked(id);
1042 if (deviceInfo == nullptr) {
1043 return false;
1044 }
1045 return deviceInfo->mIsPublicallyHiddenSecureCamera;
1046 }
1047
isHiddenPhysicalCamera(const std::string & cameraId)1048 bool CameraProviderManager::isHiddenPhysicalCamera(const std::string& cameraId) {
1049 for (auto& provider : mProviders) {
1050 for (auto& deviceInfo : provider->mDevices) {
1051 if (deviceInfo->mId == cameraId) {
1052 // cameraId is found in public camera IDs advertised by the
1053 // provider.
1054 return false;
1055 }
1056 }
1057 }
1058
1059 for (auto& provider : mProviders) {
1060 for (auto& deviceInfo : provider->mDevices) {
1061 CameraMetadata info;
1062 status_t res = deviceInfo->getCameraCharacteristics(&info);
1063 if (res != OK) {
1064 ALOGE("%s: Failed to getCameraCharacteristics for id %s", __FUNCTION__,
1065 deviceInfo->mId.c_str());
1066 return false;
1067 }
1068
1069 std::vector<std::string> physicalIds;
1070 if (deviceInfo->mIsLogicalCamera) {
1071 if (std::find(deviceInfo->mPhysicalIds.begin(), deviceInfo->mPhysicalIds.end(),
1072 cameraId) != deviceInfo->mPhysicalIds.end()) {
1073 int deviceVersion = HARDWARE_DEVICE_API_VERSION(
1074 deviceInfo->mVersion.get_major(), deviceInfo->mVersion.get_minor());
1075 if (deviceVersion < CAMERA_DEVICE_API_VERSION_3_5) {
1076 ALOGE("%s: Wrong deviceVersion %x for hiddenPhysicalCameraId %s",
1077 __FUNCTION__, deviceVersion, cameraId.c_str());
1078 return false;
1079 } else {
1080 return true;
1081 }
1082 }
1083 }
1084 }
1085 }
1086
1087 return false;
1088 }
1089
addProviderLocked(const std::string & newProvider,bool expected)1090 status_t CameraProviderManager::addProviderLocked(const std::string& newProvider, bool expected) {
1091 for (const auto& providerInfo : mProviders) {
1092 if (providerInfo->mProviderName == newProvider) {
1093 ALOGW("%s: Camera provider HAL with name '%s' already registered", __FUNCTION__,
1094 newProvider.c_str());
1095 return ALREADY_EXISTS;
1096 }
1097 }
1098
1099 sp<provider::V2_4::ICameraProvider> interface;
1100 interface = mServiceProxy->getService(newProvider);
1101
1102 if (interface == nullptr) {
1103 if (expected) {
1104 ALOGE("%s: Camera provider HAL '%s' is not actually available", __FUNCTION__,
1105 newProvider.c_str());
1106 return BAD_VALUE;
1107 } else {
1108 return OK;
1109 }
1110 }
1111
1112 sp<ProviderInfo> providerInfo = new ProviderInfo(newProvider, this);
1113 status_t res = providerInfo->initialize(interface, mDeviceState);
1114 if (res != OK) {
1115 return res;
1116 }
1117
1118 mProviders.push_back(providerInfo);
1119
1120 return OK;
1121 }
1122
removeProvider(const std::string & provider)1123 status_t CameraProviderManager::removeProvider(const std::string& provider) {
1124 std::lock_guard<std::mutex> providerLock(mProviderLifecycleLock);
1125 std::unique_lock<std::mutex> lock(mInterfaceMutex);
1126 std::vector<String8> removedDeviceIds;
1127 status_t res = NAME_NOT_FOUND;
1128 for (auto it = mProviders.begin(); it != mProviders.end(); it++) {
1129 if ((*it)->mProviderName == provider) {
1130 removedDeviceIds.reserve((*it)->mDevices.size());
1131 for (auto& deviceInfo : (*it)->mDevices) {
1132 removedDeviceIds.push_back(String8(deviceInfo->mId.c_str()));
1133 }
1134 mProviders.erase(it);
1135 res = OK;
1136 break;
1137 }
1138 }
1139 if (res != OK) {
1140 ALOGW("%s: Camera provider HAL with name '%s' is not registered", __FUNCTION__,
1141 provider.c_str());
1142 } else {
1143 // Inform camera service of loss of presence for all the devices from this provider,
1144 // without lock held for reentrancy
1145 sp<StatusListener> listener = getStatusListener();
1146 if (listener != nullptr) {
1147 lock.unlock();
1148 for (auto& id : removedDeviceIds) {
1149 listener->onDeviceStatusChanged(id, CameraDeviceStatus::NOT_PRESENT);
1150 }
1151 }
1152 }
1153 return res;
1154 }
1155
getStatusListener() const1156 sp<CameraProviderManager::StatusListener> CameraProviderManager::getStatusListener() const {
1157 return mListener.promote();
1158 }
1159
1160 /**** Methods for ProviderInfo ****/
1161
1162
ProviderInfo(const std::string & providerName,CameraProviderManager * manager)1163 CameraProviderManager::ProviderInfo::ProviderInfo(
1164 const std::string &providerName,
1165 CameraProviderManager *manager) :
1166 mProviderName(providerName),
1167 mProviderTagid(generateVendorTagId(providerName)),
1168 mUniqueDeviceCount(0),
1169 mManager(manager) {
1170 (void) mManager;
1171 }
1172
initialize(sp<provider::V2_4::ICameraProvider> & interface,hardware::hidl_bitfield<provider::V2_5::DeviceState> currentDeviceState)1173 status_t CameraProviderManager::ProviderInfo::initialize(
1174 sp<provider::V2_4::ICameraProvider>& interface,
1175 hardware::hidl_bitfield<provider::V2_5::DeviceState> currentDeviceState) {
1176 status_t res = parseProviderName(mProviderName, &mType, &mId);
1177 if (res != OK) {
1178 ALOGE("%s: Invalid provider name, ignoring", __FUNCTION__);
1179 return BAD_VALUE;
1180 }
1181 ALOGI("Connecting to new camera provider: %s, isRemote? %d",
1182 mProviderName.c_str(), interface->isRemote());
1183
1184 // Determine minor version
1185 auto castResult = provider::V2_5::ICameraProvider::castFrom(interface);
1186 if (castResult.isOk()) {
1187 mMinorVersion = 5;
1188 } else {
1189 mMinorVersion = 4;
1190 }
1191
1192 // cameraDeviceStatusChange callbacks may be called (and causing new devices added)
1193 // before setCallback returns
1194 hardware::Return<Status> status = interface->setCallback(this);
1195 if (!status.isOk()) {
1196 ALOGE("%s: Transaction error setting up callbacks with camera provider '%s': %s",
1197 __FUNCTION__, mProviderName.c_str(), status.description().c_str());
1198 return DEAD_OBJECT;
1199 }
1200 if (status != Status::OK) {
1201 ALOGE("%s: Unable to register callbacks with camera provider '%s'",
1202 __FUNCTION__, mProviderName.c_str());
1203 return mapToStatusT(status);
1204 }
1205
1206 hardware::Return<bool> linked = interface->linkToDeath(this, /*cookie*/ mId);
1207 if (!linked.isOk()) {
1208 ALOGE("%s: Transaction error in linking to camera provider '%s' death: %s",
1209 __FUNCTION__, mProviderName.c_str(), linked.description().c_str());
1210 return DEAD_OBJECT;
1211 } else if (!linked) {
1212 ALOGW("%s: Unable to link to provider '%s' death notifications",
1213 __FUNCTION__, mProviderName.c_str());
1214 }
1215
1216 if (!kEnableLazyHal) {
1217 // Save HAL reference indefinitely
1218 mSavedInterface = interface;
1219 } else {
1220 mActiveInterface = interface;
1221 }
1222
1223 ALOGV("%s: Setting device state for %s: 0x%" PRIx64,
1224 __FUNCTION__, mProviderName.c_str(), mDeviceState);
1225 notifyDeviceStateChange(currentDeviceState);
1226
1227 res = setUpVendorTags();
1228 if (res != OK) {
1229 ALOGE("%s: Unable to set up vendor tags from provider '%s'",
1230 __FUNCTION__, mProviderName.c_str());
1231 return res;
1232 }
1233
1234 // Get initial list of camera devices, if any
1235 std::vector<std::string> devices;
1236 hardware::Return<void> ret = interface->getCameraIdList([&status, this, &devices](
1237 Status idStatus,
1238 const hardware::hidl_vec<hardware::hidl_string>& cameraDeviceNames) {
1239 status = idStatus;
1240 if (status == Status::OK) {
1241 for (auto& name : cameraDeviceNames) {
1242 uint16_t major, minor;
1243 std::string type, id;
1244 status_t res = parseDeviceName(name, &major, &minor, &type, &id);
1245 if (res != OK) {
1246 ALOGE("%s: Error parsing deviceName: %s: %d", __FUNCTION__, name.c_str(), res);
1247 status = Status::INTERNAL_ERROR;
1248 } else {
1249 devices.push_back(name);
1250 mProviderPublicCameraIds.push_back(id);
1251 }
1252 }
1253 } });
1254 if (!ret.isOk()) {
1255 ALOGE("%s: Transaction error in getting camera ID list from provider '%s': %s",
1256 __FUNCTION__, mProviderName.c_str(), linked.description().c_str());
1257 return DEAD_OBJECT;
1258 }
1259 if (status != Status::OK) {
1260 ALOGE("%s: Unable to query for camera devices from provider '%s'",
1261 __FUNCTION__, mProviderName.c_str());
1262 return mapToStatusT(status);
1263 }
1264
1265 ret = interface->isSetTorchModeSupported(
1266 [this](auto status, bool supported) {
1267 if (status == Status::OK) {
1268 mSetTorchModeSupported = supported;
1269 }
1270 });
1271 if (!ret.isOk()) {
1272 ALOGE("%s: Transaction error checking torch mode support '%s': %s",
1273 __FUNCTION__, mProviderName.c_str(), ret.description().c_str());
1274 return DEAD_OBJECT;
1275 }
1276
1277 mIsRemote = interface->isRemote();
1278
1279 sp<StatusListener> listener = mManager->getStatusListener();
1280 for (auto& device : devices) {
1281 std::string id;
1282 status_t res = addDevice(device, common::V1_0::CameraDeviceStatus::PRESENT, &id);
1283 if (res != OK) {
1284 ALOGE("%s: Unable to enumerate camera device '%s': %s (%d)",
1285 __FUNCTION__, device.c_str(), strerror(-res), res);
1286 continue;
1287 }
1288 }
1289
1290 ALOGI("Camera provider %s ready with %zu camera devices",
1291 mProviderName.c_str(), mDevices.size());
1292
1293 mInitialized = true;
1294 return OK;
1295 }
1296
1297 const sp<provider::V2_4::ICameraProvider>
startProviderInterface()1298 CameraProviderManager::ProviderInfo::startProviderInterface() {
1299 ATRACE_CALL();
1300 ALOGV("Request to start camera provider: %s", mProviderName.c_str());
1301 if (mSavedInterface != nullptr) {
1302 return mSavedInterface;
1303 }
1304 if (!kEnableLazyHal) {
1305 ALOGE("Bad provider state! Should not be here on a non-lazy HAL!");
1306 return nullptr;
1307 }
1308
1309 auto interface = mActiveInterface.promote();
1310 if (interface == nullptr) {
1311 ALOGI("Camera HAL provider needs restart, calling getService(%s)", mProviderName.c_str());
1312 interface = mManager->mServiceProxy->getService(mProviderName);
1313 interface->setCallback(this);
1314 hardware::Return<bool> linked = interface->linkToDeath(this, /*cookie*/ mId);
1315 if (!linked.isOk()) {
1316 ALOGE("%s: Transaction error in linking to camera provider '%s' death: %s",
1317 __FUNCTION__, mProviderName.c_str(), linked.description().c_str());
1318 mManager->removeProvider(mProviderName);
1319 return nullptr;
1320 } else if (!linked) {
1321 ALOGW("%s: Unable to link to provider '%s' death notifications",
1322 __FUNCTION__, mProviderName.c_str());
1323 }
1324 // Send current device state
1325 if (mMinorVersion >= 5) {
1326 auto castResult = provider::V2_5::ICameraProvider::castFrom(interface);
1327 if (castResult.isOk()) {
1328 sp<provider::V2_5::ICameraProvider> interface_2_5 = castResult;
1329 if (interface_2_5 != nullptr) {
1330 ALOGV("%s: Initial device state for %s: 0x %" PRIx64,
1331 __FUNCTION__, mProviderName.c_str(), mDeviceState);
1332 interface_2_5->notifyDeviceStateChange(mDeviceState);
1333 }
1334 }
1335 }
1336
1337 mActiveInterface = interface;
1338 } else {
1339 ALOGV("Camera provider (%s) already in use. Re-using instance.", mProviderName.c_str());
1340 }
1341 return interface;
1342 }
1343
getType() const1344 const std::string& CameraProviderManager::ProviderInfo::getType() const {
1345 return mType;
1346 }
1347
addDevice(const std::string & name,CameraDeviceStatus initialStatus,std::string * parsedId)1348 status_t CameraProviderManager::ProviderInfo::addDevice(const std::string& name,
1349 CameraDeviceStatus initialStatus, /*out*/ std::string* parsedId) {
1350
1351 ALOGI("Enumerating new camera device: %s", name.c_str());
1352
1353 uint16_t major, minor;
1354 std::string type, id;
1355
1356 status_t res = parseDeviceName(name, &major, &minor, &type, &id);
1357 if (res != OK) {
1358 return res;
1359 }
1360 if (type != mType) {
1361 ALOGE("%s: Device type %s does not match provider type %s", __FUNCTION__,
1362 type.c_str(), mType.c_str());
1363 return BAD_VALUE;
1364 }
1365 if (mManager->isValidDeviceLocked(id, major)) {
1366 ALOGE("%s: Device %s: ID %s is already in use for device major version %d", __FUNCTION__,
1367 name.c_str(), id.c_str(), major);
1368 return BAD_VALUE;
1369 }
1370
1371 std::unique_ptr<DeviceInfo> deviceInfo;
1372 switch (major) {
1373 case 1:
1374 deviceInfo = initializeDeviceInfo<DeviceInfo1>(name, mProviderTagid,
1375 id, minor);
1376 break;
1377 case 3:
1378 deviceInfo = initializeDeviceInfo<DeviceInfo3>(name, mProviderTagid,
1379 id, minor);
1380 break;
1381 default:
1382 ALOGE("%s: Device %s: Unknown HIDL device HAL major version %d:", __FUNCTION__,
1383 name.c_str(), major);
1384 return BAD_VALUE;
1385 }
1386 if (deviceInfo == nullptr) return BAD_VALUE;
1387 deviceInfo->mStatus = initialStatus;
1388 bool isAPI1Compatible = deviceInfo->isAPI1Compatible();
1389
1390 mDevices.push_back(std::move(deviceInfo));
1391
1392 mUniqueCameraIds.insert(id);
1393 if (isAPI1Compatible) {
1394 // addDevice can be called more than once for the same camera id if HAL
1395 // supports openLegacy.
1396 if (std::find(mUniqueAPI1CompatibleCameraIds.begin(), mUniqueAPI1CompatibleCameraIds.end(),
1397 id) == mUniqueAPI1CompatibleCameraIds.end()) {
1398 mUniqueAPI1CompatibleCameraIds.push_back(id);
1399 }
1400 }
1401
1402 if (parsedId != nullptr) {
1403 *parsedId = id;
1404 }
1405 return OK;
1406 }
1407
removeDevice(std::string id)1408 void CameraProviderManager::ProviderInfo::removeDevice(std::string id) {
1409 for (auto it = mDevices.begin(); it != mDevices.end(); it++) {
1410 if ((*it)->mId == id) {
1411 mUniqueCameraIds.erase(id);
1412 if ((*it)->isAPI1Compatible()) {
1413 mUniqueAPI1CompatibleCameraIds.erase(std::remove(
1414 mUniqueAPI1CompatibleCameraIds.begin(),
1415 mUniqueAPI1CompatibleCameraIds.end(), id));
1416 }
1417 mDevices.erase(it);
1418 break;
1419 }
1420 }
1421 }
1422
dump(int fd,const Vector<String16> &) const1423 status_t CameraProviderManager::ProviderInfo::dump(int fd, const Vector<String16>&) const {
1424 dprintf(fd, "== Camera Provider HAL %s (v2.%d, %s) static info: %zu devices: ==\n",
1425 mProviderName.c_str(),
1426 mMinorVersion,
1427 mIsRemote ? "remote" : "passthrough",
1428 mDevices.size());
1429
1430 for (auto& device : mDevices) {
1431 dprintf(fd, "== Camera HAL device %s (v%d.%d) static information: ==\n", device->mName.c_str(),
1432 device->mVersion.get_major(), device->mVersion.get_minor());
1433 dprintf(fd, " Resource cost: %d\n", device->mResourceCost.resourceCost);
1434 if (device->mResourceCost.conflictingDevices.size() == 0) {
1435 dprintf(fd, " Conflicting devices: None\n");
1436 } else {
1437 dprintf(fd, " Conflicting devices:\n");
1438 for (size_t i = 0; i < device->mResourceCost.conflictingDevices.size(); i++) {
1439 dprintf(fd, " %s\n",
1440 device->mResourceCost.conflictingDevices[i].c_str());
1441 }
1442 }
1443 dprintf(fd, " API1 info:\n");
1444 dprintf(fd, " Has a flash unit: %s\n",
1445 device->hasFlashUnit() ? "true" : "false");
1446 hardware::CameraInfo info;
1447 status_t res = device->getCameraInfo(&info);
1448 if (res != OK) {
1449 dprintf(fd, " <Error reading camera info: %s (%d)>\n",
1450 strerror(-res), res);
1451 } else {
1452 dprintf(fd, " Facing: %s\n",
1453 info.facing == hardware::CAMERA_FACING_BACK ? "Back" : "Front");
1454 dprintf(fd, " Orientation: %d\n", info.orientation);
1455 }
1456 CameraMetadata info2;
1457 res = device->getCameraCharacteristics(&info2);
1458 if (res == INVALID_OPERATION) {
1459 dprintf(fd, " API2 not directly supported\n");
1460 } else if (res != OK) {
1461 dprintf(fd, " <Error reading camera characteristics: %s (%d)>\n",
1462 strerror(-res), res);
1463 } else {
1464 dprintf(fd, " API2 camera characteristics:\n");
1465 info2.dump(fd, /*verbosity*/ 2, /*indentation*/ 4);
1466 }
1467
1468 // Dump characteristics of non-standalone physical camera
1469 if (device->mIsLogicalCamera) {
1470 for (auto& id : device->mPhysicalIds) {
1471 // Skip if physical id is an independent camera
1472 if (std::find(mProviderPublicCameraIds.begin(), mProviderPublicCameraIds.end(), id)
1473 != mProviderPublicCameraIds.end()) {
1474 continue;
1475 }
1476
1477 CameraMetadata physicalInfo;
1478 status_t status = device->getPhysicalCameraCharacteristics(id, &physicalInfo);
1479 if (status == OK) {
1480 dprintf(fd, " Physical camera %s characteristics:\n", id.c_str());
1481 physicalInfo.dump(fd, /*verbosity*/ 2, /*indentation*/ 4);
1482 }
1483 }
1484 }
1485
1486 dprintf(fd, "== Camera HAL device %s (v%d.%d) dumpState: ==\n", device->mName.c_str(),
1487 device->mVersion.get_major(), device->mVersion.get_minor());
1488 res = device->dumpState(fd);
1489 if (res != OK) {
1490 dprintf(fd, " <Error dumping device %s state: %s (%d)>\n",
1491 device->mName.c_str(), strerror(-res), res);
1492 }
1493 }
1494 return OK;
1495 }
1496
cameraDeviceStatusChange(const hardware::hidl_string & cameraDeviceName,CameraDeviceStatus newStatus)1497 hardware::Return<void> CameraProviderManager::ProviderInfo::cameraDeviceStatusChange(
1498 const hardware::hidl_string& cameraDeviceName,
1499 CameraDeviceStatus newStatus) {
1500 sp<StatusListener> listener;
1501 std::string id;
1502 bool initialized = false;
1503 {
1504 std::lock_guard<std::mutex> lock(mLock);
1505 bool known = false;
1506 for (auto& deviceInfo : mDevices) {
1507 if (deviceInfo->mName == cameraDeviceName) {
1508 ALOGI("Camera device %s status is now %s, was %s", cameraDeviceName.c_str(),
1509 deviceStatusToString(newStatus), deviceStatusToString(deviceInfo->mStatus));
1510 deviceInfo->mStatus = newStatus;
1511 // TODO: Handle device removal (NOT_PRESENT)
1512 id = deviceInfo->mId;
1513 known = true;
1514 break;
1515 }
1516 }
1517 // Previously unseen device; status must not be NOT_PRESENT
1518 if (!known) {
1519 if (newStatus == CameraDeviceStatus::NOT_PRESENT) {
1520 ALOGW("Camera provider %s says an unknown camera device %s is not present. Curious.",
1521 mProviderName.c_str(), cameraDeviceName.c_str());
1522 return hardware::Void();
1523 }
1524 addDevice(cameraDeviceName, newStatus, &id);
1525 } else if (newStatus == CameraDeviceStatus::NOT_PRESENT) {
1526 removeDevice(id);
1527 }
1528 listener = mManager->getStatusListener();
1529 initialized = mInitialized;
1530 }
1531 // Call without lock held to allow reentrancy into provider manager
1532 // Don't send the callback if providerInfo hasn't been initialized.
1533 // CameraService will initialize device status after provider is
1534 // initialized
1535 if (listener != nullptr && initialized) {
1536 listener->onDeviceStatusChanged(String8(id.c_str()), newStatus);
1537 }
1538 return hardware::Void();
1539 }
1540
torchModeStatusChange(const hardware::hidl_string & cameraDeviceName,TorchModeStatus newStatus)1541 hardware::Return<void> CameraProviderManager::ProviderInfo::torchModeStatusChange(
1542 const hardware::hidl_string& cameraDeviceName,
1543 TorchModeStatus newStatus) {
1544 sp<StatusListener> listener;
1545 std::string id;
1546 {
1547 std::lock_guard<std::mutex> lock(mManager->mStatusListenerMutex);
1548 bool known = false;
1549 for (auto& deviceInfo : mDevices) {
1550 if (deviceInfo->mName == cameraDeviceName) {
1551 ALOGI("Camera device %s torch status is now %s", cameraDeviceName.c_str(),
1552 torchStatusToString(newStatus));
1553 id = deviceInfo->mId;
1554 known = true;
1555 if (TorchModeStatus::AVAILABLE_ON != newStatus) {
1556 mManager->removeRef(DeviceMode::TORCH, id);
1557 }
1558 break;
1559 }
1560 }
1561 if (!known) {
1562 ALOGW("Camera provider %s says an unknown camera %s now has torch status %d. Curious.",
1563 mProviderName.c_str(), cameraDeviceName.c_str(), newStatus);
1564 return hardware::Void();
1565 }
1566 listener = mManager->getStatusListener();
1567 }
1568 // Call without lock held to allow reentrancy into provider manager
1569 if (listener != nullptr) {
1570 listener->onTorchStatusChanged(String8(id.c_str()), newStatus);
1571 }
1572 return hardware::Void();
1573 }
1574
serviceDied(uint64_t cookie,const wp<hidl::base::V1_0::IBase> & who)1575 void CameraProviderManager::ProviderInfo::serviceDied(uint64_t cookie,
1576 const wp<hidl::base::V1_0::IBase>& who) {
1577 (void) who;
1578 ALOGI("Camera provider '%s' has died; removing it", mProviderName.c_str());
1579 if (cookie != mId) {
1580 ALOGW("%s: Unexpected serviceDied cookie %" PRIu64 ", expected %" PRIu32,
1581 __FUNCTION__, cookie, mId);
1582 }
1583 mManager->removeProvider(mProviderName);
1584 }
1585
setUpVendorTags()1586 status_t CameraProviderManager::ProviderInfo::setUpVendorTags() {
1587 if (mVendorTagDescriptor != nullptr)
1588 return OK;
1589
1590 hardware::hidl_vec<VendorTagSection> vts;
1591 Status status;
1592 hardware::Return<void> ret;
1593 const sp<provider::V2_4::ICameraProvider> interface = startProviderInterface();
1594 if (interface == nullptr) {
1595 return DEAD_OBJECT;
1596 }
1597 ret = interface->getVendorTags(
1598 [&](auto s, const auto& vendorTagSecs) {
1599 status = s;
1600 if (s == Status::OK) {
1601 vts = vendorTagSecs;
1602 }
1603 });
1604 if (!ret.isOk()) {
1605 ALOGE("%s: Transaction error getting vendor tags from provider '%s': %s",
1606 __FUNCTION__, mProviderName.c_str(), ret.description().c_str());
1607 return DEAD_OBJECT;
1608 }
1609 if (status != Status::OK) {
1610 return mapToStatusT(status);
1611 }
1612
1613 // Read all vendor tag definitions into a descriptor
1614 status_t res;
1615 if ((res = HidlVendorTagDescriptor::createDescriptorFromHidl(vts, /*out*/mVendorTagDescriptor))
1616 != OK) {
1617 ALOGE("%s: Could not generate descriptor from vendor tag operations,"
1618 "received error %s (%d). Camera clients will not be able to use"
1619 "vendor tags", __FUNCTION__, strerror(res), res);
1620 return res;
1621 }
1622
1623 return OK;
1624 }
1625
notifyDeviceStateChange(hardware::hidl_bitfield<provider::V2_5::DeviceState> newDeviceState)1626 status_t CameraProviderManager::ProviderInfo::notifyDeviceStateChange(
1627 hardware::hidl_bitfield<provider::V2_5::DeviceState> newDeviceState) {
1628 mDeviceState = newDeviceState;
1629 if (mMinorVersion >= 5) {
1630 // Check if the provider is currently active - not going to start it up for this notification
1631 auto interface = mSavedInterface != nullptr ? mSavedInterface : mActiveInterface.promote();
1632 if (interface != nullptr) {
1633 // Send current device state
1634 auto castResult = provider::V2_5::ICameraProvider::castFrom(interface);
1635 if (castResult.isOk()) {
1636 sp<provider::V2_5::ICameraProvider> interface_2_5 = castResult;
1637 if (interface_2_5 != nullptr) {
1638 interface_2_5->notifyDeviceStateChange(mDeviceState);
1639 }
1640 }
1641 }
1642 }
1643 return OK;
1644 }
1645
1646 template<class DeviceInfoT>
1647 std::unique_ptr<CameraProviderManager::ProviderInfo::DeviceInfo>
initializeDeviceInfo(const std::string & name,const metadata_vendor_id_t tagId,const std::string & id,uint16_t minorVersion)1648 CameraProviderManager::ProviderInfo::initializeDeviceInfo(
1649 const std::string &name, const metadata_vendor_id_t tagId,
1650 const std::string &id, uint16_t minorVersion) {
1651 Status status;
1652
1653 auto cameraInterface =
1654 startDeviceInterface<typename DeviceInfoT::InterfaceT>(name);
1655 if (cameraInterface == nullptr) return nullptr;
1656
1657 CameraResourceCost resourceCost;
1658 cameraInterface->getResourceCost([&status, &resourceCost](
1659 Status s, CameraResourceCost cost) {
1660 status = s;
1661 resourceCost = cost;
1662 });
1663 if (status != Status::OK) {
1664 ALOGE("%s: Unable to obtain resource costs for camera device %s: %s", __FUNCTION__,
1665 name.c_str(), statusToString(status));
1666 return nullptr;
1667 }
1668
1669 for (auto& conflictName : resourceCost.conflictingDevices) {
1670 uint16_t major, minor;
1671 std::string type, id;
1672 status_t res = parseDeviceName(conflictName, &major, &minor, &type, &id);
1673 if (res != OK) {
1674 ALOGE("%s: Failed to parse conflicting device %s", __FUNCTION__, conflictName.c_str());
1675 return nullptr;
1676 }
1677 conflictName = id;
1678 }
1679
1680 return std::unique_ptr<DeviceInfo>(
1681 new DeviceInfoT(name, tagId, id, minorVersion, resourceCost, this,
1682 mProviderPublicCameraIds, cameraInterface));
1683 }
1684
1685 template<class InterfaceT>
1686 sp<InterfaceT>
startDeviceInterface(const std::string & name)1687 CameraProviderManager::ProviderInfo::startDeviceInterface(const std::string &name) {
1688 ALOGE("%s: Device %s: Unknown HIDL device HAL major version %d:", __FUNCTION__,
1689 name.c_str(), InterfaceT::version.get_major());
1690 return nullptr;
1691 }
1692
1693 template<>
1694 sp<device::V1_0::ICameraDevice>
startDeviceInterface(const std::string & name)1695 CameraProviderManager::ProviderInfo::startDeviceInterface
1696 <device::V1_0::ICameraDevice>(const std::string &name) {
1697 Status status;
1698 sp<device::V1_0::ICameraDevice> cameraInterface;
1699 hardware::Return<void> ret;
1700 const sp<provider::V2_4::ICameraProvider> interface = startProviderInterface();
1701 if (interface == nullptr) {
1702 return nullptr;
1703 }
1704 ret = interface->getCameraDeviceInterface_V1_x(name, [&status, &cameraInterface](
1705 Status s, sp<device::V1_0::ICameraDevice> interface) {
1706 status = s;
1707 cameraInterface = interface;
1708 });
1709 if (!ret.isOk()) {
1710 ALOGE("%s: Transaction error trying to obtain interface for camera device %s: %s",
1711 __FUNCTION__, name.c_str(), ret.description().c_str());
1712 return nullptr;
1713 }
1714 if (status != Status::OK) {
1715 ALOGE("%s: Unable to obtain interface for camera device %s: %s", __FUNCTION__,
1716 name.c_str(), statusToString(status));
1717 return nullptr;
1718 }
1719 return cameraInterface;
1720 }
1721
1722 template<>
1723 sp<device::V3_2::ICameraDevice>
startDeviceInterface(const std::string & name)1724 CameraProviderManager::ProviderInfo::startDeviceInterface
1725 <device::V3_2::ICameraDevice>(const std::string &name) {
1726 Status status;
1727 sp<device::V3_2::ICameraDevice> cameraInterface;
1728 hardware::Return<void> ret;
1729 const sp<provider::V2_4::ICameraProvider> interface = startProviderInterface();
1730 if (interface == nullptr) {
1731 return nullptr;
1732 }
1733 ret = interface->getCameraDeviceInterface_V3_x(name, [&status, &cameraInterface](
1734 Status s, sp<device::V3_2::ICameraDevice> interface) {
1735 status = s;
1736 cameraInterface = interface;
1737 });
1738 if (!ret.isOk()) {
1739 ALOGE("%s: Transaction error trying to obtain interface for camera device %s: %s",
1740 __FUNCTION__, name.c_str(), ret.description().c_str());
1741 return nullptr;
1742 }
1743 if (status != Status::OK) {
1744 ALOGE("%s: Unable to obtain interface for camera device %s: %s", __FUNCTION__,
1745 name.c_str(), statusToString(status));
1746 return nullptr;
1747 }
1748 return cameraInterface;
1749 }
1750
~DeviceInfo()1751 CameraProviderManager::ProviderInfo::DeviceInfo::~DeviceInfo() {}
1752
1753 template<class InterfaceT>
startDeviceInterface()1754 sp<InterfaceT> CameraProviderManager::ProviderInfo::DeviceInfo::startDeviceInterface() {
1755 sp<InterfaceT> device;
1756 ATRACE_CALL();
1757 if (mSavedInterface == nullptr) {
1758 device = mParentProvider->startDeviceInterface<InterfaceT>(mName);
1759 } else {
1760 device = (InterfaceT *) mSavedInterface.get();
1761 }
1762 return device;
1763 }
1764
1765 template<class InterfaceT>
setTorchMode(InterfaceT & interface,bool enabled)1766 status_t CameraProviderManager::ProviderInfo::DeviceInfo::setTorchMode(InterfaceT& interface,
1767 bool enabled) {
1768 Status s = interface->setTorchMode(enabled ? TorchMode::ON : TorchMode::OFF);
1769 return mapToStatusT(s);
1770 }
1771
DeviceInfo1(const std::string & name,const metadata_vendor_id_t tagId,const std::string & id,uint16_t minorVersion,const CameraResourceCost & resourceCost,sp<ProviderInfo> parentProvider,const std::vector<std::string> & publicCameraIds,sp<InterfaceT> interface)1772 CameraProviderManager::ProviderInfo::DeviceInfo1::DeviceInfo1(const std::string& name,
1773 const metadata_vendor_id_t tagId, const std::string &id,
1774 uint16_t minorVersion,
1775 const CameraResourceCost& resourceCost,
1776 sp<ProviderInfo> parentProvider,
1777 const std::vector<std::string>& publicCameraIds,
1778 sp<InterfaceT> interface) :
1779 DeviceInfo(name, tagId, id, hardware::hidl_version{1, minorVersion},
1780 publicCameraIds, resourceCost, parentProvider) {
1781 // Get default parameters and initialize flash unit availability
1782 // Requires powering on the camera device
1783 hardware::Return<Status> status = interface->open(nullptr);
1784 if (!status.isOk()) {
1785 ALOGE("%s: Transaction error opening camera device %s to check for a flash unit: %s",
1786 __FUNCTION__, id.c_str(), status.description().c_str());
1787 return;
1788 }
1789 if (status != Status::OK) {
1790 ALOGE("%s: Unable to open camera device %s to check for a flash unit: %s", __FUNCTION__,
1791 id.c_str(), CameraProviderManager::statusToString(status));
1792 return;
1793 }
1794 hardware::Return<void> ret;
__anonbbf989410a02(const hardware::hidl_string& parms) 1795 ret = interface->getParameters([this](const hardware::hidl_string& parms) {
1796 mDefaultParameters.unflatten(String8(parms.c_str()));
1797 });
1798 if (!ret.isOk()) {
1799 ALOGE("%s: Transaction error reading camera device %s params to check for a flash unit: %s",
1800 __FUNCTION__, id.c_str(), status.description().c_str());
1801 return;
1802 }
1803 const char *flashMode =
1804 mDefaultParameters.get(CameraParameters::KEY_SUPPORTED_FLASH_MODES);
1805 if (flashMode && strstr(flashMode, CameraParameters::FLASH_MODE_TORCH)) {
1806 mHasFlashUnit = true;
1807 }
1808
1809 status_t res = cacheCameraInfo(interface);
1810 if (res != OK) {
1811 ALOGE("%s: Could not cache CameraInfo", __FUNCTION__);
1812 return;
1813 }
1814
1815 ret = interface->close();
1816 if (!ret.isOk()) {
1817 ALOGE("%s: Transaction error closing camera device %s after check for a flash unit: %s",
1818 __FUNCTION__, id.c_str(), status.description().c_str());
1819 }
1820
1821 if (!kEnableLazyHal) {
1822 // Save HAL reference indefinitely
1823 mSavedInterface = interface;
1824 }
1825 }
1826
~DeviceInfo1()1827 CameraProviderManager::ProviderInfo::DeviceInfo1::~DeviceInfo1() {}
1828
setTorchMode(bool enabled)1829 status_t CameraProviderManager::ProviderInfo::DeviceInfo1::setTorchMode(bool enabled) {
1830 return setTorchModeForDevice<InterfaceT>(enabled);
1831 }
1832
getCameraInfo(hardware::CameraInfo * info) const1833 status_t CameraProviderManager::ProviderInfo::DeviceInfo1::getCameraInfo(
1834 hardware::CameraInfo *info) const {
1835 if (info == nullptr) return BAD_VALUE;
1836 *info = mInfo;
1837 return OK;
1838 }
1839
cacheCameraInfo(sp<CameraProviderManager::ProviderInfo::DeviceInfo1::InterfaceT> interface)1840 status_t CameraProviderManager::ProviderInfo::DeviceInfo1::cacheCameraInfo(
1841 sp<CameraProviderManager::ProviderInfo::DeviceInfo1::InterfaceT> interface) {
1842 Status status;
1843 device::V1_0::CameraInfo cInfo;
1844 hardware::Return<void> ret;
1845 ret = interface->getCameraInfo([&status, &cInfo](Status s, device::V1_0::CameraInfo camInfo) {
1846 status = s;
1847 cInfo = camInfo;
1848 });
1849 if (!ret.isOk()) {
1850 ALOGE("%s: Transaction error reading camera info from device %s: %s",
1851 __FUNCTION__, mId.c_str(), ret.description().c_str());
1852 return DEAD_OBJECT;
1853 }
1854 if (status != Status::OK) {
1855 return mapToStatusT(status);
1856 }
1857
1858 switch(cInfo.facing) {
1859 case device::V1_0::CameraFacing::BACK:
1860 mInfo.facing = hardware::CAMERA_FACING_BACK;
1861 break;
1862 case device::V1_0::CameraFacing::EXTERNAL:
1863 // Map external to front for legacy API
1864 case device::V1_0::CameraFacing::FRONT:
1865 mInfo.facing = hardware::CAMERA_FACING_FRONT;
1866 break;
1867 default:
1868 ALOGW("%s: Device %s: Unknown camera facing: %d",
1869 __FUNCTION__, mId.c_str(), cInfo.facing);
1870 mInfo.facing = hardware::CAMERA_FACING_BACK;
1871 }
1872 mInfo.orientation = cInfo.orientation;
1873
1874 return OK;
1875 }
1876
dumpState(int fd)1877 status_t CameraProviderManager::ProviderInfo::DeviceInfo1::dumpState(int fd) {
1878 native_handle_t* handle = native_handle_create(1,0);
1879 handle->data[0] = fd;
1880 const sp<InterfaceT> interface = startDeviceInterface<InterfaceT>();
1881 if (interface == nullptr) {
1882 return DEAD_OBJECT;
1883 }
1884 hardware::Return<Status> s = interface->dumpState(handle);
1885 native_handle_delete(handle);
1886 if (!s.isOk()) {
1887 return INVALID_OPERATION;
1888 }
1889 return mapToStatusT(s);
1890 }
1891
DeviceInfo3(const std::string & name,const metadata_vendor_id_t tagId,const std::string & id,uint16_t minorVersion,const CameraResourceCost & resourceCost,sp<ProviderInfo> parentProvider,const std::vector<std::string> & publicCameraIds,sp<InterfaceT> interface)1892 CameraProviderManager::ProviderInfo::DeviceInfo3::DeviceInfo3(const std::string& name,
1893 const metadata_vendor_id_t tagId, const std::string &id,
1894 uint16_t minorVersion,
1895 const CameraResourceCost& resourceCost,
1896 sp<ProviderInfo> parentProvider,
1897 const std::vector<std::string>& publicCameraIds,
1898 sp<InterfaceT> interface) :
1899 DeviceInfo(name, tagId, id, hardware::hidl_version{3, minorVersion},
1900 publicCameraIds, resourceCost, parentProvider) {
1901 // Get camera characteristics and initialize flash unit availability
1902 Status status;
1903 hardware::Return<void> ret;
1904 ret = interface->getCameraCharacteristics([&status, this](Status s,
__anonbbf989410c02(Status s, device::V3_2::CameraMetadata metadata) 1905 device::V3_2::CameraMetadata metadata) {
1906 status = s;
1907 if (s == Status::OK) {
1908 camera_metadata_t *buffer =
1909 reinterpret_cast<camera_metadata_t*>(metadata.data());
1910 size_t expectedSize = metadata.size();
1911 int res = validate_camera_metadata_structure(buffer, &expectedSize);
1912 if (res == OK || res == CAMERA_METADATA_VALIDATION_SHIFTED) {
1913 set_camera_metadata_vendor_id(buffer, mProviderTagid);
1914 mCameraCharacteristics = buffer;
1915 } else {
1916 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
1917 status = Status::INTERNAL_ERROR;
1918 }
1919 }
1920 });
1921 if (!ret.isOk()) {
1922 ALOGE("%s: Transaction error getting camera characteristics for device %s"
1923 " to check for a flash unit: %s", __FUNCTION__, id.c_str(),
1924 ret.description().c_str());
1925 return;
1926 }
1927 if (status != Status::OK) {
1928 ALOGE("%s: Unable to get camera characteristics for device %s: %s (%d)",
1929 __FUNCTION__, id.c_str(), CameraProviderManager::statusToString(status), status);
1930 return;
1931 }
1932
1933 mIsPublicallyHiddenSecureCamera = isPublicallyHiddenSecureCamera();
1934
1935 status_t res = fixupMonochromeTags();
1936 if (OK != res) {
1937 ALOGE("%s: Unable to fix up monochrome tags based for older HAL version: %s (%d)",
1938 __FUNCTION__, strerror(-res), res);
1939 return;
1940 }
1941 auto stat = addDynamicDepthTags();
1942 if (OK != stat) {
1943 ALOGE("%s: Failed appending dynamic depth tags: %s (%d)", __FUNCTION__, strerror(-stat),
1944 stat);
1945 }
1946 res = deriveHeicTags();
1947 if (OK != res) {
1948 ALOGE("%s: Unable to derive HEIC tags based on camera and media capabilities: %s (%d)",
1949 __FUNCTION__, strerror(-res), res);
1950 }
1951
1952 camera_metadata_entry flashAvailable =
1953 mCameraCharacteristics.find(ANDROID_FLASH_INFO_AVAILABLE);
1954 if (flashAvailable.count == 1 &&
1955 flashAvailable.data.u8[0] == ANDROID_FLASH_INFO_AVAILABLE_TRUE) {
1956 mHasFlashUnit = true;
1957 } else {
1958 mHasFlashUnit = false;
1959 }
1960
1961 queryPhysicalCameraIds();
1962
1963 // Get physical camera characteristics if applicable
1964 auto castResult = device::V3_5::ICameraDevice::castFrom(interface);
1965 if (!castResult.isOk()) {
1966 ALOGV("%s: Unable to convert ICameraDevice instance to version 3.5", __FUNCTION__);
1967 return;
1968 }
1969 sp<device::V3_5::ICameraDevice> interface_3_5 = castResult;
1970 if (interface_3_5 == nullptr) {
1971 ALOGE("%s: Converted ICameraDevice instance to nullptr", __FUNCTION__);
1972 return;
1973 }
1974
1975 if (mIsLogicalCamera) {
1976 for (auto& id : mPhysicalIds) {
1977 if (std::find(mPublicCameraIds.begin(), mPublicCameraIds.end(), id) !=
1978 mPublicCameraIds.end()) {
1979 continue;
1980 }
1981
1982 hardware::hidl_string hidlId(id);
1983 ret = interface_3_5->getPhysicalCameraCharacteristics(hidlId,
__anonbbf989410d02(Status s, device::V3_2::CameraMetadata metadata) 1984 [&status, &id, this](Status s, device::V3_2::CameraMetadata metadata) {
1985 status = s;
1986 if (s == Status::OK) {
1987 camera_metadata_t *buffer =
1988 reinterpret_cast<camera_metadata_t*>(metadata.data());
1989 size_t expectedSize = metadata.size();
1990 int res = validate_camera_metadata_structure(buffer, &expectedSize);
1991 if (res == OK || res == CAMERA_METADATA_VALIDATION_SHIFTED) {
1992 set_camera_metadata_vendor_id(buffer, mProviderTagid);
1993 mPhysicalCameraCharacteristics[id] = buffer;
1994 } else {
1995 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
1996 status = Status::INTERNAL_ERROR;
1997 }
1998 }
1999 });
2000
2001 if (!ret.isOk()) {
2002 ALOGE("%s: Transaction error getting physical camera %s characteristics for %s: %s",
2003 __FUNCTION__, id.c_str(), id.c_str(), ret.description().c_str());
2004 return;
2005 }
2006 if (status != Status::OK) {
2007 ALOGE("%s: Unable to get physical camera %s characteristics for device %s: %s (%d)",
2008 __FUNCTION__, id.c_str(), mId.c_str(),
2009 CameraProviderManager::statusToString(status), status);
2010 return;
2011 }
2012 }
2013 }
2014
2015 if (!kEnableLazyHal) {
2016 // Save HAL reference indefinitely
2017 mSavedInterface = interface;
2018 }
2019 }
2020
~DeviceInfo3()2021 CameraProviderManager::ProviderInfo::DeviceInfo3::~DeviceInfo3() {}
2022
setTorchMode(bool enabled)2023 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::setTorchMode(bool enabled) {
2024 return setTorchModeForDevice<InterfaceT>(enabled);
2025 }
2026
getCameraInfo(hardware::CameraInfo * info) const2027 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::getCameraInfo(
2028 hardware::CameraInfo *info) const {
2029 if (info == nullptr) return BAD_VALUE;
2030
2031 camera_metadata_ro_entry facing =
2032 mCameraCharacteristics.find(ANDROID_LENS_FACING);
2033 if (facing.count == 1) {
2034 switch (facing.data.u8[0]) {
2035 case ANDROID_LENS_FACING_BACK:
2036 info->facing = hardware::CAMERA_FACING_BACK;
2037 break;
2038 case ANDROID_LENS_FACING_EXTERNAL:
2039 // Map external to front for legacy API
2040 case ANDROID_LENS_FACING_FRONT:
2041 info->facing = hardware::CAMERA_FACING_FRONT;
2042 break;
2043 }
2044 } else {
2045 ALOGE("%s: Unable to find android.lens.facing static metadata", __FUNCTION__);
2046 return NAME_NOT_FOUND;
2047 }
2048
2049 camera_metadata_ro_entry orientation =
2050 mCameraCharacteristics.find(ANDROID_SENSOR_ORIENTATION);
2051 if (orientation.count == 1) {
2052 info->orientation = orientation.data.i32[0];
2053 } else {
2054 ALOGE("%s: Unable to find android.sensor.orientation static metadata", __FUNCTION__);
2055 return NAME_NOT_FOUND;
2056 }
2057
2058 return OK;
2059 }
isAPI1Compatible() const2060 bool CameraProviderManager::ProviderInfo::DeviceInfo3::isAPI1Compatible() const {
2061 bool isBackwardCompatible = false;
2062 camera_metadata_ro_entry_t caps = mCameraCharacteristics.find(
2063 ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
2064 for (size_t i = 0; i < caps.count; i++) {
2065 if (caps.data.u8[i] ==
2066 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) {
2067 isBackwardCompatible = true;
2068 break;
2069 }
2070 }
2071
2072 return isBackwardCompatible;
2073 }
2074
dumpState(int fd)2075 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::dumpState(int fd) {
2076 native_handle_t* handle = native_handle_create(1,0);
2077 handle->data[0] = fd;
2078 const sp<InterfaceT> interface = startDeviceInterface<InterfaceT>();
2079 if (interface == nullptr) {
2080 return DEAD_OBJECT;
2081 }
2082 auto ret = interface->dumpState(handle);
2083 native_handle_delete(handle);
2084 if (!ret.isOk()) {
2085 return INVALID_OPERATION;
2086 }
2087 return OK;
2088 }
2089
getCameraCharacteristics(CameraMetadata * characteristics) const2090 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::getCameraCharacteristics(
2091 CameraMetadata *characteristics) const {
2092 if (characteristics == nullptr) return BAD_VALUE;
2093
2094 *characteristics = mCameraCharacteristics;
2095 return OK;
2096 }
2097
getPhysicalCameraCharacteristics(const std::string & physicalCameraId,CameraMetadata * characteristics) const2098 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::getPhysicalCameraCharacteristics(
2099 const std::string& physicalCameraId, CameraMetadata *characteristics) const {
2100 if (characteristics == nullptr) return BAD_VALUE;
2101 if (mPhysicalCameraCharacteristics.find(physicalCameraId) ==
2102 mPhysicalCameraCharacteristics.end()) {
2103 return NAME_NOT_FOUND;
2104 }
2105
2106 *characteristics = mPhysicalCameraCharacteristics.at(physicalCameraId);
2107 return OK;
2108 }
2109
isSessionConfigurationSupported(const hardware::camera::device::V3_4::StreamConfiguration & configuration,bool * status)2110 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::isSessionConfigurationSupported(
2111 const hardware::camera::device::V3_4::StreamConfiguration &configuration,
2112 bool *status /*out*/) {
2113
2114 const sp<CameraProviderManager::ProviderInfo::DeviceInfo3::InterfaceT> interface =
2115 this->startDeviceInterface<CameraProviderManager::ProviderInfo::DeviceInfo3::InterfaceT>();
2116 if (interface == nullptr) {
2117 return DEAD_OBJECT;
2118 }
2119 auto castResult = device::V3_5::ICameraDevice::castFrom(interface);
2120 sp<hardware::camera::device::V3_5::ICameraDevice> interface_3_5 = castResult;
2121 if (interface_3_5 == nullptr) {
2122 return INVALID_OPERATION;
2123 }
2124
2125 status_t res;
2126 Status callStatus;
2127 auto ret = interface_3_5->isStreamCombinationSupported(configuration,
2128 [&callStatus, &status] (Status s, bool combStatus) {
2129 callStatus = s;
2130 *status = combStatus;
2131 });
2132 if (ret.isOk()) {
2133 switch (callStatus) {
2134 case Status::OK:
2135 // Expected case, do nothing.
2136 res = OK;
2137 break;
2138 case Status::METHOD_NOT_SUPPORTED:
2139 res = INVALID_OPERATION;
2140 break;
2141 default:
2142 ALOGE("%s: Session configuration query failed: %d", __FUNCTION__, callStatus);
2143 res = UNKNOWN_ERROR;
2144 }
2145 } else {
2146 ALOGE("%s: Unexpected binder error: %s", __FUNCTION__, ret.description().c_str());
2147 res = UNKNOWN_ERROR;
2148 }
2149
2150 return res;
2151 }
2152
parseProviderName(const std::string & name,std::string * type,uint32_t * id)2153 status_t CameraProviderManager::ProviderInfo::parseProviderName(const std::string& name,
2154 std::string *type, uint32_t *id) {
2155 // Format must be "<type>/<id>"
2156 #define ERROR_MSG_PREFIX "%s: Invalid provider name '%s'. " \
2157 "Should match '<type>/<id>' - "
2158
2159 if (!type || !id) return INVALID_OPERATION;
2160
2161 std::string::size_type slashIdx = name.find('/');
2162 if (slashIdx == std::string::npos || slashIdx == name.size() - 1) {
2163 ALOGE(ERROR_MSG_PREFIX
2164 "does not have / separator between type and id",
2165 __FUNCTION__, name.c_str());
2166 return BAD_VALUE;
2167 }
2168
2169 std::string typeVal = name.substr(0, slashIdx);
2170
2171 char *endPtr;
2172 errno = 0;
2173 long idVal = strtol(name.c_str() + slashIdx + 1, &endPtr, 10);
2174 if (errno != 0) {
2175 ALOGE(ERROR_MSG_PREFIX
2176 "cannot parse provider id as an integer: %s (%d)",
2177 __FUNCTION__, name.c_str(), strerror(errno), errno);
2178 return BAD_VALUE;
2179 }
2180 if (endPtr != name.c_str() + name.size()) {
2181 ALOGE(ERROR_MSG_PREFIX
2182 "provider id has unexpected length",
2183 __FUNCTION__, name.c_str());
2184 return BAD_VALUE;
2185 }
2186 if (idVal < 0) {
2187 ALOGE(ERROR_MSG_PREFIX
2188 "id is negative: %ld",
2189 __FUNCTION__, name.c_str(), idVal);
2190 return BAD_VALUE;
2191 }
2192
2193 #undef ERROR_MSG_PREFIX
2194
2195 *type = typeVal;
2196 *id = static_cast<uint32_t>(idVal);
2197
2198 return OK;
2199 }
2200
generateVendorTagId(const std::string & name)2201 metadata_vendor_id_t CameraProviderManager::ProviderInfo::generateVendorTagId(
2202 const std::string &name) {
2203 metadata_vendor_id_t ret = std::hash<std::string> {} (name);
2204 // CAMERA_METADATA_INVALID_VENDOR_ID is not a valid hash value
2205 if (CAMERA_METADATA_INVALID_VENDOR_ID == ret) {
2206 ret = 0;
2207 }
2208
2209 return ret;
2210 }
2211
parseDeviceName(const std::string & name,uint16_t * major,uint16_t * minor,std::string * type,std::string * id)2212 status_t CameraProviderManager::ProviderInfo::parseDeviceName(const std::string& name,
2213 uint16_t *major, uint16_t *minor, std::string *type, std::string *id) {
2214
2215 // Format must be "device@<major>.<minor>/<type>/<id>"
2216
2217 #define ERROR_MSG_PREFIX "%s: Invalid device name '%s'. " \
2218 "Should match 'device@<major>.<minor>/<type>/<id>' - "
2219
2220 if (!major || !minor || !type || !id) return INVALID_OPERATION;
2221
2222 // Verify starting prefix
2223 const char expectedPrefix[] = "device@";
2224
2225 if (name.find(expectedPrefix) != 0) {
2226 ALOGE(ERROR_MSG_PREFIX
2227 "does not start with '%s'",
2228 __FUNCTION__, name.c_str(), expectedPrefix);
2229 return BAD_VALUE;
2230 }
2231
2232 // Extract major/minor versions
2233 constexpr std::string::size_type atIdx = sizeof(expectedPrefix) - 2;
2234 std::string::size_type dotIdx = name.find('.', atIdx);
2235 if (dotIdx == std::string::npos) {
2236 ALOGE(ERROR_MSG_PREFIX
2237 "does not have @<major>. version section",
2238 __FUNCTION__, name.c_str());
2239 return BAD_VALUE;
2240 }
2241 std::string::size_type typeSlashIdx = name.find('/', dotIdx);
2242 if (typeSlashIdx == std::string::npos) {
2243 ALOGE(ERROR_MSG_PREFIX
2244 "does not have .<minor>/ version section",
2245 __FUNCTION__, name.c_str());
2246 return BAD_VALUE;
2247 }
2248
2249 char *endPtr;
2250 errno = 0;
2251 long majorVal = strtol(name.c_str() + atIdx + 1, &endPtr, 10);
2252 if (errno != 0) {
2253 ALOGE(ERROR_MSG_PREFIX
2254 "cannot parse major version: %s (%d)",
2255 __FUNCTION__, name.c_str(), strerror(errno), errno);
2256 return BAD_VALUE;
2257 }
2258 if (endPtr != name.c_str() + dotIdx) {
2259 ALOGE(ERROR_MSG_PREFIX
2260 "major version has unexpected length",
2261 __FUNCTION__, name.c_str());
2262 return BAD_VALUE;
2263 }
2264 long minorVal = strtol(name.c_str() + dotIdx + 1, &endPtr, 10);
2265 if (errno != 0) {
2266 ALOGE(ERROR_MSG_PREFIX
2267 "cannot parse minor version: %s (%d)",
2268 __FUNCTION__, name.c_str(), strerror(errno), errno);
2269 return BAD_VALUE;
2270 }
2271 if (endPtr != name.c_str() + typeSlashIdx) {
2272 ALOGE(ERROR_MSG_PREFIX
2273 "minor version has unexpected length",
2274 __FUNCTION__, name.c_str());
2275 return BAD_VALUE;
2276 }
2277 if (majorVal < 0 || majorVal > UINT16_MAX || minorVal < 0 || minorVal > UINT16_MAX) {
2278 ALOGE(ERROR_MSG_PREFIX
2279 "major/minor version is out of range of uint16_t: %ld.%ld",
2280 __FUNCTION__, name.c_str(), majorVal, minorVal);
2281 return BAD_VALUE;
2282 }
2283
2284 // Extract type and id
2285
2286 std::string::size_type instanceSlashIdx = name.find('/', typeSlashIdx + 1);
2287 if (instanceSlashIdx == std::string::npos) {
2288 ALOGE(ERROR_MSG_PREFIX
2289 "does not have /<type>/ component",
2290 __FUNCTION__, name.c_str());
2291 return BAD_VALUE;
2292 }
2293 std::string typeVal = name.substr(typeSlashIdx + 1, instanceSlashIdx - typeSlashIdx - 1);
2294
2295 if (instanceSlashIdx == name.size() - 1) {
2296 ALOGE(ERROR_MSG_PREFIX
2297 "does not have an /<id> component",
2298 __FUNCTION__, name.c_str());
2299 return BAD_VALUE;
2300 }
2301 std::string idVal = name.substr(instanceSlashIdx + 1);
2302
2303 #undef ERROR_MSG_PREFIX
2304
2305 *major = static_cast<uint16_t>(majorVal);
2306 *minor = static_cast<uint16_t>(minorVal);
2307 *type = typeVal;
2308 *id = idVal;
2309
2310 return OK;
2311 }
2312
2313
2314
~ProviderInfo()2315 CameraProviderManager::ProviderInfo::~ProviderInfo() {
2316 // Destruction of ProviderInfo is only supposed to happen when the respective
2317 // CameraProvider interface dies, so do not unregister callbacks.
2318
2319 }
2320
mapToStatusT(const Status & s)2321 status_t CameraProviderManager::mapToStatusT(const Status& s) {
2322 switch(s) {
2323 case Status::OK:
2324 return OK;
2325 case Status::ILLEGAL_ARGUMENT:
2326 return BAD_VALUE;
2327 case Status::CAMERA_IN_USE:
2328 return -EBUSY;
2329 case Status::MAX_CAMERAS_IN_USE:
2330 return -EUSERS;
2331 case Status::METHOD_NOT_SUPPORTED:
2332 return UNKNOWN_TRANSACTION;
2333 case Status::OPERATION_NOT_SUPPORTED:
2334 return INVALID_OPERATION;
2335 case Status::CAMERA_DISCONNECTED:
2336 return DEAD_OBJECT;
2337 case Status::INTERNAL_ERROR:
2338 return INVALID_OPERATION;
2339 }
2340 ALOGW("Unexpected HAL status code %d", s);
2341 return INVALID_OPERATION;
2342 }
2343
statusToString(const Status & s)2344 const char* CameraProviderManager::statusToString(const Status& s) {
2345 switch(s) {
2346 case Status::OK:
2347 return "OK";
2348 case Status::ILLEGAL_ARGUMENT:
2349 return "ILLEGAL_ARGUMENT";
2350 case Status::CAMERA_IN_USE:
2351 return "CAMERA_IN_USE";
2352 case Status::MAX_CAMERAS_IN_USE:
2353 return "MAX_CAMERAS_IN_USE";
2354 case Status::METHOD_NOT_SUPPORTED:
2355 return "METHOD_NOT_SUPPORTED";
2356 case Status::OPERATION_NOT_SUPPORTED:
2357 return "OPERATION_NOT_SUPPORTED";
2358 case Status::CAMERA_DISCONNECTED:
2359 return "CAMERA_DISCONNECTED";
2360 case Status::INTERNAL_ERROR:
2361 return "INTERNAL_ERROR";
2362 }
2363 ALOGW("Unexpected HAL status code %d", s);
2364 return "UNKNOWN_ERROR";
2365 }
2366
deviceStatusToString(const CameraDeviceStatus & s)2367 const char* CameraProviderManager::deviceStatusToString(const CameraDeviceStatus& s) {
2368 switch(s) {
2369 case CameraDeviceStatus::NOT_PRESENT:
2370 return "NOT_PRESENT";
2371 case CameraDeviceStatus::PRESENT:
2372 return "PRESENT";
2373 case CameraDeviceStatus::ENUMERATING:
2374 return "ENUMERATING";
2375 }
2376 ALOGW("Unexpected HAL device status code %d", s);
2377 return "UNKNOWN_STATUS";
2378 }
2379
torchStatusToString(const TorchModeStatus & s)2380 const char* CameraProviderManager::torchStatusToString(const TorchModeStatus& s) {
2381 switch(s) {
2382 case TorchModeStatus::NOT_AVAILABLE:
2383 return "NOT_AVAILABLE";
2384 case TorchModeStatus::AVAILABLE_OFF:
2385 return "AVAILABLE_OFF";
2386 case TorchModeStatus::AVAILABLE_ON:
2387 return "AVAILABLE_ON";
2388 }
2389 ALOGW("Unexpected HAL torch mode status code %d", s);
2390 return "UNKNOWN_STATUS";
2391 }
2392
2393
createDescriptorFromHidl(const hardware::hidl_vec<common::V1_0::VendorTagSection> & vts,sp<VendorTagDescriptor> & descriptor)2394 status_t HidlVendorTagDescriptor::createDescriptorFromHidl(
2395 const hardware::hidl_vec<common::V1_0::VendorTagSection>& vts,
2396 /*out*/
2397 sp<VendorTagDescriptor>& descriptor) {
2398
2399 int tagCount = 0;
2400
2401 for (size_t s = 0; s < vts.size(); s++) {
2402 tagCount += vts[s].tags.size();
2403 }
2404
2405 if (tagCount < 0 || tagCount > INT32_MAX) {
2406 ALOGE("%s: tag count %d from vendor tag sections is invalid.", __FUNCTION__, tagCount);
2407 return BAD_VALUE;
2408 }
2409
2410 Vector<uint32_t> tagArray;
2411 LOG_ALWAYS_FATAL_IF(tagArray.resize(tagCount) != tagCount,
2412 "%s: too many (%u) vendor tags defined.", __FUNCTION__, tagCount);
2413
2414
2415 sp<HidlVendorTagDescriptor> desc = new HidlVendorTagDescriptor();
2416 desc->mTagCount = tagCount;
2417
2418 SortedVector<String8> sections;
2419 KeyedVector<uint32_t, String8> tagToSectionMap;
2420
2421 int idx = 0;
2422 for (size_t s = 0; s < vts.size(); s++) {
2423 const common::V1_0::VendorTagSection& section = vts[s];
2424 const char *sectionName = section.sectionName.c_str();
2425 if (sectionName == NULL) {
2426 ALOGE("%s: no section name defined for vendor tag section %zu.", __FUNCTION__, s);
2427 return BAD_VALUE;
2428 }
2429 String8 sectionString(sectionName);
2430 sections.add(sectionString);
2431
2432 for (size_t j = 0; j < section.tags.size(); j++) {
2433 uint32_t tag = section.tags[j].tagId;
2434 if (tag < CAMERA_METADATA_VENDOR_TAG_BOUNDARY) {
2435 ALOGE("%s: vendor tag %d not in vendor tag section.", __FUNCTION__, tag);
2436 return BAD_VALUE;
2437 }
2438
2439 tagArray.editItemAt(idx++) = section.tags[j].tagId;
2440
2441 const char *tagName = section.tags[j].tagName.c_str();
2442 if (tagName == NULL) {
2443 ALOGE("%s: no tag name defined for vendor tag %d.", __FUNCTION__, tag);
2444 return BAD_VALUE;
2445 }
2446 desc->mTagToNameMap.add(tag, String8(tagName));
2447 tagToSectionMap.add(tag, sectionString);
2448
2449 int tagType = (int) section.tags[j].tagType;
2450 if (tagType < 0 || tagType >= NUM_TYPES) {
2451 ALOGE("%s: tag type %d from vendor ops does not exist.", __FUNCTION__, tagType);
2452 return BAD_VALUE;
2453 }
2454 desc->mTagToTypeMap.add(tag, tagType);
2455 }
2456 }
2457
2458 desc->mSections = sections;
2459
2460 for (size_t i = 0; i < tagArray.size(); ++i) {
2461 uint32_t tag = tagArray[i];
2462 String8 sectionString = tagToSectionMap.valueFor(tag);
2463
2464 // Set up tag to section index map
2465 ssize_t index = sections.indexOf(sectionString);
2466 LOG_ALWAYS_FATAL_IF(index < 0, "index %zd must be non-negative", index);
2467 desc->mTagToSectionMap.add(tag, static_cast<uint32_t>(index));
2468
2469 // Set up reverse mapping
2470 ssize_t reverseIndex = -1;
2471 if ((reverseIndex = desc->mReverseMapping.indexOfKey(sectionString)) < 0) {
2472 KeyedVector<String8, uint32_t>* nameMapper = new KeyedVector<String8, uint32_t>();
2473 reverseIndex = desc->mReverseMapping.add(sectionString, nameMapper);
2474 }
2475 desc->mReverseMapping[reverseIndex]->add(desc->mTagToNameMap.valueFor(tag), tag);
2476 }
2477
2478 descriptor = std::move(desc);
2479 return OK;
2480 }
2481
getCameraCharacteristicsLocked(const std::string & id,CameraMetadata * characteristics) const2482 status_t CameraProviderManager::getCameraCharacteristicsLocked(const std::string &id,
2483 CameraMetadata* characteristics) const {
2484 auto deviceInfo = findDeviceInfoLocked(id, /*minVersion*/ {3,0}, /*maxVersion*/ {5,0});
2485 if (deviceInfo != nullptr) {
2486 return deviceInfo->getCameraCharacteristics(characteristics);
2487 }
2488
2489 // Find hidden physical camera characteristics
2490 for (auto& provider : mProviders) {
2491 for (auto& deviceInfo : provider->mDevices) {
2492 status_t res = deviceInfo->getPhysicalCameraCharacteristics(id, characteristics);
2493 if (res != NAME_NOT_FOUND) return res;
2494 }
2495 }
2496
2497 return NAME_NOT_FOUND;
2498 }
2499
filterLogicalCameraIdsLocked(std::vector<std::string> & deviceIds) const2500 void CameraProviderManager::filterLogicalCameraIdsLocked(
2501 std::vector<std::string>& deviceIds) const
2502 {
2503 // Map between camera facing and camera IDs related to logical camera.
2504 std::map<int, std::unordered_set<std::string>> idCombos;
2505
2506 // Collect all logical and its underlying physical camera IDs for each
2507 // facing.
2508 for (auto& deviceId : deviceIds) {
2509 auto deviceInfo = findDeviceInfoLocked(deviceId);
2510 if (deviceInfo == nullptr) continue;
2511
2512 if (!deviceInfo->mIsLogicalCamera) {
2513 continue;
2514 }
2515
2516 // combo contains the ids of a logical camera and its physical cameras
2517 std::vector<std::string> combo = deviceInfo->mPhysicalIds;
2518 combo.push_back(deviceId);
2519
2520 hardware::CameraInfo info;
2521 status_t res = deviceInfo->getCameraInfo(&info);
2522 if (res != OK) {
2523 ALOGE("%s: Error reading camera info: %s (%d)", __FUNCTION__, strerror(-res), res);
2524 continue;
2525 }
2526 idCombos[info.facing].insert(combo.begin(), combo.end());
2527 }
2528
2529 // Only expose one camera ID per facing for all logical and underlying
2530 // physical camera IDs.
2531 for (auto& r : idCombos) {
2532 auto& removedIds = r.second;
2533 for (auto& id : deviceIds) {
2534 auto foundId = std::find(removedIds.begin(), removedIds.end(), id);
2535 if (foundId == removedIds.end()) {
2536 continue;
2537 }
2538
2539 removedIds.erase(foundId);
2540 break;
2541 }
2542 deviceIds.erase(std::remove_if(deviceIds.begin(), deviceIds.end(),
2543 [&removedIds](const std::string& s) {
2544 return removedIds.find(s) != removedIds.end();}),
2545 deviceIds.end());
2546 }
2547 }
2548
2549 } // namespace android
2550