egl/gbm: Fix EGL_DEFAULT_DISPLAY
[mesa.git] / src / gallium / state_trackers / egl / common / egl_g3d_api.c
index 8e53e1dccbaecba3a450471f7b84aee67f0e79b5..f897054a540fbdba9d98b9e6c09037396734e7d1 100644 (file)
@@ -37,7 +37,6 @@
 #include "egl_g3d_image.h"
 #include "egl_g3d_sync.h"
 #include "egl_g3d_st.h"
-#include "egl_g3d_loader.h"
 #include "native.h"
 
 /**
@@ -47,7 +46,6 @@ static struct st_api *
 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 api = -1;
 
@@ -81,96 +79,66 @@ egl_g3d_choose_st(_EGLDriver *drv, _EGLContext *ctx,
       break;
    }
 
-   switch (api) {
-   case ST_API_OPENGL:
-      stapi = gdrv->loader->guess_gl_api(*profile);
-      break;
-   case ST_API_OPENVG:
-      stapi = gdrv->loader->get_st_api(api);
-      break;
-   default:
-      stapi = NULL;
-      break;
-   }
+   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)
 {
-   const _EGLConfig *criteria = (const _EGLConfig *) 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, const _EGLConfig *criteria)
+egl_g3d_match_config(const _EGLConfig *conf, void *priv_data)
 {
-   if (!_eglMatchConfig(conf, criteria))
-      return EGL_FALSE;
-
-   if (criteria->MatchNativePixmap != EGL_NONE &&
-       criteria->MatchNativePixmap != EGL_DONT_CARE) {
-      struct egl_g3d_display *gdpy = egl_g3d_display(conf->Display);
-      struct egl_g3d_config *gconf = egl_g3d_config(conf);
-      EGLNativePixmapType pix =
-         (EGLNativePixmapType) criteria->MatchNativePixmap;
+   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 (!gdpy->native->is_pixmap_supported(gdpy->native, pix, gconf->native))
-         return EGL_FALSE;
-   }
+   if (data->format != PIPE_FORMAT_NONE &&
+       data->format != gconf->native->color_format)
+      return EGL_FALSE;
 
-   return EGL_TRUE;
+   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)
 {
-   _EGLConfig **tmp_configs, criteria;
-   EGLint tmp_size, i;
-
-   if (!num_configs)
-      return _eglError(EGL_BAD_PARAMETER, "eglChooseConfigs");
+   struct egl_g3d_choose_config_data data;
 
-   if (!_eglParseConfigAttribList(&criteria, dpy, attribs))
+   if (!_eglParseConfigAttribList(&data.criteria, dpy, attribs))
       return _eglError(EGL_BAD_ATTRIBUTE, "eglChooseConfig");
 
-   /* get the number of matched configs */
-   tmp_size = _eglFilterArray(dpy->Configs, NULL, 0,
-         (_EGLArrayForEach) egl_g3d_match_config, (void *) &criteria);
-   if (!tmp_size) {
-      *num_configs = tmp_size;
-      return EGL_TRUE;
-   }
+   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);
 
-   tmp_configs = MALLOC(sizeof(tmp_configs[0]) * tmp_size);
-   if (!tmp_configs)
-      return _eglError(EGL_BAD_ALLOC, "eglChooseConfig(out of memory)");
-
-   /* get the matched configs */
-   _eglFilterArray(dpy->Configs, (void **) tmp_configs, tmp_size,
-         (_EGLArrayForEach) egl_g3d_match_config, (void *) &criteria);
-
-   /* perform sorting of configs */
-   if (tmp_configs && tmp_size) {
-      _eglSortConfigs((const _EGLConfig **) tmp_configs, tmp_size,
-            egl_g3d_compare_config, (void *) &criteria);
-      size = MIN2(tmp_size, size);
-      for (i = 0; i < size; i++)
-         configs[i] = _eglGetConfigHandle(tmp_configs[i]);
+      if (!gdpy->native->get_pixmap_format(gdpy->native,
+               (EGLNativePixmapType) data.criteria.MatchNativePixmap,
+               &data.format))
+         return _eglError(EGL_BAD_NATIVE_PIXMAP, "eglChooseConfig");
    }
 
-   FREE(tmp_configs);
-
-   *num_configs = size;
-
-   return EGL_TRUE;
+   return _eglFilterConfigArray(dpy->Configs, configs, size, num_configs,
+         egl_g3d_match_config, egl_g3d_compare_config, &data);
 }
 
 static _EGLContext *
@@ -324,7 +292,8 @@ 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;
 
    gsurf->stfbi = egl_g3d_create_st_framebuffer(&gsurf->base);
