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 HW_EMULATOR_CAMERA_EMULATED_FAKE_CAMERA3_H
18 #define HW_EMULATOR_CAMERA_EMULATED_FAKE_CAMERA3_H
19 
20 /**
21  * Contains declaration of a class EmulatedCamera that encapsulates
22  * functionality of a fake camera that implements version 3 of the camera device
23  * interace.
24  */
25 
26 #include "EmulatedCamera3.h"
27 #include "fake-pipeline2/Base.h"
28 #include "fake-pipeline2/Sensor.h"
29 #include "fake-pipeline2/JpegCompressor.h"
30 #include <camera/CameraMetadata.h>
31 #include <utils/List.h>
32 #include <utils/Mutex.h>
33 
34 namespace android {
35 
36 /**
37  * Encapsulates functionality for a v3 HAL camera which produces synthetic data.
38  *
39  * Note that EmulatedCameraFactory instantiates an object of this class just
40  * once, when EmulatedCameraFactory instance gets constructed. Connection to /
41  * disconnection from the actual camera device is handled by calls to
42  * connectDevice(), and closeCamera() methods of this class that are invoked in
43  * response to hw_module_methods_t::open, and camera_device::close callbacks.
44  */
45 class EmulatedFakeCamera3 : public EmulatedCamera3,
46         private Sensor::SensorListener {
47 public:
48 
49     EmulatedFakeCamera3(int cameraId, bool facingBack,
50             struct hw_module_t* module);
51 
52     virtual ~EmulatedFakeCamera3();
53 
54     /****************************************************************************
55      * EmulatedCamera3 virtual overrides
56      ***************************************************************************/
57 
58 public:
59 
60     virtual status_t Initialize();
61 
62     /****************************************************************************
63      * Camera module API and generic hardware device API implementation
64      ***************************************************************************/
65 
66 public:
67     virtual status_t connectCamera(hw_device_t** device);
68 
69     virtual status_t closeCamera();
70 
71     virtual status_t getCameraInfo(struct camera_info *info);
72 
73     /****************************************************************************
74      * EmulatedCamera3 abstract API implementation
75      ***************************************************************************/
76 
77 protected:
78 
79     virtual status_t configureStreams(
80         camera3_stream_configuration *streamList);
81 
82     virtual status_t registerStreamBuffers(
83         const camera3_stream_buffer_set *bufferSet) ;
84 
85     virtual const camera_metadata_t* constructDefaultRequestSettings(
86         int type);
87 
88     virtual status_t processCaptureRequest(camera3_capture_request *request);
89 
90     /** Debug methods */
91 
92     virtual void dump(int fd);
93 
94     /** Tag query methods */
95     virtual const char *getVendorSectionName(uint32_t tag);
96 
97     virtual const char *getVendorTagName(uint32_t tag);
98 
99     virtual int getVendorTagType(uint32_t tag);
100 
101 private:
102 
103     /**
104      * Build the static info metadata buffer for this device
105      */
106     status_t constructStaticInfo();
107 
108     /**
109      * Run the fake 3A algorithms as needed. May override/modify settings
110      * values.
111      */
112     status_t process3A(CameraMetadata &settings);
113 
114     status_t doFakeAE(CameraMetadata &settings);
115     status_t doFakeAF(CameraMetadata &settings);
116     status_t doFakeAWB(CameraMetadata &settings);
117     void     update3A(CameraMetadata &settings);
118 
119     /** Signal from readout thread that it doesn't have anything to do */
120     void     signalReadoutIdle();
121 
122     /** Handle interrupt events from the sensor */
123     void     onSensorEvent(uint32_t frameNumber, Event e, nsecs_t timestamp);
124 
125     /****************************************************************************
126      * Static configuration information
127      ***************************************************************************/
128 private:
129     static const uint32_t kMaxRawStreamCount = 1;
130     static const uint32_t kMaxProcessedStreamCount = 3;
131     static const uint32_t kMaxJpegStreamCount = 1;
132     static const uint32_t kMaxReprocessStreamCount = 2;
133     static const uint32_t kMaxBufferCount = 4;
134     // We need a positive stream ID to distinguish external buffers from
135     // sensor-generated buffers which use a nonpositive ID. Otherwise, HAL3 has
136     // no concept of a stream id.
137     static const uint32_t kGenericStreamId = 1;
138     static const int32_t  kAvailableFormats[];
139     static const uint32_t kAvailableRawSizes[];
140     static const uint64_t kAvailableRawMinDurations[];
141     static const uint32_t kAvailableProcessedSizesBack[];
142     static const uint32_t kAvailableProcessedSizesFront[];
143     static const uint64_t kAvailableProcessedMinDurations[];
144     static const uint32_t kAvailableJpegSizesBack[];
145     static const uint32_t kAvailableJpegSizesFront[];
146     static const uint64_t kAvailableJpegMinDurations[];
147 
148     static const int64_t  kSyncWaitTimeout     = 10000000; // 10 ms
149     static const int32_t  kMaxSyncTimeoutCount = 1000; // 1000 kSyncWaitTimeouts
150     static const uint32_t kFenceTimeoutMs      = 2000; // 2 s
151 
152     /****************************************************************************
153      * Data members.
154      ***************************************************************************/
155 
156     /* HAL interface serialization lock. */
157     Mutex              mLock;
158 
159     /* Facing back (true) or front (false) switch. */
160     bool               mFacingBack;
161 
162     /* Full mode (true) or limited mode (false) switch */
163     bool               mFullMode;
164 
165     /**
166      * Cache for default templates. Once one is requested, the pointer must be
167      * valid at least until close() is called on the device
168      */
169     camera_metadata_t *mDefaultTemplates[CAMERA3_TEMPLATE_COUNT];
170 
171     /**
172      * Private stream information, stored in camera3_stream_t->priv.
173      */
174     struct PrivateStreamInfo {
175         bool alive;
176         bool registered;
177     };
178 
179     // Shortcut to the input stream
180     camera3_stream_t*  mInputStream;
181 
182     typedef List<camera3_stream_t*>           StreamList;
183     typedef List<camera3_stream_t*>::iterator StreamIterator;
184     typedef Vector<camera3_stream_buffer>     HalBufferVector;
185 
186     // All streams, including input stream
187     StreamList         mStreams;
188 
189     // Cached settings from latest submitted request
190     CameraMetadata     mPrevSettings;
191 
192     /** Fake hardware interfaces */
193     sp<Sensor>         mSensor;
194     sp<JpegCompressor> mJpegCompressor;
195     friend class       JpegCompressor;
196 
197     /** Processing thread for sending out results */
198 
199     class ReadoutThread : public Thread, private JpegCompressor::JpegListener {
200       public:
201         ReadoutThread(EmulatedFakeCamera3 *parent);
202         ~ReadoutThread();
203 
204         struct Request {
205             uint32_t         frameNumber;
206             CameraMetadata   settings;
207             HalBufferVector *buffers;
208             Buffers         *sensorBuffers;
209         };
210 
211         /**
212          * Interface to parent class
213          */
214 
215         // Place request in the in-flight queue to wait for sensor capture
216         void     queueCaptureRequest(const Request &r);
217 
218         // Test if the readout thread is idle (no in-flight requests, not
219         // currently reading out anything
220         bool     isIdle();
221 
222         // Wait until isIdle is true
223         status_t waitForReadout();
224 
225       private:
226         static const nsecs_t kWaitPerLoop  = 10000000L; // 10 ms
227         static const nsecs_t kMaxWaitLoops = 1000;
228         static const size_t  kMaxQueueSize = 2;
229 
230         EmulatedFakeCamera3 *mParent;
231         Mutex mLock;
232 
233         List<Request> mInFlightQueue;
234         Condition     mInFlightSignal;
235         bool          mThreadActive;
236 
237         virtual bool threadLoop();
238 
239         // Only accessed by threadLoop
240 
241         Request mCurrentRequest;
242 
243         // Jpeg completion callbacks
244 
245         Mutex                 mJpegLock;
246         bool                  mJpegWaiting;
247         camera3_stream_buffer mJpegHalBuffer;
248         uint32_t              mJpegFrameNumber;
249         virtual void onJpegDone(const StreamBuffer &jpegBuffer, bool success);
250         virtual void onJpegInputDone(const StreamBuffer &inputBuffer);
251     };
252 
253     sp<ReadoutThread> mReadoutThread;
254 
255     /** Fake 3A constants */
256 
257     static const nsecs_t kNormalExposureTime;
258     static const nsecs_t kFacePriorityExposureTime;
259     static const int     kNormalSensitivity;
260     static const int     kFacePrioritySensitivity;
261     // Rate of converging AE to new target value, as fraction of difference between
262     // current and target value.
263     static const float   kExposureTrackRate;
264     // Minimum duration for precapture state. May be longer if slow to converge
265     // to target exposure
266     static const int     kPrecaptureMinFrames;
267     // How often to restart AE 'scanning'
268     static const int     kStableAeMaxFrames;
269     // Maximum stop below 'normal' exposure time that we'll wander to while
270     // pretending to converge AE. In powers of 2. (-2 == 1/4 as bright)
271     static const float   kExposureWanderMin;
272     // Maximum stop above 'normal' exposure time that we'll wander to while
273     // pretending to converge AE. In powers of 2. (2 == 4x as bright)
274     static const float   kExposureWanderMax;
275 
276     /** Fake 3A state */
277 
278     uint8_t mControlMode;
279     bool    mFacePriority;
280     uint8_t mAeState;
281     uint8_t mAfState;
282     uint8_t mAwbState;
283     uint8_t mAeMode;
284     uint8_t mAfMode;
285     uint8_t mAwbMode;
286     int     mAfTriggerId;
287     int     mAeTriggerId;
288 
289     int     mAeCounter;
290     nsecs_t mAeCurrentExposureTime;
291     nsecs_t mAeTargetExposureTime;
292     int     mAeCurrentSensitivity;
293 
294 };
295 
296 } // namespace android
297 
298 #endif // HW_EMULATOR_CAMERA_EMULATED_CAMERA3_H
299