• 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_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 using ResultMetadataQueue = hardware::MessageQueue<uint8_t, hardware::kSynchronizedReadWrite>;
42 
43 namespace camera3 {
44 
45     typedef struct camera_stream_configuration {
46         uint32_t num_streams;
47         camera_stream_t **streams;
48         uint32_t operation_mode;
49         bool input_is_multi_resolution;
50     } camera_stream_configuration_t;
51 
52     typedef struct camera_capture_request {
53         uint32_t frame_number;
54         const camera_metadata_t *settings;
55         camera_stream_buffer_t *input_buffer;
56         uint32_t num_output_buffers;
57         const camera_stream_buffer_t *output_buffers;
58         uint32_t num_physcam_settings;
59         const char **physcam_id;
60         const camera_metadata_t **physcam_settings;
61         int32_t input_width;
62         int32_t input_height;
63     } camera_capture_request_t;
64 
65     typedef struct camera_capture_result {
66         uint32_t frame_number;
67         const camera_metadata_t *result;
68         uint32_t num_output_buffers;
69         const camera_stream_buffer_t *output_buffers;
70         const camera_stream_buffer_t *input_buffer;
71         uint32_t partial_result;
72         uint32_t num_physcam_metadata;
73         const char **physcam_ids;
74         const camera_metadata_t **physcam_metadata;
75     } camera_capture_result_t;
76 
77     typedef struct camera_shutter_msg {
78         uint32_t frame_number;
79         uint64_t timestamp;
80     } camera_shutter_msg_t;
81 
82     typedef struct camera_error_msg {
83         uint32_t frame_number;
84         camera_stream_t *error_stream;
85         int error_code;
86     } camera_error_msg_t;
87 
88     typedef enum camera_error_msg_code {
89         CAMERA_MSG_ERROR_DEVICE = 1,
90         CAMERA_MSG_ERROR_REQUEST = 2,
91         CAMERA_MSG_ERROR_RESULT = 3,
92         CAMERA_MSG_ERROR_BUFFER = 4,
93         CAMERA_MSG_NUM_ERRORS
94     } camera_error_msg_code_t;
95 
96     typedef struct camera_notify_msg {
97         int type;
98 
99         union {
100             camera_error_msg_t error;
101             camera_shutter_msg_t shutter;
102         } message;
103     } camera_notify_msg_t;
104 
105     /**
106      * Helper methods shared between Camera3Device/Camera3OfflineSession for HAL callbacks
107      */
108 
109     // helper function to return the output buffers to output streams. The
110     // function also optionally calls notify(ERROR_BUFFER).
111     void returnOutputBuffers(
112             bool useHalBufManager,
113             sp<NotificationListener> listener, // Only needed when outputSurfaces is not empty
114             const camera_stream_buffer_t *outputBuffers,
115             size_t numBuffers, nsecs_t timestamp, bool requested, nsecs_t requestTimeNs,
116             SessionStatsBuilder& sessionStatsBuilder, bool timestampIncreasing = true,
117             // The following arguments are only meant for surface sharing use case
118             const SurfaceMap& outputSurfaces = SurfaceMap{},
119             // Used to send buffer error callback when failing to return buffer
120             const CaptureResultExtras &resultExtras = CaptureResultExtras{},
121             ERROR_BUF_STRATEGY errorBufStrategy = ERROR_BUF_RETURN);
122 
123     // helper function to return the output buffers to output streams, and
124     // remove the returned buffers from the inflight request's pending buffers
125     // vector.
126     void returnAndRemovePendingOutputBuffers(
127             bool useHalBufManager,
128             sp<NotificationListener> listener, // Only needed when outputSurfaces is not empty
129             InFlightRequest& request, SessionStatsBuilder& sessionStatsBuilder);
130 
131     // Camera3Device/Camera3OfflineSession internal states used in notify/processCaptureResult
132     // callbacks
133     struct CaptureOutputStates {
134         const String8& cameraId;
135         std::mutex& inflightLock;
136         int64_t& lastCompletedRegularFrameNumber;
137         int64_t& lastCompletedReprocessFrameNumber;
138         int64_t& lastCompletedZslFrameNumber;
139         InFlightRequestMap& inflightMap; // end of inflightLock scope
140         std::mutex& outputLock;
141         std::list<CaptureResult>& resultQueue;
142         std::condition_variable& resultSignal;
143         uint32_t& nextShutterFrameNum;
144         uint32_t& nextReprocShutterFrameNum;
145         uint32_t& nextZslShutterFrameNum;
146         uint32_t& nextResultFrameNum;
147         uint32_t& nextReprocResultFrameNum;
148         uint32_t& nextZslResultFrameNum; // end of outputLock scope
149         const bool useHalBufManager;
150         const bool usePartialResult;
151         const bool needFixupMonoChrome;
152         const uint32_t numPartialResults;
153         const metadata_vendor_id_t vendorTagId;
154         const CameraMetadata& deviceInfo;
155         const std::unordered_map<std::string, CameraMetadata>& physicalDeviceInfoMap;
156         std::unique_ptr<ResultMetadataQueue>& fmq;
157         std::unordered_map<std::string, camera3::DistortionMapper>& distortionMappers;
158         std::unordered_map<std::string, camera3::ZoomRatioMapper>& zoomRatioMappers;
159         std::unordered_map<std::string, camera3::RotateAndCropMapper>& rotateAndCropMappers;
160         TagMonitor& tagMonitor;
161         sp<Camera3Stream> inputStream;
162         StreamSet& outputStreams;
163         SessionStatsBuilder& sessionStatsBuilder;
164         sp<NotificationListener> listener;
165         SetErrorInterface& setErrIntf;
166         InflightRequestUpdateInterface& inflightIntf;
167         BufferRecordsInterface& bufferRecordsIntf;
168     };
169 
170     // Handle one capture result. Assume callers hold the lock to serialize all
171     // processCaptureResult calls
172     void processOneCaptureResultLocked(
173             CaptureOutputStates& states,
174             const hardware::camera::device::V3_2::CaptureResult& result,
175             const hardware::hidl_vec<
176                     hardware::camera::device::V3_4::PhysicalCameraMetadata> physicalCameraMetadata);
177 
178     // Handle one notify message
179     void notify(CaptureOutputStates& states,
180             const hardware::camera::device::V3_2::NotifyMsg& msg);
181 
182     struct RequestBufferStates {
183         const String8& cameraId;
184         std::mutex& reqBufferLock; // lock to serialize request buffer calls
185         const bool useHalBufManager;
186         StreamSet& outputStreams;
187         SessionStatsBuilder& sessionStatsBuilder;
188         SetErrorInterface& setErrIntf;
189         BufferRecordsInterface& bufferRecordsIntf;
190         RequestBufferInterface& reqBufferIntf;
191     };
192 
193     void requestStreamBuffers(RequestBufferStates& states,
194             const hardware::hidl_vec<hardware::camera::device::V3_5::BufferRequest>& bufReqs,
195             hardware::camera::device::V3_5::ICameraDeviceCallback::requestStreamBuffers_cb _hidl_cb);
196 
197     struct ReturnBufferStates {
198         const String8& cameraId;
199         const bool useHalBufManager;
200         StreamSet& outputStreams;
201         SessionStatsBuilder& sessionStatsBuilder;
202         BufferRecordsInterface& bufferRecordsIntf;
203     };
204 
205     void returnStreamBuffers(ReturnBufferStates& states,
206             const hardware::hidl_vec<hardware::camera::device::V3_2::StreamBuffer>& buffers);
207 
208     struct FlushInflightReqStates {
209         const String8& cameraId;
210         std::mutex& inflightLock;
211         InFlightRequestMap& inflightMap; // end of inflightLock scope
212         const bool useHalBufManager;
213         sp<NotificationListener> listener;
214         InflightRequestUpdateInterface& inflightIntf;
215         BufferRecordsInterface& bufferRecordsIntf;
216         FlushBufferInterface& flushBufferIntf;
217         SessionStatsBuilder& sessionStatsBuilder;
218     };
219 
220     void flushInflightRequests(FlushInflightReqStates& states);
221 } // namespace camera3
222 
223 } // namespace android
224 
225 #endif
226