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