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(_EGLDisplay
*disp
, struct dri2_egl_surface
*dri2_surf
)
165 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
167 /* To avoid blocking other EGL calls, release the display mutex before
168 * we enter droid_window_enqueue_buffer() and re-acquire the mutex upon
171 mtx_unlock(&disp
->Mutex
);
173 #if ANDROID_VERSION >= 0x0402
174 /* Queue the buffer without a sync fence. This informs the ANativeWindow
175 * that it may access the buffer immediately.
177 * From ANativeWindow::dequeueBuffer:
179 * The fenceFd argument specifies a libsync fence file descriptor for
180 * a fence that must signal before the buffer can be accessed. If
181 * the buffer can be accessed immediately then a value of -1 should
182 * be used. The caller must not use the file descriptor after it
183 * is passed to queueBuffer, and the ANativeWindow implementation
184 * is responsible for closing it.
187 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
190 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
);
193 dri2_surf
->buffer
->common
.decRef(&dri2_surf
->buffer
->common
);
194 dri2_surf
->buffer
= NULL
;
196 mtx_lock(&disp
->Mutex
);
198 if (dri2_surf
->dri_image
) {
199 dri2_dpy
->image
->destroyImage(dri2_surf
->dri_image
);
200 dri2_surf
->dri_image
= NULL
;
207 droid_window_cancel_buffer(_EGLDisplay
*disp
, struct dri2_egl_surface
*dri2_surf
)
209 /* no cancel buffer? */
210 droid_window_enqueue_buffer(disp
, dri2_surf
);
214 droid_alloc_local_buffer(struct dri2_egl_surface
*dri2_surf
,
215 unsigned int att
, unsigned int format
)
217 struct dri2_egl_display
*dri2_dpy
=
218 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
220 if (att
>= ARRAY_SIZE(dri2_surf
->local_buffers
))
223 if (!dri2_surf
->local_buffers
[att
]) {
224 dri2_surf
->local_buffers
[att
] =
225 dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
, att
, format
,
226 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
229 return dri2_surf
->local_buffers
[att
];
233 droid_free_local_buffers(struct dri2_egl_surface
*dri2_surf
)
235 struct dri2_egl_display
*dri2_dpy
=
236 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
239 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->local_buffers
); i
++) {
240 if (dri2_surf
->local_buffers
[i
]) {
241 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
242 dri2_surf
->local_buffers
[i
]);
243 dri2_surf
->local_buffers
[i
] = NULL
;
249 droid_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
250 _EGLConfig
*conf
, void *native_window
,
251 const EGLint
*attrib_list
)
253 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
254 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
255 struct dri2_egl_surface
*dri2_surf
;
256 struct ANativeWindow
*window
= native_window
;
257 const __DRIconfig
*config
;
259 dri2_surf
= calloc(1, sizeof *dri2_surf
);
261 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
265 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
266 goto cleanup_surface
;
268 if (type
== EGL_WINDOW_BIT
) {
271 if (!window
|| window
->common
.magic
!= ANDROID_NATIVE_WINDOW_MAGIC
) {
272 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
273 goto cleanup_surface
;
275 if (window
->query(window
, NATIVE_WINDOW_FORMAT
, &format
)) {
276 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
277 goto cleanup_surface
;
280 if (format
!= dri2_conf
->base
.NativeVisualID
) {
281 _eglLog(_EGL_WARNING
, "Native format mismatch: 0x%x != 0x%x",
282 format
, dri2_conf
->base
.NativeVisualID
);
285 window
->query(window
, NATIVE_WINDOW_WIDTH
, &dri2_surf
->base
.Width
);
286 window
->query(window
, NATIVE_WINDOW_HEIGHT
, &dri2_surf
->base
.Height
);
289 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
290 dri2_surf
->base
.GLColorspace
);
292 goto cleanup_surface
;
294 dri2_surf
->dri_drawable
=
295 (*dri2_dpy
->dri2
->createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
297 if (dri2_surf
->dri_drawable
== NULL
) {
298 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
299 goto cleanup_surface
;
303 window
->common
.incRef(&window
->common
);
304 dri2_surf
->window
= window
;
307 return &dri2_surf
->base
;
316 droid_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
317 _EGLConfig
*conf
, void *native_window
,
318 const EGLint
*attrib_list
)
320 return droid_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
321 native_window
, attrib_list
);
325 droid_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
326 _EGLConfig
*conf
, const EGLint
*attrib_list
)
328 return droid_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
333 droid_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
335 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
336 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
338 if (!_eglPutSurface(surf
))
341 droid_free_local_buffers(dri2_surf
);
343 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
344 if (dri2_surf
->buffer
)
345 droid_window_cancel_buffer(disp
, dri2_surf
);
347 dri2_surf
->window
->common
.decRef(&dri2_surf
->window
->common
);
350 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
358 update_buffers(struct dri2_egl_surface
*dri2_surf
)
360 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
363 /* try to dequeue the next back buffer */
364 if (!dri2_surf
->buffer
&& !droid_window_dequeue_buffer(dri2_surf
)) {
365 _eglLog(_EGL_WARNING
, "Could not dequeue buffer from native window");
369 /* free outdated buffers and update the surface size */
370 if (dri2_surf
->base
.Width
!= dri2_surf
->buffer
->width
||
371 dri2_surf
->base
.Height
!= dri2_surf
->buffer
->height
) {
372 droid_free_local_buffers(dri2_surf
);
373 dri2_surf
->base
.Width
= dri2_surf
->buffer
->width
;
374 dri2_surf
->base
.Height
= dri2_surf
->buffer
->height
;
381 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
383 struct dri2_egl_display
*dri2_dpy
=
384 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
388 if (dri2_surf
->dri_image
)
391 if (!dri2_surf
->buffer
)
394 fd
= get_native_buffer_fd(dri2_surf
->buffer
);
396 _eglLog(_EGL_WARNING
, "Could not get native buffer FD");
400 fourcc
= get_fourcc(get_format(dri2_surf
->buffer
->format
));
402 pitch
= dri2_surf
->buffer
->stride
*
403 get_format_bpp(dri2_surf
->buffer
->format
);
405 if (fourcc
== -1 || pitch
== 0) {
406 _eglLog(_EGL_WARNING
, "Invalid buffer fourcc(%x) or pitch(%d)",
411 dri2_surf
->dri_image
=
412 dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
413 dri2_surf
->base
.Width
,
414 dri2_surf
->base
.Height
,
421 if (!dri2_surf
->dri_image
)
428 droid_image_get_buffers(__DRIdrawable
*driDrawable
,
432 uint32_t buffer_mask
,
433 struct __DRIimageList
*images
)
435 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
437 if (update_buffers(dri2_surf
) < 0)
440 if (get_back_bo(dri2_surf
) < 0) {
441 _eglError(EGL_BAD_PARAMETER
, "get_back_bo");
445 images
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
446 images
->back
= dri2_surf
->dri_image
;
452 droid_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
454 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
455 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
457 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
460 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
462 if (dri2_surf
->buffer
)
463 droid_window_enqueue_buffer(disp
, dri2_surf
);
465 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
471 dri2_create_image_android_native_buffer(_EGLDisplay
*disp
,
473 struct ANativeWindowBuffer
*buf
)
475 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
476 struct dri2_egl_image
*dri2_img
;
481 /* From the EGL_ANDROID_image_native_buffer spec:
483 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
484 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
486 _eglError(EGL_BAD_CONTEXT
, "eglCreateEGLImageKHR: for "
487 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
492 if (!buf
|| buf
->common
.magic
!= ANDROID_NATIVE_BUFFER_MAGIC
||
493 buf
->common
.version
!= sizeof(*buf
)) {
494 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
498 fd
= get_native_buffer_fd(buf
);
500 const int fourcc
= get_fourcc(get_format(buf
->format
));
501 const int pitch
= buf
->stride
* get_format_bpp(buf
->format
);
503 const EGLint attr_list
[14] = {
504 EGL_WIDTH
, buf
->width
,
505 EGL_HEIGHT
, buf
->height
,
506 EGL_LINUX_DRM_FOURCC_EXT
, fourcc
,
507 EGL_DMA_BUF_PLANE0_FD_EXT
, fd
,
508 EGL_DMA_BUF_PLANE0_PITCH_EXT
, pitch
,
509 EGL_DMA_BUF_PLANE0_OFFSET_EXT
, 0,
513 if (fourcc
== -1 || pitch
== 0)
516 return dri2_create_image_dma_buf(disp
, ctx
, NULL
, attr_list
);
519 name
= get_native_buffer_name(buf
);
521 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
525 format
= get_format(buf
->format
);
529 dri2_img
= calloc(1, sizeof(*dri2_img
));
531 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
535 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
540 dri2_img
->dri_image
=
541 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
548 if (!dri2_img
->dri_image
) {
550 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
554 return &dri2_img
->base
;
558 droid_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
559 _EGLContext
*ctx
, EGLenum target
,
560 EGLClientBuffer buffer
, const EGLint
*attr_list
)
563 case EGL_NATIVE_BUFFER_ANDROID
:
564 return dri2_create_image_android_native_buffer(disp
, ctx
,
565 (struct ANativeWindowBuffer
*) buffer
);
567 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
572 droid_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
577 droid_get_buffers_parse_attachments(struct dri2_egl_surface
*dri2_surf
,
578 unsigned int *attachments
, int count
)
580 int num_buffers
= 0, i
;
582 /* fill dri2_surf->buffers */
583 for (i
= 0; i
< count
* 2; i
+= 2) {
584 __DRIbuffer
*buf
, *local
;
586 assert(num_buffers
< ARRAY_SIZE(dri2_surf
->buffers
));
587 buf
= &dri2_surf
->buffers
[num_buffers
];
589 switch (attachments
[i
]) {
590 case __DRI_BUFFER_BACK_LEFT
:
591 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
592 buf
->attachment
= attachments
[i
];
593 buf
->name
= get_native_buffer_name(dri2_surf
->buffer
);
594 buf
->cpp
= get_format_bpp(dri2_surf
->buffer
->format
);
595 buf
->pitch
= dri2_surf
->buffer
->stride
* buf
->cpp
;
603 /* fall through for pbuffers */
604 case __DRI_BUFFER_DEPTH
:
605 case __DRI_BUFFER_STENCIL
:
606 case __DRI_BUFFER_ACCUM
:
607 case __DRI_BUFFER_DEPTH_STENCIL
:
608 case __DRI_BUFFER_HIZ
:
609 local
= droid_alloc_local_buffer(dri2_surf
,
610 attachments
[i
], attachments
[i
+ 1]);
617 case __DRI_BUFFER_FRONT_LEFT
:
618 case __DRI_BUFFER_FRONT_RIGHT
:
619 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
620 case __DRI_BUFFER_FAKE_FRONT_RIGHT
:
621 case __DRI_BUFFER_BACK_RIGHT
:
623 /* no front or right buffers */
632 droid_get_buffers_with_format(__DRIdrawable
* driDrawable
,
633 int *width
, int *height
,
634 unsigned int *attachments
, int count
,
635 int *out_count
, void *loaderPrivate
)
637 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
638 struct dri2_egl_display
*dri2_dpy
=
639 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
642 if (update_buffers(dri2_surf
) < 0)
645 dri2_surf
->buffer_count
=
646 droid_get_buffers_parse_attachments(dri2_surf
, attachments
, count
);
649 *width
= dri2_surf
->base
.Width
;
651 *height
= dri2_surf
->base
.Height
;
653 *out_count
= dri2_surf
->buffer_count
;
655 return dri2_surf
->buffers
;
659 droid_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
661 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
664 unsigned int rgba_masks
[4];
666 { HAL_PIXEL_FORMAT_RGBA_8888
, { 0xff, 0xff00, 0xff0000, 0xff000000 } },
667 { HAL_PIXEL_FORMAT_RGBX_8888
, { 0xff, 0xff00, 0xff0000, 0x0 } },
668 { HAL_PIXEL_FORMAT_RGB_888
, { 0xff, 0xff00, 0xff0000, 0x0 } },
669 { HAL_PIXEL_FORMAT_RGB_565
, { 0xf800, 0x7e0, 0x1f, 0x0 } },
670 { HAL_PIXEL_FORMAT_BGRA_8888
, { 0xff0000, 0xff00, 0xff, 0xff000000 } },
671 { 0, { 0, 0, 0, 0 } }
673 EGLint config_attrs
[] = {
674 EGL_NATIVE_VISUAL_ID
, 0,
675 EGL_NATIVE_VISUAL_TYPE
, 0,
676 EGL_FRAMEBUFFER_TARGET_ANDROID
, EGL_TRUE
,
677 EGL_RECORDABLE_ANDROID
, EGL_TRUE
,
683 for (i
= 0; visuals
[i
].format
; i
++) {
684 int format_count
= 0;
686 config_attrs
[1] = visuals
[i
].format
;
687 config_attrs
[3] = visuals
[i
].format
;
689 for (j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
690 const EGLint surface_type
= EGL_WINDOW_BIT
| EGL_PBUFFER_BIT
;
691 struct dri2_egl_config
*dri2_conf
;
692 unsigned int double_buffered
= 0;
694 dri2_dpy
->core
->getConfigAttrib(dri2_dpy
->driver_configs
[j
],
695 __DRI_ATTRIB_DOUBLE_BUFFER
, &double_buffered
);
697 /* support only double buffered configs */
698 if (!double_buffered
)
701 dri2_conf
= dri2_add_config(dpy
, dri2_dpy
->driver_configs
[j
],
702 count
+ 1, surface_type
, config_attrs
, visuals
[i
].rgba_masks
);
710 _eglLog(_EGL_DEBUG
, "No DRI config supports native format 0x%x",
715 /* post-process configs */
716 for (i
= 0; i
< dpy
->Configs
->Size
; i
++) {
717 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(dpy
->Configs
->Elements
[i
]);
719 /* there is no front buffer so no OpenGL */
720 dri2_conf
->base
.RenderableType
&= ~EGL_OPENGL_BIT
;
721 dri2_conf
->base
.Conformant
&= ~EGL_OPENGL_BIT
;
728 droid_open_device(void)
730 const hw_module_t
*mod
;
733 err
= hw_get_module(GRALLOC_HARDWARE_MODULE_ID
, &mod
);
735 const gralloc_module_t
*gr
= (gralloc_module_t
*) mod
;
739 err
= gr
->perform(gr
, GRALLOC_MODULE_PERFORM_GET_DRM_FD
, &fd
);
742 _eglLog(_EGL_WARNING
, "fail to get drm fd");
746 return (fd
>= 0) ? dup(fd
) : -1;
749 /* support versions < JellyBean */
761 droid_log(EGLint level
, const char *msg
)
774 LOG_FATAL("%s", msg
);
781 static struct dri2_egl_display_vtbl droid_display_vtbl
= {
782 .authenticate
= NULL
,
783 .create_window_surface
= droid_create_window_surface
,
784 .create_pixmap_surface
= dri2_fallback_create_pixmap_surface
,
785 .create_pbuffer_surface
= droid_create_pbuffer_surface
,
786 .destroy_surface
= droid_destroy_surface
,
787 .create_image
= droid_create_image_khr
,
788 .swap_interval
= dri2_fallback_swap_interval
,
789 .swap_buffers
= droid_swap_buffers
,
790 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
791 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
792 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
793 .copy_buffers
= dri2_fallback_copy_buffers
,
794 .query_buffer_age
= dri2_fallback_query_buffer_age
,
795 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
796 .get_sync_values
= dri2_fallback_get_sync_values
,
797 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
800 static const __DRIimageLoaderExtension droid_image_loader_extension
= {
801 .base
= { __DRI_IMAGE_LOADER
, 1 },
803 .getBuffers
= droid_image_get_buffers
,
804 .flushFrontBuffer
= droid_flush_front_buffer
,
808 dri2_initialize_android(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
810 struct dri2_egl_display
*dri2_dpy
;
813 _eglSetLogProc(droid_log
);
815 loader_set_logger(_eglLog
);
817 dri2_dpy
= calloc(1, sizeof(*dri2_dpy
));
819 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
821 dpy
->DriverData
= (void *) dri2_dpy
;
823 dri2_dpy
->fd
= droid_open_device();
824 if (dri2_dpy
->fd
< 0) {
825 err
= "DRI2: failed to open device";
826 goto cleanup_display
;
829 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
, 0);
830 if (dri2_dpy
->driver_name
== NULL
) {
831 err
= "DRI2: failed to get driver name";
835 if (!dri2_load_driver(dpy
)) {
836 err
= "DRI2: failed to load driver";
837 goto cleanup_driver_name
;
840 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
842 /* render nodes cannot use Gem names, and thus do not support
843 * the __DRI_DRI2_LOADER extension */
844 if (!dri2_dpy
->is_render_node
) {
845 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
846 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
847 dri2_dpy
->dri2_loader_extension
.getBuffers
= NULL
;
848 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= droid_flush_front_buffer
;
849 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
850 droid_get_buffers_with_format
;
851 dri2_dpy
->extensions
[0] = &dri2_dpy
->dri2_loader_extension
.base
;
853 dri2_dpy
->extensions
[0] = &droid_image_loader_extension
.base
;
855 dri2_dpy
->extensions
[1] = &use_invalidate
.base
;
856 dri2_dpy
->extensions
[2] = &image_lookup_extension
.base
;
857 dri2_dpy
->extensions
[3] = NULL
;
860 if (!dri2_create_screen(dpy
)) {
861 err
= "DRI2: failed to create screen";
865 if (!droid_add_configs_for_visuals(drv
, dpy
)) {
866 err
= "DRI2: failed to add configs";
870 dpy
->Extensions
.ANDROID_framebuffer_target
= EGL_TRUE
;
871 dpy
->Extensions
.ANDROID_image_native_buffer
= EGL_TRUE
;
872 dpy
->Extensions
.ANDROID_recordable
= EGL_TRUE
;
873 dpy
->Extensions
.KHR_image_base
= EGL_TRUE
;
875 /* Fill vtbl last to prevent accidentally calling virtual function during
878 dri2_dpy
->vtbl
= &droid_display_vtbl
;
883 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
885 dlclose(dri2_dpy
->driver
);
887 free(dri2_dpy
->driver_name
);
893 return _eglError(EGL_NOT_INITIALIZED
, err
);