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 <utils/String8.h> 24 #include <utils/String16.h> 25 26 #include <android/hardware/camera/device/3.6/ICameraOfflineSession.h> 27 28 #include <fmq/MessageQueue.h> 29 30 #include "common/CameraOfflineSessionBase.h" 31 32 #include "device3/Camera3BufferManager.h" 33 #include "device3/DistortionMapper.h" 34 #include "device3/InFlightRequest.h" 35 #include "device3/Camera3OutputUtils.h" 36 #include "device3/RotateAndCropMapper.h" 37 #include "device3/ZoomRatioMapper.h" 38 #include "utils/TagMonitor.h" 39 #include <camera_metadata_hidden.h> 40 41 namespace android { 42 43 namespace camera3 { 44 45 class Camera3Stream; 46 class Camera3OutputStreamInterface; 47 class Camera3StreamInterface; 48 49 } // namespace camera3 50 51 52 // An immutable struct containing general states that will be copied from Camera3Device to 53 // Camera3OfflineSession 54 struct Camera3OfflineStates { Camera3OfflineStatesCamera3OfflineStates55 Camera3OfflineStates( 56 const TagMonitor& tagMonitor, const metadata_vendor_id_t vendorTagId, 57 const bool useHalBufManager, const bool needFixupMonochromeTags, 58 const bool usePartialResult, const uint32_t numPartialResults, 59 const int64_t lastCompletedRegularFN, const int64_t lastCompletedReprocessFN, 60 const int64_t lastCompletedZslFN, const uint32_t nextResultFN, 61 const uint32_t nextReprocResultFN, const uint32_t nextZslResultFN, 62 const uint32_t nextShutterFN, const uint32_t nextReprocShutterFN, 63 const uint32_t nextZslShutterFN, const CameraMetadata& deviceInfo, 64 const std::unordered_map<std::string, CameraMetadata>& physicalDeviceInfoMap, 65 const std::unordered_map<std::string, camera3::DistortionMapper>& distortionMappers, 66 const std::unordered_map<std::string, camera3::ZoomRatioMapper>& zoomRatioMappers, 67 const std::unordered_map<std::string, camera3::RotateAndCropMapper>& 68 rotateAndCropMappers) : 69 mTagMonitor(tagMonitor), mVendorTagId(vendorTagId), 70 mUseHalBufManager(useHalBufManager), mNeedFixupMonochromeTags(needFixupMonochromeTags), 71 mUsePartialResult(usePartialResult), mNumPartialResults(numPartialResults), 72 mLastCompletedRegularFrameNumber(lastCompletedRegularFN), 73 mLastCompletedReprocessFrameNumber(lastCompletedReprocessFN), 74 mLastCompletedZslFrameNumber(lastCompletedZslFN), 75 mNextResultFrameNumber(nextResultFN), 76 mNextReprocessResultFrameNumber(nextReprocResultFN), 77 mNextZslStillResultFrameNumber(nextZslResultFN), 78 mNextShutterFrameNumber(nextShutterFN), 79 mNextReprocessShutterFrameNumber(nextReprocShutterFN), 80 mNextZslStillShutterFrameNumber(nextZslShutterFN), 81 mDeviceInfo(deviceInfo), 82 mPhysicalDeviceInfoMap(physicalDeviceInfoMap), 83 mDistortionMappers(distortionMappers), 84 mZoomRatioMappers(zoomRatioMappers), 85 mRotateAndCropMappers(rotateAndCropMappers) {} 86 87 const TagMonitor& mTagMonitor; 88 const metadata_vendor_id_t mVendorTagId; 89 90 const bool mUseHalBufManager; 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 String8& 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 String8& 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 String8 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 bool mNeedFixupMonochromeTags; 188 189 const bool mUsePartialResult; 190 const uint32_t mNumPartialResults; 191 192 std::mutex mOutputLock; 193 std::list<CaptureResult> mResultQueue; 194 std::condition_variable mResultSignal; 195 // the last completed frame number of regular requests 196 int64_t mLastCompletedRegularFrameNumber; 197 // the last completed frame number of reprocess requests 198 int64_t mLastCompletedReprocessFrameNumber; 199 // the last completed frame number of ZSL still capture requests 200 int64_t mLastCompletedZslFrameNumber; 201 // the minimal frame number of the next non-reprocess result 202 uint32_t mNextResultFrameNumber; 203 // the minimal frame number of the next reprocess result 204 uint32_t mNextReprocessResultFrameNumber; 205 // the minimal frame number of the next ZSL still capture result 206 uint32_t mNextZslStillResultFrameNumber; 207 // the minimal frame number of the next non-reprocess shutter 208 uint32_t mNextShutterFrameNumber; 209 // the minimal frame number of the next reprocess shutter 210 uint32_t mNextReprocessShutterFrameNumber; 211 // the minimal frame number of the next ZSL still capture shutter 212 uint32_t mNextZslStillShutterFrameNumber; 213 // End of mOutputLock scope 214 215 const CameraMetadata mDeviceInfo; 216 std::unordered_map<std::string, CameraMetadata> mPhysicalDeviceInfoMap; 217 218 std::unordered_map<std::string, camera3::DistortionMapper> mDistortionMappers; 219 220 std::unordered_map<std::string, camera3::ZoomRatioMapper> mZoomRatioMappers; 221 222 std::unordered_map<std::string, camera3::RotateAndCropMapper> mRotateAndCropMappers; 223 224 mutable std::mutex mLock; 225 226 enum Status { 227 STATUS_UNINITIALIZED = 0, 228 STATUS_ACTIVE, 229 STATUS_ERROR, 230 STATUS_CLOSED 231 } mStatus; 232 233 wp<NotificationListener> mListener; 234 // End of mLock protect scope 235 236 std::mutex mProcessCaptureResultLock; 237 238 // Tracking cause of fatal errors when in STATUS_ERROR 239 String8 mErrorCause; 240 241 // Lock to ensure requestStreamBuffers() callbacks are serialized 242 std::mutex mRequestBufferInterfaceLock; 243 // allow request buffer until all requests are processed or disconnectImpl is called 244 bool mAllowRequestBuffer = true; 245 246 // For client methods such as disconnect/dump 247 std::mutex mInterfaceLock; 248 249 // The current minimum expected frame duration based on AE_TARGET_FPS_RANGE 250 nsecs_t mMinExpectedDuration = 0; 251 // Whether the camera device runs at fixed frame rate based on AE_MODE and 252 // AE_TARGET_FPS_RANGE 253 bool mIsFixedFps = false; 254 255 // SetErrorInterface 256 void setErrorState(const char *fmt, ...) override; 257 void setErrorStateLocked(const char *fmt, ...) override; 258 259 // InflightRequestUpdateInterface 260 void onInflightEntryRemovedLocked(nsecs_t duration) override; 261 void checkInflightMapLengthLocked() override; 262 void onInflightMapFlushedLocked() override; 263 264 // RequestBufferInterface 265 bool startRequestBuffer() override; 266 void endRequestBuffer() override; 267 nsecs_t getWaitDuration() override; 268 269 // FlushBufferInterface 270 void getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) override; 271 void getInflightRequestBufferKeys(std::vector<uint64_t>* out) override; 272 std::vector<sp<camera3::Camera3StreamInterface>> getAllStreams() override; 273 274 void setErrorStateLockedV(const char *fmt, va_list args); 275 276 status_t disconnectImpl(); 277 virtual void disconnectSession() = 0; 278 279 }; // class Camera3OfflineSession 280 281 }; // namespace android 282 283 #endif 284