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