From 854490374187749a69e23f439c70216bb656c03e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Kristian=20H=C3=B8gsberg?= Date: Mon, 2 May 2011 12:11:07 -0400 Subject: [PATCH] compositor: Screenhoot into a client provided shm buffer This moves the png writing part to the client and removes the gdk-pixbuf dependency from the compositor. --- clients/screenshot.c | 113 +++++++++++++++++++++++++++++++++++++++------ compositor/compositor.c | 4 +- compositor/screenshooter.c | 44 +++++------------- configure.ac | 2 +- protocol/screenshooter.xml | 5 +- 5 files changed, 117 insertions(+), 51 deletions(-) diff --git a/clients/screenshot.c b/clients/screenshot.c index 8f76a09..574f525 100644 --- a/clients/screenshot.c +++ b/clients/screenshot.c @@ -25,6 +25,8 @@ #include #include #include +#include +#include #include #include "wayland-client.h" @@ -35,22 +37,108 @@ * the compositor and serves as a test bed for implementing client * side marshalling outside libwayland.so */ +static struct wl_output *output; +static struct wl_shm *shm; +static struct wl_visual *visual; +static struct screenshooter *screenshooter; +static int output_width, output_height; + +static void +display_handle_geometry(void *data, + struct wl_output *output, + int32_t x, int32_t y, int32_t width, int32_t height) +{ + output_width = width; + output_height = height; +} + +static const struct wl_output_listener output_listener = { + display_handle_geometry, +}; + static void handle_global(struct wl_display *display, uint32_t id, const char *interface, uint32_t version, void *data) { - struct screenshooter **screenshooter = data; + static int visual_count; + + if (strcmp(interface, "wl_output") == 0) { + output = wl_output_create(display, id, 1); + wl_output_add_listener(output, &output_listener, NULL); + } else if (strcmp(interface, "wl_shm") == 0) { + shm = wl_shm_create(display, id, 1); + } else if (strcmp(interface, "wl_visual") == 0) { + if (visual_count++ == 1) + visual = wl_visual_create(display, id, 1); + } else if (strcmp(interface, "screenshooter") == 0) { + screenshooter = screenshooter_create(display, id, 1); + } +} + +static void +sync_callback(void *data) +{ + int *done = data; - if (strcmp(interface, "screenshooter") == 0) - *screenshooter = screenshooter_create(display, id, 1); + *done = 1; +} + +static void +roundtrip(struct wl_display *display) +{ + int done; + + done = 0; + wl_display_sync_callback(display, sync_callback, &done); + wl_display_iterate(display, WL_DISPLAY_WRITABLE); + while (!done) + wl_display_iterate(display, WL_DISPLAY_READABLE); +} + +static struct wl_buffer * +create_shm_buffer(int width, int height, void **data_out) +{ + char filename[] = "/tmp/wayland-shm-XXXXXX"; + struct wl_buffer *buffer; + int fd, size, stride; + void *data; + + fd = mkstemp(filename); + if (fd < 0) { + fprintf(stderr, "open %s failed: %m\n", filename); + return NULL; + } + stride = width * 4; + size = stride * height; + if (ftruncate(fd, size) < 0) { + fprintf(stderr, "ftruncate failed: %m\n"); + close(fd); + return NULL; + } + + data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + unlink(filename); + + if (data == MAP_FAILED) { + fprintf(stderr, "mmap failed: %m\n"); + close(fd); + return NULL; + } + + buffer = wl_shm_create_buffer(shm, fd, width, height, stride, visual); + + close(fd); + + *data_out = data; + + return buffer; } int main(int argc, char *argv[]) { struct wl_display *display; - GMainLoop *loop; - GSource *source; - struct screenshooter *screenshooter; + struct wl_buffer *buffer; + void *data; display = wl_display_connect(NULL); if (display == NULL) { @@ -58,22 +146,19 @@ int main(int argc, char *argv[]) return -1; } - screenshooter = NULL; wl_display_add_global_listener(display, handle_global, &screenshooter); wl_display_iterate(display, WL_DISPLAY_READABLE); + roundtrip(display); if (screenshooter == NULL) { fprintf(stderr, "display doesn't support screenshooter\n"); return -1; } - loop = g_main_loop_new(NULL, FALSE); - source = wl_glib_source_new(display); - g_source_attach(source, NULL); - - screenshooter_shoot(screenshooter); + buffer = create_shm_buffer(output_width, output_height, &data); + screenshooter_shoot(screenshooter, output, buffer); + roundtrip(display); - g_idle_add((GSourceFunc) g_main_loop_quit, loop); - g_main_loop_run(loop); + /* FIXME: write png */ return 0; } diff --git a/compositor/compositor.c b/compositor/compositor.c index 987267e..a295991 100644 --- a/compositor/compositor.c +++ b/compositor/compositor.c @@ -29,11 +29,11 @@ #include #include #include -#include #include #include #include #include +#include #include "wayland-server.h" #include "compositor.h" @@ -1883,8 +1883,6 @@ int main(int argc, char *argv[]) { NULL, } }; - g_type_init(); /* GdkPixbuf needs this, it seems. */ - width = 1024; height = 640; diff --git a/compositor/screenshooter.c b/compositor/screenshooter.c index 78d0be3..ee1ccf6 100644 --- a/compositor/screenshooter.c +++ b/compositor/screenshooter.c @@ -17,8 +17,6 @@ */ #include -#include -#include #include "compositor.h" #include "screenshooter-server-protocol.h" @@ -29,40 +27,22 @@ struct screenshooter { }; static void -screenshooter_shoot(struct wl_client *client, struct screenshooter *shooter) +screenshooter_shoot(struct wl_client *client, + struct screenshooter *shooter, + struct wl_output *output_base, struct wl_buffer *buffer) { - struct wlsc_compositor *ec = shooter->ec; - struct wlsc_output *output; - char buffer[256]; - GdkPixbuf *pixbuf; - GError *error = NULL; - unsigned char *data; - int i, j; + struct wlsc_output *output = (struct wlsc_output *) output_base; - i = 0; - wl_list_for_each(output, &ec->output_list, link) { - snprintf(buffer, sizeof buffer, "wayland-screenshot-%d.png", i++); - data = malloc(output->width * output->height * 4); - if (data == NULL) { - fprintf(stderr, "couldn't allocate image buffer\n"); - continue; - } - - glPixelStorei(GL_PACK_ALIGNMENT, 1); - glReadPixels(0, 0, output->width, output->height, - GL_RGBA, GL_UNSIGNED_BYTE, data); + if (!wl_buffer_is_shm(buffer)) + return; - /* FIXME: We should just use a RGB visual for the frontbuffer. */ - for (j = 3; j < output->width * output->height * 4; j += 4) - data[j] = 0xff; + if (buffer->width < output->width || buffer->height < output->height) + return; - pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE, - 8, output->width, output->height, output->width * 4, - NULL, NULL); - gdk_pixbuf_save(pixbuf, buffer, "png", &error, NULL); - g_object_unref(pixbuf); - free(data); - } + glPixelStorei(GL_PACK_ALIGNMENT, 1); + glReadPixels(0, 0, output->width, output->height, + GL_RGBA, GL_UNSIGNED_BYTE, + wl_shm_buffer_get_data(buffer)); } struct screenshooter_interface screenshooter_implementation = { diff --git a/configure.ac b/configure.ac index e5dd0d6..81f90b6 100644 --- a/configure.ac +++ b/configure.ac @@ -35,7 +35,7 @@ AC_CHECK_FUNC([dlopen], [], AC_SUBST(DLOPEN_LIBS) PKG_CHECK_MODULES(COMPOSITOR, - [wayland-server egl >= 7.10 glesv2 gdk-pixbuf-2.0 pixman-1]) + [wayland-server egl >= 7.10 glesv2 pixman-1 libpng12]) AC_CHECK_PROG(RSVG_CONVERT, rsvg-convert, rsvg-convert) AM_CONDITIONAL(HAVE_RSVG_CONVERT, test -n "$RSVG_CONVERT") diff --git a/protocol/screenshooter.xml b/protocol/screenshooter.xml index f8d993d..1487c6d 100644 --- a/protocol/screenshooter.xml +++ b/protocol/screenshooter.xml @@ -1,7 +1,10 @@ - + + + + -- 2.7.4