From 7720dde74d5abc784568d99c3f338086c128a07c Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 16 Mar 2021 11:14:38 +0100 Subject: screencopy: stop using wlr_renderer_blit_dmabuf The original motivation is the following crash: #0 0x00007f0ddeddeef5 in raise () at /usr/lib/libc.so.6 #1 0x00007f0ddedc8862 in abort () at /usr/lib/libc.so.6 #2 0x00007f0ddedc8747 in _nl_load_domain.cold () at /usr/lib/libc.so.6 #3 0x00007f0ddedd7646 in () at /usr/lib/libc.so.6 #4 0x00007f0de033f8de in gles2_get_renderer_in_context (wlr_renderer=0x612000003640) at ../subprojects/wlroots/render/gles2/renderer.c:38 #5 0x00007f0de0341675 in gles2_begin (wlr_renderer=0x612000003640, width=3840, height=2160) at ../subprojects/wlroots/render/gles2/renderer.c:186 #6 0x00007f0de033b100 in wlr_renderer_begin (r=0x612000003640, width=3840, height=2160) at ../subprojects/wlroots/render/wlr_renderer.c:56 #7 0x00007f0de03466ed in gles2_blit_dmabuf (wlr_renderer=0x612000003640, dst_attr=0x60b000150148, src_attr=0x7fff9f9bfde0) at ../subprojects/wlroots/render/gles2/renderer.c:591 #8 0x00007f0de033d717 in wlr_renderer_blit_dmabuf (r=0x612000003640, dst=0x60b000150148, src=0x7fff9f9bfde0) at ../subprojects/wlroots/render/wlr_renderer.c:210 #9 0x00007f0de04cbc3b in frame_handle_output_commit (listener=0x611000220bc8, data=0x7fff9f9c0050) at ../subprojects/wlroots/types/wlr_screencopy_v1.c:303 The GLES2 renderer assumes it'll have a wlr_buffer to render to. Instead of accomodating for the edge-case of rendering without a wlr_buffer, drop wlr_renderer_blit_dmabuf calls and instead use wlr_renderer_bind_buffer just like the rest of the wlr_renderer users. wlr_renderer_blit_dmabuf is now unused in wlroots. The upside is that the new blit_dmabuf function in screencopy is renderer-agnostic. --- types/wlr_screencopy_v1.c | 49 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/types/wlr_screencopy_v1.c b/types/wlr_screencopy_v1.c index b94e55bc..9de3172c 100644 --- a/types/wlr_screencopy_v1.c +++ b/types/wlr_screencopy_v1.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -9,6 +10,7 @@ #include #include "wlr-screencopy-unstable-v1-protocol.h" #include "render/shm_format.h" +#include "render/wlr_renderer.h" #include "util/signal.h" #define SCREENCOPY_MANAGER_VERSION 3 @@ -260,6 +262,50 @@ static void frame_handle_output_precommit(struct wl_listener *listener, frame_destroy(frame); } +static bool blit_dmabuf(struct wlr_renderer *renderer, + struct wlr_dmabuf_v1_buffer *dst_dmabuf, + struct wlr_dmabuf_attributes *src_attrs) { + if (dst_dmabuf->buffer_resource == NULL) { + return false; + } + + struct wlr_client_buffer *dst_client_buffer = + wlr_client_buffer_import(renderer, dst_dmabuf->buffer_resource); + if (dst_client_buffer == NULL) { + return false; + } + struct wlr_buffer *dst_buffer = &dst_client_buffer->base; + + struct wlr_texture *src_tex = wlr_texture_from_dmabuf(renderer, src_attrs); + if (src_tex == NULL) { + goto error_src_tex; + } + + if (!wlr_renderer_bind_buffer(renderer, dst_buffer)) { + goto error_bind_buffer; + } + + float mat[9]; + wlr_matrix_identity(mat); + + wlr_renderer_begin(renderer, dst_buffer->width, dst_buffer->height); + wlr_renderer_clear(renderer, (float[]){ 0.0, 0.0, 0.0, 0.0 }); + wlr_render_texture_with_matrix(renderer, src_tex, mat, 1.0f); + wlr_renderer_end(renderer); + + wlr_renderer_bind_buffer(renderer, NULL); + + wlr_texture_destroy(src_tex); + wlr_buffer_unlock(dst_buffer); + return true; + +error_bind_buffer: + wlr_texture_destroy(src_tex); +error_src_tex: + wlr_buffer_unlock(dst_buffer); + return false; +} + static void frame_handle_output_commit(struct wl_listener *listener, void *data) { struct wlr_screencopy_frame_v1 *frame = @@ -300,8 +346,7 @@ static void frame_handle_output_commit(struct wl_listener *listener, struct wlr_dmabuf_attributes attr = { 0 }; bool ok = wlr_output_export_dmabuf(output, &attr); - ok = ok && wlr_renderer_blit_dmabuf(renderer, - &dma_buffer->attributes, &attr); + ok = ok && blit_dmabuf(renderer, dma_buffer, &attr); uint32_t flags = dma_buffer->attributes.flags & WLR_DMABUF_ATTRIBUTES_FLAGS_Y_INVERT ? ZWLR_SCREENCOPY_FRAME_V1_FLAGS_Y_INVERT : 0; wlr_dmabuf_attributes_finish(&attr); -- cgit v1.2.3