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 <cmath>
21 #include <cstdint>
22 #include <memory>
23 #include <optional>
24 #include <string>
25
26 #include "aidl/android/companion/virtualcamera/Format.h"
27 #include "aidl/android/hardware/camera/common/Status.h"
28 #include "aidl/android/hardware/camera/device/StreamBuffer.h"
29 #include "android/binder_auto_utils.h"
30 #include "android/hardware_buffer.h"
31 #include "system/graphics.h"
32 #include "ui/Fence.h"
33
34 namespace android {
35 namespace companion {
36 namespace virtualcamera {
37
38 constexpr int kHardwareBufferUsage = AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER |
39 AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN;
40 constexpr int kHardwareBufferFormat = AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420;
41
42 // RAII utility class to safely lock AHardwareBuffer and obtain android_ycbcr
43 // structure describing YUV plane layout.
44 //
45 // Access to the buffer is locked immediatelly afer construction.
46 class YCbCrLockGuard {
47 public:
48 YCbCrLockGuard(std::shared_ptr<AHardwareBuffer> hwBuffer, uint32_t usageFlags);
49 YCbCrLockGuard(YCbCrLockGuard&& other) = default;
50 ~YCbCrLockGuard();
51
52 // Returns OK if the buffer is successfully locked.
53 status_t getStatus() const;
54
55 // Dereferencing instance of this guard returns android_ycbcr structure
56 // describing the layout.
57 // Caller needs to check whether the buffer was successfully locked
58 // before dereferencing.
59 const android_ycbcr& operator*() const;
60
61 // Disable copy.
62 YCbCrLockGuard(const YCbCrLockGuard&) = delete;
63 YCbCrLockGuard& operator=(const YCbCrLockGuard&) = delete;
64
65 private:
66 std::shared_ptr<AHardwareBuffer> mHwBuffer;
67 android_ycbcr mYCbCr = {};
68 status_t mLockStatus = DEAD_OBJECT;
69 };
70
71 // RAII utility class to safely lock AHardwareBuffer and obtain
72 // AHardwareBuffer_Planes (Suitable for interacting with RGBA / BLOB buffers.
73 //
74 // Access to the buffer is locked immediatelly afer construction.
75 class PlanesLockGuard {
76 public:
77 PlanesLockGuard(std::shared_ptr<AHardwareBuffer> hwBuffer,
78 uint64_t usageFlags, sp<Fence> fence = nullptr);
79 PlanesLockGuard(PlanesLockGuard&& other) = default;
80 ~PlanesLockGuard();
81
82 // Returns OK if the buffer is successfully locked.
83 status_t getStatus() const;
84
85 // Dereferencing instance of this guard returns AHardwareBuffer_Planes
86 // structure describing the layout.
87 //
88 // Caller needs to check whether the buffer was successfully locked
89 // before dereferencing.
90 const AHardwareBuffer_Planes& operator*() const;
91
92 // Disable copy.
93 PlanesLockGuard(const PlanesLockGuard&) = delete;
94 PlanesLockGuard& operator=(const YCbCrLockGuard&) = delete;
95
96 private:
97 std::shared_ptr<AHardwareBuffer> mHwBuffer;
98 AHardwareBuffer_Planes mPlanes;
99 status_t mLockStatus = DEAD_OBJECT;
100 };
101
102 // Converts camera AIDL status to ndk::ScopedAStatus
cameraStatus(const::aidl::android::hardware::camera::common::Status status)103 inline ndk::ScopedAStatus cameraStatus(
104 const ::aidl::android::hardware::camera::common::Status status) {
105 return ndk::ScopedAStatus::fromServiceSpecificError(
106 static_cast<int32_t>(status));
107 }
108
109 // Import Fence from AIDL NativeHandle.
110 //
111 // If the handle can't be used to construct Fence (is empty or doesn't contain
112 // only single fd) this function will return Fence instance in invalid state.
113 sp<Fence> importFence(
114 const ::aidl::android::hardware::common::NativeHandle& handle);
115
116 // Returns true if specified pixel format is supported for virtual camera input.
117 bool isPixelFormatSupportedForInput(
118 ::aidl::android::companion::virtualcamera::Format format);
119
120 // Returns true if specified format is supported for virtual camera input.
121 bool isFormatSupportedForInput(
122 int width, int height,
123 ::aidl::android::companion::virtualcamera::Format format, int maxFps);
124
125 // Representation of resolution / size.
126 struct Resolution {
127 Resolution() = default;
ResolutionResolution128 Resolution(const int w, const int h) : width(w), height(h) {
129 }
130
131 // Order by increasing pixel count, and by width for same pixel count.
132 bool operator<(const Resolution& other) const {
133 const int pixCount = width * height;
134 const int otherPixCount = other.width * other.height;
135 return pixCount == otherPixCount ? width < other.width
136 : pixCount < otherPixCount;
137 }
138
139 bool operator<=(const Resolution& other) const {
140 return *this == other || *this < other;
141 }
142
143 bool operator==(const Resolution& other) const {
144 return width == other.width && height == other.height;
145 }
146
147 int width = 0;
148 int height = 0;
149 };
150
151 struct FpsRange {
152 int32_t minFps;
153 int32_t maxFps;
154
155 bool operator<(const FpsRange& other) const {
156 return maxFps == other.maxFps ? minFps < other.minFps
157 : maxFps < other.maxFps;
158 }
159 };
160
161 struct GpsCoordinates {
162 // Represented by a double[] in metadata with index 0 for
163 // latitude and index 1 for longitude, 2 for altitude.
164 double_t latitude;
165 double_t longitude;
166 double_t altitude;
167 std::optional<int64_t> timestamp;
168 std::string provider;
169 };
170
isApproximatellySameAspectRatio(const Resolution r1,const Resolution r2)171 inline bool isApproximatellySameAspectRatio(const Resolution r1,
172 const Resolution r2) {
173 static constexpr float kAspectRatioEpsilon = 0.05;
174 float aspectRatio1 =
175 static_cast<float>(r1.width) / static_cast<float>(r1.height);
176 float aspectRatio2 =
177 static_cast<float>(r2.width) / static_cast<float>(r2.height);
178
179 return std::abs(aspectRatio1 - aspectRatio2) < kAspectRatioEpsilon;
180 }
181
182 std::ostream& operator<<(std::ostream& os, const Resolution& resolution);
183
184 } // namespace virtualcamera
185 } // namespace companion
186 } // namespace android
187
188 #endif // ANDROID_COMPANION_VIRTUALCAMERA_UTIL_H
189