Yin-Chia Yeh | b978c38 | 2019-10-30 00:22:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 ANDROID_SERVERS_CAMERA3OFFLINESESSION_H |
| 18 | #define ANDROID_SERVERS_CAMERA3OFFLINESESSION_H |
| 19 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 20 | #include <memory> |
| 21 | #include <mutex> |
| 22 | |
Yin-Chia Yeh | b978c38 | 2019-10-30 00:22:37 -0700 | [diff] [blame] | 23 | #include <android/hardware/camera/device/3.6/ICameraOfflineSession.h> |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 24 | |
Yin-Chia Yeh | b978c38 | 2019-10-30 00:22:37 -0700 | [diff] [blame] | 25 | #include <fmq/MessageQueue.h> |
| 26 | |
| 27 | #include "common/CameraOfflineSessionBase.h" |
| 28 | |
| 29 | #include "device3/Camera3BufferManager.h" |
| 30 | #include "device3/DistortionMapper.h" |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 31 | #include "device3/InFlightRequest.h" |
| 32 | #include "device3/Camera3OutputUtils.h" |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 33 | #include "device3/RotateAndCropMapper.h" |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 34 | #include "device3/ZoomRatioMapper.h" |
Yin-Chia Yeh | b978c38 | 2019-10-30 00:22:37 -0700 | [diff] [blame] | 35 | #include "utils/TagMonitor.h" |
Yin-Chia Yeh | b978c38 | 2019-10-30 00:22:37 -0700 | [diff] [blame] | 36 | #include <camera_metadata_hidden.h> |
| 37 | |
| 38 | namespace android { |
| 39 | |
| 40 | namespace camera3 { |
| 41 | |
| 42 | class Camera3Stream; |
| 43 | class Camera3OutputStreamInterface; |
| 44 | class Camera3StreamInterface; |
| 45 | |
| 46 | } // namespace camera3 |
| 47 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 48 | |
| 49 | // An immutable struct containing general states that will be copied from Camera3Device to |
| 50 | // Camera3OfflineSession |
| 51 | struct Camera3OfflineStates { |
| 52 | Camera3OfflineStates( |
| 53 | const TagMonitor& tagMonitor, const metadata_vendor_id_t vendorTagId, |
| 54 | const bool useHalBufManager, const bool needFixupMonochromeTags, |
| 55 | const bool usePartialResult, const uint32_t numPartialResults, |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 56 | const int64_t lastCompletedRegularFN, const int64_t lastCompletedReprocessFN, |
| 57 | const int64_t lastCompletedZslFN, const uint32_t nextResultFN, |
| 58 | const uint32_t nextReprocResultFN, const uint32_t nextZslResultFN, |
| 59 | const uint32_t nextShutterFN, const uint32_t nextReprocShutterFN, |
| 60 | const uint32_t nextZslShutterFN, const CameraMetadata& deviceInfo, |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 61 | const std::unordered_map<std::string, CameraMetadata>& physicalDeviceInfoMap, |
| 62 | const std::unordered_map<std::string, camera3::DistortionMapper>& distortionMappers, |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 63 | const std::unordered_map<std::string, camera3::ZoomRatioMapper>& zoomRatioMappers, |
| 64 | const std::unordered_map<std::string, camera3::RotateAndCropMapper>& |
| 65 | rotateAndCropMappers) : |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 66 | mTagMonitor(tagMonitor), mVendorTagId(vendorTagId), |
| 67 | mUseHalBufManager(useHalBufManager), mNeedFixupMonochromeTags(needFixupMonochromeTags), |
| 68 | mUsePartialResult(usePartialResult), mNumPartialResults(numPartialResults), |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 69 | mLastCompletedRegularFrameNumber(lastCompletedRegularFN), |
| 70 | mLastCompletedReprocessFrameNumber(lastCompletedReprocessFN), |
| 71 | mLastCompletedZslFrameNumber(lastCompletedZslFN), |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 72 | mNextResultFrameNumber(nextResultFN), |
| 73 | mNextReprocessResultFrameNumber(nextReprocResultFN), |
| 74 | mNextZslStillResultFrameNumber(nextZslResultFN), |
| 75 | mNextShutterFrameNumber(nextShutterFN), |
| 76 | mNextReprocessShutterFrameNumber(nextReprocShutterFN), |
| 77 | mNextZslStillShutterFrameNumber(nextZslShutterFN), |
| 78 | mDeviceInfo(deviceInfo), |
| 79 | mPhysicalDeviceInfoMap(physicalDeviceInfoMap), |
| 80 | mDistortionMappers(distortionMappers), |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 81 | mZoomRatioMappers(zoomRatioMappers), |
| 82 | mRotateAndCropMappers(rotateAndCropMappers) {} |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 83 | |
| 84 | const TagMonitor& mTagMonitor; |
| 85 | const metadata_vendor_id_t mVendorTagId; |
| 86 | |
| 87 | const bool mUseHalBufManager; |
| 88 | const bool mNeedFixupMonochromeTags; |
| 89 | |
| 90 | const bool mUsePartialResult; |
| 91 | const uint32_t mNumPartialResults; |
| 92 | |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 93 | // The last completed (buffers, result metadata, and error notify) regular |
| 94 | // request frame number |
| 95 | const int64_t mLastCompletedRegularFrameNumber; |
| 96 | // The last completed (buffers, result metadata, and error notify) reprocess |
| 97 | // request frame number |
| 98 | const int64_t mLastCompletedReprocessFrameNumber; |
| 99 | // The last completed (buffers, result metadata, and error notify) zsl |
| 100 | // request frame number |
| 101 | const int64_t mLastCompletedZslFrameNumber; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 102 | // the minimal frame number of the next non-reprocess result |
| 103 | const uint32_t mNextResultFrameNumber; |
| 104 | // the minimal frame number of the next reprocess result |
| 105 | const uint32_t mNextReprocessResultFrameNumber; |
| 106 | // the minimal frame number of the next ZSL still capture result |
| 107 | const uint32_t mNextZslStillResultFrameNumber; |
| 108 | // the minimal frame number of the next non-reprocess shutter |
| 109 | const uint32_t mNextShutterFrameNumber; |
| 110 | // the minimal frame number of the next reprocess shutter |
| 111 | const uint32_t mNextReprocessShutterFrameNumber; |
| 112 | // the minimal frame number of the next ZSL still capture shutter |
| 113 | const uint32_t mNextZslStillShutterFrameNumber; |
| 114 | |
| 115 | const CameraMetadata& mDeviceInfo; |
| 116 | |
| 117 | const std::unordered_map<std::string, CameraMetadata>& mPhysicalDeviceInfoMap; |
| 118 | |
| 119 | const std::unordered_map<std::string, camera3::DistortionMapper>& mDistortionMappers; |
| 120 | |
| 121 | const std::unordered_map<std::string, camera3::ZoomRatioMapper>& mZoomRatioMappers; |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 122 | |
| 123 | const std::unordered_map<std::string, camera3::RotateAndCropMapper>& mRotateAndCropMappers; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 124 | }; |
| 125 | |
Yin-Chia Yeh | b978c38 | 2019-10-30 00:22:37 -0700 | [diff] [blame] | 126 | /** |
| 127 | * Camera3OfflineSession for offline session defined in HIDL ICameraOfflineSession@3.6 or higher |
| 128 | */ |
| 129 | class Camera3OfflineSession : |
| 130 | public CameraOfflineSessionBase, |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 131 | public camera3::SetErrorInterface, |
| 132 | public camera3::InflightRequestUpdateInterface, |
| 133 | public camera3::RequestBufferInterface, |
| 134 | public camera3::FlushBufferInterface { |
Yin-Chia Yeh | b978c38 | 2019-10-30 00:22:37 -0700 | [diff] [blame] | 135 | public: |
| 136 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 137 | // initialize by Camera3Device. |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 138 | explicit Camera3OfflineSession(const std::string& id, |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 139 | const sp<camera3::Camera3Stream>& inputStream, |
| 140 | const camera3::StreamSet& offlineStreamSet, |
| 141 | camera3::BufferRecords&& bufferRecords, |
| 142 | const camera3::InFlightRequestMap& offlineReqs, |
Jayant Chowdhary | 22441f3 | 2021-12-26 18:35:41 -0800 | [diff] [blame] | 143 | const Camera3OfflineStates& offlineStates); |
Yin-Chia Yeh | b978c38 | 2019-10-30 00:22:37 -0700 | [diff] [blame] | 144 | |
| 145 | virtual ~Camera3OfflineSession(); |
| 146 | |
Jayant Chowdhary | 22441f3 | 2021-12-26 18:35:41 -0800 | [diff] [blame] | 147 | virtual status_t initialize(wp<NotificationListener> /*listener*/) = 0; |
Yin-Chia Yeh | b978c38 | 2019-10-30 00:22:37 -0700 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * CameraOfflineSessionBase interface |
| 151 | */ |
Yin-Chia Yeh | b978c38 | 2019-10-30 00:22:37 -0700 | [diff] [blame] | 152 | status_t disconnect() override; |
Yin-Chia Yeh | b978c38 | 2019-10-30 00:22:37 -0700 | [diff] [blame] | 153 | status_t dump(int fd) override; |
| 154 | |
Emilian Peev | faa4bde | 2020-01-23 12:19:37 -0800 | [diff] [blame] | 155 | /** |
| 156 | * FrameProducer interface |
| 157 | */ |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 158 | const std::string& getId() const override; |
Emilian Peev | faa4bde | 2020-01-23 12:19:37 -0800 | [diff] [blame] | 159 | const CameraMetadata& info() const override; |
Yin-Chia Yeh | b978c38 | 2019-10-30 00:22:37 -0700 | [diff] [blame] | 160 | status_t waitForNextFrame(nsecs_t timeout) override; |
| 161 | status_t getNextResult(CaptureResult *frame) override; |
| 162 | |
| 163 | // TODO: methods for notification (error/idle/finished etc) passing |
| 164 | |
| 165 | /** |
| 166 | * End of CameraOfflineSessionBase interface |
| 167 | */ |
| 168 | |
Jayant Chowdhary | 22441f3 | 2021-12-26 18:35:41 -0800 | [diff] [blame] | 169 | protected: |
Yin-Chia Yeh | b978c38 | 2019-10-30 00:22:37 -0700 | [diff] [blame] | 170 | // Camera device ID |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 171 | const std::string mId; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 172 | sp<camera3::Camera3Stream> mInputStream; |
| 173 | camera3::StreamSet mOutputStreams; |
| 174 | camera3::BufferRecords mBufferRecords; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 175 | SessionStatsBuilder mSessionStatsBuilder; |
Yin-Chia Yeh | b978c38 | 2019-10-30 00:22:37 -0700 | [diff] [blame] | 176 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 177 | std::mutex mOfflineReqsLock; |
| 178 | camera3::InFlightRequestMap mOfflineReqs; |
| 179 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 180 | TagMonitor mTagMonitor; |
| 181 | const metadata_vendor_id_t mVendorTagId; |
| 182 | |
| 183 | const bool mUseHalBufManager; |
| 184 | const bool mNeedFixupMonochromeTags; |
| 185 | |
| 186 | const bool mUsePartialResult; |
| 187 | const uint32_t mNumPartialResults; |
| 188 | |
| 189 | std::mutex mOutputLock; |
Jayant Chowdhary | 8a0be29 | 2020-01-08 13:10:38 -0800 | [diff] [blame] | 190 | std::list<CaptureResult> mResultQueue; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 191 | std::condition_variable mResultSignal; |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 192 | // the last completed frame number of regular requests |
| 193 | int64_t mLastCompletedRegularFrameNumber; |
| 194 | // the last completed frame number of reprocess requests |
| 195 | int64_t mLastCompletedReprocessFrameNumber; |
| 196 | // the last completed frame number of ZSL still capture requests |
| 197 | int64_t mLastCompletedZslFrameNumber; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 198 | // the minimal frame number of the next non-reprocess result |
| 199 | uint32_t mNextResultFrameNumber; |
| 200 | // the minimal frame number of the next reprocess result |
| 201 | uint32_t mNextReprocessResultFrameNumber; |
| 202 | // the minimal frame number of the next ZSL still capture result |
| 203 | uint32_t mNextZslStillResultFrameNumber; |
| 204 | // the minimal frame number of the next non-reprocess shutter |
| 205 | uint32_t mNextShutterFrameNumber; |
| 206 | // the minimal frame number of the next reprocess shutter |
| 207 | uint32_t mNextReprocessShutterFrameNumber; |
| 208 | // the minimal frame number of the next ZSL still capture shutter |
| 209 | uint32_t mNextZslStillShutterFrameNumber; |
| 210 | // End of mOutputLock scope |
| 211 | |
| 212 | const CameraMetadata mDeviceInfo; |
| 213 | std::unordered_map<std::string, CameraMetadata> mPhysicalDeviceInfoMap; |
| 214 | |
| 215 | std::unordered_map<std::string, camera3::DistortionMapper> mDistortionMappers; |
| 216 | |
| 217 | std::unordered_map<std::string, camera3::ZoomRatioMapper> mZoomRatioMappers; |
| 218 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 219 | std::unordered_map<std::string, camera3::RotateAndCropMapper> mRotateAndCropMappers; |
| 220 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 221 | mutable std::mutex mLock; |
| 222 | |
| 223 | enum Status { |
| 224 | STATUS_UNINITIALIZED = 0, |
| 225 | STATUS_ACTIVE, |
| 226 | STATUS_ERROR, |
| 227 | STATUS_CLOSED |
| 228 | } mStatus; |
| 229 | |
| 230 | wp<NotificationListener> mListener; |
| 231 | // End of mLock protect scope |
| 232 | |
| 233 | std::mutex mProcessCaptureResultLock; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 234 | |
| 235 | // Tracking cause of fatal errors when in STATUS_ERROR |
Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 236 | std::string mErrorCause; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 237 | |
| 238 | // Lock to ensure requestStreamBuffers() callbacks are serialized |
| 239 | std::mutex mRequestBufferInterfaceLock; |
| 240 | // allow request buffer until all requests are processed or disconnectImpl is called |
| 241 | bool mAllowRequestBuffer = true; |
| 242 | |
| 243 | // For client methods such as disconnect/dump |
| 244 | std::mutex mInterfaceLock; |
| 245 | |
Shuzhen Wang | 00abbeb | 2022-02-25 17:14:42 -0800 | [diff] [blame] | 246 | // The current minimum expected frame duration based on AE_TARGET_FPS_RANGE |
| 247 | nsecs_t mMinExpectedDuration = 0; |
Shuzhen Wang | 696e4da | 2022-09-08 14:31:13 -0700 | [diff] [blame] | 248 | // Whether the camera device runs at fixed frame rate based on AE_MODE and |
| 249 | // AE_TARGET_FPS_RANGE |
| 250 | bool mIsFixedFps = false; |
Shuzhen Wang | 00abbeb | 2022-02-25 17:14:42 -0800 | [diff] [blame] | 251 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 252 | // SetErrorInterface |
| 253 | void setErrorState(const char *fmt, ...) override; |
| 254 | void setErrorStateLocked(const char *fmt, ...) override; |
| 255 | |
| 256 | // InflightRequestUpdateInterface |
| 257 | void onInflightEntryRemovedLocked(nsecs_t duration) override; |
| 258 | void checkInflightMapLengthLocked() override; |
| 259 | void onInflightMapFlushedLocked() override; |
| 260 | |
| 261 | // RequestBufferInterface |
| 262 | bool startRequestBuffer() override; |
| 263 | void endRequestBuffer() override; |
| 264 | nsecs_t getWaitDuration() override; |
| 265 | |
| 266 | // FlushBufferInterface |
| 267 | void getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) override; |
| 268 | void getInflightRequestBufferKeys(std::vector<uint64_t>* out) override; |
| 269 | std::vector<sp<camera3::Camera3StreamInterface>> getAllStreams() override; |
| 270 | |
| 271 | void setErrorStateLockedV(const char *fmt, va_list args); |
| 272 | |
| 273 | status_t disconnectImpl(); |
Emilian Peev | 886ac21 | 2023-02-07 14:10:24 -0800 | [diff] [blame] | 274 | |
| 275 | // Clients need to ensure that 'mInterfaceLock' is acquired before calling this method |
| 276 | virtual void closeSessionLocked() = 0; |
| 277 | |
| 278 | // Clients need to ensure that 'mLock' is acquired before calling this method |
| 279 | virtual void releaseSessionLocked() = 0; |
Jayant Chowdhary | 22441f3 | 2021-12-26 18:35:41 -0800 | [diff] [blame] | 280 | |
Yin-Chia Yeh | b978c38 | 2019-10-30 00:22:37 -0700 | [diff] [blame] | 281 | }; // class Camera3OfflineSession |
| 282 | |
| 283 | }; // namespace android |
| 284 | |
| 285 | #endif |