Switch to cairo-drm, drop struct buffer hacks.
authorKristian Høgsberg <krh@sasori.boston.redhat.com>
Thu, 15 Jan 2009 16:37:43 +0000 (11:37 -0500)
committerKristian Høgsberg <krh@sasori.boston.redhat.com>
Fri, 16 Jan 2009 12:49:07 +0000 (07:49 -0500)
Makefile.in
cairo-util.c
flower.c
gears.c
terminal.c
window.c
window.h

index 354c8d5..e17425a 100644 (file)
@@ -39,7 +39,7 @@ wayland-system-compositor :                   \
 wayland-system-compositor : CFLAGS += @EGL_COMPOSITOR_CFLAGS@
 wayland-system-compositor : LDLIBS += -L. -lwayland-server @EGL_COMPOSITOR_LIBS@ -rdynamic -lrt
 
-flower : flower.o wayland-glib.o cairo-util.o
+flower : flower.o wayland-glib.o
 gears : gears.o window.o wayland-glib.o cairo-util.o
 screenshot : screenshot.o wayland-glib.o
 terminal : terminal.o window.o wayland-glib.o cairo-util.o
index 2d23b5e..0dfa3ad 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
-#include <i915_drm.h>
-#include <sys/ioctl.h>
 #include <math.h>
 #include <cairo.h>
 #include "cairo-util.h"
 
-struct buffer *
-buffer_create(int fd, int width, int height, int stride)
-{
-       struct buffer *buffer;
-       struct drm_i915_gem_create create;
-       struct drm_gem_flink flink;
-
-       buffer = malloc(sizeof *buffer);
-       buffer->width = width;
-       buffer->height = height;
-       buffer->stride = stride;
-
-       memset(&create, 0, sizeof(create));
-       create.size = height * stride;
-
-       if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
-               fprintf(stderr, "gem create failed: %m\n");
-               free(buffer);
-               return NULL;
-       }
-
-       flink.handle = create.handle;
-       if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
-               fprintf(stderr, "gem flink failed: %m\n");
-               free(buffer);
-               return 0;
-       }
-
-       buffer->handle = flink.handle;
-       buffer->name = flink.name;
-
-       return buffer;
-}
-
-int
-buffer_destroy(struct buffer *buffer, int fd)
-{
-       struct drm_gem_close close;
-
-       close.handle = buffer->handle;
-       if (ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close) < 0) {
-               fprintf(stderr, "gem close failed: %m\n");
-               return -1;
-       }
-       
-       free(buffer);
-
-       return 0;
-}
-
-int
-buffer_data(struct buffer *buffer, int fd, void *data)
-{
-       struct drm_i915_gem_pwrite pwrite;
-
-       pwrite.handle = buffer->handle;
-       pwrite.offset = 0;
-       pwrite.size = buffer->height * buffer->stride;
-       pwrite.data_ptr = (uint64_t) (uintptr_t) data;
-
-       if (ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite) < 0) {
-               fprintf(stderr, "gem pwrite failed: %m\n");
-               return -1;
-       }
-
-       return 0;
-}
-
-struct buffer *
-buffer_create_from_cairo_surface(int fd, cairo_surface_t *surface)
-{
-       struct buffer *buffer;
-       int32_t width, height, stride;
-       void *data;
-
-       width = cairo_image_surface_get_width(surface);
-       height = cairo_image_surface_get_height(surface);
-       stride = cairo_image_surface_get_stride(surface);
-       data = cairo_image_surface_get_data(surface);
-
-       buffer = buffer_create(fd, width, height, stride);
-       if (buffer == NULL)
-               return NULL;
-
-       if (buffer_data(buffer, fd, data) < 0) {
-               buffer_destroy(buffer, fd);
-               return NULL;
-       }                       
-
-       return buffer;
-}
-
 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
 
 void
index e55433d..ce2cbc8 100644 (file)
--- a/flower.c
+++ b/flower.c
 #include <time.h>
 #include <cairo.h>
 #include <glib.h>
