blob: d5b0b1f226066112d595525bb473513b04200ab2 [file] [log] [blame]
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +01001/*
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 Sebechlebsky43543222024-02-16 12:50:32 +010021#include <memory>
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010022
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010023#include "aidl/android/companion/virtualcamera/Format.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010024#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 Sebechlebsky43543222024-02-16 12:50:32 +010027#include "android/hardware_buffer.h"
28#include "system/graphics.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010029#include "ui/Fence.h"
30
31namespace android {
32namespace companion {
33namespace virtualcamera {
34
Jan Sebechlebsky43543222024-02-16 12:50:32 +010035// 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.
39class 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.
68class 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 Sebechlebsky5cb39962023-11-22 17:33:07 +010095// Converts camera AIDL status to ndk::ScopedAStatus
96inline 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.
106sp<Fence> importFence(
107 const ::aidl::android::hardware::common::NativeHandle& handle);
108
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +0100109// Returns true if specified pixel format is supported for virtual camera input.
110bool isPixelFormatSupportedForInput(
111 ::aidl::android::companion::virtualcamera::Format format);
112
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100113// Returns true if specified format is supported for virtual camera input.
114bool isFormatSupportedForInput(
115 int width, int height,
Biswarup Pal6152a302023-12-19 12:44:09 +0000116 ::aidl::android::companion::virtualcamera::Format format, int maxFps);
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100117
Jan Sebechlebskyad8d35f2024-02-05 11:58:59 +0100118// Representation of resolution / size.
119struct Resolution {
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +0100120 Resolution() = default;
Jan Sebechlebskyad8d35f2024-02-05 11:58:59 +0100121 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 Sebechlebsky4ce32082024-02-14 16:02:11 +0100136 int width = 0;
137 int height = 0;
Jan Sebechlebskyad8d35f2024-02-05 11:58:59 +0100138};
139
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +0100140std::ostream& operator<<(std::ostream& os, const Resolution& resolution);
141
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100142} // namespace virtualcamera
143} // namespace companion
144} // namespace android
145
146#endif // ANDROID_COMPANION_VIRTUALCAMERA_UTIL_H