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