Harish Mahendrakar | f5dec50 | 2022-04-13 15:53:55 -0700 | [diff] [blame] | 1 | /* |
| 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 Mahendrakar | dfbaa2e | 2022-05-26 11:17:36 -0700 | [diff] [blame] | 23 | #include <android-base/properties.h> |
Harish Mahendrakar | f5dec50 | 2022-04-13 15:53:55 -0700 | [diff] [blame] | 24 | #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 | |
| 32 | namespace android { |
| 33 | |
Harish Mahendrakar | 5680086 | 2023-08-08 22:14:21 +0000 | [diff] [blame] | 34 | |
Harish Mahendrakar | 7b3f96f | 2024-05-08 17:54:15 -0700 | [diff] [blame] | 35 | static bool isAtLeast(int version, const std::string codeName) { |
| 36 | static std::once_flag sCheckOnce; |
| 37 | static std::string sDeviceCodeName; |
| 38 | static int sDeviceApiLevel; |
| 39 | std::call_once(sCheckOnce, [&](){ |
| 40 | sDeviceCodeName = base::GetProperty("ro.build.version.codename", ""); |
| 41 | sDeviceApiLevel = android_get_device_api_level(); |
| 42 | }); |
| 43 | return sDeviceApiLevel >= version || sDeviceCodeName == codeName; |
Harish Mahendrakar | 5680086 | 2023-08-08 22:14:21 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | bool isAtLeastT() { |
| 47 | return isAtLeast(__ANDROID_API_T__, "Tiramisu"); |
| 48 | } |
| 49 | |
| 50 | bool isAtLeastU() { |
| 51 | return isAtLeast(__ANDROID_API_U__, "UpsideDownCake"); |
Harish Mahendrakar | f5dec50 | 2022-04-13 15:53:55 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Harish Mahendrakar | fc3f741 | 2024-03-21 17:29:37 +0000 | [diff] [blame] | 54 | bool isAtLeastV() { |
| 55 | return isAtLeast(__ANDROID_API_V__, "VanillaIceCream"); |
| 56 | } |
| 57 | |
Harish Mahendrakar | c95668e | 2022-10-26 15:06:56 -0700 | [diff] [blame] | 58 | static bool isP010Allowed() { |
Justin Yun | f16b32b | 2023-11-17 18:26:32 +0900 | [diff] [blame] | 59 | // The Vendor API level which is min(ro.product.first_api_level, ro.board.[first_]api_level). |
| 60 | // This is the api level to which VSR requirement the device conform. |
| 61 | static const int32_t kVendorApiLevel = |
| 62 | base::GetIntProperty<int32_t>("ro.vendor.api_level", 0); |
Harish Mahendrakar | dfbaa2e | 2022-05-26 11:17:36 -0700 | [diff] [blame] | 63 | |
Justin Yun | f16b32b | 2023-11-17 18:26:32 +0900 | [diff] [blame] | 64 | return kVendorApiLevel >= __ANDROID_API_T__; |
Harish Mahendrakar | dfbaa2e | 2022-05-26 11:17:36 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Fyodor Kyslov | 7f64e01 | 2024-11-15 20:17:33 +0000 | [diff] [blame^] | 67 | static bool isP210Allowed() { |
| 68 | static const int32_t kVendorApiLevel = |
| 69 | base::GetIntProperty<int32_t>("ro.vendor.api_level", 0); |
| 70 | |
| 71 | return kVendorApiLevel > __ANDROID_API_V__; |
| 72 | } |
| 73 | |
Harish Mahendrakar | f5dec50 | 2022-04-13 15:53:55 -0700 | [diff] [blame] | 74 | bool isHalPixelFormatSupported(AHardwareBuffer_Format format) { |
Harish Mahendrakar | dfbaa2e | 2022-05-26 11:17:36 -0700 | [diff] [blame] | 75 | // HAL_PIXEL_FORMAT_YCBCR_P010 requirement was added in T VSR, although it could have been |
| 76 | // supported prior to this. |
| 77 | // |
| 78 | // Unfortunately, we cannot detect if P010 is properly supported using AHardwareBuffer |
| 79 | // API alone. For now limit P010 to devices that launched with Android T or known to conform |
| 80 | // to Android T VSR (as opposed to simply limiting to a T vendor image). |
| 81 | if (format == (AHardwareBuffer_Format)HAL_PIXEL_FORMAT_YCBCR_P010 && |
Harish Mahendrakar | c95668e | 2022-10-26 15:06:56 -0700 | [diff] [blame] | 82 | !isP010Allowed()) { |
Harish Mahendrakar | f5dec50 | 2022-04-13 15:53:55 -0700 | [diff] [blame] | 83 | return false; |
| 84 | } |
| 85 | |
Fyodor Kyslov | 7f64e01 | 2024-11-15 20:17:33 +0000 | [diff] [blame^] | 86 | // P210 is not available before Android B |
| 87 | if (format == (AHardwareBuffer_Format)AHARDWAREBUFFER_FORMAT_YCbCr_P210 && |
| 88 | !isP210Allowed()) { |
| 89 | return false; |
| 90 | } |
| 91 | |
mtk12103 | a438ca0 | 2023-06-09 14:00:55 +0800 | [diff] [blame] | 92 | // Default scenario --- the consumer is display or GPU |
Wonsik Kim | c4ad1cd | 2023-07-07 17:42:09 +0000 | [diff] [blame] | 93 | const AHardwareBuffer_Desc consumableForDisplayOrGpu = { |
Harish Mahendrakar | f5dec50 | 2022-04-13 15:53:55 -0700 | [diff] [blame] | 94 | .width = 320, |
| 95 | .height = 240, |
| 96 | .format = format, |
| 97 | .layers = 1, |
Harish Mahendrakar | dfbaa2e | 2022-05-26 11:17:36 -0700 | [diff] [blame] | 98 | .usage = AHARDWAREBUFFER_USAGE_CPU_READ_RARELY | |
| 99 | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN | |
| 100 | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | |
| 101 | AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY, |
Harish Mahendrakar | f5dec50 | 2022-04-13 15:53:55 -0700 | [diff] [blame] | 102 | .stride = 0, |
| 103 | .rfu0 = 0, |
| 104 | .rfu1 = 0, |
| 105 | }; |
| 106 | |
mtk12103 | a438ca0 | 2023-06-09 14:00:55 +0800 | [diff] [blame] | 107 | // The consumer is a HW encoder |
Wonsik Kim | c4ad1cd | 2023-07-07 17:42:09 +0000 | [diff] [blame] | 108 | const AHardwareBuffer_Desc consumableForHwEncoder = { |
mtk12103 | a438ca0 | 2023-06-09 14:00:55 +0800 | [diff] [blame] | 109 | .width = 320, |
| 110 | .height = 240, |
| 111 | .format = format, |
| 112 | .layers = 1, |
| 113 | .usage = AHARDWAREBUFFER_USAGE_CPU_READ_RARELY | |
| 114 | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN | |
| 115 | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | |
mtk12103 | a438ca0 | 2023-06-09 14:00:55 +0800 | [diff] [blame] | 116 | AHARDWAREBUFFER_USAGE_VIDEO_ENCODE, |
| 117 | .stride = 0, |
| 118 | .rfu0 = 0, |
| 119 | .rfu1 = 0, |
| 120 | }; |
| 121 | |
| 122 | // The consumer is a SW encoder |
Wonsik Kim | c4ad1cd | 2023-07-07 17:42:09 +0000 | [diff] [blame] | 123 | const AHardwareBuffer_Desc consumableForSwEncoder = { |
mtk12103 | a438ca0 | 2023-06-09 14:00:55 +0800 | [diff] [blame] | 124 | .width = 320, |
| 125 | .height = 240, |
| 126 | .format = format, |
| 127 | .layers = 1, |
| 128 | .usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | |
| 129 | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN | |
| 130 | AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | |
| 131 | AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY, |
| 132 | .stride = 0, |
| 133 | .rfu0 = 0, |
| 134 | .rfu1 = 0, |
| 135 | }; |
Harish Mahendrakar | 5680086 | 2023-08-08 22:14:21 +0000 | [diff] [blame] | 136 | // Some devices running versions prior to Android U aren't guaranteed to advertise support |
| 137 | // for some color formats when the consumer is an encoder. Hence limit these checks to |
| 138 | // Android U and beyond. |
| 139 | if (isAtLeastU()) { |
| 140 | return AHardwareBuffer_isSupported(&consumableForDisplayOrGpu) |
| 141 | && AHardwareBuffer_isSupported(&consumableForHwEncoder) |
| 142 | && AHardwareBuffer_isSupported(&consumableForSwEncoder); |
| 143 | } else { |
| 144 | return AHardwareBuffer_isSupported(&consumableForDisplayOrGpu); |
| 145 | } |
Harish Mahendrakar | f5dec50 | 2022-04-13 15:53:55 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | } // namespace android |