wayland: Destroy frame callback when destroying surface
[mesa.git] / src / gallium / state_trackers / egl / wayland / native_wayland.c
index d4d5f9c2ebb00591f9cc0324887dee8c512afdf7..560e40d4cee81a0df75ecd4eae2f2b08317815da 100644 (file)
 #include "pipe/p_context.h"
 #include "pipe/p_state.h"
 #include "state_tracker/drm_driver.h"
-
 #include "egllog.h"
 
 #include "native_wayland.h"
 
-/* see get_drm_screen_name */
-#include <radeon_drm.h>
-#include "radeon/drm/radeon_drm_public.h"
-
-#include <wayland-client.h>
-#include "wayland-egl-priv.h"
-
-#include <xf86drm.h>
-
-static struct native_event_handler *wayland_event_handler;
-
 static void
-sync_callback(void *data)
+sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
 {
    int *done = data;
 
    *done = 1;
+   wl_callback_destroy(callback);
 }
 
-static void
-force_roundtrip(struct wl_display *display)
+static const struct wl_callback_listener sync_listener = {
+   sync_callback
+};
+
+int
+wayland_roundtrip(struct wayland_display *display)
 {
-   int done = 0;
+   struct wl_callback *callback;
+   int done = 0, ret = 0;
 
-   wl_display_sync_callback(display, sync_callback, &done);
-   wl_display_iterate(display, WL_DISPLAY_WRITABLE);
-   while (!done)
-      wl_display_iterate(display, WL_DISPLAY_READABLE);
+   callback = wl_display_sync(display->dpy);
+   wl_callback_add_listener(callback, &sync_listener, &done);
+   wl_proxy_set_queue((struct wl_proxy *) callback, display->queue);
+   while (ret == 0 && !done)
+      ret = wl_display_dispatch_queue(display->dpy, display->queue);
+
+   return ret;
 }
 
+static const struct native_event_handler *wayland_event_handler;
+
+const static struct {
+   enum pipe_format format;
+   enum wayland_format_flag flag;
+} wayland_formats[] = {
+   { PIPE_FORMAT_B8G8R8A8_UNORM, HAS_ARGB8888 },
+   { PIPE_FORMAT_B8G8R8X8_UNORM, HAS_XRGB8888 },
+};
+
 static const struct native_config **
