st/egl: Move sw screen creation to native helper.
[mesa.git] / src / gallium / state_trackers / egl / x11 / native_ximage.c
index d69011ac5eec6a6dde45cf607854a52f7dd20d94..45679fc9b4e5f5dd08a4102f7aba72685c41b4c6 100644 (file)
  * The above copyright notice and this permission notice shall be included
  * in all copies or substantial portions of the Software.
  *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
- * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
- * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
  */
 
-#include <assert.h>
-#include <sys/ipc.h>
-#include <sys/types.h>
-#include <sys/shm.h>
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
 #include "util/u_memory.h"
 #include "pipe/p_compiler.h"
 #include "util/u_inlines.h"
 #include "state_tracker/xlib_sw_winsys.h"
-#include "target-helpers/wrap_screen.h"
 #include "util/u_debug.h"
-#include "softpipe/sp_public.h"
-#include "llvmpipe/lp_public.h"
-#include "cell/ppu/cell_public.h"
 #include "egllog.h"
 
+#include "common/native_helper.h"
 #include "native_x11.h"
 #include "x11_screen.h"
 
 enum ximage_surface_type {
    XIMAGE_SURFACE_TYPE_WINDOW,
    XIMAGE_SURFACE_TYPE_PIXMAP,
-   XIMAGE_SURFACE_TYPE_PBUFFER
 };
 
 struct ximage_display {
@@ -64,11 +57,6 @@ struct ximage_display {
    int num_configs;
 };
 
-struct ximage_buffer {
-   struct pipe_texture *texture;
-   struct xlib_drawable xdraw;
-};
-
 struct ximage_surface {
    struct native_surface base;
    Drawable drawable;
@@ -79,11 +67,9 @@ struct ximage_surface {
 
    unsigned int server_stamp;
    unsigned int client_stamp;
-   int width, height;
-   struct ximage_buffer buffers[NUM_NATIVE_ATTACHMENTS];
-   uint valid_mask;
 
-   struct pipe_surface *draw_surface;
+   struct resource_surface *rsurf;
+   struct xlib_drawable xdraw;
 };
 
 struct ximage_config {
@@ -109,70 +95,10 @@ ximage_config(const struct native_config *nconf)
    return (struct ximage_config *) nconf;
 }
 
-static void
-ximage_surface_free_buffer(struct native_surface *nsurf,
-                           enum native_attachment which)
-{
-   struct ximage_surface *xsurf = ximage_surface(nsurf);
-   struct ximage_buffer *xbuf = &xsurf->buffers[which];
-
-   pipe_texture_reference(&xbuf->texture, NULL);
-}
-
-static boolean
-ximage_surface_alloc_buffer(struct native_surface *nsurf,
-                            enum native_attachment which)
-{
-   struct ximage_surface *xsurf = ximage_surface(nsurf);
-   struct ximage_buffer *xbuf = &xsurf->buffers[which];
-   struct pipe_screen *screen = xsurf->xdpy->base.screen;
-   struct pipe_texture templ;
-
-   /* free old data */
-   if (xbuf->texture)
-      ximage_surface_free_buffer(&xsurf->base, which);
-
-   memset(&templ, 0, sizeof(templ));
-   templ.target = PIPE_TEXTURE_2D;
-   templ.format = xsurf->color_format;
-   templ.width0 = xsurf->width;
-   templ.height0 = xsurf->height;
-   templ.depth0 = 1;
-   templ.tex_usage = PIPE_TEXTURE_USAGE_RENDER_TARGET;
-
-   if (xsurf->type != XIMAGE_SURFACE_TYPE_PBUFFER) {
-      switch (which) {
-      case NATIVE_ATTACHMENT_FRONT_LEFT:
-      case NATIVE_ATTACHMENT_FRONT_RIGHT:
-         templ.tex_usage |= PIPE_TEXTURE_USAGE_SCANOUT;
-         break;
-      case NATIVE_ATTACHMENT_BACK_LEFT:
-      case NATIVE_ATTACHMENT_BACK_RIGHT:
-         templ.tex_usage |= PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
-         break;
-      default:
-         break;
-      }
-   }
-   xbuf->texture = screen->texture_create(screen, &templ);
-   if (xbuf->texture) {
-      xbuf->xdraw.visual = xsurf->visual.visual;
-      xbuf->xdraw.depth = xsurf->visual.depth;
-      xbuf->xdraw.drawable = xsurf->drawable;
-   }
-
-   /* clean up the buffer if allocation failed */
-   if (!xbuf->texture)
-      ximage_surface_free_buffer(&xsurf->base, which);
-
-   return (xbuf->texture != NULL);
-}
-
 /**
- * Update the geometry of the surface.  Return TRUE if the geometry has changed
- * since last call.
+ * Update the geometry of the surface.  This is a slow functions.
  */
-static boolean
+static void
 ximage_surface_update_geometry(struct native_surface *nsurf)
 {
    struct ximage_surface *xsurf = ximage_surface(nsurf);
@@ -180,109 +106,41 @@ ximage_surface_update_geometry(struct native_surface *nsurf)
    Window root;
    int x, y;
    unsigned int w, h, border, depth;
-   boolean updated = FALSE;
-
-   /* pbuffer has fixed geometry */
-   if (xsurf->type == XIMAGE_SURFACE_TYPE_PBUFFER)
-      return FALSE;
 
    ok = XGetGeometry(xsurf->xdpy->dpy, xsurf->drawable,
          &root, &x, &y, &w, &h, &border, &depth);
-   if (ok && (xsurf->width != w || xsurf->height != h)) {
-      xsurf->width = w;
-      xsurf->height = h;
-
+   if (ok && resource_surface_set_size(xsurf->rsurf, w, h))
       xsurf->server_stamp++;
-      updated = TRUE;
-   }
-
-   return updated;
-}
-
-static void
-ximage_surface_notify_invalid(struct native_surface *nsurf)
-{
-   struct ximage_surface *xsurf = ximage_surface(nsurf);
-   struct ximage_display *xdpy = xsurf->xdpy;
-
-   xdpy->event_handler->invalid_surface(&xdpy->base,
-         &xsurf->base, xsurf->server_stamp);
 }
 
 /**
- * Update the buffers of the surface.  It is a slow function due to the
- * round-trip to the server.
+ * Update the buffers of the surface.
  */
 static boolean
 ximage_surface_update_buffers(struct native_surface *nsurf, uint buffer_mask)
 {
    struct ximage_surface *xsurf = ximage_surface(nsurf);
-   boolean updated;
-   uint new_valid;
-   int att;
-
-   updated = ximage_surface_update_geometry(&xsurf->base);
-   if (updated) {
-      /* all buffers become invalid */
-      xsurf->valid_mask = 0x0;
-   }
-   else {
-      buffer_mask &= ~xsurf->valid_mask;
-      /* all requested buffers are valid */
-      if (!buffer_mask) {
-         xsurf->client_stamp = xsurf->server_stamp;
-         return TRUE;
-      }
-   }
-
-   new_valid = 0x0;
-   for (att = 0; att < NUM_NATIVE_ATTACHMENTS; att++) {
-      if (native_attachment_mask_test(buffer_mask, att)) {
-         /* reallocate the texture */
-         if (!ximage_surface_alloc_buffer(&xsurf->base, att))
-            break;
 
-         new_valid |= (1 << att);
-         if (buffer_mask == new_valid)
-            break;
-      }
+   if (xsurf->client_stamp != xsurf->server_stamp) {
+      ximage_surface_update_geometry(&xsurf->base);
+      xsurf->client_stamp = xsurf->server_stamp;
    }
 
-   xsurf->valid_mask |= new_valid;
-   xsurf->client_stamp = xsurf->server_stamp;
-
-   return (new_valid == buffer_mask);
+   return resource_surface_add_resources(xsurf->rsurf, buffer_mask);
 }
 
-static boolean
-ximage_surface_draw_buffer(struct native_surface *nsurf,
-                           enum native_attachment which)
+/**
+ * Emulate an invalidate event.
+ */
+static void
+ximage_surface_invalidate(struct native_surface *nsurf)
 {
    struct ximage_surface *xsurf = ximage_surface(nsurf);
-   struct ximage_buffer *xbuf = &xsurf->buffers[which];
-   struct pipe_screen *screen = xsurf->xdpy->base.screen;
-   struct pipe_surface *psurf;
-
-   if (xsurf->type == XIMAGE_SURFACE_TYPE_PBUFFER)
-      return TRUE;
-
-   assert(xsurf->drawable && xbuf->texture);
-
-   psurf = xsurf->draw_surface;
-   if (!psurf || psurf->texture != xbuf->texture) {
-      pipe_surface_reference(&xsurf->draw_surface, NULL);
-
-      psurf = screen->get_tex_surface(screen,
-            xbuf->texture, 0, 0, 0, PIPE_BUFFER_USAGE_CPU_READ);
-      if (!psurf)
-         return FALSE;
-
-      xsurf->draw_surface = psurf;
-   }
-
-   screen->flush_frontbuffer(screen, psurf, &xbuf->xdraw);
+   struct ximage_display *xdpy = xsurf->xdpy;
 
-   return TRUE;
+   xsurf->server_stamp++;
+   xdpy->event_handler->invalid_surface(&xdpy->base,
+         &xsurf->base, xsurf->server_stamp);
 }
 
 static boolean
@@ -291,11 +149,10 @@ ximage_surface_flush_frontbuffer(struct native_surface *nsurf)
    struct ximage_surface *xsurf = ximage_surface(nsurf);
    boolean ret;
 
-   ret = ximage_surface_draw_buffer(&xsurf->base,
-         NATIVE_ATTACHMENT_FRONT_LEFT);
+   ret = resource_surface_present(xsurf->rsurf,
+         NATIVE_ATTACHMENT_FRONT_LEFT, (void *) &xsurf->xdraw);
    /* force buffers to be updated in next validation call */
-   xsurf->server_stamp++;
-   ximage_surface_notify_invalid(&xsurf->base);
+   ximage_surface_invalidate(&xsurf->base);
 
    return ret;
 }
@@ -304,61 +161,41 @@ static boolean
 ximage_surface_swap_buffers(struct native_surface *nsurf)
 {
    struct ximage_surface *xsurf = ximage_surface(nsurf);
-   struct ximage_buffer *xfront, *xback, xtmp;
    boolean ret;
 
-   /* display the back buffer first */
-   ret = ximage_surface_draw_buffer(&xsurf->base,
-         NATIVE_ATTACHMENT_BACK_LEFT);
-   /* force buffers to be updated in next validation call */
-   xsurf->server_stamp++;
-   ximage_surface_notify_invalid(&xsurf->base);
-
-   xfront = &xsurf->buffers[NATIVE_ATTACHMENT_FRONT_LEFT];
-   xback = &xsurf->buffers[NATIVE_ATTACHMENT_BACK_LEFT];
+   ret = resource_surface_present(xsurf->rsurf,
+         NATIVE_ATTACHMENT_BACK_LEFT, (void *) &xsurf->xdraw);
 
-   /* skip swapping unless there is a front buffer */
-   if (xfront->texture) {
-      xtmp = *xfront;
-      *xfront = *xback;
-      *xback = xtmp;
-   }
+   resource_surface_swap_buffers(xsurf->rsurf,
+         NATIVE_ATTACHMENT_FRONT_LEFT, NATIVE_ATTACHMENT_BACK_LEFT, TRUE);
+   /* the front/back buffers have been swapped */
+   ximage_surface_invalidate(&xsurf->base);
 
    return ret;
 }
 
 static boolean
 ximage_surface_validate(struct native_surface *nsurf, uint attachment_mask,
-                        unsigned int *seq_num, struct pipe_texture **textures,
+                        unsigned int *seq_num, struct pipe_resource **textures,
                         int *width, int *height)
 {
    struct ximage_surface *xsurf = ximage_surface(nsurf);
+   uint w, h;
 
-   if (xsurf->client_stamp != xsurf->server_stamp ||
-       (xsurf->valid_mask & attachment_mask) != attachment_mask) {
-      if (!ximage_surface_update_buffers(&xsurf->base, attachment_mask))
-         return FALSE;
-   }
+   if (!ximage_surface_update_buffers(&xsurf->base, attachment_mask))
+      return FALSE;
 
    if (seq_num)
       *seq_num = xsurf->client_stamp;
 
-   if (textures) {
-      int att;
-      for (att = 0; att < NUM_NATIVE_ATTACHMENTS; att++) {
-         if (native_attachment_mask_test(attachment_mask, att)) {
-            struct ximage_buffer *xbuf = &xsurf->buffers[att];
-
-            textures[att] = NULL;
-            pipe_texture_reference(&textures[att], xbuf->texture);
-         }
-      }
-   }
+   if (textures)
+      resource_surface_get_resources(xsurf->rsurf, textures, attachment_mask);
 
+   resource_surface_get_size(xsurf->rsurf, &w, &h);
    if (width)
-      *width = xsurf->width;
+      *width = w;
    if (height)
-      *height = xsurf->height;
+      *height = h;
 
    return TRUE;
 }
@@ -375,14 +212,9 @@ static void
 ximage_surface_destroy(struct native_surface *nsurf)
 {
    struct ximage_surface *xsurf = ximage_surface(nsurf);
-   int i;
-
-   pipe_surface_reference(&xsurf->draw_surface, NULL);
 
-   for (i = 0; i < NUM_NATIVE_ATTACHMENTS; i++)
-      ximage_surface_free_buffer(&xsurf->base, i);
-
-   free(xsurf);
+   resource_surface_destroy(xsurf->rsurf);
+   FREE(xsurf);
 }
 
 static struct ximage_surface *
@@ -404,13 +236,26 @@ ximage_display_create_surface(struct native_display *ndpy,
    xsurf->color_format = xconf->base.color_format;
    xsurf->drawable = drawable;
 
-   if (xsurf->type != XIMAGE_SURFACE_TYPE_PBUFFER) {
-      xsurf->drawable = drawable;
-      xsurf->visual = *xconf->visual;
-      /* initialize the geometry */
-      ximage_surface_update_buffers(&xsurf->base, 0x0);
+   xsurf->rsurf = resource_surface_create(xdpy->base.screen,
+         xsurf->color_format,
+         PIPE_BIND_RENDER_TARGET |
+         PIPE_BIND_SAMPLER_VIEW |
+         PIPE_BIND_DISPLAY_TARGET |
+         PIPE_BIND_SCANOUT);
+   if (!xsurf->rsurf) {
+      FREE(xsurf);
+      return NULL;
    }
 
+   xsurf->drawable = drawable;
+   xsurf->visual = *xconf->visual;
+   /* initialize the geometry */
+   ximage_surface_update_geometry(&xsurf->base);
+
+   xsurf->xdraw.visual = xsurf->visual.visual;
+   xsurf->xdraw.depth = xsurf->visual.depth;
+   xsurf->xdraw.drawable = xsurf->drawable;
+
    xsurf->base.destroy = ximage_surface_destroy;
    xsurf->base.swap_buffers = ximage_surface_swap_buffers;
    xsurf->base.flush_frontbuffer = ximage_surface_flush_frontbuffer;
@@ -444,22 +289,6 @@ ximage_display_create_pixmap_surface(struct native_display *ndpy,
    return (xsurf) ? &xsurf->base : NULL;
 }
 
-static struct native_surface *
-ximage_display_create_pbuffer_surface(struct native_display *ndpy,
-                                      const struct native_config *nconf,
-                                      uint width, uint height)
-{
-   struct ximage_surface *xsurf;
-
-   xsurf = ximage_display_create_surface(ndpy, XIMAGE_SURFACE_TYPE_PBUFFER,
-         (Drawable) None, nconf);
-   if (xsurf) {
-      xsurf->width = width;
-      xsurf->height = height;
-   }
-   return (xsurf) ? &xsurf->base : NULL;
-}
-
 static enum pipe_format
 choose_format(const XVisualInfo *vinfo)
 {
@@ -493,7 +322,7 @@ ximage_display_get_configs(struct native_display *ndpy, int *num_configs)
    /* first time */
    if (!xdpy->configs) {
       const XVisualInfo *visuals;
-      int num_visuals, count, j;
+      int num_visuals, count;
 
       visuals = x11_screen_get_visuals(xdpy->xscr, &num_visuals);
       if (!visuals)
@@ -503,57 +332,42 @@ ximage_display_get_configs(struct native_display *ndpy, int *num_configs)
        * Create two configs for each visual.
        * One with depth/stencil buffer; one without
        */
-      xdpy->configs = calloc(num_visuals * 2, sizeof(*xdpy->configs));
+      xdpy->configs = CALLOC(num_visuals * 2, sizeof(*xdpy->configs));
       if (!xdpy->configs)
          return NULL;
 
       count = 0;
       for (i = 0; i < num_visuals; i++) {
-         for (j = 0; j < 2; j++) {
-            struct ximage_config *xconf = &xdpy->configs[count];
-            __GLcontextModes *mode = &xconf->base.mode;
-
-            xconf->visual = &visuals[i];
-            xconf->base.color_format = choose_format(xconf->visual);
-            if (xconf->base.color_format == PIPE_FORMAT_NONE)
-               continue;
-
-            x11_screen_convert_visual(xdpy->xscr, xconf->visual, mode);
-            /* support double buffer mode */
-            mode->doubleBufferMode = TRUE;
-
-            xconf->base.depth_format = PIPE_FORMAT_NONE;
-            xconf->base.stencil_format = PIPE_FORMAT_NONE;
-            /* create the second config with depth/stencil buffer */
-            if (j == 1) {
-               xconf->base.depth_format = PIPE_FORMAT_Z24_UNORM_S8_USCALED;
-               xconf->base.stencil_format = PIPE_FORMAT_Z24_UNORM_S8_USCALED;
-               mode->depthBits = 24;
-               mode->stencilBits = 8;
-               mode->haveDepthBuffer = TRUE;
-               mode->haveStencilBuffer = TRUE;
-            }
-
-            mode->maxPbufferWidth = 4096;
-            mode->maxPbufferHeight = 4096;
-            mode->maxPbufferPixels = 4096 * 4096;
-            mode->drawableType =
-               GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
-            mode->swapMethod = GLX_SWAP_EXCHANGE_OML;
-
-            if (mode->alphaBits)
-               mode->bindToTextureRgba = TRUE;
-            else
-               mode->bindToTextureRgb = TRUE;
-
-            count++;
-         }
+         struct ximage_config *xconf = &xdpy->configs[count];
+
+         xconf->visual = &visuals[i];
+         xconf->base.color_format = choose_format(xconf->visual);
+         if (xconf->base.color_format == PIPE_FORMAT_NONE)
+            continue;
+
+         xconf->base.buffer_mask =
+            (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
+            (1 << NATIVE_ATTACHMENT_BACK_LEFT);
+
+         xconf->base.window_bit = TRUE;
+         xconf->base.pixmap_bit = TRUE;
+
+         xconf->base.native_visual_id = xconf->visual->visualid;
+#if defined(__cplusplus) || defined(c_plusplus)
+         xconf->base.native_visual_type = xconf->visual->c_class;
+#else
+         xconf->base.native_visual_type = xconf->visual->class;
+#endif
+
+         xconf->base.slow_config = TRUE;
+
+         count++;
       }
 
       xdpy->num_configs = count;
    }
 
-   configs = malloc(xdpy->num_configs * sizeof(*configs));
+   configs = MALLOC(xdpy->num_configs * sizeof(*configs));
    if (configs) {
       for (i = 0; i < xdpy->num_configs; i++)
          configs[i] = (const struct native_config *) &xdpy->configs[i];
@@ -616,75 +430,22 @@ ximage_display_destroy(struct native_display *ndpy)
    struct ximage_display *xdpy = ximage_display(ndpy);
 
    if (xdpy->configs)
-      free(xdpy->configs);
+      FREE(xdpy->configs);
 
    xdpy->base.screen->destroy(xdpy->base.screen);
 
    x11_screen_destroy(xdpy->xscr);
    if (xdpy->own_dpy)
       XCloseDisplay(xdpy->dpy);
-   free(xdpy);
+   FREE(xdpy);
 }
 
-
-/* Helper function to build a subset of a driver stack consisting of
- * one of the software rasterizers (cell, llvmpipe, softpipe) and the
- * xlib winsys.
- *
- * This function could be shared, but currently causes headaches for
- * the build systems, particularly scons if we try.
- *
- * Long term, want to avoid having global #defines for things like
- * GALLIUM_LLVMPIPE, GALLIUM_CELL, etc.  Scons already eliminates
- * those #defines, so things that are painful for it now are likely to
- * be painful for other build systems in the future.
- */
-static struct pipe_screen *
-swrast_xlib_create_screen( Display *display )
-{
-   struct sw_winsys *winsys;
-   struct pipe_screen *screen = NULL;
-
-   /* Create the underlying winsys, which performs presents to Xlib
-    * drawables:
-    */
-   winsys = xlib_create_sw_winsys( display );
-   if (winsys == NULL)
-      return NULL;
-
-   /* Create a software rasterizer on top of that winsys.  Use
-    * llvmpipe if it is available.
-    */
-#if defined(GALLIUM_LLVMPIPE)
-   if (screen == NULL &&
-       !debug_get_bool_option("GALLIUM_NO_LLVM", FALSE))
-      screen = llvmpipe_create_screen( winsys );
-#endif
-
-   if (screen == NULL)
-      screen = softpipe_create_screen( winsys );
-
-   if (screen == NULL)
-      goto fail;
-
-   /* Inject any wrapping layers we want to here:
-    */
-   return gallium_wrap_screen( screen );
-
-fail:
-   if (winsys)
-      winsys->destroy( winsys );
-
-   return NULL;
-}
-
-
-
 struct native_display *
 x11_create_ximage_display(EGLNativeDisplayType dpy,
                           struct native_event_handler *event_handler)
 {
    struct ximage_display *xdpy;
+   struct sw_winsys *winsys = NULL;
 
    xdpy = CALLOC_STRUCT(ximage_display);
    if (!xdpy)
@@ -694,7 +455,7 @@ x11_create_ximage_display(EGLNativeDisplayType dpy,
    if (!xdpy->dpy) {
       xdpy->dpy = XOpenDisplay(NULL);
       if (!xdpy->dpy) {
-         free(xdpy);
+         FREE(xdpy);
          return NULL;
       }
       xdpy->own_dpy = TRUE;
@@ -704,12 +465,16 @@ x11_create_ximage_display(EGLNativeDisplayType dpy,
 
    xdpy->xscr_number = DefaultScreen(xdpy->dpy);
    xdpy->xscr = x11_screen_create(xdpy->dpy, xdpy->xscr_number);
-   if (!xdpy->xscr) {
-      free(xdpy);
-      return NULL;
-   }
+   if (!xdpy->xscr)
+      goto fail;
 
-   xdpy->base.screen = swrast_xlib_create_screen(xdpy->dpy);
+   winsys = xlib_create_sw_winsys(xdpy->dpy);
+   if (!winsys)
+      goto fail;
+
+   xdpy->base.screen = native_create_sw_screen(winsys);
+   if (!xdpy->base.screen)
+      goto fail;
 
    xdpy->base.destroy = ximage_display_destroy;
    xdpy->base.get_param = ximage_display_get_param;
@@ -718,7 +483,16 @@ x11_create_ximage_display(EGLNativeDisplayType dpy,
    xdpy->base.is_pixmap_supported = ximage_display_is_pixmap_supported;
    xdpy->base.create_window_surface = ximage_display_create_window_surface;
    xdpy->base.create_pixmap_surface = ximage_display_create_pixmap_surface;
-   xdpy->base.create_pbuffer_surface = ximage_display_create_pbuffer_surface;
 
    return &xdpy->base;
+
+fail:
+   if (winsys && winsys->destroy)
+      winsys->destroy(winsys);
+   if (xdpy->xscr)
+      x11_screen_destroy(xdpy->xscr);
+   if (xdpy->dpy && xdpy->own_dpy)
+      XCloseDisplay(xdpy->dpy);
+   FREE(xdpy);
+   return NULL;
 }