From bf7560b7cda5d3df8cdc7d24e969bfd91cf0ded5 Mon Sep 17 00:00:00 2001 From: emersion Date: Sat, 23 Jun 2018 15:12:17 +0100 Subject: [PATCH] screencopy: add capture_output_region support --- include/wlr/types/wlr_screencopy_v1.h | 2 +- protocol/wlr-screencopy-unstable-v1.xml | 29 ++++++++- render/gles2/renderer.c | 11 ++-- types/wlr_screencopy_v1.c | 78 +++++++++++++++++++------ 4 files changed, 93 insertions(+), 27 deletions(-) diff --git a/include/wlr/types/wlr_screencopy_v1.h b/include/wlr/types/wlr_screencopy_v1.h index fbcc1ce20..4766d6800 100644 --- a/include/wlr/types/wlr_screencopy_v1.h +++ b/include/wlr/types/wlr_screencopy_v1.h @@ -18,7 +18,7 @@ struct wlr_screencopy_frame_v1 { struct wlr_screencopy_manager_v1 *manager; struct wl_list link; - int32_t width, height; + struct wlr_box buffer_box; struct wl_shm_buffer *buffer; diff --git a/protocol/wlr-screencopy-unstable-v1.xml b/protocol/wlr-screencopy-unstable-v1.xml index e62e3ff00..c54b72681 100644 --- a/protocol/wlr-screencopy-unstable-v1.xml +++ b/protocol/wlr-screencopy-unstable-v1.xml @@ -43,16 +43,39 @@ source. + + + + - - Capture the next frame of a an entire output. + + Capture the next frame of an entire output. + summary="composite cursor onto the frame"/> + + + Capture the next frame of an output's region. + + The region is given in output logical coordinates, see + xdg_output.logical_size. Trying to capture a region spanning outside the + output extents is a protocol error. + + + + + + + + + + All objects created by the manager will still remain valid, until their diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c index 0c57bfa15..e8b0b27d3 100644 --- a/render/gles2/renderer.c +++ b/render/gles2/renderer.c @@ -253,7 +253,8 @@ static bool gles2_read_pixels(struct wlr_renderer *wlr_renderer, enum wl_shm_format wl_fmt, uint32_t *flags, uint32_t stride, uint32_t width, uint32_t height, uint32_t src_x, uint32_t src_y, uint32_t dst_x, uint32_t dst_y, void *data) { - gles2_get_renderer_in_context(wlr_renderer); + struct wlr_gles2_renderer *renderer = + gles2_get_renderer_in_context(wlr_renderer); const struct wlr_gles2_pixel_format *fmt = get_gles2_format_from_wl(wl_fmt); if (fmt == NULL) { @@ -266,13 +267,15 @@ static bool gles2_read_pixels(struct wlr_renderer *wlr_renderer, // Make sure any pending drawing is finished before we try to read it glFinish(); + glGetError(); // Clear the error flag + unsigned char *p = data + dst_y * stride; uint32_t pack_stride = width * fmt->bpp / 8; if (pack_stride == stride && dst_x == 0 && flags != NULL) { // Under these particular conditions, we can read the pixels with only // one glReadPixels call - glReadPixels(src_x, src_y, width, height, fmt->gl_format, - fmt->gl_type, p); + glReadPixels(src_x, renderer->viewport_height - height - src_y, + width, height, fmt->gl_format, fmt->gl_type, p); *flags = WLR_RENDERER_READ_PIXELS_Y_INVERT; } else { // Unfortunately GLES2 doesn't support GL_PACK_*, so we have to read @@ -288,7 +291,7 @@ static bool gles2_read_pixels(struct wlr_renderer *wlr_renderer, POP_GLES2_DEBUG; - return true; + return (glGetError() == GL_NO_ERROR); } static bool gles2_format_supported(struct wlr_renderer *wlr_renderer, diff --git a/types/wlr_screencopy_v1.c b/types/wlr_screencopy_v1.c index 89680a486..a600a7165 100644 --- a/types/wlr_screencopy_v1.c +++ b/types/wlr_screencopy_v1.c @@ -28,10 +28,8 @@ static void frame_handle_output_swap_buffers(struct wl_listener *listener, wl_list_remove(&frame->output_swap_buffers.link); wl_list_init(&frame->output_swap_buffers.link); - if (output->width != frame->width || output->height != frame->height) { - zwlr_screencopy_frame_v1_send_failed(frame->resource); - return; - } + int x = frame->buffer_box.x; + int y = frame->buffer_box.y; struct wl_shm_buffer *buffer = frame->buffer; assert(buffer != NULL); @@ -45,7 +43,7 @@ static void frame_handle_output_swap_buffers(struct wl_listener *listener, void *data = wl_shm_buffer_get_data(buffer); uint32_t flags = 0; bool ok = wlr_renderer_read_pixels(renderer, fmt, &flags, stride, - width, height, 0, 0, 0, 0, data); + width, height, x, y, 0, 0, data); wl_shm_buffer_end_access(buffer); if (!ok) { @@ -84,8 +82,8 @@ static void frame_handle_copy(struct wl_client *client, return; } - if (frame->width != wl_shm_buffer_get_width(buffer) || - frame->height != wl_shm_buffer_get_height(buffer)) { + if (frame->buffer_box.width != wl_shm_buffer_get_width(buffer) || + frame->buffer_box.height != wl_shm_buffer_get_height(buffer)) { wl_resource_post_error(frame->resource, ZWLR_SCREENCOPY_FRAME_V1_ERROR_INVALID_DIMENSIONS, "invalid width or height"); @@ -139,24 +137,37 @@ static struct wlr_screencopy_manager_v1 *manager_from_resource( return wl_resource_get_user_data(resource); } -static void manager_handle_capture_output(struct wl_client *client, - struct wl_resource *manager_resource, uint32_t id, - int32_t overlay_cursor, struct wl_resource *output_resource) { - struct wlr_screencopy_manager_v1 *manager = - manager_from_resource(manager_resource); - struct wlr_output *output = wlr_output_from_resource(output_resource); +static void capture_output(struct wl_client *client, + struct wlr_screencopy_manager_v1 *manager, uint32_t version, uint32_t id, + int32_t overlay_cursor, struct wlr_output *output, + const struct wlr_box *box) { + struct wlr_box buffer_box = {0}; + if (box == NULL) { + buffer_box.width = output->width; + buffer_box.height = output->height; + } else { + int ow, oh; + wlr_output_effective_resolution(output, &ow, &oh); + + buffer_box = *box; + + wlr_box_transform(&buffer_box, output->transform, ow, oh, &buffer_box); + buffer_box.x *= output->scale; + buffer_box.y *= output->scale; + buffer_box.width *= output->scale; + buffer_box.height *= output->scale; + } struct wlr_screencopy_frame_v1 *frame = calloc(1, sizeof(struct wlr_screencopy_frame_v1)); if (frame == NULL) { - wl_resource_post_no_memory(manager_resource); + wl_client_post_no_memory(client); return; } frame->manager = manager; frame->output = output; - uint32_t version = wl_resource_get_version(manager_resource); frame->resource = wl_resource_create(client, &zwlr_screencopy_frame_v1_interface, version, id); if (frame->resource == NULL) { @@ -171,11 +182,39 @@ static void manager_handle_capture_output(struct wl_client *client, wl_list_init(&frame->output_swap_buffers.link); - frame->width = output->width; - frame->height = output->height; - // TODO: don't send zero + frame->buffer_box = buffer_box; zwlr_screencopy_frame_v1_send_buffer(frame->resource, - frame->width, frame->height, 0, 0); + frame->buffer_box.width, frame->buffer_box.height, + WL_SHM_FORMAT_XRGB8888, 4 * frame->buffer_box.width); +} + +static void manager_handle_capture_output(struct wl_client *client, + struct wl_resource *manager_resource, uint32_t id, + int32_t overlay_cursor, struct wl_resource *output_resource) { + struct wlr_screencopy_manager_v1 *manager = + manager_from_resource(manager_resource); + uint32_t version = wl_resource_get_version(manager_resource); + struct wlr_output *output = wlr_output_from_resource(output_resource); + + capture_output(client, manager, version, id, overlay_cursor, output, NULL); +} + +static void manager_handle_capture_output_region(struct wl_client *client, + struct wl_resource *manager_resource, uint32_t id, + int32_t overlay_cursor, struct wl_resource *output_resource, + int32_t x, int32_t y, int32_t width, int32_t height) { + struct wlr_screencopy_manager_v1 *manager = + manager_from_resource(manager_resource); + uint32_t version = wl_resource_get_version(manager_resource); + struct wlr_output *output = wlr_output_from_resource(output_resource); + + struct wlr_box box = { + .x = x, + .y = y, + .width = width, + .height = height, + }; + capture_output(client, manager, version, id, overlay_cursor, output, &box); } static void manager_handle_destroy(struct wl_client *client, @@ -185,6 +224,7 @@ static void manager_handle_destroy(struct wl_client *client, static const struct zwlr_screencopy_manager_v1_interface manager_impl = { .capture_output = manager_handle_capture_output, + .capture_output_region = manager_handle_capture_output_region, .destroy = manager_handle_destroy, };