2 * Mesa 3-D graphics library
4 * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
5 * Copyright (C) 2010-2011 LunarG Inc.
7 * Based on platform_x11, which has
9 * Copyright © 2011 Intel Corporation
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
30 #include <cutils/properties.h>
38 #include <sync/sync.h>
39 #include <sys/types.h>
40 #include <drm-uapi/drm_fourcc.h>
42 #include "util/os_file.h"
47 #ifdef HAVE_DRM_GRALLOC
48 #include <gralloc_drm_handle.h>
49 #include "gralloc_drm.h"
50 #endif /* HAVE_DRM_GRALLOC */
52 #define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1))
59 struct droid_yuv_format
{
61 int native
; /* HAL_PIXEL_FORMAT_ */
62 enum chroma_order chroma_order
; /* chroma order is {Cb, Cr} or {Cr, Cb} */
63 int chroma_step
; /* Distance in bytes between subsequent chroma pixels. */
66 int fourcc
; /* DRM_FORMAT_ */
69 /* The following table is used to look up a DRI image FourCC based
70 * on native format and information contained in android_ycbcr struct. */
71 static const struct droid_yuv_format droid_yuv_formats
[] = {
72 /* Native format, YCrCb, Chroma step, DRI image FourCC */
73 { HAL_PIXEL_FORMAT_YCbCr_420_888
, YCbCr
, 2, DRM_FORMAT_NV12
},
74 { HAL_PIXEL_FORMAT_YCbCr_420_888
, YCbCr
, 1, DRM_FORMAT_YUV420
},
75 { HAL_PIXEL_FORMAT_YCbCr_420_888
, YCrCb
, 1, DRM_FORMAT_YVU420
},
76 { HAL_PIXEL_FORMAT_YV12
, YCrCb
, 1, DRM_FORMAT_YVU420
},
77 /* HACK: See droid_create_image_from_prime_fds() and
78 * https://issuetracker.google.com/32077885. */
79 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, YCbCr
, 2, DRM_FORMAT_NV12
},
80 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, YCbCr
, 1, DRM_FORMAT_YUV420
},
81 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, YCrCb
, 1, DRM_FORMAT_YVU420
},
82 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, YCrCb
, 1, DRM_FORMAT_AYUV
},
83 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, YCrCb
, 1, DRM_FORMAT_XYUV8888
},
87 get_fourcc_yuv(int native
, enum chroma_order chroma_order
, int chroma_step
)
89 for (int i
= 0; i
< ARRAY_SIZE(droid_yuv_formats
); ++i
)
90 if (droid_yuv_formats
[i
].native
== native
&&
91 droid_yuv_formats
[i
].chroma_order
== chroma_order
&&
92 droid_yuv_formats
[i
].chroma_step
== chroma_step
)
93 return droid_yuv_formats
[i
].fourcc
;
101 for (int i
= 0; i
< ARRAY_SIZE(droid_yuv_formats
); ++i
)
102 if (droid_yuv_formats
[i
].native
== native
)
109 get_format_bpp(int native
)
114 case HAL_PIXEL_FORMAT_RGBA_FP16
:
117 case HAL_PIXEL_FORMAT_RGBA_8888
:
118 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
120 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
121 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
123 case HAL_PIXEL_FORMAT_RGBX_8888
:
124 case HAL_PIXEL_FORMAT_BGRA_8888
:
125 case HAL_PIXEL_FORMAT_RGBA_1010102
:
128 case HAL_PIXEL_FORMAT_RGB_565
:
139 /* createImageFromFds requires fourcc format */
140 static int get_fourcc(int native
)
143 case HAL_PIXEL_FORMAT_RGB_565
: return DRM_FORMAT_RGB565
;
144 case HAL_PIXEL_FORMAT_BGRA_8888
: return DRM_FORMAT_ARGB8888
;
145 case HAL_PIXEL_FORMAT_RGBA_8888
: return DRM_FORMAT_ABGR8888
;
146 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
148 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
149 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
151 case HAL_PIXEL_FORMAT_RGBX_8888
: return DRM_FORMAT_XBGR8888
;
152 case HAL_PIXEL_FORMAT_RGBA_FP16
: return DRM_FORMAT_ABGR16161616F
;
153 case HAL_PIXEL_FORMAT_RGBA_1010102
: return DRM_FORMAT_ABGR2101010
;
155 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", native
);
160 /* returns # of fds, and by reference the actual fds */
162 get_native_buffer_fds(struct ANativeWindowBuffer
*buf
, int fds
[3])
164 native_handle_t
*handle
= (native_handle_t
*)buf
->handle
;
170 * Various gralloc implementations exist, but the dma-buf fd tends
171 * to be first. Access it directly to avoid a dependency on specific
174 for (int i
= 0; i
< handle
->numFds
; i
++)
175 fds
[i
] = handle
->data
[i
];
177 return handle
->numFds
;
180 #ifdef HAVE_DRM_GRALLOC
182 get_native_buffer_name(struct ANativeWindowBuffer
*buf
)
184 return gralloc_drm_get_gem_handle(buf
->handle
);
186 #endif /* HAVE_DRM_GRALLOC */
189 droid_window_dequeue_buffer(struct dri2_egl_surface
*dri2_surf
)
193 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
197 /* If access to the buffer is controlled by a sync fence, then block on the
200 * It may be more performant to postpone blocking until there is an
201 * immediate need to write to the buffer. But doing so would require adding
202 * hooks to the DRI2 loader.
204 * From the ANativeWindow::dequeueBuffer documentation:
206 * The libsync fence file descriptor returned in the int pointed to by
207 * the fenceFd argument will refer to the fence that must signal
208 * before the dequeued buffer may be written to. A value of -1
209 * indicates that the caller may access the buffer immediately without
210 * waiting on a fence. If a valid file descriptor is returned (i.e.
211 * any value except -1) then the caller is responsible for closing the
215 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
217 * Waits indefinitely if timeout < 0.
220 sync_wait(fence_fd
, timeout
);
224 /* Record all the buffers created by ANativeWindow and update back buffer
225 * for updating buffer's age in swap_buffers.
227 EGLBoolean updated
= EGL_FALSE
;
228 for (int i
= 0; i
< dri2_surf
->color_buffers_count
; i
++) {
229 if (!dri2_surf
->color_buffers
[i
].buffer
) {
230 dri2_surf
->color_buffers
[i
].buffer
= dri2_surf
->buffer
;
232 if (dri2_surf
->color_buffers
[i
].buffer
== dri2_surf
->buffer
) {
233 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
240 /* In case of all the buffers were recreated by ANativeWindow, reset
243 for (int i
= 0; i
< dri2_surf
->color_buffers_count
; i
++) {
244 dri2_surf
->color_buffers
[i
].buffer
= NULL
;
245 dri2_surf
->color_buffers
[i
].age
= 0;
247 dri2_surf
->color_buffers
[0].buffer
= dri2_surf
->buffer
;
248 dri2_surf
->back
= &dri2_surf
->color_buffers
[0];
255 droid_window_enqueue_buffer(_EGLDisplay
*disp
, struct dri2_egl_surface
*dri2_surf
)
257 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
259 /* To avoid blocking other EGL calls, release the display mutex before
260 * we enter droid_window_enqueue_buffer() and re-acquire the mutex upon
263 mtx_unlock(&disp
->Mutex
);
265 /* Queue the buffer with stored out fence fd. The ANativeWindow or buffer
266 * consumer may choose to wait for the fence to signal before accessing
267 * it. If fence fd value is -1, buffer can be accessed by consumer
268 * immediately. Consumer or application shouldn't rely on timestamp
269 * associated with fence if the fence fd is -1.
271 * Ownership of fd is transferred to consumer after queueBuffer and the
272 * consumer is responsible for closing it. Caller must not use the fd
273 * after passing it to queueBuffer.
275 int fence_fd
= dri2_surf
->out_fence_fd
;
276 dri2_surf
->out_fence_fd
= -1;
277 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
280 dri2_surf
->buffer
= NULL
;
281 dri2_surf
->back
= NULL
;
283 mtx_lock(&disp
->Mutex
);
285 if (dri2_surf
->dri_image_back
) {
286 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
287 dri2_surf
->dri_image_back
= NULL
;
294 droid_window_cancel_buffer(struct dri2_egl_surface
*dri2_surf
)
297 int fence_fd
= dri2_surf
->out_fence_fd
;
299 dri2_surf
->out_fence_fd
= -1;
300 ret
= dri2_surf
->window
->cancelBuffer(dri2_surf
->window
,
301 dri2_surf
->buffer
, fence_fd
);
302 dri2_surf
->buffer
= NULL
;
304 _eglLog(_EGL_WARNING
, "ANativeWindow::cancelBuffer failed");
305 dri2_surf
->base
.Lost
= EGL_TRUE
;
310 droid_set_shared_buffer_mode(_EGLDisplay
*disp
, _EGLSurface
*surf
, bool mode
)
312 #if ANDROID_API_LEVEL >= 24
313 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
314 struct ANativeWindow
*window
= dri2_surf
->window
;
316 assert(surf
->Type
== EGL_WINDOW_BIT
);
317 assert(_eglSurfaceHasMutableRenderBuffer(&dri2_surf
->base
));
319 _eglLog(_EGL_DEBUG
, "%s: mode=%d", __func__
, mode
);
321 if (native_window_set_shared_buffer_mode(window
, mode
)) {
322 _eglLog(_EGL_WARNING
, "failed native_window_set_shared_buffer_mode"
323 "(window=%p, mode=%d)", window
, mode
);
329 _eglLog(_EGL_FATAL
, "%s:%d: internal error: unreachable", __FILE__
, __LINE__
);
335 droid_create_surface(_EGLDisplay
*disp
, EGLint type
, _EGLConfig
*conf
,
336 void *native_window
, const EGLint
*attrib_list
)
338 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
339 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
340 struct dri2_egl_surface
*dri2_surf
;
341 struct ANativeWindow
*window
= native_window
;
342 const __DRIconfig
*config
;
344 dri2_surf
= calloc(1, sizeof *dri2_surf
);
346 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
350 if (!dri2_init_surface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
,
351 true, native_window
))
352 goto cleanup_surface
;
354 if (type
== EGL_WINDOW_BIT
) {
357 int min_buffer_count
, max_buffer_count
;
359 /* Prefer triple buffering for performance reasons. */
360 const int preferred_buffer_count
= 3;
362 if (window
->common
.magic
!= ANDROID_NATIVE_WINDOW_MAGIC
) {
363 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
364 goto cleanup_surface
;
366 if (window
->query(window
, NATIVE_WINDOW_FORMAT
, &format
)) {
367 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
368 goto cleanup_surface
;
371 /* Query ANativeWindow for MIN_UNDEQUEUED_BUFFER, minimum amount
372 * of undequeued buffers.
374 if (window
->query(window
, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS
,
375 &min_buffer_count
)) {
376 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
377 goto cleanup_surface
;
380 /* Query for maximum buffer count, application can set this
381 * to limit the total amount of buffers.
383 if (window
->query(window
, NATIVE_WINDOW_MAX_BUFFER_COUNT
,
384 &max_buffer_count
)) {
385 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
386 goto cleanup_surface
;
389 /* Clamp preferred between minimum (min undequeued + 1 dequeued)
392 buffer_count
= CLAMP(preferred_buffer_count
, min_buffer_count
+ 1,
395 if (native_window_set_buffer_count(window
, buffer_count
)) {
396 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
397 goto cleanup_surface
;
399 dri2_surf
->color_buffers
= calloc(buffer_count
,
400 sizeof(*dri2_surf
->color_buffers
));
401 if (!dri2_surf
->color_buffers
) {
402 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
403 goto cleanup_surface
;
405 dri2_surf
->color_buffers_count
= buffer_count
;
407 if (format
!= dri2_conf
->base
.NativeVisualID
) {
408 _eglLog(_EGL_WARNING
, "Native format mismatch: 0x%x != 0x%x",
409 format
, dri2_conf
->base
.NativeVisualID
);
412 window
->query(window
, NATIVE_WINDOW_WIDTH
, &dri2_surf
->base
.Width
);
413 window
->query(window
, NATIVE_WINDOW_HEIGHT
, &dri2_surf
->base
.Height
);
415 uint32_t usage
= strcmp(dri2_dpy
->driver_name
, "kms_swrast") == 0
416 ? GRALLOC_USAGE_SW_READ_OFTEN
| GRALLOC_USAGE_SW_WRITE_OFTEN
417 : GRALLOC_USAGE_HW_RENDER
;
418 native_window_set_usage(window
, usage
);
421 config
= dri2_get_dri_config(dri2_conf
, type
,
422 dri2_surf
->base
.GLColorspace
);
424 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
425 goto cleanup_surface
;
428 if (!dri2_create_drawable(dri2_dpy
, config
, dri2_surf
, dri2_surf
))
429 goto cleanup_surface
;
432 window
->common
.incRef(&window
->common
);
433 dri2_surf
->window
= window
;
436 return &dri2_surf
->base
;
439 if (dri2_surf
->color_buffers_count
)
440 free(dri2_surf
->color_buffers
);
447 droid_create_window_surface(_EGLDisplay
*disp
, _EGLConfig
*conf
,
448 void *native_window
, const EGLint
*attrib_list
)
450 return droid_create_surface(disp
, EGL_WINDOW_BIT
, conf
,
451 native_window
, attrib_list
);
455 droid_create_pbuffer_surface(_EGLDisplay
*disp
, _EGLConfig
*conf
,
456 const EGLint
*attrib_list
)
458 return droid_create_surface(disp
, EGL_PBUFFER_BIT
, conf
,
463 droid_destroy_surface(_EGLDisplay
*disp
, _EGLSurface
*surf
)
465 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
466 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
468 dri2_egl_surface_free_local_buffers(dri2_surf
);
470 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
471 if (dri2_surf
->buffer
)
472 droid_window_cancel_buffer(dri2_surf
);
474 dri2_surf
->window
->common
.decRef(&dri2_surf
->window
->common
);
477 if (dri2_surf
->dri_image_back
) {
478 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_back", __func__
, __LINE__
);
479 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
480 dri2_surf
->dri_image_back
= NULL
;
483 if (dri2_surf
->dri_image_front
) {
484 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_front", __func__
, __LINE__
);
485 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_front
);
486 dri2_surf
->dri_image_front
= NULL
;
489 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
491 dri2_fini_surface(surf
);
492 free(dri2_surf
->color_buffers
);
499 droid_swap_interval(const _EGLDriver
*drv
, _EGLDisplay
*disp
,
500 _EGLSurface
*surf
, EGLint interval
)
502 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
503 struct ANativeWindow
*window
= dri2_surf
->window
;
505 if (window
->setSwapInterval(window
, interval
))
508 surf
->SwapInterval
= interval
;
513 update_buffers(struct dri2_egl_surface
*dri2_surf
)
515 if (dri2_surf
->base
.Lost
)
518 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
521 /* try to dequeue the next back buffer */
522 if (!dri2_surf
->buffer
&& !droid_window_dequeue_buffer(dri2_surf
)) {
523 _eglLog(_EGL_WARNING
, "Could not dequeue buffer from native window");
524 dri2_surf
->base
.Lost
= EGL_TRUE
;
528 /* free outdated buffers and update the surface size */
529 if (dri2_surf
->base
.Width
!= dri2_surf
->buffer
->width
||
530 dri2_surf
->base
.Height
!= dri2_surf
->buffer
->height
) {
531 dri2_egl_surface_free_local_buffers(dri2_surf
);
532 dri2_surf
->base
.Width
= dri2_surf
->buffer
->width
;
533 dri2_surf
->base
.Height
= dri2_surf
->buffer
->height
;
540 get_front_bo(struct dri2_egl_surface
*dri2_surf
, unsigned int format
)
542 struct dri2_egl_display
*dri2_dpy
=
543 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
545 if (dri2_surf
->dri_image_front
)
548 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
549 /* According current EGL spec, front buffer rendering
550 * for window surface is not supported now.
551 * and mesa doesn't have the implementation of this case.
552 * Add warning message, but not treat it as error.
554 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported front buffer for window surface");
555 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
556 dri2_surf
->dri_image_front
=
557 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
558 dri2_surf
->base
.Width
,
559 dri2_surf
->base
.Height
,
563 if (!dri2_surf
->dri_image_front
) {
564 _eglLog(_EGL_WARNING
, "dri2_image_front allocation failed");
573 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
575 struct dri2_egl_display
*dri2_dpy
=
576 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
578 int offset
= 0, fds
[3];
581 if (dri2_surf
->dri_image_back
)
584 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
585 if (!dri2_surf
->buffer
) {
586 _eglLog(_EGL_WARNING
, "Could not get native buffer");
590 num_fds
= get_native_buffer_fds(dri2_surf
->buffer
, fds
);
592 _eglLog(_EGL_WARNING
, "Could not get native buffer FD");
596 fourcc
= get_fourcc(dri2_surf
->buffer
->format
);
598 pitch
= dri2_surf
->buffer
->stride
*
599 get_format_bpp(dri2_surf
->buffer
->format
);
601 if (fourcc
== -1 || pitch
== 0) {
602 _eglLog(_EGL_WARNING
, "Invalid buffer fourcc(%x) or pitch(%d)",
607 dri2_surf
->dri_image_back
=
608 dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
609 dri2_surf
->base
.Width
,
610 dri2_surf
->base
.Height
,
617 if (!dri2_surf
->dri_image_back
) {
618 _eglLog(_EGL_WARNING
, "failed to create DRI image from FD");
621 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
622 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
623 * the spec states that they have a back buffer but no front buffer, in
624 * contrast to pixmaps, which have a front buffer but no back buffer.
626 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
627 * from the spec, following the precedent of Mesa's EGL X11 platform. The
628 * X11 platform correctly assigns pbuffers to single-buffered configs, but
629 * assigns the pbuffer a front buffer instead of a back buffer.
631 * Pbuffers in the X11 platform mostly work today, so let's just copy its
632 * behavior instead of trying to fix (and hence potentially breaking) the
635 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported back buffer for pbuffer surface");
641 /* Some drivers will pass multiple bits in buffer_mask.
642 * For such case, will go through all the bits, and
643 * will not return error when unsupported buffer is requested, only
644 * return error when the allocation for supported buffer failed.
647 droid_image_get_buffers(__DRIdrawable
*driDrawable
,
651 uint32_t buffer_mask
,
652 struct __DRIimageList
*images
)
654 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
656 images
->image_mask
= 0;
657 images
->front
= NULL
;
660 if (update_buffers(dri2_surf
) < 0)
663 if (_eglSurfaceInSharedBufferMode(&dri2_surf
->base
)) {
664 if (get_back_bo(dri2_surf
) < 0)
667 /* We have dri_image_back because this is a window surface and
668 * get_back_bo() succeeded.
670 assert(dri2_surf
->dri_image_back
);
671 images
->back
= dri2_surf
->dri_image_back
;
672 images
->image_mask
|= __DRI_IMAGE_BUFFER_SHARED
;
674 /* There exists no accompanying back nor front buffer. */
678 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
679 if (get_front_bo(dri2_surf
, format
) < 0)
682 if (dri2_surf
->dri_image_front
) {
683 images
->front
= dri2_surf
->dri_image_front
;
684 images
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
688 if (buffer_mask
& __DRI_IMAGE_BUFFER_BACK
) {
689 if (get_back_bo(dri2_surf
) < 0)
692 if (dri2_surf
->dri_image_back
) {
693 images
->back
= dri2_surf
->dri_image_back
;
694 images
->image_mask
|= __DRI_IMAGE_BUFFER_BACK
;
702 droid_query_buffer_age(const _EGLDriver
*drv
,
703 _EGLDisplay
*disp
, _EGLSurface
*surface
)
705 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
707 if (update_buffers(dri2_surf
) < 0) {
708 _eglError(EGL_BAD_ALLOC
, "droid_query_buffer_age");
712 return dri2_surf
->back
? dri2_surf
->back
->age
: 0;
716 droid_swap_buffers(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
718 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
719 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
720 const bool has_mutable_rb
= _eglSurfaceHasMutableRenderBuffer(draw
);
722 /* From the EGL_KHR_mutable_render_buffer spec (v12):
724 * If surface is a single-buffered window, pixmap, or pbuffer surface
725 * for which there is no pending change to the EGL_RENDER_BUFFER
726 * attribute, eglSwapBuffers has no effect.
728 if (has_mutable_rb
&&
729 draw
->RequestedRenderBuffer
== EGL_SINGLE_BUFFER
&&
730 draw
->ActiveRenderBuffer
== EGL_SINGLE_BUFFER
) {
731 _eglLog(_EGL_DEBUG
, "%s: remain in shared buffer mode", __func__
);
735 for (int i
= 0; i
< dri2_surf
->color_buffers_count
; i
++) {
736 if (dri2_surf
->color_buffers
[i
].age
> 0)
737 dri2_surf
->color_buffers
[i
].age
++;
740 /* "XXX: we don't use get_back_bo() since it causes regressions in
741 * several dEQP tests.
744 dri2_surf
->back
->age
= 1;
746 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
748 /* dri2_surf->buffer can be null even when no error has occured. For
749 * example, if the user has called no GL rendering commands since the
750 * previous eglSwapBuffers, then the driver may have not triggered
751 * a callback to ANativeWindow::dequeueBuffer, in which case
752 * dri2_surf->buffer remains null.
754 if (dri2_surf
->buffer
)
755 droid_window_enqueue_buffer(disp
, dri2_surf
);
757 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
759 /* Update the shared buffer mode */
760 if (has_mutable_rb
&&
761 draw
->ActiveRenderBuffer
!= draw
->RequestedRenderBuffer
) {
762 bool mode
= (draw
->RequestedRenderBuffer
== EGL_SINGLE_BUFFER
);
763 _eglLog(_EGL_DEBUG
, "%s: change to shared buffer mode %d",
766 if (!droid_set_shared_buffer_mode(disp
, draw
, mode
))
768 draw
->ActiveRenderBuffer
= draw
->RequestedRenderBuffer
;
775 droid_create_image_from_prime_fds_yuv(_EGLDisplay
*disp
, _EGLContext
*ctx
,
776 struct ANativeWindowBuffer
*buf
,
777 int num_fds
, int fds
[3])
779 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
780 struct android_ycbcr ycbcr
;
783 enum chroma_order chroma_order
;
787 if (!dri2_dpy
->gralloc
->lock_ycbcr
) {
788 _eglLog(_EGL_WARNING
, "Gralloc does not support lock_ycbcr");
792 memset(&ycbcr
, 0, sizeof(ycbcr
));
793 ret
= dri2_dpy
->gralloc
->lock_ycbcr(dri2_dpy
->gralloc
, buf
->handle
,
794 0, 0, 0, 0, 0, &ycbcr
);
796 /* HACK: See droid_create_image_from_prime_fds() and
797 * https://issuetracker.google.com/32077885.*/
798 if (buf
->format
== HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
)
801 _eglLog(_EGL_WARNING
, "gralloc->lock_ycbcr failed: %d", ret
);
804 dri2_dpy
->gralloc
->unlock(dri2_dpy
->gralloc
, buf
->handle
);
806 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
807 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
808 * so they can be interpreted as offsets. */
809 offsets
[0] = (size_t)ycbcr
.y
;
810 /* We assume here that all the planes are located in one DMA-buf. */
811 if ((size_t)ycbcr
.cr
< (size_t)ycbcr
.cb
) {
812 chroma_order
= YCrCb
;
813 offsets
[1] = (size_t)ycbcr
.cr
;
814 offsets
[2] = (size_t)ycbcr
.cb
;
816 chroma_order
= YCbCr
;
817 offsets
[1] = (size_t)ycbcr
.cb
;
818 offsets
[2] = (size_t)ycbcr
.cr
;
821 /* .ystride is the line length (in bytes) of the Y plane,
822 * .cstride is the line length (in bytes) of any of the remaining
823 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
825 pitches
[0] = ycbcr
.ystride
;
826 pitches
[1] = pitches
[2] = ycbcr
.cstride
;
828 /* .chroma_step is the byte distance between the same chroma channel
829 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
830 fourcc
= get_fourcc_yuv(buf
->format
, chroma_order
, ycbcr
.chroma_step
);
832 _eglLog(_EGL_WARNING
, "unsupported YUV format, native = %x, chroma_order = %s, chroma_step = %d",
833 buf
->format
, chroma_order
== YCbCr
? "YCbCr" : "YCrCb", ycbcr
.chroma_step
);
838 * Since this is EGL_NATIVE_BUFFER_ANDROID don't assume that
839 * the single-fd case cannot happen. So handle eithe single
840 * fd or fd-per-plane case:
843 fds
[2] = fds
[1] = fds
[0];
845 int expected_planes
= (ycbcr
.chroma_step
== 2) ? 2 : 3;
846 assert(num_fds
== expected_planes
);
849 if (ycbcr
.chroma_step
== 2) {
850 /* Semi-planar Y + CbCr or Y + CrCb format. */
851 const EGLint attr_list_2plane
[] = {
852 EGL_WIDTH
, buf
->width
,
853 EGL_HEIGHT
, buf
->height
,
854 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
855 EGL_DMA_BUF_PLANE0_FD_EXT
, fds
[0],
856 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
857 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, offsets
[0],
858 EGL_DMA_BUF_PLANE1_FD_EXT
, fds
[1],
859 EGL_DMA_BUF_PLANE1_PITCH_EXT
, pitches
[1],
860 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, offsets
[1],
864 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_2plane
);
866 /* Fully planar Y + Cb + Cr or Y + Cr + Cb format. */
867 const EGLint attr_list_3plane
[] = {
868 EGL_WIDTH
, buf
->width
,
869 EGL_HEIGHT
, buf
->height
,
870 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
871 EGL_DMA_BUF_PLANE0_FD_EXT
, fds
[0],
872 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
873 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, offsets
[0],
874 EGL_DMA_BUF_PLANE1_FD_EXT
, fds
[1],
875 EGL_DMA_BUF_PLANE1_PITCH_EXT
, pitches
[1],
876 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, offsets
[1],
877 EGL_DMA_BUF_PLANE2_FD_EXT
, fds
[2],
878 EGL_DMA_BUF_PLANE2_PITCH_EXT
, pitches
[2],
879 EGL_DMA_BUF_PLANE2_OFFSET_EXT
, offsets
[2],
883 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_3plane
);
888 droid_create_image_from_prime_fds(_EGLDisplay
*disp
, _EGLContext
*ctx
,
889 struct ANativeWindowBuffer
*buf
, int num_fds
, int fds
[3])
893 if (is_yuv(buf
->format
)) {
896 image
= droid_create_image_from_prime_fds_yuv(disp
, ctx
, buf
, num_fds
, fds
);
898 * HACK: https://issuetracker.google.com/32077885
899 * There is no API available to properly query the IMPLEMENTATION_DEFINED
900 * format. As a workaround we rely here on gralloc allocating either
901 * an arbitrary YCbCr 4:2:0 or RGBX_8888, with the latter being recognized
902 * by lock_ycbcr failing.
904 if (image
|| buf
->format
!= HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
)
909 * Non-YUV formats could *also* have multiple planes, such as ancillary
910 * color compression state buffer, but the rest of the code isn't ready
911 * yet to deal with modifiers:
913 assert(num_fds
== 1);
915 const int fourcc
= get_fourcc(buf
->format
);
917 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
921 pitch
= buf
->stride
* get_format_bpp(buf
->format
);
923 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
927 const EGLint attr_list
[] = {
928 EGL_WIDTH
, buf
->width
,
929 EGL_HEIGHT
, buf
->height
,
930 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
931 EGL_DMA_BUF_PLANE0_FD_EXT
, fds
[0],
932 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitch
,
933 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, 0,
937 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list
);
940 #ifdef HAVE_DRM_GRALLOC
941 static int get_format(int format
)
944 case HAL_PIXEL_FORMAT_BGRA_8888
: return __DRI_IMAGE_FORMAT_ARGB8888
;
945 case HAL_PIXEL_FORMAT_RGB_565
: return __DRI_IMAGE_FORMAT_RGB565
;
946 case HAL_PIXEL_FORMAT_RGBA_8888
: return __DRI_IMAGE_FORMAT_ABGR8888
;
947 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
949 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
950 * TODO: Revert this once https://issuetracker.google.com/32077885 is fixed.
952 case HAL_PIXEL_FORMAT_RGBX_8888
: return __DRI_IMAGE_FORMAT_XBGR8888
;
953 case HAL_PIXEL_FORMAT_RGBA_FP16
: return __DRI_IMAGE_FORMAT_ABGR16161616F
;
954 case HAL_PIXEL_FORMAT_RGBA_1010102
: return __DRI_IMAGE_FORMAT_ABGR2101010
;
956 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", format
);
962 droid_create_image_from_name(_EGLDisplay
*disp
, _EGLContext
*ctx
,
963 struct ANativeWindowBuffer
*buf
)
965 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
966 struct dri2_egl_image
*dri2_img
;
970 name
= get_native_buffer_name(buf
);
972 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
976 format
= get_format(buf
->format
);
980 dri2_img
= calloc(1, sizeof(*dri2_img
));
982 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
986 _eglInitImage(&dri2_img
->base
, disp
);
988 dri2_img
->dri_image
=
989 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
996 if (!dri2_img
->dri_image
) {
998 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
1002 return &dri2_img
->base
;
1004 #endif /* HAVE_DRM_GRALLOC */
1007 droid_query_surface(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1008 EGLint attribute
, EGLint
*value
)
1010 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1011 switch (attribute
) {
1013 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
1014 dri2_surf
->window
->query(dri2_surf
->window
,
1015 NATIVE_WINDOW_DEFAULT_WIDTH
, value
);
1020 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
1021 dri2_surf
->window
->query(dri2_surf
->window
,
1022 NATIVE_WINDOW_DEFAULT_HEIGHT
, value
);
1029 return _eglQuerySurface(drv
, disp
, surf
, attribute
, value
);
1033 dri2_create_image_android_native_buffer(_EGLDisplay
*disp
,
1035 struct ANativeWindowBuffer
*buf
)
1041 /* From the EGL_ANDROID_image_native_buffer spec:
1043 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
1044 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
1046 _eglError(EGL_BAD_CONTEXT
, "eglCreateEGLImageKHR: for "
1047 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
1052 if (!buf
|| buf
->common
.magic
!= ANDROID_NATIVE_BUFFER_MAGIC
||
1053 buf
->common
.version
!= sizeof(*buf
)) {
1054 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
1058 num_fds
= get_native_buffer_fds(buf
, fds
);
1060 return droid_create_image_from_prime_fds(disp
, ctx
, buf
, num_fds
, fds
);
1062 #ifdef HAVE_DRM_GRALLOC
1063 return droid_create_image_from_name(disp
, ctx
, buf
);
1070 droid_create_image_khr(const _EGLDriver
*drv
, _EGLDisplay
*disp
,
1071 _EGLContext
*ctx
, EGLenum target
,
1072 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1075 case EGL_NATIVE_BUFFER_ANDROID
:
1076 return dri2_create_image_android_native_buffer(disp
, ctx
,
1077 (struct ANativeWindowBuffer
*) buffer
);
1079 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
1084 droid_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
1088 #ifdef HAVE_DRM_GRALLOC
1090 droid_get_buffers_parse_attachments(struct dri2_egl_surface
*dri2_surf
,
1091 unsigned int *attachments
, int count
)
1093 int num_buffers
= 0;
1095 /* fill dri2_surf->buffers */
1096 for (int i
= 0; i
< count
* 2; i
+= 2) {
1097 __DRIbuffer
*buf
, *local
;
1099 assert(num_buffers
< ARRAY_SIZE(dri2_surf
->buffers
));
1100 buf
= &dri2_surf
->buffers
[num_buffers
];
1102 switch (attachments
[i
]) {
1103 case __DRI_BUFFER_BACK_LEFT
:
1104 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
1105 buf
->attachment
= attachments
[i
];
1106 buf
->name
= get_native_buffer_name(dri2_surf
->buffer
);
1107 buf
->cpp
= get_format_bpp(dri2_surf
->buffer
->format
);
1108 buf
->pitch
= dri2_surf
->buffer
->stride
* buf
->cpp
;
1116 /* fall through for pbuffers */
1117 case __DRI_BUFFER_DEPTH
:
1118 case __DRI_BUFFER_STENCIL
:
1119 case __DRI_BUFFER_ACCUM
:
1120 case __DRI_BUFFER_DEPTH_STENCIL
:
1121 case __DRI_BUFFER_HIZ
:
1122 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
,
1123 attachments
[i
], attachments
[i
+ 1]);
1130 case __DRI_BUFFER_FRONT_LEFT
:
1131 case __DRI_BUFFER_FRONT_RIGHT
:
1132 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
1133 case __DRI_BUFFER_FAKE_FRONT_RIGHT
:
1134 case __DRI_BUFFER_BACK_RIGHT
:
1136 /* no front or right buffers */
1144 static __DRIbuffer
*
1145 droid_get_buffers_with_format(__DRIdrawable
* driDrawable
,
1146 int *width
, int *height
,
1147 unsigned int *attachments
, int count
,
1148 int *out_count
, void *loaderPrivate
)
1150 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1152 if (update_buffers(dri2_surf
) < 0)
1155 *out_count
= droid_get_buffers_parse_attachments(dri2_surf
, attachments
, count
);
1158 *width
= dri2_surf
->base
.Width
;
1160 *height
= dri2_surf
->base
.Height
;
1162 return dri2_surf
->buffers
;
1164 #endif /* HAVE_DRM_GRALLOC */
1167 droid_get_capability(void *loaderPrivate
, enum dri_loader_cap cap
)
1169 /* Note: loaderPrivate is _EGLDisplay* */
1171 case DRI_LOADER_CAP_RGBA_ORDERING
:
1179 droid_add_configs_for_visuals(_EGLDisplay
*disp
)
1181 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1182 static const struct {
1185 unsigned int rgba_sizes
[4];
1187 { HAL_PIXEL_FORMAT_RGBA_8888
, { 0, 8, 16, 24 }, { 8, 8, 8, 8 } },
1188 { HAL_PIXEL_FORMAT_RGBX_8888
, { 0, 8, 16, -1 }, { 8, 8, 8, 0 } },
1189 { HAL_PIXEL_FORMAT_RGB_565
, { 11, 5, 0, -1 }, { 5, 6, 5, 0 } },
1190 /* This must be after HAL_PIXEL_FORMAT_RGBA_8888, we only keep BGRA
1191 * visual if it turns out RGBA visual is not available.
1193 { HAL_PIXEL_FORMAT_BGRA_8888
, { 16, 8, 0, 24 }, { 8, 8, 8, 8 } },
1196 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1197 int config_count
= 0;
1199 /* The nesting of loops is significant here. Also significant is the order
1200 * of the HAL pixel formats. Many Android apps (such as Google's official
1201 * NDK GLES2 example app), and even portions the core framework code (such
1202 * as SystemServiceManager in Nougat), incorrectly choose their EGLConfig.
1203 * They neglect to match the EGLConfig's EGL_NATIVE_VISUAL_ID against the
1204 * window's native format, and instead choose the first EGLConfig whose
1205 * channel sizes match those of the native window format while ignoring the
1206 * channel *ordering*.
1208 * We can detect such buggy clients in logcat when they call
1209 * eglCreateSurface, by detecting the mismatch between the EGLConfig's
1210 * format and the window's format.
1212 * As a workaround, we generate EGLConfigs such that all EGLConfigs for HAL
1213 * pixel format i precede those for HAL pixel format i+1. In my
1214 * (chadversary) testing on Android Nougat, this was good enough to pacify
1215 * the buggy clients.
1217 bool has_rgba
= false;
1218 for (int i
= 0; i
< ARRAY_SIZE(visuals
); i
++) {
1219 /* Only enable BGRA configs when RGBA is not available. BGRA configs are
1220 * buggy on stock Android.
1222 if (visuals
[i
].format
== HAL_PIXEL_FORMAT_BGRA_8888
&& has_rgba
)
1224 for (int j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
1225 const EGLint surface_type
= EGL_WINDOW_BIT
| EGL_PBUFFER_BIT
;
1227 const EGLint config_attrs
[] = {
1228 EGL_NATIVE_VISUAL_ID
, visuals
[i
].format
,
1229 EGL_NATIVE_VISUAL_TYPE
, visuals
[i
].format
,
1230 EGL_FRAMEBUFFER_TARGET_ANDROID
, EGL_TRUE
,
1231 EGL_RECORDABLE_ANDROID
, EGL_TRUE
,
1235 struct dri2_egl_config
*dri2_conf
=
1236 dri2_add_config(disp
, dri2_dpy
->driver_configs
[j
],
1237 config_count
+ 1, surface_type
, config_attrs
,
1238 visuals
[i
].rgba_shifts
, visuals
[i
].rgba_sizes
);
1240 if (dri2_conf
->base
.ConfigID
== config_count
+ 1)
1245 if (visuals
[i
].format
== HAL_PIXEL_FORMAT_RGBA_8888
&& format_count
[i
])
1249 for (int i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1250 if (!format_count
[i
]) {
1251 _eglLog(_EGL_DEBUG
, "No DRI config supports native format 0x%x",
1256 return (config_count
!= 0);
1259 static const struct dri2_egl_display_vtbl droid_display_vtbl
= {
1260 .authenticate
= NULL
,
1261 .create_window_surface
= droid_create_window_surface
,
1262 .create_pbuffer_surface
= droid_create_pbuffer_surface
,
1263 .destroy_surface
= droid_destroy_surface
,
1264 .create_image
= droid_create_image_khr
,
1265 .swap_buffers
= droid_swap_buffers
,
1266 .swap_interval
= droid_swap_interval
,
1267 .query_buffer_age
= droid_query_buffer_age
,
1268 .query_surface
= droid_query_surface
,
1269 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1270 .set_shared_buffer_mode
= droid_set_shared_buffer_mode
,
1273 #ifdef HAVE_DRM_GRALLOC
1274 static const __DRIdri2LoaderExtension droid_dri2_loader_extension
= {
1275 .base
= { __DRI_DRI2_LOADER
, 4 },
1278 .flushFrontBuffer
= droid_flush_front_buffer
,
1279 .getBuffersWithFormat
= droid_get_buffers_with_format
,
1280 .getCapability
= droid_get_capability
,
1283 static const __DRIextension
*droid_dri2_loader_extensions
[] = {
1284 &droid_dri2_loader_extension
.base
,
1285 &image_lookup_extension
.base
,
1286 &use_invalidate
.base
,
1287 /* No __DRI_MUTABLE_RENDER_BUFFER_LOADER because it requires
1288 * __DRI_IMAGE_LOADER.
1292 #endif /* HAVE_DRM_GRALLOC */
1294 static const __DRIimageLoaderExtension droid_image_loader_extension
= {
1295 .base
= { __DRI_IMAGE_LOADER
, 2 },
1297 .getBuffers
= droid_image_get_buffers
,
1298 .flushFrontBuffer
= droid_flush_front_buffer
,
1299 .getCapability
= droid_get_capability
,
1303 droid_display_shared_buffer(__DRIdrawable
*driDrawable
, int fence_fd
,
1304 void *loaderPrivate
)
1306 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1307 struct ANativeWindowBuffer
*old_buffer UNUSED
= dri2_surf
->buffer
;
1309 if (!_eglSurfaceInSharedBufferMode(&dri2_surf
->base
)) {
1310 _eglLog(_EGL_WARNING
, "%s: internal error: buffer is not shared",
1315 if (fence_fd
>= 0) {
1316 /* The driver's fence is more recent than the surface's out fence, if it
1317 * exists at all. So use the driver's fence.
1319 if (dri2_surf
->out_fence_fd
>= 0) {
1320 close(dri2_surf
->out_fence_fd
);
1321 dri2_surf
->out_fence_fd
= -1;
1323 } else if (dri2_surf
->out_fence_fd
>= 0) {
1324 fence_fd
= dri2_surf
->out_fence_fd
;
1325 dri2_surf
->out_fence_fd
= -1;
1328 if (dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
1330 _eglLog(_EGL_WARNING
, "%s: ANativeWindow::queueBuffer failed", __func__
);
1337 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
1339 /* Tear down the surface because it no longer has a back buffer. */
1340 struct dri2_egl_display
*dri2_dpy
=
1341 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1343 _eglLog(_EGL_WARNING
, "%s: ANativeWindow::dequeueBuffer failed", __func__
);
1345 dri2_surf
->base
.Lost
= true;
1346 dri2_surf
->buffer
= NULL
;
1347 dri2_surf
->back
= NULL
;
1349 if (dri2_surf
->dri_image_back
) {
1350 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
1351 dri2_surf
->dri_image_back
= NULL
;
1354 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
1361 /* Access to the buffer is controlled by a sync fence. Block on it.
1363 * Ideally, we would submit the fence to the driver, and the driver would
1364 * postpone command execution until it signalled. But DRI lacks API for
1365 * that (as of 2018-04-11).
1367 * SYNC_IOC_WAIT waits forever if timeout < 0
1369 sync_wait(fence_fd
, -1);
1373 static const __DRImutableRenderBufferLoaderExtension droid_mutable_render_buffer_extension
= {
1374 .base
= { __DRI_MUTABLE_RENDER_BUFFER_LOADER
, 1 },
1375 .displaySharedBuffer
= droid_display_shared_buffer
,
1378 static const __DRIextension
*droid_image_loader_extensions
[] = {
1379 &droid_image_loader_extension
.base
,
1380 &image_lookup_extension
.base
,
1381 &use_invalidate
.base
,
1382 &droid_mutable_render_buffer_extension
.base
,
1387 droid_load_driver(_EGLDisplay
*disp
, bool swrast
)
1389 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1391 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1392 if (dri2_dpy
->driver_name
== NULL
)
1395 #ifdef HAVE_DRM_GRALLOC
1396 /* Handle control nodes using __DRI_DRI2_LOADER extension and GEM names
1397 * for backwards compatibility with drm_gralloc. (Do not use on new
1399 dri2_dpy
->loader_extensions
= droid_dri2_loader_extensions
;
1400 if (!dri2_load_driver(disp
)) {
1405 /* Use kms swrast only with vgem / virtio_gpu.
1406 * virtio-gpu fallbacks to software rendering when 3D features
1407 * are unavailable since 6c5ab.
1409 if (strcmp(dri2_dpy
->driver_name
, "vgem") == 0 ||
1410 strcmp(dri2_dpy
->driver_name
, "virtio_gpu") == 0) {
1411 free(dri2_dpy
->driver_name
);
1412 dri2_dpy
->driver_name
= strdup("kms_swrast");
1418 dri2_dpy
->loader_extensions
= droid_image_loader_extensions
;
1419 if (!dri2_load_driver_dri3(disp
)) {
1427 free(dri2_dpy
->driver_name
);
1428 dri2_dpy
->driver_name
= NULL
;
1433 droid_unload_driver(_EGLDisplay
*disp
)
1435 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1437 dlclose(dri2_dpy
->driver
);
1438 dri2_dpy
->driver
= NULL
;
1439 free(dri2_dpy
->driver_name
);
1440 dri2_dpy
->driver_name
= NULL
;
1444 droid_filter_device(_EGLDisplay
*disp
, int fd
, const char *vendor
)
1446 drmVersionPtr ver
= drmGetVersion(fd
);
1450 if (strcmp(vendor
, ver
->name
) != 0) {
1451 drmFreeVersion(ver
);
1455 drmFreeVersion(ver
);
1460 droid_probe_device(_EGLDisplay
*disp
, bool swrast
)
1462 /* Check that the device is supported, by attempting to:
1463 * - load the dri module
1464 * - and, create a screen
1466 if (!droid_load_driver(disp
, swrast
))
1469 if (!dri2_create_screen(disp
)) {
1470 _eglLog(_EGL_WARNING
, "DRI2: failed to create screen");
1471 droid_unload_driver(disp
);
1477 #ifdef HAVE_DRM_GRALLOC
1479 droid_open_device(_EGLDisplay
*disp
, bool swrast
)
1481 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1482 int fd
= -1, err
= -EINVAL
;
1487 if (dri2_dpy
->gralloc
->perform
)
1488 err
= dri2_dpy
->gralloc
->perform(dri2_dpy
->gralloc
,
1489 GRALLOC_MODULE_PERFORM_GET_DRM_FD
,
1491 if (err
|| fd
< 0) {
1492 _eglLog(_EGL_WARNING
, "fail to get drm fd");
1496 dri2_dpy
->fd
= os_dupfd_cloexec(fd
);
1497 if (dri2_dpy
->fd
< 0)
1500 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
)
1503 return droid_probe_device(disp
, swrast
);
1507 droid_open_device(_EGLDisplay
*disp
, bool swrast
)
1509 #define MAX_DRM_DEVICES 64
1510 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1511 drmDevicePtr device
, devices
[MAX_DRM_DEVICES
] = { NULL
};
1514 char *vendor_name
= NULL
;
1515 char vendor_buf
[PROPERTY_VALUE_MAX
];
1517 #ifdef EGL_FORCE_RENDERNODE
1518 const unsigned node_type
= DRM_NODE_RENDER
;
1520 const unsigned node_type
= swrast
? DRM_NODE_PRIMARY
: DRM_NODE_RENDER
;
1523 if (property_get("drm.gpu.vendor_name", vendor_buf
, NULL
) > 0)
1524 vendor_name
= vendor_buf
;
1526 num_devices
= drmGetDevices2(0, devices
, ARRAY_SIZE(devices
));
1527 if (num_devices
< 0)
1530 for (int i
= 0; i
< num_devices
; i
++) {
1531 device
= devices
[i
];
1533 if (!(device
->available_nodes
& (1 << node_type
)))
1536 dri2_dpy
->fd
= loader_open_device(device
->nodes
[node_type
]);
1537 if (dri2_dpy
->fd
< 0) {
1538 _eglLog(_EGL_WARNING
, "%s() Failed to open DRM device %s",
1539 __func__
, device
->nodes
[node_type
]);
1543 /* If a vendor is explicitly provided, we use only that.
1544 * Otherwise we fall-back the first device that is supported.
1547 if (droid_filter_device(disp
, dri2_dpy
->fd
, vendor_name
)) {
1548 /* Device does not match - try next device */
1549 close(dri2_dpy
->fd
);
1553 /* If the requested device matches - use it. Regardless if
1554 * init fails, do not fall-back to any other device.
1556 if (!droid_probe_device(disp
, false)) {
1557 close(dri2_dpy
->fd
);
1563 if (droid_probe_device(disp
, swrast
))
1566 /* No explicit request - attempt the next device */
1567 close(dri2_dpy
->fd
);
1570 drmFreeDevices(devices
, num_devices
);
1572 if (dri2_dpy
->fd
< 0) {
1573 _eglLog(_EGL_WARNING
, "Failed to open %s DRM device",
1574 vendor_name
? "desired": "any");
1579 #undef MAX_DRM_DEVICES
1585 dri2_initialize_android(_EGLDisplay
*disp
)
1588 bool device_opened
= false;
1589 struct dri2_egl_display
*dri2_dpy
;
1593 dri2_dpy
= calloc(1, sizeof(*dri2_dpy
));
1595 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1598 ret
= hw_get_module(GRALLOC_HARDWARE_MODULE_ID
,
1599 (const hw_module_t
**)&dri2_dpy
->gralloc
);
1601 err
= "DRI2: failed to get gralloc module";
1605 disp
->DriverData
= (void *) dri2_dpy
;
1606 if (!disp
->Options
.ForceSoftware
)
1607 device_opened
= droid_open_device(disp
, false);
1609 device_opened
= droid_open_device(disp
, true);
1611 if (!device_opened
) {
1612 err
= "DRI2: failed to open device";
1616 dev
= _eglAddDevice(dri2_dpy
->fd
, false);
1618 err
= "DRI2: failed to find EGLDevice";
1624 if (!dri2_setup_extensions(disp
)) {
1625 err
= "DRI2: failed to setup extensions";
1629 dri2_setup_screen(disp
);
1631 /* We set the maximum swap interval as 1 for Android platform, since it is
1632 * the maximum value supported by Android according to the value of
1633 * ANativeWindow::maxSwapInterval.
1635 dri2_setup_swap_interval(disp
, 1);
1637 disp
->Extensions
.ANDROID_framebuffer_target
= EGL_TRUE
;
1638 disp
->Extensions
.ANDROID_image_native_buffer
= EGL_TRUE
;
1639 disp
->Extensions
.ANDROID_recordable
= EGL_TRUE
;
1641 /* Querying buffer age requires a buffer to be dequeued. Without
1642 * EGL_ANDROID_native_fence_sync, dequeue might call eglClientWaitSync and
1643 * result in a deadlock (the lock is already held by eglQuerySurface).
1645 if (disp
->Extensions
.ANDROID_native_fence_sync
) {
1646 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1648 /* disable KHR_partial_update that might have been enabled in
1651 disp
->Extensions
.KHR_partial_update
= EGL_FALSE
;
1654 disp
->Extensions
.KHR_image
= EGL_TRUE
;
1655 #if ANDROID_API_LEVEL >= 24
1656 if (dri2_dpy
->mutable_render_buffer
&&
1657 dri2_dpy
->loader_extensions
== droid_image_loader_extensions
) {
1658 disp
->Extensions
.KHR_mutable_render_buffer
= EGL_TRUE
;
1662 /* Create configs *after* enabling extensions because presence of DRI
1663 * driver extensions can affect the capabilities of EGLConfigs.
1665 if (!droid_add_configs_for_visuals(disp
)) {
1666 err
= "DRI2: failed to add configs";
1670 /* Fill vtbl last to prevent accidentally calling virtual function during
1673 dri2_dpy
->vtbl
= &droid_display_vtbl
;
1678 dri2_display_destroy(disp
);
1679 return _eglError(EGL_NOT_INITIALIZED
, err
);