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_create_image_from_prime_fds_yuv(_EGLDisplay
*disp
,
190 struct ANativeWindowBuffer
*buf
,
191 int num_fds
, int fds
[3])
193 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
194 struct android_ycbcr ycbcr
;
197 enum chroma_order chroma_order
;
202 if (!dri2_dpy
->gralloc
->lock_ycbcr
) {
203 _eglLog(_EGL_WARNING
, "Gralloc does not support lock_ycbcr");
207 memset(&ycbcr
, 0, sizeof(ycbcr
));
208 ret
= dri2_dpy
->gralloc
->lock_ycbcr(dri2_dpy
->gralloc
, buf
->handle
,
209 0, 0, 0, 0, 0, &ycbcr
);
211 /* HACK: See droid_create_image_from_prime_fds() and
212 * https://issuetracker.google.com/32077885.*/
213 if (buf
->format
== HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
)
216 _eglLog(_EGL_WARNING
, "gralloc->lock_ycbcr failed: %d", ret
);
219 dri2_dpy
->gralloc
->unlock(dri2_dpy
->gralloc
, buf
->handle
);
221 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
222 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
223 * so they can be interpreted as offsets. */
224 offsets
[0] = (size_t)ycbcr
.y
;
225 /* We assume here that all the planes are located in one DMA-buf. */
226 if ((size_t)ycbcr
.cr
< (size_t)ycbcr
.cb
) {
227 chroma_order
= YCrCb
;
228 offsets
[1] = (size_t)ycbcr
.cr
;
229 offsets
[2] = (size_t)ycbcr
.cb
;
231 chroma_order
= YCbCr
;
232 offsets
[1] = (size_t)ycbcr
.cb
;
233 offsets
[2] = (size_t)ycbcr
.cr
;
236 /* .ystride is the line length (in bytes) of the Y plane,
237 * .cstride is the line length (in bytes) of any of the remaining
238 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
240 pitches
[0] = ycbcr
.ystride
;
241 pitches
[1] = pitches
[2] = ycbcr
.cstride
;
243 /* .chroma_step is the byte distance between the same chroma channel
244 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
245 fourcc
= get_fourcc_yuv(buf
->format
, chroma_order
, ycbcr
.chroma_step
);
247 _eglLog(_EGL_WARNING
, "unsupported YUV format, native = %x, chroma_order = %s, chroma_step = %d",
248 buf
->format
, chroma_order
== YCbCr
? "YCbCr" : "YCrCb", ycbcr
.chroma_step
);
253 * Since this is EGL_NATIVE_BUFFER_ANDROID don't assume that
254 * the single-fd case cannot happen. So handle eithe single
255 * fd or fd-per-plane case:
258 fds
[2] = fds
[1] = fds
[0];
260 int expected_planes
= (ycbcr
.chroma_step
== 2) ? 2 : 3;
261 assert(num_fds
== expected_planes
);
264 return dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
265 buf
->width
, buf
->height
, fourcc
,
266 fds
, num_fds
, pitches
, offsets
,
268 EGL_YUV_NARROW_RANGE_EXT
,
269 EGL_YUV_CHROMA_SITING_0_EXT
,
270 EGL_YUV_CHROMA_SITING_0_EXT
,
276 droid_create_image_from_prime_fds(_EGLDisplay
*disp
,
277 struct ANativeWindowBuffer
*buf
)
279 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
280 int pitches
[4] = { 0 }, offsets
[4] = { 0 };
285 num_fds
= get_native_buffer_fds(buf
, fds
);
289 if (is_yuv(buf
->format
)) {
292 image
= droid_create_image_from_prime_fds_yuv(disp
, buf
, num_fds
, fds
);
294 * HACK: https://issuetracker.google.com/32077885
295 * There is no API available to properly query the IMPLEMENTATION_DEFINED
296 * format. As a workaround we rely here on gralloc allocating either
297 * an arbitrary YCbCr 4:2:0 or RGBX_8888, with the latter being recognized
298 * by lock_ycbcr failing.
300 if (image
|| buf
->format
!= HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
)
305 * Non-YUV formats could *also* have multiple planes, such as ancillary
306 * color compression state buffer, but the rest of the code isn't ready
307 * yet to deal with modifiers:
309 assert(num_fds
== 1);
311 const int fourcc
= get_fourcc(buf
->format
);
313 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
317 pitches
[0] = buf
->stride
* get_format_bpp(buf
->format
);
318 if (pitches
[0] == 0) {
319 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
323 return dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
324 buf
->width
, buf
->height
, fourcc
,
325 fds
, num_fds
, pitches
, offsets
,
327 EGL_YUV_NARROW_RANGE_EXT
,
328 EGL_YUV_CHROMA_SITING_0_EXT
,
329 EGL_YUV_CHROMA_SITING_0_EXT
,
335 droid_window_dequeue_buffer(struct dri2_egl_surface
*dri2_surf
)
339 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
343 /* If access to the buffer is controlled by a sync fence, then block on the
346 * It may be more performant to postpone blocking until there is an
347 * immediate need to write to the buffer. But doing so would require adding
348 * hooks to the DRI2 loader.
350 * From the ANativeWindow::dequeueBuffer documentation:
352 * The libsync fence file descriptor returned in the int pointed to by
353 * the fenceFd argument will refer to the fence that must signal
354 * before the dequeued buffer may be written to. A value of -1
355 * indicates that the caller may access the buffer immediately without
356 * waiting on a fence. If a valid file descriptor is returned (i.e.
357 * any value except -1) then the caller is responsible for closing the
361 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
363 * Waits indefinitely if timeout < 0.
366 sync_wait(fence_fd
, timeout
);
370 /* Record all the buffers created by ANativeWindow and update back buffer
371 * for updating buffer's age in swap_buffers.
373 EGLBoolean updated
= EGL_FALSE
;
374 for (int i
= 0; i
< dri2_surf
->color_buffers_count
; i
++) {
375 if (!dri2_surf
->color_buffers
[i
].buffer
) {
376 dri2_surf
->color_buffers
[i
].buffer
= dri2_surf
->buffer
;
378 if (dri2_surf
->color_buffers
[i
].buffer
== dri2_surf
->buffer
) {
379 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
386 /* In case of all the buffers were recreated by ANativeWindow, reset
389 for (int i
= 0; i
< dri2_surf
->color_buffers_count
; i
++) {
390 dri2_surf
->color_buffers
[i
].buffer
= NULL
;
391 dri2_surf
->color_buffers
[i
].age
= 0;
393 dri2_surf
->color_buffers
[0].buffer
= dri2_surf
->buffer
;
394 dri2_surf
->back
= &dri2_surf
->color_buffers
[0];
401 droid_window_enqueue_buffer(_EGLDisplay
*disp
, struct dri2_egl_surface
*dri2_surf
)
403 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
405 /* To avoid blocking other EGL calls, release the display mutex before
406 * we enter droid_window_enqueue_buffer() and re-acquire the mutex upon
409 mtx_unlock(&disp
->Mutex
);
411 /* Queue the buffer with stored out fence fd. The ANativeWindow or buffer
412 * consumer may choose to wait for the fence to signal before accessing
413 * it. If fence fd value is -1, buffer can be accessed by consumer
414 * immediately. Consumer or application shouldn't rely on timestamp
415 * associated with fence if the fence fd is -1.
417 * Ownership of fd is transferred to consumer after queueBuffer and the
418 * consumer is responsible for closing it. Caller must not use the fd
419 * after passing it to queueBuffer.
421 int fence_fd
= dri2_surf
->out_fence_fd
;
422 dri2_surf
->out_fence_fd
= -1;
423 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
426 dri2_surf
->buffer
= NULL
;
427 dri2_surf
->back
= NULL
;
429 mtx_lock(&disp
->Mutex
);
431 if (dri2_surf
->dri_image_back
) {
432 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
433 dri2_surf
->dri_image_back
= NULL
;
440 droid_window_cancel_buffer(struct dri2_egl_surface
*dri2_surf
)
443 int fence_fd
= dri2_surf
->out_fence_fd
;
445 dri2_surf
->out_fence_fd
= -1;
446 ret
= dri2_surf
->window
->cancelBuffer(dri2_surf
->window
,
447 dri2_surf
->buffer
, fence_fd
);
448 dri2_surf
->buffer
= NULL
;
450 _eglLog(_EGL_WARNING
, "ANativeWindow::cancelBuffer failed");
451 dri2_surf
->base
.Lost
= EGL_TRUE
;
456 droid_set_shared_buffer_mode(_EGLDisplay
*disp
, _EGLSurface
*surf
, bool mode
)
458 #if ANDROID_API_LEVEL >= 24
459 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
460 struct ANativeWindow
*window
= dri2_surf
->window
;
462 assert(surf
->Type
== EGL_WINDOW_BIT
);
463 assert(_eglSurfaceHasMutableRenderBuffer(&dri2_surf
->base
));
465 _eglLog(_EGL_DEBUG
, "%s: mode=%d", __func__
, mode
);
467 if (native_window_set_shared_buffer_mode(window
, mode
)) {
468 _eglLog(_EGL_WARNING
, "failed native_window_set_shared_buffer_mode"
469 "(window=%p, mode=%d)", window
, mode
);
475 _eglLog(_EGL_FATAL
, "%s:%d: internal error: unreachable", __FILE__
, __LINE__
);
481 droid_create_surface(_EGLDisplay
*disp
, EGLint type
, _EGLConfig
*conf
,
482 void *native_window
, const EGLint
*attrib_list
)
484 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
485 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
486 struct dri2_egl_surface
*dri2_surf
;
487 struct ANativeWindow
*window
= native_window
;
488 const __DRIconfig
*config
;
490 dri2_surf
= calloc(1, sizeof *dri2_surf
);
492 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
496 if (!dri2_init_surface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
,
497 true, native_window
))
498 goto cleanup_surface
;
500 if (type
== EGL_WINDOW_BIT
) {
503 int min_buffer_count
, max_buffer_count
;
505 /* Prefer triple buffering for performance reasons. */
506 const int preferred_buffer_count
= 3;
508 if (window
->common
.magic
!= ANDROID_NATIVE_WINDOW_MAGIC
) {
509 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
510 goto cleanup_surface
;
512 if (window
->query(window
, NATIVE_WINDOW_FORMAT
, &format
)) {
513 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
514 goto cleanup_surface
;
517 /* Query ANativeWindow for MIN_UNDEQUEUED_BUFFER, minimum amount
518 * of undequeued buffers.
520 if (window
->query(window
, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS
,
521 &min_buffer_count
)) {
522 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
523 goto cleanup_surface
;
526 /* Query for maximum buffer count, application can set this
527 * to limit the total amount of buffers.
529 if (window
->query(window
, NATIVE_WINDOW_MAX_BUFFER_COUNT
,
530 &max_buffer_count
)) {
531 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
532 goto cleanup_surface
;
535 /* Clamp preferred between minimum (min undequeued + 1 dequeued)
538 buffer_count
= CLAMP(preferred_buffer_count
, min_buffer_count
+ 1,
541 if (native_window_set_buffer_count(window
, buffer_count
)) {
542 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
543 goto cleanup_surface
;
545 dri2_surf
->color_buffers
= calloc(buffer_count
,
546 sizeof(*dri2_surf
->color_buffers
));
547 if (!dri2_surf
->color_buffers
) {
548 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
549 goto cleanup_surface
;
551 dri2_surf
->color_buffers_count
= buffer_count
;
553 if (format
!= dri2_conf
->base
.NativeVisualID
) {
554 _eglLog(_EGL_WARNING
, "Native format mismatch: 0x%x != 0x%x",
555 format
, dri2_conf
->base
.NativeVisualID
);
558 window
->query(window
, NATIVE_WINDOW_WIDTH
, &dri2_surf
->base
.Width
);
559 window
->query(window
, NATIVE_WINDOW_HEIGHT
, &dri2_surf
->base
.Height
);
561 uint32_t usage
= strcmp(dri2_dpy
->driver_name
, "kms_swrast") == 0
562 ? GRALLOC_USAGE_SW_READ_OFTEN
| GRALLOC_USAGE_SW_WRITE_OFTEN
563 : GRALLOC_USAGE_HW_RENDER
;
564 native_window_set_usage(window
, usage
);
567 config
= dri2_get_dri_config(dri2_conf
, type
,
568 dri2_surf
->base
.GLColorspace
);
570 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
571 goto cleanup_surface
;
574 if (!dri2_create_drawable(dri2_dpy
, config
, dri2_surf
, dri2_surf
))
575 goto cleanup_surface
;
578 window
->common
.incRef(&window
->common
);
579 dri2_surf
->window
= window
;
582 return &dri2_surf
->base
;
585 if (dri2_surf
->color_buffers_count
)
586 free(dri2_surf
->color_buffers
);
593 droid_create_window_surface(_EGLDisplay
*disp
, _EGLConfig
*conf
,
594 void *native_window
, const EGLint
*attrib_list
)
596 return droid_create_surface(disp
, EGL_WINDOW_BIT
, conf
,
597 native_window
, attrib_list
);
601 droid_create_pbuffer_surface(_EGLDisplay
*disp
, _EGLConfig
*conf
,
602 const EGLint
*attrib_list
)
604 return droid_create_surface(disp
, EGL_PBUFFER_BIT
, conf
,
609 droid_destroy_surface(_EGLDisplay
*disp
, _EGLSurface
*surf
)
611 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
612 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
614 dri2_egl_surface_free_local_buffers(dri2_surf
);
616 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
617 if (dri2_surf
->buffer
)
618 droid_window_cancel_buffer(dri2_surf
);
620 dri2_surf
->window
->common
.decRef(&dri2_surf
->window
->common
);
623 if (dri2_surf
->dri_image_back
) {
624 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_back", __func__
, __LINE__
);
625 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
626 dri2_surf
->dri_image_back
= NULL
;
629 if (dri2_surf
->dri_image_front
) {
630 _eglLog(_EGL_DEBUG
, "%s : %d : destroy dri_image_front", __func__
, __LINE__
);
631 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_front
);
632 dri2_surf
->dri_image_front
= NULL
;
635 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
637 dri2_fini_surface(surf
);
638 free(dri2_surf
->color_buffers
);
645 droid_swap_interval(_EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint interval
)
647 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
648 struct ANativeWindow
*window
= dri2_surf
->window
;
650 if (window
->setSwapInterval(window
, interval
))
653 surf
->SwapInterval
= interval
;
658 update_buffers(struct dri2_egl_surface
*dri2_surf
)
660 if (dri2_surf
->base
.Lost
)
663 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
666 /* try to dequeue the next back buffer */
667 if (!dri2_surf
->buffer
&& !droid_window_dequeue_buffer(dri2_surf
)) {
668 _eglLog(_EGL_WARNING
, "Could not dequeue buffer from native window");
669 dri2_surf
->base
.Lost
= EGL_TRUE
;
673 /* free outdated buffers and update the surface size */
674 if (dri2_surf
->base
.Width
!= dri2_surf
->buffer
->width
||
675 dri2_surf
->base
.Height
!= dri2_surf
->buffer
->height
) {
676 dri2_egl_surface_free_local_buffers(dri2_surf
);
677 dri2_surf
->base
.Width
= dri2_surf
->buffer
->width
;
678 dri2_surf
->base
.Height
= dri2_surf
->buffer
->height
;
685 get_front_bo(struct dri2_egl_surface
*dri2_surf
, unsigned int format
)
687 struct dri2_egl_display
*dri2_dpy
=
688 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
690 if (dri2_surf
->dri_image_front
)
693 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
694 /* According current EGL spec, front buffer rendering
695 * for window surface is not supported now.
696 * and mesa doesn't have the implementation of this case.
697 * Add warning message, but not treat it as error.
699 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported front buffer for window surface");
700 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
701 dri2_surf
->dri_image_front
=
702 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
703 dri2_surf
->base
.Width
,
704 dri2_surf
->base
.Height
,
708 if (!dri2_surf
->dri_image_front
) {
709 _eglLog(_EGL_WARNING
, "dri2_image_front allocation failed");
718 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
720 _EGLDisplay
*disp
= dri2_surf
->base
.Resource
.Display
;
722 if (dri2_surf
->dri_image_back
)
725 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
726 if (!dri2_surf
->buffer
) {
727 _eglLog(_EGL_WARNING
, "Could not get native buffer");
731 dri2_surf
->dri_image_back
=
732 droid_create_image_from_prime_fds(disp
, dri2_surf
->buffer
);
733 if (!dri2_surf
->dri_image_back
) {
734 _eglLog(_EGL_WARNING
, "failed to create DRI image from FD");
737 } else if (dri2_surf
->base
.Type
== EGL_PBUFFER_BIT
) {
738 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
739 * the spec states that they have a back buffer but no front buffer, in
740 * contrast to pixmaps, which have a front buffer but no back buffer.
742 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
743 * from the spec, following the precedent of Mesa's EGL X11 platform. The
744 * X11 platform correctly assigns pbuffers to single-buffered configs, but
745 * assigns the pbuffer a front buffer instead of a back buffer.
747 * Pbuffers in the X11 platform mostly work today, so let's just copy its
748 * behavior instead of trying to fix (and hence potentially breaking) the
751 _eglLog(_EGL_DEBUG
, "DRI driver requested unsupported back buffer for pbuffer surface");
757 /* Some drivers will pass multiple bits in buffer_mask.
758 * For such case, will go through all the bits, and
759 * will not return error when unsupported buffer is requested, only
760 * return error when the allocation for supported buffer failed.
763 droid_image_get_buffers(__DRIdrawable
*driDrawable
,
767 uint32_t buffer_mask
,
768 struct __DRIimageList
*images
)
770 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
772 images
->image_mask
= 0;
773 images
->front
= NULL
;
776 if (update_buffers(dri2_surf
) < 0)
779 if (_eglSurfaceInSharedBufferMode(&dri2_surf
->base
)) {
780 if (get_back_bo(dri2_surf
) < 0)
783 /* We have dri_image_back because this is a window surface and
784 * get_back_bo() succeeded.
786 assert(dri2_surf
->dri_image_back
);
787 images
->back
= dri2_surf
->dri_image_back
;
788 images
->image_mask
|= __DRI_IMAGE_BUFFER_SHARED
;
790 /* There exists no accompanying back nor front buffer. */
794 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
795 if (get_front_bo(dri2_surf
, format
) < 0)
798 if (dri2_surf
->dri_image_front
) {
799 images
->front
= dri2_surf
->dri_image_front
;
800 images
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
804 if (buffer_mask
& __DRI_IMAGE_BUFFER_BACK
) {
805 if (get_back_bo(dri2_surf
) < 0)
808 if (dri2_surf
->dri_image_back
) {
809 images
->back
= dri2_surf
->dri_image_back
;
810 images
->image_mask
|= __DRI_IMAGE_BUFFER_BACK
;
818 droid_query_buffer_age(_EGLDisplay
*disp
, _EGLSurface
*surface
)
820 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
822 if (update_buffers(dri2_surf
) < 0) {
823 _eglError(EGL_BAD_ALLOC
, "droid_query_buffer_age");
827 return dri2_surf
->back
? dri2_surf
->back
->age
: 0;
831 droid_swap_buffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
833 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
834 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
835 const bool has_mutable_rb
= _eglSurfaceHasMutableRenderBuffer(draw
);
837 /* From the EGL_KHR_mutable_render_buffer spec (v12):
839 * If surface is a single-buffered window, pixmap, or pbuffer surface
840 * for which there is no pending change to the EGL_RENDER_BUFFER
841 * attribute, eglSwapBuffers has no effect.
843 if (has_mutable_rb
&&
844 draw
->RequestedRenderBuffer
== EGL_SINGLE_BUFFER
&&
845 draw
->ActiveRenderBuffer
== EGL_SINGLE_BUFFER
) {
846 _eglLog(_EGL_DEBUG
, "%s: remain in shared buffer mode", __func__
);
850 for (int i
= 0; i
< dri2_surf
->color_buffers_count
; i
++) {
851 if (dri2_surf
->color_buffers
[i
].age
> 0)
852 dri2_surf
->color_buffers
[i
].age
++;
855 /* "XXX: we don't use get_back_bo() since it causes regressions in
856 * several dEQP tests.
859 dri2_surf
->back
->age
= 1;
861 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
863 /* dri2_surf->buffer can be null even when no error has occured. For
864 * example, if the user has called no GL rendering commands since the
865 * previous eglSwapBuffers, then the driver may have not triggered
866 * a callback to ANativeWindow::dequeueBuffer, in which case
867 * dri2_surf->buffer remains null.
869 if (dri2_surf
->buffer
)
870 droid_window_enqueue_buffer(disp
, dri2_surf
);
872 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
874 /* Update the shared buffer mode */
875 if (has_mutable_rb
&&
876 draw
->ActiveRenderBuffer
!= draw
->RequestedRenderBuffer
) {
877 bool mode
= (draw
->RequestedRenderBuffer
== EGL_SINGLE_BUFFER
);
878 _eglLog(_EGL_DEBUG
, "%s: change to shared buffer mode %d",
881 if (!droid_set_shared_buffer_mode(disp
, draw
, mode
))
883 draw
->ActiveRenderBuffer
= draw
->RequestedRenderBuffer
;
889 #ifdef HAVE_DRM_GRALLOC
890 static int get_format(int format
)
893 case HAL_PIXEL_FORMAT_BGRA_8888
: return __DRI_IMAGE_FORMAT_ARGB8888
;
894 case HAL_PIXEL_FORMAT_RGB_565
: return __DRI_IMAGE_FORMAT_RGB565
;
895 case HAL_PIXEL_FORMAT_RGBA_8888
: return __DRI_IMAGE_FORMAT_ABGR8888
;
896 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED
:
898 * HACK: Hardcode this to RGBX_8888 as per cros_gralloc hack.
899 * TODO: Revert this once https://issuetracker.google.com/32077885 is fixed.
901 case HAL_PIXEL_FORMAT_RGBX_8888
: return __DRI_IMAGE_FORMAT_XBGR8888
;
902 case HAL_PIXEL_FORMAT_RGBA_FP16
: return __DRI_IMAGE_FORMAT_ABGR16161616F
;
903 case HAL_PIXEL_FORMAT_RGBA_1010102
: return __DRI_IMAGE_FORMAT_ABGR2101010
;
905 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", format
);
911 droid_create_image_from_name(_EGLDisplay
*disp
,
912 struct ANativeWindowBuffer
*buf
)
914 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
915 __DRIimage
*dri_image
;
919 name
= get_native_buffer_name(buf
);
921 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
925 format
= get_format(buf
->format
);
930 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
938 #endif /* HAVE_DRM_GRALLOC */
941 droid_query_surface(_EGLDisplay
*disp
, _EGLSurface
*surf
,
942 EGLint attribute
, EGLint
*value
)
944 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
947 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
948 dri2_surf
->window
->query(dri2_surf
->window
,
949 NATIVE_WINDOW_DEFAULT_WIDTH
, value
);
954 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
&& dri2_surf
->window
) {
955 dri2_surf
->window
->query(dri2_surf
->window
,
956 NATIVE_WINDOW_DEFAULT_HEIGHT
, value
);
963 return _eglQuerySurface(disp
, surf
, attribute
, value
);
967 dri2_create_image_android_native_buffer(_EGLDisplay
*disp
,
969 struct ANativeWindowBuffer
*buf
)
972 /* From the EGL_ANDROID_image_native_buffer spec:
974 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
975 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
977 _eglError(EGL_BAD_CONTEXT
, "eglCreateEGLImageKHR: for "
978 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
983 if (!buf
|| buf
->common
.magic
!= ANDROID_NATIVE_BUFFER_MAGIC
||
984 buf
->common
.version
!= sizeof(*buf
)) {
985 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
989 __DRIimage
*dri_image
=
990 droid_create_image_from_prime_fds(disp
, buf
);
992 #ifdef HAVE_DRM_GRALLOC
993 if (dri_image
== NULL
)
994 dri_image
= droid_create_image_from_name(disp
, buf
);
998 return dri2_create_image_from_dri(disp
, dri_image
);
1004 droid_create_image_khr(_EGLDisplay
*disp
, _EGLContext
*ctx
, EGLenum target
,
1005 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1008 case EGL_NATIVE_BUFFER_ANDROID
:
1009 return dri2_create_image_android_native_buffer(disp
, ctx
,
1010 (struct ANativeWindowBuffer
*) buffer
);
1012 return dri2_create_image_khr(disp
, ctx
, target
, buffer
, attr_list
);
1017 droid_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
1021 #ifdef HAVE_DRM_GRALLOC
1023 droid_get_buffers_parse_attachments(struct dri2_egl_surface
*dri2_surf
,
1024 unsigned int *attachments
, int count
)
1026 int num_buffers
= 0;
1028 /* fill dri2_surf->buffers */
1029 for (int i
= 0; i
< count
* 2; i
+= 2) {
1030 __DRIbuffer
*buf
, *local
;
1032 assert(num_buffers
< ARRAY_SIZE(dri2_surf
->buffers
));
1033 buf
= &dri2_surf
->buffers
[num_buffers
];
1035 switch (attachments
[i
]) {
1036 case __DRI_BUFFER_BACK_LEFT
:
1037 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
1038 buf
->attachment
= attachments
[i
];
1039 buf
->name
= get_native_buffer_name(dri2_surf
->buffer
);
1040 buf
->cpp
= get_format_bpp(dri2_surf
->buffer
->format
);
1041 buf
->pitch
= dri2_surf
->buffer
->stride
* buf
->cpp
;
1049 /* fall through for pbuffers */
1050 case __DRI_BUFFER_DEPTH
:
1051 case __DRI_BUFFER_STENCIL
:
1052 case __DRI_BUFFER_ACCUM
:
1053 case __DRI_BUFFER_DEPTH_STENCIL
:
1054 case __DRI_BUFFER_HIZ
:
1055 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
,
1056 attachments
[i
], attachments
[i
+ 1]);
1063 case __DRI_BUFFER_FRONT_LEFT
:
1064 case __DRI_BUFFER_FRONT_RIGHT
:
1065 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
1066 case __DRI_BUFFER_FAKE_FRONT_RIGHT
:
1067 case __DRI_BUFFER_BACK_RIGHT
:
1069 /* no front or right buffers */
1077 static __DRIbuffer
*
1078 droid_get_buffers_with_format(__DRIdrawable
* driDrawable
,
1079 int *width
, int *height
,
1080 unsigned int *attachments
, int count
,
1081 int *out_count
, void *loaderPrivate
)
1083 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1085 if (update_buffers(dri2_surf
) < 0)
1088 *out_count
= droid_get_buffers_parse_attachments(dri2_surf
, attachments
, count
);
1091 *width
= dri2_surf
->base
.Width
;
1093 *height
= dri2_surf
->base
.Height
;
1095 return dri2_surf
->buffers
;
1097 #endif /* HAVE_DRM_GRALLOC */
1100 droid_get_capability(void *loaderPrivate
, enum dri_loader_cap cap
)
1102 /* Note: loaderPrivate is _EGLDisplay* */
1104 case DRI_LOADER_CAP_RGBA_ORDERING
:
1112 droid_add_configs_for_visuals(_EGLDisplay
*disp
)
1114 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1115 static const struct {
1118 unsigned int rgba_sizes
[4];
1120 { HAL_PIXEL_FORMAT_RGBA_8888
, { 0, 8, 16, 24 }, { 8, 8, 8, 8 } },
1121 { HAL_PIXEL_FORMAT_RGBX_8888
, { 0, 8, 16, -1 }, { 8, 8, 8, 0 } },
1122 { HAL_PIXEL_FORMAT_RGB_565
, { 11, 5, 0, -1 }, { 5, 6, 5, 0 } },
1123 /* This must be after HAL_PIXEL_FORMAT_RGBA_8888, we only keep BGRA
1124 * visual if it turns out RGBA visual is not available.
1126 { HAL_PIXEL_FORMAT_BGRA_8888
, { 16, 8, 0, 24 }, { 8, 8, 8, 8 } },
1129 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
1130 int config_count
= 0;
1132 /* The nesting of loops is significant here. Also significant is the order
1133 * of the HAL pixel formats. Many Android apps (such as Google's official
1134 * NDK GLES2 example app), and even portions the core framework code (such
1135 * as SystemServiceManager in Nougat), incorrectly choose their EGLConfig.
1136 * They neglect to match the EGLConfig's EGL_NATIVE_VISUAL_ID against the
1137 * window's native format, and instead choose the first EGLConfig whose
1138 * channel sizes match those of the native window format while ignoring the
1139 * channel *ordering*.
1141 * We can detect such buggy clients in logcat when they call
1142 * eglCreateSurface, by detecting the mismatch between the EGLConfig's
1143 * format and the window's format.
1145 * As a workaround, we generate EGLConfigs such that all EGLConfigs for HAL
1146 * pixel format i precede those for HAL pixel format i+1. In my
1147 * (chadversary) testing on Android Nougat, this was good enough to pacify
1148 * the buggy clients.
1150 bool has_rgba
= false;
1151 for (int i
= 0; i
< ARRAY_SIZE(visuals
); i
++) {
1152 /* Only enable BGRA configs when RGBA is not available. BGRA configs are
1153 * buggy on stock Android.
1155 if (visuals
[i
].format
== HAL_PIXEL_FORMAT_BGRA_8888
&& has_rgba
)
1157 for (int j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
1158 const EGLint surface_type
= EGL_WINDOW_BIT
| EGL_PBUFFER_BIT
;
1160 const EGLint config_attrs
[] = {
1161 EGL_NATIVE_VISUAL_ID
, visuals
[i
].format
,
1162 EGL_NATIVE_VISUAL_TYPE
, visuals
[i
].format
,
1163 EGL_FRAMEBUFFER_TARGET_ANDROID
, EGL_TRUE
,
1164 EGL_RECORDABLE_ANDROID
, EGL_TRUE
,
1168 struct dri2_egl_config
*dri2_conf
=
1169 dri2_add_config(disp
, dri2_dpy
->driver_configs
[j
],
1170 config_count
+ 1, surface_type
, config_attrs
,
1171 visuals
[i
].rgba_shifts
, visuals
[i
].rgba_sizes
);
1173 if (dri2_conf
->base
.ConfigID
== config_count
+ 1)
1178 if (visuals
[i
].format
== HAL_PIXEL_FORMAT_RGBA_8888
&& format_count
[i
])
1182 for (int i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1183 if (!format_count
[i
]) {
1184 _eglLog(_EGL_DEBUG
, "No DRI config supports native format 0x%x",
1189 return (config_count
!= 0);
1192 static const struct dri2_egl_display_vtbl droid_display_vtbl
= {
1193 .authenticate
= NULL
,
1194 .create_window_surface
= droid_create_window_surface
,
1195 .create_pbuffer_surface
= droid_create_pbuffer_surface
,
1196 .destroy_surface
= droid_destroy_surface
,
1197 .create_image
= droid_create_image_khr
,
1198 .swap_buffers
= droid_swap_buffers
,
1199 .swap_interval
= droid_swap_interval
,
1200 .query_buffer_age
= droid_query_buffer_age
,
1201 .query_surface
= droid_query_surface
,
1202 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1203 .set_shared_buffer_mode
= droid_set_shared_buffer_mode
,
1206 #ifdef HAVE_DRM_GRALLOC
1207 static const __DRIdri2LoaderExtension droid_dri2_loader_extension
= {
1208 .base
= { __DRI_DRI2_LOADER
, 4 },
1211 .flushFrontBuffer
= droid_flush_front_buffer
,
1212 .getBuffersWithFormat
= droid_get_buffers_with_format
,
1213 .getCapability
= droid_get_capability
,
1216 static const __DRIextension
*droid_dri2_loader_extensions
[] = {
1217 &droid_dri2_loader_extension
.base
,
1218 &image_lookup_extension
.base
,
1219 &use_invalidate
.base
,
1220 /* No __DRI_MUTABLE_RENDER_BUFFER_LOADER because it requires
1221 * __DRI_IMAGE_LOADER.
1225 #endif /* HAVE_DRM_GRALLOC */
1227 static const __DRIimageLoaderExtension droid_image_loader_extension
= {
1228 .base
= { __DRI_IMAGE_LOADER
, 2 },
1230 .getBuffers
= droid_image_get_buffers
,
1231 .flushFrontBuffer
= droid_flush_front_buffer
,
1232 .getCapability
= droid_get_capability
,
1236 droid_display_shared_buffer(__DRIdrawable
*driDrawable
, int fence_fd
,
1237 void *loaderPrivate
)
1239 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1240 struct ANativeWindowBuffer
*old_buffer UNUSED
= dri2_surf
->buffer
;
1242 if (!_eglSurfaceInSharedBufferMode(&dri2_surf
->base
)) {
1243 _eglLog(_EGL_WARNING
, "%s: internal error: buffer is not shared",
1248 if (fence_fd
>= 0) {
1249 /* The driver's fence is more recent than the surface's out fence, if it
1250 * exists at all. So use the driver's fence.
1252 if (dri2_surf
->out_fence_fd
>= 0) {
1253 close(dri2_surf
->out_fence_fd
);
1254 dri2_surf
->out_fence_fd
= -1;
1256 } else if (dri2_surf
->out_fence_fd
>= 0) {
1257 fence_fd
= dri2_surf
->out_fence_fd
;
1258 dri2_surf
->out_fence_fd
= -1;
1261 if (dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
1263 _eglLog(_EGL_WARNING
, "%s: ANativeWindow::queueBuffer failed", __func__
);
1270 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
1272 /* Tear down the surface because it no longer has a back buffer. */
1273 struct dri2_egl_display
*dri2_dpy
=
1274 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1276 _eglLog(_EGL_WARNING
, "%s: ANativeWindow::dequeueBuffer failed", __func__
);
1278 dri2_surf
->base
.Lost
= true;
1279 dri2_surf
->buffer
= NULL
;
1280 dri2_surf
->back
= NULL
;
1282 if (dri2_surf
->dri_image_back
) {
1283 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image_back
);
1284 dri2_surf
->dri_image_back
= NULL
;
1287 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
1294 /* Access to the buffer is controlled by a sync fence. Block on it.
1296 * Ideally, we would submit the fence to the driver, and the driver would
1297 * postpone command execution until it signalled. But DRI lacks API for
1298 * that (as of 2018-04-11).
1300 * SYNC_IOC_WAIT waits forever if timeout < 0
1302 sync_wait(fence_fd
, -1);
1306 static const __DRImutableRenderBufferLoaderExtension droid_mutable_render_buffer_extension
= {
1307 .base
= { __DRI_MUTABLE_RENDER_BUFFER_LOADER
, 1 },
1308 .displaySharedBuffer
= droid_display_shared_buffer
,
1311 static const __DRIextension
*droid_image_loader_extensions
[] = {
1312 &droid_image_loader_extension
.base
,
1313 &image_lookup_extension
.base
,
1314 &use_invalidate
.base
,
1315 &droid_mutable_render_buffer_extension
.base
,
1320 droid_load_driver(_EGLDisplay
*disp
, bool swrast
)
1322 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1324 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1325 if (dri2_dpy
->driver_name
== NULL
)
1328 #ifdef HAVE_DRM_GRALLOC
1329 /* Handle control nodes using __DRI_DRI2_LOADER extension and GEM names
1330 * for backwards compatibility with drm_gralloc. (Do not use on new
1332 dri2_dpy
->loader_extensions
= droid_dri2_loader_extensions
;
1333 if (!dri2_load_driver(disp
)) {
1338 /* Use kms swrast only with vgem / virtio_gpu.
1339 * virtio-gpu fallbacks to software rendering when 3D features
1340 * are unavailable since 6c5ab.
1342 if (strcmp(dri2_dpy
->driver_name
, "vgem") == 0 ||
1343 strcmp(dri2_dpy
->driver_name
, "virtio_gpu") == 0) {
1344 free(dri2_dpy
->driver_name
);
1345 dri2_dpy
->driver_name
= strdup("kms_swrast");
1351 dri2_dpy
->loader_extensions
= droid_image_loader_extensions
;
1352 if (!dri2_load_driver_dri3(disp
)) {
1360 free(dri2_dpy
->driver_name
);
1361 dri2_dpy
->driver_name
= NULL
;
1366 droid_unload_driver(_EGLDisplay
*disp
)
1368 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1370 dlclose(dri2_dpy
->driver
);
1371 dri2_dpy
->driver
= NULL
;
1372 free(dri2_dpy
->driver_name
);
1373 dri2_dpy
->driver_name
= NULL
;
1377 droid_filter_device(_EGLDisplay
*disp
, int fd
, const char *vendor
)
1379 drmVersionPtr ver
= drmGetVersion(fd
);
1383 if (strcmp(vendor
, ver
->name
) != 0) {
1384 drmFreeVersion(ver
);
1388 drmFreeVersion(ver
);
1393 droid_probe_device(_EGLDisplay
*disp
, bool swrast
)
1395 /* Check that the device is supported, by attempting to:
1396 * - load the dri module
1397 * - and, create a screen
1399 if (!droid_load_driver(disp
, swrast
))
1402 if (!dri2_create_screen(disp
)) {
1403 _eglLog(_EGL_WARNING
, "DRI2: failed to create screen");
1404 droid_unload_driver(disp
);
1410 #ifdef HAVE_DRM_GRALLOC
1412 droid_open_device(_EGLDisplay
*disp
, bool swrast
)
1414 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1415 int fd
= -1, err
= -EINVAL
;
1420 if (dri2_dpy
->gralloc
->perform
)
1421 err
= dri2_dpy
->gralloc
->perform(dri2_dpy
->gralloc
,
1422 GRALLOC_MODULE_PERFORM_GET_DRM_FD
,
1424 if (err
|| fd
< 0) {
1425 _eglLog(_EGL_WARNING
, "fail to get drm fd");
1429 dri2_dpy
->fd
= os_dupfd_cloexec(fd
);
1430 if (dri2_dpy
->fd
< 0)
1433 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
)
1436 return droid_probe_device(disp
, swrast
);
1440 droid_open_device(_EGLDisplay
*disp
, bool swrast
)
1442 #define MAX_DRM_DEVICES 64
1443 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1444 drmDevicePtr device
, devices
[MAX_DRM_DEVICES
] = { NULL
};
1447 char *vendor_name
= NULL
;
1448 char vendor_buf
[PROPERTY_VALUE_MAX
];
1450 #ifdef EGL_FORCE_RENDERNODE
1451 const unsigned node_type
= DRM_NODE_RENDER
;
1453 const unsigned node_type
= swrast
? DRM_NODE_PRIMARY
: DRM_NODE_RENDER
;
1456 if (property_get("drm.gpu.vendor_name", vendor_buf
, NULL
) > 0)
1457 vendor_name
= vendor_buf
;
1459 num_devices
= drmGetDevices2(0, devices
, ARRAY_SIZE(devices
));
1460 if (num_devices
< 0)
1463 for (int i
= 0; i
< num_devices
; i
++) {
1464 device
= devices
[i
];
1466 if (!(device
->available_nodes
& (1 << node_type
)))
1469 dri2_dpy
->fd
= loader_open_device(device
->nodes
[node_type
]);
1470 if (dri2_dpy
->fd
< 0) {
1471 _eglLog(_EGL_WARNING
, "%s() Failed to open DRM device %s",
1472 __func__
, device
->nodes
[node_type
]);
1476 /* If a vendor is explicitly provided, we use only that.
1477 * Otherwise we fall-back the first device that is supported.
1480 if (droid_filter_device(disp
, dri2_dpy
->fd
, vendor_name
)) {
1481 /* Device does not match - try next device */
1482 close(dri2_dpy
->fd
);
1486 /* If the requested device matches - use it. Regardless if
1487 * init fails, do not fall-back to any other device.
1489 if (!droid_probe_device(disp
, false)) {
1490 close(dri2_dpy
->fd
);
1496 if (droid_probe_device(disp
, swrast
))
1499 /* No explicit request - attempt the next device */
1500 close(dri2_dpy
->fd
);
1503 drmFreeDevices(devices
, num_devices
);
1505 if (dri2_dpy
->fd
< 0) {
1506 _eglLog(_EGL_WARNING
, "Failed to open %s DRM device",
1507 vendor_name
? "desired": "any");
1512 #undef MAX_DRM_DEVICES
1518 dri2_initialize_android(_EGLDisplay
*disp
)
1521 bool device_opened
= false;
1522 struct dri2_egl_display
*dri2_dpy
;
1526 dri2_dpy
= calloc(1, sizeof(*dri2_dpy
));
1528 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1531 ret
= hw_get_module(GRALLOC_HARDWARE_MODULE_ID
,
1532 (const hw_module_t
**)&dri2_dpy
->gralloc
);
1534 err
= "DRI2: failed to get gralloc module";
1538 disp
->DriverData
= (void *) dri2_dpy
;
1539 if (!disp
->Options
.ForceSoftware
)
1540 device_opened
= droid_open_device(disp
, false);
1542 device_opened
= droid_open_device(disp
, true);
1544 if (!device_opened
) {
1545 err
= "DRI2: failed to open device";
1549 dev
= _eglAddDevice(dri2_dpy
->fd
, false);
1551 err
= "DRI2: failed to find EGLDevice";
1557 if (!dri2_setup_extensions(disp
)) {
1558 err
= "DRI2: failed to setup extensions";
1562 dri2_setup_screen(disp
);
1564 /* We set the maximum swap interval as 1 for Android platform, since it is
1565 * the maximum value supported by Android according to the value of
1566 * ANativeWindow::maxSwapInterval.
1568 dri2_setup_swap_interval(disp
, 1);
1570 disp
->Extensions
.ANDROID_framebuffer_target
= EGL_TRUE
;
1571 disp
->Extensions
.ANDROID_image_native_buffer
= EGL_TRUE
;
1572 disp
->Extensions
.ANDROID_recordable
= EGL_TRUE
;
1574 /* Querying buffer age requires a buffer to be dequeued. Without
1575 * EGL_ANDROID_native_fence_sync, dequeue might call eglClientWaitSync and
1576 * result in a deadlock (the lock is already held by eglQuerySurface).
1578 if (disp
->Extensions
.ANDROID_native_fence_sync
) {
1579 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1581 /* disable KHR_partial_update that might have been enabled in
1584 disp
->Extensions
.KHR_partial_update
= EGL_FALSE
;
1587 disp
->Extensions
.KHR_image
= EGL_TRUE
;
1588 #if ANDROID_API_LEVEL >= 24
1589 if (dri2_dpy
->mutable_render_buffer
&&
1590 dri2_dpy
->loader_extensions
== droid_image_loader_extensions
) {
1591 disp
->Extensions
.KHR_mutable_render_buffer
= EGL_TRUE
;
1595 /* Create configs *after* enabling extensions because presence of DRI
1596 * driver extensions can affect the capabilities of EGLConfigs.
1598 if (!droid_add_configs_for_visuals(disp
)) {
1599 err
= "DRI2: failed to add configs";
1603 /* Fill vtbl last to prevent accidentally calling virtual function during
1606 dri2_dpy
->vtbl
= &droid_display_vtbl
;
1611 dri2_display_destroy(disp
);
1612 return _eglError(EGL_NOT_INITIALIZED
, err
);