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