From 1b0bcf8f768e50726324ca5b29ff78d07581cffe Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Thu, 18 Apr 2024 17:56:50 +0200 Subject: [PATCH] ext-image-capture-source-v1: add output cursor source --- types/ext_image_capture_source_v1/output.c | 154 +++++++++++++++++++++ 1 file changed, 154 insertions(+) diff --git a/types/ext_image_capture_source_v1/output.c b/types/ext_image_capture_source_v1/output.c index f547ae31..24e77d21 100644 --- a/types/ext_image_capture_source_v1/output.c +++ b/types/ext_image_capture_source_v1/output.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -11,6 +12,17 @@ #define OUTPUT_IMAGE_SOURCE_MANAGER_V1_VERSION 1 +struct output_cursor_source { + struct wlr_ext_image_capture_source_v1_cursor base; + + struct wlr_output *output; + struct wlr_buffer *prev_buffer; + bool initialized; + + struct wl_listener output_commit; + struct wl_listener prev_buffer_release; +}; + struct wlr_ext_output_image_capture_source_v1 { struct wlr_ext_image_capture_source_v1 base; struct wlr_addon addon; @@ -18,6 +30,8 @@ struct wlr_ext_output_image_capture_source_v1 { struct wlr_output *output; struct wl_listener output_commit; + + struct output_cursor_source cursor; }; struct wlr_ext_output_image_capture_source_v1_frame_event { @@ -45,9 +59,17 @@ static void output_source_copy_frame(struct wlr_ext_image_capture_source_v1 *bas } } +static struct wlr_ext_image_capture_source_v1_cursor *output_source_get_pointer_cursor( + struct wlr_ext_image_capture_source_v1 *base, struct wlr_seat *seat) { + // TODO: handle seat + struct wlr_ext_output_image_capture_source_v1 *source = wl_container_of(base, source, base); + return &source->cursor.base; +} + static const struct wlr_ext_image_capture_source_v1_interface output_source_impl = { .schedule_frame = output_source_schedule_frame, .copy_frame = output_source_copy_frame, + .get_pointer_cursor = output_source_get_pointer_cursor, }; static void source_update_buffer_constraints(struct wlr_ext_output_image_capture_source_v1 *source) { @@ -96,9 +118,14 @@ static void source_handle_output_commit(struct wl_listener *listener, } } +static void output_cursor_source_init(struct output_cursor_source *cursor_source, + struct wlr_output *output); +static void output_cursor_source_finish(struct output_cursor_source *cursor_source); + static void output_addon_destroy(struct wlr_addon *addon) { struct wlr_ext_output_image_capture_source_v1 *source = wl_container_of(addon, source, addon); wlr_ext_image_capture_source_v1_finish(&source->base); + output_cursor_source_finish(&source->cursor); wl_list_remove(&source->output_commit.link); wlr_addon_finish(&source->addon); free(source); @@ -137,6 +164,8 @@ static void output_manager_handle_create_source(struct wl_client *client, wl_signal_add(&output->events.commit, &source->output_commit); source_update_buffer_constraints(source); + + output_cursor_source_init(&source->cursor, output); } if (!wlr_ext_image_capture_source_v1_create_resource(&source->base, client, new_id)) { @@ -196,3 +225,128 @@ struct wlr_ext_output_image_capture_source_manager_v1 *wlr_ext_output_image_capt return manager; } + +static void output_cursor_source_schedule_frame(struct wlr_ext_image_capture_source_v1 *base) { + struct output_cursor_source *cursor_source = wl_container_of(base, cursor_source, base); + wlr_output_update_needs_frame(cursor_source->output); +} + +static void output_cursor_source_copy_frame(struct wlr_ext_image_capture_source_v1 *base, + struct wlr_ext_image_copy_capture_frame_v1 *frame, + struct wlr_ext_image_capture_source_v1_frame_event *base_event) { + struct output_cursor_source *cursor_source = wl_container_of(base, cursor_source, base); + + struct wlr_buffer *src_buffer = cursor_source->output->cursor_front_buffer; + if (src_buffer == NULL) { + wlr_ext_image_copy_capture_frame_v1_fail(frame, EXT_IMAGE_COPY_CAPTURE_FRAME_V1_FAILURE_REASON_STOPPED); + return; + } + + if (!wlr_ext_image_copy_capture_frame_v1_copy_buffer(frame, + src_buffer, cursor_source->output->renderer)) { + return; + } + + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + + wlr_ext_image_copy_capture_frame_v1_ready(frame, WL_OUTPUT_TRANSFORM_NORMAL, &now); +} + +static const struct wlr_ext_image_capture_source_v1_interface output_cursor_source_impl = { + .schedule_frame = output_cursor_source_schedule_frame, + .copy_frame = output_cursor_source_copy_frame, +}; + +static void output_cursor_source_update(struct output_cursor_source *cursor_source) { + struct wlr_output *output = cursor_source->output; + + if (output->cursor_swapchain != NULL && !cursor_source->initialized) { + wlr_ext_image_capture_source_v1_set_constraints_from_swapchain(&cursor_source->base.base, + output->cursor_swapchain, output->renderer); + cursor_source->initialized = true; + } + + struct wlr_output_cursor *output_cursor = output->hardware_cursor; + if (output_cursor == NULL || !output_cursor->visible) { + cursor_source->base.entered = false; + wl_signal_emit_mutable(&cursor_source->base.events.update, NULL); + return; + } + + if (output->cursor_swapchain != NULL && + ((int)cursor_source->base.base.width != output->cursor_swapchain->width || + (int)cursor_source->base.base.height != output->cursor_swapchain->height)) { + cursor_source->base.base.width = output->cursor_swapchain->width; + cursor_source->base.base.height = output->cursor_swapchain->height; + wl_signal_emit_mutable(&cursor_source->base.base.events.constraints_update, NULL); + } + + cursor_source->base.entered = true; + cursor_source->base.x = round(output_cursor->x); + cursor_source->base.y = round(output_cursor->y); + cursor_source->base.hotspot.x = output_cursor->hotspot_x; + cursor_source->base.hotspot.y = output_cursor->hotspot_y; + wl_signal_emit_mutable(&cursor_source->base.events.update, NULL); +} + +static void output_cursor_source_handle_prev_buffer_release(struct wl_listener *listener, + void *data) { + struct output_cursor_source *cursor_source = wl_container_of(listener, cursor_source, prev_buffer_release); + wl_list_remove(&cursor_source->prev_buffer_release.link); + wl_list_init(&cursor_source->prev_buffer_release.link); + cursor_source->prev_buffer = NULL; +} + +static void output_cursor_source_handle_output_commit(struct wl_listener *listener, + void *data) { + struct output_cursor_source *cursor_source = wl_container_of(listener, cursor_source, output_commit); + struct wlr_output_event_commit *event = data; + + output_cursor_source_update(cursor_source); + + struct wlr_buffer *buffer = cursor_source->output->cursor_front_buffer; + if (buffer != NULL && buffer != cursor_source->prev_buffer) { + pixman_region32_t full_damage; + pixman_region32_init_rect(&full_damage, 0, 0, buffer->width, buffer->height); + + struct wlr_ext_output_image_capture_source_v1_frame_event frame_event = { + .base = { + .damage = &full_damage, + }, + .buffer = buffer, + .when = event->when, // TODO: predict next presentation time instead + }; + wl_signal_emit_mutable(&cursor_source->base.base.events.frame, &frame_event); + + pixman_region32_fini(&full_damage); + + assert(buffer->n_locks > 0); + cursor_source->prev_buffer = buffer; + wl_list_remove(&cursor_source->prev_buffer_release.link); + cursor_source->prev_buffer_release.notify = output_cursor_source_handle_prev_buffer_release; + wl_signal_add(&buffer->events.release, &cursor_source->prev_buffer_release); + } +} + +static void output_cursor_source_init(struct output_cursor_source *cursor_source, + struct wlr_output *output) { + wlr_ext_image_capture_source_v1_cursor_init(&cursor_source->base, &output_cursor_source_impl); + + // Caller is responsible for destroying the output cursor source when the + // output is destroyed + cursor_source->output = output; + + cursor_source->output_commit.notify = output_cursor_source_handle_output_commit; + wl_signal_add(&output->events.commit, &cursor_source->output_commit); + + wl_list_init(&cursor_source->prev_buffer_release.link); + + output_cursor_source_update(cursor_source); +} + +static void output_cursor_source_finish(struct output_cursor_source *cursor_source) { + wlr_ext_image_capture_source_v1_cursor_finish(&cursor_source->base); + wl_list_remove(&cursor_source->output_commit.link); + wl_list_remove(&cursor_source->prev_buffer_release.link); +}