blob: 57dc7f182f589fcb585586adf3057f803b04bed3 [file] [log] [blame]
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +03001/*
2 * Copyright (C) 2020 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_TAG "hwc-buffer-info-getter"
18
19#include "BufferInfoGetter.h"
20
21#if PLATFORM_SDK_VERSION >= 30
22#include "BufferInfoMapperMetadata.h"
23#endif
24
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030025#include <xf86drm.h>
26#include <xf86drmMode.h>
27
Roman Stratiienko2640cd82021-02-26 17:49:40 +020028#include "utils/log.h"
29#include "utils/properties.h"
30
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030031namespace android {
32
33BufferInfoGetter *BufferInfoGetter::GetInstance() {
34 static std::unique_ptr<BufferInfoGetter> inst;
Roman Stratiienko0ee8f582021-10-28 15:52:38 +030035 if (!inst) {
36#if PLATFORM_SDK_VERSION >= 30 && defined(USE_IMAPPER4_METADATA_API)
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030037 inst.reset(BufferInfoMapperMetadata::CreateInstance());
Roman Stratiienko0ee8f582021-10-28 15:52:38 +030038 if (!inst) {
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030039 ALOGW(
40 "Generic buffer getter is not available. Falling back to legacy...");
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030041 }
42#endif
Roman Stratiienko0ee8f582021-10-28 15:52:38 +030043 if (!inst) {
44 inst = LegacyBufferInfoGetter::CreateInstance();
45 }
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030046 }
47
48 return inst.get();
49}
50
51bool BufferInfoGetter::IsHandleUsable(buffer_handle_t handle) {
Roman Stratiienko1cbaaf92022-02-21 10:52:18 +020052 BufferInfo bo{};
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030053
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020054 if (ConvertBoInfo(handle, &bo) != 0) {
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030055 return false;
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020056 }
57 if (bo.prime_fds[0] == 0) {
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030058 return false;
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020059 }
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030060 return true;
61}
62
63int LegacyBufferInfoGetter::Init() {
Roman Stratiienkofc014f52021-12-23 19:04:29 +020064 int ret = hw_get_module(
65 GRALLOC_HARDWARE_MODULE_ID,
66 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
67 reinterpret_cast<const hw_module_t **>(&gralloc_));
68 if (ret != 0) {
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030069 ALOGE("Failed to open gralloc module");
70 return ret;
71 }
72
73 ALOGI("Using %s gralloc module: %s\n", gralloc_->common.name,
74 gralloc_->common.author);
75
76 return 0;
77}
78
79uint32_t LegacyBufferInfoGetter::ConvertHalFormatToDrm(uint32_t hal_format) {
80 switch (hal_format) {
81 case HAL_PIXEL_FORMAT_RGB_888:
82 return DRM_FORMAT_BGR888;
83 case HAL_PIXEL_FORMAT_BGRA_8888:
84 return DRM_FORMAT_ARGB8888;
85 case HAL_PIXEL_FORMAT_RGBX_8888:
86 return DRM_FORMAT_XBGR8888;
87 case HAL_PIXEL_FORMAT_RGBA_8888:
88 return DRM_FORMAT_ABGR8888;
89 case HAL_PIXEL_FORMAT_RGB_565:
90 return DRM_FORMAT_BGR565;
91 case HAL_PIXEL_FORMAT_YV12:
92 return DRM_FORMAT_YVU420;
Riadh Ghaddab310e3b72021-02-05 12:06:50 +000093 case HAL_PIXEL_FORMAT_RGBA_1010102:
94 return DRM_FORMAT_ABGR2101010;
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030095 default:
96 ALOGE("Cannot convert hal format to drm format %u", hal_format);
97 return DRM_FORMAT_INVALID;
98 }
99}
100
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +0300101bool BufferInfoGetter::IsDrmFormatRgb(uint32_t drm_format) {
102 switch (drm_format) {
103 case DRM_FORMAT_ARGB8888:
104 case DRM_FORMAT_XBGR8888:
105 case DRM_FORMAT_ABGR8888:
106 case DRM_FORMAT_BGR888:
107 case DRM_FORMAT_BGR565:
Riadh Ghaddab310e3b72021-02-05 12:06:50 +0000108 case DRM_FORMAT_ABGR2101010:
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +0300109 return true;
110 default:
111 return false;
112 }
113}
114
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200115__attribute__((weak)) std::unique_ptr<LegacyBufferInfoGetter>
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +0300116LegacyBufferInfoGetter::CreateInstance() {
117 ALOGE("No legacy buffer info getters available");
118 return nullptr;
119}
120
121} // namespace android