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