egl/dri2: Dispatch API.QueryBufferAge by display, not driver
[mesa.git] / src / egl / drivers / dri2 / platform_android.c
index 284dc3a661b026032908c2c35503b6e0b501760f..69de23e2cb4b9b4d1aa9d1afa3088d4afaae69e1 100644 (file)
@@ -1,6 +1,5 @@
 /*
  * Mesa 3-D graphics library
- * Version:  7.12
  *
  * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
  * Copyright (C) 2010-2011 LunarG Inc.
 #include <errno.h>
 #include <dlfcn.h>
 
-/* for droid_get_pci_id */
-#include <xf86drm.h>
-#include <i915_drm.h>
-#include <radeon_drm.h>
+#if ANDROID_VERSION >= 0x402
+#include <sync/sync.h>
+#endif
 
+#include "loader.h"
 #include "egl_dri2.h"
+#include "egl_dri2_fallbacks.h"
+#include "gralloc_drm.h"
 
 static int
 get_format_bpp(int native)
@@ -66,24 +67,56 @@ get_format_bpp(int native)
 }
 
 static int
-get_native_buffer_name(struct android_native_buffer_t *buf)
+get_native_buffer_name(struct ANativeWindowBuffer *buf)
 {
-   struct gralloc_drm_handle_t *handle;
-
-   /* check that the buffer is allocated by drm_gralloc and cast */
-   handle = gralloc_drm_handle(buf->handle);
-
-   return (handle) ? handle->name : 0;
+   return gralloc_drm_get_gem_handle(buf->handle);
 }
 
 static EGLBoolean
 droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf)
 {
+#if ANDROID_VERSION >= 0x0402
+   int fence_fd;
+
+   if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer,
+                                        &fence_fd))
+      return EGL_FALSE;
+
+   /* If access to the buffer is controlled by a sync fence, then block on the
+    * fence.
+    *
+    * It may be more performant to postpone blocking until there is an
+    * immediate need to write to the buffer. But doing so would require adding
+    * hooks to the DRI2 loader.
+    *
+    * From the ANativeWindow::dequeueBuffer documentation:
+    *
+    *    The libsync fence file descriptor returned in the int pointed to by
+    *    the fenceFd argument will refer to the fence that must signal
+    *    before the dequeued buffer may be written to.  A value of -1
+    *    indicates that the caller may access the buffer immediately without
+    *    waiting on a fence.  If a valid file descriptor is returned (i.e.
+    *    any value except -1) then the caller is responsible for closing the
+    *    file descriptor.
+    */
+    if (fence_fd >= 0) {
+       /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
+        *
+        *    Waits indefinitely if timeout < 0.
+        */
+        int timeout = -1;
+        sync_wait(fence_fd, timeout);
+        close(fence_fd);
+   }
+
+   dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
+#else
    if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer))
       return EGL_FALSE;
 
    dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
    dri2_surf->window->lockBuffer(dri2_surf->window, dri2_surf->buffer);
+#endif
 
    return EGL_TRUE;
 }
@@ -91,7 +124,25 @@ droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf)
 static EGLBoolean
 droid_window_enqueue_buffer(struct dri2_egl_surface *dri2_surf)
 {
+#if ANDROID_VERSION >= 0x0402
+   /* Queue the buffer without a sync fence. This informs the ANativeWindow
+    * that it may access the buffer immediately.
+    *
+    * From ANativeWindow::dequeueBuffer:
+    *
+    *    The fenceFd argument specifies a libsync fence file descriptor for
+    *    a fence that must signal before the buffer can be accessed.  If
+    *    the buffer can be accessed immediately then a value of -1 should
+    *    be used.  The caller must not use the file descriptor after it
+    *    is passed to queueBuffer, and the ANativeWindow implementation
+    *    is responsible for closing it.
+    */
+   int fence_fd = -1;
+   dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer,
+                                  fence_fd);
+#else
    dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer);
+#endif
 
    dri2_surf->buffer->common.decRef(&dri2_surf->buffer->common);
    dri2_surf->buffer = NULL;
