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"
40 #include "gralloc_drm.h"
42 #define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1))
44 struct droid_yuv_format
{
46 int native
; /* HAL_PIXEL_FORMAT_ */
47 int is_ycrcb
; /* 0 if chroma order is {Cb, Cr}, 1 if {Cr, Cb} */
48 int chroma_step
; /* Distance in bytes between subsequent chroma pixels. */
51 int fourcc
; /* __DRI_IMAGE_FOURCC_ */
54 /* The following table is used to look up a DRI image FourCC based
55 * on native format and information contained in android_ycbcr struct. */
56 static const struct droid_yuv_format droid_yuv_formats
[] = {
57 /* Native format, YCrCb, Chroma step, DRI image FourCC */
58 { HAL_PIXEL_FORMAT_YCbCr_420_888
, 0, 2, __DRI_IMAGE_FOURCC_NV12
},
59 { HAL_PIXEL_FORMAT_YCbCr_420_888
, 0, 1, __DRI_IMAGE_FOURCC_YUV420
},
60 { HAL_PIXEL_FORMAT_YCbCr_420_888
, 1, 1, __DRI_IMAGE_FOURCC_YVU420
},
61 { HAL_PIXEL_FORMAT_YV12
, 1, 1, __DRI_IMAGE_FOURCC_YVU420
},
62 /* HACK: See droid_create_image_from_prime_fd() and
63 * https://issuetracker.google.com/32077885. */
64 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, 0, 2, __DRI_IMAGE_FOURCC_NV12
},
65 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, 0, 1, __DRI_IMAGE_FOURCC_YUV420
},
66 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, 1, 1, __DRI_IMAGE_FOURCC_YVU420
},
70 get_fourcc_yuv(int native
, int is_ycrcb
, int chroma_step
)
72 for (int i
= 0; i
< ARRAY_SIZE(droid_yuv_formats
); ++i
)
73 if (droid_yuv_formats
[i
].native
== native
&&
74 droid_yuv_formats
[i
].is_ycrcb
== is_ycrcb
&&
75 droid_yuv_formats
[i
].chroma_step
== chroma_step
)
76 return droid_yuv_formats
[i
].fourcc
;
84 for (int i
= 0; i
< ARRAY_SIZE(droid_yuv_formats
); ++i
)
85 if (droid_yuv_formats
[i
].native
== native
)
92 get_format_bpp(int native
)
97 case HAL_PIXEL_FORMAT_RGBA_8888
:
98 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
100 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
101 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
103 case HAL_PIXEL_FORMAT_RGBX_8888
:
104 case HAL_PIXEL_FORMAT_BGRA_8888
:
107 case HAL_PIXEL_FORMAT_RGB_565
:
118 /* createImageFromFds requires fourcc format */
119 static int get_fourcc(int native
)
122 case HAL_PIXEL_FORMAT_RGB_565
: return __DRI_IMAGE_FOURCC_RGB565
;
123 case HAL_PIXEL_FORMAT_BGRA_8888
: return __DRI_IMAGE_FOURCC_ARGB8888
;
124 case HAL_PIXEL_FORMAT_RGBA_8888
: return __DRI_IMAGE_FOURCC_ABGR8888
;
125 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
127 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
128 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
130 case HAL_PIXEL_FORMAT_RGBX_8888
: return __DRI_IMAGE_FOURCC_XBGR8888
;
132 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", native
);
137 static int get_format(int format
)
140 case HAL_PIXEL_FORMAT_BGRA_8888
: return __DRI_IMAGE_FORMAT_ARGB8888
;
141 case HAL_PIXEL_FORMAT_RGB_565
: return __DRI_IMAGE_FORMAT_RGB565
;
142 case HAL_PIXEL_FORMAT_RGBA_8888
: return __DRI_IMAGE_FORMAT_ABGR8888
;
143 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
145 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
146 * TODO: Revert this once https://issuetracker.google.com/32077885 is fixed.
148 case HAL_PIXEL_FORMAT_RGBX_8888
: return __DRI_IMAGE_FORMAT_XBGR8888
;
150 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", format
);
156 get_native_buffer_fd(struct ANativeWindowBuffer
*buf
)
158 native_handle_t
*handle
= (native_handle_t
*)buf
->handle
;
160 * Various gralloc implementations exist, but the dma-buf fd tends
161 * to be first. Access it directly to avoid a dependency on specific
164 return (handle
&& handle
->numFds
) ? handle
->data
[0] : -1;
168 get_native_buffer_name(struct ANativeWindowBuffer
*buf
)
170 return gralloc_drm_get_gem_handle(buf
->handle
);
174 droid_window_dequeue_buffer(struct dri2_egl_surface
*dri2_surf
)
178 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
182 /* If access to the buffer is controlled by a sync fence, then block on the
185 * It may be more performant to postpone blocking until there is an
186 * immediate need to write to the buffer. But doing so would require adding
187 * hooks to the DRI2 loader.
189 * From the ANativeWindow::dequeueBuffer documentation:
191 * The libsync fence file descriptor returned in the int pointed to by
192 * the fenceFd argument will refer to the fence that must signal
193 * before the dequeued buffer may be written to. A value of -1
194 * indicates that the caller may access the buffer immediately without
195 * waiting on a fence. If a valid file descriptor is returned (i.e.
196 * any value except -1) then the caller is responsible for closing the
200 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
202 * Waits indefinitely if timeout < 0.
205 sync_wait(fence_fd
, timeout
);
209 dri2_surf
->buffer
->common
.incRef(&dri2_surf
->buffer
->common
);
211 /* Record all the buffers created by ANativeWindow and update back buffer
212 * for updating buffer's age in swap_buffers.
214 EGLBoolean updated
= EGL_FALSE
;
215 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
216 if (!dri2_surf
->color_buffers
[i
].buffer
) {
217 dri2_surf
->color_buffers
[i
].buffer
= dri2_surf
->buffer
;
219 if (dri2_surf
->color_buffers
[i
].buffer
== dri2_surf
->buffer
) {
220 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
227 /* In case of all the buffers were recreated by ANativeWindow, reset
230 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
231 dri2_surf
->color_buffers
[i
].buffer
= NULL
;
232 dri2_surf
->color_buffers
[i
].age
= 0;
234 dri2_surf
->color_buffers
[0].buffer
= dri2_surf
->buffer
;
235 dri2_surf
->back
= &dri2_surf
->color_buffers
[0];
242 droid_window_enqueue_buffer(_EGLDisplay
*disp
, struct dri2_egl_surface
*dri2_surf
)
244 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
246 /* To avoid blocking other EGL calls, release the display mutex before
247 * we enter droid_window_enqueue_buffer() and re-acquire the mutex upon
250 mtx_unlock(&disp
->Mutex
);
252 /* Queue the buffer with stored out fence fd. The ANativeWindow or buffer
253 * consumer may choose to wait for the fence to signal before accessing
254 * it. If fence fd value is -1, buffer can be accessed by consumer
255 * immediately. Consumer or application shouldn't rely on timestamp
256 * associated with fence if the fence fd is -1.
258 * Ownership of fd is transferred to consumer after queueBuffer and the
259 * consumer is responsible for closing it. Caller must not use the fd
260 * after passing it to queueBuffer.
262 int fence_fd
= dri2_surf
->out_fence_fd
;
263 dri2_surf
->out_fence_fd
= -1;
264 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
267 dri2_surf
->buffer
->common
.decRef(&dri2_surf
->buffer
->common
);
268 dri2_surf
->buffer
= NULL
;
269 dri2_surf
->back
= NULL
;
271 mtx_lock(&disp
->Mutex
);
273 if (dri2_surf
->dri_image_back
) {
274 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
275 dri2_surf
->dri_image_back
= NULL
;
282 droid_window_cancel_buffer(struct dri2_egl_surface
*dri2_surf
)
285 int fence_fd
= dri2_surf
->out_fence_fd
;
287 dri2_surf
->out_fence_fd
= -1;
288 ret
= dri2_surf
->window
->cancelBuffer(dri2_surf
->window
,
289 dri2_surf
->buffer
, fence_fd
);
291 _eglLog(_EGL_WARNING
, "ANativeWindow::cancelBuffer failed");
292 dri2_surf
->base
.Lost
= EGL_TRUE
;
297 droid_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
298 _EGLConfig
*conf
, void *native_window
,
299 const EGLint
*attrib_list
)
301 __DRIcreateNewDrawableFunc createNewDrawable
;
302 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
303 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
304 struct dri2_egl_surface
*dri2_surf
;
305 struct ANativeWindow
*window
= native_window
;
306 const __DRIconfig
*config
;
308 dri2_surf
= calloc(1, sizeof *dri2_surf
);
310 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
314 if (!dri2_init_surface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
, true))
315 goto cleanup_surface
;
317 if (type
== EGL_WINDOW_BIT
) {
320 if (window
->common
.magic
!= ANDROID_NATIVE_WINDOW_MAGIC
) {
321 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
322 goto cleanup_surface
;
324 if (window
->query(window
, NATIVE_WINDOW_FORMAT
, &format
)) {
325 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
326 goto cleanup_surface
;
329 if (format
!= dri2_conf
->base
.NativeVisualID
) {
330 _eglLog(_EGL_WARNING
, "Native format mismatch: 0x%x != 0x%x",
331 format
, dri2_conf
->base
.NativeVisualID
);
334 window
->query(window
, NATIVE_WINDOW_WIDTH
, &dri2_surf
->base
.Width
);
335 window
->query(window
, NATIVE_WINDOW_HEIGHT
, &dri2_surf
->base
.Height
);
338 config
= dri2_get_dri_config(dri2_conf
, type
,
339 dri2_surf
->base
.GLColorspace
);
341 goto cleanup_surface
;
343 if (dri2_dpy
->image_driver
)
344 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
346 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
348 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
350 if (dri2_surf
->dri_drawable
== NULL
) {
351 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
352 goto cleanup_surface
;
356 window
->common
.incRef(&window
->common
);
357 dri2_surf
->window
= window
;
360 return &dri2_surf
->base
;
369 droid_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
370 _EGLConfig
*conf
, void *native_window
,
371 const EGLint
*attrib_list
)
373 return droid_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
374 native_window
, attrib_list
);
378 droid_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
379 _EGLConfig
*conf
, const EGLint
*attrib_list
)
381 return droid_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
386 droid_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
388 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
389 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
391 dri2_egl_surface_free_local_buffers(dri2_surf
);
393 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
394 if (dri2_surf
->buffer
)
395 droid_window_cancel_buffer(dri2_surf
);
397 dri2_surf
->window
->common
.decRef(&dri2_surf
->window
->common
);
400 if (dri2_surf
->dri_image_back
) {
401 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_back", __func__
, __LINE__
);
402 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
403 dri2_surf
->dri_image_back
= NULL
;
406 if (dri2_surf
->dri_image_front
) {
407 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_front", __func__
, __LINE__
);
408 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_front
);
409 dri2_surf
->dri_image_front
= NULL
;
412 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
414 dri2_fini_surface(surf
);
421 update_buffers(struct dri2_egl_surface
*dri2_surf
)
423 if (dri2_surf
->base
.Lost
)
426 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
429 /* try to dequeue the next back buffer */
430 if (!dri2_surf
->buffer
&& !droid_window_dequeue_buffer(dri2_surf
)) {
431 _eglLog(_EGL_WARNING
, "Could not dequeue buffer from native window");
432 dri2_surf
->base
.Lost
= EGL_TRUE
;
436 /* free outdated buffers and update the surface size */
437 if (dri2_surf
->base
.Width
!= dri2_surf
->buffer
->width
||
438 dri2_surf
->base
.Height
!= dri2_surf
->buffer
->height
) {
439 dri2_egl_surface_free_local_buffers(dri2_surf
);
440 dri2_surf
->base
.Width
= dri2_surf
->buffer
->width
;
441 dri2_surf
->base
.Height
= dri2_surf
->buffer
->height
;
448 get_front_bo(struct dri2_egl_surface
*dri2_surf
, unsigned int format
)
450 struct dri2_egl_display
*dri2_dpy
=
451 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
453 if (dri2_surf
->dri_image_front
)
456 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
457 /* According current EGL spec, front buffer rendering
458 * for window surface is not supported now.
459 * and mesa doesn't have the implementation of this case.
460 * Add warning message, but not treat it as error.
462 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported front buffer for window surface");
463 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
464 dri2_surf
->dri_image_front
=
465 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
466 dri2_surf
->base
.Width
,
467 dri2_surf
->base
.Height
,
471 if (!dri2_surf
->dri_image_front
) {
472 _eglLog(_EGL_WARNING
, "dri2_image_front allocation failed");
481 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
483 struct dri2_egl_display
*dri2_dpy
=
484 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
488 if (dri2_surf
->dri_image_back
)
491 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
492 if (!dri2_surf
->buffer
) {
493 _eglLog(_EGL_WARNING
, "Could not get native buffer");
497 fd
= get_native_buffer_fd(dri2_surf
->buffer
);
499 _eglLog(_EGL_WARNING
, "Could not get native buffer FD");
503 fourcc
= get_fourcc(dri2_surf
->buffer
->format
);
505 pitch
= dri2_surf
->buffer
->stride
*
506 get_format_bpp(dri2_surf
->buffer
->format
);
508 if (fourcc
== -1 || pitch
== 0) {
509 _eglLog(_EGL_WARNING
, "Invalid buffer fourcc(%x) or pitch(%d)",
514 dri2_surf
->dri_image_back
=
515 dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
516 dri2_surf
->base
.Width
,
517 dri2_surf
->base
.Height
,
524 if (!dri2_surf
->dri_image_back
) {
525 _eglLog(_EGL_WARNING
, "failed to create DRI image from FD");
528 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
529 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
530 * the spec states that they have a back buffer but no front buffer, in
531 * contrast to pixmaps, which have a front buffer but no back buffer.
533 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
534 * from the spec, following the precedent of Mesa's EGL X11 platform. The
535 * X11 platform correctly assigns pbuffers to single-buffered configs, but
536 * assigns the pbuffer a front buffer instead of a back buffer.
538 * Pbuffers in the X11 platform mostly work today, so let's just copy its
539 * behavior instead of trying to fix (and hence potentially breaking) the
542 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported back buffer for pbuffer surface");
548 /* Some drivers will pass multiple bits in buffer_mask.
549 * For such case, will go through all the bits, and
550 * will not return error when unsupported buffer is requested, only
551 * return error when the allocation for supported buffer failed.
554 droid_image_get_buffers(__DRIdrawable
*driDrawable
,
558 uint32_t buffer_mask
,
559 struct __DRIimageList
*images
)
561 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
563 images
->image_mask
= 0;
564 images
->front
= NULL
;
567 if (update_buffers(dri2_surf
) < 0)
570 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
571 if (get_front_bo(dri2_surf
, format
) < 0)
574 if (dri2_surf
->dri_image_front
) {
575 images
->front
= dri2_surf
->dri_image_front
;
576 images
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
580 if (buffer_mask
& __DRI_IMAGE_BUFFER_BACK
) {
581 if (get_back_bo(dri2_surf
) < 0)
584 if (dri2_surf
->dri_image_back
) {
585 images
->back
= dri2_surf
->dri_image_back
;
586 images
->image_mask
|= __DRI_IMAGE_BUFFER_BACK
;
594 droid_query_buffer_age(_EGLDriver
*drv
,
595 _EGLDisplay
*disp
, _EGLSurface
*surface
)
597 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
599 if (update_buffers(dri2_surf
) < 0) {
600 _eglError(EGL_BAD_ALLOC
, "droid_query_buffer_age");
604 return dri2_surf
->back
? dri2_surf
->back
->age
: 0;
608 droid_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
610 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
611 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
613 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
616 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
617 if (dri2_surf
->color_buffers
[i
].age
> 0)
618 dri2_surf
->color_buffers
[i
].age
++;
621 /* "XXX: we don't use get_back_bo() since it causes regressions in
622 * several dEQP tests.
625 dri2_surf
->back
->age
= 1;
627 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
629 /* dri2_surf->buffer can be null even when no error has occured. For
630 * example, if the user has called no GL rendering commands since the
631 * previous eglSwapBuffers, then the driver may have not triggered
632 * a callback to ANativeWindow::dequeueBuffer, in which case
633 * dri2_surf->buffer remains null.
635 if (dri2_surf
->buffer
)
636 droid_window_enqueue_buffer(disp
, dri2_surf
);
638 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
643 #if ANDROID_API_LEVEL >= 23
645 droid_set_damage_region(_EGLDriver
*drv
,
647 _EGLSurface
*draw
, const EGLint
* rects
, EGLint n_rects
)
649 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
650 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
651 android_native_rect_t
* droid_rects
= NULL
;
657 droid_rects
= malloc(n_rects
* sizeof(android_native_rect_t
));
658 if (droid_rects
== NULL
)
659 return _eglError(EGL_BAD_ALLOC
, "eglSetDamageRegionKHR");
661 for (EGLint num_drects
= 0; num_drects
< n_rects
; num_drects
++) {
662 EGLint i
= num_drects
* 4;
663 droid_rects
[num_drects
].left
= rects
[i
];
664 droid_rects
[num_drects
].bottom
= rects
[i
+ 1];
665 droid_rects
[num_drects
].right
= rects
[i
] + rects
[i
+ 2];
666 droid_rects
[num_drects
].top
= rects
[i
+ 1] + rects
[i
+ 3];
670 * XXX/TODO: Need to check for other return values
673 ret
= native_window_set_surface_damage(dri2_surf
->window
, droid_rects
, n_rects
);
676 return ret
== 0 ? EGL_TRUE
: EGL_FALSE
;
681 droid_create_image_from_prime_fd_yuv(_EGLDisplay
*disp
, _EGLContext
*ctx
,
682 struct ANativeWindowBuffer
*buf
, int fd
)
684 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
685 struct android_ycbcr ycbcr
;
692 if (!dri2_dpy
->gralloc
->lock_ycbcr
) {
693 _eglLog(_EGL_WARNING
, "Gralloc does not support lock_ycbcr");
697 memset(&ycbcr
, 0, sizeof(ycbcr
));
698 ret
= dri2_dpy
->gralloc
->lock_ycbcr(dri2_dpy
->gralloc
, buf
->handle
,
699 0, 0, 0, 0, 0, &ycbcr
);
701 /* HACK: See droid_create_image_from_prime_fd() and
702 * https://issuetracker.google.com/32077885.*/
703 if (buf
->format
== HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
)
706 _eglLog(_EGL_WARNING
, "gralloc->lock_ycbcr failed: %d", ret
);
709 dri2_dpy
->gralloc
->unlock(dri2_dpy
->gralloc
, buf
->handle
);
711 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
712 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
713 * so they can be interpreted as offsets. */
714 offsets
[0] = (size_t)ycbcr
.y
;
715 /* We assume here that all the planes are located in one DMA-buf. */
716 is_ycrcb
= (size_t)ycbcr
.cr
< (size_t)ycbcr
.cb
;
718 offsets
[1] = (size_t)ycbcr
.cr
;
719 offsets
[2] = (size_t)ycbcr
.cb
;
721 offsets
[1] = (size_t)ycbcr
.cb
;
722 offsets
[2] = (size_t)ycbcr
.cr
;
725 /* .ystride is the line length (in bytes) of the Y plane,
726 * .cstride is the line length (in bytes) of any of the remaining
727 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
729 pitches
[0] = ycbcr
.ystride
;
730 pitches
[1] = pitches
[2] = ycbcr
.cstride
;
732 /* .chroma_step is the byte distance between the same chroma channel
733 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
734 fourcc
= get_fourcc_yuv(buf
->format
, is_ycrcb
, ycbcr
.chroma_step
);
736 _eglLog(_EGL_WARNING
, "unsupported YUV format, native = %x, is_ycrcb = %d, chroma_step = %d",
737 buf
->format
, is_ycrcb
, ycbcr
.chroma_step
);
741 if (ycbcr
.chroma_step
== 2) {
742 /* Semi-planar Y + CbCr or Y + CrCb format. */
743 const EGLint attr_list_2plane
[] = {
744 EGL_WIDTH
, buf
->width
,
745 EGL_HEIGHT
, buf
->height
,
746 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
747 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
748 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
749 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, offsets
[0],
750 EGL_DMA_BUF_PLANE1_FD_EXT
, fd
,
751 EGL_DMA_BUF_PLANE1_PITCH_EXT
, pitches
[1],
752 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, offsets
[1],
756 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_2plane
);
758 /* Fully planar Y + Cb + Cr or Y + Cr + Cb format. */
759 const EGLint attr_list_3plane
[] = {
760 EGL_WIDTH
, buf
->width
,
761 EGL_HEIGHT
, buf
->height
,
762 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
763 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
764 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
765 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, offsets
[0],
766 EGL_DMA_BUF_PLANE1_FD_EXT
, fd
,
767 EGL_DMA_BUF_PLANE1_PITCH_EXT
, pitches
[1],
768 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, offsets
[1],
769 EGL_DMA_BUF_PLANE2_FD_EXT
, fd
,
770 EGL_DMA_BUF_PLANE2_PITCH_EXT
, pitches
[2],
771 EGL_DMA_BUF_PLANE2_OFFSET_EXT
, offsets
[2],
775 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_3plane
);
780 droid_create_image_from_prime_fd(_EGLDisplay
*disp
, _EGLContext
*ctx
,
781 struct ANativeWindowBuffer
*buf
, int fd
)
785 if (is_yuv(buf
->format
)) {
788 image
= droid_create_image_from_prime_fd_yuv(disp
, ctx
, buf
, fd
);
790 * HACK: https://issuetracker.google.com/32077885
791 * There is no API available to properly query the IMPLEMENTATION_DEFINED
792 * format. As a workaround we rely here on gralloc allocating either
793 * an arbitrary YCbCr 4:2:0 or RGBX_8888, with the latter being recognized
794 * by lock_ycbcr failing.
796 if (image
|| buf
->format
!= HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
)
800 const int fourcc
= get_fourcc(buf
->format
);
802 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
806 pitch
= buf
->stride
* get_format_bpp(buf
->format
);
808 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
812 const EGLint attr_list
[] = {
813 EGL_WIDTH
, buf
->width
,
814 EGL_HEIGHT
, buf
->height
,
815 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
816 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
817 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitch
,
818 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, 0,
822 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list
);
826 droid_create_image_from_name(_EGLDisplay
*disp
, _EGLContext
*ctx
,
827 struct ANativeWindowBuffer
*buf
)
829 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
830 struct dri2_egl_image
*dri2_img
;
834 name
= get_native_buffer_name(buf
);
836 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
840 format
= get_format(buf
->format
);
844 dri2_img
= calloc(1, sizeof(*dri2_img
));
846 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
850 _eglInitImage(&dri2_img
->base
, disp
);
852 dri2_img
->dri_image
=
853 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
860 if (!dri2_img
->dri_image
) {
862 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
866 return &dri2_img
->base
;
870 droid_query_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
871 EGLint attribute
, EGLint
*value
)
873 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
876 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
877 dri2_surf
->window
->query(dri2_surf
->window
,
878 NATIVE_WINDOW_DEFAULT_WIDTH
, value
);
883 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
884 dri2_surf
->window
->query(dri2_surf
->window
,
885 NATIVE_WINDOW_DEFAULT_HEIGHT
, value
);
892 return _eglQuerySurface(drv
, dpy
, surf
, attribute
, value
);
896 dri2_create_image_android_native_buffer(_EGLDisplay
*disp
,
898 struct ANativeWindowBuffer
*buf
)
903 /* From the EGL_ANDROID_image_native_buffer spec:
905 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
906 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
908 _eglError(EGL_BAD_CONTEXT
, "eglCreateEGLImageKHR: for "
909 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
914 if (!buf
|| buf
->common
.magic
!= ANDROID_NATIVE_BUFFER_MAGIC
||
915 buf
->common
.version
!= sizeof(*buf
)) {
916 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
920 fd
= get_native_buffer_fd(buf
);
922 return droid_create_image_from_prime_fd(disp
, ctx
, buf
, fd
);
924 return droid_create_image_from_name(disp
, ctx
, buf
);
928 droid_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
929 _EGLContext
*ctx
, EGLenum target
,
930 EGLClientBuffer buffer
, const EGLint
*attr_list
)
933 case EGL_NATIVE_BUFFER_ANDROID
:
934 return dri2_create_image_android_native_buffer(disp
, ctx
,
935 (struct ANativeWindowBuffer
*) buffer
);
937 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
942 droid_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
947 droid_get_buffers_parse_attachments(struct dri2_egl_surface
*dri2_surf
,
948 unsigned int *attachments
, int count
)
952 /* fill dri2_surf->buffers */
953 for (int i
= 0; i
< count
* 2; i
+= 2) {
954 __DRIbuffer
*buf
, *local
;
956 assert(num_buffers
< ARRAY_SIZE(dri2_surf
->buffers
));
957 buf
= &dri2_surf
->buffers
[num_buffers
];
959 switch (attachments
[i
]) {
960 case __DRI_BUFFER_BACK_LEFT
:
961 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
962 buf
->attachment
= attachments
[i
];
963 buf
->name
= get_native_buffer_name(dri2_surf
->buffer
);
964 buf
->cpp
= get_format_bpp(dri2_surf
->buffer
->format
);
965 buf
->pitch
= dri2_surf
->buffer
->stride
* buf
->cpp
;
973 /* fall through for pbuffers */
974 case __DRI_BUFFER_DEPTH
:
975 case __DRI_BUFFER_STENCIL
:
976 case __DRI_BUFFER_ACCUM
:
977 case __DRI_BUFFER_DEPTH_STENCIL
:
978 case __DRI_BUFFER_HIZ
:
979 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
,
980 attachments
[i
], attachments
[i
+ 1]);
987 case __DRI_BUFFER_FRONT_LEFT
:
988 case __DRI_BUFFER_FRONT_RIGHT
:
989 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
990 case __DRI_BUFFER_FAKE_FRONT_RIGHT
:
991 case __DRI_BUFFER_BACK_RIGHT
:
993 /* no front or right buffers */
1001 static __DRIbuffer
*
1002 droid_get_buffers_with_format(__DRIdrawable
* driDrawable
,
1003 int *width
, int *height
,
1004 unsigned int *attachments
, int count
,
1005 int *out_count
, void *loaderPrivate
)
1007 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1009 if (update_buffers(dri2_surf
) < 0)
1012 *out_count
= droid_get_buffers_parse_attachments(dri2_surf
, attachments
, count
);
1015 *width
= dri2_surf
->base
.Width
;
1017 *height
= dri2_surf
->base
.Height
;
1019 return dri2_surf
->buffers
;
1023 droid_get_capability(void *loaderPrivate
, enum dri_loader_cap cap
)
1025 /* Note: loaderPrivate is _EGLDisplay* */
1027 case DRI_LOADER_CAP_RGBA_ORDERING
:
1035 droid_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
1037 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1038 static const struct {
1040 unsigned int rgba_masks
[4];
1042 { HAL_PIXEL_FORMAT_RGBA_8888
, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 } },
1043 { HAL_PIXEL_FORMAT_RGBX_8888
, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 } },
1044 { HAL_PIXEL_FORMAT_RGB_565
, { 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 } },
1045 { HAL_PIXEL_FORMAT_BGRA_8888
, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 } },
1048 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1049 int config_count
= 0;
1051 /* The nesting of loops is significant here. Also significant is the order
1052 * of the HAL pixel formats. Many Android apps (such as Google's official
1053 * NDK GLES2 example app), and even portions the core framework code (such
1054 * as SystemServiceManager in Nougat), incorrectly choose their EGLConfig.
1055 * They neglect to match the EGLConfig's EGL_NATIVE_VISUAL_ID against the
1056 * window's native format, and instead choose the first EGLConfig whose
1057 * channel sizes match those of the native window format while ignoring the
1058 * channel *ordering*.
1060 * We can detect such buggy clients in logcat when they call
1061 * eglCreateSurface, by detecting the mismatch between the EGLConfig's
1062 * format and the window's format.
1064 * As a workaround, we generate EGLConfigs such that all EGLConfigs for HAL
1065 * pixel format i precede those for HAL pixel format i+1. In my
1066 * (chadversary) testing on Android Nougat, this was good enough to pacify
1067 * the buggy clients.
1069 for (int i
= 0; i
< ARRAY_SIZE(visuals
); i
++) {
1070 for (int j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
1071 const EGLint surface_type
= EGL_WINDOW_BIT
| EGL_PBUFFER_BIT
;
1073 const EGLint config_attrs
[] = {
1074 EGL_NATIVE_VISUAL_ID
, visuals
[i
].format
,
1075 EGL_NATIVE_VISUAL_TYPE
, visuals
[i
].format
,
1076 EGL_FRAMEBUFFER_TARGET_ANDROID
, EGL_TRUE
,
1077 EGL_RECORDABLE_ANDROID
, EGL_TRUE
,
1081 struct dri2_egl_config
*dri2_conf
=
1082 dri2_add_config(dpy
, dri2_dpy
->driver_configs
[j
],
1083 config_count
+ 1, surface_type
, config_attrs
,
1084 visuals
[i
].rgba_masks
);
1086 if (dri2_conf
->base
.ConfigID
== config_count
+ 1)
1093 for (int i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1094 if (!format_count
[i
]) {
1095 _eglLog(_EGL_DEBUG
, "No DRI config supports native format 0x%x",
1100 return (config_count
!= 0);
1104 droid_open_device(struct dri2_egl_display
*dri2_dpy
)
1106 int fd
= -1, err
= -EINVAL
;
1108 if (dri2_dpy
->gralloc
->perform
)
1109 err
= dri2_dpy
->gralloc
->perform(dri2_dpy
->gralloc
,
1110 GRALLOC_MODULE_PERFORM_GET_DRM_FD
,
1112 if (err
|| fd
< 0) {
1113 _eglLog(_EGL_WARNING
, "fail to get drm fd");
1117 return (fd
>= 0) ? fcntl(fd
, F_DUPFD_CLOEXEC
, 3) : -1;
1120 static const struct dri2_egl_display_vtbl droid_display_vtbl
= {
1121 .authenticate
= NULL
,
1122 .create_window_surface
= droid_create_window_surface
,
1123 .create_pixmap_surface
= dri2_fallback_create_pixmap_surface
,
1124 .create_pbuffer_surface
= droid_create_pbuffer_surface
,
1125 .destroy_surface
= droid_destroy_surface
,
1126 .create_image
= droid_create_image_khr
,
1127 .swap_buffers
= droid_swap_buffers
,
1128 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
, /* Android implements the function */
1129 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1130 #if ANDROID_API_LEVEL >= 23
1131 .set_damage_region
= droid_set_damage_region
,
1133 .set_damage_region
= dri2_fallback_set_damage_region
,
1135 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1136 .copy_buffers
= dri2_fallback_copy_buffers
,
1137 .query_buffer_age
= droid_query_buffer_age
,
1138 .query_surface
= droid_query_surface
,
1139 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1140 .get_sync_values
= dri2_fallback_get_sync_values
,
1141 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1144 static const __DRIdri2LoaderExtension droid_dri2_loader_extension
= {
1145 .base
= { __DRI_DRI2_LOADER
, 4 },
1148 .flushFrontBuffer
= droid_flush_front_buffer
,
1149 .getBuffersWithFormat
= droid_get_buffers_with_format
,
1150 .getCapability
= droid_get_capability
,
1153 static const __DRIimageLoaderExtension droid_image_loader_extension
= {
1154 .base
= { __DRI_IMAGE_LOADER
, 2 },
1156 .getBuffers
= droid_image_get_buffers
,
1157 .flushFrontBuffer
= droid_flush_front_buffer
,
1158 .getCapability
= droid_get_capability
,
1161 static const __DRIextension
*droid_dri2_loader_extensions
[] = {
1162 &droid_dri2_loader_extension
.base
,
1163 &image_lookup_extension
.base
,
1164 &use_invalidate
.base
,
1168 static const __DRIextension
*droid_image_loader_extensions
[] = {
1169 &droid_image_loader_extension
.base
,
1170 &image_lookup_extension
.base
,
1171 &use_invalidate
.base
,
1176 dri2_initialize_android(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
1178 struct dri2_egl_display
*dri2_dpy
;
1182 loader_set_logger(_eglLog
);
1184 dri2_dpy
= calloc(1, sizeof(*dri2_dpy
));
1186 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1189 ret
= hw_get_module(GRALLOC_HARDWARE_MODULE_ID
,
1190 (const hw_module_t
**)&dri2_dpy
->gralloc
);
1192 err
= "DRI2: failed to get gralloc module";
1196 dpy
->DriverData
= (void *) dri2_dpy
;
1198 dri2_dpy
->fd
= droid_open_device(dri2_dpy
);
1199 if (dri2_dpy
->fd
< 0) {
1200 err
= "DRI2: failed to open device";
1204 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1205 if (dri2_dpy
->driver_name
== NULL
) {
1206 err
= "DRI2: failed to get driver name";
1210 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1212 /* render nodes cannot use Gem names, and thus do not support
1213 * the __DRI_DRI2_LOADER extension */
1214 if (!dri2_dpy
->is_render_node
) {
1215 dri2_dpy
->loader_extensions
= droid_dri2_loader_extensions
;
1216 if (!dri2_load_driver(dpy
)) {
1217 err
= "DRI2: failed to load driver";
1221 dri2_dpy
->loader_extensions
= droid_image_loader_extensions
;
1222 if (!dri2_load_driver_dri3(dpy
)) {
1223 err
= "DRI3: failed to load driver";
1228 if (!dri2_create_screen(dpy
)) {
1229 err
= "DRI2: failed to create screen";
1233 if (!dri2_setup_extensions(dpy
)) {
1234 err
= "DRI2: failed to setup extensions";
1238 dri2_setup_screen(dpy
);
1240 if (!droid_add_configs_for_visuals(drv
, dpy
)) {
1241 err
= "DRI2: failed to add configs";
1245 dpy
->Extensions
.ANDROID_framebuffer_target
= EGL_TRUE
;
1246 dpy
->Extensions
.ANDROID_image_native_buffer
= EGL_TRUE
;
1247 dpy
->Extensions
.ANDROID_recordable
= EGL_TRUE
;
1248 dpy
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1249 #if ANDROID_API_LEVEL >= 23
1250 dpy
->Extensions
.KHR_partial_update
= EGL_TRUE
;
1252 dpy
->Extensions
.KHR_image
= EGL_TRUE
;
1254 /* Fill vtbl last to prevent accidentally calling virtual function during
1257 dri2_dpy
->vtbl
= &droid_display_vtbl
;
1262 dri2_display_destroy(dpy
);
1263 return _eglError(EGL_NOT_INITIALIZED
, err
);