blob: 7d64ce89325bc06f262cff5656dfe61d7be31a02 [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>
24#include <inttypes.h>
25#include <log/log.h>
26#include <ui/GraphicBufferMapper.h>
27#include <xf86drm.h>
28#include <xf86drmMode.h>
29
30using android::hardware::graphics::common::V1_1::BufferUsage;
31
32namespace 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))
49BufferInfoMapperMetadata::GetFds(buffer_handle_t handle, hwc_drm_bo_t *bo) {
50 int num_fds = handle->numFds;
51
52 if (num_fds >= 1 && num_fds <= 2) {
53 if (IsDrmFormatRgb(bo->format)) {
54 bo->prime_fds[0] = handle->data[0];
55 } else {
56 bo->prime_fds[0] = bo->prime_fds[1] = bo->prime_fds[2] = handle->data[0];
57 }
58 if (bo->prime_fds[0] <= 0) {
59 ALOGE("Encountered invalid fd %d", bo->prime_fds[0]);
60 return android::BAD_VALUE;
61 }
62
63 } else if (num_fds >= 3) {
64 bo->prime_fds[0] = handle->data[0];
65 bo->prime_fds[1] = handle->data[1];
66 bo->prime_fds[2] = handle->data[2];
67 for (int i = 0; i < 3; i++) {
68 if (bo->prime_fds[i] <= 0) {
69 ALOGE("Encountered invalid fd %d", bo->prime_fds[i]);
70 return android::BAD_VALUE;
71 }
72 }
73 }
74 return 0;
75}
76
77int BufferInfoMapperMetadata::ConvertBoInfo(buffer_handle_t handle,
78 hwc_drm_bo_t *bo) {
79 GraphicBufferMapper &mapper = GraphicBufferMapper::getInstance();
80 if (!handle)
81 return -EINVAL;
82
83 uint64_t usage = 0;
84 int err = mapper.getUsage(handle, &usage);
85 if (err) {
86 ALOGE("Failed to get usage err=%d", err);
87 return err;
88 }
89 bo->usage = static_cast<uint32_t>(usage);
90
91 ui::PixelFormat hal_format;
92 err = mapper.getPixelFormatRequested(handle, &hal_format);
93 if (err) {
94 ALOGE("Failed to get HAL Pixel Format err=%d", err);
95 return err;
96 }
97 bo->hal_format = static_cast<uint32_t>(hal_format);
98
99 err = mapper.getPixelFormatFourCC(handle, &bo->format);
100 if (err) {
101 ALOGE("Failed to get FourCC format err=%d", err);
102 return err;
103 }
104
105 err = mapper.getPixelFormatModifier(handle, &bo->modifiers[0]);
106 if (err) {
107 ALOGE("Failed to get DRM Modifier err=%d", err);
108 return err;
109 }
Dennis Tsiang3a5fb502020-10-30 08:43:33 +0000110
111 uint64_t width = 0;
112 err = mapper.getWidth(handle, &width);
113 if (err) {
114 ALOGE("Failed to get Width err=%d", err);
115 return err;
116 }
117 bo->width = static_cast<uint32_t>(width);
118
119 uint64_t height = 0;
120 err = mapper.getHeight(handle, &height);
121 if (err) {
122 ALOGE("Failed to get Height err=%d", err);
123 return err;
124 }
125 bo->height = static_cast<uint32_t>(height);
126
127 std::vector<ui::PlaneLayout> layouts;
128 err = mapper.getPlaneLayouts(handle, &layouts);
129 if (err) {
130 ALOGE("Failed to get Plane Layouts err=%d", err);
131 return err;
132 }
133
134 for (uint32_t i = 0; i < layouts.size(); i++) {
135 bo->modifiers[i] = bo->modifiers[0];
136 bo->pitches[i] = layouts[i].strideInBytes;
137 bo->offsets[i] = layouts[i].offsetInBytes;
138 }
139
140 return GetFds(handle, bo);
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +0300141}
142
143} // namespace android
144
145#endif