@@ -149,23 +200,6 @@ droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
    struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
    struct dri2_egl_surface *dri2_surf;
-   int format;
-
-   (void) drv;
-
-   if (!window || window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
-      _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
-      return NULL;
-   }
-   if (window->query(window, NATIVE_WINDOW_FORMAT, &format)) {
-      _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
-      return NULL;
-   }
-   if (format != dri2_conf->base.NativeVisualID) {
-      _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x",
-            format, dri2_conf->base.NativeVisualID);
-   }
-
    dri2_surf = calloc(1, sizeof *dri2_surf);
    if (!dri2_surf) {
       _eglError(EGL_BAD_ALLOC, "droid_create_surface");
@@ -173,7 +207,28 @@ droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
    }
 
    if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
-      goto cleanup_surf;
+      goto cleanup_surface;
+
+   if (type == EGL_WINDOW_BIT) {
+      int format;
+
+      if (!window || window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
+         _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
+         goto cleanup_surface;
+      }
+      if (window->query(window, NATIVE_WINDOW_FORMAT, &format)) {
+         _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
+         goto cleanup_surface;
+      }
+
+      if (format != dri2_conf->base.NativeVisualID) {
+         _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x",
+               format, dri2_conf->base.NativeVisualID);
+      }
+
+      window->query(window, NATIVE_WINDOW_WIDTH, &dri2_surf->base.Width);
+      window->query(window, NATIVE_WINDOW_HEIGHT, &dri2_surf->base.Height);
+   }
 
    dri2_surf->dri_drawable =
       (*dri2_dpy->dri2->createNewDrawable)(dri2_dpy->dri_screen,
@@ -181,18 +236,17 @@ droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
                                            dri2_surf);
    if (dri2_surf->dri_drawable == NULL) {
       _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
-      goto cleanup_surf;
+      goto cleanup_surface;
    }
 
-   window->common.incRef(&window->common);
-   window->query(window, NATIVE_WINDOW_WIDTH, &dri2_surf->base.Width);
-   window->query(window, NATIVE_WINDOW_HEIGHT, &dri2_surf->base.Height);
-
-   dri2_surf->window = window;
+   if (window) {
+      window->common.incRef(&window->common);
+      dri2_surf->window = window;
+   }
 
    return &dri2_surf->base;
 
-cleanup_surf:
+cleanup_surface:
    free(dri2_surf);
 
    return NULL;
@@ -207,19 +261,12 @@ droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
                              window, attrib_list);
 }
 
-static _EGLSurface *
-droid_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
-                          _EGLConfig *conf, EGLNativePixmapType pixmap,
-                          const EGLint *attrib_list)
-{
-   return NULL;
-}
-
 static _EGLSurface *
 droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
                            _EGLConfig *conf, const EGLint *attrib_list)
 {
-   return NULL;
+   return droid_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
+                             NULL, attrib_list);
 }
 
 static EGLBoolean
@@ -233,10 +280,12 @@ droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
 
    droid_free_local_buffers(dri2_surf);
 
-   if (dri2_surf->buffer)
-      droid_window_cancel_buffer(dri2_surf);
+   if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
+      if (dri2_surf->buffer)
+         droid_window_cancel_buffer(dri2_surf);
 
