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_UTIL_H |
| 18 | #define ANDROID_COMPANION_VIRTUALCAMERA_UTIL_H |
| 19 | |
| 20 | #include <cstdint> |
Jan Sebechlebsky | 4354322 | 2024-02-16 12:50:32 +0100 | [diff] [blame] | 21 | #include <memory> |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 22 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 23 | #include "aidl/android/companion/virtualcamera/Format.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 24 | #include "aidl/android/hardware/camera/common/Status.h" |
| 25 | #include "aidl/android/hardware/camera/device/StreamBuffer.h" |
| 26 | #include "android/binder_auto_utils.h" |
Jan Sebechlebsky | 4354322 | 2024-02-16 12:50:32 +0100 | [diff] [blame] | 27 | #include "android/hardware_buffer.h" |
| 28 | #include "system/graphics.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 29 | #include "ui/Fence.h" |
| 30 | |
| 31 | namespace android { |
| 32 | namespace companion { |
| 33 | namespace virtualcamera { |
| 34 | |
Jan Sebechlebsky | 4354322 | 2024-02-16 12:50:32 +0100 | [diff] [blame] | 35 | // RAII utility class to safely lock AHardwareBuffer and obtain android_ycbcr |
| 36 | // structure describing YUV plane layout. |
| 37 | // |
| 38 | // Access to the buffer is locked immediatelly afer construction. |
| 39 | class YCbCrLockGuard { |
| 40 | public: |
| 41 | YCbCrLockGuard(std::shared_ptr<AHardwareBuffer> hwBuffer, uint32_t usageFlags); |
| 42 | YCbCrLockGuard(YCbCrLockGuard&& other) = default; |
| 43 | ~YCbCrLockGuard(); |
| 44 | |
| 45 | // Returns OK if the buffer is successfully locked. |
| 46 | status_t getStatus() const; |
| 47 | |
| 48 | // Dereferencing instance of this guard returns android_ycbcr structure |
| 49 | // describing the layout. |
| 50 | // Caller needs to check whether the buffer was successfully locked |
| 51 | // before dereferencing. |
| 52 | const android_ycbcr& operator*() const; |
| 53 | |
| 54 | // Disable copy. |
| 55 | YCbCrLockGuard(const YCbCrLockGuard&) = delete; |
| 56 | YCbCrLockGuard& operator=(const YCbCrLockGuard&) = delete; |
| 57 | |
| 58 | private: |
| 59 | std::shared_ptr<AHardwareBuffer> mHwBuffer; |
| 60 | android_ycbcr mYCbCr = {}; |
| 61 | status_t mLockStatus = DEAD_OBJECT; |
| 62 | }; |
| 63 | |
| 64 | // RAII utility class to safely lock AHardwareBuffer and obtain |
| 65 | // AHardwareBuffer_Planes (Suitable for interacting with RGBA / BLOB buffers. |
| 66 | // |
| 67 | // Access to the buffer is locked immediatelly afer construction. |
| 68 | class PlanesLockGuard { |
| 69 | public: |
| 70 | PlanesLockGuard(std::shared_ptr<AHardwareBuffer> hwBuffer, |
| 71 | uint64_t usageFlags, sp<Fence> fence = nullptr); |
| 72 | PlanesLockGuard(PlanesLockGuard&& other) = default; |
| 73 | ~PlanesLockGuard(); |
| 74 | |
| 75 | // Returns OK if the buffer is successfully locked. |
| 76 | status_t getStatus() const; |
| 77 | |
| 78 | // Dereferencing instance of this guard returns AHardwareBuffer_Planes |
| 79 | // structure describing the layout. |
| 80 | // |
| 81 | // Caller needs to check whether the buffer was successfully locked |
| 82 | // before dereferencing. |
| 83 | const AHardwareBuffer_Planes& operator*() const; |
| 84 | |
| 85 | // Disable copy. |
| 86 | PlanesLockGuard(const PlanesLockGuard&) = delete; |
| 87 | PlanesLockGuard& operator=(const YCbCrLockGuard&) = delete; |
| 88 | |
| 89 | private: |
| 90 | std::shared_ptr<AHardwareBuffer> mHwBuffer; |
| 91 | AHardwareBuffer_Planes mPlanes; |
| 92 | status_t mLockStatus = DEAD_OBJECT; |
| 93 | }; |
| 94 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 95 | // Converts camera AIDL status to ndk::ScopedAStatus |
| 96 | inline ndk::ScopedAStatus cameraStatus( |
| 97 | const ::aidl::android::hardware::camera::common::Status status) { |
| 98 | return ndk::ScopedAStatus::fromServiceSpecificError( |
| 99 | static_cast<int32_t>(status)); |
| 100 | } |
| 101 | |
| 102 | // Import Fence from AIDL NativeHandle. |
| 103 | // |
| 104 | // If the handle can't be used to construct Fence (is empty or doesn't contain |
| 105 | // only single fd) this function will return Fence instance in invalid state. |
| 106 | sp<Fence> importFence( |
| 107 | const ::aidl::android::hardware::common::NativeHandle& handle); |
| 108 | |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 109 | // Returns true if specified pixel format is supported for virtual camera input. |
| 110 | bool isPixelFormatSupportedForInput( |
| 111 | ::aidl::android::companion::virtualcamera::Format format); |
| 112 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 113 | // Returns true if specified format is supported for virtual camera input. |
| 114 | bool isFormatSupportedForInput( |
| 115 | int width, int height, |
Biswarup Pal | 6152a30 | 2023-12-19 12:44:09 +0000 | [diff] [blame] | 116 | ::aidl::android::companion::virtualcamera::Format format, int maxFps); |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame] | 117 | |
Jan Sebechlebsky | ad8d35f | 2024-02-05 11:58:59 +0100 | [diff] [blame] | 118 | // Representation of resolution / size. |
| 119 | struct Resolution { |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 120 | Resolution() = default; |
Jan Sebechlebsky | ad8d35f | 2024-02-05 11:58:59 +0100 | [diff] [blame] | 121 | Resolution(const int w, const int h) : width(w), height(h) { |
| 122 | } |
| 123 | |
| 124 | // Order by increasing pixel count, and by width for same pixel count. |
| 125 | bool operator<(const Resolution& other) const { |
| 126 | const int pixCount = width * height; |
| 127 | const int otherPixCount = other.width * other.height; |
| 128 | return pixCount == otherPixCount ? width < other.width |
| 129 | : pixCount < otherPixCount; |
| 130 | } |
| 131 | |
| 132 | bool operator==(const Resolution& other) const { |
| 133 | return width == other.width && height == other.height; |
| 134 | } |
| 135 | |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 136 | int width = 0; |
| 137 | int height = 0; |
Jan Sebechlebsky | ad8d35f | 2024-02-05 11:58:59 +0100 | [diff] [blame] | 138 | }; |
| 139 | |
Jan Sebechlebsky | 4ce3208 | 2024-02-14 16:02:11 +0100 | [diff] [blame] | 140 | std::ostream& operator<<(std::ostream& os, const Resolution& resolution); |
| 141 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 142 | } // namespace virtualcamera |
| 143 | } // namespace companion |
| 144 | } // namespace android |
| 145 | |
| 146 | #endif // ANDROID_COMPANION_VIRTUALCAMERA_UTIL_H |