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.
35 #include <sync/sync.h>
39 #include "egl_dri2_fallbacks.h"
41 #ifdef HAVE_DRM_GRALLOC
42 #include <gralloc_drm_handle.h>
43 #include "gralloc_drm.h"
44 #endif /* HAVE_DRM_GRALLOC */
46 #define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1))
48 struct droid_yuv_format
{
50 int native
; /* HAL_PIXEL_FORMAT_ */
51 int is_ycrcb
; /* 0 if chroma order is {Cb, Cr}, 1 if {Cr, Cb} */
52 int chroma_step
; /* Distance in bytes between subsequent chroma pixels. */
55 int fourcc
; /* __DRI_IMAGE_FOURCC_ */
58 /* The following table is used to look up a DRI image FourCC based
59 * on native format and information contained in android_ycbcr struct. */
60 static const struct droid_yuv_format droid_yuv_formats
[] = {
61 /* Native format, YCrCb, Chroma step, DRI image FourCC */
62 { HAL_PIXEL_FORMAT_YCbCr_420_888
, 0, 2, __DRI_IMAGE_FOURCC_NV12
},
63 { HAL_PIXEL_FORMAT_YCbCr_420_888
, 0, 1, __DRI_IMAGE_FOURCC_YUV420
},
64 { HAL_PIXEL_FORMAT_YCbCr_420_888
, 1, 1, __DRI_IMAGE_FOURCC_YVU420
},
65 { HAL_PIXEL_FORMAT_YV12
, 1, 1, __DRI_IMAGE_FOURCC_YVU420
},
66 /* HACK: See droid_create_image_from_prime_fd() and
67 * https://issuetracker.google.com/32077885. */
68 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, 0, 2, __DRI_IMAGE_FOURCC_NV12
},
69 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, 0, 1, __DRI_IMAGE_FOURCC_YUV420
},
70 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, 1, 1, __DRI_IMAGE_FOURCC_YVU420
},
74 get_fourcc_yuv(int native
, int is_ycrcb
, int chroma_step
)
76 for (int i
= 0; i
< ARRAY_SIZE(droid_yuv_formats
); ++i
)
77 if (droid_yuv_formats
[i
].native
== native
&&
78 droid_yuv_formats
[i
].is_ycrcb
== is_ycrcb
&&
79 droid_yuv_formats
[i
].chroma_step
== chroma_step
)
80 return droid_yuv_formats
[i
].fourcc
;
88 for (int i
= 0; i
< ARRAY_SIZE(droid_yuv_formats
); ++i
)
89 if (droid_yuv_formats
[i
].native
== native
)
96 get_format_bpp(int native
)
101 case HAL_PIXEL_FORMAT_RGBA_8888
:
102 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
104 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
105 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
107 case HAL_PIXEL_FORMAT_RGBX_8888
:
108 case HAL_PIXEL_FORMAT_BGRA_8888
:
111 case HAL_PIXEL_FORMAT_RGB_565
:
122 /* createImageFromFds requires fourcc format */
123 static int get_fourcc(int native
)
126 case HAL_PIXEL_FORMAT_RGB_565
: return __DRI_IMAGE_FOURCC_RGB565
;
127 case HAL_PIXEL_FORMAT_BGRA_8888
: return __DRI_IMAGE_FOURCC_ARGB8888
;
128 case HAL_PIXEL_FORMAT_RGBA_8888
: return __DRI_IMAGE_FOURCC_ABGR8888
;
129 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
131 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
132 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
134 case HAL_PIXEL_FORMAT_RGBX_8888
: return __DRI_IMAGE_FOURCC_XBGR8888
;
136 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", native
);
141 static int get_format(int format
)
144 case HAL_PIXEL_FORMAT_BGRA_8888
: return __DRI_IMAGE_FORMAT_ARGB8888
;
145 case HAL_PIXEL_FORMAT_RGB_565
: return __DRI_IMAGE_FORMAT_RGB565
;
146 case HAL_PIXEL_FORMAT_RGBA_8888
: return __DRI_IMAGE_FORMAT_ABGR8888
;
147 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
149 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
150 * TODO: Revert this once https://issuetracker.google.com/32077885 is fixed.
152 case HAL_PIXEL_FORMAT_RGBX_8888
: return __DRI_IMAGE_FORMAT_XBGR8888
;
154 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", format
);
160 get_native_buffer_fd(struct ANativeWindowBuffer
*buf
)
162 native_handle_t
*handle
= (native_handle_t
*)buf
->handle
;
164 * Various gralloc implementations exist, but the dma-buf fd tends
165 * to be first. Access it directly to avoid a dependency on specific
168 return (handle
&& handle
->numFds
) ? handle
->data
[0] : -1;
171 #ifdef HAVE_DRM_GRALLOC
173 get_native_buffer_name(struct ANativeWindowBuffer
*buf
)
175 return gralloc_drm_get_gem_handle(buf
->handle
);
177 #endif /* HAVE_DRM_GRALLOC */
180 droid_window_dequeue_buffer(struct dri2_egl_surface
*dri2_surf
)
184 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
188 /* If access to the buffer is controlled by a sync fence, then block on the
191 * It may be more performant to postpone blocking until there is an
192 * immediate need to write to the buffer. But doing so would require adding
193 * hooks to the DRI2 loader.
195 * From the ANativeWindow::dequeueBuffer documentation:
197 * The libsync fence file descriptor returned in the int pointed to by
198 * the fenceFd argument will refer to the fence that must signal
199 * before the dequeued buffer may be written to. A value of -1
200 * indicates that the caller may access the buffer immediately without
201 * waiting on a fence. If a valid file descriptor is returned (i.e.
202 * any value except -1) then the caller is responsible for closing the
206 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
208 * Waits indefinitely if timeout < 0.
211 sync_wait(fence_fd
, timeout
);
215 dri2_surf
->buffer
->common
.incRef(&dri2_surf
->buffer
->common
);
217 /* Record all the buffers created by ANativeWindow and update back buffer
218 * for updating buffer's age in swap_buffers.
220 EGLBoolean updated
= EGL_FALSE
;
221 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
222 if (!dri2_surf
->color_buffers
[i
].buffer
) {
223 dri2_surf
->color_buffers
[i
].buffer
= dri2_surf
->buffer
;
225 if (dri2_surf
->color_buffers
[i
].buffer
== dri2_surf
->buffer
) {
226 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
233 /* In case of all the buffers were recreated by ANativeWindow, reset
236 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
237 dri2_surf
->color_buffers
[i
].buffer
= NULL
;
238 dri2_surf
->color_buffers
[i
].age
= 0;
240 dri2_surf
->color_buffers
[0].buffer
= dri2_surf
->buffer
;
241 dri2_surf
->back
= &dri2_surf
->color_buffers
[0];
248 droid_window_enqueue_buffer(_EGLDisplay
*disp
, struct dri2_egl_surface
*dri2_surf
)
250 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
252 /* To avoid blocking other EGL calls, release the display mutex before
253 * we enter droid_window_enqueue_buffer() and re-acquire the mutex upon
256 mtx_unlock(&disp
->Mutex
);
258 /* Queue the buffer with stored out fence fd. The ANativeWindow or buffer
259 * consumer may choose to wait for the fence to signal before accessing
260 * it. If fence fd value is -1, buffer can be accessed by consumer
261 * immediately. Consumer or application shouldn't rely on timestamp
262 * associated with fence if the fence fd is -1.
264 * Ownership of fd is transferred to consumer after queueBuffer and the
265 * consumer is responsible for closing it. Caller must not use the fd
266 * after passing it to queueBuffer.
268 int fence_fd
= dri2_surf
->out_fence_fd
;
269 dri2_surf
->out_fence_fd
= -1;
270 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
273 dri2_surf
->buffer
->common
.decRef(&dri2_surf
->buffer
->common
);
274 dri2_surf
->buffer
= NULL
;
275 dri2_surf
->back
= NULL
;
277 mtx_lock(&disp
->Mutex
);
279 if (dri2_surf
->dri_image_back
) {
280 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
281 dri2_surf
->dri_image_back
= NULL
;
288 droid_window_cancel_buffer(struct dri2_egl_surface
*dri2_surf
)
291 int fence_fd
= dri2_surf
->out_fence_fd
;
293 dri2_surf
->out_fence_fd
= -1;
294 ret
= dri2_surf
->window
->cancelBuffer(dri2_surf
->window
,
295 dri2_surf
->buffer
, fence_fd
);
297 _eglLog(_EGL_WARNING
, "ANativeWindow::cancelBuffer failed");
298 dri2_surf
->base
.Lost
= EGL_TRUE
;
303 droid_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
304 _EGLConfig
*conf
, void *native_window
,
305 const EGLint
*attrib_list
)
307 __DRIcreateNewDrawableFunc createNewDrawable
;
308 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
309 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
310 struct dri2_egl_surface
*dri2_surf
;
311 struct ANativeWindow
*window
= native_window
;
312 const __DRIconfig
*config
;
314 dri2_surf
= calloc(1, sizeof *dri2_surf
);
316 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
320 if (!dri2_init_surface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
, true))
321 goto cleanup_surface
;
323 if (type
== EGL_WINDOW_BIT
) {
326 if (window
->common
.magic
!= ANDROID_NATIVE_WINDOW_MAGIC
) {
327 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
328 goto cleanup_surface
;
330 if (window
->query(window
, NATIVE_WINDOW_FORMAT
, &format
)) {
331 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
332 goto cleanup_surface
;
335 if (format
!= dri2_conf
->base
.NativeVisualID
) {
336 _eglLog(_EGL_WARNING
, "Native format mismatch: 0x%x != 0x%x",
337 format
, dri2_conf
->base
.NativeVisualID
);
340 window
->query(window
, NATIVE_WINDOW_WIDTH
, &dri2_surf
->base
.Width
);
341 window
->query(window
, NATIVE_WINDOW_HEIGHT
, &dri2_surf
->base
.Height
);
344 config
= dri2_get_dri_config(dri2_conf
, type
,
345 dri2_surf
->base
.GLColorspace
);
347 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
348 goto cleanup_surface
;
351 if (dri2_dpy
->image_driver
)
352 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
354 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
356 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
358 if (dri2_surf
->dri_drawable
== NULL
) {
359 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
360 goto cleanup_surface
;
364 window
->common
.incRef(&window
->common
);
365 dri2_surf
->window
= window
;
368 return &dri2_surf
->base
;
377 droid_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
378 _EGLConfig
*conf
, void *native_window
,
379 const EGLint
*attrib_list
)
381 return droid_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
382 native_window
, attrib_list
);
386 droid_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
387 _EGLConfig
*conf
, const EGLint
*attrib_list
)
389 return droid_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
394 droid_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
396 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
397 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
399 dri2_egl_surface_free_local_buffers(dri2_surf
);
401 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
402 if (dri2_surf
->buffer
)
403 droid_window_cancel_buffer(dri2_surf
);
405 dri2_surf
->window
->common
.decRef(&dri2_surf
->window
->common
);
408 if (dri2_surf
->dri_image_back
) {
409 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_back", __func__
, __LINE__
);
410 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
411 dri2_surf
->dri_image_back
= NULL
;
414 if (dri2_surf
->dri_image_front
) {
415 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_front", __func__
, __LINE__
);
416 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_front
);
417 dri2_surf
->dri_image_front
= NULL
;
420 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
422 dri2_fini_surface(surf
);
429 droid_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
430 _EGLSurface
*surf
, EGLint interval
)
432 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
433 struct ANativeWindow
*window
= dri2_surf
->window
;
435 if (window
->setSwapInterval(window
, interval
))
438 surf
->SwapInterval
= interval
;
443 update_buffers(struct dri2_egl_surface
*dri2_surf
)
445 if (dri2_surf
->base
.Lost
)
448 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
451 /* try to dequeue the next back buffer */
452 if (!dri2_surf
->buffer
&& !droid_window_dequeue_buffer(dri2_surf
)) {
453 _eglLog(_EGL_WARNING
, "Could not dequeue buffer from native window");
454 dri2_surf
->base
.Lost
= EGL_TRUE
;
458 /* free outdated buffers and update the surface size */
459 if (dri2_surf
->base
.Width
!= dri2_surf
->buffer
->width
||
460 dri2_surf
->base
.Height
!= dri2_surf
->buffer
->height
) {
461 dri2_egl_surface_free_local_buffers(dri2_surf
);
462 dri2_surf
->base
.Width
= dri2_surf
->buffer
->width
;
463 dri2_surf
->base
.Height
= dri2_surf
->buffer
->height
;
470 get_front_bo(struct dri2_egl_surface
*dri2_surf
, unsigned int format
)
472 struct dri2_egl_display
*dri2_dpy
=
473 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
475 if (dri2_surf
->dri_image_front
)
478 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
479 /* According current EGL spec, front buffer rendering
480 * for window surface is not supported now.
481 * and mesa doesn't have the implementation of this case.
482 * Add warning message, but not treat it as error.
484 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported front buffer for window surface");
485 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
486 dri2_surf
->dri_image_front
=
487 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
488 dri2_surf
->base
.Width
,
489 dri2_surf
->base
.Height
,
493 if (!dri2_surf
->dri_image_front
) {
494 _eglLog(_EGL_WARNING
, "dri2_image_front allocation failed");
503 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
505 struct dri2_egl_display
*dri2_dpy
=
506 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
510 if (dri2_surf
->dri_image_back
)
513 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
514 if (!dri2_surf
->buffer
) {
515 _eglLog(_EGL_WARNING
, "Could not get native buffer");
519 fd
= get_native_buffer_fd(dri2_surf
->buffer
);
521 _eglLog(_EGL_WARNING
, "Could not get native buffer FD");
525 fourcc
= get_fourcc(dri2_surf
->buffer
->format
);
527 pitch
= dri2_surf
->buffer
->stride
*
528 get_format_bpp(dri2_surf
->buffer
->format
);
530 if (fourcc
== -1 || pitch
== 0) {
531 _eglLog(_EGL_WARNING
, "Invalid buffer fourcc(%x) or pitch(%d)",
536 dri2_surf
->dri_image_back
=
537 dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
538 dri2_surf
->base
.Width
,
539 dri2_surf
->base
.Height
,
546 if (!dri2_surf
->dri_image_back
) {
547 _eglLog(_EGL_WARNING
, "failed to create DRI image from FD");
550 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
551 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
552 * the spec states that they have a back buffer but no front buffer, in
553 * contrast to pixmaps, which have a front buffer but no back buffer.
555 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
556 * from the spec, following the precedent of Mesa's EGL X11 platform. The
557 * X11 platform correctly assigns pbuffers to single-buffered configs, but
558 * assigns the pbuffer a front buffer instead of a back buffer.
560 * Pbuffers in the X11 platform mostly work today, so let's just copy its
561 * behavior instead of trying to fix (and hence potentially breaking) the
564 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported back buffer for pbuffer surface");
570 /* Some drivers will pass multiple bits in buffer_mask.
571 * For such case, will go through all the bits, and
572 * will not return error when unsupported buffer is requested, only
573 * return error when the allocation for supported buffer failed.
576 droid_image_get_buffers(__DRIdrawable
*driDrawable
,
580 uint32_t buffer_mask
,
581 struct __DRIimageList
*images
)
583 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
585 images
->image_mask
= 0;
586 images
->front
= NULL
;
589 if (update_buffers(dri2_surf
) < 0)
592 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
593 if (get_front_bo(dri2_surf
, format
) < 0)
596 if (dri2_surf
->dri_image_front
) {
597 images
->front
= dri2_surf
->dri_image_front
;
598 images
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
602 if (buffer_mask
& __DRI_IMAGE_BUFFER_BACK
) {
603 if (get_back_bo(dri2_surf
) < 0)
606 if (dri2_surf
->dri_image_back
) {
607 images
->back
= dri2_surf
->dri_image_back
;
608 images
->image_mask
|= __DRI_IMAGE_BUFFER_BACK
;
616 droid_query_buffer_age(_EGLDriver
*drv
,
617 _EGLDisplay
*disp
, _EGLSurface
*surface
)
619 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
621 if (update_buffers(dri2_surf
) < 0) {
622 _eglError(EGL_BAD_ALLOC
, "droid_query_buffer_age");
626 return dri2_surf
->back
? dri2_surf
->back
->age
: 0;
630 droid_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
632 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
633 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
635 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
638 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
639 if (dri2_surf
->color_buffers
[i
].age
> 0)
640 dri2_surf
->color_buffers
[i
].age
++;
643 /* "XXX: we don't use get_back_bo() since it causes regressions in
644 * several dEQP tests.
647 dri2_surf
->back
->age
= 1;
649 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
651 /* dri2_surf->buffer can be null even when no error has occured. For
652 * example, if the user has called no GL rendering commands since the
653 * previous eglSwapBuffers, then the driver may have not triggered
654 * a callback to ANativeWindow::dequeueBuffer, in which case
655 * dri2_surf->buffer remains null.
657 if (dri2_surf
->buffer
)
658 droid_window_enqueue_buffer(disp
, dri2_surf
);
660 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
665 #if ANDROID_API_LEVEL >= 23
667 droid_set_damage_region(_EGLDriver
*drv
,
669 _EGLSurface
*draw
, const EGLint
* rects
, EGLint n_rects
)
671 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
672 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
673 android_native_rect_t
* droid_rects
= NULL
;
679 droid_rects
= malloc(n_rects
* sizeof(android_native_rect_t
));
680 if (droid_rects
== NULL
)
681 return _eglError(EGL_BAD_ALLOC
, "eglSetDamageRegionKHR");
683 for (EGLint num_drects
= 0; num_drects
< n_rects
; num_drects
++) {
684 EGLint i
= num_drects
* 4;
685 droid_rects
[num_drects
].left
= rects
[i
];
686 droid_rects
[num_drects
].bottom
= rects
[i
+ 1];
687 droid_rects
[num_drects
].right
= rects
[i
] + rects
[i
+ 2];
688 droid_rects
[num_drects
].top
= rects
[i
+ 1] + rects
[i
+ 3];
692 * XXX/TODO: Need to check for other return values
695 ret
= native_window_set_surface_damage(dri2_surf
->window
, droid_rects
, n_rects
);
698 return ret
== 0 ? EGL_TRUE
: EGL_FALSE
;
703 droid_create_image_from_prime_fd_yuv(_EGLDisplay
*disp
, _EGLContext
*ctx
,
704 struct ANativeWindowBuffer
*buf
, int fd
)
706 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
707 struct android_ycbcr ycbcr
;
714 if (!dri2_dpy
->gralloc
->lock_ycbcr
) {
715 _eglLog(_EGL_WARNING
, "Gralloc does not support lock_ycbcr");
719 memset(&ycbcr
, 0, sizeof(ycbcr
));
720 ret
= dri2_dpy
->gralloc
->lock_ycbcr(dri2_dpy
->gralloc
, buf
->handle
,
721 0, 0, 0, 0, 0, &ycbcr
);
723 /* HACK: See droid_create_image_from_prime_fd() and
724 * https://issuetracker.google.com/32077885.*/
725 if (buf
->format
== HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
)
728 _eglLog(_EGL_WARNING
, "gralloc->lock_ycbcr failed: %d", ret
);
731 dri2_dpy
->gralloc
->unlock(dri2_dpy
->gralloc
, buf
->handle
);
733 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
734 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
735 * so they can be interpreted as offsets. */
736 offsets
[0] = (size_t)ycbcr
.y
;
737 /* We assume here that all the planes are located in one DMA-buf. */
738 is_ycrcb
= (size_t)ycbcr
.cr
< (size_t)ycbcr
.cb
;
740 offsets
[1] = (size_t)ycbcr
.cr
;
741 offsets
[2] = (size_t)ycbcr
.cb
;
743 offsets
[1] = (size_t)ycbcr
.cb
;
744 offsets
[2] = (size_t)ycbcr
.cr
;
747 /* .ystride is the line length (in bytes) of the Y plane,
748 * .cstride is the line length (in bytes) of any of the remaining
749 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
751 pitches
[0] = ycbcr
.ystride
;
752 pitches
[1] = pitches
[2] = ycbcr
.cstride
;
754 /* .chroma_step is the byte distance between the same chroma channel
755 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
756 fourcc
= get_fourcc_yuv(buf
->format
, is_ycrcb
, ycbcr
.chroma_step
);
758 _eglLog(_EGL_WARNING
, "unsupported YUV format, native = %x, is_ycrcb = %d, chroma_step = %d",
759 buf
->format
, is_ycrcb
, ycbcr
.chroma_step
);
763 if (ycbcr
.chroma_step
== 2) {
764 /* Semi-planar Y + CbCr or Y + CrCb format. */
765 const EGLint attr_list_2plane
[] = {
766 EGL_WIDTH
, buf
->width
,
767 EGL_HEIGHT
, buf
->height
,
768 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
769 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
770 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
771 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, offsets
[0],
772 EGL_DMA_BUF_PLANE1_FD_EXT
, fd
,
773 EGL_DMA_BUF_PLANE1_PITCH_EXT
, pitches
[1],
774 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, offsets
[1],
778 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_2plane
);
780 /* Fully planar Y + Cb + Cr or Y + Cr + Cb format. */
781 const EGLint attr_list_3plane
[] = {
782 EGL_WIDTH
, buf
->width
,
783 EGL_HEIGHT
, buf
->height
,
784 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
785 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
786 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
787 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, offsets
[0],
788 EGL_DMA_BUF_PLANE1_FD_EXT
, fd
,
789 EGL_DMA_BUF_PLANE1_PITCH_EXT
, pitches
[1],
790 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, offsets
[1],
791 EGL_DMA_BUF_PLANE2_FD_EXT
, fd
,
792 EGL_DMA_BUF_PLANE2_PITCH_EXT
, pitches
[2],
793 EGL_DMA_BUF_PLANE2_OFFSET_EXT
, offsets
[2],
797 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_3plane
);
802 droid_create_image_from_prime_fd(_EGLDisplay
*disp
, _EGLContext
*ctx
,
803 struct ANativeWindowBuffer
*buf
, int fd
)
807 if (is_yuv(buf
->format
)) {
810 image
= droid_create_image_from_prime_fd_yuv(disp
, ctx
, buf
, fd
);
812 * HACK: https://issuetracker.google.com/32077885
813 * There is no API available to properly query the IMPLEMENTATION_DEFINED
814 * format. As a workaround we rely here on gralloc allocating either
815 * an arbitrary YCbCr 4:2:0 or RGBX_8888, with the latter being recognized
816 * by lock_ycbcr failing.
818 if (image
|| buf
->format
!= HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
)
822 const int fourcc
= get_fourcc(buf
->format
);
824 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
828 pitch
= buf
->stride
* get_format_bpp(buf
->format
);
830 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
834 const EGLint attr_list
[] = {
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
, pitch
,
840 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, 0,
844 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list
);
847 #ifdef HAVE_DRM_GRALLOC
849 droid_create_image_from_name(_EGLDisplay
*disp
, _EGLContext
*ctx
,
850 struct ANativeWindowBuffer
*buf
)
852 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
853 struct dri2_egl_image
*dri2_img
;
857 name
= get_native_buffer_name(buf
);
859 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
863 format
= get_format(buf
->format
);
867 dri2_img
= calloc(1, sizeof(*dri2_img
));
869 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
873 _eglInitImage(&dri2_img
->base
, disp
);
875 dri2_img
->dri_image
=
876 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
883 if (!dri2_img
->dri_image
) {
885 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
889 return &dri2_img
->base
;
891 #endif /* HAVE_DRM_GRALLOC */
894 droid_query_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
895 EGLint attribute
, EGLint
*value
)
897 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
900 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
901 dri2_surf
->window
->query(dri2_surf
->window
,
902 NATIVE_WINDOW_DEFAULT_WIDTH
, value
);
907 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
908 dri2_surf
->window
->query(dri2_surf
->window
,
909 NATIVE_WINDOW_DEFAULT_HEIGHT
, value
);
916 return _eglQuerySurface(drv
, dpy
, surf
, attribute
, value
);
920 dri2_create_image_android_native_buffer(_EGLDisplay
*disp
,
922 struct ANativeWindowBuffer
*buf
)
927 /* From the EGL_ANDROID_image_native_buffer spec:
929 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
930 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
932 _eglError(EGL_BAD_CONTEXT
, "eglCreateEGLImageKHR: for "
933 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
938 if (!buf
|| buf
->common
.magic
!= ANDROID_NATIVE_BUFFER_MAGIC
||
939 buf
->common
.version
!= sizeof(*buf
)) {
940 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
944 fd
= get_native_buffer_fd(buf
);
946 return droid_create_image_from_prime_fd(disp
, ctx
, buf
, fd
);
948 #ifdef HAVE_DRM_GRALLOC
949 return droid_create_image_from_name(disp
, ctx
, buf
);
956 droid_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
957 _EGLContext
*ctx
, EGLenum target
,
958 EGLClientBuffer buffer
, const EGLint
*attr_list
)
961 case EGL_NATIVE_BUFFER_ANDROID
:
962 return dri2_create_image_android_native_buffer(disp
, ctx
,
963 (struct ANativeWindowBuffer
*) buffer
);
965 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
970 droid_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
974 #ifdef HAVE_DRM_GRALLOC
976 droid_get_buffers_parse_attachments(struct dri2_egl_surface
*dri2_surf
,
977 unsigned int *attachments
, int count
)
981 /* fill dri2_surf->buffers */
982 for (int i
= 0; i
< count
* 2; i
+= 2) {
983 __DRIbuffer
*buf
, *local
;
985 assert(num_buffers
< ARRAY_SIZE(dri2_surf
->buffers
));
986 buf
= &dri2_surf
->buffers
[num_buffers
];
988 switch (attachments
[i
]) {
989 case __DRI_BUFFER_BACK_LEFT
:
990 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
991 buf
->attachment
= attachments
[i
];
992 buf
->name
= get_native_buffer_name(dri2_surf
->buffer
);
993 buf
->cpp
= get_format_bpp(dri2_surf
->buffer
->format
);
994 buf
->pitch
= dri2_surf
->buffer
->stride
* buf
->cpp
;
1002 /* fall through for pbuffers */
1003 case __DRI_BUFFER_DEPTH
:
1004 case __DRI_BUFFER_STENCIL
:
1005 case __DRI_BUFFER_ACCUM
:
1006 case __DRI_BUFFER_DEPTH_STENCIL
:
1007 case __DRI_BUFFER_HIZ
:
1008 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
,
1009 attachments
[i
], attachments
[i
+ 1]);
1016 case __DRI_BUFFER_FRONT_LEFT
:
1017 case __DRI_BUFFER_FRONT_RIGHT
:
1018 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
1019 case __DRI_BUFFER_FAKE_FRONT_RIGHT
:
1020 case __DRI_BUFFER_BACK_RIGHT
:
1022 /* no front or right buffers */
1030 static __DRIbuffer
*
1031 droid_get_buffers_with_format(__DRIdrawable
* driDrawable
,
1032 int *width
, int *height
,
1033 unsigned int *attachments
, int count
,
1034 int *out_count
, void *loaderPrivate
)
1036 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1038 if (update_buffers(dri2_surf
) < 0)
1041 *out_count
= droid_get_buffers_parse_attachments(dri2_surf
, attachments
, count
);
1044 *width
= dri2_surf
->base
.Width
;
1046 *height
= dri2_surf
->base
.Height
;
1048 return dri2_surf
->buffers
;
1050 #endif /* HAVE_DRM_GRALLOC */
1053 droid_get_capability(void *loaderPrivate
, enum dri_loader_cap cap
)
1055 /* Note: loaderPrivate is _EGLDisplay* */
1057 case DRI_LOADER_CAP_RGBA_ORDERING
:
1065 droid_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
1067 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1068 static const struct {
1070 unsigned int rgba_masks
[4];
1072 { HAL_PIXEL_FORMAT_RGBA_8888
, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 } },
1073 { HAL_PIXEL_FORMAT_RGBX_8888
, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 } },
1074 { HAL_PIXEL_FORMAT_RGB_565
, { 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 } },
1075 { HAL_PIXEL_FORMAT_BGRA_8888
, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 } },
1078 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1079 int config_count
= 0;
1081 /* The nesting of loops is significant here. Also significant is the order
1082 * of the HAL pixel formats. Many Android apps (such as Google's official
1083 * NDK GLES2 example app), and even portions the core framework code (such
1084 * as SystemServiceManager in Nougat), incorrectly choose their EGLConfig.
1085 * They neglect to match the EGLConfig's EGL_NATIVE_VISUAL_ID against the
1086 * window's native format, and instead choose the first EGLConfig whose
1087 * channel sizes match those of the native window format while ignoring the
1088 * channel *ordering*.
1090 * We can detect such buggy clients in logcat when they call
1091 * eglCreateSurface, by detecting the mismatch between the EGLConfig's
1092 * format and the window's format.
1094 * As a workaround, we generate EGLConfigs such that all EGLConfigs for HAL
1095 * pixel format i precede those for HAL pixel format i+1. In my
1096 * (chadversary) testing on Android Nougat, this was good enough to pacify
1097 * the buggy clients.
1099 for (int i
= 0; i
< ARRAY_SIZE(visuals
); i
++) {
1100 for (int j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
1101 const EGLint surface_type
= EGL_WINDOW_BIT
| EGL_PBUFFER_BIT
;
1103 const EGLint config_attrs
[] = {
1104 EGL_NATIVE_VISUAL_ID
, visuals
[i
].format
,
1105 EGL_NATIVE_VISUAL_TYPE
, visuals
[i
].format
,
1106 EGL_FRAMEBUFFER_TARGET_ANDROID
, EGL_TRUE
,
1107 EGL_RECORDABLE_ANDROID
, EGL_TRUE
,
1111 struct dri2_egl_config
*dri2_conf
=
1112 dri2_add_config(dpy
, dri2_dpy
->driver_configs
[j
],
1113 config_count
+ 1, surface_type
, config_attrs
,
1114 visuals
[i
].rgba_masks
);
1116 if (dri2_conf
->base
.ConfigID
== config_count
+ 1)
1123 for (int i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1124 if (!format_count
[i
]) {
1125 _eglLog(_EGL_DEBUG
, "No DRI config supports native format 0x%x",
1130 return (config_count
!= 0);
1134 /* perform(const struct gralloc_module_t *mod,
1138 GRALLOC_MODULE_PERFORM_GET_DRM_FD
= 0x40000002,
1142 droid_open_device(struct dri2_egl_display
*dri2_dpy
)
1144 int fd
= -1, err
= -EINVAL
;
1146 if (dri2_dpy
->gralloc
->perform
)
1147 err
= dri2_dpy
->gralloc
->perform(dri2_dpy
->gralloc
,
1148 GRALLOC_MODULE_PERFORM_GET_DRM_FD
,
1150 if (err
|| fd
< 0) {
1151 _eglLog(_EGL_WARNING
, "fail to get drm fd");
1155 return (fd
>= 0) ? fcntl(fd
, F_DUPFD_CLOEXEC
, 3) : -1;
1158 static const struct dri2_egl_display_vtbl droid_display_vtbl
= {
1159 .authenticate
= NULL
,
1160 .create_window_surface
= droid_create_window_surface
,
1161 .create_pixmap_surface
= dri2_fallback_create_pixmap_surface
,
1162 .create_pbuffer_surface
= droid_create_pbuffer_surface
,
1163 .destroy_surface
= droid_destroy_surface
,
1164 .create_image
= droid_create_image_khr
,
1165 .swap_buffers
= droid_swap_buffers
,
1166 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
, /* Android implements the function */
1167 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1168 .swap_interval
= droid_swap_interval
,
1169 #if ANDROID_API_LEVEL >= 23
1170 .set_damage_region
= droid_set_damage_region
,
1172 .set_damage_region
= dri2_fallback_set_damage_region
,
1174 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1175 .copy_buffers
= dri2_fallback_copy_buffers
,
1176 .query_buffer_age
= droid_query_buffer_age
,
1177 .query_surface
= droid_query_surface
,
1178 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1179 .get_sync_values
= dri2_fallback_get_sync_values
,
1180 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1183 #ifdef HAVE_DRM_GRALLOC
1184 static const __DRIdri2LoaderExtension droid_dri2_loader_extension
= {
1185 .base
= { __DRI_DRI2_LOADER
, 4 },
1188 .flushFrontBuffer
= droid_flush_front_buffer
,
1189 .getBuffersWithFormat
= droid_get_buffers_with_format
,
1190 .getCapability
= droid_get_capability
,
1192 #endif /* HAVE_DRM_GRALLOC */
1194 static const __DRIimageLoaderExtension droid_image_loader_extension
= {
1195 .base
= { __DRI_IMAGE_LOADER
, 2 },
1197 .getBuffers
= droid_image_get_buffers
,
1198 .flushFrontBuffer
= droid_flush_front_buffer
,
1199 .getCapability
= droid_get_capability
,
1202 #ifdef HAVE_DRM_GRALLOC
1203 static const __DRIextension
*droid_dri2_loader_extensions
[] = {
1204 &droid_dri2_loader_extension
.base
,
1205 &image_lookup_extension
.base
,
1206 &use_invalidate
.base
,
1209 #endif /* HAVE_DRM_GRALLOC */
1211 static const __DRIextension
*droid_image_loader_extensions
[] = {
1212 &droid_image_loader_extension
.base
,
1213 &image_lookup_extension
.base
,
1214 &use_invalidate
.base
,
1219 dri2_initialize_android(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1221 struct dri2_egl_display
*dri2_dpy
;
1225 /* Not supported yet */
1226 if (disp
->Options
.ForceSoftware
)
1229 loader_set_logger(_eglLog
);
1231 dri2_dpy
= calloc(1, sizeof(*dri2_dpy
));
1233 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1236 ret
= hw_get_module(GRALLOC_HARDWARE_MODULE_ID
,
1237 (const hw_module_t
**)&dri2_dpy
->gralloc
);
1239 err
= "DRI2: failed to get gralloc module";
1243 disp
->DriverData
= (void *) dri2_dpy
;
1245 dri2_dpy
->fd
= droid_open_device(dri2_dpy
);
1246 if (dri2_dpy
->fd
< 0) {
1247 err
= "DRI2: failed to open device";
1251 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1252 if (dri2_dpy
->driver_name
== NULL
) {
1253 err
= "DRI2: failed to get driver name";
1257 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1259 if (!dri2_dpy
->is_render_node
) {
1260 #ifdef HAVE_DRM_GRALLOC
1261 /* Handle control nodes using __DRI_DRI2_LOADER extension and GEM names
1262 * for backwards compatibility with drm_gralloc. (Do not use on new
1264 dri2_dpy
->loader_extensions
= droid_dri2_loader_extensions
;
1265 if (!dri2_load_driver(disp
)) {
1266 err
= "DRI2: failed to load driver";
1270 err
= "DRI2: handle is not for a render node";
1274 dri2_dpy
->loader_extensions
= droid_image_loader_extensions
;
1275 if (!dri2_load_driver_dri3(disp
)) {
1276 err
= "DRI3: failed to load driver";
1281 if (!dri2_create_screen(disp
)) {
1282 err
= "DRI2: failed to create screen";
1286 if (!dri2_setup_extensions(disp
)) {
1287 err
= "DRI2: failed to setup extensions";
1291 dri2_setup_screen(disp
);
1293 /* We set the maximum swap interval as 1 for Android platform, since it is
1294 * the maximum value supported by Android according to the value of
1295 * ANativeWindow::maxSwapInterval.
1297 dri2_setup_swap_interval(disp
, 1);
1299 if (!droid_add_configs_for_visuals(drv
, disp
)) {
1300 err
= "DRI2: failed to add configs";
1304 disp
->Extensions
.ANDROID_framebuffer_target
= EGL_TRUE
;
1305 disp
->Extensions
.ANDROID_image_native_buffer
= EGL_TRUE
;
1306 disp
->Extensions
.ANDROID_recordable
= EGL_TRUE
;
1307 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1308 #if ANDROID_API_LEVEL >= 23
1309 disp
->Extensions
.KHR_partial_update
= EGL_TRUE
;
1311 disp
->Extensions
.KHR_image
= EGL_TRUE
;
1313 /* Fill vtbl last to prevent accidentally calling virtual function during
1316 dri2_dpy
->vtbl
= &droid_display_vtbl
;
1321 dri2_display_destroy(disp
);
1322 return _eglError(EGL_NOT_INITIALIZED
, err
);