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>
43 #include "egl_dri2_fallbacks.h"
45 #ifdef HAVE_DRM_GRALLOC
46 #include <gralloc_drm_handle.h>
47 #include "gralloc_drm.h"
48 #endif /* HAVE_DRM_GRALLOC */
50 #define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1))
52 struct droid_yuv_format
{
54 int native
; /* HAL_PIXEL_FORMAT_ */
55 int is_ycrcb
; /* 0 if chroma order is {Cb, Cr}, 1 if {Cr, Cb} */
56 int chroma_step
; /* Distance in bytes between subsequent chroma pixels. */
59 int fourcc
; /* __DRI_IMAGE_FOURCC_ */
62 /* The following table is used to look up a DRI image FourCC based
63 * on native format and information contained in android_ycbcr struct. */
64 static const struct droid_yuv_format droid_yuv_formats
[] = {
65 /* Native format, YCrCb, Chroma step, DRI image FourCC */
66 { HAL_PIXEL_FORMAT_YCbCr_420_888
, 0, 2, __DRI_IMAGE_FOURCC_NV12
},
67 { HAL_PIXEL_FORMAT_YCbCr_420_888
, 0, 1, __DRI_IMAGE_FOURCC_YUV420
},
68 { HAL_PIXEL_FORMAT_YCbCr_420_888
, 1, 1, __DRI_IMAGE_FOURCC_YVU420
},
69 { HAL_PIXEL_FORMAT_YV12
, 1, 1, __DRI_IMAGE_FOURCC_YVU420
},
70 /* HACK: See droid_create_image_from_prime_fd() and
71 * https://issuetracker.google.com/32077885. */
72 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, 0, 2, __DRI_IMAGE_FOURCC_NV12
},
73 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, 0, 1, __DRI_IMAGE_FOURCC_YUV420
},
74 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, 1, 1, __DRI_IMAGE_FOURCC_YVU420
},
78 get_fourcc_yuv(int native
, int is_ycrcb
, int chroma_step
)
80 for (int i
= 0; i
< ARRAY_SIZE(droid_yuv_formats
); ++i
)
81 if (droid_yuv_formats
[i
].native
== native
&&
82 droid_yuv_formats
[i
].is_ycrcb
== is_ycrcb
&&
83 droid_yuv_formats
[i
].chroma_step
== chroma_step
)
84 return droid_yuv_formats
[i
].fourcc
;
92 for (int i
= 0; i
< ARRAY_SIZE(droid_yuv_formats
); ++i
)
93 if (droid_yuv_formats
[i
].native
== native
)
100 get_format_bpp(int native
)
105 case HAL_PIXEL_FORMAT_RGBA_8888
:
106 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
108 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
109 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
111 case HAL_PIXEL_FORMAT_RGBX_8888
:
112 case HAL_PIXEL_FORMAT_BGRA_8888
:
115 case HAL_PIXEL_FORMAT_RGB_565
:
126 /* createImageFromFds requires fourcc format */
127 static int get_fourcc(int native
)
130 case HAL_PIXEL_FORMAT_RGB_565
: return __DRI_IMAGE_FOURCC_RGB565
;
131 case HAL_PIXEL_FORMAT_BGRA_8888
: return __DRI_IMAGE_FOURCC_ARGB8888
;
132 case HAL_PIXEL_FORMAT_RGBA_8888
: return __DRI_IMAGE_FOURCC_ABGR8888
;
133 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
135 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
136 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
138 case HAL_PIXEL_FORMAT_RGBX_8888
: return __DRI_IMAGE_FOURCC_XBGR8888
;
140 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", native
);
145 static int get_format(int format
)
148 case HAL_PIXEL_FORMAT_BGRA_8888
: return __DRI_IMAGE_FORMAT_ARGB8888
;
149 case HAL_PIXEL_FORMAT_RGB_565
: return __DRI_IMAGE_FORMAT_RGB565
;
150 case HAL_PIXEL_FORMAT_RGBA_8888
: return __DRI_IMAGE_FORMAT_ABGR8888
;
151 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
153 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
154 * TODO: Revert this once https://issuetracker.google.com/32077885 is fixed.
156 case HAL_PIXEL_FORMAT_RGBX_8888
: return __DRI_IMAGE_FORMAT_XBGR8888
;
158 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", format
);
164 get_native_buffer_fd(struct ANativeWindowBuffer
*buf
)
166 native_handle_t
*handle
= (native_handle_t
*)buf
->handle
;
168 * Various gralloc implementations exist, but the dma-buf fd tends
169 * to be first. Access it directly to avoid a dependency on specific
172 return (handle
&& handle
->numFds
) ? handle
->data
[0] : -1;
175 #ifdef HAVE_DRM_GRALLOC
177 get_native_buffer_name(struct ANativeWindowBuffer
*buf
)
179 return gralloc_drm_get_gem_handle(buf
->handle
);
181 #endif /* HAVE_DRM_GRALLOC */
184 droid_window_dequeue_buffer(struct dri2_egl_surface
*dri2_surf
)
188 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
192 /* If access to the buffer is controlled by a sync fence, then block on the
195 * It may be more performant to postpone blocking until there is an
196 * immediate need to write to the buffer. But doing so would require adding
197 * hooks to the DRI2 loader.
199 * From the ANativeWindow::dequeueBuffer documentation:
201 * The libsync fence file descriptor returned in the int pointed to by
202 * the fenceFd argument will refer to the fence that must signal
203 * before the dequeued buffer may be written to. A value of -1
204 * indicates that the caller may access the buffer immediately without
205 * waiting on a fence. If a valid file descriptor is returned (i.e.
206 * any value except -1) then the caller is responsible for closing the
210 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
212 * Waits indefinitely if timeout < 0.
215 sync_wait(fence_fd
, timeout
);
219 dri2_surf
->buffer
->common
.incRef(&dri2_surf
->buffer
->common
);
221 /* Record all the buffers created by ANativeWindow and update back buffer
222 * for updating buffer's age in swap_buffers.
224 EGLBoolean updated
= EGL_FALSE
;
225 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
226 if (!dri2_surf
->color_buffers
[i
].buffer
) {
227 dri2_surf
->color_buffers
[i
].buffer
= dri2_surf
->buffer
;
229 if (dri2_surf
->color_buffers
[i
].buffer
== dri2_surf
->buffer
) {
230 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
237 /* In case of all the buffers were recreated by ANativeWindow, reset
240 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
241 dri2_surf
->color_buffers
[i
].buffer
= NULL
;
242 dri2_surf
->color_buffers
[i
].age
= 0;
244 dri2_surf
->color_buffers
[0].buffer
= dri2_surf
->buffer
;
245 dri2_surf
->back
= &dri2_surf
->color_buffers
[0];
252 droid_window_enqueue_buffer(_EGLDisplay
*disp
, struct dri2_egl_surface
*dri2_surf
)
254 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
256 /* To avoid blocking other EGL calls, release the display mutex before
257 * we enter droid_window_enqueue_buffer() and re-acquire the mutex upon
260 mtx_unlock(&disp
->Mutex
);
262 /* Queue the buffer with stored out fence fd. The ANativeWindow or buffer
263 * consumer may choose to wait for the fence to signal before accessing
264 * it. If fence fd value is -1, buffer can be accessed by consumer
265 * immediately. Consumer or application shouldn't rely on timestamp
266 * associated with fence if the fence fd is -1.
268 * Ownership of fd is transferred to consumer after queueBuffer and the
269 * consumer is responsible for closing it. Caller must not use the fd
270 * after passing it to queueBuffer.
272 int fence_fd
= dri2_surf
->out_fence_fd
;
273 dri2_surf
->out_fence_fd
= -1;
274 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
277 dri2_surf
->buffer
->common
.decRef(&dri2_surf
->buffer
->common
);
278 dri2_surf
->buffer
= NULL
;
279 dri2_surf
->back
= NULL
;
281 mtx_lock(&disp
->Mutex
);
283 if (dri2_surf
->dri_image_back
) {
284 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
285 dri2_surf
->dri_image_back
= NULL
;
292 droid_window_cancel_buffer(struct dri2_egl_surface
*dri2_surf
)
295 int fence_fd
= dri2_surf
->out_fence_fd
;
297 dri2_surf
->out_fence_fd
= -1;
298 ret
= dri2_surf
->window
->cancelBuffer(dri2_surf
->window
,
299 dri2_surf
->buffer
, fence_fd
);
301 _eglLog(_EGL_WARNING
, "ANativeWindow::cancelBuffer failed");
302 dri2_surf
->base
.Lost
= EGL_TRUE
;
307 droid_set_shared_buffer_mode(_EGLDisplay
*disp
, _EGLSurface
*surf
, bool mode
)
309 #if ANDROID_API_LEVEL >= 24
310 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
311 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
312 struct ANativeWindow
*window
= dri2_surf
->window
;
314 assert(surf
->Type
== EGL_WINDOW_BIT
);
315 assert(_eglSurfaceHasMutableRenderBuffer(&dri2_surf
->base
));
317 _eglLog(_EGL_DEBUG
, "%s: mode=%d", __func__
, mode
);
319 if (native_window_set_shared_buffer_mode(window
, mode
)) {
320 _eglLog(_EGL_WARNING
, "failed native_window_set_shared_buffer_mode"
321 "(window=%p, mode=%d)", window
, mode
);
327 _eglLog(_EGL_FATAL
, "%s:%d: internal error: unreachable", __FILE__
, __LINE__
);
333 droid_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
334 _EGLConfig
*conf
, void *native_window
,
335 const EGLint
*attrib_list
)
337 __DRIcreateNewDrawableFunc createNewDrawable
;
338 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
339 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
340 struct dri2_egl_surface
*dri2_surf
;
341 struct ANativeWindow
*window
= native_window
;
342 const __DRIconfig
*config
;
344 dri2_surf
= calloc(1, sizeof *dri2_surf
);
346 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
350 if (!dri2_init_surface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
, true))
351 goto cleanup_surface
;
353 if (type
== EGL_WINDOW_BIT
) {
356 if (window
->common
.magic
!= ANDROID_NATIVE_WINDOW_MAGIC
) {
357 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
358 goto cleanup_surface
;
360 if (window
->query(window
, NATIVE_WINDOW_FORMAT
, &format
)) {
361 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
362 goto cleanup_surface
;
365 if (format
!= dri2_conf
->base
.NativeVisualID
) {
366 _eglLog(_EGL_WARNING
, "Native format mismatch: 0x%x != 0x%x",
367 format
, dri2_conf
->base
.NativeVisualID
);
370 window
->query(window
, NATIVE_WINDOW_WIDTH
, &dri2_surf
->base
.Width
);
371 window
->query(window
, NATIVE_WINDOW_HEIGHT
, &dri2_surf
->base
.Height
);
374 config
= dri2_get_dri_config(dri2_conf
, type
,
375 dri2_surf
->base
.GLColorspace
);
377 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
378 goto cleanup_surface
;
381 if (dri2_dpy
->image_driver
)
382 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
384 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
386 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
388 if (dri2_surf
->dri_drawable
== NULL
) {
389 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
390 goto cleanup_surface
;
394 window
->common
.incRef(&window
->common
);
395 dri2_surf
->window
= window
;
398 return &dri2_surf
->base
;
407 droid_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
408 _EGLConfig
*conf
, void *native_window
,
409 const EGLint
*attrib_list
)
411 return droid_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
412 native_window
, attrib_list
);
416 droid_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
417 _EGLConfig
*conf
, const EGLint
*attrib_list
)
419 return droid_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
424 droid_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
426 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
427 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
429 dri2_egl_surface_free_local_buffers(dri2_surf
);
431 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
432 if (dri2_surf
->buffer
)
433 droid_window_cancel_buffer(dri2_surf
);
435 dri2_surf
->window
->common
.decRef(&dri2_surf
->window
->common
);
438 if (dri2_surf
->dri_image_back
) {
439 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_back", __func__
, __LINE__
);
440 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
441 dri2_surf
->dri_image_back
= NULL
;
444 if (dri2_surf
->dri_image_front
) {
445 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_front", __func__
, __LINE__
);
446 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_front
);
447 dri2_surf
->dri_image_front
= NULL
;
450 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
452 dri2_fini_surface(surf
);
459 droid_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
460 _EGLSurface
*surf
, EGLint interval
)
462 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
463 struct ANativeWindow
*window
= dri2_surf
->window
;
465 if (window
->setSwapInterval(window
, interval
))
468 surf
->SwapInterval
= interval
;
473 update_buffers(struct dri2_egl_surface
*dri2_surf
)
475 if (dri2_surf
->base
.Lost
)
478 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
481 /* try to dequeue the next back buffer */
482 if (!dri2_surf
->buffer
&& !droid_window_dequeue_buffer(dri2_surf
)) {
483 _eglLog(_EGL_WARNING
, "Could not dequeue buffer from native window");
484 dri2_surf
->base
.Lost
= EGL_TRUE
;
488 /* free outdated buffers and update the surface size */
489 if (dri2_surf
->base
.Width
!= dri2_surf
->buffer
->width
||
490 dri2_surf
->base
.Height
!= dri2_surf
->buffer
->height
) {
491 dri2_egl_surface_free_local_buffers(dri2_surf
);
492 dri2_surf
->base
.Width
= dri2_surf
->buffer
->width
;
493 dri2_surf
->base
.Height
= dri2_surf
->buffer
->height
;
500 get_front_bo(struct dri2_egl_surface
*dri2_surf
, unsigned int format
)
502 struct dri2_egl_display
*dri2_dpy
=
503 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
505 if (dri2_surf
->dri_image_front
)
508 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
509 /* According current EGL spec, front buffer rendering
510 * for window surface is not supported now.
511 * and mesa doesn't have the implementation of this case.
512 * Add warning message, but not treat it as error.
514 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported front buffer for window surface");
515 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
516 dri2_surf
->dri_image_front
=
517 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
518 dri2_surf
->base
.Width
,
519 dri2_surf
->base
.Height
,
523 if (!dri2_surf
->dri_image_front
) {
524 _eglLog(_EGL_WARNING
, "dri2_image_front allocation failed");
533 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
535 struct dri2_egl_display
*dri2_dpy
=
536 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
540 if (dri2_surf
->dri_image_back
)
543 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
544 if (!dri2_surf
->buffer
) {
545 _eglLog(_EGL_WARNING
, "Could not get native buffer");
549 fd
= get_native_buffer_fd(dri2_surf
->buffer
);
551 _eglLog(_EGL_WARNING
, "Could not get native buffer FD");
555 fourcc
= get_fourcc(dri2_surf
->buffer
->format
);
557 pitch
= dri2_surf
->buffer
->stride
*
558 get_format_bpp(dri2_surf
->buffer
->format
);
560 if (fourcc
== -1 || pitch
== 0) {
561 _eglLog(_EGL_WARNING
, "Invalid buffer fourcc(%x) or pitch(%d)",
566 dri2_surf
->dri_image_back
=
567 dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
568 dri2_surf
->base
.Width
,
569 dri2_surf
->base
.Height
,
576 if (!dri2_surf
->dri_image_back
) {
577 _eglLog(_EGL_WARNING
, "failed to create DRI image from FD");
580 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
581 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
582 * the spec states that they have a back buffer but no front buffer, in
583 * contrast to pixmaps, which have a front buffer but no back buffer.
585 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
586 * from the spec, following the precedent of Mesa's EGL X11 platform. The
587 * X11 platform correctly assigns pbuffers to single-buffered configs, but
588 * assigns the pbuffer a front buffer instead of a back buffer.
590 * Pbuffers in the X11 platform mostly work today, so let's just copy its
591 * behavior instead of trying to fix (and hence potentially breaking) the
594 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported back buffer for pbuffer surface");
600 /* Some drivers will pass multiple bits in buffer_mask.
601 * For such case, will go through all the bits, and
602 * will not return error when unsupported buffer is requested, only
603 * return error when the allocation for supported buffer failed.
606 droid_image_get_buffers(__DRIdrawable
*driDrawable
,
610 uint32_t buffer_mask
,
611 struct __DRIimageList
*images
)
613 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
615 images
->image_mask
= 0;
616 images
->front
= NULL
;
619 if (update_buffers(dri2_surf
) < 0)
622 if (_eglSurfaceInSharedBufferMode(&dri2_surf
->base
)) {
623 if (get_back_bo(dri2_surf
) < 0)
626 /* We have dri_image_back because this is a window surface and
627 * get_back_bo() succeeded.
629 assert(dri2_surf
->dri_image_back
);
630 images
->back
= dri2_surf
->dri_image_back
;
631 images
->image_mask
|= __DRI_IMAGE_BUFFER_SHARED
;
633 /* There exists no accompanying back nor front buffer. */
637 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
638 if (get_front_bo(dri2_surf
, format
) < 0)
641 if (dri2_surf
->dri_image_front
) {
642 images
->front
= dri2_surf
->dri_image_front
;
643 images
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
647 if (buffer_mask
& __DRI_IMAGE_BUFFER_BACK
) {
648 if (get_back_bo(dri2_surf
) < 0)
651 if (dri2_surf
->dri_image_back
) {
652 images
->back
= dri2_surf
->dri_image_back
;
653 images
->image_mask
|= __DRI_IMAGE_BUFFER_BACK
;
661 droid_query_buffer_age(_EGLDriver
*drv
,
662 _EGLDisplay
*disp
, _EGLSurface
*surface
)
664 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
666 if (update_buffers(dri2_surf
) < 0) {
667 _eglError(EGL_BAD_ALLOC
, "droid_query_buffer_age");
671 return dri2_surf
->back
? dri2_surf
->back
->age
: 0;
675 droid_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
677 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
678 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
680 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
683 const bool has_mutable_rb
= _eglSurfaceHasMutableRenderBuffer(draw
);
685 /* From the EGL_KHR_mutable_render_buffer spec (v12):
687 * If surface is a single-buffered window, pixmap, or pbuffer surface
688 * for which there is no pending change to the EGL_RENDER_BUFFER
689 * attribute, eglSwapBuffers has no effect.
691 if (has_mutable_rb
&&
692 draw
->RequestedRenderBuffer
== EGL_SINGLE_BUFFER
&&
693 draw
->ActiveRenderBuffer
== EGL_SINGLE_BUFFER
) {
694 _eglLog(_EGL_DEBUG
, "%s: remain in shared buffer mode", __func__
);
698 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
699 if (dri2_surf
->color_buffers
[i
].age
> 0)
700 dri2_surf
->color_buffers
[i
].age
++;
703 /* "XXX: we don't use get_back_bo() since it causes regressions in
704 * several dEQP tests.
707 dri2_surf
->back
->age
= 1;
709 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
711 /* dri2_surf->buffer can be null even when no error has occured. For
712 * example, if the user has called no GL rendering commands since the
713 * previous eglSwapBuffers, then the driver may have not triggered
714 * a callback to ANativeWindow::dequeueBuffer, in which case
715 * dri2_surf->buffer remains null.
717 if (dri2_surf
->buffer
)
718 droid_window_enqueue_buffer(disp
, dri2_surf
);
720 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
722 /* Update the shared buffer mode */
723 if (has_mutable_rb
&&
724 draw
->ActiveRenderBuffer
!= draw
->RequestedRenderBuffer
) {
725 bool mode
= (draw
->RequestedRenderBuffer
== EGL_SINGLE_BUFFER
);
726 _eglLog(_EGL_DEBUG
, "%s: change to shared buffer mode %d",
729 if (!droid_set_shared_buffer_mode(disp
, draw
, mode
))
731 draw
->ActiveRenderBuffer
= draw
->RequestedRenderBuffer
;
737 #if ANDROID_API_LEVEL >= 23
739 droid_set_damage_region(_EGLDriver
*drv
,
741 _EGLSurface
*draw
, const EGLint
* rects
, EGLint n_rects
)
743 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
744 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
745 android_native_rect_t
* droid_rects
= NULL
;
751 droid_rects
= malloc(n_rects
* sizeof(android_native_rect_t
));
752 if (droid_rects
== NULL
)
753 return _eglError(EGL_BAD_ALLOC
, "eglSetDamageRegionKHR");
755 for (EGLint num_drects
= 0; num_drects
< n_rects
; num_drects
++) {
756 EGLint i
= num_drects
* 4;
757 droid_rects
[num_drects
].left
= rects
[i
];
758 droid_rects
[num_drects
].bottom
= rects
[i
+ 1];
759 droid_rects
[num_drects
].right
= rects
[i
] + rects
[i
+ 2];
760 droid_rects
[num_drects
].top
= rects
[i
+ 1] + rects
[i
+ 3];
764 * XXX/TODO: Need to check for other return values
767 ret
= native_window_set_surface_damage(dri2_surf
->window
, droid_rects
, n_rects
);
770 return ret
== 0 ? EGL_TRUE
: EGL_FALSE
;
775 droid_create_image_from_prime_fd_yuv(_EGLDisplay
*disp
, _EGLContext
*ctx
,
776 struct ANativeWindowBuffer
*buf
, int fd
)
778 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
779 struct android_ycbcr ycbcr
;
786 if (!dri2_dpy
->gralloc
->lock_ycbcr
) {
787 _eglLog(_EGL_WARNING
, "Gralloc does not support lock_ycbcr");
791 memset(&ycbcr
, 0, sizeof(ycbcr
));
792 ret
= dri2_dpy
->gralloc
->lock_ycbcr(dri2_dpy
->gralloc
, buf
->handle
,
793 0, 0, 0, 0, 0, &ycbcr
);
795 /* HACK: See droid_create_image_from_prime_fd() and
796 * https://issuetracker.google.com/32077885.*/
797 if (buf
->format
== HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
)
800 _eglLog(_EGL_WARNING
, "gralloc->lock_ycbcr failed: %d", ret
);
803 dri2_dpy
->gralloc
->unlock(dri2_dpy
->gralloc
, buf
->handle
);
805 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
806 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
807 * so they can be interpreted as offsets. */
808 offsets
[0] = (size_t)ycbcr
.y
;
809 /* We assume here that all the planes are located in one DMA-buf. */
810 is_ycrcb
= (size_t)ycbcr
.cr
< (size_t)ycbcr
.cb
;
812 offsets
[1] = (size_t)ycbcr
.cr
;
813 offsets
[2] = (size_t)ycbcr
.cb
;
815 offsets
[1] = (size_t)ycbcr
.cb
;
816 offsets
[2] = (size_t)ycbcr
.cr
;
819 /* .ystride is the line length (in bytes) of the Y plane,
820 * .cstride is the line length (in bytes) of any of the remaining
821 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
823 pitches
[0] = ycbcr
.ystride
;
824 pitches
[1] = pitches
[2] = ycbcr
.cstride
;
826 /* .chroma_step is the byte distance between the same chroma channel
827 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
828 fourcc
= get_fourcc_yuv(buf
->format
, is_ycrcb
, ycbcr
.chroma_step
);
830 _eglLog(_EGL_WARNING
, "unsupported YUV format, native = %x, is_ycrcb = %d, chroma_step = %d",
831 buf
->format
, is_ycrcb
, ycbcr
.chroma_step
);
835 if (ycbcr
.chroma_step
== 2) {
836 /* Semi-planar Y + CbCr or Y + CrCb format. */
837 const EGLint attr_list_2plane
[] = {
838 EGL_WIDTH
, buf
->width
,
839 EGL_HEIGHT
, buf
->height
,
840 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
841 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
842 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
843 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, offsets
[0],
844 EGL_DMA_BUF_PLANE1_FD_EXT
, fd
,
845 EGL_DMA_BUF_PLANE1_PITCH_EXT
, pitches
[1],
846 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, offsets
[1],
850 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_2plane
);
852 /* Fully planar Y + Cb + Cr or Y + Cr + Cb format. */
853 const EGLint attr_list_3plane
[] = {
854 EGL_WIDTH
, buf
->width
,
855 EGL_HEIGHT
, buf
->height
,
856 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
857 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
858 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
859 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, offsets
[0],
860 EGL_DMA_BUF_PLANE1_FD_EXT
, fd
,
861 EGL_DMA_BUF_PLANE1_PITCH_EXT
, pitches
[1],
862 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, offsets
[1],
863 EGL_DMA_BUF_PLANE2_FD_EXT
, fd
,
864 EGL_DMA_BUF_PLANE2_PITCH_EXT
, pitches
[2],
865 EGL_DMA_BUF_PLANE2_OFFSET_EXT
, offsets
[2],
869 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_3plane
);
874 droid_create_image_from_prime_fd(_EGLDisplay
*disp
, _EGLContext
*ctx
,
875 struct ANativeWindowBuffer
*buf
, int fd
)
879 if (is_yuv(buf
->format
)) {
882 image
= droid_create_image_from_prime_fd_yuv(disp
, ctx
, buf
, fd
);
884 * HACK: https://issuetracker.google.com/32077885
885 * There is no API available to properly query the IMPLEMENTATION_DEFINED
886 * format. As a workaround we rely here on gralloc allocating either
887 * an arbitrary YCbCr 4:2:0 or RGBX_8888, with the latter being recognized
888 * by lock_ycbcr failing.
890 if (image
|| buf
->format
!= HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
)
894 const int fourcc
= get_fourcc(buf
->format
);
896 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
900 pitch
= buf
->stride
* get_format_bpp(buf
->format
);
902 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
906 const EGLint attr_list
[] = {
907 EGL_WIDTH
, buf
->width
,
908 EGL_HEIGHT
, buf
->height
,
909 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
910 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
911 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitch
,
912 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, 0,
916 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list
);
919 #ifdef HAVE_DRM_GRALLOC
921 droid_create_image_from_name(_EGLDisplay
*disp
, _EGLContext
*ctx
,
922 struct ANativeWindowBuffer
*buf
)
924 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
925 struct dri2_egl_image
*dri2_img
;
929 name
= get_native_buffer_name(buf
);
931 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
935 format
= get_format(buf
->format
);
939 dri2_img
= calloc(1, sizeof(*dri2_img
));
941 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
945 _eglInitImage(&dri2_img
->base
, disp
);
947 dri2_img
->dri_image
=
948 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
955 if (!dri2_img
->dri_image
) {
957 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
961 return &dri2_img
->base
;
963 #endif /* HAVE_DRM_GRALLOC */
966 droid_query_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
967 EGLint attribute
, EGLint
*value
)
969 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
972 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
973 dri2_surf
->window
->query(dri2_surf
->window
,
974 NATIVE_WINDOW_DEFAULT_WIDTH
, value
);
979 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
980 dri2_surf
->window
->query(dri2_surf
->window
,
981 NATIVE_WINDOW_DEFAULT_HEIGHT
, value
);
988 return _eglQuerySurface(drv
, dpy
, surf
, attribute
, value
);
992 dri2_create_image_android_native_buffer(_EGLDisplay
*disp
,
994 struct ANativeWindowBuffer
*buf
)
999 /* From the EGL_ANDROID_image_native_buffer spec:
1001 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
1002 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
1004 _eglError(EGL_BAD_CONTEXT
, "eglCreateEGLImageKHR: for "
1005 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
1010 if (!buf
|| buf
->common
.magic
!= ANDROID_NATIVE_BUFFER_MAGIC
||
1011 buf
->common
.version
!= sizeof(*buf
)) {
1012 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
1016 fd
= get_native_buffer_fd(buf
);
1018 return droid_create_image_from_prime_fd(disp
, ctx
, buf
, fd
);
1020 #ifdef HAVE_DRM_GRALLOC
1021 return droid_create_image_from_name(disp
, ctx
, buf
);
1028 droid_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1029 _EGLContext
*ctx
, EGLenum target
,
1030 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1033 case EGL_NATIVE_BUFFER_ANDROID
:
1034 return dri2_create_image_android_native_buffer(disp
, ctx
,
1035 (struct ANativeWindowBuffer
*) buffer
);
1037 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
1042 droid_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
1046 #ifdef HAVE_DRM_GRALLOC
1048 droid_get_buffers_parse_attachments(struct dri2_egl_surface
*dri2_surf
,
1049 unsigned int *attachments
, int count
)
1051 int num_buffers
= 0;
1053 /* fill dri2_surf->buffers */
1054 for (int i
= 0; i
< count
* 2; i
+= 2) {
1055 __DRIbuffer
*buf
, *local
;
1057 assert(num_buffers
< ARRAY_SIZE(dri2_surf
->buffers
));
1058 buf
= &dri2_surf
->buffers
[num_buffers
];
1060 switch (attachments
[i
]) {
1061 case __DRI_BUFFER_BACK_LEFT
:
1062 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
1063 buf
->attachment
= attachments
[i
];
1064 buf
->name
= get_native_buffer_name(dri2_surf
->buffer
);
1065 buf
->cpp
= get_format_bpp(dri2_surf
->buffer
->format
);
1066 buf
->pitch
= dri2_surf
->buffer
->stride
* buf
->cpp
;
1074 /* fall through for pbuffers */
1075 case __DRI_BUFFER_DEPTH
:
1076 case __DRI_BUFFER_STENCIL
:
1077 case __DRI_BUFFER_ACCUM
:
1078 case __DRI_BUFFER_DEPTH_STENCIL
:
1079 case __DRI_BUFFER_HIZ
:
1080 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
,
1081 attachments
[i
], attachments
[i
+ 1]);
1088 case __DRI_BUFFER_FRONT_LEFT
:
1089 case __DRI_BUFFER_FRONT_RIGHT
:
1090 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
1091 case __DRI_BUFFER_FAKE_FRONT_RIGHT
:
1092 case __DRI_BUFFER_BACK_RIGHT
:
1094 /* no front or right buffers */
1102 static __DRIbuffer
*
1103 droid_get_buffers_with_format(__DRIdrawable
* driDrawable
,
1104 int *width
, int *height
,
1105 unsigned int *attachments
, int count
,
1106 int *out_count
, void *loaderPrivate
)
1108 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1110 if (update_buffers(dri2_surf
) < 0)
1113 *out_count
= droid_get_buffers_parse_attachments(dri2_surf
, attachments
, count
);
1116 *width
= dri2_surf
->base
.Width
;
1118 *height
= dri2_surf
->base
.Height
;
1120 return dri2_surf
->buffers
;
1122 #endif /* HAVE_DRM_GRALLOC */
1125 droid_get_capability(void *loaderPrivate
, enum dri_loader_cap cap
)
1127 /* Note: loaderPrivate is _EGLDisplay* */
1129 case DRI_LOADER_CAP_RGBA_ORDERING
:
1137 droid_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
1139 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1140 static const struct {
1142 unsigned int rgba_masks
[4];
1144 { HAL_PIXEL_FORMAT_RGBA_8888
, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 } },
1145 { HAL_PIXEL_FORMAT_RGBX_8888
, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 } },
1146 { HAL_PIXEL_FORMAT_RGB_565
, { 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 } },
1147 { HAL_PIXEL_FORMAT_BGRA_8888
, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 } },
1150 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1151 int config_count
= 0;
1153 /* The nesting of loops is significant here. Also significant is the order
1154 * of the HAL pixel formats. Many Android apps (such as Google's official
1155 * NDK GLES2 example app), and even portions the core framework code (such
1156 * as SystemServiceManager in Nougat), incorrectly choose their EGLConfig.
1157 * They neglect to match the EGLConfig's EGL_NATIVE_VISUAL_ID against the
1158 * window's native format, and instead choose the first EGLConfig whose
1159 * channel sizes match those of the native window format while ignoring the
1160 * channel *ordering*.
1162 * We can detect such buggy clients in logcat when they call
1163 * eglCreateSurface, by detecting the mismatch between the EGLConfig's
1164 * format and the window's format.
1166 * As a workaround, we generate EGLConfigs such that all EGLConfigs for HAL
1167 * pixel format i precede those for HAL pixel format i+1. In my
1168 * (chadversary) testing on Android Nougat, this was good enough to pacify
1169 * the buggy clients.
1171 for (int i
= 0; i
< ARRAY_SIZE(visuals
); i
++) {
1172 for (int j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
1173 const EGLint surface_type
= EGL_WINDOW_BIT
| EGL_PBUFFER_BIT
;
1175 const EGLint config_attrs
[] = {
1176 EGL_NATIVE_VISUAL_ID
, visuals
[i
].format
,
1177 EGL_NATIVE_VISUAL_TYPE
, visuals
[i
].format
,
1178 EGL_FRAMEBUFFER_TARGET_ANDROID
, EGL_TRUE
,
1179 EGL_RECORDABLE_ANDROID
, EGL_TRUE
,
1183 struct dri2_egl_config
*dri2_conf
=
1184 dri2_add_config(dpy
, dri2_dpy
->driver_configs
[j
],
1185 config_count
+ 1, surface_type
, config_attrs
,
1186 visuals
[i
].rgba_masks
);
1188 if (dri2_conf
->base
.ConfigID
== config_count
+ 1)
1195 for (int i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1196 if (!format_count
[i
]) {
1197 _eglLog(_EGL_DEBUG
, "No DRI config supports native format 0x%x",
1202 return (config_count
!= 0);
1205 #ifdef HAVE_DRM_GRALLOC
1207 droid_open_device_drm_gralloc(struct dri2_egl_display
*dri2_dpy
)
1209 int fd
= -1, err
= -EINVAL
;
1211 if (dri2_dpy
->gralloc
->perform
)
1212 err
= dri2_dpy
->gralloc
->perform(dri2_dpy
->gralloc
,
1213 GRALLOC_MODULE_PERFORM_GET_DRM_FD
,
1215 if (err
|| fd
< 0) {
1216 _eglLog(_EGL_WARNING
, "fail to get drm fd");
1220 return (fd
>= 0) ? fcntl(fd
, F_DUPFD_CLOEXEC
, 3) : -1;
1222 #endif /* HAVE_DRM_GRALLOC */
1224 static const struct dri2_egl_display_vtbl droid_display_vtbl
= {
1225 .authenticate
= NULL
,
1226 .create_window_surface
= droid_create_window_surface
,
1227 .create_pixmap_surface
= dri2_fallback_create_pixmap_surface
,
1228 .create_pbuffer_surface
= droid_create_pbuffer_surface
,
1229 .destroy_surface
= droid_destroy_surface
,
1230 .create_image
= droid_create_image_khr
,
1231 .swap_buffers
= droid_swap_buffers
,
1232 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
, /* Android implements the function */
1233 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1234 .swap_interval
= droid_swap_interval
,
1235 #if ANDROID_API_LEVEL >= 23
1236 .set_damage_region
= droid_set_damage_region
,
1238 .set_damage_region
= dri2_fallback_set_damage_region
,
1240 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1241 .copy_buffers
= dri2_fallback_copy_buffers
,
1242 .query_buffer_age
= droid_query_buffer_age
,
1243 .query_surface
= droid_query_surface
,
1244 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1245 .get_sync_values
= dri2_fallback_get_sync_values
,
1246 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1247 .set_shared_buffer_mode
= droid_set_shared_buffer_mode
,
1250 #ifdef HAVE_DRM_GRALLOC
1251 static const __DRIdri2LoaderExtension droid_dri2_loader_extension
= {
1252 .base
= { __DRI_DRI2_LOADER
, 4 },
1255 .flushFrontBuffer
= droid_flush_front_buffer
,
1256 .getBuffersWithFormat
= droid_get_buffers_with_format
,
1257 .getCapability
= droid_get_capability
,
1259 #endif /* HAVE_DRM_GRALLOC */
1261 static const __DRIimageLoaderExtension droid_image_loader_extension
= {
1262 .base
= { __DRI_IMAGE_LOADER
, 2 },
1264 .getBuffers
= droid_image_get_buffers
,
1265 .flushFrontBuffer
= droid_flush_front_buffer
,
1266 .getCapability
= droid_get_capability
,
1269 #ifdef HAVE_DRM_GRALLOC
1270 static const __DRIextension
*droid_dri2_loader_extensions
[] = {
1271 &droid_dri2_loader_extension
.base
,
1272 &image_lookup_extension
.base
,
1273 &use_invalidate
.base
,
1274 /* No __DRI_MUTABLE_RENDER_BUFFER_LOADER because it requires
1275 * __DRI_IMAGE_LOADER.
1279 #endif /* HAVE_DRM_GRALLOC */
1282 droid_display_shared_buffer(__DRIdrawable
*driDrawable
, int fence_fd
,
1283 void *loaderPrivate
)
1285 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1286 struct ANativeWindowBuffer
*old_buffer UNUSED
= dri2_surf
->buffer
;
1288 if (!_eglSurfaceInSharedBufferMode(&dri2_surf
->base
)) {
1289 _eglLog(_EGL_WARNING
, "%s: internal error: buffer is not shared",
1294 if (fence_fd
>= 0) {
1295 /* The driver's fence is more recent than the surface's out fence, if it
1296 * exists at all. So use the driver's fence.
1298 if (dri2_surf
->out_fence_fd
>= 0) {
1299 close(dri2_surf
->out_fence_fd
);
1300 dri2_surf
->out_fence_fd
= -1;
1302 } else if (dri2_surf
->out_fence_fd
>= 0) {
1303 fence_fd
= dri2_surf
->out_fence_fd
;
1304 dri2_surf
->out_fence_fd
= -1;
1307 if (dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
1309 _eglLog(_EGL_WARNING
, "%s: ANativeWindow::queueBuffer failed", __func__
);
1316 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
1318 /* Tear down the surface because it no longer has a back buffer. */
1319 struct dri2_egl_display
*dri2_dpy
=
1320 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1322 _eglLog(_EGL_WARNING
, "%s: ANativeWindow::dequeueBuffer failed", __func__
);
1324 dri2_surf
->base
.Lost
= true;
1325 dri2_surf
->buffer
= NULL
;
1326 dri2_surf
->back
= NULL
;
1328 if (dri2_surf
->dri_image_back
) {
1329 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
1330 dri2_surf
->dri_image_back
= NULL
;
1333 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
1340 /* Access to the buffer is controlled by a sync fence. Block on it.
1342 * Ideally, we would submit the fence to the driver, and the driver would
1343 * postpone command execution until it signalled. But DRI lacks API for
1344 * that (as of 2018-04-11).
1346 * SYNC_IOC_WAIT waits forever if timeout < 0
1348 sync_wait(fence_fd
, -1);
1352 static const __DRImutableRenderBufferLoaderExtension droid_mutable_render_buffer_extension
= {
1353 .base
= { __DRI_MUTABLE_RENDER_BUFFER_LOADER
, 1 },
1354 .displaySharedBuffer
= droid_display_shared_buffer
,
1357 static const __DRIextension
*droid_image_loader_extensions
[] = {
1358 &droid_image_loader_extension
.base
,
1359 &image_lookup_extension
.base
,
1360 &use_invalidate
.base
,
1361 &droid_mutable_render_buffer_extension
.base
,
1366 droid_load_driver(_EGLDisplay
*disp
)
1368 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
1371 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1372 if (dri2_dpy
->driver_name
== NULL
)
1375 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1377 if (!dri2_dpy
->is_render_node
) {
1378 #ifdef HAVE_DRM_GRALLOC
1379 /* Handle control nodes using __DRI_DRI2_LOADER extension and GEM names
1380 * for backwards compatibility with drm_gralloc. (Do not use on new
1382 dri2_dpy
->loader_extensions
= droid_dri2_loader_extensions
;
1383 if (!dri2_load_driver(disp
)) {
1384 err
= "DRI2: failed to load driver";
1388 err
= "DRI2: handle is not for a render node";
1392 dri2_dpy
->loader_extensions
= droid_image_loader_extensions
;
1393 if (!dri2_load_driver_dri3(disp
)) {
1394 err
= "DRI3: failed to load driver";
1402 free(dri2_dpy
->driver_name
);
1403 dri2_dpy
->driver_name
= NULL
;
1408 droid_probe_driver(int fd
)
1412 driver_name
= loader_get_driver_for_fd(fd
);
1413 if (driver_name
== NULL
)
1423 probe_filtered_out
= 1,
1427 droid_probe_device(_EGLDisplay
*disp
, int fd
, const char *vendor
)
1431 drmVersionPtr ver
= drmGetVersion(fd
);
1440 if (vendor
&& strcmp(vendor
, ver
->name
) != 0) {
1441 ret
= probe_filtered_out
;
1445 if (!droid_probe_driver(fd
)) {
1450 ret
= probe_success
;
1453 drmFreeVersion(ver
);
1458 droid_open_device(_EGLDisplay
*disp
)
1460 #define MAX_DRM_DEVICES 32
1461 drmDevicePtr device
, devices
[MAX_DRM_DEVICES
] = { NULL
};
1462 int prop_set
, num_devices
;
1463 int fd
= -1, fallback_fd
= -1;
1465 char *vendor_name
= NULL
;
1466 char vendor_buf
[PROPERTY_VALUE_MAX
];
1468 if (property_get("drm.gpu.vendor_name", vendor_buf
, NULL
) > 0)
1469 vendor_name
= vendor_buf
;
1471 num_devices
= drmGetDevices2(0, devices
, ARRAY_SIZE(devices
));
1472 if (num_devices
< 0)
1475 for (int i
= 0; i
< num_devices
; i
++) {
1476 device
= devices
[i
];
1478 if (!(device
->available_nodes
& (1 << DRM_NODE_RENDER
)))
1481 fd
= loader_open_device(device
->nodes
[DRM_NODE_RENDER
]);
1483 _eglLog(_EGL_WARNING
, "%s() Failed to open DRM device %s",
1484 __func__
, device
->nodes
[DRM_NODE_RENDER
]);
1488 int ret
= droid_probe_device(disp
, fd
, vendor_name
);
1492 case probe_filtered_out
:
1493 /* Set as fallback */
1494 if (fallback_fd
== -1)
1501 if (fallback_fd
!= fd
)
1507 drmFreeDevices(devices
, num_devices
);
1509 if (fallback_fd
< 0 && fd
< 0) {
1510 _eglLog(_EGL_WARNING
, "Failed to open any DRM device");
1515 _eglLog(_EGL_WARNING
, "Failed to open desired DRM device, using fallback");
1521 #undef MAX_DRM_DEVICES
1525 dri2_initialize_android(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1527 struct dri2_egl_display
*dri2_dpy
;
1531 /* Not supported yet */
1532 if (disp
->Options
.ForceSoftware
)
1535 loader_set_logger(_eglLog
);
1537 dri2_dpy
= calloc(1, sizeof(*dri2_dpy
));
1539 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1542 ret
= hw_get_module(GRALLOC_HARDWARE_MODULE_ID
,
1543 (const hw_module_t
**)&dri2_dpy
->gralloc
);
1545 err
= "DRI2: failed to get gralloc module";
1549 disp
->DriverData
= (void *) dri2_dpy
;
1551 #ifdef HAVE_DRM_GRALLOC
1552 dri2_dpy
->fd
= droid_open_device_drm_gralloc(dri2_dpy
);
1554 dri2_dpy
->fd
= droid_open_device(disp
);
1556 if (dri2_dpy
->fd
< 0) {
1557 err
= "DRI2: failed to open device";
1561 if (!droid_load_driver(disp
)) {
1562 err
= "DRI2: failed to load driver";
1566 if (!dri2_create_screen(disp
)) {
1567 err
= "DRI2: failed to create screen";
1571 if (!dri2_setup_extensions(disp
)) {
1572 err
= "DRI2: failed to setup extensions";
1576 dri2_setup_screen(disp
);
1578 /* We set the maximum swap interval as 1 for Android platform, since it is
1579 * the maximum value supported by Android according to the value of
1580 * ANativeWindow::maxSwapInterval.
1582 dri2_setup_swap_interval(disp
, 1);
1584 disp
->Extensions
.ANDROID_framebuffer_target
= EGL_TRUE
;
1585 disp
->Extensions
.ANDROID_image_native_buffer
= EGL_TRUE
;
1586 disp
->Extensions
.ANDROID_recordable
= EGL_TRUE
;
1587 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1588 #if ANDROID_API_LEVEL >= 23
1589 disp
->Extensions
.KHR_partial_update
= EGL_TRUE
;
1591 disp
->Extensions
.KHR_image
= EGL_TRUE
;
1592 #if ANDROID_API_LEVEL >= 24
1593 if (dri2_dpy
->mutable_render_buffer
&&
1594 dri2_dpy
->loader_extensions
== droid_image_loader_extensions
) {
1595 disp
->Extensions
.KHR_mutable_render_buffer
= EGL_TRUE
;
1599 /* Create configs *after* enabling extensions because presence of DRI
1600 * driver extensions can affect the capabilities of EGLConfigs.
1602 if (!droid_add_configs_for_visuals(drv
, disp
)) {
1603 err
= "DRI2: failed to add configs";
1607 /* Fill vtbl last to prevent accidentally calling virtual function during
1610 dri2_dpy
->vtbl
= &droid_display_vtbl
;
1615 dri2_display_destroy(disp
);
1616 return _eglError(EGL_NOT_INITIALIZED
, err
);