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