blob: 7fdf54b9c3344fc833d422d064d32aafa0ce2b98 [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
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010017#include "Util.h"
18
19#include <unistd.h>
20
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010021#include <algorithm>
22#include <array>
Jan Sebechlebsky43543222024-02-16 12:50:32 +010023#include <cstdint>
24#include <memory>
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010025
Biswarup Pal26bff382024-04-22 12:16:12 +000026#include "EglUtil.h"
Jan Sebechlebsky43543222024-02-16 12:50:32 +010027#include "android/hardware_buffer.h"
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010028#include "jpeglib.h"
Jan Sebechlebsky43543222024-02-16 12:50:32 +010029#include "ui/GraphicBuffer.h"
30#include "utils/Errors.h"
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010031
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010032namespace android {
33namespace companion {
34namespace virtualcamera {
35
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010036using ::aidl::android::companion::virtualcamera::Format;
37using ::aidl::android::hardware::common::NativeHandle;
38
Biswarup Pal6152a302023-12-19 12:44:09 +000039constexpr int kMaxFpsUpperLimit = 60;
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010040
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010041constexpr std::array<Format, 2> kSupportedFormats{Format::YUV_420_888,
42 Format::RGBA_8888};
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010043
Jan Sebechlebsky43543222024-02-16 12:50:32 +010044YCbCrLockGuard::YCbCrLockGuard(std::shared_ptr<AHardwareBuffer> hwBuffer,
45 const uint32_t usageFlags)
46 : mHwBuffer(hwBuffer) {
47 GraphicBuffer* gBuffer = GraphicBuffer::fromAHardwareBuffer(mHwBuffer.get());
48 if (gBuffer == nullptr) {
49 ALOGE("%s: Attempting to lock nullptr buffer.", __func__);
50 return;
51 }
52 mLockStatus = gBuffer->lockYCbCr(usageFlags, &mYCbCr);
53 if (mLockStatus != OK) {
54 ALOGE("%s: Failed to lock graphic buffer: %s", __func__,
55 statusToString(mLockStatus).c_str());
56 }
57}
58
59YCbCrLockGuard::~YCbCrLockGuard() {
60 if (getStatus() != OK) {
61 return;
62 }
63
64 GraphicBuffer* gBuffer = GraphicBuffer::fromAHardwareBuffer(mHwBuffer.get());
65 if (gBuffer == nullptr) {
66 return;
67 }
68 gBuffer->unlock();
69 status_t status = gBuffer->unlock();
70 if (status != NO_ERROR) {
71 ALOGE("Failed to unlock graphic buffer: %s", statusToString(status).c_str());
72 }
73}
74
75status_t YCbCrLockGuard::getStatus() const {
76 return mLockStatus;
77}
78
79const android_ycbcr& YCbCrLockGuard::operator*() const {
80 LOG_ALWAYS_FATAL_IF(getStatus() != OK,
81 "Dereferencing unlocked YCbCrLockGuard, status is %s",
82 statusToString(mLockStatus).c_str());
83 return mYCbCr;
84}
85
86PlanesLockGuard::PlanesLockGuard(std::shared_ptr<AHardwareBuffer> hwBuffer,
87 const uint64_t usageFlags, sp<Fence> fence) {
88 if (hwBuffer == nullptr) {
89 ALOGE("%s: Attempting to lock nullptr buffer.", __func__);
90 return;
91 }
92
93 const int32_t rawFence = fence != nullptr ? fence->get() : -1;
94 mLockStatus = static_cast<status_t>(AHardwareBuffer_lockPlanes(
95 hwBuffer.get(), usageFlags, rawFence, nullptr, &mPlanes));
96 if (mLockStatus != OK) {
97 ALOGE("%s: Failed to lock graphic buffer: %s", __func__,
98 statusToString(mLockStatus).c_str());
99 }
100}
101
102PlanesLockGuard::~PlanesLockGuard() {
103 if (getStatus() != OK || mHwBuffer == nullptr) {
104 return;
105 }
106 AHardwareBuffer_unlock(mHwBuffer.get(), /*fence=*/nullptr);
107}
108
109int PlanesLockGuard::getStatus() const {
110 return mLockStatus;
111}
112
113const AHardwareBuffer_Planes& PlanesLockGuard::operator*() const {
114 LOG_ALWAYS_FATAL_IF(getStatus() != OK,
115 "Dereferencing unlocked PlanesLockGuard, status is %s",
116 statusToString(mLockStatus).c_str());
117 return mPlanes;
118}
119
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100120sp<Fence> importFence(const NativeHandle& aidlHandle) {
121 if (aidlHandle.fds.size() != 1) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100122 return sp<Fence>::make();
123 }
124
125 return sp<Fence>::make(::dup(aidlHandle.fds[0].get()));
126}
127
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +0100128bool isPixelFormatSupportedForInput(const Format format) {
129 return std::find(kSupportedFormats.begin(), kSupportedFormats.end(),
130 format) != kSupportedFormats.end();
131}
132
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100133// Returns true if specified format is supported for virtual camera input.
134bool isFormatSupportedForInput(const int width, const int height,
Biswarup Pal6152a302023-12-19 12:44:09 +0000135 const Format format, const int maxFps) {
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +0100136 if (!isPixelFormatSupportedForInput(format)) {
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100137 return false;
138 }
139
Biswarup Pal26bff382024-04-22 12:16:12 +0000140 int maxTextureSize = getMaximumTextureSize();
141 if (width <= 0 || height <= 0 || width > maxTextureSize ||
142 height > maxTextureSize) {
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100143 return false;
144 }
145
Biswarup Pal6152a302023-12-19 12:44:09 +0000146 if (maxFps <= 0 || maxFps > kMaxFpsUpperLimit) {
147 return false;
148 }
149
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100150 return true;
151}
152
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +0100153std::ostream& operator<<(std::ostream& os, const Resolution& resolution) {
154 return os << resolution.width << "x" << resolution.height;
155}
156
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100157} // namespace virtualcamera
158} // namespace companion
159} // namespace android