blob: 90757a057a2b88d403e595c4a8b25619dbcc0a30 [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;
Vadim Caenc0aff132024-03-12 17:20:07 +010060 int jpegOrientation = VirtualCameraDevice::kDefaultJpegOrientation;
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010061 Resolution thumbnailResolution = Resolution(0, 0);
62 int thumbnailJpegQuality = VirtualCameraDevice::kDefaultJpegQuality;
Vadim Caenc0aff132024-03-12 17:20:07 +010063 std::optional<FpsRange> fpsRange;
Vadim Caen11dfd932024-03-05 09:57:20 +010064 camera_metadata_enum_android_control_capture_intent_t captureIntent =
65 VirtualCameraDevice::kDefaultCaptureIntent;
Vadim Caenc0aff132024-03-12 17:20:07 +010066 std::optional<GpsCoordinates> gpsCoordinates;
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010067};
68
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010069// Represents single capture request to fill set of buffers.
70class ProcessCaptureRequestTask {
71 public:
72 ProcessCaptureRequestTask(
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010073 int frameNumber, const std::vector<CaptureRequestBuffer>& requestBuffers,
74 const RequestSettings& RequestSettings = {});
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010075
76 // Returns frame number corresponding to the request.
77 int getFrameNumber() const;
78
79 // Get reference to vector describing output buffers corresponding
80 // to this request.
81 //
82 // Note that the vector is owned by the ProcessCaptureRequestTask instance,
83 // so it cannot be access outside of its lifetime.
84 const std::vector<CaptureRequestBuffer>& getBuffers() const;
85
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010086 const RequestSettings& getRequestSettings() const;
87
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010088 private:
89 const int mFrameNumber;
90 const std::vector<CaptureRequestBuffer> mBuffers;
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010091 const RequestSettings mRequestSettings;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010092};
93
94// Wraps dedicated rendering thread and rendering business with corresponding
95// input surface.
96class VirtualCameraRenderThread {
97 public:
98 // Create VirtualCameraRenderThread instance:
99 // * sessionContext - VirtualCameraSessionContext reference for shared access
100 // to mapped buffers.
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100101 // * inputSurfaceSize - requested size of input surface.
102 // * reportedSensorSize - reported static sensor size of virtual camera.
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100103 // * cameraDeviceCallback - callback for corresponding camera instance
104 // * testMode - when set to true, test pattern is rendered to input surface
105 // before each capture request is processed to simulate client input.
106 VirtualCameraRenderThread(
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100107 VirtualCameraSessionContext& sessionContext, Resolution inputSurfaceSize,
108 Resolution reportedSensorSize,
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100109 std::shared_ptr<
110 ::aidl::android::hardware::camera::device::ICameraDeviceCallback>
111 cameraDeviceCallback,
112 bool testMode = false);
113
114 ~VirtualCameraRenderThread();
115
116 // Start rendering thread.
117 void start();
118 // Stop rendering thread.
119 void stop();
120
121 // Equeue capture task for processing on render thread.
122 void enqueueTask(std::unique_ptr<ProcessCaptureRequestTask> task)
123 EXCLUDES(mLock);
124
125 // Flush all in-flight requests.
126 void flush() EXCLUDES(mLock);
127
128 // Returns input surface corresponding to "virtual camera sensor".
129 sp<Surface> getInputSurface();
130
131 private:
132 std::unique_ptr<ProcessCaptureRequestTask> dequeueTask() EXCLUDES(mLock);
133
134 // Rendering thread entry point.
135 void threadLoop();
136
137 // Process single capture request task (always called on render thread).
138 void processCaptureRequest(const ProcessCaptureRequestTask& captureRequestTask);
139
140 // Flush single capture request task returning the error status immediately.
141 void flushCaptureRequest(const ProcessCaptureRequestTask& captureRequestTask);
142
143 // TODO(b/301023410) - Refactor the actual rendering logic off this class for
144 // easier testability.
145
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +0100146 // Create thumbnail with specified size for current image.
147 // The compressed image size is limited by 32KiB.
148 // Returns vector with compressed thumbnail if successful,
149 // empty vector otherwise.
150 std::vector<uint8_t> createThumbnail(Resolution resolution, int quality);
151
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100152 // Render current image to the BLOB buffer.
153 // If fence is specified, this function will block until the fence is cleared
154 // before writing to the buffer.
155 // Always called on render thread.
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +0100156 ndk::ScopedAStatus renderIntoBlobStreamBuffer(
157 const int streamId, const int bufferId,
Vadim Caenc0aff132024-03-12 17:20:07 +0100158 const ::aidl::android::hardware::camera::device::CameraMetadata&
159 resultMetadata,
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +0100160 const RequestSettings& requestSettings, sp<Fence> fence = nullptr);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100161
162 // Render current image to the YCbCr buffer.
163 // If fence is specified, this function will block until the fence is cleared
164 // before writing to the buffer.
165 // Always called on render thread.
166 ndk::ScopedAStatus renderIntoImageStreamBuffer(int streamId, int bufferId,
167 sp<Fence> fence = nullptr);
168
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +0100169 // Render current image into provided EglFramebuffer.
170 // If fence is specified, this function will block until the fence is cleared
171 // before writing to the buffer.
172 // Always called on the render thread.
Jan Sebechlebskyb3771312024-03-15 10:38:02 +0100173 ndk::ScopedAStatus renderIntoEglFramebuffer(
174 EglFrameBuffer& framebuffer, sp<Fence> fence = nullptr,
175 std::optional<Rect> viewport = std::nullopt);
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +0100176
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100177 // Camera callback
178 const std::shared_ptr<
179 ::aidl::android::hardware::camera::device::ICameraDeviceCallback>
180 mCameraDeviceCallback;
181
Jan Sebechlebskybb01c1d2024-02-12 11:41:37 +0100182 const Resolution mInputSurfaceSize;
183 const Resolution mReportedSensorSize;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100184 const int mTestMode;
185
186 VirtualCameraSessionContext& mSessionContext;
187
188 std::thread mThread;
189
190 // Blocking queue implementation.
191 std::mutex mLock;
192 std::deque<std::unique_ptr<ProcessCaptureRequestTask>> mQueue GUARDED_BY(mLock);
193 std::condition_variable mCondVar;
194 volatile bool mPendingExit GUARDED_BY(mLock);
195
196 // EGL helpers - constructed and accessed only from rendering thread.
197 std::unique_ptr<EglDisplayContext> mEglDisplayContext;
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +0100198 std::unique_ptr<EglTextureProgram> mEglTextureYuvProgram;
199 std::unique_ptr<EglTextureProgram> mEglTextureRgbProgram;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100200 std::unique_ptr<EglSurfaceTexture> mEglSurfaceTexture;
201
202 std::promise<sp<Surface>> mInputSurfacePromise;
203};
204
205} // namespace virtualcamera
206} // namespace companion
207} // namespace android
208
209#endif // ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERARENDERTHREAD_H