blob: 2d0545dd210b510375621fd5df80b3374762c693 [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>
23
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010024#include "jpeglib.h"
25
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010026namespace android {
27namespace companion {
28namespace virtualcamera {
29
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010030using ::aidl::android::companion::virtualcamera::Format;
31using ::aidl::android::hardware::common::NativeHandle;
32
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010033// Lower bound for maximal supported texture size is at least 2048x2048
34// but on most platforms will be more.
35// TODO(b/301023410) - Query actual max texture size.
36constexpr int kMaxTextureSize = 2048;
37constexpr int kLibJpegDctSize = DCTSIZE;
Biswarup Pal6152a302023-12-19 12:44:09 +000038constexpr int kMaxFpsUpperLimit = 60;
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010039
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010040constexpr std::array<Format, 2> kSupportedFormats{Format::YUV_420_888,
41 Format::RGBA_8888};
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010042
43sp<Fence> importFence(const NativeHandle& aidlHandle) {
44 if (aidlHandle.fds.size() != 1) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010045 return sp<Fence>::make();
46 }
47
48 return sp<Fence>::make(::dup(aidlHandle.fds[0].get()));
49}
50
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010051bool isPixelFormatSupportedForInput(const Format format) {
52 return std::find(kSupportedFormats.begin(), kSupportedFormats.end(),
53 format) != kSupportedFormats.end();
54}
55
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010056// Returns true if specified format is supported for virtual camera input.
57bool isFormatSupportedForInput(const int width, const int height,
Biswarup Pal6152a302023-12-19 12:44:09 +000058 const Format format, const int maxFps) {
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010059 if (!isPixelFormatSupportedForInput(format)) {
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010060 return false;
61 }
62
63 if (width <= 0 || height <= 0 || width > kMaxTextureSize ||
64 height > kMaxTextureSize) {
65 return false;
66 }
67
68 if (width % kLibJpegDctSize != 0 || height % kLibJpegDctSize != 0) {
69 // Input dimension needs to be multiple of libjpeg DCT size.
70 // TODO(b/301023410) This restriction can be removed once we add support for
71 // unaligned jpeg compression.
72 return false;
73 }
74
Biswarup Pal6152a302023-12-19 12:44:09 +000075 if (maxFps <= 0 || maxFps > kMaxFpsUpperLimit) {
76 return false;
77 }
78
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010079 return true;
80}
81
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010082} // namespace virtualcamera
83} // namespace companion
84} // namespace android