blob: 142889a1b32bd1a2866ac2c55eeeac027dbd3988 [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{},
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
Shuzhen Wang316781a2020-08-18 18:11:01 -0700129 InFlightRequest& request, SessionStatsBuilder& sessionStatsBuilder);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800130
131 // Camera3Device/Camera3OfflineSession internal states used in notify/processCaptureResult
132 // callbacks
133 struct CaptureOutputStates {
134 const String8& cameraId;
135 std::mutex& inflightLock;
Shuzhen Wangb7b42652020-05-07 11:59:02 -0700136 int64_t& lastCompletedRegularFrameNumber;
Shuzhen Wangb7b42652020-05-07 11:59:02 -0700137 int64_t& lastCompletedReprocessFrameNumber;
Shuzhen Wang218bce12020-10-27 18:24:23 -0700138 int64_t& lastCompletedZslFrameNumber;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800139 InFlightRequestMap& inflightMap; // end of inflightLock scope
140 std::mutex& outputLock;
Jayant Chowdhary8a0be292020-01-08 13:10:38 -0800141 std::list<CaptureResult>& resultQueue;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800142 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;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800159 std::unordered_map<std::string, camera3::RotateAndCropMapper>& rotateAndCropMappers;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800160 TagMonitor& tagMonitor;
161 sp<Camera3Stream> inputStream;
162 StreamSet& outputStreams;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700163 SessionStatsBuilder& sessionStatsBuilder;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800164 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;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700187 SessionStatsBuilder& sessionStatsBuilder;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800188 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;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700201 SessionStatsBuilder& sessionStatsBuilder;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800202 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;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700217 SessionStatsBuilder& sessionStatsBuilder;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800218 };
219
220 void flushInflightRequests(FlushInflightReqStates& states);
221} // namespace camera3
222
223} // namespace android
224
225#endif