blob: 06b7ab41677eb873f5e9f2f6ddf80bbbf01abeb5 [file] [log] [blame]
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001/*
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"
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -080032#include "device3/RotateAndCropMapper.h"
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080033#include "device3/InFlightRequest.h"
34#include "device3/Camera3Stream.h"
35#include "device3/Camera3OutputStreamInterface.h"
Shuzhen Wang316781a2020-08-18 18:11:01 -070036#include "utils/SessionStatsBuilder.h"
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080037#include "utils/TagMonitor.h"
38
39namespace android {
40
41using ResultMetadataQueue = hardware::MessageQueue<uint8_t, hardware::kSynchronizedReadWrite>;
42
43namespace camera3 {
44
Emilian Peevf4816702020-04-03 15:44:51 -070045 typedef struct camera_stream_configuration {
46 uint32_t num_streams;
47 camera_stream_t **streams;
48 uint32_t operation_mode;
Shuzhen Wang83bff122020-11-20 15:51:39 -080049 bool input_is_multi_resolution;
Emilian Peevf4816702020-04-03 15:44:51 -070050 } 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;
Shuzhen Wang83bff122020-11-20 15:51:39 -080061 int32_t input_width;
62 int32_t input_height;
Emilian Peevf4816702020-04-03 15:44:51 -070063 } 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
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800105 /**
106 * Helper methods shared between Camera3Device/Camera3OfflineSession for HAL callbacks
107 */
Shuzhen Wangb7b42652020-05-07 11:59:02 -0700108
109 // helper function to return the output buffers to output streams. The
110 // function also optionally calls notify(ERROR_BUFFER).
Greg Kaiser51b882c2020-06-10 05:41:44 +0000111 void returnOutputBuffers(
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800112 bool useHalBufManager,
113 sp<NotificationListener> listener, // Only needed when outputSurfaces is not empty
Emilian Peevf4816702020-04-03 15:44:51 -0700114 const camera_stream_buffer_t *outputBuffers,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700115 size_t numBuffers, nsecs_t timestamp, bool requested, nsecs_t requestTimeNs,
116 SessionStatsBuilder& sessionStatsBuilder, bool timestampIncreasing = true,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800117 // 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
Shuzhen Wangb7b42652020-05-07 11:59:02 -0700120 const CaptureResultExtras &resultExtras = CaptureResultExtras{},
Emilian Peev5104fe92021-10-21 14:27:09 -0700121 ERROR_BUF_STRATEGY errorBufStrategy = ERROR_BUF_RETURN,
122 int32_t transform = -1);
Shuzhen Wangb7b42652020-05-07 11:59:02 -0700123
124 // helper function to return the output buffers to output streams, and
125 // remove the returned buffers from the inflight request's pending buffers
126 // vector.
127 void returnAndRemovePendingOutputBuffers(
128 bool useHalBufManager,
129 sp<NotificationListener> listener, // Only needed when outputSurfaces is not empty
Shuzhen Wang316781a2020-08-18 18:11:01 -0700130 InFlightRequest& request, SessionStatsBuilder& sessionStatsBuilder);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800131
132 // Camera3Device/Camera3OfflineSession internal states used in notify/processCaptureResult
133 // callbacks
134 struct CaptureOutputStates {
135 const String8& cameraId;
136 std::mutex& inflightLock;
Shuzhen Wangb7b42652020-05-07 11:59:02 -0700137 int64_t& lastCompletedRegularFrameNumber;
Shuzhen Wangb7b42652020-05-07 11:59:02 -0700138 int64_t& lastCompletedReprocessFrameNumber;
Shuzhen Wang218bce12020-10-27 18:24:23 -0700139 int64_t& lastCompletedZslFrameNumber;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800140 InFlightRequestMap& inflightMap; // end of inflightLock scope
141 std::mutex& outputLock;
Jayant Chowdhary8a0be292020-01-08 13:10:38 -0800142 std::list<CaptureResult>& resultQueue;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800143 std::condition_variable& resultSignal;
144 uint32_t& nextShutterFrameNum;
145 uint32_t& nextReprocShutterFrameNum;
146 uint32_t& nextZslShutterFrameNum;
147 uint32_t& nextResultFrameNum;
148 uint32_t& nextReprocResultFrameNum;
149 uint32_t& nextZslResultFrameNum; // end of outputLock scope
150 const bool useHalBufManager;
151 const bool usePartialResult;
152 const bool needFixupMonoChrome;
153 const uint32_t numPartialResults;
154 const metadata_vendor_id_t vendorTagId;
155 const CameraMetadata& deviceInfo;
156 const std::unordered_map<std::string, CameraMetadata>& physicalDeviceInfoMap;
157 std::unique_ptr<ResultMetadataQueue>& fmq;
158 std::unordered_map<std::string, camera3::DistortionMapper>& distortionMappers;
159 std::unordered_map<std::string, camera3::ZoomRatioMapper>& zoomRatioMappers;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800160 std::unordered_map<std::string, camera3::RotateAndCropMapper>& rotateAndCropMappers;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800161 TagMonitor& tagMonitor;
162 sp<Camera3Stream> inputStream;
163 StreamSet& outputStreams;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700164 SessionStatsBuilder& sessionStatsBuilder;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800165 sp<NotificationListener> listener;
166 SetErrorInterface& setErrIntf;
167 InflightRequestUpdateInterface& inflightIntf;
168 BufferRecordsInterface& bufferRecordsIntf;
Emilian Peev5104fe92021-10-21 14:27:09 -0700169 bool legacyClient;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800170 };
171
172 // Handle one capture result. Assume callers hold the lock to serialize all
173 // processCaptureResult calls
174 void processOneCaptureResultLocked(
175 CaptureOutputStates& states,
176 const hardware::camera::device::V3_2::CaptureResult& result,
177 const hardware::hidl_vec<
178 hardware::camera::device::V3_4::PhysicalCameraMetadata> physicalCameraMetadata);
179
180 // Handle one notify message
181 void notify(CaptureOutputStates& states,
182 const hardware::camera::device::V3_2::NotifyMsg& msg);
183
184 struct RequestBufferStates {
185 const String8& cameraId;
186 std::mutex& reqBufferLock; // lock to serialize request buffer calls
187 const bool useHalBufManager;
188 StreamSet& outputStreams;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700189 SessionStatsBuilder& sessionStatsBuilder;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800190 SetErrorInterface& setErrIntf;
191 BufferRecordsInterface& bufferRecordsIntf;
192 RequestBufferInterface& reqBufferIntf;
193 };
194
195 void requestStreamBuffers(RequestBufferStates& states,
196 const hardware::hidl_vec<hardware::camera::device::V3_5::BufferRequest>& bufReqs,
197 hardware::camera::device::V3_5::ICameraDeviceCallback::requestStreamBuffers_cb _hidl_cb);
198
199 struct ReturnBufferStates {
200 const String8& cameraId;
201 const bool useHalBufManager;
202 StreamSet& outputStreams;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700203 SessionStatsBuilder& sessionStatsBuilder;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800204 BufferRecordsInterface& bufferRecordsIntf;
205 };
206
207 void returnStreamBuffers(ReturnBufferStates& states,
208 const hardware::hidl_vec<hardware::camera::device::V3_2::StreamBuffer>& buffers);
209
210 struct FlushInflightReqStates {
211 const String8& cameraId;
212 std::mutex& inflightLock;
213 InFlightRequestMap& inflightMap; // end of inflightLock scope
214 const bool useHalBufManager;
215 sp<NotificationListener> listener;
216 InflightRequestUpdateInterface& inflightIntf;
217 BufferRecordsInterface& bufferRecordsIntf;
218 FlushBufferInterface& flushBufferIntf;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700219 SessionStatsBuilder& sessionStatsBuilder;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800220 };
221
222 void flushInflightRequests(FlushInflightReqStates& states);
223} // namespace camera3
224
225} // namespace android
226
227#endif