Switch to cairo-drm, drop struct buffer hacks.

Kristian Høgsberg 16 years ago
parent 32692d2f48
commit 0ac16f056d
  1. 2
      Makefile.in
  2. 94
      cairo-util.c
  3. 28
      flower.c
  4. 32
      gears.c
  5. 21
      terminal.c
  6. 93
      window.c
  7. 9
      window.h

@ -39,7 +39,7 @@ wayland-system-compositor : \
wayland-system-compositor : CFLAGS += @EGL_COMPOSITOR_CFLAGS@ wayland-system-compositor : CFLAGS += @EGL_COMPOSITOR_CFLAGS@
wayland-system-compositor : LDLIBS += -L. -lwayland-server @EGL_COMPOSITOR_LIBS@ -rdynamic -lrt 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 gears : gears.o window.o wayland-glib.o cairo-util.o
screenshot : screenshot.o wayland-glib.o screenshot : screenshot.o wayland-glib.o
terminal : terminal.o window.o wayland-glib.o cairo-util.o terminal : terminal.o window.o wayland-glib.o cairo-util.o

@ -24,104 +24,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <i915_drm.h>
#include <sys/ioctl.h>
#include <math.h> #include <math.h>
#include <cairo.h> #include <cairo.h>
#include "cairo-util.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]) #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
void void

