blob: 70bd2dad9ee9504196c340f67f0a5a093a5f7ff2 [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#if PLATFORM_SDK_VERSION >= 30
18
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
89int BufferInfoMapperMetadata::ConvertBoInfo(buffer_handle_t handle,
Roman Stratiienko1cbaaf92022-02-21 10:52:18 +020090 BufferInfo *bo) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000091 GraphicBufferMapper &mapper = GraphicBufferMapper::getInstance();
Roman Stratiienkofc014f52021-12-23 19:04:29 +020092 if (handle == nullptr)
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000093 return -EINVAL;
94
Roman Stratiienko1cbaaf92022-02-21 10:52:18 +020095 int err = mapper.getPixelFormatFourCC(handle, &bo->format);
Roman Stratiienkofc014f52021-12-23 19:04:29 +020096 if (err != 0) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000097 ALOGE("Failed to get FourCC format err=%d", err);
98 return err;
99 }
100
101 err = mapper.getPixelFormatModifier(handle, &bo->modifiers[0]);
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200102 if (err != 0) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000103 ALOGE("Failed to get DRM Modifier err=%d", err);
104 return err;
105 }
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000106
107 uint64_t width = 0;
108 err = mapper.getWidth(handle, &width);
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200109 if (err != 0) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000110 ALOGE("Failed to get Width err=%d", err);
111 return err;
112 }
113 bo->width = static_cast<uint32_t>(width);
114
115 uint64_t height = 0;
116 err = mapper.getHeight(handle, &height);
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200117 if (err != 0) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000118 ALOGE("Failed to get Height err=%d", err);
119 return err;
120 }
121 bo->height = static_cast<uint32_t>(height);
122
123 std::vector<ui::PlaneLayout> layouts;
124 err = mapper.getPlaneLayouts(handle, &layouts);
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200125 if (err != 0) {
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000126 ALOGE("Failed to get Plane Layouts err=%d", err);
127 return err;
128 }
129
130 for (uint32_t i = 0; i < layouts.size(); i++) {
131 bo->modifiers[i] = bo->modifiers[0];
132 bo->pitches[i] = layouts[i].strideInBytes;
133 bo->offsets[i] = layouts[i].offsetInBytes;
Roman Stratiienko875f3972021-10-22 12:34:36 +0300134 bo->sizes[i] = layouts[i].totalSizeInBytes;
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000135 }
136
137 return GetFds(handle, bo);
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +0300138}
139
140} // namespace android
141
142#endif