+#include <cairo-drm.h>
 
 #include "wayland-client.h"
 #include "wayland-glib.h"
-#include "cairo-util.h"
 
 static const char gem_device[] = "/dev/dri/card0";
 static const char socket_name[] = "\0wayland";
@@ -52,8 +52,8 @@ set_random_color(cairo_t *cr)
 }
 
 
-static void *
-draw_stuff(int width, int height)
+static void
+draw_stuff(cairo_surface_t *surface, int width, int height)
 {
        const int petal_count = 3 + random() % 5;
        const double r1 = 60 + random() % 35;
@@ -61,18 +61,14 @@ draw_stuff(int width, int height)
        const double u = (10 + random() % 90) / 100.0;
        const double v = (random() % 90) / 100.0;
 
-       cairo_surface_t *surface;
        cairo_t *cr;
        int i;
        double t, dt = 2 * M_PI / (petal_count * 2);
        double x1, y1, x2, y2, x3, y3;
 
-       surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
-                                            width, height);
-
        cr = cairo_create(surface);
        cairo_translate(cr, width / 2, height / 2);
-       cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
+       cairo_move_to(cr, cos(0) * r1, sin(0) * r1);
        for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
                x1 = cos(t) * r1;
                y1 = sin(t) * r1;
@@ -99,8 +95,6 @@ draw_stuff(int width, int height)
        cairo_stroke(cr);
 
        cairo_destroy(cr);
