Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 1 | /* |
| 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 Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame^] | 20 | #include <cstdint> |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 21 | #include <deque> |
| 22 | #include <future> |
| 23 | #include <memory> |
| 24 | #include <thread> |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame^] | 25 | #include <vector> |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 26 | |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 27 | #include "VirtualCameraDevice.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 28 | #include "VirtualCameraSessionContext.h" |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame^] | 29 | #include "aidl/android/hardware/camera/device/CameraMetadata.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 30 | #include "aidl/android/hardware/camera/device/ICameraDeviceCallback.h" |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 31 | #include "android/binder_auto_utils.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 32 | #include "util/EglDisplayContext.h" |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 33 | #include "util/EglFramebuffer.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 34 | #include "util/EglProgram.h" |
| 35 | #include "util/EglSurfaceTexture.h" |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame^] | 36 | #include "util/Util.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 37 | |
| 38 | namespace android { |
| 39 | namespace companion { |
| 40 | namespace virtualcamera { |
| 41 | |
| 42 | // Represents single output buffer of capture request. |
| 43 | class CaptureRequestBuffer { |
| 44 | public: |
| 45 | CaptureRequestBuffer(int streamId, int bufferId, sp<Fence> fence = nullptr); |
| 46 | |
| 47 | int getStreamId() const; |
| 48 | int getBufferId() const; |
| 49 | sp<Fence> getFence() const; |
| 50 | |
| 51 | private: |
| 52 | const int mStreamId; |
| 53 | const int mBufferId; |
| 54 | const sp<Fence> mFence; |
| 55 | }; |
| 56 | |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame^] | 57 | struct RequestSettings { |
| 58 | int jpegQuality = VirtualCameraDevice::kDefaultJpegQuality; |
| 59 | Resolution thumbnailResolution = Resolution(0, 0); |
| 60 | int thumbnailJpegQuality = VirtualCameraDevice::kDefaultJpegQuality; |
| 61 | }; |
| 62 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 63 | // Represents single capture request to fill set of buffers. |
| 64 | class ProcessCaptureRequestTask { |
| 65 | public: |
| 66 | ProcessCaptureRequestTask( |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame^] | 67 | int frameNumber, const std::vector<CaptureRequestBuffer>& requestBuffers, |
| 68 | const RequestSettings& RequestSettings = {}); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 69 | |
| 70 | // Returns frame number corresponding to the request. |
| 71 | int getFrameNumber() const; |
| 72 | |
| 73 | // Get reference to vector describing output buffers corresponding |
| 74 | // to this request. |
| 75 | // |
| 76 | // Note that the vector is owned by the ProcessCaptureRequestTask instance, |
| 77 | // so it cannot be access outside of its lifetime. |
| 78 | const std::vector<CaptureRequestBuffer>& getBuffers() const; |
| 79 | |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame^] | 80 | const RequestSettings& getRequestSettings() const; |
| 81 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 82 | private: |
| 83 | const int mFrameNumber; |
| 84 | const std::vector<CaptureRequestBuffer> mBuffers; |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame^] | 85 | const RequestSettings mRequestSettings; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | // Wraps dedicated rendering thread and rendering business with corresponding |
| 89 | // input surface. |
| 90 | class VirtualCameraRenderThread { |
| 91 | public: |
| 92 | // Create VirtualCameraRenderThread instance: |
| 93 | // * sessionContext - VirtualCameraSessionContext reference for shared access |
| 94 | // to mapped buffers. |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 95 | // * inputSurfaceSize - requested size of input surface. |
| 96 | // * reportedSensorSize - reported static sensor size of virtual camera. |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 97 | // * cameraDeviceCallback - callback for corresponding camera instance |
| 98 | // * testMode - when set to true, test pattern is rendered to input surface |
| 99 | // before each capture request is processed to simulate client input. |
| 100 | VirtualCameraRenderThread( |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 101 | VirtualCameraSessionContext& sessionContext, Resolution inputSurfaceSize, |
| 102 | Resolution reportedSensorSize, |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 103 | std::shared_ptr< |
| 104 | ::aidl::android::hardware::camera::device::ICameraDeviceCallback> |
| 105 | cameraDeviceCallback, |
| 106 | bool testMode = false); |
| 107 | |
| 108 | ~VirtualCameraRenderThread(); |
| 109 | |
| 110 | // Start rendering thread. |
| 111 | void start(); |
| 112 | // Stop rendering thread. |
| 113 | void stop(); |
| 114 | |
| 115 | // Equeue capture task for processing on render thread. |
| 116 | void enqueueTask(std::unique_ptr<ProcessCaptureRequestTask> task) |
| 117 | EXCLUDES(mLock); |
| 118 | |
| 119 | // Flush all in-flight requests. |
| 120 | void flush() EXCLUDES(mLock); |
| 121 | |
| 122 | // Returns input surface corresponding to "virtual camera sensor". |
| 123 | sp<Surface> getInputSurface(); |
| 124 | |
| 125 | private: |
| 126 | std::unique_ptr<ProcessCaptureRequestTask> dequeueTask() EXCLUDES(mLock); |
| 127 | |
| 128 | // Rendering thread entry point. |
| 129 | void threadLoop(); |
| 130 | |
| 131 | // Process single capture request task (always called on render thread). |
| 132 | void processCaptureRequest(const ProcessCaptureRequestTask& captureRequestTask); |
| 133 | |
| 134 | // Flush single capture request task returning the error status immediately. |
| 135 | void flushCaptureRequest(const ProcessCaptureRequestTask& captureRequestTask); |
| 136 | |
| 137 | // TODO(b/301023410) - Refactor the actual rendering logic off this class for |
| 138 | // easier testability. |
| 139 | |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame^] | 140 | // Create thumbnail with specified size for current image. |
| 141 | // The compressed image size is limited by 32KiB. |
| 142 | // Returns vector with compressed thumbnail if successful, |
| 143 | // empty vector otherwise. |
| 144 | std::vector<uint8_t> createThumbnail(Resolution resolution, int quality); |
| 145 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 146 | // Render current image to the BLOB buffer. |
| 147 | // If fence is specified, this function will block until the fence is cleared |
| 148 | // before writing to the buffer. |
| 149 | // Always called on render thread. |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame^] | 150 | ndk::ScopedAStatus renderIntoBlobStreamBuffer( |
| 151 | const int streamId, const int bufferId, |
| 152 | const RequestSettings& requestSettings, sp<Fence> fence = nullptr); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 153 | |
| 154 | // Render current image to the YCbCr buffer. |
| 155 | // If fence is specified, this function will block until the fence is cleared |
| 156 | // before writing to the buffer. |
| 157 | // Always called on render thread. |
| 158 | ndk::ScopedAStatus renderIntoImageStreamBuffer(int streamId, int bufferId, |
| 159 | sp<Fence> fence = nullptr); |
| 160 | |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 161 | // Render current image into provided EglFramebuffer. |
| 162 | // If fence is specified, this function will block until the fence is cleared |
| 163 | // before writing to the buffer. |
| 164 | // Always called on the render thread. |
| 165 | ndk::ScopedAStatus renderIntoEglFramebuffer(EglFrameBuffer& framebuffer, |
| 166 | sp<Fence> fence = nullptr); |
| 167 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 168 | // Camera callback |
| 169 | const std::shared_ptr< |
| 170 | ::aidl::android::hardware::camera::device::ICameraDeviceCallback> |
| 171 | mCameraDeviceCallback; |
| 172 | |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 173 | const Resolution mInputSurfaceSize; |
| 174 | const Resolution mReportedSensorSize; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 175 | const int mTestMode; |
| 176 | |
| 177 | VirtualCameraSessionContext& mSessionContext; |
| 178 | |
| 179 | std::thread mThread; |
| 180 | |
| 181 | // Blocking queue implementation. |
| 182 | std::mutex mLock; |
| 183 | std::deque<std::unique_ptr<ProcessCaptureRequestTask>> mQueue GUARDED_BY(mLock); |
| 184 | std::condition_variable mCondVar; |
| 185 | volatile bool mPendingExit GUARDED_BY(mLock); |
| 186 | |
| 187 | // EGL helpers - constructed and accessed only from rendering thread. |
| 188 | std::unique_ptr<EglDisplayContext> mEglDisplayContext; |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 189 | std::unique_ptr<EglTextureProgram> mEglTextureYuvProgram; |
| 190 | std::unique_ptr<EglTextureProgram> mEglTextureRgbProgram; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 191 | std::unique_ptr<EglSurfaceTexture> mEglSurfaceTexture; |
| 192 | |
| 193 | std::promise<sp<Surface>> mInputSurfacePromise; |
| 194 | }; |
| 195 | |
| 196 | } // namespace virtualcamera |
| 197 | } // namespace companion |
| 198 | } // namespace android |
| 199 | |
| 200 | #endif // ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERARENDERTHREAD_H |