From 1c8b72e0cd63c4965bc04e7390d3165a9e4c08ce Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 7 Sep 2017 17:31:30 +0200 Subject: [PATCH 01/15] Add screenshooter skeleton --- examples/compositor.c | 208 ++++++++++++++++++++++++++ include/wlr/types/wlr_screenshooter.h | 21 +++ protocol/meson.build | 5 +- protocol/screenshooter.xml | 16 ++ types/meson.build | 1 + types/wlr_screenshooter.c | 66 ++++++++ 6 files changed, 315 insertions(+), 2 deletions(-) create mode 100644 examples/compositor.c create mode 100644 include/wlr/types/wlr_screenshooter.h create mode 100644 protocol/screenshooter.xml create mode 100644 types/wlr_screenshooter.c diff --git a/examples/compositor.c b/examples/compositor.c new file mode 100644 index 00000000..12577dc1 --- /dev/null +++ b/examples/compositor.c @@ -0,0 +1,208 @@ +#define _POSIX_C_SOURCE 199309L +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "wlr/types/wlr_compositor.h" +#include +#include +#include +#include "shared.h" + +// TODO: move to common header? +int os_create_anonymous_file(off_t size); + +struct sample_state { + struct wlr_renderer *renderer; + struct wlr_compositor *wlr_compositor; + struct wlr_wl_shell *wl_shell; + struct wlr_seat *wl_seat; + struct wlr_xdg_shell_v6 *xdg_shell; + struct wlr_data_device_manager *data_device_manager; + struct wl_resource *focus; + struct wl_listener keyboard_bound; + struct wlr_xwayland *xwayland; + struct wlr_gamma_control_manager *gamma_control_manager; + struct wlr_screenshooter *screenshooter; + int keymap_fd; + size_t keymap_size; + uint32_t serial; +}; + +/* + * Convert timespec to milliseconds + */ +static inline int64_t timespec_to_msec(const struct timespec *a) { + return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000; +} + +static void output_frame_handle_surface(struct sample_state *sample, + struct wlr_output *wlr_output, struct timespec *ts, + struct wl_resource *_res) { + struct wlr_surface *surface = wl_resource_get_user_data(_res); + float matrix[16]; + float transform[16]; + wlr_surface_flush_damage(surface); + if (surface->texture->valid) { + wlr_matrix_translate(&transform, 200, 200, 0); + wlr_surface_get_matrix(surface, &matrix, + &wlr_output->transform_matrix, &transform); + wlr_render_with_matrix(sample->renderer, surface->texture, &matrix); + + struct wlr_frame_callback *cb, *cnext; + wl_list_for_each_safe(cb, cnext, &surface->frame_callback_list, link) { + wl_callback_send_done(cb->resource, timespec_to_msec(ts)); + wl_resource_destroy(cb->resource); + } + } +} +static void handle_output_frame(struct output_state *output, struct timespec *ts) { + struct compositor_state *state = output->compositor; + struct sample_state *sample = state->data; + struct wlr_output *wlr_output = output->output; + + wlr_output_make_current(wlr_output); + wlr_renderer_begin(sample->renderer, wlr_output); + + struct wlr_wl_shell_surface *wl_shell_surface; + wl_list_for_each(wl_shell_surface, &sample->wl_shell->surfaces, link) { + output_frame_handle_surface(sample, wlr_output, ts, wl_shell_surface->surface); + } + struct wlr_xdg_surface_v6 *xdg_surface; + wl_list_for_each(xdg_surface, &sample->xdg_shell->surfaces, link) { + output_frame_handle_surface(sample, wlr_output, ts, xdg_surface->surface); + } + struct wlr_x11_window *x11_window; + wl_list_for_each(x11_window, &sample->xwayland->displayable_windows, link) { + output_frame_handle_surface(sample, wlr_output, ts, x11_window->surface); + } + + wlr_renderer_end(sample->renderer); + wlr_output_swap_buffers(wlr_output); +} + +static void handle_keyboard_key(struct keyboard_state *keyboard, uint32_t keycode, + xkb_keysym_t sym, enum wlr_key_state key_state) { + struct compositor_state *state = keyboard->compositor; + struct sample_state *sample = state->data; + + struct wl_resource *res = NULL; + struct wlr_seat_handle *seat_handle = NULL; + wl_list_for_each(res, &sample->wlr_compositor->surfaces, link) { + break; + } + + if (res) { + seat_handle = wlr_seat_handle_for_client(sample->wl_seat, + wl_resource_get_client(res)); + } + + if (res != sample->focus && seat_handle && seat_handle->keyboard) { + struct wl_array keys; + wl_array_init(&keys); + wl_keyboard_send_enter(seat_handle->keyboard, ++sample->serial, res, &keys); + sample->focus = res; + } + + if (seat_handle && seat_handle->keyboard) { + uint32_t depressed = xkb_state_serialize_mods(keyboard->xkb_state, + XKB_STATE_MODS_DEPRESSED); + uint32_t latched = xkb_state_serialize_mods(keyboard->xkb_state, + XKB_STATE_MODS_LATCHED); + uint32_t locked = xkb_state_serialize_mods(keyboard->xkb_state, + XKB_STATE_MODS_LOCKED); + uint32_t group = xkb_state_serialize_layout(keyboard->xkb_state, + XKB_STATE_LAYOUT_EFFECTIVE); + wl_keyboard_send_modifiers(seat_handle->keyboard, ++sample->serial, depressed, + latched, locked, group); + wl_keyboard_send_key(seat_handle->keyboard, ++sample->serial, 0, keycode, key_state); + } +} + +static void handle_keyboard_bound(struct wl_listener *listener, void *data) { + struct wlr_seat_handle *handle = data; + struct sample_state *state = wl_container_of(listener, state, keyboard_bound); + + wl_keyboard_send_keymap(handle->keyboard, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, + state->keymap_fd, state->keymap_size); + + if (wl_resource_get_version(handle->keyboard) >= 2) { + wl_keyboard_send_repeat_info(handle->keyboard, 25, 600); + } +} + +int main() { + struct sample_state state = { 0 }; + struct compositor_state compositor = { 0, + .data = &state, + .output_frame_cb = handle_output_frame, + }; + compositor_init(&compositor); + + state.renderer = wlr_gles2_renderer_create(compositor.backend); + if (!state.renderer) { + wlr_log(L_ERROR, "Could not start compositor, OOM"); + exit(EXIT_FAILURE); + } + wl_display_init_shm(compositor.display); + state.wlr_compositor = wlr_compositor_create(compositor.display, state.renderer); + state.wl_shell = wlr_wl_shell_create(compositor.display); + state.xdg_shell = wlr_xdg_shell_v6_create(compositor.display); + state.data_device_manager = wlr_data_device_manager_create(compositor.display); + state.gamma_control_manager = wlr_gamma_control_manager_create(compositor.display); + state.screenshooter = wlr_screenshooter_create(compositor.display); + + state.wl_seat = wlr_seat_create(compositor.display, "seat0"); + state.keyboard_bound.notify = handle_keyboard_bound; + wl_signal_add(&state.wl_seat->events.keyboard_bound, &state.keyboard_bound); + wlr_seat_set_capabilities(state.wl_seat, WL_SEAT_CAPABILITY_KEYBOARD + | WL_SEAT_CAPABILITY_POINTER | WL_SEAT_CAPABILITY_TOUCH); + + struct keyboard_state *kbstate; + wl_list_for_each(kbstate, &compositor.keyboards, link) { + char *keymap = xkb_keymap_get_as_string(kbstate->keymap, + XKB_KEYMAP_FORMAT_TEXT_V1); + state.keymap_size = strlen(keymap); + state.keymap_fd = os_create_anonymous_file(state.keymap_size); + void *ptr = mmap(NULL, state.keymap_size, + PROT_READ | PROT_WRITE, + MAP_SHARED, state.keymap_fd, 0); + strcpy(ptr, keymap); + free(keymap); + break; + } + state.xwayland = wlr_xwayland_create(compositor.display, state.wlr_compositor); + + compositor.keyboard_key_cb = handle_keyboard_key; + + wl_display_run(compositor.display); + + wlr_xwayland_destroy(state.xwayland); + close(state.keymap_fd); + wlr_seat_destroy(state.wl_seat); + wlr_screenshooter_destroy(state.screenshooter); + wlr_gamma_control_manager_destroy(state.gamma_control_manager); + wlr_data_device_manager_destroy(state.data_device_manager); + wlr_xdg_shell_v6_destroy(state.xdg_shell); + wlr_wl_shell_destroy(state.wl_shell); + wlr_compositor_destroy(state.wlr_compositor); + wlr_renderer_destroy(state.renderer); + compositor_fini(&compositor); +} diff --git a/include/wlr/types/wlr_screenshooter.h b/include/wlr/types/wlr_screenshooter.h new file mode 100644 index 00000000..5d1a9254 --- /dev/null +++ b/include/wlr/types/wlr_screenshooter.h @@ -0,0 +1,21 @@ +#ifndef _WLR_SCREENSHOOTER_H +#define _WLR_SCREENSHOOTER_H +#include + +struct wlr_screenshooter { + struct wl_global *wl_global; + + void *data; +}; + +struct wlr_screenshot { + struct wl_resource *resource; + struct wl_resource *output; + + void* data; +}; + +struct wlr_screenshooter *wlr_screenshooter_create(struct wl_display *display); +void wlr_screenshooter_destroy(struct wlr_screenshooter *screenshooter); + +#endif diff --git a/protocol/meson.build b/protocol/meson.build index 6dc88d76..28a7b668 100644 --- a/protocol/meson.build +++ b/protocol/meson.build @@ -22,11 +22,12 @@ 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'], ] 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 @@ + + + + + + + + + + + + + + + + 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_screenshooter.c b/types/wlr_screenshooter.c new file mode 100644 index 00000000..20be1103 --- /dev/null +++ b/types/wlr_screenshooter.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include +#include "screenshooter-protocol.h" + +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_screenshot *screenshot; + if (!(screenshot = calloc(1, sizeof(struct wlr_screenshot)))) { + return; + } + screenshot->output = _output; + 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); + // TODO: orbital_screenshot_send_done(screenshot->resource); +} + +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_screenshooter *screenshooter = + calloc(1, sizeof(struct wlr_screenshooter)); + if (!screenshooter) { + return NULL; + } + 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); +} From 35f970025133dc5ad627936a3cdeacb37b532072 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 8 Sep 2017 18:09:09 +0200 Subject: [PATCH 02/15] First attempt to implement screenshooter --- backend/drm/drm.c | 10 + examples/meson.build | 6 + examples/screenshot.c | 292 ++++++++++++++++++++++++++++ include/wlr/interfaces/wlr_output.h | 1 + include/wlr/types/wlr_output.h | 1 + protocol/meson.build | 2 + types/wlr_output.c | 6 + types/wlr_screenshooter.c | 43 +++- 8 files changed, 360 insertions(+), 1 deletion(-) create mode 100644 examples/screenshot.c 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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, ®istry_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 #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 = { From d0db6a80ab64ee1f5d090677c18f57109b36575a Mon Sep 17 00:00:00 2001 From: emersion Date: Sat, 9 Sep 2017 11:32:03 +0200 Subject: [PATCH 03/15] Add post_frame event to wlr_output, use wlr_drm_plane_make_current in wlr_drm_output_read_pixels --- backend/drm/drm.c | 7 ++++--- include/wlr/types/wlr_output.h | 1 + types/wlr_output.c | 1 + types/wlr_screenshooter.c | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/backend/drm/drm.c b/backend/drm/drm.c index 117e5749..ec0c29f7 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -636,9 +636,9 @@ static bool wlr_drm_connector_move_cursor(struct wlr_output *output, 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; + struct wlr_drm_connector *conn = (struct wlr_drm_connector *)output; + struct wlr_drm_plane *plane = conn->crtc->primary; + wlr_drm_plane_make_current(conn->renderer, plane); glReadPixels(0, 0, plane->width, plane->height, GL_BGRA_EXT, GL_UNSIGNED_BYTE, out_data); } @@ -846,6 +846,7 @@ static void page_flip_handler(int fd, unsigned seq, if (drm->session->active) { wl_signal_emit(&conn->output.events.frame, &conn->output); + wl_signal_emit(&conn->output.events.post_frame, &conn->output); } } diff --git a/include/wlr/types/wlr_output.h b/include/wlr/types/wlr_output.h index 509e1bcb..a7ad2dbd 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 post_frame; struct wl_signal resolution; struct wl_signal destroy; } events; diff --git a/types/wlr_output.c b/types/wlr_output.c index abad1aec..e0220e6b 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.post_frame); wl_signal_init(&output->events.resolution); wl_signal_init(&output->events.destroy); } diff --git a/types/wlr_screenshooter.c b/types/wlr_screenshooter.c index 43e5fcd9..5f20925c 100644 --- a/types/wlr_screenshooter.c +++ b/types/wlr_screenshooter.c @@ -59,7 +59,7 @@ static void screenshooter_shoot(struct wl_client *client, state->output = output; state->screenshot = screenshot; state->frame_listener.notify = output_frame_notify; - wl_signal_add(&output->events.frame, &state->frame_listener); + wl_signal_add(&output->events.post_frame, &state->frame_listener); } static struct orbital_screenshooter_interface screenshooter_impl = { From 3eefd75e7efa1b983fcb528ccee64f33e26e3c75 Mon Sep 17 00:00:00 2001 From: emersion Date: Sat, 7 Oct 2017 11:03:53 +0200 Subject: [PATCH 04/15] Rebase wlr_drm_connector_read_pixels --- backend/drm/drm.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/backend/drm/drm.c b/backend/drm/drm.c index ec0c29f7..d08fa7f4 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -634,13 +634,12 @@ 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, +static void wlr_drm_connector_read_pixels(struct wlr_output *output, void *out_data) { - struct wlr_drm_connector *conn = (struct wlr_drm_connector *)output; - struct wlr_drm_plane *plane = conn->crtc->primary; - wlr_drm_plane_make_current(conn->renderer, plane); - glReadPixels(0, 0, plane->width, plane->height, GL_BGRA_EXT, GL_UNSIGNED_BYTE, - out_data); + int width, height; + wlr_output_effective_resolution(output, &width, &height); + wlr_drm_connector_make_current(output); + glReadPixels(0, 0, width, height, GL_BGRA_EXT, GL_UNSIGNED_BYTE, out_data); } static void wlr_drm_connector_destroy(struct wlr_output *output) { From 05096ab45817127f75aecf10d94f082ec1815ee4 Mon Sep 17 00:00:00 2001 From: emersion Date: Sat, 7 Oct 2017 11:07:51 +0200 Subject: [PATCH 05/15] rootston: create screenshooter interface --- include/rootston/desktop.h | 2 ++ rootston/desktop.c | 12 ++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) 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 #include #include +#include #include #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/rootston/desktop.c b/rootston/desktop.c index 691d5809..65333e77 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -154,16 +154,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, @@ -182,7 +181,8 @@ 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); return desktop; } From d4cc82f11a4917328366926083a703d85e50ddd6 Mon Sep 17 00:00:00 2001 From: emersion Date: Sat, 7 Oct 2017 12:01:21 +0200 Subject: [PATCH 06/15] Call glReadPixels right before swapping buffers --- backend/drm/drm.c | 10 ---------- examples/screenshot.c | 2 +- include/wlr/interfaces/wlr_output.h | 1 - include/wlr/types/wlr_output.h | 2 +- types/wlr_output.c | 12 ++++++++---- types/wlr_screenshooter.c | 2 +- 6 files changed, 11 insertions(+), 18 deletions(-) diff --git a/backend/drm/drm.c b/backend/drm/drm.c index d08fa7f4..9e5346a1 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -634,14 +634,6 @@ 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) { - int width, height; - wlr_output_effective_resolution(output, &width, &height); - wlr_drm_connector_make_current(output); - glReadPixels(0, 0, width, 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); @@ -660,7 +652,6 @@ 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) { @@ -845,7 +836,6 @@ static void page_flip_handler(int fd, unsigned seq, if (drm->session->active) { wl_signal_emit(&conn->output.events.frame, &conn->output); - wl_signal_emit(&conn->output.events.post_frame, &conn->output); } } diff --git a/examples/screenshot.c b/examples/screenshot.c index 0f1f1efe..f70b9b56 100644 --- a/examples/screenshot.c +++ b/examples/screenshot.c @@ -204,7 +204,7 @@ write_png(int width, int height) } surface = cairo_image_surface_create_for_data(data, - CAIRO_FORMAT_ARGB32, + CAIRO_FORMAT_RGB24, width, height, buffer_stride); cairo_surface_write_to_png(surface, "wayland-screenshot.png"); cairo_surface_destroy(surface); diff --git a/include/wlr/interfaces/wlr_output.h b/include/wlr/interfaces/wlr_output.h index 2fd306d7..7d2821e0 100644 --- a/include/wlr/interfaces/wlr_output.h +++ b/include/wlr/interfaces/wlr_output.h @@ -19,7 +19,6 @@ 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 a7ad2dbd..7fab1cf6 100644 --- a/include/wlr/types/wlr_output.h +++ b/include/wlr/types/wlr_output.h @@ -37,7 +37,7 @@ struct wlr_output { struct { struct wl_signal frame; - struct wl_signal post_frame; + struct wl_signal swap_buffers; struct wl_signal resolution; struct wl_signal destroy; } events; diff --git a/types/wlr_output.c b/types/wlr_output.c index e0220e6b..85357d2a 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -105,7 +106,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.post_frame); + wl_signal_init(&output->events.swap_buffers); wl_signal_init(&output->events.resolution); wl_signal_init(&output->events.destroy); } @@ -232,6 +233,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); } @@ -250,7 +253,8 @@ uint16_t wlr_output_get_gamma_size(struct wlr_output *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); - } + // TODO: is wlr_output_make_current required? + wlr_output_make_current(output); + glReadPixels(0, 0, output->width, output->height, GL_BGRA_EXT, + GL_UNSIGNED_BYTE, out_data); } diff --git a/types/wlr_screenshooter.c b/types/wlr_screenshooter.c index 5f20925c..48db62c0 100644 --- a/types/wlr_screenshooter.c +++ b/types/wlr_screenshooter.c @@ -59,7 +59,7 @@ static void screenshooter_shoot(struct wl_client *client, state->output = output; state->screenshot = screenshot; state->frame_listener.notify = output_frame_notify; - wl_signal_add(&output->events.post_frame, &state->frame_listener); + wl_signal_add(&output->events.swap_buffers, &state->frame_listener); } static struct orbital_screenshooter_interface screenshooter_impl = { From ecd5263d4d0c243b9fecfd9b71c0afaa5991a8df Mon Sep 17 00:00:00 2001 From: emersion Date: Sat, 7 Oct 2017 12:22:48 +0200 Subject: [PATCH 07/15] Flip screenshots --- types/wlr_screenshooter.c | 47 +++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/types/wlr_screenshooter.c b/types/wlr_screenshooter.c index 48db62c0..f2d0937b 100644 --- a/types/wlr_screenshooter.c +++ b/types/wlr_screenshooter.c @@ -1,12 +1,25 @@ #include #include +#include #include #include #include #include #include "screenshooter-protocol.h" +static void copy_bgra_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_output *output; struct wlr_screenshot *screenshot; @@ -14,14 +27,20 @@ struct screenshot_state { }; static void output_frame_notify(struct wl_listener *listener, void *_data) { - struct screenshot_state *state = wl_container_of(listener, state, frame_listener); + struct screenshot_state *state = wl_container_of(listener, state, + frame_listener); + + wlr_output_read_pixels(state->output, state->pixels); 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); + copy_bgra_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); // TODO: free(state) @@ -38,23 +57,36 @@ static void screenshooter_shoot(struct wl_client *client, 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); + 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; } + uint8_t *pixels = malloc(stride * height); + if (pixels == NULL) { + wl_client_post_no_memory(client); + return; + } + struct wlr_screenshot *screenshot; if (!(screenshot = calloc(1, sizeof(struct wlr_screenshot)))) { return; } screenshot->output = _output; 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); + &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)); + state->width = width; + state->height = height; + state->stride = stride; + state->pixels = pixels; state->shm_buffer = shm_buffer; state->output = output; state->screenshot = screenshot; @@ -71,7 +103,8 @@ static void screenshooter_bind(struct wl_client *wl_client, struct wlr_screenshooter *screenshooter = _screenshooter; assert(wl_client && screenshooter); if (version > 1) { - wlr_log(L_ERROR, "Client requested unsupported screenshooter version, disconnecting"); + wlr_log(L_ERROR, "Client requested unsupported screenshooter version," + "disconnecting"); wl_client_destroy(wl_client); return; } From 4374c521353d1f3268b7bd6c7864223bc0eff3b3 Mon Sep 17 00:00:00 2001 From: emersion Date: Sat, 7 Oct 2017 13:09:55 +0200 Subject: [PATCH 08/15] Free screenshot state --- types/wlr_screenshooter.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/types/wlr_screenshooter.c b/types/wlr_screenshooter.c index f2d0937b..b16c1ba9 100644 --- a/types/wlr_screenshooter.c +++ b/types/wlr_screenshooter.c @@ -43,7 +43,7 @@ static void output_frame_notify(struct wl_listener *listener, void *_data) { orbital_screenshot_send_done(state->screenshot->resource); - // TODO: free(state) + free(state); } static void screenshooter_shoot(struct wl_client *client, @@ -69,8 +69,10 @@ static void screenshooter_shoot(struct wl_client *client, return; } - struct wlr_screenshot *screenshot; - if (!(screenshot = calloc(1, sizeof(struct wlr_screenshot)))) { + struct wlr_screenshot *screenshot = + calloc(1, sizeof(struct wlr_screenshot)); + if (!screenshot) { + wl_client_post_no_memory(client); return; } screenshot->output = _output; From b27b6cd69c25284541b8ec585ddbc5f0294f52e6 Mon Sep 17 00:00:00 2001 From: emersion Date: Sat, 7 Oct 2017 18:25:27 +0200 Subject: [PATCH 09/15] Check wl_shm_buffer format --- types/wlr_screenshooter.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/types/wlr_screenshooter.c b/types/wlr_screenshooter.c index b16c1ba9..ddcaf52b 100644 --- a/types/wlr_screenshooter.c +++ b/types/wlr_screenshooter.c @@ -7,7 +7,7 @@ #include #include "screenshooter-protocol.h" -static void copy_bgra_yflip(uint8_t *dst, uint8_t *src, int32_t height, +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) { @@ -34,7 +34,7 @@ static void output_frame_notify(struct wl_listener *listener, void *_data) { void *data = wl_shm_buffer_get_data(state->shm_buffer); wl_shm_buffer_begin_access(state->shm_buffer); - copy_bgra_yflip(data, state->pixels + state->stride * (state->height - 1), + copy_yflip(data, state->pixels + state->stride * (state->height - 1), state->height, state->stride); wl_shm_buffer_end_access(state->shm_buffer); @@ -63,6 +63,12 @@ static void screenshooter_shoot(struct wl_client *client, 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); From a87f0160174487a1bbb1280597b73d36755542de Mon Sep 17 00:00:00 2001 From: emersion Date: Sat, 7 Oct 2017 19:01:11 +0200 Subject: [PATCH 10/15] Remove cairo dependency, write raw pixels --- examples/meson.build | 2 +- examples/screenshot.c | 24 +++++++++++++++++------- include/wlr/types/wlr_output.h | 4 ++++ types/wlr_output.c | 21 +++++++++++++++------ 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/examples/meson.build b/examples/meson.build index 292ab053..a29a3310 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -26,5 +26,5 @@ executable( executable( 'screenshot', 'screenshot.c', - dependencies: [wayland_client, wlr_protos, dependency('cairo')], + dependencies: [wayland_client, wlr_protos], ) diff --git a/examples/screenshot.c b/examples/screenshot.c index f70b9b56..253638c9 100644 --- a/examples/screenshot.c +++ b/examples/screenshot.c @@ -33,7 +33,6 @@ #include #include #include -#include #include #include "../backend/wayland/os-compatibility.c" @@ -174,11 +173,20 @@ create_shm_buffer(int width, int height, void **data_out) 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_png(int width, int height) { int output_stride, buffer_stride, i; - cairo_surface_t *surface; void *data, *d, *s; struct screenshooter_output *output, *next; @@ -203,11 +211,13 @@ write_png(int width, int height) free(output); } - surface = cairo_image_surface_create_for_data(data, - CAIRO_FORMAT_RGB24, - width, height, buffer_stride); - cairo_surface_write_to_png(surface, "wayland-screenshot.png"); - cairo_surface_destroy(surface); + argb_to_rgba(data, height, buffer_stride); + + // TODO: call convert + FILE *f = fopen("wayland-screenshot", "w"); + fwrite(data, buffer_stride * height, 1, f); + fclose(f); + free(data); } diff --git a/include/wlr/types/wlr_output.h b/include/wlr/types/wlr_output.h index 7fab1cf6..ab3be5c7 100644 --- a/include/wlr/types/wlr_output.h +++ b/include/wlr/types/wlr_output.h @@ -71,6 +71,10 @@ 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); + +/** + * Reads all pixels from the output and stores them as ARGB. + */ void wlr_output_read_pixels(struct wlr_output *output, void *out_data); #endif diff --git a/types/wlr_output.c b/types/wlr_output.c index 85357d2a..1db63151 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -252,9 +251,19 @@ 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) { - // TODO: is wlr_output_make_current required? - wlr_output_make_current(output); - glReadPixels(0, 0, output->width, output->height, GL_BGRA_EXT, - GL_UNSIGNED_BYTE, out_data); +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); + } +} + +void wlr_output_read_pixels(struct wlr_output *output, void *out_data) { + wlr_output_make_current(output); + glReadPixels(0, 0, output->width, output->height, GL_RGBA, GL_UNSIGNED_BYTE, + out_data); + rgba_to_argb(out_data, output->height, output->width*4); } From 63af97800f1979387ad1972e01734392c7ed7394 Mon Sep 17 00:00:00 2001 From: emersion Date: Sat, 7 Oct 2017 19:12:47 +0200 Subject: [PATCH 11/15] Use ImageMagick to write PNG screenshot --- examples/screenshot.c | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/examples/screenshot.c b/examples/screenshot.c index 253638c9..3dcab09d 100644 --- a/examples/screenshot.c +++ b/examples/screenshot.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -184,7 +185,7 @@ static void argb_to_rgba(uint32_t *data, size_t height, size_t stride) { } static void -write_png(int width, int height) +write_image(const char *filename, int width, int height) { int output_stride, buffer_stride, i; void *data, *d, *s; @@ -213,12 +214,33 @@ write_png(int width, int height) argb_to_rgba(data, height, buffer_stride); - // TODO: call convert - FILE *f = fopen("wayland-screenshot", "w"); - fwrite(data, buffer_stride * height, 1, f); - fclose(f); + char size[10 + 1 + 10 + 2 + 1]; // int32_t are max 10 digits + sprintf(size, "%dx%d+0", width, height); - free(data); + 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"); + } + 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 @@ -296,7 +318,6 @@ int main(int argc, char *argv[]) wl_display_roundtrip(display); } - write_png(width, height); - - return 0; + write_image("wayland-screenshot.png", width, height); + return EXIT_SUCCESS; } From c9909a45abcfbc8ce909b6acdcbb990ce439c609 Mon Sep 17 00:00:00 2001 From: emersion Date: Sat, 7 Oct 2017 19:22:55 +0200 Subject: [PATCH 12/15] Code style --- examples/screenshot.c | 130 +++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 85 deletions(-) diff --git a/examples/screenshot.c b/examples/screenshot.c index 3dcab09d..c0735981 100644 --- a/examples/screenshot.c +++ b/examples/screenshot.c @@ -51,21 +51,10 @@ struct screenshooter_output { 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); +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 = wl_output_get_user_data(wl_output); if (wl_output == output->output) { output->offset_x = x; @@ -73,17 +62,9 @@ display_handle_geometry(void *data, } } -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); +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 = wl_output_get_user_data(wl_output); if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) { output->width = width; @@ -96,9 +77,7 @@ static const struct wl_output_listener output_listener = { display_handle_mode }; -static void -screenshot_done(void *data, struct orbital_screenshot *screenshot) -{ +static void screenshot_done(void *data, struct orbital_screenshot *screenshot) { buffer_copy_done = 1; } @@ -106,31 +85,27 @@ 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 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); + 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); + &orbital_screenshooter_interface, 1); } } -static void -handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) -{ - /* XXX: unimplemented */ +static void handle_global_remove(void *data, struct wl_registry *registry, + uint32_t name) { + // Unimplemented } static const struct wl_registry_listener registry_listener = { @@ -138,35 +113,28 @@ static const struct wl_registry_listener registry_listener = { 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; +static struct wl_buffer *create_shm_buffer(int width, int height, + void **data_out) { + int stride = width * 4; + int size = stride * height; - stride = width * 4; - size = stride * height; - - fd = os_create_anonymous_file(size); + int fd = os_create_anonymous_file(size); if (fd < 0) { - fprintf(stderr, "creating a buffer file for %d B failed: %m\n", - size); + 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); + 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; } - pool = wl_shm_create_pool(shm, fd, size); + struct wl_shm_pool *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); + 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; @@ -184,26 +152,22 @@ static void argb_to_rgba(uint32_t *data, size_t height, size_t stride) { } } -static void -write_image(const char *filename, int width, int height) -{ - int output_stride, buffer_stride, i; - void *data, *d, *s; - struct screenshooter_output *output, *next; +static void write_image(const char *filename, int width, int height) { + int buffer_stride = width * 4; - buffer_stride = width * 4; - - data = calloc(1, buffer_stride * height); - if (!data) + 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) { - output_stride = output->width * 4; - s = output->data; - d = data + (output->offset_y - min_y) * buffer_stride + + 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 (i = 0; i < output->height; i++) { + for (int i = 0; i < output->height; i++) { memcpy(d, s, output_stride); d += buffer_stride; s += output_stride; @@ -234,6 +198,7 @@ write_image(const char *filename, int width, int height) 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:-", @@ -243,9 +208,7 @@ write_image(const char *filename, int width, int height) } } -static int -set_buffer_size(int *width, int *height) -{ +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; @@ -263,8 +226,9 @@ set_buffer_size(int *width, int *height) max_y = MAX(max_y, output->offset_y + output->height); } - if (max_x <= min_x || max_y <= min_y) + if (max_x <= min_x || max_y <= min_y) { return -1; + } *width = max_x - min_x; *height = max_y - min_y; @@ -272,21 +236,15 @@ set_buffer_size(int *width, int *height) 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); +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); - registry = wl_display_get_registry(display); + struct wl_registry *registry = wl_display_get_registry(display); wl_registry_add_listener(registry, ®istry_listener, NULL); wl_display_dispatch(display); wl_display_roundtrip(display); @@ -296,11 +254,13 @@ int main(int argc, char *argv[]) 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; From e4a8831bf165d07cf9d2cd47a749b61e0fa4dbb6 Mon Sep 17 00:00:00 2001 From: emersion Date: Sat, 7 Oct 2017 19:30:00 +0200 Subject: [PATCH 13/15] Fix GCC build --- examples/screenshot.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/examples/screenshot.c b/examples/screenshot.c index c0735981..95af49ca 100644 --- a/examples/screenshot.c +++ b/examples/screenshot.c @@ -37,8 +37,8 @@ #include #include "../backend/wayland/os-compatibility.c" -static struct wl_shm *shm; -static struct orbital_screenshooter *screenshooter; +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; @@ -51,7 +51,7 @@ struct screenshooter_output { struct wl_list link; }; -static void display_handle_geometry(void *data, struct wl_output *wl_output, +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); @@ -62,7 +62,7 @@ static void display_handle_geometry(void *data, struct wl_output *wl_output, } } -static void display_handle_mode(void *data, struct wl_output *wl_output, +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); @@ -72,9 +72,14 @@ static void display_handle_mode(void *data, struct wl_output *wl_output, } } +static void output_handle_done(void *data, struct wl_output *wl_output) { + +} + static const struct wl_output_listener output_listener = { - display_handle_geometry, - display_handle_mode + .geometry = output_handle_geometry, + .mode = output_handle_mode, + .done = output_handle_done, }; static void screenshot_done(void *data, struct orbital_screenshot *screenshot) { @@ -82,7 +87,7 @@ static void screenshot_done(void *data, struct orbital_screenshot *screenshot) { } static const struct orbital_screenshot_listener screenshot_listener = { - screenshot_done + .done = screenshot_done, }; static void handle_global(void *data, struct wl_registry *registry, @@ -109,8 +114,8 @@ static void handle_global_remove(void *data, struct wl_registry *registry, } static const struct wl_registry_listener registry_listener = { - handle_global, - handle_global_remove + .global = handle_global, + .global_remove = handle_global_remove, }; static struct wl_buffer *create_shm_buffer(int width, int height, @@ -165,7 +170,7 @@ static void write_image(const char *filename, int width, int height) { 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; + (output->offset_x - min_x) * 4; for (int i = 0; i < output->height; i++) { memcpy(d, s, output_stride); From 55c063f440fd12b1ee8bba5cef56a75c33e66719 Mon Sep 17 00:00:00 2001 From: emersion Date: Sat, 7 Oct 2017 19:37:49 +0200 Subject: [PATCH 14/15] Remove examples/compositor.c, re-added by mistake --- examples/compositor.c | 208 ------------------------------------------ 1 file changed, 208 deletions(-) delete mode 100644 examples/compositor.c diff --git a/examples/compositor.c b/examples/compositor.c deleted file mode 100644 index 12577dc1..00000000 --- a/examples/compositor.c +++ /dev/null @@ -1,208 +0,0 @@ -#define _POSIX_C_SOURCE 199309L -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "wlr/types/wlr_compositor.h" -#include -#include -#include -#include "shared.h" - -// TODO: move to common header? -int os_create_anonymous_file(off_t size); - -struct sample_state { - struct wlr_renderer *renderer; - struct wlr_compositor *wlr_compositor; - struct wlr_wl_shell *wl_shell; - struct wlr_seat *wl_seat; - struct wlr_xdg_shell_v6 *xdg_shell; - struct wlr_data_device_manager *data_device_manager; - struct wl_resource *focus; - struct wl_listener keyboard_bound; - struct wlr_xwayland *xwayland; - struct wlr_gamma_control_manager *gamma_control_manager; - struct wlr_screenshooter *screenshooter; - int keymap_fd; - size_t keymap_size; - uint32_t serial; -}; - -/* - * Convert timespec to milliseconds - */ -static inline int64_t timespec_to_msec(const struct timespec *a) { - return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000; -} - -static void output_frame_handle_surface(struct sample_state *sample, - struct wlr_output *wlr_output, struct timespec *ts, - struct wl_resource *_res) { - struct wlr_surface *surface = wl_resource_get_user_data(_res); - float matrix[16]; - float transform[16]; - wlr_surface_flush_damage(surface); - if (surface->texture->valid) { - wlr_matrix_translate(&transform, 200, 200, 0); - wlr_surface_get_matrix(surface, &matrix, - &wlr_output->transform_matrix, &transform); - wlr_render_with_matrix(sample->renderer, surface->texture, &matrix); - - struct wlr_frame_callback *cb, *cnext; - wl_list_for_each_safe(cb, cnext, &surface->frame_callback_list, link) { - wl_callback_send_done(cb->resource, timespec_to_msec(ts)); - wl_resource_destroy(cb->resource); - } - } -} -static void handle_output_frame(struct output_state *output, struct timespec *ts) { - struct compositor_state *state = output->compositor; - struct sample_state *sample = state->data; - struct wlr_output *wlr_output = output->output; - - wlr_output_make_current(wlr_output); - wlr_renderer_begin(sample->renderer, wlr_output); - - struct wlr_wl_shell_surface *wl_shell_surface; - wl_list_for_each(wl_shell_surface, &sample->wl_shell->surfaces, link) { - output_frame_handle_surface(sample, wlr_output, ts, wl_shell_surface->surface); - } - struct wlr_xdg_surface_v6 *xdg_surface; - wl_list_for_each(xdg_surface, &sample->xdg_shell->surfaces, link) { - output_frame_handle_surface(sample, wlr_output, ts, xdg_surface->surface); - } - struct wlr_x11_window *x11_window; - wl_list_for_each(x11_window, &sample->xwayland->displayable_windows, link) { - output_frame_handle_surface(sample, wlr_output, ts, x11_window->surface); - } - - wlr_renderer_end(sample->renderer); - wlr_output_swap_buffers(wlr_output); -} - -static void handle_keyboard_key(struct keyboard_state *keyboard, uint32_t keycode, - xkb_keysym_t sym, enum wlr_key_state key_state) { - struct compositor_state *state = keyboard->compositor; - struct sample_state *sample = state->data; - - struct wl_resource *res = NULL; - struct wlr_seat_handle *seat_handle = NULL; - wl_list_for_each(res, &sample->wlr_compositor->surfaces, link) { - break; - } - - if (res) { - seat_handle = wlr_seat_handle_for_client(sample->wl_seat, - wl_resource_get_client(res)); - } - - if (res != sample->focus && seat_handle && seat_handle->keyboard) { - struct wl_array keys; - wl_array_init(&keys); - wl_keyboard_send_enter(seat_handle->keyboard, ++sample->serial, res, &keys); - sample->focus = res; - } - - if (seat_handle && seat_handle->keyboard) { - uint32_t depressed = xkb_state_serialize_mods(keyboard->xkb_state, - XKB_STATE_MODS_DEPRESSED); - uint32_t latched = xkb_state_serialize_mods(keyboard->xkb_state, - XKB_STATE_MODS_LATCHED); - uint32_t locked = xkb_state_serialize_mods(keyboard->xkb_state, - XKB_STATE_MODS_LOCKED); - uint32_t group = xkb_state_serialize_layout(keyboard->xkb_state, - XKB_STATE_LAYOUT_EFFECTIVE); - wl_keyboard_send_modifiers(seat_handle->keyboard, ++sample->serial, depressed, - latched, locked, group); - wl_keyboard_send_key(seat_handle->keyboard, ++sample->serial, 0, keycode, key_state); - } -} - -static void handle_keyboard_bound(struct wl_listener *listener, void *data) { - struct wlr_seat_handle *handle = data; - struct sample_state *state = wl_container_of(listener, state, keyboard_bound); - - wl_keyboard_send_keymap(handle->keyboard, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, - state->keymap_fd, state->keymap_size); - - if (wl_resource_get_version(handle->keyboard) >= 2) { - wl_keyboard_send_repeat_info(handle->keyboard, 25, 600); - } -} - -int main() { - struct sample_state state = { 0 }; - struct compositor_state compositor = { 0, - .data = &state, - .output_frame_cb = handle_output_frame, - }; - compositor_init(&compositor); - - state.renderer = wlr_gles2_renderer_create(compositor.backend); - if (!state.renderer) { - wlr_log(L_ERROR, "Could not start compositor, OOM"); - exit(EXIT_FAILURE); - } - wl_display_init_shm(compositor.display); - state.wlr_compositor = wlr_compositor_create(compositor.display, state.renderer); - state.wl_shell = wlr_wl_shell_create(compositor.display); - state.xdg_shell = wlr_xdg_shell_v6_create(compositor.display); - state.data_device_manager = wlr_data_device_manager_create(compositor.display); - state.gamma_control_manager = wlr_gamma_control_manager_create(compositor.display); - state.screenshooter = wlr_screenshooter_create(compositor.display); - - state.wl_seat = wlr_seat_create(compositor.display, "seat0"); - state.keyboard_bound.notify = handle_keyboard_bound; - wl_signal_add(&state.wl_seat->events.keyboard_bound, &state.keyboard_bound); - wlr_seat_set_capabilities(state.wl_seat, WL_SEAT_CAPABILITY_KEYBOARD - | WL_SEAT_CAPABILITY_POINTER | WL_SEAT_CAPABILITY_TOUCH); - - struct keyboard_state *kbstate; - wl_list_for_each(kbstate, &compositor.keyboards, link) { - char *keymap = xkb_keymap_get_as_string(kbstate->keymap, - XKB_KEYMAP_FORMAT_TEXT_V1); - state.keymap_size = strlen(keymap); - state.keymap_fd = os_create_anonymous_file(state.keymap_size); - void *ptr = mmap(NULL, state.keymap_size, - PROT_READ | PROT_WRITE, - MAP_SHARED, state.keymap_fd, 0); - strcpy(ptr, keymap); - free(keymap); - break; - } - state.xwayland = wlr_xwayland_create(compositor.display, state.wlr_compositor); - - compositor.keyboard_key_cb = handle_keyboard_key; - - wl_display_run(compositor.display); - - wlr_xwayland_destroy(state.xwayland); - close(state.keymap_fd); - wlr_seat_destroy(state.wl_seat); - wlr_screenshooter_destroy(state.screenshooter); - wlr_gamma_control_manager_destroy(state.gamma_control_manager); - wlr_data_device_manager_destroy(state.data_device_manager); - wlr_xdg_shell_v6_destroy(state.xdg_shell); - wlr_wl_shell_destroy(state.wl_shell); - wlr_compositor_destroy(state.wlr_compositor); - wlr_renderer_destroy(state.renderer); - compositor_fini(&compositor); -} From 0ce313530446b801302850f03c4a8d8c20cfd782 Mon Sep 17 00:00:00 2001 From: emersion Date: Sun, 8 Oct 2017 02:11:56 +0200 Subject: [PATCH 15/15] Move read_pixels from output to renderer --- include/wlr/render.h | 15 ++++++++++----- include/wlr/render/interface.h | 2 ++ include/wlr/types/wlr_output.h | 5 ----- include/wlr/types/wlr_screenshooter.h | 9 +++++++-- render/gles2/renderer.c | 19 ++++++++++++++++++- render/wlr_renderer.c | 5 +++++ rootston/desktop.c | 3 ++- types/wlr_output.c | 17 ----------------- types/wlr_screenshooter.c | 25 ++++++++++++++++++++----- 9 files changed, 64 insertions(+), 36 deletions(-) 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 ab3be5c7..52d377e3 100644 --- a/include/wlr/types/wlr_output.h +++ b/include/wlr/types/wlr_output.h @@ -72,9 +72,4 @@ 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); -/** - * Reads all pixels from the output and stores them as ARGB. - */ -void wlr_output_read_pixels(struct wlr_output *output, void *out_data); - #endif diff --git a/include/wlr/types/wlr_screenshooter.h b/include/wlr/types/wlr_screenshooter.h index 5d1a9254..4bda3d3c 100644 --- a/include/wlr/types/wlr_screenshooter.h +++ b/include/wlr/types/wlr_screenshooter.h @@ -4,18 +4,23 @@ 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; + 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_screenshooter *wlr_screenshooter_create(struct wl_display *display, + struct wlr_renderer *renderer); void wlr_screenshooter_destroy(struct wlr_screenshooter *screenshooter); #endif diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c index d6c22ebe..9a013338 100644 --- a/render/gles2/renderer.c +++ b/render/gles2/renderer.c @@ -226,7 +226,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 +254,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/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 65333e77..c10cd641 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -182,7 +182,8 @@ struct roots_desktop *desktop_create(struct roots_server *server, desktop->gamma_control_manager = wlr_gamma_control_manager_create( server->wl_display); - desktop->screenshooter = wlr_screenshooter_create(server->wl_display); + desktop->screenshooter = wlr_screenshooter_create(server->wl_display, + server->renderer); return desktop; } diff --git a/types/wlr_output.c b/types/wlr_output.c index 1db63151..64f67f2d 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -250,20 +250,3 @@ uint16_t wlr_output_get_gamma_size(struct wlr_output *output) { } return output->impl->get_gamma_size(output); } - -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); - } -} - -void wlr_output_read_pixels(struct wlr_output *output, void *out_data) { - wlr_output_make_current(output); - glReadPixels(0, 0, output->width, output->height, GL_RGBA, GL_UNSIGNED_BYTE, - out_data); - rgba_to_argb(out_data, output->height, output->width*4); -} diff --git a/types/wlr_screenshooter.c b/types/wlr_screenshooter.c index ddcaf52b..38204aa1 100644 --- a/types/wlr_screenshooter.c +++ b/types/wlr_screenshooter.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -21,7 +22,6 @@ struct screenshot_state { int32_t width, height, stride; uint8_t *pixels; struct wl_shm_buffer *shm_buffer; - struct wlr_output *output; struct wlr_screenshot *screenshot; struct wl_listener frame_listener; }; @@ -29,8 +29,12 @@ struct screenshot_state { 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_read_pixels(state->output, state->pixels); + 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); @@ -49,6 +53,8 @@ static void output_frame_notify(struct wl_listener *listener, void *_data) { 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"); @@ -81,7 +87,9 @@ static void screenshooter_shoot(struct wl_client *client, wl_client_post_no_memory(client); return; } - screenshot->output = _output; + 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); @@ -91,12 +99,15 @@ static void screenshooter_shoot(struct wl_client *client, 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->output = output; state->screenshot = screenshot; state->frame_listener.notify = output_frame_notify; wl_signal_add(&output->events.swap_buffers, &state->frame_listener); @@ -122,12 +133,15 @@ static void screenshooter_bind(struct wl_client *wl_client, screenshooter, NULL); } -struct wlr_screenshooter *wlr_screenshooter_create(struct wl_display *display) { +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) { @@ -135,6 +149,7 @@ struct wlr_screenshooter *wlr_screenshooter_create(struct wl_display *display) { return NULL; } screenshooter->wl_global = wl_global; + return screenshooter; }