From 10115db62f636a1b42cf7941acab6014ce133fb7 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Wed, 21 Aug 2013 18:32:18 +0200 Subject: [PATCH 01/13] 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 From b02d3d4b8f06fd6d7f8702afe2694674cf73358e Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Sat, 24 Aug 2013 00:07:32 +0200 Subject: [PATCH 02/13] Move recolor state to the renderer Signed-off-by: Sebastian Ramacher --- callbacks.c | 16 +++++------ config.c | 14 +++++----- page-widget.c | 10 ++++--- render.c | 73 ++++++++++++++++++++++++++++++++++++++++++--------- render.h | 4 +++ zathura.c | 10 ++++++- zathura.h | 4 --- 7 files changed, 95 insertions(+), 36 deletions(-) diff --git a/callbacks.c b/callbacks.c index 1c49df0..2ce408b 100644 --- a/callbacks.c +++ b/callbacks.c @@ -488,11 +488,11 @@ cb_setting_recolor_change(girara_session_t* session, const char* name, g_return_if_fail(name != NULL); zathura_t* zathura = session->global.data; - bool bool_value = *((bool*) value); + const bool bool_value = *((bool*) value); - if (zathura->global.recolor != bool_value) { - zathura->global.recolor = bool_value; - render_all(zathura); + if (zathura->sync.render_thread != NULL && zathura_renderer_recolor_enabled(zathura->sync.render_thread) != bool_value) { + zathura_renderer_enable_recolor(zathura->sync.render_thread, bool_value); + render_all(zathura); } } @@ -506,11 +506,11 @@ cb_setting_recolor_keep_hue_change(girara_session_t* session, const char* name, g_return_if_fail(name != NULL); zathura_t* zathura = session->global.data; - bool bool_value = *((bool*) value); + const bool bool_value = *((bool*) value); - if (zathura->global.recolor_keep_hue != bool_value) { - zathura->global.recolor_keep_hue = bool_value; - render_all(zathura); + if (zathura->sync.render_thread != NULL && zathura_renderer_recolor_hue_enabled(zathura->sync.render_thread) != bool_value) { + zathura_renderer_enable_recolor_hue(zathura->sync.render_thread, bool_value); + render_all(zathura); } } diff --git a/config.c b/config.c index 88f007d..2cd00aa 100644 --- a/config.c +++ b/config.c @@ -57,9 +57,13 @@ cb_color_change(girara_session_t* session, const char* name, } else if (g_strcmp0(name, "highlight-active-color") == 0) { gdk_color_parse(string_value, &(zathura->ui.colors.highlight_color_active)); } else if (g_strcmp0(name, "recolor-darkcolor") == 0) { - gdk_color_parse(string_value, &(zathura->ui.colors.recolor_dark_color)); + if (zathura->sync.render_thread != NULL) { + zathura_renderer_set_recolor_colors_str(zathura->sync.render_thread, NULL, string_value); + } } else if (g_strcmp0(name, "recolor-lightcolor") == 0) { - gdk_color_parse(string_value, &(zathura->ui.colors.recolor_light_color)); + if (zathura->sync.render_thread != NULL) { + zathura_renderer_set_recolor_colors_str(zathura->sync.render_thread, string_value, NULL); + } } else if (g_strcmp0(name, "render-loading-bg") == 0) { gdk_color_parse(string_value, &(zathura->ui.colors.render_loading_bg)); } else if (g_strcmp0(name, "render-loading-fg") == 0) { @@ -168,10 +172,8 @@ config_load_default(zathura_t* zathura) int_value = 2000; girara_setting_add(gsession, "jumplist-size", &int_value, INT, false, _("Number of positions to remember in the jumplist"), cb_jumplist_change, NULL); - girara_setting_add(gsession, "recolor-darkcolor", NULL, STRING, false, _("Recoloring (dark color)"), cb_color_change, NULL); - girara_setting_set(gsession, "recolor-darkcolor", "#FFFFFF"); - girara_setting_add(gsession, "recolor-lightcolor", NULL, STRING, false, _("Recoloring (light color)"), cb_color_change, NULL); - girara_setting_set(gsession, "recolor-lightcolor", "#000000"); + girara_setting_add(gsession, "recolor-darkcolor", "#FFFFFF", STRING, false, _("Recoloring (dark color)"), cb_color_change, NULL); + girara_setting_add(gsession, "recolor-lightcolor", "#000000", STRING, false, _("Recoloring (light color)"), cb_color_change, NULL); girara_setting_add(gsession, "highlight-color", NULL, STRING, false, _("Color for highlighting"), cb_color_change, NULL); girara_setting_set(gsession, "highlight-color", "#9FBC00"); girara_setting_add(gsession, "highlight-active-color", NULL, STRING, false, _("Color for highlighting (active)"), cb_color_change, NULL); diff --git a/page-widget.c b/page-widget.c index 86096eb..71572f1 100644 --- a/page-widget.c +++ b/page-widget.c @@ -466,8 +466,9 @@ zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo) } } else { /* set background color */ - if (priv->zathura->global.recolor == true) { - GdkColor color = priv->zathura->ui.colors.recolor_light_color; + if (zathura_renderer_recolor_enabled(priv->zathura->sync.render_thread) == true) { + GdkColor color; + zathura_renderer_get_recolor_colors(priv->zathura->sync.render_thread, &color, NULL); cairo_set_source_rgb(cairo, color.red/65535.0, color.green/65535.0, color.blue/65535.0); } else { GdkColor color = priv->zathura->ui.colors.render_loading_bg; @@ -481,8 +482,9 @@ zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo) /* write text */ if (render_loading == true) { - if (priv->zathura->global.recolor == true) { - GdkColor color = priv->zathura->ui.colors.recolor_dark_color; + if (zathura_renderer_recolor_enabled(priv->zathura->sync.render_thread) == true) { + GdkColor color; + zathura_renderer_get_recolor_colors(priv->zathura->sync.render_thread, NULL, &color); cairo_set_source_rgb(cairo, color.red/65535.0, color.green/65535.0, color.blue/65535.0); } else { GdkColor color = priv->zathura->ui.colors.render_loading_fg; diff --git a/render.c b/render.c index 648b78a..8ca519f 100644 --- a/render.c +++ b/render.c @@ -25,7 +25,7 @@ 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); +static void color2double(const GdkColor* col, double* v); /* private data for ZathuraRenderer */ @@ -40,7 +40,9 @@ typedef struct private_s { bool hue; double light[3]; + GdkColor light_gdk; double dark[3]; + GdkColor dark_gdk; } recolor; } private_t; @@ -69,8 +71,6 @@ zathura_renderer_class_init(ZathuraRendererClass* class) /* 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 @@ -84,8 +84,8 @@ zathura_renderer_init(ZathuraRenderer* renderer) 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; + + zathura_renderer_set_recolor_colors_str(renderer, "#000000", "#FFFFFF"); } ZathuraRenderer* @@ -127,8 +127,6 @@ zathura_render_request_class_init(ZathuraRenderRequestClass* class) /* 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, @@ -216,15 +214,64 @@ zathura_renderer_enable_recolor_hue(ZathuraRenderer* renderer, bool enable) GET_PRIVATE(renderer)->recolor.hue = enable; } -void zathura_renderer_set_recolor_colors(ZathuraRenderer* renderer, +void +zathura_renderer_set_recolor_colors(ZathuraRenderer* renderer, + const GdkColor* light, const GdkColor* dark) +{ + g_return_if_fail(ZATHURA_IS_RENDERER(renderer)); + + private_t* priv = GET_PRIVATE(renderer); + if (light != NULL) { + priv->recolor.light_gdk.red = light->red; + priv->recolor.light_gdk.blue = light->blue; + priv->recolor.light_gdk.green = light->green; + color2double(light, priv->recolor.light); + } + if (dark != NULL) { + priv->recolor.dark_gdk.red = dark->red; + priv->recolor.dark_gdk.blue = dark->blue; + priv->recolor.dark_gdk.green = dark->green; + color2double(dark, priv->recolor.dark); + } +} + +void +zathura_renderer_set_recolor_colors_str(ZathuraRenderer* renderer, + const char* light, const char* dark) +{ + g_return_if_fail(ZATHURA_IS_RENDERER(renderer)); + + if (dark != NULL) { + GdkColor color; + gdk_color_parse(dark, &color); + zathura_renderer_set_recolor_colors(renderer, NULL, &color); + } + if (light != NULL) { + GdkColor color; + gdk_color_parse(light, &color); + zathura_renderer_set_recolor_colors(renderer, &color, NULL); + } +} + +void +zathura_renderer_get_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); + if (light != NULL) { + light->red = priv->recolor.light_gdk.red; + light->blue = priv->recolor.light_gdk.blue; + light->green = priv->recolor.light_gdk.green; + color2double(light, priv->recolor.light); + } + if (dark != NULL) { + dark->red = priv->recolor.dark_gdk.red; + dark->blue = priv->recolor.dark_gdk.blue; + dark->green = priv->recolor.dark_gdk.green; + color2double(dark, priv->recolor.dark); + } } void @@ -307,7 +354,7 @@ render_job(void* data, void* user_data) static void -color2double(GdkColor* col, double* v) +color2double(const GdkColor* col, double* v) { v[0] = (double) col->red / 65535.; v[1] = (double) col->green / 65535.; @@ -525,7 +572,7 @@ render_all(zathura_t* zathura) } static gint -render_thread_sort(gconstpointer a, gconstpointer b, gpointer data) +render_thread_sort(gconstpointer a, gconstpointer b, gpointer UNUSED(data)) { if (a == NULL || b == NULL) { return 0; diff --git a/render.h b/render.h index 950ed66..019c485 100644 --- a/render.h +++ b/render.h @@ -52,6 +52,10 @@ 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, + const GdkColor* light, const GdkColor* dark); +void zathura_renderer_set_recolor_colors_str(ZathuraRenderer* renderer, + const char* light, const char* dark); +void zathura_renderer_get_recolor_colors(ZathuraRenderer* renderer, GdkColor* light, GdkColor* dark); void zathura_renderer_stop(ZathuraRenderer* renderer); diff --git a/zathura.c b/zathura.c index 82a2839..ce2650a 100644 --- a/zathura.c +++ b/zathura.c @@ -68,7 +68,6 @@ zathura_create(void) zathura_t* zathura = g_malloc0(sizeof(zathura_t)); /* global settings */ - zathura->global.recolor = false; zathura->global.update_page_number = true; zathura->global.search_direction = FORWARD; @@ -718,6 +717,15 @@ document_open(zathura_t* zathura, const char* path, const char* password, goto error_free; } + char* recolor_dark = NULL; + char* recolor_light = NULL; + girara_setting_get(zathura->ui.session, "recolor-darkcolor", &recolor_dark); + girara_setting_get(zathura->ui.session, "recolor-lightcolor", &recolor_light); + zathura_renderer_set_recolor_colors_str(zathura->sync.render_thread, + recolor_light, recolor_dark); + g_free(recolor_dark); + g_free(recolor_light); + /* create blank pages */ zathura->pages = calloc(number_of_pages, sizeof(GtkWidget*)); if (zathura->pages == NULL) { diff --git a/zathura.h b/zathura.h index 8864021..bea5d0a 100644 --- a/zathura.h +++ b/zathura.h @@ -56,8 +56,6 @@ struct zathura_s struct { - GdkColor recolor_dark_color; /**< Dark color for recoloring */ - GdkColor recolor_light_color; /**< Light color for recoloring */ GdkColor highlight_color; /**< Color for highlighting */ GdkColor highlight_color_active; /** Color for highlighting */ GdkColor render_loading_bg; /**< Background color for render "Loading..." */ @@ -102,8 +100,6 @@ struct zathura_s struct { - bool recolor_keep_hue; /**< Keep hue when recoloring */ - bool recolor; /**< Recoloring mode switch */ bool update_page_number; /**< Update current page number */ int search_direction; /**< Current search direction (FORWARD or BACKWARD) */ girara_list_t* marks; /**< Marker */ From e733e032ced6760416018365b9271957b23b5664 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Fri, 30 Aug 2013 12:59:27 +0200 Subject: [PATCH 03/13] Add a callback for the signal Signed-off-by: Sebastian Ramacher --- page-widget.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/page-widget.c b/page-widget.c index 71572f1..5669afb 100644 --- a/page-widget.c +++ b/page-widget.c @@ -77,6 +77,7 @@ static gboolean cb_zathura_page_widget_motion_notify(GtkWidget* widget, GdkEvent static gboolean cb_zathura_page_widget_popup_menu(GtkWidget* widget); static void cb_menu_image_copy(GtkMenuItem* item, ZathuraPage* page); static void cb_menu_image_save(GtkMenuItem* item, ZathuraPage* page); +static void cb_update_surface(ZathuraRenderRequest* request, cairo_surface_t* surface, void* data); enum properties_e { PROP_0, @@ -189,7 +190,7 @@ zathura_page_widget_new(zathura_t* zathura, zathura_page_t* page) 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); + G_CALLBACK(cb_update_surface), widget, 0); return GTK_WIDGET(ret); } @@ -544,6 +545,14 @@ zathura_page_widget_update_surface(ZathuraPage* widget, cairo_surface_t* surface } } +static void +cb_update_surface(ZathuraRenderRequest* UNUSED(request), cairo_surface_t* surface, void* data) +{ + ZathuraPage* widget = data; + g_return_if_fail(ZATHURA_IS_PAGE(widget)); + zathura_page_widget_update_surface(widget, surface); +} + static void zathura_page_widget_size_allocate(GtkWidget* widget, GdkRectangle* allocation) { From 35bd419e49ea8380a2a9225eb08e1591488c2456 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Fri, 30 Aug 2013 13:25:22 +0200 Subject: [PATCH 04/13] Emit signal from main context Signed-off-by: Sebastian Ramacher --- README | 1 + config.mk | 7 +++++-- render.c | 32 +++++++++++++++++++++++--------- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/README b/README index c6c966a..d057887 100644 --- a/README +++ b/README @@ -6,6 +6,7 @@ girara user interface library and several document libraries. Requirements ------------ gtk2 (>= 2.28) +glib (>= 2.28) girara sqlite3 (optional, >= 3.5.9) check (for tests) diff --git a/config.mk b/config.mk index 30a0285..07b9ab6 100644 --- a/config.mk +++ b/config.mk @@ -52,6 +52,9 @@ GTHREAD_LIB ?= $(shell pkg-config --libs gthread-2.0) GMODULE_INC ?= $(shell pkg-config --cflags gmodule-no-export-2.0) GMODULE_LIB ?= $(shell pkg-config --libs gmodule-no-export-2.0) +GLIB_INC ?= $(shell pkg-config --cflags --atleast-version=2.28 glib-2.0) +GLIB_LIB ?= $(shell pkg-config --libs --atleast-version=2.28 glib-2.0) + GIRARA_INC ?= $(shell pkg-config --cflags girara-gtk${ZATHURA_GTK_VERSION}) GIRARA_LIB ?= $(shell pkg-config --libs girara-gtk${ZATHURA_GTK_VERSION}) @@ -65,8 +68,8 @@ MAGIC_INC ?= MAGIC_LIB ?= -lmagic endif -INCS = ${GIRARA_INC} ${GTK_INC} ${GTHREAD_INC} ${GMODULE_INC} -LIBS = ${GIRARA_LIB} ${GTK_LIB} ${GTHREAD_LIB} ${GMODULE_LIB} -lpthread -lm +INCS = ${GIRARA_INC} ${GTK_INC} ${GTHREAD_INC} ${GMODULE_INC} ${GLIB_INC} +LIBS = ${GIRARA_LIB} ${GTK_LIB} ${GTHREAD_LIB} ${GMODULE_LIB} ${GLIB_LIB} -lpthread -lm # flags CFLAGS += -std=c99 -pedantic -Wall -Wno-format-zero-length -Wextra $(INCS) diff --git a/render.c b/render.c index 8ca519f..91e154e 100644 --- a/render.c +++ b/render.c @@ -405,6 +405,23 @@ colorumax(const double* h, double l, double l1, double l2) return fmin(u, v); } +typedef struct emit_completed_signal_s +{ + ZathuraRenderRequest* request; + cairo_surface_t* surface; +} emit_completed_signal_t; + +static gboolean +emit_completed_signal(void* data) +{ + emit_completed_signal_t* ecs = data; + /* emit the signal */ + g_signal_emit(ecs->request, request_signals[REQUEST_COMPLETED], 0, ecs->surface); + /* clean up the data */ + cairo_surface_destroy(ecs->surface); + g_free(ecs); + return true; +} static bool render(ZathuraRenderRequest* request, ZathuraRenderer* renderer) @@ -534,16 +551,13 @@ render(ZathuraRenderRequest* request, ZathuraRenderer* renderer) } 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(); */ + emit_completed_signal_t* ecs = g_malloc(sizeof(ecs)); + ecs->request = request; + ecs->surface = surface; + cairo_surface_reference(surface); - gdk_threads_enter(); - g_signal_emit(request, request_signals[REQUEST_COMPLETED], 0, surface); - gdk_threads_leave(); + /* emit signal from the main context, i.e. the main thread */ + g_main_context_invoke(NULL, emit_completed_signal, ecs); } cairo_surface_destroy(surface); From 9e77b26a3d5d31be6de683883fde0bf5d82f6415 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Fri, 30 Aug 2013 13:25:59 +0200 Subject: [PATCH 05/13] Don't loop Signed-off-by: Sebastian Ramacher --- render.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/render.c b/render.c index 91e154e..e4b13e5 100644 --- a/render.c +++ b/render.c @@ -420,7 +420,7 @@ emit_completed_signal(void* data) /* clean up the data */ cairo_surface_destroy(ecs->surface); g_free(ecs); - return true; + return FALSE; } static bool From 08a87f679e0be9ed780f7be12d732783c02f64b9 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Fri, 30 Aug 2013 13:52:58 +0200 Subject: [PATCH 06/13] Mark the request as done in the signal emitter Signed-off-by: Sebastian Ramacher --- render.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/render.c b/render.c index e4b13e5..3faf11a 100644 --- a/render.c +++ b/render.c @@ -349,10 +349,8 @@ render_job(void* data, void* user_data) if (render(request, renderer) != true) { girara_error("Rendering failed (page %d)\n", zathura_page_get_index(request_priv->page) + 1); } - request_priv->requested = false; } - static void color2double(const GdkColor* col, double* v) { @@ -407,6 +405,7 @@ colorumax(const double* h, double l, double l1, double l2) typedef struct emit_completed_signal_s { + ZathuraRenderer* renderer; ZathuraRenderRequest* request; cairo_surface_t* surface; } emit_completed_signal_t; @@ -415,11 +414,22 @@ static gboolean emit_completed_signal(void* data) { emit_completed_signal_t* ecs = data; - /* emit the signal */ - g_signal_emit(ecs->request, request_signals[REQUEST_COMPLETED], 0, ecs->surface); + private_t* priv = GET_PRIVATE(ecs->renderer); + request_private_t* request_priv = REQUEST_GET_PRIVATE(ecs->request); + + if (priv->about_to_close == false && request_priv->aborted == false) { + /* emit the signal */ + g_signal_emit(ecs->request, request_signals[REQUEST_COMPLETED], 0, ecs->surface); + } + /* mark the request as done */ + request_priv->requested = false; + /* clean up the data */ cairo_surface_destroy(ecs->surface); + g_object_unref(ecs->renderer); + g_object_unref(ecs->request); g_free(ecs); + return FALSE; } @@ -550,15 +560,16 @@ render(ZathuraRenderRequest* request, ZathuraRenderer* renderer) #undef rgb2 } - if (priv->about_to_close == false && request_priv->aborted == false) { - emit_completed_signal_t* ecs = g_malloc(sizeof(ecs)); - ecs->request = request; - ecs->surface = surface; - cairo_surface_reference(surface); + emit_completed_signal_t* ecs = g_malloc(sizeof(ecs)); + ecs->renderer = renderer; + ecs->request = request; + ecs->surface = surface; + g_object_ref(renderer); + g_object_ref(request); + cairo_surface_reference(surface); - /* emit signal from the main context, i.e. the main thread */ - g_main_context_invoke(NULL, emit_completed_signal, ecs); - } + /* emit signal from the main context, i.e. the main thread */ + g_main_context_invoke(NULL, emit_completed_signal, ecs); cairo_surface_destroy(surface); From 7d5863ae1d9087f074cdd375f270ad99442a9693 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Fri, 30 Aug 2013 15:05:00 +0200 Subject: [PATCH 07/13] Clean up includes Signed-off-by: Sebastian Ramacher --- render.c | 16 +++++++++------- render.h | 2 ++ zathura.c | 1 + 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/render.c b/render.c index 3faf11a..d0acdab 100644 --- a/render.c +++ b/render.c @@ -3,8 +3,6 @@ #include #include #include -#include -#include #include "glib-compat.h" #include "render.h" #include "zathura.h" @@ -264,13 +262,11 @@ zathura_renderer_get_recolor_colors(ZathuraRenderer* renderer, light->red = priv->recolor.light_gdk.red; light->blue = priv->recolor.light_gdk.blue; light->green = priv->recolor.light_gdk.green; - color2double(light, priv->recolor.light); } if (dark != NULL) { dark->red = priv->recolor.dark_gdk.red; dark->blue = priv->recolor.dark_gdk.blue; dark->green = priv->recolor.dark_gdk.green; - color2double(dark, priv->recolor.dark); } } @@ -504,6 +500,12 @@ render(ZathuraRenderRequest* request, ZathuraRenderer* renderer) 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]); + const double rgb_diff[] = { + rgb2[0] - rgb1[0], + rgb2[1] - rgb1[1], + rgb2[2] - rgb1[2] + }; + for (unsigned int y = 0; y < page_height; y++) { unsigned char* data = image + y * rowstride; @@ -549,9 +551,9 @@ render(ZathuraRenderRequest* request, ZathuraRenderer* renderer) } else { /* 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[2] = (unsigned char)round(255.*(t * rgb_diff[0] + rgb1[0])); + data[1] = (unsigned char)round(255.*(t * rgb_diff[1] + rgb1[1])); + data[0] = (unsigned char)round(255.*(t * rgb_diff[2] + rgb1[2])); } } } diff --git a/render.h b/render.h index 019c485..a781cd8 100644 --- a/render.h +++ b/render.h @@ -5,6 +5,8 @@ #include #include +#include +#include #include #include "types.h" diff --git a/zathura.c b/zathura.c index ce2650a..d639a38 100644 --- a/zathura.c +++ b/zathura.c @@ -717,6 +717,7 @@ document_open(zathura_t* zathura, const char* path, const char* password, goto error_free; } + /* set up recolor info in ZathuraRenderer */ char* recolor_dark = NULL; char* recolor_light = NULL; girara_setting_get(zathura->ui.session, "recolor-darkcolor", &recolor_dark); From 8a9bd7f512818d07eccf3a0d8861a76e3d4b7e8a Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Fri, 30 Aug 2013 15:43:43 +0200 Subject: [PATCH 08/13] We don't need zathura Signed-off-by: Sebastian Ramacher --- page-widget.c | 2 +- render.c | 9 +++++---- render.h | 2 +- zathura.c | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/page-widget.c b/page-widget.c index 5669afb..775c6d8 100644 --- a/page-widget.c +++ b/page-widget.c @@ -299,7 +299,7 @@ zathura_page_widget_set_property(GObject* object, guint prop_id, const GValue* v */ if (priv->search.list != NULL && zathura_page_get_visibility(priv->page)) { - gtk_widget_queue_draw(GTK_WIDGET(object)); + gtk_widget_queue_draw(GTK_WIDGET(object)); } break; default: diff --git a/render.c b/render.c index d0acdab..99644e6 100644 --- a/render.c +++ b/render.c @@ -87,11 +87,10 @@ zathura_renderer_init(ZathuraRenderer* renderer) } ZathuraRenderer* -zathura_renderer_new(zathura_t* zathura) +zathura_renderer_new() { - g_return_val_if_fail(zathura != NULL, NULL); - - return g_object_new(ZATHURA_TYPE_RENDERER, /*"page", page, "zathura", zathura, */ NULL); + GObject* obj = g_object_new(ZATHURA_TYPE_RENDERER, NULL); + return ZATHURA_RENDERER(obj); } static void @@ -325,6 +324,8 @@ zathura_render_request_abort(ZathuraRenderRequest* request) } +/* render job */ + static void render_job(void* data, void* user_data) { diff --git a/render.h b/render.h index a781cd8..f8b7e75 100644 --- a/render.h +++ b/render.h @@ -46,7 +46,7 @@ GType zathura_renderer_get_type(void); * @param page the page to be displayed * @return a page view widget */ -ZathuraRenderer* zathura_renderer_new(zathura_t* zathura); +ZathuraRenderer* zathura_renderer_new(void); bool zathura_renderer_recolor_enabled(ZathuraRenderer* renderer); void zathura_renderer_enable_recolor(ZathuraRenderer* renderer, bool enable); diff --git a/zathura.c b/zathura.c index d639a38..19d4a20 100644 --- a/zathura.c +++ b/zathura.c @@ -711,7 +711,7 @@ document_open(zathura_t* zathura, const char* path, const char* password, zathura->document = document; /* threads */ - zathura->sync.render_thread = zathura_renderer_new(zathura); + zathura->sync.render_thread = zathura_renderer_new(); if (zathura->sync.render_thread == NULL) { goto error_free; From 8d53833d815ea5baa67ca84f239f72f76b042ba1 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Fri, 30 Aug 2013 16:10:44 +0200 Subject: [PATCH 09/13] Move recolor code into separate function Signed-off-by: Sebastian Ramacher --- render.c | 158 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 81 insertions(+), 77 deletions(-) diff --git a/render.c b/render.c index 99644e6..e4d87a9 100644 --- a/render.c +++ b/render.c @@ -430,6 +430,86 @@ emit_completed_signal(void* data) return FALSE; } +static void +recolor(private_t* priv, unsigned int page_width, unsigned int page_height, + cairo_surface_t* surface) +{ + /* uses a representation of a rgb color as follows: + - a lightness scalar (between 0,1), which is a weighted average of r, g, b, + - 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. + */ + + 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 */ + static const double a[] = {0.30, 0.59, 0.11}; + +#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]); + + const double rgb_diff[] = { + rgb2[0] - rgb1[0], + rgb2[1] - rgb1[1], + rgb2[2] - rgb1[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++, data += 4) { + /* Careful. data color components blue, green, red. */ + const double rgb[3] = { + (double) data[2] / 256., + (double) data[1] / 256., + (double) data[0] / 256. + }; + + /* compute h, s, l data */ + double l = a[0]*rgb[0] + a[1]*rgb[1] + a[2]*rgb[2]; + + 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. */ + 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 */ + double u = colorumax(h, l, 0, 1); + double s = 0; + if (u != 0) { + s = 1/u; + } + + /* Interpolates lightness between light and dark colors. white goes to + * light, and black goes to dark. */ + l = l * (l2 - l1) + l1; + 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 */ + data[2] = (unsigned char)round(255.*(l * rgb_diff[0] + rgb1[0])); + data[1] = (unsigned char)round(255.*(l * rgb_diff[1] + rgb1[1])); + data[0] = (unsigned char)round(255.*(l * rgb_diff[2] + rgb1[2])); + } + } + } + +#undef rgb1 +#undef rgb2 +} + static bool render(ZathuraRenderRequest* request, ZathuraRenderer* renderer) { @@ -482,85 +562,9 @@ render(ZathuraRenderRequest* request, ZathuraRenderer* renderer) return true; } - /* recolor */ - /* uses a representation of a rgb color as follows: - - a lightness scalar (between 0,1), which is a weighted average of r, g, b, - - 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 (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 */ - static const double a[] = {0.30, 0.59, 0.11}; - -#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]); - - const double rgb_diff[] = { - rgb2[0] - rgb1[0], - rgb2[1] - rgb1[1], - rgb2[2] - rgb1[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++, data += 4) { - /* Careful. data color components blue, green, red. */ - const double rgb[3] = { - (double) data[2] / 256., - (double) data[1] / 256., - (double) data[0] / 256. - }; - - /* compute h, s, l data */ - double l = a[0]*rgb[0] + a[1]*rgb[1] + a[2]*rgb[2]; - - 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 */ - 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. */ - const double t = l; - l = t * (l2 - l1) + l1; - - 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 */ - data[2] = (unsigned char)round(255.*(t * rgb_diff[0] + rgb1[0])); - data[1] = (unsigned char)round(255.*(t * rgb_diff[1] + rgb1[1])); - data[0] = (unsigned char)round(255.*(t * rgb_diff[2] + rgb1[2])); - } - } - } - -#undef rgb1 -#undef rgb2 + recolor(priv, page_width, page_height, surface); } emit_completed_signal_t* ecs = g_malloc(sizeof(ecs)); From 07005886f8f8323446da63f2fd3daf5c00af2a85 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Fri, 30 Aug 2013 17:50:34 +0200 Subject: [PATCH 10/13] Sort requests based on last view time again Signed-off-by: Sebastian Ramacher --- page-widget.c | 2 +- render.c | 104 +++++++++++++++++++++++++------------------------- render.h | 66 +++++++++++++++++++++++++------- 3 files changed, 106 insertions(+), 66 deletions(-) diff --git a/page-widget.c b/page-widget.c index 775c6d8..fea6810 100644 --- a/page-widget.c +++ b/page-widget.c @@ -506,7 +506,7 @@ zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo) /* render real page */ if (priv->render_requested == false) { priv->render_requested = true; - zathura_render_request(priv->render_request); + zathura_render_request(priv->render_request, priv->last_view); } } mutex_unlock(&(priv->lock)); diff --git a/render.c b/render.c index e4d87a9..9c7a4ac 100644 --- a/render.c +++ b/render.c @@ -21,7 +21,6 @@ static void zathura_renderer_finalize(GObject* object); 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(const GdkColor* col, double* v); @@ -50,6 +49,7 @@ typedef struct request_private_s { zathura_page_t* page; bool requested; bool aborted; + gint64 last_view_time; } request_private_t; #define GET_PRIVATE(obj) \ @@ -298,7 +298,7 @@ zathura_renderer_stop(ZathuraRenderer* renderer) /* ZathuraRenderRequest methods */ void -zathura_render_request(ZathuraRenderRequest* request) +zathura_render_request(ZathuraRenderRequest* request, gint64 last_view_time) { g_return_if_fail(ZATHURA_IS_RENDER_REQUEST(request)); @@ -307,6 +307,7 @@ zathura_render_request(ZathuraRenderRequest* request) if (request_priv->requested == false) { request_priv->requested = true; request_priv->aborted = false; + request_priv->last_view_time = last_view_time; g_thread_pool_push(priv->pool, request, NULL); } @@ -326,26 +327,34 @@ zathura_render_request_abort(ZathuraRenderRequest* request) /* render job */ -static void -render_job(void* data, void* user_data) +typedef struct emit_completed_signal_s { - ZathuraRenderRequest* request = data; - ZathuraRenderer* renderer = user_data; - g_return_if_fail(ZATHURA_IS_RENDER_REQUEST(request)); - g_return_if_fail(ZATHURA_IS_RENDERER(renderer)); + ZathuraRenderer* renderer; + ZathuraRenderRequest* request; + cairo_surface_t* surface; +} emit_completed_signal_t; - 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; - } +static gboolean +emit_completed_signal(void* data) +{ + emit_completed_signal_t* ecs = data; + private_t* priv = GET_PRIVATE(ecs->renderer); + request_private_t* request_priv = REQUEST_GET_PRIVATE(ecs->request); - 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); + if (priv->about_to_close == false && request_priv->aborted == false) { + /* emit the signal */ + g_signal_emit(ecs->request, request_signals[REQUEST_COMPLETED], 0, ecs->surface); } + /* mark the request as done */ + request_priv->requested = false; + + /* clean up the data */ + cairo_surface_destroy(ecs->surface); + g_object_unref(ecs->renderer); + g_object_unref(ecs->request); + g_free(ecs); + + return FALSE; } static void @@ -400,36 +409,6 @@ colorumax(const double* h, double l, double l1, double l2) return fmin(u, v); } -typedef struct emit_completed_signal_s -{ - ZathuraRenderer* renderer; - ZathuraRenderRequest* request; - cairo_surface_t* surface; -} emit_completed_signal_t; - -static gboolean -emit_completed_signal(void* data) -{ - emit_completed_signal_t* ecs = data; - private_t* priv = GET_PRIVATE(ecs->renderer); - request_private_t* request_priv = REQUEST_GET_PRIVATE(ecs->request); - - if (priv->about_to_close == false && request_priv->aborted == false) { - /* emit the signal */ - g_signal_emit(ecs->request, request_signals[REQUEST_COMPLETED], 0, ecs->surface); - } - /* mark the request as done */ - request_priv->requested = false; - - /* clean up the data */ - cairo_surface_destroy(ecs->surface); - g_object_unref(ecs->renderer); - g_object_unref(ecs->request); - g_free(ecs); - - return FALSE; -} - static void recolor(private_t* priv, unsigned int page_width, unsigned int page_height, cairo_surface_t* surface) @@ -583,6 +562,29 @@ render(ZathuraRenderRequest* request, ZathuraRenderer* renderer) return true; } +static void +render_job(void* data, void* user_data) +{ + 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(request_priv->page) + 1); + if (render(request, renderer) != true) { + girara_error("Rendering failed (page %d)\n", zathura_page_get_index(request_priv->page) + 1); + } +} + + void render_all(zathura_t* zathura) { @@ -615,10 +617,8 @@ render_thread_sort(gconstpointer a, gconstpointer b, gpointer UNUSED(data)) 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 priv_a->last_view_time < priv_b->last_view_time ? -1 : + (priv_a->last_view_time > priv_b->last_view_time ? 1 : 0); } /* sort aborted entries earlier so that the are thrown out of the queue */ diff --git a/render.h b/render.h index f8b7e75..00d1e07 100644 --- a/render.h +++ b/render.h @@ -41,44 +41,82 @@ struct zathura_renderer_class_s */ 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 + * Create a renderer. + * @return a renderer object */ ZathuraRenderer* zathura_renderer_new(void); +/** + * Return whether recoloring is enabled. + * @param renderer a renderer object + * @returns true if recoloring is enabled, false otherwise + */ bool zathura_renderer_recolor_enabled(ZathuraRenderer* renderer); +/** + * Enable/disable recoloring. + * @param renderer a renderer object + * @param enable wheter to enable or disable recoloring + */ void zathura_renderer_enable_recolor(ZathuraRenderer* renderer, bool enable); +/** + * Return whether hue should be preserved while recoloring. + * @param renderer a renderer object + * @returns true if hue should be preserved, false otherwise + */ bool zathura_renderer_recolor_hue_enabled(ZathuraRenderer* renderer); +/** + * Enable/disable preservation of hue while recoloring. + * @param renderer a renderer object + * @param enable wheter to enable or disable hue preservation + */ void zathura_renderer_enable_recolor_hue(ZathuraRenderer* renderer, bool enable); +/** + * Set light and dark colors for recoloring. + * @param renderer a renderer object + * @param light light color + * @param dark dark color + */ void zathura_renderer_set_recolor_colors(ZathuraRenderer* renderer, const GdkColor* light, const GdkColor* dark); +/** + * Set light and dark colors for recoloring. + * @param renderer a renderer object + * @param light light color + * @param dark dark color + */ void zathura_renderer_set_recolor_colors_str(ZathuraRenderer* renderer, const char* light, const char* dark); +/** + * Get light and dark colors for recoloring. + * @param renderer a renderer object + * @param light light color + * @param dark dark color + */ void zathura_renderer_get_recolor_colors(ZathuraRenderer* renderer, GdkColor* light, GdkColor* dark); - +/** + * Stop rendering. + * @param renderer a render object + */ void zathura_renderer_stop(ZathuraRenderer* renderer); /** * 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 renderer renderer object */ void zathura_renderer_lock(ZathuraRenderer* renderer); /** * Unlock the render thread. * - * @param render_thread The render thread object. + * @param renderer renderer object. */ void zathura_renderer_unlock(ZathuraRenderer* renderer); - typedef struct zathura_render_request_s ZathuraRenderRequest; typedef struct zathura_render_request_class_s ZathuraRenderRequestClass; @@ -109,15 +147,15 @@ struct zathura_render_request_class_s ZathuraRenderRequestClass)) /** - * Returns the type of the renderer. + * Returns the type of the render request. * @return the type */ GType zathura_page_render_info_get_type(void); /** - * Create a page view widget. - * @param zathura the zathura instance + * Create a render request object + * @param renderer a renderer object * @param page the page to be displayed - * @return a page view widget + * @returns render request object */ ZathuraRenderRequest* zathura_render_request_new(ZathuraRenderer* renderer, zathura_page_t* page); @@ -127,8 +165,10 @@ ZathuraRenderRequest* zathura_render_request_new(ZathuraRenderer* renderer, * that should be rendered. * * @param request request object of the page that should be renderer + * @param last_view_time last view time of the page */ -void zathura_render_request(ZathuraRenderRequest* request); +void zathura_render_request(ZathuraRenderRequest* request, + gint64 last_view_time); /** * Abort an existing render request. From a9f044c951424bc50fb56d47436e1525d0358595 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Fri, 30 Aug 2013 18:55:42 +0200 Subject: [PATCH 11/13] Set recolor info initially Signed-off-by: Sebastian Ramacher --- zathura.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/zathura.c b/zathura.c index 19d4a20..eb5635f 100644 --- a/zathura.c +++ b/zathura.c @@ -727,6 +727,12 @@ document_open(zathura_t* zathura, const char* path, const char* password, g_free(recolor_dark); g_free(recolor_light); + bool recolor = false; + girara_setting_get(zathura->ui.session, "recolor", &recolor); + zathura_renderer_enable_recolor(zathura->sync.render_thread, recolor); + girara_setting_get(zathura->ui.session, "recolor-keephue", &recolor); + zathura_renderer_enable_recolor_hue(zathura->sync.render_thread, recolor); + /* create blank pages */ zathura->pages = calloc(number_of_pages, sizeof(GtkWidget*)); if (zathura->pages == NULL) { From 16fbefa5cfdd905e77764391683f11757c61eee7 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Fri, 30 Aug 2013 18:56:15 +0200 Subject: [PATCH 12/13] Abort requests if the page becomes invisible Signed-off-by: Sebastian Ramacher --- callbacks.c | 12 ++++++++---- page-widget.c | 20 +++++++++++--------- page-widget.h | 7 +++++++ render.c | 2 ++ render.h | 1 - 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/callbacks.c b/callbacks.c index 2ce408b..31364ec 100644 --- a/callbacks.c +++ b/callbacks.c @@ -90,13 +90,14 @@ cb_view_vadjustment_value_changed(GtkAdjustment* GIRARA_UNUSED(adjustment), gpoi .height = zathura_page_get_height(page) * scale }; GtkWidget* page_widget = zathura_page_get_widget(zathura, page); + ZathuraPage* zathura_page_widget = ZATHURA_PAGE(page_widget); gtk_widget_translate_coordinates(page_widget, zathura->ui.session->gtk.view, 0, 0, &page_rect.x, &page_rect.y); if (gdk_rectangle_intersect(&view_rect, &page_rect, NULL) == TRUE) { if (zathura_page_get_visibility(page) == false) { zathura_page_set_visibility(page, true); - zathura_page_widget_update_view_time(ZATHURA_PAGE(page_widget)); + zathura_page_widget_update_view_time(zathura_page_widget); zathura_page_cache_add(zathura, zathura_page_get_index(page)); } if (zathura->global.update_page_number == true && updated == false @@ -106,11 +107,13 @@ cb_view_vadjustment_value_changed(GtkAdjustment* GIRARA_UNUSED(adjustment), gpoi } } else { zathura_page_set_visibility(page, false); + /* If a page becomes invisible, abort all render requests. */ + zathura_page_widget_abort_render_request(zathura_page_widget); /* if the page is not visible and not cached, but still has a surface, we * need to get rid of the surface */ - if (zathura_page_widget_have_surface(ZATHURA_PAGE(page_widget)) == true && + if (zathura_page_widget_have_surface(zathura_page_widget) == true && zathura_page_cache_is_cached(zathura, zathura_page_get_index(page)) == false) { - zathura_page_widget_update_surface(ZATHURA_PAGE(page_widget), NULL); + zathura_page_widget_update_surface(zathura_page_widget, NULL); } girara_list_t* results = NULL; @@ -529,7 +532,8 @@ cb_unknown_command(girara_session_t* session, const char* input) } /* check for number */ - for (unsigned int i = 0; i < strlen(input); i++) { + const size_t size = strlen(input); + for (size_t i = 0; i < size; i++) { if (g_ascii_isdigit(input[i]) == FALSE) { return false; } diff --git a/page-widget.c b/page-widget.c index fea6810..5f5cd32 100644 --- a/page-widget.c +++ b/page-widget.c @@ -24,7 +24,6 @@ typedef struct zathura_page_widget_private_s { 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 */ @@ -146,7 +145,6 @@ zathura_page_widget_init(ZathuraPage* widget) zathura_page_widget_private_t* priv = ZATHURA_PAGE_GET_PRIVATE(widget); priv->page = NULL; priv->surface = NULL; - priv->render_requested = false; priv->render_request = NULL; priv->last_view = g_get_real_time(); @@ -504,10 +502,7 @@ zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo) } /* render real page */ - if (priv->render_requested == false) { - priv->render_requested = true; - zathura_render_request(priv->render_request, priv->last_view); - } + zathura_render_request(priv->render_request, priv->last_view); } mutex_unlock(&(priv->lock)); return FALSE; @@ -529,11 +524,10 @@ zathura_page_widget_update_surface(ZathuraPage* widget, cairo_surface_t* surface cairo_surface_destroy(priv->surface); priv->surface = NULL; } - priv->render_requested = false; if (surface != NULL) { /* if we're not visible or not cached, we don't care about the surface */ - if (zathura_page_get_visibility(priv->page) == true || - zathura_page_cache_is_cached(priv->zathura, zathura_page_get_index(priv->page)) == true) { + /*if (zathura_page_get_visibility(priv->page) == true || + zathura_page_cache_is_cached(priv->zathura, zathura_page_get_index(priv->page)) == true) */ { priv->surface = surface; cairo_surface_reference(surface); } @@ -919,3 +913,11 @@ zathura_page_widget_have_surface(ZathuraPage* widget) return priv->surface != NULL; } +void +zathura_page_widget_abort_render_request(ZathuraPage* widget) +{ + g_return_if_fail(ZATHURA_IS_PAGE(widget)); + zathura_page_widget_private_t* priv = ZATHURA_PAGE_GET_PRIVATE(widget); + zathura_render_request_abort(priv->render_request); +} + diff --git a/page-widget.h b/page-widget.h index b9a1c38..08944d1 100644 --- a/page-widget.h +++ b/page-widget.h @@ -96,4 +96,11 @@ void zathura_page_widget_update_view_time(ZathuraPage* widget); */ bool zathura_page_widget_have_surface(ZathuraPage* widget); +/** + * Abort outstanding render requests + * + * @param widget the widget + */ +void zathura_page_widget_abort_render_request(ZathuraPage* widget); + #endif diff --git a/render.c b/render.c index 9c7a4ac..050cc4f 100644 --- a/render.c +++ b/render.c @@ -537,6 +537,7 @@ render(ZathuraRenderRequest* request, ZathuraRenderer* renderer) /* before recoloring, check if we've been aborted */ if (priv->about_to_close == true || request_priv->aborted == true) { + request_priv->requested = false; cairo_surface_destroy(surface); return true; } @@ -581,6 +582,7 @@ render_job(void* data, void* user_data) 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; } } diff --git a/render.h b/render.h index 00d1e07..0963a9e 100644 --- a/render.h +++ b/render.h @@ -177,7 +177,6 @@ void zathura_render_request(ZathuraRenderRequest* request, */ void zathura_render_request_abort(ZathuraRenderRequest* request); - /** * This function is used to unmark all pages as not rendered. This should * be used if all pages should be rendered again (e.g.: the zoom level or the From 7127a46033f3d49eb08561cd30040a51c1114d0d Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Fri, 30 Aug 2013 19:25:24 +0200 Subject: [PATCH 13/13] Fix deprecated gdk thread function calls Signed-off-by: Sebastian Ramacher --- callbacks.c | 13 +++++++++---- main.c | 8 +++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/callbacks.c b/callbacks.c index 31364ec..14a629b 100644 --- a/callbacks.c +++ b/callbacks.c @@ -107,7 +107,7 @@ cb_view_vadjustment_value_changed(GtkAdjustment* GIRARA_UNUSED(adjustment), gpoi } } else { zathura_page_set_visibility(page, false); - /* If a page becomes invisible, abort all render requests. */ + /* If a page becomes invisible, abort the render request. */ zathura_page_widget_abort_render_request(zathura_page_widget); /* if the page is not visible and not cached, but still has a surface, we * need to get rid of the surface */ @@ -370,6 +370,13 @@ cb_sc_display_link(GtkEntry* entry, girara_session_t* session) return handle_link(entry, session, ZATHURA_LINK_ACTION_DISPLAY); } +static gboolean +file_monitor_reload(void* data) +{ + sc_reload((girara_session_t*) data, NULL, NULL, 0); + return FALSE; +} + void cb_file_monitor(GFileMonitor* monitor, GFile* file, GFile* UNUSED(other_file), GFileMonitorEvent event, girara_session_t* session) { @@ -380,9 +387,7 @@ cb_file_monitor(GFileMonitor* monitor, GFile* file, GFile* UNUSED(other_file), G switch (event) { case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT: case G_FILE_MONITOR_EVENT_CREATED: - gdk_threads_enter(); - sc_reload(session, NULL, NULL, 0); - gdk_threads_leave(); + g_main_context_invoke(NULL, file_monitor_reload, session); break; default: return; diff --git a/main.c b/main.c index 26d561a..692aa67 100644 --- a/main.c +++ b/main.c @@ -25,7 +25,9 @@ main(int argc, char* argv[]) #if !GLIB_CHECK_VERSION(2, 31, 0) g_thread_init(NULL); #endif +#if !GTK_CHECK_VERSION(3, 6, 0) gdk_threads_init(); +#endif gtk_init(&argc, &argv); /* create zathura session */ @@ -46,7 +48,7 @@ main(int argc, char* argv[]) bool synctex = false; int page_number = ZATHURA_PAGE_NUMBER_UNSPECIFIED; -#if (GTK_MAJOR_VERSION == 3) +#if GTK_CHECK_VERSION(3, 0, 0) Window embed = 0; #else GdkNativeWindow embed = 0; @@ -148,9 +150,13 @@ main(int argc, char* argv[]) } /* run zathura */ +#if !GTK_CHECK_VERSION(3, 6, 0) gdk_threads_enter(); +#endif gtk_main(); +#if !GTK_CHECK_VERSION(3, 6, 0) gdk_threads_leave(); +#endif /* free zathura */ zathura_free(zathura);