blob: 95c1a234db50562c9ec2798b56ba22890d964549 [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 Stratiienko74d2c4a2021-12-17 18:10:57 +020025#include <sys/stat.h>
26#include <sys/types.h>
27#include <unistd.h>
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030028#include <xf86drm.h>
29#include <xf86drmMode.h>
30
Roman Stratiienko74d2c4a2021-12-17 18:10:57 +020031#include <mutex>
32
Roman Stratiienko2640cd82021-02-26 17:49:40 +020033#include "utils/log.h"
34#include "utils/properties.h"
35
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030036namespace android {
37
38BufferInfoGetter *BufferInfoGetter::GetInstance() {
39 static std::unique_ptr<BufferInfoGetter> inst;
Roman Stratiienko0ee8f582021-10-28 15:52:38 +030040 if (!inst) {
41#if PLATFORM_SDK_VERSION >= 30 && defined(USE_IMAPPER4_METADATA_API)
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030042 inst.reset(BufferInfoMapperMetadata::CreateInstance());
Roman Stratiienko0ee8f582021-10-28 15:52:38 +030043 if (!inst) {
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030044 ALOGW(
45 "Generic buffer getter is not available. Falling back to legacy...");
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030046 }
47#endif
Roman Stratiienko0ee8f582021-10-28 15:52:38 +030048 if (!inst) {
49 inst = LegacyBufferInfoGetter::CreateInstance();
50 }
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030051 }
52
53 return inst.get();
54}
55
Roman Stratiienko74d2c4a2021-12-17 18:10:57 +020056std::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
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030070int LegacyBufferInfoGetter::Init() {
Roman Stratiienkofc014f52021-12-23 19:04:29 +020071 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) {
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030076 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
86uint32_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;
Riadh Ghaddab310e3b72021-02-05 12:06:50 +0000100 case HAL_PIXEL_FORMAT_RGBA_1010102:
101 return DRM_FORMAT_ABGR2101010;
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +0300102 default:
103 ALOGE("Cannot convert hal format to drm format %u", hal_format);
104 return DRM_FORMAT_INVALID;
105 }
106}
107
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +0300108bool 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:
Riadh Ghaddab310e3b72021-02-05 12:06:50 +0000115 case DRM_FORMAT_ABGR2101010:
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +0300116 return true;
117 default:
118 return false;
119 }
120}
121
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200122__attribute__((weak)) std::unique_ptr<LegacyBufferInfoGetter>
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +0300123LegacyBufferInfoGetter::CreateInstance() {
124 ALOGE("No legacy buffer info getters available");
125 return nullptr;
126}
127
128} // namespace android