summaryrefslogtreecommitdiff
path: root/v3/EmulatedFakeCamera3.h (plain)
blob: a4f1a3dc02abcadf3966896ed1c998f6093af6c1
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
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 enum sensor_type_e mSensorType;
219
220 /**
221 * Cache for default templates. Once one is requested, the pointer must be
222 * valid at least until close() is called on the device
223 */
224 camera_metadata_t *mDefaultTemplates[CAMERA3_TEMPLATE_COUNT];
225
226 /**
227 * Private stream information, stored in camera3_stream_t->priv.
228 */
229 struct PrivateStreamInfo {
230 bool alive;
231 bool registered;
232 };
233
234 // Shortcut to the input stream
235 camera3_stream_t* mInputStream;
236
237 typedef List<camera3_stream_t*> StreamList;
238 typedef List<camera3_stream_t*>::iterator StreamIterator;
239 typedef Vector<camera3_stream_buffer> HalBufferVector;
240
241 uint32_t mAvailableJpegSize[64 * 8];
242
243 struct ExifInfo info;
244
245 // All streams, including input stream
246 StreamList mStreams;
247
248 // Cached settings from latest submitted request
249 CameraMetadata mPrevSettings;
250
251 /** Fake hardware interfaces */
252 sp<Sensor> mSensor;
253 sp<JpegCompressor> mJpegCompressor;
254 friend class JpegCompressor;
255 unsigned int mSupportCap;
256 unsigned int mSupportRotate;
257 camera_status_t mCameraStatus;
258 bool mFlushTag;
259 /** Processing thread for sending out results */
260
261 class ReadoutThread : public Thread, private JpegCompressor::JpegListener {
262 public:
263 ReadoutThread(EmulatedFakeCamera3 *parent);
264 ~ReadoutThread();
265
266 struct Request {
267 uint32_t frameNumber;
268 CameraMetadata settings;
269 HalBufferVector *buffers;
270 Buffers *sensorBuffers;
271 bool havethumbnail;
272 };
273
274 /**
275 * Interface to parent class
276 */
277
278 // Place request in the in-flight queue to wait for sensor capture
279 void queueCaptureRequest(const Request &r);
280 // Test if the readout thread is idle (no in-flight requests, not
281 // currently reading out anything
282 bool isIdle();
283
284 // Wait until isIdle is true
285 status_t waitForReadout();
286 status_t setJpegCompressorListener(EmulatedFakeCamera3 *parent);
287 status_t startJpegCompressor(EmulatedFakeCamera3 *parent);
288 status_t shutdownJpegCompressor(EmulatedFakeCamera3 * parent);
289 void sendExitReadoutThreadSignal(void);
290 status_t flushAllRequest(bool flag);
291 void setFlushFlag(bool flag);
292 void sendFlushSingnal(void);
293 private:
294 static const nsecs_t kWaitPerLoop = 10000000L; // 10 ms
295 static const nsecs_t kMaxWaitLoops = 1000;
296 static const size_t kMaxQueueSize = 2;
297
298 EmulatedFakeCamera3 *mParent;
299 Mutex mLock;
300
301 List<Request> mInFlightQueue;
302 Condition mInFlightSignal;
303 bool mThreadActive;
304
305 virtual bool threadLoop();
306
307 // Only accessed by threadLoop
308
309 Request mCurrentRequest;
310
311 // Jpeg completion callbacks
312 bool mExitReadoutThread;
313 Mutex mJpegLock;
314 bool mJpegWaiting;
315 camera3_stream_buffer mJpegHalBuffer;
316 uint32_t mJpegFrameNumber;
317 bool mFlushFlag;
318 Condition mFlush;
319 virtual void onJpegDone(const StreamBuffer &jpegBuffer, bool success, CaptureRequest &r);
320 virtual void onJpegInputDone(const StreamBuffer &inputBuffer);
321 };
322
323 sp<ReadoutThread> mReadoutThread;
324
325 /** Fake 3A constants */
326
327 static const nsecs_t kNormalExposureTime;
328 static const nsecs_t kFacePriorityExposureTime;
329 static const int kNormalSensitivity;
330 static const int kFacePrioritySensitivity;
331 // Rate of converging AE to new target value, as fraction of difference between
332 // current and target value.
333 static const float kExposureTrackRate;
334 // Minimum duration for precapture state. May be longer if slow to converge
335 // to target exposure
336 static const int kPrecaptureMinFrames;
337 // How often to restart AE 'scanning'
338 static const int kStableAeMaxFrames;
339 // Maximum stop below 'normal' exposure time that we'll wander to while
340 // pretending to converge AE. In powers of 2. (-2 == 1/4 as bright)
341 static const float kExposureWanderMin;
342 // Maximum stop above 'normal' exposure time that we'll wander to while
343 // pretending to converge AE. In powers of 2. (2 == 4x as bright)
344 static const float kExposureWanderMax;
345
346 /** Fake 3A state */
347
348 uint8_t mControlMode;
349 bool mFacePriority;
350 uint8_t mAeState;
351 uint8_t mAfState;
352 uint8_t mAwbState;
353 uint8_t mAeMode;
354 uint8_t mAfMode;
355 uint8_t mAwbMode;
356 int mAfTriggerId;
357 int mZoomMin;
358 int mZoomMax;
359 int mZoomStep;
360 int mFrameDuration;
361
362 int mAeCounter;
363 nsecs_t mAeCurrentExposureTime;
364 nsecs_t mAeTargetExposureTime;
365 int mAeCurrentSensitivity;
366
367};
368
369} // namespace android
370
371#endif // HW_EMULATOR_CAMERA_EMULATED_CAMERA3_H
372