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 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 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 |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 54 | const camera_stream_buffer_t *outputBuffers, |
Shuzhen Wang | 90708ea | 2021-11-04 11:40:49 -0700 | [diff] [blame] | 55 | size_t numBuffers, nsecs_t timestamp, |
| 56 | nsecs_t readoutTimestamp, bool requested, nsecs_t requestTimeNs, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 57 | SessionStatsBuilder& sessionStatsBuilder, bool timestampIncreasing = true, |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 58 | // The following arguments are only meant for surface sharing use case |
| 59 | const SurfaceMap& outputSurfaces = SurfaceMap{}, |
| 60 | // Used to send buffer error callback when failing to return buffer |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 61 | const CaptureResultExtras &resultExtras = CaptureResultExtras{}, |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame] | 62 | ERROR_BUF_STRATEGY errorBufStrategy = ERROR_BUF_RETURN, |
| 63 | int32_t transform = -1); |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 64 | |
| 65 | // helper function to return the output buffers to output streams, and |
| 66 | // remove the returned buffers from the inflight request's pending buffers |
| 67 | // vector. |
| 68 | void returnAndRemovePendingOutputBuffers( |
| 69 | bool useHalBufManager, |
| 70 | sp<NotificationListener> listener, // Only needed when outputSurfaces is not empty |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 71 | InFlightRequest& request, SessionStatsBuilder& sessionStatsBuilder); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 72 | |
| 73 | // Camera3Device/Camera3OfflineSession internal states used in notify/processCaptureResult |
| 74 | // callbacks |
| 75 | struct CaptureOutputStates { |
| 76 | const String8& cameraId; |
| 77 | std::mutex& inflightLock; |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 78 | int64_t& lastCompletedRegularFrameNumber; |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 79 | int64_t& lastCompletedReprocessFrameNumber; |
Shuzhen Wang | 218bce1 | 2020-10-27 18:24:23 -0700 | [diff] [blame] | 80 | int64_t& lastCompletedZslFrameNumber; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 81 | InFlightRequestMap& inflightMap; // end of inflightLock scope |
| 82 | std::mutex& outputLock; |
Jayant Chowdhary | 8a0be29 | 2020-01-08 13:10:38 -0800 | [diff] [blame] | 83 | std::list<CaptureResult>& resultQueue; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 84 | std::condition_variable& resultSignal; |
| 85 | uint32_t& nextShutterFrameNum; |
| 86 | uint32_t& nextReprocShutterFrameNum; |
| 87 | uint32_t& nextZslShutterFrameNum; |
| 88 | uint32_t& nextResultFrameNum; |
| 89 | uint32_t& nextReprocResultFrameNum; |
| 90 | uint32_t& nextZslResultFrameNum; // end of outputLock scope |
| 91 | const bool useHalBufManager; |
| 92 | const bool usePartialResult; |
| 93 | const bool needFixupMonoChrome; |
| 94 | const uint32_t numPartialResults; |
| 95 | const metadata_vendor_id_t vendorTagId; |
| 96 | const CameraMetadata& deviceInfo; |
| 97 | const std::unordered_map<std::string, CameraMetadata>& physicalDeviceInfoMap; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 98 | std::unordered_map<std::string, camera3::DistortionMapper>& distortionMappers; |
| 99 | std::unordered_map<std::string, camera3::ZoomRatioMapper>& zoomRatioMappers; |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 100 | std::unordered_map<std::string, camera3::RotateAndCropMapper>& rotateAndCropMappers; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 101 | TagMonitor& tagMonitor; |
| 102 | sp<Camera3Stream> inputStream; |
| 103 | StreamSet& outputStreams; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 104 | SessionStatsBuilder& sessionStatsBuilder; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 105 | sp<NotificationListener> listener; |
| 106 | SetErrorInterface& setErrIntf; |
| 107 | InflightRequestUpdateInterface& inflightIntf; |
| 108 | BufferRecordsInterface& bufferRecordsIntf; |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame] | 109 | bool legacyClient; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 110 | }; |
| 111 | |
Jayant Chowdhary | 22441f3 | 2021-12-26 18:35:41 -0800 | [diff] [blame] | 112 | void processCaptureResult(CaptureOutputStates& states, const camera_capture_result *result); |
| 113 | void notify(CaptureOutputStates& states, const camera_notify_msg *msg); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 114 | |
| 115 | struct RequestBufferStates { |
| 116 | const String8& cameraId; |
| 117 | std::mutex& reqBufferLock; // lock to serialize request buffer calls |
| 118 | const bool useHalBufManager; |
| 119 | StreamSet& outputStreams; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 120 | SessionStatsBuilder& sessionStatsBuilder; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 121 | SetErrorInterface& setErrIntf; |
| 122 | BufferRecordsInterface& bufferRecordsIntf; |
| 123 | RequestBufferInterface& reqBufferIntf; |
| 124 | }; |
| 125 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 126 | struct ReturnBufferStates { |
| 127 | const String8& cameraId; |
| 128 | const bool useHalBufManager; |
| 129 | StreamSet& outputStreams; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 130 | SessionStatsBuilder& sessionStatsBuilder; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 131 | BufferRecordsInterface& bufferRecordsIntf; |
| 132 | }; |
| 133 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 134 | struct FlushInflightReqStates { |
| 135 | const String8& cameraId; |
| 136 | std::mutex& inflightLock; |
| 137 | InFlightRequestMap& inflightMap; // end of inflightLock scope |
| 138 | const bool useHalBufManager; |
| 139 | sp<NotificationListener> listener; |
| 140 | InflightRequestUpdateInterface& inflightIntf; |
| 141 | BufferRecordsInterface& bufferRecordsIntf; |
| 142 | FlushBufferInterface& flushBufferIntf; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 143 | SessionStatsBuilder& sessionStatsBuilder; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | void flushInflightRequests(FlushInflightReqStates& states); |
| 147 | } // namespace camera3 |
| 148 | |
| 149 | } // namespace android |
| 150 | |
| 151 | #endif |