wayland: Support EGL_WIDTH and EGL_HEIGHT queries for wl_buffer
[mesa.git] / src / gallium / state_trackers / egl / common / egl_g3d_api.c
index 478516453cef98797d4f3bfbab1058434070dc40..a73859c0b9da44a8304a2b5cccb2899fc77a8b45 100644 (file)
 #include "pipe/p_screen.h"
 #include "util/u_memory.h"
 #include "util/u_inlines.h"
+#include "util/u_box.h"
 
 #include "egl_g3d.h"
 #include "egl_g3d_api.h"
 #include "egl_g3d_image.h"
+#include "egl_g3d_sync.h"
 #include "egl_g3d_st.h"
 #include "native.h"
 
+#ifdef EGL_WL_bind_wayland_display
+#include <wayland-drm.h>
+#endif
+
 /**
  * Return the state tracker for the given context.
  */
 static struct st_api *
-egl_g3d_choose_st(_EGLDriver *drv, _EGLContext *ctx)
+egl_g3d_choose_st(_EGLDriver *drv, _EGLContext *ctx,
+                  enum st_profile_type *profile)
 {
-   struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
    struct st_api *stapi;
-   EGLint idx = -1;
+   EGLint api = -1;
+
+   *profile = ST_PROFILE_DEFAULT;
 
    switch (ctx->ClientAPI) {
    case EGL_OPENGL_ES_API:
       switch (ctx->ClientVersion) {
       case 1:
-         idx = ST_API_OPENGL_ES1;
+         api = ST_API_OPENGL;
+         *profile = ST_PROFILE_OPENGL_ES1;
          break;
       case 2:
-         idx = ST_API_OPENGL_ES2;
+         api = ST_API_OPENGL;
+         *profile = ST_PROFILE_OPENGL_ES2;
          break;
       default:
          _eglLog(_EGL_WARNING, "unknown client version %d",
@@ -63,20 +73,78 @@ egl_g3d_choose_st(_EGLDriver *drv, _EGLContext *ctx)
       }
       break;
    case EGL_OPENVG_API:
-      idx = ST_API_OPENVG;
+      api = ST_API_OPENVG;
       break;
    case EGL_OPENGL_API:
-      idx = ST_API_OPENGL;
+      api = ST_API_OPENGL;
       break;
    default:
       _eglLog(_EGL_WARNING, "unknown client API 0x%04x", ctx->ClientAPI);
       break;
    }
 
-   stapi = (idx >= 0) ? gdrv->stapis[idx] : NULL;
+   stapi = egl_g3d_get_st_api(drv, api);
+   if (stapi && !(stapi->profile_mask & (1 << *profile)))
+      stapi = NULL;
+
    return stapi;
 }
 
+struct egl_g3d_choose_config_data {
+   _EGLConfig criteria;
+   enum pipe_format format;
+};
+
+static int
+egl_g3d_compare_config(const _EGLConfig *conf1, const _EGLConfig *conf2,
+                       void *priv_data)
+{
+   struct egl_g3d_choose_config_data *data =
+      (struct egl_g3d_choose_config_data *) priv_data;
+   const _EGLConfig *criteria = &data->criteria;;
+
+   /* EGL_NATIVE_VISUAL_TYPE ignored? */
+   return _eglCompareConfigs(conf1, conf2, criteria, EGL_TRUE);
+}
+
+static EGLBoolean
+egl_g3d_match_config(const _EGLConfig *conf, void *priv_data)
+{
+   struct egl_g3d_choose_config_data *data =
+      (struct egl_g3d_choose_config_data *) priv_data;
+   struct egl_g3d_config *gconf = egl_g3d_config(conf);
+
+   if (data->format != PIPE_FORMAT_NONE &&
+       data->format != gconf->native->color_format)
+      return EGL_FALSE;
+
+   return _eglMatchConfig(conf, &data->criteria);
+}
+
+static EGLBoolean
+egl_g3d_choose_config(_EGLDriver *drv, _EGLDisplay *dpy, const EGLint *attribs,
+                      EGLConfig *configs, EGLint size, EGLint *num_configs)
+{
+   struct egl_g3d_choose_config_data data;
+
+   if (!_eglParseConfigAttribList(&data.criteria, dpy, attribs))
+      return _eglError(EGL_BAD_ATTRIBUTE, "eglChooseConfig");
+
+   data.format = PIPE_FORMAT_NONE;
+   if (data.criteria.MatchNativePixmap != EGL_NONE &&
+       data.criteria.MatchNativePixmap != EGL_DONT_CARE) {
+      struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
+
+      if (!gdpy->native->get_pixmap_format(gdpy->native,
+               (EGLNativePixmapType) data.criteria.MatchNativePixmap,
+               &data.format))
+         return _eglError(EGL_BAD_NATIVE_PIXMAP, "eglChooseConfig");
+   }
+
+   return _eglFilterConfigArray(dpy->Configs, configs, size, num_configs,
+         egl_g3d_match_config, egl_g3d_compare_config, &data);
+}
+
 static _EGLContext *
 egl_g3d_create_context(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
                        _EGLContext *share, const EGLint *attribs)
@@ -85,6 +153,8 @@ egl_g3d_create_context(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
    struct egl_g3d_context *gshare = egl_g3d_context(share);
    struct egl_g3d_config *gconf = egl_g3d_config(conf);
    struct egl_g3d_context *gctx;
+   struct st_context_attribs stattribs;
+   enum st_context_error ctx_err = 0;
 
    gctx = CALLOC_STRUCT(egl_g3d_context);
    if (!gctx) {
@@ -97,14 +167,18 @@ egl_g3d_create_context(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
       return NULL;
    }
 
-   gctx->stapi = egl_g3d_choose_st(drv, &gctx->base);
+   memset(&stattribs, 0, sizeof(stattribs));
+   if (gconf)
+      stattribs.visual = gconf->stvis;
+
+   gctx->stapi = egl_g3d_choose_st(drv, &gctx->base, &stattribs.profile);
    if (!gctx->stapi) {
       FREE(gctx);
       return NULL;
    }
 
-   gctx->stctxi = gctx->stapi->create_context(gctx->stapi, gdpy->smapi,
-         &gconf->stvis, (gshare) ? gshare->stctxi : NULL);
+   gctx->stctxi = gctx->stapi->create_context(gctx->stapi, gdpy->smapi, 
+         &stattribs, &ctx_err, (gshare) ? gshare->stctxi : NULL);
    if (!gctx->stctxi) {
       FREE(gctx);
       return NULL;
@@ -135,7 +209,7 @@ destroy_context(_EGLDisplay *dpy, _EGLContext *ctx)
 static EGLBoolean
 egl_g3d_destroy_context(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx)
 {
-   if (!_eglIsContextBound(ctx))
+   if (_eglPutContext(ctx))
       destroy_context(dpy, ctx);
    return EGL_TRUE;
 }
@@ -223,9 +297,14 @@ egl_g3d_create_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
    }
 
    gsurf->stvis = gconf->stvis;
-   if (gsurf->base.RenderBuffer == EGL_SINGLE_BUFFER)
+   if (gsurf->base.RenderBuffer == EGL_SINGLE_BUFFER &&
+       gconf->stvis.buffer_mask & ST_ATTACHMENT_FRONT_LEFT_MASK)
       gsurf->stvis.render_buffer = ST_ATTACHMENT_FRONT_LEFT;
 
+   /* surfaces can always be posted when the display supports it */
+   if (dpy->Extensions.NV_post_sub_buffer)
+      gsurf->base.PostSubBufferSupportedNV = EGL_TRUE;
+
    gsurf->stfbi = egl_g3d_create_st_framebuffer(&gsurf->base);
    if (!gsurf->stfbi) {
       nsurf->destroy(nsurf);
@@ -267,16 +346,16 @@ egl_g3d_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *dpy,
    return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
 }
 
-static _EGLSurface *
-egl_g3d_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *dpy,
-                               _EGLConfig *conf, const EGLint *attribs)
+static struct egl_g3d_surface *
+create_pbuffer_surface(_EGLDisplay *dpy, _EGLConfig *conf,
+                       const EGLint *attribs, const char *func)
 {
    struct egl_g3d_config *gconf = egl_g3d_config(conf);
    struct egl_g3d_surface *gsurf;
 
    gsurf = CALLOC_STRUCT(egl_g3d_surface);
    if (!gsurf) {
-      _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
+      _eglError(EGL_BAD_ALLOC, func);
       return NULL;
    }
 
@@ -293,6 +372,90 @@ egl_g3d_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *dpy,
       return NULL;
    }
 
+   return gsurf;
+}
+
+static _EGLSurface *
+egl_g3d_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *dpy,
+                               _EGLConfig *conf, const EGLint *attribs)
+{
+   struct egl_g3d_surface *gsurf;
+
+   gsurf = create_pbuffer_surface(dpy, conf, attribs,
+         "eglCreatePbufferSurface");
+   if (!gsurf)
+      return NULL;
+
+   gsurf->client_buffer_type = EGL_NONE;
+
+   return &gsurf->base;
+}
+
+static _EGLSurface *
+egl_g3d_create_pbuffer_from_client_buffer(_EGLDriver *drv, _EGLDisplay *dpy,
+                                          EGLenum buftype,
+                                          EGLClientBuffer buffer,
+                                          _EGLConfig *conf,
+                                          const EGLint *attribs)
+{
+   struct egl_g3d_surface *gsurf;
+   struct pipe_resource *ptex = NULL;
+   EGLint pbuffer_attribs[32];
+   EGLint count, i;
+
+   switch (buftype) {
+   case EGL_OPENVG_IMAGE:
+      break;
+   default:
+      _eglError(EGL_BAD_PARAMETER, "eglCreatePbufferFromClientBuffer");
+      return NULL;
+      break;
+   }
+
+   /* parse the attributes first */
+   count = 0;
+   for (i = 0; attribs && attribs[i] != EGL_NONE; i++) {
+      EGLint attr = attribs[i++];
+      EGLint val = attribs[i];
+      EGLint err = EGL_SUCCESS;
+
+      switch (attr) {
+      case EGL_TEXTURE_FORMAT:
+      case EGL_TEXTURE_TARGET:
+      case EGL_MIPMAP_TEXTURE:
+         pbuffer_attribs[count++] = attr;
+         pbuffer_attribs[count++] = val;
+         break;
+      default:
+         err = EGL_BAD_ATTRIBUTE;
+         break;
+      }
+      /* bail out */
+      if (err != EGL_SUCCESS) {
+         _eglError(err, "eglCreatePbufferFromClientBuffer");
+         return NULL;
+      }
+   }
+
+   pbuffer_attribs[count++] = EGL_NONE;
+
+   gsurf = create_pbuffer_surface(dpy, conf, pbuffer_attribs,
+         "eglCreatePbufferFromClientBuffer");
+   if (!gsurf)
+      return NULL;
+
+   gsurf->client_buffer_type = buftype;
+   gsurf->client_buffer = buffer;
+
+   /* validate now so that it fails if the client buffer is invalid */
+   if (!gsurf->stfbi->validate(gsurf->stfbi,
+            &gsurf->stvis.render_buffer, 1, &ptex)) {
+      egl_g3d_destroy_st_framebuffer(gsurf->stfbi);
+      FREE(gsurf);
+      return NULL;
+   }
+   pipe_resource_reference(&ptex, NULL);
+
    return &gsurf->base;
 }
 
