From e989c6c0250f4004e74eae35b96b2a3fe4f3d117 Mon Sep 17 00:00:00 2001 From: Ilia Bozhinov Date: Tue, 12 Mar 2024 22:52:48 +0100 Subject: [PATCH] dmabuf: recreate buffer on format change --- src/main.cpp | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 07d4fc2..62d5106 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,7 +9,6 @@ #include #include -#include #include #include #include @@ -122,6 +121,7 @@ const zxdg_output_v1_listener xdg_output_implementation = { struct wf_buffer : public buffer_pool_buf { struct gbm_bo *bo = nullptr; + zwp_linux_buffer_params_v1 *params = nullptr; struct wl_buffer *wl_buffer = nullptr; void *data = nullptr; size_t size = 0; @@ -332,12 +332,22 @@ static void frame_handle_linux_dmabuf(void *, struct zwlr_screencopy_frame_v1 *f auto& buffer = buffers.capture(); + auto old_format = buffer.format; buffer.format = drm_to_wl_shm_format(format); buffer.drm_format = format; buffer.width = width; buffer.height = height; - if (!buffer.wl_buffer) { + if (!buffer.wl_buffer || (old_format != buffer.format)) { + if (buffer.bo) { + if (buffer.wl_buffer) { + wl_buffer_destroy(buffer.wl_buffer); + } + + zwp_linux_buffer_params_v1_destroy(buffer.params); + gbm_bo_destroy(buffer.bo); + } + const uint64_t modifier = 0; // DRM_FORMAT_MOD_LINEAR buffer.bo = gbm_bo_create_with_modifiers(gbm_device, buffer.width, buffer.height, format, &modifier, 1); @@ -355,18 +365,17 @@ static void frame_handle_linux_dmabuf(void *, struct zwlr_screencopy_frame_v1 *f buffer.stride = gbm_bo_get_stride(buffer.bo); - struct zwp_linux_buffer_params_v1 *params = - zwp_linux_dmabuf_v1_create_params(dmabuf); + buffer.params = zwp_linux_dmabuf_v1_create_params(dmabuf); uint64_t mod = gbm_bo_get_modifier(buffer.bo); - zwp_linux_buffer_params_v1_add(params, + zwp_linux_buffer_params_v1_add(buffer.params, gbm_bo_get_fd(buffer.bo), 0, gbm_bo_get_offset(buffer.bo, 0), gbm_bo_get_stride(buffer.bo), mod >> 32, mod & 0xffffffff); - zwp_linux_buffer_params_v1_add_listener(params, ¶ms_listener, frame); - zwp_linux_buffer_params_v1_create(params, buffer.width, + zwp_linux_buffer_params_v1_add_listener(buffer.params, ¶ms_listener, frame); + zwp_linux_buffer_params_v1_create(buffer.params, buffer.width, buffer.height, format, 0); } else { if (use_damage) {