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