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.
34 #if ANDROID_VERSION >= 0x402
35 #include <sync/sync.h>
40 #include "egl_dri2_fallbacks.h"
41 #include "gralloc_drm.h"
44 get_format_bpp(int native
)
49 case HAL_PIXEL_FORMAT_RGBA_8888
:
50 case HAL_PIXEL_FORMAT_RGBX_8888
:
51 case HAL_PIXEL_FORMAT_BGRA_8888
:
54 case HAL_PIXEL_FORMAT_RGB_888
:
57 case HAL_PIXEL_FORMAT_RGB_565
:
68 /* createImageFromFds requires fourcc format */
69 static int get_fourcc(int format
)
72 case __DRI_IMAGE_FORMAT_RGB565
: return __DRI_IMAGE_FOURCC_RGB565
;
73 case __DRI_IMAGE_FORMAT_ARGB8888
: return __DRI_IMAGE_FOURCC_ARGB8888
;
74 case __DRI_IMAGE_FORMAT_XRGB8888
: return __DRI_IMAGE_FOURCC_XRGB8888
;
75 case __DRI_IMAGE_FORMAT_ABGR8888
: return __DRI_IMAGE_FOURCC_ABGR8888
;
76 case __DRI_IMAGE_FORMAT_XBGR8888
: return __DRI_IMAGE_FOURCC_XBGR8888
;
81 static int get_format(int format
)
84 case HAL_PIXEL_FORMAT_BGRA_8888
: return __DRI_IMAGE_FORMAT_ARGB8888
;
85 case HAL_PIXEL_FORMAT_RGB_565
: return __DRI_IMAGE_FORMAT_RGB565
;
86 case HAL_PIXEL_FORMAT_RGBA_8888
: return __DRI_IMAGE_FORMAT_ABGR8888
;
87 case HAL_PIXEL_FORMAT_RGBX_8888
: return __DRI_IMAGE_FORMAT_XBGR8888
;
88 case HAL_PIXEL_FORMAT_RGB_888
:
91 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", format
);
96 get_native_buffer_fd(struct ANativeWindowBuffer
*buf
)
98 native_handle_t
*handle
= (native_handle_t
*)buf
->handle
;
100 * Various gralloc implementations exist, but the dma-buf fd tends
101 * to be first. Access it directly to avoid a dependency on specific
104 return (handle
&& handle
->numFds
) ? handle
->data
[0] : -1;
108 get_native_buffer_name(struct ANativeWindowBuffer
*buf
)
110 return gralloc_drm_get_gem_handle(buf
->handle
);
114 droid_window_dequeue_buffer(struct dri2_egl_surface
*dri2_surf
)
116 #if ANDROID_VERSION >= 0x0402
119 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
123 /* If access to the buffer is controlled by a sync fence, then block on the
126 * It may be more performant to postpone blocking until there is an
127 * immediate need to write to the buffer. But doing so would require adding
128 * hooks to the DRI2 loader.
130 * From the ANativeWindow::dequeueBuffer documentation:
132 * The libsync fence file descriptor returned in the int pointed to by
133 * the fenceFd argument will refer to the fence that must signal
134 * before the dequeued buffer may be written to. A value of -1
135 * indicates that the caller may access the buffer immediately without
136 * waiting on a fence. If a valid file descriptor is returned (i.e.
137 * any value except -1) then the caller is responsible for closing the
141 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
143 * Waits indefinitely if timeout < 0.
146 sync_wait(fence_fd
, timeout
);
150 dri2_surf
->buffer
->common
.incRef(&dri2_surf
->buffer
->common
);
152 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
))
155 dri2_surf
->buffer
->common
.incRef(&dri2_surf
->buffer
->common
);
156 dri2_surf
->window
->lockBuffer(dri2_surf
->window
, dri2_surf
->buffer
);
163 droid_window_enqueue_buffer(struct dri2_egl_surface
*dri2_surf
)
165 #if ANDROID_VERSION >= 0x0402
166 /* Queue the buffer without a sync fence. This informs the ANativeWindow
167 * that it may access the buffer immediately.
169 * From ANativeWindow::dequeueBuffer:
171 * The fenceFd argument specifies a libsync fence file descriptor for
172 * a fence that must signal before the buffer can be accessed. If
173 * the buffer can be accessed immediately then a value of -1 should
174 * be used. The caller must not use the file descriptor after it
175 * is passed to queueBuffer, and the ANativeWindow implementation
176 * is responsible for closing it.
179 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
182 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
);
185 dri2_surf
->buffer
->common
.decRef(&dri2_surf
->buffer
->common
);
186 dri2_surf
->buffer
= NULL
;
192 droid_window_cancel_buffer(struct dri2_egl_surface
*dri2_surf
)
194 /* no cancel buffer? */
195 droid_window_enqueue_buffer(dri2_surf
);
199 droid_alloc_local_buffer(struct dri2_egl_surface
*dri2_surf
,
200 unsigned int att
, unsigned int format
)
202 struct dri2_egl_display
*dri2_dpy
=
203 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
205 if (att
>= ARRAY_SIZE(dri2_surf
->local_buffers
))
208 if (!dri2_surf
->local_buffers
[att
]) {
209 dri2_surf
->local_buffers
[att
] =
210 dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
, att
, format
,
211 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
214 return dri2_surf
->local_buffers
[att
];
218 droid_free_local_buffers(struct dri2_egl_surface
*dri2_surf
)
220 struct dri2_egl_display
*dri2_dpy
=
221 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
224 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->local_buffers
); i
++) {
225 if (dri2_surf
->local_buffers
[i
]) {
226 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
227 dri2_surf
->local_buffers
[i
]);
228 dri2_surf
->local_buffers
[i
] = NULL
;
234 droid_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
235 _EGLConfig
*conf
, void *native_window
,
236 const EGLint
*attrib_list
)
238 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
239 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
240 struct dri2_egl_surface
*dri2_surf
;
241 struct ANativeWindow
*window
= native_window
;
242 const __DRIconfig
*config
;
244 dri2_surf
= calloc(1, sizeof *dri2_surf
);
246 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
250 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
251 goto cleanup_surface
;
253 if (type
== EGL_WINDOW_BIT
) {
256 if (!window
|| window
->common
.magic
!= ANDROID_NATIVE_WINDOW_MAGIC
) {
257 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
258 goto cleanup_surface
;
260 if (window
->query(window
, NATIVE_WINDOW_FORMAT
, &format
)) {
261 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
262 goto cleanup_surface
;
265 if (format
!= dri2_conf
->base
.NativeVisualID
) {
266 _eglLog(_EGL_WARNING
, "Native format mismatch: 0x%x != 0x%x",
267 format
, dri2_conf
->base
.NativeVisualID
);
270 window
->query(window
, NATIVE_WINDOW_WIDTH
, &dri2_surf
->base
.Width
);
271 window
->query(window
, NATIVE_WINDOW_HEIGHT
, &dri2_surf
->base
.Height
);
274 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
275 dri2_surf
->base
.GLColorspace
);
277 dri2_surf
->dri_drawable
=
278 (*dri2_dpy
->dri2
->createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
280 if (dri2_surf
->dri_drawable
== NULL
) {
281 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
282 goto cleanup_surface
;
286 window
->common
.incRef(&window
->common
);
287 dri2_surf
->window
= window
;
290 return &dri2_surf
->base
;
299 droid_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
300 _EGLConfig
*conf
, void *native_window
,
301 const EGLint
*attrib_list
)
303 return droid_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
304 native_window
, attrib_list
);
308 droid_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
309 _EGLConfig
*conf
, const EGLint
*attrib_list
)
311 return droid_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
316 droid_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
318 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
319 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
321 if (!_eglPutSurface(surf
))
324 droid_free_local_buffers(dri2_surf
);
326 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
327 if (dri2_surf
->buffer
)
328 droid_window_cancel_buffer(dri2_surf
);
330 dri2_surf
->window
->common
.decRef(&dri2_surf
->window
->common
);
333 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
341 update_buffers(struct dri2_egl_surface
*dri2_surf
)
343 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
346 /* try to dequeue the next back buffer */
347 if (!dri2_surf
->buffer
&& !droid_window_dequeue_buffer(dri2_surf
))
350 /* free outdated buffers and update the surface size */
351 if (dri2_surf
->base
.Width
!= dri2_surf
->buffer
->width
||
352 dri2_surf
->base
.Height
!= dri2_surf
->buffer
->height
) {
353 droid_free_local_buffers(dri2_surf
);
354 dri2_surf
->base
.Width
= dri2_surf
->buffer
->width
;
355 dri2_surf
->base
.Height
= dri2_surf
->buffer
->height
;
362 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
364 struct dri2_egl_display
*dri2_dpy
=
365 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
369 if (!dri2_surf
->buffer
)
372 fd
= get_native_buffer_fd(dri2_surf
->buffer
);
376 format
= get_format(dri2_surf
->buffer
->format
);
378 stride
= dri2_surf
->buffer
->stride
*
379 get_format_bpp(dri2_surf
->buffer
->format
);
381 dri2_surf
->dri_image
=
382 dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
383 dri2_surf
->base
.Width
,
384 dri2_surf
->base
.Height
,
391 if (!dri2_surf
->dri_image
)
398 droid_image_get_buffers(__DRIdrawable
*driDrawable
,
402 uint32_t buffer_mask
,
403 struct __DRIimageList
*images
)
405 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
407 if (update_buffers(dri2_surf
) < 0)
410 if (get_back_bo(dri2_surf
) < 0) {
411 _eglError(EGL_BAD_PARAMETER
, "get_back_bo");
415 images
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
416 images
->back
= dri2_surf
->dri_image
;
422 droid_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
424 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
425 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
426 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
429 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
432 if (dri2_drv
->glFlush
) {
433 ctx
= _eglGetCurrentContext();
434 if (ctx
&& ctx
->DrawSurface
== &dri2_surf
->base
)
438 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
440 if (dri2_surf
->buffer
)
441 droid_window_enqueue_buffer(dri2_surf
);
443 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
449 dri2_create_image_android_native_buffer(_EGLDriver
*drv
, _EGLDisplay
*disp
,
451 struct ANativeWindowBuffer
*buf
)
453 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
454 struct dri2_egl_image
*dri2_img
;
458 /* From the EGL_ANDROID_image_native_buffer spec:
460 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
461 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
463 _eglError(EGL_BAD_CONTEXT
, "eglCreateEGLImageKHR: for "
464 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
469 if (!buf
|| buf
->common
.magic
!= ANDROID_NATIVE_BUFFER_MAGIC
||
470 buf
->common
.version
!= sizeof(*buf
)) {
471 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
475 fd
= get_native_buffer_fd(buf
);
477 const EGLint attr_list
[14] = {
478 EGL_WIDTH
, buf
->width
,
479 EGL_HEIGHT
, buf
->height
,
480 EGL_LINUX_DRM_FOURCC_EXT
, get_fourcc(get_format(buf
->format
)),
481 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
482 EGL_DMA_BUF_PLANE0_PITCH_EXT
, buf
->stride
* get_format_bpp(buf
->format
),
483 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, 0,
487 return dri2_create_image_khr(drv
, disp
, ctx
, EGL_LINUX_DMA_BUF_EXT
,
491 name
= get_native_buffer_name(buf
);
493 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
497 dri2_img
= calloc(1, sizeof(*dri2_img
));
499 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
503 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
508 dri2_img
->dri_image
=
509 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
512 get_format(buf
->format
),
516 if (!dri2_img
->dri_image
) {
518 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
522 return &dri2_img
->base
;
526 droid_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
527 _EGLContext
*ctx
, EGLenum target
,
528 EGLClientBuffer buffer
, const EGLint
*attr_list
)
531 case EGL_NATIVE_BUFFER_ANDROID
:
532 return dri2_create_image_android_native_buffer(drv
, disp
, ctx
,
533 (struct ANativeWindowBuffer
*) buffer
);
535 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
540 droid_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
545 droid_get_buffers_parse_attachments(struct dri2_egl_surface
*dri2_surf
,
546 unsigned int *attachments
, int count
)
548 int num_buffers
= 0, i
;
550 /* fill dri2_surf->buffers */
551 for (i
= 0; i
< count
* 2; i
+= 2) {
552 __DRIbuffer
*buf
, *local
;
554 assert(num_buffers
< ARRAY_SIZE(dri2_surf
->buffers
));
555 buf
= &dri2_surf
->buffers
[num_buffers
];
557 switch (attachments
[i
]) {
558 case __DRI_BUFFER_BACK_LEFT
:
559 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
560 buf
->attachment
= attachments
[i
];
561 buf
->name
= get_native_buffer_name(dri2_surf
->buffer
);
562 buf
->cpp
= get_format_bpp(dri2_surf
->buffer
->format
);
563 buf
->pitch
= dri2_surf
->buffer
->stride
* buf
->cpp
;
571 /* fall through for pbuffers */
572 case __DRI_BUFFER_DEPTH
:
573 case __DRI_BUFFER_STENCIL
:
574 case __DRI_BUFFER_ACCUM
:
575 case __DRI_BUFFER_DEPTH_STENCIL
:
576 case __DRI_BUFFER_HIZ
:
577 local
= droid_alloc_local_buffer(dri2_surf
,
578 attachments
[i
], attachments
[i
+ 1]);
585 case __DRI_BUFFER_FRONT_LEFT
:
586 case __DRI_BUFFER_FRONT_RIGHT
:
587 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
588 case __DRI_BUFFER_FAKE_FRONT_RIGHT
:
589 case __DRI_BUFFER_BACK_RIGHT
:
591 /* no front or right buffers */
600 droid_get_buffers_with_format(__DRIdrawable
* driDrawable
,
601 int *width
, int *height
,
602 unsigned int *attachments
, int count
,
603 int *out_count
, void *loaderPrivate
)
605 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
606 struct dri2_egl_display
*dri2_dpy
=
607 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
610 if (update_buffers(dri2_surf
) < 0)
613 dri2_surf
->buffer_count
=
614 droid_get_buffers_parse_attachments(dri2_surf
, attachments
, count
);
617 *width
= dri2_surf
->base
.Width
;
619 *height
= dri2_surf
->base
.Height
;
621 *out_count
= dri2_surf
->buffer_count
;
623 return dri2_surf
->buffers
;
627 droid_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
629 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
632 unsigned int rgba_masks
[4];
634 { HAL_PIXEL_FORMAT_RGBA_8888
, { 0xff, 0xff00, 0xff0000, 0xff000000 } },
635 { HAL_PIXEL_FORMAT_RGBX_8888
, { 0xff, 0xff00, 0xff0000, 0x0 } },
636 { HAL_PIXEL_FORMAT_RGB_888
, { 0xff, 0xff00, 0xff0000, 0x0 } },
637 { HAL_PIXEL_FORMAT_RGB_565
, { 0xf800, 0x7e0, 0x1f, 0x0 } },
638 { HAL_PIXEL_FORMAT_BGRA_8888
, { 0xff0000, 0xff00, 0xff, 0xff000000 } },
639 { 0, { 0, 0, 0, 0 } }
641 EGLint config_attrs
[] = {
642 EGL_NATIVE_VISUAL_ID
, 0,
643 EGL_NATIVE_VISUAL_TYPE
, 0,
644 EGL_FRAMEBUFFER_TARGET_ANDROID
, EGL_TRUE
,
645 EGL_RECORDABLE_ANDROID
, EGL_TRUE
,
651 for (i
= 0; visuals
[i
].format
; i
++) {
652 int format_count
= 0;
654 config_attrs
[1] = visuals
[i
].format
;
655 config_attrs
[3] = visuals
[i
].format
;
657 for (j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
658 const EGLint surface_type
= EGL_WINDOW_BIT
| EGL_PBUFFER_BIT
;
659 struct dri2_egl_config
*dri2_conf
;
660 unsigned int double_buffered
= 0;
662 dri2_dpy
->core
->getConfigAttrib(dri2_dpy
->driver_configs
[j
],
663 __DRI_ATTRIB_DOUBLE_BUFFER
, &double_buffered
);
665 /* support only double buffered configs */
666 if (!double_buffered
)
669 dri2_conf
= dri2_add_config(dpy
, dri2_dpy
->driver_configs
[j
],
670 count
+ 1, surface_type
, config_attrs
, visuals
[i
].rgba_masks
);
678 _eglLog(_EGL_DEBUG
, "No DRI config supports native format 0x%x",
683 /* post-process configs */
684 for (i
= 0; i
< dpy
->Configs
->Size
; i
++) {
685 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(dpy
->Configs
->Elements
[i
]);
687 /* there is no front buffer so no OpenGL */
688 dri2_conf
->base
.RenderableType
&= ~EGL_OPENGL_BIT
;
689 dri2_conf
->base
.Conformant
&= ~EGL_OPENGL_BIT
;
696 droid_open_device(void)
698 const hw_module_t
*mod
;
701 err
= hw_get_module(GRALLOC_HARDWARE_MODULE_ID
, &mod
);
703 const gralloc_module_t
*gr
= (gralloc_module_t
*) mod
;
707 err
= gr
->perform(gr
, GRALLOC_MODULE_PERFORM_GET_DRM_FD
, &fd
);
710 _eglLog(_EGL_WARNING
, "fail to get drm fd");
714 return (fd
>= 0) ? dup(fd
) : -1;
717 /* support versions < JellyBean */
729 droid_log(EGLint level
, const char *msg
)
742 LOG_FATAL("%s", msg
);
749 static struct dri2_egl_display_vtbl droid_display_vtbl
= {
750 .authenticate
= NULL
,
751 .create_window_surface
= droid_create_window_surface
,
752 .create_pixmap_surface
= dri2_fallback_create_pixmap_surface
,
753 .create_pbuffer_surface
= droid_create_pbuffer_surface
,
754 .destroy_surface
= droid_destroy_surface
,
755 .create_image
= droid_create_image_khr
,
756 .swap_interval
= dri2_fallback_swap_interval
,
757 .swap_buffers
= droid_swap_buffers
,
758 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
759 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
760 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
761 .copy_buffers
= dri2_fallback_copy_buffers
,
762 .query_buffer_age
= dri2_fallback_query_buffer_age
,
763 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
764 .get_sync_values
= dri2_fallback_get_sync_values
,
765 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
768 static const __DRIimageLoaderExtension droid_image_loader_extension
= {
769 .base
= { __DRI_IMAGE_LOADER
, 1 },
771 .getBuffers
= droid_image_get_buffers
,
772 .flushFrontBuffer
= droid_flush_front_buffer
,
776 dri2_initialize_android(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
778 struct dri2_egl_display
*dri2_dpy
;
781 _eglSetLogProc(droid_log
);
783 loader_set_logger(_eglLog
);
785 dri2_dpy
= calloc(1, sizeof(*dri2_dpy
));
787 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
789 dpy
->DriverData
= (void *) dri2_dpy
;
791 dri2_dpy
->fd
= droid_open_device();
792 if (dri2_dpy
->fd
< 0) {
793 err
= "DRI2: failed to open device";
794 goto cleanup_display
;
797 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
, 0);
798 if (dri2_dpy
->driver_name
== NULL
) {
799 err
= "DRI2: failed to get driver name";
803 if (!dri2_load_driver(dpy
)) {
804 err
= "DRI2: failed to load driver";
805 goto cleanup_driver_name
;
808 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
810 dri2_dpy
->extensions
[0] = &droid_image_loader_extension
.base
;
811 dri2_dpy
->extensions
[1] = &use_invalidate
.base
;
812 dri2_dpy
->extensions
[2] = &image_lookup_extension
.base
;
814 /* render nodes cannot use Gem names, and thus do not support
815 * the __DRI_DRI2_LOADER extension */
816 if (!dri2_dpy
->is_render_node
) {
817 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
818 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
819 dri2_dpy
->dri2_loader_extension
.getBuffers
= NULL
;
820 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= droid_flush_front_buffer
;
821 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
822 droid_get_buffers_with_format
;
823 dri2_dpy
->extensions
[3] = &dri2_dpy
->dri2_loader_extension
.base
;
824 dri2_dpy
->extensions
[4] = NULL
;
826 dri2_dpy
->extensions
[3] = NULL
;
829 if (!dri2_create_screen(dpy
)) {
830 err
= "DRI2: failed to create screen";
834 if (!droid_add_configs_for_visuals(drv
, dpy
)) {
835 err
= "DRI2: failed to add configs";
839 dpy
->Extensions
.ANDROID_framebuffer_target
= EGL_TRUE
;
840 dpy
->Extensions
.ANDROID_image_native_buffer
= EGL_TRUE
;
841 dpy
->Extensions
.ANDROID_recordable
= EGL_TRUE
;
842 dpy
->Extensions
.KHR_image_base
= EGL_TRUE
;
844 /* Fill vtbl last to prevent accidentally calling virtual function during
847 dri2_dpy
->vtbl
= &droid_display_vtbl
;
852 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
854 dlclose(dri2_dpy
->driver
);
856 free(dri2_dpy
->driver_name
);
862 return _eglError(EGL_NOT_INITIALIZED
, err
);