• 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.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         // Whether this capture request has its zoom ratio set to 1.0x before
521         // the framework overrides it for camera HAL consumption.
522         bool                                mZoomRatioIs1x;
523 
524 
525         // Whether this capture request's distortion correction update has
526         // been done.
527         bool                                mDistortionCorrectionUpdated = false;
528         // Whether this capture request's rotation and crop update has been
529         // done.
530         bool                                mRotationAndCropUpdated = false;
531         // Whether this capture request's zoom ratio update has been done.
532         bool                                mZoomRatioUpdated = false;
533     };
534     typedef List<sp<CaptureRequest> > RequestList;
535 
536     status_t checkStatusOkToCaptureLocked();
537 
538     status_t convertMetadataListToRequestListLocked(
539             const List<const PhysicalCameraSettingsList> &metadataList,
540             const std::list<const SurfaceMap> &surfaceMaps,
541             bool repeating,
542             /*out*/
543             RequestList *requestList);
544 
545     void convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
546             std::list<const SurfaceMap>& surfaceMaps,
547             const CameraMetadata& request);
548 
549     status_t submitRequestsHelper(const List<const PhysicalCameraSettingsList> &requestsList,
550                                   const std::list<const SurfaceMap> &surfaceMaps,
551                                   bool repeating,
552                                   int64_t *lastFrameNumber = NULL);
553 
554 
555     /**
556      * Implementation of android::hardware::camera::device::V3_5::ICameraDeviceCallback
557      */
558 
559     hardware::Return<void> processCaptureResult_3_4(
560             const hardware::hidl_vec<
561                     hardware::camera::device::V3_4::CaptureResult>& results) override;
562     hardware::Return<void> processCaptureResult(
563             const hardware::hidl_vec<
564                     hardware::camera::device::V3_2::CaptureResult>& results) override;
565     hardware::Return<void> notify(
566             const hardware::hidl_vec<
567                     hardware::camera::device::V3_2::NotifyMsg>& msgs) override;
568 
569     hardware::Return<void> requestStreamBuffers(
570             const hardware::hidl_vec<
571                     hardware::camera::device::V3_5::BufferRequest>& bufReqs,
572             requestStreamBuffers_cb _hidl_cb) override;
573 
574     hardware::Return<void> returnStreamBuffers(
575             const hardware::hidl_vec<
576                     hardware::camera::device::V3_2::StreamBuffer>& buffers) override;
577 
578     // Handle one notify message
579     void notify(const hardware::camera::device::V3_2::NotifyMsg& msg);
580 
581     // lock to ensure only one processCaptureResult is called at a time.
582     Mutex mProcessCaptureResultLock;
583 
584     /**
585      * Common initialization code shared by both HAL paths
586      *
587      * Must be called with mLock and mInterfaceLock held.
588      */
589     status_t initializeCommonLocked();
590 
591     /**
592      * Get the last request submitted to the hal by the request thread.
593      *
594      * Must be called with mLock held.
595      */
596     virtual CameraMetadata getLatestRequestLocked();
597 
598     /**
599      * Update the current device status and wake all waiting threads.
600      *
601      * Must be called with mLock held.
602      */
603     void internalUpdateStatusLocked(Status status);
604 
605     /**
606      * Pause processing and flush everything, but don't tell the clients.
607      * This is for reconfiguring outputs transparently when according to the
608      * CameraDeviceBase interface we shouldn't need to.
609      * Must be called with mLock and mInterfaceLock both held.
610      */
611     status_t internalPauseAndWaitLocked(nsecs_t maxExpectedDuration);
612 
613     /**
614      * Resume work after internalPauseAndWaitLocked()
615      * Must be called with mLock and mInterfaceLock both held.
616      */
617     status_t internalResumeLocked();
618 
619     /**
620      * Wait until status tracker tells us we've transitioned to the target state
621      * set, which is either ACTIVE when active==true or IDLE (which is any
622      * non-ACTIVE state) when active==false.
623      *
624      * Needs to be called with mLock and mInterfaceLock held.  This means there
625      * can ever only be one waiter at most.
626      *
627      * During the wait mLock is released.
628      *
629      */
630     status_t waitUntilStateThenRelock(bool active, nsecs_t timeout);
631 
632     /**
633      * Implementation of waitUntilDrained. On success, will transition to IDLE state.
634      *
635      * Need to be called with mLock and mInterfaceLock held.
636      */
637     status_t waitUntilDrainedLocked(nsecs_t maxExpectedDuration);
638 
639     /**
640      * Do common work for setting up a streaming or single capture request.
641      * On success, will transition to ACTIVE if in IDLE.
642      */
643     sp<CaptureRequest> setUpRequestLocked(const PhysicalCameraSettingsList &request,
644                                           const SurfaceMap &surfaceMap);
645 
646     /**
647      * Build a CaptureRequest request from the CameraDeviceBase request
648      * settings.
649      */
650     sp<CaptureRequest> createCaptureRequest(const PhysicalCameraSettingsList &request,
651                                             const SurfaceMap &surfaceMap);
652 
653     /**
654      * Internally re-configure camera device using new session parameters.
655      * This will get triggered by the request thread.
656      */
657     bool reconfigureCamera(const CameraMetadata& sessionParams, int clientStatusId);
658 
659     /**
660      * Return true in case of any output or input abandoned streams,
661      * otherwise return false.
662      */
663     bool checkAbandonedStreamsLocked();
664 
665     /**
666      * Filter stream session parameters and configure camera HAL.
667      */
668     status_t filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
669             int operatingMode);
670 
671     /**
672      * Take the currently-defined set of streams and configure the HAL to use
673      * them. This is a long-running operation (may be several hundered ms).
674      */
675     status_t           configureStreamsLocked(int operatingMode,
676             const CameraMetadata& sessionParams, bool notifyRequestThread = true);
677 
678     /**
679      * Cancel stream configuration that did not finish successfully.
680      */
681     void               cancelStreamsConfigurationLocked();
682 
683     /**
684      * Add a dummy stream to the current stream set as a workaround for
685      * not allowing 0 streams in the camera HAL spec.
686      */
687     status_t           addDummyStreamLocked();
688 
689     /**
690      * Remove a dummy stream if the current config includes real streams.
691      */
692     status_t           tryRemoveDummyStreamLocked();
693 
694     /**
695      * Set device into an error state due to some fatal failure, and set an
696      * error message to indicate why. Only the first call's message will be
697      * used. The message is also sent to the log.
698      */
699     void               setErrorState(const char *fmt, ...) override;
700     void               setErrorStateLocked(const char *fmt, ...) override;
701     void               setErrorStateV(const char *fmt, va_list args);
702     void               setErrorStateLockedV(const char *fmt, va_list args);
703 
704     /////////////////////////////////////////////////////////////////////
705     // Implements InflightRequestUpdateInterface
706 
707     void onInflightEntryRemovedLocked(nsecs_t duration) override;
708     void checkInflightMapLengthLocked() override;
709     void onInflightMapFlushedLocked() override;
710 
711     /////////////////////////////////////////////////////////////////////
712 
713     /**
714      * Debugging trylock/spin method
715      * Try to acquire a lock a few times with sleeps between before giving up.
716      */
717     bool               tryLockSpinRightRound(Mutex& lock);
718 
719     /**
720      * Helper function to get the largest Jpeg resolution (in area)
721      * Return Size(0, 0) if static metatdata is invalid
722      */
723     Size getMaxJpegResolution() const;
724 
725     /**
726      * Helper function to get the offset between MONOTONIC and BOOTTIME
727      * timestamp.
728      */
729     static nsecs_t getMonoToBoottimeOffset();
730 
731     struct RequestTrigger {
732         // Metadata tag number, e.g. android.control.aePrecaptureTrigger
733         uint32_t metadataTag;
734         // Metadata value, e.g. 'START' or the trigger ID
735         int32_t entryValue;
736 
737         // The last part of the fully qualified path, e.g. afTrigger
getTagNameRequestTrigger738         const char *getTagName() const {
739             return get_camera_metadata_tag_name(metadataTag) ?: "NULL";
740         }
741 
742         // e.g. TYPE_BYTE, TYPE_INT32, etc.
getTagTypeRequestTrigger743         int getTagType() const {
744             return get_camera_metadata_tag_type(metadataTag);
745         }
746     };
747 
748     /**
749      * Thread for managing capture request submission to HAL device.
750      */
751     class RequestThread : public Thread {
752 
753       public:
754 
755         RequestThread(wp<Camera3Device> parent,
756                 sp<camera3::StatusTracker> statusTracker,
757                 sp<HalInterface> interface,
758                 const Vector<int32_t>& sessionParamKeys,
759                 bool useHalBufManager);
760         ~RequestThread();
761 
762         void     setNotificationListener(wp<NotificationListener> listener);
763 
764         /**
765          * Call after stream (re)-configuration is completed.
766          */
767         void     configurationComplete(bool isConstrainedHighSpeed,
768                 const CameraMetadata& sessionParams);
769 
770         /**
771          * Set or clear the list of repeating requests. Does not block
772          * on either. Use waitUntilPaused to wait until request queue
773          * has emptied out.
774          */
775         status_t setRepeatingRequests(const RequestList& requests,
776                                       /*out*/
777                                       int64_t *lastFrameNumber = NULL);
778         status_t clearRepeatingRequests(/*out*/
779                                         int64_t *lastFrameNumber = NULL);
780 
781         status_t queueRequestList(List<sp<CaptureRequest> > &requests,
782                                   /*out*/
783                                   int64_t *lastFrameNumber = NULL);
784 
785         /**
786          * Remove all queued and repeating requests, and pending triggers
787          */
788         status_t clear(/*out*/int64_t *lastFrameNumber = NULL);
789 
790         /**
791          * Flush all pending requests in HAL.
792          */
793         status_t flush();
794 
795         /**
796          * Queue a trigger to be dispatched with the next outgoing
797          * process_capture_request. The settings for that request only
798          * will be temporarily rewritten to add the trigger tag/value.
799          * Subsequent requests will not be rewritten (for this tag).
800          */
801         status_t queueTrigger(RequestTrigger trigger[], size_t count);
802 
803         /**
804          * Pause/unpause the capture thread. Doesn't block, so use
805          * waitUntilPaused to wait until the thread is paused.
806          */
807         void     setPaused(bool paused);
808 
809         /**
810          * Wait until thread processes the capture request with settings'
811          * android.request.id == requestId.
812          *
813          * Returns TIMED_OUT in case the thread does not process the request
814          * within the timeout.
815          */
816         status_t waitUntilRequestProcessed(int32_t requestId, nsecs_t timeout);
817 
818         /**
819          * Shut down the thread. Shutdown is asynchronous, so thread may
820          * still be running once this method returns.
821          */
822         virtual void requestExit();
823 
824         /**
825          * Get the latest request that was sent to the HAL
826          * with process_capture_request.
827          */
828         CameraMetadata getLatestRequest() const;
829 
830         /**
831          * Returns true if the stream is a target of any queued or repeating
832          * capture request
833          */
834         bool isStreamPending(sp<camera3::Camera3StreamInterface>& stream);
835 
836         /**
837          * Returns true if the surface is a target of any queued or repeating
838          * capture request
839          */
840         bool isOutputSurfacePending(int streamId, size_t surfaceId);
841 
842         // dump processCaptureRequest latency
dumpCaptureRequestLatency(int fd,const char * name)843         void dumpCaptureRequestLatency(int fd, const char* name) {
844             mRequestLatency.dump(fd, name);
845         }
846 
847         void signalPipelineDrain(const std::vector<int>& streamIds);
848         void resetPipelineDrain();
849 
850         status_t switchToOffline(
851                 const std::vector<int32_t>& streamsToKeep,
852                 /*out*/hardware::camera::device::V3_6::CameraOfflineSessionInfo* offlineSessionInfo,
853                 /*out*/sp<hardware::camera::device::V3_6::ICameraOfflineSession>* offlineSession,
854                 /*out*/camera3::BufferRecords* bufferRecords);
855 
856         void clearPreviousRequest();
857 
858         status_t setRotateAndCropAutoBehavior(
859                 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue);
860 
861       protected:
862 
863         virtual bool threadLoop();
864 
865       private:
866         static const String8& getId(const wp<Camera3Device> &device);
867 
868         status_t           queueTriggerLocked(RequestTrigger trigger);
869         // Mix-in queued triggers into this request
870         int32_t            insertTriggers(const sp<CaptureRequest> &request);
871         // Purge the queued triggers from this request,
872         //  restoring the old field values for those tags.
873         status_t           removeTriggers(const sp<CaptureRequest> &request);
874 
875         // HAL workaround: Make sure a trigger ID always exists if
876         // a trigger does
877         status_t           addDummyTriggerIds(const sp<CaptureRequest> &request);
878 
879         // Override rotate_and_crop control if needed; returns true if the current value was changed
880         bool               overrideAutoRotateAndCrop(const sp<CaptureRequest> &request);
881 
882         static const nsecs_t kRequestTimeout = 50e6; // 50 ms
883 
884         // TODO: does this need to be adjusted for long exposure requests?
885         static const nsecs_t kRequestSubmitTimeout = 200e6; // 200 ms
886 
887         // Used to prepare a batch of requests.
888         struct NextRequest {
889             sp<CaptureRequest>              captureRequest;
890             camera3_capture_request_t       halRequest;
891             Vector<camera3_stream_buffer_t> outputBuffers;
892             bool                            submitted;
893         };
894 
895         // Wait for the next batch of requests and put them in mNextRequests. mNextRequests will
896         // be empty if it times out.
897         void waitForNextRequestBatch();
898 
899         // Waits for a request, or returns NULL if times out. Must be called with mRequestLock hold.
900         sp<CaptureRequest> waitForNextRequestLocked();
901 
902         // Prepare HAL requests and output buffers in mNextRequests. Return TIMED_OUT if getting any
903         // output buffer timed out. If an error is returned, the caller should clean up the pending
904         // request batch.
905         status_t prepareHalRequests();
906 
907         // Return buffers, etc, for requests in mNextRequests that couldn't be fully constructed and
908         // send request errors if sendRequestError is true. The buffers will be returned in the
909         // ERROR state to mark them as not having valid data. mNextRequests will be cleared.
910         void cleanUpFailedRequests(bool sendRequestError);
911 
912         // Stop the repeating request if any of its output streams is abandoned.
913         void checkAndStopRepeatingRequest();
914 
915         // Release physical camera settings and camera id resources.
916         void cleanupPhysicalSettings(sp<CaptureRequest> request,
917                 /*out*/camera3_capture_request_t *halRequest);
918 
919         // Pause handling
920         bool               waitIfPaused();
921         void               unpauseForNewRequests();
922 
923         // Relay error to parent device object setErrorState
924         void               setErrorState(const char *fmt, ...);
925 
926         // If the input request is in mRepeatingRequests. Must be called with mRequestLock hold
927         bool isRepeatingRequestLocked(const sp<CaptureRequest>&);
928 
929         // Clear repeating requests. Must be called with mRequestLock held.
930         status_t clearRepeatingRequestsLocked(/*out*/ int64_t *lastFrameNumber = NULL);
931 
932         // send request in mNextRequests to HAL in a batch. Return true = sucssess
933         bool sendRequestsBatch();
934 
935         // Calculate the expected maximum duration for a request
936         nsecs_t calculateMaxExpectedDuration(const camera_metadata_t *request);
937 
938         // Check and update latest session parameters based on the current request settings.
939         bool updateSessionParameters(const CameraMetadata& settings);
940 
941         // Check whether FPS range session parameter re-configuration is needed in constrained
942         // high speed recording camera sessions.
943         bool skipHFRTargetFPSUpdate(int32_t tag, const camera_metadata_ro_entry_t& newEntry,
944                 const camera_metadata_entry_t& currentEntry);
945 
946         // Update next request sent to HAL
947         void updateNextRequest(NextRequest& nextRequest);
948 
949         wp<Camera3Device>  mParent;
950         wp<camera3::StatusTracker>  mStatusTracker;
951         sp<HalInterface>   mInterface;
952 
953         wp<NotificationListener> mListener;
954 
955         const String8&     mId;       // The camera ID
956         int                mStatusId; // The RequestThread's component ID for
957                                       // status tracking
958 
959         Mutex              mRequestLock;
960         Condition          mRequestSignal;
961         Condition          mRequestSubmittedSignal;
962         RequestList        mRequestQueue;
963         RequestList        mRepeatingRequests;
964         // The next batch of requests being prepped for submission to the HAL, no longer
965         // on the request queue. Read-only even with mRequestLock held, outside
966         // of threadLoop
967         Vector<NextRequest> mNextRequests;
968 
969         // To protect flush() and sending a request batch to HAL.
970         Mutex              mFlushLock;
971 
972         bool               mReconfigured;
973 
974         // Used by waitIfPaused, waitForNextRequest, waitUntilPaused, and signalPipelineDrain
975         Mutex              mPauseLock;
976         bool               mDoPause;
977         Condition          mDoPauseSignal;
978         bool               mPaused;
979         bool               mNotifyPipelineDrain;
980         std::vector<int>   mStreamIdsToBeDrained;
981 
982         sp<CaptureRequest> mPrevRequest;
983         int32_t            mPrevTriggers;
984         std::set<std::string> mPrevCameraIdsWithZoom;
985 
986         uint32_t           mFrameNumber;
987 
988         mutable Mutex      mLatestRequestMutex;
989         Condition          mLatestRequestSignal;
990         // android.request.id for latest process_capture_request
991         int32_t            mLatestRequestId;
992         CameraMetadata     mLatestRequest;
993         std::unordered_map<std::string, CameraMetadata> mLatestPhysicalRequest;
994 
995         typedef KeyedVector<uint32_t/*tag*/, RequestTrigger> TriggerMap;
996         Mutex              mTriggerMutex;
997         TriggerMap         mTriggerMap;
998         TriggerMap         mTriggerRemovedMap;
999         TriggerMap         mTriggerReplacedMap;
1000         uint32_t           mCurrentAfTriggerId;
1001         uint32_t           mCurrentPreCaptureTriggerId;
1002         camera_metadata_enum_android_scaler_rotate_and_crop_t mRotateAndCropOverride;
1003 
1004         int64_t            mRepeatingLastFrameNumber;
1005 
1006         // Flag indicating if we should prepare video stream for video requests.
1007         bool               mPrepareVideoStream;
1008 
1009         bool               mConstrainedMode;
1010 
1011         static const int32_t kRequestLatencyBinSize = 40; // in ms
1012         CameraLatencyHistogram mRequestLatency;
1013 
1014         Vector<int32_t>    mSessionParamKeys;
1015         CameraMetadata     mLatestSessionParams;
1016 
1017         const bool         mUseHalBufManager;
1018     };
1019     sp<RequestThread> mRequestThread;
1020 
1021     /**
1022      * In-flight queue for tracking completion of capture requests.
1023      */
1024     std::mutex                    mInFlightLock;
1025     camera3::InFlightRequestMap   mInFlightMap;
1026     nsecs_t                       mExpectedInflightDuration = 0;
1027     int64_t                       mLastCompletedRegularFrameNumber = -1;
1028     int64_t                       mLastCompletedReprocessFrameNumber = -1;
1029     int64_t                       mLastCompletedZslFrameNumber = -1;
1030     // End of mInFlightLock protection scope
1031 
1032     int mInFlightStatusId; // const after initialize
1033 
1034     status_t registerInFlight(uint32_t frameNumber,
1035             int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
1036             bool callback, nsecs_t maxExpectedDuration, std::set<String8>& physicalCameraIds,
1037             bool isStillCapture, bool isZslCapture, bool rotateAndCropAuto,
1038             const std::set<std::string>& cameraIdsWithZoom, const SurfaceMap& outputSurfaces);
1039 
1040     /**
1041      * Tracking for idle detection
1042      */
1043     sp<camera3::StatusTracker> mStatusTracker;
1044 
1045     /**
1046      * Graphic buffer manager for output streams. Each device has a buffer manager, which is used
1047      * by the output streams to get and return buffers if these streams are registered to this
1048      * buffer manager.
1049      */
1050     sp<camera3::Camera3BufferManager> mBufferManager;
1051 
1052     /**
1053      * Thread for preparing streams
1054      */
1055     class PreparerThread : private Thread, public virtual RefBase {
1056       public:
1057         PreparerThread();
1058         ~PreparerThread();
1059 
1060         void setNotificationListener(wp<NotificationListener> listener);
1061 
1062         /**
1063          * Queue up a stream to be prepared. Streams are processed by a background thread in FIFO
1064          * order.  Pre-allocate up to maxCount buffers for the stream, or the maximum number needed
1065          * for the pipeline if maxCount is ALLOCATE_PIPELINE_MAX.
1066          */
1067         status_t prepare(int maxCount, sp<camera3::Camera3StreamInterface>& stream);
1068 
1069         /**
1070          * Cancel all current and pending stream preparation
1071          */
1072         status_t clear();
1073 
1074         /**
1075          * Pause all preparation activities
1076          */
1077         void pause();
1078 
1079         /**
1080          * Resume preparation activities
1081          */
1082         status_t resume();
1083 
1084       private:
1085         Mutex mLock;
1086         Condition mThreadActiveSignal;
1087 
1088         virtual bool threadLoop();
1089 
1090         // Guarded by mLock
1091 
1092         wp<NotificationListener> mListener;
1093         std::unordered_map<int, sp<camera3::Camera3StreamInterface> > mPendingStreams;
1094         bool mActive;
1095         bool mCancelNow;
1096 
1097         // Only accessed by threadLoop and the destructor
1098 
1099         sp<camera3::Camera3StreamInterface> mCurrentStream;
1100         int mCurrentMaxCount;
1101         bool mCurrentPrepareComplete;
1102     };
1103     sp<PreparerThread> mPreparerThread;
1104 
1105     /**
1106      * Output result queue and current HAL device 3A state
1107      */
1108 
1109     // Lock for output side of device
1110     std::mutex             mOutputLock;
1111 
1112     /**** Scope for mOutputLock ****/
1113     // the minimal frame number of the next non-reprocess result
1114     uint32_t               mNextResultFrameNumber;
1115     // the minimal frame number of the next reprocess result
1116     uint32_t               mNextReprocessResultFrameNumber;
1117     // the minimal frame number of the next ZSL still capture result
1118     uint32_t               mNextZslStillResultFrameNumber;
1119     // the minimal frame number of the next non-reprocess shutter
1120     uint32_t               mNextShutterFrameNumber;
1121     // the minimal frame number of the next reprocess shutter
1122     uint32_t               mNextReprocessShutterFrameNumber;
1123     // the minimal frame number of the next ZSL still capture shutter
1124     uint32_t               mNextZslStillShutterFrameNumber;
1125     std::list<CaptureResult>    mResultQueue;
1126     std::condition_variable  mResultSignal;
1127     wp<NotificationListener> mListener;
1128 
1129     /**** End scope for mOutputLock ****/
1130 
1131     /**** Scope for mInFlightLock ****/
1132 
1133     // Remove the in-flight map entry of the given index from mInFlightMap.
1134     // It must only be called with mInFlightLock held.
1135     void removeInFlightMapEntryLocked(int idx);
1136 
1137     // Remove all in-flight requests and return all buffers.
1138     // This is used after HAL interface is closed to cleanup any request/buffers
1139     // not returned by HAL.
1140     void flushInflightRequests();
1141 
1142     /**** End scope for mInFlightLock ****/
1143 
1144     /**
1145      * Distortion correction support
1146      */
1147     // Map from camera IDs to its corresponding distortion mapper. Only contains
1148     // 1 ID if the device isn't a logical multi-camera. Otherwise contains both
1149     // logical camera and its physical subcameras.
1150     std::unordered_map<std::string, camera3::DistortionMapper> mDistortionMappers;
1151 
1152     /**
1153      * Zoom ratio mapper support
1154      */
1155     std::unordered_map<std::string, camera3::ZoomRatioMapper> mZoomRatioMappers;
1156 
1157     /**
1158      * RotateAndCrop mapper support
1159      */
1160     std::unordered_map<std::string, camera3::RotateAndCropMapper> mRotateAndCropMappers;
1161 
1162     // Debug tracker for metadata tag value changes
1163     // - Enabled with the -m <taglist> option to dumpsys, such as
1164     //   dumpsys -m android.control.aeState,android.control.aeMode
1165     // - Disabled with -m off
1166     // - dumpsys -m 3a is a shortcut for ae/af/awbMode, State, and Triggers
1167     TagMonitor mTagMonitor;
1168 
1169     void monitorMetadata(TagMonitor::eventSource source, int64_t frameNumber,
1170             nsecs_t timestamp, const CameraMetadata& metadata,
1171             const std::unordered_map<std::string, CameraMetadata>& physicalMetadata);
1172 
1173     metadata_vendor_id_t mVendorTagId;
1174 
1175     // Cached last requested template id
1176     int mLastTemplateId;
1177 
1178     // Synchronizes access to status tracker between inflight updates and disconnect.
1179     // b/79972865
1180     Mutex mTrackerLock;
1181 
1182     // Whether HAL request buffers through requestStreamBuffers API
1183     bool mUseHalBufManager = false;
1184 
1185     // Lock to ensure requestStreamBuffers() callbacks are serialized
1186     std::mutex mRequestBufferInterfaceLock;
1187 
1188     // The state machine to control when requestStreamBuffers should allow
1189     // HAL to request buffers.
1190     enum RequestBufferState {
1191         /**
1192          * This is the initial state.
1193          * requestStreamBuffers call will return FAILED_CONFIGURING in this state.
1194          * Will switch to RB_STATUS_READY after a successful configureStreams or
1195          * processCaptureRequest call.
1196          */
1197         RB_STATUS_STOPPED,
1198 
1199         /**
1200          * requestStreamBuffers call will proceed in this state.
1201          * When device is asked to stay idle via waitUntilStateThenRelock() call:
1202          *     - Switch to RB_STATUS_STOPPED if there is no inflight requests and
1203          *       request thread is paused.
1204          *     - Switch to RB_STATUS_PENDING_STOP otherwise
1205          */
1206         RB_STATUS_READY,
1207 
1208         /**
1209          * requestStreamBuffers call will proceed in this state.
1210          * Switch to RB_STATUS_STOPPED when all inflight requests are fulfilled
1211          * and request thread is paused
1212          */
1213         RB_STATUS_PENDING_STOP,
1214     };
1215 
1216     class RequestBufferStateMachine {
1217       public:
1218         status_t initialize(sp<camera3::StatusTracker> statusTracker);
1219 
1220         // Return if the state machine currently allows for requestBuffers
1221         // If the state allows for it, mRequestBufferOngoing will be set to true
1222         // and caller must call endRequestBuffer() later to unset the flag
1223         bool startRequestBuffer();
1224         void endRequestBuffer();
1225 
1226         // Events triggered by application API call
1227         void onStreamsConfigured();
1228         void onWaitUntilIdle();
1229 
1230         // Events usually triggered by hwBinder processCaptureResult callback thread
1231         // But can also be triggered on request thread for failed request, or on
1232         // hwbinder notify callback thread for shutter/error callbacks
1233         void onInflightMapEmpty();
1234 
1235         // Events triggered by RequestThread
1236         void onSubmittingRequest();
1237         void onRequestThreadPaused();
1238 
1239         // Events triggered by successful switchToOffline call
1240         // Return true is there is no ongoing requestBuffer call.
1241         bool onSwitchToOfflineSuccess();
1242 
1243       private:
1244         void notifyTrackerLocked(bool active);
1245 
1246         // Switch to STOPPED state and return true if all conditions allows for it.
1247         // Otherwise do nothing and return false.
1248         bool checkSwitchToStopLocked();
1249 
1250         std::mutex mLock;
1251         RequestBufferState mStatus = RB_STATUS_STOPPED;
1252 
1253         bool mRequestThreadPaused = true;
1254         bool mInflightMapEmpty = true;
1255         bool mRequestBufferOngoing = false;
1256         bool mSwitchedToOffline = false;
1257 
1258         wp<camera3::StatusTracker> mStatusTracker;
1259         int  mRequestBufferStatusId;
1260     } mRequestBufferSM;
1261 
1262     // Fix up result metadata for monochrome camera.
1263     bool mNeedFixupMonochromeTags;
1264 
1265     // Whether HAL supports offline processing capability.
1266     bool mSupportOfflineProcessing = false;
1267 }; // class Camera3Device
1268 
1269 }; // namespace android
1270 
1271 #endif
1272