egl/main: Stop using EGLNative types internally
[mesa.git] / src / egl / drivers / dri2 / platform_x11.c
index 4757ccfca4f05f21fc184977e9a8d751e1869932..7b585a21971d8f6c6564e027a83b600d6339a2b6 100644 (file)
@@ -25,6 +25,7 @@
  *    Kristian Høgsberg <krh@bitplanet.net>
  */
 
+#include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <unistd.h>
+#ifdef HAVE_LIBDRM
 #include <xf86drm.h>
+#endif
 #include <sys/types.h>
 #include <sys/stat.h>
 
 #include "egl_dri2.h"
+#include "egl_dri2_fallbacks.h"
 
-/* From xmlpool/options.h, user exposed so should be stable */
-#define DRI_CONF_VBLANK_NEVER 0
-#define DRI_CONF_VBLANK_DEF_INTERVAL_0 1
-#define DRI_CONF_VBLANK_DEF_INTERVAL_1 2
-#define DRI_CONF_VBLANK_ALWAYS_SYNC 3
+static EGLBoolean
+dri2_x11_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
+                       EGLint interval);
 
 static void
 swrastCreateDrawable(struct dri2_egl_display * dri2_dpy,
@@ -180,9 +182,9 @@ swrastGetImage(__DRIdrawable * read,
  * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
  */
 static _EGLSurface *
-dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
-                   _EGLConfig *conf, EGLNativeWindowType native_window,
-                   const EGLint *attrib_list)
+dri2_x11_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
+                        _EGLConfig *conf, void *native_surface,
+                        const EGLint *attrib_list)
 {
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
    struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
@@ -191,7 +193,10 @@ dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
    xcb_get_geometry_reply_t *reply;
    xcb_screen_iterator_t s;
    xcb_generic_error_t *error;
-   xcb_drawable_t window = (uintptr_t )native_window;
+   xcb_drawable_t drawable;
+
+   STATIC_ASSERT(sizeof(uintptr_t) == sizeof(native_surface));
+   drawable = (uintptr_t) native_surface;
 
    (void) drv;
 
@@ -212,7 +217,7 @@ dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
                        dri2_surf->drawable, s.data->root,
                        dri2_surf->base.Width, dri2_surf->base.Height);
    } else {
-      dri2_surf->drawable = window;
+      dri2_surf->drawable = drawable;
    }
 
    if (dri2_dpy->dri2) {
@@ -275,46 +280,47 @@ dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
  * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
  */
 static _EGLSurface *
-dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
-                          _EGLConfig *conf, EGLNativeWindowType window,
-                          const EGLint *attrib_list)
+dri2_x11_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
+                               _EGLConfig *conf, void *native_window,
+                               const EGLint *attrib_list)
 {
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
    _EGLSurface *surf;
 
-   surf = dri2_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
-                              window, attrib_list);
+   surf = dri2_x11_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
+                                  native_window, attrib_list);
+   if (surf != NULL) {
+      /* When we first create the DRI2 drawable, its swap interval on the
+       * server side is 1.
+       */
+      surf->SwapInterval = 1;
 
-   /* When we first create the DRI2 drawable, its swap interval on the server
-    * side is 1.
-    */
-   surf->SwapInterval = 1;
-
-   /* Override that with a driconf-set value. */
-   drv->API.SwapInterval(drv, disp, surf, dri2_dpy->default_swap_interval);
+      /* Override that with a driconf-set value. */
+      dri2_x11_swap_interval(drv, disp, surf, dri2_dpy->default_swap_interval);
+   }
 
    return surf;
 }
 
 static _EGLSurface *
-dri2_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
-                          _EGLConfig *conf, EGLNativePixmapType pixmap,
-                          const EGLint *attrib_list)
+dri2_x11_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
+                               _EGLConfig *conf, void *native_pixmap,
+                               const EGLint *attrib_list)
 {
-   return dri2_create_surface(drv, disp, EGL_PIXMAP_BIT, conf,
-                             pixmap, attrib_list);
+   return dri2_x11_create_surface(drv, disp, EGL_PIXMAP_BIT, conf,
+                                  native_pixmap, attrib_list);
 }
 
 static _EGLSurface *
