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_CAMERA3_OUTPUT_UTILS_H
18 #define ANDROID_SERVERS_CAMERA3_OUTPUT_UTILS_H
19 
20 #include <memory>
21 #include <mutex>
22 
23 #include <cutils/native_handle.h>
24 
25 #include <fmq/MessageQueue.h>
26 
27 #include <common/CameraDeviceBase.h>
28 
29 #include "device3/BufferUtils.h"
30 #include "device3/DistortionMapper.h"
31 #include "device3/ZoomRatioMapper.h"
32 #include "device3/RotateAndCropMapper.h"
33 #include "device3/InFlightRequest.h"
34 #include "device3/Camera3Stream.h"
35 #include "device3/Camera3OutputStreamInterface.h"
36 #include "utils/SessionStatsBuilder.h"
37 #include "utils/TagMonitor.h"
38 
39 namespace android {
40 
41 namespace camera3 {
42 
43     /**
44      * Helper methods shared between Camera3Device/Camera3OfflineSession for HAL callbacks
45      */
46 
47     struct BufferToReturn {
48         Camera3StreamInterface *stream;
49         camera_stream_buffer_t buffer;
50         nsecs_t timestamp;
51         nsecs_t readoutTimestamp;
52         bool timestampIncreasing;
53         std::vector<size_t> surfaceIds;
54         const CaptureResultExtras resultExtras;
55         int32_t transform;
56         nsecs_t requestTimeNs;
57 
BufferToReturnBufferToReturn58         BufferToReturn(Camera3StreamInterface *stream,
59                 camera_stream_buffer_t buffer,
60                 nsecs_t timestamp, nsecs_t readoutTimestamp,
61                 bool timestampIncreasing, std::vector<size_t> surfaceIds,
62                 const CaptureResultExtras &resultExtras,
63                 int32_t transform, nsecs_t requestTimeNs):
64             stream(stream),
65             buffer(buffer),
66             timestamp(timestamp),
67             readoutTimestamp(readoutTimestamp),
68             timestampIncreasing(timestampIncreasing),
69             surfaceIds(surfaceIds),
70             resultExtras(resultExtras),
71             transform(transform),
72             requestTimeNs(requestTimeNs) {}
73     };
74 
75     // helper function to return the output buffers to output
76     // streams. The function also optionally calls
77     // notify(ERROR_BUFFER).  Returns the list of buffers to hand back
78     // to streams in returnableBuffers.  Does not make any two-way
79     // binder calls, so suitable for use when critical locks are being
80     // held
81     void collectReturnableOutputBuffers(
82             bool useHalBufManager,
83             const std::set<int32_t> &halBufferManagedStreams,
84             sp<NotificationListener> listener, // Only needed when outputSurfaces is not empty
85             const camera_stream_buffer_t *outputBuffers,
86             size_t numBuffers, nsecs_t timestamp,
87             nsecs_t readoutTimestamp, bool requested, nsecs_t requestTimeNs,
88             SessionStatsBuilder& sessionStatsBuilder,
89             /*out*/ std::vector<BufferToReturn> *returnableBuffers,
90             bool timestampIncreasing = true,
91             // The following arguments are only meant for surface sharing use case
92             const SurfaceMap& outputSurfaces = SurfaceMap{},
93             // Used to send buffer error callback when failing to return buffer
94             const CaptureResultExtras &resultExtras = CaptureResultExtras{},
95             ERROR_BUF_STRATEGY errorBufStrategy = ERROR_BUF_RETURN,
96             int32_t transform = -1);
97 
98     // helper function to collect the output buffers ready to be
99     // returned to output streams, and to remove these buffers from
100     // the inflight request's pending buffers vector.  Does not make
101     // any two-way binder calls, so suitable for use when critical
102     // locks are being held
103     void collectAndRemovePendingOutputBuffers(
104             bool useHalBufManager,
105             const std::set<int32_t> &halBufferManagedStreams,
106             sp<NotificationListener> listener, // Only needed when outputSurfaces is not empty
107             InFlightRequest& request, SessionStatsBuilder& sessionStatsBuilder,
108             /*out*/ std::vector<BufferToReturn> *returnableBuffers);
109 
110     // Actually return filled output buffers to the consumer to use, using the list
111     // provided by collectReturnableOutputBuffers / collectAndRemovePendingOutputBuffers
112     // Makes two-way binder calls to applications, so do not hold any critical locks when
113     // calling.
114     void finishReturningOutputBuffers(const std::vector<BufferToReturn> &returnableBuffers,
115             sp<NotificationListener> listener, SessionStatsBuilder& sessionStatsBuilder);
116 
117     // Camera3Device/Camera3OfflineSession internal states used in notify/processCaptureResult
118     // callbacks
119     struct CaptureOutputStates {
120         const std::string& cameraId;
121         std::mutex& inflightLock;
122         int64_t& lastCompletedRegularFrameNumber;
123         int64_t& lastCompletedReprocessFrameNumber;
124         int64_t& lastCompletedZslFrameNumber;
125         InFlightRequestMap& inflightMap; // end of inflightLock scope
126         std::mutex& outputLock;
127         std::list<CaptureResult>& resultQueue;
128         std::condition_variable& resultSignal;
129         uint32_t& nextShutterFrameNum;
130         uint32_t& nextReprocShutterFrameNum;
131         uint32_t& nextZslShutterFrameNum;
132         uint32_t& nextResultFrameNum;
133         uint32_t& nextReprocResultFrameNum;
134         uint32_t& nextZslResultFrameNum; // end of outputLock scope
135         const bool useHalBufManager;
136         const std::set<int32_t > &halBufManagedStreamIds;
137         const bool usePartialResult;
138         const bool needFixupMonoChrome;
139         const uint32_t numPartialResults;
140         const metadata_vendor_id_t vendorTagId;
141         const CameraMetadata& deviceInfo;
142         const std::unordered_map<std::string, CameraMetadata>& physicalDeviceInfoMap;
143         std::unordered_map<std::string, camera3::DistortionMapper>& distortionMappers;
144         std::unordered_map<std::string, camera3::ZoomRatioMapper>& zoomRatioMappers;
145         std::unordered_map<std::string, camera3::RotateAndCropMapper>& rotateAndCropMappers;
146         TagMonitor& tagMonitor;
147         sp<Camera3Stream> inputStream;
148         StreamSet& outputStreams;
149         SessionStatsBuilder& sessionStatsBuilder;
150         sp<NotificationListener> listener;
151         SetErrorInterface& setErrIntf;
152         InflightRequestUpdateInterface& inflightIntf;
153         BufferRecordsInterface& bufferRecordsIntf;
154         bool legacyClient;
155         nsecs_t& minFrameDuration;
156         bool& isFixedFps;
157         int rotationOverride;
158         std::string &activePhysicalId;
159     };
160 
161     void processCaptureResult(CaptureOutputStates& states, const camera_capture_result *result);
162     void notify(CaptureOutputStates& states, const camera_notify_msg *msg);
163 
164     struct RequestBufferStates {
165         const std::string& cameraId;
166         std::mutex& reqBufferLock; // lock to serialize request buffer calls
167         const bool useHalBufManager;
168         const std::set<int32_t > &halBufManagedStreamIds;
169         StreamSet& outputStreams;
170         SessionStatsBuilder& sessionStatsBuilder;
171         SetErrorInterface& setErrIntf;
172         BufferRecordsInterface& bufferRecordsIntf;
173         RequestBufferInterface& reqBufferIntf;
174     };
175 
176     struct ReturnBufferStates {
177         const std::string& cameraId;
178         const bool useHalBufManager;
179         const std::set<int32_t > &halBufManagedStreamIds;
180         StreamSet& outputStreams;
181         SessionStatsBuilder& sessionStatsBuilder;
182         BufferRecordsInterface& bufferRecordsIntf;
183     };
184 
185     struct FlushInflightReqStates {
186         const std::string& cameraId;
187         std::mutex& inflightLock;
188         InFlightRequestMap& inflightMap; // end of inflightLock scope
189         const bool useHalBufManager;
190         const std::set<int32_t > &halBufManagedStreamIds;
191         sp<NotificationListener> listener;
192         InflightRequestUpdateInterface& inflightIntf;
193         BufferRecordsInterface& bufferRecordsIntf;
194         FlushBufferInterface& flushBufferIntf;
195         SessionStatsBuilder& sessionStatsBuilder;
196     };
197 
198     void flushInflightRequests(FlushInflightReqStates& states);
199 } // namespace camera3
200 
201 } // namespace android
202 
203 #endif
204