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