Home | History | Annotate | Download | only in camera
      1 /*
      2  * Copyright (C) 2012 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 HW_EMULATOR_CAMERA_EMULATED_FAKE_CAMERA2_H
     18 #define HW_EMULATOR_CAMERA_EMULATED_FAKE_CAMERA2_H
     19 
     20 /*
     21  * Contains declaration of a class EmulatedFakeCamera2 that encapsulates
     22  * functionality of a fake camera that implements version 2 of the camera device
     23  * interface.
     24  */
     25 
     26 #include "EmulatedCamera2.h"
     27 #include "fake-pipeline2/Base.h"
     28 #include "fake-pipeline2/Sensor.h"
     29 #include "fake-pipeline2/JpegCompressor.h"
     30 #include <utils/Condition.h>
     31 #include <utils/KeyedVector.h>
     32 #include <utils/String8.h>
     33 #include <utils/String16.h>
     34 
     35 namespace android {
     36 
     37 /* Encapsulates functionality of an advanced fake camera.  This camera contains
     38  * a simple simulation of a scene, sensor, and image processing pipeline.
     39  */
     40 class EmulatedFakeCamera2 : public EmulatedCamera2 {
     41 public:
     42     /* Constructs EmulatedFakeCamera instance. */
     43     EmulatedFakeCamera2(int cameraId, bool facingBack, struct hw_module_t* module);
     44 
     45     /* Destructs EmulatedFakeCamera instance. */
     46     ~EmulatedFakeCamera2();
     47 
     48     /****************************************************************************
     49      * EmulatedCamera2 virtual overrides.
     50      ***************************************************************************/
     51 
     52 public:
     53     /* Initializes EmulatedFakeCamera2 instance. */
     54     status_t Initialize();
     55 
     56     /****************************************************************************
     57      * Camera Module API and generic hardware device API implementation
     58      ***************************************************************************/
     59 public:
     60 
     61     virtual status_t connectCamera(hw_device_t** device);
     62 
     63     virtual status_t plugCamera();
     64     virtual status_t unplugCamera();
     65     virtual camera_device_status_t getHotplugStatus();
     66 
     67     virtual status_t closeCamera();
     68 
     69     virtual status_t getCameraInfo(struct camera_info *info);
     70 
     71     /****************************************************************************
     72      * EmulatedCamera2 abstract API implementation.
     73      ***************************************************************************/
     74 protected:
     75     /** Request input queue */
     76 
     77     virtual int requestQueueNotify();
     78 
     79     /** Count of requests in flight */
     80     virtual int getInProgressCount();
     81 
     82     /** Cancel all captures in flight */
     83     //virtual int flushCapturesInProgress();
     84 
     85     /** Construct default request */
     86     virtual int constructDefaultRequest(
     87             int request_template,
     88             camera_metadata_t **request);
     89 
     90     virtual int allocateStream(
     91             uint32_t width,
     92             uint32_t height,
     93             int format,
     94             const camera2_stream_ops_t *stream_ops,
     95             uint32_t *stream_id,
     96             uint32_t *format_actual,
     97             uint32_t *usage,
     98             uint32_t *max_buffers);
     99 
    100     virtual int registerStreamBuffers(
    101             uint32_t stream_id,
    102             int num_buffers,
    103             buffer_handle_t *buffers);
    104 
    105     virtual int releaseStream(uint32_t stream_id);
    106 
    107     // virtual int allocateReprocessStream(
    108     //         uint32_t width,
    109     //         uint32_t height,
    110     //         uint32_t format,
    111     //         const camera2_stream_ops_t *stream_ops,
    112     //         uint32_t *stream_id,
    113     //         uint32_t *format_actual,
    114     //         uint32_t *usage,
    115     //         uint32_t *max_buffers);
    116 
    117     virtual int allocateReprocessStreamFromStream(
    118             uint32_t output_stream_id,
    119             const camera2_stream_in_ops_t *stream_ops,
    120             uint32_t *stream_id);
    121 
    122     virtual int releaseReprocessStream(uint32_t stream_id);
    123 
    124     virtual int triggerAction(uint32_t trigger_id,
    125             int32_t ext1,
    126             int32_t ext2);
    127 
    128     /** Custom tag definitions */
    129     virtual const char* getVendorSectionName(uint32_t tag);
    130     virtual const char* getVendorTagName(uint32_t tag);
    131     virtual int         getVendorTagType(uint32_t tag);
    132 
    133     /** Debug methods */
    134 
    135     virtual int dump(int fd);
    136 
    137 public:
    138     /****************************************************************************
    139      * Utility methods called by configure/readout threads and pipeline
    140      ***************************************************************************/
    141 
    142     // Get information about a given stream. Will lock mMutex
    143     const Stream &getStreamInfo(uint32_t streamId);
    144     const ReprocessStream &getReprocessStreamInfo(uint32_t streamId);
    145 
    146     // Notifies rest of camera subsystem of serious error
    147     void signalError();
    148 
    149 private:
    150     /****************************************************************************
    151      * Utility methods
    152      ***************************************************************************/
    153     /** Construct static camera metadata, two-pass */
    154     status_t constructStaticInfo(
    155             camera_metadata_t **info,
    156             bool sizeRequest) const;
    157 
    158     /** Two-pass implementation of constructDefaultRequest */
    159     status_t constructDefaultRequest(
    160             int request_template,
    161             camera_metadata_t **request,
    162             bool sizeRequest) const;
    163     /** Helper function for constructDefaultRequest */
    164     static status_t addOrSize( camera_metadata_t *request,
    165             bool sizeRequest,
    166             size_t *entryCount,
    167             size_t *dataCount,
    168             uint32_t tag,
    169             const void *entry_data,
    170             size_t entry_count);
    171 
    172     /** Determine if the stream id is listed in any currently-in-flight
    173      * requests. Assumes mMutex is locked */
    174     bool isStreamInUse(uint32_t streamId);
    175 
    176     /** Determine if the reprocess stream id is listed in any
    177      * currently-in-flight requests. Assumes mMutex is locked */
    178     bool isReprocessStreamInUse(uint32_t streamId);
    179 
    180     /****************************************************************************
    181      * Pipeline controller threads
    182      ***************************************************************************/
    183 
    184     class ConfigureThread: public Thread {
    185       public:
    186         ConfigureThread(EmulatedFakeCamera2 *parent);
    187         ~ConfigureThread();
    188 
    189         status_t waitUntilRunning();
    190         status_t newRequestAvailable();
    191         status_t readyToRun();
    192 
    193         bool isStreamInUse(uint32_t id);
    194         int getInProgressCount();
    195       private:
    196         EmulatedFakeCamera2 *mParent;
    197         static const nsecs_t kWaitPerLoop = 10000000L; // 10 ms
    198 
    199         bool mRunning;
    200         bool threadLoop();
    201 
    202         bool setupCapture();
    203         bool setupReprocess();
    204 
    205         bool configureNextCapture();
    206         bool configureNextReprocess();
    207 
    208         bool getBuffers();
    209 
    210         Mutex mInputMutex; // Protects mActive, mRequestCount
    211         Condition mInputSignal;
    212         bool mActive; // Whether we're waiting for input requests or actively
    213                       // working on them
    214         size_t mRequestCount;
    215 
    216         camera_metadata_t *mRequest;
    217 
    218         Mutex mInternalsMutex; // Lock before accessing below members.
    219         bool    mWaitingForReadout;
    220         bool    mNextNeedsJpeg;
    221         bool    mNextIsCapture;
    222         int32_t mNextFrameNumber;
    223         int64_t mNextExposureTime;
    224         int64_t mNextFrameDuration;
    225         int32_t mNextSensitivity;
    226         Buffers *mNextBuffers;
    227     };
    228 
    229     class ReadoutThread: public Thread, private JpegCompressor::JpegListener {
    230       public:
    231         ReadoutThread(EmulatedFakeCamera2 *parent);
    232         ~ReadoutThread();
    233 
    234         status_t readyToRun();
    235 
    236         // Input
    237         status_t waitUntilRunning();
    238         bool waitForReady(nsecs_t timeout);
    239         void setNextOperation(bool isCapture,
    240                 camera_metadata_t *request,
    241                 Buffers *buffers);
    242         bool isStreamInUse(uint32_t id);
    243         int getInProgressCount();
    244       private:
    245         EmulatedFakeCamera2 *mParent;
    246 
    247         bool mRunning;
    248         bool threadLoop();
    249 
    250         bool readyForNextCapture();
    251         status_t collectStatisticsMetadata(camera_metadata_t *frame);
    252 
    253         // Inputs
    254         Mutex mInputMutex; // Protects mActive, mInFlightQueue, mRequestCount
    255         Condition mInputSignal;
    256         Condition mReadySignal;
    257 
    258         bool mActive;
    259 
    260         static const int kInFlightQueueSize = 4;
    261         struct InFlightQueue {
    262             bool isCapture;
    263             camera_metadata_t *request;
    264             Buffers *buffers;
    265         } *mInFlightQueue;
    266 
    267         size_t mInFlightHead;
    268         size_t mInFlightTail;
    269 
    270         size_t mRequestCount;
    271 
    272         // Internals
    273         Mutex mInternalsMutex;
    274 
    275         bool mIsCapture;
    276         camera_metadata_t *mRequest;
    277         Buffers *mBuffers;
    278 
    279         // Jpeg completion listeners
    280         void onJpegDone(const StreamBuffer &jpegBuffer, bool success);
    281         void onJpegInputDone(const StreamBuffer &inputBuffer);
    282         nsecs_t mJpegTimestamp;
    283     };
    284 
    285     // 3A management thread (auto-exposure, focus, white balance)
    286     class ControlThread: public Thread {
    287       public:
    288         ControlThread(EmulatedFakeCamera2 *parent);
    289         ~ControlThread();
    290 
    291         status_t readyToRun();
    292 
    293         status_t waitUntilRunning();
    294 
    295         // Interpret request's control parameters and override
    296         // capture settings as needed
    297         status_t processRequest(camera_metadata_t *request);
    298 
    299         status_t triggerAction(uint32_t msgType,
    300                 int32_t ext1, int32_t ext2);
    301       private:
    302         ControlThread(const ControlThread &t);
    303         ControlThread& operator=(const ControlThread &t);
    304 
    305         // Constants controlling fake 3A behavior
    306         static const nsecs_t kControlCycleDelay;
    307         static const nsecs_t kMinAfDuration;
    308         static const nsecs_t kMaxAfDuration;
    309         static const float kAfSuccessRate;
    310         static const float kContinuousAfStartRate;
    311 
    312         static const float kAeScanStartRate;
    313         static const nsecs_t kMinAeDuration;
    314         static const nsecs_t kMaxAeDuration;
    315         static const nsecs_t kMinPrecaptureAeDuration;
    316         static const nsecs_t kMaxPrecaptureAeDuration;
    317 
    318         static const nsecs_t kNormalExposureTime;
    319         static const nsecs_t kExposureJump;
    320         static const nsecs_t kMinExposureTime;
    321 
    322         EmulatedFakeCamera2 *mParent;
    323 
    324         bool mRunning;
    325         bool threadLoop();
    326 
    327         Mutex mInputMutex; // Protects input methods
    328         Condition mInputSignal;
    329 
    330         // Trigger notifications
    331         bool mStartAf;
    332         bool mCancelAf;
    333         bool mStartPrecapture;
    334 
    335         // Latest state for 3A request fields
    336         uint8_t mControlMode;
    337 
    338         uint8_t mEffectMode;
    339         uint8_t mSceneMode;
    340 
    341         uint8_t mAfMode;
    342         bool mAfModeChange;
    343 
    344         uint8_t mAwbMode;
    345         uint8_t mAeMode;
    346 
    347         // Latest trigger IDs
    348         int32_t mAfTriggerId;
    349         int32_t mPrecaptureTriggerId;
    350 
    351         // Current state for 3A algorithms
    352         uint8_t mAfState;
    353         uint8_t mAeState;
    354         uint8_t mAwbState;
    355         bool    mAeLock;
    356 
    357         // Current control parameters
    358         nsecs_t mExposureTime;
    359 
    360         // Private to threadLoop and its utility methods
    361 
    362         nsecs_t mAfScanDuration;
    363         nsecs_t mAeScanDuration;
    364         bool mLockAfterPassiveScan;
    365 
    366         // Utility methods for AF
    367         int processAfTrigger(uint8_t afMode, uint8_t afState);
    368         int maybeStartAfScan(uint8_t afMode, uint8_t afState);
    369         int updateAfScan(uint8_t afMode, uint8_t afState, nsecs_t *maxSleep);
    370         void updateAfState(uint8_t newState, int32_t triggerId);
    371 
    372         // Utility methods for precapture trigger
    373         int processPrecaptureTrigger(uint8_t aeMode, uint8_t aeState);
    374         int maybeStartAeScan(uint8_t aeMode, bool aeLock, uint8_t aeState);
    375         int updateAeScan(uint8_t aeMode, bool aeLock, uint8_t aeState,
    376                 nsecs_t *maxSleep);
    377         void updateAeState(uint8_t newState, int32_t triggerId);
    378     };
    379 
    380     /****************************************************************************
    381      * Static configuration information
    382      ***************************************************************************/
    383 private:
    384     static const uint32_t kMaxRawStreamCount = 1;
    385     static const uint32_t kMaxProcessedStreamCount = 3;
    386     static const uint32_t kMaxJpegStreamCount = 1;
    387     static const uint32_t kMaxReprocessStreamCount = 2;
    388     static const uint32_t kMaxBufferCount = 4;
    389     static const uint32_t kAvailableFormats[];
    390     static const uint32_t kAvailableRawSizes[];
    391     static const uint64_t kAvailableRawMinDurations[];
    392     static const uint32_t kAvailableProcessedSizesBack[];
    393     static const uint32_t kAvailableProcessedSizesFront[];
    394     static const uint64_t kAvailableProcessedMinDurations[];
    395     static const uint32_t kAvailableJpegSizesBack[];
    396     static const uint32_t kAvailableJpegSizesFront[];
    397     static const uint64_t kAvailableJpegMinDurations[];
    398 
    399     /****************************************************************************
    400      * Data members.
    401      ***************************************************************************/
    402 
    403 protected:
    404     /* Facing back (true) or front (false) switch. */
    405     bool mFacingBack;
    406 
    407 private:
    408     bool mIsConnected;
    409 
    410     /** Stream manipulation */
    411     uint32_t mNextStreamId;
    412     uint32_t mRawStreamCount;
    413     uint32_t mProcessedStreamCount;
    414     uint32_t mJpegStreamCount;
    415 
    416     uint32_t mNextReprocessStreamId;
    417     uint32_t mReprocessStreamCount;
    418 
    419     KeyedVector<uint32_t, Stream> mStreams;
    420     KeyedVector<uint32_t, ReprocessStream> mReprocessStreams;
    421 
    422     /** Simulated hardware interfaces */
    423     sp<Sensor> mSensor;
    424     sp<JpegCompressor> mJpegCompressor;
    425 
    426     /** Pipeline control threads */
    427     sp<ConfigureThread> mConfigureThread;
    428     sp<ReadoutThread>   mReadoutThread;
    429     sp<ControlThread>   mControlThread;
    430 };
    431 
    432 }; /* namespace android */
    433 
    434 #endif  /* HW_EMULATOR_CAMERA_EMULATED_FAKE_CAMERA2_H */
    435