From 8b2aaf102f76921721209459eea10bed2094a730 Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Fri, 30 Aug 2019 00:40:29 +0100 Subject: source: Refactor out common async logic --- src/backend_librsvg.c | 116 +++++++++----------------------------------------- 1 file changed, 21 insertions(+), 95 deletions(-) (limited to 'src/backend_librsvg.c') diff --git a/src/backend_librsvg.c b/src/backend_librsvg.c index ff27477..9023b10 100644 --- a/src/backend_librsvg.c +++ b/src/backend_librsvg.c @@ -1,12 +1,12 @@ #include "backend.h" +#include "image.h" #include "source.h" +#include "source_private.h" -#include +#include #include #include -#include - /* Some systems like GNU/Hurd don't define PATH_MAX */ #ifndef PATH_MAX #define PATH_MAX 4096 @@ -15,85 +15,40 @@ struct private { void *data; size_t len; + char path[PATH_MAX+8]; }; -static void source_free(struct imv_source *src) -{ - pthread_mutex_lock(&src->busy); - free(src->name); - src->name = NULL; - - struct private *private = src->private; - free(private); - src->private = NULL; - - pthread_mutex_unlock(&src->busy); - pthread_mutex_destroy(&src->busy); - free(src); -} - -static void report_error(struct imv_source *src) +static void free_private(void *raw_private) { - assert(src->callback); - - struct imv_source_message msg; - msg.source = src; - msg.user_data = src->user_data; - msg.image = NULL; - msg.error = "Internal error"; - - pthread_mutex_unlock(&src->busy); - src->callback(&msg); + free(raw_private); } -static void send_svg(struct imv_source *src, RsvgHandle *handle) +static void load_image(void *raw_private, struct imv_image **image, int *frametime) { - assert(src->callback); + *image = NULL; + *frametime = 0; - struct imv_source_message msg; - msg.source = src; - msg.user_data = src->user_data; - msg.image = imv_image_create_from_svg(handle); - msg.frametime = 0; - msg.error = NULL; - - pthread_mutex_unlock(&src->busy); - src->callback(&msg); -} - -static void *load_image(struct imv_source *src) -{ - /* Don't run if this source is already active */ - if (pthread_mutex_trylock(&src->busy)) { - return NULL; - } + struct private *private = raw_private; RsvgHandle *handle = NULL; GError *error = NULL; - struct private *private = src->private; if (private->data) { handle = rsvg_handle_new_from_data(private->data, private->len, &error); } else { - char path[PATH_MAX+8]; - snprintf(path, sizeof path, "file://%s", src->name); - handle = rsvg_handle_new_from_file(path, &error); + handle = rsvg_handle_new_from_file(private->path, &error); } - if (!handle) { - report_error(src); - return NULL; + if (handle) { + *image = imv_image_create_from_svg(handle); } - - RsvgDimensionData dim; - rsvg_handle_get_dimensions(handle, &dim); - src->width = dim.width; - src->height = dim.height; - - send_svg(src, handle); - return NULL; } +static const struct imv_source_vtable vtable = { + .load_first_frame = load_image, + .free = free_private +}; + static enum backend_result open_path(const char *path, struct imv_source **src) { /* Look for an tag near the start of the file */ @@ -113,23 +68,9 @@ static enum backend_result open_path(const char *path, struct imv_source **src) struct private *private = malloc(sizeof *private); private->data = NULL; private->len = 0; + snprintf(private->path, sizeof private->path, "file://%s", path); - struct imv_source *source = calloc(1, sizeof *source); - source->name = strdup(path); - - source->width = 1024; - source->height = 1024; - source->num_frames = 1; - source->next_frame = 1; - pthread_mutex_init(&source->busy, NULL); - source->load_first_frame = &load_image; - source->load_next_frame = NULL; - source->free = &source_free; - source->callback = NULL; - source->user_data = NULL; - source->private = private; - - *src = source; + *src = imv_source_create(&vtable, private); return BACKEND_SUCCESS; } @@ -151,22 +92,7 @@ static enum backend_result open_memory(void *data, size_t len, struct imv_source private->data = data; private->len = len; - struct imv_source *source = calloc(1, sizeof *source); - source->name = strdup("-"); - - source->width = 1024; - source->height = 1024; - source->num_frames = 1; - source->next_frame = 1; - pthread_mutex_init(&source->busy, NULL); - source->load_first_frame = &load_image; - source->load_next_frame = NULL; - source->free = &source_free; - source->callback = NULL; - source->user_data = NULL; - source->private = private; - - *src = source; + *src = imv_source_create(&vtable, private); return BACKEND_SUCCESS; } -- cgit v1.2.3