wayland: add shm buffer code
authornobled <nobled@dreamwidth.org>
Mon, 31 Jan 2011 04:22:50 +0000 (04:22 +0000)
committernobled <nobled@dreamwidth.org>
Tue, 1 Feb 2011 02:58:11 +0000 (02:58 +0000)
It's not enabled to do anything yet.

clutter/wayland/clutter-backend-wayland.c
clutter/wayland/clutter-backend-wayland.h
clutter/wayland/clutter-stage-wayland.c
clutter/wayland/clutter-stage-wayland.h

index 2fede4c..662a0ca 100644 (file)
@@ -175,6 +175,10 @@ display_handle_global (struct wl_display *display,
       wl_drm_add_listener (backend_wayland->wayland_drm,
                            &drm_listener, backend_wayland);
     }
+  else if (strcmp (interface, "shm") == 0)
+    {
+      backend_wayland->wayland_shm = wl_shm_create (display, id);
+    }
 }
 
 static gboolean
index 1d3f3d4..b9174b8 100644 (file)
@@ -67,6 +67,7 @@ struct _ClutterBackendWayland
   struct wl_compositor *wayland_compositor;
   struct wl_shell *wayland_shell;
   struct wl_drm *wayland_drm;
+  struct wl_shm *wayland_shm;
   char *device_name;
   int authenticated;
   struct wl_output *wayland_output;
index 521bea2..d516341 100644 (file)
 #include "config.h"
 #endif
 
+#include <fcntl.h>
+#include <glib.h>
+#include <glib/gstdio.h>
 #include <stdlib.h>
+#include <sys/mman.h>
+#include <unistd.h>
 #include <wayland-util.h>
 #include <wayland-client.h>
 #include <xf86drm.h>
@@ -84,6 +89,50 @@ get_visual (struct wl_display *display, CoglPixelFormat format)
       return NULL;
     }
 }
+static ClutterStageWaylandWaylandBuffer *
+wayland_create_shm_buffer (ClutterBackendWayland *backend_wayland,
+                           ClutterGeometry *geom)
+{
+  ClutterStageWaylandWaylandBufferSHM *buffer;
+  struct wl_visual *visual;
+  CoglTextureFlags flags = COGL_TEXTURE_NONE; /* XXX: tweak flags? */
+  CoglPixelFormat format = VISUAL_ARGB_PRE;
+  unsigned int stride;
+  int fd;
+  gchar tmp[] = "/tmp/clutter-wayland-shm-XXXXXX";
+
+  buffer = g_slice_new (ClutterStageWaylandWaylandBufferSHM);
+
+  buffer->buffer.type = BUFFER_TYPE_SHM;
+
+  /* XXX: can we query Cogl for what the stride should
+          be for a given format and width? */
+  stride = geom->width;
+  buffer->size = stride * geom->height;
+
+  fd = g_mkstemp_full(tmp, O_RDWR, 0600);
+  ftruncate(fd, buffer->size);
+  buffer->data = mmap(NULL, buffer->size, PROT_READ | PROT_WRITE,
+                     MAP_SHARED, fd, 0);
+
+  g_unlink(tmp);
+
+  buffer->buffer.tex = cogl_texture_new_from_data ((unsigned int)geom->width,
+                                              (unsigned int)geom->height,
+                                              flags, format,
+                                              COGL_PIXEL_FORMAT_ANY,
+                                              stride, buffer->data);
+
+  visual = get_visual (backend_wayland->wayland_display, format);
+  buffer->buffer.wayland_buffer =
+    wl_shm_create_buffer (backend_wayland->wayland_shm,
+                          fd,
+                          geom->width,
+                          geom->height,
+                          stride, visual);
+  close(fd);
+  return &buffer->buffer;
+}
 
 static ClutterStageWaylandWaylandBuffer *
 wayland_create_drm_buffer (ClutterBackendWayland *backend_wayland,
@@ -155,6 +204,17 @@ wayland_create_buffer (ClutterGeometry *geom)
 }
 
 static void
+wayland_free_shm_buffer (ClutterStageWaylandWaylandBuffer *generic_buffer)
+{
+  ClutterStageWaylandWaylandBufferSHM *buffer;
+
+  buffer = (ClutterStageWaylandWaylandBufferSHM *)generic_buffer;
+
+  munmap(buffer->data, buffer->size);
+  g_slice_free (ClutterStageWaylandWaylandBufferSHM, buffer);
+}
+
+static void
 wayland_free_drm_buffer (ClutterStageWaylandWaylandBuffer *generic_buffer)
 {
   ClutterBackend *backend = clutter_get_default_backend ();
@@ -178,6 +238,8 @@ wayland_free_buffer (ClutterStageWaylandWaylandBuffer *buffer)
 
   if (buffer->type == BUFFER_TYPE_DRM)
     wayland_free_drm_buffer(buffer);
+  else if (buffer->type == BUFFER_TYPE_SHM)
+    wayland_free_shm_buffer(buffer);
 }
 
 static void
index ebf1ecb..be7b915 100644 (file)
@@ -52,6 +52,7 @@ typedef struct _ClutterStageWayland         ClutterStageWayland;
 typedef struct _ClutterStageWaylandClass    ClutterStageWaylandClass;
 
 #define BUFFER_TYPE_DRM 1
+#define BUFFER_TYPE_SHM 2
 
 typedef struct _ClutterStageWaylandWaylandBuffer
 {
@@ -69,6 +70,13 @@ typedef struct _ClutterStageWaylandWaylandBufferDRM
   GLuint texture;
 } ClutterStageWaylandWaylandBufferDRM;
 
+typedef struct _ClutterStageWaylandWaylandBufferSHM
+{
+  ClutterStageWaylandWaylandBuffer buffer;
+  guint8 *data;
+  size_t size;
+} ClutterStageWaylandWaylandBufferSHM;
+
 struct _ClutterStageWayland
 {
   GObject parent_instance;