blob: 43533fd58531eb99364054a067fa00784a3420f5 [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 Mahendrakarc95668e2022-10-26 15:06:56 -070049static bool isP010Allowed() {
Harish Mahendrakardfbaa2e2022-05-26 11:17:36 -070050 // The first SDK the device shipped with.
51 static const int32_t kProductFirstApiLevel =
52 base::GetIntProperty<int32_t>("ro.product.first_api_level", 0);
53
54 // GRF devices (introduced in Android 11) list the first and possibly the current api levels
55 // to signal which VSR requirements they conform to even if the first device SDK was higher.
56 static const int32_t kBoardFirstApiLevel =
57 base::GetIntProperty<int32_t>("ro.board.first_api_level", 0);
Harish Mahendrakarc95668e2022-10-26 15:06:56 -070058
59 // Some devices that launched prior to Android S may not support P010 correctly, even
60 // though they may advertise it as supported.
61 if (kProductFirstApiLevel != 0 && kProductFirstApiLevel < __ANDROID_API_S__) {
62 return false;
63 }
64
65 if (kBoardFirstApiLevel != 0 && kBoardFirstApiLevel < __ANDROID_API_S__) {
66 return false;
67 }
68
Harish Mahendrakardfbaa2e2022-05-26 11:17:36 -070069 static const int32_t kBoardApiLevel =
70 base::GetIntProperty<int32_t>("ro.board.api_level", 0);
71
72 // For non-GRF devices, use the first SDK version by the product.
73 static const int32_t kFirstApiLevel =
74 kBoardApiLevel != 0 ? kBoardApiLevel :
75 kBoardFirstApiLevel != 0 ? kBoardFirstApiLevel :
76 kProductFirstApiLevel;
77
78 return kFirstApiLevel >= __ANDROID_API_T__;
79}
80
Harish Mahendrakarf5dec502022-04-13 15:53:55 -070081bool isHalPixelFormatSupported(AHardwareBuffer_Format format) {
Harish Mahendrakardfbaa2e2022-05-26 11:17:36 -070082 // HAL_PIXEL_FORMAT_YCBCR_P010 requirement was added in T VSR, although it could have been
83 // supported prior to this.
84 //
85 // Unfortunately, we cannot detect if P010 is properly supported using AHardwareBuffer
86 // API alone. For now limit P010 to devices that launched with Android T or known to conform
87 // to Android T VSR (as opposed to simply limiting to a T vendor image).
88 if (format == (AHardwareBuffer_Format)HAL_PIXEL_FORMAT_YCBCR_P010 &&
Harish Mahendrakarc95668e2022-10-26 15:06:56 -070089 !isP010Allowed()) {
Harish Mahendrakarf5dec502022-04-13 15:53:55 -070090 return false;
91 }
92
mtk12103a438ca02023-06-09 14:00:55 +080093 // Default scenario --- the consumer is display or GPU
Wonsik Kimc4ad1cd2023-07-07 17:42:09 +000094 const AHardwareBuffer_Desc consumableForDisplayOrGpu = {
Harish Mahendrakarf5dec502022-04-13 15:53:55 -070095 .width = 320,
96 .height = 240,
97 .format = format,
98 .layers = 1,
Harish Mahendrakardfbaa2e2022-05-26 11:17:36 -070099 .usage = AHARDWAREBUFFER_USAGE_CPU_READ_RARELY |
100 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
101 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
102 AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY,
Harish Mahendrakarf5dec502022-04-13 15:53:55 -0700103 .stride = 0,
104 .rfu0 = 0,
105 .rfu1 = 0,
106 };
107
mtk12103a438ca02023-06-09 14:00:55 +0800108 // The consumer is a HW encoder
Wonsik Kimc4ad1cd2023-07-07 17:42:09 +0000109 const AHardwareBuffer_Desc consumableForHwEncoder = {
mtk12103a438ca02023-06-09 14:00:55 +0800110 .width = 320,
111 .height = 240,
112 .format = format,
113 .layers = 1,
114 .usage = AHARDWAREBUFFER_USAGE_CPU_READ_RARELY |
115 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
116 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
117 AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY |
118 AHARDWAREBUFFER_USAGE_VIDEO_ENCODE,
119 .stride = 0,
120 .rfu0 = 0,
121 .rfu1 = 0,
122 };
123
124 // The consumer is a SW encoder
Wonsik Kimc4ad1cd2023-07-07 17:42:09 +0000125 const AHardwareBuffer_Desc consumableForSwEncoder = {
mtk12103a438ca02023-06-09 14:00:55 +0800126 .width = 320,
127 .height = 240,
128 .format = format,
129 .layers = 1,
130 .usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
131 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
132 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
133 AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY,
134 .stride = 0,
135 .rfu0 = 0,
136 .rfu1 = 0,
137 };
Harish Mahendrakar56800862023-08-08 22:14:21 +0000138 // Some devices running versions prior to Android U aren't guaranteed to advertise support
139 // for some color formats when the consumer is an encoder. Hence limit these checks to
140 // Android U and beyond.
141 if (isAtLeastU()) {
142 return AHardwareBuffer_isSupported(&consumableForDisplayOrGpu)
143 && AHardwareBuffer_isSupported(&consumableForHwEncoder)
144 && AHardwareBuffer_isSupported(&consumableForSwEncoder);
145 } else {
146 return AHardwareBuffer_isSupported(&consumableForDisplayOrGpu);
147 }
Harish Mahendrakarf5dec502022-04-13 15:53:55 -0700148}
149
150} // namespace android