@@ -402,7 +371,6 @@ egl_g3d_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *dpy,
                                _EGLConfig *conf, const EGLint *attribs)
 {
    struct egl_g3d_surface *gsurf;
-   struct pipe_resource *ptex = NULL;
 
    gsurf = create_pbuffer_surface(dpy, conf, attribs,
          "eglCreatePbufferSurface");
@@ -411,13 +379,6 @@ egl_g3d_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *dpy,
 
    gsurf->client_buffer_type = EGL_NONE;
 
-   if (!gsurf->stfbi->validate(gsurf->stfbi,
-            &gsurf->stvis.render_buffer, 1, &ptex)) {
-      egl_g3d_destroy_st_framebuffer(gsurf->stfbi);
-      FREE(gsurf);
-      return NULL;
-   }
-
    return &gsurf->base;
 }
 
@@ -477,12 +438,14 @@ egl_g3d_create_pbuffer_from_client_buffer(_EGLDriver *drv, _EGLDisplay *dpy,
    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;
 }
@@ -533,8 +496,7 @@ egl_g3d_make_current(_EGLDriver *drv, _EGLDisplay *dpy,
    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) {
@@ -542,19 +504,12 @@ egl_g3d_make_current(_EGLDriver *drv, _EGLDisplay *dpy,
             (gdraw) ? gdraw->stfbi : NULL, (gread) ? gread->stfbi : NULL);
       if (ok) {
          if (gdraw) {
-            gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi,
-                  gdraw->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 (gread && gread != gdraw) {
-            gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi,
-                  gread->stfbi);
-         }
       }
    }
    else if (old_gctx) {
@@ -611,8 +566,7 @@ 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->present(gsurf->native,
@@ -621,21 +575,6 @@ egl_g3d_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
          gsurf->base.SwapInterval);
 }
 
-/**
- * Get the pipe surface of the given attachment of the native surface.
- */
-static struct pipe_resource *
-get_pipe_resource(struct native_display *ndpy, struct native_surface *nsurf,
-                  enum native_attachment natt)
-{
-   struct pipe_resource *textures[NUM_NATIVE_ATTACHMENTS];
-
-   textures[natt] = NULL;
-   nsurf->validate(nsurf, 1 << natt, NULL, textures, NULL, NULL);
-
-   return textures[natt];
-}
-
 static EGLBoolean
 egl_g3d_copy_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
                      EGLNativePixmapType target)
@@ -643,54 +582,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_resource *ptex;
 
    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);
-   }
-
-   /* 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;
+      gctx->stctxi->flush(gctx->stctxi, ST_FLUSH_FRONT, NULL);
    }
 
-   ptex = get_pipe_resource(gdpy->native, nsurf, NATIVE_ATTACHMENT_FRONT_LEFT);
-   if (ptex) {
-      struct pipe_resource *psrc = gsurf->render_texture;
-      struct pipe_box src_box;
-      u_box_origin_2d(ptex->width0, ptex->height0, &src_box);
-      if (psrc) {
-         gdpy->pipe->resource_copy_region(gdpy->pipe, ptex, 0, 0, 0, 0,
-               gsurf->render_texture, 0, &src_box);
-         nsurf->present(nsurf, NATIVE_ATTACHMENT_FRONT_LEFT, FALSE, 0);
-      }
-
-      pipe_resource_reference(&ptex, NULL);
-   }
-
-   nsurf->destroy(nsurf);
-
-   return EGL_TRUE;
+   return gdpy->native->copy_to_pixmap(gdpy->native,
+         target, gsurf->render_texture);
 }
 
 static EGLBoolean
@@ -701,10 +604,9 @@ 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);
+   gctx->stctxi->flush(gctx->stctxi, ST_FLUSH_FRONT, &fence);
    if (fence) {
-      screen->fence_finish(screen, fence, 0);
+      screen->fence_finish(screen, fence, PIPE_TIMEOUT_INFINITE);
       screen->fence_reference(screen, &fence, NULL);
    }
 
@@ -773,8 +675,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);
@@ -888,25 +789,34 @@ 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->Configs->Size; i++) {
-      gconf = egl_g3d_config((_EGLConfig *) dpy->Configs->Elements[i]);
-      if (gdpy->native->is_pixmap_supported(gdpy->native, pix, gconf->native))
-         break;
-   }
+   if (!gdpy->native->wayland_bufmgr)
+      return EGL_FALSE;
 
-   return (i < dpy->Configs->Size) ? &gconf->base : NULL;
+   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);
+}
+
+#endif /* EGL_WL_bind_wayland_display */
+
 void
 egl_g3d_init_driver_api(_EGLDriver *drv)
 {
@@ -936,6 +846,11 @@ egl_g3d_init_driver_api(_EGLDriver *drv)
    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;
+
+#endif
 
 #ifdef EGL_KHR_reusable_sync
    drv->API.CreateSyncKHR = egl_g3d_create_sync;