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 #define LOG_TAG "hwc-bufferinfo-libdrm"
18
19 #include "BufferInfoLibdrm.h"
20
21 #include <gralloc_handle.h>
22 #include <hardware/gralloc.h>
23 #include <xf86drm.h>
24 #include <xf86drmMode.h>
25
26 #include <mutex>
27
28 #include "utils/log.h"
29 #include "utils/properties.h"
30
31 namespace android {
32
33 LEGACY_BUFFER_INFO_GETTER(BufferInfoLibdrm);
34
35 enum chroma_order {
36 kYCbCr,
37 kYCrCb,
38 };
39
40 struct DroidYuvFormat {
41 /* Lookup keys */
42 uint32_t native; /* HAL_PIXEL_FORMAT_ */
43 enum chroma_order chroma_order; /* chroma order is {Cb, Cr} or {Cr, Cb} */
44 size_t chroma_step; /* Distance in bytes between subsequent chroma pixels. */
45
46 /* Result */
47 int fourcc; /* DRM_FORMAT_ */
48 };
49
50 #ifndef DRM_FORMAT_XYUV8888
51 #define DRM_FORMAT_XYUV8888 \
52 fourcc_code('X', 'Y', 'U', 'V') /* [31:0] X:Y:Cb:Cr 8:8:8:8 little endian */
53 #endif
54
55 /* The following table is used to look up a DRI image FourCC based
56 * on native format and information contained in android_ycbcr struct. */
57 static const struct DroidYuvFormat kDroidYuvFormats[] = {
58 /* Native format, YCrCb, Chroma step, DRI image FourCC */
59 {HAL_PIXEL_FORMAT_YCbCr_420_888, kYCbCr, 2, DRM_FORMAT_NV12},
60 {HAL_PIXEL_FORMAT_YCbCr_420_888, kYCbCr, 1, DRM_FORMAT_YUV420},
61 {HAL_PIXEL_FORMAT_YCbCr_420_888, kYCrCb, 1, DRM_FORMAT_YVU420},
62 {HAL_PIXEL_FORMAT_YV12, kYCrCb, 1, DRM_FORMAT_YVU420},
63 /* HACK: See droid_create_image_from_prime_fds() and
64 * https://issuetracker.google.com/32077885. */
65 {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, kYCbCr, 2, DRM_FORMAT_NV12},
66 {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, kYCbCr, 1, DRM_FORMAT_YUV420},
67 {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, kYCrCb, 1, DRM_FORMAT_YVU420},
68 {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, kYCrCb, 1, DRM_FORMAT_AYUV},
69 {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, kYCrCb, 1, DRM_FORMAT_XYUV8888},
70 };
71
72 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
73
get_fourcc_yuv(uint32_t native,enum chroma_order chroma_order,size_t chroma_step)74 static uint32_t get_fourcc_yuv(uint32_t native, enum chroma_order chroma_order,
75 size_t chroma_step) {
76 for (auto droid_yuv_format : kDroidYuvFormats)
77 if (droid_yuv_format.native == native &&
78 droid_yuv_format.chroma_order == chroma_order &&
79 droid_yuv_format.chroma_step == chroma_step)
80 return droid_yuv_format.fourcc;
81
82 return UINT32_MAX;
83 }
84
is_yuv(uint32_t native)85 static bool is_yuv(uint32_t native) {
86 // NOLINTNEXTLINE(readability-use-anyofallof)
87 for (auto droid_yuv_format : kDroidYuvFormats)
88 if (droid_yuv_format.native == native)
89 return true;
90
91 return false;
92 }
93
GetYuvPlaneInfo(uint32_t hal_format,int num_fds,buffer_handle_t handle,BufferInfo * bo)94 bool BufferInfoLibdrm::GetYuvPlaneInfo(uint32_t hal_format, int num_fds,
95 buffer_handle_t handle, BufferInfo *bo) {
96 struct android_ycbcr ycbcr {};
97 enum chroma_order chroma_order {};
98 int ret = 0;
99
100 if (!gralloc_->lock_ycbcr) {
101 static std::once_flag once;
102 std::call_once(once,
103 []() { ALOGW("Gralloc does not support lock_ycbcr()"); });
104 return false;
105 }
106
107 memset(&ycbcr, 0, sizeof(ycbcr));
108 ret = gralloc_->lock_ycbcr(gralloc_, handle, 0, 0, 0, 0, 0, &ycbcr);
109 if (ret) {
110 ALOGW("gralloc->lock_ycbcr failed: %d", ret);
111 return false;
112 }
113 gralloc_->unlock(gralloc_, handle);
114
115 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
116 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
117 * so they can be interpreted as offsets. */
118 bo->offsets[0] = (size_t)ycbcr.y;
119 /* We assume here that all the planes are located in one DMA-buf. */
120 if ((size_t)ycbcr.cr < (size_t)ycbcr.cb) {
121 chroma_order = kYCrCb;
122 bo->offsets[1] = (size_t)ycbcr.cr;
123 bo->offsets[2] = (size_t)ycbcr.cb;
124 } else {
125 chroma_order = kYCbCr;
126 bo->offsets[1] = (size_t)ycbcr.cb;
127 bo->offsets[2] = (size_t)ycbcr.cr;
128 }
129
130 /* .ystride is the line length (in bytes) of the Y plane,
131 * .cstride is the line length (in bytes) of any of the remaining
132 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
133 * planar formats. */
134 bo->pitches[0] = ycbcr.ystride;
135 bo->pitches[1] = bo->pitches[2] = ycbcr.cstride;
136
137 /* .chroma_step is the byte distance between the same chroma channel
138 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
139 bo->format = get_fourcc_yuv(hal_format, chroma_order, ycbcr.chroma_step);
140 if (bo->format == UINT32_MAX) {
141 ALOGW(
142 "unsupported YUV format, native = %x, chroma_order = %s, chroma_step = "
143 "%d",
144 hal_format, chroma_order == kYCbCr ? "YCbCr" : "YCrCb",
145 (int)ycbcr.chroma_step);
146 return false;
147 }
148
149 /*
150 * Since this is EGL_NATIVE_BUFFER_ANDROID don't assume that
151 * the single-fd case cannot happen. So handle eithe single
152 * fd or fd-per-plane case:
153 */
154 if (num_fds == 1) {
155 bo->prime_fds[2] = bo->prime_fds[1] = bo->prime_fds[0];
156 } else {
157 int expected_planes = (ycbcr.chroma_step == 2) ? 2 : 3;
158 if (num_fds != expected_planes)
159 return false;
160 }
161
162 return true;
163 }
164
GetBoInfo(buffer_handle_t handle)165 auto BufferInfoLibdrm::GetBoInfo(buffer_handle_t handle)
166 -> std::optional<BufferInfo> {
167 gralloc_handle_t *gr_handle = gralloc_handle(handle);
168 if (!gr_handle)
169 return {};
170
171 BufferInfo bi{};
172
173 bi.width = gr_handle->width;
174 bi.height = gr_handle->height;
175
176 #if GRALLOC_HANDLE_VERSION < 4
177 static std::once_flag once;
178 std::call_once(once, []() {
179 ALOGE(
180 "libdrm < v2.4.97 has broken gralloc_handle structure. Please update.");
181 });
182 #endif
183 #if GRALLOC_HANDLE_VERSION == 4
184 bi.modifiers[0] = gr_handle->modifier;
185 #endif
186
187 bi.prime_fds[0] = gr_handle->prime_fd;
188
189 if (is_yuv(gr_handle->format)) {
190 if (!GetYuvPlaneInfo(gr_handle->format, handle->numFds, handle, &bi))
191 return {};
192 } else {
193 bi.pitches[0] = gr_handle->stride;
194 bi.offsets[0] = 0;
195
196 /* FOSS graphic components (gbm_gralloc, mesa3d) are translating
197 * HAL_PIXEL_FORMAT_RGB_565 to DRM_FORMAT_RGB565 without swapping
198 * the R and B components. Same must be done here. */
199 switch (gr_handle->format) {
200 case HAL_PIXEL_FORMAT_RGB_565:
201 bi.format = DRM_FORMAT_RGB565;
202 break;
203 default:
204 bi.format = ConvertHalFormatToDrm(gr_handle->format);
205 }
206
207 if (bi.format == DRM_FORMAT_INVALID)
208 return {};
209 }
210
211 return bi;
212 }
213
214 constexpr char gbm_gralloc_module_name[] = "GBM Memory Allocator";
215 constexpr char drm_gralloc_module_name[] = "DRM Memory Allocator";
216
ValidateGralloc()217 int BufferInfoLibdrm::ValidateGralloc() {
218 if (strcmp(gralloc_->common.name, drm_gralloc_module_name) != 0 &&
219 strcmp(gralloc_->common.name, gbm_gralloc_module_name) != 0) {
220 ALOGE(
221 "Gralloc name isn't valid: Expected: \"%s\" or \"%s\", Actual: \"%s\"",
222 gbm_gralloc_module_name, drm_gralloc_module_name,
223 gralloc_->common.name);
224 return -EINVAL;
225 }
226
227 return 0;
228 }
229
230 } // namespace android
231