From 49464d233e230992e1e2ee8c40dbdd00a6646ad6 Mon Sep 17 00:00:00 2001 From: Manuel Stoeckl Date: Fri, 8 Nov 2024 21:45:51 -0500 Subject: [PATCH] swaynag: coalesce surface redraw operations This avoids exhausting the buffer pool when multiple events that would trigger redraws occur rapidly (for example: scrolling, resizing output). --- include/swaynag/swaynag.h | 1 + swaynag/render.c | 1 + swaynag/swaynag.c | 22 ++++++++++++---------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/include/swaynag/swaynag.h b/include/swaynag/swaynag.h index fb9e9c21..8e80b570 100644 --- a/include/swaynag/swaynag.h +++ b/include/swaynag/swaynag.h @@ -90,6 +90,7 @@ struct swaynag { struct wp_cursor_shape_manager_v1 *cursor_shape_manager; struct wl_surface *surface; + bool needs_redraw; uint32_t width; uint32_t height; int32_t scale; diff --git a/swaynag/render.c b/swaynag/render.c index bfa4b0c1..dcc0d06a 100644 --- a/swaynag/render.c +++ b/swaynag/render.c @@ -250,6 +250,7 @@ void render_frame(struct swaynag *swaynag) { if (!swaynag->run_display) { return; } + swaynag->needs_redraw = false; cairo_surface_t *recorder = cairo_recording_surface_create( CAIRO_CONTENT_COLOR_ALPHA, NULL); diff --git a/swaynag/swaynag.c b/swaynag/swaynag.c index da32eeb7..ac48cdff 100644 --- a/swaynag/swaynag.c +++ b/swaynag/swaynag.c @@ -47,7 +47,7 @@ static void swaynag_button_execute(struct swaynag *swaynag, swaynag->run_display = false; } else if (button->type == SWAYNAG_ACTION_EXPAND) { swaynag->details.visible = !swaynag->details.visible; - render_frame(swaynag); + swaynag->needs_redraw = true; } else { pid_t pid = fork(); if (pid < 0) { @@ -98,7 +98,7 @@ static void layer_surface_configure(void *data, swaynag->width = width; swaynag->height = height; zwlr_layer_surface_v1_ack_configure(surface, serial); - render_frame(swaynag); + swaynag->needs_redraw = true; } static void layer_surface_closed(void *data, @@ -122,7 +122,7 @@ static void surface_enter(void *data, struct wl_surface *surface, swaynag_output->name); swaynag->output = swaynag_output; swaynag->scale = swaynag->output->scale; - render_frame(swaynag); + swaynag->needs_redraw = true; break; } }; @@ -244,7 +244,7 @@ static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, && y < button_up.y + button_up.height && swaynag->details.offset > 0) { swaynag->details.offset--; - render_frame(swaynag); + swaynag->needs_redraw = true; return; } @@ -257,7 +257,7 @@ static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, && y < button_down.y + button_down.height && swaynag->details.offset < bot) { swaynag->details.offset++; - render_frame(swaynag); + swaynag->needs_redraw = true; return; } } @@ -284,7 +284,7 @@ static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, swaynag->details.offset++; } - render_frame(swaynag); + swaynag->needs_redraw = true; } static const struct wl_pointer_listener pointer_listener = { @@ -327,7 +327,7 @@ static void output_scale(void *data, struct wl_output *output, if (!swaynag_output->swaynag->cursor_shape_manager) { update_all_cursors(swaynag_output->swaynag); } - render_frame(swaynag_output->swaynag); + swaynag_output->swaynag->needs_redraw = true; } } @@ -503,9 +503,11 @@ void swaynag_setup(struct swaynag *swaynag) { void swaynag_run(struct swaynag *swaynag) { swaynag->run_display = true; render_frame(swaynag); - while (swaynag->run_display - && wl_display_dispatch(swaynag->display) != -1) { - // This is intentionally left blank + while (wl_display_dispatch(swaynag->display) != -1 + && swaynag->run_display) { + if (swaynag->needs_redraw) { + render_frame(swaynag); + } } }