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