Home | History | Annotate | Download | only in device3
      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 <utility>
     21 #include <unordered_map>
     22 
     23 #include <utils/Condition.h>
     24 #include <utils/Errors.h>
     25 #include <utils/List.h>
     26 #include <utils/Mutex.h>
     27 #include <utils/Thread.h>
     28 #include <utils/KeyedVector.h>
     29 #include <utils/Timers.h>
     30 
     31 #include <android/hardware/camera/device/3.2/ICameraDevice.h>
     32 #include <android/hardware/camera/device/3.2/ICameraDeviceSession.h>
     33 #include <android/hardware/camera/device/3.2/ICameraDeviceCallback.h>
     34 #include <fmq/MessageQueue.h>
     35 #include <hardware/camera3.h>
     36 
     37 #include <camera/CaptureResult.h>
     38 
     39 #include "common/CameraDeviceBase.h"
     40 #include "device3/StatusTracker.h"
     41 #include "device3/Camera3BufferManager.h"
     42 #include "utils/TagMonitor.h"
     43 #include "utils/LatencyHistogram.h"
     44 #include <camera_metadata_hidden.h>
     45 
     46 /**
     47  * Function pointer types with C calling convention to
     48  * use for HAL callback functions.
     49  */
     50 extern "C" {
     51     typedef void (callbacks_process_capture_result_t)(
     52         const struct camera3_callback_ops *,
     53         const camera3_capture_result_t *);
     54 
     55     typedef void (callbacks_notify_t)(
     56         const struct camera3_callback_ops *,
     57         const camera3_notify_msg_t *);
     58 }
     59 
     60 namespace android {
     61 
     62 namespace camera3 {
     63 
     64 class Camera3Stream;
     65 class Camera3ZslStream;
     66 class Camera3OutputStreamInterface;
     67 class Camera3StreamInterface;
     68 
     69 } // namespace camera3
     70 
     71 /**
     72  * CameraDevice for HAL devices with version CAMERA_DEVICE_API_VERSION_3_0 or higher.
     73  */
     74 class Camera3Device :
     75             public CameraDeviceBase,
     76             virtual public hardware::camera::device::V3_2::ICameraDeviceCallback,
     77             private camera3_callback_ops {
     78   public:
     79 
     80     explicit Camera3Device(const String8& id);
     81 
     82     virtual ~Camera3Device();
     83 
     84     /**
     85      * CameraDeviceBase interface
     86      */
     87 
     88     const String8& getId() const override;
     89 
     90     // Transitions to idle state on success.
     91     status_t initialize(sp<CameraProviderManager> manager) override;
     92     status_t disconnect() override;
     93     status_t dump(int fd, const Vector<String16> &args) override;
     94     const CameraMetadata& info() const override;
     95 
     96     // Capture and setStreamingRequest will configure streams if currently in
     97     // idle state
     98     status_t capture(CameraMetadata &request, int64_t *lastFrameNumber = NULL) override;
     99     status_t captureList(const List<const CameraMetadata> &requests,
    100             const std::list<const SurfaceMap> &surfaceMaps,
    101             int64_t *lastFrameNumber = NULL) override;
    102     status_t setStreamingRequest(const CameraMetadata &request,
    103             int64_t *lastFrameNumber = NULL) override;
    104     status_t setStreamingRequestList(const List<const CameraMetadata> &requests,
    105             const std::list<const SurfaceMap> &surfaceMaps,
    106             int64_t *lastFrameNumber = NULL) override;
    107     status_t clearStreamingRequest(int64_t *lastFrameNumber = NULL) override;
    108 
    109     status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) override;
    110 
    111     // Actual stream creation/deletion is delayed until first request is submitted
    112     // If adding streams while actively capturing, will pause device before adding
    113     // stream, reconfiguring device, and unpausing. If the client create a stream
    114     // with nullptr consumer surface, the client must then call setConsumers()
    115     // and finish the stream configuration before starting output streaming.
    116     status_t createStream(sp<Surface> consumer,
    117             uint32_t width, uint32_t height, int format,
    118             android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
    119             int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID,
    120             bool isShared = false, uint32_t consumerUsage = 0) override;
    121     status_t createStream(const std::vector<sp<Surface>>& consumers,
    122             bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
    123             android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
    124             int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID,
    125             bool isShared = false, uint32_t consumerUsage = 0) override;
    126 
    127     status_t createInputStream(
    128             uint32_t width, uint32_t height, int format,
    129             int *id) override;
    130 
    131     status_t getStreamInfo(int id,
    132             uint32_t *width, uint32_t *height,
    133             uint32_t *format, android_dataspace *dataSpace) override;
    134     status_t setStreamTransform(int id, int transform) override;
    135 
    136     status_t deleteStream(int id) override;
    137 
    138     status_t configureStreams(int operatingMode =
    139             static_cast<int>(hardware::camera::device::V3_2::StreamConfigurationMode::NORMAL_MODE))
    140             override;
    141     status_t getInputBufferProducer(
    142             sp<IGraphicBufferProducer> *producer) override;
    143 
    144     status_t createDefaultRequest(int templateId, CameraMetadata *request) override;
    145 
    146     // Transitions to the idle state on success
    147     status_t waitUntilDrained() override;
    148 
    149     status_t setNotifyCallback(wp<NotificationListener> listener) override;
    150     bool     willNotify3A() override;
    151     status_t waitForNextFrame(nsecs_t timeout) override;
    152     status_t getNextResult(CaptureResult *frame) override;
    153 
    154     status_t triggerAutofocus(uint32_t id) override;
    155     status_t triggerCancelAutofocus(uint32_t id) override;
    156     status_t triggerPrecaptureMetering(uint32_t id) override;
    157 
    158     status_t flush(int64_t *lastFrameNumber = NULL) override;
    159 
    160     status_t prepare(int streamId) override;
    161 
    162     status_t tearDown(int streamId) override;
    163 
    164     status_t addBufferListenerForStream(int streamId,
    165             wp<camera3::Camera3StreamBufferListener> listener) override;
    166 
    167     status_t prepare(int maxCount, int streamId) override;
    168 
    169     ssize_t getJpegBufferSize(uint32_t width, uint32_t height) const override;
    170     ssize_t getPointCloudBufferSize() const;
    171     ssize_t getRawOpaqueBufferSize(int32_t width, int32_t height) const;
    172 
    173     // Methods called by subclasses
    174     void             notifyStatus(bool idle); // updates from StatusTracker
    175 
    176     /**
    177      * Set the deferred consumer surfaces to the output stream and finish the deferred
    178      * consumer configuration.
    179      */
    180     status_t setConsumerSurfaces(int streamId, const std::vector<sp<Surface>>& consumers) override;
    181 
    182   private:
    183 
    184     // internal typedefs
    185     using RequestMetadataQueue = hardware::MessageQueue<uint8_t, hardware::kSynchronizedReadWrite>;
    186     using ResultMetadataQueue  = hardware::MessageQueue<uint8_t, hardware::kSynchronizedReadWrite>;
    187 
    188     static const size_t        kDumpLockAttempts  = 10;
    189     static const size_t        kDumpSleepDuration = 100000; // 0.10 sec
    190     static const nsecs_t       kShutdownTimeout   = 5000000000; // 5 sec
    191     static const nsecs_t       kActiveTimeout     = 500000000;  // 500 ms
    192     static const size_t        kInFlightWarnLimit = 20;
    193     static const size_t        kInFlightWarnLimitHighSpeed = 256; // batch size 32 * pipe depth 8
    194     // SCHED_FIFO priority for request submission thread in HFR mode
    195     static const int           kRequestThreadPriority = 1;
    196 
    197     struct                     RequestTrigger;
    198     // minimal jpeg buffer size: 256KB + blob header
    199     static const ssize_t       kMinJpegBufferSize = 256 * 1024 + sizeof(camera3_jpeg_blob);
    200     // Constant to use for stream ID when one doesn't exist
    201     static const int           NO_STREAM = -1;
    202 
    203     // A lock to enforce serialization on the input/configure side
    204     // of the public interface.
    205     // Only locked by public methods inherited from CameraDeviceBase.
    206     // Not locked by methods guarded by mOutputLock, since they may act
    207     // concurrently to the input/configure side of the interface.
    208     // Must be locked before mLock if both will be locked by a method
    209     Mutex                      mInterfaceLock;
    210 
    211     // The main lock on internal state
    212     Mutex                      mLock;
    213 
    214     // Camera device ID
    215     const String8              mId;
    216 
    217     // Current stream configuration mode;
    218     int                        mOperatingMode;
    219     // Constant to use for no set operating mode
    220     static const int           NO_MODE = -1;
    221 
    222     // Flag indicating is the current active stream configuration is constrained high speed.
    223     bool                       mIsConstrainedHighSpeedConfiguration;
    224 
    225     // FMQ to write result on. Must be guarded by mProcessCaptureResultLock.
    226     std::unique_ptr<ResultMetadataQueue> mResultMetadataQueue;
    227 
    228     /**** Scope for mLock ****/
    229 
    230     /**
    231      * Adapter for legacy HAL / HIDL HAL interface calls; calls either into legacy HALv3 or the
    232      * HIDL HALv3 interfaces.
    233      */
    234     class HalInterface : public camera3::Camera3StreamBufferFreedListener {
    235       public:
    236         HalInterface(sp<hardware::camera::device::V3_2::ICameraDeviceSession> &session,
    237                      std::shared_ptr<RequestMetadataQueue> queue);
    238         HalInterface(const HalInterface &other);
    239         HalInterface();
    240 
    241         // Returns true if constructed with a valid device or session, and not yet cleared
    242         bool valid();
    243 
    244         // Reset this HalInterface object (does not call close())
    245         void clear();
    246 
    247         // Check if HalInterface support sending requests in batch
    248         bool supportBatchRequest();
    249 
    250         // Calls into the HAL interface
    251 
    252         // Caller takes ownership of requestTemplate
    253         status_t constructDefaultRequestSettings(camera3_request_template_t templateId,
    254                 /*out*/ camera_metadata_t **requestTemplate);
    255         status_t configureStreams(/*inout*/ camera3_stream_configuration *config);
    256         status_t processCaptureRequest(camera3_capture_request_t *request);
    257         status_t processBatchCaptureRequests(
    258                 std::vector<camera3_capture_request_t*>& requests,
    259                 /*out*/uint32_t* numRequestProcessed);
    260         status_t flush();
    261         status_t dump(int fd);
    262         status_t close();
    263 
    264         // Find a buffer_handle_t based on frame number and stream ID
    265         status_t popInflightBuffer(int32_t frameNumber, int32_t streamId,
    266                 /*out*/ buffer_handle_t **buffer);
    267 
    268       private:
    269         camera3_device_t *mHal3Device;
    270         sp<hardware::camera::device::V3_2::ICameraDeviceSession> mHidlSession;
    271         std::shared_ptr<RequestMetadataQueue> mRequestMetadataQueue;
    272 
    273         std::mutex mInflightLock;
    274 
    275         // The output HIDL request still depends on input camera3_capture_request_t
    276         // Do not free input camera3_capture_request_t before output HIDL request
    277         void wrapAsHidlRequest(camera3_capture_request_t* in,
    278                 /*out*/hardware::camera::device::V3_2::CaptureRequest* out,
    279                 /*out*/std::vector<native_handle_t*>* handlesCreated);
    280 
    281         status_t pushInflightBufferLocked(int32_t frameNumber, int32_t streamId,
    282                 buffer_handle_t *buffer, int acquireFence);
    283         // Cache of buffer handles keyed off (frameNumber << 32 | streamId)
    284         // value is a pair of (buffer_handle_t*, acquire_fence FD)
    285         std::unordered_map<uint64_t, std::pair<buffer_handle_t*, int>> mInflightBufferMap;
    286 
    287         struct BufferHasher {
    288             size_t operator()(const buffer_handle_t& buf) const {
    289                 if (buf == nullptr)
    290                     return 0;
    291 
    292                 size_t result = 1;
    293                 result = 31 * result + buf->numFds;
    294                 for (int i = 0; i < buf->numFds; i++) {
    295                     result = 31 * result + buf->data[i];
    296                 }
    297                 return result;
    298             }
    299         };
    300 
    301         struct BufferComparator {
    302             bool operator()(const buffer_handle_t& buf1, const buffer_handle_t& buf2) const {
    303                 if (buf1->numFds == buf2->numFds) {
    304                     for (int i = 0; i < buf1->numFds; i++) {
    305                         if (buf1->data[i] != buf2->data[i]) {
    306                             return false;
    307                         }
    308                     }
    309                     return true;
    310                 }
    311                 return false;
    312             }
    313         };
    314 
    315         std::mutex mBufferIdMapLock; // protecting mBufferIdMaps and mNextBufferId
    316         typedef std::unordered_map<const buffer_handle_t, uint64_t,
    317                 BufferHasher, BufferComparator> BufferIdMap;
    318         // stream ID -> per stream buffer ID map
    319         std::unordered_map<int, BufferIdMap> mBufferIdMaps;
    320         uint64_t mNextBufferId = 1; // 0 means no buffer
    321         static const uint64_t BUFFER_ID_NO_BUFFER = 0;
    322 
    323         // method to extract buffer's unique ID
    324         // TODO: we should switch to use gralloc mapper's getBackingStore API
    325         //       once we ran in binderized gralloc mode, but before that is ready,
    326         //       we need to rely on the conventional buffer queue behavior where
    327         //       buffer_handle_t's FD won't change.
    328         // return pair of (newlySeenBuffer?, bufferId)
    329         std::pair<bool, uint64_t> getBufferId(const buffer_handle_t& buf, int streamId);
    330 
    331         virtual void onBufferFreed(int streamId, const native_handle_t* handle) override;
    332 
    333         std::vector<std::pair<int, uint64_t>> mFreedBuffers;
    334     };
    335 
    336     std::unique_ptr<HalInterface> mInterface;
    337 
    338     CameraMetadata             mDeviceInfo;
    339 
    340     CameraMetadata             mRequestTemplateCache[CAMERA3_TEMPLATE_COUNT];
    341 
    342     struct Size {
    343         uint32_t width;
    344         uint32_t height;
    345         explicit Size(uint32_t w = 0, uint32_t h = 0) : width(w), height(h){}
    346     };
    347     // Map from format to size.
    348     Vector<Size>               mSupportedOpaqueInputSizes;
    349 
    350     enum Status {
    351         STATUS_ERROR,
    352         STATUS_UNINITIALIZED,
    353         STATUS_UNCONFIGURED,
    354         STATUS_CONFIGURED,
    355         STATUS_ACTIVE
    356     }                          mStatus;
    357 
    358     // Only clear mRecentStatusUpdates, mStatusWaiters from waitUntilStateThenRelock
    359     Vector<Status>             mRecentStatusUpdates;
    360     int                        mStatusWaiters;
    361 
    362     Condition                  mStatusChanged;
    363 
    364     // Tracking cause of fatal errors when in STATUS_ERROR
    365     String8                    mErrorCause;
    366 
    367     // Mapping of stream IDs to stream instances
    368     typedef KeyedVector<int, sp<camera3::Camera3OutputStreamInterface> >
    369             StreamSet;
    370 
    371     StreamSet                  mOutputStreams;
    372     sp<camera3::Camera3Stream> mInputStream;
    373     int                        mNextStreamId;
    374     bool                       mNeedConfig;
    375 
    376     int                        mDummyStreamId;
    377 
    378     // Whether to send state updates upstream
    379     // Pause when doing transparent reconfiguration
    380     bool                       mPauseStateNotify;
    381 
    382     // Need to hold on to stream references until configure completes.
    383     Vector<sp<camera3::Camera3StreamInterface> > mDeletedStreams;
    384 
    385     // Whether the HAL will send partial result
    386     bool                       mUsePartialResult;
    387 
    388     // Number of partial results that will be delivered by the HAL.
    389     uint32_t                   mNumPartialResults;
    390 
    391     /**** End scope for mLock ****/
    392 
    393     // The offset converting from clock domain of other subsystem
    394     // (video/hardware composer) to that of camera. Assumption is that this
    395     // offset won't change during the life cycle of the camera device. In other
    396     // words, camera device shouldn't be open during CPU suspend.
    397     nsecs_t                    mTimestampOffset;
    398 
    399     class CaptureRequest : public LightRefBase<CaptureRequest> {
    400       public:
    401         CameraMetadata                      mSettings;
    402         sp<camera3::Camera3Stream>          mInputStream;
    403         camera3_stream_buffer_t             mInputBuffer;
    404         Vector<sp<camera3::Camera3OutputStreamInterface> >
    405                                             mOutputStreams;
    406         SurfaceMap                          mOutputSurfaces;
    407         CaptureResultExtras                 mResultExtras;
    408         // The number of requests that should be submitted to HAL at a time.
    409         // For example, if batch size is 8, this request and the following 7
    410         // requests will be submitted to HAL at a time. The batch size for
    411         // the following 7 requests will be ignored by the request thread.
    412         int                                 mBatchSize;
    413         //  Whether this request is from a repeating or repeating burst.
    414         bool                                mRepeating;
    415     };
    416     typedef List<sp<CaptureRequest> > RequestList;
    417 
    418     status_t checkStatusOkToCaptureLocked();
    419 
    420     status_t convertMetadataListToRequestListLocked(
    421             const List<const CameraMetadata> &metadataList,
    422             const std::list<const SurfaceMap> &surfaceMaps,
    423             bool repeating,
    424             /*out*/
    425             RequestList *requestList);
    426 
    427     void convertToRequestList(List<const CameraMetadata>& requests,
    428             std::list<const SurfaceMap>& surfaceMaps,
    429             const CameraMetadata& request);
    430 
    431     status_t submitRequestsHelper(const List<const CameraMetadata> &requests,
    432                                   const std::list<const SurfaceMap> &surfaceMaps,
    433                                   bool repeating,
    434                                   int64_t *lastFrameNumber = NULL);
    435 
    436 
    437     /**
    438      * Implementation of android::hardware::camera::device::V3_2::ICameraDeviceCallback
    439      */
    440 
    441     hardware::Return<void> processCaptureResult(
    442             const hardware::hidl_vec<
    443                     hardware::camera::device::V3_2::CaptureResult>& results) override;
    444     hardware::Return<void> notify(
    445             const hardware::hidl_vec<
    446                     hardware::camera::device::V3_2::NotifyMsg>& msgs) override;
    447 
    448     // Handle one capture result. Assume that mProcessCaptureResultLock is held.
    449     void processOneCaptureResultLocked(
    450             const hardware::camera::device::V3_2::CaptureResult& results);
    451     // Handle one notify message
    452     void notify(const hardware::camera::device::V3_2::NotifyMsg& msg);
    453 
    454     // lock to ensure only one processCaptureResult is called at a time.
    455     Mutex mProcessCaptureResultLock;
    456 
    457     /**
    458      * Common initialization code shared by both HAL paths
    459      *
    460      * Must be called with mLock and mInterfaceLock held.
    461      */
    462     status_t initializeCommonLocked();
    463 
    464     /**
    465      * Get the last request submitted to the hal by the request thread.
    466      *
    467      * Must be called with mLock held.
    468      */
    469     virtual CameraMetadata getLatestRequestLocked();
    470 
    471     /**
    472      * Update the current device status and wake all waiting threads.
    473      *
    474      * Must be called with mLock held.
    475      */
    476     void internalUpdateStatusLocked(Status status);
    477 
    478     /**
    479      * Pause processing and flush everything, but don't tell the clients.
    480      * This is for reconfiguring outputs transparently when according to the
    481      * CameraDeviceBase interface we shouldn't need to.
    482      * Must be called with mLock and mInterfaceLock both held.
    483      */
    484     status_t internalPauseAndWaitLocked();
    485 
    486     /**
    487      * Resume work after internalPauseAndWaitLocked()
    488      * Must be called with mLock and mInterfaceLock both held.
    489      */
    490     status_t internalResumeLocked();
    491 
    492     /**
    493      * Wait until status tracker tells us we've transitioned to the target state
    494      * set, which is either ACTIVE when active==true or IDLE (which is any
    495      * non-ACTIVE state) when active==false.
    496      *
    497      * Needs to be called with mLock and mInterfaceLock held.  This means there
    498      * can ever only be one waiter at most.
    499      *
    500      * During the wait mLock is released.
    501      *
    502      */
    503     status_t waitUntilStateThenRelock(bool active, nsecs_t timeout);
    504 
    505     /**
    506      * Implementation of waitUntilDrained. On success, will transition to IDLE state.
    507      *
    508      * Need to be called with mLock and mInterfaceLock held.
    509      */
    510     status_t waitUntilDrainedLocked();
    511 
    512     /**
    513      * Do common work for setting up a streaming or single capture request.
    514      * On success, will transition to ACTIVE if in IDLE.
    515      */
    516     sp<CaptureRequest> setUpRequestLocked(const CameraMetadata &request,
    517                                           const SurfaceMap &surfaceMap);
    518 
    519     /**
    520      * Build a CaptureRequest request from the CameraDeviceBase request
    521      * settings.
    522      */
    523     sp<CaptureRequest> createCaptureRequest(const CameraMetadata &request,
    524                                             const SurfaceMap &surfaceMap);
    525 
    526     /**
    527      * Take the currently-defined set of streams and configure the HAL to use
    528      * them. This is a long-running operation (may be several hundered ms).
    529      */
    530     status_t           configureStreamsLocked(int operatingMode);
    531 
    532     /**
    533      * Cancel stream configuration that did not finish successfully.
    534      */
    535     void               cancelStreamsConfigurationLocked();
    536 
    537     /**
    538      * Add a dummy stream to the current stream set as a workaround for
    539      * not allowing 0 streams in the camera HAL spec.
    540      */
    541     status_t           addDummyStreamLocked();
    542 
    543     /**
    544      * Remove a dummy stream if the current config includes real streams.
    545      */
    546     status_t           tryRemoveDummyStreamLocked();
    547 
    548     /**
    549      * Set device into an error state due to some fatal failure, and set an
    550      * error message to indicate why. Only the first call's message will be
    551      * used. The message is also sent to the log.
    552      */
    553     void               setErrorState(const char *fmt, ...);
    554     void               setErrorStateV(const char *fmt, va_list args);
    555     void               setErrorStateLocked(const char *fmt, ...);
    556     void               setErrorStateLockedV(const char *fmt, va_list args);
    557 
    558     /**
    559      * Debugging trylock/spin method
    560      * Try to acquire a lock a few times with sleeps between before giving up.
    561      */
    562     bool               tryLockSpinRightRound(Mutex& lock);
    563 
    564     /**
    565      * Helper function to determine if an input size for implementation defined
    566      * format is supported.
    567      */
    568     bool isOpaqueInputSizeSupported(uint32_t width, uint32_t height);
    569 
    570     /**
    571      * Helper function to get the largest Jpeg resolution (in area)
    572      * Return Size(0, 0) if static metatdata is invalid
    573      */
    574     Size getMaxJpegResolution() const;
    575 
    576     /**
    577      * Helper function to get the offset between MONOTONIC and BOOTTIME
    578      * timestamp.
    579      */
    580     static nsecs_t getMonoToBoottimeOffset();
    581 
    582     /**
    583      * Helper functions to map between framework and HIDL values
    584      */
    585     static hardware::graphics::common::V1_0::PixelFormat mapToPixelFormat(int frameworkFormat);
    586     static hardware::camera::device::V3_2::DataspaceFlags mapToHidlDataspace(
    587             android_dataspace dataSpace);
    588     static hardware::camera::device::V3_2::BufferUsageFlags mapToConsumerUsage(uint32_t usage);
    589     static hardware::camera::device::V3_2::StreamRotation mapToStreamRotation(
    590             camera3_stream_rotation_t rotation);
    591     // Returns a negative error code if the passed-in operation mode is not valid.
    592     static status_t mapToStreamConfigurationMode(camera3_stream_configuration_mode_t operationMode,
    593             /*out*/ hardware::camera::device::V3_2::StreamConfigurationMode *mode);
    594     static camera3_buffer_status_t mapHidlBufferStatus(hardware::camera::device::V3_2::BufferStatus status);
    595     static int mapToFrameworkFormat(hardware::graphics::common::V1_0::PixelFormat pixelFormat);
    596     static uint32_t mapConsumerToFrameworkUsage(
    597             hardware::camera::device::V3_2::BufferUsageFlags usage);
    598     static uint32_t mapProducerToFrameworkUsage(
    599             hardware::camera::device::V3_2::BufferUsageFlags usage);
    600 
    601     struct RequestTrigger {
    602         // Metadata tag number, e.g. android.control.aePrecaptureTrigger
    603         uint32_t metadataTag;
    604         // Metadata value, e.g. 'START' or the trigger ID
    605         int32_t entryValue;
    606 
    607         // The last part of the fully qualified path, e.g. afTrigger
    608         const char *getTagName() const {
    609             return get_camera_metadata_tag_name(metadataTag) ?: "NULL";
    610         }
    611 
    612         // e.g. TYPE_BYTE, TYPE_INT32, etc.
    613         int getTagType() const {
    614             return get_camera_metadata_tag_type(metadataTag);
    615         }
    616     };
    617 
    618     /**
    619      * Thread for managing capture request submission to HAL device.
    620      */
    621     class RequestThread : public Thread {
    622 
    623       public:
    624 
    625         RequestThread(wp<Camera3Device> parent,
    626                 sp<camera3::StatusTracker> statusTracker,
    627                 HalInterface* interface);
    628         ~RequestThread();
    629 
    630         void     setNotificationListener(wp<NotificationListener> listener);
    631 
    632         /**
    633          * Call after stream (re)-configuration is completed.
    634          */
    635         void     configurationComplete(bool isConstrainedHighSpeed);
    636 
    637         /**
    638          * Set or clear the list of repeating requests. Does not block
    639          * on either. Use waitUntilPaused to wait until request queue
    640          * has emptied out.
    641          */
    642         status_t setRepeatingRequests(const RequestList& requests,
    643                                       /*out*/
    644                                       int64_t *lastFrameNumber = NULL);
    645         status_t clearRepeatingRequests(/*out*/
    646                                         int64_t *lastFrameNumber = NULL);
    647 
    648         status_t queueRequestList(List<sp<CaptureRequest> > &requests,
    649                                   /*out*/
    650                                   int64_t *lastFrameNumber = NULL);
    651 
    652         /**
    653          * Remove all queued and repeating requests, and pending triggers
    654          */
    655         status_t clear(/*out*/int64_t *lastFrameNumber = NULL);
    656 
    657         /**
    658          * Flush all pending requests in HAL.
    659          */
    660         status_t flush();
    661 
    662         /**
    663          * Queue a trigger to be dispatched with the next outgoing
    664          * process_capture_request. The settings for that request only
    665          * will be temporarily rewritten to add the trigger tag/value.
    666          * Subsequent requests will not be rewritten (for this tag).
    667          */
    668         status_t queueTrigger(RequestTrigger trigger[], size_t count);
    669 
    670         /**
    671          * Pause/unpause the capture thread. Doesn't block, so use
    672          * waitUntilPaused to wait until the thread is paused.
    673          */
    674         void     setPaused(bool paused);
    675 
    676         /**
    677          * Wait until thread processes the capture request with settings'
    678          * android.request.id == requestId.
    679          *
    680          * Returns TIMED_OUT in case the thread does not process the request
    681          * within the timeout.
    682          */
    683         status_t waitUntilRequestProcessed(int32_t requestId, nsecs_t timeout);
    684 
    685         /**
    686          * Shut down the thread. Shutdown is asynchronous, so thread may
    687          * still be running once this method returns.
    688          */
    689         virtual void requestExit();
    690 
    691         /**
    692          * Get the latest request that was sent to the HAL
    693          * with process_capture_request.
    694          */
    695         CameraMetadata getLatestRequest() const;
    696 
    697         /**
    698          * Returns true if the stream is a target of any queued or repeating
    699          * capture request
    700          */
    701         bool isStreamPending(sp<camera3::Camera3StreamInterface>& stream);
    702 
    703         // dump processCaptureRequest latency
    704         void dumpCaptureRequestLatency(int fd, const char* name) {
    705             mRequestLatency.dump(fd, name);
    706         }
    707 
    708       protected:
    709 
    710         virtual bool threadLoop();
    711 
    712       private:
    713         static const String8& getId(const wp<Camera3Device> &device);
    714 
    715         status_t           queueTriggerLocked(RequestTrigger trigger);
    716         // Mix-in queued triggers into this request
    717         int32_t            insertTriggers(const sp<CaptureRequest> &request);
    718         // Purge the queued triggers from this request,
    719         //  restoring the old field values for those tags.
    720         status_t           removeTriggers(const sp<CaptureRequest> &request);
    721 
    722         // HAL workaround: Make sure a trigger ID always exists if
    723         // a trigger does
    724         status_t          addDummyTriggerIds(const sp<CaptureRequest> &request);
    725 
    726         static const nsecs_t kRequestTimeout = 50e6; // 50 ms
    727 
    728         // Used to prepare a batch of requests.
    729         struct NextRequest {
    730             sp<CaptureRequest>              captureRequest;
    731             camera3_capture_request_t       halRequest;
    732             Vector<camera3_stream_buffer_t> outputBuffers;
    733             bool                            submitted;
    734         };
    735 
    736         // Wait for the next batch of requests and put them in mNextRequests. mNextRequests will
    737         // be empty if it times out.
    738         void waitForNextRequestBatch();
    739 
    740         // Waits for a request, or returns NULL if times out. Must be called with mRequestLock hold.
    741         sp<CaptureRequest> waitForNextRequestLocked();
    742 
    743         // Prepare HAL requests and output buffers in mNextRequests. Return TIMED_OUT if getting any
    744         // output buffer timed out. If an error is returned, the caller should clean up the pending
    745         // request batch.
    746         status_t prepareHalRequests();
    747 
    748         // Return buffers, etc, for requests in mNextRequests that couldn't be fully constructed and
    749         // send request errors if sendRequestError is true. The buffers will be returned in the
    750         // ERROR state to mark them as not having valid data. mNextRequests will be cleared.
    751         void cleanUpFailedRequests(bool sendRequestError);
    752 
    753         // Stop the repeating request if any of its output streams is abandoned.
    754         void checkAndStopRepeatingRequest();
    755 
    756         // Pause handling
    757         bool               waitIfPaused();
    758         void               unpauseForNewRequests();
    759 
    760         // Relay error to parent device object setErrorState
    761         void               setErrorState(const char *fmt, ...);
    762 
    763         // If the input request is in mRepeatingRequests. Must be called with mRequestLock hold
    764         bool isRepeatingRequestLocked(const sp<CaptureRequest>&);
    765 
    766         // Clear repeating requests. Must be called with mRequestLock held.
    767         status_t clearRepeatingRequestsLocked(/*out*/ int64_t *lastFrameNumber = NULL);
    768 
    769         // send request in mNextRequests to HAL one by one. Return true = sucssess
    770         bool sendRequestsOneByOne();
    771 
    772         // send request in mNextRequests to HAL in a batch. Return true = sucssess
    773         bool sendRequestsBatch();
    774 
    775         wp<Camera3Device>  mParent;
    776         wp<camera3::StatusTracker>  mStatusTracker;
    777         HalInterface*      mInterface;
    778 
    779         wp<NotificationListener> mListener;
    780 
    781         const String8&     mId;       // The camera ID
    782         int                mStatusId; // The RequestThread's component ID for
    783                                       // status tracking
    784 
    785         Mutex              mRequestLock;
    786         Condition          mRequestSignal;
    787         RequestList        mRequestQueue;
    788         RequestList        mRepeatingRequests;
    789         // The next batch of requests being prepped for submission to the HAL, no longer
    790         // on the request queue. Read-only even with mRequestLock held, outside
    791         // of threadLoop
    792         Vector<NextRequest> mNextRequests;
    793 
    794         // To protect flush() and sending a request batch to HAL.
    795         Mutex              mFlushLock;
    796 
    797         bool               mReconfigured;
    798 
    799         // Used by waitIfPaused, waitForNextRequest, and waitUntilPaused
    800         Mutex              mPauseLock;
    801         bool               mDoPause;
    802         Condition          mDoPauseSignal;
    803         bool               mPaused;
    804         Condition          mPausedSignal;
    805 
    806         sp<CaptureRequest> mPrevRequest;
    807         int32_t            mPrevTriggers;
    808 
    809         uint32_t           mFrameNumber;
    810 
    811         mutable Mutex      mLatestRequestMutex;
    812         Condition          mLatestRequestSignal;
    813         // android.request.id for latest process_capture_request
    814         int32_t            mLatestRequestId;
    815         CameraMetadata     mLatestRequest;
    816 
    817         typedef KeyedVector<uint32_t/*tag*/, RequestTrigger> TriggerMap;
    818         Mutex              mTriggerMutex;
    819         TriggerMap         mTriggerMap;
    820         TriggerMap         mTriggerRemovedMap;
    821         TriggerMap         mTriggerReplacedMap;
    822         uint32_t           mCurrentAfTriggerId;
    823         uint32_t           mCurrentPreCaptureTriggerId;
    824 
    825         int64_t            mRepeatingLastFrameNumber;
    826 
    827         // Flag indicating if we should prepare video stream for video requests.
    828         bool               mPrepareVideoStream;
    829 
    830         static const int32_t kRequestLatencyBinSize = 40; // in ms
    831         CameraLatencyHistogram mRequestLatency;
    832     };
    833     sp<RequestThread> mRequestThread;
    834 
    835     /**
    836      * In-flight queue for tracking completion of capture requests.
    837      */
    838 
    839     struct InFlightRequest {
    840         // Set by notify() SHUTTER call.
    841         nsecs_t shutterTimestamp;
    842         // Set by process_capture_result().
    843         nsecs_t sensorTimestamp;
    844         int     requestStatus;
    845         // Set by process_capture_result call with valid metadata
    846         bool    haveResultMetadata;
    847         // Decremented by calls to process_capture_result with valid output
    848         // and input buffers
    849         int     numBuffersLeft;
    850         CaptureResultExtras resultExtras;
    851         // If this request has any input buffer
    852         bool hasInputBuffer;
    853 
    854         // The last metadata that framework receives from HAL and
    855         // not yet send out because the shutter event hasn't arrived.
    856         // It's added by process_capture_result and sent when framework
    857         // receives the shutter event.
    858         CameraMetadata pendingMetadata;
    859 
    860         // The metadata of the partial results that framework receives from HAL so far
    861         // and has sent out.
    862         CameraMetadata collectedPartialResult;
    863 
    864         // Buffers are added by process_capture_result when output buffers
    865         // return from HAL but framework has not yet received the shutter
    866         // event. They will be returned to the streams when framework receives
    867         // the shutter event.
    868         Vector<camera3_stream_buffer_t> pendingOutputBuffers;
    869 
    870         // Whether this inflight request's shutter and result callback are to be
    871         // called. The policy is that if the request is the last one in the constrained
    872         // high speed recording request list, this flag will be true. If the request list
    873         // is not for constrained high speed recording, this flag will also be true.
    874         bool hasCallback;
    875 
    876         // Default constructor needed by KeyedVector
    877         InFlightRequest() :
    878                 shutterTimestamp(0),
    879                 sensorTimestamp(0),
    880                 requestStatus(OK),
    881                 haveResultMetadata(false),
    882                 numBuffersLeft(0),
    883                 hasInputBuffer(false),
    884                 hasCallback(true) {
    885         }
    886 
    887         InFlightRequest(int numBuffers, CaptureResultExtras extras, bool hasInput,
    888                 bool hasAppCallback) :
    889                 shutterTimestamp(0),
    890                 sensorTimestamp(0),
    891                 requestStatus(OK),
    892                 haveResultMetadata(false),
    893                 numBuffersLeft(numBuffers),
    894                 resultExtras(extras),
    895                 hasInputBuffer(hasInput),
    896                 hasCallback(hasAppCallback) {
    897         }
    898     };
    899 
    900     // Map from frame number to the in-flight request state
    901     typedef KeyedVector<uint32_t, InFlightRequest> InFlightMap;
    902 
    903     Mutex                  mInFlightLock; // Protects mInFlightMap
    904     InFlightMap            mInFlightMap;
    905     int                    mInFlightStatusId;
    906 
    907     status_t registerInFlight(uint32_t frameNumber,
    908             int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
    909             bool callback);
    910 
    911     /**
    912      * Tracking for idle detection
    913      */
    914     sp<camera3::StatusTracker> mStatusTracker;
    915 
    916     /**
    917      * Graphic buffer manager for output streams. Each device has a buffer manager, which is used
    918      * by the output streams to get and return buffers if these streams are registered to this
    919      * buffer manager.
    920      */
    921     sp<camera3::Camera3BufferManager> mBufferManager;
    922 
    923     /**
    924      * Thread for preparing streams
    925      */
    926     class PreparerThread : private Thread, public virtual RefBase {
    927       public:
    928         PreparerThread();
    929         ~PreparerThread();
    930 
    931         void setNotificationListener(wp<NotificationListener> listener);
    932 
    933         /**
    934          * Queue up a stream to be prepared. Streams are processed by a background thread in FIFO
    935          * order.  Pre-allocate up to maxCount buffers for the stream, or the maximum number needed
    936          * for the pipeline if maxCount is ALLOCATE_PIPELINE_MAX.
    937          */
    938         status_t prepare(int maxCount, sp<camera3::Camera3StreamInterface>& stream);
    939 
    940         /**
    941          * Cancel all current and pending stream preparation
    942          */
    943         status_t clear();
    944 
    945       private:
    946         Mutex mLock;
    947 
    948         virtual bool threadLoop();
    949 
    950         // Guarded by mLock
    951 
    952         wp<NotificationListener> mListener;
    953         List<sp<camera3::Camera3StreamInterface> > mPendingStreams;
    954         bool mActive;
    955         bool mCancelNow;
    956 
    957         // Only accessed by threadLoop and the destructor
    958 
    959         sp<camera3::Camera3StreamInterface> mCurrentStream;
    960     };
    961     sp<PreparerThread> mPreparerThread;
    962 
    963     /**
    964      * Output result queue and current HAL device 3A state
    965      */
    966 
    967     // Lock for output side of device
    968     Mutex                  mOutputLock;
    969 
    970     /**** Scope for mOutputLock ****/
    971     // the minimal frame number of the next non-reprocess result
    972     uint32_t               mNextResultFrameNumber;
    973     // the minimal frame number of the next reprocess result
    974     uint32_t               mNextReprocessResultFrameNumber;
    975     // the minimal frame number of the next non-reprocess shutter
    976     uint32_t               mNextShutterFrameNumber;
    977     // the minimal frame number of the next reprocess shutter
    978     uint32_t               mNextReprocessShutterFrameNumber;
    979     List<CaptureResult>   mResultQueue;
    980     Condition              mResultSignal;
    981     wp<NotificationListener>  mListener;
    982 
    983     /**** End scope for mOutputLock ****/
    984 
    985     /**
    986      * Callback functions from HAL device
    987      */
    988     void processCaptureResult(const camera3_capture_result *result);
    989 
    990     void notify(const camera3_notify_msg *msg);
    991 
    992     // Specific notify handlers
    993     void notifyError(const camera3_error_msg_t &msg,
    994             sp<NotificationListener> listener);
    995     void notifyShutter(const camera3_shutter_msg_t &msg,
    996             sp<NotificationListener> listener);
    997 
    998     // helper function to return the output buffers to the streams.
    999     void returnOutputBuffers(const camera3_stream_buffer_t *outputBuffers,
   1000             size_t numBuffers, nsecs_t timestamp);
   1001 
   1002     // Send a partial capture result.
   1003     void sendPartialCaptureResult(const camera_metadata_t * partialResult,
   1004             const CaptureResultExtras &resultExtras, uint32_t frameNumber);
   1005 
   1006     // Send a total capture result given the pending metadata and result extras,
   1007     // partial results, and the frame number to the result queue.
   1008     void sendCaptureResult(CameraMetadata &pendingMetadata,
   1009             CaptureResultExtras &resultExtras,
   1010             CameraMetadata &collectedPartialResult, uint32_t frameNumber,
   1011             bool reprocess);
   1012 
   1013     // Insert the result to the result queue after updating frame number and overriding AE
   1014     // trigger cancel.
   1015     // mOutputLock must be held when calling this function.
   1016     void insertResultLocked(CaptureResult *result, uint32_t frameNumber);
   1017 
   1018     /**** Scope for mInFlightLock ****/
   1019 
   1020     // Remove the in-flight map entry of the given index from mInFlightMap.
   1021     // It must only be called with mInFlightLock held.
   1022     void removeInFlightMapEntryLocked(int idx);
   1023     // Remove the in-flight request of the given index from mInFlightMap
   1024     // if it's no longer needed. It must only be called with mInFlightLock held.
   1025     void removeInFlightRequestIfReadyLocked(int idx);
   1026 
   1027     /**** End scope for mInFlightLock ****/
   1028 
   1029     // Debug tracker for metadata tag value changes
   1030     // - Enabled with the -m <taglist> option to dumpsys, such as
   1031     //   dumpsys -m android.control.aeState,android.control.aeMode
   1032     // - Disabled with -m off
   1033     // - dumpsys -m 3a is a shortcut for ae/af/awbMode, State, and Triggers
   1034     TagMonitor mTagMonitor;
   1035 
   1036     void monitorMetadata(TagMonitor::eventSource source, int64_t frameNumber,
   1037             nsecs_t timestamp, const CameraMetadata& metadata);
   1038 
   1039     metadata_vendor_id_t mVendorTagId;
   1040 
   1041     /**
   1042      * Static callback forwarding methods from HAL to instance
   1043      */
   1044     static callbacks_process_capture_result_t sProcessCaptureResult;
   1045 
   1046     static callbacks_notify_t sNotify;
   1047 
   1048 }; // class Camera3Device
   1049 
   1050 }; // namespace android
   1051 
   1052 #endif
   1053