blob: 208f70de73997027b542c6a0c03d0cf4e7e69d57 [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 */
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700145 status_t disconnect() override;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700146 status_t dump(int fd) override;
147
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800148 /**
149 * FrameProducer interface
150 */
151 const String8& getId() const override;
152 const CameraMetadata& info() const override;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700153 status_t waitForNextFrame(nsecs_t timeout) override;
154 status_t getNextResult(CaptureResult *frame) override;
155
156 // TODO: methods for notification (error/idle/finished etc) passing
157
158 /**
159 * End of CameraOfflineSessionBase interface
160 */
161
162 /**
163 * HIDL ICameraDeviceCallback interface
164 */
165
166 /**
167 * Implementation of android::hardware::camera::device::V3_5::ICameraDeviceCallback
168 */
169
170 hardware::Return<void> processCaptureResult_3_4(
171 const hardware::hidl_vec<
172 hardware::camera::device::V3_4::CaptureResult>& results) override;
173 hardware::Return<void> processCaptureResult(
174 const hardware::hidl_vec<
175 hardware::camera::device::V3_2::CaptureResult>& results) override;
176 hardware::Return<void> notify(
177 const hardware::hidl_vec<
178 hardware::camera::device::V3_2::NotifyMsg>& msgs) override;
179
180 hardware::Return<void> requestStreamBuffers(
181 const hardware::hidl_vec<
182 hardware::camera::device::V3_5::BufferRequest>& bufReqs,
183 requestStreamBuffers_cb _hidl_cb) override;
184
185 hardware::Return<void> returnStreamBuffers(
186 const hardware::hidl_vec<
187 hardware::camera::device::V3_2::StreamBuffer>& buffers) override;
188
189 /**
190 * End of CameraOfflineSessionBase interface
191 */
192
193 private:
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700194 // Camera device ID
195 const String8 mId;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800196 sp<camera3::Camera3Stream> mInputStream;
197 camera3::StreamSet mOutputStreams;
198 camera3::BufferRecords mBufferRecords;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700199
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800200 std::mutex mOfflineReqsLock;
201 camera3::InFlightRequestMap mOfflineReqs;
202
203 sp<hardware::camera::device::V3_6::ICameraOfflineSession> mSession;
204
205 TagMonitor mTagMonitor;
206 const metadata_vendor_id_t mVendorTagId;
207
208 const bool mUseHalBufManager;
209 const bool mNeedFixupMonochromeTags;
210
211 const bool mUsePartialResult;
212 const uint32_t mNumPartialResults;
213
214 std::mutex mOutputLock;
Jayant Chowdhary8a0be292020-01-08 13:10:38 -0800215 std::list<CaptureResult> mResultQueue;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800216 std::condition_variable mResultSignal;
217 // the minimal frame number of the next non-reprocess result
218 uint32_t mNextResultFrameNumber;
219 // the minimal frame number of the next reprocess result
220 uint32_t mNextReprocessResultFrameNumber;
221 // the minimal frame number of the next ZSL still capture result
222 uint32_t mNextZslStillResultFrameNumber;
223 // the minimal frame number of the next non-reprocess shutter
224 uint32_t mNextShutterFrameNumber;
225 // the minimal frame number of the next reprocess shutter
226 uint32_t mNextReprocessShutterFrameNumber;
227 // the minimal frame number of the next ZSL still capture shutter
228 uint32_t mNextZslStillShutterFrameNumber;
229 // End of mOutputLock scope
230
231 const CameraMetadata mDeviceInfo;
232 std::unordered_map<std::string, CameraMetadata> mPhysicalDeviceInfoMap;
233
234 std::unordered_map<std::string, camera3::DistortionMapper> mDistortionMappers;
235
236 std::unordered_map<std::string, camera3::ZoomRatioMapper> mZoomRatioMappers;
237
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800238 std::unordered_map<std::string, camera3::RotateAndCropMapper> mRotateAndCropMappers;
239
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800240 mutable std::mutex mLock;
241
242 enum Status {
243 STATUS_UNINITIALIZED = 0,
244 STATUS_ACTIVE,
245 STATUS_ERROR,
246 STATUS_CLOSED
247 } mStatus;
248
249 wp<NotificationListener> mListener;
250 // End of mLock protect scope
251
252 std::mutex mProcessCaptureResultLock;
253 // FMQ to write result on. Must be guarded by mProcessCaptureResultLock.
254 std::unique_ptr<ResultMetadataQueue> mResultMetadataQueue;
255
256 // Tracking cause of fatal errors when in STATUS_ERROR
257 String8 mErrorCause;
258
259 // Lock to ensure requestStreamBuffers() callbacks are serialized
260 std::mutex mRequestBufferInterfaceLock;
261 // allow request buffer until all requests are processed or disconnectImpl is called
262 bool mAllowRequestBuffer = true;
263
264 // For client methods such as disconnect/dump
265 std::mutex mInterfaceLock;
266
267 // SetErrorInterface
268 void setErrorState(const char *fmt, ...) override;
269 void setErrorStateLocked(const char *fmt, ...) override;
270
271 // InflightRequestUpdateInterface
272 void onInflightEntryRemovedLocked(nsecs_t duration) override;
273 void checkInflightMapLengthLocked() override;
274 void onInflightMapFlushedLocked() override;
275
276 // RequestBufferInterface
277 bool startRequestBuffer() override;
278 void endRequestBuffer() override;
279 nsecs_t getWaitDuration() override;
280
281 // FlushBufferInterface
282 void getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) override;
283 void getInflightRequestBufferKeys(std::vector<uint64_t>* out) override;
284 std::vector<sp<camera3::Camera3StreamInterface>> getAllStreams() override;
285
286 void setErrorStateLockedV(const char *fmt, va_list args);
287
288 status_t disconnectImpl();
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700289}; // class Camera3OfflineSession
290
291}; // namespace android
292
293#endif