-   dri2_surf->window->common.decRef(&dri2_surf->window->common);
+      dri2_surf->window->common.decRef(&dri2_surf->window->common);
+   }
 
    (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
 
@@ -253,6 +302,9 @@ droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
    _EGLContext *ctx;
 
+   if (dri2_surf->base.Type != EGL_WINDOW_BIT)
+      return EGL_TRUE;
+
    if (dri2_drv->glFlush) {
       ctx = _eglGetCurrentContext();
       if (ctx && ctx->DrawSurface == &dri2_surf->base)
@@ -270,14 +322,26 @@ droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
 }
 
 static _EGLImage *
-dri2_create_image_android_native_buffer(_EGLDisplay *disp,
-                                        struct android_native_buffer_t *buf)
+dri2_create_image_android_native_buffer(_EGLDisplay *disp, _EGLContext *ctx,
+                                        struct ANativeWindowBuffer *buf)
 {
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
    struct dri2_egl_image *dri2_img;
    int name;
    EGLint format;
 
+   if (ctx != NULL) {
+      /* From the EGL_ANDROID_image_native_buffer spec:
+       *
+       *     * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
+       *       EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
+       */
+      _eglError(EGL_BAD_CONTEXT, "eglCreateEGLImageKHR: for "
+                "EGL_NATIVE_BUFFER_ANDROID, the context must be "
+                "EGL_NO_CONTEXT");
+      return NULL;
+   }
+
    if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC ||
        buf->common.version != sizeof(*buf)) {
       _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
@@ -299,7 +363,11 @@ dri2_create_image_android_native_buffer(_EGLDisplay *disp,
       format = __DRI_IMAGE_FORMAT_RGB565;
       break;
    case HAL_PIXEL_FORMAT_RGBA_8888:
+      format = __DRI_IMAGE_FORMAT_ABGR8888;
+      break;
    case HAL_PIXEL_FORMAT_RGBX_8888:
+      format = __DRI_IMAGE_FORMAT_XBGR8888;
+      break;
    case HAL_PIXEL_FORMAT_RGB_888:
    case HAL_PIXEL_FORMAT_RGBA_5551:
    case HAL_PIXEL_FORMAT_RGBA_4444:
@@ -345,8 +413,8 @@ droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
 {
    switch (target) {
    case EGL_NATIVE_BUFFER_ANDROID:
-      return dri2_create_image_android_native_buffer(disp,
-            (struct android_native_buffer_t *) buffer);
+      return dri2_create_image_android_native_buffer(disp, ctx,
+            (struct ANativeWindowBuffer *) buffer);
    default:
       return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
    }
@@ -355,12 +423,6 @@ droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
 static void
 droid_init_driver_functions(_EGLDriver *drv)
 {
-   drv->API.CreateWindowSurface = droid_create_window_surface;
-   drv->API.CreatePixmapSurface = droid_create_pixmap_surface;
-   drv->API.CreatePbufferSurface = droid_create_pbuffer_surface;
-   drv->API.DestroySurface = droid_destroy_surface;
-   drv->API.SwapBuffers = droid_swap_buffers;
-
    drv->API.CreateImageKHR = droid_create_image_khr;
 }
 
@@ -369,48 +431,34 @@ droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
 {
 }
 
-static __DRIbuffer *
-droid_get_buffers_with_format(__DRIdrawable * driDrawable,
-                            int *width, int *height,
-                            unsigned int *attachments, int count,
-                            int *out_count, void *loaderPrivate)
+static int
+droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf,
+                                    unsigned int *attachments, int count)
 {
-   struct dri2_egl_surface *dri2_surf = loaderPrivate;
-   struct dri2_egl_display *dri2_dpy =
-      dri2_egl_display(dri2_surf->base.Resource.Display);
-   int i;
+   int num_buffers = 0, i;
 
-   if (!dri2_surf->buffer) {
-      if (!droid_window_dequeue_buffer(dri2_surf))
-         return NULL;
-   }
-
-   /* free outdated buffers and update the surface size */
-   if (dri2_surf->base.Width != dri2_surf->buffer->width ||
-       dri2_surf->base.Height != dri2_surf->buffer->height) {
-      droid_free_local_buffers(dri2_surf);
-      dri2_surf->base.Width = dri2_surf->buffer->width;
-      dri2_surf->base.Height = dri2_surf->buffer->height;
-   }
-
-   dri2_surf->buffer_count = 0;
+   /* fill dri2_surf->buffers */
    for (i = 0; i < count * 2; i += 2) {
       __DRIbuffer *buf, *local;
 
-      assert(dri2_surf->buffer_count < ARRAY_SIZE(dri2_surf->buffers));
-      buf = &dri2_surf->buffers[dri2_surf->buffer_count];
+      assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers));
+      buf = &dri2_surf->buffers[num_buffers];
 
       switch (attachments[i]) {
       case __DRI_BUFFER_BACK_LEFT:
-         buf->attachment = attachments[i];
-         buf->name = get_native_buffer_name(dri2_surf->buffer);
-         buf->cpp = get_format_bpp(dri2_surf->buffer->format);
-         buf->pitch = dri2_surf->buffer->stride * buf->cpp;
-         buf->flags = 0;
-
-         if (buf->name)
-            dri2_surf->buffer_count++;
-         break;
+         if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
+            buf->attachment = attachments[i];
+            buf->name = get_native_buffer_name(dri2_surf->buffer);
+            buf->cpp = get_format_bpp(dri2_surf->buffer->format);
+            buf->pitch = dri2_surf->buffer->stride * buf->cpp;
+            buf->flags = 0;
+
+            if (buf->name)
+               num_buffers++;
+
+            break;
+         }
+         /* fall through for pbuffers */
       case __DRI_BUFFER_DEPTH:
       case __DRI_BUFFER_STENCIL:
       case __DRI_BUFFER_ACCUM:
@@ -421,7 +469,7 @@ droid_get_buffers_with_format(__DRIdrawable * driDrawable,
 
          if (local) {
             *buf = *local;
-            dri2_surf->buffer_count++;
+            num_buffers++;
          }
          break;
       case __DRI_BUFFER_FRONT_LEFT:
@@ -435,6 +483,37 @@ droid_get_buffers_with_format(__DRIdrawable * driDrawable,
       }
    }
 
+   return num_buffers;
+}
+
+static __DRIbuffer *
+droid_get_buffers_with_format(__DRIdrawable * driDrawable,
+                            int *width, int *height,
+                            unsigned int *attachments, int count,
+                            int *out_count, void *loaderPrivate)
+{
+   struct dri2_egl_surface *dri2_surf = loaderPrivate;
+   struct dri2_egl_display *dri2_dpy =
+      dri2_egl_display(dri2_surf->base.Resource.Display);
+   int i;
+
+   if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
+      /* try to dequeue the next back buffer */
+      if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf))
+         return NULL;
+
+      /* free outdated buffers and update the surface size */
+      if (dri2_surf->base.Width != dri2_surf->buffer->width ||
+          dri2_surf->base.Height != dri2_surf->buffer->height) {
+         droid_free_local_buffers(dri2_surf);
+         dri2_surf->base.Width = dri2_surf->buffer->width;
+         dri2_surf->base.Height = dri2_surf->buffer->height;
+      }
+   }
+
+   dri2_surf->buffer_count =
+      droid_get_buffers_parse_attachments(dri2_surf, attachments, count);
+
    if (width)
       *width = dri2_surf->base.Width;
    if (height)
