1 /*
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
25 #include <cutils/properties.h>
26 #include <gralloc_handle.h>
27 #include <hardware/gralloc.h>
28 #include <log/log.h>
29 #include <xf86drm.h>
30 #include <xf86drmMode.h>
31
32 namespace android {
33
GetInstance()34 BufferInfoGetter *BufferInfoGetter::GetInstance() {
35 static std::unique_ptr<BufferInfoGetter> inst;
36 if (inst == nullptr) {
37 #if PLATFORM_SDK_VERSION >= 30
38 inst.reset(BufferInfoMapperMetadata::CreateInstance());
39 if (inst == nullptr) {
40 ALOGW(
41 "Generic buffer getter is not available. Falling back to legacy...");
42 #endif
43 inst.reset(LegacyBufferInfoGetter::CreateInstance());
44 #if PLATFORM_SDK_VERSION >= 30
45 }
46 #endif
47 }
48
49 return inst.get();
50 }
51
IsHandleUsable(buffer_handle_t handle)52 bool BufferInfoGetter::IsHandleUsable(buffer_handle_t handle) {
53 hwc_drm_bo_t bo;
54 memset(&bo, 0, sizeof(hwc_drm_bo_t));
55
56 if (ConvertBoInfo(handle, &bo) != 0)
57 return false;
58
59 if (bo.prime_fds[0] == 0)
60 return false;
61
62 return true;
63 }
64
Init()65 int LegacyBufferInfoGetter::Init() {
66 int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
67 (const hw_module_t **)&gralloc_);
68 if (ret) {
69 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
ConvertHalFormatToDrm(uint32_t hal_format)79 uint32_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;
93 default:
94 ALOGE("Cannot convert hal format to drm format %u", hal_format);
95 return DRM_FORMAT_INVALID;
96 }
97 }
98
IsDrmFormatRgb(uint32_t drm_format)99 bool BufferInfoGetter::IsDrmFormatRgb(uint32_t drm_format) {
100 switch (drm_format) {
101 case DRM_FORMAT_ARGB8888:
102 case DRM_FORMAT_XBGR8888:
103 case DRM_FORMAT_ABGR8888:
104 case DRM_FORMAT_BGR888:
105 case DRM_FORMAT_BGR565:
106 return true;
107 default:
108 return false;
109 }
110 }
111
112 __attribute__((weak)) LegacyBufferInfoGetter *
CreateInstance()113 LegacyBufferInfoGetter::CreateInstance() {
114 ALOGE("No legacy buffer info getters available");
115 return nullptr;
116 }
117
118 } // namespace android
119