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:
57 case HAL_PIXEL_FORMAT_RGBA_5551:
58 case HAL_PIXEL_FORMAT_RGBA_4444:
70 get_native_buffer_name(struct ANativeWindowBuffer *buf)
72 return gralloc_drm_get_gem_handle(buf->handle);
76 droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf)
78 #if ANDROID_VERSION >= 0x0402
81 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer,
85 /* If access to the buffer is controlled by a sync fence, then block on the
88 * It may be more performant to postpone blocking until there is an
89 * immediate need to write to the buffer. But doing so would require adding
90 * hooks to the DRI2 loader.
92 * From the ANativeWindow::dequeueBuffer documentation:
94 * The libsync fence file descriptor returned in the int pointed to by
95 * the fenceFd argument will refer to the fence that must signal
96 * before the dequeued buffer may be written to. A value of -1
97 * indicates that the caller may access the buffer immediately without
98 * waiting on a fence. If a valid file descriptor is returned (i.e.
99 * any value except -1) then the caller is responsible for closing the
103 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
105 * Waits indefinitely if timeout < 0.
108 sync_wait(fence_fd, timeout);
112 dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
114 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer))
117 dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
118 dri2_surf->window->lockBuffer(dri2_surf->window, dri2_surf->buffer);
125 droid_window_enqueue_buffer(struct dri2_egl_surface *dri2_surf)
127 #if ANDROID_VERSION >= 0x0402
128 /* Queue the buffer without a sync fence. This informs the ANativeWindow
129 * that it may access the buffer immediately.
131 * From ANativeWindow::dequeueBuffer:
133 * The fenceFd argument specifies a libsync fence file descriptor for
134 * a fence that must signal before the buffer can be accessed. If
135 * the buffer can be accessed immediately then a value of -1 should
136 * be used. The caller must not use the file descriptor after it
137 * is passed to queueBuffer, and the ANativeWindow implementation
138 * is responsible for closing it.
141 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer,
144 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer);
147 dri2_surf->buffer->common.decRef(&dri2_surf->buffer->common);
148 dri2_surf->buffer = NULL;
154 droid_window_cancel_buffer(struct dri2_egl_surface *dri2_surf)
156 /* no cancel buffer? */
157 droid_window_enqueue_buffer(dri2_surf);
161 droid_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,
162 unsigned int att, unsigned int format)
164 struct dri2_egl_display *dri2_dpy =
165 dri2_egl_display(dri2_surf->base.Resource.Display);
167 if (att >= ARRAY_SIZE(dri2_surf->local_buffers))
170 if (!dri2_surf->local_buffers[att]) {
171 dri2_surf->local_buffers[att] =
172 dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, att, format,
173 dri2_surf->base.Width, dri2_surf->base.Height);
176 return dri2_surf->local_buffers[att];
180 droid_free_local_buffers(struct dri2_egl_surface *dri2_surf)
182 struct dri2_egl_display *dri2_dpy =
183 dri2_egl_display(dri2_surf->base.Resource.Display);
186 for (i = 0; i < ARRAY_SIZE(dri2_surf->local_buffers); i++) {
187 if (dri2_surf->local_buffers[i]) {
188 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
189 dri2_surf->local_buffers[i]);
190 dri2_surf->local_buffers[i] = NULL;
196 droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
197 _EGLConfig *conf, EGLNativeWindowType window,
198 const EGLint *attrib_list)
200 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
201 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
202 struct dri2_egl_surface *dri2_surf;
203 dri2_surf = calloc(1, sizeof *dri2_surf);
205 _eglError(EGL_BAD_ALLOC, "droid_create_surface");
209 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
210 goto cleanup_surface;
212 if (type == EGL_WINDOW_BIT) {
215 if (!window || window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
216 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
217 goto cleanup_surface;
219 if (window->query(window, NATIVE_WINDOW_FORMAT, &format)) {
220 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
221 goto cleanup_surface;
224 if (format != dri2_conf->base.NativeVisualID) {
225 _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x",
226 format, dri2_conf->base.NativeVisualID);
229 window->query(window, NATIVE_WINDOW_WIDTH, &dri2_surf->base.Width);
230 window->query(window, NATIVE_WINDOW_HEIGHT, &dri2_surf->base.Height);
233 dri2_surf->dri_drawable =
234 (*dri2_dpy->dri2->createNewDrawable)(dri2_dpy->dri_screen,
235 dri2_conf->dri_double_config,
237 if (dri2_surf->dri_drawable == NULL) {
238 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
239 goto cleanup_surface;
243 window->common.incRef(&window->common);
244 dri2_surf->window = window;
247 return &dri2_surf->base;
256 droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
257 _EGLConfig *conf, EGLNativeWindowType window,
258 const EGLint *attrib_list)
260 return droid_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
261 window, attrib_list);
265 droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
266 _EGLConfig *conf, const EGLint *attrib_list)
268 return droid_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
273 droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
275 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
276 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
278 if (!_eglPutSurface(surf))
281 droid_free_local_buffers(dri2_surf);
283 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
284 if (dri2_surf->buffer)
285 droid_window_cancel_buffer(dri2_surf);
287 dri2_surf->window->common.decRef(&dri2_surf->window->common);
290 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
298 droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
300 struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
301 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
302 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
305 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
308 if (dri2_drv->glFlush) {
309 ctx = _eglGetCurrentContext();
310 if (ctx && ctx->DrawSurface == &dri2_surf->base)
314 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
316 if (dri2_surf->buffer)
317 droid_window_enqueue_buffer(dri2_surf);
319 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
325 dri2_create_image_android_native_buffer(_EGLDisplay *disp, _EGLContext *ctx,
326 struct ANativeWindowBuffer *buf)
328 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
329 struct dri2_egl_image *dri2_img;
334 /* From the EGL_ANDROID_image_native_buffer spec:
336 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
337 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
339 _eglError(EGL_BAD_CONTEXT, "eglCreateEGLImageKHR: for "
340 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
345 if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC ||
346 buf->common.version != sizeof(*buf)) {
347 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
351 name = get_native_buffer_name(buf);
353 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
357 /* see the table in droid_add_configs_for_visuals */
358 switch (buf->format) {
359 case HAL_PIXEL_FORMAT_BGRA_8888:
360 format = __DRI_IMAGE_FORMAT_ARGB8888;
362 case HAL_PIXEL_FORMAT_RGB_565:
363 format = __DRI_IMAGE_FORMAT_RGB565;
365 case HAL_PIXEL_FORMAT_RGBA_8888:
366 format = __DRI_IMAGE_FORMAT_ABGR8888;
368 case HAL_PIXEL_FORMAT_RGBX_8888:
369 format = __DRI_IMAGE_FORMAT_XBGR8888;
371 case HAL_PIXEL_FORMAT_RGB_888:
372 case HAL_PIXEL_FORMAT_RGBA_5551:
373 case HAL_PIXEL_FORMAT_RGBA_4444:
376 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", buf->format);
381 dri2_img = calloc(1, sizeof(*dri2_img));
383 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
387 if (!_eglInitImage(&dri2_img->base, disp)) {
392 dri2_img->dri_image =
393 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
400 if (!dri2_img->dri_image) {
402 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
406 return &dri2_img->base;
410 droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
411 _EGLContext *ctx, EGLenum target,
412 EGLClientBuffer buffer, const EGLint *attr_list)
415 case EGL_NATIVE_BUFFER_ANDROID:
416 return dri2_create_image_android_native_buffer(disp, ctx,
417 (struct ANativeWindowBuffer *) buffer);
419 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
424 droid_init_driver_functions(_EGLDriver *drv)
426 drv->API.CreateImageKHR = droid_create_image_khr;
430 droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
435 droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf,
436 unsigned int *attachments, int count)
438 int num_buffers = 0, i;
440 /* fill dri2_surf->buffers */
441 for (i = 0; i < count * 2; i += 2) {
442 __DRIbuffer *buf, *local;
444 assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers));
445 buf = &dri2_surf->buffers[num_buffers];
447 switch (attachments[i]) {
448 case __DRI_BUFFER_BACK_LEFT:
449 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
450 buf->attachment = attachments[i];
451 buf->name = get_native_buffer_name(dri2_surf->buffer);
452 buf->cpp = get_format_bpp(dri2_surf->buffer->format);
453 buf->pitch = dri2_surf->buffer->stride * buf->cpp;
461 /* fall through for pbuffers */
462 case __DRI_BUFFER_DEPTH:
463 case __DRI_BUFFER_STENCIL:
464 case __DRI_BUFFER_ACCUM:
465 case __DRI_BUFFER_DEPTH_STENCIL:
466 case __DRI_BUFFER_HIZ:
467 local = droid_alloc_local_buffer(dri2_surf,
468 attachments[i], attachments[i + 1]);
475 case __DRI_BUFFER_FRONT_LEFT:
476 case __DRI_BUFFER_FRONT_RIGHT:
477 case __DRI_BUFFER_FAKE_FRONT_LEFT:
478 case __DRI_BUFFER_FAKE_FRONT_RIGHT:
479 case __DRI_BUFFER_BACK_RIGHT:
481 /* no front or right buffers */
490 droid_get_buffers_with_format(__DRIdrawable * driDrawable,
491 int *width, int *height,
492 unsigned int *attachments, int count,
493 int *out_count, void *loaderPrivate)
495 struct dri2_egl_surface *dri2_surf = loaderPrivate;
496 struct dri2_egl_display *dri2_dpy =
497 dri2_egl_display(dri2_surf->base.Resource.Display);
500 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
501 /* try to dequeue the next back buffer */
502 if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf))
505 /* free outdated buffers and update the surface size */
506 if (dri2_surf->base.Width != dri2_surf->buffer->width ||
507 dri2_surf->base.Height != dri2_surf->buffer->height) {
508 droid_free_local_buffers(dri2_surf);
509 dri2_surf->base.Width = dri2_surf->buffer->width;
510 dri2_surf->base.Height = dri2_surf->buffer->height;
514 dri2_surf->buffer_count =
515 droid_get_buffers_parse_attachments(dri2_surf, attachments, count);
518 *width = dri2_surf->base.Width;
520 *height = dri2_surf->base.Height;
522 *out_count = dri2_surf->buffer_count;;
524 return dri2_surf->buffers;
528 droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
530 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
533 unsigned int rgba_masks[4];
535 { HAL_PIXEL_FORMAT_RGBA_8888, { 0xff, 0xff00, 0xff0000, 0xff000000 } },
536 { HAL_PIXEL_FORMAT_RGBX_8888, { 0xff, 0xff00, 0xff0000, 0x0 } },
537 { HAL_PIXEL_FORMAT_RGB_888, { 0xff, 0xff00, 0xff0000, 0x0 } },
538 { HAL_PIXEL_FORMAT_RGB_565, { 0xf800, 0x7e0, 0x1f, 0x0 } },
539 { HAL_PIXEL_FORMAT_BGRA_8888, { 0xff0000, 0xff00, 0xff, 0xff000000 } },
540 { 0, 0, { 0, 0, 0, 0 } }
545 for (i = 0; visuals[i].format; i++) {
546 int format_count = 0;
548 for (j = 0; dri2_dpy->driver_configs[j]; j++) {
549 const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
550 struct dri2_egl_config *dri2_conf;
551 unsigned int double_buffered = 0;
553 dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[j],
554 __DRI_ATTRIB_DOUBLE_BUFFER, &double_buffered);
556 /* support only double buffered configs */
557 if (!double_buffered)
560 dri2_conf = dri2_add_config(dpy, dri2_dpy->driver_configs[j],
561 count + 1, surface_type, NULL, visuals[i].rgba_masks);
563 dri2_conf->base.NativeVisualID = visuals[i].format;
564 dri2_conf->base.NativeVisualType = visuals[i].format;
571 _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
576 /* post-process configs */
577 for (i = 0; i < dpy->Configs->Size; i++) {
578 struct dri2_egl_config *dri2_conf = dri2_egl_config(dpy->Configs->Elements[i]);
580 /* there is no front buffer so no OpenGL */
581 dri2_conf->base.RenderableType &= ~EGL_OPENGL_BIT;
582 dri2_conf->base.Conformant &= ~EGL_OPENGL_BIT;
589 droid_open_device(void)
591 const hw_module_t *mod;
594 err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &mod);
596 const gralloc_module_t *gr = (gralloc_module_t *) mod;
600 err = gr->perform(gr, GRALLOC_MODULE_PERFORM_GET_DRM_FD, &fd);
603 _eglLog(_EGL_WARNING, "fail to get drm fd");
607 return (fd >= 0) ? dup(fd) : -1;
610 /* support versions < JellyBean */
622 droid_log(EGLint level, const char *msg)
635 LOG_FATAL("%s", msg);
642 static struct dri2_egl_display_vtbl droid_display_vtbl = {
643 .authenticate = NULL,
644 .create_window_surface = droid_create_window_surface,
645 .create_pixmap_surface = dri2_fallback_pixmap_surface,
646 .create_pbuffer_surface = droid_create_pbuffer_surface,
647 .destroy_surface = droid_destroy_surface,
648 .swap_interval = dri2_fallback_swap_interval,
649 .swap_buffers = droid_swap_buffers,
650 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
651 .swap_buffers_region = dri2_fallback_swap_buffers_region,
652 .post_sub_buffer = dri2_fallback_post_sub_buffer,
653 .copy_buffers = dri2_fallback_copy_buffers,
654 .query_buffer_age = dri2_fallback_query_buffer_age,
655 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
659 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
661 struct dri2_egl_display *dri2_dpy;
664 _eglSetLogProc(droid_log);
666 loader_set_logger(_eglLog);
668 dri2_dpy = calloc(1, sizeof(*dri2_dpy));
670 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
672 dpy->DriverData = (void *) dri2_dpy;
674 dri2_dpy->fd = droid_open_device();
675 if (dri2_dpy->fd < 0) {
676 err = "DRI2: failed to open device";
677 goto cleanup_display;
680 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd, 0);
681 if (dri2_dpy->driver_name == NULL) {
682 err = "DRI2: failed to get driver name";
686 if (!dri2_load_driver(dpy)) {
687 err = "DRI2: failed to load driver";
688 goto cleanup_driver_name;
691 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
692 dri2_dpy->dri2_loader_extension.base.version = 3;
693 dri2_dpy->dri2_loader_extension.getBuffers = NULL;
694 dri2_dpy->dri2_loader_extension.flushFrontBuffer = droid_flush_front_buffer;
695 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
696 droid_get_buffers_with_format;
698 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
699 dri2_dpy->extensions[1] = &image_lookup_extension.base;
700 dri2_dpy->extensions[2] = &use_invalidate.base;
701 dri2_dpy->extensions[3] = NULL;
703 if (!dri2_create_screen(dpy)) {
704 err = "DRI2: failed to create screen";
708 if (!droid_add_configs_for_visuals(drv, dpy)) {
709 err = "DRI2: failed to add configs";
713 dpy->Extensions.ANDROID_image_native_buffer = EGL_TRUE;
714 dpy->Extensions.KHR_image_base = EGL_TRUE;
716 /* we're supporting EGL 1.4 */
717 dpy->VersionMajor = 1;
718 dpy->VersionMinor = 4;
720 droid_init_driver_functions(drv);
722 /* Fill vtbl last to prevent accidentally calling virtual function during
725 dri2_dpy->vtbl = &droid_display_vtbl;
730 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
732 dlclose(dri2_dpy->driver);
734 free(dri2_dpy->driver_name);
740 return _eglError(EGL_NOT_INITIALIZED, err);