blob: e8e0bd4c6fe947365b2a0238a3513d731a70d88e [file] [log] [blame]
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +01001/*
2 * Copyright (C) 2023 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_COMPANION_VIRTUALCAMERA_VIRTUALCAMERARENDERTHREAD_H
18#define ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERARENDERTHREAD_H
19
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010020#include <cstdint>
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010021#include <deque>
22#include <future>
23#include <memory>
24#include <thread>
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010025#include <vector>
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010026
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +010027#include "VirtualCameraDevice.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010028#include "VirtualCameraSessionContext.h"
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010029#include "aidl/android/hardware/camera/device/CameraMetadata.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010030#include "aidl/android/hardware/camera/device/ICameraDeviceCallback.h"
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010031#include "android/binder_auto_utils.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010032#include "util/EglDisplayContext.h"
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010033#include "util/EglFramebuffer.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010034#include "util/EglProgram.h"
35#include "util/EglSurfaceTexture.h"
Vadim Caen11dfd932024-03-05 09:57:20 +010036#include "util/MetadataUtil.h"
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010037#include "util/Util.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010038
39namespace android {
40namespace companion {
41namespace virtualcamera {
42
43// Represents single output buffer of capture request.
44class CaptureRequestBuffer {
45 public:
46 CaptureRequestBuffer(int streamId, int bufferId, sp<Fence> fence = nullptr);
47
48 int getStreamId() const;
49 int getBufferId() const;
50 sp<Fence> getFence() const;
51
52 private:
53 const int mStreamId;
54 const int mBufferId;
55 const sp<Fence> mFence;
56};
57
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010058struct RequestSettings {
59 int jpegQuality = VirtualCameraDevice::kDefaultJpegQuality;
60 Resolution thumbnailResolution = Resolution(0, 0);
61 int thumbnailJpegQuality = VirtualCameraDevice::kDefaultJpegQuality;
Vadim Caen11dfd932024-03-05 09:57:20 +010062 std::optional<FpsRange> fpsRange = {};
63 camera_metadata_enum_android_control_capture_intent_t captureIntent =
64 VirtualCameraDevice::kDefaultCaptureIntent;
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010065};
66
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010067// Represents single capture request to fill set of buffers.
68class ProcessCaptureRequestTask {
69 public:
70 ProcessCaptureRequestTask(
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010071 int frameNumber, const std::vector<CaptureRequestBuffer>& requestBuffers,
72 const RequestSettings& RequestSettings = {});
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010073
74 // Returns frame number corresponding to the request.
75 int getFrameNumber() const;
76
77 // Get reference to vector describing output buffers corresponding
78 // to this request.
79 //
80 // Note that the vector is owned by the ProcessCaptureRequestTask instance,
81 // so it cannot be access outside of its lifetime.
82 const std::vector<CaptureRequestBuffer>& getBuffers() const;
83
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010084 const RequestSettings& getRequestSettings() const;
85
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010086 private:
87 const int mFrameNumber;
88 const std::vector<CaptureRequestBuffer> mBuffers;
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010089 const RequestSettings mRequestSettings;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010090};
91
92// Wraps dedicated rendering thread and rendering business with corresponding
93// input surface.
94class VirtualCameraRenderThread {
95 public:
96 // Create VirtualCameraRenderThread instance:
97 // * sessionContext - VirtualCameraSessionContext reference for shared access
98 // to mapped buffers.
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +010099 // * inputSurfaceSize - requested size of input surface.
100 // * reportedSensorSize - reported static sensor size of virtual camera.
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100101 // * cameraDeviceCallback - callback for corresponding camera instance
102 // * testMode - when set to true, test pattern is rendered to input surface
103 // before each capture request is processed to simulate client input.
104 VirtualCameraRenderThread(
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100105 VirtualCameraSessionContext& sessionContext, Resolution inputSurfaceSize,
106 Resolution reportedSensorSize,
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100107 std::shared_ptr<
108 ::aidl::android::hardware::camera::device::ICameraDeviceCallback>
109 cameraDeviceCallback,
110 bool testMode = false);
111
112 ~VirtualCameraRenderThread();
113
114 // Start rendering thread.
115 void start();
116 // Stop rendering thread.
117 void stop();
118
119 // Equeue capture task for processing on render thread.
120 void enqueueTask(std::unique_ptr<ProcessCaptureRequestTask> task)
121 EXCLUDES(mLock);
122
123 // Flush all in-flight requests.
124 void flush() EXCLUDES(mLock);
125
126 // Returns input surface corresponding to "virtual camera sensor".
127 sp<Surface> getInputSurface();
128
129 private:
130 std::unique_ptr<ProcessCaptureRequestTask> dequeueTask() EXCLUDES(mLock);
131
132 // Rendering thread entry point.
133 void threadLoop();
134
135 // Process single capture request task (always called on render thread).
136 void processCaptureRequest(const ProcessCaptureRequestTask& captureRequestTask);
137
138 // Flush single capture request task returning the error status immediately.
139 void flushCaptureRequest(const ProcessCaptureRequestTask& captureRequestTask);
140
141 // TODO(b/301023410) - Refactor the actual rendering logic off this class for
142 // easier testability.
143
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +0100144 // Create thumbnail with specified size for current image.
145 // The compressed image size is limited by 32KiB.
146 // Returns vector with compressed thumbnail if successful,
147 // empty vector otherwise.
148 std::vector<uint8_t> createThumbnail(Resolution resolution, int quality);
149
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100150 // Render current image to the BLOB buffer.
151 // If fence is specified, this function will block until the fence is cleared
152 // before writing to the buffer.
153 // Always called on render thread.
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +0100154 ndk::ScopedAStatus renderIntoBlobStreamBuffer(
155 const int streamId, const int bufferId,
156 const RequestSettings& requestSettings, sp<Fence> fence = nullptr);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100157
158 // Render current image to the YCbCr buffer.
159 // If fence is specified, this function will block until the fence is cleared
160 // before writing to the buffer.
161 // Always called on render thread.
162 ndk::ScopedAStatus renderIntoImageStreamBuffer(int streamId, int bufferId,
163 sp<Fence> fence = nullptr);
164
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +0100165 // Render current image into provided EglFramebuffer.
166 // If fence is specified, this function will block until the fence is cleared
167 // before writing to the buffer.
168 // Always called on the render thread.
169 ndk::ScopedAStatus renderIntoEglFramebuffer(EglFrameBuffer& framebuffer,
170 sp<Fence> fence = nullptr);
171
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100172 // Camera callback
173 const std::shared_ptr<
174 ::aidl::android::hardware::camera::device::ICameraDeviceCallback>
175 mCameraDeviceCallback;
176
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100177 const Resolution mInputSurfaceSize;
178 const Resolution mReportedSensorSize;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100179 const int mTestMode;
180
181 VirtualCameraSessionContext& mSessionContext;
182
183 std::thread mThread;
184
185 // Blocking queue implementation.
186 std::mutex mLock;
187 std::deque<std::unique_ptr<ProcessCaptureRequestTask>> mQueue GUARDED_BY(mLock);
188 std::condition_variable mCondVar;
189 volatile bool mPendingExit GUARDED_BY(mLock);
190
191 // EGL helpers - constructed and accessed only from rendering thread.
192 std::unique_ptr<EglDisplayContext> mEglDisplayContext;
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +0100193 std::unique_ptr<EglTextureProgram> mEglTextureYuvProgram;
194 std::unique_ptr<EglTextureProgram> mEglTextureRgbProgram;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100195 std::unique_ptr<EglSurfaceTexture> mEglSurfaceTexture;
196
197 std::promise<sp<Surface>> mInputSurfacePromise;
198};
199
200} // namespace virtualcamera
201} // namespace companion
202} // namespace android
203
204#endif // ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERARENDERTHREAD_H