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 #include "system/graphics-base-v1.0.h"
18 #include "system/graphics-base-v1.1.h"
19 #define LOG_TAG "CameraProviderManager"
20 #define ATRACE_TAG ATRACE_TAG_CAMERA
21 //#define LOG_NDEBUG 0
22
23 #include "CameraProviderManager.h"
24
25 #include <aidl/android/hardware/camera/device/ICameraDevice.h>
26
27 #include <algorithm>
28 #include <chrono>
29 #include "common/DepthPhotoProcessor.h"
30 #include "hidl/HidlProviderInfo.h"
31 #include "aidl/AidlProviderInfo.h"
32 #include <dlfcn.h>
33 #include <future>
34 #include <inttypes.h>
35 #include <android_companion_virtualdevice_flags.h>
36 #include <android_companion_virtualdevice_build_flags.h>
37 #include <android/binder_manager.h>
38 #include <android/hidl/manager/1.2/IServiceManager.h>
39 #include <hidl/ServiceManagement.h>
40 #include <com_android_internal_camera_flags.h>
41 #include <com_android_window_flags.h>
42 #include <functional>
43 #include <camera_metadata_hidden.h>
44 #include <android-base/parseint.h>
45 #include <android-base/logging.h>
46 #include <cutils/properties.h>
47 #include <hwbinder/IPCThreadState.h>
48 #include <utils/Trace.h>
49 #include <ui/PublicFormat.h>
50 #include <camera/StringUtils.h>
51
52 #include "api2/HeicCompositeStream.h"
53 #include "device3/ZoomRatioMapper.h"
54
55 namespace android {
56
57 using namespace ::android::hardware::camera;
58 using namespace ::android::camera3;
59 using android::hardware::camera::common::V1_0::Status;
60 using namespace camera3::SessionConfigurationUtils;
61 using std::literals::chrono_literals::operator""s;
62 using hardware::camera2::utils::CameraIdAndSessionConfiguration;
63
64 namespace flags = com::android::internal::camera::flags;
65 namespace vd_flags = android::companion::virtualdevice::flags;
66 namespace wm_flags = com::android::window::flags;
67
68 namespace {
69 const bool kEnableLazyHal(property_get_bool("ro.camera.enableLazyHal", false));
70 const std::string kExternalProviderName = "external/0";
71 const std::string kVirtualProviderName = "virtual/0";
72 } // anonymous namespace
73
74 const float CameraProviderManager::kDepthARTolerance = .1f;
75 const bool CameraProviderManager::kFrameworkJpegRDisabled =
76 property_get_bool("ro.camera.disableJpegR", false);
77
78 CameraProviderManager::HidlServiceInteractionProxyImpl
79 CameraProviderManager::sHidlServiceInteractionProxy{};
80 CameraProviderManager::AidlServiceInteractionProxyImpl
81 CameraProviderManager::sAidlServiceInteractionProxy{};
82
~CameraProviderManager()83 CameraProviderManager::~CameraProviderManager() {
84 }
85
FrameworkTorchStatusToString(const TorchModeStatus & s)86 const char* FrameworkTorchStatusToString(const TorchModeStatus& s) {
87 switch (s) {
88 case TorchModeStatus::NOT_AVAILABLE:
89 return "NOT_AVAILABLE";
90 case TorchModeStatus::AVAILABLE_OFF:
91 return "AVAILABLE_OFF";
92 case TorchModeStatus::AVAILABLE_ON:
93 return "AVAILABLE_ON";
94 }
95 ALOGW("Unexpected HAL torch mode status code %d", s);
96 return "UNKNOWN_STATUS";
97 }
98
FrameworkDeviceStatusToString(const CameraDeviceStatus & s)99 const char* FrameworkDeviceStatusToString(const CameraDeviceStatus& s) {
100 switch (s) {
101 case CameraDeviceStatus::NOT_PRESENT:
102 return "NOT_PRESENT";
103 case CameraDeviceStatus::PRESENT:
104 return "PRESENT";
105 case CameraDeviceStatus::ENUMERATING:
106 return "ENUMERATING";
107 }
108 ALOGW("Unexpected HAL device status code %d", s);
109 return "UNKNOWN_STATUS";
110 }
111
112 hardware::hidl_vec<hardware::hidl_string>
listServices()113 CameraProviderManager::HidlServiceInteractionProxyImpl::listServices() {
114 hardware::hidl_vec<hardware::hidl_string> ret;
115 auto manager = hardware::defaultServiceManager1_2();
116 if (manager != nullptr) {
117 manager->listManifestByInterface(provider::V2_4::ICameraProvider::descriptor,
118 [&ret](const hardware::hidl_vec<hardware::hidl_string> ®istered) {
119 ret = registered;
120 });
121 }
122 return ret;
123 }
124
tryToInitAndAddHidlProvidersLocked(HidlServiceInteractionProxy * hidlProxy)125 status_t CameraProviderManager::tryToInitAndAddHidlProvidersLocked(
126 HidlServiceInteractionProxy *hidlProxy) {
127 mHidlServiceProxy = hidlProxy;
128 // Registering will trigger notifications for all already-known providers
129 bool success = mHidlServiceProxy->registerForNotifications(
130 /* instance name, empty means no filter */ "",
131 this);
132 if (!success) {
133 ALOGE("%s: Unable to register with hardware service manager for notifications "
134 "about camera providers", __FUNCTION__);
135 return INVALID_OPERATION;
136 }
137
138 for (const auto& instance : mHidlServiceProxy->listServices()) {
139 this->addHidlProviderLocked(instance);
140 }
141 return OK;
142 }
143
144 std::shared_ptr<aidl::android::hardware::camera::provider::ICameraProvider>
getService(const std::string & serviceName)145 CameraProviderManager::AidlServiceInteractionProxyImpl::getService(
146 const std::string& serviceName) {
147 using aidl::android::hardware::camera::provider::ICameraProvider;
148
149 AIBinder* binder = nullptr;
150 if (flags::lazy_aidl_wait_for_service()) {
151 binder = AServiceManager_waitForService(serviceName.c_str());
152 } else {
153 binder = AServiceManager_checkService(serviceName.c_str());
154 }
155
156 if (binder == nullptr) {
157 ALOGE("%s: AIDL Camera provider HAL '%s' is not actually available, despite waiting "
158 "indefinitely?", __FUNCTION__, serviceName.c_str());
159 return nullptr;
160 }
161 std::shared_ptr<ICameraProvider> interface =
162 ICameraProvider::fromBinder(ndk::SpAIBinder(binder));
163
164 return interface;
165 }
166
167 std::shared_ptr<aidl::android::hardware::camera::provider::ICameraProvider>
tryGetService(const std::string & serviceName)168 CameraProviderManager::AidlServiceInteractionProxyImpl::tryGetService(
169 const std::string& serviceName) {
170 using aidl::android::hardware::camera::provider::ICameraProvider;
171
172 std::shared_ptr<ICameraProvider> interface = ICameraProvider::fromBinder(
173 ndk::SpAIBinder(AServiceManager_checkService(serviceName.c_str())));
174 if (interface == nullptr) {
175 ALOGD("%s: AIDL Camera provider HAL '%s' is not actually available", __FUNCTION__,
176 serviceName.c_str());
177 return nullptr;
178 }
179
180 return interface;
181 }
182
getFullAidlProviderName(const std::string instance)183 static std::string getFullAidlProviderName(const std::string instance) {
184 std::string aidlHalServiceDescriptor =
185 std::string(aidl::android::hardware::camera::provider::ICameraProvider::descriptor);
186 return aidlHalServiceDescriptor + "/" + instance;
187 }
188
tryToAddAidlProvidersLocked()189 status_t CameraProviderManager::tryToAddAidlProvidersLocked() {
190 const char * aidlHalServiceDescriptor =
191 aidl::android::hardware::camera::provider::ICameraProvider::descriptor;
192 auto sm = defaultServiceManager();
193 auto aidlProviders = sm->getDeclaredInstances(
194 String16(aidlHalServiceDescriptor));
195
196 if (isVirtualCameraHalEnabled()) {
197 // Virtual Camera provider is not declared in the VINTF manifest so we
198 // manually add it if the binary is present.
199 aidlProviders.push_back(String16(kVirtualProviderName.c_str()));
200 }
201
202 for (const auto &aidlInstance : aidlProviders) {
203 std::string aidlServiceName =
204 getFullAidlProviderName(toStdString(aidlInstance));
205 auto res = sm->registerForNotifications(String16(aidlServiceName.c_str()), this);
206 if (res != OK) {
207 ALOGE("%s Unable to register for notifications with AIDL service manager",
208 __FUNCTION__);
209 return res;
210 }
211 addAidlProviderLocked(aidlServiceName);
212 }
213 return OK;
214 }
215
initialize(wp<CameraProviderManager::StatusListener> listener,HidlServiceInteractionProxy * hidlProxy,AidlServiceInteractionProxy * aidlProxy)216 status_t CameraProviderManager::initialize(wp<CameraProviderManager::StatusListener> listener,
217 HidlServiceInteractionProxy* hidlProxy, AidlServiceInteractionProxy* aidlProxy) {
218 std::lock_guard<std::mutex> lock(mInterfaceMutex);
219 if (hidlProxy == nullptr) {
220 ALOGE("%s: No valid service Hidl interaction proxy provided", __FUNCTION__);
221 return BAD_VALUE;
222 }
223
224 if (aidlProxy == nullptr) {
225 ALOGE("%s: No valid service Aidl interaction proxy provided", __FUNCTION__);
226 return BAD_VALUE;
227 }
228 mAidlServiceProxy = aidlProxy;
229
230 mListener = listener;
231 mDeviceState = 0;
232 auto res = tryToInitAndAddHidlProvidersLocked(hidlProxy);
233 if (res != OK) {
234 // Logging done in called function;
235 return res;
236 }
237 res = tryToAddAidlProvidersLocked();
238
239 IPCThreadState::self()->flushCommands();
240
241 return res;
242 }
243
getCameraCount() const244 std::pair<int, int> CameraProviderManager::getCameraCount() const {
245 std::lock_guard<std::mutex> lock(mInterfaceMutex);
246 int systemCameraCount = 0;
247 int publicCameraCount = 0;
248 for (auto& provider : mProviders) {
249 for (auto &id : provider->mUniqueCameraIds) {
250 SystemCameraKind deviceKind = SystemCameraKind::PUBLIC;
251 if (getSystemCameraKindLocked(id, &deviceKind) != OK) {
252 ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, id.c_str());
253 continue;
254 }
255 switch(deviceKind) {
256 case SystemCameraKind::PUBLIC:
257 publicCameraCount++;
258 break;
259 case SystemCameraKind::SYSTEM_ONLY_CAMERA:
260 systemCameraCount++;
261 break;
262 default:
263 break;
264 }
265 }
266 }
267 return std::make_pair(systemCameraCount, publicCameraCount);
268 }
269
getCameraDeviceIds(std::unordered_map<std::string,std::set<std::string>> * unavailablePhysicalIds) const270 std::vector<std::string> CameraProviderManager::getCameraDeviceIds(std::unordered_map<
271 std::string, std::set<std::string>>* unavailablePhysicalIds) const {
272 std::lock_guard<std::mutex> lock(mInterfaceMutex);
273 std::vector<std::string> deviceIds;
274 for (auto& provider : mProviders) {
275 for (auto& id : provider->mUniqueCameraIds) {
276 deviceIds.push_back(id);
277 if (unavailablePhysicalIds != nullptr &&
278 provider->mUnavailablePhysicalCameras.count(id) > 0) {
279 (*unavailablePhysicalIds)[id] = provider->mUnavailablePhysicalCameras.at(id);
280 }
281 }
282 }
283 return deviceIds;
284 }
285
collectDeviceIdsLocked(const std::vector<std::string> deviceIds,std::vector<std::string> & publicDeviceIds,std::vector<std::string> & systemDeviceIds) const286 void CameraProviderManager::collectDeviceIdsLocked(const std::vector<std::string> deviceIds,
287 std::vector<std::string>& publicDeviceIds,
288 std::vector<std::string>& systemDeviceIds) const {
289 for (auto &deviceId : deviceIds) {
290 SystemCameraKind deviceKind = SystemCameraKind::PUBLIC;
291 if (getSystemCameraKindLocked(deviceId, &deviceKind) != OK) {
292 ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, deviceId.c_str());
293 continue;
294 }
295 if (deviceKind == SystemCameraKind::SYSTEM_ONLY_CAMERA) {
296 systemDeviceIds.push_back(deviceId);
297 } else {
298 publicDeviceIds.push_back(deviceId);
299 }
300 }
301 }
302
getAPI1CompatibleCameraDeviceIds() const303 std::vector<std::string> CameraProviderManager::getAPI1CompatibleCameraDeviceIds() const {
304 std::lock_guard<std::mutex> lock(mInterfaceMutex);
305 std::vector<std::string> publicDeviceIds;
306 std::vector<std::string> systemDeviceIds;
307 std::vector<std::string> deviceIds;
308 for (auto& provider : mProviders) {
309 std::vector<std::string> providerDeviceIds = provider->mUniqueAPI1CompatibleCameraIds;
310 // Secure cameras should not be exposed through camera 1 api
311 providerDeviceIds.erase(std::remove_if(providerDeviceIds.begin(), providerDeviceIds.end(),
312 [this](const std::string& s) {
313 SystemCameraKind deviceKind = SystemCameraKind::PUBLIC;
314 if (getSystemCameraKindLocked(s, &deviceKind) != OK) {
315 ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, s.c_str());
316 return true;
317 }
318 return deviceKind == SystemCameraKind::HIDDEN_SECURE_CAMERA;}),
319 providerDeviceIds.end());
320 // API1 app doesn't handle logical and physical camera devices well. So
321 // for each camera facing, only take the first id advertised by HAL in
322 // all [logical, physical1, physical2, ...] id combos, and filter out the rest.
323 filterLogicalCameraIdsLocked(providerDeviceIds);
324 collectDeviceIdsLocked(providerDeviceIds, publicDeviceIds, systemDeviceIds);
325 }
326 auto sortFunc =
327 [](const std::string& a, const std::string& b) -> bool {
328 uint32_t aUint = 0, bUint = 0;
329 bool aIsUint = base::ParseUint(a, &aUint);
330 bool bIsUint = base::ParseUint(b, &bUint);
331
332 // Uint device IDs first
333 if (aIsUint && bIsUint) {
334 return aUint < bUint;
335 } else if (aIsUint) {
336 return true;
337 } else if (bIsUint) {
338 return false;
339 }
340 // Simple string compare if both id are not uint
341 return a < b;
342 };
343 // We put device ids for system cameras at the end since they will be pared
344 // off for processes not having system camera permissions.
345 std::sort(publicDeviceIds.begin(), publicDeviceIds.end(), sortFunc);
346 std::sort(systemDeviceIds.begin(), systemDeviceIds.end(), sortFunc);
347 deviceIds.insert(deviceIds.end(), publicDeviceIds.begin(), publicDeviceIds.end());
348 deviceIds.insert(deviceIds.end(), systemDeviceIds.begin(), systemDeviceIds.end());
349 return deviceIds;
350 }
351
isValidDeviceLocked(const std::string & id,uint16_t majorVersion,IPCTransport transport) const352 bool CameraProviderManager::isValidDeviceLocked(const std::string &id, uint16_t majorVersion,
353 IPCTransport transport) const {
354 for (auto& provider : mProviders) {
355 IPCTransport providerTransport = provider->getIPCTransport();
356 for (auto& deviceInfo : provider->mDevices) {
357 if (deviceInfo->mId == id && deviceInfo->mVersion.get_major() == majorVersion &&
358 transport == providerTransport) {
359 return true;
360 }
361 }
362 }
363 return false;
364 }
365
hasFlashUnit(const std::string & id) const366 bool CameraProviderManager::hasFlashUnit(const std::string &id) const {
367 std::lock_guard<std::mutex> lock(mInterfaceMutex);
368
369 auto deviceInfo = findDeviceInfoLocked(id);
370 if (deviceInfo == nullptr) return false;
371
372 return deviceInfo->hasFlashUnit();
373 }
374
supportNativeZoomRatio(const std::string & id) const375 bool CameraProviderManager::supportNativeZoomRatio(const std::string &id) const {
376 std::lock_guard<std::mutex> lock(mInterfaceMutex);
377
378 auto deviceInfo = findDeviceInfoLocked(id);
379 if (deviceInfo == nullptr) return false;
380
381 return deviceInfo->supportNativeZoomRatio();
382 }
383
isCompositeJpegRDisabled(const std::string & id) const384 bool CameraProviderManager::isCompositeJpegRDisabled(const std::string &id) const {
385 std::lock_guard<std::mutex> lock(mInterfaceMutex);
386 return isCompositeJpegRDisabledLocked(id);
387 }
388
isCompositeJpegRDisabledLocked(const std::string & id) const389 bool CameraProviderManager::isCompositeJpegRDisabledLocked(const std::string &id) const {
390 auto deviceInfo = findDeviceInfoLocked(id);
391 if (deviceInfo == nullptr) return false;
392
393 return deviceInfo->isCompositeJpegRDisabled();
394 }
395
getResourceCost(const std::string & id,CameraResourceCost * cost) const396 status_t CameraProviderManager::getResourceCost(const std::string &id,
397 CameraResourceCost* cost) const {
398 std::lock_guard<std::mutex> lock(mInterfaceMutex);
399
400 auto deviceInfo = findDeviceInfoLocked(id);
401 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
402
403 *cost = deviceInfo->mResourceCost;
404 return OK;
405 }
406
getCameraInfo(const std::string & id,int rotationOverride,int * portraitRotation,hardware::CameraInfo * info) const407 status_t CameraProviderManager::getCameraInfo(const std::string &id,
408 int rotationOverride, int *portraitRotation,
409 hardware::CameraInfo* info) const {
410 std::lock_guard<std::mutex> lock(mInterfaceMutex);
411
412 auto deviceInfo = findDeviceInfoLocked(id);
413 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
414
415 return deviceInfo->getCameraInfo(rotationOverride, portraitRotation, info);
416 }
417
isSessionConfigurationSupported(const std::string & id,const SessionConfiguration & configuration,bool overrideForPerfClass,bool checkSessionParams,bool * status) const418 status_t CameraProviderManager::isSessionConfigurationSupported(const std::string& id,
419 const SessionConfiguration &configuration, bool overrideForPerfClass,
420 bool checkSessionParams, bool *status /*out*/) const {
421 std::lock_guard<std::mutex> lock(mInterfaceMutex);
422 auto deviceInfo = findDeviceInfoLocked(id);
423 if (deviceInfo == nullptr) {
424 return NAME_NOT_FOUND;
425 }
426
427 metadataGetter getMetadata = [this](const std::string &id,
428 bool overrideForPerfClass) {
429 CameraMetadata metadata;
430 this->getCameraCharacteristicsLocked(id, overrideForPerfClass,
431 &metadata,
432 hardware::ICameraService::ROTATION_OVERRIDE_NONE);
433 return metadata;
434 };
435 return deviceInfo->isSessionConfigurationSupported(configuration,
436 overrideForPerfClass, getMetadata, checkSessionParams, status);
437 }
438
createDefaultRequest(const std::string & cameraId,camera_request_template_t templateId,CameraMetadata * metadata) const439 status_t CameraProviderManager::createDefaultRequest(const std::string& cameraId,
440 camera_request_template_t templateId,
441 CameraMetadata* metadata) const {
442 ATRACE_CALL();
443 if (templateId <= 0 || templateId >= CAMERA_TEMPLATE_COUNT) {
444 return BAD_VALUE;
445 }
446
447 std::lock_guard<std::mutex> lock(mInterfaceMutex);
448 auto deviceInfo = findDeviceInfoLocked(cameraId);
449 if (deviceInfo == nullptr) {
450 return NAME_NOT_FOUND;
451 }
452
453 status_t res = deviceInfo->createDefaultRequest(templateId,
454 metadata);
455
456 if (res == BAD_VALUE) {
457 ALOGI("%s: template %d is not supported on this camera device",
458 __FUNCTION__, templateId);
459 return res;
460 } else if (res != OK) {
461 ALOGE("Unable to construct request template %d: %s (%d)",
462 templateId, strerror(-res), res);
463 return res;
464 }
465
466 return OK;
467 }
468
getSessionCharacteristics(const std::string & id,const SessionConfiguration & configuration,bool overrideForPerfClass,int rotationOverride,CameraMetadata * sessionCharacteristics) const469 status_t CameraProviderManager::getSessionCharacteristics(
470 const std::string& id, const SessionConfiguration& configuration, bool overrideForPerfClass,
471 int rotationOverride, CameraMetadata* sessionCharacteristics /*out*/) const {
472 if (!flags::feature_combination_query()) {
473 return INVALID_OPERATION;
474 }
475
476 std::lock_guard<std::mutex> lock(mInterfaceMutex);
477 auto deviceInfo = findDeviceInfoLocked(id);
478 if (deviceInfo == nullptr) {
479 return NAME_NOT_FOUND;
480 }
481
482 metadataGetter getMetadata = [this, rotationOverride](const std::string& id,
483 bool overrideForPerfClass) {
484 CameraMetadata metadata;
485 status_t ret = this->getCameraCharacteristicsLocked(id, overrideForPerfClass, &metadata,
486 rotationOverride);
487 if (ret != OK) {
488 ALOGE("%s: Could not get CameraCharacteristics for device %s", __FUNCTION__,
489 id.c_str());
490 }
491 return metadata;
492 };
493
494 return deviceInfo->getSessionCharacteristics(configuration,
495 overrideForPerfClass, getMetadata, sessionCharacteristics);
496 }
497
getCameraIdIPCTransport(const std::string & id,IPCTransport * providerTransport) const498 status_t CameraProviderManager::getCameraIdIPCTransport(const std::string &id,
499 IPCTransport *providerTransport) const {
500 std::lock_guard<std::mutex> lock(mInterfaceMutex);
501 auto deviceInfo = findDeviceInfoLocked(id);
502 if (deviceInfo == nullptr) {
503 return NAME_NOT_FOUND;
504 }
505 sp<ProviderInfo> parentProvider = deviceInfo->mParentProvider.promote();
506 if (parentProvider == nullptr) {
507 return DEAD_OBJECT;
508 }
509 *providerTransport = parentProvider->getIPCTransport();
510 return OK;
511 }
512
getCameraCharacteristics(const std::string & id,bool overrideForPerfClass,CameraMetadata * characteristics,int rotationOverride) const513 status_t CameraProviderManager::getCameraCharacteristics(const std::string &id,
514 bool overrideForPerfClass, CameraMetadata* characteristics,
515 int rotationOverride) const {
516 std::lock_guard<std::mutex> lock(mInterfaceMutex);
517 return getCameraCharacteristicsLocked(id, overrideForPerfClass, characteristics,
518 rotationOverride);
519 }
520
getHighestSupportedVersion(const std::string & id,hardware::hidl_version * v,IPCTransport * transport)521 status_t CameraProviderManager::getHighestSupportedVersion(const std::string &id,
522 hardware::hidl_version *v, IPCTransport *transport) {
523 if (v == nullptr || transport == nullptr) {
524 return BAD_VALUE;
525 }
526 std::lock_guard<std::mutex> lock(mInterfaceMutex);
527
528 hardware::hidl_version maxVersion{0,0};
529 bool found = false;
530 IPCTransport providerTransport = IPCTransport::INVALID;
531 for (auto& provider : mProviders) {
532 for (auto& deviceInfo : provider->mDevices) {
533 if (deviceInfo->mId == id) {
534 if (deviceInfo->mVersion > maxVersion) {
535 maxVersion = deviceInfo->mVersion;
536 providerTransport = provider->getIPCTransport();
537 found = true;
538 }
539 }
540 }
541 }
542 if (!found || providerTransport == IPCTransport::INVALID) {
543 return NAME_NOT_FOUND;
544 }
545 *v = maxVersion;
546 *transport = providerTransport;
547 return OK;
548 }
549
getTorchStrengthLevel(const std::string & id,int32_t * torchStrength)550 status_t CameraProviderManager::getTorchStrengthLevel(const std::string &id,
551 int32_t* torchStrength /*out*/) {
552 std::lock_guard<std::mutex> lock(mInterfaceMutex);
553
554 auto deviceInfo = findDeviceInfoLocked(id);
555 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
556
557 return deviceInfo->getTorchStrengthLevel(torchStrength);
558 }
559
turnOnTorchWithStrengthLevel(const std::string & id,int32_t torchStrength)560 status_t CameraProviderManager::turnOnTorchWithStrengthLevel(const std::string &id,
561 int32_t torchStrength) {
562 std::lock_guard<std::mutex> lock(mInterfaceMutex);
563
564 auto deviceInfo = findDeviceInfoLocked(id);
565 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
566
567 return deviceInfo->turnOnTorchWithStrengthLevel(torchStrength);
568 }
569
shouldSkipTorchStrengthUpdate(const std::string & id,int32_t torchStrength) const570 bool CameraProviderManager::shouldSkipTorchStrengthUpdate(const std::string &id,
571 int32_t torchStrength) const {
572 std::lock_guard<std::mutex> lock(mInterfaceMutex);
573
574 auto deviceInfo = findDeviceInfoLocked(id);
575 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
576
577 if (deviceInfo->mTorchStrengthLevel == torchStrength) {
578 ALOGV("%s: Skipping torch strength level updates prev_level: %d, new_level: %d",
579 __FUNCTION__, deviceInfo->mTorchStrengthLevel, torchStrength);
580 return true;
581 }
582 return false;
583 }
584
getTorchDefaultStrengthLevel(const std::string & id) const585 int32_t CameraProviderManager::getTorchDefaultStrengthLevel(const std::string &id) const {
586 std::lock_guard<std::mutex> lock(mInterfaceMutex);
587
588 auto deviceInfo = findDeviceInfoLocked(id);
589 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
590
591 return deviceInfo->mTorchDefaultStrengthLevel;
592 }
593
supportSetTorchMode(const std::string & id) const594 bool CameraProviderManager::supportSetTorchMode(const std::string &id) const {
595 std::lock_guard<std::mutex> lock(mInterfaceMutex);
596 for (auto& provider : mProviders) {
597 for (auto& deviceInfo : provider->mDevices) {
598 if (deviceInfo->mId == id) {
599 return provider->mSetTorchModeSupported;
600 }
601 }
602 }
603 return false;
604 }
605
606 template <class ProviderInfoType, class HalCameraProviderType>
setTorchModeT(sp<ProviderInfo> & parentProvider,std::shared_ptr<HalCameraProvider> * halCameraProvider)607 status_t CameraProviderManager::setTorchModeT(sp<ProviderInfo> &parentProvider,
608 std::shared_ptr<HalCameraProvider> *halCameraProvider) {
609 if (halCameraProvider == nullptr) {
610 return BAD_VALUE;
611 }
612 ProviderInfoType *idlProviderInfo = static_cast<ProviderInfoType *>(parentProvider.get());
613 auto idlInterface = idlProviderInfo->startProviderInterface();
614 if (idlInterface == nullptr) {
615 return DEAD_OBJECT;
616 }
617 *halCameraProvider =
618 std::make_shared<HalCameraProviderType>(idlInterface, idlInterface->descriptor);
619 return OK;
620 }
621
setTorchMode(const std::string & id,bool enabled)622 status_t CameraProviderManager::setTorchMode(const std::string &id, bool enabled) {
623 std::lock_guard<std::mutex> lock(mInterfaceMutex);
624
625 auto deviceInfo = findDeviceInfoLocked(id);
626 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
627
628 // Pass the camera ID to start interface so that it will save it to the map of ICameraProviders
629 // that are currently in use.
630 sp<ProviderInfo> parentProvider = deviceInfo->mParentProvider.promote();
631 if (parentProvider == nullptr) {
632 return DEAD_OBJECT;
633 }
634 std::shared_ptr<HalCameraProvider> halCameraProvider = nullptr;
635 IPCTransport providerTransport = parentProvider->getIPCTransport();
636 status_t res = OK;
637 if (providerTransport == IPCTransport::HIDL) {
638 res = setTorchModeT<HidlProviderInfo, HidlHalCameraProvider>(parentProvider,
639 &halCameraProvider);
640 if (res != OK) {
641 return res;
642 }
643 } else if (providerTransport == IPCTransport::AIDL) {
644 res = setTorchModeT<AidlProviderInfo, AidlHalCameraProvider>(parentProvider,
645 &halCameraProvider);
646 if (res != OK) {
647 return res;
648 }
649 } else {
650 ALOGE("%s Invalid provider transport", __FUNCTION__);
651 return INVALID_OPERATION;
652 }
653 saveRef(DeviceMode::TORCH, deviceInfo->mId, halCameraProvider);
654
655 return deviceInfo->setTorchMode(enabled);
656 }
657
setUpVendorTags()658 status_t CameraProviderManager::setUpVendorTags() {
659 sp<VendorTagDescriptorCache> tagCache = new VendorTagDescriptorCache();
660
661 for (auto& provider : mProviders) {
662 tagCache->addVendorDescriptor(provider->mProviderTagid, provider->mVendorTagDescriptor);
663 }
664
665 VendorTagDescriptorCache::setAsGlobalVendorTagCache(tagCache);
666
667 return OK;
668 }
669
startExternalLazyProvider() const670 sp<CameraProviderManager::ProviderInfo> CameraProviderManager::startExternalLazyProvider() const {
671 std::lock_guard<std::mutex> providerLock(mProviderLifecycleLock);
672 std::lock_guard<std::mutex> lock(mInterfaceMutex);
673
674 for (const auto& providerInfo : mProviders) {
675 if (providerInfo->isExternalLazyHAL()) {
676 if (!providerInfo->successfullyStartedProviderInterface()) {
677 return nullptr;
678 } else {
679 return providerInfo;
680 }
681 }
682 }
683 return nullptr;
684 }
685
notifyUsbDeviceEvent(int32_t eventId,const std::string & usbDeviceId)686 status_t CameraProviderManager::notifyUsbDeviceEvent(int32_t eventId,
687 const std::string& usbDeviceId) {
688 if (!kEnableLazyHal) {
689 return OK;
690 }
691
692 ALOGV("notifySystemEvent: %d usbDeviceId : %s", eventId, usbDeviceId.c_str());
693
694 if (eventId == android::hardware::ICameraService::EVENT_USB_DEVICE_ATTACHED) {
695 sp<ProviderInfo> externalProvider = startExternalLazyProvider();
696 if (externalProvider != nullptr) {
697 auto usbDevices = mExternalUsbDevicesForProvider.first;
698 usbDevices.push_back(usbDeviceId);
699 mExternalUsbDevicesForProvider = {usbDevices, externalProvider};
700 }
701 } else if (eventId
702 == android::hardware::ICameraService::EVENT_USB_DEVICE_DETACHED) {
703 usbDeviceDetached(usbDeviceId);
704 }
705
706 return OK;
707 }
708
usbDeviceDetached(const std::string & usbDeviceId)709 status_t CameraProviderManager::usbDeviceDetached(const std::string &usbDeviceId) {
710 std::lock_guard<std::mutex> providerLock(mProviderLifecycleLock);
711 std::lock_guard<std::mutex> interfaceLock(mInterfaceMutex);
712
713 auto usbDevices = mExternalUsbDevicesForProvider.first;
714 auto foundId = std::find(usbDevices.begin(), usbDevices.end(), usbDeviceId);
715 if (foundId != usbDevices.end()) {
716 sp<ProviderInfo> providerInfo = mExternalUsbDevicesForProvider.second;
717 if (providerInfo == nullptr) {
718 ALOGE("%s No valid external provider for USB device: %s",
719 __FUNCTION__,
720 usbDeviceId.c_str());
721 mExternalUsbDevicesForProvider = {std::vector<std::string>(), nullptr};
722 return DEAD_OBJECT;
723 } else {
724 mInterfaceMutex.unlock();
725 providerInfo->removeAllDevices();
726 mInterfaceMutex.lock();
727 mExternalUsbDevicesForProvider = {std::vector<std::string>(), nullptr};
728 }
729 } else {
730 return DEAD_OBJECT;
731 }
732 return OK;
733 }
734
notifyDeviceStateChange(int64_t newState)735 status_t CameraProviderManager::notifyDeviceStateChange(int64_t newState) {
736 std::lock_guard<std::mutex> lock(mInterfaceMutex);
737 mDeviceState = newState;
738 status_t res = OK;
739 // Make a copy of mProviders because we unlock mInterfaceMutex temporarily
740 // within the loop. It's possible that during the time mInterfaceMutex is
741 // unlocked, mProviders has changed.
742 auto providers = mProviders;
743 for (auto& provider : providers) {
744 ALOGV("%s: Notifying %s for new state 0x%" PRIx64,
745 __FUNCTION__, provider->mProviderName.c_str(), newState);
746 // b/199240726 Camera providers can for example try to add/remove
747 // camera devices as part of the state change notification. Holding
748 // 'mInterfaceMutex' while calling 'notifyDeviceStateChange' can
749 // result in a recursive deadlock.
750 mInterfaceMutex.unlock();
751 status_t singleRes = provider->notifyDeviceStateChange(mDeviceState);
752 mInterfaceMutex.lock();
753 if (singleRes != OK) {
754 ALOGE("%s: Unable to notify provider %s about device state change",
755 __FUNCTION__,
756 provider->mProviderName.c_str());
757 res = singleRes;
758 // continue to do the rest of the providers instead of returning now
759 }
760 provider->notifyDeviceInfoStateChangeLocked(mDeviceState);
761 }
762 return res;
763 }
764
openAidlSession(const std::string & id,const std::shared_ptr<aidl::android::hardware::camera::device::ICameraDeviceCallback> & callback,std::shared_ptr<aidl::android::hardware::camera::device::ICameraDeviceSession> * session)765 status_t CameraProviderManager::openAidlSession(const std::string &id,
766 const std::shared_ptr<
767 aidl::android::hardware::camera::device::ICameraDeviceCallback>& callback,
768 /*out*/
769 std::shared_ptr<aidl::android::hardware::camera::device::ICameraDeviceSession> *session) {
770
771 std::lock_guard<std::mutex> lock(mInterfaceMutex);
772
773 auto deviceInfo = findDeviceInfoLocked(id);
774 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
775
776 auto *aidlDeviceInfo3 = static_cast<AidlProviderInfo::AidlDeviceInfo3*>(deviceInfo);
777 sp<ProviderInfo> parentProvider = deviceInfo->mParentProvider.promote();
778 if (parentProvider == nullptr) {
779 return DEAD_OBJECT;
780 }
781 auto provider =
782 static_cast<AidlProviderInfo *>(parentProvider.get())->startProviderInterface();
783 if (provider == nullptr) {
784 return DEAD_OBJECT;
785 }
786 std::shared_ptr<HalCameraProvider> halCameraProvider =
787 std::make_shared<AidlHalCameraProvider>(provider, provider->descriptor);
788 saveRef(DeviceMode::CAMERA, id, halCameraProvider);
789
790 auto interface = aidlDeviceInfo3->startDeviceInterface();
791 if (interface == nullptr) {
792 removeRef(DeviceMode::CAMERA, id);
793 return DEAD_OBJECT;
794 }
795
796 auto ret = interface->open(callback, session);
797 if (!ret.isOk()) {
798 removeRef(DeviceMode::CAMERA, id);
799 ALOGE("%s: Transaction error opening a session for camera device %s: %s",
800 __FUNCTION__, id.c_str(), ret.getMessage());
801 return AidlProviderInfo::mapToStatusT(ret);
802 }
803 return OK;
804 }
805
openAidlInjectionSession(const std::string & id,const std::shared_ptr<aidl::android::hardware::camera::device::ICameraDeviceCallback> & callback,std::shared_ptr<aidl::android::hardware::camera::device::ICameraInjectionSession> * session)806 status_t CameraProviderManager::openAidlInjectionSession(const std::string &id,
807 const std::shared_ptr<
808 aidl::android::hardware::camera::device::ICameraDeviceCallback>& callback,
809 /*out*/
810 std::shared_ptr<
811 aidl::android::hardware::camera::device::ICameraInjectionSession> *session) {
812
813 std::lock_guard<std::mutex> lock(mInterfaceMutex);
814
815 auto deviceInfo = findDeviceInfoLocked(id);
816 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
817
818 auto *aidlDeviceInfo3 = static_cast<AidlProviderInfo::AidlDeviceInfo3*>(deviceInfo);
819 sp<ProviderInfo> parentProvider = deviceInfo->mParentProvider.promote();
820 if (parentProvider == nullptr) {
821 return DEAD_OBJECT;
822 }
823 auto provider =
824 static_cast<AidlProviderInfo *>(parentProvider.get())->startProviderInterface();
825 if (provider == nullptr) {
826 return DEAD_OBJECT;
827 }
828 std::shared_ptr<HalCameraProvider> halCameraProvider =
829 std::make_shared<AidlHalCameraProvider>(provider, provider->descriptor);
830 saveRef(DeviceMode::CAMERA, id, halCameraProvider);
831
832 auto interface = aidlDeviceInfo3->startDeviceInterface();
833 if (interface == nullptr) {
834 return DEAD_OBJECT;
835 }
836
837 auto ret = interface->openInjectionSession(callback, session);
838 if (!ret.isOk()) {
839 removeRef(DeviceMode::CAMERA, id);
840 ALOGE("%s: Transaction error opening a session for camera device %s: %s",
841 __FUNCTION__, id.c_str(), ret.getMessage());
842 return DEAD_OBJECT;
843 }
844 return OK;
845 }
846
openHidlSession(const std::string & id,const sp<device::V3_2::ICameraDeviceCallback> & callback,sp<device::V3_2::ICameraDeviceSession> * session)847 status_t CameraProviderManager::openHidlSession(const std::string &id,
848 const sp<device::V3_2::ICameraDeviceCallback>& callback,
849 /*out*/
850 sp<device::V3_2::ICameraDeviceSession> *session) {
851
852 std::lock_guard<std::mutex> lock(mInterfaceMutex);
853
854 auto deviceInfo = findDeviceInfoLocked(id);
855 if (deviceInfo == nullptr) return NAME_NOT_FOUND;
856
857 auto *hidlDeviceInfo3 = static_cast<HidlProviderInfo::HidlDeviceInfo3*>(deviceInfo);
858 sp<ProviderInfo> parentProvider = deviceInfo->mParentProvider.promote();
859 if (parentProvider == nullptr) {
860 return DEAD_OBJECT;
861 }
862 const sp<provider::V2_4::ICameraProvider> provider =
863 static_cast<HidlProviderInfo *>(parentProvider.get())->startProviderInterface();
864 if (provider == nullptr) {
865 return DEAD_OBJECT;
866 }
867 std::shared_ptr<HalCameraProvider> halCameraProvider =
868 std::make_shared<HidlHalCameraProvider>(provider, provider->descriptor);
869 saveRef(DeviceMode::CAMERA, id, halCameraProvider);
870
871 Status status;
872 hardware::Return<void> ret;
873 auto interface = hidlDeviceInfo3->startDeviceInterface();
874 if (interface == nullptr) {
875 return DEAD_OBJECT;
876 }
877
878 ret = interface->open(callback, [&status, &session]
879 (Status s, const sp<device::V3_2::ICameraDeviceSession>& cameraSession) {
880 status = s;
881 if (status == Status::OK) {
882 *session = cameraSession;
883 }
884 });
885 if (!ret.isOk()) {
886 removeRef(DeviceMode::CAMERA, id);
887 ALOGE("%s: Transaction error opening a session for camera device %s: %s",
888 __FUNCTION__, id.c_str(), ret.description().c_str());
889 return DEAD_OBJECT;
890 }
891 return HidlProviderInfo::mapToStatusT(status);
892 }
893
saveRef(DeviceMode usageType,const std::string & cameraId,std::shared_ptr<HalCameraProvider> provider)894 void CameraProviderManager::saveRef(DeviceMode usageType, const std::string &cameraId,
895 std::shared_ptr<HalCameraProvider> provider) {
896 if (!kEnableLazyHal) {
897 return;
898 }
899 ALOGV("Saving camera provider %s for camera device %s", provider->mDescriptor.c_str(),
900 cameraId.c_str());
901 std::lock_guard<std::mutex> lock(mProviderInterfaceMapLock);
902 std::unordered_map<std::string, std::shared_ptr<HalCameraProvider>> *primaryMap, *alternateMap;
903 if (usageType == DeviceMode::TORCH) {
904 primaryMap = &mTorchProviderByCameraId;
905 alternateMap = &mCameraProviderByCameraId;
906 } else {
907 primaryMap = &mCameraProviderByCameraId;
908 alternateMap = &mTorchProviderByCameraId;
909 }
910
911 (*primaryMap)[cameraId] = provider;
912 auto search = alternateMap->find(cameraId);
913 if (search != alternateMap->end()) {
914 ALOGW("%s: Camera device %s is using both torch mode and camera mode simultaneously. "
915 "That should not be possible", __FUNCTION__, cameraId.c_str());
916 }
917 ALOGV("%s: Camera device %s connected", __FUNCTION__, cameraId.c_str());
918 }
919
removeRef(DeviceMode usageType,const std::string & cameraId)920 void CameraProviderManager::removeRef(DeviceMode usageType, const std::string &cameraId) {
921 if (!kEnableLazyHal) {
922 return;
923 }
924 ALOGV("Removing camera device %s", cameraId.c_str());
925 std::unordered_map<std::string, std::shared_ptr<HalCameraProvider>> *providerMap;
926 if (usageType == DeviceMode::TORCH) {
927 providerMap = &mTorchProviderByCameraId;
928 } else {
929 providerMap = &mCameraProviderByCameraId;
930 }
931 std::lock_guard<std::mutex> lock(mProviderInterfaceMapLock);
932 auto search = providerMap->find(cameraId);
933 if (search != providerMap->end()) {
934 // Drop the reference to this ICameraProvider. This is safe to do immediately (without an
935 // added delay) because hwservicemanager guarantees to hold the reference for at least five
936 // more seconds. We depend on this behavior so that if the provider is unreferenced and
937 // then referenced again quickly, we do not let the HAL exit and then need to immediately
938 // restart it. An example when this could happen is switching from a front-facing to a
939 // rear-facing camera. If the HAL were to exit during the camera switch, the camera could
940 // appear janky to the user.
941 providerMap->erase(cameraId);
942 IPCThreadState::self()->flushCommands();
943 } else {
944 ALOGE("%s: Asked to remove reference for camera %s, but no reference to it was found. This "
945 "could mean removeRef was called twice for the same camera ID.", __FUNCTION__,
946 cameraId.c_str());
947 }
948 }
949
950 // We ignore sp<IBinder> param here since we need std::shared_ptr<...> which
951 // will be retrieved through the ndk api through addAidlProviderLocked ->
952 // tryToInitializeAidlProvider.
onServiceRegistration(const String16 & name,const sp<IBinder> &)953 void CameraProviderManager::onServiceRegistration(const String16 &name, const sp<IBinder>&) {
954 status_t res = OK;
955 std::lock_guard<std::mutex> providerLock(mProviderLifecycleLock);
956 {
957 std::lock_guard<std::mutex> lock(mInterfaceMutex);
958
959 res = addAidlProviderLocked(toStdString(name));
960 }
961
962 sp<StatusListener> listener = getStatusListener();
963 if (nullptr != listener.get() && res == OK) {
964 listener->onNewProviderRegistered();
965 }
966
967 IPCThreadState::self()->flushCommands();
968 }
969
onRegistration(const hardware::hidl_string &,const hardware::hidl_string & name,bool preexisting)970 hardware::Return<void> CameraProviderManager::onRegistration(
971 const hardware::hidl_string& /*fqName*/,
972 const hardware::hidl_string& name,
973 bool preexisting) {
974 status_t res = OK;
975 std::lock_guard<std::mutex> providerLock(mProviderLifecycleLock);
976 {
977 std::lock_guard<std::mutex> lock(mInterfaceMutex);
978
979 res = addHidlProviderLocked(name, preexisting);
980 }
981
982 sp<StatusListener> listener = getStatusListener();
983 if (nullptr != listener.get() && res == OK) {
984 listener->onNewProviderRegistered();
985 }
986
987 IPCThreadState::self()->flushCommands();
988
989 return hardware::Return<void>();
990 }
991
dump(int fd,const Vector<String16> & args)992 status_t CameraProviderManager::dump(int fd, const Vector<String16>& args) {
993 std::lock_guard<std::mutex> lock(mInterfaceMutex);
994
995 for (auto& provider : mProviders) {
996 provider->dump(fd, args);
997 }
998 return OK;
999 }
1000
initializeProviderInfoCommon(const std::vector<std::string> & devices)1001 void CameraProviderManager::ProviderInfo::initializeProviderInfoCommon(
1002 const std::vector<std::string> &devices) {
1003 for (auto& device : devices) {
1004 std::string id;
1005 status_t res = addDevice(device, CameraDeviceStatus::PRESENT, &id);
1006 if (res != OK) {
1007 ALOGE("%s: Unable to enumerate camera device '%s': %s (%d)",
1008 __FUNCTION__, device.c_str(), strerror(-res), res);
1009 continue;
1010 }
1011 }
1012
1013 ALOGI("Camera provider %s ready with %zu camera devices",
1014 mProviderName.c_str(), mDevices.size());
1015
1016 // Process cached status callbacks
1017 {
1018 std::lock_guard<std::mutex> lock(mInitLock);
1019
1020 for (auto& statusInfo : mCachedStatus) {
1021 std::string id, physicalId;
1022 if (statusInfo.isPhysicalCameraStatus) {
1023 physicalCameraDeviceStatusChangeLocked(&id, &physicalId,
1024 statusInfo.cameraId, statusInfo.physicalCameraId, statusInfo.status);
1025 } else {
1026 cameraDeviceStatusChangeLocked(&id, statusInfo.cameraId, statusInfo.status);
1027 }
1028 }
1029 mCachedStatus.clear();
1030
1031 mInitialized = true;
1032 }
1033 }
1034
findDeviceInfoLocked(const std::string & id) const1035 CameraProviderManager::ProviderInfo::DeviceInfo* CameraProviderManager::findDeviceInfoLocked(
1036 const std::string& id) const {
1037 for (auto& provider : mProviders) {
1038 using hardware::hidl_version;
1039 IPCTransport transport = provider->getIPCTransport();
1040 // AIDL min version starts at major: 1 minor: 1
1041 hidl_version minVersion =
1042 (transport == IPCTransport::HIDL) ? hidl_version{3, 2} : hidl_version{1, 1} ;
1043 hidl_version maxVersion =
1044 (transport == IPCTransport::HIDL) ? hidl_version{3, 7} : hidl_version{1000, 0};
1045
1046 for (auto& deviceInfo : provider->mDevices) {
1047 if (deviceInfo->mId == id &&
1048 minVersion <= deviceInfo->mVersion && maxVersion >= deviceInfo->mVersion) {
1049 return deviceInfo.get();
1050 }
1051 }
1052 }
1053 return nullptr;
1054 }
1055
getProviderTagIdLocked(const std::string & id) const1056 metadata_vendor_id_t CameraProviderManager::getProviderTagIdLocked(
1057 const std::string& id) const {
1058 metadata_vendor_id_t ret = CAMERA_METADATA_INVALID_VENDOR_ID;
1059
1060 std::lock_guard<std::mutex> lock(mInterfaceMutex);
1061 for (auto& provider : mProviders) {
1062 for (auto& deviceInfo : provider->mDevices) {
1063 if (deviceInfo->mId == id) {
1064 return provider->mProviderTagid;
1065 }
1066 }
1067 }
1068
1069 return ret;
1070 }
1071
queryPhysicalCameraIds()1072 void CameraProviderManager::ProviderInfo::DeviceInfo3::queryPhysicalCameraIds() {
1073 camera_metadata_entry_t entryCap;
1074
1075 entryCap = mCameraCharacteristics.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
1076 for (size_t i = 0; i < entryCap.count; ++i) {
1077 uint8_t capability = entryCap.data.u8[i];
1078 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA) {
1079 mIsLogicalCamera = true;
1080 break;
1081 }
1082 }
1083 if (!mIsLogicalCamera) {
1084 return;
1085 }
1086
1087 camera_metadata_entry_t entryIds = mCameraCharacteristics.find(
1088 ANDROID_LOGICAL_MULTI_CAMERA_PHYSICAL_IDS);
1089 const uint8_t* ids = entryIds.data.u8;
1090 size_t start = 0;
1091 for (size_t i = 0; i < entryIds.count; ++i) {
1092 if (ids[i] == '\0') {
1093 if (start != i) {
1094 mPhysicalIds.push_back((const char*)ids+start);
1095 }
1096 start = i+1;
1097 }
1098 }
1099 }
1100
getSystemCameraKind()1101 SystemCameraKind CameraProviderManager::ProviderInfo::DeviceInfo3::getSystemCameraKind() {
1102 camera_metadata_entry_t entryCap;
1103 entryCap = mCameraCharacteristics.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
1104 if (entryCap.count == 1 &&
1105 entryCap.data.u8[0] == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_SECURE_IMAGE_DATA) {
1106 return SystemCameraKind::HIDDEN_SECURE_CAMERA;
1107 }
1108
1109 // Go through the capabilities and check if it has
1110 // ANDROID_REQUEST_AVAILABLE_CAPABILITIES_SYSTEM_CAMERA
1111 for (size_t i = 0; i < entryCap.count; ++i) {
1112 uint8_t capability = entryCap.data.u8[i];
1113 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_SYSTEM_CAMERA) {
1114 return SystemCameraKind::SYSTEM_ONLY_CAMERA;
1115 }
1116 }
1117 return SystemCameraKind::PUBLIC;
1118 }
1119
getSupportedSizes(const CameraMetadata & ch,uint32_t tag,android_pixel_format_t format,std::vector<std::tuple<size_t,size_t>> * sizes)1120 void CameraProviderManager::ProviderInfo::DeviceInfo3::getSupportedSizes(
1121 const CameraMetadata& ch, uint32_t tag, android_pixel_format_t format,
1122 std::vector<std::tuple<size_t, size_t>> *sizes/*out*/) {
1123 if (sizes == nullptr) {
1124 return;
1125 }
1126
1127 auto scalerDims = ch.find(tag);
1128 if (scalerDims.count > 0) {
1129 // Scaler entry contains 4 elements (format, width, height, type)
1130 for (size_t i = 0; i < scalerDims.count; i += 4) {
1131 if ((scalerDims.data.i32[i] == format) &&
1132 (scalerDims.data.i32[i+3] ==
1133 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT)) {
1134 sizes->push_back(std::make_tuple(scalerDims.data.i32[i+1],
1135 scalerDims.data.i32[i+2]));
1136 }
1137 }
1138 }
1139 }
1140
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)1141 void CameraProviderManager::ProviderInfo::DeviceInfo3::getSupportedDurations(
1142 const CameraMetadata& ch, uint32_t tag, android_pixel_format_t format,
1143 const std::vector<std::tuple<size_t, size_t>>& sizes,
1144 std::vector<int64_t> *durations/*out*/) {
1145 if (durations == nullptr) {
1146 return;
1147 }
1148
1149 auto availableDurations = ch.find(tag);
1150 if (availableDurations.count > 0) {
1151 // Duration entry contains 4 elements (format, width, height, duration)
1152 for (const auto& size : sizes) {
1153 int64_t width = std::get<0>(size);
1154 int64_t height = std::get<1>(size);
1155 for (size_t i = 0; i < availableDurations.count; i += 4) {
1156 if ((availableDurations.data.i64[i] == format) &&
1157 (availableDurations.data.i64[i+1] == width) &&
1158 (availableDurations.data.i64[i+2] == height)) {
1159 durations->push_back(availableDurations.data.i64[i+3]);
1160 break;
1161 }
1162 }
1163 }
1164 }
1165 }
1166
getSupportedDynamicDepthDurations(const std::vector<int64_t> & depthDurations,const std::vector<int64_t> & blobDurations,std::vector<int64_t> * dynamicDepthDurations)1167 void CameraProviderManager::ProviderInfo::DeviceInfo3::getSupportedDynamicDepthDurations(
1168 const std::vector<int64_t>& depthDurations, const std::vector<int64_t>& blobDurations,
1169 std::vector<int64_t> *dynamicDepthDurations /*out*/) {
1170 if ((dynamicDepthDurations == nullptr) || (depthDurations.size() != blobDurations.size())) {
1171 return;
1172 }
1173
1174 // Unfortunately there is no direct way to calculate the dynamic depth stream duration.
1175 // Processing time on camera service side can vary greatly depending on multiple
1176 // variables which are not under our control. Make a guesstimate by taking the maximum
1177 // corresponding duration value from depth and blob.
1178 auto depthDuration = depthDurations.begin();
1179 auto blobDuration = blobDurations.begin();
1180 dynamicDepthDurations->reserve(depthDurations.size());
1181 while ((depthDuration != depthDurations.end()) && (blobDuration != blobDurations.end())) {
1182 dynamicDepthDurations->push_back(std::max(*depthDuration, *blobDuration));
1183 depthDuration++; blobDuration++;
1184 }
1185 }
1186
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)1187 void CameraProviderManager::ProviderInfo::DeviceInfo3::getSupportedDynamicDepthSizes(
1188 const std::vector<std::tuple<size_t, size_t>>& blobSizes,
1189 const std::vector<std::tuple<size_t, size_t>>& depthSizes,
1190 std::vector<std::tuple<size_t, size_t>> *dynamicDepthSizes /*out*/,
1191 std::vector<std::tuple<size_t, size_t>> *internalDepthSizes /*out*/) {
1192 if (dynamicDepthSizes == nullptr || internalDepthSizes == nullptr) {
1193 return;
1194 }
1195
1196 // The dynamic depth spec. does not mention how close the AR ratio should be.
1197 // Try using something appropriate.
1198 float ARTolerance = kDepthARTolerance;
1199
1200 for (const auto& blobSize : blobSizes) {
1201 float jpegAR = static_cast<float> (std::get<0>(blobSize)) /
1202 static_cast<float>(std::get<1>(blobSize));
1203 bool found = false;
1204 for (const auto& depthSize : depthSizes) {
1205 if (depthSize == blobSize) {
1206 internalDepthSizes->push_back(depthSize);
1207 found = true;
1208 break;
1209 } else {
1210 float depthAR = static_cast<float> (std::get<0>(depthSize)) /
1211 static_cast<float>(std::get<1>(depthSize));
1212 if (std::fabs(jpegAR - depthAR) <= ARTolerance) {
1213 internalDepthSizes->push_back(depthSize);
1214 found = true;
1215 break;
1216 }
1217 }
1218 }
1219
1220 if (found) {
1221 dynamicDepthSizes->push_back(blobSize);
1222 }
1223 }
1224 }
1225
isConcurrentDynamicRangeCaptureSupported(const CameraMetadata & deviceInfo,int64_t profile,int64_t concurrentProfile)1226 bool CameraProviderManager::isConcurrentDynamicRangeCaptureSupported(
1227 const CameraMetadata& deviceInfo, int64_t profile, int64_t concurrentProfile) {
1228 auto entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
1229 if (entry.count == 0) {
1230 return false;
1231 }
1232
1233 const auto it = std::find(entry.data.u8, entry.data.u8 + entry.count,
1234 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DYNAMIC_RANGE_TEN_BIT);
1235 if (it == entry.data.u8 + entry.count) {
1236 return false;
1237 }
1238
1239 entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP);
1240 if (entry.count == 0 || ((entry.count % 3) != 0)) {
1241 return false;
1242 }
1243
1244 for (size_t i = 0; i < entry.count; i += 3) {
1245 if (entry.data.i64[i] == profile) {
1246 if ((entry.data.i64[i+1] == 0) || (entry.data.i64[i+1] & concurrentProfile)) {
1247 return true;
1248 }
1249 }
1250 }
1251
1252 return false;
1253 }
1254
deriveJpegRTags(bool maxResolution)1255 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::deriveJpegRTags(bool maxResolution) {
1256 if (kFrameworkJpegRDisabled || mCompositeJpegRDisabled) {
1257 return OK;
1258 }
1259
1260 const int32_t scalerSizesTag =
1261 SessionConfigurationUtils::getAppropriateModeTag(
1262 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, maxResolution);
1263 const int32_t scalerMinFrameDurationsTag = SessionConfigurationUtils::getAppropriateModeTag(
1264 ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS, maxResolution);
1265 const int32_t scalerStallDurationsTag =
1266 SessionConfigurationUtils::getAppropriateModeTag(
1267 ANDROID_SCALER_AVAILABLE_STALL_DURATIONS, maxResolution);
1268
1269 const int32_t jpegRSizesTag =
1270 SessionConfigurationUtils::getAppropriateModeTag(
1271 ANDROID_JPEGR_AVAILABLE_JPEG_R_STREAM_CONFIGURATIONS, maxResolution);
1272 const int32_t jpegRStallDurationsTag =
1273 SessionConfigurationUtils::getAppropriateModeTag(
1274 ANDROID_JPEGR_AVAILABLE_JPEG_R_STALL_DURATIONS, maxResolution);
1275 const int32_t jpegRMinFrameDurationsTag =
1276 SessionConfigurationUtils::getAppropriateModeTag(
1277 ANDROID_JPEGR_AVAILABLE_JPEG_R_MIN_FRAME_DURATIONS, maxResolution);
1278
1279 auto& c = mCameraCharacteristics;
1280 std::vector<int32_t> supportedChTags;
1281 auto chTags = c.find(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
1282 if (chTags.count == 0) {
1283 ALOGE("%s: No supported camera characteristics keys!", __FUNCTION__);
1284 return BAD_VALUE;
1285 }
1286
1287 std::vector<std::tuple<size_t, size_t>> supportedP010Sizes, supportedBlobSizes;
1288 auto capabilities = c.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
1289 if (capabilities.count == 0) {
1290 ALOGE("%s: Supported camera capabilities is empty!", __FUNCTION__);
1291 return BAD_VALUE;
1292 }
1293
1294 auto end = capabilities.data.u8 + capabilities.count;
1295 bool isTenBitOutputSupported = std::find(capabilities.data.u8, end,
1296 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DYNAMIC_RANGE_TEN_BIT) != end;
1297 if (!isTenBitOutputSupported) {
1298 // No 10-bit support, nothing more to do.
1299 return OK;
1300 }
1301
1302 if (!isConcurrentDynamicRangeCaptureSupported(c,
1303 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_HLG10,
1304 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) &&
1305 !property_get_bool("ro.camera.enableCompositeAPI0JpegR", false)) {
1306 // API0, P010 only Jpeg/R support is meant to be used only as a reference due to possible
1307 // impact on quality and performance.
1308 // This data path will be turned off by default and individual device builds must enable
1309 // 'ro.camera.enableCompositeAPI0JpegR' in order to experiment using it.
1310 mCompositeJpegRDisabled = true;
1311 return OK;
1312 }
1313
1314 getSupportedSizes(c, scalerSizesTag,
1315 static_cast<android_pixel_format_t>(HAL_PIXEL_FORMAT_BLOB), &supportedBlobSizes);
1316 getSupportedSizes(c, scalerSizesTag,
1317 static_cast<android_pixel_format_t>(HAL_PIXEL_FORMAT_YCBCR_P010), &supportedP010Sizes);
1318 auto it = supportedP010Sizes.begin();
1319 while (it != supportedP010Sizes.end()) {
1320 if (std::find(supportedBlobSizes.begin(), supportedBlobSizes.end(), *it) ==
1321 supportedBlobSizes.end()) {
1322 it = supportedP010Sizes.erase(it);
1323 } else {
1324 it++;
1325 }
1326 }
1327 if (supportedP010Sizes.empty()) {
1328 // Nothing to do in this case.
1329 return OK;
1330 }
1331
1332 std::vector<int32_t> jpegREntries;
1333 for (const auto& it : supportedP010Sizes) {
1334 int32_t entry[4] = {HAL_PIXEL_FORMAT_BLOB, static_cast<int32_t> (std::get<0>(it)),
1335 static_cast<int32_t> (std::get<1>(it)),
1336 ANDROID_JPEGR_AVAILABLE_JPEG_R_STREAM_CONFIGURATIONS_OUTPUT };
1337 jpegREntries.insert(jpegREntries.end(), entry, entry + 4);
1338 }
1339
1340 std::vector<int64_t> blobMinDurations, blobStallDurations;
1341 std::vector<int64_t> jpegRMinDurations, jpegRStallDurations;
1342
1343 // We use the jpeg stall and min frame durations to approximate the respective jpeg/r
1344 // durations.
1345 getSupportedDurations(c, scalerMinFrameDurationsTag, HAL_PIXEL_FORMAT_BLOB,
1346 supportedP010Sizes, &blobMinDurations);
1347 getSupportedDurations(c, scalerStallDurationsTag, HAL_PIXEL_FORMAT_BLOB,
1348 supportedP010Sizes, &blobStallDurations);
1349 if (blobStallDurations.empty() || blobMinDurations.empty() ||
1350 supportedP010Sizes.size() != blobMinDurations.size() ||
1351 blobMinDurations.size() != blobStallDurations.size()) {
1352 ALOGE("%s: Unexpected number of available blob durations! %zu vs. %zu with "
1353 "supportedP010Sizes size: %zu", __FUNCTION__, blobMinDurations.size(),
1354 blobStallDurations.size(), supportedP010Sizes.size());
1355 return BAD_VALUE;
1356 }
1357
1358 auto itDuration = blobMinDurations.begin();
1359 auto itSize = supportedP010Sizes.begin();
1360 while (itDuration != blobMinDurations.end()) {
1361 int64_t entry[4] = {HAL_PIXEL_FORMAT_BLOB, static_cast<int32_t> (std::get<0>(*itSize)),
1362 static_cast<int32_t> (std::get<1>(*itSize)), *itDuration};
1363 jpegRMinDurations.insert(jpegRMinDurations.end(), entry, entry + 4);
1364 itDuration++; itSize++;
1365 }
1366
1367 itDuration = blobStallDurations.begin();
1368 itSize = supportedP010Sizes.begin();
1369 while (itDuration != blobStallDurations.end()) {
1370 int64_t entry[4] = {HAL_PIXEL_FORMAT_BLOB, static_cast<int32_t> (std::get<0>(*itSize)),
1371 static_cast<int32_t> (std::get<1>(*itSize)), *itDuration};
1372 jpegRStallDurations.insert(jpegRStallDurations.end(), entry, entry + 4);
1373 itDuration++; itSize++;
1374 }
1375
1376 supportedChTags.reserve(chTags.count + 3);
1377 supportedChTags.insert(supportedChTags.end(), chTags.data.i32,
1378 chTags.data.i32 + chTags.count);
1379 supportedChTags.push_back(jpegRSizesTag);
1380 supportedChTags.push_back(jpegRMinFrameDurationsTag);
1381 supportedChTags.push_back(jpegRStallDurationsTag);
1382 c.update(jpegRSizesTag, jpegREntries.data(), jpegREntries.size());
1383 c.update(jpegRMinFrameDurationsTag, jpegRMinDurations.data(), jpegRMinDurations.size());
1384 c.update(jpegRStallDurationsTag, jpegRStallDurations.data(), jpegRStallDurations.size());
1385 c.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, supportedChTags.data(),
1386 supportedChTags.size());
1387
1388 auto colorSpaces = c.find(ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP);
1389 if (colorSpaces.count > 0 && !maxResolution) {
1390 bool displayP3Support = false;
1391 int64_t dynamicRange = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
1392 for (size_t i = 0; i < colorSpaces.count; i += 3) {
1393 auto colorSpace = colorSpaces.data.i64[i];
1394 auto format = colorSpaces.data.i64[i+1];
1395 bool formatMatch = (format == static_cast<int64_t>(PublicFormat::JPEG)) ||
1396 (format == static_cast<int64_t>(PublicFormat::UNKNOWN));
1397 bool colorSpaceMatch =
1398 colorSpace == ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_DISPLAY_P3;
1399 if (formatMatch && colorSpaceMatch) {
1400 displayP3Support = true;
1401 }
1402
1403 // Jpeg/R will support the same dynamic range profiles as P010
1404 if (format == static_cast<int64_t>(PublicFormat::YCBCR_P010)) {
1405 dynamicRange |= colorSpaces.data.i64[i+2];
1406 }
1407 }
1408 if (displayP3Support) {
1409 std::vector<int64_t> supportedColorSpaces;
1410 // Jpeg/R must support the default system as well ase display P3 color space
1411 supportedColorSpaces.reserve(colorSpaces.count + 3*2);
1412 supportedColorSpaces.insert(supportedColorSpaces.end(), colorSpaces.data.i64,
1413 colorSpaces.data.i64 + colorSpaces.count);
1414
1415 supportedColorSpaces.push_back(static_cast<int64_t>(
1416 ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_SRGB));
1417 supportedColorSpaces.push_back(static_cast<int64_t>(PublicFormat::JPEG_R));
1418 supportedColorSpaces.push_back(dynamicRange);
1419
1420 supportedColorSpaces.push_back(static_cast<int64_t>(
1421 ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_DISPLAY_P3));
1422 supportedColorSpaces.push_back(static_cast<int64_t>(PublicFormat::JPEG_R));
1423 supportedColorSpaces.push_back(dynamicRange);
1424 c.update(ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP,
1425 supportedColorSpaces.data(), supportedColorSpaces.size());
1426 }
1427 }
1428
1429 return OK;
1430 }
1431
addDynamicDepthTags(bool maxResolution)1432 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::addDynamicDepthTags(
1433 bool maxResolution) {
1434 const int32_t depthExclTag = ANDROID_DEPTH_DEPTH_IS_EXCLUSIVE;
1435
1436 const int32_t scalerSizesTag =
1437 SessionConfigurationUtils::getAppropriateModeTag(
1438 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, maxResolution);
1439 const int32_t scalerMinFrameDurationsTag =
1440 ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS;
1441 const int32_t scalerStallDurationsTag =
1442 SessionConfigurationUtils::getAppropriateModeTag(
1443 ANDROID_SCALER_AVAILABLE_STALL_DURATIONS, maxResolution);
1444
1445 const int32_t depthSizesTag =
1446 SessionConfigurationUtils::getAppropriateModeTag(
1447 ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS, maxResolution);
1448 const int32_t depthStallDurationsTag =
1449 SessionConfigurationUtils::getAppropriateModeTag(
1450 ANDROID_DEPTH_AVAILABLE_DEPTH_STALL_DURATIONS, maxResolution);
1451 const int32_t depthMinFrameDurationsTag =
1452 SessionConfigurationUtils::getAppropriateModeTag(
1453 ANDROID_DEPTH_AVAILABLE_DEPTH_MIN_FRAME_DURATIONS, maxResolution);
1454
1455 const int32_t dynamicDepthSizesTag =
1456 SessionConfigurationUtils::getAppropriateModeTag(
1457 ANDROID_DEPTH_AVAILABLE_DYNAMIC_DEPTH_STREAM_CONFIGURATIONS, maxResolution);
1458 const int32_t dynamicDepthStallDurationsTag =
1459 SessionConfigurationUtils::getAppropriateModeTag(
1460 ANDROID_DEPTH_AVAILABLE_DYNAMIC_DEPTH_STALL_DURATIONS, maxResolution);
1461 const int32_t dynamicDepthMinFrameDurationsTag =
1462 SessionConfigurationUtils::getAppropriateModeTag(
1463 ANDROID_DEPTH_AVAILABLE_DYNAMIC_DEPTH_MIN_FRAME_DURATIONS, maxResolution);
1464
1465 auto& c = mCameraCharacteristics;
1466 std::vector<std::tuple<size_t, size_t>> supportedBlobSizes, supportedDepthSizes,
1467 supportedDynamicDepthSizes, internalDepthSizes;
1468 auto chTags = c.find(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
1469 if (chTags.count == 0) {
1470 ALOGE("%s: Supported camera characteristics is empty!", __FUNCTION__);
1471 return BAD_VALUE;
1472 }
1473
1474 bool isDepthExclusivePresent = std::find(chTags.data.i32, chTags.data.i32 + chTags.count,
1475 depthExclTag) != (chTags.data.i32 + chTags.count);
1476 bool isDepthSizePresent = std::find(chTags.data.i32, chTags.data.i32 + chTags.count,
1477 depthSizesTag) != (chTags.data.i32 + chTags.count);
1478 if (!(isDepthExclusivePresent && isDepthSizePresent)) {
1479 // No depth support, nothing more to do.
1480 return OK;
1481 }
1482
1483 auto depthExclusiveEntry = c.find(depthExclTag);
1484 if (depthExclusiveEntry.count > 0) {
1485 if (depthExclusiveEntry.data.u8[0] != ANDROID_DEPTH_DEPTH_IS_EXCLUSIVE_FALSE) {
1486 // Depth support is exclusive, nothing more to do.
1487 return OK;
1488 }
1489 } else {
1490 ALOGE("%s: Advertised depth exclusive tag but value is not present!", __FUNCTION__);
1491 return BAD_VALUE;
1492 }
1493
1494 getSupportedSizes(c, scalerSizesTag, HAL_PIXEL_FORMAT_BLOB,
1495 &supportedBlobSizes);
1496 getSupportedSizes(c, depthSizesTag, HAL_PIXEL_FORMAT_Y16, &supportedDepthSizes);
1497 if (supportedBlobSizes.empty() || supportedDepthSizes.empty()) {
1498 // Nothing to do in this case.
1499 return OK;
1500 }
1501
1502 getSupportedDynamicDepthSizes(supportedBlobSizes, supportedDepthSizes,
1503 &supportedDynamicDepthSizes, &internalDepthSizes);
1504 if (supportedDynamicDepthSizes.empty()) {
1505 // Nothing more to do.
1506 return OK;
1507 }
1508
1509 std::vector<int32_t> dynamicDepthEntries;
1510 for (const auto& it : supportedDynamicDepthSizes) {
1511 int32_t entry[4] = {HAL_PIXEL_FORMAT_BLOB, static_cast<int32_t> (std::get<0>(it)),
1512 static_cast<int32_t> (std::get<1>(it)),
1513 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT };
1514 dynamicDepthEntries.insert(dynamicDepthEntries.end(), entry, entry + 4);
1515 }
1516
1517 std::vector<int64_t> depthMinDurations, depthStallDurations;
1518 std::vector<int64_t> blobMinDurations, blobStallDurations;
1519 std::vector<int64_t> dynamicDepthMinDurations, dynamicDepthStallDurations;
1520
1521 getSupportedDurations(c, depthMinFrameDurationsTag, HAL_PIXEL_FORMAT_Y16, internalDepthSizes,
1522 &depthMinDurations);
1523 getSupportedDurations(c, scalerMinFrameDurationsTag, HAL_PIXEL_FORMAT_BLOB,
1524 supportedDynamicDepthSizes, &blobMinDurations);
1525 if (blobMinDurations.empty() || depthMinDurations.empty() ||
1526 (depthMinDurations.size() != blobMinDurations.size())) {
1527 ALOGE("%s: Unexpected number of available depth min durations! %zu vs. %zu",
1528 __FUNCTION__, depthMinDurations.size(), blobMinDurations.size());
1529 return BAD_VALUE;
1530 }
1531
1532 getSupportedDurations(c, depthStallDurationsTag, HAL_PIXEL_FORMAT_Y16, internalDepthSizes,
1533 &depthStallDurations);
1534 getSupportedDurations(c, scalerStallDurationsTag, HAL_PIXEL_FORMAT_BLOB,
1535 supportedDynamicDepthSizes, &blobStallDurations);
1536 if (blobStallDurations.empty() || depthStallDurations.empty() ||
1537 (depthStallDurations.size() != blobStallDurations.size())) {
1538 ALOGE("%s: Unexpected number of available depth stall durations! %zu vs. %zu",
1539 __FUNCTION__, depthStallDurations.size(), blobStallDurations.size());
1540 return BAD_VALUE;
1541 }
1542
1543 getSupportedDynamicDepthDurations(depthMinDurations, blobMinDurations,
1544 &dynamicDepthMinDurations);
1545 getSupportedDynamicDepthDurations(depthStallDurations, blobStallDurations,
1546 &dynamicDepthStallDurations);
1547 if (dynamicDepthMinDurations.empty() || dynamicDepthStallDurations.empty() ||
1548 (dynamicDepthMinDurations.size() != dynamicDepthStallDurations.size())) {
1549 ALOGE("%s: Unexpected number of dynamic depth stall/min durations! %zu vs. %zu",
1550 __FUNCTION__, dynamicDepthMinDurations.size(), dynamicDepthStallDurations.size());
1551 return BAD_VALUE;
1552 }
1553
1554 std::vector<int64_t> dynamicDepthMinDurationEntries;
1555 auto itDuration = dynamicDepthMinDurations.begin();
1556 auto itSize = supportedDynamicDepthSizes.begin();
1557 while (itDuration != dynamicDepthMinDurations.end()) {
1558 int64_t entry[4] = {HAL_PIXEL_FORMAT_BLOB, static_cast<int32_t> (std::get<0>(*itSize)),
1559 static_cast<int32_t> (std::get<1>(*itSize)), *itDuration};
1560 dynamicDepthMinDurationEntries.insert(dynamicDepthMinDurationEntries.end(), entry,
1561 entry + 4);
1562 itDuration++; itSize++;
1563 }
1564
1565 std::vector<int64_t> dynamicDepthStallDurationEntries;
1566 itDuration = dynamicDepthStallDurations.begin();
1567 itSize = supportedDynamicDepthSizes.begin();
1568 while (itDuration != dynamicDepthStallDurations.end()) {
1569 int64_t entry[4] = {HAL_PIXEL_FORMAT_BLOB, static_cast<int32_t> (std::get<0>(*itSize)),
1570 static_cast<int32_t> (std::get<1>(*itSize)), *itDuration};
1571 dynamicDepthStallDurationEntries.insert(dynamicDepthStallDurationEntries.end(), entry,
1572 entry + 4);
1573 itDuration++; itSize++;
1574 }
1575
1576 std::vector<int32_t> supportedChTags;
1577 supportedChTags.reserve(chTags.count + 3);
1578 supportedChTags.insert(supportedChTags.end(), chTags.data.i32,
1579 chTags.data.i32 + chTags.count);
1580 supportedChTags.push_back(dynamicDepthSizesTag);
1581 supportedChTags.push_back(dynamicDepthMinFrameDurationsTag);
1582 supportedChTags.push_back(dynamicDepthStallDurationsTag);
1583 c.update(dynamicDepthSizesTag, dynamicDepthEntries.data(), dynamicDepthEntries.size());
1584 c.update(dynamicDepthMinFrameDurationsTag, dynamicDepthMinDurationEntries.data(),
1585 dynamicDepthMinDurationEntries.size());
1586 c.update(dynamicDepthStallDurationsTag, dynamicDepthStallDurationEntries.data(),
1587 dynamicDepthStallDurationEntries.size());
1588 c.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, supportedChTags.data(),
1589 supportedChTags.size());
1590
1591 return OK;
1592 }
1593
fixupTorchStrengthTags()1594 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::fixupTorchStrengthTags() {
1595 status_t res = OK;
1596 auto& c = mCameraCharacteristics;
1597 auto flashInfoStrengthDefaultLevelEntry = c.find(ANDROID_FLASH_INFO_STRENGTH_DEFAULT_LEVEL);
1598 if (flashInfoStrengthDefaultLevelEntry.count == 0) {
1599 int32_t flashInfoStrengthDefaultLevel = 1;
1600 res = c.update(ANDROID_FLASH_INFO_STRENGTH_DEFAULT_LEVEL,
1601 &flashInfoStrengthDefaultLevel, 1);
1602 if (res != OK) {
1603 ALOGE("%s: Failed to update ANDROID_FLASH_INFO_STRENGTH_DEFAULT_LEVEL: %s (%d)",
1604 __FUNCTION__,strerror(-res), res);
1605 return res;
1606 }
1607 }
1608 auto flashInfoStrengthMaximumLevelEntry = c.find(ANDROID_FLASH_INFO_STRENGTH_MAXIMUM_LEVEL);
1609 if (flashInfoStrengthMaximumLevelEntry.count == 0) {
1610 int32_t flashInfoStrengthMaximumLevel = 1;
1611 res = c.update(ANDROID_FLASH_INFO_STRENGTH_MAXIMUM_LEVEL,
1612 &flashInfoStrengthMaximumLevel, 1);
1613 if (res != OK) {
1614 ALOGE("%s: Failed to update ANDROID_FLASH_INFO_STRENGTH_MAXIMUM_LEVEL: %s (%d)",
1615 __FUNCTION__,strerror(-res), res);
1616 return res;
1617 }
1618 }
1619 return res;
1620 }
1621
1622
fixupManualFlashStrengthControlTags(CameraMetadata & ch)1623 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::fixupManualFlashStrengthControlTags(
1624 CameraMetadata& ch) {
1625 status_t res = OK;
1626 auto flashSingleStrengthMaxLevelEntry = ch.find(ANDROID_FLASH_SINGLE_STRENGTH_MAX_LEVEL);
1627 if (flashSingleStrengthMaxLevelEntry.count == 0) {
1628 int32_t flashSingleStrengthMaxLevel = 1;
1629 res = ch.update(ANDROID_FLASH_SINGLE_STRENGTH_MAX_LEVEL,
1630 &flashSingleStrengthMaxLevel, 1);
1631 if (res != OK) {
1632 ALOGE("%s: Failed to update ANDROID_FLASH_SINGLE_STRENGTH_MAX_LEVEL: %s (%d)",
1633 __FUNCTION__,strerror(-res), res);
1634 return res;
1635 }
1636 }
1637 auto flashSingleStrengthDefaultLevelEntry = ch.find(
1638 ANDROID_FLASH_SINGLE_STRENGTH_DEFAULT_LEVEL);
1639 if (flashSingleStrengthDefaultLevelEntry.count == 0) {
1640 int32_t flashSingleStrengthDefaultLevel = 1;
1641 res = ch.update(ANDROID_FLASH_SINGLE_STRENGTH_DEFAULT_LEVEL,
1642 &flashSingleStrengthDefaultLevel, 1);
1643 if (res != OK) {
1644 ALOGE("%s: Failed to update ANDROID_FLASH_SINGLE_STRENGTH_DEFAULT_LEVEL: %s (%d)",
1645 __FUNCTION__,strerror(-res), res);
1646 return res;
1647 }
1648 }
1649 auto flashTorchStrengthMaxLevelEntry = ch.find(ANDROID_FLASH_TORCH_STRENGTH_MAX_LEVEL);
1650 if (flashTorchStrengthMaxLevelEntry.count == 0) {
1651 int32_t flashTorchStrengthMaxLevel = 1;
1652 res = ch.update(ANDROID_FLASH_TORCH_STRENGTH_MAX_LEVEL,
1653 &flashTorchStrengthMaxLevel, 1);
1654 if (res != OK) {
1655 ALOGE("%s: Failed to update ANDROID_FLASH_TORCH_STRENGTH_MAX_LEVEL: %s (%d)",
1656 __FUNCTION__,strerror(-res), res);
1657 return res;
1658 }
1659 }
1660 auto flashTorchStrengthDefaultLevelEntry = ch.find(ANDROID_FLASH_TORCH_STRENGTH_DEFAULT_LEVEL);
1661 if (flashTorchStrengthDefaultLevelEntry.count == 0) {
1662 int32_t flashTorchStrengthDefaultLevel = 1;
1663 res = ch.update(ANDROID_FLASH_TORCH_STRENGTH_DEFAULT_LEVEL,
1664 &flashTorchStrengthDefaultLevel, 1);
1665 if (res != OK) {
1666 ALOGE("%s: Failed to update ANDROID_FLASH_TORCH_STRENGTH_DEFAULT_LEVEL: %s (%d)",
1667 __FUNCTION__,strerror(-res), res);
1668 return res;
1669 }
1670 }
1671 return res;
1672 }
1673
fixupMonochromeTags()1674 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::fixupMonochromeTags() {
1675 status_t res = OK;
1676 auto& c = mCameraCharacteristics;
1677 sp<ProviderInfo> parentProvider = mParentProvider.promote();
1678 if (parentProvider == nullptr) {
1679 return DEAD_OBJECT;
1680 }
1681 IPCTransport ipcTransport = parentProvider->getIPCTransport();
1682 // Override static metadata for MONOCHROME camera with older device version
1683 if (ipcTransport == IPCTransport::HIDL &&
1684 (mVersion.get_major() == 3 && mVersion.get_minor() < 5)) {
1685 camera_metadata_entry cap = c.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
1686 for (size_t i = 0; i < cap.count; i++) {
1687 if (cap.data.u8[i] == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_MONOCHROME) {
1688 // ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT
1689 uint8_t cfa = ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT_MONO;
1690 res = c.update(ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT, &cfa, 1);
1691 if (res != OK) {
1692 ALOGE("%s: Failed to update COLOR_FILTER_ARRANGEMENT: %s (%d)",
1693 __FUNCTION__, strerror(-res), res);
1694 return res;
1695 }
1696
1697 // ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS
1698 const std::vector<uint32_t> sKeys = {
1699 ANDROID_SENSOR_REFERENCE_ILLUMINANT1,
1700 ANDROID_SENSOR_REFERENCE_ILLUMINANT2,
1701 ANDROID_SENSOR_CALIBRATION_TRANSFORM1,
1702 ANDROID_SENSOR_CALIBRATION_TRANSFORM2,
1703 ANDROID_SENSOR_COLOR_TRANSFORM1,
1704 ANDROID_SENSOR_COLOR_TRANSFORM2,
1705 ANDROID_SENSOR_FORWARD_MATRIX1,
1706 ANDROID_SENSOR_FORWARD_MATRIX2,
1707 };
1708 res = removeAvailableKeys(c, sKeys,
1709 ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
1710 if (res != OK) {
1711 ALOGE("%s: Failed to update REQUEST_AVAILABLE_CHARACTERISTICS_KEYS: %s (%d)",
1712 __FUNCTION__, strerror(-res), res);
1713 return res;
1714 }
1715
1716 // ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS
1717 const std::vector<uint32_t> reqKeys = {
1718 ANDROID_COLOR_CORRECTION_MODE,
1719 ANDROID_COLOR_CORRECTION_TRANSFORM,
1720 ANDROID_COLOR_CORRECTION_GAINS,
1721 };
1722 res = removeAvailableKeys(c, reqKeys, ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS);
1723 if (res != OK) {
1724 ALOGE("%s: Failed to update REQUEST_AVAILABLE_REQUEST_KEYS: %s (%d)",
1725 __FUNCTION__, strerror(-res), res);
1726 return res;
1727 }
1728
1729 // ANDROID_REQUEST_AVAILABLE_RESULT_KEYS
1730 const std::vector<uint32_t> resKeys = {
1731 ANDROID_SENSOR_GREEN_SPLIT,
1732 ANDROID_SENSOR_NEUTRAL_COLOR_POINT,
1733 ANDROID_COLOR_CORRECTION_MODE,
1734 ANDROID_COLOR_CORRECTION_TRANSFORM,
1735 ANDROID_COLOR_CORRECTION_GAINS,
1736 };
1737 res = removeAvailableKeys(c, resKeys, ANDROID_REQUEST_AVAILABLE_RESULT_KEYS);
1738 if (res != OK) {
1739 ALOGE("%s: Failed to update REQUEST_AVAILABLE_RESULT_KEYS: %s (%d)",
1740 __FUNCTION__, strerror(-res), res);
1741 return res;
1742 }
1743
1744 // ANDROID_SENSOR_BLACK_LEVEL_PATTERN
1745 camera_metadata_entry blEntry = c.find(ANDROID_SENSOR_BLACK_LEVEL_PATTERN);
1746 for (size_t j = 1; j < blEntry.count; j++) {
1747 blEntry.data.i32[j] = blEntry.data.i32[0];
1748 }
1749 }
1750 }
1751 }
1752 return res;
1753 }
1754
addRotateCropTags()1755 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::addRotateCropTags() {
1756 status_t res = OK;
1757 auto& c = mCameraCharacteristics;
1758
1759 auto availableRotateCropEntry = c.find(ANDROID_SCALER_AVAILABLE_ROTATE_AND_CROP_MODES);
1760 if (availableRotateCropEntry.count == 0) {
1761 uint8_t defaultAvailableRotateCropEntry = ANDROID_SCALER_ROTATE_AND_CROP_NONE;
1762 res = c.update(ANDROID_SCALER_AVAILABLE_ROTATE_AND_CROP_MODES,
1763 &defaultAvailableRotateCropEntry, 1);
1764 }
1765 return res;
1766 }
1767
addAutoframingTags()1768 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::addAutoframingTags() {
1769 status_t res = OK;
1770 auto& c = mCameraCharacteristics;
1771
1772 auto availableAutoframingEntry = c.find(ANDROID_CONTROL_AUTOFRAMING_AVAILABLE);
1773 if (availableAutoframingEntry.count == 0) {
1774 uint8_t defaultAutoframingEntry = ANDROID_CONTROL_AUTOFRAMING_AVAILABLE_FALSE;
1775 res = c.update(ANDROID_CONTROL_AUTOFRAMING_AVAILABLE,
1776 &defaultAutoframingEntry, 1);
1777 }
1778 return res;
1779 }
1780
addPreCorrectionActiveArraySize()1781 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::addPreCorrectionActiveArraySize() {
1782 status_t res = OK;
1783 auto& c = mCameraCharacteristics;
1784
1785 auto activeArraySize = c.find(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE);
1786 auto preCorrectionActiveArraySize = c.find(
1787 ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE);
1788 if (activeArraySize.count == 4 && preCorrectionActiveArraySize.count == 0) {
1789 std::vector<int32_t> preCorrectionArray(
1790 activeArraySize.data.i32, activeArraySize.data.i32+4);
1791 res = c.update(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE,
1792 preCorrectionArray.data(), 4);
1793 if (res != OK) {
1794 ALOGE("%s: Failed to add ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE: %s(%d)",
1795 __FUNCTION__, strerror(-res), res);
1796 return res;
1797 }
1798 } else {
1799 return res;
1800 }
1801
1802 auto charTags = c.find(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
1803 bool hasPreCorrectionActiveArraySize = std::find(charTags.data.i32,
1804 charTags.data.i32 + charTags.count,
1805 ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE) !=
1806 (charTags.data.i32 + charTags.count);
1807 if (!hasPreCorrectionActiveArraySize) {
1808 std::vector<int32_t> supportedCharTags;
1809 supportedCharTags.reserve(charTags.count + 1);
1810 supportedCharTags.insert(supportedCharTags.end(), charTags.data.i32,
1811 charTags.data.i32 + charTags.count);
1812 supportedCharTags.push_back(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE);
1813
1814 res = c.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, supportedCharTags.data(),
1815 supportedCharTags.size());
1816 if (res != OK) {
1817 ALOGE("%s: Failed to update ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS: %s(%d)",
1818 __FUNCTION__, strerror(-res), res);
1819 return res;
1820 }
1821 }
1822
1823 return res;
1824 }
1825
addReadoutTimestampTag(bool readoutTimestampSupported)1826 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::addReadoutTimestampTag(
1827 bool readoutTimestampSupported) {
1828 status_t res = OK;
1829 auto& c = mCameraCharacteristics;
1830
1831 auto entry = c.find(ANDROID_SENSOR_READOUT_TIMESTAMP);
1832 if (entry.count == 0) {
1833 uint8_t defaultReadoutTimestamp = readoutTimestampSupported ?
1834 ANDROID_SENSOR_READOUT_TIMESTAMP_HARDWARE :
1835 ANDROID_SENSOR_READOUT_TIMESTAMP_NOT_SUPPORTED;
1836 res = c.update(ANDROID_SENSOR_READOUT_TIMESTAMP, &defaultReadoutTimestamp, 1);
1837 }
1838
1839 return res;
1840 }
1841
addSessionConfigQueryVersionTag()1842 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::addSessionConfigQueryVersionTag() {
1843 sp<ProviderInfo> parentProvider = mParentProvider.promote();
1844 if (parentProvider == nullptr) {
1845 return DEAD_OBJECT;
1846 }
1847
1848 int versionCode = ANDROID_INFO_SESSION_CONFIGURATION_QUERY_VERSION_UPSIDE_DOWN_CAKE;
1849 IPCTransport ipcTransport = parentProvider->getIPCTransport();
1850 int deviceVersion = HARDWARE_DEVICE_API_VERSION(mVersion.get_major(), mVersion.get_minor());
1851 if (ipcTransport == IPCTransport::AIDL
1852 && deviceVersion >= CAMERA_DEVICE_API_VERSION_1_3) {
1853 versionCode = ANDROID_INFO_SESSION_CONFIGURATION_QUERY_VERSION_VANILLA_ICE_CREAM;
1854 }
1855
1856 auto& c = mCameraCharacteristics;
1857 status_t res = c.update(ANDROID_INFO_SESSION_CONFIGURATION_QUERY_VERSION, &versionCode, 1);
1858 mSessionConfigQueryVersion = versionCode;
1859 return res;
1860 }
1861
removeAvailableKeys(CameraMetadata & c,const std::vector<uint32_t> & keys,uint32_t keyTag)1862 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::removeAvailableKeys(
1863 CameraMetadata& c, const std::vector<uint32_t>& keys, uint32_t keyTag) {
1864 status_t res = OK;
1865
1866 camera_metadata_entry keysEntry = c.find(keyTag);
1867 if (keysEntry.count == 0) {
1868 ALOGE("%s: Failed to find tag %u: %s (%d)", __FUNCTION__, keyTag, strerror(-res), res);
1869 return res;
1870 }
1871 std::vector<int32_t> vKeys;
1872 vKeys.reserve(keysEntry.count);
1873 for (size_t i = 0; i < keysEntry.count; i++) {
1874 if (std::find(keys.begin(), keys.end(), keysEntry.data.i32[i]) == keys.end()) {
1875 vKeys.push_back(keysEntry.data.i32[i]);
1876 }
1877 }
1878 res = c.update(keyTag, vKeys.data(), vKeys.size());
1879 return res;
1880 }
1881
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)1882 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::fillHeicStreamCombinations(
1883 std::vector<int32_t>* outputs,
1884 std::vector<int64_t>* durations,
1885 std::vector<int64_t>* stallDurations,
1886 const camera_metadata_entry& halStreamConfigs,
1887 const camera_metadata_entry& halStreamDurations) {
1888 if (outputs == nullptr || durations == nullptr || stallDurations == nullptr) {
1889 return BAD_VALUE;
1890 }
1891
1892 static bool supportInMemoryTempFile =
1893 camera3::HeicCompositeStream::isInMemoryTempFileSupported();
1894 if (!supportInMemoryTempFile) {
1895 ALOGI("%s: No HEIC support due to absence of in memory temp file support",
1896 __FUNCTION__);
1897 return OK;
1898 }
1899
1900 for (size_t i = 0; i < halStreamConfigs.count; i += 4) {
1901 int32_t format = halStreamConfigs.data.i32[i];
1902 // Only IMPLEMENTATION_DEFINED and YUV_888 can be used to generate HEIC
1903 // image.
1904 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
1905 format != HAL_PIXEL_FORMAT_YCBCR_420_888) {
1906 continue;
1907 }
1908
1909 bool sizeAvail = false;
1910 for (size_t j = 0; j < outputs->size(); j+= 4) {
1911 if ((*outputs)[j+1] == halStreamConfigs.data.i32[i+1] &&
1912 (*outputs)[j+2] == halStreamConfigs.data.i32[i+2]) {
1913 sizeAvail = true;
1914 break;
1915 }
1916 }
1917 if (sizeAvail) continue;
1918
1919 int64_t stall = 0;
1920 bool useHeic = false;
1921 bool useGrid = false;
1922 if (camera3::HeicCompositeStream::isSizeSupportedByHeifEncoder(
1923 halStreamConfigs.data.i32[i+1], halStreamConfigs.data.i32[i+2],
1924 &useHeic, &useGrid, &stall)) {
1925 if (useGrid != (format == HAL_PIXEL_FORMAT_YCBCR_420_888)) {
1926 continue;
1927 }
1928
1929 // HEIC configuration
1930 int32_t config[] = {HAL_PIXEL_FORMAT_BLOB, halStreamConfigs.data.i32[i+1],
1931 halStreamConfigs.data.i32[i+2], 0 /*isInput*/};
1932 outputs->insert(outputs->end(), config, config + 4);
1933
1934 // HEIC minFrameDuration
1935 for (size_t j = 0; j < halStreamDurations.count; j += 4) {
1936 if (halStreamDurations.data.i64[j] == format &&
1937 halStreamDurations.data.i64[j+1] == halStreamConfigs.data.i32[i+1] &&
1938 halStreamDurations.data.i64[j+2] == halStreamConfigs.data.i32[i+2]) {
1939 int64_t duration[] = {HAL_PIXEL_FORMAT_BLOB, halStreamConfigs.data.i32[i+1],
1940 halStreamConfigs.data.i32[i+2], halStreamDurations.data.i64[j+3]};
1941 durations->insert(durations->end(), duration, duration+4);
1942 break;
1943 }
1944 }
1945
1946 // HEIC stallDuration
1947 int64_t stallDuration[] = {HAL_PIXEL_FORMAT_BLOB, halStreamConfigs.data.i32[i+1],
1948 halStreamConfigs.data.i32[i+2], stall};
1949 stallDurations->insert(stallDurations->end(), stallDuration, stallDuration+4);
1950 }
1951 }
1952 return OK;
1953 }
1954
deriveHeicTags(bool maxResolution)1955 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::deriveHeicTags(bool maxResolution) {
1956 int32_t scalerStreamSizesTag =
1957 SessionConfigurationUtils::getAppropriateModeTag(
1958 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, maxResolution);
1959 int32_t scalerMinFrameDurationsTag =
1960 SessionConfigurationUtils::getAppropriateModeTag(
1961 ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS, maxResolution);
1962
1963 int32_t heicStreamSizesTag =
1964 SessionConfigurationUtils::getAppropriateModeTag(
1965 ANDROID_HEIC_AVAILABLE_HEIC_STREAM_CONFIGURATIONS, maxResolution);
1966 int32_t heicMinFrameDurationsTag =
1967 SessionConfigurationUtils::getAppropriateModeTag(
1968 ANDROID_HEIC_AVAILABLE_HEIC_MIN_FRAME_DURATIONS, maxResolution);
1969 int32_t heicStallDurationsTag =
1970 SessionConfigurationUtils::getAppropriateModeTag(
1971 ANDROID_HEIC_AVAILABLE_HEIC_STALL_DURATIONS, maxResolution);
1972
1973 auto& c = mCameraCharacteristics;
1974
1975 camera_metadata_entry halHeicSupport = c.find(ANDROID_HEIC_INFO_SUPPORTED);
1976 if (halHeicSupport.count > 1) {
1977 ALOGE("%s: Invalid entry count %zu for ANDROID_HEIC_INFO_SUPPORTED",
1978 __FUNCTION__, halHeicSupport.count);
1979 return BAD_VALUE;
1980 } else if (halHeicSupport.count == 0 ||
1981 halHeicSupport.data.u8[0] == ANDROID_HEIC_INFO_SUPPORTED_FALSE) {
1982 // Camera HAL doesn't support mandatory stream combinations for HEIC.
1983 return OK;
1984 }
1985
1986 camera_metadata_entry maxJpegAppsSegments =
1987 c.find(ANDROID_HEIC_INFO_MAX_JPEG_APP_SEGMENTS_COUNT);
1988 if (maxJpegAppsSegments.count != 1 || maxJpegAppsSegments.data.u8[0] == 0 ||
1989 maxJpegAppsSegments.data.u8[0] > 16) {
1990 ALOGE("%s: ANDROID_HEIC_INFO_MAX_JPEG_APP_SEGMENTS_COUNT must be within [1, 16]",
1991 __FUNCTION__);
1992 return BAD_VALUE;
1993 }
1994
1995 // Populate HEIC output configurations and its related min frame duration
1996 // and stall duration.
1997 std::vector<int32_t> heicOutputs;
1998 std::vector<int64_t> heicDurations;
1999 std::vector<int64_t> heicStallDurations;
2000
2001 camera_metadata_entry halStreamConfigs = c.find(scalerStreamSizesTag);
2002 camera_metadata_entry minFrameDurations = c.find(scalerMinFrameDurationsTag);
2003
2004 status_t res = fillHeicStreamCombinations(&heicOutputs, &heicDurations, &heicStallDurations,
2005 halStreamConfigs, minFrameDurations);
2006 if (res != OK) {
2007 ALOGE("%s: Failed to fill HEIC stream combinations: %s (%d)", __FUNCTION__,
2008 strerror(-res), res);
2009 return res;
2010 }
2011
2012 c.update(heicStreamSizesTag, heicOutputs.data(), heicOutputs.size());
2013 c.update(heicMinFrameDurationsTag, heicDurations.data(), heicDurations.size());
2014 c.update(heicStallDurationsTag, heicStallDurations.data(), heicStallDurations.size());
2015
2016 return OK;
2017 }
2018
isLogicalCameraLocked(const std::string & id,std::vector<std::string> * physicalCameraIds)2019 bool CameraProviderManager::isLogicalCameraLocked(const std::string& id,
2020 std::vector<std::string>* physicalCameraIds) {
2021 auto deviceInfo = findDeviceInfoLocked(id);
2022 if (deviceInfo == nullptr) return false;
2023
2024 if (deviceInfo->mIsLogicalCamera && physicalCameraIds != nullptr) {
2025 *physicalCameraIds = deviceInfo->mPhysicalIds;
2026 }
2027 return deviceInfo->mIsLogicalCamera;
2028 }
2029
isLogicalCamera(const std::string & id,std::vector<std::string> * physicalCameraIds)2030 bool CameraProviderManager::isLogicalCamera(const std::string& id,
2031 std::vector<std::string>* physicalCameraIds) {
2032 std::lock_guard<std::mutex> lock(mInterfaceMutex);
2033 return isLogicalCameraLocked(id, physicalCameraIds);
2034 }
2035
getSystemCameraKind(const std::string & id,SystemCameraKind * kind) const2036 status_t CameraProviderManager::getSystemCameraKind(const std::string& id,
2037 SystemCameraKind *kind) const {
2038 std::lock_guard<std::mutex> lock(mInterfaceMutex);
2039 return getSystemCameraKindLocked(id, kind);
2040 }
2041
getSystemCameraKindLocked(const std::string & id,SystemCameraKind * kind) const2042 status_t CameraProviderManager::getSystemCameraKindLocked(const std::string& id,
2043 SystemCameraKind *kind) const {
2044 auto deviceInfo = findDeviceInfoLocked(id);
2045 if (deviceInfo != nullptr) {
2046 *kind = deviceInfo->mSystemCameraKind;
2047 return OK;
2048 }
2049 // If this is a hidden physical camera, we should return what kind of
2050 // camera the enclosing logical camera is.
2051 auto isHiddenAndParent = isHiddenPhysicalCameraInternal(id);
2052 if (isHiddenAndParent.first) {
2053 LOG_ALWAYS_FATAL_IF(id == isHiddenAndParent.second->mId,
2054 "%s: hidden physical camera id %s and enclosing logical camera id %s are the same",
2055 __FUNCTION__, id.c_str(), isHiddenAndParent.second->mId.c_str());
2056 return getSystemCameraKindLocked(isHiddenAndParent.second->mId, kind);
2057 }
2058 // Neither a hidden physical camera nor a logical camera
2059 return NAME_NOT_FOUND;
2060 }
2061
isHiddenPhysicalCamera(const std::string & cameraId) const2062 bool CameraProviderManager::isHiddenPhysicalCamera(const std::string& cameraId) const {
2063 std::lock_guard<std::mutex> lock(mInterfaceMutex);
2064 return isHiddenPhysicalCameraInternal(cameraId).first;
2065 }
2066
filterSmallJpegSizes(const std::string & cameraId)2067 status_t CameraProviderManager::filterSmallJpegSizes(const std::string& cameraId) {
2068 std::lock_guard<std::mutex> lock(mInterfaceMutex);
2069 for (auto& provider : mProviders) {
2070 for (auto& deviceInfo : provider->mDevices) {
2071 if (deviceInfo->mId == cameraId) {
2072 return deviceInfo->filterSmallJpegSizes();
2073 }
2074 }
2075 }
2076 return NAME_NOT_FOUND;
2077 }
2078
2079 std::pair<bool, CameraProviderManager::ProviderInfo::DeviceInfo *>
isHiddenPhysicalCameraInternal(const std::string & cameraId) const2080 CameraProviderManager::isHiddenPhysicalCameraInternal(const std::string& cameraId) const {
2081 auto falseRet = std::make_pair(false, nullptr);
2082 for (auto& provider : mProviders) {
2083 for (auto& deviceInfo : provider->mDevices) {
2084 if (deviceInfo->mId == cameraId) {
2085 // cameraId is found in public camera IDs advertised by the
2086 // provider.
2087 return falseRet;
2088 }
2089 }
2090 }
2091
2092 for (auto& provider : mProviders) {
2093 IPCTransport transport = provider->getIPCTransport();
2094 for (auto& deviceInfo : provider->mDevices) {
2095 std::vector<std::string> physicalIds;
2096 if (deviceInfo->mIsLogicalCamera) {
2097 if (std::find(deviceInfo->mPhysicalIds.begin(), deviceInfo->mPhysicalIds.end(),
2098 cameraId) != deviceInfo->mPhysicalIds.end()) {
2099 int deviceVersion = HARDWARE_DEVICE_API_VERSION(
2100 deviceInfo->mVersion.get_major(), deviceInfo->mVersion.get_minor());
2101 if (transport == IPCTransport::HIDL &&
2102 deviceVersion < CAMERA_DEVICE_API_VERSION_3_5) {
2103 ALOGE("%s: Wrong deviceVersion %x for hiddenPhysicalCameraId %s",
2104 __FUNCTION__, deviceVersion, cameraId.c_str());
2105 return falseRet;
2106 } else {
2107 return std::make_pair(true, deviceInfo.get());
2108 }
2109 }
2110 }
2111 }
2112 }
2113
2114 return falseRet;
2115 }
2116
tryToInitializeAidlProviderLocked(const std::string & providerName,const sp<ProviderInfo> & providerInfo)2117 status_t CameraProviderManager::tryToInitializeAidlProviderLocked(
2118 const std::string& providerName, const sp<ProviderInfo>& providerInfo) {
2119 using aidl::android::hardware::camera::provider::ICameraProvider;
2120
2121 std::shared_ptr<ICameraProvider> interface;
2122 if (flags::delay_lazy_hal_instantiation()) {
2123 // Only get remote instance if already running. Lazy Providers will be
2124 // woken up later.
2125 interface = mAidlServiceProxy->tryGetService(providerName);
2126 } else {
2127 interface = mAidlServiceProxy->getService(providerName);
2128 }
2129
2130 if (interface == nullptr) {
2131 ALOGW("%s: AIDL Camera provider HAL '%s' is not actually available", __FUNCTION__,
2132 providerName.c_str());
2133 return BAD_VALUE;
2134 }
2135
2136 AidlProviderInfo *aidlProviderInfo = static_cast<AidlProviderInfo *>(providerInfo.get());
2137 return aidlProviderInfo->initializeAidlProvider(interface, mDeviceState);
2138 }
2139
tryToInitializeHidlProviderLocked(const std::string & providerName,const sp<ProviderInfo> & providerInfo)2140 status_t CameraProviderManager::tryToInitializeHidlProviderLocked(
2141 const std::string& providerName, const sp<ProviderInfo>& providerInfo) {
2142 sp<provider::V2_4::ICameraProvider> interface;
2143 interface = mHidlServiceProxy->tryGetService(providerName);
2144
2145 if (interface == nullptr) {
2146 // The interface may not be started yet. In that case, this is not a
2147 // fatal error.
2148 ALOGW("%s: HIDL Camera provider HAL '%s' is not actually available", __FUNCTION__,
2149 providerName.c_str());
2150 return BAD_VALUE;
2151 }
2152
2153 HidlProviderInfo *hidlProviderInfo = static_cast<HidlProviderInfo *>(providerInfo.get());
2154 return hidlProviderInfo->initializeHidlProvider(interface, mDeviceState);
2155 }
2156
addAidlProviderLocked(const std::string & newProvider)2157 status_t CameraProviderManager::addAidlProviderLocked(const std::string& newProvider) {
2158 // Several camera provider instances can be temporarily present.
2159 // Defer initialization of a new instance until the older instance is properly removed.
2160 auto providerInstance = newProvider + "-" + std::to_string(mProviderInstanceId);
2161 bool providerPresent = false;
2162 bool preexisting =
2163 (mAidlProviderWithBinders.find(newProvider) != mAidlProviderWithBinders.end());
2164 using aidl::android::hardware::camera::provider::ICameraProvider;
2165 std::string providerNameUsed =
2166 newProvider.substr(std::string(ICameraProvider::descriptor).size() + 1);
2167 if (flags::lazy_aidl_wait_for_service()) {
2168 // 'newProvider' has the fully qualified name of the provider service in case of AIDL.
2169 // ProviderInfo::mProviderName also has the fully qualified name - so we just compare them
2170 // here.
2171 providerNameUsed = newProvider;
2172 }
2173
2174 for (const auto& providerInfo : mProviders) {
2175 if (providerInfo->mProviderName == providerNameUsed) {
2176 ALOGW("%s: Camera provider HAL with name '%s' already registered",
2177 __FUNCTION__, newProvider.c_str());
2178 // Do not add new instances for lazy HAL external provider or aidl
2179 // binders previously seen.
2180 if (preexisting || providerInfo->isExternalLazyHAL()) {
2181 return ALREADY_EXISTS;
2182 } else {
2183 ALOGW("%s: The new provider instance will get initialized immediately after the"
2184 " currently present instance is removed!", __FUNCTION__);
2185 providerPresent = true;
2186 break;
2187 }
2188 }
2189 }
2190
2191 sp<AidlProviderInfo> providerInfo =
2192 new AidlProviderInfo(providerNameUsed, providerInstance, this);
2193
2194 if (!providerPresent) {
2195 status_t res = tryToInitializeAidlProviderLocked(newProvider, providerInfo);
2196 if (res != OK) {
2197 return res;
2198 }
2199 mAidlProviderWithBinders.emplace(newProvider);
2200 }
2201
2202 mProviders.push_back(providerInfo);
2203 mProviderInstanceId++;
2204
2205 return OK;
2206 }
2207
addHidlProviderLocked(const std::string & newProvider,bool preexisting)2208 status_t CameraProviderManager::addHidlProviderLocked(const std::string& newProvider,
2209 bool preexisting) {
2210 // Several camera provider instances can be temporarily present.
2211 // Defer initialization of a new instance until the older instance is properly removed.
2212 auto providerInstance = newProvider + "-" + std::to_string(mProviderInstanceId);
2213 bool providerPresent = false;
2214 for (const auto& providerInfo : mProviders) {
2215 if (providerInfo->mProviderName == newProvider) {
2216 ALOGW("%s: Camera provider HAL with name '%s' already registered",
2217 __FUNCTION__, newProvider.c_str());
2218 // Do not add new instances for lazy HAL external provider
2219 if (preexisting || providerInfo->isExternalLazyHAL()) {
2220 return ALREADY_EXISTS;
2221 } else {
2222 ALOGW("%s: The new provider instance will get initialized immediately after the"
2223 " currently present instance is removed!", __FUNCTION__);
2224 providerPresent = true;
2225 break;
2226 }
2227 }
2228 }
2229
2230 sp<HidlProviderInfo> providerInfo = new HidlProviderInfo(newProvider, providerInstance, this);
2231 if (!providerPresent) {
2232 status_t res = tryToInitializeHidlProviderLocked(newProvider, providerInfo);
2233 if (res != OK) {
2234 return res;
2235 }
2236 }
2237
2238 mProviders.push_back(providerInfo);
2239 mProviderInstanceId++;
2240
2241 return OK;
2242 }
2243
removeProvider(const std::string & provider)2244 status_t CameraProviderManager::removeProvider(const std::string& provider) {
2245 std::lock_guard<std::mutex> providerLock(mProviderLifecycleLock);
2246 std::unique_lock<std::mutex> lock(mInterfaceMutex);
2247 std::vector<std::string> removedDeviceIds;
2248 status_t res = NAME_NOT_FOUND;
2249 std::string removedProviderName;
2250 for (auto it = mProviders.begin(); it != mProviders.end(); it++) {
2251 if ((*it)->mProviderInstance == provider) {
2252 removedDeviceIds.reserve((*it)->mDevices.size());
2253 for (auto& deviceInfo : (*it)->mDevices) {
2254 removedDeviceIds.push_back(deviceInfo->mId);
2255 }
2256 removedProviderName = (*it)->mProviderName;
2257 mProviders.erase(it);
2258 res = OK;
2259 break;
2260 }
2261 }
2262 if (res != OK) {
2263 ALOGW("%s: Camera provider HAL with name '%s' is not registered", __FUNCTION__,
2264 provider.c_str());
2265 } else {
2266 // Check if there are any newer camera instances from the same provider and try to
2267 // initialize.
2268 for (const auto& providerInfo : mProviders) {
2269 if (providerInfo->mProviderName == removedProviderName) {
2270 IPCTransport providerTransport = providerInfo->getIPCTransport();
2271 std::string removedAidlProviderName = getFullAidlProviderName(removedProviderName);
2272 if (flags::lazy_aidl_wait_for_service()) {
2273 removedAidlProviderName = removedProviderName;
2274 }
2275 switch(providerTransport) {
2276 case IPCTransport::HIDL:
2277 return tryToInitializeHidlProviderLocked(removedProviderName, providerInfo);
2278 case IPCTransport::AIDL:
2279 return tryToInitializeAidlProviderLocked(removedAidlProviderName,
2280 providerInfo);
2281 default:
2282 ALOGE("%s Unsupported Transport %d", __FUNCTION__, providerTransport);
2283 }
2284 }
2285 }
2286
2287 // Inform camera service of loss of presence for all the devices from this provider,
2288 // without lock held for reentrancy
2289 sp<StatusListener> listener = getStatusListener();
2290 if (listener != nullptr) {
2291 lock.unlock();
2292 for (auto& id : removedDeviceIds) {
2293 listener->onDeviceStatusChanged(id, CameraDeviceStatus::NOT_PRESENT);
2294 }
2295 lock.lock();
2296 }
2297
2298 }
2299 return res;
2300 }
2301
getStatusListener() const2302 sp<CameraProviderManager::StatusListener> CameraProviderManager::getStatusListener() const {
2303 return mListener.promote();
2304 }
2305 /**** Methods for ProviderInfo ****/
2306
2307
ProviderInfo(const std::string & providerName,const std::string & providerInstance,CameraProviderManager * manager)2308 CameraProviderManager::ProviderInfo::ProviderInfo(
2309 const std::string &providerName,
2310 const std::string &providerInstance,
2311 [[maybe_unused]] CameraProviderManager *manager) :
2312 mProviderName(providerName),
2313 mProviderInstance(providerInstance),
2314 mProviderTagid(generateVendorTagId(providerName)),
2315 mUniqueDeviceCount(0),
2316 mManager(manager) {
2317 }
2318
getType() const2319 const std::string& CameraProviderManager::ProviderInfo::getType() const {
2320 return mType;
2321 }
2322
addDevice(const std::string & name,CameraDeviceStatus initialStatus,std::string * parsedId)2323 status_t CameraProviderManager::ProviderInfo::addDevice(
2324 const std::string& name, CameraDeviceStatus initialStatus,
2325 /*out*/ std::string* parsedId) {
2326
2327 ALOGI("Enumerating new camera device: %s", name.c_str());
2328
2329 uint16_t major, minor;
2330 std::string type, id;
2331 IPCTransport transport = getIPCTransport();
2332
2333 status_t res = parseDeviceName(name, &major, &minor, &type, &id);
2334 if (res != OK) {
2335 return res;
2336 }
2337
2338 if (type != mType) {
2339 ALOGE("%s: Device type %s does not match provider type %s", __FUNCTION__,
2340 type.c_str(), mType.c_str());
2341 return BAD_VALUE;
2342 }
2343 if (mManager->isValidDeviceLocked(id, major, transport)) {
2344 ALOGE("%s: Device %s: ID %s is already in use for device major version %d", __FUNCTION__,
2345 name.c_str(), id.c_str(), major);
2346 return BAD_VALUE;
2347 }
2348
2349 std::unique_ptr<DeviceInfo> deviceInfo;
2350 switch (transport) {
2351 case IPCTransport::HIDL:
2352 switch (major) {
2353 case 3:
2354 break;
2355 default:
2356 ALOGE("%s: Device %s: Unsupported HIDL device HAL major version %d:",
2357 __FUNCTION__, name.c_str(), major);
2358 return BAD_VALUE;
2359 }
2360 break;
2361 case IPCTransport::AIDL:
2362 if (major != 1) {
2363 ALOGE("%s: Device %s: Unsupported AIDL device HAL major version %d:", __FUNCTION__,
2364 name.c_str(), major);
2365 return BAD_VALUE;
2366 }
2367 break;
2368 default:
2369 ALOGE("%s Invalid transport %d", __FUNCTION__, transport);
2370 return BAD_VALUE;
2371 }
2372
2373 deviceInfo = initializeDeviceInfo(name, mProviderTagid, id, minor);
2374 if (deviceInfo == nullptr) return BAD_VALUE;
2375 deviceInfo->notifyDeviceStateChange(getDeviceState());
2376 deviceInfo->mStatus = initialStatus;
2377 bool isAPI1Compatible = deviceInfo->isAPI1Compatible();
2378
2379 mDevices.push_back(std::move(deviceInfo));
2380
2381 mUniqueCameraIds.insert(id);
2382 if (isAPI1Compatible) {
2383 // addDevice can be called more than once for the same camera id if HAL
2384 // supports openLegacy.
2385 if (std::find(mUniqueAPI1CompatibleCameraIds.begin(), mUniqueAPI1CompatibleCameraIds.end(),
2386 id) == mUniqueAPI1CompatibleCameraIds.end()) {
2387 mUniqueAPI1CompatibleCameraIds.push_back(id);
2388 }
2389 }
2390
2391 if (parsedId != nullptr) {
2392 *parsedId = id;
2393 }
2394 return OK;
2395 }
2396
removeDevice(const std::string & id)2397 void CameraProviderManager::ProviderInfo::removeDevice(const std::string &id) {
2398 for (auto it = mDevices.begin(); it != mDevices.end(); it++) {
2399 if ((*it)->mId == id) {
2400 mUniqueCameraIds.erase(id);
2401 mUnavailablePhysicalCameras.erase(id);
2402 if ((*it)->isAPI1Compatible()) {
2403 mUniqueAPI1CompatibleCameraIds.erase(std::remove(
2404 mUniqueAPI1CompatibleCameraIds.begin(),
2405 mUniqueAPI1CompatibleCameraIds.end(), id));
2406 }
2407
2408 // Remove reference to camera provider to avoid pointer leak when
2409 // unplugging external camera while in use with lazy HALs
2410 mManager->removeRef(DeviceMode::CAMERA, id);
2411 mManager->removeRef(DeviceMode::TORCH, id);
2412
2413 mDevices.erase(it);
2414 break;
2415 }
2416 }
2417 }
2418
removeAllDevices()2419 void CameraProviderManager::ProviderInfo::removeAllDevices() {
2420 std::lock_guard<std::mutex> lock(mLock);
2421
2422 auto itDevices = mDevices.begin();
2423 while (itDevices != mDevices.end()) {
2424 std::string id = (*itDevices)->mId;
2425 std::string deviceName = (*itDevices)->mName;
2426 removeDevice(id);
2427 // device was removed, reset iterator
2428 itDevices = mDevices.begin();
2429
2430 //notify CameraService of status change
2431 sp<StatusListener> listener = mManager->getStatusListener();
2432 if (listener != nullptr) {
2433 mLock.unlock();
2434 ALOGV("%s: notify device not_present: %s",
2435 __FUNCTION__,
2436 deviceName.c_str());
2437 listener->onDeviceStatusChanged(id, CameraDeviceStatus::NOT_PRESENT);
2438 mLock.lock();
2439 }
2440 }
2441 }
2442
isExternalLazyHAL() const2443 bool CameraProviderManager::ProviderInfo::isExternalLazyHAL() const {
2444 std::string providerName = mProviderName;
2445 if (flags::lazy_aidl_wait_for_service() && getIPCTransport() == IPCTransport::AIDL) {
2446 using aidl::android::hardware::camera::provider::ICameraProvider;
2447 providerName =
2448 mProviderName.substr(std::string(ICameraProvider::descriptor).size() + 1);
2449 }
2450 return kEnableLazyHal && (providerName == kExternalProviderName);
2451 }
2452
dump(int fd,const Vector<String16> &) const2453 status_t CameraProviderManager::ProviderInfo::dump(int fd, const Vector<String16>&) const {
2454 dprintf(fd, "== Camera Provider HAL %s (v2.%d, %s) static info: %zu devices: ==\n",
2455 mProviderInstance.c_str(),
2456 mMinorVersion,
2457 mIsRemote ? "remote" : "passthrough",
2458 mDevices.size());
2459
2460 for (auto& device : mDevices) {
2461 dprintf(fd, "== Camera HAL device %s (v%d.%d) static information: ==\n", device->mName.c_str(),
2462 device->mVersion.get_major(), device->mVersion.get_minor());
2463 dprintf(fd, " Resource cost: %d\n", device->mResourceCost.resourceCost);
2464 if (device->mResourceCost.conflictingDevices.size() == 0) {
2465 dprintf(fd, " Conflicting devices: None\n");
2466 } else {
2467 dprintf(fd, " Conflicting devices:\n");
2468 for (size_t i = 0; i < device->mResourceCost.conflictingDevices.size(); i++) {
2469 dprintf(fd, " %s\n",
2470 device->mResourceCost.conflictingDevices[i].c_str());
2471 }
2472 }
2473 dprintf(fd, " API1 info:\n");
2474 dprintf(fd, " Has a flash unit: %s\n",
2475 device->hasFlashUnit() ? "true" : "false");
2476 hardware::CameraInfo info;
2477 int portraitRotation;
2478 status_t res = device->getCameraInfo(
2479 /*rotationOverride*/hardware::ICameraService::ROTATION_OVERRIDE_NONE,
2480 &portraitRotation, &info);
2481 if (res != OK) {
2482 dprintf(fd, " <Error reading camera info: %s (%d)>\n",
2483 strerror(-res), res);
2484 } else {
2485 dprintf(fd, " Facing: %s\n",
2486 info.facing == hardware::CAMERA_FACING_BACK ? "Back" : "Front");
2487 dprintf(fd, " Orientation: %d\n", info.orientation);
2488 }
2489 CameraMetadata info2;
2490 res = device->getCameraCharacteristics(true /*overrideForPerfClass*/, &info2,
2491 hardware::ICameraService::ROTATION_OVERRIDE_NONE);
2492 if (res == INVALID_OPERATION) {
2493 dprintf(fd, " API2 not directly supported\n");
2494 } else if (res != OK) {
2495 dprintf(fd, " <Error reading camera characteristics: %s (%d)>\n",
2496 strerror(-res), res);
2497 } else {
2498 dprintf(fd, " API2 camera characteristics:\n");
2499 info2.dump(fd, /*verbosity*/ 2, /*indentation*/ 4);
2500 }
2501
2502 // Dump characteristics of non-standalone physical camera
2503 if (device->mIsLogicalCamera) {
2504 for (auto& id : device->mPhysicalIds) {
2505 // Skip if physical id is an independent camera
2506 if (std::find(mProviderPublicCameraIds.begin(), mProviderPublicCameraIds.end(), id)
2507 != mProviderPublicCameraIds.end()) {
2508 continue;
2509 }
2510
2511 CameraMetadata physicalInfo;
2512 status_t status = device->getPhysicalCameraCharacteristics(id, &physicalInfo);
2513 if (status == OK) {
2514 dprintf(fd, " Physical camera %s characteristics:\n", id.c_str());
2515 physicalInfo.dump(fd, /*verbosity*/ 2, /*indentation*/ 4);
2516 }
2517 }
2518 }
2519
2520 dprintf(fd, "== Camera HAL device %s (v%d.%d) dumpState: ==\n", device->mName.c_str(),
2521 device->mVersion.get_major(), device->mVersion.get_minor());
2522 res = device->dumpState(fd);
2523 if (res != OK) {
2524 dprintf(fd, " <Error dumping device %s state: %s (%d)>\n",
2525 device->mName.c_str(), strerror(-res), res);
2526 }
2527 }
2528 return OK;
2529 }
2530
2531 std::vector<std::unordered_set<std::string>>
getConcurrentCameraIdCombinations()2532 CameraProviderManager::ProviderInfo::getConcurrentCameraIdCombinations() {
2533 std::lock_guard<std::mutex> lock(mLock);
2534 return mConcurrentCameraIdCombinations;
2535 }
2536
cameraDeviceStatusChangeInternal(const std::string & cameraDeviceName,CameraDeviceStatus newStatus)2537 void CameraProviderManager::ProviderInfo::cameraDeviceStatusChangeInternal(
2538 const std::string& cameraDeviceName, CameraDeviceStatus newStatus) {
2539 sp<StatusListener> listener;
2540 std::string id;
2541 std::lock_guard<std::mutex> lock(mInitLock);
2542 CameraDeviceStatus internalNewStatus = newStatus;
2543 if (!mInitialized) {
2544 mCachedStatus.emplace_back(false /*isPhysicalCameraStatus*/,
2545 cameraDeviceName, std::string(), internalNewStatus);
2546 return;
2547 }
2548
2549 {
2550 std::lock_guard<std::mutex> lock(mLock);
2551 if (OK != cameraDeviceStatusChangeLocked(&id, cameraDeviceName, newStatus)) {
2552 return;
2553 }
2554 listener = mManager->getStatusListener();
2555 }
2556
2557 // Call without lock held to allow reentrancy into provider manager
2558 if (listener != nullptr) {
2559 listener->onDeviceStatusChanged(id, internalNewStatus);
2560 }
2561 }
2562
cameraDeviceStatusChangeLocked(std::string * id,const std::string & cameraDeviceName,CameraDeviceStatus newStatus)2563 status_t CameraProviderManager::ProviderInfo::cameraDeviceStatusChangeLocked(
2564 std::string* id, const std::string& cameraDeviceName,
2565 CameraDeviceStatus newStatus) {
2566 bool known = false;
2567 std::string cameraId;
2568 for (auto& deviceInfo : mDevices) {
2569 if (deviceInfo->mName == cameraDeviceName) {
2570 Mutex::Autolock l(deviceInfo->mDeviceAvailableLock);
2571 ALOGI("Camera device %s status is now %s, was %s", cameraDeviceName.c_str(),
2572 FrameworkDeviceStatusToString(newStatus),
2573 FrameworkDeviceStatusToString(deviceInfo->mStatus));
2574 deviceInfo->mStatus = newStatus;
2575 // TODO: Handle device removal (NOT_PRESENT)
2576 cameraId = deviceInfo->mId;
2577 known = true;
2578 deviceInfo->mIsDeviceAvailable =
2579 (newStatus == CameraDeviceStatus::PRESENT);
2580 deviceInfo->mDeviceAvailableSignal.signal();
2581 break;
2582 }
2583 }
2584 // Previously unseen device; status must not be NOT_PRESENT
2585 if (!known) {
2586 if (newStatus == CameraDeviceStatus::NOT_PRESENT) {
2587 ALOGW("Camera provider %s says an unknown camera device %s is not present. Curious.",
2588 mProviderName.c_str(), cameraDeviceName.c_str());
2589 return BAD_VALUE;
2590 }
2591 addDevice(cameraDeviceName, newStatus, &cameraId);
2592 } else if (newStatus == CameraDeviceStatus::NOT_PRESENT) {
2593 removeDevice(cameraId);
2594 } else if (isExternalLazyHAL()) {
2595 // Do not notify CameraService for PRESENT->PRESENT (lazy HAL restart)
2596 // because NOT_AVAILABLE is set on CameraService::connect and a PRESENT
2597 // notif. would overwrite it
2598 return BAD_VALUE;
2599 }
2600
2601 if (reCacheConcurrentStreamingCameraIdsLocked() != OK) {
2602 ALOGE("%s: CameraProvider %s could not re-cache concurrent streaming camera id list ",
2603 __FUNCTION__, mProviderName.c_str());
2604 }
2605 *id = cameraId;
2606 return OK;
2607 }
2608
physicalCameraDeviceStatusChangeInternal(const std::string & cameraDeviceName,const std::string & physicalCameraDeviceName,CameraDeviceStatus newStatus)2609 void CameraProviderManager::ProviderInfo::physicalCameraDeviceStatusChangeInternal(
2610 const std::string& cameraDeviceName,
2611 const std::string& physicalCameraDeviceName,
2612 CameraDeviceStatus newStatus) {
2613 sp<StatusListener> listener;
2614 std::string id;
2615 std::string physicalId;
2616 std::lock_guard<std::mutex> lock(mInitLock);
2617 if (!mInitialized) {
2618 mCachedStatus.emplace_back(true /*isPhysicalCameraStatus*/, cameraDeviceName,
2619 physicalCameraDeviceName, newStatus);
2620 return;
2621 }
2622
2623 {
2624 std::lock_guard<std::mutex> lock(mLock);
2625
2626 if (OK != physicalCameraDeviceStatusChangeLocked(&id, &physicalId, cameraDeviceName,
2627 physicalCameraDeviceName, newStatus)) {
2628 return;
2629 }
2630
2631 listener = mManager->getStatusListener();
2632 }
2633 // Call without lock held to allow reentrancy into provider manager
2634 if (listener != nullptr) {
2635 listener->onDeviceStatusChanged(id, physicalId, newStatus);
2636 }
2637 return;
2638 }
2639
physicalCameraDeviceStatusChangeLocked(std::string * id,std::string * physicalId,const std::string & cameraDeviceName,const std::string & physicalCameraDeviceName,CameraDeviceStatus newStatus)2640 status_t CameraProviderManager::ProviderInfo::physicalCameraDeviceStatusChangeLocked(
2641 std::string* id, std::string* physicalId,
2642 const std::string& cameraDeviceName,
2643 const std::string& physicalCameraDeviceName,
2644 CameraDeviceStatus newStatus) {
2645 bool known = false;
2646 std::string cameraId;
2647 for (auto& deviceInfo : mDevices) {
2648 if (deviceInfo->mName == cameraDeviceName) {
2649 cameraId = deviceInfo->mId;
2650 if (!deviceInfo->mIsLogicalCamera) {
2651 ALOGE("%s: Invalid combination of camera id %s, physical id %s",
2652 __FUNCTION__, cameraId.c_str(), physicalCameraDeviceName.c_str());
2653 return BAD_VALUE;
2654 }
2655 if (std::find(deviceInfo->mPhysicalIds.begin(), deviceInfo->mPhysicalIds.end(),
2656 physicalCameraDeviceName) == deviceInfo->mPhysicalIds.end()) {
2657 ALOGE("%s: Invalid combination of camera id %s, physical id %s",
2658 __FUNCTION__, cameraId.c_str(), physicalCameraDeviceName.c_str());
2659 return BAD_VALUE;
2660 }
2661 ALOGI("Camera device %s physical device %s status is now %s",
2662 cameraDeviceName.c_str(), physicalCameraDeviceName.c_str(),
2663 FrameworkDeviceStatusToString(newStatus));
2664 known = true;
2665 break;
2666 }
2667 }
2668 // Previously unseen device; status must not be NOT_PRESENT
2669 if (!known) {
2670 ALOGW("Camera provider %s says an unknown camera device %s-%s is not present. Curious.",
2671 mProviderName.c_str(), cameraDeviceName.c_str(),
2672 physicalCameraDeviceName.c_str());
2673 return BAD_VALUE;
2674 }
2675
2676 if (mUnavailablePhysicalCameras.count(cameraId) == 0) {
2677 mUnavailablePhysicalCameras.emplace(cameraId, std::set<std::string>{});
2678 }
2679 if (newStatus != CameraDeviceStatus::PRESENT) {
2680 mUnavailablePhysicalCameras[cameraId].insert(physicalCameraDeviceName);
2681 } else {
2682 mUnavailablePhysicalCameras[cameraId].erase(physicalCameraDeviceName);
2683 }
2684
2685 *id = cameraId;
2686 *physicalId = physicalCameraDeviceName;
2687 return OK;
2688 }
2689
torchModeStatusChangeInternal(const std::string & cameraDeviceName,TorchModeStatus newStatus)2690 void CameraProviderManager::ProviderInfo::torchModeStatusChangeInternal(
2691 const std::string& cameraDeviceName,
2692 TorchModeStatus newStatus) {
2693 sp<StatusListener> listener;
2694 SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC;
2695 std::string id;
2696 bool known = false;
2697 {
2698 // Hold mLock for accessing mDevices
2699 std::lock_guard<std::mutex> lock(mLock);
2700 for (auto& deviceInfo : mDevices) {
2701 if (deviceInfo->mName == cameraDeviceName) {
2702 ALOGI("Camera device %s torch status is now %s", cameraDeviceName.c_str(),
2703 FrameworkTorchStatusToString(newStatus));
2704 id = deviceInfo->mId;
2705 known = true;
2706 systemCameraKind = deviceInfo->mSystemCameraKind;
2707 if (TorchModeStatus::AVAILABLE_ON != newStatus) {
2708 mManager->removeRef(CameraProviderManager::DeviceMode::TORCH, id);
2709 }
2710 break;
2711 }
2712 }
2713 if (!known) {
2714 ALOGW("Camera provider %s says an unknown camera %s now has torch status %d. Curious.",
2715 mProviderName.c_str(), cameraDeviceName.c_str(), newStatus);
2716 return;
2717 }
2718 // no lock needed since listener is set up only once during
2719 // CameraProviderManager initialization and then never changed till it is
2720 // destructed.
2721 listener = mManager->getStatusListener();
2722 }
2723 // Call without lock held to allow reentrancy into provider manager
2724 // The problem with holding mLock here is that we
2725 // might be limiting re-entrancy : CameraService::onTorchStatusChanged calls
2726 // back into CameraProviderManager which might try to hold mLock again (eg:
2727 // findDeviceInfo, which should be holding mLock while iterating through
2728 // each provider's devices).
2729 if (listener != nullptr) {
2730 listener->onTorchStatusChanged(id, newStatus, systemCameraKind);
2731 }
2732 return;
2733 }
2734
notifyDeviceInfoStateChangeLocked(int64_t newDeviceState)2735 void CameraProviderManager::ProviderInfo::notifyDeviceInfoStateChangeLocked(
2736 int64_t newDeviceState) {
2737 std::lock_guard<std::mutex> lock(mLock);
2738 for (auto it = mDevices.begin(); it != mDevices.end(); it++) {
2739 (*it)->notifyDeviceStateChange(newDeviceState);
2740 }
2741 }
2742
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)2743 CameraProviderManager::ProviderInfo::DeviceInfo3::DeviceInfo3(const std::string& name,
2744 const metadata_vendor_id_t tagId, const std::string &id,
2745 uint16_t minorVersion,
2746 const CameraResourceCost& resourceCost,
2747 sp<ProviderInfo> parentProvider,
2748 const std::vector<std::string>& publicCameraIds) :
2749 DeviceInfo(name, tagId, id,
2750 hardware::hidl_version{
2751 static_cast<uint16_t >(
2752 parentProvider->getIPCTransport() == IPCTransport::HIDL ? 3 : 1),
2753 minorVersion},
2754 publicCameraIds, resourceCost, parentProvider) { }
2755
notifyDeviceStateChange(int64_t newState)2756 void CameraProviderManager::ProviderInfo::DeviceInfo3::notifyDeviceStateChange(int64_t newState) {
2757 if (!mDeviceStateOrientationMap.empty() &&
2758 (mDeviceStateOrientationMap.find(newState) != mDeviceStateOrientationMap.end())) {
2759 mCameraCharacteristics.update(ANDROID_SENSOR_ORIENTATION,
2760 &mDeviceStateOrientationMap[newState], 1);
2761 if (mCameraCharNoPCOverride.get() != nullptr) {
2762 mCameraCharNoPCOverride->update(ANDROID_SENSOR_ORIENTATION,
2763 &mDeviceStateOrientationMap[newState], 1);
2764 }
2765 }
2766 }
2767
getCameraInfo(int rotationOverride,int * portraitRotation,hardware::CameraInfo * info) const2768 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::getCameraInfo(
2769 int rotationOverride, int *portraitRotation,
2770 hardware::CameraInfo *info) const {
2771 if (info == nullptr) return BAD_VALUE;
2772
2773 bool freeform_compat_enabled = wm_flags::camera_compat_for_freeform();
2774 if (!freeform_compat_enabled &&
2775 rotationOverride > hardware::ICameraService::ROTATION_OVERRIDE_OVERRIDE_TO_PORTRAIT) {
2776 ALOGW("Camera compat freeform flag disabled but rotation override is %d", rotationOverride);
2777 }
2778 camera_metadata_ro_entry facing =
2779 mCameraCharacteristics.find(ANDROID_LENS_FACING);
2780 if (facing.count == 1) {
2781 switch (facing.data.u8[0]) {
2782 case ANDROID_LENS_FACING_BACK:
2783 info->facing = hardware::CAMERA_FACING_BACK;
2784 break;
2785 case ANDROID_LENS_FACING_EXTERNAL:
2786 // Map external to front for legacy API
2787 case ANDROID_LENS_FACING_FRONT:
2788 info->facing = hardware::CAMERA_FACING_FRONT;
2789 break;
2790 }
2791 } else {
2792 ALOGE("%s: Unable to find android.lens.facing static metadata", __FUNCTION__);
2793 return NAME_NOT_FOUND;
2794 }
2795
2796 camera_metadata_ro_entry orientation =
2797 mCameraCharacteristics.find(ANDROID_SENSOR_ORIENTATION);
2798 if (orientation.count == 1) {
2799 info->orientation = orientation.data.i32[0];
2800 } else {
2801 ALOGE("%s: Unable to find android.sensor.orientation static metadata", __FUNCTION__);
2802 return NAME_NOT_FOUND;
2803 }
2804
2805 if (rotationOverride == hardware::ICameraService::ROTATION_OVERRIDE_OVERRIDE_TO_PORTRAIT
2806 && (info->orientation == 0 || info->orientation == 180)) {
2807 *portraitRotation = 90;
2808 if (info->facing == hardware::CAMERA_FACING_FRONT) {
2809 info->orientation = (360 + info->orientation - 90) % 360;
2810 } else {
2811 info->orientation = (360 + info->orientation + 90) % 360;
2812 }
2813 } else if (freeform_compat_enabled &&
2814 rotationOverride == hardware::ICameraService::ROTATION_OVERRIDE_ROTATION_ONLY
2815 && (info->orientation == 90 || info->orientation == 270)) {
2816 *portraitRotation = info->facing == hardware::CAMERA_FACING_BACK ? 90 : 270;
2817 } else {
2818 *portraitRotation = 0;
2819 }
2820
2821 return OK;
2822 }
isAPI1Compatible() const2823 bool CameraProviderManager::ProviderInfo::DeviceInfo3::isAPI1Compatible() const {
2824 // Do not advertise NIR cameras to API1 camera app.
2825 camera_metadata_ro_entry cfa = mCameraCharacteristics.find(
2826 ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT);
2827 if (cfa.count == 1 && cfa.data.u8[0] == ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT_NIR) {
2828 return false;
2829 }
2830
2831 bool isBackwardCompatible = false;
2832 camera_metadata_ro_entry_t caps = mCameraCharacteristics.find(
2833 ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
2834 for (size_t i = 0; i < caps.count; i++) {
2835 if (caps.data.u8[i] ==
2836 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) {
2837 isBackwardCompatible = true;
2838 break;
2839 }
2840 }
2841
2842 return isBackwardCompatible;
2843 }
2844
getCameraCharacteristics(bool overrideForPerfClass,CameraMetadata * characteristics,int rotationOverride)2845 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::getCameraCharacteristics(
2846 bool overrideForPerfClass, CameraMetadata *characteristics,
2847 int rotationOverride) {
2848 if (characteristics == nullptr) return BAD_VALUE;
2849
2850 if (!overrideForPerfClass && mCameraCharNoPCOverride != nullptr) {
2851 *characteristics = *mCameraCharNoPCOverride;
2852 } else {
2853 *characteristics = mCameraCharacteristics;
2854 }
2855
2856 if (rotationOverride == hardware::ICameraService::ROTATION_OVERRIDE_OVERRIDE_TO_PORTRAIT) {
2857 const auto &lensFacingEntry = characteristics->find(ANDROID_LENS_FACING);
2858 const auto &sensorOrientationEntry = characteristics->find(ANDROID_SENSOR_ORIENTATION);
2859 uint8_t lensFacing = lensFacingEntry.data.u8[0];
2860 if (lensFacingEntry.count > 0 && sensorOrientationEntry.count > 0) {
2861 int32_t sensorOrientation = sensorOrientationEntry.data.i32[0];
2862 int32_t newSensorOrientation = sensorOrientation;
2863
2864 if (sensorOrientation == 0 || sensorOrientation == 180) {
2865 if (lensFacing == ANDROID_LENS_FACING_FRONT) {
2866 newSensorOrientation = (360 + sensorOrientation - 90) % 360;
2867 } else if (lensFacing == ANDROID_LENS_FACING_BACK) {
2868 newSensorOrientation = (360 + sensorOrientation + 90) % 360;
2869 }
2870 }
2871
2872 if (newSensorOrientation != sensorOrientation) {
2873 ALOGV("%s: Update ANDROID_SENSOR_ORIENTATION for lens facing %d "
2874 "from %d to %d", __FUNCTION__, lensFacing, sensorOrientation,
2875 newSensorOrientation);
2876 characteristics->update(ANDROID_SENSOR_ORIENTATION, &newSensorOrientation, 1);
2877 }
2878 }
2879
2880 if (characteristics->exists(ANDROID_INFO_DEVICE_STATE_ORIENTATIONS)) {
2881 ALOGV("%s: Erasing ANDROID_INFO_DEVICE_STATE_ORIENTATIONS for lens facing %d",
2882 __FUNCTION__, lensFacing);
2883 characteristics->erase(ANDROID_INFO_DEVICE_STATE_ORIENTATIONS);
2884 }
2885 }
2886
2887 return OK;
2888 }
2889
getPhysicalCameraCharacteristics(const std::string & physicalCameraId,CameraMetadata * characteristics) const2890 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::getPhysicalCameraCharacteristics(
2891 const std::string& physicalCameraId, CameraMetadata *characteristics) const {
2892 if (characteristics == nullptr) return BAD_VALUE;
2893 if (mPhysicalCameraCharacteristics.find(physicalCameraId) ==
2894 mPhysicalCameraCharacteristics.end()) {
2895 return NAME_NOT_FOUND;
2896 }
2897
2898 *characteristics = mPhysicalCameraCharacteristics.at(physicalCameraId);
2899 return OK;
2900 }
2901
filterSmallJpegSizes()2902 status_t CameraProviderManager::ProviderInfo::DeviceInfo3::filterSmallJpegSizes() {
2903 int32_t thresholdW = SessionConfigurationUtils::PERF_CLASS_JPEG_THRESH_W;
2904 int32_t thresholdH = SessionConfigurationUtils::PERF_CLASS_JPEG_THRESH_H;
2905
2906 if (mCameraCharNoPCOverride != nullptr) return OK;
2907
2908 mCameraCharNoPCOverride = std::make_unique<CameraMetadata>(mCameraCharacteristics);
2909
2910 // Remove small JPEG sizes from available stream configurations
2911 size_t largeJpegCount = 0;
2912 std::vector<int32_t> newStreamConfigs;
2913 camera_metadata_entry streamConfigs =
2914 mCameraCharacteristics.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
2915 for (size_t i = 0; i < streamConfigs.count; i += 4) {
2916 if ((streamConfigs.data.i32[i] == HAL_PIXEL_FORMAT_BLOB) && (streamConfigs.data.i32[i+3] ==
2917 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT)) {
2918 if (streamConfigs.data.i32[i+1] * streamConfigs.data.i32[i+2] <
2919 thresholdW * thresholdH) {
2920 continue;
2921 } else {
2922 largeJpegCount ++;
2923 }
2924 }
2925 newStreamConfigs.insert(newStreamConfigs.end(), streamConfigs.data.i32 + i,
2926 streamConfigs.data.i32 + i + 4);
2927 }
2928 if (newStreamConfigs.size() == 0 || largeJpegCount == 0) {
2929 return BAD_VALUE;
2930 }
2931
2932 // Remove small JPEG sizes from available min frame durations
2933 largeJpegCount = 0;
2934 std::vector<int64_t> newMinDurations;
2935 camera_metadata_entry minDurations =
2936 mCameraCharacteristics.find(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS);
2937 for (size_t i = 0; i < minDurations.count; i += 4) {
2938 if (minDurations.data.i64[i] == HAL_PIXEL_FORMAT_BLOB) {
2939 if ((int32_t)minDurations.data.i64[i+1] * (int32_t)minDurations.data.i64[i+2] <
2940 thresholdW * thresholdH) {
2941 continue;
2942 } else {
2943 largeJpegCount++;
2944 }
2945 }
2946 newMinDurations.insert(newMinDurations.end(), minDurations.data.i64 + i,
2947 minDurations.data.i64 + i + 4);
2948 }
2949 if (newMinDurations.size() == 0 || largeJpegCount == 0) {
2950 return BAD_VALUE;
2951 }
2952
2953 // Remove small JPEG sizes from available stall durations
2954 largeJpegCount = 0;
2955 std::vector<int64_t> newStallDurations;
2956 camera_metadata_entry stallDurations =
2957 mCameraCharacteristics.find(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS);
2958 for (size_t i = 0; i < stallDurations.count; i += 4) {
2959 if (stallDurations.data.i64[i] == HAL_PIXEL_FORMAT_BLOB) {
2960 if ((int32_t)stallDurations.data.i64[i+1] * (int32_t)stallDurations.data.i64[i+2] <
2961 thresholdW * thresholdH) {
2962 continue;
2963 } else {
2964 largeJpegCount++;
2965 }
2966 }
2967 newStallDurations.insert(newStallDurations.end(), stallDurations.data.i64 + i,
2968 stallDurations.data.i64 + i + 4);
2969 }
2970 if (newStallDurations.size() == 0 || largeJpegCount == 0) {
2971 return BAD_VALUE;
2972 }
2973
2974 mCameraCharacteristics.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
2975 newStreamConfigs.data(), newStreamConfigs.size());
2976 mCameraCharacteristics.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
2977 newMinDurations.data(), newMinDurations.size());
2978 mCameraCharacteristics.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
2979 newStallDurations.data(), newStallDurations.size());
2980
2981 // Re-generate metadata tags that have dependencies on BLOB sizes
2982 auto res = addDynamicDepthTags();
2983 if (OK != res) {
2984 ALOGE("%s: Failed to append dynamic depth tags: %s (%d)", __FUNCTION__,
2985 strerror(-res), res);
2986 // Allow filtering of small JPEG sizes to succeed even if dynamic depth
2987 // tags fail to generate.
2988 }
2989
2990 return OK;
2991 }
2992
parseProviderName(const std::string & name,std::string * type,uint32_t * id)2993 status_t CameraProviderManager::ProviderInfo::parseProviderName(const std::string& name,
2994 std::string *type, uint32_t *id) {
2995 // Format must be "<type>/<id>"
2996 #define ERROR_MSG_PREFIX "%s: Invalid provider name '%s'. " \
2997 "Should match '<type>/<id>' - "
2998
2999 if (!type || !id) return INVALID_OPERATION;
3000
3001 std::string::size_type slashIdx = name.find('/');
3002 if (slashIdx == std::string::npos || slashIdx == name.size() - 1) {
3003 ALOGE(ERROR_MSG_PREFIX
3004 "does not have / separator between type and id",
3005 __FUNCTION__, name.c_str());
3006 return BAD_VALUE;
3007 }
3008
3009 std::string typeVal = name.substr(0, slashIdx);
3010
3011 char *endPtr;
3012 errno = 0;
3013 long idVal = strtol(name.c_str() + slashIdx + 1, &endPtr, 10);
3014 if (errno != 0) {
3015 ALOGE(ERROR_MSG_PREFIX
3016 "cannot parse provider id as an integer: %s (%d)",
3017 __FUNCTION__, name.c_str(), strerror(errno), errno);
3018 return BAD_VALUE;
3019 }
3020 if (endPtr != name.c_str() + name.size()) {
3021 ALOGE(ERROR_MSG_PREFIX
3022 "provider id has unexpected length",
3023 __FUNCTION__, name.c_str());
3024 return BAD_VALUE;
3025 }
3026 if (idVal < 0) {
3027 ALOGE(ERROR_MSG_PREFIX
3028 "id is negative: %ld",
3029 __FUNCTION__, name.c_str(), idVal);
3030 return BAD_VALUE;
3031 }
3032
3033 #undef ERROR_MSG_PREFIX
3034
3035 *type = typeVal;
3036 *id = static_cast<uint32_t>(idVal);
3037
3038 return OK;
3039 }
3040
generateVendorTagId(const std::string & name)3041 metadata_vendor_id_t CameraProviderManager::ProviderInfo::generateVendorTagId(
3042 const std::string &name) {
3043 metadata_vendor_id_t ret = std::hash<std::string> {} (name);
3044 // CAMERA_METADATA_INVALID_VENDOR_ID is not a valid hash value
3045 if (CAMERA_METADATA_INVALID_VENDOR_ID == ret) {
3046 ret = 0;
3047 }
3048
3049 return ret;
3050 }
3051
parseDeviceName(const std::string & name,uint16_t * major,uint16_t * minor,std::string * type,std::string * id)3052 status_t CameraProviderManager::ProviderInfo::parseDeviceName(const std::string& name,
3053 uint16_t *major, uint16_t *minor, std::string *type, std::string *id) {
3054
3055 // Format must be "device@<major>.<minor>/<type>/<id>"
3056
3057 #define ERROR_MSG_PREFIX "%s: Invalid device name '%s'. " \
3058 "Should match 'device@<major>.<minor>/<type>/<id>' - "
3059
3060 if (!major || !minor || !type || !id) return INVALID_OPERATION;
3061
3062 // Verify starting prefix
3063 const char expectedPrefix[] = "device@";
3064
3065 if (name.find(expectedPrefix) != 0) {
3066 ALOGE(ERROR_MSG_PREFIX
3067 "does not start with '%s'",
3068 __FUNCTION__, name.c_str(), expectedPrefix);
3069 return BAD_VALUE;
3070 }
3071
3072 // Extract major/minor versions
3073 constexpr std::string::size_type atIdx = sizeof(expectedPrefix) - 2;
3074 std::string::size_type dotIdx = name.find('.', atIdx);
3075 if (dotIdx == std::string::npos) {
3076 ALOGE(ERROR_MSG_PREFIX
3077 "does not have @<major>. version section",
3078 __FUNCTION__, name.c_str());
3079 return BAD_VALUE;
3080 }
3081 std::string::size_type typeSlashIdx = name.find('/', dotIdx);
3082 if (typeSlashIdx == std::string::npos) {
3083 ALOGE(ERROR_MSG_PREFIX
3084 "does not have .<minor>/ version section",
3085 __FUNCTION__, name.c_str());
3086 return BAD_VALUE;
3087 }
3088
3089 char *endPtr;
3090 errno = 0;
3091 long majorVal = strtol(name.c_str() + atIdx + 1, &endPtr, 10);
3092 if (errno != 0) {
3093 ALOGE(ERROR_MSG_PREFIX
3094 "cannot parse major version: %s (%d)",
3095 __FUNCTION__, name.c_str(), strerror(errno), errno);
3096 return BAD_VALUE;
3097 }
3098 if (endPtr != name.c_str() + dotIdx) {
3099 ALOGE(ERROR_MSG_PREFIX
3100 "major version has unexpected length",
3101 __FUNCTION__, name.c_str());
3102 return BAD_VALUE;
3103 }
3104 long minorVal = strtol(name.c_str() + dotIdx + 1, &endPtr, 10);
3105 if (errno != 0) {
3106 ALOGE(ERROR_MSG_PREFIX
3107 "cannot parse minor version: %s (%d)",
3108 __FUNCTION__, name.c_str(), strerror(errno), errno);
3109 return BAD_VALUE;
3110 }
3111 if (endPtr != name.c_str() + typeSlashIdx) {
3112 ALOGE(ERROR_MSG_PREFIX
3113 "minor version has unexpected length",
3114 __FUNCTION__, name.c_str());
3115 return BAD_VALUE;
3116 }
3117 if (majorVal < 0 || majorVal > UINT16_MAX || minorVal < 0 || minorVal > UINT16_MAX) {
3118 ALOGE(ERROR_MSG_PREFIX
3119 "major/minor version is out of range of uint16_t: %ld.%ld",
3120 __FUNCTION__, name.c_str(), majorVal, minorVal);
3121 return BAD_VALUE;
3122 }
3123
3124 // Extract type and id
3125
3126 std::string::size_type instanceSlashIdx = name.find('/', typeSlashIdx + 1);
3127 if (instanceSlashIdx == std::string::npos) {
3128 ALOGE(ERROR_MSG_PREFIX
3129 "does not have /<type>/ component",
3130 __FUNCTION__, name.c_str());
3131 return BAD_VALUE;
3132 }
3133 std::string typeVal = name.substr(typeSlashIdx + 1, instanceSlashIdx - typeSlashIdx - 1);
3134
3135 if (instanceSlashIdx == name.size() - 1) {
3136 ALOGE(ERROR_MSG_PREFIX
3137 "does not have an /<id> component",
3138 __FUNCTION__, name.c_str());
3139 return BAD_VALUE;
3140 }
3141 std::string idVal = name.substr(instanceSlashIdx + 1);
3142
3143 #undef ERROR_MSG_PREFIX
3144
3145 *major = static_cast<uint16_t>(majorVal);
3146 *minor = static_cast<uint16_t>(minorVal);
3147 *type = typeVal;
3148 *id = idVal;
3149
3150 return OK;
3151 }
3152
~ProviderInfo()3153 CameraProviderManager::ProviderInfo::~ProviderInfo() {
3154 // Destruction of ProviderInfo is only supposed to happen when the respective
3155 // CameraProvider interface dies, so do not unregister callbacks.
3156 }
3157
3158 // Expects to have mInterfaceMutex locked
3159 std::vector<std::unordered_set<std::string>>
getConcurrentCameraIds() const3160 CameraProviderManager::getConcurrentCameraIds() const {
3161 std::vector<std::unordered_set<std::string>> deviceIdCombinations;
3162 std::lock_guard<std::mutex> lock(mInterfaceMutex);
3163 for (auto &provider : mProviders) {
3164 for (auto &combinations : provider->getConcurrentCameraIdCombinations()) {
3165 deviceIdCombinations.push_back(combinations);
3166 }
3167 }
3168 return deviceIdCombinations;
3169 }
3170
3171 // Checks if the containing vector of sets has any set that contains all of the
3172 // camera ids in cameraIdsAndSessionConfigs.
checkIfSetContainsAll(const std::vector<CameraIdAndSessionConfiguration> & cameraIdsAndSessionConfigs,const std::vector<std::unordered_set<std::string>> & containingSets)3173 static bool checkIfSetContainsAll(
3174 const std::vector<CameraIdAndSessionConfiguration> &cameraIdsAndSessionConfigs,
3175 const std::vector<std::unordered_set<std::string>> &containingSets) {
3176 for (auto &containingSet : containingSets) {
3177 bool didHaveAll = true;
3178 for (auto &cameraIdAndSessionConfig : cameraIdsAndSessionConfigs) {
3179 if (containingSet.find(cameraIdAndSessionConfig.mCameraId) == containingSet.end()) {
3180 // a camera id doesn't belong to this set, keep looking in other
3181 // sets
3182 didHaveAll = false;
3183 break;
3184 }
3185 }
3186 if (didHaveAll) {
3187 // found a set that has all camera ids, lets return;
3188 return true;
3189 }
3190 }
3191 return false;
3192 }
3193
isConcurrentSessionConfigurationSupported(const std::vector<CameraIdAndSessionConfiguration> & cameraIdsAndSessionConfigs,const std::set<std::string> & perfClassPrimaryCameraIds,int targetSdkVersion,bool * isSupported)3194 status_t CameraProviderManager::isConcurrentSessionConfigurationSupported(
3195 const std::vector<CameraIdAndSessionConfiguration> &cameraIdsAndSessionConfigs,
3196 const std::set<std::string>& perfClassPrimaryCameraIds,
3197 int targetSdkVersion, bool *isSupported) {
3198 std::lock_guard<std::mutex> lock(mInterfaceMutex);
3199 // Check if all the devices are a subset of devices advertised by the
3200 // same provider through getConcurrentStreamingCameraIds()
3201 // TODO: we should also do a findDeviceInfoLocked here ?
3202 for (auto &provider : mProviders) {
3203 if (checkIfSetContainsAll(cameraIdsAndSessionConfigs,
3204 provider->getConcurrentCameraIdCombinations())) {
3205 return provider->isConcurrentSessionConfigurationSupported(
3206 cameraIdsAndSessionConfigs, perfClassPrimaryCameraIds, targetSdkVersion,
3207 isSupported);
3208 }
3209 }
3210 *isSupported = false;
3211 //The set of camera devices were not found
3212 return INVALID_OPERATION;
3213 }
3214
getCameraCharacteristicsLocked(const std::string & id,bool overrideForPerfClass,CameraMetadata * characteristics,int rotationOverride) const3215 status_t CameraProviderManager::getCameraCharacteristicsLocked(const std::string &id,
3216 bool overrideForPerfClass, CameraMetadata* characteristics,
3217 int rotationOverride) const {
3218 auto deviceInfo = findDeviceInfoLocked(id);
3219 if (deviceInfo != nullptr) {
3220 return deviceInfo->getCameraCharacteristics(overrideForPerfClass, characteristics,
3221 rotationOverride);
3222 }
3223
3224 // Find hidden physical camera characteristics
3225 for (auto& provider : mProviders) {
3226 for (auto& deviceInfo : provider->mDevices) {
3227 status_t res = deviceInfo->getPhysicalCameraCharacteristics(id, characteristics);
3228 if (res != NAME_NOT_FOUND) return res;
3229 }
3230 }
3231
3232 return NAME_NOT_FOUND;
3233 }
3234
filterLogicalCameraIdsLocked(std::vector<std::string> & deviceIds) const3235 void CameraProviderManager::filterLogicalCameraIdsLocked(
3236 std::vector<std::string>& deviceIds) const
3237 {
3238 // Map between camera facing and camera IDs related to logical camera.
3239 std::map<int, std::unordered_set<std::string>> idCombos;
3240
3241 // Collect all logical and its underlying physical camera IDs for each
3242 // facing.
3243 for (auto& deviceId : deviceIds) {
3244 auto deviceInfo = findDeviceInfoLocked(deviceId);
3245 if (deviceInfo == nullptr) continue;
3246
3247 if (!deviceInfo->mIsLogicalCamera) {
3248 continue;
3249 }
3250
3251 // combo contains the ids of a logical camera and its physical cameras
3252 std::vector<std::string> combo = deviceInfo->mPhysicalIds;
3253 combo.push_back(deviceId);
3254
3255 hardware::CameraInfo info;
3256 int portraitRotation;
3257 status_t res = deviceInfo->getCameraInfo(
3258 /*rotationOverride*/hardware::ICameraService::ROTATION_OVERRIDE_NONE,
3259 &portraitRotation, &info);
3260 if (res != OK) {
3261 ALOGE("%s: Error reading camera info: %s (%d)", __FUNCTION__, strerror(-res), res);
3262 continue;
3263 }
3264 idCombos[info.facing].insert(combo.begin(), combo.end());
3265 }
3266
3267 // Only expose one camera ID per facing for all logical and underlying
3268 // physical camera IDs.
3269 for (auto& r : idCombos) {
3270 auto& removedIds = r.second;
3271 for (auto& id : deviceIds) {
3272 auto foundId = std::find(removedIds.begin(), removedIds.end(), id);
3273 if (foundId == removedIds.end()) {
3274 continue;
3275 }
3276
3277 removedIds.erase(foundId);
3278 break;
3279 }
3280 deviceIds.erase(std::remove_if(deviceIds.begin(), deviceIds.end(),
3281 [&removedIds](const std::string& s) {
3282 return removedIds.find(s) != removedIds.end();}),
3283 deviceIds.end());
3284 }
3285 }
3286
isVirtualCameraHalEnabled()3287 bool CameraProviderManager::isVirtualCameraHalEnabled() {
3288 return vd_flags::virtual_camera_service_discovery() &&
3289 vd_flags::virtual_camera_service_build_flag();
3290 }
3291
3292 } // namespace android
3293