@ -33,10 +33,10 @@
#include <time.h> #include <time.h>
#include <cairo.h> #include <cairo.h>
#include <glib.h> #include <glib.h>
#include <cairo-drm.h>
#include "wayland-client.h" #include "wayland-client.h"
#include "wayland-glib.h" #include "wayland-glib.h"
#include "cairo-util.h"
static const char gem_device[] = "/dev/dri/card0"; static const char gem_device[] = "/dev/dri/card0";
static const char socket_name[] = "\0wayland"; static const char socket_name[] = "\0wayland";
@ -52,8 +52,8 @@ set_random_color(cairo_t *cr)
} }
static void * static void
draw_stuff(int width, int height) draw_stuff(cairo_surface_t *surface, int width, int height)
{ {
const int petal_count = 3 + random() % 5; const int petal_count = 3 + random() % 5;
const double r1 = 60 + random() % 35; 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 u = (10 + random() % 90) / 100.0;
const double v = (random() % 90) / 100.0; const double v = (random() % 90) / 100.0;
cairo_surface_t *surface;
cairo_t *cr; cairo_t *cr;
int i; int i;
double t, dt = 2 * M_PI / (petal_count * 2); double t, dt = 2 * M_PI / (petal_count * 2);
double x1, y1, x2, y2, x3, y3; double x1, y1, x2, y2, x3, y3;
surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
width, height);
cr = cairo_create(surface); cr = cairo_create(surface);
cairo_translate(cr, width / 2, height / 2); 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) { for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
x1 = cos(t) * r1; x1 = cos(t) * r1;
y1 = sin(t) * r1; y1 = sin(t) * r1;
@ -99,8 +95,6 @@ draw_stuff(int width, int height)
cairo_stroke(cr); cairo_stroke(cr);
cairo_destroy(cr); cairo_destroy(cr);
return surface;
} }
struct flower { struct flower {
@ -141,12 +135,12 @@ int main(int argc, char *argv[])
struct wl_display *display; struct wl_display *display;
struct wl_visual *visual; struct wl_visual *visual;
int fd; int fd;
cairo_drm_context_t *ctx;
cairo_surface_t *s; cairo_surface_t *s;
struct timespec ts; struct timespec ts;
GMainLoop *loop; GMainLoop *loop;
GSource *source; GSource *source;
struct flower flower; struct flower flower;
struct buffer *buffer;
fd = open(gem_device, O_RDWR); fd = open(gem_device, O_RDWR);
if (fd < 0) { if (fd < 0) {
@ -176,13 +170,17 @@ int main(int argc, char *argv[])
srandom(ts.tv_nsec); srandom(ts.tv_nsec);
flower.offset = random(); flower.offset = random();
s = draw_stuff(flower.width, flower.height); ctx = cairo_drm_context_get_for_fd(fd);
buffer = buffer_create_from_cairo_surface(fd, s); 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); visual = wl_display_get_premultiplied_argb_visual(display);
wl_surface_attach(flower.surface, wl_surface_attach(flower.surface,
buffer->name, flower.width, flower.height, cairo_drm_surface_get_name(s),
buffer->stride, visual); flower.width, flower.height,
cairo_drm_surface_get_stride(s),
visual);
wl_compositor_add_listener(flower.compositor, wl_compositor_add_listener(flower.compositor,
&compositor_listener, &flower); &compositor_listener, &flower);

@ -30,6 +30,7 @@
#include <time.h> #include <time.h>
#include <cairo.h> #include <cairo.h>
#include <glib.h> #include <glib.h>
#include <cairo-drm.h>
#include <GL/gl.h> #include <GL/gl.h>
#include <eagle.h> #include <eagle.h>
@ -37,7 +38,6 @@
#include "wayland-client.h" #include "wayland-client.h"
#include "wayland-glib.h" #include "wayland-glib.h"
#include "cairo-util.h"
#include "window.h" #include "window.h"
static const char gem_device[] = "/dev/dri/card0"; static const char gem_device[] = "/dev/dri/card0";
@ -55,9 +55,8 @@ struct gears {
EGLSurface surface; EGLSurface surface;
EGLContext context; EGLContext context;
int resized; int resized;
int fd;
GLfloat angle; GLfloat angle;
struct buffer *buffer; cairo_surface_t *cairo_surface;
GLint gear_list[3]; GLint gear_list[3];
}; };
@ -244,6 +243,8 @@ draw_gears(struct gears *gears)
static void static void
resize_window(struct gears *gears) resize_window(struct gears *gears)
{ {
uint32_t name, stride;
/* Constrain child size to be square and at least 300x300 */ /* Constrain child size to be square and at least 300x300 */
window_get_child_rectangle(gears->window, &gears->rectangle); window_get_child_rectangle(gears->window, &gears->rectangle);
if (gears->rectangle.width > gears->rectangle.height) if (gears->rectangle.width > gears->rectangle.height)
@ -258,20 +259,20 @@ resize_window(struct gears *gears)
window_draw(gears->window); window_draw(gears->window);
if (gears->buffer != NULL) if (gears->cairo_surface != NULL)
buffer_destroy(gears->buffer, gears->fd); cairo_surface_destroy(gears->cairo_surface);
gears->buffer = buffer_create(gears->fd, gears->cairo_surface = window_create_surface(gears->window,
gears->rectangle.width, &gears->rectangle);
gears->rectangle.height,
(gears->rectangle.width * 4 + 15) & ~15);
name = cairo_drm_surface_get_name(gears->cairo_surface);
stride = cairo_drm_surface_get_stride(gears->cairo_surface),
gears->surface = eglCreateSurfaceForName(gears->display, gears->surface = eglCreateSurfaceForName(gears->display,
gears->config, gears->config,
gears->buffer->name, name,
gears->buffer->width, gears->rectangle.width,
gears->buffer->height, gears->rectangle.height,
gears->buffer->stride, NULL); stride, NULL);
eglMakeCurrent(gears->display, eglMakeCurrent(gears->display,
gears->surface, gears->surface, gears->context); gears->surface, gears->surface, gears->context);
@ -321,9 +322,9 @@ handle_frame(void *data,
{ {
struct gears *gears = data; struct gears *gears = data;
window_copy(gears->window, window_copy_surface(gears->window,
&gears->rectangle, &gears->rectangle,
gears->buffer->name, gears->buffer->stride); gears->cairo_surface);
wl_compositor_commit(gears->compositor, 0); wl_compositor_commit(gears->compositor, 0);
@ -352,7 +353,6 @@ gears_create(struct wl_display *display, int fd)
gears = malloc(sizeof *gears); gears = malloc(sizeof *gears);
memset(gears, 0, sizeof *gears); memset(gears, 0, sizeof *gears);
gears->wl_display = display; gears->wl_display = display;
gears->fd = fd;
gears->window = window_create(display, fd, "Wayland Gears", gears->window = window_create(display, fd, "Wayland Gears",
x, y, width, height); x, y, width, height);

@ -33,6 +33,7 @@
#include <cairo.h> #include <cairo.h>
#include <glib.h> #include <glib.h>
#include <linux/input.h> #include <linux/input.h>
#include <cairo-drm.h>
#include <GL/gl.h> #include <GL/gl.h>
#include <eagle.h> #include <eagle.h>
@ -40,7 +41,6 @@
#include "wayland-client.h" #include "wayland-client.h"
#include "wayland-glib.h" #include "wayland-glib.h"
#include "cairo-util.h"
#include "window.h" #include "window.h"
static int option_fullscreen; static int option_fullscreen;
@ -59,7 +59,7 @@ struct terminal {
char *data; char *data;
int width, height, start, row, column; int width, height, start, row, column;
int fd, master; int fd, master;
struct buffer *buffer; cairo_surface_t *surface;
GIOChannel *channel; GIOChannel *channel;
uint32_t modifiers; uint32_t modifiers;
char escape[64]; char escape[64];
@ -128,16 +128,15 @@ static void
terminal_draw_contents(struct terminal *terminal) terminal_draw_contents(struct terminal *terminal)
{ {
struct rectangle rectangle; struct rectangle rectangle;
cairo_surface_t *surface;
cairo_t *cr; cairo_t *cr;
cairo_font_extents_t extents; cairo_font_extents_t extents;
int i, top_margin, side_margin; int i, top_margin, side_margin;
window_get_child_rectangle(terminal->window, &rectangle); window_get_child_rectangle(terminal->window, &rectangle);
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, terminal->surface =
rectangle.width, rectangle.height); window_create_surface(terminal->window, &rectangle);
cr = cairo_create(surface); cr = cairo_create(terminal->surface);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_set_source_rgba(cr, 0, 0, 0, 0.9); cairo_set_source_rgba(cr, 0, 0, 0, 0.9);
cairo_paint(cr); cairo_paint(cr);
@ -160,12 +159,9 @@ terminal_draw_contents(struct terminal *terminal)
} }
cairo_destroy(cr); cairo_destroy(cr);
terminal->buffer = buffer_create_from_cairo_surface(terminal->fd, surface); window_copy_surface(terminal->window,
cairo_surface_destroy(surface);
window_copy(terminal->window,
&rectangle, &rectangle,
terminal->buffer->name, terminal->buffer->stride); terminal->surface);
} }
static void static void
@ -405,7 +401,7 @@ handle_acknowledge(void *data,
terminal->redraw_scheduled = 0; terminal->redraw_scheduled = 0;
if (key == 0) if (key == 0)
buffer_destroy(terminal->buffer, terminal->fd); cairo_surface_destroy(terminal->surface);
if (terminal->redraw_pending) { if (terminal->redraw_pending) {
terminal->redraw_pending = 0; terminal->redraw_pending = 0;
@ -553,7 +549,6 @@ terminal_create(struct wl_display *display, int fd, int fullscreen)
return terminal; return terminal;
memset(terminal, 0, sizeof *terminal); memset(terminal, 0, sizeof *terminal);
terminal->fd = fd;
terminal->fullscreen = fullscreen; terminal->fullscreen = fullscreen;
terminal->window = window_create(display, fd, "Wayland Terminal", terminal->window = window_create(display, fd, "Wayland Terminal",
500, 100, 500, 400); 500, 100, 500, 400);

@ -30,13 +30,12 @@
#include <time.h> #include <time.h>
#include <cairo.h> #include <cairo.h>
#include <glib.h> #include <glib.h>
#include <cairo-drm.h>
#include <linux/input.h> #include <linux/input.h>
#include "wayland-client.h" #include "wayland-client.h"
#include "wayland-glib.h" #include "wayland-glib.h"
#include "cairo-util.h"
#include "window.h" #include "window.h"
struct window { struct window {
@ -52,9 +51,9 @@ struct window {
int fullscreen; int fullscreen;
struct wl_input_device *grab_device; struct wl_input_device *grab_device;
uint32_t name; 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_resize_handler_t resize_handler;
window_key_handler_t key_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 static void
window_draw_decorations(struct window *window) window_draw_decorations(struct window *window)
{ {
cairo_surface_t *surface;
cairo_t *cr; cairo_t *cr;
int border = 2, radius = 5; int border = 2, radius = 5;
cairo_text_extents_t extents; cairo_text_extents_t extents;
@ -86,7 +84,9 @@ window_draw_decorations(struct window *window)
struct wl_visual *visual; struct wl_visual *visual;
int width, height; int width, height;
surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, window->cairo_surface =
cairo_drm_surface_create(window->ctx,
CAIRO_CONTENT_COLOR_ALPHA,
window->allocation.width, window->allocation.width,
window->allocation.height); window->allocation.height);
@ -94,7 +94,7 @@ window_draw_decorations(struct window *window)
bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8); bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4); 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; width = window->allocation.width - window->margin * 2;
height = window->allocation.height - 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); cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
rounded_rect(cr, 0, 0, width, height, radius); rounded_rect(cr, 0, 0, width, height, radius);
cairo_fill(cr); 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_translate(cr, -7, -5);
cairo_set_line_width (cr, border); cairo_set_line_width (cr, border);
@ -157,15 +170,12 @@ window_draw_decorations(struct window *window)
cairo_fill(cr); cairo_fill(cr);
cairo_destroy(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); visual = wl_display_get_premultiplied_argb_visual(window->display);
wl_surface_attach(window->surface, wl_surface_attach(window->surface,
window->buffer->name, cairo_drm_surface_get_name(window->cairo_surface),
window->buffer->width, window->allocation.width,
window->buffer->height, window->allocation.height,
window->buffer->stride, cairo_drm_surface_get_stride(window->cairo_surface),
visual); visual);
wl_surface_map(window->surface, wl_surface_map(window->surface,
@ -179,19 +189,19 @@ static void
window_draw_fullscreen(struct window *window) window_draw_fullscreen(struct window *window)
{ {
struct wl_visual *visual; struct wl_visual *visual;
int stride = window->allocation.width * 4;
window->buffer = buffer_create(window->fd, window->cairo_surface =
cairo_drm_surface_create(window->ctx,
CAIRO_CONTENT_COLOR_ALPHA,
window->allocation.width, window->allocation.width,
window->allocation.height, window->allocation.height);
stride);
visual = wl_display_get_premultiplied_argb_visual(window->display); visual = wl_display_get_premultiplied_argb_visual(window->display);
wl_surface_attach(window->surface, wl_surface_attach(window->surface,
window->buffer->name, cairo_drm_surface_get_name(window->cairo_surface),
window->buffer->width, window->allocation.width,
window->buffer->height, window->allocation.height,
window->buffer->stride, cairo_drm_surface_get_stride(window->cairo_surface),
visual); visual);
wl_surface_map(window->surface, wl_surface_map(window->surface,
@ -222,9 +232,9 @@ window_handle_acknowledge(void *data,
* safely free the old window buffer if we resized and * safely free the old window buffer if we resized and
* render the next frame into our back buffer.. */ * render the next frame into our back buffer.. */
if (key == 0 && window->buffer != NULL) { if (key == 0 && window->cairo_surface != NULL) {
buffer_destroy(window->buffer, window->fd); cairo_surface_destroy(window->cairo_surface);
window->buffer = NULL; 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 void
window_copy(struct window *window, window_copy(struct window *window,
struct rectangle *rectangle, struct rectangle *rectangle,
@ -396,6 +416,21 @@ window_copy(struct window *window,
rectangle->height); rectangle->height);
} }
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 void
window_set_fullscreen(struct window *window, int fullscreen) window_set_fullscreen(struct window *window, int fullscreen)
{ {
@ -488,7 +523,11 @@ window_create(struct wl_display *display, int fd,
window->saved_allocation = window->allocation; window->saved_allocation = window->allocation;
window->margin = 16; window->margin = 16;
window->state = WINDOW_STABLE; 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, wl_display_add_global_listener(display,
window_handle_global, window); window_handle_global, window);

@ -56,6 +56,15 @@ window_copy(struct window *window,
struct rectangle *rectangle, struct rectangle *rectangle,
uint32_t name, uint32_t stride); 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 void
window_set_fullscreen(struct window *window, int fullscreen); window_set_fullscreen(struct window *window, int fullscreen);

Loading…
Cancel
Save