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>
23 #include <android-base/properties.h>
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
isAtLeastT()34 bool isAtLeastT() {
35 char deviceCodeName[PROP_VALUE_MAX];
36 __system_property_get("ro.build.version.codename", deviceCodeName);
37 return android_get_device_api_level() >= __ANDROID_API_T__ ||
38 !strcmp(deviceCodeName, "Tiramisu");
39 }
40
isVendorApiOrFirstApiAtLeastT()41 bool isVendorApiOrFirstApiAtLeastT() {
42 // The first SDK the device shipped with.
43 static const int32_t kProductFirstApiLevel =
44 base::GetIntProperty<int32_t>("ro.product.first_api_level", 0);
45
46 // GRF devices (introduced in Android 11) list the first and possibly the current api levels
47 // to signal which VSR requirements they conform to even if the first device SDK was higher.
48 static const int32_t kBoardFirstApiLevel =
49 base::GetIntProperty<int32_t>("ro.board.first_api_level", 0);
50 static const int32_t kBoardApiLevel =
51 base::GetIntProperty<int32_t>("ro.board.api_level", 0);
52
53 // For non-GRF devices, use the first SDK version by the product.
54 static const int32_t kFirstApiLevel =
55 kBoardApiLevel != 0 ? kBoardApiLevel :
56 kBoardFirstApiLevel != 0 ? kBoardFirstApiLevel :
57 kProductFirstApiLevel;
58
59 return kFirstApiLevel >= __ANDROID_API_T__;
60 }
61
isHalPixelFormatSupported(AHardwareBuffer_Format format)62 bool isHalPixelFormatSupported(AHardwareBuffer_Format format) {
63 // 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 &&
70 !isVendorApiOrFirstApiAtLeastT()) {
71 return false;
72 }
73
74 const AHardwareBuffer_Desc desc = {
75 .width = 320,
76 .height = 240,
77 .format = format,
78 .layers = 1,
79 .usage = AHARDWAREBUFFER_USAGE_CPU_READ_RARELY |
80 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
81 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
82 AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY,
83 .stride = 0,
84 .rfu0 = 0,
85 .rfu1 = 0,
86 };
87
88 return AHardwareBuffer_isSupported(&desc);
89 }
90
91 } // namespace android
92