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
},
75 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, 1, 1, __DRI_IMAGE_FOURCC_AYUV
},
79 get_fourcc_yuv(int native
, int is_ycrcb
, int chroma_step
)
81 for (int i
= 0; i
< ARRAY_SIZE(droid_yuv_formats
); ++i
)
82 if (droid_yuv_formats
[i
].native
== native
&&
83 droid_yuv_formats
[i
].is_ycrcb
== is_ycrcb
&&
84 droid_yuv_formats
[i
].chroma_step
== chroma_step
)
85 return droid_yuv_formats
[i
].fourcc
;
93 for (int i
= 0; i
< ARRAY_SIZE(droid_yuv_formats
); ++i
)
94 if (droid_yuv_formats
[i
].native
== native
)
101 get_format_bpp(int native
)
106 case HAL_PIXEL_FORMAT_RGBA_8888
:
107 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
109 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
110 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
112 case HAL_PIXEL_FORMAT_RGBX_8888
:
113 case HAL_PIXEL_FORMAT_BGRA_8888
:
116 case HAL_PIXEL_FORMAT_RGB_565
:
127 /* createImageFromFds requires fourcc format */
128 static int get_fourcc(int native
)
131 case HAL_PIXEL_FORMAT_RGB_565
: return __DRI_IMAGE_FOURCC_RGB565
;
132 case HAL_PIXEL_FORMAT_BGRA_8888
: return __DRI_IMAGE_FOURCC_ARGB8888
;
133 case HAL_PIXEL_FORMAT_RGBA_8888
: return __DRI_IMAGE_FOURCC_ABGR8888
;
134 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
136 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
137 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
139 case HAL_PIXEL_FORMAT_RGBX_8888
: return __DRI_IMAGE_FOURCC_XBGR8888
;
141 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", native
);
146 static int get_format(int format
)
149 case HAL_PIXEL_FORMAT_BGRA_8888
: return __DRI_IMAGE_FORMAT_ARGB8888
;
150 case HAL_PIXEL_FORMAT_RGB_565
: return __DRI_IMAGE_FORMAT_RGB565
;
151 case HAL_PIXEL_FORMAT_RGBA_8888
: return __DRI_IMAGE_FORMAT_ABGR8888
;
152 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
154 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
155 * TODO: Revert this once https://issuetracker.google.com/32077885 is fixed.
157 case HAL_PIXEL_FORMAT_RGBX_8888
: return __DRI_IMAGE_FORMAT_XBGR8888
;
159 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", format
);
165 get_native_buffer_fd(struct ANativeWindowBuffer
*buf
)
167 native_handle_t
*handle
= (native_handle_t
*)buf
->handle
;
169 * Various gralloc implementations exist, but the dma-buf fd tends
170 * to be first. Access it directly to avoid a dependency on specific
173 return (handle
&& handle
->numFds
) ? handle
->data
[0] : -1;
176 #ifdef HAVE_DRM_GRALLOC
178 get_native_buffer_name(struct ANativeWindowBuffer
*buf
)
180 return gralloc_drm_get_gem_handle(buf
->handle
);
182 #endif /* HAVE_DRM_GRALLOC */
185 droid_window_dequeue_buffer(struct dri2_egl_surface
*dri2_surf
)
189 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
193 /* If access to the buffer is controlled by a sync fence, then block on the
196 * It may be more performant to postpone blocking until there is an
197 * immediate need to write to the buffer. But doing so would require adding
198 * hooks to the DRI2 loader.
200 * From the ANativeWindow::dequeueBuffer documentation:
202 * The libsync fence file descriptor returned in the int pointed to by
203 * the fenceFd argument will refer to the fence that must signal
204 * before the dequeued buffer may be written to. A value of -1
205 * indicates that the caller may access the buffer immediately without
206 * waiting on a fence. If a valid file descriptor is returned (i.e.
207 * any value except -1) then the caller is responsible for closing the
211 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
213 * Waits indefinitely if timeout < 0.
216 sync_wait(fence_fd
, timeout
);
220 dri2_surf
->buffer
->common
.incRef(&dri2_surf
->buffer
->common
);
222 /* Record all the buffers created by ANativeWindow and update back buffer
223 * for updating buffer's age in swap_buffers.
225 EGLBoolean updated
= EGL_FALSE
;
226 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
227 if (!dri2_surf
->color_buffers
[i
].buffer
) {
228 dri2_surf
->color_buffers
[i
].buffer
= dri2_surf
->buffer
;
230 if (dri2_surf
->color_buffers
[i
].buffer
== dri2_surf
->buffer
) {
231 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
238 /* In case of all the buffers were recreated by ANativeWindow, reset
241 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
242 dri2_surf
->color_buffers
[i
].buffer
= NULL
;
243 dri2_surf
->color_buffers
[i
].age
= 0;
245 dri2_surf
->color_buffers
[0].buffer
= dri2_surf
->buffer
;
246 dri2_surf
->back
= &dri2_surf
->color_buffers
[0];
253 droid_window_enqueue_buffer(_EGLDisplay
*disp
, struct dri2_egl_surface
*dri2_surf
)
255 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
257 /* To avoid blocking other EGL calls, release the display mutex before
258 * we enter droid_window_enqueue_buffer() and re-acquire the mutex upon
261 mtx_unlock(&disp
->Mutex
);
263 /* Queue the buffer with stored out fence fd. The ANativeWindow or buffer
264 * consumer may choose to wait for the fence to signal before accessing
265 * it. If fence fd value is -1, buffer can be accessed by consumer
266 * immediately. Consumer or application shouldn't rely on timestamp
267 * associated with fence if the fence fd is -1.
269 * Ownership of fd is transferred to consumer after queueBuffer and the
270 * consumer is responsible for closing it. Caller must not use the fd
271 * after passing it to queueBuffer.
273 int fence_fd
= dri2_surf
->out_fence_fd
;
274 dri2_surf
->out_fence_fd
= -1;
275 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
278 dri2_surf
->buffer
->common
.decRef(&dri2_surf
->buffer
->common
);
279 dri2_surf
->buffer
= NULL
;
280 dri2_surf
->back
= NULL
;
282 mtx_lock(&disp
->Mutex
);
284 if (dri2_surf
->dri_image_back
) {
285 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
286 dri2_surf
->dri_image_back
= NULL
;
293 droid_window_cancel_buffer(struct dri2_egl_surface
*dri2_surf
)
296 int fence_fd
= dri2_surf
->out_fence_fd
;
298 dri2_surf
->out_fence_fd
= -1;
299 ret
= dri2_surf
->window
->cancelBuffer(dri2_surf
->window
,
300 dri2_surf
->buffer
, fence_fd
);
302 _eglLog(_EGL_WARNING
, "ANativeWindow::cancelBuffer failed");
303 dri2_surf
->base
.Lost
= EGL_TRUE
;
308 droid_set_shared_buffer_mode(_EGLDisplay
*disp
, _EGLSurface
*surf
, bool mode
)
310 #if ANDROID_API_LEVEL >= 24
311 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
312 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
313 struct ANativeWindow
*window
= dri2_surf
->window
;
315 assert(surf
->Type
== EGL_WINDOW_BIT
);
316 assert(_eglSurfaceHasMutableRenderBuffer(&dri2_surf
->base
));
318 _eglLog(_EGL_DEBUG
, "%s: mode=%d", __func__
, mode
);
320 if (native_window_set_shared_buffer_mode(window
, mode
)) {
321 _eglLog(_EGL_WARNING
, "failed native_window_set_shared_buffer_mode"
322 "(window=%p, mode=%d)", window
, mode
);
328 _eglLog(_EGL_FATAL
, "%s:%d: internal error: unreachable", __FILE__
, __LINE__
);
334 droid_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
335 _EGLConfig
*conf
, void *native_window
,
336 const EGLint
*attrib_list
)
338 __DRIcreateNewDrawableFunc createNewDrawable
;
339 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
340 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
341 struct dri2_egl_surface
*dri2_surf
;
342 struct ANativeWindow
*window
= native_window
;
343 const __DRIconfig
*config
;
345 dri2_surf
= calloc(1, sizeof *dri2_surf
);
347 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
351 if (!dri2_init_surface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
, true))
352 goto cleanup_surface
;
354 if (type
== EGL_WINDOW_BIT
) {
357 if (window
->common
.magic
!= ANDROID_NATIVE_WINDOW_MAGIC
) {
358 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
359 goto cleanup_surface
;
361 if (window
->query(window
, NATIVE_WINDOW_FORMAT
, &format
)) {
362 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
363 goto cleanup_surface
;
366 if (format
!= dri2_conf
->base
.NativeVisualID
) {
367 _eglLog(_EGL_WARNING
, "Native format mismatch: 0x%x != 0x%x",
368 format
, dri2_conf
->base
.NativeVisualID
);
371 window
->query(window
, NATIVE_WINDOW_WIDTH
, &dri2_surf
->base
.Width
);
372 window
->query(window
, NATIVE_WINDOW_HEIGHT
, &dri2_surf
->base
.Height
);
375 config
= dri2_get_dri_config(dri2_conf
, type
,
376 dri2_surf
->base
.GLColorspace
);
378 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
379 goto cleanup_surface
;
382 if (dri2_dpy
->image_driver
)
383 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
385 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
387 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
389 if (dri2_surf
->dri_drawable
== NULL
) {
390 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
391 goto cleanup_surface
;
395 window
->common
.incRef(&window
->common
);
396 dri2_surf
->window
= window
;
399 return &dri2_surf
->base
;
408 droid_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
409 _EGLConfig
*conf
, void *native_window
,
410 const EGLint
*attrib_list
)
412 return droid_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
413 native_window
, attrib_list
);
417 droid_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
418 _EGLConfig
*conf
, const EGLint
*attrib_list
)
420 return droid_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
425 droid_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
427 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
428 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
430 dri2_egl_surface_free_local_buffers(dri2_surf
);
432 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
433 if (dri2_surf
->buffer
)
434 droid_window_cancel_buffer(dri2_surf
);
436 dri2_surf
->window
->common
.decRef(&dri2_surf
->window
->common
);
439 if (dri2_surf
->dri_image_back
) {
440 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_back", __func__
, __LINE__
);
441 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
442 dri2_surf
->dri_image_back
= NULL
;
445 if (dri2_surf
->dri_image_front
) {
446 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_front", __func__
, __LINE__
);
447 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_front
);
448 dri2_surf
->dri_image_front
= NULL
;
451 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
453 dri2_fini_surface(surf
);
460 droid_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
,
461 _EGLSurface
*surf
, EGLint interval
)
463 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
464 struct ANativeWindow
*window
= dri2_surf
->window
;
466 if (window
->setSwapInterval(window
, interval
))
469 surf
->SwapInterval
= interval
;
474 update_buffers(struct dri2_egl_surface
*dri2_surf
)
476 if (dri2_surf
->base
.Lost
)
479 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
482 /* try to dequeue the next back buffer */
483 if (!dri2_surf
->buffer
&& !droid_window_dequeue_buffer(dri2_surf
)) {
484 _eglLog(_EGL_WARNING
, "Could not dequeue buffer from native window");
485 dri2_surf
->base
.Lost
= EGL_TRUE
;
489 /* free outdated buffers and update the surface size */
490 if (dri2_surf
->base
.Width
!= dri2_surf
->buffer
->width
||
491 dri2_surf
->base
.Height
!= dri2_surf
->buffer
->height
) {
492 dri2_egl_surface_free_local_buffers(dri2_surf
);
493 dri2_surf
->base
.Width
= dri2_surf
->buffer
->width
;
494 dri2_surf
->base
.Height
= dri2_surf
->buffer
->height
;
501 get_front_bo(struct dri2_egl_surface
*dri2_surf
, unsigned int format
)
503 struct dri2_egl_display
*dri2_dpy
=
504 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
506 if (dri2_surf
->dri_image_front
)
509 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
510 /* According current EGL spec, front buffer rendering
511 * for window surface is not supported now.
512 * and mesa doesn't have the implementation of this case.
513 * Add warning message, but not treat it as error.
515 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported front buffer for window surface");
516 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
517 dri2_surf
->dri_image_front
=
518 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
519 dri2_surf
->base
.Width
,
520 dri2_surf
->base
.Height
,
524 if (!dri2_surf
->dri_image_front
) {
525 _eglLog(_EGL_WARNING
, "dri2_image_front allocation failed");
534 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
536 struct dri2_egl_display
*dri2_dpy
=
537 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
541 if (dri2_surf
->dri_image_back
)
544 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
545 if (!dri2_surf
->buffer
) {
546 _eglLog(_EGL_WARNING
, "Could not get native buffer");
550 fd
= get_native_buffer_fd(dri2_surf
->buffer
);
552 _eglLog(_EGL_WARNING
, "Could not get native buffer FD");
556 fourcc
= get_fourcc(dri2_surf
->buffer
->format
);
558 pitch
= dri2_surf
->buffer
->stride
*
559 get_format_bpp(dri2_surf
->buffer
->format
);
561 if (fourcc
== -1 || pitch
== 0) {
562 _eglLog(_EGL_WARNING
, "Invalid buffer fourcc(%x) or pitch(%d)",
567 dri2_surf
->dri_image_back
=
568 dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
569 dri2_surf
->base
.Width
,
570 dri2_surf
->base
.Height
,
577 if (!dri2_surf
->dri_image_back
) {
578 _eglLog(_EGL_WARNING
, "failed to create DRI image from FD");
581 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
582 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
583 * the spec states that they have a back buffer but no front buffer, in
584 * contrast to pixmaps, which have a front buffer but no back buffer.
586 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
587 * from the spec, following the precedent of Mesa's EGL X11 platform. The
588 * X11 platform correctly assigns pbuffers to single-buffered configs, but
589 * assigns the pbuffer a front buffer instead of a back buffer.
591 * Pbuffers in the X11 platform mostly work today, so let's just copy its
592 * behavior instead of trying to fix (and hence potentially breaking) the
595 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported back buffer for pbuffer surface");
601 /* Some drivers will pass multiple bits in buffer_mask.
602 * For such case, will go through all the bits, and
603 * will not return error when unsupported buffer is requested, only
604 * return error when the allocation for supported buffer failed.
607 droid_image_get_buffers(__DRIdrawable
*driDrawable
,
611 uint32_t buffer_mask
,
612 struct __DRIimageList
*images
)
614 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
616 images
->image_mask
= 0;
617 images
->front
= NULL
;
620 if (update_buffers(dri2_surf
) < 0)
623 if (_eglSurfaceInSharedBufferMode(&dri2_surf
->base
)) {
624 if (get_back_bo(dri2_surf
) < 0)
627 /* We have dri_image_back because this is a window surface and
628 * get_back_bo() succeeded.
630 assert(dri2_surf
->dri_image_back
);
631 images
->back
= dri2_surf
->dri_image_back
;
632 images
->image_mask
|= __DRI_IMAGE_BUFFER_SHARED
;
634 /* There exists no accompanying back nor front buffer. */
638 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
639 if (get_front_bo(dri2_surf
, format
) < 0)
642 if (dri2_surf
->dri_image_front
) {
643 images
->front
= dri2_surf
->dri_image_front
;
644 images
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
648 if (buffer_mask
& __DRI_IMAGE_BUFFER_BACK
) {
649 if (get_back_bo(dri2_surf
) < 0)
652 if (dri2_surf
->dri_image_back
) {
653 images
->back
= dri2_surf
->dri_image_back
;
654 images
->image_mask
|= __DRI_IMAGE_BUFFER_BACK
;
662 droid_query_buffer_age(_EGLDriver
*drv
,
663 _EGLDisplay
*disp
, _EGLSurface
*surface
)
665 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
667 if (update_buffers(dri2_surf
) < 0) {
668 _eglError(EGL_BAD_ALLOC
, "droid_query_buffer_age");
672 return dri2_surf
->back
? dri2_surf
->back
->age
: 0;
676 droid_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
678 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
679 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
680 const bool has_mutable_rb
= _eglSurfaceHasMutableRenderBuffer(draw
);
682 /* From the EGL_KHR_mutable_render_buffer spec (v12):
684 * If surface is a single-buffered window, pixmap, or pbuffer surface
685 * for which there is no pending change to the EGL_RENDER_BUFFER
686 * attribute, eglSwapBuffers has no effect.
688 if (has_mutable_rb
&&
689 draw
->RequestedRenderBuffer
== EGL_SINGLE_BUFFER
&&
690 draw
->ActiveRenderBuffer
== EGL_SINGLE_BUFFER
) {
691 _eglLog(_EGL_DEBUG
, "%s: remain in shared buffer mode", __func__
);
695 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
696 if (dri2_surf
->color_buffers
[i
].age
> 0)
697 dri2_surf
->color_buffers
[i
].age
++;
700 /* "XXX: we don't use get_back_bo() since it causes regressions in
701 * several dEQP tests.
704 dri2_surf
->back
->age
= 1;
706 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
708 /* dri2_surf->buffer can be null even when no error has occured. For
709 * example, if the user has called no GL rendering commands since the
710 * previous eglSwapBuffers, then the driver may have not triggered
711 * a callback to ANativeWindow::dequeueBuffer, in which case
712 * dri2_surf->buffer remains null.
714 if (dri2_surf
->buffer
)
715 droid_window_enqueue_buffer(disp
, dri2_surf
);
717 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
719 /* Update the shared buffer mode */
720 if (has_mutable_rb
&&
721 draw
->ActiveRenderBuffer
!= draw
->RequestedRenderBuffer
) {
722 bool mode
= (draw
->RequestedRenderBuffer
== EGL_SINGLE_BUFFER
);
723 _eglLog(_EGL_DEBUG
, "%s: change to shared buffer mode %d",
726 if (!droid_set_shared_buffer_mode(disp
, draw
, mode
))
728 draw
->ActiveRenderBuffer
= draw
->RequestedRenderBuffer
;
734 #if ANDROID_API_LEVEL >= 23
736 droid_set_damage_region(_EGLDriver
*drv
,
738 _EGLSurface
*draw
, const EGLint
* rects
, EGLint n_rects
)
740 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
741 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
742 android_native_rect_t
* droid_rects
= NULL
;
748 droid_rects
= malloc(n_rects
* sizeof(android_native_rect_t
));
749 if (droid_rects
== NULL
)
750 return _eglError(EGL_BAD_ALLOC
, "eglSetDamageRegionKHR");
752 for (EGLint num_drects
= 0; num_drects
< n_rects
; num_drects
++) {
753 EGLint i
= num_drects
* 4;
754 droid_rects
[num_drects
].left
= rects
[i
];
755 droid_rects
[num_drects
].bottom
= rects
[i
+ 1];
756 droid_rects
[num_drects
].right
= rects
[i
] + rects
[i
+ 2];
757 droid_rects
[num_drects
].top
= rects
[i
+ 1] + rects
[i
+ 3];
761 * XXX/TODO: Need to check for other return values
764 ret
= native_window_set_surface_damage(dri2_surf
->window
, droid_rects
, n_rects
);
767 return ret
== 0 ? EGL_TRUE
: EGL_FALSE
;
772 droid_create_image_from_prime_fd_yuv(_EGLDisplay
*disp
, _EGLContext
*ctx
,
773 struct ANativeWindowBuffer
*buf
, int fd
)
775 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
776 struct android_ycbcr ycbcr
;
783 if (!dri2_dpy
->gralloc
->lock_ycbcr
) {
784 _eglLog(_EGL_WARNING
, "Gralloc does not support lock_ycbcr");
788 memset(&ycbcr
, 0, sizeof(ycbcr
));
789 ret
= dri2_dpy
->gralloc
->lock_ycbcr(dri2_dpy
->gralloc
, buf
->handle
,
790 0, 0, 0, 0, 0, &ycbcr
);
792 /* HACK: See droid_create_image_from_prime_fd() and
793 * https://issuetracker.google.com/32077885.*/
794 if (buf
->format
== HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
)
797 _eglLog(_EGL_WARNING
, "gralloc->lock_ycbcr failed: %d", ret
);
800 dri2_dpy
->gralloc
->unlock(dri2_dpy
->gralloc
, buf
->handle
);
802 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
803 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
804 * so they can be interpreted as offsets. */
805 offsets
[0] = (size_t)ycbcr
.y
;
806 /* We assume here that all the planes are located in one DMA-buf. */
807 is_ycrcb
= (size_t)ycbcr
.cr
< (size_t)ycbcr
.cb
;
809 offsets
[1] = (size_t)ycbcr
.cr
;
810 offsets
[2] = (size_t)ycbcr
.cb
;
812 offsets
[1] = (size_t)ycbcr
.cb
;
813 offsets
[2] = (size_t)ycbcr
.cr
;
816 /* .ystride is the line length (in bytes) of the Y plane,
817 * .cstride is the line length (in bytes) of any of the remaining
818 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
820 pitches
[0] = ycbcr
.ystride
;
821 pitches
[1] = pitches
[2] = ycbcr
.cstride
;
823 /* .chroma_step is the byte distance between the same chroma channel
824 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
825 fourcc
= get_fourcc_yuv(buf
->format
, is_ycrcb
, ycbcr
.chroma_step
);
827 _eglLog(_EGL_WARNING
, "unsupported YUV format, native = %x, is_ycrcb = %d, chroma_step = %d",
828 buf
->format
, is_ycrcb
, ycbcr
.chroma_step
);
832 if (ycbcr
.chroma_step
== 2) {
833 /* Semi-planar Y + CbCr or Y + CrCb format. */
834 const EGLint attr_list_2plane
[] = {
835 EGL_WIDTH
, buf
->width
,
836 EGL_HEIGHT
, buf
->height
,
837 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
838 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
839 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
840 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, offsets
[0],
841 EGL_DMA_BUF_PLANE1_FD_EXT
, fd
,
842 EGL_DMA_BUF_PLANE1_PITCH_EXT
, pitches
[1],
843 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, offsets
[1],
847 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_2plane
);
849 /* Fully planar Y + Cb + Cr or Y + Cr + Cb format. */
850 const EGLint attr_list_3plane
[] = {
851 EGL_WIDTH
, buf
->width
,
852 EGL_HEIGHT
, buf
->height
,
853 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
854 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
855 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
856 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, offsets
[0],
857 EGL_DMA_BUF_PLANE1_FD_EXT
, fd
,
858 EGL_DMA_BUF_PLANE1_PITCH_EXT
, pitches
[1],
859 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, offsets
[1],
860 EGL_DMA_BUF_PLANE2_FD_EXT
, fd
,
861 EGL_DMA_BUF_PLANE2_PITCH_EXT
, pitches
[2],
862 EGL_DMA_BUF_PLANE2_OFFSET_EXT
, offsets
[2],
866 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_3plane
);
871 droid_create_image_from_prime_fd(_EGLDisplay
*disp
, _EGLContext
*ctx
,
872 struct ANativeWindowBuffer
*buf
, int fd
)
876 if (is_yuv(buf
->format
)) {
879 image
= droid_create_image_from_prime_fd_yuv(disp
, ctx
, buf
, fd
);
881 * HACK: https://issuetracker.google.com/32077885
882 * There is no API available to properly query the IMPLEMENTATION_DEFINED
883 * format. As a workaround we rely here on gralloc allocating either
884 * an arbitrary YCbCr 4:2:0 or RGBX_8888, with the latter being recognized
885 * by lock_ycbcr failing.
887 if (image
|| buf
->format
!= HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
)
891 const int fourcc
= get_fourcc(buf
->format
);
893 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
897 pitch
= buf
->stride
* get_format_bpp(buf
->format
);
899 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
903 const EGLint attr_list
[] = {
904 EGL_WIDTH
, buf
->width
,
905 EGL_HEIGHT
, buf
->height
,
906 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
907 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
908 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitch
,
909 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, 0,
913 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list
);
916 #ifdef HAVE_DRM_GRALLOC
918 droid_create_image_from_name(_EGLDisplay
*disp
, _EGLContext
*ctx
,
919 struct ANativeWindowBuffer
*buf
)
921 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
922 struct dri2_egl_image
*dri2_img
;
926 name
= get_native_buffer_name(buf
);
928 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
932 format
= get_format(buf
->format
);
936 dri2_img
= calloc(1, sizeof(*dri2_img
));
938 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
942 _eglInitImage(&dri2_img
->base
, disp
);
944 dri2_img
->dri_image
=
945 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
952 if (!dri2_img
->dri_image
) {
954 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
958 return &dri2_img
->base
;
960 #endif /* HAVE_DRM_GRALLOC */
963 droid_query_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
964 EGLint attribute
, EGLint
*value
)
966 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
969 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
970 dri2_surf
->window
->query(dri2_surf
->window
,
971 NATIVE_WINDOW_DEFAULT_WIDTH
, value
);
976 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
977 dri2_surf
->window
->query(dri2_surf
->window
,
978 NATIVE_WINDOW_DEFAULT_HEIGHT
, value
);
985 return _eglQuerySurface(drv
, disp
, surf
, attribute
, value
);
989 dri2_create_image_android_native_buffer(_EGLDisplay
*disp
,
991 struct ANativeWindowBuffer
*buf
)
996 /* From the EGL_ANDROID_image_native_buffer spec:
998 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
999 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
1001 _eglError(EGL_BAD_CONTEXT
, "eglCreateEGLImageKHR: for "
1002 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
1007 if (!buf
|| buf
->common
.magic
!= ANDROID_NATIVE_BUFFER_MAGIC
||
1008 buf
->common
.version
!= sizeof(*buf
)) {
1009 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
1013 fd
= get_native_buffer_fd(buf
);
1015 return droid_create_image_from_prime_fd(disp
, ctx
, buf
, fd
);
1017 #ifdef HAVE_DRM_GRALLOC
1018 return droid_create_image_from_name(disp
, ctx
, buf
);
1025 droid_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1026 _EGLContext
*ctx
, EGLenum target
,
1027 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1030 case EGL_NATIVE_BUFFER_ANDROID
:
1031 return dri2_create_image_android_native_buffer(disp
, ctx
,
1032 (struct ANativeWindowBuffer
*) buffer
);
1034 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
1039 droid_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
1043 #ifdef HAVE_DRM_GRALLOC
1045 droid_get_buffers_parse_attachments(struct dri2_egl_surface
*dri2_surf
,
1046 unsigned int *attachments
, int count
)
1048 int num_buffers
= 0;
1050 /* fill dri2_surf->buffers */
1051 for (int i
= 0; i
< count
* 2; i
+= 2) {
1052 __DRIbuffer
*buf
, *local
;
1054 assert(num_buffers
< ARRAY_SIZE(dri2_surf
->buffers
));
1055 buf
= &dri2_surf
->buffers
[num_buffers
];
1057 switch (attachments
[i
]) {
1058 case __DRI_BUFFER_BACK_LEFT
:
1059 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
1060 buf
->attachment
= attachments
[i
];
1061 buf
->name
= get_native_buffer_name(dri2_surf
->buffer
);
1062 buf
->cpp
= get_format_bpp(dri2_surf
->buffer
->format
);
1063 buf
->pitch
= dri2_surf
->buffer
->stride
* buf
->cpp
;
1071 /* fall through for pbuffers */
1072 case __DRI_BUFFER_DEPTH
:
1073 case __DRI_BUFFER_STENCIL
:
1074 case __DRI_BUFFER_ACCUM
:
1075 case __DRI_BUFFER_DEPTH_STENCIL
:
1076 case __DRI_BUFFER_HIZ
:
1077 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
,
1078 attachments
[i
], attachments
[i
+ 1]);
1085 case __DRI_BUFFER_FRONT_LEFT
:
1086 case __DRI_BUFFER_FRONT_RIGHT
:
1087 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
1088 case __DRI_BUFFER_FAKE_FRONT_RIGHT
:
1089 case __DRI_BUFFER_BACK_RIGHT
:
1091 /* no front or right buffers */
1099 static __DRIbuffer
*
1100 droid_get_buffers_with_format(__DRIdrawable
* driDrawable
,
1101 int *width
, int *height
,
1102 unsigned int *attachments
, int count
,
1103 int *out_count
, void *loaderPrivate
)
1105 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1107 if (update_buffers(dri2_surf
) < 0)
1110 *out_count
= droid_get_buffers_parse_attachments(dri2_surf
, attachments
, count
);
1113 *width
= dri2_surf
->base
.Width
;
1115 *height
= dri2_surf
->base
.Height
;
1117 return dri2_surf
->buffers
;
1119 #endif /* HAVE_DRM_GRALLOC */
1122 droid_get_capability(void *loaderPrivate
, enum dri_loader_cap cap
)
1124 /* Note: loaderPrivate is _EGLDisplay* */
1126 case DRI_LOADER_CAP_RGBA_ORDERING
:
1134 droid_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1136 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1137 static const struct {
1139 unsigned int rgba_masks
[4];
1141 { HAL_PIXEL_FORMAT_RGBA_8888
, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 } },
1142 { HAL_PIXEL_FORMAT_RGBX_8888
, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 } },
1143 { HAL_PIXEL_FORMAT_RGB_565
, { 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 } },
1144 { HAL_PIXEL_FORMAT_BGRA_8888
, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 } },
1147 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1148 int config_count
= 0;
1150 /* The nesting of loops is significant here. Also significant is the order
1151 * of the HAL pixel formats. Many Android apps (such as Google's official
1152 * NDK GLES2 example app), and even portions the core framework code (such
1153 * as SystemServiceManager in Nougat), incorrectly choose their EGLConfig.
1154 * They neglect to match the EGLConfig's EGL_NATIVE_VISUAL_ID against the
1155 * window's native format, and instead choose the first EGLConfig whose
1156 * channel sizes match those of the native window format while ignoring the
1157 * channel *ordering*.
1159 * We can detect such buggy clients in logcat when they call
1160 * eglCreateSurface, by detecting the mismatch between the EGLConfig's
1161 * format and the window's format.
1163 * As a workaround, we generate EGLConfigs such that all EGLConfigs for HAL
1164 * pixel format i precede those for HAL pixel format i+1. In my
1165 * (chadversary) testing on Android Nougat, this was good enough to pacify
1166 * the buggy clients.
1168 for (int i
= 0; i
< ARRAY_SIZE(visuals
); i
++) {
1169 for (int j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
1170 const EGLint surface_type
= EGL_WINDOW_BIT
| EGL_PBUFFER_BIT
;
1172 const EGLint config_attrs
[] = {
1173 EGL_NATIVE_VISUAL_ID
, visuals
[i
].format
,
1174 EGL_NATIVE_VISUAL_TYPE
, visuals
[i
].format
,
1175 EGL_FRAMEBUFFER_TARGET_ANDROID
, EGL_TRUE
,
1176 EGL_RECORDABLE_ANDROID
, EGL_TRUE
,
1180 struct dri2_egl_config
*dri2_conf
=
1181 dri2_add_config(disp
, dri2_dpy
->driver_configs
[j
],
1182 config_count
+ 1, surface_type
, config_attrs
,
1183 visuals
[i
].rgba_masks
);
1185 if (dri2_conf
->base
.ConfigID
== config_count
+ 1)
1192 for (int i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1193 if (!format_count
[i
]) {
1194 _eglLog(_EGL_DEBUG
, "No DRI config supports native format 0x%x",
1199 return (config_count
!= 0);
1203 droid_probe_device(_EGLDisplay
*disp
);
1205 #ifdef HAVE_DRM_GRALLOC
1207 droid_open_device_drm_gralloc(_EGLDisplay
*disp
)
1209 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1210 int fd
= -1, err
= -EINVAL
;
1212 if (dri2_dpy
->gralloc
->perform
)
1213 err
= dri2_dpy
->gralloc
->perform(dri2_dpy
->gralloc
,
1214 GRALLOC_MODULE_PERFORM_GET_DRM_FD
,
1216 if (err
|| fd
< 0) {
1217 _eglLog(_EGL_WARNING
, "fail to get drm fd");
1221 dri2_dpy
->fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
1222 if (dri2_dpy
->fd
< 0)
1225 return droid_probe_device(disp
);
1227 #endif /* HAVE_DRM_GRALLOC */
1229 static const struct dri2_egl_display_vtbl droid_display_vtbl
= {
1230 .authenticate
= NULL
,
1231 .create_window_surface
= droid_create_window_surface
,
1232 .create_pixmap_surface
= dri2_fallback_create_pixmap_surface
,
1233 .create_pbuffer_surface
= droid_create_pbuffer_surface
,
1234 .destroy_surface
= droid_destroy_surface
,
1235 .create_image
= droid_create_image_khr
,
1236 .swap_buffers
= droid_swap_buffers
,
1237 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
, /* Android implements the function */
1238 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1239 .swap_interval
= droid_swap_interval
,
1240 #if ANDROID_API_LEVEL >= 23
1241 .set_damage_region
= droid_set_damage_region
,
1243 .set_damage_region
= dri2_fallback_set_damage_region
,
1245 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1246 .copy_buffers
= dri2_fallback_copy_buffers
,
1247 .query_buffer_age
= droid_query_buffer_age
,
1248 .query_surface
= droid_query_surface
,
1249 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1250 .get_sync_values
= dri2_fallback_get_sync_values
,
1251 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1252 .set_shared_buffer_mode
= droid_set_shared_buffer_mode
,
1255 #ifdef HAVE_DRM_GRALLOC
1256 static const __DRIdri2LoaderExtension droid_dri2_loader_extension
= {
1257 .base
= { __DRI_DRI2_LOADER
, 4 },
1260 .flushFrontBuffer
= droid_flush_front_buffer
,
1261 .getBuffersWithFormat
= droid_get_buffers_with_format
,
1262 .getCapability
= droid_get_capability
,
1264 #endif /* HAVE_DRM_GRALLOC */
1266 static const __DRIimageLoaderExtension droid_image_loader_extension
= {
1267 .base
= { __DRI_IMAGE_LOADER
, 2 },
1269 .getBuffers
= droid_image_get_buffers
,
1270 .flushFrontBuffer
= droid_flush_front_buffer
,
1271 .getCapability
= droid_get_capability
,
1274 #ifdef HAVE_DRM_GRALLOC
1275 static const __DRIextension
*droid_dri2_loader_extensions
[] = {
1276 &droid_dri2_loader_extension
.base
,
1277 &image_lookup_extension
.base
,
1278 &use_invalidate
.base
,
1279 /* No __DRI_MUTABLE_RENDER_BUFFER_LOADER because it requires
1280 * __DRI_IMAGE_LOADER.
1284 #endif /* HAVE_DRM_GRALLOC */
1287 droid_display_shared_buffer(__DRIdrawable
*driDrawable
, int fence_fd
,
1288 void *loaderPrivate
)
1290 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1291 struct ANativeWindowBuffer
*old_buffer UNUSED
= dri2_surf
->buffer
;
1293 if (!_eglSurfaceInSharedBufferMode(&dri2_surf
->base
)) {
1294 _eglLog(_EGL_WARNING
, "%s: internal error: buffer is not shared",
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.
1303 if (dri2_surf
->out_fence_fd
>= 0) {
1304 close(dri2_surf
->out_fence_fd
);
1305 dri2_surf
->out_fence_fd
= -1;
1307 } else if (dri2_surf
->out_fence_fd
>= 0) {
1308 fence_fd
= dri2_surf
->out_fence_fd
;
1309 dri2_surf
->out_fence_fd
= -1;
1312 if (dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
1314 _eglLog(_EGL_WARNING
, "%s: ANativeWindow::queueBuffer failed", __func__
);
1321 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
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
);
1327 _eglLog(_EGL_WARNING
, "%s: ANativeWindow::dequeueBuffer failed", __func__
);
1329 dri2_surf
->base
.Lost
= true;
1330 dri2_surf
->buffer
= NULL
;
1331 dri2_surf
->back
= NULL
;
1333 if (dri2_surf
->dri_image_back
) {
1334 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
1335 dri2_surf
->dri_image_back
= NULL
;
1338 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
1345 /* Access to the buffer is controlled by a sync fence. Block on it.
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).
1351 * SYNC_IOC_WAIT waits forever if timeout < 0
1353 sync_wait(fence_fd
, -1);
1357 static const __DRImutableRenderBufferLoaderExtension droid_mutable_render_buffer_extension
= {
1358 .base
= { __DRI_MUTABLE_RENDER_BUFFER_LOADER
, 1 },
1359 .displaySharedBuffer
= droid_display_shared_buffer
,
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
,
1371 droid_load_driver(_EGLDisplay
*disp
)
1373 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
1376 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1377 if (dri2_dpy
->driver_name
== NULL
)
1380 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1382 if (!dri2_dpy
->is_render_node
) {
1383 #ifdef HAVE_DRM_GRALLOC
1384 /* Handle control nodes using __DRI_DRI2_LOADER extension and GEM names
1385 * for backwards compatibility with drm_gralloc. (Do not use on new
1387 dri2_dpy
->loader_extensions
= droid_dri2_loader_extensions
;
1388 if (!dri2_load_driver(disp
)) {
1389 err
= "DRI2: failed to load driver";
1393 err
= "DRI2: handle is not for a render node";
1397 dri2_dpy
->loader_extensions
= droid_image_loader_extensions
;
1398 if (!dri2_load_driver_dri3(disp
)) {
1399 err
= "DRI3: failed to load driver";
1407 free(dri2_dpy
->driver_name
);
1408 dri2_dpy
->driver_name
= NULL
;
1413 droid_unload_driver(_EGLDisplay
*disp
)
1415 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1417 dlclose(dri2_dpy
->driver
);
1418 dri2_dpy
->driver
= NULL
;
1419 free(dri2_dpy
->driver_name
);
1420 dri2_dpy
->driver_name
= NULL
;
1424 droid_filter_device(_EGLDisplay
*disp
, int fd
, const char *vendor
)
1426 drmVersionPtr ver
= drmGetVersion(fd
);
1430 if (strcmp(vendor
, ver
->name
) != 0) {
1431 drmFreeVersion(ver
);
1435 drmFreeVersion(ver
);
1440 droid_probe_device(_EGLDisplay
*disp
)
1442 /* Check that the device is supported, by attempting to:
1443 * - load the dri module
1444 * - and, create a screen
1446 if (!droid_load_driver(disp
))
1449 if (!dri2_create_screen(disp
)) {
1450 _eglLog(_EGL_WARNING
, "DRI2: failed to create screen");
1451 droid_unload_driver(disp
);
1458 droid_open_device(_EGLDisplay
*disp
)
1460 #define MAX_DRM_DEVICES 32
1461 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1462 drmDevicePtr device
, devices
[MAX_DRM_DEVICES
] = { NULL
};
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 dri2_dpy
->fd
= loader_open_device(device
->nodes
[DRM_NODE_RENDER
]);
1482 if (dri2_dpy
->fd
< 0) {
1483 _eglLog(_EGL_WARNING
, "%s() Failed to open DRM device %s",
1484 __func__
, device
->nodes
[DRM_NODE_RENDER
]);
1488 /* If a vendor is explicitly provided, we use only that.
1489 * Otherwise we fall-back the first device that is supported.
1492 if (droid_filter_device(disp
, dri2_dpy
->fd
, vendor_name
)) {
1493 /* Device does not match - try next device */
1494 close(dri2_dpy
->fd
);
1498 /* If the requested device matches - use it. Regardless if
1499 * init fails, do not fall-back to any other device.
1501 if (!droid_probe_device(disp
)) {
1502 close(dri2_dpy
->fd
);
1508 if (droid_probe_device(disp
))
1511 /* No explicit request - attempt the next device */
1512 close(dri2_dpy
->fd
);
1515 drmFreeDevices(devices
, num_devices
);
1517 if (dri2_dpy
->fd
< 0) {
1518 _eglLog(_EGL_WARNING
, "Failed to open %s DRM device",
1519 vendor_name
? "desired": "any");
1524 #undef MAX_DRM_DEVICES
1528 dri2_initialize_android(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1531 struct dri2_egl_display
*dri2_dpy
;
1535 /* Not supported yet */
1536 if (disp
->Options
.ForceSoftware
)
1539 dri2_dpy
= calloc(1, sizeof(*dri2_dpy
));
1541 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1544 ret
= hw_get_module(GRALLOC_HARDWARE_MODULE_ID
,
1545 (const hw_module_t
**)&dri2_dpy
->gralloc
);
1547 err
= "DRI2: failed to get gralloc module";
1551 disp
->DriverData
= (void *) dri2_dpy
;
1553 #ifdef HAVE_DRM_GRALLOC
1554 if (!droid_open_device_drm_gralloc(disp
)) {
1556 if (!droid_open_device(disp
)) {
1558 err
= "DRI2: failed to open device";
1562 dev
= _eglAddDevice(dri2_dpy
->fd
, false);
1564 err
= "DRI2: failed to find EGLDevice";
1570 if (!dri2_setup_extensions(disp
)) {
1571 err
= "DRI2: failed to setup extensions";
1575 dri2_setup_screen(disp
);
1577 /* We set the maximum swap interval as 1 for Android platform, since it is
1578 * the maximum value supported by Android according to the value of
1579 * ANativeWindow::maxSwapInterval.
1581 dri2_setup_swap_interval(disp
, 1);
1583 disp
->Extensions
.ANDROID_framebuffer_target
= EGL_TRUE
;
1584 disp
->Extensions
.ANDROID_image_native_buffer
= EGL_TRUE
;
1585 disp
->Extensions
.ANDROID_recordable
= EGL_TRUE
;
1586 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1587 #if ANDROID_API_LEVEL >= 23
1588 disp
->Extensions
.KHR_partial_update
= EGL_TRUE
;
1590 disp
->Extensions
.KHR_image
= EGL_TRUE
;
1591 #if ANDROID_API_LEVEL >= 24
1592 if (dri2_dpy
->mutable_render_buffer
&&
1593 dri2_dpy
->loader_extensions
== droid_image_loader_extensions
) {
1594 disp
->Extensions
.KHR_mutable_render_buffer
= EGL_TRUE
;
1598 /* Create configs *after* enabling extensions because presence of DRI
1599 * driver extensions can affect the capabilities of EGLConfigs.
1601 if (!droid_add_configs_for_visuals(drv
, disp
)) {
1602 err
= "DRI2: failed to add configs";
1606 /* Fill vtbl last to prevent accidentally calling virtual function during
1609 dri2_dpy
->vtbl
= &droid_display_vtbl
;
1614 dri2_display_destroy(disp
);
1615 return _eglError(EGL_NOT_INITIALIZED
, err
);