blob: d6107c28e4de8200e39b4cd456c1a2830c639d6b [file] [log] [blame]
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001/*
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 Talvalaf2e37092020-01-07 15:32:32 -080032#include "device3/RotateAndCropMapper.h"
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080033#include "device3/InFlightRequest.h"
34#include "device3/Camera3Stream.h"
35#include "device3/Camera3OutputStreamInterface.h"
Shuzhen Wang316781a2020-08-18 18:11:01 -070036#include "utils/SessionStatsBuilder.h"
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080037#include "utils/TagMonitor.h"
38
39namespace android {
40
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080041namespace camera3 {
42
43 /**
44 * Helper methods shared between Camera3Device/Camera3OfflineSession for HAL callbacks
45 */
Shuzhen Wangb7b42652020-05-07 11:59:02 -070046
47 // helper function to return the output buffers to output streams. The
48 // function also optionally calls notify(ERROR_BUFFER).
Greg Kaiser51b882c2020-06-10 05:41:44 +000049 void returnOutputBuffers(
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080050 bool useHalBufManager,
51 sp<NotificationListener> listener, // Only needed when outputSurfaces is not empty
Emilian Peevf4816702020-04-03 15:44:51 -070052 const camera_stream_buffer_t *outputBuffers,
Shuzhen Wang90708ea2021-11-04 11:40:49 -070053 size_t numBuffers, nsecs_t timestamp,
54 nsecs_t readoutTimestamp, bool requested, nsecs_t requestTimeNs,
Shuzhen Wang316781a2020-08-18 18:11:01 -070055 SessionStatsBuilder& sessionStatsBuilder, bool timestampIncreasing = true,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080056 // 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
Shuzhen Wangb7b42652020-05-07 11:59:02 -070059 const CaptureResultExtras &resultExtras = CaptureResultExtras{},
Emilian Peev5104fe92021-10-21 14:27:09 -070060 ERROR_BUF_STRATEGY errorBufStrategy = ERROR_BUF_RETURN,
61 int32_t transform = -1);
Shuzhen Wangb7b42652020-05-07 11:59:02 -070062
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 Wang316781a2020-08-18 18:11:01 -070069 InFlightRequest& request, SessionStatsBuilder& sessionStatsBuilder);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080070
71 // Camera3Device/Camera3OfflineSession internal states used in notify/processCaptureResult
72 // callbacks
73 struct CaptureOutputStates {
74 const String8& cameraId;
75 std::mutex& inflightLock;
Shuzhen Wangb7b42652020-05-07 11:59:02 -070076 int64_t& lastCompletedRegularFrameNumber;
Shuzhen Wangb7b42652020-05-07 11:59:02 -070077 int64_t& lastCompletedReprocessFrameNumber;
Shuzhen Wang218bce12020-10-27 18:24:23 -070078 int64_t& lastCompletedZslFrameNumber;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080079 InFlightRequestMap& inflightMap; // end of inflightLock scope
80 std::mutex& outputLock;
Jayant Chowdhary8a0be292020-01-08 13:10:38 -080081 std::list<CaptureResult>& resultQueue;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080082 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;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080096 std::unordered_map<std::string, camera3::DistortionMapper>& distortionMappers;
97 std::unordered_map<std::string, camera3::ZoomRatioMapper>& zoomRatioMappers;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -080098 std::unordered_map<std::string, camera3::RotateAndCropMapper>& rotateAndCropMappers;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080099 TagMonitor& tagMonitor;
100 sp<Camera3Stream> inputStream;
101 StreamSet& outputStreams;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700102 SessionStatsBuilder& sessionStatsBuilder;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800103 sp<NotificationListener> listener;
104 SetErrorInterface& setErrIntf;
105 InflightRequestUpdateInterface& inflightIntf;
106 BufferRecordsInterface& bufferRecordsIntf;
Emilian Peev5104fe92021-10-21 14:27:09 -0700107 bool legacyClient;
Shuzhen Wang00abbeb2022-02-25 17:14:42 -0800108 nsecs_t& minFrameDuration;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800109 };
110
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800111 void processCaptureResult(CaptureOutputStates& states, const camera_capture_result *result);
112 void notify(CaptureOutputStates& states, const camera_notify_msg *msg);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800113
114 struct RequestBufferStates {
115 const String8& cameraId;
116 std::mutex& reqBufferLock; // lock to serialize request buffer calls
117 const bool useHalBufManager;
118 StreamSet& outputStreams;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700119 SessionStatsBuilder& sessionStatsBuilder;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800120 SetErrorInterface& setErrIntf;
121 BufferRecordsInterface& bufferRecordsIntf;
122 RequestBufferInterface& reqBufferIntf;
123 };
124
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800125 struct ReturnBufferStates {
126 const String8& cameraId;
127 const bool useHalBufManager;
128 StreamSet& outputStreams;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700129 SessionStatsBuilder& sessionStatsBuilder;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800130 BufferRecordsInterface& bufferRecordsIntf;
131 };
132
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800133 struct FlushInflightReqStates {
134 const String8& cameraId;
135 std::mutex& inflightLock;
136 InFlightRequestMap& inflightMap; // end of inflightLock scope
137 const bool useHalBufManager;
138 sp<NotificationListener> listener;
139 InflightRequestUpdateInterface& inflightIntf;
140 BufferRecordsInterface& bufferRecordsIntf;
141 FlushBufferInterface& flushBufferIntf;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700142 SessionStatsBuilder& sessionStatsBuilder;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800143 };
144
145 void flushInflightRequests(FlushInflightReqStates& states);
146} // namespace camera3
147
148} // namespace android
149
150#endif