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