blob: 11dc3a6b79d2c07bb2ad30bb6d86fe471d249e57 [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 Sebechlebsky3b478c42023-11-23 13:15:56 +010021#include "jpeglib.h"
22
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010023namespace android {
24namespace companion {
25namespace virtualcamera {
26
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010027// Lower bound for maximal supported texture size is at least 2048x2048
28// but on most platforms will be more.
29// TODO(b/301023410) - Query actual max texture size.
30constexpr int kMaxTextureSize = 2048;
31constexpr int kLibJpegDctSize = DCTSIZE;
32
33using ::aidl::android::companion::virtualcamera::Format;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010034using ::aidl::android::hardware::common::NativeHandle;
35
36sp<Fence> importFence(const NativeHandle& aidlHandle) {
37 if (aidlHandle.fds.size() != 1) {
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010038 return sp<Fence>::make();
39 }
40
41 return sp<Fence>::make(::dup(aidlHandle.fds[0].get()));
42}
43
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010044// Returns true if specified format is supported for virtual camera input.
45bool isFormatSupportedForInput(const int width, const int height,
46 const Format format) {
47 if (format != Format::YUV_420_888) {
48 // For now only YUV_420_888 is supported for input.
49 return false;
50 }
51
52 if (width <= 0 || height <= 0 || width > kMaxTextureSize ||
53 height > kMaxTextureSize) {
54 return false;
55 }
56
57 if (width % kLibJpegDctSize != 0 || height % kLibJpegDctSize != 0) {
58 // Input dimension needs to be multiple of libjpeg DCT size.
59 // TODO(b/301023410) This restriction can be removed once we add support for
60 // unaligned jpeg compression.
61 return false;
62 }
63
64 return true;
65}
66
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010067} // namespace virtualcamera
68} // namespace companion
69} // namespace android