2 * Mesa 3-D graphics library
4 * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
5 * Copyright (C) 2010-2011 LunarG Inc.
7 * Based on platform_x11, which has
9 * Copyright © 2011 Intel Corporation
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
30 #include <cutils/properties.h>
38 #include <sync/sync.h>
39 #include <sys/types.h>
43 #include "egl_dri2_fallbacks.h"
45 #ifdef HAVE_DRM_GRALLOC
46 #include <gralloc_drm_handle.h>
47 #include "gralloc_drm.h"
48 #endif /* HAVE_DRM_GRALLOC */
50 #define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1))
57 struct droid_yuv_format
{
59 int native
; /* HAL_PIXEL_FORMAT_ */
60 enum chroma_order chroma_order
; /* chroma order is {Cb, Cr} or {Cr, Cb} */
61 int chroma_step
; /* Distance in bytes between subsequent chroma pixels. */
64 int fourcc
; /* DRM_FORMAT_ */
67 /* The following table is used to look up a DRI image FourCC based
68 * on native format and information contained in android_ycbcr struct. */
69 static const struct droid_yuv_format droid_yuv_formats
[] = {
70 /* Native format, YCrCb, Chroma step, DRI image FourCC */
71 { HAL_PIXEL_FORMAT_YCbCr_420_888
, YCbCr
, 2, DRM_FORMAT_NV12
},
72 { HAL_PIXEL_FORMAT_YCbCr_420_888
, YCbCr
, 1, DRM_FORMAT_YUV420
},
73 { HAL_PIXEL_FORMAT_YCbCr_420_888
, YCrCb
, 1, DRM_FORMAT_YVU420
},
74 { HAL_PIXEL_FORMAT_YV12
, YCrCb
, 1, DRM_FORMAT_YVU420
},
75 /* HACK: See droid_create_image_from_prime_fds() and
76 * https://issuetracker.google.com/32077885. */
77 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, YCbCr
, 2, DRM_FORMAT_NV12
},
78 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, YCbCr
, 1, DRM_FORMAT_YUV420
},
79 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, YCrCb
, 1, DRM_FORMAT_YVU420
},
80 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, YCrCb
, 1, DRM_FORMAT_AYUV
},
81 { HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
, YCrCb
, 1, DRM_FORMAT_XYUV8888
},
85 get_fourcc_yuv(int native
, enum chroma_order chroma_order
, int chroma_step
)
87 for (int i
= 0; i
< ARRAY_SIZE(droid_yuv_formats
); ++i
)
88 if (droid_yuv_formats
[i
].native
== native
&&
89 droid_yuv_formats
[i
].chroma_order
== chroma_order
&&
90 droid_yuv_formats
[i
].chroma_step
== chroma_step
)
91 return droid_yuv_formats
[i
].fourcc
;
99 for (int i
= 0; i
< ARRAY_SIZE(droid_yuv_formats
); ++i
)
100 if (droid_yuv_formats
[i
].native
== native
)
107 get_format_bpp(int native
)
112 case HAL_PIXEL_FORMAT_RGBA_FP16
:
115 case HAL_PIXEL_FORMAT_RGBA_8888
:
116 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
118 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
119 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
121 case HAL_PIXEL_FORMAT_RGBX_8888
:
122 case HAL_PIXEL_FORMAT_BGRA_8888
:
123 case HAL_PIXEL_FORMAT_RGBA_1010102
:
126 case HAL_PIXEL_FORMAT_RGB_565
:
137 /* createImageFromFds requires fourcc format */
138 static int get_fourcc(int native
)
141 case HAL_PIXEL_FORMAT_RGB_565
: return DRM_FORMAT_RGB565
;
142 case HAL_PIXEL_FORMAT_BGRA_8888
: return DRM_FORMAT_ARGB8888
;
143 case HAL_PIXEL_FORMAT_RGBA_8888
: return DRM_FORMAT_ABGR8888
;
144 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
146 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
147 * TODO: Remove this once https://issuetracker.google.com/32077885 is fixed.
149 case HAL_PIXEL_FORMAT_RGBX_8888
: return DRM_FORMAT_XBGR8888
;
150 case HAL_PIXEL_FORMAT_RGBA_FP16
: return DRM_FORMAT_ABGR16161616F
;
151 case HAL_PIXEL_FORMAT_RGBA_1010102
: return DRM_FORMAT_ABGR2101010
;
153 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", native
);
158 static int get_format(int format
)
161 case HAL_PIXEL_FORMAT_BGRA_8888
: return __DRI_IMAGE_FORMAT_ARGB8888
;
162 case HAL_PIXEL_FORMAT_RGB_565
: return __DRI_IMAGE_FORMAT_RGB565
;
163 case HAL_PIXEL_FORMAT_RGBA_8888
: return __DRI_IMAGE_FORMAT_ABGR8888
;
164 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
166 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
167 * TODO: Revert this once https://issuetracker.google.com/32077885 is fixed.
169 case HAL_PIXEL_FORMAT_RGBX_8888
: return __DRI_IMAGE_FORMAT_XBGR8888
;
170 case HAL_PIXEL_FORMAT_RGBA_FP16
: return __DRI_IMAGE_FORMAT_ABGR16161616F
;
171 case HAL_PIXEL_FORMAT_RGBA_1010102
: return __DRI_IMAGE_FORMAT_ABGR2101010
;
173 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", format
);
178 /* returns # of fds, and by reference the actual fds */
180 get_native_buffer_fds(struct ANativeWindowBuffer
*buf
, int fds
[3])
182 native_handle_t
*handle
= (native_handle_t
*)buf
->handle
;
188 * Various gralloc implementations exist, but the dma-buf fd tends
189 * to be first. Access it directly to avoid a dependency on specific
192 for (int i
= 0; i
< handle
->numFds
; i
++)
193 fds
[i
] = handle
->data
[i
];
195 return handle
->numFds
;
198 #ifdef HAVE_DRM_GRALLOC
200 get_native_buffer_name(struct ANativeWindowBuffer
*buf
)
202 return gralloc_drm_get_gem_handle(buf
->handle
);
204 #endif /* HAVE_DRM_GRALLOC */
207 droid_window_dequeue_buffer(struct dri2_egl_surface
*dri2_surf
)
211 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
215 /* If access to the buffer is controlled by a sync fence, then block on the
218 * It may be more performant to postpone blocking until there is an
219 * immediate need to write to the buffer. But doing so would require adding
220 * hooks to the DRI2 loader.
222 * From the ANativeWindow::dequeueBuffer documentation:
224 * The libsync fence file descriptor returned in the int pointed to by
225 * the fenceFd argument will refer to the fence that must signal
226 * before the dequeued buffer may be written to. A value of -1
227 * indicates that the caller may access the buffer immediately without
228 * waiting on a fence. If a valid file descriptor is returned (i.e.
229 * any value except -1) then the caller is responsible for closing the
233 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
235 * Waits indefinitely if timeout < 0.
238 sync_wait(fence_fd
, timeout
);
242 dri2_surf
->buffer
->common
.incRef(&dri2_surf
->buffer
->common
);
244 /* Record all the buffers created by ANativeWindow and update back buffer
245 * for updating buffer's age in swap_buffers.
247 EGLBoolean updated
= EGL_FALSE
;
248 for (int i
= 0; i
< dri2_surf
->color_buffers_count
; i
++) {
249 if (!dri2_surf
->color_buffers
[i
].buffer
) {
250 dri2_surf
->color_buffers
[i
].buffer
= dri2_surf
->buffer
;
252 if (dri2_surf
->color_buffers
[i
].buffer
== dri2_surf
->buffer
) {
253 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
260 /* In case of all the buffers were recreated by ANativeWindow, reset
263 for (int i
= 0; i
< dri2_surf
->color_buffers_count
; i
++) {
264 dri2_surf
->color_buffers
[i
].buffer
= NULL
;
265 dri2_surf
->color_buffers
[i
].age
= 0;
267 dri2_surf
->color_buffers
[0].buffer
= dri2_surf
->buffer
;
268 dri2_surf
->back
= &dri2_surf
->color_buffers
[0];
275 droid_window_enqueue_buffer(_EGLDisplay
*disp
, struct dri2_egl_surface
*dri2_surf
)
277 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
279 /* To avoid blocking other EGL calls, release the display mutex before
280 * we enter droid_window_enqueue_buffer() and re-acquire the mutex upon
283 mtx_unlock(&disp
->Mutex
);
285 /* Queue the buffer with stored out fence fd. The ANativeWindow or buffer
286 * consumer may choose to wait for the fence to signal before accessing
287 * it. If fence fd value is -1, buffer can be accessed by consumer
288 * immediately. Consumer or application shouldn't rely on timestamp
289 * associated with fence if the fence fd is -1.
291 * Ownership of fd is transferred to consumer after queueBuffer and the
292 * consumer is responsible for closing it. Caller must not use the fd
293 * after passing it to queueBuffer.
295 int fence_fd
= dri2_surf
->out_fence_fd
;
296 dri2_surf
->out_fence_fd
= -1;
297 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
300 dri2_surf
->buffer
->common
.decRef(&dri2_surf
->buffer
->common
);
301 dri2_surf
->buffer
= NULL
;
302 dri2_surf
->back
= NULL
;
304 mtx_lock(&disp
->Mutex
);
306 if (dri2_surf
->dri_image_back
) {
307 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
308 dri2_surf
->dri_image_back
= NULL
;
315 droid_window_cancel_buffer(struct dri2_egl_surface
*dri2_surf
)
318 int fence_fd
= dri2_surf
->out_fence_fd
;
320 dri2_surf
->out_fence_fd
= -1;
321 ret
= dri2_surf
->window
->cancelBuffer(dri2_surf
->window
,
322 dri2_surf
->buffer
, fence_fd
);
324 _eglLog(_EGL_WARNING
, "ANativeWindow::cancelBuffer failed");
325 dri2_surf
->base
.Lost
= EGL_TRUE
;
330 droid_set_shared_buffer_mode(_EGLDisplay
*disp
, _EGLSurface
*surf
, bool mode
)
332 #if ANDROID_API_LEVEL >= 24
333 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
334 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
335 struct ANativeWindow
*window
= dri2_surf
->window
;
337 assert(surf
->Type
== EGL_WINDOW_BIT
);
338 assert(_eglSurfaceHasMutableRenderBuffer(&dri2_surf
->base
));
340 _eglLog(_EGL_DEBUG
, "%s: mode=%d", __func__
, mode
);
342 if (native_window_set_shared_buffer_mode(window
, mode
)) {
343 _eglLog(_EGL_WARNING
, "failed native_window_set_shared_buffer_mode"
344 "(window=%p, mode=%d)", window
, mode
);
350 _eglLog(_EGL_FATAL
, "%s:%d: internal error: unreachable", __FILE__
, __LINE__
);
356 droid_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
357 _EGLConfig
*conf
, void *native_window
,
358 const EGLint
*attrib_list
)
360 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
361 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
362 struct dri2_egl_surface
*dri2_surf
;
363 struct ANativeWindow
*window
= native_window
;
364 const __DRIconfig
*config
;
366 dri2_surf
= calloc(1, sizeof *dri2_surf
);
368 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
372 if (!dri2_init_surface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
,
373 true, native_window
))
374 goto cleanup_surface
;
376 if (type
== EGL_WINDOW_BIT
) {
380 if (window
->common
.magic
!= ANDROID_NATIVE_WINDOW_MAGIC
) {
381 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
382 goto cleanup_surface
;
384 if (window
->query(window
, NATIVE_WINDOW_FORMAT
, &format
)) {
385 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
386 goto cleanup_surface
;
389 /* Query ANativeWindow for MIN_UNDEQUEUED_BUFFER, set buffer count
390 * and allocate color_buffers.
392 if (window
->query(window
, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS
,
394 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
395 goto cleanup_surface
;
397 if (native_window_set_buffer_count(window
, buffer_count
+1)) {
398 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
399 goto cleanup_surface
;
401 dri2_surf
->color_buffers
= calloc(buffer_count
+1,
402 sizeof(*dri2_surf
->color_buffers
));
403 if (!dri2_surf
->color_buffers
) {
404 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
405 goto cleanup_surface
;
407 dri2_surf
->color_buffers_count
= buffer_count
+1;
409 if (format
!= dri2_conf
->base
.NativeVisualID
) {
410 _eglLog(_EGL_WARNING
, "Native format mismatch: 0x%x != 0x%x",
411 format
, dri2_conf
->base
.NativeVisualID
);
414 window
->query(window
, NATIVE_WINDOW_WIDTH
, &dri2_surf
->base
.Width
);
415 window
->query(window
, NATIVE_WINDOW_HEIGHT
, &dri2_surf
->base
.Height
);
418 config
= dri2_get_dri_config(dri2_conf
, type
,
419 dri2_surf
->base
.GLColorspace
);
421 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
422 goto cleanup_surface
;
425 if (!dri2_create_drawable(dri2_dpy
, config
, dri2_surf
, dri2_surf
))
426 goto cleanup_surface
;
429 window
->common
.incRef(&window
->common
);
430 dri2_surf
->window
= window
;
433 return &dri2_surf
->base
;
436 if (dri2_surf
->color_buffers_count
)
437 free(dri2_surf
->color_buffers
);
444 droid_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
445 _EGLConfig
*conf
, void *native_window
,
446 const EGLint
*attrib_list
)
448 return droid_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
449 native_window
, attrib_list
);
453 droid_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
454 _EGLConfig
*conf
, const EGLint
*attrib_list
)
456 return droid_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
461 droid_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
463 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
464 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
466 dri2_egl_surface_free_local_buffers(dri2_surf
);
468 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
469 if (dri2_surf
->buffer
)
470 droid_window_cancel_buffer(dri2_surf
);
472 dri2_surf
->window
->common
.decRef(&dri2_surf
->window
->common
);
475 if (dri2_surf
->dri_image_back
) {
476 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_back", __func__
, __LINE__
);
477 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
478 dri2_surf
->dri_image_back
= NULL
;
481 if (dri2_surf
->dri_image_front
) {
482 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_front", __func__
, __LINE__
);
483 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_front
);
484 dri2_surf
->dri_image_front
= NULL
;
487 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
489 dri2_fini_surface(surf
);
490 free(dri2_surf
->color_buffers
);
497 droid_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
,
498 _EGLSurface
*surf
, EGLint interval
)
500 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
501 struct ANativeWindow
*window
= dri2_surf
->window
;
503 if (window
->setSwapInterval(window
, interval
))
506 surf
->SwapInterval
= interval
;
511 update_buffers(struct dri2_egl_surface
*dri2_surf
)
513 if (dri2_surf
->base
.Lost
)
516 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
519 /* try to dequeue the next back buffer */
520 if (!dri2_surf
->buffer
&& !droid_window_dequeue_buffer(dri2_surf
)) {
521 _eglLog(_EGL_WARNING
, "Could not dequeue buffer from native window");
522 dri2_surf
->base
.Lost
= EGL_TRUE
;
526 /* free outdated buffers and update the surface size */
527 if (dri2_surf
->base
.Width
!= dri2_surf
->buffer
->width
||
528 dri2_surf
->base
.Height
!= dri2_surf
->buffer
->height
) {
529 dri2_egl_surface_free_local_buffers(dri2_surf
);
530 dri2_surf
->base
.Width
= dri2_surf
->buffer
->width
;
531 dri2_surf
->base
.Height
= dri2_surf
->buffer
->height
;
538 get_front_bo(struct dri2_egl_surface
*dri2_surf
, unsigned int format
)
540 struct dri2_egl_display
*dri2_dpy
=
541 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
543 if (dri2_surf
->dri_image_front
)
546 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
547 /* According current EGL spec, front buffer rendering
548 * for window surface is not supported now.
549 * and mesa doesn't have the implementation of this case.
550 * Add warning message, but not treat it as error.
552 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported front buffer for window surface");
553 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
554 dri2_surf
->dri_image_front
=
555 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
556 dri2_surf
->base
.Width
,
557 dri2_surf
->base
.Height
,
561 if (!dri2_surf
->dri_image_front
) {
562 _eglLog(_EGL_WARNING
, "dri2_image_front allocation failed");
571 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
573 struct dri2_egl_display
*dri2_dpy
=
574 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
576 int offset
= 0, fds
[3];
579 if (dri2_surf
->dri_image_back
)
582 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
583 if (!dri2_surf
->buffer
) {
584 _eglLog(_EGL_WARNING
, "Could not get native buffer");
588 num_fds
= get_native_buffer_fds(dri2_surf
->buffer
, fds
);
590 _eglLog(_EGL_WARNING
, "Could not get native buffer FD");
594 fourcc
= get_fourcc(dri2_surf
->buffer
->format
);
596 pitch
= dri2_surf
->buffer
->stride
*
597 get_format_bpp(dri2_surf
->buffer
->format
);
599 if (fourcc
== -1 || pitch
== 0) {
600 _eglLog(_EGL_WARNING
, "Invalid buffer fourcc(%x) or pitch(%d)",
605 dri2_surf
->dri_image_back
=
606 dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
607 dri2_surf
->base
.Width
,
608 dri2_surf
->base
.Height
,
615 if (!dri2_surf
->dri_image_back
) {
616 _eglLog(_EGL_WARNING
, "failed to create DRI image from FD");
619 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
620 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
621 * the spec states that they have a back buffer but no front buffer, in
622 * contrast to pixmaps, which have a front buffer but no back buffer.
624 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
625 * from the spec, following the precedent of Mesa's EGL X11 platform. The
626 * X11 platform correctly assigns pbuffers to single-buffered configs, but
627 * assigns the pbuffer a front buffer instead of a back buffer.
629 * Pbuffers in the X11 platform mostly work today, so let's just copy its
630 * behavior instead of trying to fix (and hence potentially breaking) the
633 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported back buffer for pbuffer surface");
639 /* Some drivers will pass multiple bits in buffer_mask.
640 * For such case, will go through all the bits, and
641 * will not return error when unsupported buffer is requested, only
642 * return error when the allocation for supported buffer failed.
645 droid_image_get_buffers(__DRIdrawable
*driDrawable
,
649 uint32_t buffer_mask
,
650 struct __DRIimageList
*images
)
652 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
654 images
->image_mask
= 0;
655 images
->front
= NULL
;
658 if (update_buffers(dri2_surf
) < 0)
661 if (_eglSurfaceInSharedBufferMode(&dri2_surf
->base
)) {
662 if (get_back_bo(dri2_surf
) < 0)
665 /* We have dri_image_back because this is a window surface and
666 * get_back_bo() succeeded.
668 assert(dri2_surf
->dri_image_back
);
669 images
->back
= dri2_surf
->dri_image_back
;
670 images
->image_mask
|= __DRI_IMAGE_BUFFER_SHARED
;
672 /* There exists no accompanying back nor front buffer. */
676 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
677 if (get_front_bo(dri2_surf
, format
) < 0)
680 if (dri2_surf
->dri_image_front
) {
681 images
->front
= dri2_surf
->dri_image_front
;
682 images
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
686 if (buffer_mask
& __DRI_IMAGE_BUFFER_BACK
) {
687 if (get_back_bo(dri2_surf
) < 0)
690 if (dri2_surf
->dri_image_back
) {
691 images
->back
= dri2_surf
->dri_image_back
;
692 images
->image_mask
|= __DRI_IMAGE_BUFFER_BACK
;
700 droid_query_buffer_age(_EGLDriver
*drv
,
701 _EGLDisplay
*disp
, _EGLSurface
*surface
)
703 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
705 if (update_buffers(dri2_surf
) < 0) {
706 _eglError(EGL_BAD_ALLOC
, "droid_query_buffer_age");
710 return dri2_surf
->back
? dri2_surf
->back
->age
: 0;
714 droid_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
716 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
717 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
718 const bool has_mutable_rb
= _eglSurfaceHasMutableRenderBuffer(draw
);
720 /* From the EGL_KHR_mutable_render_buffer spec (v12):
722 * If surface is a single-buffered window, pixmap, or pbuffer surface
723 * for which there is no pending change to the EGL_RENDER_BUFFER
724 * attribute, eglSwapBuffers has no effect.
726 if (has_mutable_rb
&&
727 draw
->RequestedRenderBuffer
== EGL_SINGLE_BUFFER
&&
728 draw
->ActiveRenderBuffer
== EGL_SINGLE_BUFFER
) {
729 _eglLog(_EGL_DEBUG
, "%s: remain in shared buffer mode", __func__
);
733 for (int i
= 0; i
< dri2_surf
->color_buffers_count
; i
++) {
734 if (dri2_surf
->color_buffers
[i
].age
> 0)
735 dri2_surf
->color_buffers
[i
].age
++;
738 /* "XXX: we don't use get_back_bo() since it causes regressions in
739 * several dEQP tests.
742 dri2_surf
->back
->age
= 1;
744 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
746 /* dri2_surf->buffer can be null even when no error has occured. For
747 * example, if the user has called no GL rendering commands since the
748 * previous eglSwapBuffers, then the driver may have not triggered
749 * a callback to ANativeWindow::dequeueBuffer, in which case
750 * dri2_surf->buffer remains null.
752 if (dri2_surf
->buffer
)
753 droid_window_enqueue_buffer(disp
, dri2_surf
);
755 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
757 /* Update the shared buffer mode */
758 if (has_mutable_rb
&&
759 draw
->ActiveRenderBuffer
!= draw
->RequestedRenderBuffer
) {
760 bool mode
= (draw
->RequestedRenderBuffer
== EGL_SINGLE_BUFFER
);
761 _eglLog(_EGL_DEBUG
, "%s: change to shared buffer mode %d",
764 if (!droid_set_shared_buffer_mode(disp
, draw
, mode
))
766 draw
->ActiveRenderBuffer
= draw
->RequestedRenderBuffer
;
773 droid_create_image_from_prime_fds_yuv(_EGLDisplay
*disp
, _EGLContext
*ctx
,
774 struct ANativeWindowBuffer
*buf
,
775 int num_fds
, int fds
[3])
777 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
778 struct android_ycbcr ycbcr
;
781 enum chroma_order chroma_order
;
785 if (!dri2_dpy
->gralloc
->lock_ycbcr
) {
786 _eglLog(_EGL_WARNING
, "Gralloc does not support lock_ycbcr");
790 memset(&ycbcr
, 0, sizeof(ycbcr
));
791 ret
= dri2_dpy
->gralloc
->lock_ycbcr(dri2_dpy
->gralloc
, buf
->handle
,
792 0, 0, 0, 0, 0, &ycbcr
);
794 /* HACK: See droid_create_image_from_prime_fds() and
795 * https://issuetracker.google.com/32077885.*/
796 if (buf
->format
== HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
)
799 _eglLog(_EGL_WARNING
, "gralloc->lock_ycbcr failed: %d", ret
);
802 dri2_dpy
->gralloc
->unlock(dri2_dpy
->gralloc
, buf
->handle
);
804 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
805 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
806 * so they can be interpreted as offsets. */
807 offsets
[0] = (size_t)ycbcr
.y
;
808 /* We assume here that all the planes are located in one DMA-buf. */
809 if ((size_t)ycbcr
.cr
< (size_t)ycbcr
.cb
) {
810 chroma_order
= YCrCb
;
811 offsets
[1] = (size_t)ycbcr
.cr
;
812 offsets
[2] = (size_t)ycbcr
.cb
;
814 chroma_order
= YCbCr
;
815 offsets
[1] = (size_t)ycbcr
.cb
;
816 offsets
[2] = (size_t)ycbcr
.cr
;
819 /* .ystride is the line length (in bytes) of the Y plane,
820 * .cstride is the line length (in bytes) of any of the remaining
821 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
823 pitches
[0] = ycbcr
.ystride
;
824 pitches
[1] = pitches
[2] = ycbcr
.cstride
;
826 /* .chroma_step is the byte distance between the same chroma channel
827 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
828 fourcc
= get_fourcc_yuv(buf
->format
, chroma_order
, ycbcr
.chroma_step
);
830 _eglLog(_EGL_WARNING
, "unsupported YUV format, native = %x, chroma_order = %s, chroma_step = %d",
831 buf
->format
, chroma_order
== YCbCr
? "YCbCr" : "YCrCb", ycbcr
.chroma_step
);
836 * Since this is EGL_NATIVE_BUFFER_ANDROID don't assume that
837 * the single-fd case cannot happen. So handle eithe single
838 * fd or fd-per-plane case:
841 fds
[2] = fds
[1] = fds
[0];
843 int expected_planes
= (ycbcr
.chroma_step
== 2) ? 2 : 3;
844 assert(num_fds
== expected_planes
);
847 if (ycbcr
.chroma_step
== 2) {
848 /* Semi-planar Y + CbCr or Y + CrCb format. */
849 const EGLint attr_list_2plane
[] = {
850 EGL_WIDTH
, buf
->width
,
851 EGL_HEIGHT
, buf
->height
,
852 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
853 EGL_DMA_BUF_PLANE0_FD_EXT
, fds
[0],
854 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
855 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, offsets
[0],
856 EGL_DMA_BUF_PLANE1_FD_EXT
, fds
[1],
857 EGL_DMA_BUF_PLANE1_PITCH_EXT
, pitches
[1],
858 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, offsets
[1],
862 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_2plane
);
864 /* Fully planar Y + Cb + Cr or Y + Cr + Cb format. */
865 const EGLint attr_list_3plane
[] = {
866 EGL_WIDTH
, buf
->width
,
867 EGL_HEIGHT
, buf
->height
,
868 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
869 EGL_DMA_BUF_PLANE0_FD_EXT
, fds
[0],
870 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitches
[0],
871 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, offsets
[0],
872 EGL_DMA_BUF_PLANE1_FD_EXT
, fds
[1],
873 EGL_DMA_BUF_PLANE1_PITCH_EXT
, pitches
[1],
874 EGL_DMA_BUF_PLANE1_OFFSET_EXT
, offsets
[1],
875 EGL_DMA_BUF_PLANE2_FD_EXT
, fds
[2],
876 EGL_DMA_BUF_PLANE2_PITCH_EXT
, pitches
[2],
877 EGL_DMA_BUF_PLANE2_OFFSET_EXT
, offsets
[2],
881 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list_3plane
);
886 droid_create_image_from_prime_fds(_EGLDisplay
*disp
, _EGLContext
*ctx
,
887 struct ANativeWindowBuffer
*buf
, int num_fds
, int fds
[3])
891 if (is_yuv(buf
->format
)) {
894 image
= droid_create_image_from_prime_fds_yuv(disp
, ctx
, buf
, num_fds
, fds
);
896 * HACK: https://issuetracker.google.com/32077885
897 * There is no API available to properly query the IMPLEMENTATION_DEFINED
898 * format. As a workaround we rely here on gralloc allocating either
899 * an arbitrary YCbCr 4:2:0 or RGBX_8888, with the latter being recognized
900 * by lock_ycbcr failing.
902 if (image
|| buf
->format
!= HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
)
907 * Non-YUV formats could *also* have multiple planes, such as ancillary
908 * color compression state buffer, but the rest of the code isn't ready
909 * yet to deal with modifiers:
911 assert(num_fds
== 1);
913 const int fourcc
= get_fourcc(buf
->format
);
915 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
919 pitch
= buf
->stride
* get_format_bpp(buf
->format
);
921 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
925 const EGLint attr_list
[] = {
926 EGL_WIDTH
, buf
->width
,
927 EGL_HEIGHT
, buf
->height
,
928 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
929 EGL_DMA_BUF_PLANE0_FD_EXT
, fds
[0],
930 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitch
,
931 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, 0,
935 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list
);
938 #ifdef HAVE_DRM_GRALLOC
940 droid_create_image_from_name(_EGLDisplay
*disp
, _EGLContext
*ctx
,
941 struct ANativeWindowBuffer
*buf
)
943 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
944 struct dri2_egl_image
*dri2_img
;
948 name
= get_native_buffer_name(buf
);
950 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
954 format
= get_format(buf
->format
);
958 dri2_img
= calloc(1, sizeof(*dri2_img
));
960 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
964 _eglInitImage(&dri2_img
->base
, disp
);
966 dri2_img
->dri_image
=
967 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
974 if (!dri2_img
->dri_image
) {
976 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
980 return &dri2_img
->base
;
982 #endif /* HAVE_DRM_GRALLOC */
985 droid_query_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
986 EGLint attribute
, EGLint
*value
)
988 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
991 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
992 dri2_surf
->window
->query(dri2_surf
->window
,
993 NATIVE_WINDOW_DEFAULT_WIDTH
, value
);
998 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
999 dri2_surf
->window
->query(dri2_surf
->window
,
1000 NATIVE_WINDOW_DEFAULT_HEIGHT
, value
);
1007 return _eglQuerySurface(drv
, disp
, surf
, attribute
, value
);
1011 dri2_create_image_android_native_buffer(_EGLDisplay
*disp
,
1013 struct ANativeWindowBuffer
*buf
)
1019 /* From the EGL_ANDROID_image_native_buffer spec:
1021 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
1022 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
1024 _eglError(EGL_BAD_CONTEXT
, "eglCreateEGLImageKHR: for "
1025 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
1030 if (!buf
|| buf
->common
.magic
!= ANDROID_NATIVE_BUFFER_MAGIC
||
1031 buf
->common
.version
!= sizeof(*buf
)) {
1032 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
1036 num_fds
= get_native_buffer_fds(buf
, fds
);
1038 return droid_create_image_from_prime_fds(disp
, ctx
, buf
, num_fds
, fds
);
1040 #ifdef HAVE_DRM_GRALLOC
1041 return droid_create_image_from_name(disp
, ctx
, buf
);
1048 droid_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1049 _EGLContext
*ctx
, EGLenum target
,
1050 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1053 case EGL_NATIVE_BUFFER_ANDROID
:
1054 return dri2_create_image_android_native_buffer(disp
, ctx
,
1055 (struct ANativeWindowBuffer
*) buffer
);
1057 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
1062 droid_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
1066 #ifdef HAVE_DRM_GRALLOC
1068 droid_get_buffers_parse_attachments(struct dri2_egl_surface
*dri2_surf
,
1069 unsigned int *attachments
, int count
)
1071 int num_buffers
= 0;
1073 /* fill dri2_surf->buffers */
1074 for (int i
= 0; i
< count
* 2; i
+= 2) {
1075 __DRIbuffer
*buf
, *local
;
1077 assert(num_buffers
< ARRAY_SIZE(dri2_surf
->buffers
));
1078 buf
= &dri2_surf
->buffers
[num_buffers
];
1080 switch (attachments
[i
]) {
1081 case __DRI_BUFFER_BACK_LEFT
:
1082 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
1083 buf
->attachment
= attachments
[i
];
1084 buf
->name
= get_native_buffer_name(dri2_surf
->buffer
);
1085 buf
->cpp
= get_format_bpp(dri2_surf
->buffer
->format
);
1086 buf
->pitch
= dri2_surf
->buffer
->stride
* buf
->cpp
;
1094 /* fall through for pbuffers */
1095 case __DRI_BUFFER_DEPTH
:
1096 case __DRI_BUFFER_STENCIL
:
1097 case __DRI_BUFFER_ACCUM
:
1098 case __DRI_BUFFER_DEPTH_STENCIL
:
1099 case __DRI_BUFFER_HIZ
:
1100 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
,
1101 attachments
[i
], attachments
[i
+ 1]);
1108 case __DRI_BUFFER_FRONT_LEFT
:
1109 case __DRI_BUFFER_FRONT_RIGHT
:
1110 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
1111 case __DRI_BUFFER_FAKE_FRONT_RIGHT
:
1112 case __DRI_BUFFER_BACK_RIGHT
:
1114 /* no front or right buffers */
1122 static __DRIbuffer
*
1123 droid_get_buffers_with_format(__DRIdrawable
* driDrawable
,
1124 int *width
, int *height
,
1125 unsigned int *attachments
, int count
,
1126 int *out_count
, void *loaderPrivate
)
1128 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1130 if (update_buffers(dri2_surf
) < 0)
1133 *out_count
= droid_get_buffers_parse_attachments(dri2_surf
, attachments
, count
);
1136 *width
= dri2_surf
->base
.Width
;
1138 *height
= dri2_surf
->base
.Height
;
1140 return dri2_surf
->buffers
;
1142 #endif /* HAVE_DRM_GRALLOC */
1145 droid_get_capability(void *loaderPrivate
, enum dri_loader_cap cap
)
1147 /* Note: loaderPrivate is _EGLDisplay* */
1149 case DRI_LOADER_CAP_RGBA_ORDERING
:
1157 droid_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1159 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1160 static const struct {
1163 unsigned int rgba_sizes
[4];
1165 { HAL_PIXEL_FORMAT_RGBA_8888
, { 0, 8, 16, 24 }, { 8, 8, 8, 8 } },
1166 { HAL_PIXEL_FORMAT_RGBX_8888
, { 0, 8, 16, -1 }, { 8, 8, 8, 0 } },
1167 { HAL_PIXEL_FORMAT_RGB_565
, { 11, 5, 0, -1 }, { 5, 6, 5, 0 } },
1168 /* This must be after HAL_PIXEL_FORMAT_RGBA_8888, we only keep BGRA
1169 * visual if it turns out RGBA visual is not available.
1171 { HAL_PIXEL_FORMAT_BGRA_8888
, { 16, 8, 0, 24 }, { 8, 8, 8, 8 } },
1174 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1175 int config_count
= 0;
1177 /* The nesting of loops is significant here. Also significant is the order
1178 * of the HAL pixel formats. Many Android apps (such as Google's official
1179 * NDK GLES2 example app), and even portions the core framework code (such
1180 * as SystemServiceManager in Nougat), incorrectly choose their EGLConfig.
1181 * They neglect to match the EGLConfig's EGL_NATIVE_VISUAL_ID against the
1182 * window's native format, and instead choose the first EGLConfig whose
1183 * channel sizes match those of the native window format while ignoring the
1184 * channel *ordering*.
1186 * We can detect such buggy clients in logcat when they call
1187 * eglCreateSurface, by detecting the mismatch between the EGLConfig's
1188 * format and the window's format.
1190 * As a workaround, we generate EGLConfigs such that all EGLConfigs for HAL
1191 * pixel format i precede those for HAL pixel format i+1. In my
1192 * (chadversary) testing on Android Nougat, this was good enough to pacify
1193 * the buggy clients.
1195 bool has_rgba
= false;
1196 for (int i
= 0; i
< ARRAY_SIZE(visuals
); i
++) {
1197 /* Only enable BGRA configs when RGBA is not available. BGRA configs are
1198 * buggy on stock Android.
1200 if (visuals
[i
].format
== HAL_PIXEL_FORMAT_BGRA_8888
&& has_rgba
)
1202 for (int j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
1203 const EGLint surface_type
= EGL_WINDOW_BIT
| EGL_PBUFFER_BIT
;
1205 const EGLint config_attrs
[] = {
1206 EGL_NATIVE_VISUAL_ID
, visuals
[i
].format
,
1207 EGL_NATIVE_VISUAL_TYPE
, visuals
[i
].format
,
1208 EGL_FRAMEBUFFER_TARGET_ANDROID
, EGL_TRUE
,
1209 EGL_RECORDABLE_ANDROID
, EGL_TRUE
,
1213 struct dri2_egl_config
*dri2_conf
=
1214 dri2_add_config(disp
, dri2_dpy
->driver_configs
[j
],
1215 config_count
+ 1, surface_type
, config_attrs
,
1216 visuals
[i
].rgba_shifts
, visuals
[i
].rgba_sizes
);
1218 if (dri2_conf
->base
.ConfigID
== config_count
+ 1)
1223 if (visuals
[i
].format
== HAL_PIXEL_FORMAT_RGBA_8888
&& format_count
[i
])
1227 for (int i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1228 if (!format_count
[i
]) {
1229 _eglLog(_EGL_DEBUG
, "No DRI config supports native format 0x%x",
1234 return (config_count
!= 0);
1237 static const struct dri2_egl_display_vtbl droid_display_vtbl
= {
1238 .authenticate
= NULL
,
1239 .create_window_surface
= droid_create_window_surface
,
1240 .create_pixmap_surface
= dri2_fallback_create_pixmap_surface
,
1241 .create_pbuffer_surface
= droid_create_pbuffer_surface
,
1242 .destroy_surface
= droid_destroy_surface
,
1243 .create_image
= droid_create_image_khr
,
1244 .swap_buffers
= droid_swap_buffers
,
1245 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
, /* Android implements the function */
1246 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1247 .swap_interval
= droid_swap_interval
,
1248 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1249 .copy_buffers
= dri2_fallback_copy_buffers
,
1250 .query_buffer_age
= droid_query_buffer_age
,
1251 .query_surface
= droid_query_surface
,
1252 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1253 .get_sync_values
= dri2_fallback_get_sync_values
,
1254 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1255 .set_shared_buffer_mode
= droid_set_shared_buffer_mode
,
1258 #ifdef HAVE_DRM_GRALLOC
1259 static const __DRIdri2LoaderExtension droid_dri2_loader_extension
= {
1260 .base
= { __DRI_DRI2_LOADER
, 4 },
1263 .flushFrontBuffer
= droid_flush_front_buffer
,
1264 .getBuffersWithFormat
= droid_get_buffers_with_format
,
1265 .getCapability
= droid_get_capability
,
1268 static const __DRIextension
*droid_dri2_loader_extensions
[] = {
1269 &droid_dri2_loader_extension
.base
,
1270 &image_lookup_extension
.base
,
1271 &use_invalidate
.base
,
1272 /* No __DRI_MUTABLE_RENDER_BUFFER_LOADER because it requires
1273 * __DRI_IMAGE_LOADER.
1277 #endif /* HAVE_DRM_GRALLOC */
1279 static const __DRIimageLoaderExtension droid_image_loader_extension
= {
1280 .base
= { __DRI_IMAGE_LOADER
, 2 },
1282 .getBuffers
= droid_image_get_buffers
,
1283 .flushFrontBuffer
= droid_flush_front_buffer
,
1284 .getCapability
= droid_get_capability
,
1288 droid_display_shared_buffer(__DRIdrawable
*driDrawable
, int fence_fd
,
1289 void *loaderPrivate
)
1291 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1292 struct ANativeWindowBuffer
*old_buffer UNUSED
= dri2_surf
->buffer
;
1294 if (!_eglSurfaceInSharedBufferMode(&dri2_surf
->base
)) {
1295 _eglLog(_EGL_WARNING
, "%s: internal error: buffer is not shared",
1300 if (fence_fd
>= 0) {
1301 /* The driver's fence is more recent than the surface's out fence, if it
1302 * exists at all. So use the driver's fence.
1304 if (dri2_surf
->out_fence_fd
>= 0) {
1305 close(dri2_surf
->out_fence_fd
);
1306 dri2_surf
->out_fence_fd
= -1;
1308 } else if (dri2_surf
->out_fence_fd
>= 0) {
1309 fence_fd
= dri2_surf
->out_fence_fd
;
1310 dri2_surf
->out_fence_fd
= -1;
1313 if (dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
1315 _eglLog(_EGL_WARNING
, "%s: ANativeWindow::queueBuffer failed", __func__
);
1322 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
1324 /* Tear down the surface because it no longer has a back buffer. */
1325 struct dri2_egl_display
*dri2_dpy
=
1326 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1328 _eglLog(_EGL_WARNING
, "%s: ANativeWindow::dequeueBuffer failed", __func__
);
1330 dri2_surf
->base
.Lost
= true;
1331 dri2_surf
->buffer
= NULL
;
1332 dri2_surf
->back
= NULL
;
1334 if (dri2_surf
->dri_image_back
) {
1335 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
1336 dri2_surf
->dri_image_back
= NULL
;
1339 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
1346 /* Access to the buffer is controlled by a sync fence. Block on it.
1348 * Ideally, we would submit the fence to the driver, and the driver would
1349 * postpone command execution until it signalled. But DRI lacks API for
1350 * that (as of 2018-04-11).
1352 * SYNC_IOC_WAIT waits forever if timeout < 0
1354 sync_wait(fence_fd
, -1);
1358 static const __DRImutableRenderBufferLoaderExtension droid_mutable_render_buffer_extension
= {
1359 .base
= { __DRI_MUTABLE_RENDER_BUFFER_LOADER
, 1 },
1360 .displaySharedBuffer
= droid_display_shared_buffer
,
1363 static const __DRIextension
*droid_image_loader_extensions
[] = {
1364 &droid_image_loader_extension
.base
,
1365 &image_lookup_extension
.base
,
1366 &use_invalidate
.base
,
1367 &droid_mutable_render_buffer_extension
.base
,
1372 droid_load_driver(_EGLDisplay
*disp
, bool swrast
)
1374 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
1377 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1378 if (dri2_dpy
->driver_name
== NULL
)
1381 #ifdef HAVE_DRM_GRALLOC
1382 /* Handle control nodes using __DRI_DRI2_LOADER extension and GEM names
1383 * for backwards compatibility with drm_gralloc. (Do not use on new
1385 dri2_dpy
->loader_extensions
= droid_dri2_loader_extensions
;
1386 if (!dri2_load_driver(disp
)) {
1387 err
= "DRI2: failed to load driver";
1392 /* Use kms swrast only with vgem / virtio_gpu.
1393 * virtio-gpu fallbacks to software rendering when 3D features
1394 * are unavailable since 6c5ab.
1396 if (strcmp(dri2_dpy
->driver_name
, "vgem") == 0 ||
1397 strcmp(dri2_dpy
->driver_name
, "virtio_gpu") == 0) {
1398 free(dri2_dpy
->driver_name
);
1399 dri2_dpy
->driver_name
= strdup("kms_swrast");
1401 err
= "DRI3: failed to find software capable driver";
1406 dri2_dpy
->loader_extensions
= droid_image_loader_extensions
;
1407 if (!dri2_load_driver_dri3(disp
)) {
1408 err
= "DRI3: failed to load driver";
1416 free(dri2_dpy
->driver_name
);
1417 dri2_dpy
->driver_name
= NULL
;
1422 droid_unload_driver(_EGLDisplay
*disp
)
1424 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1426 dlclose(dri2_dpy
->driver
);
1427 dri2_dpy
->driver
= NULL
;
1428 free(dri2_dpy
->driver_name
);
1429 dri2_dpy
->driver_name
= NULL
;
1433 droid_filter_device(_EGLDisplay
*disp
, int fd
, const char *vendor
)
1435 drmVersionPtr ver
= drmGetVersion(fd
);
1439 if (strcmp(vendor
, ver
->name
) != 0) {
1440 drmFreeVersion(ver
);
1444 drmFreeVersion(ver
);
1449 droid_probe_device(_EGLDisplay
*disp
, bool swrast
)
1451 /* Check that the device is supported, by attempting to:
1452 * - load the dri module
1453 * - and, create a screen
1455 if (!droid_load_driver(disp
, swrast
))
1458 if (!dri2_create_screen(disp
)) {
1459 _eglLog(_EGL_WARNING
, "DRI2: failed to create screen");
1460 droid_unload_driver(disp
);
1466 #ifdef HAVE_DRM_GRALLOC
1468 droid_open_device(_EGLDisplay
*disp
, bool swrast
)
1470 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1471 int fd
= -1, err
= -EINVAL
;
1476 if (dri2_dpy
->gralloc
->perform
)
1477 err
= dri2_dpy
->gralloc
->perform(dri2_dpy
->gralloc
,
1478 GRALLOC_MODULE_PERFORM_GET_DRM_FD
,
1480 if (err
|| fd
< 0) {
1481 _eglLog(_EGL_WARNING
, "fail to get drm fd");
1485 dri2_dpy
->fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
1486 if (dri2_dpy
->fd
< 0)
1489 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
)
1492 return droid_probe_device(disp
, swrast
);
1496 droid_open_device(_EGLDisplay
*disp
, bool swrast
)
1498 #define MAX_DRM_DEVICES 64
1499 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1500 drmDevicePtr device
, devices
[MAX_DRM_DEVICES
] = { NULL
};
1503 char *vendor_name
= NULL
;
1504 char vendor_buf
[PROPERTY_VALUE_MAX
];
1506 #ifdef EGL_FORCE_RENDERNODE
1507 const unsigned node_type
= DRM_NODE_RENDER
;
1509 const unsigned node_type
= swrast
? DRM_NODE_PRIMARY
: DRM_NODE_RENDER
;
1512 if (property_get("drm.gpu.vendor_name", vendor_buf
, NULL
) > 0)
1513 vendor_name
= vendor_buf
;
1515 num_devices
= drmGetDevices2(0, devices
, ARRAY_SIZE(devices
));
1516 if (num_devices
< 0)
1519 for (int i
= 0; i
< num_devices
; i
++) {
1520 device
= devices
[i
];
1522 if (!(device
->available_nodes
& (1 << node_type
)))
1525 dri2_dpy
->fd
= loader_open_device(device
->nodes
[node_type
]);
1526 if (dri2_dpy
->fd
< 0) {
1527 _eglLog(_EGL_WARNING
, "%s() Failed to open DRM device %s",
1528 __func__
, device
->nodes
[node_type
]);
1532 /* If a vendor is explicitly provided, we use only that.
1533 * Otherwise we fall-back the first device that is supported.
1536 if (droid_filter_device(disp
, dri2_dpy
->fd
, vendor_name
)) {
1537 /* Device does not match - try next device */
1538 close(dri2_dpy
->fd
);
1542 /* If the requested device matches - use it. Regardless if
1543 * init fails, do not fall-back to any other device.
1545 if (!droid_probe_device(disp
, false)) {
1546 close(dri2_dpy
->fd
);
1552 if (droid_probe_device(disp
, swrast
))
1555 /* No explicit request - attempt the next device */
1556 close(dri2_dpy
->fd
);
1559 drmFreeDevices(devices
, num_devices
);
1561 if (dri2_dpy
->fd
< 0) {
1562 _eglLog(_EGL_WARNING
, "Failed to open %s DRM device",
1563 vendor_name
? "desired": "any");
1568 #undef MAX_DRM_DEVICES
1574 dri2_initialize_android(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1577 bool device_opened
= false;
1578 struct dri2_egl_display
*dri2_dpy
;
1582 dri2_dpy
= calloc(1, sizeof(*dri2_dpy
));
1584 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1587 ret
= hw_get_module(GRALLOC_HARDWARE_MODULE_ID
,
1588 (const hw_module_t
**)&dri2_dpy
->gralloc
);
1590 err
= "DRI2: failed to get gralloc module";
1594 disp
->DriverData
= (void *) dri2_dpy
;
1595 if (!disp
->Options
.ForceSoftware
)
1596 device_opened
= droid_open_device(disp
, false);
1598 device_opened
= droid_open_device(disp
, true);
1600 if (!device_opened
) {
1601 err
= "DRI2: failed to open device";
1605 dev
= _eglAddDevice(dri2_dpy
->fd
, false);
1607 err
= "DRI2: failed to find EGLDevice";
1613 if (!dri2_setup_extensions(disp
)) {
1614 err
= "DRI2: failed to setup extensions";
1618 dri2_setup_screen(disp
);
1620 /* We set the maximum swap interval as 1 for Android platform, since it is
1621 * the maximum value supported by Android according to the value of
1622 * ANativeWindow::maxSwapInterval.
1624 dri2_setup_swap_interval(disp
, 1);
1626 disp
->Extensions
.ANDROID_framebuffer_target
= EGL_TRUE
;
1627 disp
->Extensions
.ANDROID_image_native_buffer
= EGL_TRUE
;
1628 disp
->Extensions
.ANDROID_recordable
= EGL_TRUE
;
1629 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1630 disp
->Extensions
.KHR_image
= EGL_TRUE
;
1631 #if ANDROID_API_LEVEL >= 24
1632 if (dri2_dpy
->mutable_render_buffer
&&
1633 dri2_dpy
->loader_extensions
== droid_image_loader_extensions
) {
1634 disp
->Extensions
.KHR_mutable_render_buffer
= EGL_TRUE
;
1638 /* Create configs *after* enabling extensions because presence of DRI
1639 * driver extensions can affect the capabilities of EGLConfigs.
1641 if (!droid_add_configs_for_visuals(drv
, disp
)) {
1642 err
= "DRI2: failed to add configs";
1646 /* Fill vtbl last to prevent accidentally calling virtual function during
1649 dri2_dpy
->vtbl
= &droid_display_vtbl
;
1654 dri2_display_destroy(disp
);
1655 return _eglError(EGL_NOT_INITIALIZED
, err
);