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 | #define LOG_TAG "VirtualCameraRenderThread" |
| 18 | #include "VirtualCameraRenderThread.h" |
| 19 | |
| 20 | #include <chrono> |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 21 | #include <cstdint> |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 22 | #include <cstring> |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 23 | #include <future> |
| 24 | #include <memory> |
| 25 | #include <mutex> |
| 26 | #include <thread> |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 27 | #include <vector> |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 28 | |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 29 | #include "Exif.h" |
Jan Sebechlebsky | 9ae496f | 2023-12-05 15:56:28 +0100 | [diff] [blame] | 30 | #include "GLES/gl.h" |
Biswarup Pal | 8ad8bc5 | 2024-02-08 13:41:44 +0000 | [diff] [blame] | 31 | #include "VirtualCameraDevice.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 32 | #include "VirtualCameraSessionContext.h" |
| 33 | #include "aidl/android/hardware/camera/common/Status.h" |
| 34 | #include "aidl/android/hardware/camera/device/BufferStatus.h" |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 35 | #include "aidl/android/hardware/camera/device/CameraBlob.h" |
| 36 | #include "aidl/android/hardware/camera/device/CameraBlobId.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 37 | #include "aidl/android/hardware/camera/device/CameraMetadata.h" |
| 38 | #include "aidl/android/hardware/camera/device/CaptureResult.h" |
| 39 | #include "aidl/android/hardware/camera/device/ErrorCode.h" |
| 40 | #include "aidl/android/hardware/camera/device/ICameraDeviceCallback.h" |
| 41 | #include "aidl/android/hardware/camera/device/NotifyMsg.h" |
| 42 | #include "aidl/android/hardware/camera/device/ShutterMsg.h" |
| 43 | #include "aidl/android/hardware/camera/device/StreamBuffer.h" |
| 44 | #include "android-base/thread_annotations.h" |
| 45 | #include "android/binder_auto_utils.h" |
| 46 | #include "android/hardware_buffer.h" |
Jan Sebechlebsky | 2f4478e | 2024-05-08 17:26:42 +0200 | [diff] [blame] | 47 | #include "hardware/gralloc.h" |
Vadim Caen | c0aff13 | 2024-03-12 17:20:07 +0100 | [diff] [blame] | 48 | #include "system/camera_metadata.h" |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 49 | #include "ui/GraphicBuffer.h" |
Jan Sebechlebsky | b377131 | 2024-03-15 10:38:02 +0100 | [diff] [blame] | 50 | #include "ui/Rect.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 51 | #include "util/EglFramebuffer.h" |
| 52 | #include "util/JpegUtil.h" |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 53 | #include "util/MetadataUtil.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 54 | #include "util/Util.h" |
| 55 | #include "utils/Errors.h" |
| 56 | |
| 57 | namespace android { |
| 58 | namespace companion { |
| 59 | namespace virtualcamera { |
| 60 | |
| 61 | using ::aidl::android::hardware::camera::common::Status; |
| 62 | using ::aidl::android::hardware::camera::device::BufferStatus; |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 63 | using ::aidl::android::hardware::camera::device::CameraBlob; |
| 64 | using ::aidl::android::hardware::camera::device::CameraBlobId; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 65 | using ::aidl::android::hardware::camera::device::CameraMetadata; |
| 66 | using ::aidl::android::hardware::camera::device::CaptureResult; |
| 67 | using ::aidl::android::hardware::camera::device::ErrorCode; |
| 68 | using ::aidl::android::hardware::camera::device::ErrorMsg; |
| 69 | using ::aidl::android::hardware::camera::device::ICameraDeviceCallback; |
| 70 | using ::aidl::android::hardware::camera::device::NotifyMsg; |
| 71 | using ::aidl::android::hardware::camera::device::ShutterMsg; |
| 72 | using ::aidl::android::hardware::camera::device::Stream; |
| 73 | using ::aidl::android::hardware::camera::device::StreamBuffer; |
| 74 | using ::aidl::android::hardware::graphics::common::PixelFormat; |
| 75 | using ::android::base::ScopedLockAssertion; |
| 76 | |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 77 | using ::android::hardware::camera::common::helper::ExifUtils; |
| 78 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 79 | namespace { |
| 80 | |
| 81 | using namespace std::chrono_literals; |
| 82 | |
| 83 | static constexpr std::chrono::milliseconds kAcquireFenceTimeout = 500ms; |
| 84 | |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 85 | // See REQUEST_PIPELINE_DEPTH in CaptureResult.java. |
| 86 | // This roughly corresponds to frame latency, we set to |
| 87 | // documented minimum of 2. |
| 88 | static constexpr uint8_t kPipelineDepth = 2; |
| 89 | |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 90 | static constexpr size_t kJpegThumbnailBufferSize = 32 * 1024; // 32 KiB |
| 91 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 92 | CameraMetadata createCaptureResultMetadata( |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 93 | const std::chrono::nanoseconds timestamp, |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 94 | const RequestSettings& requestSettings, |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 95 | const Resolution reportedSensorSize) { |
Vadim Caen | 11dfd93 | 2024-03-05 09:57:20 +0100 | [diff] [blame] | 96 | // All of the keys used in the response needs to be referenced in |
| 97 | // availableResultKeys in CameraCharacteristics (see initCameraCharacteristics |
| 98 | // in VirtualCameraDevice.cc). |
| 99 | MetadataBuilder builder = |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 100 | MetadataBuilder() |
Jan Sebechlebsky | 4be2bd0 | 2024-02-26 18:35:18 +0100 | [diff] [blame] | 101 | .setAberrationCorrectionMode( |
| 102 | ANDROID_COLOR_CORRECTION_ABERRATION_MODE_OFF) |
Vadim Caen | 11dfd93 | 2024-03-05 09:57:20 +0100 | [diff] [blame] | 103 | .setControlAeAvailableAntibandingModes( |
| 104 | {ANDROID_CONTROL_AE_ANTIBANDING_MODE_OFF}) |
| 105 | .setControlAeAntibandingMode(ANDROID_CONTROL_AE_ANTIBANDING_MODE_OFF) |
| 106 | .setControlAeExposureCompensation(0) |
| 107 | .setControlAeLockAvailable(false) |
| 108 | .setControlAeLock(ANDROID_CONTROL_AE_LOCK_OFF) |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 109 | .setControlAeMode(ANDROID_CONTROL_AE_MODE_ON) |
| 110 | .setControlAePrecaptureTrigger( |
Vadim Caen | 6a43beb | 2024-04-12 15:06:42 +0200 | [diff] [blame] | 111 | // Limited devices are expected to have precapture ae enabled and |
| 112 | // respond to cancellation request. Since we don't actuall support |
| 113 | // AE at all, let's just respect the cancellation expectation in |
| 114 | // case it's requested |
| 115 | requestSettings.aePrecaptureTrigger == |
| 116 | ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL |
| 117 | ? ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL |
| 118 | : ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE) |
Vadim Caen | 11dfd93 | 2024-03-05 09:57:20 +0100 | [diff] [blame] | 119 | .setControlAeState(ANDROID_CONTROL_AE_STATE_INACTIVE) |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 120 | .setControlAfMode(ANDROID_CONTROL_AF_MODE_OFF) |
Vadim Caen | 11dfd93 | 2024-03-05 09:57:20 +0100 | [diff] [blame] | 121 | .setControlAfTrigger(ANDROID_CONTROL_AF_TRIGGER_IDLE) |
| 122 | .setControlAfState(ANDROID_CONTROL_AF_STATE_INACTIVE) |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 123 | .setControlAwbMode(ANDROID_CONTROL_AWB_MODE_AUTO) |
Vadim Caen | 11dfd93 | 2024-03-05 09:57:20 +0100 | [diff] [blame] | 124 | .setControlAwbLock(ANDROID_CONTROL_AWB_LOCK_OFF) |
| 125 | .setControlAwbState(ANDROID_CONTROL_AWB_STATE_INACTIVE) |
| 126 | .setControlCaptureIntent(requestSettings.captureIntent) |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 127 | .setControlEffectMode(ANDROID_CONTROL_EFFECT_MODE_OFF) |
| 128 | .setControlMode(ANDROID_CONTROL_MODE_AUTO) |
Vadim Caen | 11dfd93 | 2024-03-05 09:57:20 +0100 | [diff] [blame] | 129 | .setControlSceneMode(ANDROID_CONTROL_SCENE_MODE_DISABLED) |
| 130 | .setControlVideoStabilizationMode( |
| 131 | ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF) |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 132 | .setCropRegion(0, 0, reportedSensorSize.width, |
| 133 | reportedSensorSize.height) |
| 134 | .setFaceDetectMode(ANDROID_STATISTICS_FACE_DETECT_MODE_OFF) |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 135 | .setFlashState(ANDROID_FLASH_STATE_UNAVAILABLE) |
Vadim Caen | 11dfd93 | 2024-03-05 09:57:20 +0100 | [diff] [blame] | 136 | .setFlashMode(ANDROID_FLASH_MODE_OFF) |
Biswarup Pal | 8ad8bc5 | 2024-02-08 13:41:44 +0000 | [diff] [blame] | 137 | .setFocalLength(VirtualCameraDevice::kFocalLength) |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 138 | .setJpegQuality(requestSettings.jpegQuality) |
Vadim Caen | c0aff13 | 2024-03-12 17:20:07 +0100 | [diff] [blame] | 139 | .setJpegOrientation(requestSettings.jpegOrientation) |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 140 | .setJpegThumbnailSize(requestSettings.thumbnailResolution.width, |
| 141 | requestSettings.thumbnailResolution.height) |
| 142 | .setJpegThumbnailQuality(requestSettings.thumbnailJpegQuality) |
Vadim Caen | 11dfd93 | 2024-03-05 09:57:20 +0100 | [diff] [blame] | 143 | .setLensOpticalStabilizationMode( |
| 144 | ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF) |
Jan Sebechlebsky | 4be2bd0 | 2024-02-26 18:35:18 +0100 | [diff] [blame] | 145 | .setNoiseReductionMode(ANDROID_NOISE_REDUCTION_MODE_OFF) |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 146 | .setPipelineDepth(kPipelineDepth) |
Jan Sebechlebsky | c3e1a63 | 2024-02-06 14:19:05 +0100 | [diff] [blame] | 147 | .setSensorTimestamp(timestamp) |
Vadim Caen | 11dfd93 | 2024-03-05 09:57:20 +0100 | [diff] [blame] | 148 | .setStatisticsHotPixelMapMode( |
| 149 | ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF) |
| 150 | .setStatisticsLensShadingMapMode( |
| 151 | ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF) |
| 152 | .setStatisticsSceneFlicker(ANDROID_STATISTICS_SCENE_FLICKER_NONE); |
| 153 | |
| 154 | if (requestSettings.fpsRange.has_value()) { |
| 155 | builder.setControlAeTargetFpsRange(requestSettings.fpsRange.value()); |
| 156 | } |
| 157 | |
Vadim Caen | c0aff13 | 2024-03-12 17:20:07 +0100 | [diff] [blame] | 158 | if (requestSettings.gpsCoordinates.has_value()) { |
| 159 | const GpsCoordinates& coordinates = requestSettings.gpsCoordinates.value(); |
| 160 | builder.setJpegGpsCoordinates(coordinates); |
| 161 | } |
| 162 | |
Vadim Caen | 11dfd93 | 2024-03-05 09:57:20 +0100 | [diff] [blame] | 163 | std::unique_ptr<CameraMetadata> metadata = builder.build(); |
| 164 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 165 | if (metadata == nullptr) { |
| 166 | ALOGE("%s: Failed to build capture result metadata", __func__); |
| 167 | return CameraMetadata(); |
| 168 | } |
| 169 | return std::move(*metadata); |
| 170 | } |
| 171 | |
| 172 | NotifyMsg createShutterNotifyMsg(int frameNumber, |
| 173 | std::chrono::nanoseconds timestamp) { |
| 174 | NotifyMsg msg; |
| 175 | msg.set<NotifyMsg::Tag::shutter>(ShutterMsg{ |
| 176 | .frameNumber = frameNumber, |
| 177 | .timestamp = timestamp.count(), |
| 178 | }); |
| 179 | return msg; |
| 180 | } |
| 181 | |
| 182 | NotifyMsg createBufferErrorNotifyMsg(int frameNumber, int streamId) { |
| 183 | NotifyMsg msg; |
| 184 | msg.set<NotifyMsg::Tag::error>(ErrorMsg{.frameNumber = frameNumber, |
| 185 | .errorStreamId = streamId, |
| 186 | .errorCode = ErrorCode::ERROR_BUFFER}); |
| 187 | return msg; |
| 188 | } |
| 189 | |
| 190 | NotifyMsg createRequestErrorNotifyMsg(int frameNumber) { |
| 191 | NotifyMsg msg; |
| 192 | msg.set<NotifyMsg::Tag::error>(ErrorMsg{ |
Jan Sebechlebsky | b0d8cab | 2023-11-28 10:55:04 +0100 | [diff] [blame] | 193 | .frameNumber = frameNumber, |
| 194 | // errorStreamId needs to be set to -1 for ERROR_REQUEST |
| 195 | // (not tied to specific stream). |
| 196 | .errorStreamId = -1, |
| 197 | .errorCode = ErrorCode::ERROR_REQUEST}); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 198 | return msg; |
| 199 | } |
| 200 | |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 201 | std::shared_ptr<EglFrameBuffer> allocateTemporaryFramebuffer( |
| 202 | EGLDisplay eglDisplay, const uint width, const int height) { |
| 203 | const AHardwareBuffer_Desc desc{ |
| 204 | .width = static_cast<uint32_t>(width), |
| 205 | .height = static_cast<uint32_t>(height), |
| 206 | .layers = 1, |
| 207 | .format = AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420, |
| 208 | .usage = AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER | |
| 209 | AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN, |
| 210 | .rfu0 = 0, |
| 211 | .rfu1 = 0}; |
| 212 | |
| 213 | AHardwareBuffer* hwBufferPtr; |
| 214 | int status = AHardwareBuffer_allocate(&desc, &hwBufferPtr); |
| 215 | if (status != NO_ERROR) { |
| 216 | ALOGE( |
| 217 | "%s: Failed to allocate hardware buffer for temporary framebuffer: %d", |
| 218 | __func__, status); |
| 219 | return nullptr; |
| 220 | } |
| 221 | |
| 222 | return std::make_shared<EglFrameBuffer>( |
| 223 | eglDisplay, |
| 224 | std::shared_ptr<AHardwareBuffer>(hwBufferPtr, AHardwareBuffer_release)); |
| 225 | } |
| 226 | |
| 227 | bool isYuvFormat(const PixelFormat pixelFormat) { |
| 228 | switch (static_cast<android_pixel_format_t>(pixelFormat)) { |
| 229 | case HAL_PIXEL_FORMAT_YCBCR_422_I: |
| 230 | case HAL_PIXEL_FORMAT_YCBCR_422_SP: |
| 231 | case HAL_PIXEL_FORMAT_Y16: |
| 232 | case HAL_PIXEL_FORMAT_YV12: |
| 233 | case HAL_PIXEL_FORMAT_YCBCR_420_888: |
| 234 | return true; |
| 235 | default: |
| 236 | return false; |
| 237 | } |
| 238 | } |
| 239 | |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 240 | std::vector<uint8_t> createExif( |
Vadim Caen | c0aff13 | 2024-03-12 17:20:07 +0100 | [diff] [blame] | 241 | Resolution imageSize, const CameraMetadata resultMetadata, |
| 242 | const std::vector<uint8_t>& compressedThumbnail = {}) { |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 243 | std::unique_ptr<ExifUtils> exifUtils(ExifUtils::create()); |
| 244 | exifUtils->initialize(); |
Vadim Caen | c0aff13 | 2024-03-12 17:20:07 +0100 | [diff] [blame] | 245 | |
| 246 | // Make a copy of the metadata in order to converting it the HAL metadata |
| 247 | // format (as opposed to the AIDL class) and use the setFromMetadata method |
| 248 | // from ExifUtil |
| 249 | camera_metadata_t* rawSettings = |
| 250 | clone_camera_metadata((camera_metadata_t*)resultMetadata.metadata.data()); |
| 251 | if (rawSettings != nullptr) { |
| 252 | android::hardware::camera::common::helper::CameraMetadata halMetadata( |
| 253 | rawSettings); |
| 254 | exifUtils->setFromMetadata(halMetadata, imageSize.width, imageSize.height); |
| 255 | } |
| 256 | exifUtils->setMake(VirtualCameraDevice::kDefaultMakeAndModel); |
| 257 | exifUtils->setModel(VirtualCameraDevice::kDefaultMakeAndModel); |
| 258 | exifUtils->setFlash(0); |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 259 | |
| 260 | std::vector<uint8_t> app1Data; |
| 261 | |
| 262 | size_t thumbnailDataSize = compressedThumbnail.size(); |
| 263 | const void* thumbnailData = |
| 264 | thumbnailDataSize > 0 |
| 265 | ? reinterpret_cast<const void*>(compressedThumbnail.data()) |
| 266 | : nullptr; |
| 267 | |
| 268 | if (!exifUtils->generateApp1(thumbnailData, thumbnailDataSize)) { |
| 269 | ALOGE("%s: Failed to generate APP1 segment for EXIF metadata", __func__); |
| 270 | return app1Data; |
| 271 | } |
| 272 | |
| 273 | const uint8_t* data = exifUtils->getApp1Buffer(); |
| 274 | const size_t size = exifUtils->getApp1Length(); |
| 275 | |
| 276 | app1Data.insert(app1Data.end(), data, data + size); |
| 277 | return app1Data; |
| 278 | } |
| 279 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 280 | } // namespace |
| 281 | |
| 282 | CaptureRequestBuffer::CaptureRequestBuffer(int streamId, int bufferId, |
| 283 | sp<Fence> fence) |
| 284 | : mStreamId(streamId), mBufferId(bufferId), mFence(fence) { |
| 285 | } |
| 286 | |
| 287 | int CaptureRequestBuffer::getStreamId() const { |
| 288 | return mStreamId; |
| 289 | } |
| 290 | |
| 291 | int CaptureRequestBuffer::getBufferId() const { |
| 292 | return mBufferId; |
| 293 | } |
| 294 | |
| 295 | sp<Fence> CaptureRequestBuffer::getFence() const { |
| 296 | return mFence; |
| 297 | } |
| 298 | |
| 299 | VirtualCameraRenderThread::VirtualCameraRenderThread( |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 300 | VirtualCameraSessionContext& sessionContext, |
| 301 | const Resolution inputSurfaceSize, const Resolution reportedSensorSize, |
Jan Sebechlebsky | 288900f | 2024-05-24 14:47:54 +0200 | [diff] [blame^] | 302 | std::shared_ptr<ICameraDeviceCallback> cameraDeviceCallback) |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 303 | : mCameraDeviceCallback(cameraDeviceCallback), |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 304 | mInputSurfaceSize(inputSurfaceSize), |
| 305 | mReportedSensorSize(reportedSensorSize), |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 306 | mSessionContext(sessionContext) { |
| 307 | } |
| 308 | |
| 309 | VirtualCameraRenderThread::~VirtualCameraRenderThread() { |
| 310 | stop(); |
| 311 | if (mThread.joinable()) { |
| 312 | mThread.join(); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | ProcessCaptureRequestTask::ProcessCaptureRequestTask( |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 317 | int frameNumber, const std::vector<CaptureRequestBuffer>& requestBuffers, |
| 318 | const RequestSettings& requestSettings) |
| 319 | : mFrameNumber(frameNumber), |
| 320 | mBuffers(requestBuffers), |
| 321 | mRequestSettings(requestSettings) { |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | int ProcessCaptureRequestTask::getFrameNumber() const { |
| 325 | return mFrameNumber; |
| 326 | } |
| 327 | |
| 328 | const std::vector<CaptureRequestBuffer>& ProcessCaptureRequestTask::getBuffers() |
| 329 | const { |
| 330 | return mBuffers; |
| 331 | } |
| 332 | |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 333 | const RequestSettings& ProcessCaptureRequestTask::getRequestSettings() const { |
| 334 | return mRequestSettings; |
| 335 | } |
| 336 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 337 | void VirtualCameraRenderThread::enqueueTask( |
| 338 | std::unique_ptr<ProcessCaptureRequestTask> task) { |
| 339 | std::lock_guard<std::mutex> lock(mLock); |
| 340 | mQueue.emplace_back(std::move(task)); |
| 341 | mCondVar.notify_one(); |
| 342 | } |
| 343 | |
| 344 | void VirtualCameraRenderThread::flush() { |
| 345 | std::lock_guard<std::mutex> lock(mLock); |
Jan Sebechlebsky | b0d8cab | 2023-11-28 10:55:04 +0100 | [diff] [blame] | 346 | while (!mQueue.empty()) { |
| 347 | std::unique_ptr<ProcessCaptureRequestTask> task = std::move(mQueue.front()); |
| 348 | mQueue.pop_front(); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 349 | flushCaptureRequest(*task); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | void VirtualCameraRenderThread::start() { |
| 354 | mThread = std::thread(&VirtualCameraRenderThread::threadLoop, this); |
| 355 | } |
| 356 | |
| 357 | void VirtualCameraRenderThread::stop() { |
| 358 | { |
| 359 | std::lock_guard<std::mutex> lock(mLock); |
| 360 | mPendingExit = true; |
| 361 | mCondVar.notify_one(); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | sp<Surface> VirtualCameraRenderThread::getInputSurface() { |
| 366 | return mInputSurfacePromise.get_future().get(); |
| 367 | } |
| 368 | |
| 369 | std::unique_ptr<ProcessCaptureRequestTask> |
| 370 | VirtualCameraRenderThread::dequeueTask() { |
| 371 | std::unique_lock<std::mutex> lock(mLock); |
| 372 | // Clang's thread safety analysis doesn't perform alias analysis, |
| 373 | // so it doesn't support moveable std::unique_lock. |
| 374 | // |
| 375 | // Lock assertion below is basically explicit declaration that |
| 376 | // the lock is held in this scope, which is true, since it's only |
| 377 | // released during waiting inside mCondVar.wait calls. |
| 378 | ScopedLockAssertion lockAssertion(mLock); |
| 379 | |
| 380 | mCondVar.wait(lock, [this]() REQUIRES(mLock) { |
| 381 | return mPendingExit || !mQueue.empty(); |
| 382 | }); |
| 383 | if (mPendingExit) { |
| 384 | return nullptr; |
| 385 | } |
| 386 | std::unique_ptr<ProcessCaptureRequestTask> task = std::move(mQueue.front()); |
| 387 | mQueue.pop_front(); |
| 388 | return task; |
| 389 | } |
| 390 | |
| 391 | void VirtualCameraRenderThread::threadLoop() { |
| 392 | ALOGV("Render thread starting"); |
| 393 | |
| 394 | mEglDisplayContext = std::make_unique<EglDisplayContext>(); |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 395 | mEglTextureYuvProgram = |
| 396 | std::make_unique<EglTextureProgram>(EglTextureProgram::TextureFormat::YUV); |
| 397 | mEglTextureRgbProgram = std::make_unique<EglTextureProgram>( |
| 398 | EglTextureProgram::TextureFormat::RGBA); |
Jan Sebechlebsky | bb01c1d | 2024-02-12 11:41:37 +0100 | [diff] [blame] | 399 | mEglSurfaceTexture = std::make_unique<EglSurfaceTexture>( |
| 400 | mInputSurfaceSize.width, mInputSurfaceSize.height); |
Jan Sebechlebsky | 6402fef | 2024-03-25 16:30:26 +0100 | [diff] [blame] | 401 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 402 | mInputSurfacePromise.set_value(mEglSurfaceTexture->getSurface()); |
| 403 | |
| 404 | while (std::unique_ptr<ProcessCaptureRequestTask> task = dequeueTask()) { |
| 405 | processCaptureRequest(*task); |
| 406 | } |
| 407 | |
Jan Sebechlebsky | 06b3667 | 2024-03-18 11:52:35 +0100 | [diff] [blame] | 408 | // Destroy EGL utilities still on the render thread. |
| 409 | mEglSurfaceTexture.reset(); |
| 410 | mEglTextureRgbProgram.reset(); |
| 411 | mEglTextureYuvProgram.reset(); |
| 412 | mEglDisplayContext.reset(); |
| 413 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 414 | ALOGV("Render thread exiting"); |
| 415 | } |
| 416 | |
| 417 | void VirtualCameraRenderThread::processCaptureRequest( |
| 418 | const ProcessCaptureRequestTask& request) { |
Jan Sebechlebsky | 2f4478e | 2024-05-08 17:26:42 +0200 | [diff] [blame] | 419 | std::chrono::nanoseconds timestamp = |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 420 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 421 | std::chrono::steady_clock::now().time_since_epoch()); |
Jan Sebechlebsky | 2f4478e | 2024-05-08 17:26:42 +0200 | [diff] [blame] | 422 | std::chrono::nanoseconds lastAcquisitionTimestamp( |
| 423 | mLastAcquisitionTimestampNanoseconds.exchange(timestamp.count(), |
| 424 | std::memory_order_relaxed)); |
| 425 | |
| 426 | if (request.getRequestSettings().fpsRange) { |
| 427 | const int maxFps = |
| 428 | std::max(1, request.getRequestSettings().fpsRange->maxFps); |
| 429 | const std::chrono::nanoseconds minFrameDuration( |
| 430 | static_cast<uint64_t>(1e9 / maxFps)); |
| 431 | const std::chrono::nanoseconds frameDuration = |
| 432 | timestamp - lastAcquisitionTimestamp; |
| 433 | if (frameDuration < minFrameDuration) { |
| 434 | // We're too fast for the configured maxFps, let's wait a bit. |
| 435 | const std::chrono::nanoseconds sleepTime = |
| 436 | minFrameDuration - frameDuration; |
| 437 | ALOGV("Current frame duration would be %" PRIu64 |
| 438 | " ns corresponding to, " |
| 439 | "sleeping for %" PRIu64 |
| 440 | " ns before updating texture to match maxFps %d", |
| 441 | static_cast<uint64_t>(frameDuration.count()), |
| 442 | static_cast<uint64_t>(sleepTime.count()), maxFps); |
| 443 | |
| 444 | std::this_thread::sleep_for(sleepTime); |
| 445 | timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 446 | std::chrono::steady_clock::now().time_since_epoch()); |
| 447 | mLastAcquisitionTimestampNanoseconds.store(timestamp.count(), |
| 448 | std::memory_order_relaxed); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | // Acquire new (most recent) image from the Surface. |
| 453 | mEglSurfaceTexture->updateTexture(); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 454 | |
| 455 | CaptureResult captureResult; |
| 456 | captureResult.fmqResultSize = 0; |
| 457 | captureResult.frameNumber = request.getFrameNumber(); |
Jan Sebechlebsky | b0d8cab | 2023-11-28 10:55:04 +0100 | [diff] [blame] | 458 | // Partial result needs to be set to 1 when metadata are present. |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 459 | captureResult.partialResult = 1; |
| 460 | captureResult.inputBuffer.streamId = -1; |
| 461 | captureResult.physicalCameraMetadata.resize(0); |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 462 | captureResult.result = createCaptureResultMetadata( |
| 463 | timestamp, request.getRequestSettings(), mReportedSensorSize); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 464 | |
| 465 | const std::vector<CaptureRequestBuffer>& buffers = request.getBuffers(); |
| 466 | captureResult.outputBuffers.resize(buffers.size()); |
| 467 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 468 | for (int i = 0; i < buffers.size(); ++i) { |
| 469 | const CaptureRequestBuffer& reqBuffer = buffers[i]; |
| 470 | StreamBuffer& resBuffer = captureResult.outputBuffers[i]; |
| 471 | resBuffer.streamId = reqBuffer.getStreamId(); |
| 472 | resBuffer.bufferId = reqBuffer.getBufferId(); |
| 473 | resBuffer.status = BufferStatus::OK; |
| 474 | |
| 475 | const std::optional<Stream> streamConfig = |
| 476 | mSessionContext.getStreamConfig(reqBuffer.getStreamId()); |
| 477 | |
| 478 | if (!streamConfig.has_value()) { |
| 479 | resBuffer.status = BufferStatus::ERROR; |
| 480 | continue; |
| 481 | } |
| 482 | |
| 483 | auto status = streamConfig->format == PixelFormat::BLOB |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 484 | ? renderIntoBlobStreamBuffer( |
| 485 | reqBuffer.getStreamId(), reqBuffer.getBufferId(), |
Vadim Caen | c0aff13 | 2024-03-12 17:20:07 +0100 | [diff] [blame] | 486 | captureResult.result, request.getRequestSettings(), |
| 487 | reqBuffer.getFence()) |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 488 | : renderIntoImageStreamBuffer(reqBuffer.getStreamId(), |
| 489 | reqBuffer.getBufferId(), |
| 490 | reqBuffer.getFence()); |
| 491 | if (!status.isOk()) { |
| 492 | resBuffer.status = BufferStatus::ERROR; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | std::vector<NotifyMsg> notifyMsg{ |
| 497 | createShutterNotifyMsg(request.getFrameNumber(), timestamp)}; |
| 498 | for (const StreamBuffer& resBuffer : captureResult.outputBuffers) { |
| 499 | if (resBuffer.status != BufferStatus::OK) { |
| 500 | notifyMsg.push_back(createBufferErrorNotifyMsg(request.getFrameNumber(), |
| 501 | resBuffer.streamId)); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | auto status = mCameraDeviceCallback->notify(notifyMsg); |
| 506 | if (!status.isOk()) { |
| 507 | ALOGE("%s: notify call failed: %s", __func__, |
| 508 | status.getDescription().c_str()); |
| 509 | return; |
| 510 | } |
| 511 | |
| 512 | std::vector<::aidl::android::hardware::camera::device::CaptureResult> |
| 513 | captureResults(1); |
| 514 | captureResults[0] = std::move(captureResult); |
| 515 | |
| 516 | status = mCameraDeviceCallback->processCaptureResult(captureResults); |
| 517 | if (!status.isOk()) { |
| 518 | ALOGE("%s: processCaptureResult call failed: %s", __func__, |
| 519 | status.getDescription().c_str()); |
| 520 | return; |
| 521 | } |
| 522 | |
Vadim Caen | 324fcfb | 2024-03-21 16:49:08 +0100 | [diff] [blame] | 523 | ALOGV("%s: Successfully called processCaptureResult", __func__); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | void VirtualCameraRenderThread::flushCaptureRequest( |
| 527 | const ProcessCaptureRequestTask& request) { |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 528 | CaptureResult captureResult; |
| 529 | captureResult.fmqResultSize = 0; |
| 530 | captureResult.frameNumber = request.getFrameNumber(); |
| 531 | captureResult.inputBuffer.streamId = -1; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 532 | |
| 533 | const std::vector<CaptureRequestBuffer>& buffers = request.getBuffers(); |
| 534 | captureResult.outputBuffers.resize(buffers.size()); |
| 535 | |
| 536 | for (int i = 0; i < buffers.size(); ++i) { |
| 537 | const CaptureRequestBuffer& reqBuffer = buffers[i]; |
| 538 | StreamBuffer& resBuffer = captureResult.outputBuffers[i]; |
| 539 | resBuffer.streamId = reqBuffer.getStreamId(); |
| 540 | resBuffer.bufferId = reqBuffer.getBufferId(); |
| 541 | resBuffer.status = BufferStatus::ERROR; |
| 542 | sp<Fence> fence = reqBuffer.getFence(); |
| 543 | if (fence != nullptr && fence->isValid()) { |
| 544 | resBuffer.releaseFence.fds.emplace_back(fence->dup()); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | auto status = mCameraDeviceCallback->notify( |
| 549 | {createRequestErrorNotifyMsg(request.getFrameNumber())}); |
| 550 | if (!status.isOk()) { |
| 551 | ALOGE("%s: notify call failed: %s", __func__, |
| 552 | status.getDescription().c_str()); |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | std::vector<::aidl::android::hardware::camera::device::CaptureResult> |
| 557 | captureResults(1); |
| 558 | captureResults[0] = std::move(captureResult); |
| 559 | |
| 560 | status = mCameraDeviceCallback->processCaptureResult(captureResults); |
| 561 | if (!status.isOk()) { |
| 562 | ALOGE("%s: processCaptureResult call failed: %s", __func__, |
| 563 | status.getDescription().c_str()); |
| 564 | } |
| 565 | } |
| 566 | |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 567 | std::vector<uint8_t> VirtualCameraRenderThread::createThumbnail( |
| 568 | const Resolution resolution, const int quality) { |
| 569 | if (resolution.width == 0 || resolution.height == 0) { |
| 570 | ALOGV("%s: Skipping thumbnail creation, zero size requested", __func__); |
| 571 | return {}; |
| 572 | } |
| 573 | |
| 574 | ALOGV("%s: Creating thumbnail with size %d x %d, quality %d", __func__, |
| 575 | resolution.width, resolution.height, quality); |
Jan Sebechlebsky | b377131 | 2024-03-15 10:38:02 +0100 | [diff] [blame] | 576 | Resolution bufferSize = roundTo2DctSize(resolution); |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 577 | std::shared_ptr<EglFrameBuffer> framebuffer = allocateTemporaryFramebuffer( |
Jan Sebechlebsky | b377131 | 2024-03-15 10:38:02 +0100 | [diff] [blame] | 578 | mEglDisplayContext->getEglDisplay(), bufferSize.width, bufferSize.height); |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 579 | if (framebuffer == nullptr) { |
| 580 | ALOGE( |
| 581 | "Failed to allocate temporary framebuffer for JPEG thumbnail " |
| 582 | "compression"); |
| 583 | return {}; |
| 584 | } |
| 585 | |
| 586 | // TODO(b/324383963) Add support for letterboxing if the thumbnail size |
| 587 | // doesn't correspond |
| 588 | // to input texture aspect ratio. |
Jan Sebechlebsky | b377131 | 2024-03-15 10:38:02 +0100 | [diff] [blame] | 589 | if (!renderIntoEglFramebuffer(*framebuffer, /*fence=*/nullptr, |
| 590 | Rect(resolution.width, resolution.height)) |
| 591 | .isOk()) { |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 592 | ALOGE( |
| 593 | "Failed to render input texture into temporary framebuffer for JPEG " |
| 594 | "thumbnail"); |
| 595 | return {}; |
| 596 | } |
| 597 | |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 598 | std::vector<uint8_t> compressedThumbnail; |
| 599 | compressedThumbnail.resize(kJpegThumbnailBufferSize); |
Jan Sebechlebsky | b377131 | 2024-03-15 10:38:02 +0100 | [diff] [blame] | 600 | ALOGE("%s: Compressing thumbnail %d x %d", __func__, resolution.width, |
| 601 | resolution.height); |
| 602 | std::optional<size_t> compressedSize = |
| 603 | compressJpeg(resolution.width, resolution.height, quality, |
| 604 | framebuffer->getHardwareBuffer(), {}, |
| 605 | compressedThumbnail.size(), compressedThumbnail.data()); |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 606 | if (!compressedSize.has_value()) { |
| 607 | ALOGE("%s: Failed to compress jpeg thumbnail", __func__); |
| 608 | return {}; |
| 609 | } |
| 610 | compressedThumbnail.resize(compressedSize.value()); |
| 611 | return compressedThumbnail; |
| 612 | } |
| 613 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 614 | ndk::ScopedAStatus VirtualCameraRenderThread::renderIntoBlobStreamBuffer( |
Vadim Caen | c0aff13 | 2024-03-12 17:20:07 +0100 | [diff] [blame] | 615 | const int streamId, const int bufferId, const CameraMetadata& resultMetadata, |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 616 | const RequestSettings& requestSettings, sp<Fence> fence) { |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 617 | std::shared_ptr<AHardwareBuffer> hwBuffer = |
| 618 | mSessionContext.fetchHardwareBuffer(streamId, bufferId); |
Jan Sebechlebsky | 9ae496f | 2023-12-05 15:56:28 +0100 | [diff] [blame] | 619 | if (hwBuffer == nullptr) { |
| 620 | ALOGE("%s: Failed to fetch hardware buffer %d for streamId %d", __func__, |
| 621 | bufferId, streamId); |
| 622 | return cameraStatus(Status::INTERNAL_ERROR); |
| 623 | } |
| 624 | |
| 625 | std::optional<Stream> stream = mSessionContext.getStreamConfig(streamId); |
| 626 | if (!stream.has_value()) { |
| 627 | ALOGE("%s, failed to fetch information about stream %d", __func__, streamId); |
| 628 | return cameraStatus(Status::INTERNAL_ERROR); |
| 629 | } |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 630 | |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 631 | ALOGV("%s: Rendering JPEG with size %d x %d, quality %d", __func__, |
| 632 | stream->width, stream->height, requestSettings.jpegQuality); |
| 633 | |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 634 | // Let's create YUV framebuffer and render the surface into this. |
| 635 | // This will take care about rescaling as well as potential format conversion. |
Jan Sebechlebsky | b377131 | 2024-03-15 10:38:02 +0100 | [diff] [blame] | 636 | // The buffer dimensions need to be rounded to nearest multiple of JPEG DCT |
| 637 | // size, however we pass the viewport corresponding to size of the stream so |
| 638 | // the image will be only rendered to the area corresponding to the stream |
| 639 | // size. |
| 640 | Resolution bufferSize = |
| 641 | roundTo2DctSize(Resolution(stream->width, stream->height)); |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 642 | std::shared_ptr<EglFrameBuffer> framebuffer = allocateTemporaryFramebuffer( |
Jan Sebechlebsky | b377131 | 2024-03-15 10:38:02 +0100 | [diff] [blame] | 643 | mEglDisplayContext->getEglDisplay(), bufferSize.width, bufferSize.height); |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 644 | if (framebuffer == nullptr) { |
| 645 | ALOGE("Failed to allocate temporary framebuffer for JPEG compression"); |
| 646 | return cameraStatus(Status::INTERNAL_ERROR); |
| 647 | } |
| 648 | |
| 649 | // Render into temporary framebuffer. |
Jan Sebechlebsky | b377131 | 2024-03-15 10:38:02 +0100 | [diff] [blame] | 650 | ndk::ScopedAStatus status = renderIntoEglFramebuffer( |
| 651 | *framebuffer, /*fence=*/nullptr, Rect(stream->width, stream->height)); |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 652 | if (!status.isOk()) { |
| 653 | ALOGE("Failed to render input texture into temporary framebuffer"); |
| 654 | return status; |
| 655 | } |
| 656 | |
Jan Sebechlebsky | 4354322 | 2024-02-16 12:50:32 +0100 | [diff] [blame] | 657 | PlanesLockGuard planesLock(hwBuffer, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, |
| 658 | fence); |
| 659 | if (planesLock.getStatus() != OK) { |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 660 | return cameraStatus(Status::INTERNAL_ERROR); |
| 661 | } |
| 662 | |
Jan Sebechlebsky | 5c789e4 | 2024-02-29 16:32:17 +0100 | [diff] [blame] | 663 | std::vector<uint8_t> app1ExifData = |
Vadim Caen | c0aff13 | 2024-03-12 17:20:07 +0100 | [diff] [blame] | 664 | createExif(Resolution(stream->width, stream->height), resultMetadata, |
Jan Sebechlebsky | 5c789e4 | 2024-02-29 16:32:17 +0100 | [diff] [blame] | 665 | createThumbnail(requestSettings.thumbnailResolution, |
| 666 | requestSettings.thumbnailJpegQuality)); |
| 667 | std::optional<size_t> compressedSize = compressJpeg( |
Jan Sebechlebsky | b377131 | 2024-03-15 10:38:02 +0100 | [diff] [blame] | 668 | stream->width, stream->height, requestSettings.jpegQuality, |
| 669 | framebuffer->getHardwareBuffer(), app1ExifData, |
| 670 | stream->bufferSize - sizeof(CameraBlob), (*planesLock).planes[0].data); |
Jan Sebechlebsky | 5c789e4 | 2024-02-29 16:32:17 +0100 | [diff] [blame] | 671 | |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 672 | if (!compressedSize.has_value()) { |
| 673 | ALOGE("%s: Failed to compress JPEG image", __func__); |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 674 | return cameraStatus(Status::INTERNAL_ERROR); |
| 675 | } |
| 676 | |
| 677 | CameraBlob cameraBlob{ |
| 678 | .blobId = CameraBlobId::JPEG, |
| 679 | .blobSizeBytes = static_cast<int32_t>(compressedSize.value())}; |
| 680 | |
Jan Sebechlebsky | 4354322 | 2024-02-16 12:50:32 +0100 | [diff] [blame] | 681 | memcpy(reinterpret_cast<uint8_t*>((*planesLock).planes[0].data) + |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 682 | (stream->bufferSize - sizeof(cameraBlob)), |
| 683 | &cameraBlob, sizeof(cameraBlob)); |
| 684 | |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 685 | ALOGV("%s: Successfully compressed JPEG image, resulting size %zu B", |
| 686 | __func__, compressedSize.value()); |
| 687 | |
| 688 | return ndk::ScopedAStatus::ok(); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | ndk::ScopedAStatus VirtualCameraRenderThread::renderIntoImageStreamBuffer( |
| 692 | int streamId, int bufferId, sp<Fence> fence) { |
| 693 | ALOGV("%s", __func__); |
| 694 | |
| 695 | const std::chrono::nanoseconds before = |
| 696 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 697 | std::chrono::steady_clock::now().time_since_epoch()); |
| 698 | |
| 699 | // Render test pattern using EGL. |
| 700 | std::shared_ptr<EglFrameBuffer> framebuffer = |
| 701 | mSessionContext.fetchOrCreateEglFramebuffer( |
| 702 | mEglDisplayContext->getEglDisplay(), streamId, bufferId); |
| 703 | if (framebuffer == nullptr) { |
| 704 | ALOGE( |
| 705 | "%s: Failed to get EGL framebuffer corresponding to buffer id " |
| 706 | "%d for streamId %d", |
| 707 | __func__, bufferId, streamId); |
| 708 | return cameraStatus(Status::ILLEGAL_ARGUMENT); |
| 709 | } |
| 710 | |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 711 | ndk::ScopedAStatus status = renderIntoEglFramebuffer(*framebuffer, fence); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 712 | |
| 713 | const std::chrono::nanoseconds after = |
| 714 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 715 | std::chrono::steady_clock::now().time_since_epoch()); |
| 716 | |
| 717 | ALOGV("Rendering to buffer %d, stream %d took %lld ns", bufferId, streamId, |
| 718 | after.count() - before.count()); |
| 719 | |
| 720 | return ndk::ScopedAStatus::ok(); |
| 721 | } |
| 722 | |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 723 | ndk::ScopedAStatus VirtualCameraRenderThread::renderIntoEglFramebuffer( |
Jan Sebechlebsky | b377131 | 2024-03-15 10:38:02 +0100 | [diff] [blame] | 724 | EglFrameBuffer& framebuffer, sp<Fence> fence, std::optional<Rect> viewport) { |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 725 | ALOGV("%s", __func__); |
| 726 | // Wait for fence to clear. |
| 727 | if (fence != nullptr && fence->isValid()) { |
| 728 | status_t ret = fence->wait(kAcquireFenceTimeout.count()); |
| 729 | if (ret != 0) { |
| 730 | ALOGE("Timeout while waiting for the acquire fence for buffer"); |
| 731 | return cameraStatus(Status::INTERNAL_ERROR); |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | mEglDisplayContext->makeCurrent(); |
| 736 | framebuffer.beforeDraw(); |
| 737 | |
Jan Sebechlebsky | b377131 | 2024-03-15 10:38:02 +0100 | [diff] [blame] | 738 | Rect viewportRect = |
| 739 | viewport.value_or(Rect(framebuffer.getWidth(), framebuffer.getHeight())); |
| 740 | glViewport(viewportRect.leftTop().x, viewportRect.leftTop().y, |
| 741 | viewportRect.getWidth(), viewportRect.getHeight()); |
| 742 | |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 743 | sp<GraphicBuffer> textureBuffer = mEglSurfaceTexture->getCurrentBuffer(); |
| 744 | if (textureBuffer == nullptr) { |
| 745 | // If there's no current buffer, nothing was written to the surface and |
| 746 | // texture is not initialized yet. Let's render the framebuffer black |
| 747 | // instead of rendering the texture. |
| 748 | glClearColor(0.0f, 0.5f, 0.5f, 0.0f); |
| 749 | glClear(GL_COLOR_BUFFER_BIT); |
| 750 | } else { |
| 751 | const bool renderSuccess = |
| 752 | isYuvFormat(static_cast<PixelFormat>(textureBuffer->getPixelFormat())) |
Jan Sebechlebsky | 99492e3 | 2023-12-20 09:49:45 +0100 | [diff] [blame] | 753 | ? mEglTextureYuvProgram->draw( |
| 754 | mEglSurfaceTexture->getTextureId(), |
| 755 | mEglSurfaceTexture->getTransformMatrix()) |
| 756 | : mEglTextureRgbProgram->draw( |
| 757 | mEglSurfaceTexture->getTextureId(), |
| 758 | mEglSurfaceTexture->getTransformMatrix()); |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 759 | if (!renderSuccess) { |
| 760 | ALOGE("%s: Failed to render texture", __func__); |
| 761 | return cameraStatus(Status::INTERNAL_ERROR); |
| 762 | } |
| 763 | } |
| 764 | framebuffer.afterDraw(); |
| 765 | |
| 766 | return ndk::ScopedAStatus::ok(); |
| 767 | } |
| 768 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 769 | } // namespace virtualcamera |
| 770 | } // namespace companion |
| 771 | } // namespace android |