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