6ed0b416cf81266d79668f51ee9c6792e2190dac
[mesa.git] / src / egl / drivers / dri2 / platform_android.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
5 * Copyright (C) 2010-2011 LunarG Inc.
6 *
7 * Based on platform_x11, which has
8 *
9 * Copyright © 2011 Intel Corporation
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 */
29
30 #include <cutils/properties.h>
31 #include <errno.h>
32 #include <dirent.h>
33 #include <dlfcn.h>
34 #include <fcntl.h>
35 #include <xf86drm.h>
36 #include <stdbool.h>
37 #include <stdio.h>
38 #include <sync/sync.h>
39 #include <sys/types.h>
40 #include <drm-uapi/drm_fourcc.h>
41
42 #include "loader.h"
43 #include "egl_dri2.h"
44 #include "egl_dri2_fallbacks.h"
45
46 #ifdef HAVE_DRM_GRALLOC
47 #include <gralloc_drm_handle.h>
48 #include "gralloc_drm.h"
49 #endif /* HAVE_DRM_GRALLOC */
50
51 #define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1))
52
53 enum chroma_order {
54 YCbCr,
55 YCrCb,
56 };
57
58 struct droid_yuv_format {
59 /* Lookup keys */
60 int native; /* HAL_PIXEL_FORMAT_ */
61 enum chroma_order chroma_order; /* chroma order is {Cb, Cr} or {Cr, Cb} */
62 int chroma_step; /* Distance in bytes between subsequent chroma pixels. */
63
64 /* Result */
65 int fourcc; /* DRM_FORMAT_ */
66 };
67
68 /* The following table is used to look up a DRI image FourCC based
69 * on native format and information contained in android_ycbcr struct. */
70 static const struct droid_yuv_format droid_yuv_formats[] = {
71 /* Native format, YCrCb, Chroma step, DRI image FourCC */
72 { HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 2, DRM_FORMAT_NV12 },
73 { HAL_PIXEL_FORMAT_YCbCr_420_888, YCbCr, 1, DRM_FORMAT_YUV420 },
74 { HAL_PIXEL_FORMAT_YCbCr_420_888, YCrCb, 1, DRM_FORMAT_YVU420 },
75 { HAL_PIXEL_FORMAT_YV12, YCrCb, 1, DRM_FORMAT_YVU420 },
76 /* HACK: See droid_create_image_from_prime_fds() and
77 * https://issuetracker.google.com/32077885. */
78 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 2, DRM_FORMAT_NV12 },
79 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCbCr, 1, DRM_FORMAT_YUV420 },
80 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_YVU420 },
81 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_AYUV },
82 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, YCrCb, 1, DRM_FORMAT_XYUV8888 },
83 };
84
85 static int
86 get_fourcc_yuv(int native, enum chroma_order chroma_order, int chroma_step)
87 {
88 for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
89 if (droid_yuv_formats[i].native == native &&
90 droid_yuv_formats[i].chroma_order == chroma_order &&
91 droid_yuv_formats[i].chroma_step == chroma_step)
92 return droid_yuv_formats[i].fourcc;
93
94 return -1;
95 }
96
97 static bool
98 is_yuv(int native)
99 {
100 for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
101 if (droid_yuv_formats[i].native == native)
102 return true;
103
104 return false;
105 }
106
107 static int
108 get_format_bpp(int native)
109 {
110 int bpp;
111
112 switch (native) {
113 case HAL_PIXEL_FORMAT_RGBA_FP16:
114 bpp = 8;
115 break;
116 case HAL_PIXEL_FORMAT_RGBA_8888:
117 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
118 /*
119 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
120 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
121 */
122 case HAL_PIXEL_FORMAT_RGBX_8888:
123 case HAL_PIXEL_FORMAT_BGRA_8888:
124 case HAL_PIXEL_FORMAT_RGBA_1010102:
125 bpp = 4;
126 break;
127 case HAL_PIXEL_FORMAT_RGB_565:
128 bpp = 2;
129 break;
130 default:
131 bpp = 0;
132 break;
133 }
134
135 return bpp;
136 }
137
138 /* createImageFromFds requires fourcc format */
139 static int get_fourcc(int native)
140 {
141 switch (native) {
142 case HAL_PIXEL_FORMAT_RGB_565: return DRM_FORMAT_RGB565;
143 case HAL_PIXEL_FORMAT_BGRA_8888: return DRM_FORMAT_ARGB8888;
144 case HAL_PIXEL_FORMAT_RGBA_8888: return DRM_FORMAT_ABGR8888;
145 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
146 /*
147 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
148 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
149 */
150 case HAL_PIXEL_FORMAT_RGBX_8888: return DRM_FORMAT_XBGR8888;
151 case HAL_PIXEL_FORMAT_RGBA_FP16: return DRM_FORMAT_ABGR16161616F;
152 case HAL_PIXEL_FORMAT_RGBA_1010102: return DRM_FORMAT_ABGR2101010;
153 default:
154 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", native);
155 }
156 return -1;
157 }
158
159 static int get_format(int format)
160 {
161 switch (format) {
162 case HAL_PIXEL_FORMAT_BGRA_8888: return __DRI_IMAGE_FORMAT_ARGB8888;
163 case HAL_PIXEL_FORMAT_RGB_565: return __DRI_IMAGE_FORMAT_RGB565;
164 case HAL_PIXEL_FORMAT_RGBA_8888: return __DRI_IMAGE_FORMAT_ABGR8888;
165 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
166 /*
167 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
168 * TODO: Revert this once https://issuetracker.google.com/32077885 is fixed.
169 */
170 case HAL_PIXEL_FORMAT_RGBX_8888: return __DRI_IMAGE_FORMAT_XBGR8888;
171 case HAL_PIXEL_FORMAT_RGBA_FP16: return __DRI_IMAGE_FORMAT_ABGR16161616F;
172 case HAL_PIXEL_FORMAT_RGBA_1010102: return __DRI_IMAGE_FORMAT_ABGR2101010;
173 default:
174 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", format);
175 }
176 return -1;
177 }
178
179 /* returns # of fds, and by reference the actual fds */
180 static unsigned
181 get_native_buffer_fds(struct ANativeWindowBuffer *buf, int fds[3])
182 {
183 native_handle_t *handle = (native_handle_t *)buf->handle;
184
185 if (!handle)
186 return 0;
187
188 /*
189 * Various gralloc implementations exist, but the dma-buf fd tends
190 * to be first. Access it directly to avoid a dependency on specific
191 * gralloc versions.
192 */
193 for (int i = 0; i < handle->numFds; i++)
194 fds[i] = handle->data[i];
195
196 return handle->numFds;
197 }
198
199 #ifdef HAVE_DRM_GRALLOC
200 static int
201 get_native_buffer_name(struct ANativeWindowBuffer *buf)
202 {
203 return gralloc_drm_get_gem_handle(buf->handle);
204 }
205 #endif /* HAVE_DRM_GRALLOC */
206
207 static EGLBoolean
208 droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf)
209 {
210 int fence_fd;
211
212 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer,
213 &fence_fd))
214 return EGL_FALSE;
215
216 /* If access to the buffer is controlled by a sync fence, then block on the
217 * fence.
218 *
219 * It may be more performant to postpone blocking until there is an
220 * immediate need to write to the buffer. But doing so would require adding
221 * hooks to the DRI2 loader.
222 *
223 * From the ANativeWindow::dequeueBuffer documentation:
224 *
225 * The libsync fence file descriptor returned in the int pointed to by
226 * the fenceFd argument will refer to the fence that must signal
227 * before the dequeued buffer may be written to. A value of -1
228 * indicates that the caller may access the buffer immediately without
229 * waiting on a fence. If a valid file descriptor is returned (i.e.
230 * any value except -1) then the caller is responsible for closing the
231 * file descriptor.
232 */
233 if (fence_fd >= 0) {
234 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
235 *
236 * Waits indefinitely if timeout < 0.
237 */
238 int timeout = -1;
239 sync_wait(fence_fd, timeout);
240 close(fence_fd);
241 }
242
243 /* Record all the buffers created by ANativeWindow and update back buffer
244 * for updating buffer's age in swap_buffers.
245 */
246 EGLBoolean updated = EGL_FALSE;
247 for (int i = 0; i < dri2_surf->color_buffers_count; i++) {
248 if (!dri2_surf->color_buffers[i].buffer) {
249 dri2_surf->color_buffers[i].buffer = dri2_surf->buffer;
250 }
251 if (dri2_surf->color_buffers[i].buffer == dri2_surf->buffer) {
252 dri2_surf->back = &dri2_surf->color_buffers[i];
253 updated = EGL_TRUE;
254 break;
255 }
256 }
257
258 if (!updated) {
259 /* In case of all the buffers were recreated by ANativeWindow, reset
260 * the color_buffers
261 */
262 for (int i = 0; i < dri2_surf->color_buffers_count; i++) {
263 dri2_surf->color_buffers[i].buffer = NULL;
264 dri2_surf->color_buffers[i].age = 0;
265 }
266 dri2_surf->color_buffers[0].buffer = dri2_surf->buffer;
267 dri2_surf->back = &dri2_surf->color_buffers[0];
268 }
269
270 return EGL_TRUE;
271 }
272
273 static EGLBoolean
274 droid_window_enqueue_buffer(_EGLDisplay *disp, struct dri2_egl_surface *dri2_surf)
275 {
276 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
277
278 /* To avoid blocking other EGL calls, release the display mutex before
279 * we enter droid_window_enqueue_buffer() and re-acquire the mutex upon
280 * return.
281 */
282 mtx_unlock(&disp->Mutex);
283
284 /* Queue the buffer with stored out fence fd. The ANativeWindow or buffer
285 * consumer may choose to wait for the fence to signal before accessing
286 * it. If fence fd value is -1, buffer can be accessed by consumer
287 * immediately. Consumer or application shouldn't rely on timestamp
288 * associated with fence if the fence fd is -1.
289 *
290 * Ownership of fd is transferred to consumer after queueBuffer and the
291 * consumer is responsible for closing it. Caller must not use the fd
292 * after passing it to queueBuffer.
293 */
294 int fence_fd = dri2_surf->out_fence_fd;
295 dri2_surf->out_fence_fd = -1;
296 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer,
297 fence_fd);
298
299 dri2_surf->buffer = NULL;
300 dri2_surf->back = NULL;
301
302 mtx_lock(&disp->Mutex);
303
304 if (dri2_surf->dri_image_back) {
305 dri2_dpy->image->destroyImage(dri2_surf->dri_image_back);
306 dri2_surf->dri_image_back = NULL;
307 }
308
309 return EGL_TRUE;
310 }
311
312 static void
313 droid_window_cancel_buffer(struct dri2_egl_surface *dri2_surf)
314 {
315 int ret;
316 int fence_fd = dri2_surf->out_fence_fd;
317
318 dri2_surf->out_fence_fd = -1;
319 ret = dri2_surf->window->cancelBuffer(dri2_surf->window,
320 dri2_surf->buffer, fence_fd);
321 dri2_surf->buffer = NULL;
322 if (ret < 0) {
323 _eglLog(_EGL_WARNING, "ANativeWindow::cancelBuffer failed");
324 dri2_surf->base.Lost = EGL_TRUE;
325 }
326 }
327
328 static bool
329 droid_set_shared_buffer_mode(_EGLDisplay *disp, _EGLSurface *surf, bool mode)
330 {
331 #if ANDROID_API_LEVEL >= 24
332 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
333 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
334 struct ANativeWindow *window = dri2_surf->window;
335
336 assert(surf->Type == EGL_WINDOW_BIT);
337 assert(_eglSurfaceHasMutableRenderBuffer(&dri2_surf->base));
338
339 _eglLog(_EGL_DEBUG, "%s: mode=%d", __func__, mode);
340
341 if (native_window_set_shared_buffer_mode(window, mode)) {
342 _eglLog(_EGL_WARNING, "failed native_window_set_shared_buffer_mode"
343 "(window=%p, mode=%d)", window, mode);
344 return false;
345 }
346
347 return true;
348 #else
349 _eglLog(_EGL_FATAL, "%s:%d: internal error: unreachable", __FILE__, __LINE__);
350 return false;
351 #endif
352 }
353
354 static _EGLSurface *
355 droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
356 _EGLConfig *conf, void *native_window,
357 const EGLint *attrib_list)
358 {
359 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
360 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
361 struct dri2_egl_surface *dri2_surf;
362 struct ANativeWindow *window = native_window;
363 const __DRIconfig *config;
364
365 dri2_surf = calloc(1, sizeof *dri2_surf);
366 if (!dri2_surf) {
367 _eglError(EGL_BAD_ALLOC, "droid_create_surface");
368 return NULL;
369 }
370
371 if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
372 true, native_window))
373 goto cleanup_surface;
374
375 if (type == EGL_WINDOW_BIT) {
376 int format;
377 int buffer_count;
378
379 if (window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
380 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
381 goto cleanup_surface;
382 }
383 if (window->query(window, NATIVE_WINDOW_FORMAT, &format)) {
384 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
385 goto cleanup_surface;
386 }
387
388 /* Query ANativeWindow for MIN_UNDEQUEUED_BUFFER, set buffer count
389 * and allocate color_buffers.
390 */
391 if (window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
392 &buffer_count)) {
393 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
394 goto cleanup_surface;
395 }
396 if (native_window_set_buffer_count(window, buffer_count+1)) {
397 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
398 goto cleanup_surface;
399 }
400 dri2_surf->color_buffers = calloc(buffer_count+1,
401 sizeof(*dri2_surf->color_buffers));
402 if (!dri2_surf->color_buffers) {
403 _eglError(EGL_BAD_ALLOC, "droid_create_surface");
404 goto cleanup_surface;
405 }
406 dri2_surf->color_buffers_count = buffer_count+1;
407
408 if (format != dri2_conf->base.NativeVisualID) {
409 _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x",
410 format, dri2_conf->base.NativeVisualID);
411 }
412
413 window->query(window, NATIVE_WINDOW_WIDTH, &dri2_surf->base.Width);
414 window->query(window, NATIVE_WINDOW_HEIGHT, &dri2_surf->base.Height);
415 }
416
417 config = dri2_get_dri_config(dri2_conf, type,
418 dri2_surf->base.GLColorspace);
419 if (!config) {
420 _eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration");
421 goto cleanup_surface;
422 }
423
424 if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
425 goto cleanup_surface;
426
427 if (window) {
428 window->common.incRef(&window->common);
429 dri2_surf->window = window;
430 }
431
432 return &dri2_surf->base;
433
434 cleanup_surface:
435 if (dri2_surf->color_buffers_count)
436 free(dri2_surf->color_buffers);
437 free(dri2_surf);
438
439 return NULL;
440 }
441
442 static _EGLSurface *
443 droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
444 _EGLConfig *conf, void *native_window,
445 const EGLint *attrib_list)
446 {
447 return droid_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
448 native_window, attrib_list);
449 }
450
451 static _EGLSurface *
452 droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
453 _EGLConfig *conf, const EGLint *attrib_list)
454 {
455 return droid_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
456 NULL, attrib_list);
457 }
458
459 static EGLBoolean
460 droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
461 {
462 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
463 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
464
465 dri2_egl_surface_free_local_buffers(dri2_surf);
466
467 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
468 if (dri2_surf->buffer)
469 droid_window_cancel_buffer(dri2_surf);
470
471 dri2_surf->window->common.decRef(&dri2_surf->window->common);
472 }
473
474 if (dri2_surf->dri_image_back) {
475 _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_back", __func__, __LINE__);
476 dri2_dpy->image->destroyImage(dri2_surf->dri_image_back);
477 dri2_surf->dri_image_back = NULL;
478 }
479
480 if (dri2_surf->dri_image_front) {
481 _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_front", __func__, __LINE__);
482 dri2_dpy->image->destroyImage(dri2_surf->dri_image_front);
483 dri2_surf->dri_image_front = NULL;
484 }
485
486 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
487
488 dri2_fini_surface(surf);
489 free(dri2_surf->color_buffers);
490 free(dri2_surf);
491
492 return EGL_TRUE;
493 }
494
495 static EGLBoolean
496 droid_swap_interval(_EGLDriver *drv, _EGLDisplay *disp,
497 _EGLSurface *surf, EGLint interval)
498 {
499 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
500 struct ANativeWindow *window = dri2_surf->window;
501
502 if (window->setSwapInterval(window, interval))
503 return EGL_FALSE;
504
505 surf->SwapInterval = interval;
506 return EGL_TRUE;
507 }
508
509 static int
510 update_buffers(struct dri2_egl_surface *dri2_surf)
511 {
512 if (dri2_surf->base.Lost)
513 return -1;
514
515 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
516 return 0;
517
518 /* try to dequeue the next back buffer */
519 if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf)) {
520 _eglLog(_EGL_WARNING, "Could not dequeue buffer from native window");
521 dri2_surf->base.Lost = EGL_TRUE;
522 return -1;
523 }
524
525 /* free outdated buffers and update the surface size */
526 if (dri2_surf->base.Width != dri2_surf->buffer->width ||
527 dri2_surf->base.Height != dri2_surf->buffer->height) {
528 dri2_egl_surface_free_local_buffers(dri2_surf);
529 dri2_surf->base.Width = dri2_surf->buffer->width;
530 dri2_surf->base.Height = dri2_surf->buffer->height;
531 }
532
533 return 0;
534 }
535
536 static int
537 get_front_bo(struct dri2_egl_surface *dri2_surf, unsigned int format)
538 {
539 struct dri2_egl_display *dri2_dpy =
540 dri2_egl_display(dri2_surf->base.Resource.Display);
541
542 if (dri2_surf->dri_image_front)
543 return 0;
544
545 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
546 /* According current EGL spec, front buffer rendering
547 * for window surface is not supported now.
548 * and mesa doesn't have the implementation of this case.
549 * Add warning message, but not treat it as error.
550 */
551 _eglLog(_EGL_DEBUG, "DRI driver requested unsupported front buffer for window surface");
552 } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
553 dri2_surf->dri_image_front =
554 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
555 dri2_surf->base.Width,
556 dri2_surf->base.Height,
557 format,
558 0,
559 dri2_surf);
560 if (!dri2_surf->dri_image_front) {
561 _eglLog(_EGL_WARNING, "dri2_image_front allocation failed");
562 return -1;
563 }
564 }
565
566 return 0;
567 }
568
569 static int
570 get_back_bo(struct dri2_egl_surface *dri2_surf)
571 {
572 struct dri2_egl_display *dri2_dpy =
573 dri2_egl_display(dri2_surf->base.Resource.Display);
574 int fourcc, pitch;
575 int offset = 0, fds[3];
576 unsigned num_fds;
577
578 if (dri2_surf->dri_image_back)
579 return 0;
580
581 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
582 if (!dri2_surf->buffer) {
583 _eglLog(_EGL_WARNING, "Could not get native buffer");
584 return -1;
585 }
586
587 num_fds = get_native_buffer_fds(dri2_surf->buffer, fds);
588 if (num_fds == 0) {
589 _eglLog(_EGL_WARNING, "Could not get native buffer FD");
590 return -1;
591 }
592
593 fourcc = get_fourcc(dri2_surf->buffer->format);
594
595 pitch = dri2_surf->buffer->stride *
596 get_format_bpp(dri2_surf->buffer->format);
597
598 if (fourcc == -1 || pitch == 0) {
599 _eglLog(_EGL_WARNING, "Invalid buffer fourcc(%x) or pitch(%d)",
600 fourcc, pitch);
601 return -1;
602 }
603
604 dri2_surf->dri_image_back =
605 dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
606 dri2_surf->base.Width,
607 dri2_surf->base.Height,
608 fourcc,
609 fds,
610 num_fds,
611 &pitch,
612 &offset,
613 dri2_surf);
614 if (!dri2_surf->dri_image_back) {
615 _eglLog(_EGL_WARNING, "failed to create DRI image from FD");
616 return -1;
617 }
618 } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
619 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
620 * the spec states that they have a back buffer but no front buffer, in
621 * contrast to pixmaps, which have a front buffer but no back buffer.
622 *
623 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
624 * from the spec, following the precedent of Mesa's EGL X11 platform. The
625 * X11 platform correctly assigns pbuffers to single-buffered configs, but
626 * assigns the pbuffer a front buffer instead of a back buffer.
627 *
628 * Pbuffers in the X11 platform mostly work today, so let's just copy its
629 * behavior instead of trying to fix (and hence potentially breaking) the
630 * world.
631 */
632 _eglLog(_EGL_DEBUG, "DRI driver requested unsupported back buffer for pbuffer surface");
633 }
634
635 return 0;
636 }
637
638 /* Some drivers will pass multiple bits in buffer_mask.
639 * For such case, will go through all the bits, and
640 * will not return error when unsupported buffer is requested, only
641 * return error when the allocation for supported buffer failed.
642 */
643 static int
644 droid_image_get_buffers(__DRIdrawable *driDrawable,
645 unsigned int format,
646 uint32_t *stamp,
647 void *loaderPrivate,
648 uint32_t buffer_mask,
649 struct __DRIimageList *images)
650 {
651 struct dri2_egl_surface *dri2_surf = loaderPrivate;
652
653 images->image_mask = 0;
654 images->front = NULL;
655 images->back = NULL;
656
657 if (update_buffers(dri2_surf) < 0)
658 return 0;
659
660 if (_eglSurfaceInSharedBufferMode(&dri2_surf->base)) {
661 if (get_back_bo(dri2_surf) < 0)
662 return 0;
663
664 /* We have dri_image_back because this is a window surface and
665 * get_back_bo() succeeded.
666 */
667 assert(dri2_surf->dri_image_back);
668 images->back = dri2_surf->dri_image_back;
669 images->image_mask |= __DRI_IMAGE_BUFFER_SHARED;
670
671 /* There exists no accompanying back nor front buffer. */
672 return 1;
673 }
674
675 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
676 if (get_front_bo(dri2_surf, format) < 0)
677 return 0;
678
679 if (dri2_surf->dri_image_front) {
680 images->front = dri2_surf->dri_image_front;
681 images->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
682 }
683 }
684
685 if (buffer_mask & __DRI_IMAGE_BUFFER_BACK) {
686 if (get_back_bo(dri2_surf) < 0)
687 return 0;
688
689 if (dri2_surf->dri_image_back) {
690 images->back = dri2_surf->dri_image_back;
691 images->image_mask |= __DRI_IMAGE_BUFFER_BACK;
692 }
693 }
694
695 return 1;
696 }
697
698 static EGLint
699 droid_query_buffer_age(_EGLDriver *drv,
700 _EGLDisplay *disp, _EGLSurface *surface)
701 {
702 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
703
704 if (update_buffers(dri2_surf) < 0) {
705 _eglError(EGL_BAD_ALLOC, "droid_query_buffer_age");
706 return -1;
707 }
708
709 return dri2_surf->back ? dri2_surf->back->age : 0;
710 }
711
712 static EGLBoolean
713 droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
714 {
715 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
716 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
717 const bool has_mutable_rb = _eglSurfaceHasMutableRenderBuffer(draw);
718
719 /* From the EGL_KHR_mutable_render_buffer spec (v12):
720 *
721 * If surface is a single-buffered window, pixmap, or pbuffer surface
722 * for which there is no pending change to the EGL_RENDER_BUFFER
723 * attribute, eglSwapBuffers has no effect.
724 */
725 if (has_mutable_rb &&
726 draw->RequestedRenderBuffer == EGL_SINGLE_BUFFER &&
727 draw->ActiveRenderBuffer == EGL_SINGLE_BUFFER) {
728 _eglLog(_EGL_DEBUG, "%s: remain in shared buffer mode", __func__);
729 return EGL_TRUE;
730 }
731
732 for (int i = 0; i < dri2_surf->color_buffers_count; i++) {
733 if (dri2_surf->color_buffers[i].age > 0)
734 dri2_surf->color_buffers[i].age++;
735 }
736
737 /* "XXX: we don't use get_back_bo() since it causes regressions in
738 * several dEQP tests.
739 */
740 if (dri2_surf->back)
741 dri2_surf->back->age = 1;
742
743 dri2_flush_drawable_for_swapbuffers(disp, draw);
744
745 /* dri2_surf->buffer can be null even when no error has occured. For
746 * example, if the user has called no GL rendering commands since the
747 * previous eglSwapBuffers, then the driver may have not triggered
748 * a callback to ANativeWindow::dequeueBuffer, in which case
749 * dri2_surf->buffer remains null.
750 */
751 if (dri2_surf->buffer)
752 droid_window_enqueue_buffer(disp, dri2_surf);
753
754 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
755
756 /* Update the shared buffer mode */
757 if (has_mutable_rb &&
758 draw->ActiveRenderBuffer != draw->RequestedRenderBuffer) {
759 bool mode = (draw->RequestedRenderBuffer == EGL_SINGLE_BUFFER);
760 _eglLog(_EGL_DEBUG, "%s: change to shared buffer mode %d",
761 __func__, mode);
762
763 if (!droid_set_shared_buffer_mode(disp, draw, mode))
764 return EGL_FALSE;
765 draw->ActiveRenderBuffer = draw->RequestedRenderBuffer;
766 }
767
768 return EGL_TRUE;
769 }
770
771 static _EGLImage *
772 droid_create_image_from_prime_fds_yuv(_EGLDisplay *disp, _EGLContext *ctx,
773 struct ANativeWindowBuffer *buf,
774 int num_fds, int fds[3])
775 {
776 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
777 struct android_ycbcr ycbcr;
778 size_t offsets[3];
779 size_t pitches[3];
780 enum chroma_order chroma_order;
781 int fourcc;
782 int ret;
783
784 if (!dri2_dpy->gralloc->lock_ycbcr) {
785 _eglLog(_EGL_WARNING, "Gralloc does not support lock_ycbcr");
786 return NULL;
787 }
788
789 memset(&ycbcr, 0, sizeof(ycbcr));
790 ret = dri2_dpy->gralloc->lock_ycbcr(dri2_dpy->gralloc, buf->handle,
791 0, 0, 0, 0, 0, &ycbcr);
792 if (ret) {
793 /* HACK: See droid_create_image_from_prime_fds() and
794 * https://issuetracker.google.com/32077885.*/
795 if (buf->format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)
796 return NULL;
797
798 _eglLog(_EGL_WARNING, "gralloc->lock_ycbcr failed: %d", ret);
799 return NULL;
800 }
801 dri2_dpy->gralloc->unlock(dri2_dpy->gralloc, buf->handle);
802
803 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
804 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
805 * so they can be interpreted as offsets. */
806 offsets[0] = (size_t)ycbcr.y;
807 /* We assume here that all the planes are located in one DMA-buf. */
808 if ((size_t)ycbcr.cr < (size_t)ycbcr.cb) {
809 chroma_order = YCrCb;
810 offsets[1] = (size_t)ycbcr.cr;
811 offsets[2] = (size_t)ycbcr.cb;
812 } else {
813 chroma_order = YCbCr;
814 offsets[1] = (size_t)ycbcr.cb;
815 offsets[2] = (size_t)ycbcr.cr;
816 }
817
818 /* .ystride is the line length (in bytes) of the Y plane,
819 * .cstride is the line length (in bytes) of any of the remaining
820 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
821 * planar formats. */
822 pitches[0] = ycbcr.ystride;
823 pitches[1] = pitches[2] = ycbcr.cstride;
824
825 /* .chroma_step is the byte distance between the same chroma channel
826 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
827 fourcc = get_fourcc_yuv(buf->format, chroma_order, ycbcr.chroma_step);
828 if (fourcc == -1) {
829 _eglLog(_EGL_WARNING, "unsupported YUV format, native = %x, chroma_order = %s, chroma_step = %d",
830 buf->format, chroma_order == YCbCr ? "YCbCr" : "YCrCb", ycbcr.chroma_step);
831 return NULL;
832 }
833
834 /*
835 * Since this is EGL_NATIVE_BUFFER_ANDROID don't assume that
836 * the single-fd case cannot happen. So handle eithe single
837 * fd or fd-per-plane case:
838 */
839 if (num_fds == 1) {
840 fds[2] = fds[1] = fds[0];
841 } else {
842 int expected_planes = (ycbcr.chroma_step == 2) ? 2 : 3;
843 assert(num_fds == expected_planes);
844 }
845
846 if (ycbcr.chroma_step == 2) {
847 /* Semi-planar Y + CbCr or Y + CrCb format. */
848 const EGLint attr_list_2plane[] = {
849 EGL_WIDTH, buf->width,
850 EGL_HEIGHT, buf->height,
851 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
852 EGL_DMA_BUF_PLANE0_FD_EXT, fds[0],
853 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0],
854 EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0],
855 EGL_DMA_BUF_PLANE1_FD_EXT, fds[1],
856 EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1],
857 EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1],
858 EGL_NONE, 0
859 };
860
861 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_2plane);
862 } else {
863 /* Fully planar Y + Cb + Cr or Y + Cr + Cb format. */
864 const EGLint attr_list_3plane[] = {
865 EGL_WIDTH, buf->width,
866 EGL_HEIGHT, buf->height,
867 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
868 EGL_DMA_BUF_PLANE0_FD_EXT, fds[0],
869 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0],
870 EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0],
871 EGL_DMA_BUF_PLANE1_FD_EXT, fds[1],
872 EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1],
873 EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1],
874 EGL_DMA_BUF_PLANE2_FD_EXT, fds[2],
875 EGL_DMA_BUF_PLANE2_PITCH_EXT, pitches[2],
876 EGL_DMA_BUF_PLANE2_OFFSET_EXT, offsets[2],
877 EGL_NONE, 0
878 };
879
880 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_3plane);
881 }
882 }
883
884 static _EGLImage *
885 droid_create_image_from_prime_fds(_EGLDisplay *disp, _EGLContext *ctx,
886 struct ANativeWindowBuffer *buf, int num_fds, int fds[3])
887 {
888 unsigned int pitch;
889
890 if (is_yuv(buf->format)) {
891 _EGLImage *image;
892
893 image = droid_create_image_from_prime_fds_yuv(disp, ctx, buf, num_fds, fds);
894 /*
895 * HACK: https://issuetracker.google.com/32077885
896 * There is no API available to properly query the IMPLEMENTATION_DEFINED
897 * format. As a workaround we rely here on gralloc allocating either
898 * an arbitrary YCbCr 4:2:0 or RGBX_8888, with the latter being recognized
899 * by lock_ycbcr failing.
900 */
901 if (image || buf->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)
902 return image;
903 }
904
905 /*
906 * Non-YUV formats could *also* have multiple planes, such as ancillary
907 * color compression state buffer, but the rest of the code isn't ready
908 * yet to deal with modifiers:
909 */
910 assert(num_fds == 1);
911
912 const int fourcc = get_fourcc(buf->format);
913 if (fourcc == -1) {
914 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
915 return NULL;
916 }
917
918 pitch = buf->stride * get_format_bpp(buf->format);
919 if (pitch == 0) {
920 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
921 return NULL;
922 }
923
924 const EGLint attr_list[] = {
925 EGL_WIDTH, buf->width,
926 EGL_HEIGHT, buf->height,
927 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
928 EGL_DMA_BUF_PLANE0_FD_EXT, fds[0],
929 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitch,
930 EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,
931 EGL_NONE, 0
932 };
933
934 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list);
935 }
936
937 #ifdef HAVE_DRM_GRALLOC
938 static _EGLImage *
939 droid_create_image_from_name(_EGLDisplay *disp, _EGLContext *ctx,
940 struct ANativeWindowBuffer *buf)
941 {
942 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
943 struct dri2_egl_image *dri2_img;
944 int name;
945 int format;
946
947 name = get_native_buffer_name(buf);
948 if (!name) {
949 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
950 return NULL;
951 }
952
953 format = get_format(buf->format);
954 if (format == -1)
955 return NULL;
956
957 dri2_img = calloc(1, sizeof(*dri2_img));
958 if (!dri2_img) {
959 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
960 return NULL;
961 }
962
963 _eglInitImage(&dri2_img->base, disp);
964
965 dri2_img->dri_image =
966 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
967 buf->width,
968 buf->height,
969 format,
970 name,
971 buf->stride,
972 dri2_img);
973 if (!dri2_img->dri_image) {
974 free(dri2_img);
975 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
976 return NULL;
977 }
978
979 return &dri2_img->base;
980 }
981 #endif /* HAVE_DRM_GRALLOC */
982
983 static EGLBoolean
984 droid_query_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
985 EGLint attribute, EGLint *value)
986 {
987 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
988 switch (attribute) {
989 case EGL_WIDTH:
990 if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) {
991 dri2_surf->window->query(dri2_surf->window,
992 NATIVE_WINDOW_DEFAULT_WIDTH, value);
993 return EGL_TRUE;
994 }
995 break;
996 case EGL_HEIGHT:
997 if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) {
998 dri2_surf->window->query(dri2_surf->window,
999 NATIVE_WINDOW_DEFAULT_HEIGHT, value);
1000 return EGL_TRUE;
1001 }
1002 break;
1003 default:
1004 break;
1005 }
1006 return _eglQuerySurface(drv, disp, surf, attribute, value);
1007 }
1008
1009 static _EGLImage *
1010 dri2_create_image_android_native_buffer(_EGLDisplay *disp,
1011 _EGLContext *ctx,
1012 struct ANativeWindowBuffer *buf)
1013 {
1014 int fds[3];
1015 unsigned num_fds;
1016
1017 if (ctx != NULL) {
1018 /* From the EGL_ANDROID_image_native_buffer spec:
1019 *
1020 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
1021 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
1022 */
1023 _eglError(EGL_BAD_CONTEXT, "eglCreateEGLImageKHR: for "
1024 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
1025 "EGL_NO_CONTEXT");
1026 return NULL;
1027 }
1028
1029 if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC ||
1030 buf->common.version != sizeof(*buf)) {
1031 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
1032 return NULL;
1033 }
1034
1035 num_fds = get_native_buffer_fds(buf, fds);
1036 if (num_fds > 0)
1037 return droid_create_image_from_prime_fds(disp, ctx, buf, num_fds, fds);
1038
1039 #ifdef HAVE_DRM_GRALLOC
1040 return droid_create_image_from_name(disp, ctx, buf);
1041 #else
1042 return NULL;
1043 #endif
1044 }
1045
1046 static _EGLImage *
1047 droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
1048 _EGLContext *ctx, EGLenum target,
1049 EGLClientBuffer buffer, const EGLint *attr_list)
1050 {
1051 switch (target) {
1052 case EGL_NATIVE_BUFFER_ANDROID:
1053 return dri2_create_image_android_native_buffer(disp, ctx,
1054 (struct ANativeWindowBuffer *) buffer);
1055 default:
1056 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
1057 }
1058 }
1059
1060 static void
1061 droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
1062 {
1063 }
1064
1065 #ifdef HAVE_DRM_GRALLOC
1066 static int
1067 droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf,
1068 unsigned int *attachments, int count)
1069 {
1070 int num_buffers = 0;
1071
1072 /* fill dri2_surf->buffers */
1073 for (int i = 0; i < count * 2; i += 2) {
1074 __DRIbuffer *buf, *local;
1075
1076 assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers));
1077 buf = &dri2_surf->buffers[num_buffers];
1078
1079 switch (attachments[i]) {
1080 case __DRI_BUFFER_BACK_LEFT:
1081 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
1082 buf->attachment = attachments[i];
1083 buf->name = get_native_buffer_name(dri2_surf->buffer);
1084 buf->cpp = get_format_bpp(dri2_surf->buffer->format);
1085 buf->pitch = dri2_surf->buffer->stride * buf->cpp;
1086 buf->flags = 0;
1087
1088 if (buf->name)
1089 num_buffers++;
1090
1091 break;
1092 }
1093 /* fall through for pbuffers */
1094 case __DRI_BUFFER_DEPTH:
1095 case __DRI_BUFFER_STENCIL:
1096 case __DRI_BUFFER_ACCUM:
1097 case __DRI_BUFFER_DEPTH_STENCIL:
1098 case __DRI_BUFFER_HIZ:
1099 local = dri2_egl_surface_alloc_local_buffer(dri2_surf,
1100 attachments[i], attachments[i + 1]);
1101
1102 if (local) {
1103 *buf = *local;
1104 num_buffers++;
1105 }
1106 break;
1107 case __DRI_BUFFER_FRONT_LEFT:
1108 case __DRI_BUFFER_FRONT_RIGHT:
1109 case __DRI_BUFFER_FAKE_FRONT_LEFT:
1110 case __DRI_BUFFER_FAKE_FRONT_RIGHT:
1111 case __DRI_BUFFER_BACK_RIGHT:
1112 default:
1113 /* no front or right buffers */
1114 break;
1115 }
1116 }
1117
1118 return num_buffers;
1119 }
1120
1121 static __DRIbuffer *
1122 droid_get_buffers_with_format(__DRIdrawable * driDrawable,
1123 int *width, int *height,
1124 unsigned int *attachments, int count,
1125 int *out_count, void *loaderPrivate)
1126 {
1127 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1128
1129 if (update_buffers(dri2_surf) < 0)
1130 return NULL;
1131
1132 *out_count = droid_get_buffers_parse_attachments(dri2_surf, attachments, count);
1133
1134 if (width)
1135 *width = dri2_surf->base.Width;
1136 if (height)
1137 *height = dri2_surf->base.Height;
1138
1139 return dri2_surf->buffers;
1140 }
1141 #endif /* HAVE_DRM_GRALLOC */
1142
1143 static unsigned
1144 droid_get_capability(void *loaderPrivate, enum dri_loader_cap cap)
1145 {
1146 /* Note: loaderPrivate is _EGLDisplay* */
1147 switch (cap) {
1148 case DRI_LOADER_CAP_RGBA_ORDERING:
1149 return 1;
1150 default:
1151 return 0;
1152 }
1153 }
1154
1155 static EGLBoolean
1156 droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
1157 {
1158 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1159 static const struct {
1160 int format;
1161 int rgba_shifts[4];
1162 unsigned int rgba_sizes[4];
1163 } visuals[] = {
1164 { HAL_PIXEL_FORMAT_RGBA_8888, { 0, 8, 16, 24 }, { 8, 8, 8, 8 } },
1165 { HAL_PIXEL_FORMAT_RGBX_8888, { 0, 8, 16, -1 }, { 8, 8, 8, 0 } },
1166 { HAL_PIXEL_FORMAT_RGB_565, { 11, 5, 0, -1 }, { 5, 6, 5, 0 } },
1167 /* This must be after HAL_PIXEL_FORMAT_RGBA_8888, we only keep BGRA
1168 * visual if it turns out RGBA visual is not available.
1169 */
1170 { HAL_PIXEL_FORMAT_BGRA_8888, { 16, 8, 0, 24 }, { 8, 8, 8, 8 } },
1171 };
1172
1173 unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
1174 int config_count = 0;
1175
1176 /* The nesting of loops is significant here. Also significant is the order
1177 * of the HAL pixel formats. Many Android apps (such as Google's official
1178 * NDK GLES2 example app), and even portions the core framework code (such
1179 * as SystemServiceManager in Nougat), incorrectly choose their EGLConfig.
1180 * They neglect to match the EGLConfig's EGL_NATIVE_VISUAL_ID against the
1181 * window's native format, and instead choose the first EGLConfig whose
1182 * channel sizes match those of the native window format while ignoring the
1183 * channel *ordering*.
1184 *
1185 * We can detect such buggy clients in logcat when they call
1186 * eglCreateSurface, by detecting the mismatch between the EGLConfig's
1187 * format and the window's format.
1188 *
1189 * As a workaround, we generate EGLConfigs such that all EGLConfigs for HAL
1190 * pixel format i precede those for HAL pixel format i+1. In my
1191 * (chadversary) testing on Android Nougat, this was good enough to pacify
1192 * the buggy clients.
1193 */
1194 bool has_rgba = false;
1195 for (int i = 0; i < ARRAY_SIZE(visuals); i++) {
1196 /* Only enable BGRA configs when RGBA is not available. BGRA configs are
1197 * buggy on stock Android.
1198 */
1199 if (visuals[i].format == HAL_PIXEL_FORMAT_BGRA_8888 && has_rgba)
1200 continue;
1201 for (int j = 0; dri2_dpy->driver_configs[j]; j++) {
1202 const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
1203
1204 const EGLint config_attrs[] = {
1205 EGL_NATIVE_VISUAL_ID, visuals[i].format,
1206 EGL_NATIVE_VISUAL_TYPE, visuals[i].format,
1207 EGL_FRAMEBUFFER_TARGET_ANDROID, EGL_TRUE,
1208 EGL_RECORDABLE_ANDROID, EGL_TRUE,
1209 EGL_NONE
1210 };
1211
1212 struct dri2_egl_config *dri2_conf =
1213 dri2_add_config(disp, dri2_dpy->driver_configs[j],
1214 config_count + 1, surface_type, config_attrs,
1215 visuals[i].rgba_shifts, visuals[i].rgba_sizes);
1216 if (dri2_conf) {
1217 if (dri2_conf->base.ConfigID == config_count + 1)
1218 config_count++;
1219 format_count[i]++;
1220 }
1221 }
1222 if (visuals[i].format == HAL_PIXEL_FORMAT_RGBA_8888 && format_count[i])
1223 has_rgba = true;
1224 }
1225
1226 for (int i = 0; i < ARRAY_SIZE(format_count); i++) {
1227 if (!format_count[i]) {
1228 _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
1229 visuals[i].format);
1230 }
1231 }
1232
1233 return (config_count != 0);
1234 }
1235
1236 static const struct dri2_egl_display_vtbl droid_display_vtbl = {
1237 .authenticate = NULL,
1238 .create_window_surface = droid_create_window_surface,
1239 .create_pixmap_surface = dri2_fallback_create_pixmap_surface,
1240 .create_pbuffer_surface = droid_create_pbuffer_surface,
1241 .destroy_surface = droid_destroy_surface,
1242 .create_image = droid_create_image_khr,
1243 .swap_buffers = droid_swap_buffers,
1244 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage, /* Android implements the function */
1245 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1246 .swap_interval = droid_swap_interval,
1247 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1248 .copy_buffers = dri2_fallback_copy_buffers,
1249 .query_buffer_age = droid_query_buffer_age,
1250 .query_surface = droid_query_surface,
1251 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1252 .get_sync_values = dri2_fallback_get_sync_values,
1253 .get_dri_drawable = dri2_surface_get_dri_drawable,
1254 .set_shared_buffer_mode = droid_set_shared_buffer_mode,
1255 };
1256
1257 #ifdef HAVE_DRM_GRALLOC
1258 static const __DRIdri2LoaderExtension droid_dri2_loader_extension = {
1259 .base = { __DRI_DRI2_LOADER, 4 },
1260
1261 .getBuffers = NULL,
1262 .flushFrontBuffer = droid_flush_front_buffer,
1263 .getBuffersWithFormat = droid_get_buffers_with_format,
1264 .getCapability = droid_get_capability,
1265 };
1266
1267 static const __DRIextension *droid_dri2_loader_extensions[] = {
1268 &droid_dri2_loader_extension.base,
1269 &image_lookup_extension.base,
1270 &use_invalidate.base,
1271 /* No __DRI_MUTABLE_RENDER_BUFFER_LOADER because it requires
1272 * __DRI_IMAGE_LOADER.
1273 */
1274 NULL,
1275 };
1276 #endif /* HAVE_DRM_GRALLOC */
1277
1278 static const __DRIimageLoaderExtension droid_image_loader_extension = {
1279 .base = { __DRI_IMAGE_LOADER, 2 },
1280
1281 .getBuffers = droid_image_get_buffers,
1282 .flushFrontBuffer = droid_flush_front_buffer,
1283 .getCapability = droid_get_capability,
1284 };
1285
1286 static void
1287 droid_display_shared_buffer(__DRIdrawable *driDrawable, int fence_fd,
1288 void *loaderPrivate)
1289 {
1290 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1291 struct ANativeWindowBuffer *old_buffer UNUSED = dri2_surf->buffer;
1292
1293 if (!_eglSurfaceInSharedBufferMode(&dri2_surf->base)) {
1294 _eglLog(_EGL_WARNING, "%s: internal error: buffer is not shared",
1295 __func__);
1296 return;
1297 }
1298
1299 if (fence_fd >= 0) {
1300 /* The driver's fence is more recent than the surface's out fence, if it
1301 * exists at all. So use the driver's fence.
1302 */
1303 if (dri2_surf->out_fence_fd >= 0) {
1304 close(dri2_surf->out_fence_fd);
1305 dri2_surf->out_fence_fd = -1;
1306 }
1307 } else if (dri2_surf->out_fence_fd >= 0) {
1308 fence_fd = dri2_surf->out_fence_fd;
1309 dri2_surf->out_fence_fd = -1;
1310 }
1311
1312 if (dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer,
1313 fence_fd)) {
1314 _eglLog(_EGL_WARNING, "%s: ANativeWindow::queueBuffer failed", __func__);
1315 close(fence_fd);
1316 return;
1317 }
1318
1319 fence_fd = -1;
1320
1321 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer,
1322 &fence_fd)) {
1323 /* Tear down the surface because it no longer has a back buffer. */
1324 struct dri2_egl_display *dri2_dpy =
1325 dri2_egl_display(dri2_surf->base.Resource.Display);
1326
1327 _eglLog(_EGL_WARNING, "%s: ANativeWindow::dequeueBuffer failed", __func__);
1328
1329 dri2_surf->base.Lost = true;
1330 dri2_surf->buffer = NULL;
1331 dri2_surf->back = NULL;
1332
1333 if (dri2_surf->dri_image_back) {
1334 dri2_dpy->image->destroyImage(dri2_surf->dri_image_back);
1335 dri2_surf->dri_image_back = NULL;
1336 }
1337
1338 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
1339 return;
1340 }
1341
1342 if (fence_fd < 0)
1343 return;
1344
1345 /* Access to the buffer is controlled by a sync fence. Block on it.
1346 *
1347 * Ideally, we would submit the fence to the driver, and the driver would
1348 * postpone command execution until it signalled. But DRI lacks API for
1349 * that (as of 2018-04-11).
1350 *
1351 * SYNC_IOC_WAIT waits forever if timeout < 0
1352 */
1353 sync_wait(fence_fd, -1);
1354 close(fence_fd);
1355 }
1356
1357 static const __DRImutableRenderBufferLoaderExtension droid_mutable_render_buffer_extension = {
1358 .base = { __DRI_MUTABLE_RENDER_BUFFER_LOADER, 1 },
1359 .displaySharedBuffer = droid_display_shared_buffer,
1360 };
1361
1362 static const __DRIextension *droid_image_loader_extensions[] = {
1363 &droid_image_loader_extension.base,
1364 &image_lookup_extension.base,
1365 &use_invalidate.base,
1366 &droid_mutable_render_buffer_extension.base,
1367 NULL,
1368 };
1369
1370 static EGLBoolean
1371 droid_load_driver(_EGLDisplay *disp, bool swrast)
1372 {
1373 struct dri2_egl_display *dri2_dpy = disp->DriverData;
1374 const char *err;
1375
1376 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
1377 if (dri2_dpy->driver_name == NULL)
1378 return false;
1379
1380 #ifdef HAVE_DRM_GRALLOC
1381 /* Handle control nodes using __DRI_DRI2_LOADER extension and GEM names
1382 * for backwards compatibility with drm_gralloc. (Do not use on new
1383 * systems.) */
1384 dri2_dpy->loader_extensions = droid_dri2_loader_extensions;
1385 if (!dri2_load_driver(disp)) {
1386 err = "DRI2: failed to load driver";
1387 goto error;
1388 }
1389 #else
1390 if (swrast) {
1391 /* Use kms swrast only with vgem / virtio_gpu.
1392 * virtio-gpu fallbacks to software rendering when 3D features
1393 * are unavailable since 6c5ab.
1394 */
1395 if (strcmp(dri2_dpy->driver_name, "vgem") == 0 ||
1396 strcmp(dri2_dpy->driver_name, "virtio_gpu") == 0) {
1397 free(dri2_dpy->driver_name);
1398 dri2_dpy->driver_name = strdup("kms_swrast");
1399 } else {
1400 err = "DRI3: failed to find software capable driver";
1401 goto error;
1402 }
1403 }
1404
1405 dri2_dpy->loader_extensions = droid_image_loader_extensions;
1406 if (!dri2_load_driver_dri3(disp)) {
1407 err = "DRI3: failed to load driver";
1408 goto error;
1409 }
1410 #endif
1411
1412 return true;
1413
1414 error:
1415 free(dri2_dpy->driver_name);
1416 dri2_dpy->driver_name = NULL;
1417 return false;
1418 }
1419
1420 static void
1421 droid_unload_driver(_EGLDisplay *disp)
1422 {
1423 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1424
1425 dlclose(dri2_dpy->driver);
1426 dri2_dpy->driver = NULL;
1427 free(dri2_dpy->driver_name);
1428 dri2_dpy->driver_name = NULL;
1429 }
1430
1431 static int
1432 droid_filter_device(_EGLDisplay *disp, int fd, const char *vendor)
1433 {
1434 drmVersionPtr ver = drmGetVersion(fd);
1435 if (!ver)
1436 return -1;
1437
1438 if (strcmp(vendor, ver->name) != 0) {
1439 drmFreeVersion(ver);
1440 return -1;
1441 }
1442
1443 drmFreeVersion(ver);
1444 return 0;
1445 }
1446
1447 static EGLBoolean
1448 droid_probe_device(_EGLDisplay *disp, bool swrast)
1449 {
1450 /* Check that the device is supported, by attempting to:
1451 * - load the dri module
1452 * - and, create a screen
1453 */
1454 if (!droid_load_driver(disp, swrast))
1455 return EGL_FALSE;
1456
1457 if (!dri2_create_screen(disp)) {
1458 _eglLog(_EGL_WARNING, "DRI2: failed to create screen");
1459 droid_unload_driver(disp);
1460 return EGL_FALSE;
1461 }
1462 return EGL_TRUE;
1463 }
1464
1465 #ifdef HAVE_DRM_GRALLOC
1466 static EGLBoolean
1467 droid_open_device(_EGLDisplay *disp, bool swrast)
1468 {
1469 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1470 int fd = -1, err = -EINVAL;
1471
1472 if (swrast)
1473 return EGL_FALSE;
1474
1475 if (dri2_dpy->gralloc->perform)
1476 err = dri2_dpy->gralloc->perform(dri2_dpy->gralloc,
1477 GRALLOC_MODULE_PERFORM_GET_DRM_FD,
1478 &fd);
1479 if (err || fd < 0) {
1480 _eglLog(_EGL_WARNING, "fail to get drm fd");
1481 return EGL_FALSE;
1482 }
1483
1484 dri2_dpy->fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
1485 if (dri2_dpy->fd < 0)
1486 return EGL_FALSE;
1487
1488 if (drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER)
1489 return EGL_FALSE;
1490
1491 return droid_probe_device(disp, swrast);
1492 }
1493 #else
1494 static EGLBoolean
1495 droid_open_device(_EGLDisplay *disp, bool swrast)
1496 {
1497 #define MAX_DRM_DEVICES 64
1498 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1499 drmDevicePtr device, devices[MAX_DRM_DEVICES] = { NULL };
1500 int num_devices;
1501
1502 char *vendor_name = NULL;
1503 char vendor_buf[PROPERTY_VALUE_MAX];
1504
1505 #ifdef EGL_FORCE_RENDERNODE
1506 const unsigned node_type = DRM_NODE_RENDER;
1507 #else
1508 const unsigned node_type = swrast ? DRM_NODE_PRIMARY : DRM_NODE_RENDER;
1509 #endif
1510
1511 if (property_get("drm.gpu.vendor_name", vendor_buf, NULL) > 0)
1512 vendor_name = vendor_buf;
1513
1514 num_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
1515 if (num_devices < 0)
1516 return EGL_FALSE;
1517
1518 for (int i = 0; i < num_devices; i++) {
1519 device = devices[i];
1520
1521 if (!(device->available_nodes & (1 << node_type)))
1522 continue;
1523
1524 dri2_dpy->fd = loader_open_device(device->nodes[node_type]);
1525 if (dri2_dpy->fd < 0) {
1526 _eglLog(_EGL_WARNING, "%s() Failed to open DRM device %s",
1527 __func__, device->nodes[node_type]);
1528 continue;
1529 }
1530
1531 /* If a vendor is explicitly provided, we use only that.
1532 * Otherwise we fall-back the first device that is supported.
1533 */
1534 if (vendor_name) {
1535 if (droid_filter_device(disp, dri2_dpy->fd, vendor_name)) {
1536 /* Device does not match - try next device */
1537 close(dri2_dpy->fd);
1538 dri2_dpy->fd = -1;
1539 continue;
1540 }
1541 /* If the requested device matches - use it. Regardless if
1542 * init fails, do not fall-back to any other device.
1543 */
1544 if (!droid_probe_device(disp, false)) {
1545 close(dri2_dpy->fd);
1546 dri2_dpy->fd = -1;
1547 }
1548
1549 break;
1550 }
1551 if (droid_probe_device(disp, swrast))
1552 break;
1553
1554 /* No explicit request - attempt the next device */
1555 close(dri2_dpy->fd);
1556 dri2_dpy->fd = -1;
1557 }
1558 drmFreeDevices(devices, num_devices);
1559
1560 if (dri2_dpy->fd < 0) {
1561 _eglLog(_EGL_WARNING, "Failed to open %s DRM device",
1562 vendor_name ? "desired": "any");
1563 return EGL_FALSE;
1564 }
1565
1566 return EGL_TRUE;
1567 #undef MAX_DRM_DEVICES
1568 }
1569
1570 #endif
1571
1572 EGLBoolean
1573 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *disp)
1574 {
1575 _EGLDevice *dev;
1576 bool device_opened = false;
1577 struct dri2_egl_display *dri2_dpy;
1578 const char *err;
1579 int ret;
1580
1581 dri2_dpy = calloc(1, sizeof(*dri2_dpy));
1582 if (!dri2_dpy)
1583 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1584
1585 dri2_dpy->fd = -1;
1586 ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
1587 (const hw_module_t **)&dri2_dpy->gralloc);
1588 if (ret) {
1589 err = "DRI2: failed to get gralloc module";
1590 goto cleanup;
1591 }
1592
1593 disp->DriverData = (void *) dri2_dpy;
1594 if (!disp->Options.ForceSoftware)
1595 device_opened = droid_open_device(disp, false);
1596 if (!device_opened)
1597 device_opened = droid_open_device(disp, true);
1598
1599 if (!device_opened) {
1600 err = "DRI2: failed to open device";
1601 goto cleanup;
1602 }
1603
1604 dev = _eglAddDevice(dri2_dpy->fd, false);
1605 if (!dev) {
1606 err = "DRI2: failed to find EGLDevice";
1607 goto cleanup;
1608 }
1609
1610 disp->Device = dev;
1611
1612 if (!dri2_setup_extensions(disp)) {
1613 err = "DRI2: failed to setup extensions";
1614 goto cleanup;
1615 }
1616
1617 dri2_setup_screen(disp);
1618
1619 /* We set the maximum swap interval as 1 for Android platform, since it is
1620 * the maximum value supported by Android according to the value of
1621 * ANativeWindow::maxSwapInterval.
1622 */
1623 dri2_setup_swap_interval(disp, 1);
1624
1625 disp->Extensions.ANDROID_framebuffer_target = EGL_TRUE;
1626 disp->Extensions.ANDROID_image_native_buffer = EGL_TRUE;
1627 disp->Extensions.ANDROID_recordable = EGL_TRUE;
1628 disp->Extensions.EXT_buffer_age = EGL_TRUE;
1629 disp->Extensions.KHR_image = EGL_TRUE;
1630 #if ANDROID_API_LEVEL >= 24
1631 if (dri2_dpy->mutable_render_buffer &&
1632 dri2_dpy->loader_extensions == droid_image_loader_extensions) {
1633 disp->Extensions.KHR_mutable_render_buffer = EGL_TRUE;
1634 }
1635 #endif
1636
1637 /* Create configs *after* enabling extensions because presence of DRI
1638 * driver extensions can affect the capabilities of EGLConfigs.
1639 */
1640 if (!droid_add_configs_for_visuals(drv, disp)) {
1641 err = "DRI2: failed to add configs";
1642 goto cleanup;
1643 }
1644
1645 /* Fill vtbl last to prevent accidentally calling virtual function during
1646 * initialization.
1647 */
1648 dri2_dpy->vtbl = &droid_display_vtbl;
1649
1650 return EGL_TRUE;
1651
1652 cleanup:
1653 dri2_display_destroy(disp);
1654 return _eglError(EGL_NOT_INITIALIZED, err);
1655 }