@@ -451,14 +530,13 @@ droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
    const struct {
       int format;
-      int size;
       unsigned int rgba_masks[4];
    } visuals[] = {
-      { HAL_PIXEL_FORMAT_RGBA_8888, 32, { 0xff, 0xff00, 0xff0000, 0xff000000 } },
-      { HAL_PIXEL_FORMAT_RGBX_8888, 32, { 0xff, 0xff00, 0xff0000, 0x0 } },
-      { HAL_PIXEL_FORMAT_RGB_888,   24, { 0xff, 0xff00, 0xff0000, 0x0 } },
-      { HAL_PIXEL_FORMAT_RGB_565,   16, { 0xf800, 0x7e0, 0x1f, 0x0 } },
-      { HAL_PIXEL_FORMAT_BGRA_8888, 32, { 0xff0000, 0xff00, 0xff, 0xff000000 } },
+      { HAL_PIXEL_FORMAT_RGBA_8888, { 0xff, 0xff00, 0xff0000, 0xff000000 } },
+      { HAL_PIXEL_FORMAT_RGBX_8888, { 0xff, 0xff00, 0xff0000, 0x0 } },
+      { HAL_PIXEL_FORMAT_RGB_888,   { 0xff, 0xff00, 0xff0000, 0x0 } },
+      { HAL_PIXEL_FORMAT_RGB_565,   { 0xf800, 0x7e0, 0x1f, 0x0 } },
+      { HAL_PIXEL_FORMAT_BGRA_8888, { 0xff0000, 0xff00, 0xff, 0xff000000 } },
       { 0, 0, { 0, 0, 0, 0 } }
    };
    int count, i, j;
