From d84f31ef3a9ad56463b34af74a4a5f68ef0621a4 Mon Sep 17 00:00:00 2001 From: nobled Date: Mon, 31 Jan 2011 04:22:50 +0000 Subject: [PATCH] wayland: add shm buffer code It's not enabled to do anything yet. --- clutter/wayland/clutter-backend-wayland.c | 4 ++ clutter/wayland/clutter-backend-wayland.h | 1 + clutter/wayland/clutter-stage-wayland.c | 62 +++++++++++++++++++++++ clutter/wayland/clutter-stage-wayland.h | 8 +++ 4 files changed, 75 insertions(+) diff --git a/clutter/wayland/clutter-backend-wayland.c b/clutter/wayland/clutter-backend-wayland.c index 2fede4c1e..662a0caed 100644 --- a/clutter/wayland/clutter-backend-wayland.c +++ b/clutter/wayland/clutter-backend-wayland.c @@ -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 diff --git a/clutter/wayland/clutter-backend-wayland.h b/clutter/wayland/clutter-backend-wayland.h index 1d3f3d49e..b9174b8d2 100644 --- a/clutter/wayland/clutter-backend-wayland.h +++ b/clutter/wayland/clutter-backend-wayland.h @@ -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; diff --git a/clutter/wayland/clutter-stage-wayland.c b/clutter/wayland/clutter-stage-wayland.c index 521bea2e5..d51634169 100644 --- a/clutter/wayland/clutter-stage-wayland.c +++ b/clutter/wayland/clutter-stage-wayland.c @@ -28,7 +28,12 @@ #include "config.h" #endif +#include +#include +#include #include +#include +#include #include #include #include @@ -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, @@ -154,6 +203,17 @@ wayland_create_buffer (ClutterGeometry *geom) return buffer; } +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) { @@ -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 diff --git a/clutter/wayland/clutter-stage-wayland.h b/clutter/wayland/clutter-stage-wayland.h index ebf1ecb8c..be7b915a6 100644 --- a/clutter/wayland/clutter-stage-wayland.h +++ b/clutter/wayland/clutter-stage-wayland.h @@ -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;