From 02dfeea54c4eac2a96c0d8d614d5c16d96358ad0 Mon Sep 17 00:00:00 2001 From: Ian Fan Date: Fri, 28 Sep 2018 16:17:15 +0100 Subject: [PATCH] swaybar: synchronize rendering to output frames --- include/swaybar/bar.h | 2 ++ include/swaybar/render.h | 4 +--- swaybar/bar.c | 41 +++++++++++++++++++++------------------- swaybar/render.c | 24 ++++++++++++++++++++++- 4 files changed, 48 insertions(+), 23 deletions(-) diff --git a/include/swaybar/bar.h b/include/swaybar/bar.h index 20992014..de234111 100644 --- a/include/swaybar/bar.h +++ b/include/swaybar/bar.h @@ -83,6 +83,8 @@ struct swaybar_output { enum wl_output_subpixel subpixel; struct pool_buffer buffers[2]; struct pool_buffer *current_buffer; + bool dirty; + bool frame_scheduled; }; struct swaybar_workspace { diff --git a/include/swaybar/render.h b/include/swaybar/render.h index 071e2298..ebdc69e4 100644 --- a/include/swaybar/render.h +++ b/include/swaybar/render.h @@ -1,10 +1,8 @@ #ifndef _SWAYBAR_RENDER_H #define _SWAYBAR_RENDER_H -struct swaybar; struct swaybar_output; -struct swaybar_config; -void render_frame(struct swaybar *bar, struct swaybar_output *output); +void render_frame(struct swaybar_output *output); #endif diff --git a/swaybar/bar.c b/swaybar/bar.c index 15e81976..388c24c4 100644 --- a/swaybar/bar.c +++ b/swaybar/bar.c @@ -72,6 +72,16 @@ static void swaybar_output_free(struct swaybar_output *output) { free(output); } +static void set_output_dirty(struct swaybar_output *output) { + if (output->frame_scheduled) { + output->dirty = true; + return; + } + if (output->surface) { + render_frame(output); + } +} + static void layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *surface, uint32_t serial, uint32_t width, uint32_t height) { @@ -79,7 +89,7 @@ static void layer_surface_configure(void *data, output->width = width; output->height = height; zwlr_layer_surface_v1_ack_configure(surface, serial); - render_frame(output->bar, output); + set_output_dirty(output); } static void layer_surface_closed(void *_output, @@ -325,27 +335,22 @@ static void output_geometry(void *data, struct wl_output *wl_output, int32_t x, const char *make, const char *model, int32_t transform) { struct swaybar_output *output = data; output->subpixel = subpixel; - if (output->surface) { - render_frame(output->bar, output); - } } -static void output_mode(void *data, struct wl_output *output, uint32_t flags, +static void output_mode(void *data, struct wl_output *wl_output, uint32_t flags, int32_t width, int32_t height, int32_t refresh) { // Who cares } -static void output_done(void *data, struct wl_output *output) { - // Who cares +static void output_done(void *data, struct wl_output *wl_output) { + struct swaybar_output *output = data; + set_output_dirty(output); } static void output_scale(void *data, struct wl_output *wl_output, int32_t factor) { struct swaybar_output *output = data; output->scale = factor; - if (output->surface) { - render_frame(output->bar, output); - } } struct wl_output_listener output_listener = { @@ -381,7 +386,7 @@ static void xdg_output_handle_done(void *data, wl_list_insert(&bar->outputs, &output->link); add_layer_surface(output); - render_frame(bar, output); + set_output_dirty(output); } } @@ -470,12 +475,10 @@ static const struct wl_registry_listener registry_listener = { .global_remove = handle_global_remove, }; -static void render_all_frames(struct swaybar *bar) { +static void set_bar_dirty(struct swaybar *bar) { struct swaybar_output *output; wl_list_for_each(output, &bar->outputs, link) { - if (output->surface != NULL) { - render_frame(bar, output); - } + set_output_dirty(output); } } @@ -528,7 +531,7 @@ bool bar_setup(struct swaybar *bar, assert(pointer->cursor_surface); ipc_get_workspaces(bar); - render_all_frames(bar); + set_bar_dirty(bar); return true; } @@ -543,7 +546,7 @@ static void display_in(int fd, short mask, void *data) { static void ipc_in(int fd, short mask, void *data) { struct swaybar *bar = data; if (handle_ipc_readable(bar)) { - render_all_frames(bar); + set_bar_dirty(bar); } } @@ -551,10 +554,10 @@ static void status_in(int fd, short mask, void *data) { struct swaybar *bar = data; if (mask & (POLLHUP | POLLERR)) { status_error(bar->status, "[error reading from status command]"); - render_all_frames(bar); + set_bar_dirty(bar); remove_event(fd); } else if (status_handle_readable(bar->status)) { - render_all_frames(bar); + set_bar_dirty(bar); } } diff --git a/swaybar/render.c b/swaybar/render.c index 90e5bac7..dc31a5ea 100644 --- a/swaybar/render.c +++ b/swaybar/render.c @@ -473,7 +473,22 @@ static uint32_t render_to_cairo(cairo_t *cairo, struct swaybar_output *output) { return max_height > output->height ? max_height : output->height; } -void render_frame(struct swaybar *bar, struct swaybar_output *output) { +static void output_frame_handle_done(void *data, struct wl_callback *callback, + uint32_t time) { + wl_callback_destroy(callback); + struct swaybar_output *output = data; + output->frame_scheduled = false; + if (output->dirty) { + render_frame(output); + output->dirty = false; + } +} + +static const struct wl_callback_listener output_frame_listener = { + .done = output_frame_handle_done +}; + +void render_frame(struct swaybar_output *output) { assert(output->surface != NULL); struct swaybar_hotspot *hotspot, *tmp; @@ -518,6 +533,8 @@ void render_frame(struct swaybar *bar, struct swaybar_output *output) { output->width * output->scale, output->height * output->scale); if (!output->current_buffer) { + cairo_surface_destroy(recorder); + cairo_destroy(cairo); return; } cairo_t *shm = output->current_buffer->cairo; @@ -535,6 +552,11 @@ void render_frame(struct swaybar *bar, struct swaybar_output *output) { output->current_buffer->buffer, 0, 0); wl_surface_damage(output->surface, 0, 0, output->width, output->height); + + struct wl_callback *frame_callback = wl_surface_frame(output->surface); + wl_callback_add_listener(frame_callback, &output_frame_listener, output); + output->frame_scheduled = true; + wl_surface_commit(output->surface); } cairo_surface_destroy(recorder);