1 /* 2 * Copyright (C) 2012 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_CAMERA2_H 18 #define HW_EMULATOR_CAMERA_EMULATED_FAKE_CAMERA2_H 19 20 /* 21 * Contains declaration of a class EmulatedFakeCamera2 that encapsulates 22 * functionality of a fake camera that implements version 2 of the camera device 23 * interface. 24 */ 25 26 #include "EmulatedCamera2.h" 27 #include "fake-pipeline2/Base.h" 28 #include "fake-pipeline2/Sensor.h" 29 #include "fake-pipeline2/JpegCompressor.h" 30 #include <ui/GraphicBufferMapper.h> 31 #include <utils/Condition.h> 32 #include <utils/KeyedVector.h> 33 #include <utils/String8.h> 34 #include <utils/String16.h> 35 36 namespace android { 37 38 /* Encapsulates functionality of an advanced fake camera. This camera contains 39 * a simple simulation of a scene, sensor, and image processing pipeline. 40 */ 41 class EmulatedFakeCamera2 : public EmulatedCamera2 { 42 public: 43 /* Constructs EmulatedFakeCamera instance. */ 44 EmulatedFakeCamera2(int cameraId, bool facingBack, struct hw_module_t* module, 45 GraphicBufferMapper* gbm); 46 47 /* Destructs EmulatedFakeCamera instance. */ 48 ~EmulatedFakeCamera2(); 49 50 /**************************************************************************** 51 * EmulatedCamera2 virtual overrides. 52 ***************************************************************************/ 53 54 public: 55 /* Initializes EmulatedFakeCamera2 instance. */ 56 status_t Initialize(); 57 58 /**************************************************************************** 59 * Camera Module API and generic hardware device API implementation 60 ***************************************************************************/ 61 public: 62 63 virtual status_t connectCamera(hw_device_t** device); 64 65 virtual status_t plugCamera(); 66 virtual status_t unplugCamera(); 67 virtual camera_device_status_t getHotplugStatus(); 68 69 virtual status_t closeCamera(); 70 71 virtual status_t getCameraInfo(struct camera_info *info); 72 73 /**************************************************************************** 74 * EmulatedCamera2 abstract API implementation. 75 ***************************************************************************/ 76 protected: 77 /** Request input queue */ 78 79 virtual int requestQueueNotify(); 80 81 /** Count of requests in flight */ 82 virtual int getInProgressCount(); 83 84 /** Cancel all captures in flight */ 85 //virtual int flushCapturesInProgress(); 86 87 /** Construct default request */ 88 virtual int constructDefaultRequest( 89 int request_template, 90 camera_metadata_t **request); 91 92 virtual int allocateStream( 93 uint32_t width, 94 uint32_t height, 95 int format, 96 const camera2_stream_ops_t *stream_ops, 97 uint32_t *stream_id, 98 uint32_t *format_actual, 99 uint32_t *usage, 100 uint32_t *max_buffers); 101 102 virtual int registerStreamBuffers( 103 uint32_t stream_id, 104 int num_buffers, 105 buffer_handle_t *buffers); 106 107 virtual int releaseStream(uint32_t stream_id); 108 109 // virtual int allocateReprocessStream( 110 // uint32_t width, 111 // uint32_t height, 112 // uint32_t format, 113 // const camera2_stream_ops_t *stream_ops, 114 // uint32_t *stream_id, 115 // uint32_t *format_actual, 116 // uint32_t *usage, 117 // uint32_t *max_buffers); 118 119 virtual int allocateReprocessStreamFromStream( 120 uint32_t output_stream_id, 121 const camera2_stream_in_ops_t *stream_ops, 122 uint32_t *stream_id); 123 124 virtual int releaseReprocessStream(uint32_t stream_id); 125 126 virtual int triggerAction(uint32_t trigger_id, 127 int32_t ext1, 128 int32_t ext2); 129 130 /** Debug methods */ 131 132 virtual int dump(int fd); 133 134 public: 135 /**************************************************************************** 136 * Utility methods called by configure/readout threads and pipeline 137 ***************************************************************************/ 138 139 // Get information about a given stream. Will lock mMutex 140 const Stream &getStreamInfo(uint32_t streamId); 141 const ReprocessStream &getReprocessStreamInfo(uint32_t streamId); 142 143 // Notifies rest of camera subsystem of serious error 144 void signalError(); 145 146 private: 147 /**************************************************************************** 148 * Utility methods 149 ***************************************************************************/ 150 /** Construct static camera metadata, two-pass */ 151 status_t constructStaticInfo( 152 camera_metadata_t **info, 153 bool sizeRequest) const; 154 155 /** Two-pass implementation of constructDefaultRequest */ 156 status_t constructDefaultRequest( 157 int request_template, 158 camera_metadata_t **request, 159 bool sizeRequest) const; 160 /** Helper function for constructDefaultRequest */ 161 static status_t addOrSize( camera_metadata_t *request, 162 bool sizeRequest, 163 size_t *entryCount, 164 size_t *dataCount, 165 uint32_t tag, 166 const void *entry_data, 167 size_t entry_count); 168 169 /** Determine if the stream id is listed in any currently-in-flight 170 * requests. Assumes mMutex is locked */ 171 bool isStreamInUse(uint32_t streamId); 172 173 /** Determine if the reprocess stream id is listed in any 174 * currently-in-flight requests. Assumes mMutex is locked */ 175 bool isReprocessStreamInUse(uint32_t streamId); 176 177 /**************************************************************************** 178 * Pipeline controller threads 179 ***************************************************************************/ 180 181 class ConfigureThread: public Thread { 182 public: 183 ConfigureThread(EmulatedFakeCamera2 *parent); 184 ~ConfigureThread(); 185 186 status_t waitUntilRunning(); 187 status_t newRequestAvailable(); 188 status_t readyToRun(); 189 190 bool isStreamInUse(uint32_t id); 191 int getInProgressCount(); 192 private: 193 EmulatedFakeCamera2 *mParent; 194 static const nsecs_t kWaitPerLoop = 10000000L; // 10 ms 195 196 bool mRunning; 197 bool threadLoop(); 198 199 bool setupCapture(); 200 bool setupReprocess(); 201 202 bool configureNextCapture(); 203 bool configureNextReprocess(); 204 205 bool getBuffers(); 206 207 Mutex mInputMutex; // Protects mActive, mRequestCount 208 Condition mInputSignal; 209 bool mActive; // Whether we're waiting for input requests or actively 210 // working on them 211 size_t mRequestCount; 212 213 camera_metadata_t *mRequest; 214 215 Mutex mInternalsMutex; // Lock before accessing below members. 216 bool mWaitingForReadout; 217 bool mNextNeedsJpeg; 218 bool mNextIsCapture; 219 int32_t mNextFrameNumber; 220 int64_t mNextExposureTime; 221 int64_t mNextFrameDuration; 222 int32_t mNextSensitivity; 223 Buffers *mNextBuffers; 224 }; 225 226 class ReadoutThread: public Thread, private JpegCompressor::JpegListener { 227 public: 228 ReadoutThread(EmulatedFakeCamera2 *parent); 229 ~ReadoutThread(); 230 231 status_t readyToRun(); 232 233 // Input 234 status_t waitUntilRunning(); 235 bool waitForReady(nsecs_t timeout); 236 void setNextOperation(bool isCapture, 237 camera_metadata_t *request, 238 Buffers *buffers); 239 bool isStreamInUse(uint32_t id); 240 int getInProgressCount(); 241 private: 242 EmulatedFakeCamera2 *mParent; 243 244 bool mRunning; 245 bool threadLoop(); 246 247 bool readyForNextCapture(); 248 status_t collectStatisticsMetadata(camera_metadata_t *frame); 249 250 // Inputs 251 Mutex mInputMutex; // Protects mActive, mInFlightQueue, mRequestCount 252 Condition mInputSignal; 253 Condition mReadySignal; 254 255 bool mActive; 256 257 static const int kInFlightQueueSize = 4; 258 struct InFlightQueue { 259 bool isCapture; 260 camera_metadata_t *request; 261 Buffers *buffers; 262 } *mInFlightQueue; 263 264 size_t mInFlightHead; 265 size_t mInFlightTail; 266 267 size_t mRequestCount; 268 269 // Internals 270 Mutex mInternalsMutex; 271 272 bool mIsCapture; 273 camera_metadata_t *mRequest; 274 Buffers *mBuffers; 275 276 // Jpeg completion listeners 277 void onJpegDone(const StreamBuffer &jpegBuffer, bool success); 278 void onJpegInputDone(const StreamBuffer &inputBuffer); 279 nsecs_t mJpegTimestamp; 280 }; 281 282 // 3A management thread (auto-exposure, focus, white balance) 283 class ControlThread: public Thread { 284 public: 285 ControlThread(EmulatedFakeCamera2 *parent); 286 ~ControlThread(); 287 288 status_t readyToRun(); 289 290 status_t waitUntilRunning(); 291 292 // Interpret request's control parameters and override 293 // capture settings as needed 294 status_t processRequest(camera_metadata_t *request); 295 296 status_t triggerAction(uint32_t msgType, 297 int32_t ext1, int32_t ext2); 298 private: 299 ControlThread(const ControlThread &t); 300 ControlThread& operator=(const ControlThread &t); 301 302 // Constants controlling fake 3A behavior 303 static const nsecs_t kControlCycleDelay; 304 static const nsecs_t kMinAfDuration; 305 static const nsecs_t kMaxAfDuration; 306 static const float kAfSuccessRate; 307 static const float kContinuousAfStartRate; 308 309 static const float kAeScanStartRate; 310 static const nsecs_t kMinAeDuration; 311 static const nsecs_t kMaxAeDuration; 312 static const nsecs_t kMinPrecaptureAeDuration; 313 static const nsecs_t kMaxPrecaptureAeDuration; 314 315 static const nsecs_t kNormalExposureTime; 316 static const nsecs_t kExposureJump; 317 static const nsecs_t kMinExposureTime; 318 319 EmulatedFakeCamera2 *mParent; 320 321 bool mRunning; 322 bool threadLoop(); 323 324 Mutex mInputMutex; // Protects input methods 325 Condition mInputSignal; 326 327 // Trigger notifications 328 bool mStartAf; 329 bool mCancelAf; 330 bool mStartPrecapture; 331 332 // Latest state for 3A request fields 333 uint8_t mControlMode; 334 335 uint8_t mEffectMode; 336 uint8_t mSceneMode; 337 338 uint8_t mAfMode; 339 bool mAfModeChange; 340 341 uint8_t mAwbMode; 342 uint8_t mAeMode; 343 344 // Latest trigger IDs 345 int32_t mAfTriggerId; 346 int32_t mPrecaptureTriggerId; 347 348 // Current state for 3A algorithms 349 uint8_t mAfState; 350 uint8_t mAeState; 351 uint8_t mAwbState; 352 bool mAeLock; 353 354 // Current control parameters 355 nsecs_t mExposureTime; 356 357 // Private to threadLoop and its utility methods 358 359 nsecs_t mAfScanDuration; 360 nsecs_t mAeScanDuration; 361 bool mLockAfterPassiveScan; 362 363 // Utility methods for AF 364 int processAfTrigger(uint8_t afMode, uint8_t afState); 365 int maybeStartAfScan(uint8_t afMode, uint8_t afState); 366 int updateAfScan(uint8_t afMode, uint8_t afState, nsecs_t *maxSleep); 367 void updateAfState(uint8_t newState, int32_t triggerId); 368 369 // Utility methods for precapture trigger 370 int processPrecaptureTrigger(uint8_t aeMode, uint8_t aeState); 371 int maybeStartAeScan(uint8_t aeMode, bool aeLock, uint8_t aeState); 372 int updateAeScan(uint8_t aeMode, bool aeLock, uint8_t aeState, 373 nsecs_t *maxSleep); 374 void updateAeState(uint8_t newState, int32_t triggerId); 375 }; 376 377 /**************************************************************************** 378 * Static configuration information 379 ***************************************************************************/ 380 private: 381 static const uint32_t kMaxRawStreamCount = 1; 382 static const uint32_t kMaxProcessedStreamCount = 3; 383 static const uint32_t kMaxJpegStreamCount = 1; 384 static const uint32_t kMaxReprocessStreamCount = 2; 385 static const uint32_t kMaxBufferCount = 4; 386 static const uint32_t kAvailableFormats[]; 387 static const uint32_t kAvailableRawSizes[]; 388 static const uint64_t kAvailableRawMinDurations[]; 389 static const uint32_t kAvailableProcessedSizesBack[]; 390 static const uint32_t kAvailableProcessedSizesFront[]; 391 static const uint64_t kAvailableProcessedMinDurations[]; 392 static const uint32_t kAvailableJpegSizesBack[]; 393 static const uint32_t kAvailableJpegSizesFront[]; 394 static const uint64_t kAvailableJpegMinDurations[]; 395 396 /**************************************************************************** 397 * Data members. 398 ***************************************************************************/ 399 400 protected: 401 /* Facing back (true) or front (false) switch. */ 402 bool mFacingBack; 403 404 private: 405 bool mIsConnected; 406 407 int32_t mSensorWidth, mSensorHeight; 408 409 /** Stream manipulation */ 410 uint32_t mNextStreamId; 411 uint32_t mRawStreamCount; 412 uint32_t mProcessedStreamCount; 413 uint32_t mJpegStreamCount; 414 415 uint32_t mNextReprocessStreamId; 416 uint32_t mReprocessStreamCount; 417 418 KeyedVector<uint32_t, Stream> mStreams; 419 KeyedVector<uint32_t, ReprocessStream> mReprocessStreams; 420 GraphicBufferMapper* mGBM; 421 422 /** Simulated hardware interfaces */ 423 sp<Sensor> mSensor; 424 sp<JpegCompressor> mJpegCompressor; 425 426 /** Pipeline control threads */ 427 sp<ConfigureThread> mConfigureThread; 428 sp<ReadoutThread> mReadoutThread; 429 sp<ControlThread> mControlThread; 430 }; 431 432 }; /* namespace android */ 433 434 #endif /* HW_EMULATOR_CAMERA_EMULATED_FAKE_CAMERA2_H */ 435