• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
20 #include <memory>
21 #include <mutex>
22 
23 #include <utils/String8.h>
24 #include <utils/String16.h>
25 
26 #include <android/hardware/camera/device/3.6/ICameraOfflineSession.h>
27 
28 #include <fmq/MessageQueue.h>
29 
30 #include "common/CameraOfflineSessionBase.h"
31 
32 #include "device3/Camera3BufferManager.h"
33 #include "device3/DistortionMapper.h"
34 #include "device3/InFlightRequest.h"
35 #include "device3/Camera3OutputUtils.h"
36 #include "device3/RotateAndCropMapper.h"
37 #include "device3/ZoomRatioMapper.h"
38 #include "utils/TagMonitor.h"
39 #include <camera_metadata_hidden.h>
40 
41 namespace android {
42 
43 namespace camera3 {
44 
45 class Camera3Stream;
46 class Camera3OutputStreamInterface;
47 class Camera3StreamInterface;
48 
49 } // namespace camera3
50 
51 
52 // An immutable struct containing general states that will be copied from Camera3Device to
53 // Camera3OfflineSession
54 struct Camera3OfflineStates {
Camera3OfflineStatesCamera3OfflineStates55     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 int64_t lastCompletedRegularFN, const int64_t lastCompletedReprocessFN,
60             const int64_t lastCompletedZslFN, const uint32_t nextResultFN,
61             const uint32_t nextReprocResultFN, const uint32_t nextZslResultFN,
62             const uint32_t nextShutterFN, const uint32_t nextReprocShutterFN,
63             const uint32_t nextZslShutterFN, const CameraMetadata& deviceInfo,
64             const std::unordered_map<std::string, CameraMetadata>& physicalDeviceInfoMap,
65             const std::unordered_map<std::string, camera3::DistortionMapper>& distortionMappers,
66             const std::unordered_map<std::string, camera3::ZoomRatioMapper>& zoomRatioMappers,
67             const std::unordered_map<std::string, camera3::RotateAndCropMapper>&
68                 rotateAndCropMappers) :
69             mTagMonitor(tagMonitor), mVendorTagId(vendorTagId),
70             mUseHalBufManager(useHalBufManager), mNeedFixupMonochromeTags(needFixupMonochromeTags),
71             mUsePartialResult(usePartialResult), mNumPartialResults(numPartialResults),
72             mLastCompletedRegularFrameNumber(lastCompletedRegularFN),
73             mLastCompletedReprocessFrameNumber(lastCompletedReprocessFN),
74             mLastCompletedZslFrameNumber(lastCompletedZslFN),
75             mNextResultFrameNumber(nextResultFN),
76             mNextReprocessResultFrameNumber(nextReprocResultFN),
77             mNextZslStillResultFrameNumber(nextZslResultFN),
78             mNextShutterFrameNumber(nextShutterFN),
79             mNextReprocessShutterFrameNumber(nextReprocShutterFN),
80             mNextZslStillShutterFrameNumber(nextZslShutterFN),
81             mDeviceInfo(deviceInfo),
82             mPhysicalDeviceInfoMap(physicalDeviceInfoMap),
83             mDistortionMappers(distortionMappers),
84             mZoomRatioMappers(zoomRatioMappers),
85             mRotateAndCropMappers(rotateAndCropMappers) {}
86 
87     const TagMonitor& mTagMonitor;
88     const metadata_vendor_id_t mVendorTagId;
89 
90     const bool mUseHalBufManager;
91     const bool mNeedFixupMonochromeTags;
92 
93     const bool mUsePartialResult;
94     const uint32_t mNumPartialResults;
95 
96     // The last completed (buffers, result metadata, and error notify) regular
97     // request frame number
98     const int64_t mLastCompletedRegularFrameNumber;
99     // The last completed (buffers, result metadata, and error notify) reprocess
100     // request frame number
101     const int64_t mLastCompletedReprocessFrameNumber;
102     // The last completed (buffers, result metadata, and error notify) zsl
103     // request frame number
104     const int64_t mLastCompletedZslFrameNumber;
105     // the minimal frame number of the next non-reprocess result
106     const uint32_t mNextResultFrameNumber;
107     // the minimal frame number of the next reprocess result
108     const uint32_t mNextReprocessResultFrameNumber;
109     // the minimal frame number of the next ZSL still capture result
110     const uint32_t mNextZslStillResultFrameNumber;
111     // the minimal frame number of the next non-reprocess shutter
112     const uint32_t mNextShutterFrameNumber;
113     // the minimal frame number of the next reprocess shutter
114     const uint32_t mNextReprocessShutterFrameNumber;
115     // the minimal frame number of the next ZSL still capture shutter
116     const uint32_t mNextZslStillShutterFrameNumber;
117 
118     const CameraMetadata& mDeviceInfo;
119 
120     const std::unordered_map<std::string, CameraMetadata>& mPhysicalDeviceInfoMap;
121 
122     const std::unordered_map<std::string, camera3::DistortionMapper>& mDistortionMappers;
123 
124     const std::unordered_map<std::string, camera3::ZoomRatioMapper>& mZoomRatioMappers;
125 
126     const std::unordered_map<std::string, camera3::RotateAndCropMapper>& mRotateAndCropMappers;
127 };
128 
129 /**
130  * Camera3OfflineSession for offline session defined in HIDL ICameraOfflineSession@3.6 or higher
131  */
132 class Camera3OfflineSession :
133             public CameraOfflineSessionBase,
134             virtual public hardware::camera::device::V3_5::ICameraDeviceCallback,
135             public camera3::SetErrorInterface,
136             public camera3::InflightRequestUpdateInterface,
137             public camera3::RequestBufferInterface,
138             public camera3::FlushBufferInterface {
139   public:
140 
141     // initialize by Camera3Device.
142     explicit Camera3OfflineSession(const String8& id,
143             const sp<camera3::Camera3Stream>& inputStream,
144             const camera3::StreamSet& offlineStreamSet,
145             camera3::BufferRecords&& bufferRecords,
146             const camera3::InFlightRequestMap& offlineReqs,
147             const Camera3OfflineStates& offlineStates,
148             sp<hardware::camera::device::V3_6::ICameraOfflineSession> offlineSession);
149 
150     virtual ~Camera3OfflineSession();
151 
152     virtual status_t initialize(wp<NotificationListener> listener) override;
153 
154     /**
155      * CameraOfflineSessionBase interface
156      */
157     status_t disconnect() override;
158     status_t dump(int fd) override;
159 
160     /**
161      * FrameProducer interface
162      */
163     const String8& getId() const override;
164     const CameraMetadata& info() const override;
165     status_t waitForNextFrame(nsecs_t timeout) override;
166     status_t getNextResult(CaptureResult *frame) override;
167 
168     // TODO: methods for notification (error/idle/finished etc) passing
169 
170     /**
171      * End of CameraOfflineSessionBase interface
172      */
173 
174     /**
175      * HIDL ICameraDeviceCallback interface
176      */
177 
178     /**
179      * Implementation of android::hardware::camera::device::V3_5::ICameraDeviceCallback
180      */
181 
182     hardware::Return<void> processCaptureResult_3_4(
183             const hardware::hidl_vec<
184                     hardware::camera::device::V3_4::CaptureResult>& results) override;
185     hardware::Return<void> processCaptureResult(
186             const hardware::hidl_vec<
187                     hardware::camera::device::V3_2::CaptureResult>& results) override;
188     hardware::Return<void> notify(
189             const hardware::hidl_vec<
190                     hardware::camera::device::V3_2::NotifyMsg>& msgs) override;
191 
192     hardware::Return<void> requestStreamBuffers(
193             const hardware::hidl_vec<
194                     hardware::camera::device::V3_5::BufferRequest>& bufReqs,
195             requestStreamBuffers_cb _hidl_cb) override;
196 
197     hardware::Return<void> returnStreamBuffers(
198             const hardware::hidl_vec<
199                     hardware::camera::device::V3_2::StreamBuffer>& buffers) override;
200 
201     /**
202      * End of CameraOfflineSessionBase interface
203      */
204 
205   private:
206     // Camera device ID
207     const String8 mId;
208     sp<camera3::Camera3Stream> mInputStream;
209     camera3::StreamSet mOutputStreams;
210     camera3::BufferRecords mBufferRecords;
211     SessionStatsBuilder mSessionStatsBuilder;
212 
213     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;
228     std::list<CaptureResult> mResultQueue;
229     std::condition_variable mResultSignal;
230     // 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;
236     // 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 
257     std::unordered_map<std::string, camera3::RotateAndCropMapper> mRotateAndCropMappers;
258 
259     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();
308 }; // class Camera3OfflineSession
309 
310 }; // namespace android
311 
312 #endif
313