blob: c4c7a85a49a26a298364f81ac361e0a216f1da6e [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"
39#include "utils/LatencyHistogram.h"
40#include <camera_metadata_hidden.h>
41
42namespace android {
43
44namespace camera3 {
45
46class Camera3Stream;
47class Camera3OutputStreamInterface;
48class Camera3StreamInterface;
49
50} // namespace camera3
51
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080052
53// An immutable struct containing general states that will be copied from Camera3Device to
54// Camera3OfflineSession
55struct Camera3OfflineStates {
56 Camera3OfflineStates(
57 const TagMonitor& tagMonitor, const metadata_vendor_id_t vendorTagId,
58 const bool useHalBufManager, const bool needFixupMonochromeTags,
59 const bool usePartialResult, const uint32_t numPartialResults,
Shuzhen Wangb7b42652020-05-07 11:59:02 -070060 const int64_t lastCompletedRegularFN, const int64_t lastCompletedReprocessFN,
61 const int64_t lastCompletedZslFN, const uint32_t nextResultFN,
62 const uint32_t nextReprocResultFN, const uint32_t nextZslResultFN,
63 const uint32_t nextShutterFN, const uint32_t nextReprocShutterFN,
64 const uint32_t nextZslShutterFN, const CameraMetadata& deviceInfo,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080065 const std::unordered_map<std::string, CameraMetadata>& physicalDeviceInfoMap,
66 const std::unordered_map<std::string, camera3::DistortionMapper>& distortionMappers,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -080067 const std::unordered_map<std::string, camera3::ZoomRatioMapper>& zoomRatioMappers,
68 const std::unordered_map<std::string, camera3::RotateAndCropMapper>&
69 rotateAndCropMappers) :
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080070 mTagMonitor(tagMonitor), mVendorTagId(vendorTagId),
71 mUseHalBufManager(useHalBufManager), mNeedFixupMonochromeTags(needFixupMonochromeTags),
72 mUsePartialResult(usePartialResult), mNumPartialResults(numPartialResults),
Shuzhen Wangb7b42652020-05-07 11:59:02 -070073 mLastCompletedRegularFrameNumber(lastCompletedRegularFN),
74 mLastCompletedReprocessFrameNumber(lastCompletedReprocessFN),
75 mLastCompletedZslFrameNumber(lastCompletedZslFN),
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080076 mNextResultFrameNumber(nextResultFN),
77 mNextReprocessResultFrameNumber(nextReprocResultFN),
78 mNextZslStillResultFrameNumber(nextZslResultFN),
79 mNextShutterFrameNumber(nextShutterFN),
80 mNextReprocessShutterFrameNumber(nextReprocShutterFN),
81 mNextZslStillShutterFrameNumber(nextZslShutterFN),
82 mDeviceInfo(deviceInfo),
83 mPhysicalDeviceInfoMap(physicalDeviceInfoMap),
84 mDistortionMappers(distortionMappers),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -080085 mZoomRatioMappers(zoomRatioMappers),
86 mRotateAndCropMappers(rotateAndCropMappers) {}
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080087
88 const TagMonitor& mTagMonitor;
89 const metadata_vendor_id_t mVendorTagId;
90
91 const bool mUseHalBufManager;
92 const bool mNeedFixupMonochromeTags;
93
94 const bool mUsePartialResult;
95 const uint32_t mNumPartialResults;
96
Shuzhen Wangb7b42652020-05-07 11:59:02 -070097 // The last completed (buffers, result metadata, and error notify) regular
98 // request frame number
99 const int64_t mLastCompletedRegularFrameNumber;
100 // The last completed (buffers, result metadata, and error notify) reprocess
101 // request frame number
102 const int64_t mLastCompletedReprocessFrameNumber;
103 // The last completed (buffers, result metadata, and error notify) zsl
104 // request frame number
105 const int64_t mLastCompletedZslFrameNumber;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800106 // the minimal frame number of the next non-reprocess result
107 const uint32_t mNextResultFrameNumber;
108 // the minimal frame number of the next reprocess result
109 const uint32_t mNextReprocessResultFrameNumber;
110 // the minimal frame number of the next ZSL still capture result
111 const uint32_t mNextZslStillResultFrameNumber;
112 // the minimal frame number of the next non-reprocess shutter
113 const uint32_t mNextShutterFrameNumber;
114 // the minimal frame number of the next reprocess shutter
115 const uint32_t mNextReprocessShutterFrameNumber;
116 // the minimal frame number of the next ZSL still capture shutter
117 const uint32_t mNextZslStillShutterFrameNumber;
118
119 const CameraMetadata& mDeviceInfo;
120
121 const std::unordered_map<std::string, CameraMetadata>& mPhysicalDeviceInfoMap;
122
123 const std::unordered_map<std::string, camera3::DistortionMapper>& mDistortionMappers;
124
125 const std::unordered_map<std::string, camera3::ZoomRatioMapper>& mZoomRatioMappers;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800126
127 const std::unordered_map<std::string, camera3::RotateAndCropMapper>& mRotateAndCropMappers;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800128};
129
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700130/**
131 * Camera3OfflineSession for offline session defined in HIDL ICameraOfflineSession@3.6 or higher
132 */
133class Camera3OfflineSession :
134 public CameraOfflineSessionBase,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800135 virtual public hardware::camera::device::V3_5::ICameraDeviceCallback,
136 public camera3::SetErrorInterface,
137 public camera3::InflightRequestUpdateInterface,
138 public camera3::RequestBufferInterface,
139 public camera3::FlushBufferInterface {
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700140 public:
141
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800142 // initialize by Camera3Device.
143 explicit Camera3OfflineSession(const String8& id,
144 const sp<camera3::Camera3Stream>& inputStream,
145 const camera3::StreamSet& offlineStreamSet,
146 camera3::BufferRecords&& bufferRecords,
147 const camera3::InFlightRequestMap& offlineReqs,
148 const Camera3OfflineStates& offlineStates,
149 sp<hardware::camera::device::V3_6::ICameraOfflineSession> offlineSession);
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700150
151 virtual ~Camera3OfflineSession();
152
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800153 virtual status_t initialize(wp<NotificationListener> listener) override;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700154
155 /**
156 * CameraOfflineSessionBase interface
157 */
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700158 status_t disconnect() override;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700159 status_t dump(int fd) override;
160
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800161 /**
162 * FrameProducer interface
163 */
164 const String8& getId() const override;
165 const CameraMetadata& info() const override;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700166 status_t waitForNextFrame(nsecs_t timeout) override;
167 status_t getNextResult(CaptureResult *frame) override;
168
169 // TODO: methods for notification (error/idle/finished etc) passing
170
171 /**
172 * End of CameraOfflineSessionBase interface
173 */
174
175 /**
176 * HIDL ICameraDeviceCallback interface
177 */
178
179 /**
180 * Implementation of android::hardware::camera::device::V3_5::ICameraDeviceCallback
181 */
182
183 hardware::Return<void> processCaptureResult_3_4(
184 const hardware::hidl_vec<
185 hardware::camera::device::V3_4::CaptureResult>& results) override;
186 hardware::Return<void> processCaptureResult(
187 const hardware::hidl_vec<
188 hardware::camera::device::V3_2::CaptureResult>& results) override;
189 hardware::Return<void> notify(
190 const hardware::hidl_vec<
191 hardware::camera::device::V3_2::NotifyMsg>& msgs) override;
192
193 hardware::Return<void> requestStreamBuffers(
194 const hardware::hidl_vec<
195 hardware::camera::device::V3_5::BufferRequest>& bufReqs,
196 requestStreamBuffers_cb _hidl_cb) override;
197
198 hardware::Return<void> returnStreamBuffers(
199 const hardware::hidl_vec<
200 hardware::camera::device::V3_2::StreamBuffer>& buffers) override;
201
202 /**
203 * End of CameraOfflineSessionBase interface
204 */
205
206 private:
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700207 // Camera device ID
208 const String8 mId;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800209 sp<camera3::Camera3Stream> mInputStream;
210 camera3::StreamSet mOutputStreams;
211 camera3::BufferRecords mBufferRecords;
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