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
#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
#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";
}
-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;
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;
cairo_stroke(cr);
cairo_destroy(cr);
-
- return surface;
}
struct flower {
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) {
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);
#include <time.h>
#include <cairo.h>
#include <glib.h>
+#include <cairo-drm.h>
#include <GL/gl.h>
#include <eagle.h>
#include "wayland-client.h"
#include "wayland-glib.h"
-#include "cairo-util.h"
#include "window.h"
static const char gem_device[] = "/dev/dri/card0";
EGLSurface surface;
EGLContext context;
int resized;
- int fd;
GLfloat angle;
- struct buffer *buffer;
+ cairo_surface_t *cairo_surface;
GLint gear_list[3];
};
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)
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);
{
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);
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);
#include <cairo.h>
#include <glib.h>
#include <linux/input.h>
+#include <cairo-drm.h>
#include <GL/gl.h>
#include <eagle.h>
#include "wayland-client.h"
#include "wayland-glib.h"
-#include "cairo-util.h"
#include "window.h"
static int option_fullscreen;
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];
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);
}
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
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;
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);
#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 {
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;
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;
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;
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);
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,
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,
* 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;
}
}
}
}
+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,
}
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;
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);
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);