blob: 969220f3816dfb87938e81985f5aeef6c6390aa7 [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,
60 const uint32_t nextResultFN, const uint32_t nextReprocResultFN,
61 const uint32_t nextZslResultFN, const uint32_t nextShutterFN,
62 const uint32_t nextReprocShutterFN, const uint32_t nextZslShutterFN,
63 const CameraMetadata& deviceInfo,
64 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),
72 mNextResultFrameNumber(nextResultFN),
73 mNextReprocessResultFrameNumber(nextReprocResultFN),
74 mNextZslStillResultFrameNumber(nextZslResultFN),
75 mNextShutterFrameNumber(nextShutterFN),
76 mNextReprocessShutterFrameNumber(nextReprocShutterFN),
77 mNextZslStillShutterFrameNumber(nextZslShutterFN),
78 mDeviceInfo(deviceInfo),
79 mPhysicalDeviceInfoMap(physicalDeviceInfoMap),
80 mDistortionMappers(distortionMappers),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -080081 mZoomRatioMappers(zoomRatioMappers),
82 mRotateAndCropMappers(rotateAndCropMappers) {}
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080083
84 const TagMonitor& mTagMonitor;
85 const metadata_vendor_id_t mVendorTagId;
86
87 const bool mUseHalBufManager;
88 const bool mNeedFixupMonochromeTags;
89
90 const bool mUsePartialResult;
91 const uint32_t mNumPartialResults;
92
93 // the minimal frame number of the next non-reprocess result
94 const uint32_t mNextResultFrameNumber;
95 // the minimal frame number of the next reprocess result
96 const uint32_t mNextReprocessResultFrameNumber;
97 // the minimal frame number of the next ZSL still capture result
98 const uint32_t mNextZslStillResultFrameNumber;
99 // the minimal frame number of the next non-reprocess shutter
100 const uint32_t mNextShutterFrameNumber;
101 // the minimal frame number of the next reprocess shutter
102 const uint32_t mNextReprocessShutterFrameNumber;
103 // the minimal frame number of the next ZSL still capture shutter
104 const uint32_t mNextZslStillShutterFrameNumber;
105
106 const CameraMetadata& mDeviceInfo;
107
108 const std::unordered_map<std::string, CameraMetadata>& mPhysicalDeviceInfoMap;
109
110 const std::unordered_map<std::string, camera3::DistortionMapper>& mDistortionMappers;
111
112 const std::unordered_map<std::string, camera3::ZoomRatioMapper>& mZoomRatioMappers;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800113
114 const std::unordered_map<std::string, camera3::RotateAndCropMapper>& mRotateAndCropMappers;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800115};
116
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700117/**
118 * Camera3OfflineSession for offline session defined in HIDL ICameraOfflineSession@3.6 or higher
119 */
120class Camera3OfflineSession :
121 public CameraOfflineSessionBase,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800122 virtual public hardware::camera::device::V3_5::ICameraDeviceCallback,
123 public camera3::SetErrorInterface,
124 public camera3::InflightRequestUpdateInterface,
125 public camera3::RequestBufferInterface,
126 public camera3::FlushBufferInterface {
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700127 public:
128
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800129 // initialize by Camera3Device.
130 explicit Camera3OfflineSession(const String8& id,
131 const sp<camera3::Camera3Stream>& inputStream,
132 const camera3::StreamSet& offlineStreamSet,
133 camera3::BufferRecords&& bufferRecords,
134 const camera3::InFlightRequestMap& offlineReqs,
135 const Camera3OfflineStates& offlineStates,
136 sp<hardware::camera::device::V3_6::ICameraOfflineSession> offlineSession);
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700137
138 virtual ~Camera3OfflineSession();
139
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800140 virtual status_t initialize(wp<NotificationListener> listener) override;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700141
142 /**
143 * CameraOfflineSessionBase interface
144 */
145 const String8& getId() const override;
146
147 status_t disconnect() override;
148
149 status_t dump(int fd) override;
150
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700151 // methods for capture result passing
152 status_t waitForNextFrame(nsecs_t timeout) override;
153 status_t getNextResult(CaptureResult *frame) override;
154
155 // TODO: methods for notification (error/idle/finished etc) passing
156
157 /**
158 * End of CameraOfflineSessionBase interface
159 */
160
161 /**
162 * HIDL ICameraDeviceCallback interface
163 */
164
165 /**
166 * Implementation of android::hardware::camera::device::V3_5::ICameraDeviceCallback
167 */
168
169 hardware::Return<void> processCaptureResult_3_4(
170 const hardware::hidl_vec<
171 hardware::camera::device::V3_4::CaptureResult>& results) override;
172 hardware::Return<void> processCaptureResult(
173 const hardware::hidl_vec<
174 hardware::camera::device::V3_2::CaptureResult>& results) override;
175 hardware::Return<void> notify(
176 const hardware::hidl_vec<
177 hardware::camera::device::V3_2::NotifyMsg>& msgs) override;
178
179 hardware::Return<void> requestStreamBuffers(
180 const hardware::hidl_vec<
181 hardware::camera::device::V3_5::BufferRequest>& bufReqs,
182 requestStreamBuffers_cb _hidl_cb) override;
183
184 hardware::Return<void> returnStreamBuffers(
185 const hardware::hidl_vec<
186 hardware::camera::device::V3_2::StreamBuffer>& buffers) override;
187
188 /**
189 * End of CameraOfflineSessionBase interface
190 */
191
192 private:
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700193 // Camera device ID
194 const String8 mId;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800195 sp<camera3::Camera3Stream> mInputStream;
196 camera3::StreamSet mOutputStreams;
197 camera3::BufferRecords mBufferRecords;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700198
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800199 std::mutex mOfflineReqsLock;
200 camera3::InFlightRequestMap mOfflineReqs;
201
202 sp<hardware::camera::device::V3_6::ICameraOfflineSession> mSession;
203
204 TagMonitor mTagMonitor;
205 const metadata_vendor_id_t mVendorTagId;
206
207 const bool mUseHalBufManager;
208 const bool mNeedFixupMonochromeTags;
209
210 const bool mUsePartialResult;
211 const uint32_t mNumPartialResults;
212
213 std::mutex mOutputLock;
214 List<CaptureResult> mResultQueue;
215 std::condition_variable mResultSignal;
216 // the minimal frame number of the next non-reprocess result
217 uint32_t mNextResultFrameNumber;
218 // the minimal frame number of the next reprocess result
219 uint32_t mNextReprocessResultFrameNumber;
220 // the minimal frame number of the next ZSL still capture result
221 uint32_t mNextZslStillResultFrameNumber;
222 // the minimal frame number of the next non-reprocess shutter
223 uint32_t mNextShutterFrameNumber;
224 // the minimal frame number of the next reprocess shutter
225 uint32_t mNextReprocessShutterFrameNumber;
226 // the minimal frame number of the next ZSL still capture shutter
227 uint32_t mNextZslStillShutterFrameNumber;
228 // End of mOutputLock scope
229
230 const CameraMetadata mDeviceInfo;
231 std::unordered_map<std::string, CameraMetadata> mPhysicalDeviceInfoMap;
232
233 std::unordered_map<std::string, camera3::DistortionMapper> mDistortionMappers;
234
235 std::unordered_map<std::string, camera3::ZoomRatioMapper> mZoomRatioMappers;
236
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800237 std::unordered_map<std::string, camera3::RotateAndCropMapper> mRotateAndCropMappers;
238
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800239 mutable std::mutex mLock;
240
241 enum Status {
242 STATUS_UNINITIALIZED = 0,
243 STATUS_ACTIVE,
244 STATUS_ERROR,
245 STATUS_CLOSED
246 } mStatus;
247
248 wp<NotificationListener> mListener;
249 // End of mLock protect scope
250
251 std::mutex mProcessCaptureResultLock;
252 // FMQ to write result on. Must be guarded by mProcessCaptureResultLock.
253 std::unique_ptr<ResultMetadataQueue> mResultMetadataQueue;
254
255 // Tracking cause of fatal errors when in STATUS_ERROR
256 String8 mErrorCause;
257
258 // Lock to ensure requestStreamBuffers() callbacks are serialized
259 std::mutex mRequestBufferInterfaceLock;
260 // allow request buffer until all requests are processed or disconnectImpl is called
261 bool mAllowRequestBuffer = true;
262
263 // For client methods such as disconnect/dump
264 std::mutex mInterfaceLock;
265
266 // SetErrorInterface
267 void setErrorState(const char *fmt, ...) override;
268 void setErrorStateLocked(const char *fmt, ...) override;
269
270 // InflightRequestUpdateInterface
271 void onInflightEntryRemovedLocked(nsecs_t duration) override;
272 void checkInflightMapLengthLocked() override;
273 void onInflightMapFlushedLocked() override;
274
275 // RequestBufferInterface
276 bool startRequestBuffer() override;
277 void endRequestBuffer() override;
278 nsecs_t getWaitDuration() override;
279
280 // FlushBufferInterface
281 void getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) override;
282 void getInflightRequestBufferKeys(std::vector<uint64_t>* out) override;
283 std::vector<sp<camera3::Camera3StreamInterface>> getAllStreams() override;
284
285 void setErrorStateLockedV(const char *fmt, va_list args);
286
287 status_t disconnectImpl();
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700288}; // class Camera3OfflineSession
289
290}; // namespace android
291
292#endif