@@ -318,7 +481,7 @@ destroy_surface(_EGLDisplay *dpy, _EGLSurface *surf)
 static EGLBoolean
 egl_g3d_destroy_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
 {
-   if (!_eglIsSurfaceBound(surf))
+   if (_eglPutSurface(surf))
       destroy_surface(dpy, surf);
    return EGL_TRUE;
 }
@@ -331,57 +494,74 @@ egl_g3d_make_current(_EGLDriver *drv, _EGLDisplay *dpy,
    struct egl_g3d_surface *gdraw = egl_g3d_surface(draw);
    struct egl_g3d_surface *gread = egl_g3d_surface(read);
    struct egl_g3d_context *old_gctx;
+   _EGLContext *old_ctx;
+   _EGLSurface *old_draw, *old_read;
    EGLBoolean ok = EGL_TRUE;
 
-   /* bind the new context and return the "orphaned" one */
-   if (!_eglBindContext(&ctx, &draw, &read))
+   /* make new bindings */
+   if (!_eglBindContext(ctx, draw, read, &old_ctx, &old_draw, &old_read))
       return EGL_FALSE;
-   old_gctx = egl_g3d_context(ctx);
 
+   old_gctx = egl_g3d_context(old_ctx);
    if (old_gctx) {
       /* flush old context */
-      old_gctx->stctxi->flush(old_gctx->stctxi,
-            PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
+      old_gctx->stctxi->flush(old_gctx->stctxi, ST_FLUSH_FRONT, NULL);
    }
 
    if (gctx) {
       ok = gctx->stapi->make_current(gctx->stapi, gctx->stctxi,
             (gdraw) ? gdraw->stfbi : NULL, (gread) ? gread->stfbi : NULL);
       if (ok) {
-         gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi, gdraw->stfbi);
-         if (gread != gdraw) {
-            gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi,
-                  gread->stfbi);
-         }
-
-         if (gdraw->base.Type == EGL_WINDOW_BIT) {
-            gctx->base.WindowRenderBuffer =
-               (gdraw->stvis.render_buffer == ST_ATTACHMENT_FRONT_LEFT) ?
-               EGL_SINGLE_BUFFER : EGL_BACK_BUFFER;
+         if (gdraw) {
+            if (gdraw->base.Type == EGL_WINDOW_BIT) {
+               gctx->base.WindowRenderBuffer =
+                  (gdraw->stvis.render_buffer == ST_ATTACHMENT_FRONT_LEFT) ?
+                  EGL_SINGLE_BUFFER : EGL_BACK_BUFFER;
+            }
          }
       }
    }
    else if (old_gctx) {
       ok = old_gctx->stapi->make_current(old_gctx->stapi, NULL, NULL, NULL);
-      old_gctx->base.WindowRenderBuffer = EGL_NONE;
+      if (ok)
+         old_gctx->base.WindowRenderBuffer = EGL_NONE;
    }
 
-   if (ctx && !_eglIsContextLinked(ctx))
-      destroy_context(dpy, ctx);
-   if (draw && !_eglIsSurfaceLinked(draw))
-      destroy_surface(dpy, draw);
-   if (read && read != draw && !_eglIsSurfaceLinked(read))
-      destroy_surface(dpy, read);
+   if (ok) {
+      if (_eglPutContext(old_ctx))
+         destroy_context(dpy, old_ctx);
+      if (_eglPutSurface(old_draw))
+         destroy_surface(dpy, old_draw);
+      if (_eglPutSurface(old_read))
+         destroy_surface(dpy, old_read);
+   }
+   else {
+      /* undo the previous _eglBindContext */
+      _eglBindContext(old_ctx, old_draw, old_read, &ctx, &draw, &read);
+      assert(&gctx->base == ctx &&
+             &gdraw->base == draw &&
+             &gread->base == read);
+
+      _eglPutSurface(draw);
+      _eglPutSurface(read);
+      _eglPutContext(ctx);
+
+      _eglPutSurface(old_draw);
+      _eglPutSurface(old_read);
+      _eglPutContext(old_ctx);
+   }
 
    return ok;
 }
 
 static EGLBoolean
