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
},
65 get_fourcc_yuv(int native
, int is_ycrcb
, int chroma_step
)
67 for (int i
= 0; i
< ARRAY_SIZE(droid_yuv_formats
); ++i
)
68 if (droid_yuv_formats
[i
].native
== native
&&
69 droid_yuv_formats
[i
].is_ycrcb
== is_ycrcb
&&
70 droid_yuv_formats
[i
].chroma_step
== chroma_step
)
71 return droid_yuv_formats
[i
].fourcc
;
79 for (int i
= 0; i
< ARRAY_SIZE(droid_yuv_formats
); ++i
)
80 if (droid_yuv_formats
[i
].native
== native
)
87 get_format_bpp(int native
)
92 case HAL_PIXEL_FORMAT_RGBA_8888
:
93 case HAL_PIXEL_FORMAT_RGBX_8888
:
94 case HAL_PIXEL_FORMAT_BGRA_8888
:
97 case HAL_PIXEL_FORMAT_RGB_565
:
108 /* createImageFromFds requires fourcc format */
109 static int get_fourcc(int native
)
112 case HAL_PIXEL_FORMAT_RGB_565
: return __DRI_IMAGE_FOURCC_RGB565
;
113 case HAL_PIXEL_FORMAT_BGRA_8888
: return __DRI_IMAGE_FOURCC_ARGB8888
;
114 case HAL_PIXEL_FORMAT_RGBA_8888
: return __DRI_IMAGE_FOURCC_ABGR8888
;
115 case HAL_PIXEL_FORMAT_RGBX_8888
: return __DRI_IMAGE_FOURCC_XBGR8888
;
117 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", native
);
122 static int get_format(int format
)
125 case HAL_PIXEL_FORMAT_BGRA_8888
: return __DRI_IMAGE_FORMAT_ARGB8888
;
126 case HAL_PIXEL_FORMAT_RGB_565
: return __DRI_IMAGE_FORMAT_RGB565
;
127 case HAL_PIXEL_FORMAT_RGBA_8888
: return __DRI_IMAGE_FORMAT_ABGR8888
;
128 case HAL_PIXEL_FORMAT_RGBX_8888
: return __DRI_IMAGE_FORMAT_XBGR8888
;
130 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", format
);
136 get_native_buffer_fd(struct ANativeWindowBuffer
*buf
)
138 native_handle_t
*handle
= (native_handle_t
*)buf
->handle
;
140 * Various gralloc implementations exist, but the dma-buf fd tends
141 * to be first. Access it directly to avoid a dependency on specific
144 return (handle
&& handle
->numFds
) ? handle
->data
[0] : -1;
148 get_native_buffer_name(struct ANativeWindowBuffer
*buf
)
150 return gralloc_drm_get_gem_handle(buf
->handle
);
154 droid_window_dequeue_buffer(struct dri2_egl_surface
*dri2_surf
)
158 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
162 /* If access to the buffer is controlled by a sync fence, then block on the
165 * It may be more performant to postpone blocking until there is an
166 * immediate need to write to the buffer. But doing so would require adding
167 * hooks to the DRI2 loader.
169 * From the ANativeWindow::dequeueBuffer documentation:
171 * The libsync fence file descriptor returned in the int pointed to by
172 * the fenceFd argument will refer to the fence that must signal
173 * before the dequeued buffer may be written to. A value of -1
174 * indicates that the caller may access the buffer immediately without
175 * waiting on a fence. If a valid file descriptor is returned (i.e.
176 * any value except -1) then the caller is responsible for closing the
180 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
182 * Waits indefinitely if timeout < 0.
185 sync_wait(fence_fd
, timeout
);
189 dri2_surf
->buffer
->common
.incRef(&dri2_surf
->buffer
->common
);
191 /* Record all the buffers created by ANativeWindow and update back buffer
192 * for updating buffer's age in swap_buffers.
194 EGLBoolean updated
= EGL_FALSE
;
195 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
196 if (!dri2_surf
->color_buffers
[i
].buffer
) {
197 dri2_surf
->color_buffers
[i
].buffer
= dri2_surf
->buffer
;
199 if (dri2_surf
->color_buffers
[i
].buffer
== dri2_surf
->buffer
) {
200 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
207 /* In case of all the buffers were recreated by ANativeWindow, reset
210 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
211 dri2_surf
->color_buffers
[i
].buffer
= NULL
;
212 dri2_surf
->color_buffers
[i
].age
= 0;
214 dri2_surf
->color_buffers
[0].buffer
= dri2_surf
->buffer
;
215 dri2_surf
->back
= &dri2_surf
->color_buffers
[0];
222 droid_window_enqueue_buffer(_EGLDisplay
*disp
, struct dri2_egl_surface
*dri2_surf
)
224 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
226 /* To avoid blocking other EGL calls, release the display mutex before
227 * we enter droid_window_enqueue_buffer() and re-acquire the mutex upon
230 mtx_unlock(&disp
->Mutex
);
232 /* Queue the buffer without a sync fence. This informs the ANativeWindow
233 * that it may access the buffer immediately.
235 * From ANativeWindow::dequeueBuffer:
237 * The fenceFd argument specifies a libsync fence file descriptor for
238 * a fence that must signal before the buffer can be accessed. If
239 * the buffer can be accessed immediately then a value of -1 should
240 * be used. The caller must not use the file descriptor after it
241 * is passed to queueBuffer, and the ANativeWindow implementation
242 * is responsible for closing it.
245 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
248 dri2_surf
->buffer
->common
.decRef(&dri2_surf
->buffer
->common
);
249 dri2_surf
->buffer
= NULL
;
250 dri2_surf
->back
= NULL
;
252 mtx_lock(&disp
->Mutex
);
254 if (dri2_surf
->dri_image_back
) {
255 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
256 dri2_surf
->dri_image_back
= NULL
;
263 droid_window_cancel_buffer(struct dri2_egl_surface
*dri2_surf
)
267 ret
= dri2_surf
->window
->cancelBuffer(dri2_surf
->window
, dri2_surf
->buffer
, -1);
269 _eglLog(_EGL_WARNING
, "ANativeWindow::cancelBuffer failed");
270 dri2_surf
->base
.Lost
= EGL_TRUE
;
275 droid_alloc_local_buffer(struct dri2_egl_surface
*dri2_surf
,
276 unsigned int att
, unsigned int format
)
278 struct dri2_egl_display
*dri2_dpy
=
279 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
281 if (att
>= ARRAY_SIZE(dri2_surf
->local_buffers
))
284 if (!dri2_surf
->local_buffers
[att
]) {
285 dri2_surf
->local_buffers
[att
] =
286 dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
, att
, format
,
287 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
290 return dri2_surf
->local_buffers
[att
];
294 droid_free_local_buffers(struct dri2_egl_surface
*dri2_surf
)
296 struct dri2_egl_display
*dri2_dpy
=
297 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
299 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->local_buffers
); i
++) {
300 if (dri2_surf
->local_buffers
[i
]) {
301 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
302 dri2_surf
->local_buffers
[i
]);
303 dri2_surf
->local_buffers
[i
] = NULL
;
309 droid_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
310 _EGLConfig
*conf
, void *native_window
,
311 const EGLint
*attrib_list
)
313 __DRIcreateNewDrawableFunc createNewDrawable
;
314 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
315 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
316 struct dri2_egl_surface
*dri2_surf
;
317 struct ANativeWindow
*window
= native_window
;
318 const __DRIconfig
*config
;
320 dri2_surf
= calloc(1, sizeof *dri2_surf
);
322 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
326 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
327 goto cleanup_surface
;
329 if (type
== EGL_WINDOW_BIT
) {
332 if (!window
|| window
->common
.magic
!= ANDROID_NATIVE_WINDOW_MAGIC
) {
333 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
334 goto cleanup_surface
;
336 if (window
->query(window
, NATIVE_WINDOW_FORMAT
, &format
)) {
337 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
338 goto cleanup_surface
;
341 if (format
!= dri2_conf
->base
.NativeVisualID
) {
342 _eglLog(_EGL_WARNING
, "Native format mismatch: 0x%x != 0x%x",
343 format
, dri2_conf
->base
.NativeVisualID
);
346 window
->query(window
, NATIVE_WINDOW_WIDTH
, &dri2_surf
->base
.Width
);
347 window
->query(window
, NATIVE_WINDOW_HEIGHT
, &dri2_surf
->base
.Height
);
350 config
= dri2_get_dri_config(dri2_conf
, type
,
351 dri2_surf
->base
.GLColorspace
);
353 goto cleanup_surface
;
355 if (dri2_dpy
->image_driver
)
356 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
358 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
360 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
362 if (dri2_surf
->dri_drawable
== NULL
) {
363 _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
364 goto cleanup_surface
;
368 window
->common
.incRef(&window
->common
);
369 dri2_surf
->window
= window
;
372 return &dri2_surf
->base
;
381 droid_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
382 _EGLConfig
*conf
, void *native_window
,
383 const EGLint
*attrib_list
)
385 return droid_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
386 native_window
, attrib_list
);
390 droid_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
391 _EGLConfig
*conf
, const EGLint
*attrib_list
)
393 return droid_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
398 droid_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
400 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
401 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
403 droid_free_local_buffers(dri2_surf
);
405 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
406 if (dri2_surf
->buffer
)
407 droid_window_cancel_buffer(dri2_surf
);
409 dri2_surf
->window
->common
.decRef(&dri2_surf
->window
->common
);
412 if (dri2_surf
->dri_image_back
) {
413 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_back", __func__
, __LINE__
);
414 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
415 dri2_surf
->dri_image_back
= NULL
;
418 if (dri2_surf
->dri_image_front
) {
419 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_front", __func__
, __LINE__
);
420 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_front
);
421 dri2_surf
->dri_image_front
= NULL
;
424 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
432 update_buffers(struct dri2_egl_surface
*dri2_surf
)
434 if (dri2_surf
->base
.Lost
)
437 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
440 /* try to dequeue the next back buffer */
441 if (!dri2_surf
->buffer
&& !droid_window_dequeue_buffer(dri2_surf
)) {
442 _eglLog(_EGL_WARNING
, "Could not dequeue buffer from native window");
443 dri2_surf
->base
.Lost
= EGL_TRUE
;
447 /* free outdated buffers and update the surface size */
448 if (dri2_surf
->base
.Width
!= dri2_surf
->buffer
->width
||
449 dri2_surf
->base
.Height
!= dri2_surf
->buffer
->height
) {
450 droid_free_local_buffers(dri2_surf
);
451 dri2_surf
->base
.Width
= dri2_surf
->buffer
->width
;
452 dri2_surf
->base
.Height
= dri2_surf
->buffer
->height
;
459 get_front_bo(struct dri2_egl_surface
*dri2_surf
, unsigned int format
)
461 struct dri2_egl_display
*dri2_dpy
=
462 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
464 if (dri2_surf
->dri_image_front
)
467 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
468 /* According current EGL spec, front buffer rendering
469 * for window surface is not supported now.
470 * and mesa doesn't have the implementation of this case.
471 * Add warning message, but not treat it as error.
473 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported front buffer for window surface");
474 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
475 dri2_surf
->dri_image_front
=
476 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
477 dri2_surf
->base
.Width
,
478 dri2_surf
->base
.Height
,
482 if (!dri2_surf
->dri_image_front
) {
483 _eglLog(_EGL_WARNING
, "dri2_image_front allocation failed");
492 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
494 struct dri2_egl_display
*dri2_dpy
=
495 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
499 if (dri2_surf
->dri_image_back
)
502 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
503 if (!dri2_surf
->buffer
) {
504 _eglLog(_EGL_WARNING
, "Could not get native buffer");
508 fd
= get_native_buffer_fd(dri2_surf
->buffer
);
510 _eglLog(_EGL_WARNING
, "Could not get native buffer FD");
514 fourcc
= get_fourcc(dri2_surf
->buffer
->format
);
516 pitch
= dri2_surf
->buffer
->stride
*
517 get_format_bpp(dri2_surf
->buffer
->format
);
519 if (fourcc
== -1 || pitch
== 0) {
520 _eglLog(_EGL_WARNING
, "Invalid buffer fourcc(%x) or pitch(%d)",
525 dri2_surf
->dri_image_back
=
526 dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
527 dri2_surf
->base
.Width
,
528 dri2_surf
->base
.Height
,
535 if (!dri2_surf
->dri_image_back
) {
536 _eglLog(_EGL_WARNING
, "failed to create DRI image from FD");
539 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
540 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
541 * the spec states that they have a back buffer but no front buffer, in
542 * contrast to pixmaps, which have a front buffer but no back buffer.
544 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
545 * from the spec, following the precedent of Mesa's EGL X11 platform. The
546 * X11 platform correctly assigns pbuffers to single-buffered configs, but
547 * assigns the pbuffer a front buffer instead of a back buffer.
549 * Pbuffers in the X11 platform mostly work today, so let's just copy its
550 * behavior instead of trying to fix (and hence potentially breaking) the
553 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported back buffer for pbuffer surface");
559 /* Some drivers will pass multiple bits in buffer_mask.
560 * For such case, will go through all the bits, and
561 * will not return error when unsupported buffer is requested, only
562 * return error when the allocation for supported buffer failed.
565 droid_image_get_buffers(__DRIdrawable
*driDrawable
,
569 uint32_t buffer_mask
,
570 struct __DRIimageList
*images
)
572 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
574 images
->image_mask
= 0;
575 images
->front
= NULL
;
578 if (update_buffers(dri2_surf
) < 0)
581 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
582 if (get_front_bo(dri2_surf
, format
) < 0)
585 if (dri2_surf
->dri_image_front
) {
586 images
->front
= dri2_surf
->dri_image_front
;
587 images
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
591 if (buffer_mask
& __DRI_IMAGE_BUFFER_BACK
) {
592 if (get_back_bo(dri2_surf
) < 0)
595 if (dri2_surf
->dri_image_back
) {
596 images
->back
= dri2_surf
->dri_image_back
;
597 images
->image_mask
|= __DRI_IMAGE_BUFFER_BACK
;
605 droid_query_buffer_age(_EGLDriver
*drv
,
606 _EGLDisplay
*disp
, _EGLSurface
*surface
)
608 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
610 if (update_buffers(dri2_surf
) < 0) {
611 _eglError(EGL_BAD_ALLOC
, "droid_query_buffer_age");
615 return dri2_surf
->back
? dri2_surf
->back
->age
: 0;
619 droid_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
621 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
622 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
624 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
627 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
628 if (dri2_surf
->color_buffers
[i
].age
> 0)
629 dri2_surf
->color_buffers
[i
].age
++;
632 /* "XXX: we don't use get_back_bo() since it causes regressions in
633 * several dEQP tests.
636 dri2_surf
->back
->age
= 1;
638 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
640 /* dri2_surf->buffer can be null even when no error has occured. For
641 * example, if the user has called no GL rendering commands since the
642 * previous eglSwapBuffers, then the driver may have not triggered
643 * a callback to ANativeWindow::dequeueBuffer, in which case
644 * dri2_surf->buffer remains null.
646 if (dri2_surf
->buffer
)
647 droid_window_enqueue_buffer(disp
, dri2_surf
);
649 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
654 #if ANDROID_API_LEVEL >= 23
656 droid_set_damage_region(_EGLDriver
*drv
,
658 _EGLSurface
*draw
, const EGLint
* rects
, EGLint n_rects
)
660 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
661 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
662 android_native_rect_t
* droid_rects
= NULL
;
668 droid_rects
= malloc(n_rects
* sizeof(android_native_rect_t
));
669 if (droid_rects
== NULL
) {
670 _eglError(EGL_BAD_ALLOC
, "eglSetDamageRegionKHR");
674 for (EGLint num_drects
= 0; num_drects
< n_rects
; num_drects
++) {
675 EGLint i
= num_drects
* 4;
676 droid_rects
[num_drects
].left
= rects
[i
];
677 droid_rects
[num_drects
].bottom
= rects
[i
+ 1];
678 droid_rects
[num_drects
].right
= rects
[i
] + rects
[i
+ 2];
679 droid_rects
[num_drects
].top
= rects
[i
+ 1] + rects
[i
+ 3];
683 * XXX/TODO: Need to check for other return values
686 ret
= native_window_set_surface_damage(dri2_surf
->window
, droid_rects
, n_rects
);
689 return ret
== 0 ? EGL_TRUE
: EGL_FALSE
;
694 droid_create_image_from_prime_fd_yuv(_EGLDisplay
*disp
, _EGLContext
*ctx
,
695 struct ANativeWindowBuffer
*buf
, int fd
)
697 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
698 struct android_ycbcr ycbcr
;
705 if (!dri2_dpy
->gralloc
->lock_ycbcr
) {
706 _eglLog(_EGL_WARNING
, "Gralloc does not support lock_ycbcr");
710 memset(&ycbcr
, 0, sizeof(ycbcr
));
711 ret
= dri2_dpy
->gralloc
->lock_ycbcr(dri2_dpy
->gralloc
, buf
->handle
,
712 0, 0, 0, 0, 0, &ycbcr
);
714 _eglLog(_EGL_WARNING
, "gralloc->lock_ycbcr failed: %d", ret
);
717 dri2_dpy
->gralloc
->unlock(dri2_dpy
->gralloc
, buf
->handle
);
719 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
720 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
721 * so they can be interpreted as offsets. */
722 offsets
[0] = (size_t)ycbcr
.y
;
723 /* We assume here that all the planes are located in one DMA-buf. */
724 is_ycrcb
= (size_t)ycbcr
.cr
< (size_t)ycbcr
.cb
;
726 offsets
[1] = (size_t)ycbcr
.cr
;
727 offsets
[2] = (size_t)ycbcr
.cb
;
729 offsets
[1] = (size_t)ycbcr
.cb
;
730 offsets
[2] = (size_t)ycbcr
.cr
;
733 /* .ystride is the line length (in bytes) of the Y plane,
734 * .cstride is the line length (in bytes) of any of the remaining
735 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
737 pitches
[0] = ycbcr
.ystride
;
738 pitches
[1] = pitches
[2] = ycbcr
.cstride
;
740 /* .chroma_step is the byte distance between the same chroma channel
741 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
742 fourcc
= get_fourcc_yuv(buf
->format
, is_ycrcb
, ycbcr
.chroma_step
);
744 _eglLog(_EGL_WARNING
, "unsupported YUV format, native = %x, is_ycrcb = %d, chroma_step = %d",
745 buf
->format
, is_ycrcb
, ycbcr
.chroma_step
);
749 if (ycbcr
.chroma_step
== 2) {
750 /* Semi-planar Y + CbCr or Y + CrCb format. */
751 const EGLint attr_list_2plane
[] = {
752 EGL_WIDTH
, buf
->width
,
753 EGL_HEIGHT
, buf
->height
,
754 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
755 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
756 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
757 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, offsets
[0],
758 EGL_DMA_BUF_PLANE1_FD_EXT
, fd
,
759 EGL_DMA_BUF_PLANE1_PITCH_EXT
, pitches
[1],
760 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, offsets
[1],
764 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_2plane
);
766 /* Fully planar Y + Cb + Cr or Y + Cr + Cb format. */
767 const EGLint attr_list_3plane
[] = {
768 EGL_WIDTH
, buf
->width
,
769 EGL_HEIGHT
, buf
->height
,
770 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
771 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
772 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
773 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, offsets
[0],
774 EGL_DMA_BUF_PLANE1_FD_EXT
, fd
,
775 EGL_DMA_BUF_PLANE1_PITCH_EXT
, pitches
[1],
776 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, offsets
[1],
777 EGL_DMA_BUF_PLANE2_FD_EXT
, fd
,
778 EGL_DMA_BUF_PLANE2_PITCH_EXT
, pitches
[2],
779 EGL_DMA_BUF_PLANE2_OFFSET_EXT
, offsets
[2],
783 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_3plane
);
788 droid_create_image_from_prime_fd(_EGLDisplay
*disp
, _EGLContext
*ctx
,
789 struct ANativeWindowBuffer
*buf
, int fd
)
793 if (is_yuv(buf
->format
))
794 return droid_create_image_from_prime_fd_yuv(disp
, ctx
, buf
, fd
);
796 const int fourcc
= get_fourcc(buf
->format
);
798 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
802 pitch
= buf
->stride
* get_format_bpp(buf
->format
);
804 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
808 const EGLint attr_list
[] = {
809 EGL_WIDTH
, buf
->width
,
810 EGL_HEIGHT
, buf
->height
,
811 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
812 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
813 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitch
,
814 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, 0,
818 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list
);
822 droid_create_image_from_name(_EGLDisplay
*disp
, _EGLContext
*ctx
,
823 struct ANativeWindowBuffer
*buf
)
825 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
826 struct dri2_egl_image
*dri2_img
;
830 name
= get_native_buffer_name(buf
);
832 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
836 format
= get_format(buf
->format
);
840 dri2_img
= calloc(1, sizeof(*dri2_img
));
842 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
846 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
851 dri2_img
->dri_image
=
852 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
859 if (!dri2_img
->dri_image
) {
861 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
865 return &dri2_img
->base
;
869 droid_query_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
870 EGLint attribute
, EGLint
*value
)
872 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
875 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
876 dri2_surf
->window
->query(dri2_surf
->window
,
877 NATIVE_WINDOW_DEFAULT_WIDTH
, value
);
882 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
883 dri2_surf
->window
->query(dri2_surf
->window
,
884 NATIVE_WINDOW_DEFAULT_HEIGHT
, value
);
891 return _eglQuerySurface(drv
, dpy
, surf
, attribute
, value
);
895 dri2_create_image_android_native_buffer(_EGLDisplay
*disp
,
897 struct ANativeWindowBuffer
*buf
)
902 /* From the EGL_ANDROID_image_native_buffer spec:
904 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
905 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
907 _eglError(EGL_BAD_CONTEXT
, "eglCreateEGLImageKHR: for "
908 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
913 if (!buf
|| buf
->common
.magic
!= ANDROID_NATIVE_BUFFER_MAGIC
||
914 buf
->common
.version
!= sizeof(*buf
)) {
915 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
919 fd
= get_native_buffer_fd(buf
);
921 return droid_create_image_from_prime_fd(disp
, ctx
, buf
, fd
);
923 return droid_create_image_from_name(disp
, ctx
, buf
);
927 droid_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
928 _EGLContext
*ctx
, EGLenum target
,
929 EGLClientBuffer buffer
, const EGLint
*attr_list
)
932 case EGL_NATIVE_BUFFER_ANDROID
:
933 return dri2_create_image_android_native_buffer(disp
, ctx
,
934 (struct ANativeWindowBuffer
*) buffer
);
936 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
941 droid_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
946 droid_get_buffers_parse_attachments(struct dri2_egl_surface
*dri2_surf
,
947 unsigned int *attachments
, int count
)
951 /* fill dri2_surf->buffers */
952 for (int i
= 0; i
< count
* 2; i
+= 2) {
953 __DRIbuffer
*buf
, *local
;
955 assert(num_buffers
< ARRAY_SIZE(dri2_surf
->buffers
));
956 buf
= &dri2_surf
->buffers
[num_buffers
];
958 switch (attachments
[i
]) {
959 case __DRI_BUFFER_BACK_LEFT
:
960 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
961 buf
->attachment
= attachments
[i
];
962 buf
->name
= get_native_buffer_name(dri2_surf
->buffer
);
963 buf
->cpp
= get_format_bpp(dri2_surf
->buffer
->format
);
964 buf
->pitch
= dri2_surf
->buffer
->stride
* buf
->cpp
;
972 /* fall through for pbuffers */
973 case __DRI_BUFFER_DEPTH
:
974 case __DRI_BUFFER_STENCIL
:
975 case __DRI_BUFFER_ACCUM
:
976 case __DRI_BUFFER_DEPTH_STENCIL
:
977 case __DRI_BUFFER_HIZ
:
978 local
= droid_alloc_local_buffer(dri2_surf
,
979 attachments
[i
], attachments
[i
+ 1]);
986 case __DRI_BUFFER_FRONT_LEFT
:
987 case __DRI_BUFFER_FRONT_RIGHT
:
988 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
989 case __DRI_BUFFER_FAKE_FRONT_RIGHT
:
990 case __DRI_BUFFER_BACK_RIGHT
:
992 /* no front or right buffers */
1000 static __DRIbuffer
*
1001 droid_get_buffers_with_format(__DRIdrawable
* driDrawable
,
1002 int *width
, int *height
,
1003 unsigned int *attachments
, int count
,
1004 int *out_count
, void *loaderPrivate
)
1006 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1008 if (update_buffers(dri2_surf
) < 0)
1011 dri2_surf
->buffer_count
=
1012 droid_get_buffers_parse_attachments(dri2_surf
, attachments
, count
);
1015 *width
= dri2_surf
->base
.Width
;
1017 *height
= dri2_surf
->base
.Height
;
1019 *out_count
= dri2_surf
->buffer_count
;
1021 return dri2_surf
->buffers
;
1025 droid_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
1027 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1028 static const struct {
1030 unsigned int rgba_masks
[4];
1032 { HAL_PIXEL_FORMAT_RGBA_8888
, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 } },
1033 { HAL_PIXEL_FORMAT_RGBX_8888
, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 } },
1034 { HAL_PIXEL_FORMAT_RGB_565
, { 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 } },
1035 { HAL_PIXEL_FORMAT_BGRA_8888
, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 } },
1037 EGLint config_attrs
[] = {
1038 EGL_NATIVE_VISUAL_ID
, 0,
1039 EGL_NATIVE_VISUAL_TYPE
, 0,
1040 EGL_FRAMEBUFFER_TARGET_ANDROID
, EGL_TRUE
,
1041 EGL_RECORDABLE_ANDROID
, EGL_TRUE
,
1045 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1048 /* The nesting of loops is significant here. Also significant is the order
1049 * of the HAL pixel formats. Many Android apps (such as Google's official
1050 * NDK GLES2 example app), and even portions the core framework code (such
1051 * as SystemServiceManager in Nougat), incorrectly choose their EGLConfig.
1052 * They neglect to match the EGLConfig's EGL_NATIVE_VISUAL_ID against the
1053 * window's native format, and instead choose the first EGLConfig whose
1054 * channel sizes match those of the native window format while ignoring the
1055 * channel *ordering*.
1057 * We can detect such buggy clients in logcat when they call
1058 * eglCreateSurface, by detecting the mismatch between the EGLConfig's
1059 * format and the window's format.
1061 * As a workaround, we generate EGLConfigs such that all EGLConfigs for HAL
1062 * pixel format i precede those for HAL pixel format i+1. In my
1063 * (chadversary) testing on Android Nougat, this was good enough to pacify
1064 * the buggy clients.
1066 for (int i
= 0; i
< ARRAY_SIZE(visuals
); i
++) {
1067 for (int j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
1068 const EGLint surface_type
= EGL_WINDOW_BIT
| EGL_PBUFFER_BIT
;
1070 config_attrs
[1] = visuals
[i
].format
;
1071 config_attrs
[3] = visuals
[i
].format
;
1073 struct dri2_egl_config
*dri2_conf
=
1074 dri2_add_config(dpy
, dri2_dpy
->driver_configs
[j
],
1075 count
+ 1, surface_type
, config_attrs
,
1076 visuals
[i
].rgba_masks
);
1078 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1085 for (int i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1086 if (!format_count
[i
]) {
1087 _eglLog(_EGL_DEBUG
, "No DRI config supports native format 0x%x",
1092 return (count
!= 0);
1096 droid_open_device(struct dri2_egl_display
*dri2_dpy
)
1098 int fd
= -1, err
= -EINVAL
;
1100 if (dri2_dpy
->gralloc
->perform
)
1101 err
= dri2_dpy
->gralloc
->perform(dri2_dpy
->gralloc
,
1102 GRALLOC_MODULE_PERFORM_GET_DRM_FD
,
1104 if (err
|| fd
< 0) {
1105 _eglLog(_EGL_WARNING
, "fail to get drm fd");
1109 return (fd
>= 0) ? fcntl(fd
, F_DUPFD_CLOEXEC
, 3) : -1;
1112 static const struct dri2_egl_display_vtbl droid_display_vtbl
= {
1113 .authenticate
= NULL
,
1114 .create_window_surface
= droid_create_window_surface
,
1115 .create_pixmap_surface
= dri2_fallback_create_pixmap_surface
,
1116 .create_pbuffer_surface
= droid_create_pbuffer_surface
,
1117 .destroy_surface
= droid_destroy_surface
,
1118 .create_image
= droid_create_image_khr
,
1119 .swap_interval
= dri2_fallback_swap_interval
,
1120 .swap_buffers
= droid_swap_buffers
,
1121 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1122 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1123 #if ANDROID_API_LEVEL >= 23
1124 .set_damage_region
= droid_set_damage_region
,
1126 .set_damage_region
= dri2_fallback_set_damage_region
,
1128 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1129 .copy_buffers
= dri2_fallback_copy_buffers
,
1130 .query_buffer_age
= droid_query_buffer_age
,
1131 .query_surface
= droid_query_surface
,
1132 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1133 .get_sync_values
= dri2_fallback_get_sync_values
,
1134 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1137 static const __DRIdri2LoaderExtension droid_dri2_loader_extension
= {
1138 .base
= { __DRI_DRI2_LOADER
, 3 },
1141 .flushFrontBuffer
= droid_flush_front_buffer
,
1142 .getBuffersWithFormat
= droid_get_buffers_with_format
,
1145 static const __DRIimageLoaderExtension droid_image_loader_extension
= {
1146 .base
= { __DRI_IMAGE_LOADER
, 1 },
1148 .getBuffers
= droid_image_get_buffers
,
1149 .flushFrontBuffer
= droid_flush_front_buffer
,
1152 static const __DRIextension
*droid_dri2_loader_extensions
[] = {
1153 &droid_dri2_loader_extension
.base
,
1154 &image_lookup_extension
.base
,
1155 &use_invalidate
.base
,
1159 static const __DRIextension
*droid_image_loader_extensions
[] = {
1160 &droid_image_loader_extension
.base
,
1161 &image_lookup_extension
.base
,
1162 &use_invalidate
.base
,
1167 dri2_initialize_android(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
1169 struct dri2_egl_display
*dri2_dpy
;
1173 loader_set_logger(_eglLog
);
1175 dri2_dpy
= calloc(1, sizeof(*dri2_dpy
));
1177 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1180 ret
= hw_get_module(GRALLOC_HARDWARE_MODULE_ID
,
1181 (const hw_module_t
**)&dri2_dpy
->gralloc
);
1183 err
= "DRI2: failed to get gralloc module";
1187 dpy
->DriverData
= (void *) dri2_dpy
;
1189 dri2_dpy
->fd
= droid_open_device(dri2_dpy
);
1190 if (dri2_dpy
->fd
< 0) {
1191 err
= "DRI2: failed to open device";
1195 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1196 if (dri2_dpy
->driver_name
== NULL
) {
1197 err
= "DRI2: failed to get driver name";
1201 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1203 /* render nodes cannot use Gem names, and thus do not support
1204 * the __DRI_DRI2_LOADER extension */
1205 if (!dri2_dpy
->is_render_node
) {
1206 dri2_dpy
->loader_extensions
= droid_dri2_loader_extensions
;
1207 if (!dri2_load_driver(dpy
)) {
1208 err
= "DRI2: failed to load driver";
1212 dri2_dpy
->loader_extensions
= droid_image_loader_extensions
;
1213 if (!dri2_load_driver_dri3(dpy
)) {
1214 err
= "DRI3: failed to load driver";
1219 if (!dri2_create_screen(dpy
)) {
1220 err
= "DRI2: failed to create screen";
1224 if (!dri2_setup_extensions(dpy
))
1227 dri2_setup_screen(dpy
);
1229 if (!droid_add_configs_for_visuals(drv
, dpy
)) {
1230 err
= "DRI2: failed to add configs";
1234 dpy
->Extensions
.ANDROID_framebuffer_target
= EGL_TRUE
;
1235 dpy
->Extensions
.ANDROID_image_native_buffer
= EGL_TRUE
;
1236 dpy
->Extensions
.ANDROID_recordable
= EGL_TRUE
;
1237 dpy
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1238 #if ANDROID_API_LEVEL >= 23
1239 dpy
->Extensions
.KHR_partial_update
= EGL_TRUE
;
1242 /* Fill vtbl last to prevent accidentally calling virtual function during
1245 dri2_dpy
->vtbl
= &droid_display_vtbl
;
1250 dri2_display_destroy(dpy
);
1251 return _eglError(EGL_NOT_INITIALIZED
, err
);