screencopy: add capture_output_region support
This commit is contained in:
parent
2b9cbaddf3
commit
bf7560b7cd
|
@ -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;
|
||||
|
||||
|
|
|
@ -43,16 +43,39 @@
|
|||
source.
|
||||
</description>
|
||||
|
||||
<enum name="error">
|
||||
<entry name="invalid_output_region" value="0"
|
||||
summary="tried to capture an invalid output region"/>
|
||||
</enum>
|
||||
|
||||
<request name="capture_output">
|
||||
<description summary="start capturing">
|
||||
Capture the next frame of a an entire output.
|
||||
<description summary="capture an output">
|
||||
Capture the next frame of an entire output.
|
||||
</description>
|
||||
<arg name="frame" type="new_id" interface="zwlr_screencopy_frame_v1"/>
|
||||
<arg name="overlay_cursor" type="int"
|
||||
summary="include custom client hardware cursor on top of the frame"/>
|
||||
summary="composite cursor onto the frame"/>
|
||||
<arg name="output" type="object" interface="wl_output"/>
|
||||
</request>
|
||||
|
||||
<request name="capture_output_region">
|
||||
<description summary="capture an output's region">
|
||||
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.
|
||||
</description>
|
||||
<arg name="frame" type="new_id" interface="zwlr_screencopy_frame_v1"/>
|
||||
<arg name="overlay_cursor" type="int"
|
||||
summary="composite cursor onto the frame"/>
|
||||
<arg name="output" type="object" interface="wl_output"/>
|
||||
<arg name="x" type="int"/>
|
||||
<arg name="y" type="int"/>
|
||||
<arg name="width" type="int"/>
|
||||
<arg name="height" type="int"/>
|
||||
</request>
|
||||
|
||||
<request name="destroy" type="destructor">
|
||||
<description summary="destroy the manager">
|
||||
All objects created by the manager will still remain valid, until their
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue