blob: 516c7e17c48af7572d76819c7e6bdf1dda2a1129 [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 <log/log.h>
25#include <ui/GraphicBufferMapper.h>
26#include <xf86drm.h>
27#include <xf86drmMode.h>
28
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020029#include <cinttypes>
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030030
31namespace android {
32
33BufferInfoGetter *BufferInfoMapperMetadata::CreateInstance() {
34 if (GraphicBufferMapper::getInstance().getMapperVersion() <
35 GraphicBufferMapper::GRALLOC_4)
36 return nullptr;
37
38 return new BufferInfoMapperMetadata();
39}
40
Dennis Tsiang3a5fb502020-10-30 08:43:33 +000041/* The implementation below makes assumptions on the order and number of file
42 * descriptors that Gralloc places in the native_handle_t and as such it very
43 * likely needs to be adapted to match the particular Gralloc implementation
44 * used in the system. For this reason it is been declared as a weak symbol,
45 * so that it can be overridden.
46 */
47int __attribute__((weak))
48BufferInfoMapperMetadata::GetFds(buffer_handle_t handle, hwc_drm_bo_t *bo) {
49 int num_fds = handle->numFds;
50
51 if (num_fds >= 1 && num_fds <= 2) {
52 if (IsDrmFormatRgb(bo->format)) {
53 bo->prime_fds[0] = handle->data[0];
54 } else {
55 bo->prime_fds[0] = bo->prime_fds[1] = bo->prime_fds[2] = handle->data[0];
56 }
57 if (bo->prime_fds[0] <= 0) {
58 ALOGE("Encountered invalid fd %d", bo->prime_fds[0]);
59 return android::BAD_VALUE;
60 }
61
62 } else if (num_fds >= 3) {
63 bo->prime_fds[0] = handle->data[0];
64 bo->prime_fds[1] = handle->data[1];
65 bo->prime_fds[2] = handle->data[2];
66 for (int i = 0; i < 3; i++) {
67 if (bo->prime_fds[i] <= 0) {
68 ALOGE("Encountered invalid fd %d", bo->prime_fds[i]);
69 return android::BAD_VALUE;
70 }
71 }
72 }
73 return 0;
74}
75
76int BufferInfoMapperMetadata::ConvertBoInfo(buffer_handle_t handle,
77 hwc_drm_bo_t *bo) {
78 GraphicBufferMapper &mapper = GraphicBufferMapper::getInstance();
79 if (!handle)
80 return -EINVAL;
81
82 uint64_t usage = 0;
83 int err = mapper.getUsage(handle, &usage);
84 if (err) {
85 ALOGE("Failed to get usage err=%d", err);
86 return err;
87 }
88 bo->usage = static_cast<uint32_t>(usage);
89
90 ui::PixelFormat hal_format;
91 err = mapper.getPixelFormatRequested(handle, &hal_format);
92 if (err) {
93 ALOGE("Failed to get HAL Pixel Format err=%d", err);
94 return err;
95 }
96 bo->hal_format = static_cast<uint32_t>(hal_format);
97
98 err = mapper.getPixelFormatFourCC(handle, &bo->format);
99 if (err) {
100 ALOGE("Failed to get FourCC format err=%d", err);
101 return err;
102 }
103
104 err = mapper.getPixelFormatModifier(handle, &bo->modifiers[0]);
105 if (err) {
106 ALOGE("Failed to get DRM Modifier err=%d", err);
107 return err;
108 }
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000109
110 uint64_t width = 0;
111 err = mapper.getWidth(handle, &width);
112 if (err) {
113 ALOGE("Failed to get Width err=%d", err);
114 return err;
115 }
116 bo->width = static_cast<uint32_t>(width);
117
118 uint64_t height = 0;
119 err = mapper.getHeight(handle, &height);
120 if (err) {
121 ALOGE("Failed to get Height err=%d", err);
122 return err;
123 }
124 bo->height = static_cast<uint32_t>(height);
125
126 std::vector<ui::PlaneLayout> layouts;
127 err = mapper.getPlaneLayouts(handle, &layouts);
128 if (err) {
129 ALOGE("Failed to get Plane Layouts err=%d", err);
130 return err;
131 }
132
133 for (uint32_t i = 0; i < layouts.size(); i++) {
134 bo->modifiers[i] = bo->modifiers[0];
135 bo->pitches[i] = layouts[i].strideInBytes;
136 bo->offsets[i] = layouts[i].offsetInBytes;
137 }
138
139 return GetFds(handle, bo);
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +0300140}
141
142} // namespace android
143
144#endif