From 10115db62f636a1b42cf7941acab6014ce133fb7 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Wed, 21 Aug 2013 18:32:18 +0200 Subject: [PATCH] Rewrite render thread as request based system ZathuraRenderer is a thread pool rendering the pages. Every page widget holds a ZathuraRenderRequest instance to request its page to be rendered. This object can also be used to abort the request in case the page is not visible anymore. Signed-off-by: Sebastian Ramacher --- commands.c | 4 +- page-widget.c | 22 ++- print.c | 8 +- render.c | 488 +++++++++++++++++++++++++++++++++++--------------- render.h | 140 ++++++++++++--- types.h | 5 + zathura.c | 19 +- zathura.h | 6 +- 8 files changed, 500 insertions(+), 192 deletions(-) diff --git a/commands.c b/commands.c index 76e2a27..b12c979 100644 --- a/commands.c +++ b/commands.c @@ -381,9 +381,9 @@ cmd_search(girara_session_t* session, const char* input, girara_argument_t* argu GtkWidget* page_widget = zathura_page_get_widget(zathura, page); g_object_set(page_widget, "draw-links", FALSE, NULL); - render_lock(zathura->sync.render_thread); + zathura_renderer_lock(zathura->sync.render_thread); girara_list_t* result = zathura_page_search_text(page, input, &error); - render_unlock(zathura->sync.render_thread); + zathura_renderer_unlock(zathura->sync.render_thread); if (result == NULL || girara_list_size(result) == 0) { girara_list_free(result); diff --git a/page-widget.c b/page-widget.c index adf69da..86096eb 100644 --- a/page-widget.c +++ b/page-widget.c @@ -15,6 +15,7 @@ #include "utils.h" #include "shortcuts.h" #include "synctex.h" +#include "zathura.h" G_DEFINE_TYPE(ZathuraPage, zathura_page_widget, GTK_TYPE_DRAWING_AREA) @@ -22,6 +23,7 @@ typedef struct zathura_page_widget_private_s { zathura_page_t* page; /**< Page object */ zathura_t* zathura; /**< Zathura object */ cairo_surface_t* surface; /**< Cairo surface */ + ZathuraRenderRequest* render_request; /* Request object */ bool render_requested; /**< No surface and rendering has been requested */ gint64 last_view; /**< Last time the page has been viewed */ mutex lock; /**< Lock */ @@ -144,6 +146,7 @@ zathura_page_widget_init(ZathuraPage* widget) priv->page = NULL; priv->surface = NULL; priv->render_requested = false; + priv->render_request = NULL; priv->last_view = g_get_real_time(); priv->links.list = NULL; @@ -177,7 +180,18 @@ zathura_page_widget_new(zathura_t* zathura, zathura_page_t* page) { g_return_val_if_fail(page != NULL, NULL); - return g_object_new(ZATHURA_TYPE_PAGE, "page", page, "zathura", zathura, NULL); + GObject* ret = g_object_new(ZATHURA_TYPE_PAGE, "page", page, "zathura", zathura, NULL); + if (ret == NULL) { + return NULL; + } + + ZathuraPage* widget = ZATHURA_PAGE(ret); + zathura_page_widget_private_t* priv = ZATHURA_PAGE_GET_PRIVATE(widget); + priv->render_request = zathura_render_request_new(zathura->sync.render_thread, page); + g_signal_connect_object(priv->render_request, "completed", + G_CALLBACK(zathura_page_widget_update_surface), widget, G_CONNECT_SWAPPED); + + return GTK_WIDGET(ret); } static void @@ -190,6 +204,10 @@ zathura_page_widget_finalize(GObject* object) cairo_surface_destroy(priv->surface); } + if (priv->render_request != NULL) { + g_object_unref(priv->render_request); + } + if (priv->search.list != NULL) { girara_list_free(priv->search.list); } @@ -485,7 +503,7 @@ zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo) /* render real page */ if (priv->render_requested == false) { priv->render_requested = true; - render_page(priv->zathura->sync.render_thread, priv->page); + zathura_render_request(priv->render_request); } } mutex_unlock(&(priv->lock)); diff --git a/print.c b/print.c index b32e5b3..8f0a39c 100644 --- a/print.c +++ b/print.c @@ -109,9 +109,9 @@ cb_print_draw_page(GtkPrintOperation* print_operation, GtkPrintContext* /* Try to render the page without a temporary surface. This only works with * plugins that support rendering to any surface. */ girara_debug("printing page %d ...", page_number); - render_lock(zathura->sync.render_thread); + zathura_renderer_lock(zathura->sync.render_thread); int err = zathura_page_render(page, cairo, true); - render_unlock(zathura->sync.render_thread); + zathura_renderer_unlock(zathura->sync.render_thread); if (err == ZATHURA_ERROR_OK) { return; } @@ -144,9 +144,9 @@ cb_print_draw_page(GtkPrintOperation* print_operation, GtkPrintContext* /* Render the page to the temporary surface */ girara_debug("printing page %d ...", page_number); - render_lock(zathura->sync.render_thread); + zathura_renderer_lock(zathura->sync.render_thread); err = zathura_page_render(page, temp_cairo, true); - render_unlock(zathura->sync.render_thread); + zathura_renderer_unlock(zathura->sync.render_thread); if (err != ZATHURA_ERROR_OK) { cairo_destroy(temp_cairo); cairo_surface_destroy(surface); diff --git a/render.c b/render.c index e8bcde3..648b78a 100644 --- a/render.c +++ b/render.c @@ -5,7 +5,6 @@ #include #include #include - #include "glib-compat.h" #include "render.h" #include "zathura.h" @@ -14,80 +13,298 @@ #include "page-widget.h" #include "utils.h" -static void render_job(void* data, void* user_data); -static bool render(zathura_t* zathura, zathura_page_t* page); -static gint render_thread_sort(gconstpointer a, gconstpointer b, gpointer data); +/* define the two types */ +G_DEFINE_TYPE(ZathuraRenderer, zathura_renderer, G_TYPE_OBJECT) +G_DEFINE_TYPE(ZathuraRenderRequest, zathura_render_request, G_TYPE_OBJECT) -struct render_thread_s { +/* private methods for ZathuraRenderer */ +static void zathura_renderer_finalize(GObject* object); +/* private methods for ZathuraRenderRequest */ +static void zathura_render_request_finalize(GObject* object); + +static void render_job(void* data, void* user_data); +static bool render(ZathuraRenderRequest* request, ZathuraRenderer* renderer); +static gint render_thread_sort(gconstpointer a, gconstpointer b, gpointer data); +static void color2double(GdkColor* col, double* v); + + +/* private data for ZathuraRenderer */ +typedef struct private_s { GThreadPool* pool; /**< Pool of threads */ mutex mutex; /**< Render lock */ - bool about_to_close; /**< Render thread is to be freed */ + volatile bool about_to_close; /**< Render thread is to be freed */ + + /* recolor information */ + struct { + bool enabled; + bool hue; + + double light[3]; + double dark[3]; + } recolor; +} private_t; + +/* private data for ZathuraRenderRequest */ +typedef struct request_private_s { + ZathuraRenderer* renderer; + zathura_page_t* page; + bool requested; + bool aborted; +} request_private_t; + +#define GET_PRIVATE(obj) \ + (G_TYPE_INSTANCE_GET_PRIVATE((obj), ZATHURA_TYPE_RENDERER, private_t)) +#define REQUEST_GET_PRIVATE(obj) \ + (G_TYPE_INSTANCE_GET_PRIVATE((obj), ZATHURA_TYPE_RENDER_REQUEST, \ + request_private_t)) + +/* init, new and free for ZathuraRenderer */ + +static void +zathura_renderer_class_init(ZathuraRendererClass* class) +{ + /* add private members */ + g_type_class_add_private(class, sizeof(private_t)); + + /* overwrite methods */ + GObjectClass* object_class = G_OBJECT_CLASS(class); + object_class->finalize = zathura_renderer_finalize; +// object_class->set_property = zathura_page_widget_set_property; +// object_class->get_property = zathura_page_widget_get_property; +} + +static void +zathura_renderer_init(ZathuraRenderer* renderer) +{ + private_t* priv = GET_PRIVATE(renderer); + priv->pool = g_thread_pool_new(render_job, renderer, 1, TRUE, NULL); + priv->about_to_close = false; + g_thread_pool_set_sort_function(priv->pool, render_thread_sort, NULL); + mutex_init(&priv->mutex); + + priv->recolor.enabled = false; + priv->recolor.hue = true; + priv->recolor.light[0] = priv->recolor.light[1] = priv->recolor.light[2] = 1; + priv->recolor.dark[0] = priv->recolor.dark[1] = priv->recolor.dark[2] = 1; +} + +ZathuraRenderer* +zathura_renderer_new(zathura_t* zathura) +{ + g_return_val_if_fail(zathura != NULL, NULL); + + return g_object_new(ZATHURA_TYPE_RENDERER, /*"page", page, "zathura", zathura, */ NULL); +} + +static void +zathura_renderer_finalize(GObject* object) +{ + ZathuraRenderer* renderer = ZATHURA_RENDERER(object); + private_t* priv = GET_PRIVATE(renderer); + + zathura_renderer_stop(renderer); + if (priv->pool) { + g_thread_pool_free(priv->pool, TRUE, TRUE); + } + mutex_free(&(priv->mutex)); +} + +/* init, new and free for ZathuraRenderRequest */ + +enum { + REQUEST_COMPLETED, + REQUEST_LAST_SIGNAL }; +static guint request_signals[REQUEST_LAST_SIGNAL] = { 0 }; + +static void +zathura_render_request_class_init(ZathuraRenderRequestClass* class) +{ + /* add private members */ + g_type_class_add_private(class, sizeof(request_private_t)); + + /* overwrite methods */ + GObjectClass* object_class = G_OBJECT_CLASS(class); + object_class->finalize = zathura_render_request_finalize; +// object_class->set_property = zathura_page_widget_set_property; +// object_class->get_property = zathura_page_widget_get_property; + + request_signals[REQUEST_COMPLETED] = g_signal_new("completed", + ZATHURA_TYPE_RENDER_REQUEST, + G_SIGNAL_RUN_LAST, + 0, + NULL, + NULL, + g_cclosure_marshal_generic, + G_TYPE_NONE, + 1, + G_TYPE_POINTER); +} + +static void +zathura_render_request_init(ZathuraRenderRequest* request) +{ + request_private_t* priv = REQUEST_GET_PRIVATE(request); + priv->renderer = NULL; + priv->page = NULL; +} + +ZathuraRenderRequest* +zathura_render_request_new(ZathuraRenderer* renderer, zathura_page_t* page) +{ + g_return_val_if_fail(renderer != NULL && page != NULL, NULL); + + GObject* obj = g_object_new(ZATHURA_TYPE_RENDER_REQUEST, NULL); + if (obj == NULL) { + return NULL; + } + + ZathuraRenderRequest* request = ZATHURA_RENDER_REQUEST(obj); + request_private_t* priv = REQUEST_GET_PRIVATE(request); + /* we want to make sure that renderer lives long enough */ + priv->renderer = g_object_ref(renderer); + priv->page = page; + priv->aborted = false; + priv->requested = false; + + return request; +} + +static void +zathura_render_request_finalize(GObject* object) +{ + ZathuraRenderRequest* request = ZATHURA_RENDER_REQUEST(object); + request_private_t* priv = REQUEST_GET_PRIVATE(request); + + if (priv->renderer) { + g_object_unref(priv->renderer); + } +} + +/* renderer methods */ + +bool +zathura_renderer_recolor_enabled(ZathuraRenderer* renderer) +{ + g_return_val_if_fail(ZATHURA_IS_RENDERER(renderer), false); + + return GET_PRIVATE(renderer)->recolor.enabled; +} + +void +zathura_renderer_enable_recolor(ZathuraRenderer* renderer, bool enable) +{ + g_return_if_fail(ZATHURA_IS_RENDERER(renderer)); + + GET_PRIVATE(renderer)->recolor.enabled = enable; +} + +bool +zathura_renderer_recolor_hue_enabled(ZathuraRenderer* renderer) +{ + g_return_val_if_fail(ZATHURA_IS_RENDERER(renderer), false); + + return GET_PRIVATE(renderer)->recolor.hue; +} + +void +zathura_renderer_enable_recolor_hue(ZathuraRenderer* renderer, bool enable) +{ + g_return_if_fail(ZATHURA_IS_RENDERER(renderer)); + + GET_PRIVATE(renderer)->recolor.hue = enable; +} + +void zathura_renderer_set_recolor_colors(ZathuraRenderer* renderer, + GdkColor* light, GdkColor* dark) +{ + g_return_if_fail(ZATHURA_IS_RENDERER(renderer)); + g_return_if_fail(light != NULL && dark != NULL); + + private_t* priv = GET_PRIVATE(renderer); + color2double(light, priv->recolor.light); + color2double(dark, priv->recolor.dark); +} + +void +zathura_renderer_lock(ZathuraRenderer* renderer) +{ + g_return_if_fail(ZATHURA_IS_RENDERER(renderer)); + + private_t* priv = GET_PRIVATE(renderer); + mutex_lock(&priv->mutex); +} + +void +zathura_renderer_unlock(ZathuraRenderer* renderer) +{ + g_return_if_fail(ZATHURA_IS_RENDERER(renderer)); + + private_t* priv = GET_PRIVATE(renderer); + mutex_unlock(&priv->mutex); +} + +void +zathura_renderer_stop(ZathuraRenderer* renderer) +{ + g_return_if_fail(ZATHURA_IS_RENDERER(renderer)); + GET_PRIVATE(renderer)->about_to_close = true; +} + + +/* ZathuraRenderRequest methods */ + +void +zathura_render_request(ZathuraRenderRequest* request) +{ + g_return_if_fail(ZATHURA_IS_RENDER_REQUEST(request)); + + request_private_t* request_priv = REQUEST_GET_PRIVATE(request); + private_t* priv = GET_PRIVATE(request_priv->renderer); + if (request_priv->requested == false) { + request_priv->requested = true; + request_priv->aborted = false; + + g_thread_pool_push(priv->pool, request, NULL); + } +} + +void +zathura_render_request_abort(ZathuraRenderRequest* request) +{ + g_return_if_fail(ZATHURA_IS_RENDER_REQUEST(request)); + + request_private_t* request_priv = REQUEST_GET_PRIVATE(request); + if (request_priv->requested == true) { + request_priv->aborted = true; + } +} + + static void render_job(void* data, void* user_data) { - zathura_page_t* page = data; - zathura_t* zathura = user_data; - if (page == NULL || zathura == NULL) { + ZathuraRenderRequest* request = data; + ZathuraRenderer* renderer = user_data; + g_return_if_fail(ZATHURA_IS_RENDER_REQUEST(request)); + g_return_if_fail(ZATHURA_IS_RENDERER(renderer)); + + private_t* priv = GET_PRIVATE(renderer); + request_private_t* request_priv = REQUEST_GET_PRIVATE(request); + if (priv->about_to_close == true || request_priv->aborted == true) { + /* back out early */ + request_priv->requested = false; return; } - girara_debug("rendering page %d ...", zathura_page_get_index(page) + 1); - if (render(zathura, page) != true) { - girara_error("Rendering failed (page %d)\n", zathura_page_get_index(page) + 1); + girara_debug("Rendering page %d ...", zathura_page_get_index(request_priv->page) + 1); + if (render(request, renderer) != true) { + girara_error("Rendering failed (page %d)\n", zathura_page_get_index(request_priv->page) + 1); } + request_priv->requested = false; } -render_thread_t* -render_init(zathura_t* zathura) -{ - render_thread_t* render_thread = g_malloc0(sizeof(render_thread_t)); - - /* setup */ - render_thread->pool = g_thread_pool_new(render_job, zathura, 1, TRUE, NULL); - if (render_thread->pool == NULL) { - goto error_free; - } - - render_thread->about_to_close = false; - g_thread_pool_set_sort_function(render_thread->pool, render_thread_sort, zathura); - mutex_init(&render_thread->mutex); - - return render_thread; - -error_free: - - render_free(render_thread); - return NULL; -} - -void -render_free(render_thread_t* render_thread) -{ - if (render_thread == NULL) { - return; - } - - render_thread->about_to_close = true; - if (render_thread->pool) { - g_thread_pool_free(render_thread->pool, TRUE, TRUE); - } - - mutex_free(&(render_thread->mutex)); - g_free(render_thread); -} - -bool -render_page(render_thread_t* render_thread, zathura_page_t* page) -{ - if (render_thread == NULL || page == NULL || render_thread->pool == NULL || render_thread->about_to_close == true) { - return false; - } - - g_thread_pool_push(render_thread->pool, page, NULL); - return true; -} static void color2double(GdkColor* col, double* v) @@ -101,7 +318,7 @@ color2double(GdkColor* col, double* v) Assumes that l is in the interval l1, l2 and corrects the value to force u=0 on l1 and l2 */ static double -colorumax(double* h, double l, double l1, double l2) +colorumax(const double* h, double l, double l1, double l2) { double u, uu, v, vv, lv; if (h[0] == 0 && h[1] == 0 && h[2] == 0) { @@ -143,11 +360,11 @@ colorumax(double* h, double l, double l1, double l2) static bool -render(zathura_t* zathura, zathura_page_t* page) +render(ZathuraRenderRequest* request, ZathuraRenderer* renderer) { - if (zathura == NULL || page == NULL || zathura->sync.render_thread->about_to_close == true) { - return false; - } + private_t* priv = GET_PRIVATE(renderer); + request_private_t* request_priv = REQUEST_GET_PRIVATE(request); + zathura_page_t* page = request_priv->page; /* create cairo surface */ unsigned int page_width = 0; @@ -155,13 +372,11 @@ render(zathura_t* zathura, zathura_page_t* page) const double real_scale = page_calc_height_width(page, &page_height, &page_width, false); cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, page_width, page_height); - if (surface == NULL) { return false; } cairo_t* cairo = cairo_create(surface); - if (cairo == NULL) { cairo_surface_destroy(surface); return false; @@ -178,20 +393,24 @@ render(zathura_t* zathura, zathura_page_t* page) cairo_scale(cairo, real_scale, real_scale); } - render_lock(zathura->sync.render_thread); + zathura_renderer_lock(renderer); if (zathura_page_render(page, cairo, false) != ZATHURA_ERROR_OK) { - render_unlock(zathura->sync.render_thread); + zathura_renderer_unlock(renderer); cairo_destroy(cairo); cairo_surface_destroy(surface); return false; } - render_unlock(zathura->sync.render_thread); + zathura_renderer_unlock(renderer); cairo_restore(cairo); cairo_destroy(cairo); - const int rowstride = cairo_image_surface_get_stride(surface); - unsigned char* image = cairo_image_surface_get_data(surface); + /* before recoloring, check if we've been aborted */ + if (priv->about_to_close == true || request_priv->aborted == true) { + cairo_surface_destroy(surface); + return true; + } + /* recolor */ /* uses a representation of a rgb color as follows: @@ -199,72 +418,84 @@ render(zathura_t* zathura, zathura_page_t* page) - a hue vector, which indicates a radian direction from the grey axis, inside the equal lightness plane. - a saturation scalar between 0,1. It is 0 when grey, 1 when the color is in the boundary of the rgb cube. */ - if (zathura->global.recolor == true) { + if (priv->recolor.enabled == true) { + const int rowstride = cairo_image_surface_get_stride(surface); + unsigned char* image = cairo_image_surface_get_data(surface); + /* RGB weights for computing lightness. Must sum to one */ - double a[] = {0.30, 0.59, 0.11}; + static const double a[] = {0.30, 0.59, 0.11}; - double l1, l2, l, s, u, t; - double h[3]; - double rgb1[3], rgb2[3], rgb[3]; - - color2double(&zathura->ui.colors.recolor_dark_color, rgb1); - color2double(&zathura->ui.colors.recolor_light_color, rgb2); - - l1 = (a[0]*rgb1[0] + a[1]*rgb1[1] + a[2]*rgb1[2]); - l2 = (a[0]*rgb2[0] + a[1]*rgb2[1] + a[2]*rgb2[2]); +#define rgb1 priv->recolor.dark +#define rgb2 priv->recolor.light + const double l1 = (a[0]*rgb1[0] + a[1]*rgb1[1] + a[2]*rgb1[2]); + const double l2 = (a[0]*rgb2[0] + a[1]*rgb2[1] + a[2]*rgb2[2]); for (unsigned int y = 0; y < page_height; y++) { unsigned char* data = image + y * rowstride; - for (unsigned int x = 0; x < page_width; x++) { + for (unsigned int x = 0; x < page_width; x++, data += 4) { /* Careful. data color components blue, green, red. */ - rgb[0] = (double) data[2] / 256.; - rgb[1] = (double) data[1] / 256.; - rgb[2] = (double) data[0] / 256.; + const double rgb[3] = { + (double) data[2] / 256., + (double) data[1] / 256., + (double) data[0] / 256. + }; /* compute h, s, l data */ - l = a[0]*rgb[0] + a[1]*rgb[1] + a[2]*rgb[2]; + double l = a[0]*rgb[0] + a[1]*rgb[1] + a[2]*rgb[2]; - h[0] = rgb[0] - l; - h[1] = rgb[1] - l; - h[2] = rgb[2] - l; + const double h[3] = { + rgb[0] - l, + rgb[1] - l, + rgb[2] - l + }; - /* u is the maximum possible saturation for given h and l. s is a rescaled saturation between 0 and 1 */ - u = colorumax(h, l, 0, 1); + /* u is the maximum possible saturation for given h and l. s is a + * rescaled saturation between 0 and 1 */ + double u = colorumax(h, l, 0, 1); + double s; if (u == 0) { s = 0; } else { s = 1/u; } - /* Interpolates lightness between light and dark colors. white goes to light, and black goes to dark. */ - t = l; + /* Interpolates lightness between light and dark colors. white goes to + * light, and black goes to dark. */ + const double t = l; l = t * (l2 - l1) + l1; - if (zathura->global.recolor_keep_hue == true) { - /* adjusting lightness keeping hue of current color. white and black go to grays of same ligtness - as light and dark colors. */ + if (priv->recolor.hue == true) { + /* adjusting lightness keeping hue of current color. white and black + * go to grays of same ligtness as light and dark colors. */ u = colorumax(h, l, l1, l2); data[2] = (unsigned char)round(255.*(l + s*u * h[0])); data[1] = (unsigned char)round(255.*(l + s*u * h[1])); data[0] = (unsigned char)round(255.*(l + s*u * h[2])); } else { - /* Linear interpolation between dark and light with color ligtness as a parameter */ + /* linear interpolation between dark and light with color ligtness as + * a parameter */ data[2] = (unsigned char)round(255.*(t * (rgb2[0] - rgb1[0]) + rgb1[0])); data[1] = (unsigned char)round(255.*(t * (rgb2[1] - rgb1[1]) + rgb1[1])); data[0] = (unsigned char)round(255.*(t * (rgb2[2] - rgb1[2]) + rgb1[2])); } - - data += 4; } } + +#undef rgb1 +#undef rgb2 } - if (zathura->sync.render_thread->about_to_close == false) { + if (priv->about_to_close == false && request_priv->aborted == false) { /* update the widget */ + /* gdk_threads_enter(); GtkWidget* widget = zathura_page_get_widget(zathura, page); zathura_page_widget_update_surface(ZATHURA_PAGE(widget), surface); + gdk_threads_leave(); */ + + gdk_threads_enter(); + g_signal_emit(request, request_signals[REQUEST_COMPLETED], 0, surface); gdk_threads_leave(); } @@ -296,48 +527,21 @@ render_all(zathura_t* zathura) static gint render_thread_sort(gconstpointer a, gconstpointer b, gpointer data) { - if (a == NULL || b == NULL || data == NULL) { + if (a == NULL || b == NULL) { return 0; } - zathura_page_t* page_a = (zathura_page_t*) a; - zathura_page_t* page_b = (zathura_page_t*) b; - zathura_t* zathura = (zathura_t*) data; - - unsigned int page_a_index = zathura_page_get_index(page_a); - unsigned int page_b_index = zathura_page_get_index(page_b); - - gint64 last_view_a = 0; - gint64 last_view_b = 0; - - g_object_get(zathura->pages[page_a_index], "last-view", &last_view_a, NULL); - g_object_get(zathura->pages[page_b_index], "last-view", &last_view_b, NULL); - - if (last_view_a < last_view_b) { - return -1; - } else if (last_view_a > last_view_b) { - return 1; + ZathuraRenderRequest* request_a = (ZathuraRenderRequest*) a; + ZathuraRenderRequest* request_b = (ZathuraRenderRequest*) b; + request_private_t* priv_a = REQUEST_GET_PRIVATE(request_a); + request_private_t* priv_b = REQUEST_GET_PRIVATE(request_b); + if (priv_a->aborted == priv_b->aborted) { + unsigned int page_a_index = zathura_page_get_index(priv_a->page); + unsigned int page_b_index = zathura_page_get_index(priv_b->page); + return page_a_index < page_b_index ? -1 : + (page_a_index > page_b_index ? 1 : 0); } - return 0; -} - -void -render_lock(render_thread_t* render_thread) -{ - if (render_thread == NULL) { - return; - } - - mutex_lock(&render_thread->mutex); -} - -void -render_unlock(render_thread_t* render_thread) -{ - if (render_thread == NULL) { - return; - } - - mutex_unlock(&render_thread->mutex); + /* sort aborted entries earlier so that the are thrown out of the queue */ + return priv_a->aborted ? 1 : -1; } diff --git a/render.h b/render.h index 2d7a12c..950ed66 100644 --- a/render.h +++ b/render.h @@ -6,34 +6,131 @@ #include #include #include +#include "types.h" -#include "zathura.h" -#include "callbacks.h" +typedef struct zathura_renderer_class_s ZathuraRendererClass; + +struct zathura_renderer_s +{ + GObject parent; +}; + +struct zathura_renderer_class_s +{ + GObjectClass parent_class; +}; + +#define ZATHURA_TYPE_RENDERER \ + (zathura_renderer_get_type()) +#define ZATHURA_RENDERER(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), ZATHURA_TYPE_RENDERER, ZathuraRenderer)) +#define ZATHURA_RENDERER_CLASS(obj) \ + (G_TYPE_CHECK_CLASS_CAST((obj), ZATHURA_TYPE_RENDERER, ZathuraRendererClass)) +#define ZATHURA_IS_RENDERER(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), ZATHURA_TYPE_RENDERER)) +#define ZATHURA_IS_RENDERER_CLASS(obj) \ + (G_TYPE_CHECK_CLASS_TYPE((obj), ZATHURA_TYPE_RENDERER)) +#define ZATHURA_RENDERER_GET_CLASS \ + (G_TYPE_INSTANCE_GET_CLASS((obj), ZATHURA_TYPE_RENDERER, ZathuraRendererClass)) /** - * This function initializes a render thread - * - * @param zathura object - * @return The render thread object or NULL if an error occured + * Returns the type of the renderer. + * @return the type */ -render_thread_t* render_init(zathura_t* zathura); +GType zathura_renderer_get_type(void); +/** + * Create a page view widget. + * @param zathura the zathura instance + * @param page the page to be displayed + * @return a page view widget + */ +ZathuraRenderer* zathura_renderer_new(zathura_t* zathura); + +bool zathura_renderer_recolor_enabled(ZathuraRenderer* renderer); +void zathura_renderer_enable_recolor(ZathuraRenderer* renderer, bool enable); +bool zathura_renderer_recolor_hue_enabled(ZathuraRenderer* renderer); +void zathura_renderer_enable_recolor_hue(ZathuraRenderer* renderer, + bool enable); +void zathura_renderer_set_recolor_colors(ZathuraRenderer* renderer, + GdkColor* light, GdkColor* dark); + +void zathura_renderer_stop(ZathuraRenderer* renderer); /** - * This function destroys the render thread object + * Lock the render thread. This is useful if you want to render on your own (e.g + * for printing). * - * @param render_thread The render thread object + * @param render_thread The render thread object. */ -void render_free(render_thread_t* render_thread); +void zathura_renderer_lock(ZathuraRenderer* renderer); + +/** + * Unlock the render thread. + * + * @param render_thread The render thread object. + */ +void zathura_renderer_unlock(ZathuraRenderer* renderer); + + + +typedef struct zathura_render_request_s ZathuraRenderRequest; +typedef struct zathura_render_request_class_s ZathuraRenderRequestClass; + +struct zathura_render_request_s +{ + GObject parent; +}; + +struct zathura_render_request_class_s +{ + GObjectClass parent_class; +}; + +#define ZATHURA_TYPE_RENDER_REQUEST \ + (zathura_render_request_get_type()) +#define ZATHURA_RENDER_REQUEST(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), ZATHURA_TYPE_RENDER_REQUEST, \ + ZathuraRenderRequest)) +#define ZATHURA_RENDER_REQUEST_CLASS(obj) \ + (G_TYPE_CHECK_CLASS_CAST((obj), ZATHURA_TYPE_RENDER_REQUEST, \ + ZathuraRenderRequestClass)) +#define ZATHURA_IS_RENDER_REQUEST(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), ZATHURA_TYPE_RENDER_REQUEST)) +#define ZATHURA_IS_RENDER_REQUEST_CLASS(obj) \ + (G_TYPE_CHECK_CLASS_TYPE((obj), ZATHURA_TYPE_RENDER_REQUEST)) +#define ZATHURA_RENDER_REQUEST_GET_CLASS \ + (G_TYPE_INSTANCE_GET_CLASS((obj), ZATHURA_TYPE_RENDER_REQUEST, \ + ZathuraRenderRequestClass)) + +/** + * Returns the type of the renderer. + * @return the type + */ +GType zathura_page_render_info_get_type(void); +/** + * Create a page view widget. + * @param zathura the zathura instance + * @param page the page to be displayed + * @return a page view widget + */ +ZathuraRenderRequest* zathura_render_request_new(ZathuraRenderer* renderer, + zathura_page_t* page); /** * This function is used to add a page to the render thread list * that should be rendered. * - * @param render_thread The render thread object - * @param page The page that should be rendered - * @return true if no error occured + * @param request request object of the page that should be renderer */ -bool render_page(render_thread_t* render_thread, zathura_page_t* page); +void zathura_render_request(ZathuraRenderRequest* request); + +/** + * Abort an existing render request. + * + * @param reqeust request that should be aborted + */ +void zathura_render_request_abort(ZathuraRenderRequest* request); + /** * This function is used to unmark all pages as not rendered. This should @@ -44,19 +141,4 @@ bool render_page(render_thread_t* render_thread, zathura_page_t* page); */ void render_all(zathura_t* zathura); -/** - * Lock the render thread. This is useful if you want to render on your own (e.g - * for printing). - * - * @param render_thread The render thread object. - */ -void render_lock(render_thread_t* render_thread); - -/** - * Unlock the render thread. - * - * @param render_thread The render thread object. - */ -void render_unlock(render_thread_t* render_thread); - #endif // RENDER_H diff --git a/types.h b/types.h index 50bacb6..6c4731a 100644 --- a/types.h +++ b/types.h @@ -25,6 +25,11 @@ typedef struct zathura_s zathura_t; */ typedef struct zathura_plugin_manager_s zathura_plugin_manager_t; +/** + * Renderer + */ +typedef struct zathura_renderer_s ZathuraRenderer; + /** * Error types */ diff --git a/zathura.c b/zathura.c index 182a99a..82a2839 100644 --- a/zathura.c +++ b/zathura.c @@ -711,6 +711,13 @@ document_open(zathura_t* zathura, const char* path, const char* password, zathura->document = document; + /* threads */ + zathura->sync.render_thread = zathura_renderer_new(zathura); + + if (zathura->sync.render_thread == NULL) { + goto error_free; + } + /* create blank pages */ zathura->pages = calloc(number_of_pages, sizeof(GtkWidget*)); if (zathura->pages == NULL) { @@ -759,13 +766,6 @@ document_open(zathura_t* zathura, const char* path, const char* password, girara_set_view(zathura->ui.session, zathura->ui.page_widget_alignment); - /* threads */ - zathura->sync.render_thread = render_init(zathura); - - if (zathura->sync.render_thread == NULL) { - goto error_free; - } - for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) { gtk_widget_realize(zathura->pages[page_id]); } @@ -883,6 +883,9 @@ document_close(zathura_t* zathura, bool keep_monitor) return false; } + /* stop rendering */ + zathura_renderer_stop(zathura->sync.render_thread); + /* remove monitor */ if (keep_monitor == false) { if (zathura->file_monitor.monitor != NULL) { @@ -945,7 +948,7 @@ document_close(zathura_t* zathura, bool keep_monitor) zathura->jumplist.size = 0; /* release render thread */ - render_free(zathura->sync.render_thread); + g_object_unref(zathura->sync.render_thread); zathura->sync.render_thread = NULL; /* remove widgets */ diff --git a/zathura.h b/zathura.h index 13df384..8864021 100644 --- a/zathura.h +++ b/zathura.h @@ -31,10 +31,6 @@ enum { /* forward declaration for types from database.h */ typedef struct _ZathuraDatabase zathura_database_t; -/* forward declaration for types from render.h */ -struct render_thread_s; -typedef struct render_thread_s render_thread_t; - /** * Jump */ @@ -78,7 +74,7 @@ struct zathura_s struct { - render_thread_t* render_thread; /**< The thread responsible for rendering the pages */ + ZathuraRenderer* render_thread; /**< The thread responsible for rendering the pages */ } sync; struct