-
-       return surface;
 }
 
 struct flower {
@@ -141,12 +135,12 @@ int main(int argc, char *argv[])
        struct wl_display *display;
        struct wl_visual *visual;
        int fd;
+       cairo_drm_context_t *ctx;
        cairo_surface_t *s;
        struct timespec ts;
        GMainLoop *loop;
        GSource *source;
        struct flower flower;
-       struct buffer *buffer;
 
        fd = open(gem_device, O_RDWR);
        if (fd < 0) {
@@ -176,13 +170,17 @@ int main(int argc, char *argv[])
        srandom(ts.tv_nsec);
        flower.offset = random();
 
-       s = draw_stuff(flower.width, flower.height);
-       buffer = buffer_create_from_cairo_surface(fd, s);
+       ctx = cairo_drm_context_get_for_fd(fd);
+       s = cairo_drm_surface_create(ctx, CAIRO_CONTENT_COLOR_ALPHA,
+                                    flower.width, flower.height);
+       draw_stuff(s, flower.width, flower.height);
 
        visual = wl_display_get_premultiplied_argb_visual(display);
        wl_surface_attach(flower.surface,
-                         buffer->name, flower.width, flower.height,
-                         buffer->stride, visual);
+                         cairo_drm_surface_get_name(s),
+                         flower.width, flower.height,
+                         cairo_drm_surface_get_stride(s),
+                         visual);
 
        wl_compositor_add_listener(flower.compositor,
                                   &compositor_listener, &flower);
diff --git a/gears.c b/gears.c
index 91fa59d..63085bb 100644 (file)
--- a/gears.c
+++ b/gears.c
@@ -30,6 +30,7 @@
 #include <time.h>
 #include <cairo.h>
 #include <glib.h>
+#include <cairo-drm.h>
 
 #include <GL/gl.h>
 #include <eagle.h>
@@ -37,7 +38,6 @@
 #include "wayland-client.h"
 #include "wayland-glib.h"
 
-#include "cairo-util.h"
 #include "window.h"
 
 static const char gem_device[] = "/dev/dri/card0";
@@ -55,9 +55,8 @@ struct gears {
        EGLSurface surface;
        EGLContext context;
        int resized;
-       int fd;
        GLfloat angle;
-       struct buffer *buffer;
+       cairo_surface_t *cairo_surface;
 
        GLint gear_list[3];
 };
@@ -244,6 +243,8 @@ draw_gears(struct gears *gears)
 static void
 resize_window(struct gears *gears)
 {
+       uint32_t name, stride;
+
        /* Constrain child size to be square and at least 300x300 */
        window_get_child_rectangle(gears->window, &gears->rectangle);
        if (gears->rectangle.width > gears->rectangle.height)
@@ -258,20 +259,20 @@ resize_window(struct gears *gears)
 
        window_draw(gears->window);
 
-       if (gears->buffer != NULL)
-               buffer_destroy(gears->buffer, gears->fd);
+       if (gears->cairo_surface != NULL)
+               cairo_surface_destroy(gears->cairo_surface);
 
-       gears->buffer = buffer_create(gears->fd,
-                                     gears->rectangle.width,
-                                     gears->rectangle.height,
-                                     (gears->rectangle.width * 4 + 15) & ~15);
+       gears->cairo_surface = window_create_surface(gears->window,
+                                                    &gears->rectangle);
 
+       name = cairo_drm_surface_get_name(gears->cairo_surface);
+       stride = cairo_drm_surface_get_stride(gears->cairo_surface),
        gears->surface = eglCreateSurfaceForName(gears->display,
                                                 gears->config,
-                                                gears->buffer->name,
-                                                gears->buffer->width,
-                                                gears->buffer->height,
-                                                gears->buffer->stride, NULL);
+                                                name,
+                                                gears->rectangle.width,
+                                                gears->rectangle.height,
+                                                stride, NULL);
 
        eglMakeCurrent(gears->display,
                       gears->surface, gears->surface, gears->context);
@@ -321,9 +322,9 @@ handle_frame(void *data,
 {
        struct gears *gears = data;
 
-       window_copy(gears->window,
-                   &gears->rectangle,
-                   gears->buffer->name, gears->buffer->stride);
+       window_copy_surface(gears->window,
+                           &gears->rectangle,
+                           gears->cairo_surface);
 
        wl_compositor_commit(gears->compositor, 0);
 
@@ -352,7 +353,6 @@ gears_create(struct wl_display *display, int fd)
        gears = malloc(sizeof *gears);
        memset(gears, 0, sizeof *gears);
        gears->wl_display = display;
-       gears->fd = fd;
        gears->window = window_create(display, fd, "Wayland Gears",
                                      x, y, width, height);
 
index 61d5697..02a4f79 100644 (file)
@@ -33,6 +33,7 @@
 #include <cairo.h>
 #include <glib.h>
 #include <linux/input.h>
+#include <cairo-drm.h>
 
 #include <GL/gl.h>
 #include <eagle.h>
@@ -40,7 +41,6 @@
 #include "wayland-client.h"
 #include "wayland-glib.h"
 
-#include "cairo-util.h"
 #include "window.h"
 
 static int option_fullscreen;
@@ -59,7 +59,7 @@ struct terminal {
        char *data;
        int width, height, start, row, column;
        int fd, master;
-       struct buffer *buffer;
+       cairo_surface_t *surface;
        GIOChannel *channel;
        uint32_t modifiers;
        char escape[64];
@@ -128,16 +128,15 @@ static void
 terminal_draw_contents(struct terminal *terminal)
 {
        struct rectangle rectangle;
-       cairo_surface_t *surface;
        cairo_t *cr;
        cairo_font_extents_t extents;
        int i, top_margin, side_margin;
 
        window_get_child_rectangle(terminal->window, &rectangle);
 
-       surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
-                                            rectangle.width, rectangle.height);
-       cr = cairo_create(surface);
+       terminal->surface =
+               window_create_surface(terminal->window, &rectangle);
+       cr = cairo_create(terminal->surface);
        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
        cairo_set_source_rgba(cr, 0, 0, 0, 0.9);
        cairo_paint(cr);
@@ -160,12 +159,9 @@ terminal_draw_contents(struct terminal *terminal)
        }
        cairo_destroy(cr);
 
-       terminal->buffer = buffer_create_from_cairo_surface(terminal->fd, surface);
-       cairo_surface_destroy(surface);
-
-       window_copy(terminal->window,
-                   &rectangle,
-                   terminal->buffer->name, terminal->buffer->stride);
+       window_copy_surface(terminal->window,
+                           &rectangle,
+                           terminal->surface);
 }
 
 static void
@@ -405,7 +401,7 @@ handle_acknowledge(void *data,
 
        terminal->redraw_scheduled = 0;
        if (key == 0)
-               buffer_destroy(terminal->buffer, terminal->fd);
+               cairo_surface_destroy(terminal->surface);
 
        if (terminal->redraw_pending) {
                terminal->redraw_pending = 0;
@@ -553,7 +549,6 @@ terminal_create(struct wl_display *display, int fd, int fullscreen)
                return terminal;
 
        memset(terminal, 0, sizeof *terminal);
-       terminal->fd = fd;
        terminal->fullscreen = fullscreen;
        terminal->window = window_create(display, fd, "Wayland Terminal",
                                         500, 100, 500, 400);
index ce7823a..1c318e8 100644 (file)
--- a/window.c
+++ b/window.c
 #include <time.h>
 #include <cairo.h>
 #include <glib.h>
+#include <cairo-drm.h>
 
 #include <linux/input.h>
 #include "wayland-client.h"
 #include "wayland-glib.h"
 
-#include "cairo-util.h"
-
 #include "window.h"
 
 struct window {
@@ -52,9 +51,9 @@ struct window {
        int fullscreen;
        struct wl_input_device *grab_device;
        uint32_t name;
-       int fd;
+       cairo_drm_context_t *ctx;
 
-       struct buffer *buffer;
+       cairo_surface_t *cairo_surface;
 
        window_resize_handler_t resize_handler;
        window_key_handler_t key_handler;
@@ -78,7 +77,6 @@ rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
 static void
 window_draw_decorations(struct window *window)
 {
-       cairo_surface_t *surface;
        cairo_t *cr;
        int border = 2, radius = 5;
        cairo_text_extents_t extents;
@@ -86,15 +84,17 @@ window_draw_decorations(struct window *window)
        struct wl_visual *visual;
        int width, height;
 
-       surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
-                                            window->allocation.width,
-                                            window->allocation.height);
+       window->cairo_surface =
+               cairo_drm_surface_create(window->ctx,
+                                        CAIRO_CONTENT_COLOR_ALPHA,
+                                        window->allocation.width,
+                                        window->allocation.height);
 
        outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
        bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
        dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
 
-       cr = cairo_create(surface);
+       cr = cairo_create(window->cairo_surface);
 
        width = window->allocation.width - window->margin * 2;
        height = window->allocation.height - window->margin * 2;
@@ -104,7 +104,20 @@ window_draw_decorations(struct window *window)
        cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
        rounded_rect(cr, 0, 0, width, height, radius);
        cairo_fill(cr);
-       blur_surface(surface, 24 + radius);
+
+#ifdef SLOW_BUT_PWETTY
+       /* FIXME: Aw, pretty drop shadows now have to fallback to sw.
+        * Ideally we should have convolution filters in cairo, but we
+        * can also fallback to compositing the shadow image a bunch
+        * of times according to the blur kernel. */
+       {
+               cairo_surface_t *map;
+
+               map = cairo_drm_surface_map(window->cairo_surface);
+               blur_surface(map);
+               cairo_drm_surface_unmap(window->cairo_surface, map);
+       }
+#endif
 
        cairo_translate(cr, -7, -5);
        cairo_set_line_width (cr, border);
@@ -157,15 +170,12 @@ window_draw_decorations(struct window *window)
        cairo_fill(cr);
        cairo_destroy(cr);
 
-       window->buffer = buffer_create_from_cairo_surface(window->fd, surface);
-       cairo_surface_destroy(surface);
-
        visual = wl_display_get_premultiplied_argb_visual(window->display);
        wl_surface_attach(window->surface,
-                         window->buffer->name,
-                         window->buffer->width,
-                         window->buffer->height,
-                         window->buffer->stride,
+                         cairo_drm_surface_get_name(window->cairo_surface),
+                         window->allocation.width,
+                         window->allocation.height,
+                         cairo_drm_surface_get_stride(window->cairo_surface),
                          visual);
 
        wl_surface_map(window->surface,
@@ -179,19 +189,19 @@ static void
 window_draw_fullscreen(struct window *window)
 {
        struct wl_visual *visual;
-       int stride = window->allocation.width * 4;
 
-       window->buffer = buffer_create(window->fd,
-                                      window->allocation.width,
-                                      window->allocation.height,
-                                      stride);
+       window->cairo_surface =
+               cairo_drm_surface_create(window->ctx,
+                                        CAIRO_CONTENT_COLOR_ALPHA,
+                                        window->allocation.width,
+                                        window->allocation.height);
 
        visual = wl_display_get_premultiplied_argb_visual(window->display);
        wl_surface_attach(window->surface,
-                         window->buffer->name,
-                         window->buffer->width,
-                         window->buffer->height,
-                         window->buffer->stride,
+                         cairo_drm_surface_get_name(window->cairo_surface),
+                         window->allocation.width,
+                         window->allocation.height,
+                         cairo_drm_surface_get_stride(window->cairo_surface),
                          visual);
 
        wl_surface_map(window->surface,
@@ -222,9 +232,9 @@ window_handle_acknowledge(void *data,
         * safely free the old window buffer if we resized and
         * render the next frame into our back buffer.. */
 
-       if (key == 0 && window->buffer != NULL) {
-               buffer_destroy(window->buffer, window->fd);
-               window->buffer = NULL;
+       if (key == 0 && window->cairo_surface != NULL) {
+               cairo_surface_destroy(window->cairo_surface);
+               window->cairo_surface = NULL;
        }
 }
 
@@ -382,6 +392,16 @@ window_set_child_size(struct window *window,
        }
 }
 
+cairo_surface_t *
+window_create_surface(struct window *window,
+                     struct rectangle *rectangle)
+{
+       return cairo_drm_surface_create(window->ctx,
+                                       CAIRO_CONTENT_COLOR_ALPHA,
+                                       rectangle->width,
+                                       rectangle->height);
+}
+
 void
 window_copy(struct window *window,
            struct rectangle *rectangle,
@@ -397,6 +417,21 @@ window_copy(struct window *window,
 }
 
 void
+window_copy_surface(struct window *window,
+                   struct rectangle *rectangle,
+                   cairo_surface_t *surface)
+{
+       wl_surface_copy(window->surface,
+                       rectangle->x,
+                       rectangle->y,
+                       cairo_drm_surface_get_name(surface),
+                       cairo_drm_surface_get_stride(surface),
+                       0, 0,
+                       rectangle->width,
+                       rectangle->height);
+}
+
+void
 window_set_fullscreen(struct window *window, int fullscreen)
 {
        window->fullscreen = fullscreen;
@@ -488,7 +523,11 @@ window_create(struct wl_display *display, int fd,
        window->saved_allocation = window->allocation;
        window->margin = 16;
        window->state = WINDOW_STABLE;
-       window->fd = fd;
+       window->ctx = cairo_drm_context_get_for_fd(fd);
+       if (window->ctx == NULL) {
+               fprintf(stderr, "failed to get cairo drm context\n");
+               return NULL;
+       }
 
        wl_display_add_global_listener(display,
                                       window_handle_global, window);
index ffe81ad..9ec34f0 100644 (file)
--- a/window.h
+++ b/window.h
@@ -56,6 +56,15 @@ window_copy(struct window *window,
            struct rectangle *rectangle,
            uint32_t name, uint32_t stride);
 
+cairo_surface_t *
+window_create_surface(struct window *window,
+                     struct rectangle *rectangle);
+
+void
+window_copy_surface(struct window *window,
+                   struct rectangle *rectangle,
+                   cairo_surface_t *surface);
+
 void
 window_set_fullscreen(struct window *window, int fullscreen);