blob: 0f7d14526ef0b6b8ebf0daf0cbd0967d4c7f629e [file] [log] [blame]
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001/*
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 Yeh5fd603e2019-11-20 11:22:27 -080020#include <memory>
21#include <mutex>
22
Yin-Chia Yehb978c382019-10-30 00:22:37 -070023#include <utils/String8.h>
24#include <utils/String16.h>
25
26#include <android/hardware/camera/device/3.6/ICameraOfflineSession.h>
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080027
Yin-Chia Yehb978c382019-10-30 00:22:37 -070028#include <fmq/MessageQueue.h>
29
30#include "common/CameraOfflineSessionBase.h"
31
32#include "device3/Camera3BufferManager.h"
33#include "device3/DistortionMapper.h"
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080034#include "device3/InFlightRequest.h"
35#include "device3/Camera3OutputUtils.h"
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -080036#include "device3/RotateAndCropMapper.h"
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080037#include "device3/ZoomRatioMapper.h"
Yin-Chia Yehb978c382019-10-30 00:22:37 -070038#include "utils/TagMonitor.h"
Yin-Chia Yehb978c382019-10-30 00:22:37 -070039#include <camera_metadata_hidden.h>
40
41namespace android {
42
43namespace camera3 {
44
45class Camera3Stream;
46class Camera3OutputStreamInterface;
47class Camera3StreamInterface;
48
49} // namespace camera3
50
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080051
52// An immutable struct containing general states that will be copied from Camera3Device to
53// Camera3OfflineSession
54struct Camera3OfflineStates {
55 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,
Shuzhen Wangb7b42652020-05-07 11:59:02 -070059 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,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080064 const std::unordered_map<std::string, CameraMetadata>& physicalDeviceInfoMap,
65 const std::unordered_map<std::string, camera3::DistortionMapper>& distortionMappers,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -080066 const std::unordered_map<std::string, camera3::ZoomRatioMapper>& zoomRatioMappers,
67 const std::unordered_map<std::string, camera3::RotateAndCropMapper>&
68 rotateAndCropMappers) :
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080069 mTagMonitor(tagMonitor), mVendorTagId(vendorTagId),
70 mUseHalBufManager(useHalBufManager), mNeedFixupMonochromeTags(needFixupMonochromeTags),
71 mUsePartialResult(usePartialResult), mNumPartialResults(numPartialResults),
Shuzhen Wangb7b42652020-05-07 11:59:02 -070072 mLastCompletedRegularFrameNumber(lastCompletedRegularFN),
73 mLastCompletedReprocessFrameNumber(lastCompletedReprocessFN),
74 mLastCompletedZslFrameNumber(lastCompletedZslFN),
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080075 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),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -080084 mZoomRatioMappers(zoomRatioMappers),
85 mRotateAndCropMappers(rotateAndCropMappers) {}
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080086
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
Shuzhen Wangb7b42652020-05-07 11:59:02 -070096 // 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;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800105 // 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;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800125
126 const std::unordered_map<std::string, camera3::RotateAndCropMapper>& mRotateAndCropMappers;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800127};
128
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700129/**
130 * Camera3OfflineSession for offline session defined in HIDL ICameraOfflineSession@3.6 or higher
131 */
132class Camera3OfflineSession :
133 public CameraOfflineSessionBase,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800134 public camera3::SetErrorInterface,
135 public camera3::InflightRequestUpdateInterface,
136 public camera3::RequestBufferInterface,
137 public camera3::FlushBufferInterface {
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700138 public:
139
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800140 // 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,
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800146 const Camera3OfflineStates& offlineStates);
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700147
148 virtual ~Camera3OfflineSession();
149
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800150 virtual status_t initialize(wp<NotificationListener> /*listener*/) = 0;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700151
152 /**
153 * CameraOfflineSessionBase interface
154 */
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700155 status_t disconnect() override;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700156 status_t dump(int fd) override;
157
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800158 /**
159 * FrameProducer interface
160 */
161 const String8& getId() const override;
162 const CameraMetadata& info() const override;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700163 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
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800172 protected:
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700173 // Camera device ID
174 const String8 mId;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800175 sp<camera3::Camera3Stream> mInputStream;
176 camera3::StreamSet mOutputStreams;
177 camera3::BufferRecords mBufferRecords;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700178 SessionStatsBuilder mSessionStatsBuilder;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700179
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800180 std::mutex mOfflineReqsLock;
181 camera3::InFlightRequestMap mOfflineReqs;
182
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800183 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;
Jayant Chowdhary8a0be292020-01-08 13:10:38 -0800193 std::list<CaptureResult> mResultQueue;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800194 std::condition_variable mResultSignal;
Shuzhen Wangb7b42652020-05-07 11:59:02 -0700195 // 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;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800201 // 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
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800222 std::unordered_map<std::string, camera3::RotateAndCropMapper> mRotateAndCropMappers;
223
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800224 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;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800237
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 // SetErrorInterface
250 void setErrorState(const char *fmt, ...) override;
251 void setErrorStateLocked(const char *fmt, ...) override;
252
253 // InflightRequestUpdateInterface
254 void onInflightEntryRemovedLocked(nsecs_t duration) override;
255 void checkInflightMapLengthLocked() override;
256 void onInflightMapFlushedLocked() override;
257
258 // RequestBufferInterface
259 bool startRequestBuffer() override;
260 void endRequestBuffer() override;
261 nsecs_t getWaitDuration() override;
262
263 // FlushBufferInterface
264 void getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) override;
265 void getInflightRequestBufferKeys(std::vector<uint64_t>* out) override;
266 std::vector<sp<camera3::Camera3StreamInterface>> getAllStreams() override;
267
268 void setErrorStateLockedV(const char *fmt, va_list args);
269
270 status_t disconnectImpl();
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800271 virtual void disconnectSession() = 0;
272
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700273}; // class Camera3OfflineSession
274
275}; // namespace android
276
277#endif