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