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