blob: 823d28a06fb0a22f02dc44f8ce5e3263b0e27771 [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
19#define LOG_TAG "hwc-bufferinfo-mappermetadata"
20
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
34BufferInfoGetter *BufferInfoMapperMetadata::CreateInstance() {
35 if (GraphicBufferMapper::getInstance().getMapperVersion() <
36 GraphicBufferMapper::GRALLOC_4)
37 return nullptr;
38
39 return new BufferInfoMapperMetadata();
40}
41
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000042/* The implementation below makes assumptions on the order and number of file
43 * descriptors that Gralloc places in the native_handle_t and as such it very
44 * likely needs to be adapted to match the particular Gralloc implementation
45 * used in the system. For this reason it is been declared as a weak symbol,
46 * so that it can be overridden.
47 */
48int __attribute__((weak))
Roman Stratiienko1cbaaf92022-02-21 10:52:18 +020049BufferInfoMapperMetadata::GetFds(buffer_handle_t handle, BufferInfo *bo) {
Roman Stratiienko875f3972021-10-22 12:34:36 +030050 int fd_index = 0;
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000051
Roman Stratiienko875f3972021-10-22 12:34:36 +030052 if (handle->numFds <= 0) {
53 ALOGE("Handle has no fds");
54 return android::BAD_VALUE;
55 }
56
Roman Stratiienko1cbaaf92022-02-21 10:52:18 +020057 for (int i = 0; i < kBufferMaxPlanes; i++) {
Roman Stratiienko875f3972021-10-22 12:34:36 +030058 /* If no size, we're out of usable planes */
59 if (bo->sizes[i] <= 0) {
60 if (i == 0) {
61 ALOGE("Bad handle metadata");
62 return android::BAD_VALUE;
63 }
64 break;
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000065 }
66
Roman Stratiienko875f3972021-10-22 12:34:36 +030067 /*
68 * If the offset is zero, its multi-buffer
69 * so move to the next fd
70 */
71 if (i != 0 && bo->offsets[i] == 0) {
72 fd_index++;
73 if (fd_index >= handle->numFds) {
74 ALOGE("Handle has no more fds");
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000075 return android::BAD_VALUE;
76 }
77 }
Roman Stratiienko875f3972021-10-22 12:34:36 +030078
79 bo->prime_fds[i] = handle->data[fd_index];
80 if (bo->prime_fds[i] <= 0) {
81 ALOGE("Invalid prime fd");
82 return android::BAD_VALUE;
83 }
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000084 }
Roman Stratiienko875f3972021-10-22 12:34:36 +030085
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000086 return 0;
87}
88
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +020089auto BufferInfoMapperMetadata::GetBoInfo(buffer_handle_t handle)
90 -> std::optional<BufferInfo> {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000091 GraphicBufferMapper &mapper = GraphicBufferMapper::getInstance();
Roman Stratiienkofc014f52021-12-23 19:04:29 +020092 if (handle == nullptr)
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +020093 return {};
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000094
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +020095 BufferInfo bi{};
96
97 int err = mapper.getPixelFormatFourCC(handle, &bi.format);
Roman Stratiienkofc014f52021-12-23 19:04:29 +020098 if (err != 0) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000099 ALOGE("Failed to get FourCC format err=%d", err);
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200100 return {};
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000101 }
102
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200103 err = mapper.getPixelFormatModifier(handle, &bi.modifiers[0]);
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200104 if (err != 0) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000105 ALOGE("Failed to get DRM Modifier err=%d", err);
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200106 return {};
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000107 }
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000108
109 uint64_t width = 0;
110 err = mapper.getWidth(handle, &width);
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200111 if (err != 0) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000112 ALOGE("Failed to get Width err=%d", err);
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200113 return {};
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000114 }
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200115 bi.width = static_cast<uint32_t>(width);
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000116
117 uint64_t height = 0;
118 err = mapper.getHeight(handle, &height);
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200119 if (err != 0) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000120 ALOGE("Failed to get Height err=%d", err);
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200121 return {};
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000122 }
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200123 bi.height = static_cast<uint32_t>(height);
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000124
125 std::vector<ui::PlaneLayout> layouts;
126 err = mapper.getPlaneLayouts(handle, &layouts);
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200127 if (err != 0) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000128 ALOGE("Failed to get Plane Layouts err=%d", err);
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200129 return {};
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000130 }
131
132 for (uint32_t i = 0; i < layouts.size(); i++) {
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200133 bi.modifiers[i] = bi.modifiers[0];
134 bi.pitches[i] = layouts[i].strideInBytes;
135 bi.offsets[i] = layouts[i].offsetInBytes;
136 bi.sizes[i] = layouts[i].totalSizeInBytes;
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000137 }
138
Roman Stratiienkoe9fbd8d2022-02-21 13:03:29 +0200139 err = GetFds(handle, &bi);
140 if (err != 0) {
141 ALOGE("Failed to get fds (err=%d)", err);
142 return {};
143 }
144
145 return bi;
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +0300146}
147
148} // namespace android
149
150#endif