wayland-drm: Drop the non-premul formats, use format codes from drm_fourcc.h
[mesa.git] / src / gallium / state_trackers / egl / wayland / native_shm.c
index 8614a761abf29a7fa76dea60f379f40e4e03ba14..b76e5db33db734073792421032734dff243961af 100644 (file)
@@ -47,7 +47,7 @@
 struct wayland_shm_display {
    struct wayland_display base;
 
-   struct native_event_handler *event_handler;
+   const struct native_event_handler *event_handler;
    struct wl_shm *wl_shm;
 };
 
@@ -63,8 +63,8 @@ wayland_shm_display_destroy(struct native_display *ndpy)
 {
    struct wayland_shm_display *shmdpy = wayland_shm_display(ndpy);
 
-   if (shmdpy->base.config)
-      FREE(shmdpy->base.config);
+   if (shmdpy->base.configs)
+      FREE(shmdpy->base.configs);
    if (shmdpy->base.own_dpy)
       wl_display_destroy(shmdpy->base.dpy);
 
@@ -73,7 +73,6 @@ wayland_shm_display_destroy(struct native_display *ndpy)
    FREE(shmdpy);
 }
 
-
 static struct wl_buffer *
 wayland_create_shm_buffer(struct wayland_display *display,
                           struct wayland_surface *surface,
@@ -84,7 +83,7 @@ wayland_create_shm_buffer(struct wayland_display *display,
    struct pipe_resource *resource;
    struct winsys_handle wsh;
    uint width, height;
-   struct wl_visual *visual;
+   enum wl_shm_format format;
 
    resource = resource_surface_get_single_resource(surface->rsurf, attachment);
    resource_surface_get_size(surface->rsurf, &width, &height);
@@ -93,22 +92,43 @@ wayland_create_shm_buffer(struct wayland_display *display,
 
    pipe_resource_reference(&resource, NULL);
 
-   switch (surface->type) {
-   case WL_WINDOW_SURFACE:
-      visual = surface->win->visual;
+   switch (surface->color_format) {
+   case PIPE_FORMAT_B8G8R8A8_UNORM:
+      format = (surface->premultiplied_alpha) ?
+         WL_SHM_FORMAT_PREMULTIPLIED_ARGB32 : WL_SHM_FORMAT_ARGB32;
       break;
-   case WL_PIXMAP_SURFACE:
-      visual = surface->pix->visual;
+   case PIPE_FORMAT_B8G8R8X8_UNORM:
+      format = WL_SHM_FORMAT_XRGB32;
       break;
    default:
       return NULL;
+      break;
    }
 
    return wl_shm_create_buffer(shmdpy->wl_shm, wsh.fd,
                                width, height,
-                               wsh.stride, visual);
+                               wsh.stride, format);
+}
+
+static void
+shm_handle_format(void *data, struct wl_shm *shm, uint32_t format)
+{
+   struct wayland_shm_display *shmdpy = data;
+
+   switch (format) {
+   case WL_SHM_FORMAT_ARGB32:
+      shmdpy->base.formats |= HAS_ARGB8888;
+      break;
+   case WL_SHM_FORMAT_XRGB8888:
+      shmdpy->base.formats |= HAS_XRGB8888;
+      break;
+   }
 }
 
+static const struct wl_shm_listener shm_listener = {
+   shm_handle_format
+};
+
 static boolean
 wayland_shm_display_init_screen(struct native_display *ndpy)
 {
@@ -123,10 +143,17 @@ wayland_shm_display_init_screen(struct native_display *ndpy)
    if (id == 0)
       return FALSE;
 
-   shmdpy->wl_shm = wl_shm_create(shmdpy->base.dpy, id, 1);
+   shmdpy->wl_shm = wl_display_bind(shmdpy->base.dpy, id, &wl_shm_interface);
    if (!shmdpy->wl_shm)
       return FALSE;
 
+   wl_shm_add_listener(shmdpy->wl_shm, &shm_listener, shmdpy);
+
+   if (shmdpy->base.formats == 0)
+      wl_display_roundtrip(shmdpy->base.dpy);
+   if (shmdpy->base.formats == 0)
+      return FALSE;
+
    winsys = wayland_create_sw_winsys(shmdpy->base.dpy);
    if (!winsys)
       return FALSE;
@@ -144,8 +171,7 @@ wayland_shm_display_init_screen(struct native_display *ndpy)
 
 struct wayland_display *
 wayland_create_shm_display(struct wl_display *dpy,
-                           struct native_event_handler *event_handler,
-                           void *user_data)
+                           const struct native_event_handler *event_handler)
 {
    struct wayland_shm_display *shmdpy;
 
@@ -154,7 +180,6 @@ wayland_create_shm_display(struct wl_display *dpy,
       return NULL;
 
    shmdpy->event_handler = event_handler;
-   shmdpy->base.base.user_data = user_data;
 
    shmdpy->base.dpy = dpy;
    if (!shmdpy->base.dpy) {
@@ -162,11 +187,7 @@ wayland_create_shm_display(struct wl_display *dpy,
       return NULL;
    }
 
-   if (!wayland_shm_display_init_screen(&shmdpy->base.base)) {
-      wayland_shm_display_destroy(&shmdpy->base.base);
-      return NULL;
-   }
-
+   shmdpy->base.base.init_screen = wayland_shm_display_init_screen;
    shmdpy->base.base.destroy = wayland_shm_display_destroy;
    shmdpy->base.create_buffer = wayland_create_shm_buffer;