[go: nahoru, domu]

Camera3Device.h revision c66969beb346b878701c4adccf83a1475c3a0687
1/*
2 * Copyright (C) 2013 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 <utils/Condition.h>
21#include <utils/Errors.h>
22#include <utils/List.h>
23#include <utils/Mutex.h>
24#include <utils/Thread.h>
25#include <utils/KeyedVector.h>
26#include <utils/Timers.h>
27#include <hardware/camera3.h>
28#include <camera/CaptureResult.h>
29
30#include "common/CameraDeviceBase.h"
31#include "device3/StatusTracker.h"
32#include "device3/Camera3BufferManager.h"
33
34/**
35 * Function pointer types with C calling convention to
36 * use for HAL callback functions.
37 */
38extern "C" {
39    typedef void (callbacks_process_capture_result_t)(
40        const struct camera3_callback_ops *,
41        const camera3_capture_result_t *);
42
43    typedef void (callbacks_notify_t)(
44        const struct camera3_callback_ops *,
45        const camera3_notify_msg_t *);
46}
47
48namespace android {
49
50namespace camera3 {
51
52class Camera3Stream;
53class Camera3ZslStream;
54class Camera3OutputStreamInterface;
55class Camera3StreamInterface;
56
57}
58
59/**
60 * CameraDevice for HAL devices with version CAMERA_DEVICE_API_VERSION_3_0 or higher.
61 */
62class Camera3Device :
63            public CameraDeviceBase,
64            private camera3_callback_ops {
65  public:
66
67    Camera3Device(int id);
68
69    virtual ~Camera3Device();
70
71    /**
72     * CameraDeviceBase interface
73     */
74
75    virtual int      getId() const;
76
77    // Transitions to idle state on success.
78    virtual status_t initialize(CameraModule *module);
79    virtual status_t disconnect();
80    virtual status_t dump(int fd, const Vector<String16> &args);
81    virtual const CameraMetadata& info() const;
82
83    // Capture and setStreamingRequest will configure streams if currently in
84    // idle state
85    virtual status_t capture(CameraMetadata &request, int64_t *lastFrameNumber = NULL);
86    virtual status_t captureList(const List<const CameraMetadata> &requests,
87                                 int64_t *lastFrameNumber = NULL);
88    virtual status_t setStreamingRequest(const CameraMetadata &request,
89                                         int64_t *lastFrameNumber = NULL);
90    virtual status_t setStreamingRequestList(const List<const CameraMetadata> &requests,
91                                             int64_t *lastFrameNumber = NULL);
92    virtual status_t clearStreamingRequest(int64_t *lastFrameNumber = NULL);
93
94    virtual status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout);
95
96    // Actual stream creation/deletion is delayed until first request is submitted
97    // If adding streams while actively capturing, will pause device before adding
98    // stream, reconfiguring device, and unpausing.
99    virtual status_t createStream(sp<Surface> consumer,
100            uint32_t width, uint32_t height, int format,
101            android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
102            int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID);
103    virtual status_t createInputStream(
104            uint32_t width, uint32_t height, int format,
105            int *id);
106    virtual status_t createZslStream(
107            uint32_t width, uint32_t height,
108            int depth,
109            /*out*/
110            int *id,
111            sp<camera3::Camera3ZslStream>* zslStream);
112    virtual status_t createReprocessStreamFromStream(int outputId, int *id);
113
114    virtual status_t getStreamInfo(int id,
115            uint32_t *width, uint32_t *height,
116            uint32_t *format, android_dataspace *dataSpace);
117    virtual status_t setStreamTransform(int id, int transform);
118
119    virtual status_t deleteStream(int id);
120    virtual status_t deleteReprocessStream(int id);
121
122    virtual status_t configureStreams(bool isConstraiedHighSpeed = false);
123    virtual status_t getInputBufferProducer(
124            sp<IGraphicBufferProducer> *producer);
125
126    virtual status_t createDefaultRequest(int templateId, CameraMetadata *request);
127
128    // Transitions to the idle state on success
129    virtual status_t waitUntilDrained();
130
131    virtual status_t setNotifyCallback(NotificationListener *listener);
132    virtual bool     willNotify3A();
133    virtual status_t waitForNextFrame(nsecs_t timeout);
134    virtual status_t getNextResult(CaptureResult *frame);
135
136    virtual status_t triggerAutofocus(uint32_t id);
137    virtual status_t triggerCancelAutofocus(uint32_t id);
138    virtual status_t triggerPrecaptureMetering(uint32_t id);
139
140    virtual status_t pushReprocessBuffer(int reprocessStreamId,
141            buffer_handle_t *buffer, wp<BufferReleasedListener> listener);
142
143    virtual status_t flush(int64_t *lastFrameNumber = NULL);
144
145    virtual status_t prepare(int streamId);
146
147    virtual status_t tearDown(int streamId);
148
149    virtual status_t addBufferListenerForStream(int streamId,
150            wp<camera3::Camera3StreamBufferListener> listener);
151
152    virtual status_t prepare(int maxCount, int streamId);
153
154    virtual uint32_t getDeviceVersion();
155
156    virtual ssize_t getJpegBufferSize(uint32_t width, uint32_t height) const;
157    ssize_t getPointCloudBufferSize() const;
158    ssize_t getRawOpaqueBufferSize(int32_t width, int32_t height) const;
159
160    // Methods called by subclasses
161    void             notifyStatus(bool idle); // updates from StatusTracker
162
163  private:
164    static const size_t        kDumpLockAttempts  = 10;
165    static const size_t        kDumpSleepDuration = 100000; // 0.10 sec
166    static const nsecs_t       kShutdownTimeout   = 5000000000; // 5 sec
167    static const nsecs_t       kActiveTimeout     = 500000000;  // 500 ms
168    static const size_t        kInFlightWarnLimit = 20;
169    static const size_t        kInFlightWarnLimitHighSpeed = 256; // batch size 32 * pipe depth 8
170    // SCHED_FIFO priority for request submission thread in HFR mode
171    static const int           kConstrainedHighSpeedThreadPriority = 1;
172
173    struct                     RequestTrigger;
174    // minimal jpeg buffer size: 256KB + blob header
175    static const ssize_t       kMinJpegBufferSize = 256 * 1024 + sizeof(camera3_jpeg_blob);
176    // Constant to use for stream ID when one doesn't exist
177    static const int           NO_STREAM = -1;
178
179    // A lock to enforce serialization on the input/configure side
180    // of the public interface.
181    // Only locked by public methods inherited from CameraDeviceBase.
182    // Not locked by methods guarded by mOutputLock, since they may act
183    // concurrently to the input/configure side of the interface.
184    // Must be locked before mLock if both will be locked by a method
185    Mutex                      mInterfaceLock;
186
187    // The main lock on internal state
188    Mutex                      mLock;
189
190    // Camera device ID
191    const int                  mId;
192
193    // Flag indicating is the current active stream configuration is constrained high speed.
194    bool                       mIsConstrainedHighSpeedConfiguration;
195
196    /**** Scope for mLock ****/
197
198    camera3_device_t          *mHal3Device;
199
200    CameraMetadata             mDeviceInfo;
201
202    CameraMetadata             mRequestTemplateCache[CAMERA3_TEMPLATE_COUNT];
203
204    uint32_t                   mDeviceVersion;
205
206    // whether Camera3Device should derive ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST for
207    // backward compatibility. Should not be changed after initialization.
208    bool                       mDerivePostRawSensKey = false;
209
210    struct Size {
211        uint32_t width;
212        uint32_t height;
213        Size(uint32_t w = 0, uint32_t h = 0) : width(w), height(h){}
214    };
215    // Map from format to size.
216    Vector<Size>               mSupportedOpaqueInputSizes;
217
218    enum Status {
219        STATUS_ERROR,
220        STATUS_UNINITIALIZED,
221        STATUS_UNCONFIGURED,
222        STATUS_CONFIGURED,
223        STATUS_ACTIVE
224    }                          mStatus;
225
226    // Only clear mRecentStatusUpdates, mStatusWaiters from waitUntilStateThenRelock
227    Vector<Status>             mRecentStatusUpdates;
228    int                        mStatusWaiters;
229
230    Condition                  mStatusChanged;
231
232    // Tracking cause of fatal errors when in STATUS_ERROR
233    String8                    mErrorCause;
234
235    // Mapping of stream IDs to stream instances
236    typedef KeyedVector<int, sp<camera3::Camera3OutputStreamInterface> >
237            StreamSet;
238
239    StreamSet                  mOutputStreams;
240    sp<camera3::Camera3Stream> mInputStream;
241    int                        mNextStreamId;
242    bool                       mNeedConfig;
243
244    int                        mDummyStreamId;
245
246    // Whether to send state updates upstream
247    // Pause when doing transparent reconfiguration
248    bool                       mPauseStateNotify;
249
250    // Need to hold on to stream references until configure completes.
251    Vector<sp<camera3::Camera3StreamInterface> > mDeletedStreams;
252
253    // Whether the HAL will send partial result
254    bool                       mUsePartialResult;
255
256    // Number of partial results that will be delivered by the HAL.
257    uint32_t                   mNumPartialResults;
258
259    /**** End scope for mLock ****/
260
261    // The offset converting from clock domain of other subsystem
262    // (video/hardware composer) to that of camera. Assumption is that this
263    // offset won't change during the life cycle of the camera device. In other
264    // words, camera device shouldn't be open during CPU suspend.
265    nsecs_t                    mTimestampOffset;
266
267    typedef struct AeTriggerCancelOverride {
268        bool applyAeLock;
269        uint8_t aeLock;
270        bool applyAePrecaptureTrigger;
271        uint8_t aePrecaptureTrigger;
272    } AeTriggerCancelOverride_t;
273
274    class CaptureRequest : public LightRefBase<CaptureRequest> {
275      public:
276        CameraMetadata                      mSettings;
277        sp<camera3::Camera3Stream>          mInputStream;
278        camera3_stream_buffer_t             mInputBuffer;
279        Vector<sp<camera3::Camera3OutputStreamInterface> >
280                                            mOutputStreams;
281        CaptureResultExtras                 mResultExtras;
282        // Used to cancel AE precapture trigger for devices doesn't support
283        // CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL
284        AeTriggerCancelOverride_t           mAeTriggerCancelOverride;
285        // The number of requests that should be submitted to HAL at a time.
286        // For example, if batch size is 8, this request and the following 7
287        // requests will be submitted to HAL at a time. The batch size for
288        // the following 7 requests will be ignored by the request thread.
289        int                                 mBatchSize;
290    };
291    typedef List<sp<CaptureRequest> > RequestList;
292
293    status_t checkStatusOkToCaptureLocked();
294
295    status_t convertMetadataListToRequestListLocked(
296            const List<const CameraMetadata> &metadataList,
297            /*out*/
298            RequestList *requestList);
299
300    status_t submitRequestsHelper(const List<const CameraMetadata> &requests, bool repeating,
301                                  int64_t *lastFrameNumber = NULL);
302
303    /**
304     * Get the last request submitted to the hal by the request thread.
305     *
306     * Takes mLock.
307     */
308    virtual CameraMetadata getLatestRequestLocked();
309
310    /**
311     * Update the current device status and wake all waiting threads.
312     *
313     * Must be called with mLock held.
314     */
315    void internalUpdateStatusLocked(Status status);
316
317    /**
318     * Pause processing and flush everything, but don't tell the clients.
319     * This is for reconfiguring outputs transparently when according to the
320     * CameraDeviceBase interface we shouldn't need to.
321     * Must be called with mLock and mInterfaceLock both held.
322     */
323    status_t internalPauseAndWaitLocked();
324
325    /**
326     * Resume work after internalPauseAndWaitLocked()
327     * Must be called with mLock and mInterfaceLock both held.
328     */
329    status_t internalResumeLocked();
330
331    /**
332     * Wait until status tracker tells us we've transitioned to the target state
333     * set, which is either ACTIVE when active==true or IDLE (which is any
334     * non-ACTIVE state) when active==false.
335     *
336     * Needs to be called with mLock and mInterfaceLock held.  This means there
337     * can ever only be one waiter at most.
338     *
339     * During the wait mLock is released.
340     *
341     */
342    status_t waitUntilStateThenRelock(bool active, nsecs_t timeout);
343
344    /**
345     * Implementation of waitUntilDrained. On success, will transition to IDLE state.
346     *
347     * Need to be called with mLock and mInterfaceLock held.
348     */
349    status_t waitUntilDrainedLocked();
350
351    /**
352     * Do common work for setting up a streaming or single capture request.
353     * On success, will transition to ACTIVE if in IDLE.
354     */
355    sp<CaptureRequest> setUpRequestLocked(const CameraMetadata &request);
356
357    /**
358     * Build a CaptureRequest request from the CameraDeviceBase request
359     * settings.
360     */
361    sp<CaptureRequest> createCaptureRequest(const CameraMetadata &request);
362
363    /**
364     * Take the currently-defined set of streams and configure the HAL to use
365     * them. This is a long-running operation (may be several hundered ms).
366     */
367    status_t           configureStreamsLocked();
368
369    /**
370     * Add a dummy stream to the current stream set as a workaround for
371     * not allowing 0 streams in the camera HAL spec.
372     */
373    status_t           addDummyStreamLocked();
374
375    /**
376     * Remove a dummy stream if the current config includes real streams.
377     */
378    status_t           tryRemoveDummyStreamLocked();
379
380    /**
381     * Set device into an error state due to some fatal failure, and set an
382     * error message to indicate why. Only the first call's message will be
383     * used. The message is also sent to the log.
384     */
385    void               setErrorState(const char *fmt, ...);
386    void               setErrorStateV(const char *fmt, va_list args);
387    void               setErrorStateLocked(const char *fmt, ...);
388    void               setErrorStateLockedV(const char *fmt, va_list args);
389
390    /**
391     * Debugging trylock/spin method
392     * Try to acquire a lock a few times with sleeps between before giving up.
393     */
394    bool               tryLockSpinRightRound(Mutex& lock);
395
396    /**
397     * Helper function to determine if an input size for implementation defined
398     * format is supported.
399     */
400    bool isOpaqueInputSizeSupported(uint32_t width, uint32_t height);
401
402    /**
403     * Helper function to get the largest Jpeg resolution (in area)
404     * Return Size(0, 0) if static metatdata is invalid
405     */
406    Size getMaxJpegResolution() const;
407
408    /**
409     * Helper function to get the offset between MONOTONIC and BOOTTIME
410     * timestamp.
411     */
412    static nsecs_t getMonoToBoottimeOffset();
413
414    /**
415     * Helper function to map between legacy and new dataspace enums
416     */
417    static android_dataspace mapToLegacyDataspace(android_dataspace dataSpace);
418
419    struct RequestTrigger {
420        // Metadata tag number, e.g. android.control.aePrecaptureTrigger
421        uint32_t metadataTag;
422        // Metadata value, e.g. 'START' or the trigger ID
423        int32_t entryValue;
424
425        // The last part of the fully qualified path, e.g. afTrigger
426        const char *getTagName() const {
427            return get_camera_metadata_tag_name(metadataTag) ?: "NULL";
428        }
429
430        // e.g. TYPE_BYTE, TYPE_INT32, etc.
431        int getTagType() const {
432            return get_camera_metadata_tag_type(metadataTag);
433        }
434    };
435
436    /**
437     * Thread for managing capture request submission to HAL device.
438     */
439    class RequestThread : public Thread {
440
441      public:
442
443        RequestThread(wp<Camera3Device> parent,
444                sp<camera3::StatusTracker> statusTracker,
445                camera3_device_t *hal3Device,
446                bool aeLockAvailable);
447
448        void     setNotificationListener(NotificationListener *listener);
449
450        /**
451         * Call after stream (re)-configuration is completed.
452         */
453        void     configurationComplete(bool isConstrainedHighSpeed);
454
455        /**
456         * Set or clear the list of repeating requests. Does not block
457         * on either. Use waitUntilPaused to wait until request queue
458         * has emptied out.
459         */
460        status_t setRepeatingRequests(const RequestList& requests,
461                                      /*out*/
462                                      int64_t *lastFrameNumber = NULL);
463        status_t clearRepeatingRequests(/*out*/
464                                        int64_t *lastFrameNumber = NULL);
465
466        status_t queueRequestList(List<sp<CaptureRequest> > &requests,
467                                  /*out*/
468                                  int64_t *lastFrameNumber = NULL);
469
470        /**
471         * Remove all queued and repeating requests, and pending triggers
472         */
473        status_t clear(NotificationListener *listener,
474                       /*out*/
475                       int64_t *lastFrameNumber = NULL);
476
477        /**
478         * Flush all pending requests in HAL.
479         */
480        status_t flush();
481
482        /**
483         * Queue a trigger to be dispatched with the next outgoing
484         * process_capture_request. The settings for that request only
485         * will be temporarily rewritten to add the trigger tag/value.
486         * Subsequent requests will not be rewritten (for this tag).
487         */
488        status_t queueTrigger(RequestTrigger trigger[], size_t count);
489
490        /**
491         * Pause/unpause the capture thread. Doesn't block, so use
492         * waitUntilPaused to wait until the thread is paused.
493         */
494        void     setPaused(bool paused);
495
496        /**
497         * Wait until thread processes the capture request with settings'
498         * android.request.id == requestId.
499         *
500         * Returns TIMED_OUT in case the thread does not process the request
501         * within the timeout.
502         */
503        status_t waitUntilRequestProcessed(int32_t requestId, nsecs_t timeout);
504
505        /**
506         * Shut down the thread. Shutdown is asynchronous, so thread may
507         * still be running once this method returns.
508         */
509        virtual void requestExit();
510
511        /**
512         * Get the latest request that was sent to the HAL
513         * with process_capture_request.
514         */
515        CameraMetadata getLatestRequest() const;
516
517        /**
518         * Returns true if the stream is a target of any queued or repeating
519         * capture request
520         */
521        bool isStreamPending(sp<camera3::Camera3StreamInterface>& stream);
522
523      protected:
524
525        virtual bool threadLoop();
526
527      private:
528        static int         getId(const wp<Camera3Device> &device);
529
530        status_t           queueTriggerLocked(RequestTrigger trigger);
531        // Mix-in queued triggers into this request
532        int32_t            insertTriggers(const sp<CaptureRequest> &request);
533        // Purge the queued triggers from this request,
534        //  restoring the old field values for those tags.
535        status_t           removeTriggers(const sp<CaptureRequest> &request);
536
537        // HAL workaround: Make sure a trigger ID always exists if
538        // a trigger does
539        status_t          addDummyTriggerIds(const sp<CaptureRequest> &request);
540
541        static const nsecs_t kRequestTimeout = 50e6; // 50 ms
542
543        // Used to prepare a batch of requests.
544        struct NextRequest {
545            sp<CaptureRequest>              captureRequest;
546            camera3_capture_request_t       halRequest;
547            Vector<camera3_stream_buffer_t> outputBuffers;
548            bool                            submitted;
549        };
550
551        // Wait for the next batch of requests and put them in mNextRequests. mNextRequests will
552        // be empty if it times out.
553        void waitForNextRequestBatch();
554
555        // Waits for a request, or returns NULL if times out. Must be called with mRequestLock hold.
556        sp<CaptureRequest> waitForNextRequestLocked();
557
558        // Prepare HAL requests and output buffers in mNextRequests. Return TIMED_OUT if getting any
559        // output buffer timed out. If an error is returned, the caller should clean up the pending
560        // request batch.
561        status_t prepareHalRequests();
562
563        // Return buffers, etc, for requests in mNextRequests that couldn't be fully constructed and
564        // send request errors if sendRequestError is true. The buffers will be returned in the
565        // ERROR state to mark them as not having valid data. mNextRequests will be cleared.
566        void cleanUpFailedRequests(bool sendRequestError);
567
568        // Stop the repeating request if any of its output streams is abandoned.
569        void checkAndStopRepeatingRequest();
570
571        // Pause handling
572        bool               waitIfPaused();
573        void               unpauseForNewRequests();
574
575        // Relay error to parent device object setErrorState
576        void               setErrorState(const char *fmt, ...);
577
578        // If the input request is in mRepeatingRequests. Must be called with mRequestLock hold
579        bool isRepeatingRequestLocked(const sp<CaptureRequest>);
580
581        // Handle AE precapture trigger cancel for devices <= CAMERA_DEVICE_API_VERSION_3_2.
582        void handleAePrecaptureCancelRequest(sp<CaptureRequest> request);
583
584        // Clear repeating requests. Must be called with mRequestLock held.
585        status_t clearRepeatingRequestsLocked(/*out*/ int64_t *lastFrameNumber = NULL);
586
587        wp<Camera3Device>  mParent;
588        wp<camera3::StatusTracker>  mStatusTracker;
589        camera3_device_t  *mHal3Device;
590
591        NotificationListener *mListener;
592
593        const int          mId;       // The camera ID
594        int                mStatusId; // The RequestThread's component ID for
595                                      // status tracking
596
597        Mutex              mRequestLock;
598        Condition          mRequestSignal;
599        RequestList        mRequestQueue;
600        RequestList        mRepeatingRequests;
601        // The next batch of requests being prepped for submission to the HAL, no longer
602        // on the request queue. Read-only even with mRequestLock held, outside
603        // of threadLoop
604        Vector<NextRequest> mNextRequests;
605
606        // To protect flush() and sending a request batch to HAL.
607        Mutex              mFlushLock;
608
609        bool               mReconfigured;
610
611        // Used by waitIfPaused, waitForNextRequest, and waitUntilPaused
612        Mutex              mPauseLock;
613        bool               mDoPause;
614        Condition          mDoPauseSignal;
615        bool               mPaused;
616        Condition          mPausedSignal;
617
618        sp<CaptureRequest> mPrevRequest;
619        int32_t            mPrevTriggers;
620
621        uint32_t           mFrameNumber;
622
623        mutable Mutex      mLatestRequestMutex;
624        Condition          mLatestRequestSignal;
625        // android.request.id for latest process_capture_request
626        int32_t            mLatestRequestId;
627        CameraMetadata     mLatestRequest;
628
629        typedef KeyedVector<uint32_t/*tag*/, RequestTrigger> TriggerMap;
630        Mutex              mTriggerMutex;
631        TriggerMap         mTriggerMap;
632        TriggerMap         mTriggerRemovedMap;
633        TriggerMap         mTriggerReplacedMap;
634        uint32_t           mCurrentAfTriggerId;
635        uint32_t           mCurrentPreCaptureTriggerId;
636
637        int64_t            mRepeatingLastFrameNumber;
638
639        // Whether the device supports AE lock
640        bool               mAeLockAvailable;
641
642        // Flag indicating if we should prepare video stream for video requests.
643        bool               mPrepareVideoStream;
644    };
645    sp<RequestThread> mRequestThread;
646
647    /**
648     * In-flight queue for tracking completion of capture requests.
649     */
650
651    struct InFlightRequest {
652        // Set by notify() SHUTTER call.
653        nsecs_t shutterTimestamp;
654        // Set by process_capture_result().
655        nsecs_t sensorTimestamp;
656        int     requestStatus;
657        // Set by process_capture_result call with valid metadata
658        bool    haveResultMetadata;
659        // Decremented by calls to process_capture_result with valid output
660        // and input buffers
661        int     numBuffersLeft;
662        CaptureResultExtras resultExtras;
663        // If this request has any input buffer
664        bool hasInputBuffer;
665
666        // The last metadata that framework receives from HAL and
667        // not yet send out because the shutter event hasn't arrived.
668        // It's added by process_capture_result and sent when framework
669        // receives the shutter event.
670        CameraMetadata pendingMetadata;
671
672        // The metadata of the partial results that framework receives from HAL so far
673        // and has sent out.
674        CameraMetadata collectedPartialResult;
675
676        // Buffers are added by process_capture_result when output buffers
677        // return from HAL but framework has not yet received the shutter
678        // event. They will be returned to the streams when framework receives
679        // the shutter event.
680        Vector<camera3_stream_buffer_t> pendingOutputBuffers;
681
682        // Used to cancel AE precapture trigger for devices doesn't support
683        // CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL
684        AeTriggerCancelOverride_t aeTriggerCancelOverride;
685
686        // Default constructor needed by KeyedVector
687        InFlightRequest() :
688                shutterTimestamp(0),
689                sensorTimestamp(0),
690                requestStatus(OK),
691                haveResultMetadata(false),
692                numBuffersLeft(0),
693                hasInputBuffer(false),
694                aeTriggerCancelOverride({false, 0, false, 0}){
695        }
696
697        InFlightRequest(int numBuffers, CaptureResultExtras extras, bool hasInput,
698                AeTriggerCancelOverride aeTriggerCancelOverride) :
699                shutterTimestamp(0),
700                sensorTimestamp(0),
701                requestStatus(OK),
702                haveResultMetadata(false),
703                numBuffersLeft(numBuffers),
704                resultExtras(extras),
705                hasInputBuffer(hasInput),
706                aeTriggerCancelOverride(aeTriggerCancelOverride){
707        }
708    };
709
710    // Map from frame number to the in-flight request state
711    typedef KeyedVector<uint32_t, InFlightRequest> InFlightMap;
712
713    Mutex                  mInFlightLock; // Protects mInFlightMap
714    InFlightMap            mInFlightMap;
715
716    status_t registerInFlight(uint32_t frameNumber,
717            int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
718            const AeTriggerCancelOverride_t &aeTriggerCancelOverride);
719
720    /**
721     * Override result metadata for cancelling AE precapture trigger applied in
722     * handleAePrecaptureCancelRequest().
723     */
724    void overrideResultForPrecaptureCancel(CameraMetadata* result,
725            const AeTriggerCancelOverride_t &aeTriggerCancelOverride);
726
727    /**
728     * Tracking for idle detection
729     */
730    sp<camera3::StatusTracker> mStatusTracker;
731
732    /**
733     * Graphic buffer manager for output streams. Each device has a buffer manager, which is used
734     * by the output streams to get and return buffers if these streams are registered to this
735     * buffer manager.
736     */
737    sp<camera3::Camera3BufferManager> mBufferManager;
738
739    /**
740     * Thread for preparing streams
741     */
742    class PreparerThread : private Thread, public virtual RefBase {
743      public:
744        PreparerThread();
745        ~PreparerThread();
746
747        void setNotificationListener(NotificationListener *listener);
748
749        /**
750         * Queue up a stream to be prepared. Streams are processed by a background thread in FIFO
751         * order.  Pre-allocate up to maxCount buffers for the stream, or the maximum number needed
752         * for the pipeline if maxCount is ALLOCATE_PIPELINE_MAX.
753         */
754        status_t prepare(int maxCount, sp<camera3::Camera3StreamInterface>& stream);
755
756        /**
757         * Cancel all current and pending stream preparation
758         */
759        status_t clear();
760
761      private:
762        Mutex mLock;
763
764        virtual bool threadLoop();
765
766        // Guarded by mLock
767
768        NotificationListener *mListener;
769        List<sp<camera3::Camera3StreamInterface> > mPendingStreams;
770        bool mActive;
771        bool mCancelNow;
772
773        // Only accessed by threadLoop and the destructor
774
775        sp<camera3::Camera3StreamInterface> mCurrentStream;
776    };
777    sp<PreparerThread> mPreparerThread;
778
779    /**
780     * Output result queue and current HAL device 3A state
781     */
782
783    // Lock for output side of device
784    Mutex                  mOutputLock;
785
786    /**** Scope for mOutputLock ****/
787    // the minimal frame number of the next non-reprocess result
788    uint32_t               mNextResultFrameNumber;
789    // the minimal frame number of the next reprocess result
790    uint32_t               mNextReprocessResultFrameNumber;
791    // the minimal frame number of the next non-reprocess shutter
792    uint32_t               mNextShutterFrameNumber;
793    // the minimal frame number of the next reprocess shutter
794    uint32_t               mNextReprocessShutterFrameNumber;
795    List<CaptureResult>   mResultQueue;
796    Condition              mResultSignal;
797    NotificationListener  *mListener;
798
799    /**** End scope for mOutputLock ****/
800
801    /**
802     * Callback functions from HAL device
803     */
804    void processCaptureResult(const camera3_capture_result *result);
805
806    void notify(const camera3_notify_msg *msg);
807
808    // Specific notify handlers
809    void notifyError(const camera3_error_msg_t &msg,
810            NotificationListener *listener);
811    void notifyShutter(const camera3_shutter_msg_t &msg,
812            NotificationListener *listener);
813
814    // helper function to return the output buffers to the streams.
815    void returnOutputBuffers(const camera3_stream_buffer_t *outputBuffers,
816            size_t numBuffers, nsecs_t timestamp);
817
818    // Send a partial capture result.
819    void sendPartialCaptureResult(const camera_metadata_t * partialResult,
820            const CaptureResultExtras &resultExtras, uint32_t frameNumber,
821            const AeTriggerCancelOverride_t &aeTriggerCancelOverride);
822
823    // Send a total capture result given the pending metadata and result extras,
824    // partial results, and the frame number to the result queue.
825    void sendCaptureResult(CameraMetadata &pendingMetadata,
826            CaptureResultExtras &resultExtras,
827            CameraMetadata &collectedPartialResult, uint32_t frameNumber,
828            bool reprocess, const AeTriggerCancelOverride_t &aeTriggerCancelOverride);
829
830    // Insert the result to the result queue after updating frame number and overriding AE
831    // trigger cancel.
832    // mOutputLock must be held when calling this function.
833    void insertResultLocked(CaptureResult *result, uint32_t frameNumber,
834            const AeTriggerCancelOverride_t &aeTriggerCancelOverride);
835
836    /**** Scope for mInFlightLock ****/
837
838    // Remove the in-flight request of the given index from mInFlightMap
839    // if it's no longer needed. It must only be called with mInFlightLock held.
840    void removeInFlightRequestIfReadyLocked(int idx);
841
842    /**** End scope for mInFlightLock ****/
843
844    /**
845     * Static callback forwarding methods from HAL to instance
846     */
847    static callbacks_process_capture_result_t sProcessCaptureResult;
848
849    static callbacks_notify_t sNotify;
850
851}; // class Camera3Device
852
853}; // namespace android
854
855#endif
856