From cca2436ba535d17c49b7d7ba75ebfa6a18ec9209 Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Sat, 7 Sep 2024 00:27:37 +0200 Subject: Improve frame callback and commit handling The few places that required a surface commit relied on render_frame and render_frame_background to form it form them after they had set up frame callback events. This would fail if render_frame ran out of buffers and exited early, as the caller would still wait indefinitely on the frame callback. swaylock would quite consistently run out of buffers when rendering immediately after a configure event, such as when the keypress causing render also caused outputs to be enabled from idle. Restructure the render and commit handling slightly so that the whole frame callback and commit setup is handled by the render code, which now has a single render entrypoint. This both avoids stalls from lacking commits, but also fixes the case the configure path to respect frame callbacks so we do not run out of buffers in the first place. --- render.c | 51 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 10 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index e2d76f7..1aa8ae5 100644 --- a/render.c +++ b/render.c @@ -33,7 +33,23 @@ static void set_color_for_state(cairo_t *cairo, struct swaylock_state *state, } } -void render_frame_background(struct swaylock_surface *surface) { +static void surface_frame_handle_done(void *data, struct wl_callback *callback, + uint32_t time) { + struct swaylock_surface *surface = data; + + wl_callback_destroy(callback); + surface->frame = NULL; + + render(surface); +} + +static const struct wl_callback_listener surface_frame_listener = { + .done = surface_frame_handle_done, +}; + +static bool render_frame(struct swaylock_surface *surface); + +void render(struct swaylock_surface *surface) { struct swaylock_state *state = surface->state; int buffer_width = surface->width * surface->scale; @@ -42,11 +58,17 @@ void render_frame_background(struct swaylock_surface *surface) { return; // not yet configured } - wl_surface_set_buffer_scale(surface->surface, surface->scale); + if (!surface->dirty || surface->frame) { + // Nothing to do or frame already pending + return; + } + + bool need_destroy = false; + struct pool_buffer buffer; if (buffer_width != surface->last_buffer_width || buffer_height != surface->last_buffer_height) { - struct pool_buffer buffer; + need_destroy = true; if (!create_buffer(state->shm, &buffer, buffer_width, buffer_height, WL_SHM_FORMAT_ARGB8888)) { swaylock_log(LOG_ERROR, @@ -69,15 +91,23 @@ void render_frame_background(struct swaylock_surface *surface) { cairo_restore(cairo); cairo_identity_matrix(cairo); + wl_surface_set_buffer_scale(surface->surface, surface->scale); wl_surface_attach(surface->surface, buffer.buffer, 0, 0); wl_surface_damage_buffer(surface->surface, 0, 0, INT32_MAX, INT32_MAX); - wl_surface_commit(surface->surface); - destroy_buffer(&buffer); + need_destroy = true; surface->last_buffer_width = buffer_width; surface->last_buffer_height = buffer_height; - } else { - wl_surface_commit(surface->surface); + } + + render_frame(surface); + surface->dirty = false; + surface->frame = wl_surface_frame(surface->surface); + wl_callback_add_listener(surface->frame, &surface_frame_listener, surface); + wl_surface_commit(surface->surface); + + if (need_destroy) { + destroy_buffer(&buffer); } } @@ -99,7 +129,7 @@ static void configure_font_drawing(cairo_t *cairo, struct swaylock_state *state, cairo_font_options_destroy(fo); } -void render_frame(struct swaylock_surface *surface) { +static bool render_frame(struct swaylock_surface *surface) { struct swaylock_state *state = surface->state; // First, compute the text that will be drawn, if any, since this @@ -210,7 +240,8 @@ void render_frame(struct swaylock_surface *surface) { struct pool_buffer *buffer = get_next_buffer(state->shm, surface->indicator_buffers, buffer_width, buffer_height); if (buffer == NULL) { - return; + swaylock_log(LOG_ERROR, "No buffer"); + return false; } // Render the buffer @@ -352,5 +383,5 @@ void render_frame(struct swaylock_surface *surface) { wl_surface_damage_buffer(surface->child, 0, 0, INT32_MAX, INT32_MAX); wl_surface_commit(surface->child); - wl_surface_commit(surface->surface); + return true; } -- cgit v1.2.3