summaryrefslogtreecommitdiff
path: root/v3/EmulatedFakeCamera3.h (plain)
blob: e984cc3ce8d0287d40bf3321e8621cdbc9a9461a
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#include <LoadXml.h>
34
35
36namespace android {
37
38/**
39 * Encapsulates functionality for a v3 HAL camera which produces synthetic data.
40 *
41 * Note that EmulatedCameraFactory instantiates an object of this class just
42 * once, when EmulatedCameraFactory instance gets constructed. Connection to /
43 * disconnection from the actual camera device is handled by calls to
44 * connectDevice(), and closeCamera() methods of this class that are invoked in
45 * response to hw_module_methods_t::open, and camera_device::close callbacks.
46 */
47struct jpegsize {
48 uint32_t width;
49 uint32_t height;
50};
51
52class EmulatedFakeCamera3 : public EmulatedCamera3,
53 private Sensor::SensorListener {
54public:
55
56 EmulatedFakeCamera3(int cameraId, struct hw_module_t* module);
57
58 virtual ~EmulatedFakeCamera3();
59
60 /****************************************************************************
61 * EmulatedCamera3 virtual overrides
62 ***************************************************************************/
63
64public:
65
66 virtual status_t Initialize();
67
68 /****************************************************************************
69 * Camera module API and generic hardware device API implementation
70 ***************************************************************************/
71
72public:
73 virtual status_t connectCamera(hw_device_t** device);
74
75 virtual status_t plugCamera();
76 virtual status_t unplugCamera();
77 virtual camera_device_status_t getHotplugStatus();
78
79 virtual status_t closeCamera();
80
81 virtual status_t getCameraInfo(struct camera_info *info);
82 virtual bool getCameraStatus();
83
84 /****************************************************************************
85 * EmulatedCamera3 abstract API implementation
86 ***************************************************************************/
87
88protected:
89
90 virtual status_t configureStreams(
91 camera3_stream_configuration *streamList);
92
93 virtual status_t registerStreamBuffers(
94 const camera3_stream_buffer_set *bufferSet) ;
95
96 virtual const camera_metadata_t* constructDefaultRequestSettings(
97 int type);
98
99 virtual status_t processCaptureRequest(camera3_capture_request *request);
100
101 /** Debug methods */
102
103 virtual void dump(int fd);
104 virtual int flush_all_requests();
105
106 /** Tag query methods */
107 virtual const char *getVendorSectionName(uint32_t tag);
108
109 virtual const char *getVendorTagName(uint32_t tag);
110
111 virtual int getVendorTagType(uint32_t tag);
112
113private:
114
115 /**
116 * Build the static info metadata buffer for this device
117 */
118 status_t constructStaticInfo();
119 int getAvailableChKeys(CameraMetadata *info, uint8_t level);
120 void updateCameraMetaData(CameraMetadata *info);
121 camera_metadata_ro_entry_t staticInfo(const CameraMetadata *info, uint32_t tag,
122 size_t minCount=0, size_t maxCount=0, bool required=true) const;
123 void getStreamConfigurationDurations(CameraMetadata *info);
124
125 void getStreamConfigurationp(CameraMetadata *info);
126 void getValidJpegSize(uint32_t picSizes[], uint32_t availablejpegsize[], int count);
127 status_t checkValidJpegSize(uint32_t width, uint32_t height);
128
129 //HW levels worst<->best, 0 = worst, 2 = best */
130 //compareHardwareLevel
131 //cts/tests/tests/hardware/src/android/hardware/camera2/cts/ExtendedCameraCharacteristicsTest.java
132 typedef enum hardware_level_e {
133 LEGACY,
134 LIMITED,
135 FULL,
136 OPT = 0xFF, //max of uint8_t
137 } hardware_level_t;
138
139 typedef enum available_capabilities_e {
140 NONE = 0,
141 BC = 0x01,
142 MANUAL_SENSOR = 0x02,
143 MANUAL_POST_PROCESSING = 0x04,
144 RAW = 0x08,
145 ZSL = 0x10,
146 }available_capabilities_t;
147
148 struct KeyInfo_s{
149 int32_t key;
150 uint8_t level;
151 uint8_t capmask;
152 }KeyInfo_t;
153
154 static const struct KeyInfo_s sKeyInfo[];
155 static const struct KeyInfo_s sKeyInfoReq[];
156 static const struct KeyInfo_s sKeyInfoResult[];
157 static const struct KeyInfo_s sKeyBackwardCompat[];
158 jpegsize maxJpegResolution;
159 jpegsize getMaxJpegResolution(uint32_t picSizes[],int count);
160 ssize_t getJpegBufferSize(int width, int height);
161 /**
162 * Run the fake 3A algorithms as needed. May override/modify settings
163 * values.
164 */
165 status_t process3A(CameraMetadata &settings);
166
167 status_t doFakeAE(CameraMetadata &settings);
168 status_t doFakeAF(CameraMetadata &settings);
169 status_t doFakeAWB(CameraMetadata &settings);
170 void update3A(CameraMetadata &settings);
171
172 /** Signal from readout thread that it doesn't have anything to do */
173 void signalReadoutIdle();
174
175 /** Handle interrupt events from the sensor */
176 void onSensorEvent(uint32_t frameNumber, Event e, nsecs_t timestamp);
177
178 /****************************************************************************
179 * Static configuration information
180 ***************************************************************************/
181private:
182 static const uint32_t kMaxRawStreamCount = 1;
183 static const uint32_t kMaxProcessedStreamCount = 3;
184 static const uint32_t kMaxJpegStreamCount = 1;
185 static const uint32_t kMaxReprocessStreamCount = 2;
186 static const uint32_t kMaxBufferCount = 4;
187 // We need a positive stream ID to distinguish external buffers from
188 // sensor-generated buffers which use a nonpositive ID. Otherwise, HAL3 has
189 // no concept of a stream id.
190 static const uint32_t kGenericStreamId = 1;
191 static const int32_t kAvailableFormats[];
192 static const uint32_t kAvailableRawSizes[];
193 static const uint64_t kAvailableRawMinDurations[];
194 static const uint32_t kAvailableProcessedSizesBack[];
195 static const uint32_t kAvailableProcessedSizesFront[];
196 static const uint64_t kAvailableProcessedMinDurations[];
197 static const uint32_t kAvailableJpegSizesBack[];
198 static const uint32_t kAvailableJpegSizesFront[];
199 static const uint64_t kAvailableJpegMinDurations[];
200
201 static const int64_t kSyncWaitTimeout = 10000000; // 10 ms
202 static const int32_t kMaxSyncTimeoutCount = 300; // 1000 kSyncWaitTimeouts
203 static const uint32_t kFenceTimeoutMs = 2000; // 2 s
204
205 /****************************************************************************
206 * Data members.
207 ***************************************************************************/
208
209 /* HAL interface serialization lock. */
210 Mutex mLock;
211
212 /* Facing back (true) or front (false) switch. */
213 bool mFacingBack;
214
215 /* Full mode (true) or limited mode (false) switch */
216 bool mFullMode;
217
218 LoadXml gLoadXml;
219
220 enum sensor_type_e mSensorType;
221
222 /**
223 * Cache for default templates. Once one is requested, the pointer must be
224 * valid at least until close() is called on the device
225 */
226 camera_metadata_t *mDefaultTemplates[CAMERA3_TEMPLATE_COUNT];
227
228 /**
229 * Private stream information, stored in camera3_stream_t->priv.
230 */
231 struct PrivateStreamInfo {
232 bool alive;
233 bool registered;
234 };
235
236 // Shortcut to the input stream
237 camera3_stream_t* mInputStream;
238
239 typedef List<camera3_stream_t*> StreamList;
240 typedef List<camera3_stream_t*>::iterator StreamIterator;
241 typedef Vector<camera3_stream_buffer> HalBufferVector;
242
243 uint32_t mAvailableJpegSize[64 * 8];
244
245 struct ExifInfo info;
246
247 // All streams, including input stream
248 StreamList mStreams;
249
250 // Cached settings from latest submitted request
251 CameraMetadata mPrevSettings;
252
253 /** Fake hardware interfaces */
254 sp<Sensor> mSensor;
255 sp<JpegCompressor> mJpegCompressor;
256 friend class JpegCompressor;
257 unsigned int mSupportCap;
258 unsigned int mSupportRotate;
259 camera_status_t mCameraStatus;
260 /** Processing thread for sending out results */
261
262 class ReadoutThread : public Thread, private JpegCompressor::JpegListener {
263 public:
264 ReadoutThread(EmulatedFakeCamera3 *parent);
265 ~ReadoutThread();
266
267 struct Request {
268 uint32_t frameNumber;
269 CameraMetadata settings;
270 HalBufferVector *buffers;
271 Buffers *sensorBuffers;
272 bool havethumbnail;
273 };
274
275 /**
276 * Interface to parent class
277 */
278
279 // Place request in the in-flight queue to wait for sensor capture
280 void queueCaptureRequest(const Request &r);
281 // Test if the readout thread is idle (no in-flight requests, not
282 // currently reading out anything
283 bool isIdle();
284
285 // Wait until isIdle is true
286 status_t waitForReadout();
287 status_t setJpegCompressorListener(EmulatedFakeCamera3 *parent);
288 status_t startJpegCompressor(EmulatedFakeCamera3 *parent);
289 status_t shutdownJpegCompressor(EmulatedFakeCamera3 * parent);
290 void sendExitReadoutThreadSignal(void);
291
292 private:
293 static const nsecs_t kWaitPerLoop = 10000000L; // 10 ms
294 static const nsecs_t kMaxWaitLoops = 1000;
295 static const size_t kMaxQueueSize = 2;
296
297 EmulatedFakeCamera3 *mParent;
298 Mutex mLock;
299
300 List<Request> mInFlightQueue;
301 Condition mInFlightSignal;
302 bool mThreadActive;
303
304 virtual bool threadLoop();
305
306 // Only accessed by threadLoop
307
308 Request mCurrentRequest;
309
310 // Jpeg completion callbacks
311 bool mExitReadoutThread;
312 Mutex mJpegLock;
313 bool mJpegWaiting;
314 camera3_stream_buffer mJpegHalBuffer;
315 uint32_t mJpegFrameNumber;
316 virtual void onJpegDone(const StreamBuffer &jpegBuffer, bool success, CaptureRequest &r);
317 virtual void onJpegInputDone(const StreamBuffer &inputBuffer);
318 };
319
320 sp<ReadoutThread> mReadoutThread;
321
322 /** Fake 3A constants */
323
324 static const nsecs_t kNormalExposureTime;
325 static const nsecs_t kFacePriorityExposureTime;
326 static const int kNormalSensitivity;
327 static const int kFacePrioritySensitivity;
328 // Rate of converging AE to new target value, as fraction of difference between
329 // current and target value.
330 static const float kExposureTrackRate;
331 // Minimum duration for precapture state. May be longer if slow to converge
332 // to target exposure
333 static const int kPrecaptureMinFrames;
334 // How often to restart AE 'scanning'
335 static const int kStableAeMaxFrames;
336 // Maximum stop below 'normal' exposure time that we'll wander to while
337 // pretending to converge AE. In powers of 2. (-2 == 1/4 as bright)
338 static const float kExposureWanderMin;
339 // Maximum stop above 'normal' exposure time that we'll wander to while
340 // pretending to converge AE. In powers of 2. (2 == 4x as bright)
341 static const float kExposureWanderMax;
342
343 /** Fake 3A state */
344
345 uint8_t mControlMode;
346 bool mFacePriority;
347 uint8_t mAeState;
348 uint8_t mAfState;
349 uint8_t mAwbState;
350 uint8_t mAeMode;
351 uint8_t mAfMode;
352 uint8_t mAwbMode;
353 int mAfTriggerId;
354 int mZoomMin;
355 int mZoomMax;
356 int mZoomStep;
357
358 int mAeCounter;
359 nsecs_t mAeCurrentExposureTime;
360 nsecs_t mAeTargetExposureTime;
361 int mAeCurrentSensitivity;
362
363};
364
365} // namespace android
366
367#endif // HW_EMULATOR_CAMERA_EMULATED_CAMERA3_H
368