blob: 5a5966b635c447e1895ac484ac394a7d73c131b9 [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 Sebechlebsky2f4478e2024-05-08 17:26:42 +020020#include <atomic>
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010021#include <cstdint>
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010022#include <deque>
23#include <future>
24#include <memory>
25#include <thread>
Jan Sebechlebsky18ac32c2024-06-07 09:53:53 +020026#include <variant>
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010027#include <vector>
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010028
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +010029#include "VirtualCameraDevice.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010030#include "VirtualCameraSessionContext.h"
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010031#include "aidl/android/hardware/camera/device/CameraMetadata.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010032#include "aidl/android/hardware/camera/device/ICameraDeviceCallback.h"
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010033#include "android/binder_auto_utils.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010034#include "util/EglDisplayContext.h"
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010035#include "util/EglFramebuffer.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010036#include "util/EglProgram.h"
37#include "util/EglSurfaceTexture.h"
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010038#include "util/Util.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010039
40namespace android {
41namespace companion {
42namespace virtualcamera {
43
44// Represents single output buffer of capture request.
45class CaptureRequestBuffer {
46 public:
47 CaptureRequestBuffer(int streamId, int bufferId, sp<Fence> fence = nullptr);
48
49 int getStreamId() const;
50 int getBufferId() const;
51 sp<Fence> getFence() const;
52
53 private:
54 const int mStreamId;
55 const int mBufferId;
56 const sp<Fence> mFence;
57};
58
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010059struct RequestSettings {
60 int jpegQuality = VirtualCameraDevice::kDefaultJpegQuality;
Vadim Caenc0aff132024-03-12 17:20:07 +010061 int jpegOrientation = VirtualCameraDevice::kDefaultJpegOrientation;
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010062 Resolution thumbnailResolution = Resolution(0, 0);
63 int thumbnailJpegQuality = VirtualCameraDevice::kDefaultJpegQuality;
Vadim Caenc0aff132024-03-12 17:20:07 +010064 std::optional<FpsRange> fpsRange;
Vadim Caen11dfd932024-03-05 09:57:20 +010065 camera_metadata_enum_android_control_capture_intent_t captureIntent =
66 VirtualCameraDevice::kDefaultCaptureIntent;
Vadim Caenc0aff132024-03-12 17:20:07 +010067 std::optional<GpsCoordinates> gpsCoordinates;
Vadim Caen6a43beb2024-04-12 15:06:42 +020068 std::optional<camera_metadata_enum_android_control_ae_precapture_trigger>
69 aePrecaptureTrigger;
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010070};
71
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010072// Represents single capture request to fill set of buffers.
73class ProcessCaptureRequestTask {
74 public:
75 ProcessCaptureRequestTask(
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010076 int frameNumber, const std::vector<CaptureRequestBuffer>& requestBuffers,
77 const RequestSettings& RequestSettings = {});
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010078
79 // Returns frame number corresponding to the request.
80 int getFrameNumber() const;
81
82 // Get reference to vector describing output buffers corresponding
83 // to this request.
84 //
85 // Note that the vector is owned by the ProcessCaptureRequestTask instance,
86 // so it cannot be access outside of its lifetime.
87 const std::vector<CaptureRequestBuffer>& getBuffers() const;
88
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010089 const RequestSettings& getRequestSettings() const;
90
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010091 private:
92 const int mFrameNumber;
93 const std::vector<CaptureRequestBuffer> mBuffers;
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010094 const RequestSettings mRequestSettings;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010095};
96
Jan Sebechlebsky18ac32c2024-06-07 09:53:53 +020097struct UpdateTextureTask {};
98
99struct RenderThreadTask
100 : public std::variant<std::unique_ptr<ProcessCaptureRequestTask>,
101 UpdateTextureTask> {
102 // Allow implicit conversion to bool.
103 //
104 // Returns false, if the RenderThreadTask consist of null
105 // ProcessCaptureRequestTask, which signals that the thread should terminate.
106 operator bool() const {
107 const bool isExitSignal =
108 std::holds_alternative<std::unique_ptr<ProcessCaptureRequestTask>>(
109 *this) &&
110 std::get<std::unique_ptr<ProcessCaptureRequestTask>>(*this) == nullptr;
111 return !isExitSignal;
112 }
113};
114
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100115// Wraps dedicated rendering thread and rendering business with corresponding
116// input surface.
117class VirtualCameraRenderThread {
118 public:
119 // Create VirtualCameraRenderThread instance:
120 // * sessionContext - VirtualCameraSessionContext reference for shared access
121 // to mapped buffers.
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100122 // * inputSurfaceSize - requested size of input surface.
123 // * reportedSensorSize - reported static sensor size of virtual camera.
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100124 // * cameraDeviceCallback - callback for corresponding camera instance
125 // * testMode - when set to true, test pattern is rendered to input surface
126 // before each capture request is processed to simulate client input.
127 VirtualCameraRenderThread(
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100128 VirtualCameraSessionContext& sessionContext, Resolution inputSurfaceSize,
129 Resolution reportedSensorSize,
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100130 std::shared_ptr<
131 ::aidl::android::hardware::camera::device::ICameraDeviceCallback>
Jan Sebechlebsky288900f2024-05-24 14:47:54 +0200132 cameraDeviceCallback);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100133
134 ~VirtualCameraRenderThread();
135
136 // Start rendering thread.
137 void start();
138 // Stop rendering thread.
139 void stop();
140
Jan Sebechlebsky18ac32c2024-06-07 09:53:53 +0200141 // Send request to render thread to update the texture.
142 // Currently queued buffers in the input surface will be consumed and the most
143 // recent buffer in the input surface will be attached to the texture), all
144 // other buffers will be returned to the buffer queue.
145 void requestTextureUpdate() EXCLUDES(mLock);
146
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100147 // Equeue capture task for processing on render thread.
148 void enqueueTask(std::unique_ptr<ProcessCaptureRequestTask> task)
149 EXCLUDES(mLock);
150
151 // Flush all in-flight requests.
152 void flush() EXCLUDES(mLock);
153
154 // Returns input surface corresponding to "virtual camera sensor".
155 sp<Surface> getInputSurface();
156
157 private:
Jan Sebechlebsky18ac32c2024-06-07 09:53:53 +0200158 RenderThreadTask dequeueTask() EXCLUDES(mLock);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100159
160 // Rendering thread entry point.
161 void threadLoop();
162
163 // Process single capture request task (always called on render thread).
Jan Sebechlebsky18ac32c2024-06-07 09:53:53 +0200164 void processTask(const ProcessCaptureRequestTask& captureRequestTask);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100165
166 // Flush single capture request task returning the error status immediately.
167 void flushCaptureRequest(const ProcessCaptureRequestTask& captureRequestTask);
168
169 // TODO(b/301023410) - Refactor the actual rendering logic off this class for
170 // easier testability.
171
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +0100172 // Create thumbnail with specified size for current image.
173 // The compressed image size is limited by 32KiB.
174 // Returns vector with compressed thumbnail if successful,
175 // empty vector otherwise.
176 std::vector<uint8_t> createThumbnail(Resolution resolution, int quality);
177
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100178 // Render current image to the BLOB buffer.
179 // If fence is specified, this function will block until the fence is cleared
180 // before writing to the buffer.
181 // Always called on render thread.
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +0100182 ndk::ScopedAStatus renderIntoBlobStreamBuffer(
183 const int streamId, const int bufferId,
Vadim Caenc0aff132024-03-12 17:20:07 +0100184 const ::aidl::android::hardware::camera::device::CameraMetadata&
185 resultMetadata,
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +0100186 const RequestSettings& requestSettings, sp<Fence> fence = nullptr);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100187
188 // Render current image to the YCbCr buffer.
189 // If fence is specified, this function will block until the fence is cleared
190 // before writing to the buffer.
191 // Always called on render thread.
192 ndk::ScopedAStatus renderIntoImageStreamBuffer(int streamId, int bufferId,
193 sp<Fence> fence = nullptr);
194
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +0100195 // Render current image into provided EglFramebuffer.
196 // If fence is specified, this function will block until the fence is cleared
197 // before writing to the buffer.
198 // Always called on the render thread.
Jan Sebechlebskyb3771312024-03-15 10:38:02 +0100199 ndk::ScopedAStatus renderIntoEglFramebuffer(
200 EglFrameBuffer& framebuffer, sp<Fence> fence = nullptr,
201 std::optional<Rect> viewport = std::nullopt);
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +0100202
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100203 // Camera callback
204 const std::shared_ptr<
205 ::aidl::android::hardware::camera::device::ICameraDeviceCallback>
206 mCameraDeviceCallback;
207
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100208 const Resolution mInputSurfaceSize;
209 const Resolution mReportedSensorSize;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100210
211 VirtualCameraSessionContext& mSessionContext;
212
213 std::thread mThread;
214
215 // Blocking queue implementation.
216 std::mutex mLock;
217 std::deque<std::unique_ptr<ProcessCaptureRequestTask>> mQueue GUARDED_BY(mLock);
218 std::condition_variable mCondVar;
Jan Sebechlebsky18ac32c2024-06-07 09:53:53 +0200219 volatile bool mTextureUpdateRequested GUARDED_BY(mLock);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100220 volatile bool mPendingExit GUARDED_BY(mLock);
221
Jan Sebechlebsky2f4478e2024-05-08 17:26:42 +0200222 // Acquisition timestamp of last frame.
223 std::atomic<uint64_t> mLastAcquisitionTimestampNanoseconds;
224
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100225 // EGL helpers - constructed and accessed only from rendering thread.
226 std::unique_ptr<EglDisplayContext> mEglDisplayContext;
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +0100227 std::unique_ptr<EglTextureProgram> mEglTextureYuvProgram;
228 std::unique_ptr<EglTextureProgram> mEglTextureRgbProgram;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100229 std::unique_ptr<EglSurfaceTexture> mEglSurfaceTexture;
230
231 std::promise<sp<Surface>> mInputSurfacePromise;
Jan Sebechlebsky9fcd0262024-05-31 15:20:09 +0200232 std::shared_future<sp<Surface>> mInputSurfaceFuture;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100233};
234
235} // namespace virtualcamera
236} // namespace companion
237} // namespace android
238
239#endif // ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERARENDERTHREAD_H