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.
33 #if ANDROID_VERSION >= 0x402
34 #include <sync/sync.h>
39 #include "egl_dri2_fallbacks.h"
40 #include "gralloc_drm.h"
43 get_format_bpp(int native
)
48 case HAL_PIXEL_FORMAT_RGBA_8888
:
49 case HAL_PIXEL_FORMAT_RGBX_8888
:
50 case HAL_PIXEL_FORMAT_BGRA_8888
:
53 case HAL_PIXEL_FORMAT_RGB_888
:
56 case HAL_PIXEL_FORMAT_RGB_565
:
68 get_native_buffer_name(struct ANativeWindowBuffer
*buf
)
70 return gralloc_drm_get_gem_handle(buf
->handle
);
74 droid_window_dequeue_buffer(struct dri2_egl_surface
*dri2_surf
)
76 #if ANDROID_VERSION >= 0x0402
79 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
,
83 /* If access to the buffer is controlled by a sync fence, then block on the
86 * It may be more performant to postpone blocking until there is an
87 * immediate need to write to the buffer. But doing so would require adding
88 * hooks to the DRI2 loader.
90 * From the ANativeWindow::dequeueBuffer documentation:
92 * The libsync fence file descriptor returned in the int pointed to by
93 * the fenceFd argument will refer to the fence that must signal
94 * before the dequeued buffer may be written to. A value of -1
95 * indicates that the caller may access the buffer immediately without
96 * waiting on a fence. If a valid file descriptor is returned (i.e.
97 * any value except -1) then the caller is responsible for closing the
101 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
103 * Waits indefinitely if timeout < 0.
106 sync_wait(fence_fd
, timeout
);
110 dri2_surf
->buffer
->common
.incRef(&dri2_surf
->buffer
->common
);
112 if (dri2_surf
->window
->dequeueBuffer(dri2_surf
->window
, &dri2_surf
->buffer
))
115 dri2_surf
->buffer
->common
.incRef(&dri2_surf
->buffer
->common
);
116 dri2_surf
->window
->lockBuffer(dri2_surf
->window
, dri2_surf
->buffer
);
123 droid_window_enqueue_buffer(struct dri2_egl_surface
*dri2_surf
)
125 #if ANDROID_VERSION >= 0x0402
126 /* Queue the buffer without a sync fence. This informs the ANativeWindow
127 * that it may access the buffer immediately.
129 * From ANativeWindow::dequeueBuffer:
131 * The fenceFd argument specifies a libsync fence file descriptor for
132 * a fence that must signal before the buffer can be accessed. If
133 * the buffer can be accessed immediately then a value of -1 should
134 * be used. The caller must not use the file descriptor after it
135 * is passed to queueBuffer, and the ANativeWindow implementation
136 * is responsible for closing it.
139 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
,
142 dri2_surf
->window
->queueBuffer(dri2_surf
->window
, dri2_surf
->buffer
);
145 dri2_surf
->buffer
->common
.decRef(&dri2_surf
->buffer
->common
);
146 dri2_surf
->buffer
= NULL
;
152 droid_window_cancel_buffer(struct dri2_egl_surface
*dri2_surf
)
154 /* no cancel buffer? */
155 droid_window_enqueue_buffer(dri2_surf
);
159 droid_alloc_local_buffer(struct dri2_egl_surface
*dri2_surf
,
160 unsigned int att
, unsigned int format
)
162 struct dri2_egl_display
*dri2_dpy
=
163 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
165 if (att
>= ARRAY_SIZE(dri2_surf
->local_buffers
))
168 if (!dri2_surf
->local_buffers
[att
]) {
169 dri2_surf
->local_buffers
[att
] =
170 dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
, att
, format
,
171 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
174 return dri2_surf
->local_buffers
[att
];
178 droid_free_local_buffers(struct dri2_egl_surface
*dri2_surf
)
180 struct dri2_egl_display
*dri2_dpy
=
181 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
184 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->local_buffers
); i
++) {
185 if (dri2_surf
->local_buffers
[i
]) {
186 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
187 dri2_surf
->local_buffers
[i
]);
188 dri2_surf
->local_buffers
[i
] = NULL
;
194 droid_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
195 _EGLConfig
*conf
, void *native_window
,
196 const EGLint
*attrib_list
)
198 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
199 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
200 struct dri2_egl_surface
*dri2_surf
;
201 struct ANativeWindow
*window
= native_window
;
202 const __DRIconfig
*config
;
204 dri2_surf
= calloc(1, sizeof *dri2_surf
);
206 _eglError(EGL_BAD_ALLOC
, "droid_create_surface");
210 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
211 goto cleanup_surface
;
213 if (type
== EGL_WINDOW_BIT
) {
216 if (!window
|| window
->common
.magic
!= ANDROID_NATIVE_WINDOW_MAGIC
) {
217 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
218 goto cleanup_surface
;
220 if (window
->query(window
, NATIVE_WINDOW_FORMAT
, &format
)) {
221 _eglError(EGL_BAD_NATIVE_WINDOW
, "droid_create_surface");
222 goto cleanup_surface
;
225 if (format
!= dri2_conf
->base
.NativeVisualID
) {
226 _eglLog(_EGL_WARNING
, "Native format mismatch: 0x%x != 0x%x",
227 format
, dri2_conf
->base
.NativeVisualID
);
230 window
->query(window
, NATIVE_WINDOW_WIDTH
, &dri2_surf
->base
.Width
);
231 window
->query(window
, NATIVE_WINDOW_HEIGHT
, &dri2_surf
->base
.Height
);
234 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
235 dri2_surf
->base
.GLColorspace
);
237 dri2_surf
->dri_drawable
=
238 (*dri2_dpy
->dri2
->createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
240 if (dri2_surf
->dri_drawable
== NULL
) {
241 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
242 goto cleanup_surface
;
246 window
->common
.incRef(&window
->common
);
247 dri2_surf
->window
= window
;
250 return &dri2_surf
->base
;
259 droid_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
260 _EGLConfig
*conf
, void *native_window
,
261 const EGLint
*attrib_list
)
263 return droid_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
264 native_window
, attrib_list
);
268 droid_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
269 _EGLConfig
*conf
, const EGLint
*attrib_list
)
271 return droid_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
276 droid_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
278 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
279 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
281 if (!_eglPutSurface(surf
))
284 droid_free_local_buffers(dri2_surf
);
286 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
287 if (dri2_surf
->buffer
)
288 droid_window_cancel_buffer(dri2_surf
);
290 dri2_surf
->window
->common
.decRef(&dri2_surf
->window
->common
);
293 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
301 droid_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
303 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
304 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
305 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
308 if (dri2_surf
->base
.Type
!= EGL_WINDOW_BIT
)
311 if (dri2_drv
->glFlush
) {
312 ctx
= _eglGetCurrentContext();
313 if (ctx
&& ctx
->DrawSurface
== &dri2_surf
->base
)
317 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
319 if (dri2_surf
->buffer
)
320 droid_window_enqueue_buffer(dri2_surf
);
322 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
328 dri2_create_image_android_native_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
329 struct ANativeWindowBuffer
*buf
)
331 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
332 struct dri2_egl_image
*dri2_img
;
337 /* From the EGL_ANDROID_image_native_buffer spec:
339 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
340 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
342 _eglError(EGL_BAD_CONTEXT
, "eglCreateEGLImageKHR: for "
343 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
348 if (!buf
|| buf
->common
.magic
!= ANDROID_NATIVE_BUFFER_MAGIC
||
349 buf
->common
.version
!= sizeof(*buf
)) {
350 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
354 name
= get_native_buffer_name(buf
);
356 _eglError(EGL_BAD_PARAMETER
, "eglCreateEGLImageKHR");
360 /* see the table in droid_add_configs_for_visuals */
361 switch (buf
->format
) {
362 case HAL_PIXEL_FORMAT_BGRA_8888
:
363 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
365 case HAL_PIXEL_FORMAT_RGB_565
:
366 format
= __DRI_IMAGE_FORMAT_RGB565
;
368 case HAL_PIXEL_FORMAT_RGBA_8888
:
369 format
= __DRI_IMAGE_FORMAT_ABGR8888
;
371 case HAL_PIXEL_FORMAT_RGBX_8888
:
372 format
= __DRI_IMAGE_FORMAT_XBGR8888
;
374 case HAL_PIXEL_FORMAT_RGB_888
:
377 _eglLog(_EGL_WARNING
, "unsupported native buffer format 0x%x", buf
->format
);
382 dri2_img
= calloc(1, sizeof(*dri2_img
));
384 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
388 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
393 dri2_img
->dri_image
=
394 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
401 if (!dri2_img
->dri_image
) {
403 _eglError(EGL_BAD_ALLOC
, "droid_create_image_mesa_drm");
407 return &dri2_img
->base
;
411 droid_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
412 _EGLContext
*ctx
, EGLenum target
,
413 EGLClientBuffer buffer
, const EGLint
*attr_list
)
416 case EGL_NATIVE_BUFFER_ANDROID
:
417 return dri2_create_image_android_native_buffer(disp
, ctx
,
418 (struct ANativeWindowBuffer
*) buffer
);
420 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
425 droid_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
430 droid_get_buffers_parse_attachments(struct dri2_egl_surface
*dri2_surf
,
431 unsigned int *attachments
, int count
)
433 int num_buffers
= 0, i
;
435 /* fill dri2_surf->buffers */
436 for (i
= 0; i
< count
* 2; i
+= 2) {
437 __DRIbuffer
*buf
, *local
;
439 assert(num_buffers
< ARRAY_SIZE(dri2_surf
->buffers
));
440 buf
= &dri2_surf
->buffers
[num_buffers
];
442 switch (attachments
[i
]) {
443 case __DRI_BUFFER_BACK_LEFT
:
444 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
445 buf
->attachment
= attachments
[i
];
446 buf
->name
= get_native_buffer_name(dri2_surf
->buffer
);
447 buf
->cpp
= get_format_bpp(dri2_surf
->buffer
->format
);
448 buf
->pitch
= dri2_surf
->buffer
->stride
* buf
->cpp
;
456 /* fall through for pbuffers */
457 case __DRI_BUFFER_DEPTH
:
458 case __DRI_BUFFER_STENCIL
:
459 case __DRI_BUFFER_ACCUM
:
460 case __DRI_BUFFER_DEPTH_STENCIL
:
461 case __DRI_BUFFER_HIZ
:
462 local
= droid_alloc_local_buffer(dri2_surf
,
463 attachments
[i
], attachments
[i
+ 1]);
470 case __DRI_BUFFER_FRONT_LEFT
:
471 case __DRI_BUFFER_FRONT_RIGHT
:
472 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
473 case __DRI_BUFFER_FAKE_FRONT_RIGHT
:
474 case __DRI_BUFFER_BACK_RIGHT
:
476 /* no front or right buffers */
485 droid_get_buffers_with_format(__DRIdrawable
* driDrawable
,
486 int *width
, int *height
,
487 unsigned int *attachments
, int count
,
488 int *out_count
, void *loaderPrivate
)
490 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
491 struct dri2_egl_display
*dri2_dpy
=
492 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
495 if (dri2_surf
->base
.Type
== EGL_WINDOW_BIT
) {
496 /* try to dequeue the next back buffer */
497 if (!dri2_surf
->buffer
&& !droid_window_dequeue_buffer(dri2_surf
))
500 /* free outdated buffers and update the surface size */
501 if (dri2_surf
->base
.Width
!= dri2_surf
->buffer
->width
||
502 dri2_surf
->base
.Height
!= dri2_surf
->buffer
->height
) {
503 droid_free_local_buffers(dri2_surf
);
504 dri2_surf
->base
.Width
= dri2_surf
->buffer
->width
;
505 dri2_surf
->base
.Height
= dri2_surf
->buffer
->height
;
509 dri2_surf
->buffer_count
=
510 droid_get_buffers_parse_attachments(dri2_surf
, attachments
, count
);
513 *width
= dri2_surf
->base
.Width
;
515 *height
= dri2_surf
->base
.Height
;
517 *out_count
= dri2_surf
->buffer_count
;;
519 return dri2_surf
->buffers
;
523 droid_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
525 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
528 unsigned int rgba_masks
[4];
530 { HAL_PIXEL_FORMAT_RGBA_8888
, { 0xff, 0xff00, 0xff0000, 0xff000000 } },
531 { HAL_PIXEL_FORMAT_RGBX_8888
, { 0xff, 0xff00, 0xff0000, 0x0 } },
532 { HAL_PIXEL_FORMAT_RGB_888
, { 0xff, 0xff00, 0xff0000, 0x0 } },
533 { HAL_PIXEL_FORMAT_RGB_565
, { 0xf800, 0x7e0, 0x1f, 0x0 } },
534 { HAL_PIXEL_FORMAT_BGRA_8888
, { 0xff0000, 0xff00, 0xff, 0xff000000 } },
535 { 0, 0, { 0, 0, 0, 0 } }
540 for (i
= 0; visuals
[i
].format
; i
++) {
541 int format_count
= 0;
543 for (j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
544 const EGLint surface_type
= EGL_WINDOW_BIT
| EGL_PBUFFER_BIT
;
545 struct dri2_egl_config
*dri2_conf
;
546 unsigned int double_buffered
= 0;
548 dri2_dpy
->core
->getConfigAttrib(dri2_dpy
->driver_configs
[j
],
549 __DRI_ATTRIB_DOUBLE_BUFFER
, &double_buffered
);
551 /* support only double buffered configs */
552 if (!double_buffered
)
555 dri2_conf
= dri2_add_config(dpy
, dri2_dpy
->driver_configs
[j
],
556 count
+ 1, surface_type
, NULL
, visuals
[i
].rgba_masks
);
558 dri2_conf
->base
.NativeVisualID
= visuals
[i
].format
;
559 dri2_conf
->base
.NativeVisualType
= visuals
[i
].format
;
566 _eglLog(_EGL_DEBUG
, "No DRI config supports native format 0x%x",
571 /* post-process configs */
572 for (i
= 0; i
< dpy
->Configs
->Size
; i
++) {
573 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(dpy
->Configs
->Elements
[i
]);
575 /* there is no front buffer so no OpenGL */
576 dri2_conf
->base
.RenderableType
&= ~EGL_OPENGL_BIT
;
577 dri2_conf
->base
.Conformant
&= ~EGL_OPENGL_BIT
;
584 droid_open_device(void)
586 const hw_module_t
*mod
;
589 err
= hw_get_module(GRALLOC_HARDWARE_MODULE_ID
, &mod
);
591 const gralloc_module_t
*gr
= (gralloc_module_t
*) mod
;
595 err
= gr
->perform(gr
, GRALLOC_MODULE_PERFORM_GET_DRM_FD
, &fd
);
598 _eglLog(_EGL_WARNING
, "fail to get drm fd");
602 return (fd
>= 0) ? dup(fd
) : -1;
605 /* support versions < JellyBean */
617 droid_log(EGLint level
, const char *msg
)
630 LOG_FATAL("%s", msg
);
637 static struct dri2_egl_display_vtbl droid_display_vtbl
= {
638 .authenticate
= NULL
,
639 .create_window_surface
= droid_create_window_surface
,
640 .create_pixmap_surface
= dri2_fallback_create_pixmap_surface
,
641 .create_pbuffer_surface
= droid_create_pbuffer_surface
,
642 .destroy_surface
= droid_destroy_surface
,
643 .create_image
= droid_create_image_khr
,
644 .swap_interval
= dri2_fallback_swap_interval
,
645 .swap_buffers
= droid_swap_buffers
,
646 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
647 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
648 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
649 .copy_buffers
= dri2_fallback_copy_buffers
,
650 .query_buffer_age
= dri2_fallback_query_buffer_age
,
651 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
652 .get_sync_values
= dri2_fallback_get_sync_values
,
656 dri2_initialize_android(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
658 struct dri2_egl_display
*dri2_dpy
;
661 _eglSetLogProc(droid_log
);
663 loader_set_logger(_eglLog
);
665 dri2_dpy
= calloc(1, sizeof(*dri2_dpy
));
667 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
669 dpy
->DriverData
= (void *) dri2_dpy
;
671 dri2_dpy
->fd
= droid_open_device();
672 if (dri2_dpy
->fd
< 0) {
673 err
= "DRI2: failed to open device";
674 goto cleanup_display
;
677 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
, 0);
678 if (dri2_dpy
->driver_name
== NULL
) {
679 err
= "DRI2: failed to get driver name";
683 if (!dri2_load_driver(dpy
)) {
684 err
= "DRI2: failed to load driver";
685 goto cleanup_driver_name
;
688 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
689 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
690 dri2_dpy
->dri2_loader_extension
.getBuffers
= NULL
;
691 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= droid_flush_front_buffer
;
692 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
693 droid_get_buffers_with_format
;
695 dri2_dpy
->extensions
[0] = &dri2_dpy
->dri2_loader_extension
.base
;
696 dri2_dpy
->extensions
[1] = &image_lookup_extension
.base
;
697 dri2_dpy
->extensions
[2] = &use_invalidate
.base
;
698 dri2_dpy
->extensions
[3] = NULL
;
700 if (!dri2_create_screen(dpy
)) {
701 err
= "DRI2: failed to create screen";
705 if (!droid_add_configs_for_visuals(drv
, dpy
)) {
706 err
= "DRI2: failed to add configs";
710 dpy
->Extensions
.ANDROID_image_native_buffer
= EGL_TRUE
;
711 dpy
->Extensions
.KHR_image_base
= EGL_TRUE
;
713 /* Fill vtbl last to prevent accidentally calling virtual function during
716 dri2_dpy
->vtbl
= &droid_display_vtbl
;
721 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
723 dlclose(dri2_dpy
->driver
);
725 free(dri2_dpy
->driver_name
);
731 return _eglError(EGL_NOT_INITIALIZED
, err
);