Logan Weber | e23f777 | 2017-07-24 16:15:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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_QEMU_CAMERA3_H |
| 18 | #define HW_EMULATOR_CAMERA_EMULATED_QEMU_CAMERA3_H |
| 19 | |
| 20 | /* |
| 21 | * Contains declaration of a class EmulatedQemuCamera3 that encapsulates |
| 22 | * functionality of a video capture device that implements version 3 of the |
| 23 | * camera device interface. |
| 24 | */ |
| 25 | |
| 26 | #include "EmulatedCamera3.h" |
| 27 | #include "fake-pipeline2/JpegCompressor.h" |
| 28 | #include "qemu-pipeline3/QemuSensor.h" |
| 29 | |
huans | d036b6e | 2018-01-30 16:11:17 -0800 | [diff] [blame] | 30 | #include <CameraMetadata.h> |
Logan Weber | e23f777 | 2017-07-24 16:15:12 -0700 | [diff] [blame] | 31 | #include <utils/SortedVector.h> |
| 32 | #include <utils/List.h> |
| 33 | #include <utils/Mutex.h> |
| 34 | |
huans | d036b6e | 2018-01-30 16:11:17 -0800 | [diff] [blame] | 35 | using ::android::hardware::camera::common::V1_0::helper::CameraMetadata; |
| 36 | |
Logan Weber | e23f777 | 2017-07-24 16:15:12 -0700 | [diff] [blame] | 37 | namespace android { |
| 38 | |
| 39 | /* |
| 40 | * Encapsulates functionality for a v3 HAL camera which interfaces with a video |
| 41 | * capture device on the host computer. |
| 42 | * |
| 43 | * NOTE: Currently, resolutions larger than 640x480 are susceptible to |
| 44 | * performance problems. |
| 45 | * |
| 46 | * TODO: Optimize to allow regular usage of higher resolutions. |
| 47 | */ |
| 48 | class EmulatedQemuCamera3 : public EmulatedCamera3, |
| 49 | private QemuSensor::QemuSensorListener { |
| 50 | public: |
| 51 | EmulatedQemuCamera3(int cameraId, struct hw_module_t* module); |
| 52 | virtual ~EmulatedQemuCamera3(); |
| 53 | |
| 54 | /* |
| 55 | * Args: |
| 56 | * deviceName: File path where the capture device can be found (e.g., |
| 57 | * "/dev/video0"). |
| 58 | * frameDims: Space-delimited resolutions with each dimension delimited |
| 59 | * by a comma (e.g., "640,480 320,240"). |
| 60 | * facingDir: Contains either "front" or "back". |
| 61 | */ |
| 62 | virtual status_t Initialize(const char *deviceName, |
| 63 | const char *frameDims, |
| 64 | const char *facingDir); |
| 65 | |
| 66 | /************************************************************************** |
| 67 | * Camera Module API and Generic Hardware Device API Implementation |
| 68 | *************************************************************************/ |
| 69 | virtual status_t connectCamera(hw_device_t **device); |
| 70 | virtual status_t closeCamera(); |
| 71 | virtual status_t getCameraInfo(struct camera_info *info); |
| 72 | |
| 73 | protected: |
| 74 | /************************************************************************** |
| 75 | * EmulatedCamera3 Abstract API Implementation |
| 76 | *************************************************************************/ |
| 77 | virtual status_t configureStreams(camera3_stream_configuration *streamList); |
| 78 | virtual status_t registerStreamBuffers( |
| 79 | const camera3_stream_buffer_set *bufferSet); |
| 80 | virtual const camera_metadata_t* constructDefaultRequestSettings(int type); |
| 81 | virtual status_t processCaptureRequest(camera3_capture_request *request); |
| 82 | virtual status_t flush(); |
| 83 | |
| 84 | private: |
| 85 | /* |
| 86 | * Get the requested capability set (from boot properties) for this camera |
| 87 | * and populate "mCapabilities". |
| 88 | */ |
| 89 | status_t getCameraCapabilities(); |
| 90 | |
| 91 | /* |
| 92 | * Extracts supported resolutions into "mResolutions". |
| 93 | * |
| 94 | * Args: |
| 95 | * frameDims: A string of space-delimited resolutions with each |
| 96 | * dimension delimited by a comma (e.g., "640,480 320,240"). |
| 97 | */ |
| 98 | void parseResolutions(const char *frameDims); |
| 99 | |
| 100 | bool hasCapability(AvailableCapabilities cap); |
| 101 | |
| 102 | /* |
| 103 | * Build the static info metadata buffer for this device. |
| 104 | */ |
| 105 | status_t constructStaticInfo(); |
| 106 | |
huans | 8c8cef8 | 2017-11-27 09:36:47 -0800 | [diff] [blame] | 107 | status_t process3A(CameraMetadata &settings); |
| 108 | |
| 109 | status_t doFakeAE(CameraMetadata &settings); |
| 110 | status_t doFakeAF(CameraMetadata &settings); |
| 111 | status_t doFakeAWB(CameraMetadata &settings); |
| 112 | void update3A(CameraMetadata &settings); |
| 113 | |
Logan Weber | e23f777 | 2017-07-24 16:15:12 -0700 | [diff] [blame] | 114 | /* |
| 115 | * Signal from readout thread that it doesn't have anything to do. |
| 116 | */ |
| 117 | void signalReadoutIdle(); |
| 118 | |
| 119 | /* |
| 120 | * Handle interrupt events from the sensor. |
| 121 | */ |
| 122 | void onQemuSensorEvent(uint32_t frameNumber, Event e, nsecs_t timestamp); |
| 123 | |
| 124 | private: |
| 125 | /************************************************************************** |
| 126 | * Static Configuration Information |
| 127 | *************************************************************************/ |
| 128 | static const uint32_t kMaxRawStreamCount = 0; |
| 129 | static const uint32_t kMaxProcessedStreamCount = 3; |
| 130 | static const uint32_t kMaxJpegStreamCount = 1; |
| 131 | static const uint32_t kMaxReprocessStreamCount = 0; |
| 132 | static const uint32_t kMaxBufferCount = 3; |
| 133 | // We need a positive stream ID to distinguish external buffers from |
| 134 | // sensor-generated buffers which use a nonpositive ID. Otherwise, HAL3 has |
| 135 | // no concept of a stream id. |
| 136 | static const uint32_t kGenericStreamId = 1; |
| 137 | static const int32_t kAvailableFormats[]; |
| 138 | static const int64_t kSyncWaitTimeout = 10000000; // 10 ms |
| 139 | static const int32_t kMaxSyncTimeoutCount = 1000; // 1000 kSyncWaitTimeouts |
| 140 | static const uint32_t kFenceTimeoutMs = 2000; // 2 s |
| 141 | static const nsecs_t kJpegTimeoutNs = 5000000000l; // 5 s |
| 142 | |
| 143 | /************************************************************************** |
| 144 | * Data Members |
| 145 | *************************************************************************/ |
| 146 | |
| 147 | // HAL interface serialization lock. |
| 148 | Mutex mLock; |
| 149 | |
| 150 | const char *mDeviceName; |
| 151 | bool mFacingBack; |
| 152 | uint32_t mSensorWidth; |
| 153 | uint32_t mSensorHeight; |
| 154 | std::vector<std::pair<int32_t,int32_t>> mResolutions; |
| 155 | |
| 156 | |
| 157 | SortedVector<AvailableCapabilities> mCapabilities; |
| 158 | |
| 159 | /* |
| 160 | * Cache for default templates. Once one is requested, the pointer must be |
| 161 | * valid at least until close() is called on the device. |
| 162 | */ |
| 163 | camera_metadata_t *mDefaultTemplates[CAMERA3_TEMPLATE_COUNT]; |
| 164 | |
| 165 | // Private stream information, stored in camera3_stream_t->priv. |
| 166 | struct PrivateStreamInfo { |
| 167 | bool alive; |
| 168 | }; |
| 169 | |
| 170 | // Shortcut to the input stream. |
| 171 | camera3_stream_t* mInputStream; |
| 172 | |
| 173 | typedef List<camera3_stream_t*> StreamList; |
| 174 | typedef List<camera3_stream_t*>::iterator StreamIterator; |
| 175 | typedef Vector<camera3_stream_buffer> HalBufferVector; |
| 176 | |
| 177 | // All streams, including input stream. |
| 178 | StreamList mStreams; |
| 179 | |
| 180 | // Cached settings from latest submitted request. |
| 181 | CameraMetadata mPrevSettings; |
| 182 | |
| 183 | // Fake Hardware Interfaces |
| 184 | sp<QemuSensor> mSensor; |
| 185 | sp<JpegCompressor> mJpegCompressor; |
| 186 | friend class JpegCompressor; |
| 187 | |
| 188 | /* |
| 189 | * Processing thread for sending out results. |
| 190 | */ |
| 191 | class ReadoutThread : public Thread, private JpegCompressor::JpegListener { |
| 192 | public: |
| 193 | ReadoutThread(EmulatedQemuCamera3 *parent); |
| 194 | ~ReadoutThread(); |
| 195 | |
| 196 | struct Request { |
| 197 | uint32_t frameNumber; |
| 198 | CameraMetadata settings; |
| 199 | HalBufferVector *buffers; |
| 200 | Buffers *sensorBuffers; |
| 201 | }; |
| 202 | |
| 203 | /* |
| 204 | * Interface to Parent Class |
| 205 | */ |
| 206 | |
| 207 | /* |
| 208 | * Place request in the in-flight queue to wait for sensor capture. |
| 209 | */ |
| 210 | void queueCaptureRequest(const Request &r); |
| 211 | |
| 212 | /* |
| 213 | * Test if the readout thread is idle (no in-flight requests, not |
| 214 | * currently reading out anything). |
| 215 | */ |
| 216 | bool isIdle(); |
| 217 | |
| 218 | /* |
| 219 | * Wait until isIdle is true. |
| 220 | */ |
| 221 | status_t waitForReadout(); |
| 222 | |
| 223 | private: |
| 224 | static const nsecs_t kWaitPerLoop = 10000000L; // 10 ms |
| 225 | static const nsecs_t kMaxWaitLoops = 1000; |
| 226 | static const size_t kMaxQueueSize = 2; |
| 227 | |
| 228 | EmulatedQemuCamera3 *mParent; |
| 229 | Mutex mLock; |
| 230 | |
| 231 | List<Request> mInFlightQueue; |
| 232 | Condition mInFlightSignal; |
| 233 | bool mThreadActive; |
| 234 | |
| 235 | virtual bool threadLoop(); |
| 236 | |
| 237 | // Only accessed by threadLoop. |
| 238 | Request mCurrentRequest; |
| 239 | |
| 240 | Mutex mJpegLock; |
| 241 | bool mJpegWaiting; |
| 242 | camera3_stream_buffer mJpegHalBuffer; |
| 243 | uint32_t mJpegFrameNumber; |
| 244 | |
| 245 | /* |
| 246 | * Jpeg Completion Callbacks |
| 247 | */ |
| 248 | virtual void onJpegDone(const StreamBuffer &jpegBuffer, bool success); |
| 249 | virtual void onJpegInputDone(const StreamBuffer &inputBuffer); |
| 250 | }; |
| 251 | |
| 252 | sp<ReadoutThread> mReadoutThread; |
huans | 8c8cef8 | 2017-11-27 09:36:47 -0800 | [diff] [blame] | 253 | |
| 254 | /** Fake 3A constants */ |
| 255 | |
| 256 | static const nsecs_t kNormalExposureTime; |
| 257 | static const nsecs_t kFacePriorityExposureTime; |
| 258 | static const int kNormalSensitivity; |
| 259 | static const int kFacePrioritySensitivity; |
| 260 | // Rate of converging AE to new target value, as fraction of difference between |
| 261 | // current and target value. |
| 262 | static const float kExposureTrackRate; |
| 263 | // Minimum duration for precapture state. May be longer if slow to converge |
| 264 | // to target exposure |
| 265 | static const int kPrecaptureMinFrames; |
| 266 | // How often to restart AE 'scanning' |
| 267 | static const int kStableAeMaxFrames; |
| 268 | // Maximum stop below 'normal' exposure time that we'll wander to while |
| 269 | // pretending to converge AE. In powers of 2. (-2 == 1/4 as bright) |
| 270 | static const float kExposureWanderMin; |
| 271 | // Maximum stop above 'normal' exposure time that we'll wander to while |
| 272 | // pretending to converge AE. In powers of 2. (2 == 4x as bright) |
| 273 | static const float kExposureWanderMax; |
| 274 | |
| 275 | /** Fake 3A state */ |
| 276 | |
| 277 | uint8_t mControlMode; |
| 278 | bool mFacePriority; |
| 279 | uint8_t mAeState; |
| 280 | uint8_t mAfState; |
| 281 | uint8_t mAwbState; |
| 282 | uint8_t mAeMode; |
| 283 | uint8_t mAfMode; |
| 284 | uint8_t mAwbMode; |
| 285 | |
| 286 | int mAeCounter; |
| 287 | nsecs_t mAeCurrentExposureTime; |
| 288 | nsecs_t mAeTargetExposureTime; |
| 289 | int mAeCurrentSensitivity; |
Logan Weber | e23f777 | 2017-07-24 16:15:12 -0700 | [diff] [blame] | 290 | }; |
| 291 | |
| 292 | }; // end of namespace android |
| 293 | |
| 294 | #endif // HW_EMULATOR_CAMERA_EMULATED_QEMU_CAMERA3_H |