blob: 53f78f2057b4fca28b46c2ad1a15d14ddf4b4bfd [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"
36#include "utils/TagMonitor.h"
37
38namespace android {
39
40using ResultMetadataQueue = hardware::MessageQueue<uint8_t, hardware::kSynchronizedReadWrite>;
41
42namespace camera3 {
43
44 /**
45 * Helper methods shared between Camera3Device/Camera3OfflineSession for HAL callbacks
46 */
47 // helper function to return the output buffers to output streams.
Shuzhen Wang730a7912020-05-07 11:59:02 -070048 // returns the number of STATUS_ERROR buffers
49 size_t returnOutputBuffers(
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080050 bool useHalBufManager,
51 sp<NotificationListener> listener, // Only needed when outputSurfaces is not empty
52 const camera3_stream_buffer_t *outputBuffers,
53 size_t numBuffers, nsecs_t timestamp, bool timestampIncreasing = true,
54 // The following arguments are only meant for surface sharing use case
55 const SurfaceMap& outputSurfaces = SurfaceMap{},
56 // Used to send buffer error callback when failing to return buffer
57 const CaptureResultExtras &resultExtras = CaptureResultExtras{});
58
59 // Camera3Device/Camera3OfflineSession internal states used in notify/processCaptureResult
60 // callbacks
61 struct CaptureOutputStates {
62 const String8& cameraId;
63 std::mutex& inflightLock;
Shuzhen Wang730a7912020-05-07 11:59:02 -070064 int64_t& lastCompletedRegularFrameNumber;
65 int64_t& lastCompletedZslFrameNumber;
66 int64_t& lastCompletedReprocessFrameNumber;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080067 InFlightRequestMap& inflightMap; // end of inflightLock scope
68 std::mutex& outputLock;
Jayant Chowdhary8a0be292020-01-08 13:10:38 -080069 std::list<CaptureResult>& resultQueue;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080070 std::condition_variable& resultSignal;
71 uint32_t& nextShutterFrameNum;
72 uint32_t& nextReprocShutterFrameNum;
73 uint32_t& nextZslShutterFrameNum;
74 uint32_t& nextResultFrameNum;
75 uint32_t& nextReprocResultFrameNum;
76 uint32_t& nextZslResultFrameNum; // end of outputLock scope
77 const bool useHalBufManager;
78 const bool usePartialResult;
79 const bool needFixupMonoChrome;
80 const uint32_t numPartialResults;
81 const metadata_vendor_id_t vendorTagId;
82 const CameraMetadata& deviceInfo;
83 const std::unordered_map<std::string, CameraMetadata>& physicalDeviceInfoMap;
84 std::unique_ptr<ResultMetadataQueue>& fmq;
85 std::unordered_map<std::string, camera3::DistortionMapper>& distortionMappers;
86 std::unordered_map<std::string, camera3::ZoomRatioMapper>& zoomRatioMappers;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -080087 std::unordered_map<std::string, camera3::RotateAndCropMapper>& rotateAndCropMappers;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080088 TagMonitor& tagMonitor;
89 sp<Camera3Stream> inputStream;
90 StreamSet& outputStreams;
91 sp<NotificationListener> listener;
92 SetErrorInterface& setErrIntf;
93 InflightRequestUpdateInterface& inflightIntf;
94 BufferRecordsInterface& bufferRecordsIntf;
95 };
96
97 // Handle one capture result. Assume callers hold the lock to serialize all
98 // processCaptureResult calls
99 void processOneCaptureResultLocked(
100 CaptureOutputStates& states,
101 const hardware::camera::device::V3_2::CaptureResult& result,
102 const hardware::hidl_vec<
103 hardware::camera::device::V3_4::PhysicalCameraMetadata> physicalCameraMetadata);
104
105 // Handle one notify message
106 void notify(CaptureOutputStates& states,
107 const hardware::camera::device::V3_2::NotifyMsg& msg);
108
109 struct RequestBufferStates {
110 const String8& cameraId;
111 std::mutex& reqBufferLock; // lock to serialize request buffer calls
112 const bool useHalBufManager;
113 StreamSet& outputStreams;
114 SetErrorInterface& setErrIntf;
115 BufferRecordsInterface& bufferRecordsIntf;
116 RequestBufferInterface& reqBufferIntf;
117 };
118
119 void requestStreamBuffers(RequestBufferStates& states,
120 const hardware::hidl_vec<hardware::camera::device::V3_5::BufferRequest>& bufReqs,
121 hardware::camera::device::V3_5::ICameraDeviceCallback::requestStreamBuffers_cb _hidl_cb);
122
123 struct ReturnBufferStates {
124 const String8& cameraId;
125 const bool useHalBufManager;
126 StreamSet& outputStreams;
127 BufferRecordsInterface& bufferRecordsIntf;
128 };
129
130 void returnStreamBuffers(ReturnBufferStates& states,
131 const hardware::hidl_vec<hardware::camera::device::V3_2::StreamBuffer>& buffers);
132
133 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;
142 };
143
144 void flushInflightRequests(FlushInflightReqStates& states);
145} // namespace camera3
146
147} // namespace android
148
149#endif