• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <xf86drm.h>
26 #include <xf86drmMode.h>
27 
28 #include "utils/log.h"
29 #include "utils/properties.h"
30 
31 namespace android {
32 
GetInstance()33 BufferInfoGetter *BufferInfoGetter::GetInstance() {
34   static std::unique_ptr<BufferInfoGetter> inst;
35   if (!inst) {
36 #if PLATFORM_SDK_VERSION >= 30 && defined(USE_IMAPPER4_METADATA_API)
37     inst.reset(BufferInfoMapperMetadata::CreateInstance());
38     if (!inst) {
39       ALOGW(
40           "Generic buffer getter is not available. Falling back to legacy...");
41     }
42 #endif
43     if (!inst) {
44       inst = LegacyBufferInfoGetter::CreateInstance();
45     }
46   }
47 
48   return inst.get();
49 }
50 
IsHandleUsable(buffer_handle_t handle)51 bool BufferInfoGetter::IsHandleUsable(buffer_handle_t handle) {
52   hwc_drm_bo_t bo;
53   memset(&bo, 0, sizeof(hwc_drm_bo_t));
54 
55   if (ConvertBoInfo(handle, &bo) != 0) {
56     return false;
57   }
58   if (bo.prime_fds[0] == 0) {
59     return false;
60   }
61   return true;
62 }
63 
Init()64 int LegacyBufferInfoGetter::Init() {
65   int ret = hw_get_module(
66       GRALLOC_HARDWARE_MODULE_ID,
67       // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
68       reinterpret_cast<const hw_module_t **>(&gralloc_));
69   if (ret != 0) {
70     ALOGE("Failed to open gralloc module");
71     return ret;
72   }
73 
74   ALOGI("Using %s gralloc module: %s\n", gralloc_->common.name,
75         gralloc_->common.author);
76 
77   return 0;
78 }
79 
ConvertHalFormatToDrm(uint32_t hal_format)80 uint32_t LegacyBufferInfoGetter::ConvertHalFormatToDrm(uint32_t hal_format) {
81   switch (hal_format) {
82     case HAL_PIXEL_FORMAT_RGB_888:
83       return DRM_FORMAT_BGR888;
84     case HAL_PIXEL_FORMAT_BGRA_8888:
85       return DRM_FORMAT_ARGB8888;
86     case HAL_PIXEL_FORMAT_RGBX_8888:
87       return DRM_FORMAT_XBGR8888;
88     case HAL_PIXEL_FORMAT_RGBA_8888:
89       return DRM_FORMAT_ABGR8888;
90     case HAL_PIXEL_FORMAT_RGB_565:
91       return DRM_FORMAT_BGR565;
92     case HAL_PIXEL_FORMAT_YV12:
93       return DRM_FORMAT_YVU420;
94     case HAL_PIXEL_FORMAT_RGBA_1010102:
95       return DRM_FORMAT_ABGR2101010;
96     default:
97       ALOGE("Cannot convert hal format to drm format %u", hal_format);
98       return DRM_FORMAT_INVALID;
99   }
100 }
101 
IsDrmFormatRgb(uint32_t drm_format)102 bool BufferInfoGetter::IsDrmFormatRgb(uint32_t drm_format) {
103   switch (drm_format) {
104     case DRM_FORMAT_ARGB8888:
105     case DRM_FORMAT_XBGR8888:
106     case DRM_FORMAT_ABGR8888:
107     case DRM_FORMAT_BGR888:
108     case DRM_FORMAT_BGR565:
109     case DRM_FORMAT_ABGR2101010:
110       return true;
111     default:
112       return false;
113   }
114 }
115 
116 __attribute__((weak)) std::unique_ptr<LegacyBufferInfoGetter>
CreateInstance()117 LegacyBufferInfoGetter::CreateInstance() {
118   ALOGE("No legacy buffer info getters available");
119   return nullptr;
120 }
121 
122 }  // namespace android
123