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