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