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 | |
Shuzhen Wang | 90708ea | 2021-11-04 11:40:49 -0700 | [diff] [blame] | 29 | #include <android/hardware/camera/device/3.8/ICameraDeviceCallback.h> |
| 30 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 31 | #include "device3/BufferUtils.h" |
| 32 | #include "device3/DistortionMapper.h" |
| 33 | #include "device3/ZoomRatioMapper.h" |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 34 | #include "device3/RotateAndCropMapper.h" |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 35 | #include "device3/InFlightRequest.h" |
| 36 | #include "device3/Camera3Stream.h" |
| 37 | #include "device3/Camera3OutputStreamInterface.h" |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 38 | #include "utils/SessionStatsBuilder.h" |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 39 | #include "utils/TagMonitor.h" |
| 40 | |
| 41 | namespace android { |
| 42 | |
| 43 | using ResultMetadataQueue = hardware::MessageQueue<uint8_t, hardware::kSynchronizedReadWrite>; |
| 44 | |
| 45 | namespace camera3 { |
| 46 | |
| 47 | /** |
| 48 | * Helper methods shared between Camera3Device/Camera3OfflineSession for HAL callbacks |
| 49 | */ |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 50 | |
| 51 | // helper function to return the output buffers to output streams. The |
| 52 | // function also optionally calls notify(ERROR_BUFFER). |
Greg Kaiser | 51b882c | 2020-06-10 05:41:44 +0000 | [diff] [blame] | 53 | void returnOutputBuffers( |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 54 | bool useHalBufManager, |
| 55 | sp<NotificationListener> listener, // Only needed when outputSurfaces is not empty |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 56 | const camera_stream_buffer_t *outputBuffers, |
Shuzhen Wang | 90708ea | 2021-11-04 11:40:49 -0700 | [diff] [blame] | 57 | size_t numBuffers, nsecs_t timestamp, |
| 58 | nsecs_t readoutTimestamp, bool requested, nsecs_t requestTimeNs, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 59 | SessionStatsBuilder& sessionStatsBuilder, bool timestampIncreasing = true, |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 60 | // The following arguments are only meant for surface sharing use case |
| 61 | const SurfaceMap& outputSurfaces = SurfaceMap{}, |
| 62 | // Used to send buffer error callback when failing to return buffer |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 63 | const CaptureResultExtras &resultExtras = CaptureResultExtras{}, |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame] | 64 | ERROR_BUF_STRATEGY errorBufStrategy = ERROR_BUF_RETURN, |
| 65 | int32_t transform = -1); |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 66 | |
| 67 | // helper function to return the output buffers to output streams, and |
| 68 | // remove the returned buffers from the inflight request's pending buffers |
| 69 | // vector. |
| 70 | void returnAndRemovePendingOutputBuffers( |
| 71 | bool useHalBufManager, |
| 72 | sp<NotificationListener> listener, // Only needed when outputSurfaces is not empty |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 73 | InFlightRequest& request, SessionStatsBuilder& sessionStatsBuilder); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 74 | |
| 75 | // Camera3Device/Camera3OfflineSession internal states used in notify/processCaptureResult |
| 76 | // callbacks |
| 77 | struct CaptureOutputStates { |
| 78 | const String8& cameraId; |
| 79 | std::mutex& inflightLock; |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 80 | int64_t& lastCompletedRegularFrameNumber; |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 81 | int64_t& lastCompletedReprocessFrameNumber; |
Shuzhen Wang | 218bce1 | 2020-10-27 18:24:23 -0700 | [diff] [blame] | 82 | int64_t& lastCompletedZslFrameNumber; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 83 | InFlightRequestMap& inflightMap; // end of inflightLock scope |
| 84 | std::mutex& outputLock; |
Jayant Chowdhary | 8a0be29 | 2020-01-08 13:10:38 -0800 | [diff] [blame] | 85 | std::list<CaptureResult>& resultQueue; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 86 | std::condition_variable& resultSignal; |
| 87 | uint32_t& nextShutterFrameNum; |
| 88 | uint32_t& nextReprocShutterFrameNum; |
| 89 | uint32_t& nextZslShutterFrameNum; |
| 90 | uint32_t& nextResultFrameNum; |
| 91 | uint32_t& nextReprocResultFrameNum; |
| 92 | uint32_t& nextZslResultFrameNum; // end of outputLock scope |
| 93 | const bool useHalBufManager; |
| 94 | const bool usePartialResult; |
| 95 | const bool needFixupMonoChrome; |
| 96 | const uint32_t numPartialResults; |
| 97 | const metadata_vendor_id_t vendorTagId; |
| 98 | const CameraMetadata& deviceInfo; |
| 99 | const std::unordered_map<std::string, CameraMetadata>& physicalDeviceInfoMap; |
| 100 | std::unique_ptr<ResultMetadataQueue>& fmq; |
| 101 | std::unordered_map<std::string, camera3::DistortionMapper>& distortionMappers; |
| 102 | std::unordered_map<std::string, camera3::ZoomRatioMapper>& zoomRatioMappers; |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 103 | std::unordered_map<std::string, camera3::RotateAndCropMapper>& rotateAndCropMappers; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 104 | TagMonitor& tagMonitor; |
| 105 | sp<Camera3Stream> inputStream; |
| 106 | StreamSet& outputStreams; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 107 | SessionStatsBuilder& sessionStatsBuilder; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 108 | sp<NotificationListener> listener; |
| 109 | SetErrorInterface& setErrIntf; |
| 110 | InflightRequestUpdateInterface& inflightIntf; |
| 111 | BufferRecordsInterface& bufferRecordsIntf; |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame] | 112 | bool legacyClient; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | // Handle one capture result. Assume callers hold the lock to serialize all |
| 116 | // processCaptureResult calls |
| 117 | void processOneCaptureResultLocked( |
| 118 | CaptureOutputStates& states, |
| 119 | const hardware::camera::device::V3_2::CaptureResult& result, |
| 120 | const hardware::hidl_vec< |
| 121 | hardware::camera::device::V3_4::PhysicalCameraMetadata> physicalCameraMetadata); |
| 122 | |
| 123 | // Handle one notify message |
| 124 | void notify(CaptureOutputStates& states, |
Shuzhen Wang | 90708ea | 2021-11-04 11:40:49 -0700 | [diff] [blame] | 125 | const hardware::camera::device::V3_2::NotifyMsg& msg, |
| 126 | bool hasReadoutTime = false, uint64_t readoutTime = 0LL); |
| 127 | void notify(CaptureOutputStates& states, |
| 128 | const hardware::camera::device::V3_8::NotifyMsg& msg); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 129 | |
| 130 | struct RequestBufferStates { |
| 131 | const String8& cameraId; |
| 132 | std::mutex& reqBufferLock; // lock to serialize request buffer calls |
| 133 | const bool useHalBufManager; |
| 134 | StreamSet& outputStreams; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 135 | SessionStatsBuilder& sessionStatsBuilder; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 136 | SetErrorInterface& setErrIntf; |
| 137 | BufferRecordsInterface& bufferRecordsIntf; |
| 138 | RequestBufferInterface& reqBufferIntf; |
| 139 | }; |
| 140 | |
| 141 | void requestStreamBuffers(RequestBufferStates& states, |
| 142 | const hardware::hidl_vec<hardware::camera::device::V3_5::BufferRequest>& bufReqs, |
| 143 | hardware::camera::device::V3_5::ICameraDeviceCallback::requestStreamBuffers_cb _hidl_cb); |
| 144 | |
| 145 | struct ReturnBufferStates { |
| 146 | const String8& cameraId; |
| 147 | const bool useHalBufManager; |
| 148 | StreamSet& outputStreams; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 149 | SessionStatsBuilder& sessionStatsBuilder; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 150 | BufferRecordsInterface& bufferRecordsIntf; |
| 151 | }; |
| 152 | |
| 153 | void returnStreamBuffers(ReturnBufferStates& states, |
| 154 | const hardware::hidl_vec<hardware::camera::device::V3_2::StreamBuffer>& buffers); |
| 155 | |
| 156 | struct FlushInflightReqStates { |
| 157 | const String8& cameraId; |
| 158 | std::mutex& inflightLock; |
| 159 | InFlightRequestMap& inflightMap; // end of inflightLock scope |
| 160 | const bool useHalBufManager; |
| 161 | sp<NotificationListener> listener; |
| 162 | InflightRequestUpdateInterface& inflightIntf; |
| 163 | BufferRecordsInterface& bufferRecordsIntf; |
| 164 | FlushBufferInterface& flushBufferIntf; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 165 | SessionStatsBuilder& sessionStatsBuilder; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | void flushInflightRequests(FlushInflightReqStates& states); |
| 169 | } // namespace camera3 |
| 170 | |
| 171 | } // namespace android |
| 172 | |
| 173 | #endif |