-wayland_display_get_configs (struct native_display *ndpy, int *num_configs)
+wayland_display_get_configs(struct native_display *ndpy, int *num_configs)
 {
    struct wayland_display *display = wayland_display(ndpy);
    const struct native_config **configs;
+   int i;
 
-   if (!display->config) {
+   if (!display->configs) {
       struct native_config *nconf;
-      enum pipe_format format;
-      display->config = CALLOC(1, sizeof(*display->config));
-      if (!display->config)
-         return NULL;
-      nconf = &display->config->base;
 
-      nconf->buffer_mask =
-         (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
-         (1 << NATIVE_ATTACHMENT_BACK_LEFT);
-
-      format = PIPE_FORMAT_B8G8R8A8_UNORM;
+      display->num_configs = 0;
+      display->configs = CALLOC(Elements(wayland_formats),
+                                sizeof(*display->configs));
+      if (!display->configs)
+         return NULL;
 
-      nconf->color_format = format;
-      nconf->window_bit = TRUE;
-      nconf->pixmap_bit = TRUE;
+      for (i = 0; i < Elements(wayland_formats); ++i) {
+         if (!(display->formats & wayland_formats[i].flag))
+            continue;
+
+         nconf = &display->configs[display->num_configs].base;
+         nconf->buffer_mask =
+            (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
+            (1 << NATIVE_ATTACHMENT_BACK_LEFT);
+         
+         nconf->window_bit = TRUE;
+         
+         nconf->color_format = wayland_formats[i].format;
+         display->num_configs++;
+      }
    }
 
-   configs = MALLOC(sizeof(*configs));
+   configs = MALLOC(display->num_configs * sizeof(*configs));
    if (configs) {
-      configs[0] = &display->config->base;
+      for (i = 0; i < display->num_configs; ++i)
+         configs[i] = &display->configs[i].base;
       if (num_configs)
-         *num_configs = 1;
+         *num_configs = display->num_configs;
    }
 
    return configs;
@@ -108,105 +123,29 @@ wayland_display_get_param(struct native_display *ndpy,
    int val;
 
    switch (param) {
-      case NATIVE_PARAM_USE_NATIVE_BUFFER:
-      case NATIVE_PARAM_PRESERVE_BUFFER:
-      case NATIVE_PARAM_MAX_SWAP_INTERVAL:
-      default:
-         val = 0;
-         break;
+   case NATIVE_PARAM_PREMULTIPLIED_ALPHA:
+      val = 1;
+      break;
+   case NATIVE_PARAM_USE_NATIVE_BUFFER:
+   case NATIVE_PARAM_PRESERVE_BUFFER:
+   case NATIVE_PARAM_MAX_SWAP_INTERVAL:
+   default:
+      val = 0;
+      break;
    }
 
    return val;
 }
 
-static boolean
-wayland_display_is_pixmap_supported(struct native_display *ndpy,
-                                    EGLNativePixmapType pix,
-                                    const struct native_config *nconf)
-{
-   /* all wl_egl_pixmaps are supported */
-
-   return TRUE;
-}
-
-static void 
-wayland_display_destroy(struct native_display *ndpy)
-{
-   struct wayland_display *display = wayland_display(ndpy);
-
-   if (display->config)
-      FREE(display->config);
-
-   if (display->base.screen)
-      display->base.screen->destroy(display->base.screen);
-
-   FREE(display);
-}
-
-
-static struct wl_buffer *
-wayland_create_buffer(struct wayland_surface *surface,
-                      enum native_attachment attachment)
-{
-   struct wayland_display *display = surface->display;
-   struct pipe_resource *resource;
-   struct winsys_handle wsh;
-   uint width, height;
-
-   resource = resource_surface_get_single_resource(surface->rsurf, attachment);
-   resource_surface_get_size(surface->rsurf, &width, &height);
-
-   wsh.type = DRM_API_HANDLE_TYPE_SHARED;
-   display->base.screen->resource_get_handle(display->base.screen, resource, &wsh);
-
-   pipe_resource_reference(&resource, NULL);
-
-   return wl_drm_create_buffer(display->dpy->drm, wsh.handle,
-                               width, height,
-                               wsh.stride, surface->win->visual);
-}
-
-static void
-wayland_pixmap_destroy(struct wl_egl_pixmap *egl_pixmap)
-{
-   struct pipe_resource *resource = egl_pixmap->driver_private;
-
-   assert(resource);
-
-   pipe_resource_reference(&resource, NULL);
-   
-   egl_pixmap->driver_private = NULL;
-   egl_pixmap->destroy = NULL;
-   egl_pixmap->name = 0;
-}
-
-static void
-wayland_pixmap_surface_intialize(struct wayland_surface *surface)
-{
-   struct native_display *ndpy = &surface->display->base;
-   struct pipe_resource *resource;
-   struct winsys_handle wsh;
-   const enum native_attachment front_natt = NATIVE_ATTACHMENT_FRONT_LEFT;
-
-   if (surface->pix->name > 0)
-      return;
-
-   resource = resource_surface_get_single_resource(surface->rsurf, front_natt);
-
-   wsh.type = DRM_API_HANDLE_TYPE_SHARED;
-   ndpy->screen->resource_get_handle(ndpy->screen, resource, &wsh);
-
-   surface->pix->name           = wsh.handle;
-   surface->pix->stride         = wsh.stride;
-   surface->pix->destroy        = wayland_pixmap_destroy;
-   surface->pix->driver_private = resource;
-}
-
 static void
-wayland_release_pending_resource(void *data)
+wayland_release_pending_resource(void *data,
+                                 struct wl_callback *callback,
+                                 uint32_t time)
 {
    struct wayland_surface *surface = data;
 
+   wl_callback_destroy(callback);
+
    /* FIXME: print internal error */
    if (!surface->pending_resource)
       return;
@@ -214,6 +153,10 @@ wayland_release_pending_resource(void *data)
    pipe_resource_reference(&surface->pending_resource, NULL);
 }
 
+static const struct wl_callback_listener release_buffer_listener = {
+   wayland_release_pending_resource
+};
+
 static void
 wayland_window_surface_handle_resize(struct wayland_surface *surface)
 {
@@ -221,20 +164,24 @@ wayland_window_surface_handle_resize(struct wayland_surface *surface)
    struct pipe_resource *front_resource;
    const enum native_attachment front_natt = NATIVE_ATTACHMENT_FRONT_LEFT;
    int i;
-   
+
    front_resource = resource_surface_get_single_resource(surface->rsurf,
                                                          front_natt);
    if (resource_surface_set_size(surface->rsurf,
-       surface->win->width, surface->win->height)) {
+                                 surface->win->width, surface->win->height)) {
 
       if (surface->pending_resource)
-         force_roundtrip(display->dpy->display);
+         wayland_roundtrip(display);
 
       if (front_resource) {
+         struct wl_callback *callback;
+
          surface->pending_resource = front_resource;
          front_resource = NULL;
-         wl_display_sync_callback(display->dpy->display,
-                                  wayland_release_pending_resource, surface);
+
+         callback = wl_display_sync(display->dpy);
+         wl_callback_add_listener(callback, &release_buffer_listener, surface);
+         wl_proxy_set_queue((struct wl_proxy *) callback, display->queue);
       }
 
       for (i = 0; i < WL_BUFFER_COUNT; ++i) {
@@ -242,13 +189,11 @@ wayland_window_surface_handle_resize(struct wayland_surface *surface)
             wl_buffer_destroy(surface->buffer[i]);
          surface->buffer[i] = NULL;
       }
+
+      surface->dx = surface->win->dx;
+      surface->dy = surface->win->dy;
    }
    pipe_resource_reference(&front_resource, NULL);
-
-   surface->dx = surface->win->dx;
-   surface->dy = surface->win->dy;
-   surface->win->dx = 0;
-   surface->win->dy = 0;
 }
 
 static boolean
@@ -273,20 +218,23 @@ wayland_surface_validate(struct native_surface *nsurf, uint attachment_mask,
 
    resource_surface_get_size(surface->rsurf, (uint *) width, (uint *) height);
 
-   if (surface->type == WL_PIXMAP_SURFACE)
-      wayland_pixmap_surface_intialize(surface);
-
    return TRUE;
 }
 
 static void
-wayland_frame_callback(void *data, uint32_t time)
+wayland_frame_callback(void *data, struct wl_callback *callback, uint32_t time)
 {
    struct wayland_surface *surface = data;
 
-   surface->block_swap_buffers = FALSE;
+   surface->frame_callback = NULL;
+
+   wl_callback_destroy(callback);
 }
 
+static const struct wl_callback_listener frame_listener = {
+   wayland_frame_callback
+};
+
 static INLINE void
 wayland_buffers_swap(struct wl_buffer **buffer,
                      enum wayland_buffer_type buf1,
@@ -302,27 +250,33 @@ wayland_surface_swap_buffers(struct native_surface *nsurf)
 {
    struct wayland_surface *surface = wayland_surface(nsurf);
    struct wayland_display *display = surface->display;
+   int ret = 0;
 
-   while (surface->block_swap_buffers)
-      wl_display_iterate(display->dpy->display, WL_DISPLAY_READABLE);
+   while (surface->frame_callback && ret != -1)
+      ret = wl_display_dispatch_queue(display->dpy, display->queue);
+   if (ret == -1)
+      return EGL_FALSE;
 
-   surface->block_swap_buffers = TRUE;
-   wl_display_frame_callback(display->dpy->display, wayland_frame_callback,
-                             surface);
+   surface->frame_callback = wl_surface_frame(surface->win->surface);
+   wl_callback_add_listener(surface->frame_callback, &frame_listener, surface);
+   wl_proxy_set_queue((struct wl_proxy *) surface->frame_callback,
+                      display->queue);
 
    if (surface->type == WL_WINDOW_SURFACE) {
       resource_surface_swap_buffers(surface->rsurf,
-       NATIVE_ATTACHMENT_FRONT_LEFT, NATIVE_ATTACHMENT_BACK_LEFT, FALSE);
+                                    NATIVE_ATTACHMENT_FRONT_LEFT,
+                                    NATIVE_ATTACHMENT_BACK_LEFT, FALSE);
 
       wayland_buffers_swap(surface->buffer, WL_BUFFER_FRONT, WL_BUFFER_BACK);
 
       if (surface->buffer[WL_BUFFER_FRONT] == NULL)
          surface->buffer[WL_BUFFER_FRONT] =
-       wayland_create_buffer(surface, NATIVE_ATTACHMENT_FRONT_LEFT);
+            display->create_buffer(display, surface,
+                                   NATIVE_ATTACHMENT_FRONT_LEFT);
 
       wl_surface_attach(surface->win->surface, surface->buffer[WL_BUFFER_FRONT],
                         surface->dx, surface->dy);
-     
+
       resource_surface_get_size(surface->rsurf,
                                 (uint *) &surface->win->attached_width,
                                 (uint *) &surface->win->attached_height);
@@ -332,25 +286,38 @@ wayland_surface_swap_buffers(struct native_surface *nsurf)
 
    surface->sequence_number++;
    wayland_event_handler->invalid_surface(&display->base,
-        &surface->base, surface->sequence_number);
+                                          &surface->base,
+                                          surface->sequence_number);
 
    return TRUE;
 }
 
 static boolean
 wayland_surface_present(struct native_surface *nsurf,
-                        enum native_attachment natt,
-                        boolean preserve,
-                        uint swap_interval)
+                        const struct native_present_control *ctrl)
 {
    struct wayland_surface *surface = wayland_surface(nsurf);
    uint width, height;
    boolean ret;
 
-   if (preserve || swap_interval)
+   if (ctrl->preserve || ctrl->swap_interval)
       return FALSE;
 
-   switch (natt) {
+   /* force buffers to be re-created if they will be presented differently */
+   if (surface->premultiplied_alpha != ctrl->premultiplied_alpha) {
+      enum wayland_buffer_type buffer;
+
+      for (buffer = 0; buffer < WL_BUFFER_COUNT; ++buffer) {
+         if (surface->buffer[buffer]) {
+            wl_buffer_destroy(surface->buffer[buffer]);
+            surface->buffer[buffer] = NULL;
+         }
+      }
+
+      surface->premultiplied_alpha = ctrl->premultiplied_alpha;
+   }
+
+   switch (ctrl->natt) {
    case NATIVE_ATTACHMENT_FRONT_LEFT:
       ret = TRUE;
       break;
@@ -365,6 +332,7 @@ wayland_surface_present(struct native_surface *nsurf,
    if (surface->type == WL_WINDOW_SURFACE) {
       resource_surface_get_size(surface->rsurf, &width, &height);
       wl_surface_damage(surface->win->surface, 0, 0, width, height);
+      wl_surface_commit(surface->win->surface);
    }
 
    return ret;
@@ -387,63 +355,13 @@ wayland_surface_destroy(struct native_surface *nsurf)
          wl_buffer_destroy(surface->buffer[buffer]);
    }
 
+   if (surface->frame_callback)
+      wl_callback_destroy(surface->frame_callback);
+
    resource_surface_destroy(surface->rsurf);
    FREE(surface);
 }
 
-static struct native_surface *
-wayland_create_pixmap_surface(struct native_display *ndpy,
-                              EGLNativePixmapType pix,
-                              const struct native_config *nconf)
-{
-   struct wayland_display *display = wayland_display(ndpy);
-   struct wayland_config *config = wayland_config(nconf);
-   struct wayland_surface *surface;
-   struct wl_egl_pixmap *egl_pixmap = (struct wl_egl_pixmap *) pix;
-   enum native_attachment natt = NATIVE_ATTACHMENT_FRONT_LEFT;
-
-   surface = CALLOC_STRUCT(wayland_surface);
-   if (!surface)
-      return NULL;
-
-   surface->display = display;
-
-   surface->pending_resource = NULL;
-   surface->type = WL_PIXMAP_SURFACE;
-   surface->pix = egl_pixmap;
-
-   if (surface->pix->visual == wl_display_get_rgb_visual(display->dpy->display))
-      surface->color_format = PIPE_FORMAT_B8G8R8X8_UNORM;
-   else
-      surface->color_format = PIPE_FORMAT_B8G8R8A8_UNORM;
-
-   surface->attachment_mask = (1 << NATIVE_ATTACHMENT_FRONT_LEFT);
-   
-   surface->rsurf = resource_surface_create(display->base.screen,
-           surface->color_format,
-           PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW |
-           PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT);
-
-   if (!surface->rsurf) {
-       FREE(surface);
-       return NULL;
-   }
-
-   resource_surface_set_size(surface->rsurf,
-                             egl_pixmap->width, egl_pixmap->height);
-
-   /* the pixmap is already allocated, so import it */
-   if (surface->pix->name > 0)
-      resource_surface_import_resource(surface->rsurf, natt,
-                                       surface->pix->driver_private);
-
-   surface->base.destroy = wayland_surface_destroy;
-   surface->base.present = wayland_surface_present;
-   surface->base.validate = wayland_surface_validate;
-   surface->base.wait = wayland_surface_wait;
-
-   return &surface->base;
-}
 
 static struct native_surface *
 wayland_create_window_surface(struct native_display *ndpy,
@@ -453,6 +371,8 @@ wayland_create_window_surface(struct native_display *ndpy,
    struct wayland_display *display = wayland_display(ndpy);
    struct wayland_config *config = wayland_config(nconf);
    struct wayland_surface *surface;
+   uint bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW |
+      PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT;
 
    surface = CALLOC_STRUCT(wayland_surface);
    if (!surface)
@@ -464,22 +384,20 @@ wayland_create_window_surface(struct native_display *ndpy,
    surface->win = (struct wl_egl_window *) win;
 
    surface->pending_resource = NULL;
-   surface->block_swap_buffers = FALSE;
+   surface->frame_callback = NULL;
    surface->type = WL_WINDOW_SURFACE;
 
    surface->buffer[WL_BUFFER_FRONT] = NULL;
    surface->buffer[WL_BUFFER_BACK] = NULL;
    surface->attachment_mask = (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
-                              (1 << NATIVE_ATTACHMENT_BACK_LEFT);
+      (1 << NATIVE_ATTACHMENT_BACK_LEFT);
 
    surface->rsurf = resource_surface_create(display->base.screen,
-           surface->color_format,
-           PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW |
-           PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT);
+                                            surface->color_format, bind);
 
    if (!surface->rsurf) {
-       FREE(surface);
-       return NULL;
+      FREE(surface);
+      return NULL;
    }
 
    surface->base.destroy = wayland_surface_destroy;
@@ -490,137 +408,52 @@ wayland_create_window_surface(struct native_display *ndpy,
    return &surface->base;
 }
 
-static const char *
-get_drm_screen_name(int fd, drmVersionPtr version)
+static struct native_display *
+native_create_display(void *dpy, boolean use_sw)
 {
-   const char *name = version->name;
+   struct wayland_display *display = NULL;
+   boolean own_dpy = FALSE;
 
-   if (name && !strcmp(name, "radeon")) {
-      int chip_id;
-      struct drm_radeon_info info;
+   use_sw = use_sw || debug_get_bool_option("EGL_SOFTWARE", FALSE);
 
-      memset(&info, 0, sizeof(info));
-      info.request = RADEON_INFO_DEVICE_ID;
-      info.value = pointer_to_intptr(&chip_id);
-      if (drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info)) != 0)
+   if (dpy == NULL) {
+      dpy = wl_display_connect(NULL);
+      if (dpy == NULL)
          return NULL;
-
-      name = is_r3xx(chip_id) ? "r300" : "r600";
+      own_dpy = TRUE;
    }
 
-   return name;
-}
-
-static boolean
-wayland_display_init_screen(struct native_display *ndpy)
-{
-   struct wayland_display *display = wayland_display(ndpy);
-   drmVersionPtr version;
-   const char *driver_name;
-
-   if (display->dpy->fd == -1)
-          force_roundtrip(display->dpy->display);
-   if (display->dpy->fd == -1)
-          return FALSE;
-
-   if (!display->dpy->authenticated)
-          force_roundtrip(display->dpy->display);
-   if (!display->dpy->authenticated)
-          return FALSE;
-
-   version = drmGetVersion(display->dpy->fd);
-   if (!version) {
-      _eglLog(_EGL_WARNING, "invalid fd %d", display->dpy->fd);
-      return FALSE;
-   }
-
-   /* FIXME: share this with native_drm or egl_dri2 */
-   driver_name = get_drm_screen_name(display->dpy->fd, version);
-
-   display->base.screen =
-      wayland_event_handler->new_drm_screen(&display->base,
-            driver_name, display->dpy->fd);
-   drmFreeVersion(version);
-
-   if (!display->base.screen) {
-      _eglLog(_EGL_WARNING, "failed to create DRM screen");
-      return FALSE;
+   if (use_sw) {
+      _eglLog(_EGL_INFO, "use software fallback");
+      display = wayland_create_shm_display((struct wl_display *) dpy,
+                                           wayland_event_handler);
+   } else {
+      display = wayland_create_drm_display((struct wl_display *) dpy,
+                                           wayland_event_handler);
    }
 
-   return TRUE;
-}
-
-
-static void
-wayland_set_event_handler(struct native_event_handler *event_handler)
-{
-   wayland_event_handler = event_handler;
-}
-
-static struct pipe_resource *
-wayland_display_import_buffer(struct native_display *ndpy,
-                              const struct pipe_resource *templ,
-                              void *buf)
-{
-   return ndpy->screen->resource_from_handle(ndpy->screen,
-         templ, (struct winsys_handle *) buf);
-}
-
-static boolean
-wayland_display_export_buffer(struct native_display *ndpy,
-                              struct pipe_resource *res,
-                              void *buf)
-{
-   return ndpy->screen->resource_get_handle(ndpy->screen,
-         res, (struct winsys_handle *) buf);
-}
-
-static struct native_display_buffer wayland_display_buffer = {
-   wayland_display_import_buffer,
-   wayland_display_export_buffer
-};
-
-static struct native_display *
-wayland_display_create(void *dpy, boolean use_sw, void *user_data)
-{
-   struct wayland_display *display;
-
-   display = CALLOC_STRUCT(wayland_display);
    if (!display)
       return NULL;
 
-   display->base.user_data = user_data;
-
-   display->dpy = dpy;
-   if (!display->dpy->display) {
-      wayland_display_destroy(&display->base);
-      return NULL;
-   }
-
-   if (!wayland_display_init_screen(&display->base)) {
-      wayland_display_destroy(&display->base);
-      return NULL;
-   }
-
-   display->base.destroy = wayland_display_destroy;
    display->base.get_param = wayland_display_get_param;
    display->base.get_configs = wayland_display_get_configs;
-   display->base.is_pixmap_supported = wayland_display_is_pixmap_supported;
    display->base.create_window_surface = wayland_create_window_surface;
-   display->base.create_pixmap_surface = wayland_create_pixmap_surface;
-   display->base.buffer = &wayland_display_buffer;
+
+   display->own_dpy = own_dpy;
 
    return &display->base;
 }
 
 static const struct native_platform wayland_platform = {
    "wayland", /* name */
-   wayland_set_event_handler,
-   wayland_display_create
+   native_create_display
 };
 
 const struct native_platform *
-native_get_wayland_platform(void)
+native_get_wayland_platform(const struct native_event_handler *event_handler)
 {
+   wayland_event_handler = event_handler;
    return &wayland_platform;
 }
+
+/* vim: set sw=3 ts=8 sts=3 expandtab: */