aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--backend/drm/renderer.c3
-rw-r--r--backend/meson.build2
-rw-r--r--backend/x11/backend.c34
-rw-r--r--examples/meson.build6
-rw-r--r--examples/screenshot.c288
-rwxr-xr-xglgen.sh91
-rw-r--r--include/rootston/desktop.h2
-rw-r--r--include/wlr/egl.h9
-rw-r--r--include/wlr/render.h15
-rw-r--r--include/wlr/render/interface.h2
-rw-r--r--include/wlr/types/wlr_output.h1
-rw-r--r--include/wlr/types/wlr_screenshooter.h26
-rw-r--r--meson.build6
-rw-r--r--protocol/meson.build7
-rw-r--r--protocol/screenshooter.xml16
-rw-r--r--render/egl.c61
-rw-r--r--render/glapi.txt8
-rw-r--r--render/gles2/renderer.c39
-rw-r--r--render/meson.build20
-rw-r--r--render/wlr_renderer.c5
-rw-r--r--rootston/desktop.c13
-rw-r--r--types/meson.build1
-rw-r--r--types/wlr_output.c3
-rw-r--r--types/wlr_screenshooter.c163
24 files changed, 721 insertions, 100 deletions
diff --git a/backend/drm/renderer.c b/backend/drm/renderer.c
index c5840436..0d338490 100644
--- a/backend/drm/renderer.c
+++ b/backend/drm/renderer.c
@@ -14,6 +14,7 @@
#include <wlr/render/gles2.h>
#include <wlr/render.h>
#include "backend/drm/drm.h"
+#include "render/glapi.h"
bool wlr_drm_renderer_init(struct wlr_drm_backend *drm,
struct wlr_drm_renderer *renderer) {
@@ -191,7 +192,7 @@ static struct wlr_texture *get_tex_for_bo(struct wlr_drm_renderer *renderer, str
EGL_NONE,
};
- tex->img = renderer->egl.eglCreateImageKHR(renderer->egl.display, EGL_NO_CONTEXT,
+ tex->img = eglCreateImageKHR(renderer->egl.display, EGL_NO_CONTEXT,
EGL_LINUX_DMA_BUF_EXT, NULL, attribs);
if (!tex->img) {
wlr_log(L_ERROR, "Failed to create EGL image: %s", egl_error());
diff --git a/backend/meson.build b/backend/meson.build
index c5c73288..cf62a56f 100644
--- a/backend/meson.build
+++ b/backend/meson.build
@@ -38,5 +38,5 @@ lib_wlr_backend = static_library(
'wlr_backend',
backend_files,
include_directories: wlr_inc,
- dependencies: [wayland_server, egl, gbm, libinput, systemd, elogind, wlr_protos],
+ dependencies: [wayland_server, egl, gbm, libinput, systemd, elogind, wlr_render, wlr_protos],
)
diff --git a/backend/x11/backend.c b/backend/x11/backend.c
index 2134536c..1eb5a952 100644
--- a/backend/x11/backend.c
+++ b/backend/x11/backend.c
@@ -62,16 +62,31 @@ static bool handle_x11_event(struct wlr_x11_backend *x11, xcb_generic_event_t *e
case XCB_BUTTON_PRESS:
case XCB_BUTTON_RELEASE: {
xcb_button_press_event_t *ev = (xcb_button_press_event_t *)event;
- struct wlr_event_pointer_button button = {
- .device = &x11->pointer_dev,
- .time_sec = ev->time / 1000,
- .time_usec = ev->time * 1000,
- .button = xcb_button_to_wl(ev->detail),
- .state = event->response_type == XCB_BUTTON_PRESS ?
- WLR_BUTTON_PRESSED : WLR_BUTTON_RELEASED,
- };
- wl_signal_emit(&x11->pointer.events.button, &button);
+ if (ev->detail == XCB_BUTTON_INDEX_4 ||
+ ev->detail == XCB_BUTTON_INDEX_5) {
+ double delta = (ev->detail == XCB_BUTTON_INDEX_4 ? -15 : 15);
+ struct wlr_event_pointer_axis axis = {
+ .device = &x11->pointer_dev,
+ .time_sec = ev->time / 1000,
+ .time_usec = ev->time * 1000,
+ .source = WLR_AXIS_SOURCE_WHEEL,
+ .orientation = WLR_AXIS_ORIENTATION_VERTICAL,
+ .delta = delta,
+ };
+ wl_signal_emit(&x11->pointer.events.axis, &axis);
+ } else {
+ struct wlr_event_pointer_button button = {
+ .device = &x11->pointer_dev,
+ .time_sec = ev->time / 1000,
+ .time_usec = ev->time * 1000,
+ .button = xcb_button_to_wl(ev->detail),
+ .state = event->response_type == XCB_BUTTON_PRESS ?
+ WLR_BUTTON_PRESSED : WLR_BUTTON_RELEASED,
+ };
+
+ wl_signal_emit(&x11->pointer.events.button, &button);
+ }
x11->time = ev->time;
break;
}
@@ -260,6 +275,7 @@ static bool wlr_x11_backend_start(struct wlr_backend *backend) {
xcb_map_window(x11->xcb_conn, output->win);
xcb_flush(x11->xcb_conn);
+ wlr_output_create_global(&output->wlr_output, x11->wl_display);
wl_signal_emit(&x11->backend.events.output_add, output);
wl_signal_emit(&x11->backend.events.input_add, &x11->keyboard_dev);
diff --git a/examples/meson.build b/examples/meson.build
index 6ec40843..a29a3310 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],
+)
diff --git a/examples/screenshot.c b/examples/screenshot.c
new file mode 100644
index 00000000..95af49ca
--- /dev/null
+++ b/examples/screenshot.c
@@ -0,0 +1,288 @@
+/*
+ * 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 <sys/wait.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <wayland-client.h>
+#include <limits.h>
+#include <sys/param.h>
+#include <screenshooter-client-protocol.h>
+#include "../backend/wayland/os-compatibility.c"
+
+static struct wl_shm *shm = NULL;
+static struct orbital_screenshooter *screenshooter = NULL;
+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 output_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 = wl_output_get_user_data(wl_output);
+
+ if (wl_output == output->output) {
+ output->offset_x = x;
+ output->offset_y = y;
+ }
+}
+
+static void output_handle_mode(void *data, struct wl_output *wl_output,
+ uint32_t flags, int width, int height, int refresh) {
+ struct screenshooter_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 void output_handle_done(void *data, struct wl_output *wl_output) {
+
+}
+
+static const struct wl_output_listener output_listener = {
+ .geometry = output_handle_geometry,
+ .mode = output_handle_mode,
+ .done = output_handle_done,
+};
+
+static void screenshot_done(void *data, struct orbital_screenshot *screenshot) {
+ buffer_copy_done = 1;
+}
+
+static const struct orbital_screenshot_listener screenshot_listener = {
+ .done = 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) {
+ // Unimplemented
+}
+
+static const struct wl_registry_listener registry_listener = {
+ .global = handle_global,
+ .global_remove = handle_global_remove,
+};
+
+static struct wl_buffer *create_shm_buffer(int width, int height,
+ void **data_out) {
+ int stride = width * 4;
+ int size = stride * height;
+
+ int fd = os_create_anonymous_file(size);
+ if (fd < 0) {
+ fprintf(stderr, "creating a buffer file for %d B failed: %m\n", size);
+ return NULL;
+ }
+
+ void *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;
+ }
+
+ struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size);
+ close(fd);
+ struct wl_buffer *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 argb_to_rgba(uint32_t *data, size_t height, size_t stride) {
+ size_t n = height*stride/4;
+ for (size_t i = 0; i < n; ++i) {
+ uint32_t v = data[i];
+ uint32_t rgb = v & 0x00ffffff;
+ uint32_t a = (v & 0xff000000) >> 24;
+ data[i] = (rgb << 8) | a;
+ }
+}
+
+static void write_image(const char *filename, int width, int height) {
+ int buffer_stride = width * 4;
+
+ void *data = calloc(1, buffer_stride * height);
+ if (!data) {
+ return;
+ }
+
+ struct screenshooter_output *output, *next;
+ wl_list_for_each_safe(output, next, &output_list, link) {
+ int output_stride = output->width * 4;
+ void *s = output->data;
+ void *d = data + (output->offset_y - min_y) * buffer_stride +
+ (output->offset_x - min_x) * 4;
+
+ for (int i = 0; i < output->height; i++) {
+ memcpy(d, s, output_stride);
+ d += buffer_stride;
+ s += output_stride;
+ }
+
+ free(output);
+ }
+
+ argb_to_rgba(data, height, buffer_stride);
+
+ char size[10 + 1 + 10 + 2 + 1]; // int32_t are max 10 digits
+ sprintf(size, "%dx%d+0", width, height);
+
+ int fd[2];
+ pipe(fd);
+
+ pid_t child = fork();
+ if (child < 0) {
+ fprintf(stderr, "fork() failed\n");
+ exit(EXIT_FAILURE);
+ } else if (child != 0) {
+ close(fd[0]);
+ write(fd[1], data, buffer_stride * height);
+ close(fd[1]);
+ free(data);
+ waitpid(child, NULL, 0);
+ } else {
+ close(fd[1]);
+ if (dup2(fd[0], 0) != 0) {
+ fprintf(stderr, "cannot dup the pipe\n");
+ exit(EXIT_FAILURE);
+ }
+ close(fd[0]);
+ execlp("convert", "convert", "-depth", "8", "-size", size, "rgba:-",
+ "-alpha", "opaque", filename, NULL);
+ fprintf(stderr, "cannot execute convert\n");
+ exit(EXIT_FAILURE);
+ }
+}
+
+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 = wl_display_connect(NULL);
+ if (display == NULL) {
+ fprintf(stderr, "failed to create display: %m\n");
+ return -1;
+ }
+
+ wl_list_init(&output_list);
+ struct wl_registry *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;
+ }
+
+ int width, height;
+ if (set_buffer_size(&width, &height)) {
+ fprintf(stderr, "cannot set buffer size\n");
+ return -1;
+ }
+
+ struct screenshooter_output *output;
+ 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_image("wayland-screenshot.png", width, height);
+ return EXIT_SUCCESS;
+}
diff --git a/glgen.sh b/glgen.sh
new file mode 100755
index 00000000..d9de6f9f
--- /dev/null
+++ b/glgen.sh
@@ -0,0 +1,91 @@
+#!/bin/sh
+
+# Generates a simple GL/EGL extension function loader.
+#
+# The input is a .txt file, with each function to load on its own line.
+# If a line starts with a -, it is optional, and will not cause the loader
+# to fail if it can't load the function. You'll need to check if that function
+# is NULL before using it.
+
+if [ $# -ne 2 ]; then
+ exit 1
+fi
+
+SPEC=$1
+OUT=$2
+
+BASE=$(basename "$SPEC" .txt)
+INCLUDE_GUARD=$(printf %s "$SPEC" | tr -c [:alnum:] _ | tr [:lower:] [:upper:])
+
+DECL=""
+DEFN=""
+LOADER=""
+
+DECL_FMT='extern %s %s;'
+DEFN_FMT='%s %s;'
+LOADER_FMT='%s = (%s)eglGetProcAddress("%s");'
+CHECK_FMT='if (!%s) {
+ wlr_log(L_ERROR, "Unable to load %s");
+ return false;
+}'
+
+while read -r COMMAND; do
+ [ ${COMMAND::1} = "-" ]
+ OPTIONAL=$?
+
+ COMMAND=${COMMAND#-}
+ if [ ${COMMAND: -2} = "WL" ]; then
+ FUNC_PTR_FMT='PFN%s'
+ else
+ FUNC_PTR_FMT='PFN%sPROC'
+ fi
+
+ FUNC_PTR=$(printf "$FUNC_PTR_FMT" "$COMMAND" | tr [:lower:] [:upper:])
+
+ DECL="$DECL$(printf "\n$DECL_FMT" "$FUNC_PTR" "$COMMAND")"
+ DEFN="$DEFN$(printf "\n$DEFN_FMT" "$FUNC_PTR" "$COMMAND")"
+ LOADER="$LOADER$(printf "\n$LOADER_FMT" "$COMMAND" "$FUNC_PTR" "$COMMAND")"
+
+ if [ $OPTIONAL -ne 0 ]; then
+ LOADER="$LOADER$(printf "\n$CHECK_FMT" "$COMMAND" "$COMMAND")"
+ fi
+done < $SPEC
+
+if [ ${OUT: -2} = '.h' ]; then
+ cat > $OUT << EOF
+ #ifndef $INCLUDE_GUARD
+ #define $INCLUDE_GUARD
+
+ #include <stdbool.h>
+
+ #include <EGL/egl.h>
+ #include <EGL/eglext.h>
+ #include <EGL/eglmesaext.h>
+ #include <GLES2/gl2.h>
+ #include <GLES2/gl2ext.h>
+
+ bool load_$BASE(void);
+ $DECL
+
+ #endif
+EOF
+elif [ ${OUT: -2} = '.c' ]; then
+ cat > $OUT << EOF
+ #include <wlr/util/log.h>
+ #include "$BASE.h"
+ $DEFN
+
+ bool load_$BASE(void) {
+ static bool done = false;
+ if (done) {
+ return true;
+ }
+ $LOADER
+
+ done = true;
+ return true;
+ }
+EOF
+else
+ exit 1
+fi
diff --git a/include/rootston/desktop.h b/include/rootston/desktop.h
index 0d641848..68fc5b94 100644
--- a/include/rootston/desktop.h
+++ b/include/rootston/desktop.h
@@ -8,6 +8,7 @@
#include <wlr/types/wlr_wl_shell.h>
#include <wlr/types/wlr_xdg_shell_v6.h>
#include <wlr/types/wlr_gamma_control.h>
+#include <wlr/types/wlr_screenshooter.h>
#include <wlr/util/list.h>
#include "rootston/view.h"
#include "rootston/config.h"
@@ -36,6 +37,7 @@ struct roots_desktop {
struct wlr_xdg_shell_v6 *xdg_shell_v6;
struct wlr_xwayland *xwayland;
struct wlr_gamma_control_manager *gamma_control_manager;
+ struct wlr_screenshooter *screenshooter;
struct wl_listener output_add;
struct wl_listener output_remove;
diff --git a/include/wlr/egl.h b/include/wlr/egl.h
index 25329175..9ab4d9ce 100644
--- a/include/wlr/egl.h
+++ b/include/wlr/egl.h
@@ -10,15 +10,6 @@ struct wlr_egl {
EGLConfig config;
EGLContext context;
- PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display;
- PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC create_platform_window_surface;
-
- PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR;
- PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR;
- PFNEGLQUERYWAYLANDBUFFERWL eglQueryWaylandBufferWL;
- PFNEGLBINDWAYLANDDISPLAYWL eglBindWaylandDisplayWL;
- PFNEGLUNBINDWAYLANDDISPLAYWL eglUnbindWaylandDisplayWL;
-
const char *egl_exts;
const char *gl_exts;
diff --git a/include/wlr/render.h b/include/wlr/render.h
index 2fbfb476..5027064d 100644
--- a/include/wlr/render.h
+++ b/include/wlr/render.h
@@ -30,27 +30,32 @@ struct wlr_texture *wlr_render_texture_create(struct wlr_renderer *r);
* This will render the texture at <123, 321>.
*/
bool wlr_render_with_matrix(struct wlr_renderer *r,
- struct wlr_texture *texture, const float (*matrix)[16]);
+ struct wlr_texture *texture, const float (*matrix)[16]);
/**
* Renders a solid quad in the specified color.
*/
void wlr_render_colored_quad(struct wlr_renderer *r,
- const float (*color)[4], const float (*matrix)[16]);
+ const float (*color)[4], const float (*matrix)[16]);
/**
* Renders a solid ellipse in the specified color.
*/
void wlr_render_colored_ellipse(struct wlr_renderer *r,
- const float (*color)[4], const float (*matrix)[16]);
+ const float (*color)[4], const float (*matrix)[16]);
/**
* Returns a list of pixel formats supported by this renderer.
*/
const enum wl_shm_format *wlr_renderer_get_formats(
- struct wlr_renderer *r, size_t *len);
+ struct wlr_renderer *r, size_t *len);
/**
* Returns true if this wl_buffer is a DRM buffer.
*/
bool wlr_renderer_buffer_is_drm(struct wlr_renderer *renderer,
- struct wl_resource *buffer);
+ struct wl_resource *buffer);
+/**
+ * Reads pixels and stores them in out_data as ARGB8888.
+ */
+void wlr_renderer_read_pixels(struct wlr_renderer *r, int x, int y,
+ int width, int height, void *out_data);
/**
* Destroys this wlr_renderer. Textures must be destroyed separately.
*/
diff --git a/include/wlr/render/interface.h b/include/wlr/render/interface.h
index cbe33822..bbc5acb4 100644
--- a/include/wlr/render/interface.h
+++ b/include/wlr/render/interface.h
@@ -28,6 +28,8 @@ struct wlr_renderer_impl {
struct wlr_renderer *renderer, size_t *len);
bool (*buffer_is_drm)(struct wlr_renderer *renderer,
struct wl_resource *buffer);
+ void (*read_pixels)(struct wlr_renderer *renderer, int x, int y, int width,
+ int height, void *out_data);
void (*destroy)(struct wlr_renderer *renderer);
};
diff --git a/include/wlr/types/wlr_output.h b/include/wlr/types/wlr_output.h
index 1fa6ad9e..52d377e3 100644
--- a/include/wlr/types/wlr_output.h
+++ b/include/wlr/types/wlr_output.h
@@ -37,6 +37,7 @@ struct wlr_output {
struct {
struct wl_signal frame;
+ struct wl_signal swap_buffers;
struct wl_signal resolution;
struct wl_signal destroy;
} events;
diff --git a/include/wlr/types/wlr_screenshooter.h b/include/wlr/types/wlr_screenshooter.h
new file mode 100644
index 00000000..4bda3d3c
--- /dev/null
+++ b/include/wlr/types/wlr_screenshooter.h
@@ -0,0 +1,26 @@
+#ifndef _WLR_SCREENSHOOTER_H
+#define _WLR_SCREENSHOOTER_H
+#include <wayland-server.h>
+
+struct wlr_screenshooter {
+ struct wl_global *wl_global;
+ struct wlr_renderer *renderer;
+
+ void *data;
+};
+
+struct wlr_screenshot {
+ struct wl_resource *resource;
+ struct wl_resource *output_resource;
+
+ struct wlr_output *output;
+ struct wlr_screenshooter *screenshooter;
+
+ void* data;
+};
+
+struct wlr_screenshooter *wlr_screenshooter_create(struct wl_display *display,
+ struct wlr_renderer *renderer);
+void wlr_screenshooter_destroy(struct wlr_screenshooter *screenshooter);
+
+#endif
diff --git a/meson.build b/meson.build
index 57c26b0f..179550d6 100644
--- a/meson.build
+++ b/meson.build
@@ -14,6 +14,10 @@ add_project_arguments(
'-DWLR_SRC_DIR="@0@"'.format(meson.source_root()),
language: 'c',
)
+add_project_arguments(
+ '-I@0@'.format(meson.build_root()),
+ language: 'c',
+)
add_project_link_arguments(
'-Wl,-rpath,@0@'.format(meson.build_root()),
language: 'c',
@@ -69,8 +73,8 @@ if elogind.found() and get_option('enable_elogind')
endif
subdir('protocol')
-subdir('backend')
subdir('render')
+subdir('backend')
subdir('types')
subdir('util')
subdir('xcursor')
diff --git a/protocol/meson.build b/protocol/meson.build
index 6dc88d76..79871fea 100644
--- a/protocol/meson.build
+++ b/protocol/meson.build
@@ -22,11 +22,14 @@ wayland_scanner_client = generator(
protocols = [
[wl_protocol_dir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml'],
- 'gamma-control.xml'
+ 'gamma-control.xml',
+ 'screenshooter.xml',
]
client_protocols = [
- [wl_protocol_dir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml']
+ [wl_protocol_dir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml'],
+ 'gamma-control.xml',
+ 'screenshooter.xml',
]
wl_protos_src = []
diff --git a/protocol/screenshooter.xml b/protocol/screenshooter.xml
new file mode 100644
index 00000000..fc48eac9
--- /dev/null
+++ b/protocol/screenshooter.xml
@@ -0,0 +1,16 @@
+<protocol name="orbital_screenshooter">
+
+ <interface name="orbital_screenshooter" version="1">
+ <request name="shoot">
+ <arg name="id" type="new_id" interface="orbital_screenshot"/>
+ <arg name="output" type="object" interface="wl_output"/>
+ <arg name="buffer" type="object" interface="wl_buffer"/>
+ </request>
+ </interface>
+
+ <interface name="orbital_screenshot" version="1">
+ <event name="done">
+ </event>
+ </interface>
+
+</protocol>
diff --git a/render/egl.c b/render/egl.c
index 82dea50c..9815b923 100644
--- a/render/egl.c
+++ b/render/egl.c
@@ -4,6 +4,7 @@
#include <stdlib.h>
#include <wlr/util/log.h>
#include <wlr/egl.h>
+#include "render/glapi.h"
// Extension documentation
// https://www.khronos.org/registry/EGL/extensions/KHR/EGL_KHR_image_base.txt.
@@ -46,27 +47,6 @@ const char *egl_error(void) {
}
}
-static bool egl_exts(struct wlr_egl *egl) {
- egl->get_platform_display = (PFNEGLGETPLATFORMDISPLAYEXTPROC)
- eglGetProcAddress("eglGetPlatformDisplayEXT");
-
- if (!egl->get_platform_display) {
- wlr_log(L_ERROR, "Failed to load EGL extension 'eglGetPlatformDisplayEXT'");
- return false;
- }
-
- egl->create_platform_window_surface = (PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC)
- eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT");
-
- if (!egl->create_platform_window_surface) {
- wlr_log(L_ERROR,
- "Failed to load EGL extension 'eglCreatePlatformWindowSurfaceEXT'");
- return false;
- }
-
- return true;
-}
-
static bool egl_get_config(EGLDisplay disp, EGLConfig *out, EGLint visual_id) {
EGLint count = 0, matched = 0, ret;
@@ -103,7 +83,7 @@ static bool egl_get_config(EGLDisplay disp, EGLConfig *out, EGLint visual_id) {
bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, EGLint visual_id,
void *remote_display) {
- if (!egl_exts(egl)) {
+ if (!load_glapi()) {
return false;
}
@@ -112,7 +92,7 @@ bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, EGLint visual_id,
goto error;
}
- egl->display = egl->get_platform_display(platform, remote_display, NULL);
+ egl->display = eglGetPlatformDisplayEXT(platform, remote_display, NULL);
if (egl->display == EGL_NO_DISPLAY) {
wlr_log(L_ERROR, "Failed to create EGL display: %s", egl_error());
goto error;
@@ -142,22 +122,11 @@ bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, EGLint visual_id,
eglMakeCurrent(egl->display, EGL_NO_SURFACE, EGL_NO_SURFACE, egl->context);
egl->egl_exts = eglQueryString(egl->display, EGL_EXTENSIONS);
if (strstr(egl->egl_exts, "EGL_WL_bind_wayland_display") == NULL ||
- strstr(egl->egl_exts, "EGL_KHR_image_base") == NULL) {
+ strstr(egl->egl_exts, "EGL_KHR_image_base") == NULL) {
wlr_log(L_ERROR, "Required egl extensions not supported");
goto error;
}
- egl->eglCreateImageKHR = (PFNEGLCREATEIMAGEKHRPROC)
- eglGetProcAddress("eglCreateImageKHR");
- egl->eglDestroyImageKHR = (PFNEGLDESTROYIMAGEKHRPROC)
- eglGetProcAddress("eglDestroyImageKHR");
- egl->eglQueryWaylandBufferWL = (PFNEGLQUERYWAYLANDBUFFERWL)
- (void*) eglGetProcAddress("eglQueryWaylandBufferWL");
- egl->eglBindWaylandDisplayWL = (PFNEGLBINDWAYLANDDISPLAYWL)
- (void*) eglGetProcAddress("eglBindWaylandDisplayWL");
- egl->eglUnbindWaylandDisplayWL = (PFNEGLUNBINDWAYLANDDISPLAYWL)
- (void*) eglGetProcAddress("eglUnbindWaylandDisplayWL");
-
egl->gl_exts = (const char*) glGetString(GL_EXTENSIONS);
wlr_log(L_INFO, "Using EGL %d.%d", (int)major, (int)minor);
wlr_log(L_INFO, "Supported EGL extensions: %s", egl->egl_exts);
@@ -174,8 +143,8 @@ error:
void wlr_egl_free(struct wlr_egl *egl) {
eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
- if (egl->wl_display && egl->eglUnbindWaylandDisplayWL) {
- egl->eglUnbindWaylandDisplayWL(egl->display, egl->wl_display);
+ if (egl->wl_display && eglUnbindWaylandDisplayWL) {
+ eglUnbindWaylandDisplayWL(egl->display, egl->wl_display);
}
eglDestroyContext(egl->display, egl->context);
@@ -184,11 +153,11 @@ void wlr_egl_free(struct wlr_egl *egl) {
}
bool wlr_egl_bind_display(struct wlr_egl *egl, struct wl_display *local_display) {
- if (!egl->eglBindWaylandDisplayWL) {
+ if (!eglBindWaylandDisplayWL) {
return false;
}
- if (egl->eglBindWaylandDisplayWL(egl->display, local_display)) {
+ if (eglBindWaylandDisplayWL(egl->display, local_display)) {
egl->wl_display = local_display;
return true;
}
@@ -198,33 +167,33 @@ bool wlr_egl_bind_display(struct wlr_egl *egl, struct wl_display *local_display)
bool wlr_egl_query_buffer(struct wlr_egl *egl, struct wl_resource *buf,
int attrib, int *value) {
- if (!egl->eglQueryWaylandBufferWL) {
+ if (!eglQueryWaylandBufferWL) {
return false;
}
- return egl->eglQueryWaylandBufferWL(egl->display, buf, attrib, value);
+ return eglQueryWaylandBufferWL(egl->display, buf, attrib, value);
}
EGLImage wlr_egl_create_image(struct wlr_egl *egl, EGLenum target,
EGLClientBuffer buffer, const EGLint *attribs) {
- if (!egl->eglCreateImageKHR) {
+ if (!eglCreateImageKHR) {
return false;
}
- return egl->eglCreateImageKHR(egl->display, egl->context, target,
+ return eglCreateImageKHR(egl->display, egl->context, target,
buffer, attribs);
}
bool wlr_egl_destroy_image(struct wlr_egl *egl, EGLImage image) {
- if (!egl->eglDestroyImageKHR) {
+ if (!eglDestroyImageKHR) {
return false;
}
- egl->eglDestroyImageKHR(egl->display, image);
+ eglDestroyImageKHR(egl->display, image);
return true;
}
EGLSurface wlr_egl_create_surface(struct wlr_egl *egl, void *window) {
- EGLSurface surf = egl->create_platform_window_surface(egl->display, egl->config,
+ EGLSurface surf = eglCreatePlatformWindowSurfaceEXT(egl->display, egl->config,
window, NULL);
if (surf == EGL_NO_SURFACE) {
wlr_log(L_ERROR, "Failed to create EGL surface: %s", egl_error());
diff --git a/render/glapi.txt b/render/glapi.txt
new file mode 100644
index 00000000..81791204
--- /dev/null
+++ b/render/glapi.txt
@@ -0,0 +1,8 @@
+eglGetPlatformDisplayEXT
+eglCreatePlatformWindowSurfaceEXT
+-eglCreateImageKHR
+-eglDestroyImageKHR
+-eglQueryWaylandBufferWL
+-eglBindWaylandDisplayWL
+-eglUnbindWaylandDisplayWL
+-glEGLImageTargetTexture2DOES
diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c
index d6c22ebe..94c50b9a 100644
--- a/render/gles2/renderer.c
+++ b/render/gles2/renderer.c
@@ -12,8 +12,8 @@
#include <wlr/render/matrix.h>
#include <wlr/util/log.h>
#include "render/gles2.h"
+#include "render/glapi.h"
-PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES = NULL;
struct shaders shaders;
static bool compile_shader(GLuint type, const GLchar *src, GLuint *shader) {
@@ -101,25 +101,7 @@ error:
wlr_log(L_ERROR, "Failed to set up default shaders!");
}
-static void init_image_ext() {
- if (glEGLImageTargetTexture2DOES) {
- return;
- }
-
- const char *exts = (const char*) glGetString(GL_EXTENSIONS);
- if (strstr(exts, "GL_OES_EGL_image_external")) {
- glEGLImageTargetTexture2DOES = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)
- eglGetProcAddress("glEGLImageTargetTexture2DOES");
- }
-
- if (!glEGLImageTargetTexture2DOES) {
- wlr_log(L_INFO, "Failed to load glEGLImageTargetTexture2DOES "
- "Will not be able to attach drm buffers");
- }
-}
-
static void init_globals() {
- init_image_ext();
init_default_shaders();
}
@@ -226,7 +208,23 @@ static bool wlr_gles2_buffer_is_drm(struct wlr_renderer *_renderer,
(struct wlr_gles2_renderer *)_renderer;
EGLint format;
return wlr_egl_query_buffer(renderer->egl, buffer,
- EGL_TEXTURE_FORMAT, &format);
+ EGL_TEXTURE_FORMAT, &format);
+}
+
+static void rgba_to_argb(uint32_t *data, size_t height, size_t stride) {
+ size_t n = height*stride/4;
+ for (size_t i = 0; i < n; ++i) {
+ uint32_t v = data[i];
+ uint32_t rgb = (v & 0xffffff00) >> 8;
+ uint32_t a = v & 0x000000ff;
+ data[i] = rgb | (a << 24);
+ }
+}
+
+static void wlr_gles2_read_pixels(struct wlr_renderer *renderer, int x, int y,
+ int width, int height, void *out_data) {
+ glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, out_data);
+ rgba_to_argb(out_data, height, width*4);
}
static struct wlr_renderer_impl wlr_renderer_impl = {
@@ -238,6 +236,7 @@ static struct wlr_renderer_impl wlr_renderer_impl = {
.render_ellipse = wlr_gles2_render_ellipse,
.formats = wlr_gles2_formats,
.buffer_is_drm = wlr_gles2_buffer_is_drm,
+ .read_pixels = wlr_gles2_read_pixels,
};
struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_backend *backend) {
diff --git a/render/meson.build b/render/meson.build
index dc0ceeb9..749d1393 100644
--- a/render/meson.build
+++ b/render/meson.build
@@ -1,3 +1,16 @@
+glgen = find_program('../glgen.sh')
+
+glapi_c = custom_target('glapi.c',
+ input: 'glapi.txt',
+ output: '@BASENAME@.c',
+ command: [glgen, '@INPUT@', '@OUTPUT@'],
+)
+glapi_h = custom_target('glapi.h',
+ input: 'glapi.txt',
+ output: '@BASENAME@.h',
+ command: [glgen, '@INPUT@', '@OUTPUT@'],
+)
+
lib_wlr_render = static_library(
'wlr_render',
files(
@@ -11,6 +24,13 @@ lib_wlr_render = static_library(
'wlr_renderer.c',
'wlr_texture.c',
),
+ glapi_c,
+ glapi_h,
include_directories: wlr_inc,
dependencies: [glesv2, egl],
)
+
+wlr_render = declare_dependency(
+ link_with: lib_wlr_render,
+ sources: glapi_h,
+)
diff --git a/render/wlr_renderer.c b/render/wlr_renderer.c
index fec5e38a..ef0c31be 100644
--- a/render/wlr_renderer.c
+++ b/render/wlr_renderer.c
@@ -51,3 +51,8 @@ bool wlr_renderer_buffer_is_drm(struct wlr_renderer *r,
struct wl_resource *buffer) {
return r->impl->buffer_is_drm(r, buffer);
}
+
+void wlr_renderer_read_pixels(struct wlr_renderer *r, int x, int y,
+ int width, int height, void *out_data) {
+ r->impl->read_pixels(r, x, y, width, height, out_data);
+}
diff --git a/rootston/desktop.c b/rootston/desktop.c
index 75030ad2..562ba6d0 100644
--- a/rootston/desktop.c
+++ b/rootston/desktop.c
@@ -238,16 +238,15 @@ struct roots_desktop *desktop_create(struct roots_server *server,
wl_list_init(&desktop->output_remove.link);
desktop->output_remove.notify = output_remove_notify;
- wl_signal_add(&server->backend->events.output_add,
- &desktop->output_add);
+ wl_signal_add(&server->backend->events.output_add, &desktop->output_add);
wl_signal_add(&server->backend->events.output_remove,
- &desktop->output_remove);
+ &desktop->output_remove);
desktop->server = server;
desktop->config = config;
desktop->layout = wlr_output_layout_create();
- desktop->compositor = wlr_compositor_create(
- server->wl_display, server->renderer);
+ desktop->compositor = wlr_compositor_create(server->wl_display,
+ server->renderer);
desktop->xdg_shell_v6 = wlr_xdg_shell_v6_create(server->wl_display);
wl_signal_add(&desktop->xdg_shell_v6->events.new_surface,
@@ -266,7 +265,9 @@ struct roots_desktop *desktop_create(struct roots_server *server,
desktop->xwayland_surface.notify = handle_xwayland_surface;
desktop->gamma_control_manager = wlr_gamma_control_manager_create(
- server->wl_display);
+ server->wl_display);
+ desktop->screenshooter = wlr_screenshooter_create(server->wl_display,
+ server->renderer);
return desktop;
}
diff --git a/types/meson.build b/types/meson.build
index cff40150..a151e8a3 100644
--- a/types/meson.build
+++ b/types/meson.build
@@ -20,6 +20,7 @@ lib_wlr_types = static_library(
'wlr_compositor.c',
'wlr_box.c',
'wlr_gamma_control.c',
+ 'wlr_screenshooter.c',
),
include_directories: wlr_inc,
dependencies: [wayland_server, pixman, wlr_protos],
diff --git a/types/wlr_output.c b/types/wlr_output.c
index f167a213..64f67f2d 100644
--- a/types/wlr_output.c
+++ b/types/wlr_output.c
@@ -105,6 +105,7 @@ void wlr_output_init(struct wlr_output *output,
output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
output->scale = 1;
wl_signal_init(&output->events.frame);
+ wl_signal_init(&output->events.swap_buffers);
wl_signal_init(&output->events.resolution);
wl_signal_init(&output->events.destroy);
}
@@ -231,6 +232,8 @@ void wlr_output_swap_buffers(struct wlr_output *output) {
wlr_render_with_matrix(output->cursor.renderer, output->cursor.texture, &matrix);
}
+ wl_signal_emit(&output->events.swap_buffers, &output);
+
output->impl->swap_buffers(output);
}
diff --git a/types/wlr_screenshooter.c b/types/wlr_screenshooter.c
new file mode 100644
index 00000000..38204aa1
--- /dev/null
+++ b/types/wlr_screenshooter.c
@@ -0,0 +1,163 @@
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include <wayland-server.h>
+#include <wlr/render.h>
+#include <wlr/types/wlr_screenshooter.h>
+#include <wlr/types/wlr_output.h>
+#include <wlr/util/log.h>
+#include "screenshooter-protocol.h"
+
+static void copy_yflip(uint8_t *dst, uint8_t *src, int32_t height,
+ int32_t stride) {
+ uint8_t *end = dst + height * stride;
+ while (dst < end) {
+ memcpy(dst, src, stride);
+ dst += stride;
+ src -= stride;
+ }
+}
+
+struct screenshot_state {
+ int32_t width, height, stride;
+ uint8_t *pixels;
+ struct wl_shm_buffer *shm_buffer;
+ 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);
+ struct wlr_renderer *renderer = state->screenshot->screenshooter->renderer;
+ struct wlr_output *output = state->screenshot->output;
+
+ wlr_output_make_current(output);
+ wlr_renderer_read_pixels(renderer, 0, 0, output->width, output->height,
+ state->pixels);
+
+ void *data = wl_shm_buffer_get_data(state->shm_buffer);
+ wl_shm_buffer_begin_access(state->shm_buffer);
+ copy_yflip(data, state->pixels + state->stride * (state->height - 1),
+ state->height, state->stride);
+ wl_shm_buffer_end_access(state->shm_buffer);
+
+ free(state->pixels);
+ wl_list_remove(&listener->link);
+
+ orbital_screenshot_send_done(state->screenshot->resource);
+
+ 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_screenshooter *screenshooter =
+ wl_resource_get_user_data(_screenshooter);
+ 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);
+ 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;
+ }
+
+ uint32_t format = wl_shm_buffer_get_format(shm_buffer);
+ if (format != WL_SHM_FORMAT_XRGB8888) {
+ wlr_log(L_ERROR, "Invalid buffer: unsupported format");
+ return;
+ }
+
+ uint8_t *pixels = malloc(stride * height);
+ if (pixels == NULL) {
+ wl_client_post_no_memory(client);
+ return;
+ }
+
+ struct wlr_screenshot *screenshot =
+ calloc(1, sizeof(struct wlr_screenshot));
+ if (!screenshot) {
+ wl_client_post_no_memory(client);
+ return;
+ }
+ screenshot->output_resource = _output;
+ screenshot->output = output;
+ screenshot->screenshooter = screenshooter;
+ screenshot->resource = wl_resource_create(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);
+
+ struct screenshot_state *state = calloc(1, sizeof(struct screenshot_state));
+ if (!state) {
+ wl_client_post_no_memory(client);
+ return;
+ }
+ state->width = width;
+ state->height = height;
+ state->stride = stride;
+ state->pixels = pixels;
+ state->shm_buffer = shm_buffer;
+ state->screenshot = screenshot;
+ state->frame_listener.notify = output_frame_notify;
+ wl_signal_add(&output->events.swap_buffers, &state->frame_listener);
+}
+
+static struct orbital_screenshooter_interface screenshooter_impl = {
+ .shoot = screenshooter_shoot,
+};
+
+static void screenshooter_bind(struct wl_client *wl_client,
+ void *_screenshooter, uint32_t version, uint32_t id) {
+ struct wlr_screenshooter *screenshooter = _screenshooter;
+ assert(wl_client && screenshooter);
+ if (version > 1) {
+ wlr_log(L_ERROR, "Client requested unsupported screenshooter version,"
+ "disconnecting");
+ wl_client_destroy(wl_client);
+ return;
+ }
+ struct wl_resource *wl_resource = wl_resource_create(wl_client,
+ &orbital_screenshooter_interface, version, id);
+ wl_resource_set_implementation(wl_resource, &screenshooter_impl,
+ screenshooter, NULL);
+}
+
+struct wlr_screenshooter *wlr_screenshooter_create(struct wl_display *display,
+ struct wlr_renderer *renderer) {
+ struct wlr_screenshooter *screenshooter =
+ calloc(1, sizeof(struct wlr_screenshooter));
+ if (!screenshooter) {
+ return NULL;
+ }
+ screenshooter->renderer = renderer;
+
+ struct wl_global *wl_global = wl_global_create(display,
+ &orbital_screenshooter_interface, 1, screenshooter, screenshooter_bind);
+ if (!wl_global) {
+ free(screenshooter);
+ return NULL;
+ }
+ screenshooter->wl_global = wl_global;
+
+ return screenshooter;
+}
+
+void wlr_screenshooter_destroy(struct wlr_screenshooter *screenshooter) {
+ if (!screenshooter) {
+ return;
+ }
+ // TODO: this segfault (wl_display->registry_resource_list is not init)
+ // wl_global_destroy(screenshooter->wl_global);
+ free(screenshooter);
+}