blob: 125c5c8ae03413ed4534365092d291827aeb8c9f [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
Roman Stratiienko6b405052022-12-10 19:09:10 +020017#if __ANDROID_API__ >= 30
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030018
Sean Paul468a7542024-07-16 19:50:58 +000019#define LOG_TAG "drmhwc"
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030020
21#include "BufferInfoMapperMetadata.h"
22
23#include <drm/drm_fourcc.h>
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030024#include <ui/GraphicBufferMapper.h>
25#include <xf86drm.h>
26#include <xf86drmMode.h>
27
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020028#include <cinttypes>
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030029
Roman Stratiienkod21071f2021-03-09 21:56:50 +020030#include "utils/log.h"
31
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030032namespace android {
33
Andrew Wolferse5ca88b2024-12-03 16:31:05 +000034namespace {
35
36std::optional<std::pair<uint32_t, uint32_t>> GetAlignedDimensions(
37 const ui::PlaneLayout &layout) {
38 if (layout.sampleIncrementInBits == 0 || layout.strideInBytes == 0) {
39 ALOGW("Invalid plane layout");
40 return std::nullopt;
41 }
42
43 constexpr uint32_t kBitsPerByte = 8;
44 return std::pair{layout.strideInBytes * kBitsPerByte /
45 layout.sampleIncrementInBits,
46 layout.totalSizeInBytes / layout.strideInBytes};
47}
48
49} // namespace
50
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030051BufferInfoGetter *BufferInfoMapperMetadata::CreateInstance() {
52 if (GraphicBufferMapper::getInstance().getMapperVersion() <
53 GraphicBufferMapper::GRALLOC_4)
54 return nullptr;
55
56 return new BufferInfoMapperMetadata();
57}
58
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000059/* The implementation below makes assumptions on the order and number of file
60 * descriptors that Gralloc places in the native_handle_t and as such it very
61 * likely needs to be adapted to match the particular Gralloc implementation
62 * used in the system. For this reason it is been declared as a weak symbol,
63 * so that it can be overridden.
64 */
65int __attribute__((weak))
Roman Stratiienko1cbaaf92022-02-21 10:52:18 +020066BufferInfoMapperMetadata::GetFds(buffer_handle_t handle, BufferInfo *bo) {
Roman Stratiienko875f3972021-10-22 12:34:36 +030067 int fd_index = 0;
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000068
Roman Stratiienko875f3972021-10-22 12:34:36 +030069 if (handle->numFds <= 0) {
70 ALOGE("Handle has no fds");
71 return android::BAD_VALUE;
72 }
73
Roman Stratiienko1cbaaf92022-02-21 10:52:18 +020074 for (int i = 0; i < kBufferMaxPlanes; i++) {
Roman Stratiienko875f3972021-10-22 12:34:36 +030075 /* If no size, we're out of usable planes */
76 if (bo->sizes[i] <= 0) {
77 if (i == 0) {
78 ALOGE("Bad handle metadata");
79 return android::BAD_VALUE;
80 }
81 break;
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000082 }
83
Roman Stratiienko875f3972021-10-22 12:34:36 +030084 /*
85 * If the offset is zero, its multi-buffer
86 * so move to the next fd
87 */
88 if (i != 0 && bo->offsets[i] == 0) {
89 fd_index++;
90 if (fd_index >= handle->numFds) {
91 ALOGE("Handle has no more fds");
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000092 return android::BAD_VALUE;
93 }
94 }
Roman Stratiienko875f3972021-10-22 12:34:36 +030095
96 bo->prime_fds[i] = handle->data[fd_index];
97 if (bo->prime_fds[i] <= 0) {
98 ALOGE("Invalid prime fd");
99 return android::BAD_VALUE;
100 }
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000101 }
Roman Stratiienko875f3972021-10-22 12:34:36 +0300102
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000103 return 0;
104}
105
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200106auto BufferInfoMapperMetadata::GetBoInfo(buffer_handle_t handle)
107 -> std::optional<BufferInfo> {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000108 GraphicBufferMapper &mapper = GraphicBufferMapper::getInstance();
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200109 if (handle == nullptr)
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200110 return {};
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000111
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200112 BufferInfo bi{};
113
114 int err = mapper.getPixelFormatFourCC(handle, &bi.format);
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200115 if (err != 0) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000116 ALOGE("Failed to get FourCC format err=%d", err);
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200117 return {};
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000118 }
119
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200120 err = mapper.getPixelFormatModifier(handle, &bi.modifiers[0]);
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200121 if (err != 0) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000122 ALOGE("Failed to get DRM Modifier err=%d", err);
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200123 return {};
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000124 }
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000125
126 uint64_t width = 0;
127 err = mapper.getWidth(handle, &width);
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200128 if (err != 0) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000129 ALOGE("Failed to get Width err=%d", err);
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200130 return {};
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000131 }
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200132 bi.width = static_cast<uint32_t>(width);
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000133
134 uint64_t height = 0;
135 err = mapper.getHeight(handle, &height);
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200136 if (err != 0) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000137 ALOGE("Failed to get Height err=%d", err);
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200138 return {};
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000139 }
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200140 bi.height = static_cast<uint32_t>(height);
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000141
142 std::vector<ui::PlaneLayout> layouts;
143 err = mapper.getPlaneLayouts(handle, &layouts);
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200144 if (err != 0) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000145 ALOGE("Failed to get Plane Layouts err=%d", err);
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200146 return {};
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000147 }
148
149 for (uint32_t i = 0; i < layouts.size(); i++) {
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200150 bi.modifiers[i] = bi.modifiers[0];
151 bi.pitches[i] = layouts[i].strideInBytes;
152 bi.offsets[i] = layouts[i].offsetInBytes;
153 bi.sizes[i] = layouts[i].totalSizeInBytes;
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000154 }
155
Andrew Wolferse5ca88b2024-12-03 16:31:05 +0000156 uint64_t usage = 0;
157 err = mapper.getUsage(handle, &usage);
158 if (err != 0) {
159 ALOGE("Failed to get Usage err=%d", err);
160 return {};
161 }
162
163 if ((usage & GRALLOC_USAGE_CURSOR) != 0) {
164 if (layouts.size() > 1) {
165 ALOGW("Multiplanar format buffer alignment not supported");
166 } else {
167 auto aligned = GetAlignedDimensions(layouts[0]);
168 if (aligned.has_value()) {
169 bi.width = aligned->first;
170 bi.height = aligned->second;
171 }
172 }
173 }
174
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200175 err = GetFds(handle, &bi);
176 if (err != 0) {
177 ALOGE("Failed to get fds (err=%d)", err);
178 return {};
179 }
180
181 return bi;
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +0300182}
183
184} // namespace android
185
186#endif