blob: 55819645f590b509f4f63dcb835c3aeaca79225c [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 virtual public hardware::camera::device::V3_5::ICameraDeviceCallback,
135 public camera3::SetErrorInterface,
136 public camera3::InflightRequestUpdateInterface,
137 public camera3::RequestBufferInterface,
138 public camera3::FlushBufferInterface {
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700139 public:
140
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800141 // initialize by Camera3Device.
142 explicit Camera3OfflineSession(const String8& id,
143 const sp<camera3::Camera3Stream>& inputStream,
144 const camera3::StreamSet& offlineStreamSet,
145 camera3::BufferRecords&& bufferRecords,
146 const camera3::InFlightRequestMap& offlineReqs,
147 const Camera3OfflineStates& offlineStates,
148 sp<hardware::camera::device::V3_6::ICameraOfflineSession> offlineSession);
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700149
150 virtual ~Camera3OfflineSession();
151
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800152 virtual status_t initialize(wp<NotificationListener> listener) override;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700153
154 /**
155 * CameraOfflineSessionBase interface
156 */
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700157 status_t disconnect() override;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700158 status_t dump(int fd) override;
159
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800160 /**
161 * FrameProducer interface
162 */
163 const String8& getId() const override;
164 const CameraMetadata& info() const override;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700165 status_t waitForNextFrame(nsecs_t timeout) override;
166 status_t getNextResult(CaptureResult *frame) override;
167
168 // TODO: methods for notification (error/idle/finished etc) passing
169
170 /**
171 * End of CameraOfflineSessionBase interface
172 */
173
174 /**
175 * HIDL ICameraDeviceCallback interface
176 */
177
178 /**
179 * Implementation of android::hardware::camera::device::V3_5::ICameraDeviceCallback
180 */
181
182 hardware::Return<void> processCaptureResult_3_4(
183 const hardware::hidl_vec<
184 hardware::camera::device::V3_4::CaptureResult>& results) override;
185 hardware::Return<void> processCaptureResult(
186 const hardware::hidl_vec<
187 hardware::camera::device::V3_2::CaptureResult>& results) override;
188 hardware::Return<void> notify(
189 const hardware::hidl_vec<
190 hardware::camera::device::V3_2::NotifyMsg>& msgs) override;
191
192 hardware::Return<void> requestStreamBuffers(
193 const hardware::hidl_vec<
194 hardware::camera::device::V3_5::BufferRequest>& bufReqs,
195 requestStreamBuffers_cb _hidl_cb) override;
196
197 hardware::Return<void> returnStreamBuffers(
198 const hardware::hidl_vec<
199 hardware::camera::device::V3_2::StreamBuffer>& buffers) override;
200
201 /**
202 * End of CameraOfflineSessionBase interface
203 */
204
205 private:
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700206 // Camera device ID
207 const String8 mId;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800208 sp<camera3::Camera3Stream> mInputStream;
209 camera3::StreamSet mOutputStreams;
210 camera3::BufferRecords mBufferRecords;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700211 SessionStatsBuilder mSessionStatsBuilder;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700212
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800213 std::mutex mOfflineReqsLock;
214 camera3::InFlightRequestMap mOfflineReqs;
215
216 sp<hardware::camera::device::V3_6::ICameraOfflineSession> mSession;
217
218 TagMonitor mTagMonitor;
219 const metadata_vendor_id_t mVendorTagId;
220
221 const bool mUseHalBufManager;
222 const bool mNeedFixupMonochromeTags;
223
224 const bool mUsePartialResult;
225 const uint32_t mNumPartialResults;
226
227 std::mutex mOutputLock;
Jayant Chowdhary8a0be292020-01-08 13:10:38 -0800228 std::list<CaptureResult> mResultQueue;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800229 std::condition_variable mResultSignal;
Shuzhen Wangb7b42652020-05-07 11:59:02 -0700230 // the last completed frame number of regular requests
231 int64_t mLastCompletedRegularFrameNumber;
232 // the last completed frame number of reprocess requests
233 int64_t mLastCompletedReprocessFrameNumber;
234 // the last completed frame number of ZSL still capture requests
235 int64_t mLastCompletedZslFrameNumber;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800236 // the minimal frame number of the next non-reprocess result
237 uint32_t mNextResultFrameNumber;
238 // the minimal frame number of the next reprocess result
239 uint32_t mNextReprocessResultFrameNumber;
240 // the minimal frame number of the next ZSL still capture result
241 uint32_t mNextZslStillResultFrameNumber;
242 // the minimal frame number of the next non-reprocess shutter
243 uint32_t mNextShutterFrameNumber;
244 // the minimal frame number of the next reprocess shutter
245 uint32_t mNextReprocessShutterFrameNumber;
246 // the minimal frame number of the next ZSL still capture shutter
247 uint32_t mNextZslStillShutterFrameNumber;
248 // End of mOutputLock scope
249
250 const CameraMetadata mDeviceInfo;
251 std::unordered_map<std::string, CameraMetadata> mPhysicalDeviceInfoMap;
252
253 std::unordered_map<std::string, camera3::DistortionMapper> mDistortionMappers;
254
255 std::unordered_map<std::string, camera3::ZoomRatioMapper> mZoomRatioMappers;
256
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800257 std::unordered_map<std::string, camera3::RotateAndCropMapper> mRotateAndCropMappers;
258
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800259 mutable std::mutex mLock;
260
261 enum Status {
262 STATUS_UNINITIALIZED = 0,
263 STATUS_ACTIVE,
264 STATUS_ERROR,
265 STATUS_CLOSED
266 } mStatus;
267
268 wp<NotificationListener> mListener;
269 // End of mLock protect scope
270
271 std::mutex mProcessCaptureResultLock;
272 // FMQ to write result on. Must be guarded by mProcessCaptureResultLock.
273 std::unique_ptr<ResultMetadataQueue> mResultMetadataQueue;
274
275 // Tracking cause of fatal errors when in STATUS_ERROR
276 String8 mErrorCause;
277
278 // Lock to ensure requestStreamBuffers() callbacks are serialized
279 std::mutex mRequestBufferInterfaceLock;
280 // allow request buffer until all requests are processed or disconnectImpl is called
281 bool mAllowRequestBuffer = true;
282
283 // For client methods such as disconnect/dump
284 std::mutex mInterfaceLock;
285
286 // SetErrorInterface
287 void setErrorState(const char *fmt, ...) override;
288 void setErrorStateLocked(const char *fmt, ...) override;
289
290 // InflightRequestUpdateInterface
291 void onInflightEntryRemovedLocked(nsecs_t duration) override;
292 void checkInflightMapLengthLocked() override;
293 void onInflightMapFlushedLocked() override;
294
295 // RequestBufferInterface
296 bool startRequestBuffer() override;
297 void endRequestBuffer() override;
298 nsecs_t getWaitDuration() override;
299
300 // FlushBufferInterface
301 void getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) override;
302 void getInflightRequestBufferKeys(std::vector<uint64_t>* out) override;
303 std::vector<sp<camera3::Camera3StreamInterface>> getAllStreams() override;
304
305 void setErrorStateLockedV(const char *fmt, va_list args);
306
307 status_t disconnectImpl();
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700308}; // class Camera3OfflineSession
309
310}; // namespace android
311
312#endif