blob: 77a76e8584ef8e1d5dc21514f885e7335f0640ec [file] [log] [blame]
Harish Mahendrakarf5dec502022-04-13 15:53:55 -07001/*
2 * Copyright 2018, 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//#define LOG_NDEBUG 0
18#define LOG_TAG "Codec2BufferUtils"
19#define ATRACE_TAG ATRACE_TAG_VIDEO
20#include <utils/Log.h>
21
22#include <android/hardware_buffer.h>
Harish Mahendrakardfbaa2e2022-05-26 11:17:36 -070023#include <android-base/properties.h>
Harish Mahendrakarf5dec502022-04-13 15:53:55 -070024#include <cutils/properties.h>
25#include <media/hardware/HardwareAPI.h>
26#include <system/graphics.h>
27
28#include <C2Debug.h>
29
30#include "Codec2CommonUtils.h"
31
32namespace android {
33
Harish Mahendrakar56800862023-08-08 22:14:21 +000034
35static bool isAtLeast(int version, const char *codeName) {
Harish Mahendrakarf5dec502022-04-13 15:53:55 -070036 char deviceCodeName[PROP_VALUE_MAX];
37 __system_property_get("ro.build.version.codename", deviceCodeName);
Harish Mahendrakar56800862023-08-08 22:14:21 +000038 return android_get_device_api_level() >= version || !strcmp(deviceCodeName, codeName);
39}
40
41bool isAtLeastT() {
42 return isAtLeast(__ANDROID_API_T__, "Tiramisu");
43}
44
45bool isAtLeastU() {
46 return isAtLeast(__ANDROID_API_U__, "UpsideDownCake");
Harish Mahendrakarf5dec502022-04-13 15:53:55 -070047}
48
Harish Mahendrakarfc3f7412024-03-21 17:29:37 +000049bool isAtLeastV() {
50 return isAtLeast(__ANDROID_API_V__, "VanillaIceCream");
51}
52
Harish Mahendrakarc95668e2022-10-26 15:06:56 -070053static bool isP010Allowed() {
Justin Yunf16b32b2023-11-17 18:26:32 +090054 // The Vendor API level which is min(ro.product.first_api_level, ro.board.[first_]api_level).
55 // This is the api level to which VSR requirement the device conform.
56 static const int32_t kVendorApiLevel =
57 base::GetIntProperty<int32_t>("ro.vendor.api_level", 0);
Harish Mahendrakardfbaa2e2022-05-26 11:17:36 -070058
Justin Yunf16b32b2023-11-17 18:26:32 +090059 return kVendorApiLevel >= __ANDROID_API_T__;
Harish Mahendrakardfbaa2e2022-05-26 11:17:36 -070060}
61
Harish Mahendrakarf5dec502022-04-13 15:53:55 -070062bool isHalPixelFormatSupported(AHardwareBuffer_Format format) {
Harish Mahendrakardfbaa2e2022-05-26 11:17:36 -070063 // HAL_PIXEL_FORMAT_YCBCR_P010 requirement was added in T VSR, although it could have been
64 // supported prior to this.
65 //
66 // Unfortunately, we cannot detect if P010 is properly supported using AHardwareBuffer
67 // API alone. For now limit P010 to devices that launched with Android T or known to conform
68 // to Android T VSR (as opposed to simply limiting to a T vendor image).
69 if (format == (AHardwareBuffer_Format)HAL_PIXEL_FORMAT_YCBCR_P010 &&
Harish Mahendrakarc95668e2022-10-26 15:06:56 -070070 !isP010Allowed()) {
Harish Mahendrakarf5dec502022-04-13 15:53:55 -070071 return false;
72 }
73
mtk12103a438ca02023-06-09 14:00:55 +080074 // Default scenario --- the consumer is display or GPU
Wonsik Kimc4ad1cd2023-07-07 17:42:09 +000075 const AHardwareBuffer_Desc consumableForDisplayOrGpu = {
Harish Mahendrakarf5dec502022-04-13 15:53:55 -070076 .width = 320,
77 .height = 240,
78 .format = format,
79 .layers = 1,
Harish Mahendrakardfbaa2e2022-05-26 11:17:36 -070080 .usage = AHARDWAREBUFFER_USAGE_CPU_READ_RARELY |
81 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
82 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
83 AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY,
Harish Mahendrakarf5dec502022-04-13 15:53:55 -070084 .stride = 0,
85 .rfu0 = 0,
86 .rfu1 = 0,
87 };
88
mtk12103a438ca02023-06-09 14:00:55 +080089 // The consumer is a HW encoder
Wonsik Kimc4ad1cd2023-07-07 17:42:09 +000090 const AHardwareBuffer_Desc consumableForHwEncoder = {
mtk12103a438ca02023-06-09 14:00:55 +080091 .width = 320,
92 .height = 240,
93 .format = format,
94 .layers = 1,
95 .usage = AHARDWAREBUFFER_USAGE_CPU_READ_RARELY |
96 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
97 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
mtk12103a438ca02023-06-09 14:00:55 +080098 AHARDWAREBUFFER_USAGE_VIDEO_ENCODE,
99 .stride = 0,
100 .rfu0 = 0,
101 .rfu1 = 0,
102 };
103
104 // The consumer is a SW encoder
Wonsik Kimc4ad1cd2023-07-07 17:42:09 +0000105 const AHardwareBuffer_Desc consumableForSwEncoder = {
mtk12103a438ca02023-06-09 14:00:55 +0800106 .width = 320,
107 .height = 240,
108 .format = format,
109 .layers = 1,
110 .usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
111 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
112 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
113 AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY,
114 .stride = 0,
115 .rfu0 = 0,
116 .rfu1 = 0,
117 };
Harish Mahendrakar56800862023-08-08 22:14:21 +0000118 // Some devices running versions prior to Android U aren't guaranteed to advertise support
119 // for some color formats when the consumer is an encoder. Hence limit these checks to
120 // Android U and beyond.
121 if (isAtLeastU()) {
122 return AHardwareBuffer_isSupported(&consumableForDisplayOrGpu)
123 && AHardwareBuffer_isSupported(&consumableForHwEncoder)
124 && AHardwareBuffer_isSupported(&consumableForSwEncoder);
125 } else {
126 return AHardwareBuffer_isSupported(&consumableForDisplayOrGpu);
127 }
Harish Mahendrakarf5dec502022-04-13 15:53:55 -0700128}
129
130} // namespace android