blob: df771b145af7e9340307f6c438e69912d59db5af [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;
38
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010039constexpr std::array<Format, 2> kSupportedFormats{Format::YUV_420_888,
40 Format::RGBA_8888};
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010041
42sp<Fence> importFence(const NativeHandle& aidlHandle) {
43 if (aidlHandle.fds.size() != 1) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010044 return sp<Fence>::make();
45 }
46
47 return sp<Fence>::make(::dup(aidlHandle.fds[0].get()));
48}
49
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010050bool isPixelFormatSupportedForInput(const Format format) {
51 return std::find(kSupportedFormats.begin(), kSupportedFormats.end(),
52 format) != kSupportedFormats.end();
53}
54
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010055// Returns true if specified format is supported for virtual camera input.
56bool isFormatSupportedForInput(const int width, const int height,
57 const Format format) {
Jan Sebechlebsky042d1fb2023-12-12 16:37:00 +010058 if (!isPixelFormatSupportedForInput(format)) {
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010059 return false;
60 }
61
62 if (width <= 0 || height <= 0 || width > kMaxTextureSize ||
63 height > kMaxTextureSize) {
64 return false;
65 }
66
67 if (width % kLibJpegDctSize != 0 || height % kLibJpegDctSize != 0) {
68 // Input dimension needs to be multiple of libjpeg DCT size.
69 // TODO(b/301023410) This restriction can be removed once we add support for
70 // unaligned jpeg compression.
71 return false;
72 }
73
74 return true;
75}
76
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010077} // namespace virtualcamera
78} // namespace companion
79} // namespace android