From cded7797f74616a1355cdbdab94630ea5da6ada2 Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Mon, 7 Apr 2025 18:08:01 +0000 Subject: [PATCH 1/3] export-dmabuf: Implement release semantics This implements release semantics so that clients can hold onto buffers while encoding. --- include/wlr/types/wlr_export_dmabuf_v1.h | 1 + protocol/wlr-export-dmabuf-unstable-v1.xml | 12 ++++++++---- types/wlr_export_dmabuf_v1.c | 11 +++++++++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/include/wlr/types/wlr_export_dmabuf_v1.h b/include/wlr/types/wlr_export_dmabuf_v1.h index 2c5a24525..5e5c21cd2 100644 --- a/include/wlr/types/wlr_export_dmabuf_v1.h +++ b/include/wlr/types/wlr_export_dmabuf_v1.h @@ -32,6 +32,7 @@ struct wlr_export_dmabuf_frame_v1 { struct wl_list link; // wlr_export_dmabuf_manager_v1.frames struct wlr_output *output; + struct wlr_buffer *buffer; bool cursor_locked; diff --git a/protocol/wlr-export-dmabuf-unstable-v1.xml b/protocol/wlr-export-dmabuf-unstable-v1.xml index 751f7efbf..cdae52432 100644 --- a/protocol/wlr-export-dmabuf-unstable-v1.xml +++ b/protocol/wlr-export-dmabuf-unstable-v1.xml @@ -36,14 +36,14 @@ interface version number is reset. - + This object is a manager with which to start capturing from sources. - Capture the next frame of a an entire output. + Capture the next frame of an entire output. - + This object represents a single DMA-BUF frame. @@ -136,7 +136,7 @@ + summary="index of the plane the data in the object applies to"/> @@ -195,6 +195,10 @@ Unreferences the frame. This request must be called as soon as its no longer used. + Starting from version 2, this indicates that the client has finished + processing the frame. The client must not access the underlying buffer + after destroying the zwlr_export_dmabuf_frame_v1 object. + It can be called at any time by the client. The client will still have to close any FDs it has been given. diff --git a/types/wlr_export_dmabuf_v1.c b/types/wlr_export_dmabuf_v1.c index 4e629dcfd..04ea5b8d6 100644 --- a/types/wlr_export_dmabuf_v1.c +++ b/types/wlr_export_dmabuf_v1.c @@ -7,7 +7,7 @@ #include #include "wlr-export-dmabuf-unstable-v1-protocol.h" -#define EXPORT_DMABUF_MANAGER_VERSION 1 +#define EXPORT_DMABUF_MANAGER_VERSION 2 static const struct zwlr_export_dmabuf_frame_v1_interface frame_impl; @@ -32,6 +32,7 @@ static void frame_destroy(struct wlr_export_dmabuf_frame_v1 *frame) { if (frame == NULL) { return; } + wlr_buffer_unlock(frame->buffer); if (frame->output != NULL) { wlr_output_lock_attach_render(frame->output, false); if (frame->cursor_locked) { @@ -90,7 +91,13 @@ static void frame_output_handle_commit(struct wl_listener *listener, uint32_t tv_sec_lo = tv_sec & 0xFFFFFFFF; zwlr_export_dmabuf_frame_v1_send_ready(frame->resource, tv_sec_hi, tv_sec_lo, event->when.tv_nsec); - frame_destroy(frame); + + uint32_t version = wl_resource_get_version(frame->resource); + if (version < 2) { + frame_destroy(frame); + } else { + frame->buffer = wlr_buffer_lock(event->state->buffer); + } } static void frame_output_handle_destroy(struct wl_listener *listener, void *data) { From 1146650d713a80eeacc12449c97bfb1257128828 Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Mon, 16 Jun 2025 17:44:02 +0000 Subject: [PATCH 2/3] render: swapchain: Add method to count free slots --- include/wlr/render/swapchain.h | 4 ++++ render/swapchain.c | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/include/wlr/render/swapchain.h b/include/wlr/render/swapchain.h index 4ca1ac590..e09158aae 100644 --- a/include/wlr/render/swapchain.h +++ b/include/wlr/render/swapchain.h @@ -46,5 +46,9 @@ struct wlr_buffer *wlr_swapchain_acquire(struct wlr_swapchain *swapchain); */ bool wlr_swapchain_has_buffer(struct wlr_swapchain *swapchain, struct wlr_buffer *buffer); +/** + * Count how many free slots there are left in the swapchain. + */ +int wlr_swapchain_count_free_slots(const struct wlr_swapchain *swapchain); #endif diff --git a/render/swapchain.c b/render/swapchain.c index 24d6f1a87..f42507ddf 100644 --- a/render/swapchain.c +++ b/render/swapchain.c @@ -120,3 +120,12 @@ bool wlr_swapchain_has_buffer(struct wlr_swapchain *swapchain, } return false; } + +int wlr_swapchain_count_free_slots(const struct wlr_swapchain *swapchain) +{ + int count = 0; + for (size_t i = 0; i < WLR_SWAPCHAIN_CAP; i++) { + count += !swapchain->slots[i].acquired; + } + return count; +} From 31fd68b46bd4e257f22ec3a1ddc8415146a4f289 Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Mon, 16 Jun 2025 18:56:50 +0000 Subject: [PATCH 3/3] export-dmabuf: Cancel capture if there are too few free slots We don't want this to stall compositing, so there must be at least 2 free slots available at all times. --- types/wlr_export_dmabuf_v1.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/types/wlr_export_dmabuf_v1.c b/types/wlr_export_dmabuf_v1.c index 04ea5b8d6..65707d459 100644 --- a/types/wlr_export_dmabuf_v1.c +++ b/types/wlr_export_dmabuf_v1.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include "wlr-export-dmabuf-unstable-v1-protocol.h" @@ -65,8 +66,11 @@ static void frame_output_handle_commit(struct wl_listener *listener, wl_list_remove(&frame->output_commit.link); wl_list_init(&frame->output_commit.link); + uint32_t version = wl_resource_get_version(frame->resource); + struct wlr_dmabuf_attributes attribs = {0}; - if (!wlr_buffer_get_dmabuf(event->state->buffer, &attribs)) { + if (!wlr_buffer_get_dmabuf(event->state->buffer, &attribs) || + (version >= 2 && wlr_swapchain_count_free_slots(frame->output->swapchain) < 2)) { zwlr_export_dmabuf_frame_v1_send_cancel(frame->resource, ZWLR_EXPORT_DMABUF_FRAME_V1_CANCEL_REASON_TEMPORARY); frame_destroy(frame); @@ -92,7 +96,6 @@ static void frame_output_handle_commit(struct wl_listener *listener, zwlr_export_dmabuf_frame_v1_send_ready(frame->resource, tv_sec_hi, tv_sec_lo, event->when.tv_nsec); - uint32_t version = wl_resource_get_version(frame->resource); if (version < 2) { frame_destroy(frame); } else {