• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 #include "HidlProviderInfo.h"
17 #include "common/HalConversionsTemplated.h"
18 #include "common/CameraProviderInfoTemplated.h"
19 
20 #include <cutils/properties.h>
21 
22 #include <android/hardware/ICameraService.h>
23 #include <camera_metadata_hidden.h>
24 
25 #include "device3/ZoomRatioMapper.h"
26 #include <utils/SessionConfigurationUtilsHidl.h>
27 #include <utils/Trace.h>
28 
29 #include <android/hardware/camera/device/3.7/ICameraDevice.h>
30 
31 namespace {
32 const bool kEnableLazyHal(property_get_bool("ro.camera.enableLazyHal", false));
33 } // anonymous namespace
34 
35 namespace android {
36 
37 using namespace android::camera3;
38 using namespace hardware::camera;
39 using hardware::camera::common::V1_0::VendorTagSection;
40 using hardware::camera::common::V1_0::Status;
41 using hardware::camera::provider::V2_7::CameraIdAndStreamCombination;
42 using hardware::camera2::utils::CameraIdAndSessionConfiguration;
43 
44 
45 using StatusListener = CameraProviderManager::StatusListener;
46 using HalDeviceStatusType = android::hardware::camera::common::V1_0::CameraDeviceStatus;
47 
48 using hardware::camera::provider::V2_5::DeviceState;
49 using hardware::ICameraService;
50 
mapToStatusT(const Status & s)51 status_t HidlProviderInfo::mapToStatusT(const Status& s)  {
52     switch(s) {
53         case Status::OK:
54             return OK;
55         case Status::ILLEGAL_ARGUMENT:
56             return BAD_VALUE;
57         case Status::CAMERA_IN_USE:
58             return -EBUSY;
59         case Status::MAX_CAMERAS_IN_USE:
60             return -EUSERS;
61         case Status::METHOD_NOT_SUPPORTED:
62             return UNKNOWN_TRANSACTION;
63         case Status::OPERATION_NOT_SUPPORTED:
64             return INVALID_OPERATION;
65         case Status::CAMERA_DISCONNECTED:
66             return DEAD_OBJECT;
67         case Status::INTERNAL_ERROR:
68             return INVALID_OPERATION;
69     }
70     ALOGW("Unexpected HAL status code %d", s);
71     return INVALID_OPERATION;
72 }
73 
mapToHidlDeviceState(int64_t newState)74 static hardware::hidl_bitfield<DeviceState> mapToHidlDeviceState(int64_t newState) {
75     hardware::hidl_bitfield<DeviceState> newDeviceState{};
76     if (newState & ICameraService::DEVICE_STATE_BACK_COVERED) {
77         newDeviceState |= DeviceState::BACK_COVERED;
78     }
79     if (newState & ICameraService::DEVICE_STATE_FRONT_COVERED) {
80         newDeviceState |= DeviceState::FRONT_COVERED;
81     }
82     if (newState & ICameraService::DEVICE_STATE_FOLDED) {
83         newDeviceState |= DeviceState::FOLDED;
84     }
85     // Only map vendor bits directly
86     uint64_t vendorBits = static_cast<uint64_t>(newState) & 0xFFFFFFFF00000000l;
87     newDeviceState |= vendorBits;
88 
89     ALOGV("%s: New device state 0x%" PRIx64, __FUNCTION__, newDeviceState);
90     return newDeviceState;
91 }
92 
statusToString(const Status & s)93 const char* statusToString(const Status& s) {
94     switch(s) {
95         case Status::OK:
96             return "OK";
97         case Status::ILLEGAL_ARGUMENT:
98             return "ILLEGAL_ARGUMENT";
99         case Status::CAMERA_IN_USE:
100             return "CAMERA_IN_USE";
101         case Status::MAX_CAMERAS_IN_USE:
102             return "MAX_CAMERAS_IN_USE";
103         case Status::METHOD_NOT_SUPPORTED:
104             return "METHOD_NOT_SUPPORTED";
105         case Status::OPERATION_NOT_SUPPORTED:
106             return "OPERATION_NOT_SUPPORTED";
107         case Status::CAMERA_DISCONNECTED:
108             return "CAMERA_DISCONNECTED";
109         case Status::INTERNAL_ERROR:
110             return "INTERNAL_ERROR";
111     }
112     ALOGW("Unexpected HAL status code %d", s);
113     return "UNKNOWN_ERROR";
114 }
115 
initializeHidlProvider(sp<provider::V2_4::ICameraProvider> & interface,int64_t currentDeviceState)116 status_t HidlProviderInfo::initializeHidlProvider(
117         sp<provider::V2_4::ICameraProvider>& interface,
118         int64_t currentDeviceState) {
119     status_t res = parseProviderName(mProviderName, &mType, &mId);
120     if (res != OK) {
121         ALOGE("%s: Invalid provider name, ignoring", __FUNCTION__);
122         return BAD_VALUE;
123     }
124     ALOGI("Connecting to new camera provider: %s, isRemote? %d",
125             mProviderName.c_str(), interface->isRemote());
126 
127     // Determine minor version
128     mMinorVersion = 4;
129     auto cast2_6 = provider::V2_6::ICameraProvider::castFrom(interface);
130     sp<provider::V2_6::ICameraProvider> interface2_6 = nullptr;
131     if (cast2_6.isOk()) {
132         interface2_6 = cast2_6;
133         if (interface2_6 != nullptr) {
134             mMinorVersion = 6;
135         }
136     }
137     // We need to check again since cast2_6.isOk() succeeds even if the provider
138     // version isn't actually 2.6.
139     if (interface2_6 == nullptr){
140         auto cast2_5 =
141                 provider::V2_5::ICameraProvider::castFrom(interface);
142         sp<provider::V2_5::ICameraProvider> interface2_5 = nullptr;
143         if (cast2_5.isOk()) {
144             interface2_5 = cast2_5;
145             if (interface != nullptr) {
146                 mMinorVersion = 5;
147             }
148         }
149     } else {
150         auto cast2_7 = provider::V2_7::ICameraProvider::castFrom(interface);
151         if (cast2_7.isOk()) {
152             sp<provider::V2_7::ICameraProvider> interface2_7 = cast2_7;
153             if (interface2_7 != nullptr) {
154                 mMinorVersion = 7;
155             }
156         }
157     }
158 
159     // cameraDeviceStatusChange callbacks may be called (and causing new devices added)
160     // before setCallback returns
161     hardware::Return<Status> status = interface->setCallback(this);
162     if (!status.isOk()) {
163         ALOGE("%s: Transaction error setting up callbacks with camera provider '%s': %s",
164                 __FUNCTION__, mProviderName.c_str(), status.description().c_str());
165         return DEAD_OBJECT;
166     }
167     if (status != Status::OK) {
168         ALOGE("%s: Unable to register callbacks with camera provider '%s'",
169                 __FUNCTION__, mProviderName.c_str());
170         return mapToStatusT(status);
171     }
172 
173     hardware::Return<bool> linked = interface->linkToDeath(this, /*cookie*/ mId);
174     if (!linked.isOk()) {
175         ALOGE("%s: Transaction error in linking to camera provider '%s' death: %s",
176                 __FUNCTION__, mProviderName.c_str(), linked.description().c_str());
177         return DEAD_OBJECT;
178     } else if (!linked) {
179         ALOGW("%s: Unable to link to provider '%s' death notifications",
180                 __FUNCTION__, mProviderName.c_str());
181     }
182 
183     if (!kEnableLazyHal) {
184         // Save HAL reference indefinitely
185         mSavedInterface = interface;
186     } else {
187         mActiveInterface = interface;
188     }
189 
190     ALOGV("%s: Setting device state for %s: 0x%" PRIx64,
191             __FUNCTION__, mProviderName.c_str(), mDeviceState);
192     notifyDeviceStateChange(currentDeviceState);
193 
194     res = setUpVendorTags();
195     if (res != OK) {
196         ALOGE("%s: Unable to set up vendor tags from provider '%s'",
197                 __FUNCTION__, mProviderName.c_str());
198         return res;
199     }
200 
201     // Get initial list of camera devices, if any
202     std::vector<std::string> devices;
203     hardware::Return<void> ret = interface->getCameraIdList([&status, this, &devices](
204             Status idStatus,
205             const hardware::hidl_vec<hardware::hidl_string>& cameraDeviceNames) {
206         status = idStatus;
207         if (status == Status::OK) {
208             for (auto& name : cameraDeviceNames) {
209                 uint16_t major, minor;
210                 std::string type, id;
211                 status_t res = parseDeviceName(name, &major, &minor, &type, &id);
212                 if (res != OK) {
213                     ALOGE("%s: Error parsing deviceName: %s: %d", __FUNCTION__, name.c_str(), res);
214                     status = Status::INTERNAL_ERROR;
215                 } else {
216                     devices.push_back(name);
217                     mProviderPublicCameraIds.push_back(id);
218                 }
219             }
220         } });
221     if (!ret.isOk()) {
222         ALOGE("%s: Transaction error in getting camera ID list from provider '%s': %s",
223                 __FUNCTION__, mProviderName.c_str(), linked.description().c_str());
224         return DEAD_OBJECT;
225     }
226     if (status != Status::OK) {
227         ALOGE("%s: Unable to query for camera devices from provider '%s'",
228                 __FUNCTION__, mProviderName.c_str());
229         return mapToStatusT(status);
230     }
231 
232     // Get list of concurrent streaming camera device combinations
233     if (mMinorVersion >= 6) {
234         res = getConcurrentCameraIdsInternalLocked(interface2_6);
235         if (res != OK) {
236             return res;
237         }
238     }
239 
240     ret = interface->isSetTorchModeSupported(
241         [this](auto status, bool supported) {
242             if (status == Status::OK) {
243                 mSetTorchModeSupported = supported;
244             }
245         });
246     if (!ret.isOk()) {
247         ALOGE("%s: Transaction error checking torch mode support '%s': %s",
248                 __FUNCTION__, mProviderName.c_str(), ret.description().c_str());
249         return DEAD_OBJECT;
250     }
251 
252     mIsRemote = interface->isRemote();
253 
254     initializeProviderInfoCommon(devices);
255 
256     return OK;
257 }
258 
setUpVendorTags()259 status_t HidlProviderInfo::setUpVendorTags() {
260     if (mVendorTagDescriptor != nullptr)
261         return OK;
262 
263     hardware::hidl_vec<VendorTagSection> vts;
264     Status status;
265     hardware::Return<void> ret;
266     const sp<hardware::camera::provider::V2_4::ICameraProvider> interface =
267             startProviderInterface();
268     if (interface == nullptr) {
269         return DEAD_OBJECT;
270     }
271     ret = interface->getVendorTags(
272         [&](auto s, const auto& vendorTagSecs) {
273             status = s;
274             if (s == Status::OK) {
275                 vts = vendorTagSecs;
276             }
277     });
278     if (!ret.isOk()) {
279         ALOGE("%s: Transaction error getting vendor tags from provider '%s': %s",
280                 __FUNCTION__, mProviderName.c_str(), ret.description().c_str());
281         return DEAD_OBJECT;
282     }
283     if (status != Status::OK) {
284         return mapToStatusT(status);
285     }
286 
287     // Read all vendor tag definitions into a descriptor
288     status_t res;
289     if ((res = IdlVendorTagDescriptor::createDescriptorFromIdl<
290                 hardware::hidl_vec<hardware::camera::common::V1_0::VendorTagSection>,
291                         hardware::camera::common::V1_0::VendorTagSection>(vts,
292                                 /*out*/mVendorTagDescriptor))
293             != OK) {
294         ALOGE("%s: Could not generate descriptor from vendor tag operations,"
295                 "received error %s (%d). Camera clients will not be able to use"
296                 "vendor tags", __FUNCTION__, strerror(res), res);
297         return res;
298     }
299 
300     return OK;
301 }
302 
notifyDeviceStateChange(int64_t newDeviceState)303 status_t HidlProviderInfo::notifyDeviceStateChange(int64_t newDeviceState) {
304     mDeviceState = mapToHidlDeviceState(newDeviceState);
305     if (mMinorVersion >= 5) {
306         // Check if the provider is currently active - not going to start it for this notification
307         auto interface = mSavedInterface != nullptr ? mSavedInterface : mActiveInterface.promote();
308         if (interface != nullptr) {
309             // Send current device state
310             auto castResult = provider::V2_5::ICameraProvider::castFrom(interface);
311             if (castResult.isOk()) {
312                 sp<provider::V2_5::ICameraProvider> interface_2_5 = castResult;
313                 if (interface_2_5 != nullptr) {
314                     interface_2_5->notifyDeviceStateChange(mDeviceState);
315                 }
316             }
317         }
318     }
319     return OK;
320 }
321 
322 sp<device::V3_2::ICameraDevice>
startDeviceInterface(const std::string & name)323 HidlProviderInfo::startDeviceInterface(const std::string &name) {
324     Status status;
325     sp<device::V3_2::ICameraDevice> cameraInterface;
326     hardware::Return<void> ret;
327     const sp<provider::V2_4::ICameraProvider> interface = startProviderInterface();
328     if (interface == nullptr) {
329         return nullptr;
330     }
331     ret = interface->getCameraDeviceInterface_V3_x(name, [&status, &cameraInterface](
332         Status s, sp<device::V3_2::ICameraDevice> interface) {
333                 status = s;
334                 cameraInterface = interface;
335             });
336     if (!ret.isOk()) {
337         ALOGE("%s: Transaction error trying to obtain interface for camera device %s: %s",
338                 __FUNCTION__, name.c_str(), ret.description().c_str());
339         return nullptr;
340     }
341     if (status != Status::OK) {
342         ALOGE("%s: Unable to obtain interface for camera device %s: %s", __FUNCTION__,
343                 name.c_str(), statusToString(status));
344         return nullptr;
345     }
346     return cameraInterface;
347 }
348 
successfullyStartedProviderInterface()349 bool HidlProviderInfo::successfullyStartedProviderInterface() {
350     return startProviderInterface() != nullptr;
351 }
352 
353 const sp<provider::V2_4::ICameraProvider>
startProviderInterface()354 HidlProviderInfo::startProviderInterface() {
355     ATRACE_CALL();
356     ALOGV("Request to start camera provider: %s", mProviderName.c_str());
357     if (mSavedInterface != nullptr) {
358         return mSavedInterface;
359     }
360     if (!kEnableLazyHal) {
361         ALOGE("Bad provider state! Should not be here on a non-lazy HAL!");
362         return nullptr;
363     }
364 
365     auto interface = mActiveInterface.promote();
366     if (interface == nullptr) {
367         // Try to get service without starting
368         interface = mManager->mHidlServiceProxy->tryGetService(mProviderName);
369         if (interface == nullptr) {
370             ALOGV("Camera provider actually needs restart, calling getService(%s)",
371                   mProviderName.c_str());
372             interface = mManager->mHidlServiceProxy->getService(mProviderName);
373 
374             // Set all devices as ENUMERATING, provider should update status
375             // to PRESENT after initializing.
376             // This avoids failing getCameraDeviceInterface_V3_x before devices
377             // are ready.
378             for (auto& device : mDevices) {
379               device->mIsDeviceAvailable = false;
380             }
381 
382             interface->setCallback(this);
383             hardware::Return<bool>
384                 linked = interface->linkToDeath(this, /*cookie*/ mId);
385             if (!linked.isOk()) {
386               ALOGE(
387                   "%s: Transaction error in linking to camera provider '%s' death: %s",
388                   __FUNCTION__,
389                   mProviderName.c_str(),
390                   linked.description().c_str());
391               mManager->removeProvider(mProviderInstance);
392               return nullptr;
393             } else if (!linked) {
394               ALOGW("%s: Unable to link to provider '%s' death notifications",
395                     __FUNCTION__, mProviderName.c_str());
396             }
397             // Send current device state
398             if (mMinorVersion >= 5) {
399               auto castResult =
400                   provider::V2_5::ICameraProvider::castFrom(interface);
401               if (castResult.isOk()) {
402                 sp<provider::V2_5::ICameraProvider> interface_2_5 = castResult;
403                 if (interface_2_5 != nullptr) {
404                   ALOGV("%s: Initial device state for %s: 0x %" PRIx64,
405                         __FUNCTION__, mProviderName.c_str(), mDeviceState);
406                   interface_2_5->notifyDeviceStateChange(mDeviceState);
407                 }
408               }
409             }
410         }
411         mActiveInterface = interface;
412     } else {
413         ALOGV("Camera provider (%s) already in use. Re-using instance.",
414               mProviderName.c_str());
415     }
416 
417     return interface;
418 }
419 
cameraDeviceStatusChange(const hardware::hidl_string & cameraDeviceName,HalDeviceStatusType newStatus)420 hardware::Return<void> HidlProviderInfo::cameraDeviceStatusChange(
421         const hardware::hidl_string& cameraDeviceName,
422         HalDeviceStatusType newStatus) {
423     cameraDeviceStatusChangeInternal(cameraDeviceName, HalToFrameworkCameraDeviceStatus(newStatus));
424     return hardware::Void();
425 }
426 
physicalCameraDeviceStatusChange(const hardware::hidl_string & cameraDeviceName,const hardware::hidl_string & physicalCameraDeviceName,HalDeviceStatusType newStatus)427 hardware::Return<void> HidlProviderInfo::physicalCameraDeviceStatusChange(
428         const hardware::hidl_string& cameraDeviceName,
429         const hardware::hidl_string& physicalCameraDeviceName,
430         HalDeviceStatusType newStatus) {
431     physicalCameraDeviceStatusChangeInternal(cameraDeviceName, physicalCameraDeviceName,
432             HalToFrameworkCameraDeviceStatus(newStatus));
433     return hardware::Void();
434 }
435 
torchModeStatusChange(const hardware::hidl_string & cameraDeviceName,hardware::camera::common::V1_0::TorchModeStatus newStatus)436 hardware::Return<void> HidlProviderInfo::torchModeStatusChange(
437         const hardware::hidl_string& cameraDeviceName,
438         hardware::camera::common::V1_0::TorchModeStatus newStatus) {
439 
440     torchModeStatusChangeInternal(cameraDeviceName, HalToFrameworkTorchModeStatus(newStatus));
441     return hardware::Void();
442 }
443 
serviceDied(uint64_t cookie,const wp<hidl::base::V1_0::IBase> & who)444 void HidlProviderInfo::serviceDied(uint64_t cookie,
445         [[maybe_unused]] const wp<hidl::base::V1_0::IBase>& who) {
446     ALOGI("Camera provider '%s' has died; removing it", mProviderInstance.c_str());
447     if (cookie != mId) {
448         ALOGW("%s: Unexpected serviceDied cookie %" PRIu64 ", expected %" PRIu32,
449                 __FUNCTION__, cookie, mId);
450     }
451     mManager->removeProvider(mProviderInstance);
452 }
453 
454 std::unique_ptr<CameraProviderManager::ProviderInfo::DeviceInfo>
initializeDeviceInfo(const std::string & name,const metadata_vendor_id_t tagId,const std::string & id,uint16_t minorVersion)455     HidlProviderInfo::initializeDeviceInfo(
456         const std::string &name, const metadata_vendor_id_t tagId,
457         const std::string &id, uint16_t minorVersion) {
458     Status status;
459 
460     auto cameraInterface = startDeviceInterface(name);
461     if (cameraInterface == nullptr) return nullptr;
462 
463     common::V1_0::CameraResourceCost resourceCost;
464     cameraInterface->getResourceCost([&status, &resourceCost](
465         Status s, common::V1_0::CameraResourceCost cost) {
466                 status = s;
467                 resourceCost = cost;
468             });
469     if (status != Status::OK) {
470         ALOGE("%s: Unable to obtain resource costs for camera device %s: %s", __FUNCTION__,
471                 name.c_str(), statusToString(status));
472         return nullptr;
473     }
474 
475     for (auto& conflictName : resourceCost.conflictingDevices) {
476         uint16_t major, minor;
477         std::string type, id;
478         status_t res = parseDeviceName(conflictName, &major, &minor, &type, &id);
479         if (res != OK) {
480             ALOGE("%s: Failed to parse conflicting device %s", __FUNCTION__, conflictName.c_str());
481             return nullptr;
482         }
483         conflictName = id;
484     }
485 
486     return std::unique_ptr<DeviceInfo3>(
487         new HidlDeviceInfo3(name, tagId, id, minorVersion, HalToFrameworkResourceCost(resourceCost),
488                 this, mProviderPublicCameraIds, cameraInterface));
489 }
490 
reCacheConcurrentStreamingCameraIdsLocked()491 status_t HidlProviderInfo::reCacheConcurrentStreamingCameraIdsLocked() {
492     if (mMinorVersion < 6) {
493       // Unsupported operation, nothing to do here
494       return OK;
495     }
496     // Check if the provider is currently active - not going to start it up for this notification
497     auto interface = mSavedInterface != nullptr ? mSavedInterface : mActiveInterface.promote();
498     if (interface == nullptr) {
499         ALOGE("%s: camera provider interface for %s is not valid", __FUNCTION__,
500                 mProviderName.c_str());
501         return INVALID_OPERATION;
502     }
503     auto castResult = provider::V2_6::ICameraProvider::castFrom(interface);
504 
505     if (castResult.isOk()) {
506         sp<provider::V2_6::ICameraProvider> interface2_6 = castResult;
507         if (interface2_6 != nullptr) {
508             return getConcurrentCameraIdsInternalLocked(interface2_6);
509         } else {
510             // This should not happen since mMinorVersion >= 6
511             ALOGE("%s: mMinorVersion was >= 6, but interface2_6 was nullptr", __FUNCTION__);
512             return UNKNOWN_ERROR;
513         }
514     }
515     return OK;
516 }
517 
getConcurrentCameraIdsInternalLocked(sp<provider::V2_6::ICameraProvider> & interface2_6)518 status_t HidlProviderInfo::getConcurrentCameraIdsInternalLocked(
519         sp<provider::V2_6::ICameraProvider> &interface2_6) {
520     if (interface2_6 == nullptr) {
521         ALOGE("%s: null interface provided", __FUNCTION__);
522         return BAD_VALUE;
523     }
524     Status status = Status::OK;
525     hardware::Return<void> ret =
526             interface2_6->getConcurrentStreamingCameraIds([&status, this](
527             Status concurrentIdStatus, // TODO: Move all instances of hidl_string to 'using'
528             const hardware::hidl_vec<hardware::hidl_vec<hardware::hidl_string>>&
529                         cameraDeviceIdCombinations) {
530             status = concurrentIdStatus;
531             if (status == Status::OK) {
532                 mConcurrentCameraIdCombinations.clear();
533                 for (auto& combination : cameraDeviceIdCombinations) {
534                     std::unordered_set<std::string> deviceIds;
535                     for (auto &cameraDeviceId : combination) {
536                         deviceIds.insert(cameraDeviceId.c_str());
537                     }
538                     mConcurrentCameraIdCombinations.push_back(std::move(deviceIds));
539                 }
540             } });
541     if (!ret.isOk()) {
542         ALOGE("%s: Transaction error in getting concurrent camera ID list from provider '%s'",
543                 __FUNCTION__, mProviderName.c_str());
544             return DEAD_OBJECT;
545     }
546     if (status != Status::OK) {
547         ALOGE("%s: Unable to query for camera devices from provider '%s'",
548                     __FUNCTION__, mProviderName.c_str());
549         return mapToStatusT(status);
550     }
551     return OK;
552 }
553 
HidlDeviceInfo3(const std::string & name,const metadata_vendor_id_t tagId,const std::string & id,uint16_t minorVersion,const CameraResourceCost & resourceCost,sp<CameraProviderManager::ProviderInfo> parentProvider,const std::vector<std::string> & publicCameraIds,sp<hardware::camera::device::V3_2::ICameraDevice> interface)554 HidlProviderInfo::HidlDeviceInfo3::HidlDeviceInfo3(
555         const std::string& name,
556         const metadata_vendor_id_t tagId,
557         const std::string &id, uint16_t minorVersion,
558         const CameraResourceCost& resourceCost,
559         sp<CameraProviderManager::ProviderInfo> parentProvider,
560         const std::vector<std::string>& publicCameraIds,
561         sp<hardware::camera::device::V3_2::ICameraDevice> interface) :
562         DeviceInfo3(name, tagId, id, minorVersion, resourceCost, parentProvider, publicCameraIds) {
563 
564     // Get camera characteristics and initialize flash unit availability
565     Status status;
566     hardware::Return<void> ret;
567     ret = interface->getCameraCharacteristics([&status, this](Status s,
568                     device::V3_2::CameraMetadata metadata) {
569                 status = s;
570                 if (s == Status::OK) {
571                     camera_metadata_t *buffer =
572                             reinterpret_cast<camera_metadata_t*>(metadata.data());
573                     size_t expectedSize = metadata.size();
574                     int res = validate_camera_metadata_structure(buffer, &expectedSize);
575                     if (res == OK || res == CAMERA_METADATA_VALIDATION_SHIFTED) {
576                         set_camera_metadata_vendor_id(buffer, mProviderTagid);
577                         mCameraCharacteristics = buffer;
578                     } else {
579                         ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
580                         status = Status::INTERNAL_ERROR;
581                     }
582                 }
583             });
584     if (!ret.isOk()) {
585         ALOGE("%s: Transaction error getting camera characteristics for device %s"
586                 " to check for a flash unit: %s", __FUNCTION__, id.c_str(),
587                 ret.description().c_str());
588         return;
589     }
590     if (status != Status::OK) {
591         ALOGE("%s: Unable to get camera characteristics for device %s: %s (%d)",
592                 __FUNCTION__, id.c_str(), statusToString(status), status);
593         return;
594     }
595 
596     if (mCameraCharacteristics.exists(ANDROID_INFO_DEVICE_STATE_ORIENTATIONS)) {
597         const auto &stateMap = mCameraCharacteristics.find(ANDROID_INFO_DEVICE_STATE_ORIENTATIONS);
598         if ((stateMap.count > 0) && ((stateMap.count % 2) == 0)) {
599             for (size_t i = 0; i < stateMap.count; i += 2) {
600                 mDeviceStateOrientationMap.emplace(stateMap.data.i64[i], stateMap.data.i64[i+1]);
601             }
602         } else {
603             ALOGW("%s: Invalid ANDROID_INFO_DEVICE_STATE_ORIENTATIONS map size: %zu", __FUNCTION__,
604                     stateMap.count);
605         }
606     }
607 
608     mSystemCameraKind = getSystemCameraKind();
609 
610     status_t res = fixupMonochromeTags();
611     if (OK != res) {
612         ALOGE("%s: Unable to fix up monochrome tags based for older HAL version: %s (%d)",
613                 __FUNCTION__, strerror(-res), res);
614         return;
615     }
616     auto stat = addDynamicDepthTags();
617     if (OK != stat) {
618         ALOGE("%s: Failed appending dynamic depth tags: %s (%d)", __FUNCTION__, strerror(-stat),
619                 stat);
620     }
621     res = deriveHeicTags();
622     if (OK != res) {
623         ALOGE("%s: Unable to derive HEIC tags based on camera and media capabilities: %s (%d)",
624                 __FUNCTION__, strerror(-res), res);
625     }
626 
627     if (SessionConfigurationUtils::supportsUltraHighResolutionCapture(mCameraCharacteristics)) {
628         status_t status = addDynamicDepthTags(/*maxResolution*/true);
629         if (OK != status) {
630             ALOGE("%s: Failed appending dynamic depth tags for maximum resolution mode: %s (%d)",
631                     __FUNCTION__, strerror(-status), status);
632         }
633 
634         status = deriveHeicTags(/*maxResolution*/true);
635         if (OK != status) {
636             ALOGE("%s: Unable to derive HEIC tags based on camera and media capabilities for"
637                     "maximum resolution mode: %s (%d)", __FUNCTION__, strerror(-status), status);
638         }
639     }
640 
641     res = addRotateCropTags();
642     if (OK != res) {
643         ALOGE("%s: Unable to add default SCALER_ROTATE_AND_CROP tags: %s (%d)", __FUNCTION__,
644                 strerror(-res), res);
645     }
646     res = addAutoframingTags();
647     if (OK != res) {
648         ALOGE("%s: Unable to add default AUTOFRAMING tags: %s (%d)", __FUNCTION__,
649                 strerror(-res), res);
650     }
651     res = addPreCorrectionActiveArraySize();
652     if (OK != res) {
653         ALOGE("%s: Unable to add PRE_CORRECTION_ACTIVE_ARRAY_SIZE: %s (%d)", __FUNCTION__,
654                 strerror(-res), res);
655     }
656     res = camera3::ZoomRatioMapper::overrideZoomRatioTags(
657             &mCameraCharacteristics, &mSupportNativeZoomRatio);
658     if (OK != res) {
659         ALOGE("%s: Unable to override zoomRatio related tags: %s (%d)",
660                 __FUNCTION__, strerror(-res), res);
661     }
662     res = addReadoutTimestampTag(/*readoutTimestampSupported*/false);
663     if (OK != res) {
664         ALOGE("%s: Unable to add sensorReadoutTimestamp tag: %s (%d)",
665                 __FUNCTION__, strerror(-res), res);
666     }
667 
668     camera_metadata_entry flashAvailable =
669             mCameraCharacteristics.find(ANDROID_FLASH_INFO_AVAILABLE);
670     if (flashAvailable.count == 1 &&
671             flashAvailable.data.u8[0] == ANDROID_FLASH_INFO_AVAILABLE_TRUE) {
672         mHasFlashUnit = true;
673         // Fix up flash strength tags for devices without these keys.
674         res = fixupTorchStrengthTags();
675         if (OK != res) {
676             ALOGE("%s: Unable to add default ANDROID_FLASH_INFO_STRENGTH_DEFAULT_LEVEL and"
677                     "ANDROID_FLASH_INFO_STRENGTH_MAXIMUM_LEVEL tags: %s (%d)", __FUNCTION__,
678                     strerror(-res), res);
679         }
680     } else {
681         mHasFlashUnit = false;
682     }
683 
684     camera_metadata_entry entry =
685             mCameraCharacteristics.find(ANDROID_FLASH_INFO_STRENGTH_DEFAULT_LEVEL);
686     if (entry.count == 1) {
687         mTorchDefaultStrengthLevel = entry.data.i32[0];
688     } else {
689         mTorchDefaultStrengthLevel = 0;
690     }
691     entry = mCameraCharacteristics.find(ANDROID_FLASH_INFO_STRENGTH_MAXIMUM_LEVEL);
692     if (entry.count == 1) {
693         mTorchMaximumStrengthLevel = entry.data.i32[0];
694     } else {
695         mTorchMaximumStrengthLevel = 0;
696     }
697 
698     mTorchStrengthLevel = 0;
699 
700     if (!kEnableLazyHal) {
701         // Save HAL reference indefinitely
702         mSavedInterface = interface;
703     }
704 
705     queryPhysicalCameraIds();
706 
707     // Get physical camera characteristics if applicable
708     auto castResult = device::V3_5::ICameraDevice::castFrom(interface);
709     if (!castResult.isOk()) {
710         ALOGV("%s: Unable to convert ICameraDevice instance to version 3.5", __FUNCTION__);
711         return;
712     }
713     sp<device::V3_5::ICameraDevice> interface_3_5 = castResult;
714     if (interface_3_5 == nullptr) {
715         ALOGE("%s: Converted ICameraDevice instance to nullptr", __FUNCTION__);
716         return;
717     }
718 
719     if (mIsLogicalCamera) {
720         for (auto& id : mPhysicalIds) {
721             if (std::find(mPublicCameraIds.begin(), mPublicCameraIds.end(), id) !=
722                     mPublicCameraIds.end()) {
723                 continue;
724             }
725 
726             hardware::hidl_string hidlId(id);
727             ret = interface_3_5->getPhysicalCameraCharacteristics(hidlId,
728                     [&status, &id, this](Status s, device::V3_2::CameraMetadata metadata) {
729                 status = s;
730                 if (s == Status::OK) {
731                     camera_metadata_t *buffer =
732                             reinterpret_cast<camera_metadata_t*>(metadata.data());
733                     size_t expectedSize = metadata.size();
734                     int res = validate_camera_metadata_structure(buffer, &expectedSize);
735                     if (res == OK || res == CAMERA_METADATA_VALIDATION_SHIFTED) {
736                         set_camera_metadata_vendor_id(buffer, mProviderTagid);
737                         mPhysicalCameraCharacteristics[id] = buffer;
738                     } else {
739                         ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
740                         status = Status::INTERNAL_ERROR;
741                     }
742                 }
743             });
744 
745             if (!ret.isOk()) {
746                 ALOGE("%s: Transaction error getting physical camera %s characteristics for %s: %s",
747                         __FUNCTION__, id.c_str(), id.c_str(), ret.description().c_str());
748                 return;
749             }
750             if (status != Status::OK) {
751                 ALOGE("%s: Unable to get physical camera %s characteristics for device %s: %s (%d)",
752                         __FUNCTION__, id.c_str(), mId.c_str(),
753                         statusToString(status), status);
754                 return;
755             }
756 
757             res = camera3::ZoomRatioMapper::overrideZoomRatioTags(
758                     &mPhysicalCameraCharacteristics[id], &mSupportNativeZoomRatio);
759             if (OK != res) {
760                 ALOGE("%s: Unable to override zoomRatio related tags: %s (%d)",
761                         __FUNCTION__, strerror(-res), res);
762             }
763         }
764     }
765 }
766 
setTorchMode(bool enabled)767 status_t HidlProviderInfo::HidlDeviceInfo3::setTorchMode(bool enabled) {
768     using hardware::camera::common::V1_0::TorchMode;
769     const sp<hardware::camera::device::V3_2::ICameraDevice> interface = startDeviceInterface();
770     Status s = interface->setTorchMode(enabled ? TorchMode::ON : TorchMode::OFF);
771     return mapToStatusT(s);
772 }
773 
turnOnTorchWithStrengthLevel(int32_t)774 status_t HidlProviderInfo::HidlDeviceInfo3::turnOnTorchWithStrengthLevel(
775         int32_t /*torchStrengthLevel*/) {
776     ALOGE("%s HIDL does not support turning on torch with variable strength", __FUNCTION__);
777     return INVALID_OPERATION;
778 }
779 
getTorchStrengthLevel(int32_t *)780 status_t HidlProviderInfo::HidlDeviceInfo3::getTorchStrengthLevel(int32_t * /*torchStrength*/) {
781     ALOGE("%s HIDL does not support variable torch strength level", __FUNCTION__);
782     return INVALID_OPERATION;
783 }
784 
785 sp<hardware::camera::device::V3_2::ICameraDevice>
startDeviceInterface()786 HidlProviderInfo::HidlDeviceInfo3::startDeviceInterface() {
787     Mutex::Autolock l(mDeviceAvailableLock);
788     sp<hardware::camera::device::V3_2::ICameraDevice> device;
789     ATRACE_CALL();
790     if (mSavedInterface == nullptr) {
791         sp<HidlProviderInfo> parentProvider =
792                 static_cast<HidlProviderInfo *>(mParentProvider.promote().get());
793         if (parentProvider != nullptr) {
794             // Wait for lazy HALs to confirm device availability
795             if (parentProvider->isExternalLazyHAL() && !mIsDeviceAvailable) {
796                 ALOGV("%s: Wait for external device to become available %s",
797                       __FUNCTION__,
798                       mId.c_str());
799 
800                 auto res = mDeviceAvailableSignal.waitRelative(mDeviceAvailableLock,
801                                                          kDeviceAvailableTimeout);
802                 if (res != OK) {
803                     ALOGE("%s: Failed waiting for device to become available",
804                           __FUNCTION__);
805                     return nullptr;
806                 }
807             }
808 
809             device = parentProvider->startDeviceInterface(mName);
810         }
811     } else {
812         device = (hardware::camera::device::V3_2::ICameraDevice *) mSavedInterface.get();
813     }
814     return device;
815 }
816 
dumpState(int fd)817 status_t HidlProviderInfo::HidlDeviceInfo3::dumpState(int fd) {
818     native_handle_t* handle = native_handle_create(1,0);
819     handle->data[0] = fd;
820     const sp<hardware::camera::device::V3_2::ICameraDevice> interface =
821             startDeviceInterface();
822     if (interface == nullptr) {
823         return DEAD_OBJECT;
824     }
825     auto ret = interface->dumpState(handle);
826     native_handle_delete(handle);
827     if (!ret.isOk()) {
828         return INVALID_OPERATION;
829     }
830     return OK;
831 }
832 
isSessionConfigurationSupported(const SessionConfiguration & configuration,bool overrideForPerfClass,metadataGetter getMetadata,bool * status)833 status_t HidlProviderInfo::HidlDeviceInfo3::isSessionConfigurationSupported(
834         const SessionConfiguration &configuration, bool overrideForPerfClass,
835         metadataGetter getMetadata, bool *status) {
836 
837     hardware::camera::device::V3_7::StreamConfiguration configuration_3_7;
838     bool earlyExit = false;
839     auto bRes = SessionConfigurationUtils::convertToHALStreamCombination(configuration,
840             String8(mId.c_str()), mCameraCharacteristics, getMetadata, mPhysicalIds,
841             configuration_3_7, overrideForPerfClass, &earlyExit);
842 
843     if (!bRes.isOk()) {
844         return UNKNOWN_ERROR;
845     }
846 
847     if (earlyExit) {
848         *status = false;
849         return OK;
850     }
851 
852     const sp<hardware::camera::device::V3_2::ICameraDevice> interface =
853             startDeviceInterface();
854 
855     if (interface == nullptr) {
856         return DEAD_OBJECT;
857     }
858 
859     auto castResult_3_5 = device::V3_5::ICameraDevice::castFrom(interface);
860     sp<hardware::camera::device::V3_5::ICameraDevice> interface_3_5 = castResult_3_5;
861     auto castResult_3_7 = device::V3_7::ICameraDevice::castFrom(interface);
862     sp<hardware::camera::device::V3_7::ICameraDevice> interface_3_7 = castResult_3_7;
863 
864     status_t res;
865     Status callStatus;
866     ::android::hardware::Return<void> ret;
867     auto halCb =
868             [&callStatus, &status] (Status s, bool combStatus) {
869                 callStatus = s;
870                 *status = combStatus;
871             };
872     if (interface_3_7 != nullptr) {
873         ret = interface_3_7->isStreamCombinationSupported_3_7(configuration_3_7, halCb);
874     } else if (interface_3_5 != nullptr) {
875         hardware::camera::device::V3_4::StreamConfiguration configuration_3_4;
876         bool success = SessionConfigurationUtils::convertHALStreamCombinationFromV37ToV34(
877                 configuration_3_4, configuration_3_7);
878         if (!success) {
879             *status = false;
880             return OK;
881         }
882         ret = interface_3_5->isStreamCombinationSupported(configuration_3_4, halCb);
883     } else {
884         return INVALID_OPERATION;
885     }
886     if (ret.isOk()) {
887         switch (callStatus) {
888             case Status::OK:
889                 // Expected case, do nothing.
890                 res = OK;
891                 break;
892             case Status::METHOD_NOT_SUPPORTED:
893                 res = INVALID_OPERATION;
894                 break;
895             default:
896                 ALOGE("%s: Session configuration query failed: %d", __FUNCTION__, callStatus);
897                 res = UNKNOWN_ERROR;
898         }
899     } else {
900         ALOGE("%s: Unexpected binder error: %s", __FUNCTION__, ret.description().c_str());
901         res = UNKNOWN_ERROR;
902     }
903 
904     return res;
905 }
906 
convertToHALStreamCombinationAndCameraIdsLocked(const std::vector<CameraIdAndSessionConfiguration> & cameraIdsAndSessionConfigs,const std::set<std::string> & perfClassPrimaryCameraIds,int targetSdkVersion,hardware::hidl_vec<CameraIdAndStreamCombination> * halCameraIdsAndStreamCombinations,bool * earlyExit)907 status_t HidlProviderInfo::convertToHALStreamCombinationAndCameraIdsLocked(
908         const std::vector<CameraIdAndSessionConfiguration> &cameraIdsAndSessionConfigs,
909         const std::set<std::string>& perfClassPrimaryCameraIds,
910         int targetSdkVersion,
911         hardware::hidl_vec<CameraIdAndStreamCombination> *halCameraIdsAndStreamCombinations,
912         bool *earlyExit) {
913     binder::Status bStatus = binder::Status::ok();
914     std::vector<CameraIdAndStreamCombination> halCameraIdsAndStreamsV;
915     bool shouldExit = false;
916     status_t res = OK;
917     for (auto &cameraIdAndSessionConfig : cameraIdsAndSessionConfigs) {
918         const std::string& cameraId = cameraIdAndSessionConfig.mCameraId;
919         hardware::camera::device::V3_7::StreamConfiguration streamConfiguration;
920         CameraMetadata deviceInfo;
921         bool overrideForPerfClass =
922                 SessionConfigurationUtils::targetPerfClassPrimaryCamera(
923                         perfClassPrimaryCameraIds, cameraId, targetSdkVersion);
924         res = mManager->getCameraCharacteristicsLocked(cameraId, overrideForPerfClass, &deviceInfo,
925                 /*overrideToPortrait*/false);
926         if (res != OK) {
927             return res;
928         }
929         camera3::metadataGetter getMetadata =
930                 [this](const String8 &id, bool overrideForPerfClass) {
931                     CameraMetadata physicalDeviceInfo;
932                     mManager->getCameraCharacteristicsLocked(id.string(), overrideForPerfClass,
933                             &physicalDeviceInfo, /*overrideToPortrait*/false);
934                     return physicalDeviceInfo;
935                 };
936         std::vector<std::string> physicalCameraIds;
937         mManager->isLogicalCameraLocked(cameraId, &physicalCameraIds);
938         bStatus =
939             SessionConfigurationUtils::convertToHALStreamCombination(
940                     cameraIdAndSessionConfig.mSessionConfiguration,
941                     String8(cameraId.c_str()), deviceInfo, getMetadata,
942                     physicalCameraIds, streamConfiguration,
943                     overrideForPerfClass, &shouldExit);
944         if (!bStatus.isOk()) {
945             ALOGE("%s: convertToHALStreamCombination failed", __FUNCTION__);
946             return INVALID_OPERATION;
947         }
948         if (shouldExit) {
949             *earlyExit = true;
950             return OK;
951         }
952         CameraIdAndStreamCombination halCameraIdAndStream;
953         halCameraIdAndStream.cameraId = cameraId;
954         halCameraIdAndStream.streamConfiguration = streamConfiguration;
955         halCameraIdsAndStreamsV.push_back(halCameraIdAndStream);
956     }
957     *halCameraIdsAndStreamCombinations = halCameraIdsAndStreamsV;
958     return OK;
959 }
960 
isConcurrentSessionConfigurationSupported(const std::vector<CameraIdAndSessionConfiguration> & cameraIdsAndSessionConfigs,const std::set<std::string> & perfClassPrimaryCameraIds,int targetSdkVersion,bool * isSupported)961 status_t HidlProviderInfo::isConcurrentSessionConfigurationSupported(
962         const std::vector<CameraIdAndSessionConfiguration> &cameraIdsAndSessionConfigs,
963         const std::set<std::string>& perfClassPrimaryCameraIds,
964         int targetSdkVersion, bool *isSupported) {
965 
966       hardware::hidl_vec<CameraIdAndStreamCombination> halCameraIdsAndStreamCombinations;
967       bool knowUnsupported = false;
968       status_t res = convertToHALStreamCombinationAndCameraIdsLocked(
969               cameraIdsAndSessionConfigs, perfClassPrimaryCameraIds,
970               targetSdkVersion, &halCameraIdsAndStreamCombinations, &knowUnsupported);
971       if (res != OK) {
972           ALOGE("%s unable to convert session configurations provided to HAL stream"
973                 "combinations", __FUNCTION__);
974           return res;
975       }
976       if (knowUnsupported) {
977           // We got to know the streams aren't valid before doing the HAL
978           // call itself.
979           *isSupported = false;
980           return OK;
981       }
982 
983     if (mMinorVersion >= 6) {
984         // Check if the provider is currently active - not going to start it for this notification
985         auto interface = mSavedInterface != nullptr ? mSavedInterface : mActiveInterface.promote();
986         if (interface == nullptr) {
987             // TODO: This might be some other problem
988             return INVALID_OPERATION;
989         }
990         auto castResult2_6 = provider::V2_6::ICameraProvider::castFrom(interface);
991         auto castResult2_7 = provider::V2_7::ICameraProvider::castFrom(interface);
992         Status callStatus;
993         auto cb =
994                 [&isSupported, &callStatus](Status s, bool supported) {
995                       callStatus = s;
996                       *isSupported = supported; };
997 
998         ::android::hardware::Return<void> ret;
999         sp<provider::V2_7::ICameraProvider> interface_2_7;
1000         sp<provider::V2_6::ICameraProvider> interface_2_6;
1001         if (mMinorVersion >= 7 && castResult2_7.isOk()) {
1002             interface_2_7 = castResult2_7;
1003             if (interface_2_7 != nullptr) {
1004                 ret = interface_2_7->isConcurrentStreamCombinationSupported_2_7(
1005                         halCameraIdsAndStreamCombinations, cb);
1006             }
1007         } else if (mMinorVersion == 6 && castResult2_6.isOk()) {
1008             interface_2_6 = castResult2_6;
1009             if (interface_2_6 != nullptr) {
1010                 hardware::hidl_vec<provider::V2_6::CameraIdAndStreamCombination>
1011                         halCameraIdsAndStreamCombinations_2_6;
1012                 size_t numStreams = halCameraIdsAndStreamCombinations.size();
1013                 halCameraIdsAndStreamCombinations_2_6.resize(numStreams);
1014                 for (size_t i = 0; i < numStreams; i++) {
1015                     using namespace camera3;
1016                     auto const& combination = halCameraIdsAndStreamCombinations[i];
1017                     halCameraIdsAndStreamCombinations_2_6[i].cameraId = combination.cameraId;
1018                     bool success =
1019                             SessionConfigurationUtils::convertHALStreamCombinationFromV37ToV34(
1020                                     halCameraIdsAndStreamCombinations_2_6[i].streamConfiguration,
1021                                     combination.streamConfiguration);
1022                     if (!success) {
1023                         *isSupported = false;
1024                         return OK;
1025                     }
1026                 }
1027                 ret = interface_2_6->isConcurrentStreamCombinationSupported(
1028                         halCameraIdsAndStreamCombinations_2_6, cb);
1029             }
1030         }
1031 
1032         if (interface_2_7 != nullptr || interface_2_6 != nullptr) {
1033             if (ret.isOk()) {
1034                 switch (callStatus) {
1035                     case Status::OK:
1036                         // Expected case, do nothing.
1037                         res = OK;
1038                         break;
1039                     case Status::METHOD_NOT_SUPPORTED:
1040                         res = INVALID_OPERATION;
1041                         break;
1042                     default:
1043                         ALOGE("%s: Session configuration query failed: %d", __FUNCTION__,
1044                                   callStatus);
1045                         res = UNKNOWN_ERROR;
1046                 }
1047             } else {
1048                 ALOGE("%s: Unexpected binder error: %s", __FUNCTION__, ret.description().c_str());
1049                 res = UNKNOWN_ERROR;
1050             }
1051             return res;
1052         }
1053     }
1054     // unsupported operation
1055     return INVALID_OPERATION;
1056 }
1057 
1058 } //namespace android
1059