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