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 
17 #define LOG_TAG "HidlCamera3-Device"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 //#define LOG_NDEBUG 0
20 //#define LOG_NNDEBUG 0  // Per-frame verbose logging
21 
22 #ifdef LOG_NNDEBUG
23 #define ALOGVV(...) ALOGV(__VA_ARGS__)
24 #else
25 #define ALOGVV(...) ((void)0)
26 #endif
27 
28 // Convenience macro for transient errors
29 #define CLOGE(fmt, ...) ALOGE("Camera %s: %s: " fmt, mId.c_str(), __FUNCTION__, \
30             ##__VA_ARGS__)
31 
32 // Convenience macros for transitioning to the error state
33 #define SET_ERR(fmt, ...) setErrorState(   \
34     "%s: " fmt, __FUNCTION__,              \
35     ##__VA_ARGS__)
36 #define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37     "%s: " fmt, __FUNCTION__,                    \
38     ##__VA_ARGS__)
39 
40 
41 #include <inttypes.h>
42 
43 #include <utility>
44 
45 #include <utils/Log.h>
46 #include <utils/Trace.h>
47 #include <utils/Timers.h>
48 #include <cutils/properties.h>
49 #include <camera/StringUtils.h>
50 #include <com_android_internal_camera_flags.h>
51 
52 #include <android/hardware/camera/device/3.7/ICameraInjectionSession.h>
53 #include <android/hardware/camera2/ICameraDeviceUser.h>
54 
55 #include "device3/hidl/HidlCamera3OutputUtils.h"
56 #include "device3/hidl/HidlCamera3OfflineSession.h"
57 #include "utils/SessionConfigurationUtilsHidl.h"
58 #include "utils/TraceHFR.h"
59 
60 #include "../../common/hidl/HidlProviderInfo.h"
61 #include "HidlCamera3Device.h"
62 
63 #include <algorithm>
64 #include <tuple>
65 
66 using namespace android::camera3;
67 using namespace android::hardware::camera;
68 using namespace android::hardware::camera::device::V3_2;
69 using android::hardware::camera::metadata::V3_6::CameraMetadataEnumAndroidSensorPixelMode;
70 namespace flags = com::android::internal::camera::flags;
71 
72 namespace android {
73 
mapToPixelFormat(int frameworkFormat)74 hardware::graphics::common::V1_0::PixelFormat HidlCamera3Device::mapToPixelFormat(
75         int frameworkFormat) {
76     return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat;
77 }
78 
mapToHidlDataspace(android_dataspace dataSpace)79 DataspaceFlags HidlCamera3Device::mapToHidlDataspace(
80         android_dataspace dataSpace) {
81     return dataSpace;
82 }
83 
mapToConsumerUsage(uint64_t usage)84 BufferUsageFlags HidlCamera3Device::mapToConsumerUsage(
85         uint64_t usage) {
86     return usage;
87 }
88 
mapToStreamRotation(camera_stream_rotation_t rotation)89 StreamRotation HidlCamera3Device::mapToStreamRotation(camera_stream_rotation_t rotation) {
90     switch (rotation) {
91         case CAMERA_STREAM_ROTATION_0:
92             return StreamRotation::ROTATION_0;
93         case CAMERA_STREAM_ROTATION_90:
94             return StreamRotation::ROTATION_90;
95         case CAMERA_STREAM_ROTATION_180:
96             return StreamRotation::ROTATION_180;
97         case CAMERA_STREAM_ROTATION_270:
98             return StreamRotation::ROTATION_270;
99     }
100     ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation);
101     return StreamRotation::ROTATION_0;
102 }
103 
mapToStreamConfigurationMode(camera_stream_configuration_mode_t operationMode,StreamConfigurationMode * mode)104 status_t HidlCamera3Device::mapToStreamConfigurationMode(
105         camera_stream_configuration_mode_t operationMode, StreamConfigurationMode *mode) {
106     if (mode == nullptr) return BAD_VALUE;
107     if (operationMode < CAMERA_VENDOR_STREAM_CONFIGURATION_MODE_START) {
108         switch(operationMode) {
109             case CAMERA_STREAM_CONFIGURATION_NORMAL_MODE:
110                 *mode = StreamConfigurationMode::NORMAL_MODE;
111                 break;
112             case CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE:
113                 *mode = StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE;
114                 break;
115             default:
116                 ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode);
117                 return BAD_VALUE;
118         }
119     } else {
120         *mode = static_cast<StreamConfigurationMode>(operationMode);
121     }
122     return OK;
123 }
124 
mapToFrameworkFormat(hardware::graphics::common::V1_0::PixelFormat pixelFormat)125 int HidlCamera3Device::mapToFrameworkFormat(
126         hardware::graphics::common::V1_0::PixelFormat pixelFormat) {
127     return static_cast<uint32_t>(pixelFormat);
128 }
129 
mapToFrameworkDataspace(DataspaceFlags dataSpace)130 android_dataspace HidlCamera3Device::mapToFrameworkDataspace(
131         DataspaceFlags dataSpace) {
132     return static_cast<android_dataspace>(dataSpace);
133 }
134 
mapConsumerToFrameworkUsage(BufferUsageFlags usage)135 uint64_t HidlCamera3Device::mapConsumerToFrameworkUsage(
136         BufferUsageFlags usage) {
137     return usage;
138 }
139 
mapProducerToFrameworkUsage(BufferUsageFlags usage)140 uint64_t HidlCamera3Device::mapProducerToFrameworkUsage(
141         BufferUsageFlags usage) {
142     return usage;
143 }
144 
initialize(sp<CameraProviderManager> manager,const std::string & monitorTags)145 status_t HidlCamera3Device::initialize(sp<CameraProviderManager> manager,
146         const std::string& monitorTags) {
147     ATRACE_CALL();
148     Mutex::Autolock il(mInterfaceLock);
149     Mutex::Autolock l(mLock);
150 
151     ALOGV("%s: Initializing HIDL device for camera %s", __FUNCTION__, mId.c_str());
152     if (mStatus != STATUS_UNINITIALIZED) {
153         CLOGE("Already initialized!");
154         return INVALID_OPERATION;
155     }
156     if (manager == nullptr) return INVALID_OPERATION;
157 
158     sp<ICameraDeviceSession> session;
159     ATRACE_BEGIN("CameraHal::openSession");
160     status_t res = manager->openHidlSession(mId, this,
161             /*out*/ &session);
162     ATRACE_END();
163     if (res != OK) {
164         SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res);
165         return res;
166     }
167 
168     res = manager->getCameraCharacteristics(mId, mOverrideForPerfClass, &mDeviceInfo,
169             hardware::ICameraService::ROTATION_OVERRIDE_NONE);
170     if (res != OK) {
171         SET_ERR_L("Could not retrieve camera characteristics: %s (%d)", strerror(-res), res);
172         session->close();
173         return res;
174     }
175     mSupportNativeZoomRatio = manager->supportNativeZoomRatio(mId);
176 
177     std::vector<std::string> physicalCameraIds;
178     bool isLogical = manager->isLogicalCamera(mId, &physicalCameraIds);
179     if (isLogical) {
180         for (auto& physicalId : physicalCameraIds) {
181             // Do not override characteristics for physical cameras
182             res = manager->getCameraCharacteristics(
183                     physicalId, /*overrideForPerfClass*/false, &mPhysicalDeviceInfoMap[physicalId],
184                     hardware::ICameraService::ROTATION_OVERRIDE_NONE);
185             if (res != OK) {
186                 SET_ERR_L("Could not retrieve camera %s characteristics: %s (%d)",
187                         physicalId.c_str(), strerror(-res), res);
188                 session->close();
189                 return res;
190             }
191 
192             bool usePrecorrectArray =
193                     DistortionMapper::isDistortionSupported(mPhysicalDeviceInfoMap[physicalId]);
194             if (usePrecorrectArray) {
195                 res = mDistortionMappers[physicalId].setupStaticInfo(
196                         mPhysicalDeviceInfoMap[physicalId]);
197                 if (res != OK) {
198                     SET_ERR_L("Unable to read camera %s's calibration fields for distortion "
199                             "correction", physicalId.c_str());
200                     session->close();
201                     return res;
202                 }
203             }
204 
205             mZoomRatioMappers[physicalId] = ZoomRatioMapper(
206                     &mPhysicalDeviceInfoMap[physicalId],
207                     mSupportNativeZoomRatio, usePrecorrectArray);
208 
209             if (SessionConfigurationUtils::supportsUltraHighResolutionCapture(
210                     mPhysicalDeviceInfoMap[physicalId])) {
211                 mUHRCropAndMeteringRegionMappers[physicalId] =
212                         UHRCropAndMeteringRegionMapper(mPhysicalDeviceInfoMap[physicalId],
213                                 usePrecorrectArray);
214             }
215         }
216     }
217 
218     std::shared_ptr<RequestMetadataQueue> queue;
219     auto requestQueueRet = session->getCaptureRequestMetadataQueue(
220         [&queue](const auto& descriptor) {
221             queue = std::make_shared<RequestMetadataQueue>(descriptor);
222             if (!queue->isValid() || queue->availableToWrite() <= 0) {
223                 ALOGE("HAL returns empty request metadata fmq, not use it");
224                 queue = nullptr;
225                 // don't use the queue onwards.
226             }
227         });
228     if (!requestQueueRet.isOk()) {
229         ALOGE("Transaction error when getting request metadata fmq: %s, not use it",
230                 requestQueueRet.description().c_str());
231         return DEAD_OBJECT;
232     }
233 
234     std::unique_ptr<ResultMetadataQueue>& resQueue = mResultMetadataQueue;
235     auto resultQueueRet = session->getCaptureResultMetadataQueue(
236         [&resQueue](const auto& descriptor) {
237             resQueue = std::make_unique<ResultMetadataQueue>(descriptor);
238             if (!resQueue->isValid() || resQueue->availableToWrite() <= 0) {
239                 ALOGE("HAL returns empty result metadata fmq, not use it");
240                 resQueue = nullptr;
241                 // Don't use the resQueue onwards.
242             }
243         });
244     if (!resultQueueRet.isOk()) {
245         ALOGE("Transaction error when getting result metadata queue from camera session: %s",
246                 resultQueueRet.description().c_str());
247         return DEAD_OBJECT;
248     }
249     IF_ALOGV() {
250         session->interfaceChain([](
251             ::android::hardware::hidl_vec<::android::hardware::hidl_string> interfaceChain) {
252                 ALOGV("Session interface chain:");
253                 for (const auto& iface : interfaceChain) {
254                     ALOGV("  %s", iface.c_str());
255                 }
256             });
257     }
258 
259     camera_metadata_entry bufMgrMode =
260             mDeviceInfo.find(ANDROID_INFO_SUPPORTED_BUFFER_MANAGEMENT_VERSION);
261     if (bufMgrMode.count > 0) {
262          mUseHalBufManager = (bufMgrMode.data.u8[0] ==
263             ANDROID_INFO_SUPPORTED_BUFFER_MANAGEMENT_VERSION_HIDL_DEVICE_3_5);
264     }
265 
266     camera_metadata_entry_t capabilities = mDeviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
267     for (size_t i = 0; i < capabilities.count; i++) {
268         uint8_t capability = capabilities.data.u8[i];
269         if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_OFFLINE_PROCESSING) {
270             mSupportOfflineProcessing = true;
271         }
272     }
273 
274     mInterface = new HidlHalInterface(session, queue, mUseHalBufManager, mSupportOfflineProcessing);
275 
276     std::string providerType;
277     mVendorTagId = manager->getProviderTagIdLocked(mId);
278     mTagMonitor.initialize(mVendorTagId);
279     if (!monitorTags.empty()) {
280         mTagMonitor.parseTagsToMonitor(monitorTags);
281     }
282 
283     // Metadata tags needs fixup for monochrome camera device version less
284     // than 3.5.
285     hardware::hidl_version maxVersion{0,0};
286     IPCTransport transport = IPCTransport::HIDL;
287     res = manager->getHighestSupportedVersion(mId, &maxVersion, &transport);
288     if (res != OK) {
289         ALOGE("%s: Error in getting camera device version id: %s (%d)",
290                 __FUNCTION__, strerror(-res), res);
291         return res;
292     }
293     int deviceVersion = HARDWARE_DEVICE_API_VERSION(
294             maxVersion.get_major(), maxVersion.get_minor());
295 
296     bool isMonochrome = false;
297     for (size_t i = 0; i < capabilities.count; i++) {
298         uint8_t capability = capabilities.data.u8[i];
299         if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_MONOCHROME) {
300             isMonochrome = true;
301         }
302     }
303     mNeedFixupMonochromeTags = (isMonochrome && deviceVersion < CAMERA_DEVICE_API_VERSION_3_5);
304 
305     return initializeCommonLocked();
306 }
307 
requestStreamBuffers(const hardware::hidl_vec<hardware::camera::device::V3_5::BufferRequest> & bufReqs,requestStreamBuffers_cb _hidl_cb)308 hardware::Return<void> HidlCamera3Device::requestStreamBuffers(
309         const hardware::hidl_vec<hardware::camera::device::V3_5::BufferRequest>& bufReqs,
310         requestStreamBuffers_cb _hidl_cb) {
311     RequestBufferStates states {
312         mId, mRequestBufferInterfaceLock, mUseHalBufManager, mHalBufManagedStreamIds,
313         mOutputStreams, mSessionStatsBuilder,
314         *this, *mInterface, *this};
315     camera3::requestStreamBuffers(states, bufReqs, _hidl_cb);
316     return hardware::Void();
317 }
318 
returnStreamBuffers(const hardware::hidl_vec<hardware::camera::device::V3_2::StreamBuffer> & buffers)319 hardware::Return<void> HidlCamera3Device::returnStreamBuffers(
320         const hardware::hidl_vec<hardware::camera::device::V3_2::StreamBuffer>& buffers) {
321     ReturnBufferStates states {
322         mId, mUseHalBufManager, mHalBufManagedStreamIds, mOutputStreams,
323         mSessionStatsBuilder, *mInterface};
324     camera3::returnStreamBuffers(states, buffers);
325     return hardware::Void();
326 }
327 
processCaptureResult_3_4(const hardware::hidl_vec<hardware::camera::device::V3_4::CaptureResult> & results)328 hardware::Return<void> HidlCamera3Device::processCaptureResult_3_4(
329         const hardware::hidl_vec<
330                 hardware::camera::device::V3_4::CaptureResult>& results) {
331     // Ideally we should grab mLock, but that can lead to deadlock, and
332     // it's not super important to get up to date value of mStatus for this
333     // warning print, hence skipping the lock here
334     if (mStatus == STATUS_ERROR) {
335         // Per API contract, HAL should act as closed after device error
336         // But mStatus can be set to error by framework as well, so just log
337         // a warning here.
338         ALOGW("%s: received capture result in error state.", __FUNCTION__);
339     }
340 
341     sp<NotificationListener> listener;
342     {
343         std::lock_guard<std::mutex> l(mOutputLock);
344         listener = mListener.promote();
345     }
346 
347     if (mProcessCaptureResultLock.tryLock() != OK) {
348         // This should never happen; it indicates a wrong client implementation
349         // that doesn't follow the contract. But, we can be tolerant here.
350         ALOGE("%s: callback overlapped! waiting 1s...",
351                 __FUNCTION__);
352         if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
353             ALOGE("%s: cannot acquire lock in 1s, dropping results",
354                     __FUNCTION__);
355             // really don't know what to do, so bail out.
356             return hardware::Void();
357         }
358     }
359     HidlCaptureOutputStates states {
360        {
361         mId,
362         mInFlightLock, mLastCompletedRegularFrameNumber,
363         mLastCompletedReprocessFrameNumber, mLastCompletedZslFrameNumber,
364         mInFlightMap, mOutputLock,  mResultQueue, mResultSignal,
365         mNextShutterFrameNumber,
366         mNextReprocessShutterFrameNumber, mNextZslStillShutterFrameNumber,
367         mNextResultFrameNumber,
368         mNextReprocessResultFrameNumber, mNextZslStillResultFrameNumber,
369         mUseHalBufManager, mHalBufManagedStreamIds, mUsePartialResult, mNeedFixupMonochromeTags,
370         mNumPartialResults, mVendorTagId, mDeviceInfo, mPhysicalDeviceInfoMap,
371         mDistortionMappers, mZoomRatioMappers, mRotateAndCropMappers,
372         mTagMonitor, mInputStream, mOutputStreams, mSessionStatsBuilder, listener, *this, *this,
373         *mInterface, mLegacyClient, mMinExpectedDuration, mIsFixedFps, mRotationOverride,
374         mActivePhysicalId}, mResultMetadataQueue
375     };
376 
377     //HidlCaptureOutputStates hidlStates {
378     //}
379 
380     for (const auto& result : results) {
381         processOneCaptureResultLocked(states, result.v3_2, result.physicalCameraMetadata);
382     }
383     mProcessCaptureResultLock.unlock();
384     return hardware::Void();
385 }
386 
387 // Only one processCaptureResult should be called at a time, so
388 // the locks won't block. The locks are present here simply to enforce this.
processCaptureResult(const hardware::hidl_vec<hardware::camera::device::V3_2::CaptureResult> & results)389 hardware::Return<void> HidlCamera3Device::processCaptureResult(
390         const hardware::hidl_vec<
391                 hardware::camera::device::V3_2::CaptureResult>& results) {
392     hardware::hidl_vec<hardware::camera::device::V3_4::PhysicalCameraMetadata> noPhysMetadata;
393 
394     // Ideally we should grab mLock, but that can lead to deadlock, and
395     // it's not super important to get up to date value of mStatus for this
396     // warning print, hence skipping the lock here
397     if (mStatus == STATUS_ERROR) {
398         // Per API contract, HAL should act as closed after device error
399         // But mStatus can be set to error by framework as well, so just log
400         // a warning here.
401         ALOGW("%s: received capture result in error state.", __FUNCTION__);
402     }
403 
404     sp<NotificationListener> listener;
405     {
406         std::lock_guard<std::mutex> l(mOutputLock);
407         listener = mListener.promote();
408     }
409 
410     if (mProcessCaptureResultLock.tryLock() != OK) {
411         // This should never happen; it indicates a wrong client implementation
412         // that doesn't follow the contract. But, we can be tolerant here.
413         ALOGE("%s: callback overlapped! waiting 1s...",
414                 __FUNCTION__);
415         if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
416             ALOGE("%s: cannot acquire lock in 1s, dropping results",
417                     __FUNCTION__);
418             // really don't know what to do, so bail out.
419             return hardware::Void();
420         }
421     }
422 
423     HidlCaptureOutputStates states {
424       { mId,
425         mInFlightLock, mLastCompletedRegularFrameNumber,
426         mLastCompletedReprocessFrameNumber, mLastCompletedZslFrameNumber,
427         mInFlightMap, mOutputLock,  mResultQueue, mResultSignal,
428         mNextShutterFrameNumber,
429         mNextReprocessShutterFrameNumber, mNextZslStillShutterFrameNumber,
430         mNextResultFrameNumber,
431         mNextReprocessResultFrameNumber, mNextZslStillResultFrameNumber,
432         mUseHalBufManager, mHalBufManagedStreamIds, mUsePartialResult, mNeedFixupMonochromeTags,
433         mNumPartialResults, mVendorTagId, mDeviceInfo, mPhysicalDeviceInfoMap,
434         mDistortionMappers, mZoomRatioMappers, mRotateAndCropMappers,
435         mTagMonitor, mInputStream, mOutputStreams, mSessionStatsBuilder, listener, *this, *this,
436         *mInterface, mLegacyClient, mMinExpectedDuration, mIsFixedFps, mRotationOverride,
437         mActivePhysicalId}, mResultMetadataQueue
438     };
439 
440     for (const auto& result : results) {
441         processOneCaptureResultLocked(states, result, noPhysMetadata);
442     }
443     mProcessCaptureResultLock.unlock();
444     return hardware::Void();
445 }
446 
notify(const hardware::hidl_vec<hardware::camera::device::V3_2::NotifyMsg> & msgs)447 hardware::Return<void> HidlCamera3Device::notify(
448         const hardware::hidl_vec<hardware::camera::device::V3_2::NotifyMsg>& msgs) {
449     return notifyHelper<hardware::camera::device::V3_2::NotifyMsg>(msgs);
450 }
451 
452 template<typename NotifyMsgType>
notifyHelper(const hardware::hidl_vec<NotifyMsgType> & msgs)453 hardware::Return<void> HidlCamera3Device::notifyHelper(
454         const hardware::hidl_vec<NotifyMsgType>& msgs) {
455     // Ideally we should grab mLock, but that can lead to deadlock, and
456     // it's not super important to get up to date value of mStatus for this
457     // warning print, hence skipping the lock here
458     if (mStatus == STATUS_ERROR) {
459         // Per API contract, HAL should act as closed after device error
460         // But mStatus can be set to error by framework as well, so just log
461         // a warning here.
462         ALOGW("%s: received notify message in error state.", __FUNCTION__);
463     }
464 
465     sp<NotificationListener> listener;
466     {
467         std::lock_guard<std::mutex> l(mOutputLock);
468         listener = mListener.promote();
469     }
470 
471     HidlCaptureOutputStates states {
472       { mId,
473         mInFlightLock, mLastCompletedRegularFrameNumber,
474         mLastCompletedReprocessFrameNumber, mLastCompletedZslFrameNumber,
475         mInFlightMap, mOutputLock,  mResultQueue, mResultSignal,
476         mNextShutterFrameNumber,
477         mNextReprocessShutterFrameNumber, mNextZslStillShutterFrameNumber,
478         mNextResultFrameNumber,
479         mNextReprocessResultFrameNumber, mNextZslStillResultFrameNumber,
480         mUseHalBufManager, mHalBufManagedStreamIds, mUsePartialResult, mNeedFixupMonochromeTags,
481         mNumPartialResults, mVendorTagId, mDeviceInfo, mPhysicalDeviceInfoMap,
482         mDistortionMappers, mZoomRatioMappers, mRotateAndCropMappers,
483         mTagMonitor, mInputStream, mOutputStreams, mSessionStatsBuilder, listener, *this, *this,
484         *mInterface, mLegacyClient, mMinExpectedDuration, mIsFixedFps, mRotationOverride,
485         mActivePhysicalId}, mResultMetadataQueue
486     };
487     for (const auto& msg : msgs) {
488         camera3::notify(states, msg);
489     }
490     return hardware::Void();
491 }
492 
switchToOffline(const std::vector<int32_t> & streamsToKeep,sp<CameraOfflineSessionBase> * session)493 status_t HidlCamera3Device::switchToOffline(
494         const std::vector<int32_t>& streamsToKeep,
495         /*out*/ sp<CameraOfflineSessionBase>* session) {
496     ATRACE_CALL();
497     if (session == nullptr) {
498         ALOGE("%s: session must not be null", __FUNCTION__);
499         return BAD_VALUE;
500     }
501 
502     Mutex::Autolock il(mInterfaceLock);
503 
504     bool hasInputStream = mInputStream != nullptr;
505     int32_t inputStreamId = hasInputStream ? mInputStream->getId() : -1;
506     bool inputStreamSupportsOffline = hasInputStream ?
507             mInputStream->getOfflineProcessingSupport() : false;
508     auto outputStreamIds = mOutputStreams.getStreamIds();
509     auto streamIds = outputStreamIds;
510     if (hasInputStream) {
511         streamIds.push_back(mInputStream->getId());
512     }
513 
514     // Check all streams in streamsToKeep supports offline mode
515     for (auto id : streamsToKeep) {
516         if (std::find(streamIds.begin(), streamIds.end(), id) == streamIds.end()) {
517             ALOGE("%s: Unknown stream ID %d", __FUNCTION__, id);
518             return BAD_VALUE;
519         } else if (id == inputStreamId) {
520             if (!inputStreamSupportsOffline) {
521                 ALOGE("%s: input stream %d cannot be switched to offline",
522                         __FUNCTION__, id);
523                 return BAD_VALUE;
524             }
525         } else {
526             sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
527             if (!stream->getOfflineProcessingSupport()) {
528                 ALOGE("%s: output stream %d cannot be switched to offline",
529                         __FUNCTION__, id);
530                 return BAD_VALUE;
531             }
532         }
533     }
534     // TODO: block surface sharing and surface group streams until we can support them
535 
536     // Stop repeating request, wait until all remaining requests are submitted, then call into
537     // HAL switchToOffline
538     hardware::camera::device::V3_6::CameraOfflineSessionInfo offlineSessionInfo;
539     sp<hardware::camera::device::V3_6::ICameraOfflineSession> offlineSession;
540     camera3::BufferRecords bufferRecords;
541     status_t ret = static_cast<HidlRequestThread *>(mRequestThread.get())->switchToOffline(
542             streamsToKeep, &offlineSessionInfo, &offlineSession, &bufferRecords);
543 
544     if (ret != OK) {
545         SET_ERR("Switch to offline failed: %s (%d)", strerror(-ret), ret);
546         return ret;
547     }
548 
549     bool succ = mRequestBufferSM.onSwitchToOfflineSuccess();
550     if (!succ) {
551         SET_ERR("HAL must not be calling requestStreamBuffers call");
552         // TODO: block ALL callbacks from HAL till app configured new streams?
553         return UNKNOWN_ERROR;
554     }
555 
556     // Verify offlineSessionInfo
557     std::vector<int32_t> offlineStreamIds;
558     offlineStreamIds.reserve(offlineSessionInfo.offlineStreams.size());
559     for (auto offlineStream : offlineSessionInfo.offlineStreams) {
560         // verify stream IDs
561         int32_t id = offlineStream.id;
562         if (std::find(streamIds.begin(), streamIds.end(), id) == streamIds.end()) {
563             SET_ERR("stream ID %d not found!", id);
564             return UNKNOWN_ERROR;
565         }
566 
567         // When not using HAL buf manager, only allow streams requested by app to be preserved
568         if (!mUseHalBufManager) {
569             if (std::find(streamsToKeep.begin(), streamsToKeep.end(), id) == streamsToKeep.end()) {
570                 SET_ERR("stream ID %d must not be switched to offline!", id);
571                 return UNKNOWN_ERROR;
572             }
573         }
574 
575         offlineStreamIds.push_back(id);
576         sp<Camera3StreamInterface> stream = (id == inputStreamId) ?
577                 static_cast<sp<Camera3StreamInterface>>(mInputStream) :
578                 static_cast<sp<Camera3StreamInterface>>(mOutputStreams.get(id));
579         // Verify number of outstanding buffers
580         if (stream->getOutstandingBuffersCount() != offlineStream.numOutstandingBuffers) {
581             SET_ERR("Offline stream %d # of remaining buffer mismatch: (%zu,%d) (service/HAL)",
582                     id, stream->getOutstandingBuffersCount(), offlineStream.numOutstandingBuffers);
583             return UNKNOWN_ERROR;
584         }
585     }
586 
587     // Verify all streams to be deleted don't have any outstanding buffers
588     if (hasInputStream && std::find(offlineStreamIds.begin(), offlineStreamIds.end(),
589                 inputStreamId) == offlineStreamIds.end()) {
590         if (mInputStream->hasOutstandingBuffers()) {
591             SET_ERR("Input stream %d still has %zu outstanding buffer!",
592                     inputStreamId, mInputStream->getOutstandingBuffersCount());
593             return UNKNOWN_ERROR;
594         }
595     }
596 
597     for (const auto& outStreamId : outputStreamIds) {
598         if (std::find(offlineStreamIds.begin(), offlineStreamIds.end(),
599                 outStreamId) == offlineStreamIds.end()) {
600             auto outStream = mOutputStreams.get(outStreamId);
601             if (outStream->hasOutstandingBuffers()) {
602                 SET_ERR("Output stream %d still has %zu outstanding buffer!",
603                         outStreamId, outStream->getOutstandingBuffersCount());
604                 return UNKNOWN_ERROR;
605             }
606         }
607     }
608 
609     InFlightRequestMap offlineReqs;
610     // Verify inflight requests and their pending buffers
611     {
612         std::lock_guard<std::mutex> l(mInFlightLock);
613         for (auto offlineReq : offlineSessionInfo.offlineRequests) {
614             int idx = mInFlightMap.indexOfKey(offlineReq.frameNumber);
615             if (idx == NAME_NOT_FOUND) {
616                 SET_ERR("Offline request frame number %d not found!", offlineReq.frameNumber);
617                 return UNKNOWN_ERROR;
618             }
619 
620             const auto& inflightReq = mInFlightMap.valueAt(idx);
621             // TODO: check specific stream IDs
622             size_t numBuffersLeft = static_cast<size_t>(inflightReq.numBuffersLeft);
623             if (numBuffersLeft != offlineReq.pendingStreams.size()) {
624                 SET_ERR("Offline request # of remaining buffer mismatch: (%d,%d) (service/HAL)",
625                         inflightReq.numBuffersLeft, offlineReq.pendingStreams.size());
626                 return UNKNOWN_ERROR;
627             }
628             offlineReqs.add(offlineReq.frameNumber, inflightReq);
629         }
630     }
631 
632     // Create Camera3OfflineSession and transfer object ownership
633     //   (streams, inflight requests, buffer caches)
634     camera3::StreamSet offlineStreamSet;
635     sp<camera3::Camera3Stream> inputStream;
636     for (auto offlineStream : offlineSessionInfo.offlineStreams) {
637         int32_t id = offlineStream.id;
638         if (mInputStream != nullptr && id == mInputStream->getId()) {
639             inputStream = mInputStream;
640         } else {
641             offlineStreamSet.add(id, mOutputStreams.get(id));
642         }
643     }
644 
645     // TODO: check if we need to lock before copying states
646     //       though technically no other thread should be talking to Camera3Device at this point
647     Camera3OfflineStates offlineStates(
648             mTagMonitor, mVendorTagId, mUseHalBufManager, mHalBufManagedStreamIds,
649             mNeedFixupMonochromeTags, mUsePartialResult, mNumPartialResults,
650             mLastCompletedRegularFrameNumber, mLastCompletedReprocessFrameNumber,
651             mLastCompletedZslFrameNumber, mNextResultFrameNumber,
652             mNextReprocessResultFrameNumber, mNextZslStillResultFrameNumber,
653             mNextShutterFrameNumber, mNextReprocessShutterFrameNumber,
654             mNextZslStillShutterFrameNumber, mDeviceInfo, mPhysicalDeviceInfoMap,
655             mDistortionMappers, mZoomRatioMappers, mRotateAndCropMappers);
656 
657     *session = new HidlCamera3OfflineSession(mId, inputStream, offlineStreamSet,
658             std::move(bufferRecords), offlineReqs, offlineStates, offlineSession);
659 
660     // Delete all streams that has been transferred to offline session
661     Mutex::Autolock l(mLock);
662     for (auto offlineStream : offlineSessionInfo.offlineStreams) {
663         int32_t id = offlineStream.id;
664         if (mInputStream != nullptr && id == mInputStream->getId()) {
665             mInputStream.clear();
666         } else {
667             mOutputStreams.remove(id);
668         }
669     }
670 
671     // disconnect all other streams and switch to UNCONFIGURED state
672     if (mInputStream != nullptr) {
673         ret = mInputStream->disconnect();
674         if (ret != OK) {
675             SET_ERR_L("disconnect input stream failed!");
676             return UNKNOWN_ERROR;
677         }
678     }
679 
680     for (auto streamId : mOutputStreams.getStreamIds()) {
681         sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
682         ret = stream->disconnect();
683         if (ret != OK) {
684             SET_ERR_L("disconnect output stream %d failed!", streamId);
685             return UNKNOWN_ERROR;
686         }
687     }
688 
689     mInputStream.clear();
690     mOutputStreams.clear();
691     mNeedConfig = true;
692     internalUpdateStatusLocked(STATUS_UNCONFIGURED);
693     mOperatingMode = NO_MODE;
694     mIsConstrainedHighSpeedConfiguration = false;
695     mRequestThread->clearPreviousRequest();
696 
697     return OK;
698     // TO be done by CameraDeviceClient/Camera3OfflineSession
699     // register the offline client to camera service
700     // Setup result passthing threads etc
701     // Initialize offline session so HAL can start sending callback to it (result Fmq)
702     // TODO: check how many onIdle callback will be sent
703     // Java side to make sure the CameraCaptureSession is properly closed
704 }
705 
applyMaxBatchSizeLocked(RequestList * requestList,const sp<camera3::Camera3OutputStreamInterface> & stream)706 void HidlCamera3Device::applyMaxBatchSizeLocked(
707         RequestList* requestList, const sp<camera3::Camera3OutputStreamInterface>& stream) {
708     int batchSize = requestList->size();
709 
710     (*requestList->begin())->mBatchSize = batchSize;
711     stream->setBatchSize(batchSize);
712 }
713 
createNewRequestThread(wp<Camera3Device> parent,sp<camera3::StatusTracker> statusTracker,sp<Camera3Device::HalInterface> interface,const Vector<int32_t> & sessionParamKeys,bool useHalBufManager,bool supportCameraMute,int rotationOverride,bool supportSettingsOverride)714 sp<Camera3Device::RequestThread> HidlCamera3Device::createNewRequestThread(
715                 wp<Camera3Device> parent, sp<camera3::StatusTracker> statusTracker,
716                 sp<Camera3Device::HalInterface> interface,
717                 const Vector<int32_t>& sessionParamKeys,
718                 bool useHalBufManager,
719                 bool supportCameraMute,
720                 int rotationOverride,
721                 bool supportSettingsOverride) {
722         return new HidlRequestThread(parent, statusTracker, interface, sessionParamKeys,
723                 useHalBufManager, supportCameraMute, rotationOverride,
724                 supportSettingsOverride);
725 };
726 
727 sp<Camera3Device::Camera3DeviceInjectionMethods>
createCamera3DeviceInjectionMethods(wp<Camera3Device> parent)728 HidlCamera3Device::createCamera3DeviceInjectionMethods(wp<Camera3Device> parent) {
729     return new HidlCamera3DeviceInjectionMethods(parent);
730 }
731 
injectionCameraInitialize(const std::string & injectedCamId,sp<CameraProviderManager> manager)732 status_t HidlCamera3Device::injectionCameraInitialize(const std::string &injectedCamId,
733             sp<CameraProviderManager> manager) {
734         return (static_cast<HidlCamera3DeviceInjectionMethods *>(
735                 mInjectionMethods.get()))->injectionInitialize(injectedCamId, manager, this);
736 };
737 
738 
HidlHalInterface(sp<device::V3_2::ICameraDeviceSession> & session,std::shared_ptr<RequestMetadataQueue> queue,bool useHalBufManager,bool supportOfflineProcessing)739 HidlCamera3Device::HidlHalInterface::HidlHalInterface(
740             sp<device::V3_2::ICameraDeviceSession> &session,
741             std::shared_ptr<RequestMetadataQueue> queue,
742             bool useHalBufManager, bool supportOfflineProcessing) :
743         HalInterface(useHalBufManager, supportOfflineProcessing),
744         mHidlSession(session),
745         mRequestMetadataQueue(queue) {
746     // Check with hardware service manager if we can downcast these interfaces
747     // Somewhat expensive, so cache the results at startup
748     auto castResult_3_7 = device::V3_7::ICameraDeviceSession::castFrom(mHidlSession);
749     if (castResult_3_7.isOk()) {
750         mHidlSession_3_7 = castResult_3_7;
751     }
752     auto castResult_3_6 = device::V3_6::ICameraDeviceSession::castFrom(mHidlSession);
753     if (castResult_3_6.isOk()) {
754         mHidlSession_3_6 = castResult_3_6;
755     }
756     auto castResult_3_5 = device::V3_5::ICameraDeviceSession::castFrom(mHidlSession);
757     if (castResult_3_5.isOk()) {
758         mHidlSession_3_5 = castResult_3_5;
759     }
760     auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
761     if (castResult_3_4.isOk()) {
762         mHidlSession_3_4 = castResult_3_4;
763     }
764     auto castResult_3_3 = device::V3_3::ICameraDeviceSession::castFrom(mHidlSession);
765     if (castResult_3_3.isOk()) {
766         mHidlSession_3_3 = castResult_3_3;
767     }
768 }
769 
valid()770 bool HidlCamera3Device::HidlHalInterface::valid() {
771     return (mHidlSession != nullptr);
772 }
773 
clear()774 void HidlCamera3Device::HidlHalInterface::clear() {
775     mHidlSession_3_7.clear();
776     mHidlSession_3_6.clear();
777     mHidlSession_3_5.clear();
778     mHidlSession_3_4.clear();
779     mHidlSession_3_3.clear();
780     mHidlSession.clear();
781 }
782 
constructDefaultRequestSettings(camera_request_template_t templateId,camera_metadata_t ** requestTemplate)783 status_t HidlCamera3Device::HidlHalInterface::constructDefaultRequestSettings(
784         camera_request_template_t templateId,
785         /*out*/ camera_metadata_t **requestTemplate) {
786     ATRACE_NAME("CameraHidlHal::constructDefaultRequestSettings");
787     if (!valid()) return INVALID_OPERATION;
788     status_t res = OK;
789 
790     common::V1_0::Status status;
791 
792     auto requestCallback = [&status, &requestTemplate]
793             (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
794             status = s;
795             if (status == common::V1_0::Status::OK) {
796                 const camera_metadata *r =
797                         reinterpret_cast<const camera_metadata_t*>(request.data());
798                 size_t expectedSize = request.size();
799                 int ret = validate_camera_metadata_structure(r, &expectedSize);
800                 if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) {
801                     *requestTemplate = clone_camera_metadata(r);
802                     if (*requestTemplate == nullptr) {
803                         ALOGE("%s: Unable to clone camera metadata received from HAL",
804                                 __FUNCTION__);
805                         status = common::V1_0::Status::INTERNAL_ERROR;
806                     }
807                 } else {
808                     ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
809                     status = common::V1_0::Status::INTERNAL_ERROR;
810                 }
811             }
812         };
813     hardware::Return<void> err;
814     RequestTemplate id;
815     switch (templateId) {
816         case CAMERA_TEMPLATE_PREVIEW:
817             id = RequestTemplate::PREVIEW;
818             break;
819         case CAMERA_TEMPLATE_STILL_CAPTURE:
820             id = RequestTemplate::STILL_CAPTURE;
821             break;
822         case CAMERA_TEMPLATE_VIDEO_RECORD:
823             id = RequestTemplate::VIDEO_RECORD;
824             break;
825         case CAMERA_TEMPLATE_VIDEO_SNAPSHOT:
826             id = RequestTemplate::VIDEO_SNAPSHOT;
827             break;
828         case CAMERA_TEMPLATE_ZERO_SHUTTER_LAG:
829             id = RequestTemplate::ZERO_SHUTTER_LAG;
830             break;
831         case CAMERA_TEMPLATE_MANUAL:
832             id = RequestTemplate::MANUAL;
833             break;
834         default:
835             // Unknown template ID, or this HAL is too old to support it
836             return BAD_VALUE;
837     }
838     err = mHidlSession->constructDefaultRequestSettings(id, requestCallback);
839 
840     if (!err.isOk()) {
841         ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
842         res = DEAD_OBJECT;
843     } else {
844         res = HidlProviderInfo::mapToStatusT(status);
845     }
846 
847     return res;
848 }
849 
isReconfigurationRequired(CameraMetadata & oldSessionParams,CameraMetadata & newSessionParams)850 bool HidlCamera3Device::HidlHalInterface::isReconfigurationRequired(
851         CameraMetadata& oldSessionParams, CameraMetadata& newSessionParams) {
852     // We do reconfiguration by default;
853     bool ret = true;
854     if ((mHidlSession_3_5 != nullptr) && mIsReconfigurationQuerySupported) {
855         android::hardware::hidl_vec<uint8_t> oldParams, newParams;
856         camera_metadata_t* oldSessioMeta = const_cast<camera_metadata_t*>(
857                 oldSessionParams.getAndLock());
858         camera_metadata_t* newSessioMeta = const_cast<camera_metadata_t*>(
859                 newSessionParams.getAndLock());
860         oldParams.setToExternal(reinterpret_cast<uint8_t*>(oldSessioMeta),
861                 get_camera_metadata_size(oldSessioMeta));
862         newParams.setToExternal(reinterpret_cast<uint8_t*>(newSessioMeta),
863                 get_camera_metadata_size(newSessioMeta));
864         hardware::camera::common::V1_0::Status callStatus;
865         bool required;
866         auto hidlCb = [&callStatus, &required] (hardware::camera::common::V1_0::Status s,
867                 bool requiredFlag) {
868             callStatus = s;
869             required = requiredFlag;
870         };
871         auto err = mHidlSession_3_5->isReconfigurationRequired(oldParams, newParams, hidlCb);
872         oldSessionParams.unlock(oldSessioMeta);
873         newSessionParams.unlock(newSessioMeta);
874         if (err.isOk()) {
875             switch (callStatus) {
876                 case hardware::camera::common::V1_0::Status::OK:
877                     ret = required;
878                     break;
879                 case hardware::camera::common::V1_0::Status::METHOD_NOT_SUPPORTED:
880                     mIsReconfigurationQuerySupported = false;
881                     ret = true;
882                     break;
883                 default:
884                     ALOGV("%s: Reconfiguration query failed: %d", __FUNCTION__, callStatus);
885                     ret = true;
886             }
887         } else {
888             ALOGE("%s: Unexpected binder error: %s", __FUNCTION__, err.description().c_str());
889             ret = true;
890         }
891     }
892 
893     return ret;
894 }
895 
configureStreams(const camera_metadata_t * sessionParams,camera_stream_configuration * config,const std::vector<uint32_t> & bufferSizes,int64_t)896 status_t HidlCamera3Device::HidlHalInterface::configureStreams(
897         const camera_metadata_t *sessionParams,
898         camera_stream_configuration *config, const std::vector<uint32_t>& bufferSizes,
899         int64_t /*logId*/) {
900     ATRACE_NAME("CameraHal::configureStreams");
901     if (!valid()) return INVALID_OPERATION;
902     status_t res = OK;
903 
904     if (config->input_is_multi_resolution && mHidlSession_3_7 == nullptr) {
905         ALOGE("%s: Camera device doesn't support multi-resolution input stream", __FUNCTION__);
906         return BAD_VALUE;
907     }
908 
909     // Convert stream config to HIDL
910     std::set<int> activeStreams;
911     device::V3_2::StreamConfiguration requestedConfiguration3_2;
912     device::V3_4::StreamConfiguration requestedConfiguration3_4;
913     device::V3_7::StreamConfiguration requestedConfiguration3_7;
914     requestedConfiguration3_2.streams.resize(config->num_streams);
915     requestedConfiguration3_4.streams.resize(config->num_streams);
916     requestedConfiguration3_7.streams.resize(config->num_streams);
917     mHalBufManagedStreamIds.clear();
918     for (size_t i = 0; i < config->num_streams; i++) {
919         device::V3_2::Stream &dst3_2 = requestedConfiguration3_2.streams[i];
920         device::V3_4::Stream &dst3_4 = requestedConfiguration3_4.streams[i];
921         device::V3_7::Stream &dst3_7 = requestedConfiguration3_7.streams[i];
922         camera3::camera_stream_t *src = config->streams[i];
923 
924         Camera3Stream* cam3stream = Camera3Stream::cast(src);
925         cam3stream->setBufferFreedListener(this);
926         int streamId = cam3stream->getId();
927         StreamType streamType;
928         switch (src->stream_type) {
929             case CAMERA_STREAM_OUTPUT:
930                 streamType = StreamType::OUTPUT;
931                 if (flags::session_hal_buf_manager() && mUseHalBufManager) {
932                     mHalBufManagedStreamIds.insert(streamId);
933                 }
934                 break;
935             case CAMERA_STREAM_INPUT:
936                 streamType = StreamType::INPUT;
937                 break;
938             default:
939                 ALOGE("%s: Stream %d: Unsupported stream type %d",
940                         __FUNCTION__, streamId, config->streams[i]->stream_type);
941                 return BAD_VALUE;
942         }
943 
944         dst3_2.id = streamId;
945         dst3_2.streamType = streamType;
946         dst3_2.width = src->width;
947         dst3_2.height = src->height;
948         dst3_2.usage = mapToConsumerUsage(cam3stream->getUsage());
949         dst3_2.rotation = mapToStreamRotation((camera_stream_rotation_t) src->rotation);
950         // For HidlSession version 3.5 or newer, the format and dataSpace sent
951         // to HAL are original, not the overridden ones.
952         if (mHidlSession_3_5 != nullptr) {
953             dst3_2.format = mapToPixelFormat(cam3stream->isFormatOverridden() ?
954                     cam3stream->getOriginalFormat() : src->format);
955             dst3_2.dataSpace = mapToHidlDataspace(cam3stream->isDataSpaceOverridden() ?
956                     cam3stream->getOriginalDataSpace() : src->data_space);
957         } else {
958             dst3_2.format = mapToPixelFormat(src->format);
959             dst3_2.dataSpace = mapToHidlDataspace(src->data_space);
960         }
961         dst3_4.v3_2 = dst3_2;
962         dst3_4.bufferSize = bufferSizes[i];
963         if (!src->physical_camera_id.empty()) {
964             dst3_4.physicalCameraId = src->physical_camera_id;
965         }
966         dst3_7.v3_4 = dst3_4;
967         dst3_7.groupId = cam3stream->getHalStreamGroupId();
968         dst3_7.sensorPixelModesUsed.resize(src->sensor_pixel_modes_used.size());
969         size_t j = 0;
970         for (int mode : src->sensor_pixel_modes_used) {
971             dst3_7.sensorPixelModesUsed[j++] =
972                     static_cast<CameraMetadataEnumAndroidSensorPixelMode>(mode);
973         }
974         if (src->dynamic_range_profile !=
975                     ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
976             ALOGE("%s: Camera device doesn't support non-standard dynamic range profiles: %" PRIx64,
977                     __FUNCTION__, src->dynamic_range_profile);
978             return BAD_VALUE;
979         }
980         if (src->use_case != ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT) {
981             ALOGE("%s: Camera device doesn't support non-default stream use case %" PRId64 "!",
982                     __FUNCTION__, src->use_case);
983             return BAD_VALUE;
984         }
985         activeStreams.insert(streamId);
986         // Create Buffer ID map if necessary
987         mBufferRecords.tryCreateBufferCache(streamId);
988     }
989     // remove BufferIdMap for deleted streams
990     mBufferRecords.removeInactiveBufferCaches(activeStreams);
991 
992     StreamConfigurationMode operationMode;
993     res = mapToStreamConfigurationMode(
994             (camera_stream_configuration_mode_t) config->operation_mode,
995             /*out*/ &operationMode);
996     if (res != OK) {
997         return res;
998     }
999     requestedConfiguration3_2.operationMode = operationMode;
1000     requestedConfiguration3_4.operationMode = operationMode;
1001     requestedConfiguration3_7.operationMode = operationMode;
1002     size_t sessionParamSize = get_camera_metadata_size(sessionParams);
1003     requestedConfiguration3_4.sessionParams.setToExternal(
1004             reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(sessionParams)),
1005             sessionParamSize);
1006     requestedConfiguration3_7.sessionParams.setToExternal(
1007             reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(sessionParams)),
1008             sessionParamSize);
1009 
1010     // Invoke configureStreams
1011     device::V3_3::HalStreamConfiguration finalConfiguration;
1012     device::V3_4::HalStreamConfiguration finalConfiguration3_4;
1013     device::V3_6::HalStreamConfiguration finalConfiguration3_6;
1014     common::V1_0::Status status;
1015 
1016     auto configStream34Cb = [&status, &finalConfiguration3_4]
1017             (common::V1_0::Status s, const device::V3_4::HalStreamConfiguration& halConfiguration) {
1018                 finalConfiguration3_4 = halConfiguration;
1019                 status = s;
1020             };
1021 
1022     auto configStream36Cb = [&status, &finalConfiguration3_6]
1023             (common::V1_0::Status s, const device::V3_6::HalStreamConfiguration& halConfiguration) {
1024                 finalConfiguration3_6 = halConfiguration;
1025                 status = s;
1026             };
1027 
1028     auto postprocConfigStream34 = [&finalConfiguration, &finalConfiguration3_4]
1029             (hardware::Return<void>& err) -> status_t {
1030                 if (!err.isOk()) {
1031                     ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
1032                     return DEAD_OBJECT;
1033                 }
1034                 finalConfiguration.streams.resize(finalConfiguration3_4.streams.size());
1035                 for (size_t i = 0; i < finalConfiguration3_4.streams.size(); i++) {
1036                     finalConfiguration.streams[i] = finalConfiguration3_4.streams[i].v3_3;
1037                 }
1038                 return OK;
1039             };
1040 
1041     auto postprocConfigStream36 = [&finalConfiguration, &finalConfiguration3_6]
1042             (hardware::Return<void>& err) -> status_t {
1043                 if (!err.isOk()) {
1044                     ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
1045                     return DEAD_OBJECT;
1046                 }
1047                 finalConfiguration.streams.resize(finalConfiguration3_6.streams.size());
1048                 for (size_t i = 0; i < finalConfiguration3_6.streams.size(); i++) {
1049                     finalConfiguration.streams[i] = finalConfiguration3_6.streams[i].v3_4.v3_3;
1050                 }
1051                 return OK;
1052             };
1053 
1054     if (mHidlSession_3_7 != nullptr) {
1055         ALOGV("%s: v3.7 device found", __FUNCTION__);
1056         requestedConfiguration3_7.streamConfigCounter = mNextStreamConfigCounter++;
1057         requestedConfiguration3_7.multiResolutionInputImage = config->input_is_multi_resolution;
1058         auto err = mHidlSession_3_7->configureStreams_3_7(
1059                 requestedConfiguration3_7, configStream36Cb);
1060         res = postprocConfigStream36(err);
1061         if (res != OK) {
1062             return res;
1063         }
1064     } else if (mHidlSession_3_6 != nullptr) {
1065         ALOGV("%s: v3.6 device found", __FUNCTION__);
1066         device::V3_5::StreamConfiguration requestedConfiguration3_5;
1067         requestedConfiguration3_5.v3_4 = requestedConfiguration3_4;
1068         requestedConfiguration3_5.streamConfigCounter = mNextStreamConfigCounter++;
1069         auto err = mHidlSession_3_6->configureStreams_3_6(
1070                 requestedConfiguration3_5, configStream36Cb);
1071         res = postprocConfigStream36(err);
1072         if (res != OK) {
1073             return res;
1074         }
1075     } else if (mHidlSession_3_5 != nullptr) {
1076         ALOGV("%s: v3.5 device found", __FUNCTION__);
1077         device::V3_5::StreamConfiguration requestedConfiguration3_5;
1078         requestedConfiguration3_5.v3_4 = requestedConfiguration3_4;
1079         requestedConfiguration3_5.streamConfigCounter = mNextStreamConfigCounter++;
1080         auto err = mHidlSession_3_5->configureStreams_3_5(
1081                 requestedConfiguration3_5, configStream34Cb);
1082         res = postprocConfigStream34(err);
1083         if (res != OK) {
1084             return res;
1085         }
1086     } else if (mHidlSession_3_4 != nullptr) {
1087         // We do; use v3.4 for the call
1088         ALOGV("%s: v3.4 device found", __FUNCTION__);
1089         auto err = mHidlSession_3_4->configureStreams_3_4(
1090                 requestedConfiguration3_4, configStream34Cb);
1091         res = postprocConfigStream34(err);
1092         if (res != OK) {
1093             return res;
1094         }
1095     } else if (mHidlSession_3_3 != nullptr) {
1096         // We do; use v3.3 for the call
1097         ALOGV("%s: v3.3 device found", __FUNCTION__);
1098         auto err = mHidlSession_3_3->configureStreams_3_3(requestedConfiguration3_2,
1099             [&status, &finalConfiguration]
1100             (common::V1_0::Status s, const device::V3_3::HalStreamConfiguration& halConfiguration) {
1101                 finalConfiguration = halConfiguration;
1102                 status = s;
1103             });
1104         if (!err.isOk()) {
1105             ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
1106             return DEAD_OBJECT;
1107         }
1108     } else {
1109         // We don't; use v3.2 call and construct a v3.3 HalStreamConfiguration
1110         ALOGV("%s: v3.2 device found", __FUNCTION__);
1111         HalStreamConfiguration finalConfiguration_3_2;
1112         auto err = mHidlSession->configureStreams(requestedConfiguration3_2,
1113                 [&status, &finalConfiguration_3_2]
1114                 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
1115                     finalConfiguration_3_2 = halConfiguration;
1116                     status = s;
1117                 });
1118         if (!err.isOk()) {
1119             ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
1120             return DEAD_OBJECT;
1121         }
1122         finalConfiguration.streams.resize(finalConfiguration_3_2.streams.size());
1123         for (size_t i = 0; i < finalConfiguration_3_2.streams.size(); i++) {
1124             finalConfiguration.streams[i].v3_2 = finalConfiguration_3_2.streams[i];
1125             finalConfiguration.streams[i].overrideDataSpace =
1126                     requestedConfiguration3_2.streams[i].dataSpace;
1127         }
1128     }
1129 
1130     if (status != common::V1_0::Status::OK ) {
1131         return HidlProviderInfo::mapToStatusT(status);
1132     }
1133 
1134     // And convert output stream configuration from HIDL
1135 
1136     for (size_t i = 0; i < config->num_streams; i++) {
1137         camera3::camera_stream_t *dst = config->streams[i];
1138         int streamId = Camera3Stream::cast(dst)->getId();
1139 
1140         // Start scan at i, with the assumption that the stream order matches
1141         size_t realIdx = i;
1142         bool found = false;
1143         size_t halStreamCount = finalConfiguration.streams.size();
1144         for (size_t idx = 0; idx < halStreamCount; idx++) {
1145             if (finalConfiguration.streams[realIdx].v3_2.id == streamId) {
1146                 found = true;
1147                 break;
1148             }
1149             realIdx = (realIdx >= halStreamCount - 1) ? 0 : realIdx + 1;
1150         }
1151         if (!found) {
1152             ALOGE("%s: Stream %d not found in stream configuration response from HAL",
1153                     __FUNCTION__, streamId);
1154             return INVALID_OPERATION;
1155         }
1156         device::V3_3::HalStream &src = finalConfiguration.streams[realIdx];
1157         device::V3_6::HalStream &src_36 = finalConfiguration3_6.streams[realIdx];
1158 
1159         Camera3Stream* dstStream = Camera3Stream::cast(dst);
1160         int overrideFormat = mapToFrameworkFormat(src.v3_2.overrideFormat);
1161         android_dataspace overrideDataSpace = mapToFrameworkDataspace(src.overrideDataSpace);
1162 
1163         if (mHidlSession_3_6 != nullptr) {
1164             dstStream->setOfflineProcessingSupport(src_36.supportOffline);
1165         }
1166 
1167         if (dstStream->getOriginalFormat() != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
1168             dstStream->setFormatOverride(false);
1169             dstStream->setDataSpaceOverride(false);
1170             if (dst->format != overrideFormat) {
1171                 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
1172                         streamId, dst->format);
1173             }
1174             if (dst->data_space != overrideDataSpace) {
1175                 ALOGE("%s: Stream %d: DataSpace override not allowed for format 0x%x", __FUNCTION__,
1176                         streamId, dst->format);
1177             }
1178         } else {
1179             bool needFormatOverride =
1180                     requestedConfiguration3_2.streams[i].format != src.v3_2.overrideFormat;
1181             bool needDataspaceOverride =
1182                     requestedConfiguration3_2.streams[i].dataSpace != src.overrideDataSpace;
1183             // Override allowed with IMPLEMENTATION_DEFINED
1184             dstStream->setFormatOverride(needFormatOverride);
1185             dstStream->setDataSpaceOverride(needDataspaceOverride);
1186             dst->format = overrideFormat;
1187             dst->data_space = overrideDataSpace;
1188         }
1189 
1190         if (dst->stream_type == CAMERA_STREAM_INPUT) {
1191             if (src.v3_2.producerUsage != 0) {
1192                 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
1193                         __FUNCTION__, streamId);
1194                 return INVALID_OPERATION;
1195             }
1196             dstStream->setUsage(
1197                     mapConsumerToFrameworkUsage(src.v3_2.consumerUsage));
1198         } else {
1199             // OUTPUT
1200             if (src.v3_2.consumerUsage != 0) {
1201                 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
1202                         __FUNCTION__, streamId);
1203                 return INVALID_OPERATION;
1204             }
1205             dstStream->setUsage(
1206                     mapProducerToFrameworkUsage(src.v3_2.producerUsage));
1207         }
1208         dst->max_buffers = src.v3_2.maxBuffers;
1209     }
1210 
1211     return res;
1212 }
1213 
configureInjectedStreams(const camera_metadata_t * sessionParams,camera_stream_configuration * config,const std::vector<uint32_t> & bufferSizes,const CameraMetadata & cameraCharacteristics)1214 status_t HidlCamera3Device::HidlHalInterface::configureInjectedStreams(
1215         const camera_metadata_t* sessionParams, camera_stream_configuration* config,
1216         const std::vector<uint32_t>& bufferSizes,
1217         const CameraMetadata& cameraCharacteristics) {
1218     ATRACE_NAME("InjectionCameraHal::configureStreams");
1219     if (!valid()) return INVALID_OPERATION;
1220     status_t res = OK;
1221 
1222     if (config->input_is_multi_resolution) {
1223         ALOGE("%s: Injection camera device doesn't support multi-resolution input "
1224                 "stream", __FUNCTION__);
1225         return BAD_VALUE;
1226     }
1227 
1228     // Convert stream config to HIDL
1229     std::set<int> activeStreams;
1230     device::V3_2::StreamConfiguration requestedConfiguration3_2;
1231     device::V3_4::StreamConfiguration requestedConfiguration3_4;
1232     device::V3_7::StreamConfiguration requestedConfiguration3_7;
1233     requestedConfiguration3_2.streams.resize(config->num_streams);
1234     requestedConfiguration3_4.streams.resize(config->num_streams);
1235     requestedConfiguration3_7.streams.resize(config->num_streams);
1236     for (size_t i = 0; i < config->num_streams; i++) {
1237         device::V3_2::Stream& dst3_2 = requestedConfiguration3_2.streams[i];
1238         device::V3_4::Stream& dst3_4 = requestedConfiguration3_4.streams[i];
1239         device::V3_7::Stream& dst3_7 = requestedConfiguration3_7.streams[i];
1240         camera3::camera_stream_t* src = config->streams[i];
1241 
1242         Camera3Stream* cam3stream = Camera3Stream::cast(src);
1243         cam3stream->setBufferFreedListener(this);
1244         int streamId = cam3stream->getId();
1245         StreamType streamType;
1246         switch (src->stream_type) {
1247             case CAMERA_STREAM_OUTPUT:
1248                 streamType = StreamType::OUTPUT;
1249                 break;
1250             case CAMERA_STREAM_INPUT:
1251                 streamType = StreamType::INPUT;
1252                 break;
1253             default:
1254                 ALOGE("%s: Stream %d: Unsupported stream type %d", __FUNCTION__,
1255                         streamId, config->streams[i]->stream_type);
1256             return BAD_VALUE;
1257         }
1258         dst3_2.id = streamId;
1259         dst3_2.streamType = streamType;
1260         dst3_2.width = src->width;
1261         dst3_2.height = src->height;
1262         dst3_2.usage = mapToConsumerUsage(cam3stream->getUsage());
1263         dst3_2.rotation =
1264                 mapToStreamRotation((camera_stream_rotation_t)src->rotation);
1265         // For HidlSession version 3.5 or newer, the format and dataSpace sent
1266         // to HAL are original, not the overridden ones.
1267         if (mHidlSession_3_5 != nullptr) {
1268             dst3_2.format = mapToPixelFormat(cam3stream->isFormatOverridden()
1269                                             ? cam3stream->getOriginalFormat()
1270                                             : src->format);
1271             dst3_2.dataSpace =
1272                     mapToHidlDataspace(cam3stream->isDataSpaceOverridden()
1273                                     ? cam3stream->getOriginalDataSpace()
1274                                     : src->data_space);
1275         } else {
1276             dst3_2.format = mapToPixelFormat(src->format);
1277             dst3_2.dataSpace = mapToHidlDataspace(src->data_space);
1278         }
1279         dst3_4.v3_2 = dst3_2;
1280         dst3_4.bufferSize = bufferSizes[i];
1281         if (!src->physical_camera_id.empty()) {
1282             dst3_4.physicalCameraId = src->physical_camera_id;
1283         }
1284         dst3_7.v3_4 = dst3_4;
1285         dst3_7.groupId = cam3stream->getHalStreamGroupId();
1286         dst3_7.sensorPixelModesUsed.resize(src->sensor_pixel_modes_used.size());
1287         size_t j = 0;
1288         for (int mode : src->sensor_pixel_modes_used) {
1289             dst3_7.sensorPixelModesUsed[j++] =
1290                     static_cast<CameraMetadataEnumAndroidSensorPixelMode>(mode);
1291         }
1292         activeStreams.insert(streamId);
1293         // Create Buffer ID map if necessary
1294         mBufferRecords.tryCreateBufferCache(streamId);
1295     }
1296     // remove BufferIdMap for deleted streams
1297     mBufferRecords.removeInactiveBufferCaches(activeStreams);
1298 
1299     StreamConfigurationMode operationMode;
1300     res = mapToStreamConfigurationMode(
1301             (camera_stream_configuration_mode_t)config->operation_mode,
1302             /*out*/ &operationMode);
1303     if (res != OK) {
1304         return res;
1305     }
1306     requestedConfiguration3_7.operationMode = operationMode;
1307     size_t sessionParamSize = get_camera_metadata_size(sessionParams);
1308     requestedConfiguration3_7.operationMode = operationMode;
1309     requestedConfiguration3_7.sessionParams.setToExternal(
1310             reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(sessionParams)),
1311             sessionParamSize);
1312 
1313     // See which version of HAL we have
1314     if (mHidlSession_3_7 != nullptr) {
1315         requestedConfiguration3_7.streamConfigCounter = mNextStreamConfigCounter++;
1316         requestedConfiguration3_7.multiResolutionInputImage =
1317                 config->input_is_multi_resolution;
1318 
1319         const camera_metadata_t* rawMetadata = cameraCharacteristics.getAndLock();
1320         ::android::hardware::camera::device::V3_2::CameraMetadata hidlChars = {};
1321         hidlChars.setToExternal(
1322                 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(rawMetadata)),
1323                 get_camera_metadata_size(rawMetadata));
1324         cameraCharacteristics.unlock(rawMetadata);
1325 
1326         sp<hardware::camera::device::V3_7::ICameraInjectionSession>
1327                 hidlInjectionSession_3_7;
1328         auto castInjectionResult_3_7 =
1329                 device::V3_7::ICameraInjectionSession::castFrom(mHidlSession_3_7);
1330         if (castInjectionResult_3_7.isOk()) {
1331             hidlInjectionSession_3_7 = castInjectionResult_3_7;
1332         } else {
1333             ALOGE("%s: Transaction error: %s", __FUNCTION__,
1334                     castInjectionResult_3_7.description().c_str());
1335             return DEAD_OBJECT;
1336         }
1337 
1338         auto err = hidlInjectionSession_3_7->configureInjectionStreams(
1339                 requestedConfiguration3_7, hidlChars);
1340         if (!err.isOk()) {
1341             ALOGE("%s: Transaction error: %s", __FUNCTION__,
1342                     err.description().c_str());
1343             return DEAD_OBJECT;
1344         }
1345     } else {
1346         ALOGE("%s: mHidlSession_3_7 does not exist, the lowest version of injection "
1347                 "session is 3.7", __FUNCTION__);
1348         return DEAD_OBJECT;
1349     }
1350 
1351     return res;
1352 }
1353 
wrapAsHidlRequest(camera_capture_request_t * request,device::V3_2::CaptureRequest * captureRequest,std::vector<native_handle_t * > * handlesCreated,std::vector<std::pair<int32_t,int32_t>> * inflightBuffers)1354 status_t HidlCamera3Device::HidlHalInterface::wrapAsHidlRequest(camera_capture_request_t* request,
1355         /*out*/device::V3_2::CaptureRequest* captureRequest,
1356         /*out*/std::vector<native_handle_t*>* handlesCreated,
1357         /*out*/std::vector<std::pair<int32_t, int32_t>>* inflightBuffers) {
1358     ATRACE_CALL();
1359     if (captureRequest == nullptr || handlesCreated == nullptr || inflightBuffers == nullptr) {
1360         ALOGE("%s: captureRequest (%p), handlesCreated (%p), and inflightBuffers(%p) "
1361                 "must not be null", __FUNCTION__, captureRequest, handlesCreated, inflightBuffers);
1362         return BAD_VALUE;
1363     }
1364 
1365     captureRequest->frameNumber = request->frame_number;
1366 
1367     captureRequest->fmqSettingsSize = 0;
1368 
1369     {
1370         if (request->input_buffer != nullptr) {
1371             int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
1372             buffer_handle_t buf = *(request->input_buffer->buffer);
1373             auto pair = getBufferId(buf, streamId);
1374             bool isNewBuffer = pair.first;
1375             uint64_t bufferId = pair.second;
1376             captureRequest->inputBuffer.streamId = streamId;
1377             captureRequest->inputBuffer.bufferId = bufferId;
1378             captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
1379             captureRequest->inputBuffer.status = BufferStatus::OK;
1380             native_handle_t *acquireFence = nullptr;
1381             if (request->input_buffer->acquire_fence != -1) {
1382                 acquireFence = native_handle_create(1,0);
1383                 acquireFence->data[0] = request->input_buffer->acquire_fence;
1384                 handlesCreated->push_back(acquireFence);
1385             }
1386             captureRequest->inputBuffer.acquireFence = acquireFence;
1387             captureRequest->inputBuffer.releaseFence = nullptr;
1388 
1389             mBufferRecords.pushInflightBuffer(captureRequest->frameNumber, streamId,
1390                     request->input_buffer->buffer);
1391             inflightBuffers->push_back(std::make_pair(captureRequest->frameNumber, streamId));
1392         } else {
1393             captureRequest->inputBuffer.streamId = -1;
1394             captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
1395         }
1396 
1397         captureRequest->outputBuffers.resize(request->num_output_buffers);
1398         for (size_t i = 0; i < request->num_output_buffers; i++) {
1399             const camera_stream_buffer_t *src = request->output_buffers + i;
1400             StreamBuffer &dst = captureRequest->outputBuffers[i];
1401             int32_t streamId = Camera3Stream::cast(src->stream)->getId();
1402             if (src->buffer != nullptr) {
1403                 buffer_handle_t buf = *(src->buffer);
1404                 auto pair = getBufferId(buf, streamId);
1405                 bool isNewBuffer = pair.first;
1406                 dst.bufferId = pair.second;
1407                 dst.buffer = isNewBuffer ? buf : nullptr;
1408                 native_handle_t *acquireFence = nullptr;
1409                 if (src->acquire_fence != -1) {
1410                     acquireFence = native_handle_create(1,0);
1411                     acquireFence->data[0] = src->acquire_fence;
1412                     handlesCreated->push_back(acquireFence);
1413                 }
1414                 dst.acquireFence = acquireFence;
1415             } else if (mUseHalBufManager) {
1416                 // HAL buffer management path
1417                 dst.bufferId = BUFFER_ID_NO_BUFFER;
1418                 dst.buffer = nullptr;
1419                 dst.acquireFence = nullptr;
1420             } else {
1421                 ALOGE("%s: cannot send a null buffer in capture request!", __FUNCTION__);
1422                 return BAD_VALUE;
1423             }
1424             dst.streamId = streamId;
1425             dst.status = BufferStatus::OK;
1426             dst.releaseFence = nullptr;
1427 
1428             // Output buffers are empty when using HAL buffer manager
1429             if (!mUseHalBufManager) {
1430                 mBufferRecords.pushInflightBuffer(
1431                         captureRequest->frameNumber, streamId, src->buffer);
1432                 inflightBuffers->push_back(std::make_pair(captureRequest->frameNumber, streamId));
1433             }
1434         }
1435     }
1436     return OK;
1437 }
1438 
flush()1439 status_t HidlCamera3Device::HidlHalInterface::flush() {
1440     ATRACE_NAME("CameraHal::flush");
1441     if (!valid()) return INVALID_OPERATION;
1442     status_t res = OK;
1443 
1444     auto err = mHidlSession->flush();
1445     if (!err.isOk()) {
1446         ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
1447         res = DEAD_OBJECT;
1448     } else {
1449         res = HidlProviderInfo::mapToStatusT(err);
1450     }
1451 
1452     return res;
1453 }
1454 
dump(int)1455 status_t HidlCamera3Device::HidlHalInterface::dump(int /*fd*/) {
1456     ATRACE_NAME("CameraHal::dump");
1457     if (!valid()) return INVALID_OPERATION;
1458 
1459     // Handled by CameraProviderManager::dump
1460 
1461     return OK;
1462 }
1463 
repeatingRequestEnd(uint32_t,const std::vector<int32_t> &)1464 status_t HidlCamera3Device::HidlHalInterface::repeatingRequestEnd(uint32_t /*frameNumber*/,
1465         const std::vector<int32_t> &/*streamIds*/) {
1466     ATRACE_NAME("CameraHal::repeatingRequestEnd");
1467     return INVALID_OPERATION;
1468 }
1469 
close()1470 status_t HidlCamera3Device::HidlHalInterface::close() {
1471     ATRACE_NAME("CameraHal::close()");
1472     if (!valid()) return INVALID_OPERATION;
1473     status_t res = OK;
1474 
1475     auto err = mHidlSession->close();
1476     // Interface will be dead shortly anyway, so don't log errors
1477     if (!err.isOk()) {
1478         res = DEAD_OBJECT;
1479     }
1480 
1481     return res;
1482 }
1483 
signalPipelineDrain(const std::vector<int> & streamIds)1484 void HidlCamera3Device::HidlHalInterface::signalPipelineDrain(const std::vector<int>& streamIds) {
1485     ATRACE_NAME("CameraHal::signalPipelineDrain");
1486     if (!valid() || mHidlSession_3_5 == nullptr) {
1487         ALOGE("%s called on invalid camera!", __FUNCTION__);
1488         return;
1489     }
1490 
1491     auto err = mHidlSession_3_5->signalStreamFlush(streamIds, mNextStreamConfigCounter - 1);
1492     if (!err.isOk()) {
1493         ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
1494         return;
1495     }
1496 }
1497 
processBatchCaptureRequests(std::vector<camera_capture_request_t * > & requests,uint32_t * numRequestProcessed)1498 status_t HidlCamera3Device::HidlHalInterface::processBatchCaptureRequests(
1499         std::vector<camera_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) {
1500     ATRACE_NAME("CameraHal::processBatchCaptureRequests");
1501     if (!valid()) return INVALID_OPERATION;
1502 
1503     sp<device::V3_4::ICameraDeviceSession> hidlSession_3_4;
1504     sp<device::V3_7::ICameraDeviceSession> hidlSession_3_7;
1505     auto castResult_3_7 = device::V3_7::ICameraDeviceSession::castFrom(mHidlSession);
1506     if (castResult_3_7.isOk()) {
1507         hidlSession_3_7 = castResult_3_7;
1508     }
1509     auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
1510     if (castResult_3_4.isOk()) {
1511         hidlSession_3_4 = castResult_3_4;
1512     }
1513 
1514     hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests;
1515     hardware::hidl_vec<device::V3_4::CaptureRequest> captureRequests_3_4;
1516     hardware::hidl_vec<device::V3_7::CaptureRequest> captureRequests_3_7;
1517     size_t batchSize = requests.size();
1518     if (hidlSession_3_7 != nullptr) {
1519         captureRequests_3_7.resize(batchSize);
1520     } else if (hidlSession_3_4 != nullptr) {
1521         captureRequests_3_4.resize(batchSize);
1522     } else {
1523         captureRequests.resize(batchSize);
1524     }
1525     std::vector<native_handle_t*> handlesCreated;
1526     std::vector<std::pair<int32_t, int32_t>> inflightBuffers;
1527 
1528     status_t res = OK;
1529     for (size_t i = 0; i < batchSize; i++) {
1530         if (hidlSession_3_7 != nullptr) {
1531             res = wrapAsHidlRequest(requests[i], /*out*/&captureRequests_3_7[i].v3_4.v3_2,
1532                     /*out*/&handlesCreated, /*out*/&inflightBuffers);
1533         } else if (hidlSession_3_4 != nullptr) {
1534             res = wrapAsHidlRequest(requests[i], /*out*/&captureRequests_3_4[i].v3_2,
1535                     /*out*/&handlesCreated, /*out*/&inflightBuffers);
1536         } else {
1537             res = wrapAsHidlRequest(requests[i], /*out*/&captureRequests[i],
1538                     /*out*/&handlesCreated, /*out*/&inflightBuffers);
1539         }
1540         if (res != OK) {
1541             mBufferRecords.popInflightBuffers(inflightBuffers);
1542             cleanupNativeHandles(&handlesCreated);
1543             return res;
1544         }
1545     }
1546 
1547     std::vector<device::V3_2::BufferCache> cachesToRemove;
1548     {
1549         std::lock_guard<std::mutex> lock(mFreedBuffersLock);
1550         for (auto& pair : mFreedBuffers) {
1551             // The stream might have been removed since onBufferFreed
1552             if (mBufferRecords.isStreamCached(pair.first)) {
1553                 cachesToRemove.push_back({pair.first, pair.second});
1554             }
1555         }
1556         mFreedBuffers.clear();
1557     }
1558 
1559     common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
1560     *numRequestProcessed = 0;
1561 
1562     // Write metadata to FMQ.
1563     for (size_t i = 0; i < batchSize; i++) {
1564         camera_capture_request_t* request = requests[i];
1565         device::V3_2::CaptureRequest* captureRequest;
1566         if (hidlSession_3_7 != nullptr) {
1567             captureRequest = &captureRequests_3_7[i].v3_4.v3_2;
1568         } else if (hidlSession_3_4 != nullptr) {
1569             captureRequest = &captureRequests_3_4[i].v3_2;
1570         } else {
1571             captureRequest = &captureRequests[i];
1572         }
1573 
1574         if (request->settings != nullptr) {
1575             size_t settingsSize = get_camera_metadata_size(request->settings);
1576             if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
1577                     reinterpret_cast<const uint8_t*>(request->settings), settingsSize)) {
1578                 captureRequest->settings.resize(0);
1579                 captureRequest->fmqSettingsSize = settingsSize;
1580             } else {
1581                 if (mRequestMetadataQueue != nullptr) {
1582                     ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
1583                 }
1584                 captureRequest->settings.setToExternal(
1585                         reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(
1586                                 request->settings)),
1587                         get_camera_metadata_size(request->settings));
1588                 captureRequest->fmqSettingsSize = 0u;
1589             }
1590         } else {
1591             // A null request settings maps to a size-0 CameraMetadata
1592             captureRequest->settings.resize(0);
1593             captureRequest->fmqSettingsSize = 0u;
1594         }
1595 
1596         // hidl session 3.7 specific handling.
1597         if (hidlSession_3_7 != nullptr) {
1598             captureRequests_3_7[i].inputWidth = request->input_width;
1599             captureRequests_3_7[i].inputHeight = request->input_height;
1600         }
1601 
1602         // hidl session 3.7 and 3.4 specific handling.
1603         if (hidlSession_3_7 != nullptr || hidlSession_3_4 != nullptr) {
1604             hardware::hidl_vec<device::V3_4::PhysicalCameraSetting>& physicalCameraSettings =
1605                     (hidlSession_3_7 != nullptr) ?
1606                     captureRequests_3_7[i].v3_4.physicalCameraSettings :
1607                     captureRequests_3_4[i].physicalCameraSettings;
1608             physicalCameraSettings.resize(request->num_physcam_settings);
1609             for (size_t j = 0; j < request->num_physcam_settings; j++) {
1610                 if (request->physcam_settings != nullptr) {
1611                     size_t settingsSize = get_camera_metadata_size(request->physcam_settings[j]);
1612                     if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
1613                                 reinterpret_cast<const uint8_t*>(request->physcam_settings[j]),
1614                                 settingsSize)) {
1615                         physicalCameraSettings[j].settings.resize(0);
1616                         physicalCameraSettings[j].fmqSettingsSize = settingsSize;
1617                     } else {
1618                         if (mRequestMetadataQueue != nullptr) {
1619                             ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
1620                         }
1621                         physicalCameraSettings[j].settings.setToExternal(
1622                                 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(
1623                                         request->physcam_settings[j])),
1624                                 get_camera_metadata_size(request->physcam_settings[j]));
1625                         physicalCameraSettings[j].fmqSettingsSize = 0u;
1626                     }
1627                 } else {
1628                     physicalCameraSettings[j].fmqSettingsSize = 0u;
1629                     physicalCameraSettings[j].settings.resize(0);
1630                 }
1631                 physicalCameraSettings[j].physicalCameraId = request->physcam_id[j];
1632             }
1633         }
1634     }
1635 
1636     hardware::details::return_status err;
1637     auto resultCallback =
1638         [&status, &numRequestProcessed] (auto s, uint32_t n) {
1639                 status = s;
1640                 *numRequestProcessed = n;
1641         };
1642     if (hidlSession_3_7 != nullptr) {
1643         err = hidlSession_3_7->processCaptureRequest_3_7(captureRequests_3_7, cachesToRemove,
1644                                                          resultCallback);
1645     } else if (hidlSession_3_4 != nullptr) {
1646         err = hidlSession_3_4->processCaptureRequest_3_4(captureRequests_3_4, cachesToRemove,
1647                                                          resultCallback);
1648     } else {
1649         err = mHidlSession->processCaptureRequest(captureRequests, cachesToRemove,
1650                                                   resultCallback);
1651     }
1652     if (!err.isOk()) {
1653         ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
1654         status = common::V1_0::Status::CAMERA_DISCONNECTED;
1655     }
1656 
1657     if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) {
1658         ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests",
1659                 __FUNCTION__, *numRequestProcessed, batchSize);
1660         status = common::V1_0::Status::INTERNAL_ERROR;
1661     }
1662 
1663     res = HidlProviderInfo::mapToStatusT(status);
1664     if (res == OK) {
1665         if (mHidlSession->isRemote()) {
1666             // Only close acquire fence FDs when the HIDL transaction succeeds (so the FDs have been
1667             // sent to camera HAL processes)
1668             cleanupNativeHandles(&handlesCreated, /*closeFd*/true);
1669         } else {
1670             // In passthrough mode the FDs are now owned by HAL
1671             cleanupNativeHandles(&handlesCreated);
1672         }
1673     } else {
1674         mBufferRecords.popInflightBuffers(inflightBuffers);
1675         cleanupNativeHandles(&handlesCreated);
1676     }
1677     return res;
1678 }
1679 
switchToOffline(const std::vector<int32_t> & streamsToKeep,hardware::camera::device::V3_6::CameraOfflineSessionInfo * offlineSessionInfo,sp<hardware::camera::device::V3_6::ICameraOfflineSession> * offlineSession,camera3::BufferRecords * bufferRecords)1680 status_t HidlCamera3Device::HidlHalInterface::switchToOffline(
1681         const std::vector<int32_t>& streamsToKeep,
1682         /*out*/hardware::camera::device::V3_6::CameraOfflineSessionInfo* offlineSessionInfo,
1683         /*out*/sp<hardware::camera::device::V3_6::ICameraOfflineSession>* offlineSession,
1684         /*out*/camera3::BufferRecords* bufferRecords) {
1685     ATRACE_NAME("CameraHal::switchToOffline");
1686     if (!valid() || mHidlSession_3_6 == nullptr) {
1687         ALOGE("%s called on invalid camera!", __FUNCTION__);
1688         return INVALID_OPERATION;
1689     }
1690 
1691     if (offlineSessionInfo == nullptr || offlineSession == nullptr || bufferRecords == nullptr) {
1692         ALOGE("%s: output arguments must not be null!", __FUNCTION__);
1693         return INVALID_OPERATION;
1694     }
1695 
1696     common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
1697     auto resultCallback =
1698         [&status, &offlineSessionInfo, &offlineSession] (auto s, auto info, auto session) {
1699                 status = s;
1700                 *offlineSessionInfo = info;
1701                 *offlineSession = session;
1702         };
1703     auto err = mHidlSession_3_6->switchToOffline(streamsToKeep, resultCallback);
1704 
1705     if (!err.isOk()) {
1706         ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
1707         return DEAD_OBJECT;
1708     }
1709 
1710     status_t ret = HidlProviderInfo::mapToStatusT(status);
1711     if (ret != OK) {
1712         return ret;
1713     }
1714 
1715     return verifyBufferCaches(offlineSessionInfo, bufferRecords);
1716 }
1717 
HidlRequestThread(wp<Camera3Device> parent,sp<camera3::StatusTracker> statusTracker,sp<HalInterface> interface,const Vector<int32_t> & sessionParamKeys,bool useHalBufManager,bool supportCameraMute,int rotationOverride,bool supportSettingsOverride)1718 HidlCamera3Device::HidlRequestThread::HidlRequestThread(wp<Camera3Device> parent,
1719                 sp<camera3::StatusTracker> statusTracker,
1720                 sp<HalInterface> interface,
1721                 const Vector<int32_t>& sessionParamKeys,
1722                 bool useHalBufManager,
1723                 bool supportCameraMute,
1724                 int rotationOverride,
1725                 bool supportSettingsOverride) :
1726           RequestThread(parent, statusTracker, interface, sessionParamKeys, useHalBufManager,
1727                   supportCameraMute, rotationOverride, supportSettingsOverride) {}
1728 
switchToOffline(const std::vector<int32_t> & streamsToKeep,hardware::camera::device::V3_6::CameraOfflineSessionInfo * offlineSessionInfo,sp<hardware::camera::device::V3_6::ICameraOfflineSession> * offlineSession,camera3::BufferRecords * bufferRecords)1729 status_t HidlCamera3Device::HidlRequestThread::switchToOffline(
1730         const std::vector<int32_t>& streamsToKeep,
1731         /*out*/hardware::camera::device::V3_6::CameraOfflineSessionInfo* offlineSessionInfo,
1732         /*out*/sp<hardware::camera::device::V3_6::ICameraOfflineSession>* offlineSession,
1733         /*out*/camera3::BufferRecords* bufferRecords) {
1734     Mutex::Autolock l(mRequestLock);
1735     clearRepeatingRequestsLocked(/*lastFrameNumber*/nullptr);
1736 
1737     // Wait until request thread is fully stopped
1738     // TBD: check if request thread is being paused by other APIs (shouldn't be)
1739 
1740     // We could also check for mRepeatingRequests.empty(), but the API interface
1741     // is serialized by Camera3Device::mInterfaceLock so no one should be able to submit any
1742     // new requests during the call; hence skip that check.
1743     bool queueEmpty = mNextRequests.empty() && mRequestQueue.empty();
1744     while (!queueEmpty) {
1745         status_t res = mRequestSubmittedSignal.waitRelative(mRequestLock, kRequestSubmitTimeout);
1746         if (res == TIMED_OUT) {
1747             ALOGE("%s: request thread failed to submit one request within timeout!", __FUNCTION__);
1748             return res;
1749         } else if (res != OK) {
1750             ALOGE("%s: request thread failed to submit a request: %s (%d)!",
1751                     __FUNCTION__, strerror(-res), res);
1752             return res;
1753         }
1754         queueEmpty = mNextRequests.empty() && mRequestQueue.empty();
1755     }
1756     return (static_cast<HidlHalInterface *>(mInterface.get()))->switchToOffline(
1757             streamsToKeep, offlineSessionInfo, offlineSession, bufferRecords);
1758 }
1759 
injectionInitialize(const std::string & injectedCamId,sp<CameraProviderManager> manager,const sp<android::hardware::camera::device::V3_2::ICameraDeviceCallback> & callback)1760 status_t HidlCamera3Device::HidlCamera3DeviceInjectionMethods::injectionInitialize(
1761         const std::string& injectedCamId, sp<CameraProviderManager> manager,
1762         const sp<android::hardware::camera::device::V3_2::ICameraDeviceCallback>&
1763                 callback) {
1764     ATRACE_CALL();
1765     Mutex::Autolock lock(mInjectionLock);
1766 
1767     if (manager == nullptr) {
1768         ALOGE("%s: manager does not exist!", __FUNCTION__);
1769         return INVALID_OPERATION;
1770     }
1771 
1772     sp<Camera3Device> parent = mParent.promote();
1773     if (parent == nullptr) {
1774         ALOGE("%s: parent does not exist!", __FUNCTION__);
1775         return INVALID_OPERATION;
1776     }
1777 
1778     mInjectedCamId = injectedCamId;
1779     sp<ICameraDeviceSession> session;
1780     ATRACE_BEGIN("Injection CameraHal::openSession");
1781     status_t res = manager->openHidlSession(injectedCamId, callback,
1782                                           /*out*/ &session);
1783     ATRACE_END();
1784     if (res != OK) {
1785         ALOGE("Injection camera could not open camera session: %s (%d)",
1786                 strerror(-res), res);
1787         return res;
1788     }
1789 
1790     std::shared_ptr<RequestMetadataQueue> queue;
1791     auto requestQueueRet =
1792         session->getCaptureRequestMetadataQueue([&queue](const auto& descriptor) {
1793             queue = std::make_shared<RequestMetadataQueue>(descriptor);
1794             if (!queue->isValid() || queue->availableToWrite() <= 0) {
1795                 ALOGE("Injection camera HAL returns empty request metadata fmq, not "
1796                         "use it");
1797                 queue = nullptr;
1798                 // don't use the queue onwards.
1799             }
1800         });
1801     if (!requestQueueRet.isOk()) {
1802         ALOGE("Injection camera transaction error when getting request metadata fmq: "
1803                 "%s, not use it", requestQueueRet.description().c_str());
1804         return DEAD_OBJECT;
1805     }
1806 
1807     std::unique_ptr<ResultMetadataQueue>& resQueue = mInjectionResultMetadataQueue;
1808     auto resultQueueRet = session->getCaptureResultMetadataQueue(
1809         [&resQueue](const auto& descriptor) {
1810             resQueue = std::make_unique<ResultMetadataQueue>(descriptor);
1811             if (!resQueue->isValid() || resQueue->availableToWrite() <= 0) {
1812                 ALOGE("Injection camera HAL returns empty result metadata fmq, not use "
1813                         "it");
1814                 resQueue = nullptr;
1815                 // Don't use the resQueue onwards.
1816             }
1817         });
1818     if (!resultQueueRet.isOk()) {
1819         ALOGE("Injection camera transaction error when getting result metadata queue "
1820                 "from camera session: %s", resultQueueRet.description().c_str());
1821         return DEAD_OBJECT;
1822     }
1823     IF_ALOGV() {
1824         session->interfaceChain(
1825                 [](::android::hardware::hidl_vec<::android::hardware::hidl_string>
1826                         interfaceChain) {
1827                         ALOGV("Injection camera session interface chain:");
1828                         for (const auto& iface : interfaceChain) {
1829                             ALOGV("  %s", iface.c_str());
1830                         }
1831                 });
1832     }
1833 
1834     ALOGV("%s: Injection camera interface = new HalInterface()", __FUNCTION__);
1835 
1836     mInjectedCamHalInterface =
1837             new HidlHalInterface(session, queue, parent->mUseHalBufManager,
1838                        parent->mSupportOfflineProcessing);
1839     if (mInjectedCamHalInterface == nullptr) {
1840         ALOGE("%s: mInjectedCamHalInterface does not exist!", __FUNCTION__);
1841         return DEAD_OBJECT;
1842     }
1843 
1844     return OK;
1845 }
1846 
replaceHalInterface(sp<HalInterface> newHalInterface,bool keepBackup)1847 status_t HidlCamera3Device::HidlCamera3DeviceInjectionMethods::replaceHalInterface(
1848         sp<HalInterface> newHalInterface, bool keepBackup) {
1849     Mutex::Autolock lock(mInjectionLock);
1850     if (newHalInterface.get() == nullptr) {
1851         ALOGE("%s: The newHalInterface does not exist, to stop replacing.",
1852                 __FUNCTION__);
1853         return DEAD_OBJECT;
1854     }
1855 
1856     sp<Camera3Device> parent = mParent.promote();
1857     if (parent == nullptr) {
1858         ALOGE("%s: parent does not exist!", __FUNCTION__);
1859         return INVALID_OPERATION;
1860     }
1861     if (newHalInterface->getTransportType() != IPCTransport::HIDL) {
1862         ALOGE("%s Replacing HIDL HalInterface with another transport unsupported", __FUNCTION__);
1863         return INVALID_OPERATION;
1864     }
1865 
1866     HidlCamera3Device *hidlParent = static_cast<HidlCamera3Device *>(parent.get());
1867     if (keepBackup) {
1868         if (mBackupHalInterface == nullptr) {
1869             mBackupHalInterface = parent->mInterface;
1870         }
1871         if (mBackupResultMetadataQueue == nullptr) {
1872             mBackupResultMetadataQueue = std::move(hidlParent->mResultMetadataQueue);
1873             hidlParent->mResultMetadataQueue = std::move(mInjectionResultMetadataQueue);
1874         }
1875     } else {
1876         mBackupHalInterface = nullptr;
1877         hidlParent->mResultMetadataQueue = std::move(mBackupResultMetadataQueue);
1878         mBackupResultMetadataQueue = nullptr;
1879     }
1880     parent->mInterface = newHalInterface;
1881 
1882     return OK;
1883 }
1884 
1885 }; // namespace android
1886