1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright © 2021, Google Inc.
5  * SPDX-License-Identifier: MIT
6  */
7 
8 #include "u_gralloc_internal.h"
9 
10 #include <hardware/gralloc.h>
11 
12 #include "drm-uapi/drm_fourcc.h"
13 #include "util/log.h"
14 #include "util/macros.h"
15 #include "util/u_memory.h"
16 
17 #include <dlfcn.h>
18 #include <errno.h>
19 #include <string.h>
20 
21 struct fallback_gralloc {
22    struct u_gralloc base;
23    gralloc_module_t *gralloc_module;
24 };
25 
26 enum chroma_order {
27    YCbCr,
28    YCrCb,
29 };
30 
31 struct droid_yuv_format {
32    /* Lookup keys */
33    int native;                     /* HAL_PIXEL_FORMAT_ */
34    enum chroma_order chroma_order; /* chroma order is {Cb, Cr} or {Cr, Cb} */
35    int chroma_step; /* Distance in bytes between subsequent chroma pixels. */
36 
37    /* Result */
38    int fourcc; /* DRM_FORMAT_ */
39 };
40 
41 /* The following table is used to look up a DRI image FourCC based
42  * on native format and information contained in android_ycbcr struct. */
43 static const struct droid_yuv_format droid_yuv_formats[] = {
44    /* Native format, YCrCb, Chroma step, DRI image FourCC */
45    {HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 2, DRM_FORMAT_NV12},
46    {HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 1, DRM_FORMAT_YUV420},
47    {HAL_PIXEL_FORMAT_YCbCr_420_888, YCrCb, 1, DRM_FORMAT_YVU420},
48    {HAL_PIXEL_FORMAT_YV12, YCrCb, 1, DRM_FORMAT_YVU420},
49    /* HACK: See droid_create_image_from_prime_fds() and
50     * https://issuetracker.google.com/32077885. */
51    {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 2, DRM_FORMAT_NV12},
52    {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 1, DRM_FORMAT_YUV420},
53    {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_YVU420},
54    {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_AYUV},
55    {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_XYUV8888},
56 };
57 
58 static int
get_fourcc_yuv(int native,enum chroma_order chroma_order,int chroma_step)59 get_fourcc_yuv(int native, enum chroma_order chroma_order, int chroma_step)
60 {
61    for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
62       if (droid_yuv_formats[i].native == native &&
63           droid_yuv_formats[i].chroma_order == chroma_order &&
64           droid_yuv_formats[i].chroma_step == chroma_step)
65          return droid_yuv_formats[i].fourcc;
66 
67    return -1;
68 }
69 
70 static bool
is_yuv(int native)71 is_yuv(int native)
72 {
73    for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
74       if (droid_yuv_formats[i].native == native)
75          return true;
76 
77    return false;
78 }
79 
80 static int
get_format_bpp(int native)81 get_format_bpp(int native)
82 {
83    int bpp;
84 
85    switch (native) {
86    case HAL_PIXEL_FORMAT_RGBA_FP16:
87       bpp = 8;
88       break;
89    case HAL_PIXEL_FORMAT_RGBA_8888:
90    case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
91       /*
92        * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
93        * TODO: Remove this once https://issuetracker.google.com/32077885 is
94        * fixed.
95        */
96    case HAL_PIXEL_FORMAT_RGBX_8888:
97    case HAL_PIXEL_FORMAT_BGRA_8888:
98    case HAL_PIXEL_FORMAT_RGBA_1010102:
99       bpp = 4;
100       break;
101    case HAL_PIXEL_FORMAT_RGB_565:
102       bpp = 2;
103       break;
104    default:
105       bpp = 0;
106       break;
107    }
108 
109    return bpp;
110 }
111 
112 /* createImageFromFds requires fourcc format */
113 static int
get_fourcc(int native)114 get_fourcc(int native)
115 {
116    switch (native) {
117    case HAL_PIXEL_FORMAT_RGB_565:
118       return DRM_FORMAT_RGB565;
119    case HAL_PIXEL_FORMAT_BGRA_8888:
120       return DRM_FORMAT_ARGB8888;
121    case HAL_PIXEL_FORMAT_RGBA_8888:
122       return DRM_FORMAT_ABGR8888;
123    case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
124       /*
125        * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
126        * TODO: Remove this once https://issuetracker.google.com/32077885 is
127        * fixed.
128        */
129    case HAL_PIXEL_FORMAT_RGBX_8888:
130       return DRM_FORMAT_XBGR8888;
131    case HAL_PIXEL_FORMAT_RGBA_FP16:
132       return DRM_FORMAT_ABGR16161616F;
133    case HAL_PIXEL_FORMAT_RGBA_1010102:
134       return DRM_FORMAT_ABGR2101010;
135    default:
136       mesa_logw("unsupported native buffer format 0x%x", native);
137    }
138    return -1;
139 }
140 
141 /* returns # of fds, and by reference the actual fds */
142 static unsigned
get_native_buffer_fds(const native_handle_t * handle,int fds[3])143 get_native_buffer_fds(const native_handle_t *handle, int fds[3])
144 {
145    if (!handle)
146       return 0;
147 
148    /*
149     * Various gralloc implementations exist, but the dma-buf fd tends
150     * to be first. Access it directly to avoid a dependency on specific
151     * gralloc versions.
152     */
153    for (int i = 0; i < handle->numFds; i++)
154       fds[i] = handle->data[i];
155 
156    return handle->numFds;
157 }
158 
159 static int
fallback_gralloc_get_yuv_info(struct u_gralloc * gralloc,struct u_gralloc_buffer_handle * hnd,struct u_gralloc_buffer_basic_info * out)160 fallback_gralloc_get_yuv_info(struct u_gralloc *gralloc,
161                               struct u_gralloc_buffer_handle *hnd,
162                               struct u_gralloc_buffer_basic_info *out)
163 {
164    struct fallback_gralloc *gr = (struct fallback_gralloc *)gralloc;
165    gralloc_module_t *gr_mod = gr->gralloc_module;
166    enum chroma_order chroma_order;
167    struct android_ycbcr ycbcr;
168    int drm_fourcc = 0;
169    int num_fds = 0;
170    int fds[3];
171    int ret;
172 
173    num_fds = get_native_buffer_fds(hnd->handle, fds);
174    if (num_fds == 0)
175       return -EINVAL;
176 
177    if (!gr_mod || !gr_mod->lock_ycbcr) {
178       return -EINVAL;
179    }
180 
181    memset(&ycbcr, 0, sizeof(ycbcr));
182    ret = gr_mod->lock_ycbcr(gr_mod, hnd->handle, 0, 0, 0, 0, 0, &ycbcr);
183    if (ret) {
184       /* HACK: See native_window_buffer_get_buffer_info() and
185        * https://issuetracker.google.com/32077885.*/
186       if (hnd->hal_format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)
187          return -EAGAIN;
188 
189       mesa_logw("gralloc->lock_ycbcr failed: %d", ret);
190       return -EINVAL;
191    }
192    gr_mod->unlock(gr_mod, hnd->handle);
193 
194    chroma_order = ((size_t)ycbcr.cr < (size_t)ycbcr.cb) ? YCrCb : YCbCr;
195 
196    /* .chroma_step is the byte distance between the same chroma channel
197     * values of subsequent pixels, assumed to be the same for Cb and Cr. */
198    drm_fourcc =
199       get_fourcc_yuv(hnd->hal_format, chroma_order, ycbcr.chroma_step);
200    if (drm_fourcc == -1) {
201       mesa_logw("unsupported YUV format, native = %x, chroma_order = %s, "
202                 "chroma_step = %zu",
203                 hnd->hal_format, chroma_order == YCbCr ? "YCbCr" : "YCrCb",
204                 ycbcr.chroma_step);
205       return -EINVAL;
206    }
207 
208    out->drm_fourcc = drm_fourcc;
209    out->modifier = DRM_FORMAT_MOD_INVALID;
210 
211    out->num_planes = ycbcr.chroma_step == 2 ? 2 : 3;
212    /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
213     * it will return the .y/.cb/.cr pointers based on a NULL pointer,
214     * so they can be interpreted as offsets. */
215    out->offsets[0] = (size_t)ycbcr.y;
216    /* We assume here that all the planes are located in one DMA-buf. */
217    if (chroma_order == YCrCb) {
218       out->offsets[1] = (size_t)ycbcr.cr;
219       out->offsets[2] = (size_t)ycbcr.cb;
220    } else {
221       out->offsets[1] = (size_t)ycbcr.cb;
222       out->offsets[2] = (size_t)ycbcr.cr;
223    }
224 
225    /* .ystride is the line length (in bytes) of the Y plane,
226     * .cstride is the line length (in bytes) of any of the remaining
227     * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
228     * planar formats. */
229    out->strides[0] = ycbcr.ystride;
230    out->strides[1] = out->strides[2] = ycbcr.cstride;
231 
232    /*
233     * Since this is EGL_NATIVE_BUFFER_ANDROID don't assume that
234     * the single-fd case cannot happen.  So handle eithe single
235     * fd or fd-per-plane case:
236     */
237    if (num_fds == 1) {
238       out->fds[1] = out->fds[0] = fds[0];
239       if (out->num_planes == 3)
240          out->fds[2] = fds[0];
241    } else {
242       assert(num_fds == out->num_planes);
243       out->fds[0] = fds[0];
244       out->fds[1] = fds[1];
245       out->fds[2] = fds[2];
246    }
247 
248    return 0;
249 }
250 
251 static int
fallback_gralloc_get_buffer_info(struct u_gralloc * gralloc,struct u_gralloc_buffer_handle * hnd,struct u_gralloc_buffer_basic_info * out)252 fallback_gralloc_get_buffer_info(struct u_gralloc *gralloc,
253                                  struct u_gralloc_buffer_handle *hnd,
254                                  struct u_gralloc_buffer_basic_info *out)
255 {
256    int num_planes = 0;
257    int drm_fourcc = 0;
258    int stride = 0;
259    int fds[3];
260 
261    if (is_yuv(hnd->hal_format)) {
262       int ret = fallback_gralloc_get_yuv_info(gralloc, hnd, out);
263       /*
264        * HACK: https://issuetracker.google.com/32077885
265        * There is no API available to properly query the
266        * IMPLEMENTATION_DEFINED format. As a workaround we rely here on
267        * gralloc allocating either an arbitrary YCbCr 4:2:0 or RGBX_8888, with
268        * the latter being recognized by lock_ycbcr failing.
269        */
270       if (ret != -EAGAIN)
271          return ret;
272    }
273 
274    /*
275     * Non-YUV formats could *also* have multiple planes, such as ancillary
276     * color compression state buffer, but the rest of the code isn't ready
277     * yet to deal with modifiers:
278     */
279    num_planes = get_native_buffer_fds(hnd->handle, fds);
280    if (num_planes == 0)
281       return -EINVAL;
282 
283    assert(num_planes == 1);
284 
285    drm_fourcc = get_fourcc(hnd->hal_format);
286    if (drm_fourcc == -1) {
287       mesa_loge("Failed to get drm_fourcc");
288       return -EINVAL;
289    }
290 
291    stride = hnd->pixel_stride * get_format_bpp(hnd->hal_format);
292    if (stride == 0) {
293       mesa_loge("Failed to calcuulate stride");
294       return -EINVAL;
295    }
296 
297    out->drm_fourcc = drm_fourcc;
298    out->modifier = DRM_FORMAT_MOD_INVALID;
299    out->num_planes = num_planes;
300    out->fds[0] = fds[0];
301    out->strides[0] = stride;
302 
303    return 0;
304 }
305 
306 static int
destroy(struct u_gralloc * gralloc)307 destroy(struct u_gralloc *gralloc)
308 {
309    struct fallback_gralloc *gr = (struct fallback_gralloc *)gralloc;
310    if (gr->gralloc_module) {
311       dlclose(gr->gralloc_module->common.dso);
312    }
313 
314    FREE(gr);
315 
316    return 0;
317 }
318 
319 struct u_gralloc *
u_gralloc_fallback_create()320 u_gralloc_fallback_create()
321 {
322    struct fallback_gralloc *gr = CALLOC_STRUCT(fallback_gralloc);
323    int err = 0;
324 
325    err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
326                        (const hw_module_t **)&gr->gralloc_module);
327 
328    if (err) {
329       mesa_logw(
330          "No gralloc hwmodule detected (video buffers won't be supported)");
331    } else if (!gr->gralloc_module->lock_ycbcr) {
332       mesa_logw("Gralloc doesn't support lock_ycbcr (video buffers won't be "
333                 "supported)");
334    }
335 
336    gr->base.ops.get_buffer_basic_info = fallback_gralloc_get_buffer_info;
337    gr->base.ops.destroy = destroy;
338 
339    mesa_logi("Using fallback gralloc implementation");
340 
341    return &gr->base;
342 }
343