2 * Copyright (C) 2020 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #if PLATFORM_SDK_VERSION >= 30
19 #define LOG_TAG "hwc-bufferinfo-mappermetadata"
21 #include "BufferInfoMapperMetadata.h"
23 #include <drm/drm_fourcc.h>
24 #include <ui/GraphicBufferMapper.h>
26 #include <xf86drmMode.h>
30 #include "utils/log.h"
34 BufferInfoGetter *BufferInfoMapperMetadata::CreateInstance() {
35 if (GraphicBufferMapper::getInstance().getMapperVersion() <
36 GraphicBufferMapper::GRALLOC_4)
39 return new BufferInfoMapperMetadata();
42 /* 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.
48 int __attribute__((weak))
49 BufferInfoMapperMetadata::GetFds(buffer_handle_t handle, hwc_drm_bo_t *bo) {
52 if (handle->numFds <= 0) {
53 ALOGE("Handle has no fds");
54 return android::BAD_VALUE;
57 for (int i = 0; i < kHwcDrmBoMaxPlanes; i++) {
58 /* If no size, we're out of usable planes */
59 if (bo->sizes[i] <= 0) {
61 ALOGE("Bad handle metadata");
62 return android::BAD_VALUE;
68 * If the offset is zero, its multi-buffer
69 * so move to the next fd
71 if (i != 0 && bo->offsets[i] == 0) {
73 if (fd_index >= handle->numFds) {
74 ALOGE("Handle has no more fds");
75 return android::BAD_VALUE;
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;
89 int BufferInfoMapperMetadata::ConvertBoInfo(buffer_handle_t handle,
91 GraphicBufferMapper &mapper = GraphicBufferMapper::getInstance();
92 if (handle == nullptr)
96 int err = mapper.getUsage(handle, &usage);
98 ALOGE("Failed to get usage err=%d", err);
101 bo->usage = static_cast<uint32_t>(usage);
103 ui::PixelFormat hal_format;
104 err = mapper.getPixelFormatRequested(handle, &hal_format);
106 ALOGE("Failed to get HAL Pixel Format err=%d", err);
109 bo->hal_format = static_cast<uint32_t>(hal_format);
111 err = mapper.getPixelFormatFourCC(handle, &bo->format);
113 ALOGE("Failed to get FourCC format err=%d", err);
117 err = mapper.getPixelFormatModifier(handle, &bo->modifiers[0]);
119 ALOGE("Failed to get DRM Modifier err=%d", err);
124 err = mapper.getWidth(handle, &width);
126 ALOGE("Failed to get Width err=%d", err);
129 bo->width = static_cast<uint32_t>(width);
132 err = mapper.getHeight(handle, &height);
134 ALOGE("Failed to get Height err=%d", err);
137 bo->height = static_cast<uint32_t>(height);
139 std::vector<ui::PlaneLayout> layouts;
140 err = mapper.getPlaneLayouts(handle, &layouts);
142 ALOGE("Failed to get Plane Layouts err=%d", err);
146 for (uint32_t i = 0; i < layouts.size(); i++) {
147 bo->modifiers[i] = bo->modifiers[0];
148 bo->pitches[i] = layouts[i].strideInBytes;
149 bo->offsets[i] = layouts[i].offsetInBytes;
150 bo->sizes[i] = layouts[i].totalSizeInBytes;
153 return GetFds(handle, bo);
156 } // namespace android