@@ -468,11 +546,19 @@ droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
       int format_count = 0;
 
       for (j = 0; dri2_dpy->driver_configs[j]; j++) {
+         const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
          struct dri2_egl_config *dri2_conf;
+         unsigned int double_buffered = 0;
+
+         dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[j],
+            __DRI_ATTRIB_DOUBLE_BUFFER, &double_buffered);
+
+         /* support only double buffered configs */
+         if (!double_buffered)
+            continue;
 
          dri2_conf = dri2_add_config(dpy, dri2_dpy->driver_configs[j],
-               count + 1, visuals[i].size, EGL_WINDOW_BIT, NULL,
-               visuals[i].rgba_masks);
+               count + 1, surface_type, NULL, visuals[i].rgba_masks);
          if (dri2_conf) {
             dri2_conf->base.NativeVisualID = visuals[i].format;
             dri2_conf->base.NativeVisualType = visuals[i].format;
@@ -494,112 +580,11 @@ droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
       /* there is no front buffer so no OpenGL */
       dri2_conf->base.RenderableType &= ~EGL_OPENGL_BIT;
       dri2_conf->base.Conformant &= ~EGL_OPENGL_BIT;
-
-      /* and we want to make sure GL_DRAW_BUFFER is always GL_BACK */
-      if (!dri2_conf->dri_double_config)
-         dri2_conf->base.SurfaceType &= ~EGL_WINDOW_BIT;
    }
 
    return (count != 0);
 }
 
-static EGLBoolean
-droid_get_pci_id(int fd, int *vendor_id, int *chip_id)
-{
-   drmVersionPtr version;
-
-   *chip_id = -1;
-
-   version = drmGetVersion(fd);
-   if (!version) {
-      _eglLog(_EGL_WARNING, "invalid drm fd");
-      return EGL_FALSE;
-   }
-   if (!version->name) {
-      _eglLog(_EGL_WARNING, "unable to determine the driver name");
-      drmFreeVersion(version);
-      return EGL_FALSE;
-   }
-
-   if (strcmp(version->name, "i915") == 0) {
-      struct drm_i915_getparam gp;
-      int ret;
-
-      *vendor_id = 0x8086;
-
-      memset(&gp, 0, sizeof(gp));
-      gp.param = I915_PARAM_CHIPSET_ID;
-      gp.value = chip_id;
-      ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
-      if (ret) {
-         _eglLog(_EGL_WARNING, "failed to get param for i915");
-        *chip_id = -1;
-      }
-   }
-   else if (strcmp(version->name, "radeon") == 0) {
-      struct drm_radeon_info info;
-      int ret;
-
-      *vendor_id = 0x1002;
-
-      memset(&info, 0, sizeof(info));
-      info.request = RADEON_INFO_DEVICE_ID;
-      info.value = (unsigned long) chip_id;
-      ret = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
-      if (ret) {
-         _eglLog(_EGL_WARNING, "failed to get info for radeon");
-        *chip_id = -1;
-      }
-   }
-   else if (strcmp(version->name, "nouveau") == 0) {
-      *vendor_id = 0x10de;
-      /* not used */
-      *chip_id = 0;
-   }
-   else if (strcmp(version->name, "vmwgfx") == 0) {
-      *vendor_id = 0x15ad;
-      /* assume SVGA II */
-      *chip_id = 0x0405;
-   }
-
-   drmFreeVersion(version);
-
-   return (*chip_id >= 0);
-}
-
-#define DRIVER_MAP_DRI2_ONLY
-#include "pci_ids/pci_id_driver_map.h"
-static const char *
-droid_get_driver_name(int fd)
-{
-   int vendor_id = -1, chip_id = -1;
-   int idx, i;
-   char *name;
-
-   if (!droid_get_pci_id(fd, &vendor_id, &chip_id))
-      return NULL;
-
-   for (idx = 0; driver_map[idx].driver; idx++) {
-      if (vendor_id != driver_map[idx].vendor_id)
-         continue;
-
-      if (driver_map[idx].num_chips_ids == -1)
-         break;
-
-      for (i = 0; i < driver_map[idx].num_chips_ids; i++) {
-         if (driver_map[idx].chip_ids[i] == chip_id)
-            break;
-      }
-      if (i < driver_map[idx].num_chips_ids)
-             break;
-   }
-
-   _eglLog(_EGL_INFO, "pci id for fd %d: %04x:%04x, driver %s",
-         fd, vendor_id, chip_id, driver_map[idx].driver);
-
-   return driver_map[idx].driver;
-}
-
 static int
 droid_open_device(void)
 {
@@ -622,18 +607,29 @@ droid_open_device(void)
    return (fd >= 0) ? dup(fd) : -1;
 }
 
