1 /*
2  * Copyright (C) 2017 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_QEMU_CAMERA3_H
18 #define HW_EMULATOR_CAMERA_EMULATED_QEMU_CAMERA3_H
19 
20 /*
21  * Contains declaration of a class EmulatedQemuCamera3 that encapsulates
22  * functionality of a video capture device that implements version 3 of the
23  * camera device interface.
24  */
25 
26 #include "EmulatedCamera3.h"
27 #include "fake-pipeline2/JpegCompressor.h"
28 #include "qemu-pipeline3/QemuSensor.h"
29 
30 #include <CameraMetadata.h>
31 #include <utils/SortedVector.h>
32 #include <utils/List.h>
33 #include <utils/Mutex.h>
34 
35 using ::android::hardware::camera::common::V1_0::helper::CameraMetadata;
36 
37 namespace android {
38 
39 /*
40  * Encapsulates functionality for a v3 HAL camera which interfaces with a video
41  * capture device on the host computer.
42  *
43  * NOTE: Currently, resolutions larger than 640x480 are susceptible to
44  * performance problems.
45  *
46  * TODO: Optimize to allow regular usage of higher resolutions.
47  */
48 class EmulatedQemuCamera3 : public EmulatedCamera3,
49         private QemuSensor::QemuSensorListener {
50 public:
51     EmulatedQemuCamera3(int cameraId, struct hw_module_t* module,
52                         GraphicBufferMapper* gbm);
53     virtual ~EmulatedQemuCamera3();
54 
55     /*
56      * Args:
57      *     deviceName: File path where the capture device can be found (e.g.,
58      *                 "/dev/video0").
59      *     frameDims: Space-delimited resolutions with each dimension delimited
60      *                by a comma (e.g., "640,480 320,240").
61      *     facingDir: Contains either "front" or "back".
62      */
63     virtual status_t Initialize(const char *deviceName,
64                                 const char *frameDims,
65                                 const char *facingDir);
66 
67     /**************************************************************************
68      * Camera Module API and Generic Hardware Device API Implementation
69      *************************************************************************/
70     virtual status_t connectCamera(hw_device_t **device);
71     virtual status_t closeCamera();
72     virtual status_t getCameraInfo(struct camera_info *info);
73 
74 protected:
75     /**************************************************************************
76      * EmulatedCamera3 Abstract API Implementation
77      *************************************************************************/
78     virtual status_t configureStreams(camera3_stream_configuration *streamList);
79     virtual status_t registerStreamBuffers(
80             const camera3_stream_buffer_set *bufferSet);
81     virtual const camera_metadata_t* constructDefaultRequestSettings(int type);
82     virtual status_t processCaptureRequest(camera3_capture_request *request);
83     virtual status_t flush();
84 
85 private:
86     /*
87      * Get the requested capability set (from boot properties) for this camera
88      * and populate "mCapabilities".
89      */
90     status_t getCameraCapabilities();
91 
92     /*
93      * Extracts supported resolutions into "mResolutions".
94      *
95      * Args:
96      *     frameDims: A string of space-delimited resolutions with each
97      *                dimension delimited by a comma (e.g., "640,480 320,240").
98      */
99     void parseResolutions(const char *frameDims);
100 
101     bool hasCapability(AvailableCapabilities cap);
102 
103     /*
104      * Build the static info metadata buffer for this device.
105      */
106     status_t constructStaticInfo();
107 
108     status_t process3A(CameraMetadata &settings);
109 
110     status_t doFakeAE(CameraMetadata &settings);
111     status_t doFakeAF(CameraMetadata &settings);
112     status_t doFakeAWB(CameraMetadata &settings);
113     void     update3A(CameraMetadata &settings);
114 
115     /*
116      * Signal from readout thread that it doesn't have anything to do.
117      */
118     void signalReadoutIdle();
119 
120     /*
121      * Handle interrupt events from the sensor.
122      */
123     void onQemuSensorEvent(uint32_t frameNumber, Event e, nsecs_t timestamp);
124     using EmulatedCamera3::Initialize;
125 
126 private:
127     /**************************************************************************
128      * Static Configuration Information
129      *************************************************************************/
130     static const uint32_t kMaxRawStreamCount = 0;
131     static const uint32_t kMaxProcessedStreamCount = 3;
132     static const uint32_t kMaxJpegStreamCount = 1;
133     static const uint32_t kMaxReprocessStreamCount = 0;
134     static const uint32_t kMaxBufferCount = 3;
135     // We need a positive stream ID to distinguish external buffers from
136     // sensor-generated buffers which use a nonpositive ID. Otherwise, HAL3 has
137     // no concept of a stream id.
138     static const uint32_t kGenericStreamId = 1;
139     static const int32_t kAvailableFormats[];
140     static const int64_t kSyncWaitTimeout = 10000000;  // 10 ms
141     static const int32_t kMaxSyncTimeoutCount = 1000;  // 1000 kSyncWaitTimeouts
142     static const uint32_t kFenceTimeoutMs = 2000;  // 2 s
143     static const nsecs_t kJpegTimeoutNs = 5000000000l;  // 5 s
144 
145     /**************************************************************************
146      * Data Members
147      *************************************************************************/
148 
149     // HAL interface serialization lock.
150     Mutex mLock;
151 
152     const char *mDeviceName;
153     bool mFacingBack;
154     uint32_t mSensorWidth;
155     uint32_t mSensorHeight;
156     std::vector<std::pair<int32_t,int32_t>> mResolutions;
157 
158 
159     SortedVector<AvailableCapabilities> mCapabilities;
160 
161     /*
162      * Cache for default templates. Once one is requested, the pointer must be
163      * valid at least until close() is called on the device.
164      */
165     camera_metadata_t *mDefaultTemplates[CAMERA3_TEMPLATE_COUNT];
166 
167     // Private stream information, stored in camera3_stream_t->priv.
168     struct PrivateStreamInfo {
169         bool alive;
170     };
171 
172     // Shortcut to the input stream.
173     camera3_stream_t* mInputStream;
174     GraphicBufferMapper* mGBM;
175 
176     typedef List<camera3_stream_t*> StreamList;
177     typedef List<camera3_stream_t*>::iterator StreamIterator;
178     typedef Vector<camera3_stream_buffer> HalBufferVector;
179 
180     // All streams, including input stream.
181     StreamList mStreams;
182 
183     // Cached settings from latest submitted request.
184     CameraMetadata mPrevSettings;
185 
186     // Fake Hardware Interfaces
187     sp<QemuSensor> mSensor;
188     sp<JpegCompressor> mJpegCompressor;
189     friend class JpegCompressor;
190 
191     /*
192      * Processing thread for sending out results.
193      */
194     class ReadoutThread : public Thread, private JpegCompressor::JpegListener {
195       public:
196         ReadoutThread(EmulatedQemuCamera3 *parent);
197         ~ReadoutThread();
198 
199         struct Request {
200             uint32_t frameNumber;
201             CameraMetadata settings;
202             HalBufferVector *buffers;
203             Buffers *sensorBuffers;
204         };
205 
206         /*
207          * Interface to Parent Class
208          */
209 
210         /*
211          * Place request in the in-flight queue to wait for sensor capture.
212          */
213         void queueCaptureRequest(const Request &r);
214 
215         /*
216          * Test if the readout thread is idle (no in-flight requests, not
217          * currently reading out anything).
218          */
219         bool isIdle();
220 
221         /*
222          * Wait until isIdle is true.
223          */
224         status_t waitForReadout();
225 
226       private:
227         static const nsecs_t kWaitPerLoop = 10000000L;  // 10 ms
228         static const nsecs_t kMaxWaitLoops = 1000;
229         static const size_t kMaxQueueSize = 4;
230 
231         EmulatedQemuCamera3 *mParent;
232         Mutex mLock;
233 
234         List<Request> mInFlightQueue;
235         Condition mInFlightSignal;
236         bool mThreadActive;
237 
238         virtual bool threadLoop();
239 
240         // Only accessed by threadLoop.
241         Request mCurrentRequest;
242 
243         Mutex mJpegLock;
244         bool mJpegWaiting;
245         camera3_stream_buffer mJpegHalBuffer;
246         uint32_t mJpegFrameNumber;
247 
248         /*
249          * Jpeg Completion Callbacks
250          */
251         virtual void onJpegDone(const StreamBuffer &jpegBuffer, bool success);
252         virtual void onJpegInputDone(const StreamBuffer &inputBuffer);
253     };
254 
255     sp<ReadoutThread> mReadoutThread;
256 
257     /** Fake 3A constants */
258 
259     static const nsecs_t kNormalExposureTime;
260     static const nsecs_t kFacePriorityExposureTime;
261     static const int     kNormalSensitivity;
262     static const int     kFacePrioritySensitivity;
263     // Rate of converging AE to new target value, as fraction of difference between
264     // current and target value.
265     static const float   kExposureTrackRate;
266     // Minimum duration for precapture state. May be longer if slow to converge
267     // to target exposure
268     static const int     kPrecaptureMinFrames;
269     // How often to restart AE 'scanning'
270     static const int     kStableAeMaxFrames;
271     // Maximum stop below 'normal' exposure time that we'll wander to while
272     // pretending to converge AE. In powers of 2. (-2 == 1/4 as bright)
273     static const float   kExposureWanderMin;
274     // Maximum stop above 'normal' exposure time that we'll wander to while
275     // pretending to converge AE. In powers of 2. (2 == 4x as bright)
276     static const float   kExposureWanderMax;
277 
278     /** Fake 3A state */
279 
280     uint8_t mControlMode;
281     bool    mFacePriority;
282     uint8_t mAeState;
283     uint8_t mAfState;
284     uint8_t mAwbState;
285     uint8_t mAeMode;
286     uint8_t mAfMode;
287     uint8_t mAwbMode;
288 
289     int     mAeCounter;
290     nsecs_t mAeCurrentExposureTime;
291     nsecs_t mAeTargetExposureTime;
292     int     mAeCurrentSensitivity;
293 };
294 
295 }; // end of namespace android
296 
297 #endif // HW_EMULATOR_CAMERA_EMULATED_QEMU_CAMERA3_H
298