2 * Mesa 3-D graphics library
4 * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
5 * Copyright (C) 2010-2011 LunarG Inc.
7 * Based on platform_x11, which has
9 * Copyright © 2011 Intel Corporation
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:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
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.
30 #include <cutils/properties.h>
38 #include <sync/sync.h>
39 #include <sys/types.h>
40 #include <drm-uapi/drm_fourcc.h>
44 #include "egl_dri2_fallbacks.h"
46 #ifdef HAVE_DRM_GRALLOC
47 #include <gralloc_drm_handle.h>
48 #include "gralloc_drm.h"
49 #endif /* HAVE_DRM_GRALLOC */
51 #define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1))
58 struct droid_yuv_format
{
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. */
65 int fourcc
; /* DRM_FORMAT_ */
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
},
86 get_fourcc_yuv(int native
, enum chroma_order chroma_order
, int chroma_step
)
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
;
100 for (int i
= 0; i
< ARRAY_SIZE(droid_yuv_formats
); ++i
)
101 if (droid_yuv_formats
[i
].native
== native
)
108 get_format_bpp(int native
)
113 case HAL_PIXEL_FORMAT_RGBA_FP16
:
116 case HAL_PIXEL_FORMAT_RGBA_8888
:
117 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
119 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
120 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
122 case HAL_PIXEL_FORMAT_RGBX_8888
:
123 case HAL_PIXEL_FORMAT_BGRA_8888
:
124 case HAL_PIXEL_FORMAT_RGBA_1010102
:
127 case HAL_PIXEL_FORMAT_RGB_565
:
138 /* createImageFromFds requires fourcc format */
139 static int get_fourcc(int 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
:
147 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
148 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
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
;
154 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", native
);
159 static int get_format(int 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
:
167 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
168 * TODO: Revert this once https://issuetracker.google.com/32077885 is fixed.
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
;
174 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", format
);
179 /* returns # of fds, and by reference the actual fds */
181 get_native_buffer_fds(struct ANativeWindowBuffer
*buf
, int fds
[3])
183 native_handle_t
*handle
= (native_handle_t
*)buf
->handle
;
189 * Various gralloc implementations exist, but the dma-buf fd tends
190 * to be first. Access it directly to avoid a dependency on specific
193 for (int i
= 0; i
< handle
->numFds
; i
++)
194 fds
[i
] = handle
->data
[i
];
196 return handle
->numFds
;
199 #ifdef HAVE_DRM_GRALLOC
201 get_native_buffer_name(struct ANativeWindowBuffer
*buf
)
203 return gralloc_drm_get_gem_handle(buf
->handle
);
205 #endif /* HAVE_DRM_GRALLOC */
208 droid_window_dequeue_buffer(struct dri2_egl_surface
*dri2_surf
)
212 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
216 /* If access to the buffer is controlled by a sync fence, then block on the
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.
223 * From the ANativeWindow::dequeueBuffer documentation:
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
234 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
236 * Waits indefinitely if timeout < 0.
239 sync_wait(fence_fd
, timeout
);
243 /* Record all the buffers created by ANativeWindow and update back buffer
244 * for updating buffer's age in swap_buffers.
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
;
251 if (dri2_surf
->color_buffers
[i
].buffer
== dri2_surf
->buffer
) {
252 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
259 /* In case of all the buffers were recreated by ANativeWindow, reset
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;
266 dri2_surf
->color_buffers
[0].buffer
= dri2_surf
->buffer
;
267 dri2_surf
->back
= &dri2_surf
->color_buffers
[0];
274 droid_window_enqueue_buffer(_EGLDisplay
*disp
, struct dri2_egl_surface
*dri2_surf
)
276 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
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
282 mtx_unlock(&disp
->Mutex
);
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.
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.
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
,
299 dri2_surf
->buffer
= NULL
;
300 dri2_surf
->back
= NULL
;
302 mtx_lock(&disp
->Mutex
);
304 if (dri2_surf
->dri_image_back
) {
305 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
306 dri2_surf
->dri_image_back
= NULL
;
313 droid_window_cancel_buffer(struct dri2_egl_surface
*dri2_surf
)
316 int fence_fd
= dri2_surf
->out_fence_fd
;
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
;
323 _eglLog(_EGL_WARNING
, "ANativeWindow::cancelBuffer failed");
324 dri2_surf
->base
.Lost
= EGL_TRUE
;
329 droid_set_shared_buffer_mode(_EGLDisplay
*disp
, _EGLSurface
*surf
, bool mode
)
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
;
336 assert(surf
->Type
== EGL_WINDOW_BIT
);
337 assert(_eglSurfaceHasMutableRenderBuffer(&dri2_surf
->base
));
339 _eglLog(_EGL_DEBUG
, "%s: mode=%d", __func__
, mode
);
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
);
349 _eglLog(_EGL_FATAL
, "%s:%d: internal error: unreachable", __FILE__
, __LINE__
);
355 droid_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
356 _EGLConfig
*conf
, void *native_window
,
357 const EGLint
*attrib_list
)
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
;
365 dri2_surf
= calloc(1, sizeof *dri2_surf
);
367 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
371 if (!dri2_init_surface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
,
372 true, native_window
))
373 goto cleanup_surface
;
375 if (type
== EGL_WINDOW_BIT
) {
378 int min_buffer_count
, max_buffer_count
;
380 /* Prefer triple buffering for performance reasons. */
381 const int preferred_buffer_count
= 3;
383 if (window
->common
.magic
!= ANDROID_NATIVE_WINDOW_MAGIC
) {
384 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
385 goto cleanup_surface
;
387 if (window
->query(window
, NATIVE_WINDOW_FORMAT
, &format
)) {
388 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
389 goto cleanup_surface
;
392 /* Query ANativeWindow for MIN_UNDEQUEUED_BUFFER, minimum amount
393 * of undequeued buffers.
395 if (window
->query(window
, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS
,
396 &min_buffer_count
)) {
397 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
398 goto cleanup_surface
;
401 /* Query for maximum buffer count, application can set this
402 * to limit the total amount of buffers.
404 if (window
->query(window
, NATIVE_WINDOW_MAX_BUFFER_COUNT
,
405 &max_buffer_count
)) {
406 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
407 goto cleanup_surface
;
410 /* Clamp preferred between minimum (min undequeued + 1 dequeued)
413 buffer_count
= CLAMP(preferred_buffer_count
, min_buffer_count
+ 1,
416 if (native_window_set_buffer_count(window
, buffer_count
)) {
417 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
418 goto cleanup_surface
;
420 dri2_surf
->color_buffers
= calloc(buffer_count
,
421 sizeof(*dri2_surf
->color_buffers
));
422 if (!dri2_surf
->color_buffers
) {
423 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
424 goto cleanup_surface
;
426 dri2_surf
->color_buffers_count
= buffer_count
;
428 if (format
!= dri2_conf
->base
.NativeVisualID
) {
429 _eglLog(_EGL_WARNING
, "Native format mismatch: 0x%x != 0x%x",
430 format
, dri2_conf
->base
.NativeVisualID
);
433 window
->query(window
, NATIVE_WINDOW_WIDTH
, &dri2_surf
->base
.Width
);
434 window
->query(window
, NATIVE_WINDOW_HEIGHT
, &dri2_surf
->base
.Height
);
436 uint32_t usage
= strcmp(dri2_dpy
->driver_name
, "kms_swrast") == 0
437 ? GRALLOC_USAGE_SW_READ_OFTEN
| GRALLOC_USAGE_SW_WRITE_OFTEN
438 : GRALLOC_USAGE_HW_RENDER
;
439 native_window_set_usage(window
, usage
);
442 config
= dri2_get_dri_config(dri2_conf
, type
,
443 dri2_surf
->base
.GLColorspace
);
445 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
446 goto cleanup_surface
;
449 if (!dri2_create_drawable(dri2_dpy
, config
, dri2_surf
, dri2_surf
))
450 goto cleanup_surface
;
453 window
->common
.incRef(&window
->common
);
454 dri2_surf
->window
= window
;
457 return &dri2_surf
->base
;
460 if (dri2_surf
->color_buffers_count
)
461 free(dri2_surf
->color_buffers
);
468 droid_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
469 _EGLConfig
*conf
, void *native_window
,
470 const EGLint
*attrib_list
)
472 return droid_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
473 native_window
, attrib_list
);
477 droid_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
478 _EGLConfig
*conf
, const EGLint
*attrib_list
)
480 return droid_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
485 droid_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
487 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
488 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
490 dri2_egl_surface_free_local_buffers(dri2_surf
);
492 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
493 if (dri2_surf
->buffer
)
494 droid_window_cancel_buffer(dri2_surf
);
496 dri2_surf
->window
->common
.decRef(&dri2_surf
->window
->common
);
499 if (dri2_surf
->dri_image_back
) {
500 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_back", __func__
, __LINE__
);
501 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
502 dri2_surf
->dri_image_back
= NULL
;
505 if (dri2_surf
->dri_image_front
) {
506 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_front", __func__
, __LINE__
);
507 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_front
);
508 dri2_surf
->dri_image_front
= NULL
;
511 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
513 dri2_fini_surface(surf
);
514 free(dri2_surf
->color_buffers
);
521 droid_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
,
522 _EGLSurface
*surf
, EGLint interval
)
524 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
525 struct ANativeWindow
*window
= dri2_surf
->window
;
527 if (window
->setSwapInterval(window
, interval
))
530 surf
->SwapInterval
= interval
;
535 update_buffers(struct dri2_egl_surface
*dri2_surf
)
537 if (dri2_surf
->base
.Lost
)
540 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
543 /* try to dequeue the next back buffer */
544 if (!dri2_surf
->buffer
&& !droid_window_dequeue_buffer(dri2_surf
)) {
545 _eglLog(_EGL_WARNING
, "Could not dequeue buffer from native window");
546 dri2_surf
->base
.Lost
= EGL_TRUE
;
550 /* free outdated buffers and update the surface size */
551 if (dri2_surf
->base
.Width
!= dri2_surf
->buffer
->width
||
552 dri2_surf
->base
.Height
!= dri2_surf
->buffer
->height
) {
553 dri2_egl_surface_free_local_buffers(dri2_surf
);
554 dri2_surf
->base
.Width
= dri2_surf
->buffer
->width
;
555 dri2_surf
->base
.Height
= dri2_surf
->buffer
->height
;
562 get_front_bo(struct dri2_egl_surface
*dri2_surf
, unsigned int format
)
564 struct dri2_egl_display
*dri2_dpy
=
565 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
567 if (dri2_surf
->dri_image_front
)
570 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
571 /* According current EGL spec, front buffer rendering
572 * for window surface is not supported now.
573 * and mesa doesn't have the implementation of this case.
574 * Add warning message, but not treat it as error.
576 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported front buffer for window surface");
577 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
578 dri2_surf
->dri_image_front
=
579 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
580 dri2_surf
->base
.Width
,
581 dri2_surf
->base
.Height
,
585 if (!dri2_surf
->dri_image_front
) {
586 _eglLog(_EGL_WARNING
, "dri2_image_front allocation failed");
595 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
597 struct dri2_egl_display
*dri2_dpy
=
598 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
600 int offset
= 0, fds
[3];
603 if (dri2_surf
->dri_image_back
)
606 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
607 if (!dri2_surf
->buffer
) {
608 _eglLog(_EGL_WARNING
, "Could not get native buffer");
612 num_fds
= get_native_buffer_fds(dri2_surf
->buffer
, fds
);
614 _eglLog(_EGL_WARNING
, "Could not get native buffer FD");
618 fourcc
= get_fourcc(dri2_surf
->buffer
->format
);
620 pitch
= dri2_surf
->buffer
->stride
*
621 get_format_bpp(dri2_surf
->buffer
->format
);
623 if (fourcc
== -1 || pitch
== 0) {
624 _eglLog(_EGL_WARNING
, "Invalid buffer fourcc(%x) or pitch(%d)",
629 dri2_surf
->dri_image_back
=
630 dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
631 dri2_surf
->base
.Width
,
632 dri2_surf
->base
.Height
,
639 if (!dri2_surf
->dri_image_back
) {
640 _eglLog(_EGL_WARNING
, "failed to create DRI image from FD");
643 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
644 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
645 * the spec states that they have a back buffer but no front buffer, in
646 * contrast to pixmaps, which have a front buffer but no back buffer.
648 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
649 * from the spec, following the precedent of Mesa's EGL X11 platform. The
650 * X11 platform correctly assigns pbuffers to single-buffered configs, but
651 * assigns the pbuffer a front buffer instead of a back buffer.
653 * Pbuffers in the X11 platform mostly work today, so let's just copy its
654 * behavior instead of trying to fix (and hence potentially breaking) the
657 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported back buffer for pbuffer surface");
663 /* Some drivers will pass multiple bits in buffer_mask.
664 * For such case, will go through all the bits, and
665 * will not return error when unsupported buffer is requested, only
666 * return error when the allocation for supported buffer failed.
669 droid_image_get_buffers(__DRIdrawable
*driDrawable
,
673 uint32_t buffer_mask
,
674 struct __DRIimageList
*images
)
676 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
678 images
->image_mask
= 0;
679 images
->front
= NULL
;
682 if (update_buffers(dri2_surf
) < 0)
685 if (_eglSurfaceInSharedBufferMode(&dri2_surf
->base
)) {
686 if (get_back_bo(dri2_surf
) < 0)
689 /* We have dri_image_back because this is a window surface and
690 * get_back_bo() succeeded.
692 assert(dri2_surf
->dri_image_back
);
693 images
->back
= dri2_surf
->dri_image_back
;
694 images
->image_mask
|= __DRI_IMAGE_BUFFER_SHARED
;
696 /* There exists no accompanying back nor front buffer. */
700 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
701 if (get_front_bo(dri2_surf
, format
) < 0)
704 if (dri2_surf
->dri_image_front
) {
705 images
->front
= dri2_surf
->dri_image_front
;
706 images
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
710 if (buffer_mask
& __DRI_IMAGE_BUFFER_BACK
) {
711 if (get_back_bo(dri2_surf
) < 0)
714 if (dri2_surf
->dri_image_back
) {
715 images
->back
= dri2_surf
->dri_image_back
;
716 images
->image_mask
|= __DRI_IMAGE_BUFFER_BACK
;
724 droid_query_buffer_age(_EGLDriver
*drv
,
725 _EGLDisplay
*disp
, _EGLSurface
*surface
)
727 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
729 if (update_buffers(dri2_surf
) < 0) {
730 _eglError(EGL_BAD_ALLOC
, "droid_query_buffer_age");
734 return dri2_surf
->back
? dri2_surf
->back
->age
: 0;
738 droid_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
740 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
741 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
742 const bool has_mutable_rb
= _eglSurfaceHasMutableRenderBuffer(draw
);
744 /* From the EGL_KHR_mutable_render_buffer spec (v12):
746 * If surface is a single-buffered window, pixmap, or pbuffer surface
747 * for which there is no pending change to the EGL_RENDER_BUFFER
748 * attribute, eglSwapBuffers has no effect.
750 if (has_mutable_rb
&&
751 draw
->RequestedRenderBuffer
== EGL_SINGLE_BUFFER
&&
752 draw
->ActiveRenderBuffer
== EGL_SINGLE_BUFFER
) {
753 _eglLog(_EGL_DEBUG
, "%s: remain in shared buffer mode", __func__
);
757 for (int i
= 0; i
< dri2_surf
->color_buffers_count
; i
++) {
758 if (dri2_surf
->color_buffers
[i
].age
> 0)
759 dri2_surf
->color_buffers
[i
].age
++;
762 /* "XXX: we don't use get_back_bo() since it causes regressions in
763 * several dEQP tests.
766 dri2_surf
->back
->age
= 1;
768 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
770 /* dri2_surf->buffer can be null even when no error has occured. For
771 * example, if the user has called no GL rendering commands since the
772 * previous eglSwapBuffers, then the driver may have not triggered
773 * a callback to ANativeWindow::dequeueBuffer, in which case
774 * dri2_surf->buffer remains null.
776 if (dri2_surf
->buffer
)
777 droid_window_enqueue_buffer(disp
, dri2_surf
);
779 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
781 /* Update the shared buffer mode */
782 if (has_mutable_rb
&&
783 draw
->ActiveRenderBuffer
!= draw
->RequestedRenderBuffer
) {
784 bool mode
= (draw
->RequestedRenderBuffer
== EGL_SINGLE_BUFFER
);
785 _eglLog(_EGL_DEBUG
, "%s: change to shared buffer mode %d",
788 if (!droid_set_shared_buffer_mode(disp
, draw
, mode
))
790 draw
->ActiveRenderBuffer
= draw
->RequestedRenderBuffer
;
797 droid_create_image_from_prime_fds_yuv(_EGLDisplay
*disp
, _EGLContext
*ctx
,
798 struct ANativeWindowBuffer
*buf
,
799 int num_fds
, int fds
[3])
801 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
802 struct android_ycbcr ycbcr
;
805 enum chroma_order chroma_order
;
809 if (!dri2_dpy
->gralloc
->lock_ycbcr
) {
810 _eglLog(_EGL_WARNING
, "Gralloc does not support lock_ycbcr");
814 memset(&ycbcr
, 0, sizeof(ycbcr
));
815 ret
= dri2_dpy
->gralloc
->lock_ycbcr(dri2_dpy
->gralloc
, buf
->handle
,
816 0, 0, 0, 0, 0, &ycbcr
);
818 /* HACK: See droid_create_image_from_prime_fds() and
819 * https://issuetracker.google.com/32077885.*/
820 if (buf
->format
== HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
)
823 _eglLog(_EGL_WARNING
, "gralloc->lock_ycbcr failed: %d", ret
);
826 dri2_dpy
->gralloc
->unlock(dri2_dpy
->gralloc
, buf
->handle
);
828 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
829 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
830 * so they can be interpreted as offsets. */
831 offsets
[0] = (size_t)ycbcr
.y
;
832 /* We assume here that all the planes are located in one DMA-buf. */
833 if ((size_t)ycbcr
.cr
< (size_t)ycbcr
.cb
) {
834 chroma_order
= YCrCb
;
835 offsets
[1] = (size_t)ycbcr
.cr
;
836 offsets
[2] = (size_t)ycbcr
.cb
;
838 chroma_order
= YCbCr
;
839 offsets
[1] = (size_t)ycbcr
.cb
;
840 offsets
[2] = (size_t)ycbcr
.cr
;
843 /* .ystride is the line length (in bytes) of the Y plane,
844 * .cstride is the line length (in bytes) of any of the remaining
845 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
847 pitches
[0] = ycbcr
.ystride
;
848 pitches
[1] = pitches
[2] = ycbcr
.cstride
;
850 /* .chroma_step is the byte distance between the same chroma channel
851 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
852 fourcc
= get_fourcc_yuv(buf
->format
, chroma_order
, ycbcr
.chroma_step
);
854 _eglLog(_EGL_WARNING
, "unsupported YUV format, native = %x, chroma_order = %s, chroma_step = %d",
855 buf
->format
, chroma_order
== YCbCr
? "YCbCr" : "YCrCb", ycbcr
.chroma_step
);
860 * Since this is EGL_NATIVE_BUFFER_ANDROID don't assume that
861 * the single-fd case cannot happen. So handle eithe single
862 * fd or fd-per-plane case:
865 fds
[2] = fds
[1] = fds
[0];
867 int expected_planes
= (ycbcr
.chroma_step
== 2) ? 2 : 3;
868 assert(num_fds
== expected_planes
);
871 if (ycbcr
.chroma_step
== 2) {
872 /* Semi-planar Y + CbCr or Y + CrCb format. */
873 const EGLint attr_list_2plane
[] = {
874 EGL_WIDTH
, buf
->width
,
875 EGL_HEIGHT
, buf
->height
,
876 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
877 EGL_DMA_BUF_PLANE0_FD_EXT
, fds
[0],
878 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
879 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, offsets
[0],
880 EGL_DMA_BUF_PLANE1_FD_EXT
, fds
[1],
881 EGL_DMA_BUF_PLANE1_PITCH_EXT
, pitches
[1],
882 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, offsets
[1],
886 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_2plane
);
888 /* Fully planar Y + Cb + Cr or Y + Cr + Cb format. */
889 const EGLint attr_list_3plane
[] = {
890 EGL_WIDTH
, buf
->width
,
891 EGL_HEIGHT
, buf
->height
,
892 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
893 EGL_DMA_BUF_PLANE0_FD_EXT
, fds
[0],
894 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
895 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, offsets
[0],
896 EGL_DMA_BUF_PLANE1_FD_EXT
, fds
[1],
897 EGL_DMA_BUF_PLANE1_PITCH_EXT
, pitches
[1],
898 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, offsets
[1],
899 EGL_DMA_BUF_PLANE2_FD_EXT
, fds
[2],
900 EGL_DMA_BUF_PLANE2_PITCH_EXT
, pitches
[2],
901 EGL_DMA_BUF_PLANE2_OFFSET_EXT
, offsets
[2],
905 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_3plane
);
910 droid_create_image_from_prime_fds(_EGLDisplay
*disp
, _EGLContext
*ctx
,
911 struct ANativeWindowBuffer
*buf
, int num_fds
, int fds
[3])
915 if (is_yuv(buf
->format
)) {
918 image
= droid_create_image_from_prime_fds_yuv(disp
, ctx
, buf
, num_fds
, fds
);
920 * HACK: https://issuetracker.google.com/32077885
921 * There is no API available to properly query the IMPLEMENTATION_DEFINED
922 * format. As a workaround we rely here on gralloc allocating either
923 * an arbitrary YCbCr 4:2:0 or RGBX_8888, with the latter being recognized
924 * by lock_ycbcr failing.
926 if (image
|| buf
->format
!= HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
)
931 * Non-YUV formats could *also* have multiple planes, such as ancillary
932 * color compression state buffer, but the rest of the code isn't ready
933 * yet to deal with modifiers:
935 assert(num_fds
== 1);
937 const int fourcc
= get_fourcc(buf
->format
);
939 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
943 pitch
= buf
->stride
* get_format_bpp(buf
->format
);
945 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
949 const EGLint attr_list
[] = {
950 EGL_WIDTH
, buf
->width
,
951 EGL_HEIGHT
, buf
->height
,
952 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
953 EGL_DMA_BUF_PLANE0_FD_EXT
, fds
[0],
954 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitch
,
955 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, 0,
959 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list
);
962 #ifdef HAVE_DRM_GRALLOC
964 droid_create_image_from_name(_EGLDisplay
*disp
, _EGLContext
*ctx
,
965 struct ANativeWindowBuffer
*buf
)
967 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
968 struct dri2_egl_image
*dri2_img
;
972 name
= get_native_buffer_name(buf
);
974 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
978 format
= get_format(buf
->format
);
982 dri2_img
= calloc(1, sizeof(*dri2_img
));
984 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
988 _eglInitImage(&dri2_img
->base
, disp
);
990 dri2_img
->dri_image
=
991 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
998 if (!dri2_img
->dri_image
) {
1000 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
1004 return &dri2_img
->base
;
1006 #endif /* HAVE_DRM_GRALLOC */
1009 droid_query_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1010 EGLint attribute
, EGLint
*value
)
1012 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1013 switch (attribute
) {
1015 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
1016 dri2_surf
->window
->query(dri2_surf
->window
,
1017 NATIVE_WINDOW_DEFAULT_WIDTH
, value
);
1022 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
1023 dri2_surf
->window
->query(dri2_surf
->window
,
1024 NATIVE_WINDOW_DEFAULT_HEIGHT
, value
);
1031 return _eglQuerySurface(drv
, disp
, surf
, attribute
, value
);
1035 dri2_create_image_android_native_buffer(_EGLDisplay
*disp
,
1037 struct ANativeWindowBuffer
*buf
)
1043 /* From the EGL_ANDROID_image_native_buffer spec:
1045 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
1046 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
1048 _eglError(EGL_BAD_CONTEXT
, "eglCreateEGLImageKHR: for "
1049 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
1054 if (!buf
|| buf
->common
.magic
!= ANDROID_NATIVE_BUFFER_MAGIC
||
1055 buf
->common
.version
!= sizeof(*buf
)) {
1056 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
1060 num_fds
= get_native_buffer_fds(buf
, fds
);
1062 return droid_create_image_from_prime_fds(disp
, ctx
, buf
, num_fds
, fds
);
1064 #ifdef HAVE_DRM_GRALLOC
1065 return droid_create_image_from_name(disp
, ctx
, buf
);
1072 droid_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1073 _EGLContext
*ctx
, EGLenum target
,
1074 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1077 case EGL_NATIVE_BUFFER_ANDROID
:
1078 return dri2_create_image_android_native_buffer(disp
, ctx
,
1079 (struct ANativeWindowBuffer
*) buffer
);
1081 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
1086 droid_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
1090 #ifdef HAVE_DRM_GRALLOC
1092 droid_get_buffers_parse_attachments(struct dri2_egl_surface
*dri2_surf
,
1093 unsigned int *attachments
, int count
)
1095 int num_buffers
= 0;
1097 /* fill dri2_surf->buffers */
1098 for (int i
= 0; i
< count
* 2; i
+= 2) {
1099 __DRIbuffer
*buf
, *local
;
1101 assert(num_buffers
< ARRAY_SIZE(dri2_surf
->buffers
));
1102 buf
= &dri2_surf
->buffers
[num_buffers
];
1104 switch (attachments
[i
]) {
1105 case __DRI_BUFFER_BACK_LEFT
:
1106 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
1107 buf
->attachment
= attachments
[i
];
1108 buf
->name
= get_native_buffer_name(dri2_surf
->buffer
);
1109 buf
->cpp
= get_format_bpp(dri2_surf
->buffer
->format
);
1110 buf
->pitch
= dri2_surf
->buffer
->stride
* buf
->cpp
;
1118 /* fall through for pbuffers */
1119 case __DRI_BUFFER_DEPTH
:
1120 case __DRI_BUFFER_STENCIL
:
1121 case __DRI_BUFFER_ACCUM
:
1122 case __DRI_BUFFER_DEPTH_STENCIL
:
1123 case __DRI_BUFFER_HIZ
:
1124 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
,
1125 attachments
[i
], attachments
[i
+ 1]);
1132 case __DRI_BUFFER_FRONT_LEFT
:
1133 case __DRI_BUFFER_FRONT_RIGHT
:
1134 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
1135 case __DRI_BUFFER_FAKE_FRONT_RIGHT
:
1136 case __DRI_BUFFER_BACK_RIGHT
:
1138 /* no front or right buffers */
1146 static __DRIbuffer
*
1147 droid_get_buffers_with_format(__DRIdrawable
* driDrawable
,
1148 int *width
, int *height
,
1149 unsigned int *attachments
, int count
,
1150 int *out_count
, void *loaderPrivate
)
1152 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1154 if (update_buffers(dri2_surf
) < 0)
1157 *out_count
= droid_get_buffers_parse_attachments(dri2_surf
, attachments
, count
);
1160 *width
= dri2_surf
->base
.Width
;
1162 *height
= dri2_surf
->base
.Height
;
1164 return dri2_surf
->buffers
;
1166 #endif /* HAVE_DRM_GRALLOC */
1169 droid_get_capability(void *loaderPrivate
, enum dri_loader_cap cap
)
1171 /* Note: loaderPrivate is _EGLDisplay* */
1173 case DRI_LOADER_CAP_RGBA_ORDERING
:
1181 droid_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1183 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1184 static const struct {
1187 unsigned int rgba_sizes
[4];
1189 { HAL_PIXEL_FORMAT_RGBA_8888
, { 0, 8, 16, 24 }, { 8, 8, 8, 8 } },
1190 { HAL_PIXEL_FORMAT_RGBX_8888
, { 0, 8, 16, -1 }, { 8, 8, 8, 0 } },
1191 { HAL_PIXEL_FORMAT_RGB_565
, { 11, 5, 0, -1 }, { 5, 6, 5, 0 } },
1192 /* This must be after HAL_PIXEL_FORMAT_RGBA_8888, we only keep BGRA
1193 * visual if it turns out RGBA visual is not available.
1195 { HAL_PIXEL_FORMAT_BGRA_8888
, { 16, 8, 0, 24 }, { 8, 8, 8, 8 } },
1198 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1199 int config_count
= 0;
1201 /* The nesting of loops is significant here. Also significant is the order
1202 * of the HAL pixel formats. Many Android apps (such as Google's official
1203 * NDK GLES2 example app), and even portions the core framework code (such
1204 * as SystemServiceManager in Nougat), incorrectly choose their EGLConfig.
1205 * They neglect to match the EGLConfig's EGL_NATIVE_VISUAL_ID against the
1206 * window's native format, and instead choose the first EGLConfig whose
1207 * channel sizes match those of the native window format while ignoring the
1208 * channel *ordering*.
1210 * We can detect such buggy clients in logcat when they call
1211 * eglCreateSurface, by detecting the mismatch between the EGLConfig's
1212 * format and the window's format.
1214 * As a workaround, we generate EGLConfigs such that all EGLConfigs for HAL
1215 * pixel format i precede those for HAL pixel format i+1. In my
1216 * (chadversary) testing on Android Nougat, this was good enough to pacify
1217 * the buggy clients.
1219 bool has_rgba
= false;
1220 for (int i
= 0; i
< ARRAY_SIZE(visuals
); i
++) {
1221 /* Only enable BGRA configs when RGBA is not available. BGRA configs are
1222 * buggy on stock Android.
1224 if (visuals
[i
].format
== HAL_PIXEL_FORMAT_BGRA_8888
&& has_rgba
)
1226 for (int j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
1227 const EGLint surface_type
= EGL_WINDOW_BIT
| EGL_PBUFFER_BIT
;
1229 const EGLint config_attrs
[] = {
1230 EGL_NATIVE_VISUAL_ID
, visuals
[i
].format
,
1231 EGL_NATIVE_VISUAL_TYPE
, visuals
[i
].format
,
1232 EGL_FRAMEBUFFER_TARGET_ANDROID
, EGL_TRUE
,
1233 EGL_RECORDABLE_ANDROID
, EGL_TRUE
,
1237 struct dri2_egl_config
*dri2_conf
=
1238 dri2_add_config(disp
, dri2_dpy
->driver_configs
[j
],
1239 config_count
+ 1, surface_type
, config_attrs
,
1240 visuals
[i
].rgba_shifts
, visuals
[i
].rgba_sizes
);
1242 if (dri2_conf
->base
.ConfigID
== config_count
+ 1)
1247 if (visuals
[i
].format
== HAL_PIXEL_FORMAT_RGBA_8888
&& format_count
[i
])
1251 for (int i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1252 if (!format_count
[i
]) {
1253 _eglLog(_EGL_DEBUG
, "No DRI config supports native format 0x%x",
1258 return (config_count
!= 0);
1261 static const struct dri2_egl_display_vtbl droid_display_vtbl
= {
1262 .authenticate
= NULL
,
1263 .create_window_surface
= droid_create_window_surface
,
1264 .create_pixmap_surface
= dri2_fallback_create_pixmap_surface
,
1265 .create_pbuffer_surface
= droid_create_pbuffer_surface
,
1266 .destroy_surface
= droid_destroy_surface
,
1267 .create_image
= droid_create_image_khr
,
1268 .swap_buffers
= droid_swap_buffers
,
1269 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
, /* Android implements the function */
1270 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1271 .swap_interval
= droid_swap_interval
,
1272 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1273 .copy_buffers
= dri2_fallback_copy_buffers
,
1274 .query_buffer_age
= droid_query_buffer_age
,
1275 .query_surface
= droid_query_surface
,
1276 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1277 .get_sync_values
= dri2_fallback_get_sync_values
,
1278 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1279 .set_shared_buffer_mode
= droid_set_shared_buffer_mode
,
1282 #ifdef HAVE_DRM_GRALLOC
1283 static const __DRIdri2LoaderExtension droid_dri2_loader_extension
= {
1284 .base
= { __DRI_DRI2_LOADER
, 4 },
1287 .flushFrontBuffer
= droid_flush_front_buffer
,
1288 .getBuffersWithFormat
= droid_get_buffers_with_format
,
1289 .getCapability
= droid_get_capability
,
1292 static const __DRIextension
*droid_dri2_loader_extensions
[] = {
1293 &droid_dri2_loader_extension
.base
,
1294 &image_lookup_extension
.base
,
1295 &use_invalidate
.base
,
1296 /* No __DRI_MUTABLE_RENDER_BUFFER_LOADER because it requires
1297 * __DRI_IMAGE_LOADER.
1301 #endif /* HAVE_DRM_GRALLOC */
1303 static const __DRIimageLoaderExtension droid_image_loader_extension
= {
1304 .base
= { __DRI_IMAGE_LOADER
, 2 },
1306 .getBuffers
= droid_image_get_buffers
,
1307 .flushFrontBuffer
= droid_flush_front_buffer
,
1308 .getCapability
= droid_get_capability
,
1312 droid_display_shared_buffer(__DRIdrawable
*driDrawable
, int fence_fd
,
1313 void *loaderPrivate
)
1315 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1316 struct ANativeWindowBuffer
*old_buffer UNUSED
= dri2_surf
->buffer
;
1318 if (!_eglSurfaceInSharedBufferMode(&dri2_surf
->base
)) {
1319 _eglLog(_EGL_WARNING
, "%s: internal error: buffer is not shared",
1324 if (fence_fd
>= 0) {
1325 /* The driver's fence is more recent than the surface's out fence, if it
1326 * exists at all. So use the driver's fence.
1328 if (dri2_surf
->out_fence_fd
>= 0) {
1329 close(dri2_surf
->out_fence_fd
);
1330 dri2_surf
->out_fence_fd
= -1;
1332 } else if (dri2_surf
->out_fence_fd
>= 0) {
1333 fence_fd
= dri2_surf
->out_fence_fd
;
1334 dri2_surf
->out_fence_fd
= -1;
1337 if (dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
1339 _eglLog(_EGL_WARNING
, "%s: ANativeWindow::queueBuffer failed", __func__
);
1346 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
1348 /* Tear down the surface because it no longer has a back buffer. */
1349 struct dri2_egl_display
*dri2_dpy
=
1350 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1352 _eglLog(_EGL_WARNING
, "%s: ANativeWindow::dequeueBuffer failed", __func__
);
1354 dri2_surf
->base
.Lost
= true;
1355 dri2_surf
->buffer
= NULL
;
1356 dri2_surf
->back
= NULL
;
1358 if (dri2_surf
->dri_image_back
) {
1359 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
1360 dri2_surf
->dri_image_back
= NULL
;
1363 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
1370 /* Access to the buffer is controlled by a sync fence. Block on it.
1372 * Ideally, we would submit the fence to the driver, and the driver would
1373 * postpone command execution until it signalled. But DRI lacks API for
1374 * that (as of 2018-04-11).
1376 * SYNC_IOC_WAIT waits forever if timeout < 0
1378 sync_wait(fence_fd
, -1);
1382 static const __DRImutableRenderBufferLoaderExtension droid_mutable_render_buffer_extension
= {
1383 .base
= { __DRI_MUTABLE_RENDER_BUFFER_LOADER
, 1 },
1384 .displaySharedBuffer
= droid_display_shared_buffer
,
1387 static const __DRIextension
*droid_image_loader_extensions
[] = {
1388 &droid_image_loader_extension
.base
,
1389 &image_lookup_extension
.base
,
1390 &use_invalidate
.base
,
1391 &droid_mutable_render_buffer_extension
.base
,
1396 droid_load_driver(_EGLDisplay
*disp
, bool swrast
)
1398 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
1401 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1402 if (dri2_dpy
->driver_name
== NULL
)
1405 #ifdef HAVE_DRM_GRALLOC
1406 /* Handle control nodes using __DRI_DRI2_LOADER extension and GEM names
1407 * for backwards compatibility with drm_gralloc. (Do not use on new
1409 dri2_dpy
->loader_extensions
= droid_dri2_loader_extensions
;
1410 if (!dri2_load_driver(disp
)) {
1411 err
= "DRI2: failed to load driver";
1416 /* Use kms swrast only with vgem / virtio_gpu.
1417 * virtio-gpu fallbacks to software rendering when 3D features
1418 * are unavailable since 6c5ab.
1420 if (strcmp(dri2_dpy
->driver_name
, "vgem") == 0 ||
1421 strcmp(dri2_dpy
->driver_name
, "virtio_gpu") == 0) {
1422 free(dri2_dpy
->driver_name
);
1423 dri2_dpy
->driver_name
= strdup("kms_swrast");
1425 err
= "DRI3: failed to find software capable driver";
1430 dri2_dpy
->loader_extensions
= droid_image_loader_extensions
;
1431 if (!dri2_load_driver_dri3(disp
)) {
1432 err
= "DRI3: failed to load driver";
1440 free(dri2_dpy
->driver_name
);
1441 dri2_dpy
->driver_name
= NULL
;
1446 droid_unload_driver(_EGLDisplay
*disp
)
1448 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1450 dlclose(dri2_dpy
->driver
);
1451 dri2_dpy
->driver
= NULL
;
1452 free(dri2_dpy
->driver_name
);
1453 dri2_dpy
->driver_name
= NULL
;
1457 droid_filter_device(_EGLDisplay
*disp
, int fd
, const char *vendor
)
1459 drmVersionPtr ver
= drmGetVersion(fd
);
1463 if (strcmp(vendor
, ver
->name
) != 0) {
1464 drmFreeVersion(ver
);
1468 drmFreeVersion(ver
);
1473 droid_probe_device(_EGLDisplay
*disp
, bool swrast
)
1475 /* Check that the device is supported, by attempting to:
1476 * - load the dri module
1477 * - and, create a screen
1479 if (!droid_load_driver(disp
, swrast
))
1482 if (!dri2_create_screen(disp
)) {
1483 _eglLog(_EGL_WARNING
, "DRI2: failed to create screen");
1484 droid_unload_driver(disp
);
1490 #ifdef HAVE_DRM_GRALLOC
1492 droid_open_device(_EGLDisplay
*disp
, bool swrast
)
1494 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1495 int fd
= -1, err
= -EINVAL
;
1500 if (dri2_dpy
->gralloc
->perform
)
1501 err
= dri2_dpy
->gralloc
->perform(dri2_dpy
->gralloc
,
1502 GRALLOC_MODULE_PERFORM_GET_DRM_FD
,
1504 if (err
|| fd
< 0) {
1505 _eglLog(_EGL_WARNING
, "fail to get drm fd");
1509 dri2_dpy
->fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
1510 if (dri2_dpy
->fd
< 0)
1513 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
)
1516 return droid_probe_device(disp
, swrast
);
1520 droid_open_device(_EGLDisplay
*disp
, bool swrast
)
1522 #define MAX_DRM_DEVICES 64
1523 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1524 drmDevicePtr device
, devices
[MAX_DRM_DEVICES
] = { NULL
};
1527 char *vendor_name
= NULL
;
1528 char vendor_buf
[PROPERTY_VALUE_MAX
];
1530 #ifdef EGL_FORCE_RENDERNODE
1531 const unsigned node_type
= DRM_NODE_RENDER
;
1533 const unsigned node_type
= swrast
? DRM_NODE_PRIMARY
: DRM_NODE_RENDER
;
1536 if (property_get("drm.gpu.vendor_name", vendor_buf
, NULL
) > 0)
1537 vendor_name
= vendor_buf
;
1539 num_devices
= drmGetDevices2(0, devices
, ARRAY_SIZE(devices
));
1540 if (num_devices
< 0)
1543 for (int i
= 0; i
< num_devices
; i
++) {
1544 device
= devices
[i
];
1546 if (!(device
->available_nodes
& (1 << node_type
)))
1549 dri2_dpy
->fd
= loader_open_device(device
->nodes
[node_type
]);
1550 if (dri2_dpy
->fd
< 0) {
1551 _eglLog(_EGL_WARNING
, "%s() Failed to open DRM device %s",
1552 __func__
, device
->nodes
[node_type
]);
1556 /* If a vendor is explicitly provided, we use only that.
1557 * Otherwise we fall-back the first device that is supported.
1560 if (droid_filter_device(disp
, dri2_dpy
->fd
, vendor_name
)) {
1561 /* Device does not match - try next device */
1562 close(dri2_dpy
->fd
);
1566 /* If the requested device matches - use it. Regardless if
1567 * init fails, do not fall-back to any other device.
1569 if (!droid_probe_device(disp
, false)) {
1570 close(dri2_dpy
->fd
);
1576 if (droid_probe_device(disp
, swrast
))
1579 /* No explicit request - attempt the next device */
1580 close(dri2_dpy
->fd
);
1583 drmFreeDevices(devices
, num_devices
);
1585 if (dri2_dpy
->fd
< 0) {
1586 _eglLog(_EGL_WARNING
, "Failed to open %s DRM device",
1587 vendor_name
? "desired": "any");
1592 #undef MAX_DRM_DEVICES
1598 dri2_initialize_android(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1601 bool device_opened
= false;
1602 struct dri2_egl_display
*dri2_dpy
;
1606 dri2_dpy
= calloc(1, sizeof(*dri2_dpy
));
1608 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1611 ret
= hw_get_module(GRALLOC_HARDWARE_MODULE_ID
,
1612 (const hw_module_t
**)&dri2_dpy
->gralloc
);
1614 err
= "DRI2: failed to get gralloc module";
1618 disp
->DriverData
= (void *) dri2_dpy
;
1619 if (!disp
->Options
.ForceSoftware
)
1620 device_opened
= droid_open_device(disp
, false);
1622 device_opened
= droid_open_device(disp
, true);
1624 if (!device_opened
) {
1625 err
= "DRI2: failed to open device";
1629 dev
= _eglAddDevice(dri2_dpy
->fd
, false);
1631 err
= "DRI2: failed to find EGLDevice";
1637 if (!dri2_setup_extensions(disp
)) {
1638 err
= "DRI2: failed to setup extensions";
1642 dri2_setup_screen(disp
);
1644 /* We set the maximum swap interval as 1 for Android platform, since it is
1645 * the maximum value supported by Android according to the value of
1646 * ANativeWindow::maxSwapInterval.
1648 dri2_setup_swap_interval(disp
, 1);
1650 disp
->Extensions
.ANDROID_framebuffer_target
= EGL_TRUE
;
1651 disp
->Extensions
.ANDROID_image_native_buffer
= EGL_TRUE
;
1652 disp
->Extensions
.ANDROID_recordable
= EGL_TRUE
;
1654 /* Querying buffer age requires a buffer to be dequeued. Without
1655 * EGL_ANDROID_native_fence_sync, dequeue might call eglClientWaitSync and
1656 * result in a deadlock (the lock is already held by eglQuerySurface).
1658 if (disp
->Extensions
.ANDROID_native_fence_sync
) {
1659 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1661 /* disable KHR_partial_update that might have been enabled in
1664 disp
->Extensions
.KHR_partial_update
= EGL_FALSE
;
1667 disp
->Extensions
.KHR_image
= EGL_TRUE
;
1668 #if ANDROID_API_LEVEL >= 24
1669 if (dri2_dpy
->mutable_render_buffer
&&
1670 dri2_dpy
->loader_extensions
== droid_image_loader_extensions
) {
1671 disp
->Extensions
.KHR_mutable_render_buffer
= EGL_TRUE
;
1675 /* Create configs *after* enabling extensions because presence of DRI
1676 * driver extensions can affect the capabilities of EGLConfigs.
1678 if (!droid_add_configs_for_visuals(drv
, disp
)) {
1679 err
= "DRI2: failed to add configs";
1683 /* Fill vtbl last to prevent accidentally calling virtual function during
1686 dri2_dpy
->vtbl
= &droid_display_vtbl
;
1691 dri2_display_destroy(disp
);
1692 return _eglError(EGL_NOT_INITIALIZED
, err
);