From 6b5e7626f5458864726712cf7e49a70fa421f934 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Kristian=20H=C3=B8gsberg?= Date: Sun, 2 Nov 2008 17:11:51 -0500 Subject: [PATCH] Move background drawing to a client. --- Makefile | 8 ++- background.c | 159 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ egl-compositor.c | 6 --- pointer.c | 3 -- 4 files changed, 165 insertions(+), 11 deletions(-) create mode 100644 background.c diff --git a/Makefile b/Makefile index fa7ac49..dcb7e5c 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CFLAGS += -Wall -g $(shell pkg-config --cflags libffi libdrm) LDLIBS += $(shell pkg-config --libs libffi libdrm) -clients = flower pointer +clients = flower pointer background all : wayland $(clients) @@ -19,9 +19,13 @@ libwayland.so : $(libwayland_objs) flower_objs = flower.o pointer_objs = pointer.o +background_objs = background.o $(clients) : CFLAGS += $(shell pkg-config --cflags cairo) -$(clients) : LDLIBS += $(shell pkg-config --libs cairo) -lrt +$(clients) : LDLIBS += $(shell pkg-config --libs cairo gdk-pixbuf-2.0) -lrt + +background : CFLAGS += $(shell pkg-config --cflags gdk-pixbuf-2.0) +background : LDLIBS += $(shell pkg-config --libs gdk-pixbuf-2.0) define client_template $(1): $$($(1)_objs) libwayland.so diff --git a/background.c b/background.c new file mode 100644 index 0000000..5c7775f --- /dev/null +++ b/background.c @@ -0,0 +1,159 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "wayland-client.h" + +static const char gem_device[] = "/dev/dri/card0"; +static const char socket_name[] = "\0wayland"; + +static uint8_t *convert_to_argb(GdkPixbuf *pixbuf, int *new_stride) +{ + uint8_t *data, *row, *src; + uint32_t *pixel; + int width, height, stride, channels, i, j; + + width = gdk_pixbuf_get_width(pixbuf); + height = gdk_pixbuf_get_height(pixbuf); + stride = gdk_pixbuf_get_rowstride(pixbuf); + row = gdk_pixbuf_get_pixels(pixbuf); + channels = gdk_pixbuf_get_n_channels(pixbuf); + + data = malloc(height * width * 4); + if (data == NULL) { + fprintf(stderr, "out of memory\n"); + return NULL; + } + + for (i = 0; i < height; i++) { + for (j = 0; j < width; j++) { + src = &row[i * stride + j * channels]; + pixel = (uint32_t *) &data[i * width * 4 + j * 4]; + *pixel = 0xff000000 | (src[0] << 16) | (src[1] << 8) | src[2]; + } + } + + *new_stride = width * 4; + + return data; +} + +static uint32_t name_pixbuf(int fd, GdkPixbuf *pixbuf) +{ + struct drm_i915_gem_create create; + struct drm_gem_flink flink; + struct drm_i915_gem_pwrite pwrite; + int32_t width, height, stride; + void *data; + + width = gdk_pixbuf_get_width(pixbuf); + height = gdk_pixbuf_get_height(pixbuf); + data = convert_to_argb(pixbuf, &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"); + return 0; + } + + pwrite.handle = create.handle; + pwrite.offset = 0; + pwrite.size = height * 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 0; + } + + flink.handle = create.handle; + if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) { + fprintf(stderr, "gem flink failed: %m\n"); + return 0; + } + +#if 0 + /* We need to hold on to the handle until the server has received + * the attach request... we probably need a confirmation event. + * I guess the breadcrumb idea will suffice. */ + struct drm_gem_close close; + close.handle = create.handle; + if (ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close) < 0) { + fprintf(stderr, "gem close failed: %m\n"); + return 0; + } +#endif + + return flink.name; +} + +static int +connection_update(struct wl_connection *connection, + uint32_t mask, void *data) +{ + struct pollfd *p = data; + + p->events = 0; + if (mask & WL_CONNECTION_READABLE) + p->events |= POLLIN; + if (mask & WL_CONNECTION_WRITABLE) + p->events |= POLLOUT; + + return 0; +} + +int main(int argc, char *argv[]) +{ + GdkPixbuf *image; + GError *error = NULL; + struct wl_display *display; + struct wl_surface *surface; + int fd, width, height, stride; + uint32_t name, mask; + struct pollfd p[1]; + + fd = open(gem_device, O_RDWR); + if (fd < 0) { + fprintf(stderr, "drm open failed: %m\n"); + return -1; + } + + display = wl_display_create(socket_name, + connection_update, &p[0]); + p[0].fd = wl_display_get_fd(display); + + surface = wl_display_create_surface(display); + + g_type_init(); + image = gdk_pixbuf_new_from_file (argv[1], &error); + + name = name_pixbuf(fd, image); + + width = gdk_pixbuf_get_width(image); + height = gdk_pixbuf_get_height(image); + stride = gdk_pixbuf_get_rowstride(image); + + printf("width %d, height %d\n", width, height); + + wl_surface_attach(surface, name, width, height, width * 4); + + wl_surface_map(surface, 0, 0, width, height); + + while (1) { + poll(p, 1, -1); + mask = 0; + if (p[0].revents & POLLIN) + mask |= WL_CONNECTION_READABLE; + if (p[0].revents & POLLOUT) + mask |= WL_CONNECTION_WRITABLE; + wl_display_iterate(display, mask); + } + + return 0; +} diff --git a/egl-compositor.c b/egl-compositor.c index 3489215..049af98 100644 --- a/egl-compositor.c +++ b/egl-compositor.c @@ -42,12 +42,6 @@ repaint(void *data) GLint vertices[12]; GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 }; GLuint indices[4] = { 0, 1, 2, 3 }; - - /* This part is where we actually copy the buffer to screen. - * Needs to be part of the repaint loop, not called from the - * notify_map handler. */ - - glClear(GL_COLOR_BUFFER_BIT); iterator = wl_surface_iterator_create(ec->wl_display, 0); while (wl_surface_iterator_next(iterator, &surface)) { diff --git a/pointer.c b/pointer.c index ec9f0a3..e9b9b9e 100644 --- a/pointer.c +++ b/pointer.c @@ -145,7 +145,6 @@ void event_handler(struct wl_display *display, struct pointer *pointer = data; wl_surface_map(pointer->surface, arg1, arg2, pointer->width, pointer->height); - } int main(int argc, char *argv[]) @@ -156,8 +155,6 @@ int main(int argc, char *argv[]) uint32_t name; cairo_surface_t *s; - srandom(time(NULL)); - fd = open(gem_device, O_RDWR); if (fd < 0) { fprintf(stderr, "drm open failed: %m\n"); -- 2.7.4