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