blob: 4a23f26a693114b58eb49d2d7733809a59055076
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 bool getCameraStatus(); |
83 | |
84 | /**************************************************************************** |
85 | * EmulatedCamera3 abstract API implementation |
86 | ***************************************************************************/ |
87 | |
88 | protected: |
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 | |
113 | private: |
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 | ***************************************************************************/ |
181 | private: |
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 = 1000; // 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 | // 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 | /** Processing thread for sending out results */ |
259 | |
260 | class ReadoutThread : public Thread, private JpegCompressor::JpegListener { |
261 | public: |
262 | ReadoutThread(EmulatedFakeCamera3 *parent); |
263 | ~ReadoutThread(); |
264 | |
265 | struct Request { |
266 | uint32_t frameNumber; |
267 | CameraMetadata settings; |
268 | HalBufferVector *buffers; |
269 | Buffers *sensorBuffers; |
270 | bool havethumbnail; |
271 | }; |
272 | |
273 | /** |
274 | * Interface to parent class |
275 | */ |
276 | |
277 | // Place request in the in-flight queue to wait for sensor capture |
278 | void queueCaptureRequest(const Request &r); |
279 | // Test if the readout thread is idle (no in-flight requests, not |
280 | // currently reading out anything |
281 | bool isIdle(); |
282 | |
283 | // Wait until isIdle is true |
284 | status_t waitForReadout(); |
285 | status_t setJpegCompressorListener(EmulatedFakeCamera3 *parent); |
286 | status_t startJpegCompressor(EmulatedFakeCamera3 *parent); |
287 | status_t shutdownJpegCompressor(EmulatedFakeCamera3 * parent); |
288 | |
289 | private: |
290 | static const nsecs_t kWaitPerLoop = 10000000L; // 10 ms |
291 | static const nsecs_t kMaxWaitLoops = 1000; |
292 | static const size_t kMaxQueueSize = 2; |
293 | |
294 | EmulatedFakeCamera3 *mParent; |
295 | Mutex mLock; |
296 | |
297 | List<Request> mInFlightQueue; |
298 | Condition mInFlightSignal; |
299 | bool mThreadActive; |
300 | |
301 | virtual bool threadLoop(); |
302 | |
303 | // Only accessed by threadLoop |
304 | |
305 | Request mCurrentRequest; |
306 | |
307 | // Jpeg completion callbacks |
308 | |
309 | Mutex mJpegLock; |
310 | bool mJpegWaiting; |
311 | camera3_stream_buffer mJpegHalBuffer; |
312 | uint32_t mJpegFrameNumber; |
313 | virtual void onJpegDone(const StreamBuffer &jpegBuffer, bool success, CaptureRequest &r); |
314 | virtual void onJpegInputDone(const StreamBuffer &inputBuffer); |
315 | }; |
316 | |
317 | sp<ReadoutThread> mReadoutThread; |
318 | |
319 | /** Fake 3A constants */ |
320 | |
321 | static const nsecs_t kNormalExposureTime; |
322 | static const nsecs_t kFacePriorityExposureTime; |
323 | static const int kNormalSensitivity; |
324 | static const int kFacePrioritySensitivity; |
325 | // Rate of converging AE to new target value, as fraction of difference between |
326 | // current and target value. |
327 | static const float kExposureTrackRate; |
328 | // Minimum duration for precapture state. May be longer if slow to converge |
329 | // to target exposure |
330 | static const int kPrecaptureMinFrames; |
331 | // How often to restart AE 'scanning' |
332 | static const int kStableAeMaxFrames; |
333 | // Maximum stop below 'normal' exposure time that we'll wander to while |
334 | // pretending to converge AE. In powers of 2. (-2 == 1/4 as bright) |
335 | static const float kExposureWanderMin; |
336 | // Maximum stop above 'normal' exposure time that we'll wander to while |
337 | // pretending to converge AE. In powers of 2. (2 == 4x as bright) |
338 | static const float kExposureWanderMax; |
339 | |
340 | /** Fake 3A state */ |
341 | |
342 | uint8_t mControlMode; |
343 | bool mFacePriority; |
344 | uint8_t mAeState; |
345 | uint8_t mAfState; |
346 | uint8_t mAwbState; |
347 | uint8_t mAeMode; |
348 | uint8_t mAfMode; |
349 | uint8_t mAwbMode; |
350 | int mAfTriggerId; |
351 | int mZoomMin; |
352 | int mZoomMax; |
353 | int mZoomStep; |
354 | |
355 | int mAeCounter; |
356 | nsecs_t mAeCurrentExposureTime; |
357 | nsecs_t mAeTargetExposureTime; |
358 | int mAeCurrentSensitivity; |
359 | |
360 | }; |
361 | |
362 | } // namespace android |
363 | |
364 | #endif // HW_EMULATOR_CAMERA_EMULATED_CAMERA3_H |
365 |