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