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))
57 struct droid_yuv_format
{
59 int native
; /* HAL_PIXEL_FORMAT_ */
60 enum chroma_order chroma_order
; /* chroma order is {Cb, Cr} or {Cr, Cb} */
61 int chroma_step
; /* Distance in bytes between subsequent chroma pixels. */
64 int fourcc
; /* __DRI_IMAGE_FOURCC_ */
67 /* The following table is used to look up a DRI image FourCC based
68 * on native format and information contained in android_ycbcr struct. */
69 static const struct droid_yuv_format droid_yuv_formats
[] = {
70 /* Native format, YCrCb, Chroma step, DRI image FourCC */
71 { HAL_PIXEL_FORMAT_YCbCr_420_888
, YCbCr
, 2, __DRI_IMAGE_FOURCC_NV12
},
72 { HAL_PIXEL_FORMAT_YCbCr_420_888
, YCbCr
, 1, __DRI_IMAGE_FOURCC_YUV420
},
73 { HAL_PIXEL_FORMAT_YCbCr_420_888
, YCrCb
, 1, __DRI_IMAGE_FOURCC_YVU420
},
74 { HAL_PIXEL_FORMAT_YV12
, YCrCb
, 1, __DRI_IMAGE_FOURCC_YVU420
},
75 /* HACK: See droid_create_image_from_prime_fds() and
76 * https://issuetracker.google.com/32077885. */
77 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, YCbCr
, 2, __DRI_IMAGE_FOURCC_NV12
},
78 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, YCbCr
, 1, __DRI_IMAGE_FOURCC_YUV420
},
79 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, YCrCb
, 1, __DRI_IMAGE_FOURCC_YVU420
},
80 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, YCrCb
, 1, __DRI_IMAGE_FOURCC_AYUV
},
81 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, YCrCb
, 1, __DRI_IMAGE_FOURCC_XYUV8888
},
85 get_fourcc_yuv(int native
, enum chroma_order chroma_order
, int chroma_step
)
87 for (int i
= 0; i
< ARRAY_SIZE(droid_yuv_formats
); ++i
)
88 if (droid_yuv_formats
[i
].native
== native
&&
89 droid_yuv_formats
[i
].chroma_order
== chroma_order
&&
90 droid_yuv_formats
[i
].chroma_step
== chroma_step
)
91 return droid_yuv_formats
[i
].fourcc
;
99 for (int i
= 0; i
< ARRAY_SIZE(droid_yuv_formats
); ++i
)
100 if (droid_yuv_formats
[i
].native
== native
)
107 get_format_bpp(int native
)
112 case HAL_PIXEL_FORMAT_RGBA_8888
:
113 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
115 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
116 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
118 case HAL_PIXEL_FORMAT_RGBX_8888
:
119 case HAL_PIXEL_FORMAT_BGRA_8888
:
122 case HAL_PIXEL_FORMAT_RGB_565
:
133 /* createImageFromFds requires fourcc format */
134 static int get_fourcc(int native
)
137 case HAL_PIXEL_FORMAT_RGB_565
: return __DRI_IMAGE_FOURCC_RGB565
;
138 case HAL_PIXEL_FORMAT_BGRA_8888
: return __DRI_IMAGE_FOURCC_ARGB8888
;
139 case HAL_PIXEL_FORMAT_RGBA_8888
: return __DRI_IMAGE_FOURCC_ABGR8888
;
140 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
142 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
143 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
145 case HAL_PIXEL_FORMAT_RGBX_8888
: return __DRI_IMAGE_FOURCC_XBGR8888
;
147 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", native
);
152 static int get_format(int format
)
155 case HAL_PIXEL_FORMAT_BGRA_8888
: return __DRI_IMAGE_FORMAT_ARGB8888
;
156 case HAL_PIXEL_FORMAT_RGB_565
: return __DRI_IMAGE_FORMAT_RGB565
;
157 case HAL_PIXEL_FORMAT_RGBA_8888
: return __DRI_IMAGE_FORMAT_ABGR8888
;
158 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
160 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
161 * TODO: Revert this once https://issuetracker.google.com/32077885 is fixed.
163 case HAL_PIXEL_FORMAT_RGBX_8888
: return __DRI_IMAGE_FORMAT_XBGR8888
;
165 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", format
);
170 /* returns # of fds, and by reference the actual fds */
172 get_native_buffer_fds(struct ANativeWindowBuffer
*buf
, int fds
[3])
174 native_handle_t
*handle
= (native_handle_t
*)buf
->handle
;
180 * Various gralloc implementations exist, but the dma-buf fd tends
181 * to be first. Access it directly to avoid a dependency on specific
184 for (int i
= 0; i
< handle
->numFds
; i
++)
185 fds
[i
] = handle
->data
[i
];
187 return handle
->numFds
;
190 #ifdef HAVE_DRM_GRALLOC
192 get_native_buffer_name(struct ANativeWindowBuffer
*buf
)
194 return gralloc_drm_get_gem_handle(buf
->handle
);
196 #endif /* HAVE_DRM_GRALLOC */
199 droid_window_dequeue_buffer(struct dri2_egl_surface
*dri2_surf
)
203 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
207 /* If access to the buffer is controlled by a sync fence, then block on the
210 * It may be more performant to postpone blocking until there is an
211 * immediate need to write to the buffer. But doing so would require adding
212 * hooks to the DRI2 loader.
214 * From the ANativeWindow::dequeueBuffer documentation:
216 * The libsync fence file descriptor returned in the int pointed to by
217 * the fenceFd argument will refer to the fence that must signal
218 * before the dequeued buffer may be written to. A value of -1
219 * indicates that the caller may access the buffer immediately without
220 * waiting on a fence. If a valid file descriptor is returned (i.e.
221 * any value except -1) then the caller is responsible for closing the
225 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
227 * Waits indefinitely if timeout < 0.
230 sync_wait(fence_fd
, timeout
);
234 dri2_surf
->buffer
->common
.incRef(&dri2_surf
->buffer
->common
);
236 /* Record all the buffers created by ANativeWindow and update back buffer
237 * for updating buffer's age in swap_buffers.
239 EGLBoolean updated
= EGL_FALSE
;
240 for (int i
= 0; i
< dri2_surf
->color_buffers_count
; i
++) {
241 if (!dri2_surf
->color_buffers
[i
].buffer
) {
242 dri2_surf
->color_buffers
[i
].buffer
= dri2_surf
->buffer
;
244 if (dri2_surf
->color_buffers
[i
].buffer
== dri2_surf
->buffer
) {
245 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
252 /* In case of all the buffers were recreated by ANativeWindow, reset
255 for (int i
= 0; i
< dri2_surf
->color_buffers_count
; i
++) {
256 dri2_surf
->color_buffers
[i
].buffer
= NULL
;
257 dri2_surf
->color_buffers
[i
].age
= 0;
259 dri2_surf
->color_buffers
[0].buffer
= dri2_surf
->buffer
;
260 dri2_surf
->back
= &dri2_surf
->color_buffers
[0];
267 droid_window_enqueue_buffer(_EGLDisplay
*disp
, struct dri2_egl_surface
*dri2_surf
)
269 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
271 /* To avoid blocking other EGL calls, release the display mutex before
272 * we enter droid_window_enqueue_buffer() and re-acquire the mutex upon
275 mtx_unlock(&disp
->Mutex
);
277 /* Queue the buffer with stored out fence fd. The ANativeWindow or buffer
278 * consumer may choose to wait for the fence to signal before accessing
279 * it. If fence fd value is -1, buffer can be accessed by consumer
280 * immediately. Consumer or application shouldn't rely on timestamp
281 * associated with fence if the fence fd is -1.
283 * Ownership of fd is transferred to consumer after queueBuffer and the
284 * consumer is responsible for closing it. Caller must not use the fd
285 * after passing it to queueBuffer.
287 int fence_fd
= dri2_surf
->out_fence_fd
;
288 dri2_surf
->out_fence_fd
= -1;
289 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
292 dri2_surf
->buffer
->common
.decRef(&dri2_surf
->buffer
->common
);
293 dri2_surf
->buffer
= NULL
;
294 dri2_surf
->back
= NULL
;
296 mtx_lock(&disp
->Mutex
);
298 if (dri2_surf
->dri_image_back
) {
299 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
300 dri2_surf
->dri_image_back
= NULL
;
307 droid_window_cancel_buffer(struct dri2_egl_surface
*dri2_surf
)
310 int fence_fd
= dri2_surf
->out_fence_fd
;
312 dri2_surf
->out_fence_fd
= -1;
313 ret
= dri2_surf
->window
->cancelBuffer(dri2_surf
->window
,
314 dri2_surf
->buffer
, fence_fd
);
316 _eglLog(_EGL_WARNING
, "ANativeWindow::cancelBuffer failed");
317 dri2_surf
->base
.Lost
= EGL_TRUE
;
322 droid_set_shared_buffer_mode(_EGLDisplay
*disp
, _EGLSurface
*surf
, bool mode
)
324 #if ANDROID_API_LEVEL >= 24
325 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
326 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
327 struct ANativeWindow
*window
= dri2_surf
->window
;
329 assert(surf
->Type
== EGL_WINDOW_BIT
);
330 assert(_eglSurfaceHasMutableRenderBuffer(&dri2_surf
->base
));
332 _eglLog(_EGL_DEBUG
, "%s: mode=%d", __func__
, mode
);
334 if (native_window_set_shared_buffer_mode(window
, mode
)) {
335 _eglLog(_EGL_WARNING
, "failed native_window_set_shared_buffer_mode"
336 "(window=%p, mode=%d)", window
, mode
);
342 _eglLog(_EGL_FATAL
, "%s:%d: internal error: unreachable", __FILE__
, __LINE__
);
348 droid_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
349 _EGLConfig
*conf
, void *native_window
,
350 const EGLint
*attrib_list
)
352 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
353 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
354 struct dri2_egl_surface
*dri2_surf
;
355 struct ANativeWindow
*window
= native_window
;
356 const __DRIconfig
*config
;
358 dri2_surf
= calloc(1, sizeof *dri2_surf
);
360 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
364 if (!dri2_init_surface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
,
365 true, native_window
))
366 goto cleanup_surface
;
368 if (type
== EGL_WINDOW_BIT
) {
372 if (window
->common
.magic
!= ANDROID_NATIVE_WINDOW_MAGIC
) {
373 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
374 goto cleanup_surface
;
376 if (window
->query(window
, NATIVE_WINDOW_FORMAT
, &format
)) {
377 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
378 goto cleanup_surface
;
381 /* Query ANativeWindow for MIN_UNDEQUEUED_BUFFER, set buffer count
382 * and allocate color_buffers.
384 if (window
->query(window
, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS
,
386 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
387 goto cleanup_surface
;
389 if (native_window_set_buffer_count(window
, buffer_count
+1)) {
390 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
391 goto cleanup_surface
;
393 dri2_surf
->color_buffers
= calloc(buffer_count
+1,
394 sizeof(*dri2_surf
->color_buffers
));
395 if (!dri2_surf
->color_buffers
) {
396 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
397 goto cleanup_surface
;
399 dri2_surf
->color_buffers_count
= buffer_count
+1;
401 if (format
!= dri2_conf
->base
.NativeVisualID
) {
402 _eglLog(_EGL_WARNING
, "Native format mismatch: 0x%x != 0x%x",
403 format
, dri2_conf
->base
.NativeVisualID
);
406 window
->query(window
, NATIVE_WINDOW_WIDTH
, &dri2_surf
->base
.Width
);
407 window
->query(window
, NATIVE_WINDOW_HEIGHT
, &dri2_surf
->base
.Height
);
410 config
= dri2_get_dri_config(dri2_conf
, type
,
411 dri2_surf
->base
.GLColorspace
);
413 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
414 goto cleanup_surface
;
417 if (!dri2_create_drawable(dri2_dpy
, config
, dri2_surf
, dri2_surf
))
418 goto cleanup_surface
;
421 window
->common
.incRef(&window
->common
);
422 dri2_surf
->window
= window
;
425 return &dri2_surf
->base
;
428 if (dri2_surf
->color_buffers_count
)
429 free(dri2_surf
->color_buffers
);
436 droid_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
437 _EGLConfig
*conf
, void *native_window
,
438 const EGLint
*attrib_list
)
440 return droid_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
441 native_window
, attrib_list
);
445 droid_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
446 _EGLConfig
*conf
, const EGLint
*attrib_list
)
448 return droid_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
453 droid_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
455 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
456 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
458 dri2_egl_surface_free_local_buffers(dri2_surf
);
460 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
461 if (dri2_surf
->buffer
)
462 droid_window_cancel_buffer(dri2_surf
);
464 dri2_surf
->window
->common
.decRef(&dri2_surf
->window
->common
);
467 if (dri2_surf
->dri_image_back
) {
468 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_back", __func__
, __LINE__
);
469 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
470 dri2_surf
->dri_image_back
= NULL
;
473 if (dri2_surf
->dri_image_front
) {
474 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_front", __func__
, __LINE__
);
475 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_front
);
476 dri2_surf
->dri_image_front
= NULL
;
479 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
481 dri2_fini_surface(surf
);
482 free(dri2_surf
->color_buffers
);
489 droid_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
,
490 _EGLSurface
*surf
, EGLint interval
)
492 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
493 struct ANativeWindow
*window
= dri2_surf
->window
;
495 if (window
->setSwapInterval(window
, interval
))
498 surf
->SwapInterval
= interval
;
503 update_buffers(struct dri2_egl_surface
*dri2_surf
)
505 if (dri2_surf
->base
.Lost
)
508 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
511 /* try to dequeue the next back buffer */
512 if (!dri2_surf
->buffer
&& !droid_window_dequeue_buffer(dri2_surf
)) {
513 _eglLog(_EGL_WARNING
, "Could not dequeue buffer from native window");
514 dri2_surf
->base
.Lost
= EGL_TRUE
;
518 /* free outdated buffers and update the surface size */
519 if (dri2_surf
->base
.Width
!= dri2_surf
->buffer
->width
||
520 dri2_surf
->base
.Height
!= dri2_surf
->buffer
->height
) {
521 dri2_egl_surface_free_local_buffers(dri2_surf
);
522 dri2_surf
->base
.Width
= dri2_surf
->buffer
->width
;
523 dri2_surf
->base
.Height
= dri2_surf
->buffer
->height
;
530 get_front_bo(struct dri2_egl_surface
*dri2_surf
, unsigned int format
)
532 struct dri2_egl_display
*dri2_dpy
=
533 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
535 if (dri2_surf
->dri_image_front
)
538 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
539 /* According current EGL spec, front buffer rendering
540 * for window surface is not supported now.
541 * and mesa doesn't have the implementation of this case.
542 * Add warning message, but not treat it as error.
544 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported front buffer for window surface");
545 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
546 dri2_surf
->dri_image_front
=
547 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
548 dri2_surf
->base
.Width
,
549 dri2_surf
->base
.Height
,
553 if (!dri2_surf
->dri_image_front
) {
554 _eglLog(_EGL_WARNING
, "dri2_image_front allocation failed");
563 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
565 struct dri2_egl_display
*dri2_dpy
=
566 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
568 int offset
= 0, fds
[3];
571 if (dri2_surf
->dri_image_back
)
574 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
575 if (!dri2_surf
->buffer
) {
576 _eglLog(_EGL_WARNING
, "Could not get native buffer");
580 num_fds
= get_native_buffer_fds(dri2_surf
->buffer
, fds
);
582 _eglLog(_EGL_WARNING
, "Could not get native buffer FD");
586 fourcc
= get_fourcc(dri2_surf
->buffer
->format
);
588 pitch
= dri2_surf
->buffer
->stride
*
589 get_format_bpp(dri2_surf
->buffer
->format
);
591 if (fourcc
== -1 || pitch
== 0) {
592 _eglLog(_EGL_WARNING
, "Invalid buffer fourcc(%x) or pitch(%d)",
597 dri2_surf
->dri_image_back
=
598 dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
599 dri2_surf
->base
.Width
,
600 dri2_surf
->base
.Height
,
607 if (!dri2_surf
->dri_image_back
) {
608 _eglLog(_EGL_WARNING
, "failed to create DRI image from FD");
611 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
612 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
613 * the spec states that they have a back buffer but no front buffer, in
614 * contrast to pixmaps, which have a front buffer but no back buffer.
616 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
617 * from the spec, following the precedent of Mesa's EGL X11 platform. The
618 * X11 platform correctly assigns pbuffers to single-buffered configs, but
619 * assigns the pbuffer a front buffer instead of a back buffer.
621 * Pbuffers in the X11 platform mostly work today, so let's just copy its
622 * behavior instead of trying to fix (and hence potentially breaking) the
625 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported back buffer for pbuffer surface");
631 /* Some drivers will pass multiple bits in buffer_mask.
632 * For such case, will go through all the bits, and
633 * will not return error when unsupported buffer is requested, only
634 * return error when the allocation for supported buffer failed.
637 droid_image_get_buffers(__DRIdrawable
*driDrawable
,
641 uint32_t buffer_mask
,
642 struct __DRIimageList
*images
)
644 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
646 images
->image_mask
= 0;
647 images
->front
= NULL
;
650 if (update_buffers(dri2_surf
) < 0)
653 if (_eglSurfaceInSharedBufferMode(&dri2_surf
->base
)) {
654 if (get_back_bo(dri2_surf
) < 0)
657 /* We have dri_image_back because this is a window surface and
658 * get_back_bo() succeeded.
660 assert(dri2_surf
->dri_image_back
);
661 images
->back
= dri2_surf
->dri_image_back
;
662 images
->image_mask
|= __DRI_IMAGE_BUFFER_SHARED
;
664 /* There exists no accompanying back nor front buffer. */
668 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
669 if (get_front_bo(dri2_surf
, format
) < 0)
672 if (dri2_surf
->dri_image_front
) {
673 images
->front
= dri2_surf
->dri_image_front
;
674 images
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
678 if (buffer_mask
& __DRI_IMAGE_BUFFER_BACK
) {
679 if (get_back_bo(dri2_surf
) < 0)
682 if (dri2_surf
->dri_image_back
) {
683 images
->back
= dri2_surf
->dri_image_back
;
684 images
->image_mask
|= __DRI_IMAGE_BUFFER_BACK
;
692 droid_query_buffer_age(_EGLDriver
*drv
,
693 _EGLDisplay
*disp
, _EGLSurface
*surface
)
695 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
697 if (update_buffers(dri2_surf
) < 0) {
698 _eglError(EGL_BAD_ALLOC
, "droid_query_buffer_age");
702 return dri2_surf
->back
? dri2_surf
->back
->age
: 0;
706 droid_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
708 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
709 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
710 const bool has_mutable_rb
= _eglSurfaceHasMutableRenderBuffer(draw
);
712 /* From the EGL_KHR_mutable_render_buffer spec (v12):
714 * If surface is a single-buffered window, pixmap, or pbuffer surface
715 * for which there is no pending change to the EGL_RENDER_BUFFER
716 * attribute, eglSwapBuffers has no effect.
718 if (has_mutable_rb
&&
719 draw
->RequestedRenderBuffer
== EGL_SINGLE_BUFFER
&&
720 draw
->ActiveRenderBuffer
== EGL_SINGLE_BUFFER
) {
721 _eglLog(_EGL_DEBUG
, "%s: remain in shared buffer mode", __func__
);
725 for (int i
= 0; i
< dri2_surf
->color_buffers_count
; i
++) {
726 if (dri2_surf
->color_buffers
[i
].age
> 0)
727 dri2_surf
->color_buffers
[i
].age
++;
730 /* "XXX: we don't use get_back_bo() since it causes regressions in
731 * several dEQP tests.
734 dri2_surf
->back
->age
= 1;
736 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
738 /* dri2_surf->buffer can be null even when no error has occured. For
739 * example, if the user has called no GL rendering commands since the
740 * previous eglSwapBuffers, then the driver may have not triggered
741 * a callback to ANativeWindow::dequeueBuffer, in which case
742 * dri2_surf->buffer remains null.
744 if (dri2_surf
->buffer
)
745 droid_window_enqueue_buffer(disp
, dri2_surf
);
747 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
749 /* Update the shared buffer mode */
750 if (has_mutable_rb
&&
751 draw
->ActiveRenderBuffer
!= draw
->RequestedRenderBuffer
) {
752 bool mode
= (draw
->RequestedRenderBuffer
== EGL_SINGLE_BUFFER
);
753 _eglLog(_EGL_DEBUG
, "%s: change to shared buffer mode %d",
756 if (!droid_set_shared_buffer_mode(disp
, draw
, mode
))
758 draw
->ActiveRenderBuffer
= draw
->RequestedRenderBuffer
;
765 droid_create_image_from_prime_fds_yuv(_EGLDisplay
*disp
, _EGLContext
*ctx
,
766 struct ANativeWindowBuffer
*buf
,
767 int num_fds
, int fds
[3])
769 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
770 struct android_ycbcr ycbcr
;
773 enum chroma_order chroma_order
;
777 if (!dri2_dpy
->gralloc
->lock_ycbcr
) {
778 _eglLog(_EGL_WARNING
, "Gralloc does not support lock_ycbcr");
782 memset(&ycbcr
, 0, sizeof(ycbcr
));
783 ret
= dri2_dpy
->gralloc
->lock_ycbcr(dri2_dpy
->gralloc
, buf
->handle
,
784 0, 0, 0, 0, 0, &ycbcr
);
786 /* HACK: See droid_create_image_from_prime_fds() and
787 * https://issuetracker.google.com/32077885.*/
788 if (buf
->format
== HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
)
791 _eglLog(_EGL_WARNING
, "gralloc->lock_ycbcr failed: %d", ret
);
794 dri2_dpy
->gralloc
->unlock(dri2_dpy
->gralloc
, buf
->handle
);
796 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
797 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
798 * so they can be interpreted as offsets. */
799 offsets
[0] = (size_t)ycbcr
.y
;
800 /* We assume here that all the planes are located in one DMA-buf. */
801 if ((size_t)ycbcr
.cr
< (size_t)ycbcr
.cb
) {
802 chroma_order
= YCrCb
;
803 offsets
[1] = (size_t)ycbcr
.cr
;
804 offsets
[2] = (size_t)ycbcr
.cb
;
806 chroma_order
= YCbCr
;
807 offsets
[1] = (size_t)ycbcr
.cb
;
808 offsets
[2] = (size_t)ycbcr
.cr
;
811 /* .ystride is the line length (in bytes) of the Y plane,
812 * .cstride is the line length (in bytes) of any of the remaining
813 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
815 pitches
[0] = ycbcr
.ystride
;
816 pitches
[1] = pitches
[2] = ycbcr
.cstride
;
818 /* .chroma_step is the byte distance between the same chroma channel
819 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
820 fourcc
= get_fourcc_yuv(buf
->format
, chroma_order
, ycbcr
.chroma_step
);
822 _eglLog(_EGL_WARNING
, "unsupported YUV format, native = %x, chroma_order = %s, chroma_step = %d",
823 buf
->format
, chroma_order
== YCbCr
? "YCbCr" : "YCrCb", ycbcr
.chroma_step
);
828 * Since this is EGL_NATIVE_BUFFER_ANDROID don't assume that
829 * the single-fd case cannot happen. So handle eithe single
830 * fd or fd-per-plane case:
833 fds
[2] = fds
[1] = fds
[0];
835 int expected_planes
= (ycbcr
.chroma_step
== 2) ? 2 : 3;
836 assert(num_fds
== expected_planes
);
839 if (ycbcr
.chroma_step
== 2) {
840 /* Semi-planar Y + CbCr or Y + CrCb format. */
841 const EGLint attr_list_2plane
[] = {
842 EGL_WIDTH
, buf
->width
,
843 EGL_HEIGHT
, buf
->height
,
844 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
845 EGL_DMA_BUF_PLANE0_FD_EXT
, fds
[0],
846 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
847 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, offsets
[0],
848 EGL_DMA_BUF_PLANE1_FD_EXT
, fds
[1],
849 EGL_DMA_BUF_PLANE1_PITCH_EXT
, pitches
[1],
850 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, offsets
[1],
854 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_2plane
);
856 /* Fully planar Y + Cb + Cr or Y + Cr + Cb format. */
857 const EGLint attr_list_3plane
[] = {
858 EGL_WIDTH
, buf
->width
,
859 EGL_HEIGHT
, buf
->height
,
860 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
861 EGL_DMA_BUF_PLANE0_FD_EXT
, fds
[0],
862 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
863 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, offsets
[0],
864 EGL_DMA_BUF_PLANE1_FD_EXT
, fds
[1],
865 EGL_DMA_BUF_PLANE1_PITCH_EXT
, pitches
[1],
866 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, offsets
[1],
867 EGL_DMA_BUF_PLANE2_FD_EXT
, fds
[2],
868 EGL_DMA_BUF_PLANE2_PITCH_EXT
, pitches
[2],
869 EGL_DMA_BUF_PLANE2_OFFSET_EXT
, offsets
[2],
873 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_3plane
);
878 droid_create_image_from_prime_fds(_EGLDisplay
*disp
, _EGLContext
*ctx
,
879 struct ANativeWindowBuffer
*buf
, int num_fds
, int fds
[3])
883 if (is_yuv(buf
->format
)) {
886 image
= droid_create_image_from_prime_fds_yuv(disp
, ctx
, buf
, num_fds
, fds
);
888 * HACK: https://issuetracker.google.com/32077885
889 * There is no API available to properly query the IMPLEMENTATION_DEFINED
890 * format. As a workaround we rely here on gralloc allocating either
891 * an arbitrary YCbCr 4:2:0 or RGBX_8888, with the latter being recognized
892 * by lock_ycbcr failing.
894 if (image
|| buf
->format
!= HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
)
899 * Non-YUV formats could *also* have multiple planes, such as ancillary
900 * color compression state buffer, but the rest of the code isn't ready
901 * yet to deal with modifiers:
903 assert(num_fds
== 1);
905 const int fourcc
= get_fourcc(buf
->format
);
907 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
911 pitch
= buf
->stride
* get_format_bpp(buf
->format
);
913 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
917 const EGLint attr_list
[] = {
918 EGL_WIDTH
, buf
->width
,
919 EGL_HEIGHT
, buf
->height
,
920 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
921 EGL_DMA_BUF_PLANE0_FD_EXT
, fds
[0],
922 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitch
,
923 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, 0,
927 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list
);
930 #ifdef HAVE_DRM_GRALLOC
932 droid_create_image_from_name(_EGLDisplay
*disp
, _EGLContext
*ctx
,
933 struct ANativeWindowBuffer
*buf
)
935 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
936 struct dri2_egl_image
*dri2_img
;
940 name
= get_native_buffer_name(buf
);
942 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
946 format
= get_format(buf
->format
);
950 dri2_img
= calloc(1, sizeof(*dri2_img
));
952 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
956 _eglInitImage(&dri2_img
->base
, disp
);
958 dri2_img
->dri_image
=
959 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
966 if (!dri2_img
->dri_image
) {
968 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
972 return &dri2_img
->base
;
974 #endif /* HAVE_DRM_GRALLOC */
977 droid_query_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
978 EGLint attribute
, EGLint
*value
)
980 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
983 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
984 dri2_surf
->window
->query(dri2_surf
->window
,
985 NATIVE_WINDOW_DEFAULT_WIDTH
, value
);
990 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
991 dri2_surf
->window
->query(dri2_surf
->window
,
992 NATIVE_WINDOW_DEFAULT_HEIGHT
, value
);
999 return _eglQuerySurface(drv
, disp
, surf
, attribute
, value
);
1003 dri2_create_image_android_native_buffer(_EGLDisplay
*disp
,
1005 struct ANativeWindowBuffer
*buf
)
1011 /* From the EGL_ANDROID_image_native_buffer spec:
1013 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
1014 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
1016 _eglError(EGL_BAD_CONTEXT
, "eglCreateEGLImageKHR: for "
1017 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
1022 if (!buf
|| buf
->common
.magic
!= ANDROID_NATIVE_BUFFER_MAGIC
||
1023 buf
->common
.version
!= sizeof(*buf
)) {
1024 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
1028 num_fds
= get_native_buffer_fds(buf
, fds
);
1030 return droid_create_image_from_prime_fds(disp
, ctx
, buf
, num_fds
, fds
);
1032 #ifdef HAVE_DRM_GRALLOC
1033 return droid_create_image_from_name(disp
, ctx
, buf
);
1040 droid_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1041 _EGLContext
*ctx
, EGLenum target
,
1042 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1045 case EGL_NATIVE_BUFFER_ANDROID
:
1046 return dri2_create_image_android_native_buffer(disp
, ctx
,
1047 (struct ANativeWindowBuffer
*) buffer
);
1049 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
1054 droid_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
1058 #ifdef HAVE_DRM_GRALLOC
1060 droid_get_buffers_parse_attachments(struct dri2_egl_surface
*dri2_surf
,
1061 unsigned int *attachments
, int count
)
1063 int num_buffers
= 0;
1065 /* fill dri2_surf->buffers */
1066 for (int i
= 0; i
< count
* 2; i
+= 2) {
1067 __DRIbuffer
*buf
, *local
;
1069 assert(num_buffers
< ARRAY_SIZE(dri2_surf
->buffers
));
1070 buf
= &dri2_surf
->buffers
[num_buffers
];
1072 switch (attachments
[i
]) {
1073 case __DRI_BUFFER_BACK_LEFT
:
1074 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
1075 buf
->attachment
= attachments
[i
];
1076 buf
->name
= get_native_buffer_name(dri2_surf
->buffer
);
1077 buf
->cpp
= get_format_bpp(dri2_surf
->buffer
->format
);
1078 buf
->pitch
= dri2_surf
->buffer
->stride
* buf
->cpp
;
1086 /* fall through for pbuffers */
1087 case __DRI_BUFFER_DEPTH
:
1088 case __DRI_BUFFER_STENCIL
:
1089 case __DRI_BUFFER_ACCUM
:
1090 case __DRI_BUFFER_DEPTH_STENCIL
:
1091 case __DRI_BUFFER_HIZ
:
1092 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
,
1093 attachments
[i
], attachments
[i
+ 1]);
1100 case __DRI_BUFFER_FRONT_LEFT
:
1101 case __DRI_BUFFER_FRONT_RIGHT
:
1102 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
1103 case __DRI_BUFFER_FAKE_FRONT_RIGHT
:
1104 case __DRI_BUFFER_BACK_RIGHT
:
1106 /* no front or right buffers */
1114 static __DRIbuffer
*
1115 droid_get_buffers_with_format(__DRIdrawable
* driDrawable
,
1116 int *width
, int *height
,
1117 unsigned int *attachments
, int count
,
1118 int *out_count
, void *loaderPrivate
)
1120 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1122 if (update_buffers(dri2_surf
) < 0)
1125 *out_count
= droid_get_buffers_parse_attachments(dri2_surf
, attachments
, count
);
1128 *width
= dri2_surf
->base
.Width
;
1130 *height
= dri2_surf
->base
.Height
;
1132 return dri2_surf
->buffers
;
1134 #endif /* HAVE_DRM_GRALLOC */
1137 droid_get_capability(void *loaderPrivate
, enum dri_loader_cap cap
)
1139 /* Note: loaderPrivate is _EGLDisplay* */
1141 case DRI_LOADER_CAP_RGBA_ORDERING
:
1149 droid_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1151 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1152 static const struct {
1155 unsigned int rgba_sizes
[4];
1157 { HAL_PIXEL_FORMAT_RGBA_8888
, { 0, 8, 16, 24 }, { 8, 8, 8, 8 } },
1158 { HAL_PIXEL_FORMAT_RGBX_8888
, { 0, 8, 16, -1 }, { 8, 8, 8, 0 } },
1159 { HAL_PIXEL_FORMAT_RGB_565
, { 11, 5, 0, -1 }, { 5, 6, 5, 0 } },
1160 { HAL_PIXEL_FORMAT_BGRA_8888
, { 16, 8, 0, 24 }, { 8, 8, 8, 8 } },
1163 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1164 int config_count
= 0;
1166 /* The nesting of loops is significant here. Also significant is the order
1167 * of the HAL pixel formats. Many Android apps (such as Google's official
1168 * NDK GLES2 example app), and even portions the core framework code (such
1169 * as SystemServiceManager in Nougat), incorrectly choose their EGLConfig.
1170 * They neglect to match the EGLConfig's EGL_NATIVE_VISUAL_ID against the
1171 * window's native format, and instead choose the first EGLConfig whose
1172 * channel sizes match those of the native window format while ignoring the
1173 * channel *ordering*.
1175 * We can detect such buggy clients in logcat when they call
1176 * eglCreateSurface, by detecting the mismatch between the EGLConfig's
1177 * format and the window's format.
1179 * As a workaround, we generate EGLConfigs such that all EGLConfigs for HAL
1180 * pixel format i precede those for HAL pixel format i+1. In my
1181 * (chadversary) testing on Android Nougat, this was good enough to pacify
1182 * the buggy clients.
1184 for (int i
= 0; i
< ARRAY_SIZE(visuals
); i
++) {
1185 for (int j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
1186 const EGLint surface_type
= EGL_WINDOW_BIT
| EGL_PBUFFER_BIT
;
1188 const EGLint config_attrs
[] = {
1189 EGL_NATIVE_VISUAL_ID
, visuals
[i
].format
,
1190 EGL_NATIVE_VISUAL_TYPE
, visuals
[i
].format
,
1191 EGL_FRAMEBUFFER_TARGET_ANDROID
, EGL_TRUE
,
1192 EGL_RECORDABLE_ANDROID
, EGL_TRUE
,
1196 struct dri2_egl_config
*dri2_conf
=
1197 dri2_add_config(disp
, dri2_dpy
->driver_configs
[j
],
1198 config_count
+ 1, surface_type
, config_attrs
,
1199 visuals
[i
].rgba_shifts
, visuals
[i
].rgba_sizes
);
1201 if (dri2_conf
->base
.ConfigID
== config_count
+ 1)
1208 for (int i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1209 if (!format_count
[i
]) {
1210 _eglLog(_EGL_DEBUG
, "No DRI config supports native format 0x%x",
1215 return (config_count
!= 0);
1218 static const struct dri2_egl_display_vtbl droid_display_vtbl
= {
1219 .authenticate
= NULL
,
1220 .create_window_surface
= droid_create_window_surface
,
1221 .create_pixmap_surface
= dri2_fallback_create_pixmap_surface
,
1222 .create_pbuffer_surface
= droid_create_pbuffer_surface
,
1223 .destroy_surface
= droid_destroy_surface
,
1224 .create_image
= droid_create_image_khr
,
1225 .swap_buffers
= droid_swap_buffers
,
1226 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
, /* Android implements the function */
1227 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1228 .swap_interval
= droid_swap_interval
,
1229 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1230 .copy_buffers
= dri2_fallback_copy_buffers
,
1231 .query_buffer_age
= droid_query_buffer_age
,
1232 .query_surface
= droid_query_surface
,
1233 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1234 .get_sync_values
= dri2_fallback_get_sync_values
,
1235 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1236 .set_shared_buffer_mode
= droid_set_shared_buffer_mode
,
1239 #ifdef HAVE_DRM_GRALLOC
1240 static const __DRIdri2LoaderExtension droid_dri2_loader_extension
= {
1241 .base
= { __DRI_DRI2_LOADER
, 4 },
1244 .flushFrontBuffer
= droid_flush_front_buffer
,
1245 .getBuffersWithFormat
= droid_get_buffers_with_format
,
1246 .getCapability
= droid_get_capability
,
1249 static const __DRIextension
*droid_dri2_loader_extensions
[] = {
1250 &droid_dri2_loader_extension
.base
,
1251 &image_lookup_extension
.base
,
1252 &use_invalidate
.base
,
1253 /* No __DRI_MUTABLE_RENDER_BUFFER_LOADER because it requires
1254 * __DRI_IMAGE_LOADER.
1258 #endif /* HAVE_DRM_GRALLOC */
1260 static const __DRIimageLoaderExtension droid_image_loader_extension
= {
1261 .base
= { __DRI_IMAGE_LOADER
, 2 },
1263 .getBuffers
= droid_image_get_buffers
,
1264 .flushFrontBuffer
= droid_flush_front_buffer
,
1265 .getCapability
= droid_get_capability
,
1269 droid_display_shared_buffer(__DRIdrawable
*driDrawable
, int fence_fd
,
1270 void *loaderPrivate
)
1272 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1273 struct ANativeWindowBuffer
*old_buffer UNUSED
= dri2_surf
->buffer
;
1275 if (!_eglSurfaceInSharedBufferMode(&dri2_surf
->base
)) {
1276 _eglLog(_EGL_WARNING
, "%s: internal error: buffer is not shared",
1281 if (fence_fd
>= 0) {
1282 /* The driver's fence is more recent than the surface's out fence, if it
1283 * exists at all. So use the driver's fence.
1285 if (dri2_surf
->out_fence_fd
>= 0) {
1286 close(dri2_surf
->out_fence_fd
);
1287 dri2_surf
->out_fence_fd
= -1;
1289 } else if (dri2_surf
->out_fence_fd
>= 0) {
1290 fence_fd
= dri2_surf
->out_fence_fd
;
1291 dri2_surf
->out_fence_fd
= -1;
1294 if (dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
1296 _eglLog(_EGL_WARNING
, "%s: ANativeWindow::queueBuffer failed", __func__
);
1303 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
1305 /* Tear down the surface because it no longer has a back buffer. */
1306 struct dri2_egl_display
*dri2_dpy
=
1307 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1309 _eglLog(_EGL_WARNING
, "%s: ANativeWindow::dequeueBuffer failed", __func__
);
1311 dri2_surf
->base
.Lost
= true;
1312 dri2_surf
->buffer
= NULL
;
1313 dri2_surf
->back
= NULL
;
1315 if (dri2_surf
->dri_image_back
) {
1316 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
1317 dri2_surf
->dri_image_back
= NULL
;
1320 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
1327 /* Access to the buffer is controlled by a sync fence. Block on it.
1329 * Ideally, we would submit the fence to the driver, and the driver would
1330 * postpone command execution until it signalled. But DRI lacks API for
1331 * that (as of 2018-04-11).
1333 * SYNC_IOC_WAIT waits forever if timeout < 0
1335 sync_wait(fence_fd
, -1);
1339 static const __DRImutableRenderBufferLoaderExtension droid_mutable_render_buffer_extension
= {
1340 .base
= { __DRI_MUTABLE_RENDER_BUFFER_LOADER
, 1 },
1341 .displaySharedBuffer
= droid_display_shared_buffer
,
1344 static const __DRIextension
*droid_image_loader_extensions
[] = {
1345 &droid_image_loader_extension
.base
,
1346 &image_lookup_extension
.base
,
1347 &use_invalidate
.base
,
1348 &droid_mutable_render_buffer_extension
.base
,
1353 droid_load_driver(_EGLDisplay
*disp
, bool swrast
)
1355 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
1358 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1359 if (dri2_dpy
->driver_name
== NULL
)
1362 #ifdef HAVE_DRM_GRALLOC
1363 /* Handle control nodes using __DRI_DRI2_LOADER extension and GEM names
1364 * for backwards compatibility with drm_gralloc. (Do not use on new
1366 dri2_dpy
->loader_extensions
= droid_dri2_loader_extensions
;
1367 if (!dri2_load_driver(disp
)) {
1368 err
= "DRI2: failed to load driver";
1373 /* Use kms swrast only with vgem / virtio_gpu.
1374 * virtio-gpu fallbacks to software rendering when 3D features
1375 * are unavailable since 6c5ab.
1377 if (strcmp(dri2_dpy
->driver_name
, "vgem") == 0 ||
1378 strcmp(dri2_dpy
->driver_name
, "virtio_gpu") == 0) {
1379 free(dri2_dpy
->driver_name
);
1380 dri2_dpy
->driver_name
= strdup("kms_swrast");
1382 err
= "DRI3: failed to find software capable driver";
1387 dri2_dpy
->loader_extensions
= droid_image_loader_extensions
;
1388 if (!dri2_load_driver_dri3(disp
)) {
1389 err
= "DRI3: failed to load driver";
1397 free(dri2_dpy
->driver_name
);
1398 dri2_dpy
->driver_name
= NULL
;
1403 droid_unload_driver(_EGLDisplay
*disp
)
1405 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1407 dlclose(dri2_dpy
->driver
);
1408 dri2_dpy
->driver
= NULL
;
1409 free(dri2_dpy
->driver_name
);
1410 dri2_dpy
->driver_name
= NULL
;
1414 droid_filter_device(_EGLDisplay
*disp
, int fd
, const char *vendor
)
1416 drmVersionPtr ver
= drmGetVersion(fd
);
1420 if (strcmp(vendor
, ver
->name
) != 0) {
1421 drmFreeVersion(ver
);
1425 drmFreeVersion(ver
);
1430 droid_probe_device(_EGLDisplay
*disp
, bool swrast
)
1432 /* Check that the device is supported, by attempting to:
1433 * - load the dri module
1434 * - and, create a screen
1436 if (!droid_load_driver(disp
, swrast
))
1439 if (!dri2_create_screen(disp
)) {
1440 _eglLog(_EGL_WARNING
, "DRI2: failed to create screen");
1441 droid_unload_driver(disp
);
1447 #ifdef HAVE_DRM_GRALLOC
1449 droid_open_device(_EGLDisplay
*disp
, bool swrast
)
1451 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1452 int fd
= -1, err
= -EINVAL
;
1457 if (dri2_dpy
->gralloc
->perform
)
1458 err
= dri2_dpy
->gralloc
->perform(dri2_dpy
->gralloc
,
1459 GRALLOC_MODULE_PERFORM_GET_DRM_FD
,
1461 if (err
|| fd
< 0) {
1462 _eglLog(_EGL_WARNING
, "fail to get drm fd");
1466 dri2_dpy
->fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
1467 if (dri2_dpy
->fd
< 0)
1470 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
)
1473 return droid_probe_device(disp
, swrast
);
1477 droid_open_device(_EGLDisplay
*disp
, bool swrast
)
1479 #define MAX_DRM_DEVICES 64
1480 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1481 drmDevicePtr device
, devices
[MAX_DRM_DEVICES
] = { NULL
};
1484 char *vendor_name
= NULL
;
1485 char vendor_buf
[PROPERTY_VALUE_MAX
];
1487 #ifdef EGL_FORCE_RENDERNODE
1488 const unsigned node_type
= DRM_NODE_RENDER
;
1490 const unsigned node_type
= swrast
? DRM_NODE_PRIMARY
: DRM_NODE_RENDER
;
1493 if (property_get("drm.gpu.vendor_name", vendor_buf
, NULL
) > 0)
1494 vendor_name
= vendor_buf
;
1496 num_devices
= drmGetDevices2(0, devices
, ARRAY_SIZE(devices
));
1497 if (num_devices
< 0)
1500 for (int i
= 0; i
< num_devices
; i
++) {
1501 device
= devices
[i
];
1503 if (!(device
->available_nodes
& (1 << node_type
)))
1506 dri2_dpy
->fd
= loader_open_device(device
->nodes
[node_type
]);
1507 if (dri2_dpy
->fd
< 0) {
1508 _eglLog(_EGL_WARNING
, "%s() Failed to open DRM device %s",
1509 __func__
, device
->nodes
[node_type
]);
1513 /* If a vendor is explicitly provided, we use only that.
1514 * Otherwise we fall-back the first device that is supported.
1517 if (droid_filter_device(disp
, dri2_dpy
->fd
, vendor_name
)) {
1518 /* Device does not match - try next device */
1519 close(dri2_dpy
->fd
);
1523 /* If the requested device matches - use it. Regardless if
1524 * init fails, do not fall-back to any other device.
1526 if (!droid_probe_device(disp
, false)) {
1527 close(dri2_dpy
->fd
);
1533 if (droid_probe_device(disp
, swrast
))
1536 /* No explicit request - attempt the next device */
1537 close(dri2_dpy
->fd
);
1540 drmFreeDevices(devices
, num_devices
);
1542 if (dri2_dpy
->fd
< 0) {
1543 _eglLog(_EGL_WARNING
, "Failed to open %s DRM device",
1544 vendor_name
? "desired": "any");
1549 #undef MAX_DRM_DEVICES
1555 dri2_initialize_android(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1558 bool device_opened
= false;
1559 struct dri2_egl_display
*dri2_dpy
;
1563 dri2_dpy
= calloc(1, sizeof(*dri2_dpy
));
1565 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1568 ret
= hw_get_module(GRALLOC_HARDWARE_MODULE_ID
,
1569 (const hw_module_t
**)&dri2_dpy
->gralloc
);
1571 err
= "DRI2: failed to get gralloc module";
1575 disp
->DriverData
= (void *) dri2_dpy
;
1576 if (!disp
->Options
.ForceSoftware
)
1577 device_opened
= droid_open_device(disp
, false);
1579 device_opened
= droid_open_device(disp
, true);
1581 if (!device_opened
) {
1582 err
= "DRI2: failed to open device";
1586 dev
= _eglAddDevice(dri2_dpy
->fd
, false);
1588 err
= "DRI2: failed to find EGLDevice";
1594 if (!dri2_setup_extensions(disp
)) {
1595 err
= "DRI2: failed to setup extensions";
1599 dri2_setup_screen(disp
);
1601 /* We set the maximum swap interval as 1 for Android platform, since it is
1602 * the maximum value supported by Android according to the value of
1603 * ANativeWindow::maxSwapInterval.
1605 dri2_setup_swap_interval(disp
, 1);
1607 disp
->Extensions
.ANDROID_framebuffer_target
= EGL_TRUE
;
1608 disp
->Extensions
.ANDROID_image_native_buffer
= EGL_TRUE
;
1609 disp
->Extensions
.ANDROID_recordable
= EGL_TRUE
;
1610 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1611 disp
->Extensions
.KHR_image
= EGL_TRUE
;
1612 #if ANDROID_API_LEVEL >= 24
1613 if (dri2_dpy
->mutable_render_buffer
&&
1614 dri2_dpy
->loader_extensions
== droid_image_loader_extensions
) {
1615 disp
->Extensions
.KHR_mutable_render_buffer
= EGL_TRUE
;
1619 /* Create configs *after* enabling extensions because presence of DRI
1620 * driver extensions can affect the capabilities of EGLConfigs.
1622 if (!droid_add_configs_for_visuals(drv
, disp
)) {
1623 err
= "DRI2: failed to add configs";
1627 /* Fill vtbl last to prevent accidentally calling virtual function during
1630 dri2_dpy
->vtbl
= &droid_display_vtbl
;
1635 dri2_display_destroy(disp
);
1636 return _eglError(EGL_NOT_INITIALIZED
, err
);