From 399220f14bc60581490936d9f1a0fd353bfc9ef5 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 18 Nov 2015 08:22:37 -0500 Subject: [PATCH] Fix up wayland client implementation Now it receives frame callbacks and renders properly, and is double buffered and such. --- include/client/buffer.h | 8 +++ include/{ => client}/client.h | 15 +++-- swaybg/main.c | 33 +++------- wayland/buffers.c | 116 ++++++++++++++++++++++++++++++++++ wayland/client.c | 115 ++++++++------------------------- 5 files changed, 167 insertions(+), 120 deletions(-) create mode 100644 include/client/buffer.h rename include/{ => client}/client.h (78%) create mode 100644 wayland/buffers.c diff --git a/include/client/buffer.h b/include/client/buffer.h new file mode 100644 index 000000000..aa8d68a11 --- /dev/null +++ b/include/client/buffer.h @@ -0,0 +1,8 @@ +#ifndef _BUFFER_H +#define _BUFFER_H + +#include "client/client.h" + +struct buffer *get_next_buffer(struct client_state *state); + +#endif diff --git a/include/client.h b/include/client/client.h similarity index 78% rename from include/client.h rename to include/client/client.h index f10e6b1aa..bac2c1a6f 100644 --- a/include/client.h +++ b/include/client/client.h @@ -10,13 +10,16 @@ struct output_state { struct wl_output *output; uint32_t flags; - int width, height; + uint32_t width, height; }; struct buffer { struct wl_buffer *buffer; - struct wl_shm_pool *pool; + cairo_surface_t *surface; + cairo_t *cairo; + PangoContext *pango; uint32_t width, height; + bool busy; }; struct client_state { @@ -26,20 +29,18 @@ struct client_state { struct wl_seat *seat; struct wl_shell *shell; struct wl_shm *shm; + struct buffer buffers[2]; struct buffer *buffer; struct wl_surface *surface; struct wl_shell_surface *shell_surface; struct wl_callback *frame_cb; - bool busy; + uint32_t width, height; cairo_t *cairo; - cairo_surface_t *cairo_surface; - PangoContext *pango; list_t *outputs; }; -struct client_state *client_setup(void); +struct client_state *client_setup(uint32_t width, uint32_t height); void client_teardown(struct client_state *state); -struct buffer *create_memory_pool(struct client_state *state, int32_t width, int32_t height, uint32_t format); int client_prerender(struct client_state *state); int client_render(struct client_state *state); diff --git a/swaybg/main.c b/swaybg/main.c index db5af375c..e8f400c08 100644 --- a/swaybg/main.c +++ b/swaybg/main.c @@ -2,7 +2,7 @@ #include #include #include -#include "client.h" +#include "client/client.h" #include "log.h" struct client_state *state; @@ -14,36 +14,23 @@ void sway_terminate(void) { int main(int argc, char **argv) { init_log(L_INFO); - if (!(state = client_setup())) { + if (!(state = client_setup(100, 100))) { return -1; } - uint8_t r = 0, g = 0, b = 0; + uint8_t r = 100, g = 100, b = 100; - long last_ms = 0; - int rs; do { - struct timespec spec; - clock_gettime(CLOCK_MONOTONIC, &spec); - long ms = round(spec.tv_nsec / 1.0e6); + if (client_prerender(state)) { + cairo_set_source_rgb(state->cairo, r / 256.0, g / 256.0, b / 256.0); + cairo_rectangle(state->cairo, 0, 0, state->width, state->height); + cairo_fill(state->cairo); - cairo_set_source_rgb(state->cairo, r, g, b); - cairo_rectangle(state->cairo, 0, 0, 100, 100); - cairo_fill(state->cairo); + client_render(state); - rs = client_render(state); - - if (ms - last_ms > 100) { - r++; - if (r == 0) { - g++; - if (g == 0) { - b++; - } - } - ms = last_ms; + r++; if (r == 0) { g++; if (g == 0) { b++; } } } - } while (rs); + } while (wl_display_dispatch(state->display) != -1); client_teardown(state); return 0; diff --git a/wayland/buffers.c b/wayland/buffers.c new file mode 100644 index 000000000..13193423e --- /dev/null +++ b/wayland/buffers.c @@ -0,0 +1,116 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "client/buffer.h" +#include "list.h" +#include "log.h" + +static int create_pool_file(size_t size) { + static const char template[] = "/sway-client-XXXXXX"; + const char *path = getenv("XDG_RUNTIME_DIR"); + if (!path) { + return -1; + } + + int ts = (path[strlen(path) - 1] == '/'); + + char *name = malloc( + strlen(template) + + strlen(path) + + (ts ? 1 : 0) + 1); + sprintf(name, "%s%s%s", path, ts ? "" : "/", template); + + int fd = mkstemp(name); + free(name); + + if (fd < 0) { + return -1; + } + + if (ftruncate(fd, size) < 0) { + close(fd); + return -1; + } + + return fd; +} + +static void buffer_release(void *data, struct wl_buffer *wl_buffer) { + struct buffer *buffer = data; + buffer->busy = false; +} + +static const struct wl_buffer_listener buffer_listener = { + .release = buffer_release +}; + +static struct buffer *create_buffer(struct client_state *state, struct buffer *buf, + int32_t width, int32_t height, uint32_t format) { + + uint32_t stride = width * 4; + uint32_t size = stride * height; + + int fd = create_pool_file(size); + void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + struct wl_shm_pool *pool = wl_shm_create_pool(state->shm, fd, size); + buf->buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, format); + wl_shm_pool_destroy(pool); + close(fd); + fd = -1; + + buf->surface = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, width, height, stride); + buf->cairo = cairo_create(buf->surface); + buf->pango = pango_cairo_create_context(buf->cairo); + + wl_buffer_add_listener(buf->buffer, &buffer_listener, state); + return buf; +} + +static void destroy_buffer(struct buffer *buffer) { + if (buffer->buffer) { + wl_buffer_destroy(buffer->buffer); + } + if (buffer->cairo) { + cairo_destroy(buffer->cairo); + } + if (buffer->surface) { + cairo_surface_destroy(buffer->surface); + } + memset(buffer, 0, sizeof(struct buffer)); +} + +struct buffer *get_next_buffer(struct client_state *state) { + struct buffer *buffer = NULL; + + int i; + for (i = 0; i < 2; ++i) { + if (state->buffers[i].busy) { + continue; + } + buffer = &state->buffers[i]; + } + + if (!buffer) { + return NULL; + } + + if (buffer->width != state->width || buffer->height != state->height) { + destroy_buffer(buffer); + } + + if (!buffer->buffer) { + if (!create_buffer(state, buffer, state->width, state->height, WL_SHM_FORMAT_ARGB8888)) { + return NULL; + } + } + + state->cairo = buffer->cairo; + state->buffer = buffer; + return buffer; +} diff --git a/wayland/client.c b/wayland/client.c index fcbba8858..4d4a6bbb1 100644 --- a/wayland/client.c +++ b/wayland/client.c @@ -7,7 +7,8 @@ #include #include #include -#include "client.h" +#include "client/client.h" +#include "client/buffer.h" #include "list.h" #include "log.h" @@ -73,87 +74,12 @@ static const struct wl_registry_listener registry_listener = { .global_remove = registry_global_remove }; -static int create_pool_file(size_t size) { - static const char template[] = "/swaybg-XXXXXX"; - const char *path = getenv("XDG_RUNTIME_DIR"); - if (!path) { - return -1; - } - - int ts = (path[strlen(path) - 1] == '/'); - - char *name = malloc( - strlen(template) + - strlen(path) + - (ts ? 1 : 0) + 1); - sprintf(name, "%s%s%s", path, ts ? "" : "/", template); - - int fd = mkstemp(name); - free(name); - - if (fd < 0) { - return -1; - } - - if (ftruncate(fd, size) < 0) { - close(fd); - return -1; - } - - return fd; -} - -static void buffer_release(void *data, struct wl_buffer *buffer) { - struct client_state *state = data; - state->busy = false; - sway_log(L_INFO, "buffer release"); -} - -static const struct wl_buffer_listener buffer_listener = { - .release = buffer_release -}; - -struct buffer *create_buffer(struct client_state *state, - int32_t width, int32_t height, uint32_t format) { - - struct buffer *buf = malloc(sizeof(struct buffer)); - memset(buf, 0, sizeof(struct buffer)); - uint32_t stride = width * 4; - uint32_t size = stride * height; - - int fd = create_pool_file(size); - void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - buf->pool = wl_shm_create_pool(state->shm, fd, size); - buf->buffer = wl_shm_pool_create_buffer(buf->pool, 0, width, height, stride, format); - wl_shm_pool_destroy(buf->pool); - close(fd); - fd = -1; - - state->cairo_surface = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, width, height, stride); - state->cairo = cairo_create(state->cairo_surface); - state->pango = pango_cairo_create_context(state->cairo); - - wl_buffer_add_listener(buf->buffer, &buffer_listener, state); - - sway_log(L_INFO, "%p %p", buf->pool, buf->buffer); - return buf; -} - -static void frame_callback(void *data, struct wl_callback *callback, uint32_t time) { - sway_log(L_INFO, "frame callback"); - struct client_state *state = data; - wl_callback_destroy(callback); - state->frame_cb = NULL; -} - -static const struct wl_callback_listener listener = { - frame_callback -}; - -struct client_state *client_setup(void) { +struct client_state *client_setup(uint32_t width, uint32_t height) { struct client_state *state = malloc(sizeof(struct client_state)); memset(state, 0, sizeof(struct client_state)); state->outputs = create_list(); + state->width = width; + state->height = height; state->display = wl_display_connect(NULL); if (!state->display) { @@ -168,22 +94,33 @@ struct client_state *client_setup(void) { wl_display_roundtrip(state->display); wl_registry_destroy(registry); - state->buffer = create_buffer(state, 100, 100, WL_SHM_FORMAT_ARGB8888); state->surface = wl_compositor_create_surface(state->compositor); state->shell_surface = wl_shell_get_shell_surface(state->shell, state->surface); wl_shell_surface_set_toplevel(state->shell_surface); - wl_surface_damage(state->surface, 0, 0, 100, 100); - return state; } -int client_render(struct client_state *state) { - if (state->frame_cb || state->busy) { - return 2; - } - sway_log(L_INFO, "Rendering"); +static void frame_callback(void *data, struct wl_callback *callback, uint32_t time) { + struct client_state *state = data; + wl_callback_destroy(callback); + state->frame_cb = NULL; +} +static const struct wl_callback_listener listener = { + frame_callback +}; + +int client_prerender(struct client_state *state) { + if (state->frame_cb) { + return 0; + } + + get_next_buffer(state); + return 1; +} + +int client_render(struct client_state *state) { state->frame_cb = wl_surface_frame(state->surface); wl_callback_add_listener(state->frame_cb, &listener, state); @@ -191,9 +128,7 @@ int client_render(struct client_state *state) { wl_surface_attach(state->surface, state->buffer->buffer, 0, 0); wl_surface_commit(state->surface); - state->busy = true; - - return wl_display_dispatch(state->display) != -1; + return 1; } void client_teardown(struct client_state *state) {