xref: /aosp_15_r20/external/drm_hwcomposer/bufferinfo/BufferInfoMapperMetadata.cpp (revision 0a9764fe0a15e71ebbeb85e87e10990c23aab47f)
1 /*
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 __ANDROID_API__ >= 30
18 
19 #define LOG_TAG "drmhwc"
20 
21 #include "BufferInfoMapperMetadata.h"
22 
23 #include <drm/drm_fourcc.h>
24 #include <ui/GraphicBufferMapper.h>
25 #include <xf86drm.h>
26 #include <xf86drmMode.h>
27 
28 #include <cinttypes>
29 
30 #include "utils/log.h"
31 
32 namespace android {
33 
CreateInstance()34 BufferInfoGetter *BufferInfoMapperMetadata::CreateInstance() {
35   if (GraphicBufferMapper::getInstance().getMapperVersion() <
36       GraphicBufferMapper::GRALLOC_4)
37     return nullptr;
38 
39   return new BufferInfoMapperMetadata();
40 }
41 
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.
47  */
48 int __attribute__((weak))
GetFds(buffer_handle_t handle,BufferInfo * bo)49 BufferInfoMapperMetadata::GetFds(buffer_handle_t handle, BufferInfo *bo) {
50   int fd_index = 0;
51 
52   if (handle->numFds <= 0) {
53     ALOGE("Handle has no fds");
54     return android::BAD_VALUE;
55   }
56 
57   for (int i = 0; i < kBufferMaxPlanes; i++) {
58     /* 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;
65     }
66 
67     /*
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");
75         return android::BAD_VALUE;
76       }
77     }
78 
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     }
84   }
85 
86   return 0;
87 }
88 
GetBoInfo(buffer_handle_t handle)89 auto BufferInfoMapperMetadata::GetBoInfo(buffer_handle_t handle)
90     -> std::optional<BufferInfo> {
91   GraphicBufferMapper &mapper = GraphicBufferMapper::getInstance();
92   if (handle == nullptr)
93     return {};
94 
95   BufferInfo bi{};
96 
97   int err = mapper.getPixelFormatFourCC(handle, &bi.format);
98   if (err != 0) {
99     ALOGE("Failed to get FourCC format err=%d", err);
100     return {};
101   }
102 
103   err = mapper.getPixelFormatModifier(handle, &bi.modifiers[0]);
104   if (err != 0) {
105     ALOGE("Failed to get DRM Modifier err=%d", err);
106     return {};
107   }
108 
109   uint64_t width = 0;
110   err = mapper.getWidth(handle, &width);
111   if (err != 0) {
112     ALOGE("Failed to get Width err=%d", err);
113     return {};
114   }
115   bi.width = static_cast<uint32_t>(width);
116 
117   uint64_t height = 0;
118   err = mapper.getHeight(handle, &height);
119   if (err != 0) {
120     ALOGE("Failed to get Height err=%d", err);
121     return {};
122   }
123   bi.height = static_cast<uint32_t>(height);
124 
125   std::vector<ui::PlaneLayout> layouts;
126   err = mapper.getPlaneLayouts(handle, &layouts);
127   if (err != 0) {
128     ALOGE("Failed to get Plane Layouts err=%d", err);
129     return {};
130   }
131 
132   for (uint32_t i = 0; i < layouts.size(); i++) {
133     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;
137   }
138 
139   err = GetFds(handle, &bi);
140   if (err != 0) {
141     ALOGE("Failed to get fds (err=%d)", err);
142     return {};
143   }
144 
145   return bi;
146 }
147 
148 }  // namespace android
149 
150 #endif
151