-dri2_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
-                           _EGLConfig *conf, const EGLint *attrib_list)
+dri2_x11_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
+                                _EGLConfig *conf, const EGLint *attrib_list)
 {
-   return dri2_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
-                             XCB_WINDOW_NONE, attrib_list);
+   return dri2_x11_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
+                                  XCB_WINDOW_NONE, attrib_list);
 }
 
 static EGLBoolean
-dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
+dri2_x11_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
 {
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
@@ -348,8 +354,8 @@ dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
  * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
  */
 static void
-dri2_process_buffers(struct dri2_egl_surface *dri2_surf,
-                    xcb_dri2_dri2_buffer_t *buffers, unsigned count)
+dri2_x11_process_buffers(struct dri2_egl_surface *dri2_surf,
+                         xcb_dri2_dri2_buffer_t *buffers, unsigned count)
 {
    struct dri2_egl_display *dri2_dpy =
       dri2_egl_display(dri2_surf->base.Resource.Display);
@@ -389,10 +395,10 @@ dri2_process_buffers(struct dri2_egl_surface *dri2_surf,
 }
 
 static __DRIbuffer *
-dri2_get_buffers(__DRIdrawable * driDrawable,
-               int *width, int *height,
-               unsigned int *attachments, int count,
-               int *out_count, void *loaderPrivate)
+dri2_x11_get_buffers(__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 =
@@ -414,7 +420,7 @@ dri2_get_buffers(__DRIdrawable * driDrawable,
    *out_count = reply->count;
    dri2_surf->base.Width = *width = reply->width;
    dri2_surf->base.Height = *height = reply->height;
-   dri2_process_buffers(dri2_surf, buffers, *out_count);
+   dri2_x11_process_buffers(dri2_surf, buffers, *out_count);
 
    free(reply);
 
@@ -422,10 +428,10 @@ dri2_get_buffers(__DRIdrawable * driDrawable,
 }
 
 static __DRIbuffer *
-dri2_get_buffers_with_format(__DRIdrawable * driDrawable,
-                            int *width, int *height,
-                            unsigned int *attachments, int count,
-                            int *out_count, void *loaderPrivate)
+dri2_x11_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 =
@@ -452,7 +458,7 @@ dri2_get_buffers_with_format(__DRIdrawable * driDrawable,
    dri2_surf->base.Width = *width = reply->width;
    dri2_surf->base.Height = *height = reply->height;
    *out_count = reply->count;
-   dri2_process_buffers(dri2_surf, buffers, *out_count);
+   dri2_x11_process_buffers(dri2_surf, buffers, *out_count);
 
    free(reply);
 
@@ -460,7 +466,7 @@ dri2_get_buffers_with_format(__DRIdrawable * driDrawable,
 }
 
 static void
-dri2_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
+dri2_x11_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
 {
    (void) driDrawable;
 
@@ -476,7 +482,7 @@ dri2_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
 }
 
 static char *
-dri2_strndup(const char *s, int length)
+dri2_x11_strndup(const char *s, int length)
 {
    char *d;
 
@@ -491,7 +497,7 @@ dri2_strndup(const char *s, int length)
 }
 
 static EGLBoolean
-dri2_connect(struct dri2_egl_display *dri2_dpy)
+dri2_x11_connect(struct dri2_egl_display *dri2_dpy)
 {
    xcb_xfixes_query_version_reply_t *xfixes_query;
    xcb_xfixes_query_version_cookie_t xfixes_query_cookie;
@@ -559,14 +565,14 @@ dri2_connect(struct dri2_egl_display *dri2_dpy)
 
    driver_name = xcb_dri2_connect_driver_name (connect);
    dri2_dpy->driver_name =
-      dri2_strndup(driver_name,
-                  xcb_dri2_connect_driver_name_length (connect));
+      dri2_x11_strndup(driver_name,
+                       xcb_dri2_connect_driver_name_length(connect));
 
    device_name = xcb_dri2_connect_device_name (connect);
 
    dri2_dpy->device_name =
-      dri2_strndup(device_name,
-                  xcb_dri2_connect_device_name_length (connect));
+      dri2_x11_strndup(device_name,
+                       xcb_dri2_connect_device_name_length(connect));
 
    if (dri2_dpy->device_name == NULL || dri2_dpy->driver_name == NULL) {
       free(dri2_dpy->device_name);
@@ -603,8 +609,9 @@ dri2_x11_authenticate(_EGLDisplay *disp, uint32_t id)
 }
 
 static EGLBoolean
-dri2_authenticate(_EGLDisplay *disp)
+dri2_x11_local_authenticate(_EGLDisplay *disp)
 {
+#ifdef HAVE_LIBDRM
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
    drm_magic_t magic;
 
@@ -617,18 +624,19 @@ dri2_authenticate(_EGLDisplay *disp)
       _eglLog(_EGL_WARNING, "DRI2: failed to authenticate");
       return EGL_FALSE;
    }
-
+#endif
    return EGL_TRUE;
 }
 
 static EGLBoolean
-dri2_add_configs_for_visuals(struct dri2_egl_display *dri2_dpy,
-                            _EGLDisplay *disp)
+dri2_x11_add_configs_for_visuals(struct dri2_egl_display *dri2_dpy,
+                                 _EGLDisplay *disp)
 {
    xcb_screen_iterator_t s;
    xcb_depth_iterator_t d;
    xcb_visualtype_t *visuals;
    int i, j, id;
+   unsigned int rgba_masks[4];
    EGLint surface_type;
    EGLint config_attrs[] = {
           EGL_NATIVE_VISUAL_ID,   0,
@@ -659,8 +667,26 @@ dri2_add_configs_for_visuals(struct dri2_egl_display *dri2_dpy,
             config_attrs[1] = visuals[i].visual_id;
             config_attrs[3] = visuals[i]._class;
 
+            rgba_masks[0] = visuals[i].red_mask;
+            rgba_masks[1] = visuals[i].green_mask;
+            rgba_masks[2] = visuals[i].blue_mask;
+            rgba_masks[3] = 0;
            dri2_add_config(disp, dri2_dpy->driver_configs[j], id++,
-                           d.data->depth, surface_type, config_attrs, NULL);
+                           surface_type, config_attrs, rgba_masks);
+
+            /* Allow a 24-bit RGB visual to match a 32-bit RGBA EGLConfig.
+             * Otherwise it will only match a 32-bit RGBA visual.  On a
+             * composited window manager on X11, this will make all of the
+             * EGLConfigs with destination alpha get blended by the
+             * compositor.  This is probably not what the application
+             * wants... especially on drivers that only have 32-bit RGBA
+             * EGLConfigs! */
+            if (d.data->depth == 24) {
+               rgba_masks[3] =
+                  ~(rgba_masks[0] | rgba_masks[1] | rgba_masks[2]);
+               dri2_add_config(disp, dri2_dpy->driver_configs[j], id++,
+                               surface_type, config_attrs, rgba_masks);
+            }
         }
       }
 
@@ -707,8 +733,8 @@ dri2_copy_region(_EGLDriver *drv, _EGLDisplay *disp,
 }
 
 static int64_t
-dri2_swap_buffers_msc(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
-                      int64_t msc, int64_t divisor, int64_t remainder)
+dri2_x11_swap_buffers_msc(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
+                          int64_t msc, int64_t divisor, int64_t remainder)
 {
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
@@ -742,8 +768,17 @@ dri2_swap_buffers_msc(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
       free(reply);
    }
 
-   /* If the server doesn't send invalidate events */
-   if (dri2_dpy->invalidate_available && dri2_dpy->flush &&
+   /* Since we aren't watching for the server's invalidate events like we're
+    * supposed to (due to XCB providing no mechanism for filtering the events
+    * the way xlib does), and SwapBuffers is a common cause of invalidate
+    * events, just shove one down to the driver, even though we haven't told
+    * the driver that we're the kind of loader that provides reliable
+    * invalidate events.  This causes the driver to request buffers again at
+    * its next draw, so that we get the correct buffers if a pageflip
+    * happened.  The driver should still be using the viewport hack to catch
+    * window resizes.
+    */
+   if (dri2_dpy->flush &&
        dri2_dpy->flush->base.version >= 3 && dri2_dpy->flush->invalidate)
       (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
 
@@ -751,13 +786,13 @@ dri2_swap_buffers_msc(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
 }
 
 static EGLBoolean
-dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
+dri2_x11_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
 {
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
 
    if (dri2_dpy->dri2) {
-      return dri2_swap_buffers_msc(drv, disp, draw, 0, 0, 0) != -1;
+      return dri2_x11_swap_buffers_msc(drv, disp, draw, 0, 0, 0) != -1;
    } else {
       assert(dri2_dpy->swrast);
 
@@ -767,8 +802,9 @@ dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
 }
 
 static EGLBoolean
-dri2_swap_buffers_region(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
-                        EGLint numRects, const EGLint *rects)
+dri2_x11_swap_buffers_region(_EGLDriver *drv, _EGLDisplay *disp,
+                             _EGLSurface *draw,
+                             EGLint numRects, const EGLint *rects)
 {
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
@@ -796,19 +832,20 @@ dri2_swap_buffers_region(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
 }
 
 static EGLBoolean
-dri2_post_sub_buffer(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
+dri2_x11_post_sub_buffer(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
                     EGLint x, EGLint y, EGLint width, EGLint height)
 {
-   const EGLint rect[4] = { x, draw->Height - y - height, width, height };
+   const EGLint rect[4] = { x, y, width, height };
 
    if (x < 0 || y < 0 || width < 0 || height < 0)
       _eglError(EGL_BAD_PARAMETER, "eglPostSubBufferNV");
 
-   return dri2_swap_buffers_region(drv, disp, draw, 1, rect);
+   return dri2_x11_swap_buffers_region(drv, disp, draw, 1, rect);
 }
 
 static EGLBoolean
-dri2_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf, EGLint interval)
+dri2_x11_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
+                       EGLint interval)
 {
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
@@ -827,13 +864,16 @@ dri2_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf, EGLint
 }
 
 static EGLBoolean
-dri2_copy_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
-                 EGLNativePixmapType native_target)
+dri2_x11_copy_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
+                      void *native_pixmap_target)
 {
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
    xcb_gcontext_t gc;
-   xcb_pixmap_t target = (uintptr_t )native_target;
+   xcb_pixmap_t target;
+
+   STATIC_ASSERT(sizeof(uintptr_t) == sizeof(native_pixmap_target));
+   target = (uintptr_t) native_pixmap_target;
 
    (void) drv;
 
@@ -959,24 +999,53 @@ dri2_x11_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
    }
 }
 
+static _EGLImage*
+dri2_x11_swrast_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
+                                 _EGLContext *ctx, EGLenum target,
+                                 EGLClientBuffer buffer,
+                                 const EGLint *attr_list)
+{
+   return NULL;
+}
+
+static struct dri2_egl_display_vtbl dri2_x11_swrast_display_vtbl = {
+   .authenticate = NULL,
+   .create_window_surface = dri2_x11_create_window_surface,
+   .create_pixmap_surface = dri2_x11_create_pixmap_surface,
+   .create_pbuffer_surface = dri2_x11_create_pbuffer_surface,
+   .destroy_surface = dri2_x11_destroy_surface,
+   .create_image = dri2_x11_swrast_create_image_khr,
+   .swap_interval = dri2_fallback_swap_interval,
+   .swap_buffers = dri2_x11_swap_buffers,
+   .swap_buffers_region = dri2_fallback_swap_buffers_region,
+   .post_sub_buffer = dri2_fallback_post_sub_buffer,
+   .copy_buffers = dri2_x11_copy_buffers,
+   .query_buffer_age = dri2_fallback_query_buffer_age,
+   .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
+};
+
+static struct dri2_egl_display_vtbl dri2_x11_display_vtbl = {
+   .authenticate = dri2_x11_authenticate,
+   .create_window_surface = dri2_x11_create_window_surface,
+   .create_pixmap_surface = dri2_x11_create_pixmap_surface,
+   .create_pbuffer_surface = dri2_x11_create_pbuffer_surface,
+   .destroy_surface = dri2_x11_destroy_surface,
+   .create_image = dri2_x11_create_image_khr,
+   .swap_interval = dri2_x11_swap_interval,
+   .swap_buffers = dri2_x11_swap_buffers,
+   .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
+   .swap_buffers_region = dri2_x11_swap_buffers_region,
+   .post_sub_buffer = dri2_x11_post_sub_buffer,
+   .copy_buffers = dri2_x11_copy_buffers,
+   .query_buffer_age = dri2_fallback_query_buffer_age,
+   .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
+};
+
 static EGLBoolean
 dri2_initialize_x11_swrast(_EGLDriver *drv, _EGLDisplay *disp)
 {
    struct dri2_egl_display *dri2_dpy;
 
-   drv->API.CreateWindowSurface = dri2_create_window_surface;
-   drv->API.CreatePixmapSurface = dri2_create_pixmap_surface;
-   drv->API.CreatePbufferSurface = dri2_create_pbuffer_surface;
-   drv->API.DestroySurface = dri2_destroy_surface;
-   drv->API.SwapBuffers = dri2_swap_buffers;
-   drv->API.CopyBuffers = dri2_copy_buffers;
-
-   drv->API.SwapBuffersRegionNOK = NULL;
-   drv->API.CreateImageKHR  = NULL;
-   drv->API.DestroyImageKHR = NULL;
-   drv->API.CreateDRMImageMESA = NULL;
-   drv->API.ExportDRMImageMESA = NULL;
-
    dri2_dpy = calloc(1, sizeof *dri2_dpy);
    if (!dri2_dpy)
       return _eglError(EGL_BAD_ALLOC, "eglInitialize");
@@ -984,6 +1053,7 @@ dri2_initialize_x11_swrast(_EGLDriver *drv, _EGLDisplay *disp)
    disp->DriverData = (void *) dri2_dpy;
    if (disp->PlatformDisplay == NULL) {
       dri2_dpy->conn = xcb_connect(0, 0);
+      dri2_dpy->own_device = true;
    } else {
       dri2_dpy->conn = XGetXCBConnection((Display *) disp->PlatformDisplay);
    }
@@ -1010,7 +1080,7 @@ dri2_initialize_x11_swrast(_EGLDriver *drv, _EGLDisplay *disp)
       goto cleanup_driver;
 
    if (dri2_dpy->conn) {
-      if (!dri2_add_configs_for_visuals(dri2_dpy, disp))
+      if (!dri2_x11_add_configs_for_visuals(dri2_dpy, disp))
          goto cleanup_configs;
    }
 
@@ -1018,6 +1088,11 @@ dri2_initialize_x11_swrast(_EGLDriver *drv, _EGLDisplay *disp)
    disp->VersionMajor = 1;
    disp->VersionMinor = 4;
 
+   /* Fill vtbl last to prevent accidentally calling virtual function during
+    * initialization.
+    */
+   dri2_dpy->vtbl = &dri2_x11_swrast_display_vtbl;
+
    return EGL_TRUE;
 
  cleanup_configs:
@@ -1035,7 +1110,7 @@ dri2_initialize_x11_swrast(_EGLDriver *drv, _EGLDisplay *disp)
 }
 
 static void
-dri2_setup_swap_interval(struct dri2_egl_display *dri2_dpy)
+dri2_x11_setup_swap_interval(struct dri2_egl_display *dri2_dpy)
 {
    GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
    int arbitrary_max_interval = 1000;
@@ -1085,17 +1160,6 @@ dri2_initialize_x11_dri2(_EGLDriver *drv, _EGLDisplay *disp)
 {
    struct dri2_egl_display *dri2_dpy;
 
-   drv->API.CreateWindowSurface = dri2_create_window_surface;
-   drv->API.CreatePixmapSurface = dri2_create_pixmap_surface;
-   drv->API.CreatePbufferSurface = dri2_create_pbuffer_surface;
-   drv->API.DestroySurface = dri2_destroy_surface;
-   drv->API.SwapBuffers = dri2_swap_buffers;
-   drv->API.CopyBuffers = dri2_copy_buffers;
-   drv->API.CreateImageKHR = dri2_x11_create_image_khr;
-   drv->API.SwapBuffersRegionNOK = dri2_swap_buffers_region;
-   drv->API.PostSubBufferNV = dri2_post_sub_buffer;
-   drv->API.SwapInterval = dri2_swap_interval;
-
    dri2_dpy = calloc(1, sizeof *dri2_dpy);
    if (!dri2_dpy)
       return _eglError(EGL_BAD_ALLOC, "eglInitialize");
@@ -1103,6 +1167,7 @@ dri2_initialize_x11_dri2(_EGLDriver *drv, _EGLDisplay *disp)
    disp->DriverData = (void *) dri2_dpy;
    if (disp->PlatformDisplay == NULL) {
       dri2_dpy->conn = xcb_connect(0, 0);
+      dri2_dpy->own_device = true;
    } else {
       dri2_dpy->conn = XGetXCBConnection((Display *) disp->PlatformDisplay);
    }
@@ -1113,7 +1178,7 @@ dri2_initialize_x11_dri2(_EGLDriver *drv, _EGLDisplay *disp)
    }
 
    if (dri2_dpy->conn) {
-      if (!dri2_connect(dri2_dpy))
+      if (!dri2_x11_connect(dri2_dpy))
         goto cleanup_conn;
    }
 
@@ -1138,22 +1203,22 @@ dri2_initialize_x11_dri2(_EGLDriver *drv, _EGLDisplay *disp)
    }
 
    if (dri2_dpy->conn) {
-      if (!dri2_authenticate(disp))
+      if (!dri2_x11_local_authenticate(disp))
         goto cleanup_fd;
    }
 
    if (dri2_dpy->dri2_minor >= 1) {
       dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
       dri2_dpy->dri2_loader_extension.base.version = 3;
-      dri2_dpy->dri2_loader_extension.getBuffers = dri2_get_buffers;
-      dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
+      dri2_dpy->dri2_loader_extension.getBuffers = dri2_x11_get_buffers;
+      dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_x11_flush_front_buffer;
       dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
-        dri2_get_buffers_with_format;
+        dri2_x11_get_buffers_with_format;
    } else {
       dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
       dri2_dpy->dri2_loader_extension.base.version = 2;
-      dri2_dpy->dri2_loader_extension.getBuffers = dri2_get_buffers;
-      dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
+      dri2_dpy->dri2_loader_extension.getBuffers = dri2_x11_get_buffers;
+      dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_x11_flush_front_buffer;
       dri2_dpy->dri2_loader_extension.getBuffersWithFormat = NULL;
    }
       
@@ -1167,8 +1232,10 @@ dri2_initialize_x11_dri2(_EGLDriver *drv, _EGLDisplay *disp)
    if (!dri2_create_screen(disp))
       goto cleanup_fd;
 
+   dri2_x11_setup_swap_interval(dri2_dpy);
+
    if (dri2_dpy->conn) {
-      if (!dri2_add_configs_for_visuals(dri2_dpy, disp))
+      if (!dri2_x11_add_configs_for_visuals(dri2_dpy, disp))
         goto cleanup_configs;
    }
 
@@ -1180,13 +1247,20 @@ dri2_initialize_x11_dri2(_EGLDriver *drv, _EGLDisplay *disp)
 #ifdef HAVE_WAYLAND_PLATFORM
    disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
 #endif
-   dri2_dpy->authenticate = dri2_x11_authenticate;
+
+   if (dri2_dpy->conn) {
+      if (!dri2_x11_add_configs_for_visuals(dri2_dpy, disp))
+        goto cleanup_configs;
+   }
 
    /* we're supporting EGL 1.4 */
    disp->VersionMajor = 1;
    disp->VersionMinor = 4;
 
-   dri2_setup_swap_interval(dri2_dpy);
+   /* Fill vtbl last to prevent accidentally calling virtual function during
+    * initialization.
+    */
+   dri2_dpy->vtbl = &dri2_x11_display_vtbl;
 
    return EGL_TRUE;