+/* support versions < JellyBean */
+#ifndef ALOGW
+#define ALOGW LOGW
+#endif
+#ifndef ALOGD
+#define ALOGD LOGD
+#endif
+#ifndef ALOGI
+#define ALOGI LOGI
+#endif
+
 static void
 droid_log(EGLint level, const char *msg)
 {
    switch (level) {
    case _EGL_DEBUG:
-      LOGD("%s", msg);
+      ALOGD("%s", msg);
       break;
    case _EGL_INFO:
-      LOGI("%s", msg);
+      ALOGI("%s", msg);
       break;
    case _EGL_WARNING:
-      LOGW("%s", msg);
+      ALOGW("%s", msg);
       break;
    case _EGL_FATAL:
       LOG_FATAL("%s", msg);
@@ -643,6 +639,18 @@ droid_log(EGLint level, const char *msg)
    }
 }
 
+static struct dri2_egl_display_vtbl droid_display_vtbl = {
+   .authenticate = NULL,
+   .create_window_surface = droid_create_window_surface,
+   .create_pixmap_surface = dri2_fallback_pixmap_surface,
+   .create_pbuffer_surface = droid_create_pbuffer_surface,
+   .destroy_surface = droid_destroy_surface,
+   .swap_interval = dri2_fallback_swap_interval,
+   .swap_buffers = droid_swap_buffers,
+   .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
+   .query_buffer_age = dri2_fallback_query_buffer_age,
+};
+
 EGLBoolean
 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
 {
@@ -651,6 +659,8 @@ dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
 
    _eglSetLogProc(droid_log);
 
+   loader_set_logger(_eglLog);
+
    dri2_dpy = calloc(1, sizeof(*dri2_dpy));
    if (!dri2_dpy)
       return _eglError(EGL_BAD_ALLOC, "eglInitialize");
@@ -663,7 +673,7 @@ dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
       goto cleanup_display;
    }
 
-   dri2_dpy->driver_name = (char *) droid_get_driver_name(dri2_dpy->fd);
+   dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd, 0);
    if (dri2_dpy->driver_name == NULL) {
       err = "DRI2: failed to get driver name";
       goto cleanup_device;
@@ -671,7 +681,7 @@ dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
 
    if (!dri2_load_driver(dpy)) {
       err = "DRI2: failed to load driver";
-      goto cleanup_device;
+      goto cleanup_driver_name;
    }
 
    dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
@@ -705,12 +715,19 @@ dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
 
    droid_init_driver_functions(drv);
 
+   /* Fill vtbl last to prevent accidentally calling virtual function during
+    * initialization.
+    */
+   dri2_dpy->vtbl = &droid_display_vtbl;
+
    return EGL_TRUE;
 
 cleanup_screen:
    dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
 cleanup_driver:
    dlclose(dri2_dpy->driver);
+cleanup_driver_name:
+   free(dri2_dpy->driver_name);
 cleanup_device:
    close(dri2_dpy->fd);
 cleanup_display: