aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremersion <contact@emersion.fr>2017-09-08 18:09:09 +0200
committeremersion <contact@emersion.fr>2017-10-07 16:22:02 +0200
commit35f970025133dc5ad627936a3cdeacb37b532072 (patch)
treecb413db2542cf87eb96c4c9b6923729cd956717a
parent1c8b72e0cd63c4965bc04e7390d3165a9e4c08ce (diff)
First attempt to implement screenshooter
-rw-r--r--backend/drm/drm.c10
-rw-r--r--examples/meson.build6
-rw-r--r--examples/screenshot.c292
-rw-r--r--include/wlr/interfaces/wlr_output.h1
-rw-r--r--include/wlr/types/wlr_output.h1
-rw-r--r--protocol/meson.build2
-rw-r--r--types/wlr_output.c6
-rw-r--r--types/wlr_screenshooter.c43
8 files changed, 360 insertions, 1 deletions
diff --git a/backend/drm/drm.c b/backend/drm/drm.c
index 9e5346a1..117e5749 100644
--- a/backend/drm/drm.c
+++ b/backend/drm/drm.c
@@ -634,6 +634,15 @@ static bool wlr_drm_connector_move_cursor(struct wlr_output *output,
return drm->iface->crtc_move_cursor(drm, conn->crtc, x, y);
}
+static void wlr_drm_connector_read_pixels(struct wlr_output *_output,
+ void *out_data) {
+ struct wlr_drm_output *output = (struct wlr_drm_output *)_output;
+ struct wlr_drm_crtc *crtc = output->crtc;
+ struct wlr_drm_plane *plane = crtc->primary;
+ glReadPixels(0, 0, plane->width, plane->height, GL_BGRA_EXT, GL_UNSIGNED_BYTE,
+ out_data);
+}
+
static void wlr_drm_connector_destroy(struct wlr_output *output) {
struct wlr_drm_connector *conn = (struct wlr_drm_connector *)output;
wlr_drm_connector_cleanup(conn);
@@ -652,6 +661,7 @@ static struct wlr_output_impl output_impl = {
.swap_buffers = wlr_drm_connector_swap_buffers,
.set_gamma = wlr_drm_connector_set_gamma,
.get_gamma_size = wlr_drm_connector_get_gamma_size,
+ .read_pixels = wlr_drm_connector_read_pixels,
};
static int retry_pageflip(void *data) {
diff --git a/examples/meson.build b/examples/meson.build
index 6ec40843..292ab053 100644
--- a/examples/meson.build
+++ b/examples/meson.build
@@ -22,3 +22,9 @@ executable(
dependencies: wlroots,
link_with: lib_shared,
)
+
+executable(
+ 'screenshot',
+ 'screenshot.c',
+ dependencies: [wayland_client, wlr_protos, dependency('cairo')],
+)
diff --git a/examples/screenshot.c b/examples/screenshot.c
new file mode 100644
index 00000000..0f1f1efe
--- /dev/null
+++ b/examples/screenshot.c
@@ -0,0 +1,292 @@
+/*
+ * Copyright © 2008 Kristian Høgsberg
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#define _XOPEN_SOURCE 700
+#define _POSIX_C_SOURCE 199309L
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <wayland-client.h>
+#include <limits.h>
+#include <sys/param.h>
+#include <cairo.h>
+#include <screenshooter-client-protocol.h>
+#include "../backend/wayland/os-compatibility.c"
+
+static struct wl_shm *shm;
+static struct orbital_screenshooter *screenshooter;
+static struct wl_list output_list;
+int min_x, min_y, max_x, max_y;
+int buffer_copy_done;
+
+struct screenshooter_output {
+ struct wl_output *output;
+ struct wl_buffer *buffer;
+ int width, height, offset_x, offset_y;
+ void *data;
+ struct wl_list link;
+};
+
+static void
+display_handle_geometry(void *data,
+ struct wl_output *wl_output,
+ int x,
+ int y,
+ int physical_width,
+ int physical_height,
+ int subpixel,
+ const char *make,
+ const char *model,
+ int transform)
+{
+ struct screenshooter_output *output;
+
+ output = wl_output_get_user_data(wl_output);
+
+ if (wl_output == output->output) {
+ output->offset_x = x;
+ output->offset_y = y;
+ }
+}
+
+static void
+display_handle_mode(void *data,
+ struct wl_output *wl_output,
+ uint32_t flags,
+ int width,
+ int height,
+ int refresh)
+{
+ struct screenshooter_output *output;
+
+ output = wl_output_get_user_data(wl_output);
+
+ if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) {
+ output->width = width;
+ output->height = height;
+ }
+}
+
+static const struct wl_output_listener output_listener = {
+ display_handle_geometry,
+ display_handle_mode
+};
+
+static void
+screenshot_done(void *data, struct orbital_screenshot *screenshot)
+{
+ buffer_copy_done = 1;
+}
+
+static const struct orbital_screenshot_listener screenshot_listener = {
+ screenshot_done
+};
+
+static void
+handle_global(void *data, struct wl_registry *registry,
+ uint32_t name, const char *interface, uint32_t version)
+{
+ static struct screenshooter_output *output;
+
+ if (strcmp(interface, "wl_output") == 0) {
+ output = calloc(1, sizeof *output);
+ output->output = wl_registry_bind(registry, name,
+ &wl_output_interface, 1);
+ wl_list_insert(&output_list, &output->link);
+ wl_output_add_listener(output->output, &output_listener, output);
+ } else if (strcmp(interface, "wl_shm") == 0) {
+ shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
+ } else if (strcmp(interface, "orbital_screenshooter") == 0) {
+ screenshooter = wl_registry_bind(registry, name,
+ &orbital_screenshooter_interface,
+ 1);
+ }
+}
+
+static void
+handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
+{
+ /* XXX: unimplemented */
+}
+
+static const struct wl_registry_listener registry_listener = {
+ handle_global,
+ handle_global_remove
+};
+
+static struct wl_buffer *
+create_shm_buffer(int width, int height, void **data_out)
+{
+ struct wl_shm_pool *pool;
+ struct wl_buffer *buffer;
+ int fd, size, stride;
+ void *data;
+
+ stride = width * 4;
+ size = stride * height;
+
+ fd = os_create_anonymous_file(size);
+ if (fd < 0) {
+ fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
+ size);
+ return NULL;
+ }
+
+ data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if (data == MAP_FAILED) {
+ fprintf(stderr, "mmap failed: %m\n");
+ close(fd);
+ return NULL;
+ }
+
+ pool = wl_shm_create_pool(shm, fd, size);
+ close(fd);
+ buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
+ WL_SHM_FORMAT_XRGB8888);
+ wl_shm_pool_destroy(pool);
+
+ *data_out = data;
+
+ return buffer;
+}
+
+static void
+write_png(int width, int height)
+{
+ int output_stride, buffer_stride, i;
+ cairo_surface_t *surface;
+ void *data, *d, *s;
+ struct screenshooter_output *output, *next;
+
+ buffer_stride = width * 4;
+
+ data = calloc(1, buffer_stride * height);
+ if (!data)
+ return;
+
+ wl_list_for_each_safe(output, next, &output_list, link) {
+ output_stride = output->width * 4;
+ s = output->data;
+ d = data + (output->offset_y - min_y) * buffer_stride +
+ (output->offset_x - min_x) * 4;
+
+ for (i = 0; i < output->height; i++) {
+ memcpy(d, s, output_stride);
+ d += buffer_stride;
+ s += output_stride;
+ }
+
+ free(output);
+ }
+
+ surface = cairo_image_surface_create_for_data(data,
+ CAIRO_FORMAT_ARGB32,
+ width, height, buffer_stride);
+ cairo_surface_write_to_png(surface, "wayland-screenshot.png");
+ cairo_surface_destroy(surface);
+ free(data);
+}
+
+static int
+set_buffer_size(int *width, int *height)
+{
+ struct screenshooter_output *output;
+ min_x = min_y = INT_MAX;
+ max_x = max_y = INT_MIN;
+ int position = 0;
+
+ wl_list_for_each_reverse(output, &output_list, link) {
+ output->offset_x = position;
+ position += output->width;
+ }
+
+ wl_list_for_each(output, &output_list, link) {
+ min_x = MIN(min_x, output->offset_x);
+ min_y = MIN(min_y, output->offset_y);
+ max_x = MAX(max_x, output->offset_x + output->width);
+ max_y = MAX(max_y, output->offset_y + output->height);
+ }
+
+ if (max_x <= min_x || max_y <= min_y)
+ return -1;
+
+ *width = max_x - min_x;
+ *height = max_y - min_y;
+
+ return 0;
+}
+
+int main(int argc, char *argv[])
+{
+ struct wl_display *display;
+ struct wl_registry *registry;
+ struct screenshooter_output *output;
+ int width, height;
+
+ display = wl_display_connect(NULL);
+ if (display == NULL) {
+ fprintf(stderr, "failed to create display: %m\n");
+ return -1;
+ }
+
+ wl_list_init(&output_list);
+ registry = wl_display_get_registry(display);
+ wl_registry_add_listener(registry, &registry_listener, NULL);
+ wl_display_dispatch(display);
+ wl_display_roundtrip(display);
+
+ if (screenshooter == NULL) {
+ fprintf(stderr, "display doesn't support screenshooter\n");
+ return -1;
+ }
+
+ if (set_buffer_size(&width, &height)) {
+ fprintf(stderr, "cannot set buffer size\n");
+ return -1;
+ }
+
+ wl_list_for_each(output, &output_list, link) {
+ if (output->width == 0 || output->height == 0) {
+ continue;
+ }
+
+ output->buffer = create_shm_buffer(output->width, output->height, &output->data);
+ if (output->buffer == NULL) {
+ return -1;
+ }
+ struct orbital_screenshot *screenshot = orbital_screenshooter_shoot(
+ screenshooter, output->output, output->buffer);
+ orbital_screenshot_add_listener(screenshot, &screenshot_listener, screenshot);
+ buffer_copy_done = 0;
+ while (!buffer_copy_done)
+ wl_display_roundtrip(display);
+ }
+
+ write_png(width, height);
+
+ return 0;
+}
diff --git a/include/wlr/interfaces/wlr_output.h b/include/wlr/interfaces/wlr_output.h
index 7d2821e0..2fd306d7 100644
--- a/include/wlr/interfaces/wlr_output.h
+++ b/include/wlr/interfaces/wlr_output.h
@@ -19,6 +19,7 @@ struct wlr_output_impl {
void (*set_gamma)(struct wlr_output *output,
uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b);
uint16_t (*get_gamma_size)(struct wlr_output *output);
+ void (*read_pixels)(struct wlr_output *_output, void *out_data);
};
void wlr_output_init(struct wlr_output *output, const struct wlr_output_impl *impl);
diff --git a/include/wlr/types/wlr_output.h b/include/wlr/types/wlr_output.h
index 1fa6ad9e..509e1bcb 100644
--- a/include/wlr/types/wlr_output.h
+++ b/include/wlr/types/wlr_output.h
@@ -70,5 +70,6 @@ void wlr_output_swap_buffers(struct wlr_output *output);
void wlr_output_set_gamma(struct wlr_output *output,
uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b);
uint16_t wlr_output_get_gamma_size(struct wlr_output *output);
+void wlr_output_read_pixels(struct wlr_output *output, void *out_data);
#endif
diff --git a/protocol/meson.build b/protocol/meson.build
index 28a7b668..79871fea 100644
--- a/protocol/meson.build
+++ b/protocol/meson.build
@@ -28,6 +28,8 @@ protocols = [
client_protocols = [
[wl_protocol_dir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml'],
+ 'gamma-control.xml',
+ 'screenshooter.xml',
]
wl_protos_src = []
diff --git a/types/wlr_output.c b/types/wlr_output.c
index f167a213..abad1aec 100644
--- a/types/wlr_output.c
+++ b/types/wlr_output.c
@@ -247,3 +247,9 @@ uint16_t wlr_output_get_gamma_size(struct wlr_output *output) {
}
return output->impl->get_gamma_size(output);
}
+
+void wlr_output_read_pixels(struct wlr_output *output, void *out_data) {
+ if (output->impl->read_pixels) {
+ output->impl->read_pixels(output, out_data);
+ }
+}
diff --git a/types/wlr_screenshooter.c b/types/wlr_screenshooter.c
index 20be1103..43e5fcd9 100644
--- a/types/wlr_screenshooter.c
+++ b/types/wlr_screenshooter.c
@@ -6,9 +6,44 @@
#include <wlr/util/log.h>
#include "screenshooter-protocol.h"
+struct screenshot_state {
+ struct wl_shm_buffer *shm_buffer;
+ struct wlr_output *output;
+ struct wlr_screenshot *screenshot;
+ struct wl_listener frame_listener;
+};
+
+static void output_frame_notify(struct wl_listener *listener, void *_data) {
+ struct screenshot_state *state = wl_container_of(listener, state, frame_listener);
+
+ void *data = wl_shm_buffer_get_data(state->shm_buffer);
+ wl_shm_buffer_begin_access(state->shm_buffer);
+ wlr_output_read_pixels(state->output, data);
+ wl_shm_buffer_end_access(state->shm_buffer);
+
+ wl_list_remove(&listener->link);
+ orbital_screenshot_send_done(state->screenshot->resource);
+
+ // TODO: free(state)
+}
+
static void screenshooter_shoot(struct wl_client *client,
struct wl_resource *_screenshooter, uint32_t id,
struct wl_resource *_output, struct wl_resource *_buffer) {
+ struct wlr_output *output = wl_resource_get_user_data(_output);
+ if (!wl_shm_buffer_get(_buffer)) {
+ wlr_log(L_ERROR, "Invalid buffer: not a shared memory buffer");
+ return;
+ }
+ struct wl_shm_buffer *shm_buffer = wl_shm_buffer_get(_buffer);
+ int32_t width = wl_shm_buffer_get_width(shm_buffer);
+ int32_t height = wl_shm_buffer_get_height(shm_buffer);
+ // TODO: int32_t stride = wl_shm_buffer_get_stride(shm_buffer);
+ if (width < output->width || height < output->height) {
+ wlr_log(L_ERROR, "Invalid buffer: too small");
+ return;
+ }
+
struct wlr_screenshot *screenshot;
if (!(screenshot = calloc(1, sizeof(struct wlr_screenshot)))) {
return;
@@ -18,7 +53,13 @@ static void screenshooter_shoot(struct wl_client *client,
&orbital_screenshot_interface, wl_resource_get_version(_screenshooter), id);
wlr_log(L_DEBUG, "new screenshot %p (res %p)", screenshot, screenshot->resource);
wl_resource_set_implementation(screenshot->resource, NULL, screenshot, NULL);
- // TODO: orbital_screenshot_send_done(screenshot->resource);
+
+ struct screenshot_state *state = calloc(1, sizeof(struct screenshot_state));
+ state->shm_buffer = shm_buffer;
+ state->output = output;
+ state->screenshot = screenshot;
+ state->frame_listener.notify = output_frame_notify;
+ wl_signal_add(&output->events.frame, &state->frame_listener);
}
static struct orbital_screenshooter_interface screenshooter_impl = {