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