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