-egl_g3d_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
+swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
+             EGLint num_rects, const EGLint *rects, EGLBoolean preserve)
 {
    struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
    _EGLContext *ctx = _eglGetCurrentContext();
    struct egl_g3d_context *gctx = NULL;
+   struct native_present_control ctrl;
 
    /* no-op for pixmap or pbuffer surface */
    if (gsurf->base.Type == EGL_PIXMAP_BIT ||
@@ -397,34 +577,64 @@ egl_g3d_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
 
    /* flush if the surface is current */
    if (gctx) {
-      gctx->stctxi->flush(gctx->stctxi,
-            PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
+      gctx->stctxi->flush(gctx->stctxi, ST_FLUSH_FRONT, NULL);
    }
 
-   return gsurf->native->swap_buffers(gsurf->native);
+   memset(&ctrl, 0, sizeof(ctrl));
+   ctrl.natt = NATIVE_ATTACHMENT_BACK_LEFT;
+   ctrl.preserve = preserve;
+   ctrl.swap_interval = gsurf->base.SwapInterval;
+   ctrl.premultiplied_alpha = (gsurf->base.VGAlphaFormat == EGL_VG_ALPHA_FORMAT_PRE);
+   ctrl.num_rects = num_rects;
+   ctrl.rects = rects;
+
+   return gsurf->native->present(gsurf->native, &ctrl);
 }
 
-/**
- * Get the pipe surface of the given attachment of the native surface.
- */
-static struct pipe_surface *
-get_pipe_surface(struct native_display *ndpy, struct native_surface *nsurf,
-                 enum native_attachment natt,
-                unsigned bind)
+static EGLBoolean
+egl_g3d_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
 {
-   struct pipe_resource *textures[NUM_NATIVE_ATTACHMENTS];
-   struct pipe_surface *psurf;
+   struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
 
-   textures[natt] = NULL;
-   nsurf->validate(nsurf, 1 << natt, NULL, textures, NULL, NULL);
-   if (!textures[natt])
-      return NULL;
+   return swap_buffers(drv, dpy, surf, 0, NULL,
+                       (gsurf->base.SwapBehavior == EGL_BUFFER_PRESERVED));
+}
+
+#ifdef EGL_NOK_swap_region
+static EGLBoolean
+egl_g3d_swap_buffers_region(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
+                            EGLint num_rects, const EGLint *rects)
+{
+   /* Note: y=0=top */
+   return swap_buffers(drv, dpy, surf, num_rects, rects, EGL_TRUE);
+}
+#endif /* EGL_NOK_swap_region */
+
+static EGLBoolean
+egl_g3d_post_sub_buffer(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
+                        EGLint x, EGLint y, EGLint width, EGLint height)
+{
+   EGLint rect[4];
 
-   psurf = ndpy->screen->get_tex_surface(ndpy->screen, textures[natt],
-         0, 0, 0, bind);
-   pipe_resource_reference(&textures[natt], NULL);
+   if (x < 0 || y < 0 || width < 0 || height < 0)
+      return _eglError(EGL_BAD_PARAMETER, "eglPostSubBufferNV");
 
-   return psurf;
+   /* clamp */
+   if (x + width > surf->Width)
+      width = surf->Width - x;
+   if (y + height > surf->Height)
+      height = surf->Height - y;
+
+   if (width <= 0 || height <= 0)
+      return EGL_TRUE;
+
+   rect[0] = x;
+   /* Note: y=0=bottom */
+   rect[1] = surf->Height - y - height;
+   rect[2] = width;
+   rect[3] = height;
+
+   return swap_buffers(drv, dpy, surf, 1, rect, EGL_TRUE);
 }
 
 static EGLBoolean
@@ -434,59 +644,18 @@ egl_g3d_copy_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
    struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
    struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
    _EGLContext *ctx = _eglGetCurrentContext();
-   struct egl_g3d_config *gconf;
-   struct native_surface *nsurf;
-   struct pipe_screen *screen = gdpy->native->screen;
-   struct pipe_surface *psurf;
 
    if (!gsurf->render_texture)
       return EGL_TRUE;
 
-   gconf = egl_g3d_config(egl_g3d_find_pixmap_config(dpy, target));
-   if (!gconf)
-      return _eglError(EGL_BAD_NATIVE_PIXMAP, "eglCopyBuffers");
-
-   nsurf = gdpy->native->create_pixmap_surface(gdpy->native,
-         target, gconf->native);
-   if (!nsurf)
-      return _eglError(EGL_BAD_NATIVE_PIXMAP, "eglCopyBuffers");
-
    /* flush if the surface is current */
    if (ctx && ctx->DrawSurface == &gsurf->base) {
       struct egl_g3d_context *gctx = egl_g3d_context(ctx);
-      gctx->stctxi->flush(gctx->stctxi,
-            PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
+      gctx->stctxi->flush(gctx->stctxi, ST_FLUSH_FRONT, NULL);
    }
 
-   /* create a pipe context to copy surfaces */
-   if (!gdpy->pipe) {
-      gdpy->pipe =
-         gdpy->native->screen->context_create(gdpy->native->screen, NULL);
-      if (!gdpy->pipe)
-         return EGL_FALSE;
-   }
-
-   psurf = get_pipe_surface(gdpy->native, nsurf, NATIVE_ATTACHMENT_FRONT_LEFT,
-                           PIPE_BIND_BLIT_DESTINATION);
-   if (psurf) {
-      struct pipe_surface *psrc;
-
-      psrc = screen->get_tex_surface(screen, gsurf->render_texture,
-            0, 0, 0, PIPE_BIND_BLIT_SOURCE);
-      if (psrc) {
-         gdpy->pipe->surface_copy(gdpy->pipe, psurf, 0, 0,
-               psrc, 0, 0, psurf->width, psurf->height);
-         pipe_surface_reference(&psrc, NULL);
-
-         nsurf->flush_frontbuffer(nsurf);
-      }
-
-      pipe_surface_reference(&psurf, NULL);
-   }
-
-   nsurf->destroy(nsurf);
-
-   return EGL_TRUE;
+   return gdpy->native->copy_to_pixmap(gdpy->native,
+         target, gsurf->render_texture);
 }
 
 static EGLBoolean
@@ -497,10 +666,11 @@ egl_g3d_wait_client(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx)
    struct pipe_screen *screen = gdpy->native->screen;
    struct pipe_fence_handle *fence = NULL;
 
-   gctx->stctxi->flush(gctx->stctxi,
-         PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, &fence);
-   screen->fence_finish(screen, fence, 0);
-   screen->fence_reference(screen, &fence, NULL);
+   gctx->stctxi->flush(gctx->stctxi, ST_FLUSH_FRONT, &fence);
+   if (fence) {
+      screen->fence_finish(screen, fence, PIPE_TIMEOUT_INFINITE);
+      screen->fence_reference(screen, &fence, NULL);
+   }
 
    return EGL_TRUE;
 }
@@ -567,8 +737,7 @@ egl_g3d_bind_tex_image(_EGLDriver *drv, _EGLDisplay *dpy,
    /* flush properly if the surface is bound */
    if (gsurf->base.CurrentContext) {
       gctx = egl_g3d_context(gsurf->base.CurrentContext);
-      gctx->stctxi->flush(gctx->stctxi,
-            PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
+      gctx->stctxi->flush(gctx->stctxi, ST_FLUSH_FRONT, NULL);
    }
 
    gctx = egl_g3d_context(es1);
@@ -682,35 +851,80 @@ egl_g3d_show_screen_surface(_EGLDriver *drv, _EGLDisplay *dpy,
 
 #endif /* EGL_MESA_screen_surface */
 
-/**
- * Find a config that supports the pixmap.
- */
-_EGLConfig *
-egl_g3d_find_pixmap_config(_EGLDisplay *dpy, EGLNativePixmapType pix)
+#ifdef EGL_WL_bind_wayland_display
+
+static EGLBoolean
+egl_g3d_bind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *dpy,
+                                struct wl_display *wl_dpy)
 {
    struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
-   struct egl_g3d_config *gconf;
-   EGLint i;
 
-   for (i = 0; i < dpy->NumConfigs; i++) {
-      gconf = egl_g3d_config(dpy->Configs[i]);
-      if (gdpy->native->is_pixmap_supported(gdpy->native, pix, gconf->native))
-         break;
-   }
+   if (!gdpy->native->wayland_bufmgr)
+      return EGL_FALSE;
+
+   return gdpy->native->wayland_bufmgr->bind_display(gdpy->native, wl_dpy);
+}
+
+static EGLBoolean
+egl_g3d_unbind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *dpy,
+                                  struct wl_display *wl_dpy)
+{
+   struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
+
+   if (!gdpy->native->wayland_bufmgr)
+      return EGL_FALSE;
+
+   return gdpy->native->wayland_bufmgr->unbind_display(gdpy->native, wl_dpy);
+}
+
+static EGLBoolean
+egl_g3d_query_wayland_buffer_wl(_EGLDriver *drv, _EGLDisplay *dpy,
+                                struct wl_buffer *_buffer,
+                                EGLint attribute, EGLint *value)
+{
+   struct wl_drm_buffer *buffer = (struct wl_drm_buffer *) _buffer;
+   struct pipe_resource *resource = buffer->driver_buffer;
 
-   return (i < dpy->NumConfigs) ? &gconf->base : NULL;
+   if (!wayland_buffer_is_drm(&buffer->buffer))
+      return EGL_FALSE;
+
+   switch (attribute) {
+   case EGL_TEXTURE_FORMAT:
+      switch (resource->format) {
+      case PIPE_FORMAT_B8G8R8A8_UNORM:
+         *value = EGL_TEXTURE_RGBA;
+         return EGL_TRUE;
+      case PIPE_FORMAT_B8G8R8X8_UNORM:
+         *value = EGL_TEXTURE_RGB;
+         return EGL_TRUE;
+      default:
+         return EGL_FALSE;
+      }
+   case EGL_WIDTH:
+      *value = buffer->buffer.width;
+      return EGL_TRUE;
+   case EGL_HEIGHT:
+      *value = buffer->buffer.height;
+      return EGL_TRUE;
+   default:
+      return EGL_FALSE;
+   }
 }
+#endif /* EGL_WL_bind_wayland_display */
 
 void
 egl_g3d_init_driver_api(_EGLDriver *drv)
 {
    _eglInitDriverFallbacks(drv);
 
+   drv->API.ChooseConfig = egl_g3d_choose_config;
+
    drv->API.CreateContext = egl_g3d_create_context;
    drv->API.DestroyContext = egl_g3d_destroy_context;
    drv->API.CreateWindowSurface = egl_g3d_create_window_surface;
    drv->API.CreatePixmapSurface = egl_g3d_create_pixmap_surface;
    drv->API.CreatePbufferSurface = egl_g3d_create_pbuffer_surface;
+   drv->API.CreatePbufferFromClientBuffer = egl_g3d_create_pbuffer_from_client_buffer;
    drv->API.DestroySurface = egl_g3d_destroy_surface;
    drv->API.MakeCurrent = egl_g3d_make_current;
    drv->API.SwapBuffers = egl_g3d_swap_buffers;
@@ -723,9 +937,29 @@ egl_g3d_init_driver_api(_EGLDriver *drv)
 
    drv->API.CreateImageKHR = egl_g3d_create_image;
    drv->API.DestroyImageKHR = egl_g3d_destroy_image;
+#ifdef EGL_MESA_drm_image
+   drv->API.CreateDRMImageMESA = egl_g3d_create_drm_image;
+   drv->API.ExportDRMImageMESA = egl_g3d_export_drm_image;
+#endif
+#ifdef EGL_WL_bind_wayland_display
+   drv->API.BindWaylandDisplayWL = egl_g3d_bind_wayland_display_wl;
+   drv->API.UnbindWaylandDisplayWL = egl_g3d_unbind_wayland_display_wl;
+   drv->API.QueryWaylandBufferWL = egl_g3d_query_wayland_buffer_wl;
+#endif
+
+   drv->API.CreateSyncKHR = egl_g3d_create_sync;
+   drv->API.DestroySyncKHR = egl_g3d_destroy_sync;
+   drv->API.ClientWaitSyncKHR = egl_g3d_client_wait_sync;
+   drv->API.SignalSyncKHR = egl_g3d_signal_sync;
 
 #ifdef EGL_MESA_screen_surface
    drv->API.CreateScreenSurfaceMESA = egl_g3d_create_screen_surface;
    drv->API.ShowScreenSurfaceMESA = egl_g3d_show_screen_surface;
 #endif
+
+#ifdef EGL_NOK_swap_region
+   drv->API.SwapBuffersRegionNOK = egl_g3d_swap_buffers_region;
+#endif
+
+   drv->API.PostSubBufferNV = egl_g3d_post_sub_buffer;
 }