2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef HW_EMULATOR_CAMERA_EMULATED_FAKE_CAMERA3_H
18 #define HW_EMULATOR_CAMERA_EMULATED_FAKE_CAMERA3_H
21 * Contains declaration of a class EmulatedCamera that encapsulates
22 * functionality of a fake camera that implements version 3 of the camera device
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/SortedVector.h>
32 #include <utils/List.h>
33 #include <utils/Mutex.h>
38 * Encapsulates functionality for a v3 HAL camera which produces synthetic data.
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.
46 class EmulatedFakeCamera3 : public EmulatedCamera3,
47 private Sensor::SensorListener {
50 EmulatedFakeCamera3(int cameraId, bool facingBack,
51 struct hw_module_t* module);
53 virtual ~EmulatedFakeCamera3();
55 /****************************************************************************
56 * EmulatedCamera3 virtual overrides
57 ***************************************************************************/
61 virtual status_t Initialize();
63 /****************************************************************************
64 * Camera module API and generic hardware device API implementation
65 ***************************************************************************/
68 virtual status_t connectCamera(hw_device_t** device);
70 virtual status_t closeCamera();
72 virtual status_t getCameraInfo(struct camera_info *info);
74 /****************************************************************************
75 * EmulatedCamera3 abstract API implementation
76 ***************************************************************************/
80 virtual status_t configureStreams(
81 camera3_stream_configuration *streamList);
83 virtual status_t registerStreamBuffers(
84 const camera3_stream_buffer_set *bufferSet) ;
86 virtual const camera_metadata_t* constructDefaultRequestSettings(
89 virtual status_t processCaptureRequest(camera3_capture_request *request);
91 virtual status_t flush();
95 virtual void dump(int fd);
100 * Get the requested capability set for this camera
102 status_t getCameraCapabilities();
104 bool hasCapability(AvailableCapabilities cap);
107 * Build the static info metadata buffer for this device
109 status_t constructStaticInfo();
112 * Run the fake 3A algorithms as needed. May override/modify settings
115 status_t process3A(CameraMetadata &settings);
117 status_t doFakeAE(CameraMetadata &settings);
118 status_t doFakeAF(CameraMetadata &settings);
119 status_t doFakeAWB(CameraMetadata &settings);
120 void update3A(CameraMetadata &settings);
122 /** Signal from readout thread that it doesn't have anything to do */
123 void signalReadoutIdle();
125 /** Handle interrupt events from the sensor */
126 void onSensorEvent(uint32_t frameNumber, Event e, nsecs_t timestamp);
128 /****************************************************************************
129 * Static configuration information
130 ***************************************************************************/
132 static const uint32_t kMaxRawStreamCount = 1;
133 static const uint32_t kMaxProcessedStreamCount = 3;
134 static const uint32_t kMaxJpegStreamCount = 1;
135 static const uint32_t kMaxReprocessStreamCount = 2;
136 static const uint32_t kMaxBufferCount = 4;
137 // We need a positive stream ID to distinguish external buffers from
138 // sensor-generated buffers which use a nonpositive ID. Otherwise, HAL3 has
139 // no concept of a stream id.
140 static const uint32_t kGenericStreamId = 1;
141 static const int32_t kAvailableFormats[];
143 static const int64_t kSyncWaitTimeout = 10000000; // 10 ms
144 static const int32_t kMaxSyncTimeoutCount = 1000; // 1000 kSyncWaitTimeouts
145 static const uint32_t kFenceTimeoutMs = 2000; // 2 s
147 /****************************************************************************
149 ***************************************************************************/
151 /* HAL interface serialization lock. */
154 /* Facing back (true) or front (false) switch. */
157 SortedVector<AvailableCapabilities> mCapabilities;
160 * Cache for default templates. Once one is requested, the pointer must be
161 * valid at least until close() is called on the device
163 camera_metadata_t *mDefaultTemplates[CAMERA3_TEMPLATE_COUNT];
166 * Private stream information, stored in camera3_stream_t->priv.
168 struct PrivateStreamInfo {
172 // Shortcut to the input stream
173 camera3_stream_t* mInputStream;
175 typedef List<camera3_stream_t*> StreamList;
176 typedef List<camera3_stream_t*>::iterator StreamIterator;
177 typedef Vector<camera3_stream_buffer> HalBufferVector;
179 // All streams, including input stream
182 // Cached settings from latest submitted request
183 CameraMetadata mPrevSettings;
185 /** Fake hardware interfaces */
187 sp<JpegCompressor> mJpegCompressor;
188 friend class JpegCompressor;
190 /** Processing thread for sending out results */
192 class ReadoutThread : public Thread, private JpegCompressor::JpegListener {
194 ReadoutThread(EmulatedFakeCamera3 *parent);
198 uint32_t frameNumber;
199 CameraMetadata settings;
200 HalBufferVector *buffers;
201 Buffers *sensorBuffers;
205 * Interface to parent class
208 // Place request in the in-flight queue to wait for sensor capture
209 void queueCaptureRequest(const Request &r);
211 // Test if the readout thread is idle (no in-flight requests, not
212 // currently reading out anything
215 // Wait until isIdle is true
216 status_t waitForReadout();
219 static const nsecs_t kWaitPerLoop = 10000000L; // 10 ms
220 static const nsecs_t kMaxWaitLoops = 1000;
221 static const size_t kMaxQueueSize = 2;
223 EmulatedFakeCamera3 *mParent;
226 List<Request> mInFlightQueue;
227 Condition mInFlightSignal;
230 virtual bool threadLoop();
232 // Only accessed by threadLoop
234 Request mCurrentRequest;
236 // Jpeg completion callbacks
240 camera3_stream_buffer mJpegHalBuffer;
241 uint32_t mJpegFrameNumber;
242 virtual void onJpegDone(const StreamBuffer &jpegBuffer, bool success);
243 virtual void onJpegInputDone(const StreamBuffer &inputBuffer);
246 sp<ReadoutThread> mReadoutThread;
248 /** Fake 3A constants */
250 static const nsecs_t kNormalExposureTime;
251 static const nsecs_t kFacePriorityExposureTime;
252 static const int kNormalSensitivity;
253 static const int kFacePrioritySensitivity;
254 // Rate of converging AE to new target value, as fraction of difference between
255 // current and target value.
256 static const float kExposureTrackRate;
257 // Minimum duration for precapture state. May be longer if slow to converge
258 // to target exposure
259 static const int kPrecaptureMinFrames;
260 // How often to restart AE 'scanning'
261 static const int kStableAeMaxFrames;
262 // Maximum stop below 'normal' exposure time that we'll wander to while
263 // pretending to converge AE. In powers of 2. (-2 == 1/4 as bright)
264 static const float kExposureWanderMin;
265 // Maximum stop above 'normal' exposure time that we'll wander to while
266 // pretending to converge AE. In powers of 2. (2 == 4x as bright)
267 static const float kExposureWanderMax;
271 uint8_t mControlMode;
281 nsecs_t mAeCurrentExposureTime;
282 nsecs_t mAeTargetExposureTime;
283 int mAeCurrentSensitivity;
287 } // namespace android
289 #endif // HW_EMULATOR_CAMERA_EMULATED_CAMERA3_H