• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SessionStatsBuilder.h"
37 #include "utils/TagMonitor.h"
38 
39 namespace android {
40 
41 namespace camera3 {
42 
43     /**
44      * Helper methods shared between Camera3Device/Camera3OfflineSession for HAL callbacks
45      */
46 
47     // helper function to return the output buffers to output streams. The
48     // function also optionally calls notify(ERROR_BUFFER).
49     void returnOutputBuffers(
50             bool useHalBufManager,
51             sp<NotificationListener> listener, // Only needed when outputSurfaces is not empty
52             const camera_stream_buffer_t *outputBuffers,
53             size_t numBuffers, nsecs_t timestamp,
54             nsecs_t readoutTimestamp, bool requested, nsecs_t requestTimeNs,
55             SessionStatsBuilder& sessionStatsBuilder, bool timestampIncreasing = true,
56             // The following arguments are only meant for surface sharing use case
57             const SurfaceMap& outputSurfaces = SurfaceMap{},
58             // Used to send buffer error callback when failing to return buffer
59             const CaptureResultExtras &resultExtras = CaptureResultExtras{},
60             ERROR_BUF_STRATEGY errorBufStrategy = ERROR_BUF_RETURN,
61             int32_t transform = -1);
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
69             InFlightRequest& request, SessionStatsBuilder& sessionStatsBuilder);
70 
71     // Camera3Device/Camera3OfflineSession internal states used in notify/processCaptureResult
72     // callbacks
73     struct CaptureOutputStates {
74         const String8& cameraId;
75         std::mutex& inflightLock;
76         int64_t& lastCompletedRegularFrameNumber;
77         int64_t& lastCompletedReprocessFrameNumber;
78         int64_t& lastCompletedZslFrameNumber;
79         InFlightRequestMap& inflightMap; // end of inflightLock scope
80         std::mutex& outputLock;
81         std::list<CaptureResult>& resultQueue;
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::unordered_map<std::string, camera3::DistortionMapper>& distortionMappers;
97         std::unordered_map<std::string, camera3::ZoomRatioMapper>& zoomRatioMappers;
98         std::unordered_map<std::string, camera3::RotateAndCropMapper>& rotateAndCropMappers;
99         TagMonitor& tagMonitor;
100         sp<Camera3Stream> inputStream;
101         StreamSet& outputStreams;
102         SessionStatsBuilder& sessionStatsBuilder;
103         sp<NotificationListener> listener;
104         SetErrorInterface& setErrIntf;
105         InflightRequestUpdateInterface& inflightIntf;
106         BufferRecordsInterface& bufferRecordsIntf;
107         bool legacyClient;
108         nsecs_t& minFrameDuration;
109         bool& isFixedFps;
110         bool overrideToPortrait;
111         std::string &activePhysicalId;
112     };
113 
114     void processCaptureResult(CaptureOutputStates& states, const camera_capture_result *result);
115     void notify(CaptureOutputStates& states, const camera_notify_msg *msg);
116 
117     struct RequestBufferStates {
118         const String8& cameraId;
119         std::mutex& reqBufferLock; // lock to serialize request buffer calls
120         const bool useHalBufManager;
121         StreamSet& outputStreams;
122         SessionStatsBuilder& sessionStatsBuilder;
123         SetErrorInterface& setErrIntf;
124         BufferRecordsInterface& bufferRecordsIntf;
125         RequestBufferInterface& reqBufferIntf;
126     };
127 
128     struct ReturnBufferStates {
129         const String8& cameraId;
130         const bool useHalBufManager;
131         StreamSet& outputStreams;
132         SessionStatsBuilder& sessionStatsBuilder;
133         BufferRecordsInterface& bufferRecordsIntf;
134     };
135 
136     struct FlushInflightReqStates {
137         const String8& cameraId;
138         std::mutex& inflightLock;
139         InFlightRequestMap& inflightMap; // end of inflightLock scope
140         const bool useHalBufManager;
141         sp<NotificationListener> listener;
142         InflightRequestUpdateInterface& inflightIntf;
143         BufferRecordsInterface& bufferRecordsIntf;
144         FlushBufferInterface& flushBufferIntf;
145         SessionStatsBuilder& sessionStatsBuilder;
146     };
147 
148     void flushInflightRequests(FlushInflightReqStates& states);
149 } // namespace camera3
150 
151 } // namespace android
152 
153 #endif
154