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