blob: b5fd4865c6258a24d4aef876efb8ea3678aa2644 [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 <android/hardware/camera/device/3.6/ICameraOfflineSession.h>
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080024
Yin-Chia Yehb978c382019-10-30 00:22:37 -070025#include <fmq/MessageQueue.h>
26
27#include "common/CameraOfflineSessionBase.h"
28
29#include "device3/Camera3BufferManager.h"
30#include "device3/DistortionMapper.h"
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080031#include "device3/InFlightRequest.h"
32#include "device3/Camera3OutputUtils.h"
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -080033#include "device3/RotateAndCropMapper.h"
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080034#include "device3/ZoomRatioMapper.h"
Yin-Chia Yehb978c382019-10-30 00:22:37 -070035#include "utils/TagMonitor.h"
Yin-Chia Yehb978c382019-10-30 00:22:37 -070036#include <camera_metadata_hidden.h>
37
38namespace android {
39
40namespace camera3 {
41
42class Camera3Stream;
43class Camera3OutputStreamInterface;
44class Camera3StreamInterface;
45
46} // namespace camera3
47
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080048
49// An immutable struct containing general states that will be copied from Camera3Device to
50// Camera3OfflineSession
51struct Camera3OfflineStates {
52 Camera3OfflineStates(
53 const TagMonitor& tagMonitor, const metadata_vendor_id_t vendorTagId,
54 const bool useHalBufManager, const bool needFixupMonochromeTags,
55 const bool usePartialResult, const uint32_t numPartialResults,
Shuzhen Wangb7b42652020-05-07 11:59:02 -070056 const int64_t lastCompletedRegularFN, const int64_t lastCompletedReprocessFN,
57 const int64_t lastCompletedZslFN, const uint32_t nextResultFN,
58 const uint32_t nextReprocResultFN, const uint32_t nextZslResultFN,
59 const uint32_t nextShutterFN, const uint32_t nextReprocShutterFN,
60 const uint32_t nextZslShutterFN, const CameraMetadata& deviceInfo,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080061 const std::unordered_map<std::string, CameraMetadata>& physicalDeviceInfoMap,
62 const std::unordered_map<std::string, camera3::DistortionMapper>& distortionMappers,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -080063 const std::unordered_map<std::string, camera3::ZoomRatioMapper>& zoomRatioMappers,
64 const std::unordered_map<std::string, camera3::RotateAndCropMapper>&
65 rotateAndCropMappers) :
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080066 mTagMonitor(tagMonitor), mVendorTagId(vendorTagId),
67 mUseHalBufManager(useHalBufManager), mNeedFixupMonochromeTags(needFixupMonochromeTags),
68 mUsePartialResult(usePartialResult), mNumPartialResults(numPartialResults),
Shuzhen Wangb7b42652020-05-07 11:59:02 -070069 mLastCompletedRegularFrameNumber(lastCompletedRegularFN),
70 mLastCompletedReprocessFrameNumber(lastCompletedReprocessFN),
71 mLastCompletedZslFrameNumber(lastCompletedZslFN),
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080072 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
Shuzhen Wangb7b42652020-05-07 11:59:02 -070093 // The last completed (buffers, result metadata, and error notify) regular
94 // request frame number
95 const int64_t mLastCompletedRegularFrameNumber;
96 // The last completed (buffers, result metadata, and error notify) reprocess
97 // request frame number
98 const int64_t mLastCompletedReprocessFrameNumber;
99 // The last completed (buffers, result metadata, and error notify) zsl
100 // request frame number
101 const int64_t mLastCompletedZslFrameNumber;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800102 // the minimal frame number of the next non-reprocess result
103 const uint32_t mNextResultFrameNumber;
104 // the minimal frame number of the next reprocess result
105 const uint32_t mNextReprocessResultFrameNumber;
106 // the minimal frame number of the next ZSL still capture result
107 const uint32_t mNextZslStillResultFrameNumber;
108 // the minimal frame number of the next non-reprocess shutter
109 const uint32_t mNextShutterFrameNumber;
110 // the minimal frame number of the next reprocess shutter
111 const uint32_t mNextReprocessShutterFrameNumber;
112 // the minimal frame number of the next ZSL still capture shutter
113 const uint32_t mNextZslStillShutterFrameNumber;
114
115 const CameraMetadata& mDeviceInfo;
116
117 const std::unordered_map<std::string, CameraMetadata>& mPhysicalDeviceInfoMap;
118
119 const std::unordered_map<std::string, camera3::DistortionMapper>& mDistortionMappers;
120
121 const std::unordered_map<std::string, camera3::ZoomRatioMapper>& mZoomRatioMappers;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800122
123 const std::unordered_map<std::string, camera3::RotateAndCropMapper>& mRotateAndCropMappers;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800124};
125
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700126/**
127 * Camera3OfflineSession for offline session defined in HIDL ICameraOfflineSession@3.6 or higher
128 */
129class Camera3OfflineSession :
130 public CameraOfflineSessionBase,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800131 public camera3::SetErrorInterface,
132 public camera3::InflightRequestUpdateInterface,
133 public camera3::RequestBufferInterface,
134 public camera3::FlushBufferInterface {
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700135 public:
136
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800137 // initialize by Camera3Device.
Austin Borger0fb3ad92023-06-01 16:51:35 -0700138 explicit Camera3OfflineSession(const std::string& id,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800139 const sp<camera3::Camera3Stream>& inputStream,
140 const camera3::StreamSet& offlineStreamSet,
141 camera3::BufferRecords&& bufferRecords,
142 const camera3::InFlightRequestMap& offlineReqs,
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800143 const Camera3OfflineStates& offlineStates);
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700144
145 virtual ~Camera3OfflineSession();
146
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800147 virtual status_t initialize(wp<NotificationListener> /*listener*/) = 0;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700148
149 /**
150 * CameraOfflineSessionBase interface
151 */
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700152 status_t disconnect() override;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700153 status_t dump(int fd) override;
154
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800155 /**
156 * FrameProducer interface
157 */
Austin Borger0fb3ad92023-06-01 16:51:35 -0700158 const std::string& getId() const override;
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800159 const CameraMetadata& info() const override;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700160 status_t waitForNextFrame(nsecs_t timeout) override;
161 status_t getNextResult(CaptureResult *frame) override;
162
163 // TODO: methods for notification (error/idle/finished etc) passing
164
165 /**
166 * End of CameraOfflineSessionBase interface
167 */
168
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800169 protected:
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700170 // Camera device ID
Austin Borger0fb3ad92023-06-01 16:51:35 -0700171 const std::string mId;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800172 sp<camera3::Camera3Stream> mInputStream;
173 camera3::StreamSet mOutputStreams;
174 camera3::BufferRecords mBufferRecords;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700175 SessionStatsBuilder mSessionStatsBuilder;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700176
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800177 std::mutex mOfflineReqsLock;
178 camera3::InFlightRequestMap mOfflineReqs;
179
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800180 TagMonitor mTagMonitor;
181 const metadata_vendor_id_t mVendorTagId;
182
183 const bool mUseHalBufManager;
184 const bool mNeedFixupMonochromeTags;
185
186 const bool mUsePartialResult;
187 const uint32_t mNumPartialResults;
188
189 std::mutex mOutputLock;
Jayant Chowdhary8a0be292020-01-08 13:10:38 -0800190 std::list<CaptureResult> mResultQueue;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800191 std::condition_variable mResultSignal;
Shuzhen Wangb7b42652020-05-07 11:59:02 -0700192 // the last completed frame number of regular requests
193 int64_t mLastCompletedRegularFrameNumber;
194 // the last completed frame number of reprocess requests
195 int64_t mLastCompletedReprocessFrameNumber;
196 // the last completed frame number of ZSL still capture requests
197 int64_t mLastCompletedZslFrameNumber;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800198 // the minimal frame number of the next non-reprocess result
199 uint32_t mNextResultFrameNumber;
200 // the minimal frame number of the next reprocess result
201 uint32_t mNextReprocessResultFrameNumber;
202 // the minimal frame number of the next ZSL still capture result
203 uint32_t mNextZslStillResultFrameNumber;
204 // the minimal frame number of the next non-reprocess shutter
205 uint32_t mNextShutterFrameNumber;
206 // the minimal frame number of the next reprocess shutter
207 uint32_t mNextReprocessShutterFrameNumber;
208 // the minimal frame number of the next ZSL still capture shutter
209 uint32_t mNextZslStillShutterFrameNumber;
210 // End of mOutputLock scope
211
212 const CameraMetadata mDeviceInfo;
213 std::unordered_map<std::string, CameraMetadata> mPhysicalDeviceInfoMap;
214
215 std::unordered_map<std::string, camera3::DistortionMapper> mDistortionMappers;
216
217 std::unordered_map<std::string, camera3::ZoomRatioMapper> mZoomRatioMappers;
218
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800219 std::unordered_map<std::string, camera3::RotateAndCropMapper> mRotateAndCropMappers;
220
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800221 mutable std::mutex mLock;
222
223 enum Status {
224 STATUS_UNINITIALIZED = 0,
225 STATUS_ACTIVE,
226 STATUS_ERROR,
227 STATUS_CLOSED
228 } mStatus;
229
230 wp<NotificationListener> mListener;
231 // End of mLock protect scope
232
233 std::mutex mProcessCaptureResultLock;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800234
235 // Tracking cause of fatal errors when in STATUS_ERROR
Austin Borger0fb3ad92023-06-01 16:51:35 -0700236 std::string mErrorCause;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800237
238 // Lock to ensure requestStreamBuffers() callbacks are serialized
239 std::mutex mRequestBufferInterfaceLock;
240 // allow request buffer until all requests are processed or disconnectImpl is called
241 bool mAllowRequestBuffer = true;
242
243 // For client methods such as disconnect/dump
244 std::mutex mInterfaceLock;
245
Shuzhen Wang00abbeb2022-02-25 17:14:42 -0800246 // The current minimum expected frame duration based on AE_TARGET_FPS_RANGE
247 nsecs_t mMinExpectedDuration = 0;
Shuzhen Wang696e4da2022-09-08 14:31:13 -0700248 // Whether the camera device runs at fixed frame rate based on AE_MODE and
249 // AE_TARGET_FPS_RANGE
250 bool mIsFixedFps = false;
Shuzhen Wang00abbeb2022-02-25 17:14:42 -0800251
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800252 // SetErrorInterface
253 void setErrorState(const char *fmt, ...) override;
254 void setErrorStateLocked(const char *fmt, ...) override;
255
256 // InflightRequestUpdateInterface
257 void onInflightEntryRemovedLocked(nsecs_t duration) override;
258 void checkInflightMapLengthLocked() override;
259 void onInflightMapFlushedLocked() override;
260
261 // RequestBufferInterface
262 bool startRequestBuffer() override;
263 void endRequestBuffer() override;
264 nsecs_t getWaitDuration() override;
265
266 // FlushBufferInterface
267 void getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) override;
268 void getInflightRequestBufferKeys(std::vector<uint64_t>* out) override;
269 std::vector<sp<camera3::Camera3StreamInterface>> getAllStreams() override;
270
271 void setErrorStateLockedV(const char *fmt, va_list args);
272
273 status_t disconnectImpl();
Emilian Peev886ac212023-02-07 14:10:24 -0800274
275 // Clients need to ensure that 'mInterfaceLock' is acquired before calling this method
276 virtual void closeSessionLocked() = 0;
277
278 // Clients need to ensure that 'mLock' is acquired before calling this method
279 virtual void releaseSessionLocked() = 0;
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800280
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700281}; // class Camera3OfflineSession
282
283}; // namespace android
284
285#endif