• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013-2018 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 #ifndef ANDROID_SERVERS_CAMERA3DEVICE_H
18 #define ANDROID_SERVERS_CAMERA3DEVICE_H
19 
20 #include <utility>
21 #include <unordered_map>
22 #include <set>
23 
24 #include <utils/Condition.h>
25 #include <utils/Errors.h>
26 #include <utils/List.h>
27 #include <utils/Mutex.h>
28 #include <utils/Thread.h>
29 #include <utils/KeyedVector.h>
30 #include <utils/Timers.h>
31 
32 #include <android/hardware/camera/device/3.2/ICameraDevice.h>
33 #include <android/hardware/camera/device/3.2/ICameraDeviceSession.h>
34 #include <android/hardware/camera/device/3.3/ICameraDeviceSession.h>
35 #include <android/hardware/camera/device/3.4/ICameraDeviceSession.h>
36 #include <android/hardware/camera/device/3.5/ICameraDeviceSession.h>
37 #include <android/hardware/camera/device/3.2/ICameraDeviceCallback.h>
38 #include <android/hardware/camera/device/3.4/ICameraDeviceCallback.h>
39 #include <android/hardware/camera/device/3.5/ICameraDeviceCallback.h>
40 #include <fmq/MessageQueue.h>
41 
42 #include <camera/CaptureResult.h>
43 
44 #include "common/CameraDeviceBase.h"
45 #include "device3/StatusTracker.h"
46 #include "device3/Camera3BufferManager.h"
47 #include "device3/DistortionMapper.h"
48 #include "utils/TagMonitor.h"
49 #include "utils/LatencyHistogram.h"
50 #include <camera_metadata_hidden.h>
51 
52 using android::camera3::OutputStreamInfo;
53 
54 namespace android {
55 
56 namespace camera3 {
57 
58 class Camera3Stream;
59 class Camera3ZslStream;
60 class Camera3OutputStreamInterface;
61 class Camera3StreamInterface;
62 
63 } // namespace camera3
64 
65 /**
66  * CameraDevice for HAL devices with version CAMERA_DEVICE_API_VERSION_3_0 or higher.
67  */
68 class Camera3Device :
69             public CameraDeviceBase,
70             virtual public hardware::camera::device::V3_5::ICameraDeviceCallback {
71   public:
72 
73     explicit Camera3Device(const String8& id);
74 
75     virtual ~Camera3Device();
76 
77     /**
78      * CameraDeviceBase interface
79      */
80 
81     const String8& getId() const override;
82 
getVendorTagId()83     metadata_vendor_id_t getVendorTagId() const override { return mVendorTagId; }
84 
85     // Transitions to idle state on success.
86     status_t initialize(sp<CameraProviderManager> manager, const String8& monitorTags) override;
87     status_t disconnect() override;
88     status_t dump(int fd, const Vector<String16> &args) override;
89     const CameraMetadata& info() const override;
90     const CameraMetadata& info(const String8& physicalId) const override;
91 
92     // Capture and setStreamingRequest will configure streams if currently in
93     // idle state
94     status_t capture(CameraMetadata &request, int64_t *lastFrameNumber = NULL) override;
95     status_t captureList(const List<const PhysicalCameraSettingsList> &requestsList,
96             const std::list<const SurfaceMap> &surfaceMaps,
97             int64_t *lastFrameNumber = NULL) override;
98     status_t setStreamingRequest(const CameraMetadata &request,
99             int64_t *lastFrameNumber = NULL) override;
100     status_t setStreamingRequestList(const List<const PhysicalCameraSettingsList> &requestsList,
101             const std::list<const SurfaceMap> &surfaceMaps,
102             int64_t *lastFrameNumber = NULL) override;
103     status_t clearStreamingRequest(int64_t *lastFrameNumber = NULL) override;
104 
105     status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) override;
106 
107     // Actual stream creation/deletion is delayed until first request is submitted
108     // If adding streams while actively capturing, will pause device before adding
109     // stream, reconfiguring device, and unpausing. If the client create a stream
110     // with nullptr consumer surface, the client must then call setConsumers()
111     // and finish the stream configuration before starting output streaming.
112     status_t createStream(sp<Surface> consumer,
113             uint32_t width, uint32_t height, int format,
114             android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
115             const String8& physicalCameraId,
116             std::vector<int> *surfaceIds = nullptr,
117             int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID,
118             bool isShared = false, uint64_t consumerUsage = 0) override;
119     status_t createStream(const std::vector<sp<Surface>>& consumers,
120             bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
121             android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
122             const String8& physicalCameraId,
123             std::vector<int> *surfaceIds = nullptr,
124             int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID,
125             bool isShared = false, uint64_t consumerUsage = 0) override;
126 
127     status_t createInputStream(
128             uint32_t width, uint32_t height, int format,
129             int *id) override;
130 
131     status_t getStreamInfo(int id, StreamInfo *streamInfo) override;
132     status_t setStreamTransform(int id, int transform) override;
133 
134     status_t deleteStream(int id) override;
135 
136     status_t configureStreams(const CameraMetadata& sessionParams,
137             int operatingMode =
138             static_cast<int>(hardware::camera::device::V3_2::StreamConfigurationMode::NORMAL_MODE))
139             override;
140     status_t getInputBufferProducer(
141             sp<IGraphicBufferProducer> *producer) override;
142 
143     status_t createDefaultRequest(int templateId, CameraMetadata *request) override;
144 
145     // Transitions to the idle state on success
146     status_t waitUntilDrained() override;
147 
148     status_t setNotifyCallback(wp<NotificationListener> listener) override;
149     bool     willNotify3A() override;
150     status_t waitForNextFrame(nsecs_t timeout) override;
151     status_t getNextResult(CaptureResult *frame) override;
152 
153     status_t triggerAutofocus(uint32_t id) override;
154     status_t triggerCancelAutofocus(uint32_t id) override;
155     status_t triggerPrecaptureMetering(uint32_t id) override;
156 
157     status_t flush(int64_t *lastFrameNumber = NULL) override;
158 
159     status_t prepare(int streamId) override;
160 
161     status_t tearDown(int streamId) override;
162 
163     status_t addBufferListenerForStream(int streamId,
164             wp<camera3::Camera3StreamBufferListener> listener) override;
165 
166     status_t prepare(int maxCount, int streamId) override;
167 
168     ssize_t getJpegBufferSize(uint32_t width, uint32_t height) const override;
169     ssize_t getPointCloudBufferSize() const;
170     ssize_t getRawOpaqueBufferSize(int32_t width, int32_t height) const;
171 
172     // Methods called by subclasses
173     void             notifyStatus(bool idle); // updates from StatusTracker
174 
175     /**
176      * Set the deferred consumer surfaces to the output stream and finish the deferred
177      * consumer configuration.
178      */
179     status_t setConsumerSurfaces(
180             int streamId, const std::vector<sp<Surface>>& consumers,
181             std::vector<int> *surfaceIds /*out*/) override;
182 
183     /**
184      * Update a given stream.
185      */
186     status_t updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
187             const std::vector<OutputStreamInfo> &outputInfo,
188             const std::vector<size_t> &removedSurfaceIds,
189             KeyedVector<sp<Surface>, size_t> *outputMap/*out*/);
190 
191     /**
192      * Drop buffers for stream of streamId if dropping is true. If dropping is false, do not
193      * drop buffers for stream of streamId.
194      */
195     status_t dropStreamBuffers(bool dropping, int streamId) override;
196 
197     /**
198      * Helper functions to map between framework and HIDL values
199      */
200     static hardware::graphics::common::V1_0::PixelFormat mapToPixelFormat(int frameworkFormat);
201     static hardware::camera::device::V3_2::DataspaceFlags mapToHidlDataspace(
202             android_dataspace dataSpace);
203     static hardware::camera::device::V3_2::BufferUsageFlags mapToConsumerUsage(uint64_t usage);
204     static hardware::camera::device::V3_2::StreamRotation mapToStreamRotation(
205             camera3_stream_rotation_t rotation);
206     // Returns a negative error code if the passed-in operation mode is not valid.
207     static status_t mapToStreamConfigurationMode(camera3_stream_configuration_mode_t operationMode,
208             /*out*/ hardware::camera::device::V3_2::StreamConfigurationMode *mode);
209     static camera3_buffer_status_t mapHidlBufferStatus(
210             hardware::camera::device::V3_2::BufferStatus status);
211     static int mapToFrameworkFormat(hardware::graphics::common::V1_0::PixelFormat pixelFormat);
212     static android_dataspace mapToFrameworkDataspace(
213             hardware::camera::device::V3_2::DataspaceFlags);
214     static uint64_t mapConsumerToFrameworkUsage(
215             hardware::camera::device::V3_2::BufferUsageFlags usage);
216     static uint64_t mapProducerToFrameworkUsage(
217             hardware::camera::device::V3_2::BufferUsageFlags usage);
218 
219   private:
220 
221     status_t disconnectImpl();
222 
223     // internal typedefs
224     using RequestMetadataQueue = hardware::MessageQueue<uint8_t, hardware::kSynchronizedReadWrite>;
225     using ResultMetadataQueue  = hardware::MessageQueue<uint8_t, hardware::kSynchronizedReadWrite>;
226 
227     static const size_t        kDumpLockAttempts  = 10;
228     static const size_t        kDumpSleepDuration = 100000; // 0.10 sec
229     static const nsecs_t       kActiveTimeout     = 500000000;  // 500 ms
230     static const size_t        kInFlightWarnLimit = 30;
231     static const size_t        kInFlightWarnLimitHighSpeed = 256; // batch size 32 * pipe depth 8
232     static const nsecs_t       kDefaultExpectedDuration = 100000000; // 100 ms
233     static const nsecs_t       kMinInflightDuration = 5000000000; // 5 s
234     static const nsecs_t       kBaseGetBufferWait = 3000000000; // 3 sec.
235     // SCHED_FIFO priority for request submission thread in HFR mode
236     static const int           kRequestThreadPriority = 1;
237 
238     struct                     RequestTrigger;
239     // minimal jpeg buffer size: 256KB + blob header
240     static const ssize_t       kMinJpegBufferSize = 256 * 1024 + sizeof(camera3_jpeg_blob);
241     // Constant to use for stream ID when one doesn't exist
242     static const int           NO_STREAM = -1;
243 
244     // A lock to enforce serialization on the input/configure side
245     // of the public interface.
246     // Only locked by public methods inherited from CameraDeviceBase.
247     // Not locked by methods guarded by mOutputLock, since they may act
248     // concurrently to the input/configure side of the interface.
249     // Must be locked before mLock if both will be locked by a method
250     Mutex                      mInterfaceLock;
251 
252     // The main lock on internal state
253     Mutex                      mLock;
254 
255     // Camera device ID
256     const String8              mId;
257 
258     // Current stream configuration mode;
259     int                        mOperatingMode;
260     // Current session wide parameters
261     hardware::camera2::impl::CameraMetadataNative mSessionParams;
262 
263     // Constant to use for no set operating mode
264     static const int           NO_MODE = -1;
265 
266     // Flag indicating is the current active stream configuration is constrained high speed.
267     bool                       mIsConstrainedHighSpeedConfiguration;
268 
269     // FMQ to write result on. Must be guarded by mProcessCaptureResultLock.
270     std::unique_ptr<ResultMetadataQueue> mResultMetadataQueue;
271 
272     /**** Scope for mLock ****/
273 
274     /**
275      * Adapter for legacy HAL / HIDL HAL interface calls; calls either into legacy HALv3 or the
276      * HIDL HALv3 interfaces.
277      */
278     class HalInterface : public camera3::Camera3StreamBufferFreedListener {
279       public:
280         HalInterface(sp<hardware::camera::device::V3_2::ICameraDeviceSession> &session,
281                      std::shared_ptr<RequestMetadataQueue> queue,
282                      bool useHalBufManager);
283         HalInterface(const HalInterface &other);
284         HalInterface();
285 
286         // Returns true if constructed with a valid device or session, and not yet cleared
287         bool valid();
288 
289         // Reset this HalInterface object (does not call close())
290         void clear();
291 
292         // Calls into the HAL interface
293 
294         // Caller takes ownership of requestTemplate
295         status_t constructDefaultRequestSettings(camera3_request_template_t templateId,
296                 /*out*/ camera_metadata_t **requestTemplate);
297         status_t configureStreams(const camera_metadata_t *sessionParams,
298                 /*inout*/ camera3_stream_configuration *config,
299                 const std::vector<uint32_t>& bufferSizes);
300 
301         // When the call succeeds, the ownership of acquire fences in requests is transferred to
302         // HalInterface. More specifically, the current implementation will send the fence to
303         // HAL process and close the FD in cameraserver process. When the call fails, the ownership
304         // of the acquire fence still belongs to the caller.
305         status_t processBatchCaptureRequests(
306                 std::vector<camera3_capture_request_t*>& requests,
307                 /*out*/uint32_t* numRequestProcessed);
308         status_t flush();
309         status_t dump(int fd);
310         status_t close();
311 
312         void signalPipelineDrain(const std::vector<int>& streamIds);
313         bool isReconfigurationRequired(CameraMetadata& oldSessionParams,
314                 CameraMetadata& newSessionParams);
315 
316         // method to extract buffer's unique ID
317         // return pair of (newlySeenBuffer?, bufferId)
318         std::pair<bool, uint64_t> getBufferId(const buffer_handle_t& buf, int streamId);
319 
320         // Find a buffer_handle_t based on frame number and stream ID
321         status_t popInflightBuffer(int32_t frameNumber, int32_t streamId,
322                 /*out*/ buffer_handle_t **buffer);
323 
324         // Register a bufId (streamId, buffer_handle_t) to inflight request buffer
325         status_t pushInflightRequestBuffer(
326                 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId);
327 
328         // Find a buffer_handle_t based on bufferId
329         status_t popInflightRequestBuffer(uint64_t bufferId,
330                 /*out*/ buffer_handle_t** buffer,
331                 /*optional out*/ int32_t* streamId = nullptr);
332 
333         // Get a vector of (frameNumber, streamId) pair of currently inflight
334         // buffers
335         void getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out);
336 
337         // Get a vector of bufferId of currently inflight buffers
338         void getInflightRequestBufferKeys(std::vector<uint64_t>* out);
339 
340         void onStreamReConfigured(int streamId);
341 
342         static const uint64_t BUFFER_ID_NO_BUFFER = 0;
343       private:
344         // Always valid
345         sp<hardware::camera::device::V3_2::ICameraDeviceSession> mHidlSession;
346         // Valid if ICameraDeviceSession is @3.3 or newer
347         sp<hardware::camera::device::V3_3::ICameraDeviceSession> mHidlSession_3_3;
348         // Valid if ICameraDeviceSession is @3.4 or newer
349         sp<hardware::camera::device::V3_4::ICameraDeviceSession> mHidlSession_3_4;
350         // Valid if ICameraDeviceSession is @3.5 or newer
351         sp<hardware::camera::device::V3_5::ICameraDeviceSession> mHidlSession_3_5;
352 
353         std::shared_ptr<RequestMetadataQueue> mRequestMetadataQueue;
354 
355         std::mutex mInflightLock;
356 
357         // The output HIDL request still depends on input camera3_capture_request_t
358         // Do not free input camera3_capture_request_t before output HIDL request
359         status_t wrapAsHidlRequest(camera3_capture_request_t* in,
360                 /*out*/hardware::camera::device::V3_2::CaptureRequest* out,
361                 /*out*/std::vector<native_handle_t*>* handlesCreated,
362                 /*out*/std::vector<std::pair<int32_t, int32_t>>* inflightBuffers);
363 
364         status_t pushInflightBufferLocked(int32_t frameNumber, int32_t streamId,
365                 buffer_handle_t *buffer);
366 
367         // Pop inflight buffers based on pairs of (frameNumber,streamId)
368         void popInflightBuffers(const std::vector<std::pair<int32_t, int32_t>>& buffers);
369 
370         // Cache of buffer handles keyed off (frameNumber << 32 | streamId)
371         std::unordered_map<uint64_t, buffer_handle_t*> mInflightBufferMap;
372 
373         // Delete and optionally close native handles and clear the input vector afterward
374         static void cleanupNativeHandles(
375                 std::vector<native_handle_t*> *handles, bool closeFd = false);
376 
377         struct BufferHasher {
operatorBufferHasher378             size_t operator()(const buffer_handle_t& buf) const {
379                 if (buf == nullptr)
380                     return 0;
381 
382                 size_t result = 1;
383                 result = 31 * result + buf->numFds;
384                 for (int i = 0; i < buf->numFds; i++) {
385                     result = 31 * result + buf->data[i];
386                 }
387                 return result;
388             }
389         };
390 
391         struct BufferComparator {
operatorBufferComparator392             bool operator()(const buffer_handle_t& buf1, const buffer_handle_t& buf2) const {
393                 if (buf1->numFds == buf2->numFds) {
394                     for (int i = 0; i < buf1->numFds; i++) {
395                         if (buf1->data[i] != buf2->data[i]) {
396                             return false;
397                         }
398                     }
399                     return true;
400                 }
401                 return false;
402             }
403         };
404 
405         std::mutex mBufferIdMapLock; // protecting mBufferIdMaps and mNextBufferId
406         typedef std::unordered_map<const buffer_handle_t, uint64_t,
407                 BufferHasher, BufferComparator> BufferIdMap;
408         // stream ID -> per stream buffer ID map
409         std::unordered_map<int, BufferIdMap> mBufferIdMaps;
410         uint64_t mNextBufferId = 1; // 0 means no buffer
411 
412         virtual void onBufferFreed(int streamId, const native_handle_t* handle) override;
413 
414         std::vector<std::pair<int, uint64_t>> mFreedBuffers;
415 
416         // Buffers given to HAL through requestStreamBuffer API
417         std::mutex mRequestedBuffersLock;
418         std::unordered_map<uint64_t, std::pair<int32_t, buffer_handle_t*>> mRequestedBuffers;
419 
420         uint32_t mNextStreamConfigCounter = 1;
421 
422         const bool mUseHalBufManager;
423         bool mIsReconfigurationQuerySupported;
424     };
425 
426     sp<HalInterface> mInterface;
427 
428     CameraMetadata             mDeviceInfo;
429     std::unordered_map<std::string, CameraMetadata> mPhysicalDeviceInfoMap;
430 
431     CameraMetadata             mRequestTemplateCache[CAMERA3_TEMPLATE_COUNT];
432 
433     struct Size {
434         uint32_t width;
435         uint32_t height;
widthSize436         explicit Size(uint32_t w = 0, uint32_t h = 0) : width(w), height(h){}
437     };
438     // Map from format to size.
439     Vector<Size>               mSupportedOpaqueInputSizes;
440 
441     enum Status {
442         STATUS_ERROR,
443         STATUS_UNINITIALIZED,
444         STATUS_UNCONFIGURED,
445         STATUS_CONFIGURED,
446         STATUS_ACTIVE
447     }                          mStatus;
448 
449     // Only clear mRecentStatusUpdates, mStatusWaiters from waitUntilStateThenRelock
450     Vector<Status>             mRecentStatusUpdates;
451     int                        mStatusWaiters;
452 
453     Condition                  mStatusChanged;
454 
455     // Tracking cause of fatal errors when in STATUS_ERROR
456     String8                    mErrorCause;
457 
458     // Synchronized mapping of stream IDs to stream instances
459     class StreamSet {
460       public:
461         status_t add(int streamId, sp<camera3::Camera3OutputStreamInterface>);
462         ssize_t remove(int streamId);
463         sp<camera3::Camera3OutputStreamInterface> get(int streamId);
464         // get by (underlying) vector index
465         sp<camera3::Camera3OutputStreamInterface> operator[] (size_t index);
466         size_t size() const;
467         std::vector<int> getStreamIds();
468         void clear();
469 
470       private:
471         mutable std::mutex mLock;
472         KeyedVector<int, sp<camera3::Camera3OutputStreamInterface>> mData;
473     };
474 
475     StreamSet                  mOutputStreams;
476     sp<camera3::Camera3Stream> mInputStream;
477     int                        mNextStreamId;
478     bool                       mNeedConfig;
479 
480     int                        mDummyStreamId;
481 
482     // Whether to send state updates upstream
483     // Pause when doing transparent reconfiguration
484     bool                       mPauseStateNotify;
485 
486     // Need to hold on to stream references until configure completes.
487     Vector<sp<camera3::Camera3StreamInterface> > mDeletedStreams;
488 
489     // Whether the HAL will send partial result
490     bool                       mUsePartialResult;
491 
492     // Number of partial results that will be delivered by the HAL.
493     uint32_t                   mNumPartialResults;
494 
495     /**** End scope for mLock ****/
496 
497     // The offset converting from clock domain of other subsystem
498     // (video/hardware composer) to that of camera. Assumption is that this
499     // offset won't change during the life cycle of the camera device. In other
500     // words, camera device shouldn't be open during CPU suspend.
501     nsecs_t                    mTimestampOffset;
502 
503     class CaptureRequest : public LightRefBase<CaptureRequest> {
504       public:
505         PhysicalCameraSettingsList          mSettingsList;
506         sp<camera3::Camera3Stream>          mInputStream;
507         camera3_stream_buffer_t             mInputBuffer;
508         Vector<sp<camera3::Camera3OutputStreamInterface> >
509                                             mOutputStreams;
510         SurfaceMap                          mOutputSurfaces;
511         CaptureResultExtras                 mResultExtras;
512         // The number of requests that should be submitted to HAL at a time.
513         // For example, if batch size is 8, this request and the following 7
514         // requests will be submitted to HAL at a time. The batch size for
515         // the following 7 requests will be ignored by the request thread.
516         int                                 mBatchSize;
517         //  Whether this request is from a repeating or repeating burst.
518         bool                                mRepeating;
519     };
520     typedef List<sp<CaptureRequest> > RequestList;
521 
522     status_t checkStatusOkToCaptureLocked();
523 
524     status_t convertMetadataListToRequestListLocked(
525             const List<const PhysicalCameraSettingsList> &metadataList,
526             const std::list<const SurfaceMap> &surfaceMaps,
527             bool repeating,
528             /*out*/
529             RequestList *requestList);
530 
531     void convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
532             std::list<const SurfaceMap>& surfaceMaps,
533             const CameraMetadata& request);
534 
535     status_t submitRequestsHelper(const List<const PhysicalCameraSettingsList> &requestsList,
536                                   const std::list<const SurfaceMap> &surfaceMaps,
537                                   bool repeating,
538                                   int64_t *lastFrameNumber = NULL);
539 
540 
541     /**
542      * Implementation of android::hardware::camera::device::V3_5::ICameraDeviceCallback
543      */
544 
545     hardware::Return<void> processCaptureResult_3_4(
546             const hardware::hidl_vec<
547                     hardware::camera::device::V3_4::CaptureResult>& results) override;
548     hardware::Return<void> processCaptureResult(
549             const hardware::hidl_vec<
550                     hardware::camera::device::V3_2::CaptureResult>& results) override;
551     hardware::Return<void> notify(
552             const hardware::hidl_vec<
553                     hardware::camera::device::V3_2::NotifyMsg>& msgs) override;
554 
555     hardware::Return<void> requestStreamBuffers(
556             const hardware::hidl_vec<
557                     hardware::camera::device::V3_5::BufferRequest>& bufReqs,
558             requestStreamBuffers_cb _hidl_cb) override;
559 
560     hardware::Return<void> returnStreamBuffers(
561             const hardware::hidl_vec<
562                     hardware::camera::device::V3_2::StreamBuffer>& buffers) override;
563 
564     // Handle one capture result. Assume that mProcessCaptureResultLock is held.
565     void processOneCaptureResultLocked(
566             const hardware::camera::device::V3_2::CaptureResult& result,
567             const hardware::hidl_vec<
568             hardware::camera::device::V3_4::PhysicalCameraMetadata> physicalCameraMetadata);
569     status_t readOneCameraMetadataLocked(uint64_t fmqResultSize,
570             hardware::camera::device::V3_2::CameraMetadata& resultMetadata,
571             const hardware::camera::device::V3_2::CameraMetadata& result);
572 
573     // Handle one notify message
574     void notify(const hardware::camera::device::V3_2::NotifyMsg& msg);
575 
576     // lock to ensure only one processCaptureResult is called at a time.
577     Mutex mProcessCaptureResultLock;
578 
579     /**
580      * Common initialization code shared by both HAL paths
581      *
582      * Must be called with mLock and mInterfaceLock held.
583      */
584     status_t initializeCommonLocked();
585 
586     /**
587      * Get the last request submitted to the hal by the request thread.
588      *
589      * Must be called with mLock held.
590      */
591     virtual CameraMetadata getLatestRequestLocked();
592 
593     /**
594      * Update the current device status and wake all waiting threads.
595      *
596      * Must be called with mLock held.
597      */
598     void internalUpdateStatusLocked(Status status);
599 
600     /**
601      * Pause processing and flush everything, but don't tell the clients.
602      * This is for reconfiguring outputs transparently when according to the
603      * CameraDeviceBase interface we shouldn't need to.
604      * Must be called with mLock and mInterfaceLock both held.
605      */
606     status_t internalPauseAndWaitLocked(nsecs_t maxExpectedDuration);
607 
608     /**
609      * Resume work after internalPauseAndWaitLocked()
610      * Must be called with mLock and mInterfaceLock both held.
611      */
612     status_t internalResumeLocked();
613 
614     /**
615      * Wait until status tracker tells us we've transitioned to the target state
616      * set, which is either ACTIVE when active==true or IDLE (which is any
617      * non-ACTIVE state) when active==false.
618      *
619      * Needs to be called with mLock and mInterfaceLock held.  This means there
620      * can ever only be one waiter at most.
621      *
622      * During the wait mLock is released.
623      *
624      */
625     status_t waitUntilStateThenRelock(bool active, nsecs_t timeout);
626 
627     /**
628      * Implementation of waitUntilDrained. On success, will transition to IDLE state.
629      *
630      * Need to be called with mLock and mInterfaceLock held.
631      */
632     status_t waitUntilDrainedLocked(nsecs_t maxExpectedDuration);
633 
634     /**
635      * Do common work for setting up a streaming or single capture request.
636      * On success, will transition to ACTIVE if in IDLE.
637      */
638     sp<CaptureRequest> setUpRequestLocked(const PhysicalCameraSettingsList &request,
639                                           const SurfaceMap &surfaceMap);
640 
641     /**
642      * Build a CaptureRequest request from the CameraDeviceBase request
643      * settings.
644      */
645     sp<CaptureRequest> createCaptureRequest(const PhysicalCameraSettingsList &request,
646                                             const SurfaceMap &surfaceMap);
647 
648     /**
649      * Pause state updates to the client application.  Needed to mask out idle/active
650      * transitions during internal reconfigure
651      */
652     void pauseStateNotify(bool enable);
653 
654     /**
655      * Internally re-configure camera device using new session parameters.
656      * This will get triggered by the request thread. Be sure to call
657      * pauseStateNotify(true) before going idle in the requesting location.
658      */
659     bool reconfigureCamera(const CameraMetadata& sessionParams);
660 
661     /**
662      * Filter stream session parameters and configure camera HAL.
663      */
664     status_t filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
665             int operatingMode);
666 
667     /**
668      * Take the currently-defined set of streams and configure the HAL to use
669      * them. This is a long-running operation (may be several hundered ms).
670      */
671     status_t           configureStreamsLocked(int operatingMode,
672             const CameraMetadata& sessionParams, bool notifyRequestThread = true);
673 
674     /**
675      * Cancel stream configuration that did not finish successfully.
676      */
677     void               cancelStreamsConfigurationLocked();
678 
679     /**
680      * Add a dummy stream to the current stream set as a workaround for
681      * not allowing 0 streams in the camera HAL spec.
682      */
683     status_t           addDummyStreamLocked();
684 
685     /**
686      * Remove a dummy stream if the current config includes real streams.
687      */
688     status_t           tryRemoveDummyStreamLocked();
689 
690     /**
691      * Set device into an error state due to some fatal failure, and set an
692      * error message to indicate why. Only the first call's message will be
693      * used. The message is also sent to the log.
694      */
695     void               setErrorState(const char *fmt, ...);
696     void               setErrorStateV(const char *fmt, va_list args);
697     void               setErrorStateLocked(const char *fmt, ...);
698     void               setErrorStateLockedV(const char *fmt, va_list args);
699 
700     /**
701      * Debugging trylock/spin method
702      * Try to acquire a lock a few times with sleeps between before giving up.
703      */
704     bool               tryLockSpinRightRound(Mutex& lock);
705 
706     /**
707      * Helper function to determine if an input size for implementation defined
708      * format is supported.
709      */
710     bool isOpaqueInputSizeSupported(uint32_t width, uint32_t height);
711 
712     /**
713      * Helper function to get the largest Jpeg resolution (in area)
714      * Return Size(0, 0) if static metatdata is invalid
715      */
716     Size getMaxJpegResolution() const;
717 
718     /**
719      * Helper function to get the offset between MONOTONIC and BOOTTIME
720      * timestamp.
721      */
722     static nsecs_t getMonoToBoottimeOffset();
723 
724     struct RequestTrigger {
725         // Metadata tag number, e.g. android.control.aePrecaptureTrigger
726         uint32_t metadataTag;
727         // Metadata value, e.g. 'START' or the trigger ID
728         int32_t entryValue;
729 
730         // The last part of the fully qualified path, e.g. afTrigger
getTagNameRequestTrigger731         const char *getTagName() const {
732             return get_camera_metadata_tag_name(metadataTag) ?: "NULL";
733         }
734 
735         // e.g. TYPE_BYTE, TYPE_INT32, etc.
getTagTypeRequestTrigger736         int getTagType() const {
737             return get_camera_metadata_tag_type(metadataTag);
738         }
739     };
740 
741     /**
742      * Thread for managing capture request submission to HAL device.
743      */
744     class RequestThread : public Thread {
745 
746       public:
747 
748         RequestThread(wp<Camera3Device> parent,
749                 sp<camera3::StatusTracker> statusTracker,
750                 sp<HalInterface> interface,
751                 const Vector<int32_t>& sessionParamKeys,
752                 bool useHalBufManager);
753         ~RequestThread();
754 
755         void     setNotificationListener(wp<NotificationListener> listener);
756 
757         /**
758          * Call after stream (re)-configuration is completed.
759          */
760         void     configurationComplete(bool isConstrainedHighSpeed,
761                 const CameraMetadata& sessionParams);
762 
763         /**
764          * Set or clear the list of repeating requests. Does not block
765          * on either. Use waitUntilPaused to wait until request queue
766          * has emptied out.
767          */
768         status_t setRepeatingRequests(const RequestList& requests,
769                                       /*out*/
770                                       int64_t *lastFrameNumber = NULL);
771         status_t clearRepeatingRequests(/*out*/
772                                         int64_t *lastFrameNumber = NULL);
773 
774         status_t queueRequestList(List<sp<CaptureRequest> > &requests,
775                                   /*out*/
776                                   int64_t *lastFrameNumber = NULL);
777 
778         /**
779          * Remove all queued and repeating requests, and pending triggers
780          */
781         status_t clear(/*out*/int64_t *lastFrameNumber = NULL);
782 
783         /**
784          * Flush all pending requests in HAL.
785          */
786         status_t flush();
787 
788         /**
789          * Queue a trigger to be dispatched with the next outgoing
790          * process_capture_request. The settings for that request only
791          * will be temporarily rewritten to add the trigger tag/value.
792          * Subsequent requests will not be rewritten (for this tag).
793          */
794         status_t queueTrigger(RequestTrigger trigger[], size_t count);
795 
796         /**
797          * Pause/unpause the capture thread. Doesn't block, so use
798          * waitUntilPaused to wait until the thread is paused.
799          */
800         void     setPaused(bool paused);
801 
802         /**
803          * Wait until thread processes the capture request with settings'
804          * android.request.id == requestId.
805          *
806          * Returns TIMED_OUT in case the thread does not process the request
807          * within the timeout.
808          */
809         status_t waitUntilRequestProcessed(int32_t requestId, nsecs_t timeout);
810 
811         /**
812          * Shut down the thread. Shutdown is asynchronous, so thread may
813          * still be running once this method returns.
814          */
815         virtual void requestExit();
816 
817         /**
818          * Get the latest request that was sent to the HAL
819          * with process_capture_request.
820          */
821         CameraMetadata getLatestRequest() const;
822 
823         /**
824          * Returns true if the stream is a target of any queued or repeating
825          * capture request
826          */
827         bool isStreamPending(sp<camera3::Camera3StreamInterface>& stream);
828 
829         /**
830          * Returns true if the surface is a target of any queued or repeating
831          * capture request
832          */
833         bool isOutputSurfacePending(int streamId, size_t surfaceId);
834 
835         // dump processCaptureRequest latency
dumpCaptureRequestLatency(int fd,const char * name)836         void dumpCaptureRequestLatency(int fd, const char* name) {
837             mRequestLatency.dump(fd, name);
838         }
839 
840         void signalPipelineDrain(const std::vector<int>& streamIds);
841 
842       protected:
843 
844         virtual bool threadLoop();
845 
846       private:
847         static const String8& getId(const wp<Camera3Device> &device);
848 
849         status_t           queueTriggerLocked(RequestTrigger trigger);
850         // Mix-in queued triggers into this request
851         int32_t            insertTriggers(const sp<CaptureRequest> &request);
852         // Purge the queued triggers from this request,
853         //  restoring the old field values for those tags.
854         status_t           removeTriggers(const sp<CaptureRequest> &request);
855 
856         // HAL workaround: Make sure a trigger ID always exists if
857         // a trigger does
858         status_t          addDummyTriggerIds(const sp<CaptureRequest> &request);
859 
860         static const nsecs_t kRequestTimeout = 50e6; // 50 ms
861 
862         // Used to prepare a batch of requests.
863         struct NextRequest {
864             sp<CaptureRequest>              captureRequest;
865             camera3_capture_request_t       halRequest;
866             Vector<camera3_stream_buffer_t> outputBuffers;
867             bool                            submitted;
868         };
869 
870         // Wait for the next batch of requests and put them in mNextRequests. mNextRequests will
871         // be empty if it times out.
872         void waitForNextRequestBatch();
873 
874         // Waits for a request, or returns NULL if times out. Must be called with mRequestLock hold.
875         sp<CaptureRequest> waitForNextRequestLocked();
876 
877         // Prepare HAL requests and output buffers in mNextRequests. Return TIMED_OUT if getting any
878         // output buffer timed out. If an error is returned, the caller should clean up the pending
879         // request batch.
880         status_t prepareHalRequests();
881 
882         // Return buffers, etc, for requests in mNextRequests that couldn't be fully constructed and
883         // send request errors if sendRequestError is true. The buffers will be returned in the
884         // ERROR state to mark them as not having valid data. mNextRequests will be cleared.
885         void cleanUpFailedRequests(bool sendRequestError);
886 
887         // Stop the repeating request if any of its output streams is abandoned.
888         void checkAndStopRepeatingRequest();
889 
890         // Release physical camera settings and camera id resources.
891         void cleanupPhysicalSettings(sp<CaptureRequest> request,
892                 /*out*/camera3_capture_request_t *halRequest);
893 
894         // Pause handling
895         bool               waitIfPaused();
896         void               unpauseForNewRequests();
897 
898         // Relay error to parent device object setErrorState
899         void               setErrorState(const char *fmt, ...);
900 
901         // If the input request is in mRepeatingRequests. Must be called with mRequestLock hold
902         bool isRepeatingRequestLocked(const sp<CaptureRequest>&);
903 
904         // Clear repeating requests. Must be called with mRequestLock held.
905         status_t clearRepeatingRequestsLocked(/*out*/ int64_t *lastFrameNumber = NULL);
906 
907         // send request in mNextRequests to HAL in a batch. Return true = sucssess
908         bool sendRequestsBatch();
909 
910         // Calculate the expected maximum duration for a request
911         nsecs_t calculateMaxExpectedDuration(const camera_metadata_t *request);
912 
913         // Check and update latest session parameters based on the current request settings.
914         bool updateSessionParameters(const CameraMetadata& settings);
915 
916         // Check whether FPS range session parameter re-configuration is needed in constrained
917         // high speed recording camera sessions.
918         bool skipHFRTargetFPSUpdate(int32_t tag, const camera_metadata_ro_entry_t& newEntry,
919                 const camera_metadata_entry_t& currentEntry);
920 
921         // Update next request sent to HAL
922         void updateNextRequest(NextRequest& nextRequest);
923 
924         wp<Camera3Device>  mParent;
925         wp<camera3::StatusTracker>  mStatusTracker;
926         sp<HalInterface>   mInterface;
927 
928         wp<NotificationListener> mListener;
929 
930         const String8&     mId;       // The camera ID
931         int                mStatusId; // The RequestThread's component ID for
932                                       // status tracking
933 
934         Mutex              mRequestLock;
935         Condition          mRequestSignal;
936         RequestList        mRequestQueue;
937         RequestList        mRepeatingRequests;
938         // The next batch of requests being prepped for submission to the HAL, no longer
939         // on the request queue. Read-only even with mRequestLock held, outside
940         // of threadLoop
941         Vector<NextRequest> mNextRequests;
942 
943         // To protect flush() and sending a request batch to HAL.
944         Mutex              mFlushLock;
945 
946         bool               mReconfigured;
947 
948         // Used by waitIfPaused, waitForNextRequest, waitUntilPaused, and signalPipelineDrain
949         Mutex              mPauseLock;
950         bool               mDoPause;
951         Condition          mDoPauseSignal;
952         bool               mPaused;
953         bool               mNotifyPipelineDrain;
954         std::vector<int>   mStreamIdsToBeDrained;
955 
956         sp<CaptureRequest> mPrevRequest;
957         int32_t            mPrevTriggers;
958 
959         uint32_t           mFrameNumber;
960 
961         mutable Mutex      mLatestRequestMutex;
962         Condition          mLatestRequestSignal;
963         // android.request.id for latest process_capture_request
964         int32_t            mLatestRequestId;
965         CameraMetadata     mLatestRequest;
966         std::unordered_map<std::string, CameraMetadata> mLatestPhysicalRequest;
967 
968         typedef KeyedVector<uint32_t/*tag*/, RequestTrigger> TriggerMap;
969         Mutex              mTriggerMutex;
970         TriggerMap         mTriggerMap;
971         TriggerMap         mTriggerRemovedMap;
972         TriggerMap         mTriggerReplacedMap;
973         uint32_t           mCurrentAfTriggerId;
974         uint32_t           mCurrentPreCaptureTriggerId;
975 
976         int64_t            mRepeatingLastFrameNumber;
977 
978         // Flag indicating if we should prepare video stream for video requests.
979         bool               mPrepareVideoStream;
980 
981         bool               mConstrainedMode;
982 
983         static const int32_t kRequestLatencyBinSize = 40; // in ms
984         CameraLatencyHistogram mRequestLatency;
985 
986         Vector<int32_t>    mSessionParamKeys;
987         CameraMetadata     mLatestSessionParams;
988 
989         const bool         mUseHalBufManager;
990     };
991     sp<RequestThread> mRequestThread;
992 
993     /**
994      * In-flight queue for tracking completion of capture requests.
995      */
996 
997     struct InFlightRequest {
998         // Set by notify() SHUTTER call.
999         nsecs_t shutterTimestamp;
1000         // Set by process_capture_result().
1001         nsecs_t sensorTimestamp;
1002         int     requestStatus;
1003         // Set by process_capture_result call with valid metadata
1004         bool    haveResultMetadata;
1005         // Decremented by calls to process_capture_result with valid output
1006         // and input buffers
1007         int     numBuffersLeft;
1008         CaptureResultExtras resultExtras;
1009         // If this request has any input buffer
1010         bool hasInputBuffer;
1011 
1012         // The last metadata that framework receives from HAL and
1013         // not yet send out because the shutter event hasn't arrived.
1014         // It's added by process_capture_result and sent when framework
1015         // receives the shutter event.
1016         CameraMetadata pendingMetadata;
1017 
1018         // The metadata of the partial results that framework receives from HAL so far
1019         // and has sent out.
1020         CameraMetadata collectedPartialResult;
1021 
1022         // Buffers are added by process_capture_result when output buffers
1023         // return from HAL but framework has not yet received the shutter
1024         // event. They will be returned to the streams when framework receives
1025         // the shutter event.
1026         Vector<camera3_stream_buffer_t> pendingOutputBuffers;
1027 
1028         // Whether this inflight request's shutter and result callback are to be
1029         // called. The policy is that if the request is the last one in the constrained
1030         // high speed recording request list, this flag will be true. If the request list
1031         // is not for constrained high speed recording, this flag will also be true.
1032         bool hasCallback;
1033 
1034         // Maximum expected frame duration for this request.
1035         // For manual captures, equal to the max of requested exposure time and frame duration
1036         // For auto-exposure modes, equal to 1/(lower end of target FPS range)
1037         nsecs_t maxExpectedDuration;
1038 
1039         // Whether the result metadata for this request is to be skipped. The
1040         // result metadata should be skipped in the case of
1041         // REQUEST/RESULT error.
1042         bool skipResultMetadata;
1043 
1044         // The physical camera ids being requested.
1045         std::set<String8> physicalCameraIds;
1046 
1047         // Map of physicalCameraId <-> Metadata
1048         std::vector<PhysicalCaptureResultInfo> physicalMetadatas;
1049 
1050         // Indicates a still capture request.
1051         bool stillCapture;
1052 
1053         // Indicates a ZSL capture request
1054         bool zslCapture;
1055 
1056         // What shared surfaces an output should go to
1057         SurfaceMap outputSurfaces;
1058 
1059         // Default constructor needed by KeyedVector
InFlightRequestInFlightRequest1060         InFlightRequest() :
1061                 shutterTimestamp(0),
1062                 sensorTimestamp(0),
1063                 requestStatus(OK),
1064                 haveResultMetadata(false),
1065                 numBuffersLeft(0),
1066                 hasInputBuffer(false),
1067                 hasCallback(true),
1068                 maxExpectedDuration(kDefaultExpectedDuration),
1069                 skipResultMetadata(false),
1070                 stillCapture(false),
1071                 zslCapture(false) {
1072         }
1073 
1074         InFlightRequest(int numBuffers, CaptureResultExtras extras, bool hasInput,
1075                 bool hasAppCallback, nsecs_t maxDuration,
1076                 const std::set<String8>& physicalCameraIdSet, bool isStillCapture,
1077                 bool isZslCapture,
1078                 const SurfaceMap& outSurfaces = SurfaceMap{}) :
1079                 shutterTimestamp(0),
1080                 sensorTimestamp(0),
1081                 requestStatus(OK),
1082                 haveResultMetadata(false),
1083                 numBuffersLeft(numBuffers),
1084                 resultExtras(extras),
1085                 hasInputBuffer(hasInput),
1086                 hasCallback(hasAppCallback),
1087                 maxExpectedDuration(maxDuration),
1088                 skipResultMetadata(false),
1089                 physicalCameraIds(physicalCameraIdSet),
1090                 stillCapture(isStillCapture),
1091                 zslCapture(isZslCapture),
1092                 outputSurfaces(outSurfaces) {
1093         }
1094     };
1095 
1096     // Map from frame number to the in-flight request state
1097     typedef KeyedVector<uint32_t, InFlightRequest> InFlightMap;
1098 
1099 
1100     Mutex                  mInFlightLock; // Protects mInFlightMap and
1101                                           // mExpectedInflightDuration
1102     InFlightMap            mInFlightMap;
1103     nsecs_t                mExpectedInflightDuration = 0;
1104     int                    mInFlightStatusId;
1105 
1106     status_t registerInFlight(uint32_t frameNumber,
1107             int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
1108             bool callback, nsecs_t maxExpectedDuration, std::set<String8>& physicalCameraIds,
1109             bool isStillCapture, bool isZslCapture,
1110             const SurfaceMap& outputSurfaces);
1111 
1112     /**
1113      * Returns the maximum expected time it'll take for all currently in-flight
1114      * requests to complete, based on their settings
1115      */
1116     nsecs_t getExpectedInFlightDuration();
1117 
1118     /**
1119      * Tracking for idle detection
1120      */
1121     sp<camera3::StatusTracker> mStatusTracker;
1122 
1123     /**
1124      * Graphic buffer manager for output streams. Each device has a buffer manager, which is used
1125      * by the output streams to get and return buffers if these streams are registered to this
1126      * buffer manager.
1127      */
1128     sp<camera3::Camera3BufferManager> mBufferManager;
1129 
1130     /**
1131      * Thread for preparing streams
1132      */
1133     class PreparerThread : private Thread, public virtual RefBase {
1134       public:
1135         PreparerThread();
1136         ~PreparerThread();
1137 
1138         void setNotificationListener(wp<NotificationListener> listener);
1139 
1140         /**
1141          * Queue up a stream to be prepared. Streams are processed by a background thread in FIFO
1142          * order.  Pre-allocate up to maxCount buffers for the stream, or the maximum number needed
1143          * for the pipeline if maxCount is ALLOCATE_PIPELINE_MAX.
1144          */
1145         status_t prepare(int maxCount, sp<camera3::Camera3StreamInterface>& stream);
1146 
1147         /**
1148          * Cancel all current and pending stream preparation
1149          */
1150         status_t clear();
1151 
1152         /**
1153          * Pause all preparation activities
1154          */
1155         void pause();
1156 
1157         /**
1158          * Resume preparation activities
1159          */
1160         status_t resume();
1161 
1162       private:
1163         Mutex mLock;
1164         Condition mThreadActiveSignal;
1165 
1166         virtual bool threadLoop();
1167 
1168         // Guarded by mLock
1169 
1170         wp<NotificationListener> mListener;
1171         std::unordered_map<int, sp<camera3::Camera3StreamInterface> > mPendingStreams;
1172         bool mActive;
1173         bool mCancelNow;
1174 
1175         // Only accessed by threadLoop and the destructor
1176 
1177         sp<camera3::Camera3StreamInterface> mCurrentStream;
1178         int mCurrentMaxCount;
1179         bool mCurrentPrepareComplete;
1180     };
1181     sp<PreparerThread> mPreparerThread;
1182 
1183     /**
1184      * Output result queue and current HAL device 3A state
1185      */
1186 
1187     // Lock for output side of device
1188     Mutex                  mOutputLock;
1189 
1190     /**** Scope for mOutputLock ****/
1191     // the minimal frame number of the next non-reprocess result
1192     uint32_t               mNextResultFrameNumber;
1193     // the minimal frame number of the next reprocess result
1194     uint32_t               mNextReprocessResultFrameNumber;
1195     // the minimal frame number of the next ZSL still capture result
1196     uint32_t               mNextZslStillResultFrameNumber;
1197     // the minimal frame number of the next non-reprocess shutter
1198     uint32_t               mNextShutterFrameNumber;
1199     // the minimal frame number of the next reprocess shutter
1200     uint32_t               mNextReprocessShutterFrameNumber;
1201     // the minimal frame number of the next ZSL still capture shutter
1202     uint32_t               mNextZslStillShutterFrameNumber;
1203     List<CaptureResult>   mResultQueue;
1204     Condition              mResultSignal;
1205     wp<NotificationListener>  mListener;
1206 
1207     /**** End scope for mOutputLock ****/
1208 
1209     /**
1210      * Callback functions from HAL device
1211      */
1212     void processCaptureResult(const camera3_capture_result *result);
1213 
1214     void notify(const camera3_notify_msg *msg);
1215 
1216     // Specific notify handlers
1217     void notifyError(const camera3_error_msg_t &msg,
1218             sp<NotificationListener> listener);
1219     void notifyShutter(const camera3_shutter_msg_t &msg,
1220             sp<NotificationListener> listener);
1221 
1222     // helper function to return the output buffers to the streams.
1223     void returnOutputBuffers(const camera3_stream_buffer_t *outputBuffers,
1224             size_t numBuffers, nsecs_t timestamp, bool timestampIncreasing = true,
1225             // The following arguments are only meant for surface sharing use case
1226             const SurfaceMap& outputSurfaces = SurfaceMap{},
1227             // Used to send buffer error callback when failing to return buffer
1228             const CaptureResultExtras &resultExtras = CaptureResultExtras{});
1229 
1230     // Send a partial capture result.
1231     void sendPartialCaptureResult(const camera_metadata_t * partialResult,
1232             const CaptureResultExtras &resultExtras, uint32_t frameNumber);
1233 
1234     // Send a total capture result given the pending metadata and result extras,
1235     // partial results, and the frame number to the result queue.
1236     void sendCaptureResult(CameraMetadata &pendingMetadata,
1237             CaptureResultExtras &resultExtras,
1238             CameraMetadata &collectedPartialResult, uint32_t frameNumber,
1239             bool reprocess, bool zslStillCapture,
1240             const std::vector<PhysicalCaptureResultInfo>& physicalMetadatas);
1241 
1242     bool isLastFullResult(const InFlightRequest& inFlightRequest);
1243 
1244     // Insert the result to the result queue after updating frame number and overriding AE
1245     // trigger cancel.
1246     // mOutputLock must be held when calling this function.
1247     void insertResultLocked(CaptureResult *result, uint32_t frameNumber);
1248 
1249     /**** Scope for mInFlightLock ****/
1250 
1251     // Remove the in-flight map entry of the given index from mInFlightMap.
1252     // It must only be called with mInFlightLock held.
1253     void removeInFlightMapEntryLocked(int idx);
1254     // Remove the in-flight request of the given index from mInFlightMap
1255     // if it's no longer needed. It must only be called with mInFlightLock held.
1256     void removeInFlightRequestIfReadyLocked(int idx);
1257     // Remove all in-flight requests and return all buffers.
1258     // This is used after HAL interface is closed to cleanup any request/buffers
1259     // not returned by HAL.
1260     void flushInflightRequests();
1261 
1262     /**** End scope for mInFlightLock ****/
1263 
1264     /**
1265      * Distortion correction support
1266      */
1267     // Map from camera IDs to its corresponding distortion mapper. Only contains
1268     // 1 ID if the device isn't a logical multi-camera. Otherwise contains both
1269     // logical camera and its physical subcameras.
1270     std::unordered_map<std::string, camera3::DistortionMapper> mDistortionMappers;
1271 
1272     // Debug tracker for metadata tag value changes
1273     // - Enabled with the -m <taglist> option to dumpsys, such as
1274     //   dumpsys -m android.control.aeState,android.control.aeMode
1275     // - Disabled with -m off
1276     // - dumpsys -m 3a is a shortcut for ae/af/awbMode, State, and Triggers
1277     TagMonitor mTagMonitor;
1278 
1279     void monitorMetadata(TagMonitor::eventSource source, int64_t frameNumber,
1280             nsecs_t timestamp, const CameraMetadata& metadata,
1281             const std::unordered_map<std::string, CameraMetadata>& physicalMetadata);
1282 
1283     metadata_vendor_id_t mVendorTagId;
1284 
1285     // Cached last requested template id
1286     int mLastTemplateId;
1287 
1288     // Synchronizes access to status tracker between inflight updates and disconnect.
1289     // b/79972865
1290     Mutex mTrackerLock;
1291 
1292     // Whether HAL request buffers through requestStreamBuffers API
1293     bool mUseHalBufManager = false;
1294 
1295     // Lock to ensure requestStreamBuffers() callbacks are serialized
1296     std::mutex mRequestBufferInterfaceLock;
1297 
1298     // The state machine to control when requestStreamBuffers should allow
1299     // HAL to request buffers.
1300     enum RequestBufferState {
1301         /**
1302          * This is the initial state.
1303          * requestStreamBuffers call will return FAILED_CONFIGURING in this state.
1304          * Will switch to RB_STATUS_READY after a successful configureStreams or
1305          * processCaptureRequest call.
1306          */
1307         RB_STATUS_STOPPED,
1308 
1309         /**
1310          * requestStreamBuffers call will proceed in this state.
1311          * When device is asked to stay idle via waitUntilStateThenRelock() call:
1312          *     - Switch to RB_STATUS_STOPPED if there is no inflight requests and
1313          *       request thread is paused.
1314          *     - Switch to RB_STATUS_PENDING_STOP otherwise
1315          */
1316         RB_STATUS_READY,
1317 
1318         /**
1319          * requestStreamBuffers call will proceed in this state.
1320          * Switch to RB_STATUS_STOPPED when all inflight requests are fulfilled
1321          * and request thread is paused
1322          */
1323         RB_STATUS_PENDING_STOP,
1324     };
1325 
1326     class RequestBufferStateMachine {
1327       public:
1328         status_t initialize(sp<camera3::StatusTracker> statusTracker);
1329 
1330         // Return if the state machine currently allows for requestBuffers
1331         // If the state allows for it, mRequestBufferOngoing will be set to true
1332         // and caller must call endRequestBuffer() later to unset the flag
1333         bool startRequestBuffer();
1334         void endRequestBuffer();
1335 
1336         // Events triggered by application API call
1337         void onStreamsConfigured();
1338         void onWaitUntilIdle();
1339 
1340         // Events usually triggered by hwBinder processCaptureResult callback thread
1341         // But can also be triggered on request thread for failed request, or on
1342         // hwbinder notify callback thread for shutter/error callbacks
1343         void onInflightMapEmpty();
1344 
1345         // Events triggered by RequestThread
1346         void onSubmittingRequest();
1347         void onRequestThreadPaused();
1348 
1349       private:
1350         void notifyTrackerLocked(bool active);
1351 
1352         // Switch to STOPPED state and return true if all conditions allows for it.
1353         // Otherwise do nothing and return false.
1354         bool checkSwitchToStopLocked();
1355 
1356         std::mutex mLock;
1357         RequestBufferState mStatus = RB_STATUS_STOPPED;
1358 
1359         bool mRequestThreadPaused = true;
1360         bool mInflightMapEmpty = true;
1361         bool mRequestBufferOngoing = false;
1362 
1363         wp<camera3::StatusTracker> mStatusTracker;
1364         int  mRequestBufferStatusId;
1365     } mRequestBufferSM;
1366 
1367     // Fix up result metadata for monochrome camera.
1368     bool mNeedFixupMonochromeTags;
1369     status_t fixupMonochromeTags(const CameraMetadata& deviceInfo, CameraMetadata& resultMetadata);
1370 }; // class Camera3Device
1371 
1372 }; // namespace android
1373 
1374 #endif
1375