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 <CameraMetadata.h> 27 using ::android::hardware::camera::common::V1_0::helper::CameraMetadata; 28 29 #include <utils/List.h> 30 #include <utils/Mutex.h> 31 #include <utils/SortedVector.h> 32 #include "EmulatedCamera3.h" 33 #include "fake-pipeline2/Base.h" 34 #include "fake-pipeline2/JpegCompressor.h" 35 #include "fake-pipeline2/Sensor.h" 36 37 namespace android { 38 39 /** 40 * Encapsulates functionality for a v3 HAL camera which produces synthetic data. 41 * 42 * Note that EmulatedCameraFactory instantiates an object of this class just 43 * once, when EmulatedCameraFactory instance gets constructed. Connection to / 44 * disconnection from the actual camera device is handled by calls to 45 * connectDevice(), and closeCamera() methods of this class that are invoked in 46 * response to hw_module_methods_t::open, and camera_device::close callbacks. 47 */ 48 class EmulatedFakeCamera3 : public EmulatedCamera3, 49 private Sensor::SensorListener { 50 public: 51 EmulatedFakeCamera3(int cameraId, bool facingBack, 52 struct hw_module_t *module); 53 54 virtual ~EmulatedFakeCamera3(); 55 56 /**************************************************************************** 57 * EmulatedCamera3 virtual overrides 58 ***************************************************************************/ 59 60 public: 61 virtual status_t Initialize(const cuttlefish::CameraDefinition ¶ms); 62 63 /**************************************************************************** 64 * Camera module API and generic hardware device API implementation 65 ***************************************************************************/ 66 67 public: 68 virtual status_t connectCamera(hw_device_t **device); 69 70 virtual status_t closeCamera(); 71 72 virtual status_t getCameraInfo(struct camera_info *info); 73 74 virtual status_t setTorchMode(bool enabled); 75 76 /**************************************************************************** 77 * EmulatedCamera3 abstract API implementation 78 ***************************************************************************/ 79 80 protected: 81 virtual status_t configureStreams(camera3_stream_configuration *streamList); 82 83 virtual status_t registerStreamBuffers( 84 const camera3_stream_buffer_set *bufferSet); 85 86 virtual const camera_metadata_t *constructDefaultRequestSettings(int type); 87 88 virtual status_t processCaptureRequest(camera3_capture_request *request); 89 90 virtual status_t flush(); 91 92 /** Debug methods */ 93 94 virtual void dump(int fd); 95 96 private: 97 /** 98 * Get the requested capability set for this camera 99 */ 100 status_t getCameraCapabilities(); 101 102 bool hasCapability(AvailableCapabilities cap); 103 104 /** 105 * Build the static info metadata buffer for this device 106 */ 107 status_t constructStaticInfo(const cuttlefish::CameraDefinition ¶ms); 108 109 /** 110 * Run the fake 3A algorithms as needed. May override/modify settings 111 * values. 112 */ 113 status_t process3A(CameraMetadata &settings); 114 115 status_t doFakeAE(CameraMetadata &settings); 116 status_t doFakeAF(CameraMetadata &settings); 117 status_t doFakeAWB(CameraMetadata &settings); 118 void update3A(CameraMetadata &settings); 119 120 /** Signal from readout thread that it doesn't have anything to do */ 121 void signalReadoutIdle(); 122 123 /** Handle interrupt events from the sensor */ 124 void onSensorEvent(uint32_t frameNumber, Event e, nsecs_t timestamp); 125 126 /**************************************************************************** 127 * Static configuration information 128 ***************************************************************************/ 129 private: 130 static const uint32_t kMaxRawStreamCount = 1; 131 static const uint32_t kMaxProcessedStreamCount = 3; 132 static const uint32_t kMaxJpegStreamCount = 1; 133 static const uint32_t kMaxReprocessStreamCount = 2; 134 static const uint32_t kMaxBufferCount = 4; 135 // We need a positive stream ID to distinguish external buffers from 136 // sensor-generated buffers which use a nonpositive ID. Otherwise, HAL3 has 137 // no concept of a stream id. 138 static const uint32_t kGenericStreamId = 1; 139 static const int32_t kAvailableFormats[]; 140 141 static const int64_t kSyncWaitTimeout = 10000000; // 10 ms 142 static const int32_t kMaxSyncTimeoutCount = 1000; // 1000 kSyncWaitTimeouts 143 static const uint32_t kFenceTimeoutMs = 2000; // 2 s 144 static const nsecs_t kJpegTimeoutNs = 5000000000l; // 5 s 145 146 /**************************************************************************** 147 * Data members. 148 ***************************************************************************/ 149 150 /* HAL interface serialization lock. */ 151 Mutex mLock; 152 153 /* Facing back (true) or front (false) switch. */ 154 bool mFacingBack; 155 int32_t mSensorWidth; 156 int32_t mSensorHeight; 157 158 SortedVector<AvailableCapabilities> mCapabilities; 159 160 /** 161 * Cache for default templates. Once one is requested, the pointer must be 162 * valid at least until close() is called on the device 163 */ 164 camera_metadata_t *mDefaultTemplates[CAMERA3_TEMPLATE_COUNT]; 165 166 /** 167 * Private stream information, stored in camera3_stream_t->priv. 168 */ 169 struct PrivateStreamInfo { 170 bool alive; 171 }; 172 173 // Shortcut to the input stream 174 camera3_stream_t *mInputStream; 175 176 typedef List<camera3_stream_t *> StreamList; 177 typedef List<camera3_stream_t *>::iterator StreamIterator; 178 typedef Vector<camera3_stream_buffer> HalBufferVector; 179 180 // All streams, including input stream 181 StreamList mStreams; 182 183 // Cached settings from latest submitted request 184 CameraMetadata mPrevSettings; 185 186 /** Fake hardware interfaces */ 187 sp<Sensor> mSensor; 188 sp<JpegCompressor> mJpegCompressor; 189 friend class JpegCompressor; 190 191 /** Processing thread for sending out results */ 192 193 class ReadoutThread : public Thread, private JpegCompressor::JpegListener { 194 public: 195 ReadoutThread(EmulatedFakeCamera3 *parent); 196 ~ReadoutThread(); 197 198 struct Request { 199 uint32_t frameNumber; 200 CameraMetadata settings; 201 HalBufferVector *buffers; 202 Buffers *sensorBuffers; 203 }; 204 205 /** 206 * Interface to parent class 207 */ 208 209 // Place request in the in-flight queue to wait for sensor capture 210 void queueCaptureRequest(const Request &r); 211 212 // Test if the readout thread is idle (no in-flight requests, not 213 // currently reading out anything 214 bool isIdle(); 215 216 // Wait until isIdle is true 217 status_t waitForReadout(); 218 219 private: 220 static const nsecs_t kWaitPerLoop = 10000000L; // 10 ms 221 static const nsecs_t kMaxWaitLoops = 1000; 222 static const size_t kMaxQueueSize = 2; 223 224 EmulatedFakeCamera3 *mParent; 225 Mutex mLock; 226 227 List<Request> mInFlightQueue; 228 Condition mInFlightSignal; 229 bool mThreadActive; 230 231 virtual bool threadLoop(); 232 233 // Only accessed by threadLoop 234 235 Request mCurrentRequest; 236 237 // Jpeg completion callbacks 238 239 Mutex mJpegLock; 240 bool mJpegWaiting; 241 camera3_stream_buffer mJpegHalBuffer; 242 uint32_t mJpegFrameNumber; 243 virtual void onJpegDone(const StreamBuffer &jpegBuffer, bool success); 244 virtual void onJpegInputDone(const StreamBuffer &inputBuffer); 245 }; 246 247 sp<ReadoutThread> mReadoutThread; 248 249 /** Fake 3A constants */ 250 251 static const nsecs_t kNormalExposureTime; 252 static const nsecs_t kFacePriorityExposureTime; 253 static const int kNormalSensitivity; 254 static const int kFacePrioritySensitivity; 255 // Rate of converging AE to new target value, as fraction of difference 256 // between current and target value. 257 static const float kExposureTrackRate; 258 // Minimum duration for precapture state. May be longer if slow to converge 259 // to target exposure 260 static const int kPrecaptureMinFrames; 261 // How often to restart AE 'scanning' 262 static const int kStableAeMaxFrames; 263 // Maximum stop below 'normal' exposure time that we'll wander to while 264 // pretending to converge AE. In powers of 2. (-2 == 1/4 as bright) 265 static const float kExposureWanderMin; 266 // Maximum stop above 'normal' exposure time that we'll wander to while 267 // pretending to converge AE. In powers of 2. (2 == 4x as bright) 268 static const float kExposureWanderMax; 269 270 /** Fake 3A state */ 271 272 uint8_t mControlMode; 273 bool mFacePriority; 274 uint8_t mAeState; 275 uint8_t mAfState; 276 uint8_t mAwbState; 277 uint8_t mAeMode; 278 uint8_t mAfMode; 279 uint8_t mAwbMode; 280 281 int mAeCounter; 282 nsecs_t mAeCurrentExposureTime; 283 nsecs_t mAeTargetExposureTime; 284 int mAeCurrentSensitivity; 285 }; 286 287 } // namespace android 288 289 #endif // HW_EMULATOR_CAMERA_EMULATED_CAMERA3_H 290