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