blob: 4d1eb75cdc1fafaa0300bd5cbaa1a224daf3e1d3 [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
Shuzhen Wang90708ea2021-11-04 11:40:49 -070029#include <android/hardware/camera/device/3.8/ICameraDeviceCallback.h>
30
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080031#include "device3/BufferUtils.h"
32#include "device3/DistortionMapper.h"
33#include "device3/ZoomRatioMapper.h"
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -080034#include "device3/RotateAndCropMapper.h"
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080035#include "device3/InFlightRequest.h"
36#include "device3/Camera3Stream.h"
37#include "device3/Camera3OutputStreamInterface.h"
Shuzhen Wang316781a2020-08-18 18:11:01 -070038#include "utils/SessionStatsBuilder.h"
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080039#include "utils/TagMonitor.h"
40
41namespace android {
42
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080043namespace camera3 {
44
45 /**
46 * Helper methods shared between Camera3Device/Camera3OfflineSession for HAL callbacks
47 */
Shuzhen Wangb7b42652020-05-07 11:59:02 -070048
49 // helper function to return the output buffers to output streams. The
50 // function also optionally calls notify(ERROR_BUFFER).
Greg Kaiser51b882c2020-06-10 05:41:44 +000051 void returnOutputBuffers(
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080052 bool useHalBufManager,
53 sp<NotificationListener> listener, // Only needed when outputSurfaces is not empty
Emilian Peevf4816702020-04-03 15:44:51 -070054 const camera_stream_buffer_t *outputBuffers,
Shuzhen Wang90708ea2021-11-04 11:40:49 -070055 size_t numBuffers, nsecs_t timestamp,
56 nsecs_t readoutTimestamp, bool requested, nsecs_t requestTimeNs,
Shuzhen Wang316781a2020-08-18 18:11:01 -070057 SessionStatsBuilder& sessionStatsBuilder, bool timestampIncreasing = true,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080058 // 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 Wangb7b42652020-05-07 11:59:02 -070061 const CaptureResultExtras &resultExtras = CaptureResultExtras{},
Emilian Peev5104fe92021-10-21 14:27:09 -070062 ERROR_BUF_STRATEGY errorBufStrategy = ERROR_BUF_RETURN,
63 int32_t transform = -1);
Shuzhen Wangb7b42652020-05-07 11:59:02 -070064
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 Wang316781a2020-08-18 18:11:01 -070071 InFlightRequest& request, SessionStatsBuilder& sessionStatsBuilder);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080072
73 // Camera3Device/Camera3OfflineSession internal states used in notify/processCaptureResult
74 // callbacks
75 struct CaptureOutputStates {
76 const String8& cameraId;
77 std::mutex& inflightLock;
Shuzhen Wangb7b42652020-05-07 11:59:02 -070078 int64_t& lastCompletedRegularFrameNumber;
Shuzhen Wangb7b42652020-05-07 11:59:02 -070079 int64_t& lastCompletedReprocessFrameNumber;
Shuzhen Wang218bce12020-10-27 18:24:23 -070080 int64_t& lastCompletedZslFrameNumber;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080081 InFlightRequestMap& inflightMap; // end of inflightLock scope
82 std::mutex& outputLock;
Jayant Chowdhary8a0be292020-01-08 13:10:38 -080083 std::list<CaptureResult>& resultQueue;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080084 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 Yeh5fd603e2019-11-20 11:22:27 -080098 std::unordered_map<std::string, camera3::DistortionMapper>& distortionMappers;
99 std::unordered_map<std::string, camera3::ZoomRatioMapper>& zoomRatioMappers;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800100 std::unordered_map<std::string, camera3::RotateAndCropMapper>& rotateAndCropMappers;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800101 TagMonitor& tagMonitor;
102 sp<Camera3Stream> inputStream;
103 StreamSet& outputStreams;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700104 SessionStatsBuilder& sessionStatsBuilder;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800105 sp<NotificationListener> listener;
106 SetErrorInterface& setErrIntf;
107 InflightRequestUpdateInterface& inflightIntf;
108 BufferRecordsInterface& bufferRecordsIntf;
Emilian Peev5104fe92021-10-21 14:27:09 -0700109 bool legacyClient;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800110 };
111
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800112 void processCaptureResult(CaptureOutputStates& states, const camera_capture_result *result);
113 void notify(CaptureOutputStates& states, const camera_notify_msg *msg);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800114
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 Wang316781a2020-08-18 18:11:01 -0700120 SessionStatsBuilder& sessionStatsBuilder;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800121 SetErrorInterface& setErrIntf;
122 BufferRecordsInterface& bufferRecordsIntf;
123 RequestBufferInterface& reqBufferIntf;
124 };
125
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800126 struct ReturnBufferStates {
127 const String8& cameraId;
128 const bool useHalBufManager;
129 StreamSet& outputStreams;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700130 SessionStatsBuilder& sessionStatsBuilder;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800131 BufferRecordsInterface& bufferRecordsIntf;
132 };
133
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800134 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 Wang316781a2020-08-18 18:11:01 -0700143 SessionStatsBuilder& sessionStatsBuilder;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800144 };
145
146 void flushInflightRequests(FlushInflightReqStates& states);
147} // namespace camera3
148
149} // namespace android
150
151#endif