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