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(mProviderName);
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 const wp<hidl::base::V1_0::IBase>& who) {
446 (void) who;
447 ALOGI("Camera provider '%s' has died; removing it", mProviderInstance.c_str());
448 if (cookie != mId) {
449 ALOGW("%s: Unexpected serviceDied cookie %" PRIu64 ", expected %" PRIu32,
450 __FUNCTION__, cookie, mId);
451 }
452 mManager->removeProvider(mProviderInstance);
453 }
454
455 std::unique_ptr<CameraProviderManager::ProviderInfo::DeviceInfo>
initializeDeviceInfo(const std::string & name,const metadata_vendor_id_t tagId,const std::string & id,uint16_t minorVersion)456 HidlProviderInfo::initializeDeviceInfo(
457 const std::string &name, const metadata_vendor_id_t tagId,
458 const std::string &id, uint16_t minorVersion) {
459 Status status;
460
461 auto cameraInterface = startDeviceInterface(name);
462 if (cameraInterface == nullptr) return nullptr;
463
464 common::V1_0::CameraResourceCost resourceCost;
465 cameraInterface->getResourceCost([&status, &resourceCost](
466 Status s, common::V1_0::CameraResourceCost cost) {
467 status = s;
468 resourceCost = cost;
469 });
470 if (status != Status::OK) {
471 ALOGE("%s: Unable to obtain resource costs for camera device %s: %s", __FUNCTION__,
472 name.c_str(), statusToString(status));
473 return nullptr;
474 }
475
476 for (auto& conflictName : resourceCost.conflictingDevices) {
477 uint16_t major, minor;
478 std::string type, id;
479 status_t res = parseDeviceName(conflictName, &major, &minor, &type, &id);
480 if (res != OK) {
481 ALOGE("%s: Failed to parse conflicting device %s", __FUNCTION__, conflictName.c_str());
482 return nullptr;
483 }
484 conflictName = id;
485 }
486
487 return std::unique_ptr<DeviceInfo3>(
488 new HidlDeviceInfo3(name, tagId, id, minorVersion, HalToFrameworkResourceCost(resourceCost),
489 this, mProviderPublicCameraIds, cameraInterface));
490 }
491
reCacheConcurrentStreamingCameraIdsLocked()492 status_t HidlProviderInfo::reCacheConcurrentStreamingCameraIdsLocked() {
493 if (mMinorVersion < 6) {
494 // Unsupported operation, nothing to do here
495 return OK;
496 }
497 // Check if the provider is currently active - not going to start it up for this notification
498 auto interface = mSavedInterface != nullptr ? mSavedInterface : mActiveInterface.promote();
499 if (interface == nullptr) {
500 ALOGE("%s: camera provider interface for %s is not valid", __FUNCTION__,
501 mProviderName.c_str());
502 return INVALID_OPERATION;
503 }
504 auto castResult = provider::V2_6::ICameraProvider::castFrom(interface);
505
506 if (castResult.isOk()) {
507 sp<provider::V2_6::ICameraProvider> interface2_6 = castResult;
508 if (interface2_6 != nullptr) {
509 return getConcurrentCameraIdsInternalLocked(interface2_6);
510 } else {
511 // This should not happen since mMinorVersion >= 6
512 ALOGE("%s: mMinorVersion was >= 6, but interface2_6 was nullptr", __FUNCTION__);
513 return UNKNOWN_ERROR;
514 }
515 }
516 return OK;
517 }
518
getConcurrentCameraIdsInternalLocked(sp<provider::V2_6::ICameraProvider> & interface2_6)519 status_t HidlProviderInfo::getConcurrentCameraIdsInternalLocked(
520 sp<provider::V2_6::ICameraProvider> &interface2_6) {
521 if (interface2_6 == nullptr) {
522 ALOGE("%s: null interface provided", __FUNCTION__);
523 return BAD_VALUE;
524 }
525 Status status = Status::OK;
526 hardware::Return<void> ret =
527 interface2_6->getConcurrentStreamingCameraIds([&status, this](
528 Status concurrentIdStatus, // TODO: Move all instances of hidl_string to 'using'
529 const hardware::hidl_vec<hardware::hidl_vec<hardware::hidl_string>>&
530 cameraDeviceIdCombinations) {
531 status = concurrentIdStatus;
532 if (status == Status::OK) {
533 mConcurrentCameraIdCombinations.clear();
534 for (auto& combination : cameraDeviceIdCombinations) {
535 std::unordered_set<std::string> deviceIds;
536 for (auto &cameraDeviceId : combination) {
537 deviceIds.insert(cameraDeviceId.c_str());
538 }
539 mConcurrentCameraIdCombinations.push_back(std::move(deviceIds));
540 }
541 } });
542 if (!ret.isOk()) {
543 ALOGE("%s: Transaction error in getting concurrent camera ID list from provider '%s'",
544 __FUNCTION__, mProviderName.c_str());
545 return DEAD_OBJECT;
546 }
547 if (status != Status::OK) {
548 ALOGE("%s: Unable to query for camera devices from provider '%s'",
549 __FUNCTION__, mProviderName.c_str());
550 return mapToStatusT(status);
551 }
552 return OK;
553 }
554
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)555 HidlProviderInfo::HidlDeviceInfo3::HidlDeviceInfo3(
556 const std::string& name,
557 const metadata_vendor_id_t tagId,
558 const std::string &id, uint16_t minorVersion,
559 const CameraResourceCost& resourceCost,
560 sp<CameraProviderManager::ProviderInfo> parentProvider,
561 const std::vector<std::string>& publicCameraIds,
562 sp<hardware::camera::device::V3_2::ICameraDevice> interface) :
563 DeviceInfo3(name, tagId, id, minorVersion, resourceCost, parentProvider, publicCameraIds) {
564
565 // Get camera characteristics and initialize flash unit availability
566 Status status;
567 hardware::Return<void> ret;
568 ret = interface->getCameraCharacteristics([&status, this](Status s,
569 device::V3_2::CameraMetadata metadata) {
570 status = s;
571 if (s == Status::OK) {
572 camera_metadata_t *buffer =
573 reinterpret_cast<camera_metadata_t*>(metadata.data());
574 size_t expectedSize = metadata.size();
575 int res = validate_camera_metadata_structure(buffer, &expectedSize);
576 if (res == OK || res == CAMERA_METADATA_VALIDATION_SHIFTED) {
577 set_camera_metadata_vendor_id(buffer, mProviderTagid);
578 mCameraCharacteristics = buffer;
579 } else {
580 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
581 status = Status::INTERNAL_ERROR;
582 }
583 }
584 });
585 if (!ret.isOk()) {
586 ALOGE("%s: Transaction error getting camera characteristics for device %s"
587 " to check for a flash unit: %s", __FUNCTION__, id.c_str(),
588 ret.description().c_str());
589 return;
590 }
591 if (status != Status::OK) {
592 ALOGE("%s: Unable to get camera characteristics for device %s: %s (%d)",
593 __FUNCTION__, id.c_str(), statusToString(status), status);
594 return;
595 }
596
597 if (mCameraCharacteristics.exists(ANDROID_INFO_DEVICE_STATE_ORIENTATIONS)) {
598 const auto &stateMap = mCameraCharacteristics.find(ANDROID_INFO_DEVICE_STATE_ORIENTATIONS);
599 if ((stateMap.count > 0) && ((stateMap.count % 2) == 0)) {
600 for (size_t i = 0; i < stateMap.count; i += 2) {
601 mDeviceStateOrientationMap.emplace(stateMap.data.i64[i], stateMap.data.i64[i+1]);
602 }
603 } else {
604 ALOGW("%s: Invalid ANDROID_INFO_DEVICE_STATE_ORIENTATIONS map size: %zu", __FUNCTION__,
605 stateMap.count);
606 }
607 }
608
609 mSystemCameraKind = getSystemCameraKind();
610
611 status_t res = fixupMonochromeTags();
612 if (OK != res) {
613 ALOGE("%s: Unable to fix up monochrome tags based for older HAL version: %s (%d)",
614 __FUNCTION__, strerror(-res), res);
615 return;
616 }
617 auto stat = addDynamicDepthTags();
618 if (OK != stat) {
619 ALOGE("%s: Failed appending dynamic depth tags: %s (%d)", __FUNCTION__, strerror(-stat),
620 stat);
621 }
622 res = deriveHeicTags();
623 if (OK != res) {
624 ALOGE("%s: Unable to derive HEIC tags based on camera and media capabilities: %s (%d)",
625 __FUNCTION__, strerror(-res), res);
626 }
627
628 if (SessionConfigurationUtils::isUltraHighResolutionSensor(mCameraCharacteristics)) {
629 status_t status = addDynamicDepthTags(/*maxResolution*/true);
630 if (OK != status) {
631 ALOGE("%s: Failed appending dynamic depth tags for maximum resolution mode: %s (%d)",
632 __FUNCTION__, strerror(-status), status);
633 }
634
635 status = deriveHeicTags(/*maxResolution*/true);
636 if (OK != status) {
637 ALOGE("%s: Unable to derive HEIC tags based on camera and media capabilities for"
638 "maximum resolution mode: %s (%d)", __FUNCTION__, strerror(-status), status);
639 }
640 }
641
642 res = addRotateCropTags();
643 if (OK != res) {
644 ALOGE("%s: Unable to add default SCALER_ROTATE_AND_CROP tags: %s (%d)", __FUNCTION__,
645 strerror(-res), res);
646 }
647 res = addPreCorrectionActiveArraySize();
648 if (OK != res) {
649 ALOGE("%s: Unable to add PRE_CORRECTION_ACTIVE_ARRAY_SIZE: %s (%d)", __FUNCTION__,
650 strerror(-res), res);
651 }
652 res = camera3::ZoomRatioMapper::overrideZoomRatioTags(
653 &mCameraCharacteristics, &mSupportNativeZoomRatio);
654 if (OK != res) {
655 ALOGE("%s: Unable to override zoomRatio related tags: %s (%d)",
656 __FUNCTION__, strerror(-res), res);
657 }
658 res = addReadoutTimestampTag(/*readoutTimestampSupported*/false);
659 if (OK != res) {
660 ALOGE("%s: Unable to add sensorReadoutTimestamp tag: %s (%d)",
661 __FUNCTION__, strerror(-res), res);
662 }
663
664 camera_metadata_entry flashAvailable =
665 mCameraCharacteristics.find(ANDROID_FLASH_INFO_AVAILABLE);
666 if (flashAvailable.count == 1 &&
667 flashAvailable.data.u8[0] == ANDROID_FLASH_INFO_AVAILABLE_TRUE) {
668 mHasFlashUnit = true;
669 // Fix up flash strength tags for devices without these keys.
670 res = fixupTorchStrengthTags();
671 if (OK != res) {
672 ALOGE("%s: Unable to add default ANDROID_FLASH_INFO_STRENGTH_DEFAULT_LEVEL and"
673 "ANDROID_FLASH_INFO_STRENGTH_MAXIMUM_LEVEL tags: %s (%d)", __FUNCTION__,
674 strerror(-res), res);
675 }
676 } else {
677 mHasFlashUnit = false;
678 }
679
680 camera_metadata_entry entry =
681 mCameraCharacteristics.find(ANDROID_FLASH_INFO_STRENGTH_DEFAULT_LEVEL);
682 if (entry.count == 1) {
683 mTorchDefaultStrengthLevel = entry.data.i32[0];
684 } else {
685 mTorchDefaultStrengthLevel = 0;
686 }
687 entry = mCameraCharacteristics.find(ANDROID_FLASH_INFO_STRENGTH_MAXIMUM_LEVEL);
688 if (entry.count == 1) {
689 mTorchMaximumStrengthLevel = entry.data.i32[0];
690 } else {
691 mTorchMaximumStrengthLevel = 0;
692 }
693
694 mTorchStrengthLevel = 0;
695
696 queryPhysicalCameraIds();
697
698 // Get physical camera characteristics if applicable
699 auto castResult = device::V3_5::ICameraDevice::castFrom(interface);
700 if (!castResult.isOk()) {
701 ALOGV("%s: Unable to convert ICameraDevice instance to version 3.5", __FUNCTION__);
702 return;
703 }
704 sp<device::V3_5::ICameraDevice> interface_3_5 = castResult;
705 if (interface_3_5 == nullptr) {
706 ALOGE("%s: Converted ICameraDevice instance to nullptr", __FUNCTION__);
707 return;
708 }
709
710 if (mIsLogicalCamera) {
711 for (auto& id : mPhysicalIds) {
712 if (std::find(mPublicCameraIds.begin(), mPublicCameraIds.end(), id) !=
713 mPublicCameraIds.end()) {
714 continue;
715 }
716
717 hardware::hidl_string hidlId(id);
718 ret = interface_3_5->getPhysicalCameraCharacteristics(hidlId,
719 [&status, &id, this](Status s, device::V3_2::CameraMetadata metadata) {
720 status = s;
721 if (s == Status::OK) {
722 camera_metadata_t *buffer =
723 reinterpret_cast<camera_metadata_t*>(metadata.data());
724 size_t expectedSize = metadata.size();
725 int res = validate_camera_metadata_structure(buffer, &expectedSize);
726 if (res == OK || res == CAMERA_METADATA_VALIDATION_SHIFTED) {
727 set_camera_metadata_vendor_id(buffer, mProviderTagid);
728 mPhysicalCameraCharacteristics[id] = buffer;
729 } else {
730 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
731 status = Status::INTERNAL_ERROR;
732 }
733 }
734 });
735
736 if (!ret.isOk()) {
737 ALOGE("%s: Transaction error getting physical camera %s characteristics for %s: %s",
738 __FUNCTION__, id.c_str(), id.c_str(), ret.description().c_str());
739 return;
740 }
741 if (status != Status::OK) {
742 ALOGE("%s: Unable to get physical camera %s characteristics for device %s: %s (%d)",
743 __FUNCTION__, id.c_str(), mId.c_str(),
744 statusToString(status), status);
745 return;
746 }
747
748 res = camera3::ZoomRatioMapper::overrideZoomRatioTags(
749 &mPhysicalCameraCharacteristics[id], &mSupportNativeZoomRatio);
750 if (OK != res) {
751 ALOGE("%s: Unable to override zoomRatio related tags: %s (%d)",
752 __FUNCTION__, strerror(-res), res);
753 }
754 }
755 }
756
757 if (!kEnableLazyHal) {
758 // Save HAL reference indefinitely
759 mSavedInterface = interface;
760 }
761
762
763 }
764
setTorchMode(bool enabled)765 status_t HidlProviderInfo::HidlDeviceInfo3::setTorchMode(bool enabled) {
766 using hardware::camera::common::V1_0::TorchMode;
767 const sp<hardware::camera::device::V3_2::ICameraDevice> interface = startDeviceInterface();
768 Status s = interface->setTorchMode(enabled ? TorchMode::ON : TorchMode::OFF);
769 return mapToStatusT(s);
770 }
771
turnOnTorchWithStrengthLevel(int32_t)772 status_t HidlProviderInfo::HidlDeviceInfo3::turnOnTorchWithStrengthLevel(
773 int32_t /*torchStrengthLevel*/) {
774 ALOGE("%s HIDL does not support turning on torch with variable strength", __FUNCTION__);
775 return INVALID_OPERATION;
776 }
777
getTorchStrengthLevel(int32_t *)778 status_t HidlProviderInfo::HidlDeviceInfo3::getTorchStrengthLevel(int32_t * /*torchStrength*/) {
779 ALOGE("%s HIDL does not support variable torch strength level", __FUNCTION__);
780 return INVALID_OPERATION;
781 }
782
783 sp<hardware::camera::device::V3_2::ICameraDevice>
startDeviceInterface()784 HidlProviderInfo::HidlDeviceInfo3::startDeviceInterface() {
785 Mutex::Autolock l(mDeviceAvailableLock);
786 sp<hardware::camera::device::V3_2::ICameraDevice> device;
787 ATRACE_CALL();
788 if (mSavedInterface == nullptr) {
789 sp<HidlProviderInfo> parentProvider =
790 static_cast<HidlProviderInfo *>(mParentProvider.promote().get());
791 if (parentProvider != nullptr) {
792 // Wait for lazy HALs to confirm device availability
793 if (parentProvider->isExternalLazyHAL() && !mIsDeviceAvailable) {
794 ALOGV("%s: Wait for external device to become available %s",
795 __FUNCTION__,
796 mId.c_str());
797
798 auto res = mDeviceAvailableSignal.waitRelative(mDeviceAvailableLock,
799 kDeviceAvailableTimeout);
800 if (res != OK) {
801 ALOGE("%s: Failed waiting for device to become available",
802 __FUNCTION__);
803 return nullptr;
804 }
805 }
806
807 device = parentProvider->startDeviceInterface(mName);
808 }
809 } else {
810 device = (hardware::camera::device::V3_2::ICameraDevice *) mSavedInterface.get();
811 }
812 return device;
813 }
814
dumpState(int fd)815 status_t HidlProviderInfo::HidlDeviceInfo3::dumpState(int fd) {
816 native_handle_t* handle = native_handle_create(1,0);
817 handle->data[0] = fd;
818 const sp<hardware::camera::device::V3_2::ICameraDevice> interface =
819 startDeviceInterface();
820 if (interface == nullptr) {
821 return DEAD_OBJECT;
822 }
823 auto ret = interface->dumpState(handle);
824 native_handle_delete(handle);
825 if (!ret.isOk()) {
826 return INVALID_OPERATION;
827 }
828 return OK;
829 }
830
isSessionConfigurationSupported(const SessionConfiguration & configuration,bool overrideForPerfClass,metadataGetter getMetadata,bool * status)831 status_t HidlProviderInfo::HidlDeviceInfo3::isSessionConfigurationSupported(
832 const SessionConfiguration &configuration, bool overrideForPerfClass,
833 metadataGetter getMetadata, bool *status) {
834
835 hardware::camera::device::V3_7::StreamConfiguration configuration_3_7;
836 bool earlyExit = false;
837 auto bRes = SessionConfigurationUtils::convertToHALStreamCombination(configuration,
838 String8(mId.c_str()), mCameraCharacteristics, getMetadata, mPhysicalIds,
839 configuration_3_7, overrideForPerfClass, &earlyExit);
840
841 if (!bRes.isOk()) {
842 return UNKNOWN_ERROR;
843 }
844
845 if (earlyExit) {
846 *status = false;
847 return OK;
848 }
849
850 const sp<hardware::camera::device::V3_2::ICameraDevice> interface =
851 startDeviceInterface();
852
853 if (interface == nullptr) {
854 return DEAD_OBJECT;
855 }
856
857 auto castResult_3_5 = device::V3_5::ICameraDevice::castFrom(interface);
858 sp<hardware::camera::device::V3_5::ICameraDevice> interface_3_5 = castResult_3_5;
859 auto castResult_3_7 = device::V3_7::ICameraDevice::castFrom(interface);
860 sp<hardware::camera::device::V3_7::ICameraDevice> interface_3_7 = castResult_3_7;
861
862 status_t res;
863 Status callStatus;
864 ::android::hardware::Return<void> ret;
865 auto halCb =
866 [&callStatus, &status] (Status s, bool combStatus) {
867 callStatus = s;
868 *status = combStatus;
869 };
870 if (interface_3_7 != nullptr) {
871 ret = interface_3_7->isStreamCombinationSupported_3_7(configuration_3_7, halCb);
872 } else if (interface_3_5 != nullptr) {
873 hardware::camera::device::V3_4::StreamConfiguration configuration_3_4;
874 bool success = SessionConfigurationUtils::convertHALStreamCombinationFromV37ToV34(
875 configuration_3_4, configuration_3_7);
876 if (!success) {
877 *status = false;
878 return OK;
879 }
880 ret = interface_3_5->isStreamCombinationSupported(configuration_3_4, halCb);
881 } else {
882 return INVALID_OPERATION;
883 }
884 if (ret.isOk()) {
885 switch (callStatus) {
886 case Status::OK:
887 // Expected case, do nothing.
888 res = OK;
889 break;
890 case Status::METHOD_NOT_SUPPORTED:
891 res = INVALID_OPERATION;
892 break;
893 default:
894 ALOGE("%s: Session configuration query failed: %d", __FUNCTION__, callStatus);
895 res = UNKNOWN_ERROR;
896 }
897 } else {
898 ALOGE("%s: Unexpected binder error: %s", __FUNCTION__, ret.description().c_str());
899 res = UNKNOWN_ERROR;
900 }
901
902 return res;
903 }
904
convertToHALStreamCombinationAndCameraIdsLocked(const std::vector<CameraIdAndSessionConfiguration> & cameraIdsAndSessionConfigs,const std::set<std::string> & perfClassPrimaryCameraIds,int targetSdkVersion,hardware::hidl_vec<CameraIdAndStreamCombination> * halCameraIdsAndStreamCombinations,bool * earlyExit)905 status_t HidlProviderInfo::convertToHALStreamCombinationAndCameraIdsLocked(
906 const std::vector<CameraIdAndSessionConfiguration> &cameraIdsAndSessionConfigs,
907 const std::set<std::string>& perfClassPrimaryCameraIds,
908 int targetSdkVersion,
909 hardware::hidl_vec<CameraIdAndStreamCombination> *halCameraIdsAndStreamCombinations,
910 bool *earlyExit) {
911 binder::Status bStatus = binder::Status::ok();
912 std::vector<CameraIdAndStreamCombination> halCameraIdsAndStreamsV;
913 bool shouldExit = false;
914 status_t res = OK;
915 for (auto &cameraIdAndSessionConfig : cameraIdsAndSessionConfigs) {
916 const std::string& cameraId = cameraIdAndSessionConfig.mCameraId;
917 hardware::camera::device::V3_7::StreamConfiguration streamConfiguration;
918 CameraMetadata deviceInfo;
919 bool overrideForPerfClass =
920 SessionConfigurationUtils::targetPerfClassPrimaryCamera(
921 perfClassPrimaryCameraIds, cameraId, targetSdkVersion);
922 res = mManager->getCameraCharacteristicsLocked(cameraId, overrideForPerfClass, &deviceInfo,
923 /*overrideToPortrait*/false);
924 if (res != OK) {
925 return res;
926 }
927 camera3::metadataGetter getMetadata =
928 [this](const String8 &id, bool overrideForPerfClass) {
929 CameraMetadata physicalDeviceInfo;
930 mManager->getCameraCharacteristicsLocked(id.string(), overrideForPerfClass,
931 &physicalDeviceInfo, /*overrideToPortrait*/false);
932 return physicalDeviceInfo;
933 };
934 std::vector<std::string> physicalCameraIds;
935 mManager->isLogicalCameraLocked(cameraId, &physicalCameraIds);
936 bStatus =
937 SessionConfigurationUtils::convertToHALStreamCombination(
938 cameraIdAndSessionConfig.mSessionConfiguration,
939 String8(cameraId.c_str()), deviceInfo, getMetadata,
940 physicalCameraIds, streamConfiguration,
941 overrideForPerfClass, &shouldExit);
942 if (!bStatus.isOk()) {
943 ALOGE("%s: convertToHALStreamCombination failed", __FUNCTION__);
944 return INVALID_OPERATION;
945 }
946 if (shouldExit) {
947 *earlyExit = true;
948 return OK;
949 }
950 CameraIdAndStreamCombination halCameraIdAndStream;
951 halCameraIdAndStream.cameraId = cameraId;
952 halCameraIdAndStream.streamConfiguration = streamConfiguration;
953 halCameraIdsAndStreamsV.push_back(halCameraIdAndStream);
954 }
955 *halCameraIdsAndStreamCombinations = halCameraIdsAndStreamsV;
956 return OK;
957 }
958
isConcurrentSessionConfigurationSupported(const std::vector<CameraIdAndSessionConfiguration> & cameraIdsAndSessionConfigs,const std::set<std::string> & perfClassPrimaryCameraIds,int targetSdkVersion,bool * isSupported)959 status_t HidlProviderInfo::isConcurrentSessionConfigurationSupported(
960 const std::vector<CameraIdAndSessionConfiguration> &cameraIdsAndSessionConfigs,
961 const std::set<std::string>& perfClassPrimaryCameraIds,
962 int targetSdkVersion, bool *isSupported) {
963
964 hardware::hidl_vec<CameraIdAndStreamCombination> halCameraIdsAndStreamCombinations;
965 bool knowUnsupported = false;
966 status_t res = convertToHALStreamCombinationAndCameraIdsLocked(
967 cameraIdsAndSessionConfigs, perfClassPrimaryCameraIds,
968 targetSdkVersion, &halCameraIdsAndStreamCombinations, &knowUnsupported);
969 if (res != OK) {
970 ALOGE("%s unable to convert session configurations provided to HAL stream"
971 "combinations", __FUNCTION__);
972 return res;
973 }
974 if (knowUnsupported) {
975 // We got to know the streams aren't valid before doing the HAL
976 // call itself.
977 *isSupported = false;
978 return OK;
979 }
980
981 if (mMinorVersion >= 6) {
982 // Check if the provider is currently active - not going to start it for this notification
983 auto interface = mSavedInterface != nullptr ? mSavedInterface : mActiveInterface.promote();
984 if (interface == nullptr) {
985 // TODO: This might be some other problem
986 return INVALID_OPERATION;
987 }
988 auto castResult2_6 = provider::V2_6::ICameraProvider::castFrom(interface);
989 auto castResult2_7 = provider::V2_7::ICameraProvider::castFrom(interface);
990 Status callStatus;
991 auto cb =
992 [&isSupported, &callStatus](Status s, bool supported) {
993 callStatus = s;
994 *isSupported = supported; };
995
996 ::android::hardware::Return<void> ret;
997 sp<provider::V2_7::ICameraProvider> interface_2_7;
998 sp<provider::V2_6::ICameraProvider> interface_2_6;
999 if (mMinorVersion >= 7 && castResult2_7.isOk()) {
1000 interface_2_7 = castResult2_7;
1001 if (interface_2_7 != nullptr) {
1002 ret = interface_2_7->isConcurrentStreamCombinationSupported_2_7(
1003 halCameraIdsAndStreamCombinations, cb);
1004 }
1005 } else if (mMinorVersion == 6 && castResult2_6.isOk()) {
1006 interface_2_6 = castResult2_6;
1007 if (interface_2_6 != nullptr) {
1008 hardware::hidl_vec<provider::V2_6::CameraIdAndStreamCombination>
1009 halCameraIdsAndStreamCombinations_2_6;
1010 size_t numStreams = halCameraIdsAndStreamCombinations.size();
1011 halCameraIdsAndStreamCombinations_2_6.resize(numStreams);
1012 for (size_t i = 0; i < numStreams; i++) {
1013 using namespace camera3;
1014 auto const& combination = halCameraIdsAndStreamCombinations[i];
1015 halCameraIdsAndStreamCombinations_2_6[i].cameraId = combination.cameraId;
1016 bool success =
1017 SessionConfigurationUtils::convertHALStreamCombinationFromV37ToV34(
1018 halCameraIdsAndStreamCombinations_2_6[i].streamConfiguration,
1019 combination.streamConfiguration);
1020 if (!success) {
1021 *isSupported = false;
1022 return OK;
1023 }
1024 }
1025 ret = interface_2_6->isConcurrentStreamCombinationSupported(
1026 halCameraIdsAndStreamCombinations_2_6, cb);
1027 }
1028 }
1029
1030 if (interface_2_7 != nullptr || interface_2_6 != nullptr) {
1031 if (ret.isOk()) {
1032 switch (callStatus) {
1033 case Status::OK:
1034 // Expected case, do nothing.
1035 res = OK;
1036 break;
1037 case Status::METHOD_NOT_SUPPORTED:
1038 res = INVALID_OPERATION;
1039 break;
1040 default:
1041 ALOGE("%s: Session configuration query failed: %d", __FUNCTION__,
1042 callStatus);
1043 res = UNKNOWN_ERROR;
1044 }
1045 } else {
1046 ALOGE("%s: Unexpected binder error: %s", __FUNCTION__, ret.description().c_str());
1047 res = UNKNOWN_ERROR;
1048 }
1049 return res;
1050 }
1051 }
1052 // unsupported operation
1053 return INVALID_OPERATION;
1054 }
1055
1056 } //namespace android
1057