Commit 10115db6 authored by Sebastian Ramacher's avatar Sebastian Ramacher
Browse files

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's avatarSebastian Ramacher <sebastian+dev@ramacher.at>
parent 0afc0e53
...@@ -381,9 +381,9 @@ cmd_search(girara_session_t* session, const char* input, girara_argument_t* argu ...@@ -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); GtkWidget* page_widget = zathura_page_get_widget(zathura, page);
g_object_set(page_widget, "draw-links", FALSE, NULL); 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); 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) { if (result == NULL || girara_list_size(result) == 0) {
girara_list_free(result); girara_list_free(result);
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "utils.h" #include "utils.h"
#include "shortcuts.h" #include "shortcuts.h"
#include "synctex.h" #include "synctex.h"
#include "zathura.h"
G_DEFINE_TYPE(ZathuraPage, zathura_page_widget, GTK_TYPE_DRAWING_AREA) G_DEFINE_TYPE(ZathuraPage, zathura_page_widget, GTK_TYPE_DRAWING_AREA)
...@@ -22,6 +23,7 @@ typedef struct zathura_page_widget_private_s { ...@@ -22,6 +23,7 @@ typedef struct zathura_page_widget_private_s {
zathura_page_t* page; /**< Page object */ zathura_page_t* page; /**< Page object */
zathura_t* zathura; /**< Zathura object */ zathura_t* zathura; /**< Zathura object */
cairo_surface_t* surface; /**< Cairo surface */ cairo_surface_t* surface; /**< Cairo surface */
ZathuraRenderRequest* render_request; /* Request object */
bool render_requested; /**< No surface and rendering has been requested */ bool render_requested; /**< No surface and rendering has been requested */
gint64 last_view; /**< Last time the page has been viewed */ gint64 last_view; /**< Last time the page has been viewed */
mutex lock; /**< Lock */ mutex lock; /**< Lock */
...@@ -144,6 +146,7 @@ zathura_page_widget_init(ZathuraPage* widget) ...@@ -144,6 +146,7 @@ zathura_page_widget_init(ZathuraPage* widget)
priv->page = NULL; priv->page = NULL;
priv->surface = NULL; priv->surface = NULL;
priv->render_requested = false; priv->render_requested = false;
priv->render_request = NULL;
priv->last_view = g_get_real_time(); priv->last_view = g_get_real_time();
priv->links.list = NULL; priv->links.list = NULL;
...@@ -177,7 +180,18 @@ zathura_page_widget_new(zathura_t* zathura, zathura_page_t* page) ...@@ -177,7 +180,18 @@ zathura_page_widget_new(zathura_t* zathura, zathura_page_t* page)
{ {
g_return_val_if_fail(page != NULL, NULL); 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 static void
...@@ -190,6 +204,10 @@ zathura_page_widget_finalize(GObject* object) ...@@ -190,6 +204,10 @@ zathura_page_widget_finalize(GObject* object)
cairo_surface_destroy(priv->surface); cairo_surface_destroy(priv->surface);
} }
if (priv->render_request != NULL) {
g_object_unref(priv->render_request);
}
if (priv->search.list != NULL) { if (priv->search.list != NULL) {
girara_list_free(priv->search.list); girara_list_free(priv->search.list);
} }
...@@ -485,7 +503,7 @@ zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo) ...@@ -485,7 +503,7 @@ zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo)
/* render real page */ /* render real page */
if (priv->render_requested == false) { if (priv->render_requested == false) {
priv->render_requested = true; priv->render_requested = true;
render_page(priv->zathura->sync.render_thread, priv->page); zathura_render_request(priv->render_request);
} }
} }
mutex_unlock(&(priv->lock)); mutex_unlock(&(priv->lock));
......
...@@ -109,9 +109,9 @@ cb_print_draw_page(GtkPrintOperation* print_operation, GtkPrintContext* ...@@ -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 /* Try to render the page without a temporary surface. This only works with
* plugins that support rendering to any surface. */ * plugins that support rendering to any surface. */
girara_debug("printing page %d ...", page_number); 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); 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) { if (err == ZATHURA_ERROR_OK) {
return; return;
} }
...@@ -144,9 +144,9 @@ cb_print_draw_page(GtkPrintOperation* print_operation, GtkPrintContext* ...@@ -144,9 +144,9 @@ cb_print_draw_page(GtkPrintOperation* print_operation, GtkPrintContext*
/* Render the page to the temporary surface */ /* Render the page to the temporary surface */
girara_debug("printing page %d ...", page_number); 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); 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) { if (err != ZATHURA_ERROR_OK) {
cairo_destroy(temp_cairo); cairo_destroy(temp_cairo);
cairo_surface_destroy(surface); cairo_surface_destroy(surface);
......
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
#include <girara/utils.h> #include <girara/utils.h>
#include <girara/session.h> #include <girara/session.h>
#include <girara/settings.h> #include <girara/settings.h>
#include "glib-compat.h" #include "glib-compat.h"
#include "render.h" #include "render.h"
#include "zathura.h" #include "zathura.h"
...@@ -14,81 +13,299 @@ ...@@ -14,81 +13,299 @@
#include "page-widget.h" #include "page-widget.h"
#include "utils.h" #include "utils.h"
/* define the two types */
G_DEFINE_TYPE(ZathuraRenderer, zathura_renderer, G_TYPE_OBJECT)
G_DEFINE_TYPE(ZathuraRenderRequest, zathura_render_request, G_TYPE_OBJECT)
/* 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 void render_job(void* data, void* user_data);
static bool render(zathura_t* zathura, zathura_page_t* page); static bool render(ZathuraRenderRequest* request, ZathuraRenderer* renderer);
static gint render_thread_sort(gconstpointer a, gconstpointer b, gpointer data); static gint render_thread_sort(gconstpointer a, gconstpointer b, gpointer data);
static void color2double(GdkColor* col, double* v);
struct render_thread_s { /* private data for ZathuraRenderer */
typedef struct private_s {
GThreadPool* pool; /**< Pool of threads */ GThreadPool* pool; /**< Pool of threads */
mutex mutex; /**< Render lock */ 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 static void
render_job(void* data, void* user_data) zathura_renderer_class_init(ZathuraRendererClass* class)
{ {
zathura_page_t* page = data; /* add private members */
zathura_t* zathura = user_data; g_type_class_add_private(class, sizeof(private_t));
if (page == NULL || zathura == NULL) {
return; /* 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
};
girara_debug("rendering page %d ...", zathura_page_get_index(page) + 1); static guint request_signals[REQUEST_LAST_SIGNAL] = { 0 };
if (render(zathura, page) != true) {
girara_error("Rendering failed (page %d)\n", zathura_page_get_index(page) + 1); 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;
} }
render_thread_t* static void
render_init(zathura_t* zathura) zathura_render_request_finalize(GObject* object)
{ {
render_thread_t* render_thread = g_malloc0(sizeof(render_thread_t)); ZathuraRenderRequest* request = ZATHURA_RENDER_REQUEST(object);
request_private_t* priv = REQUEST_GET_PRIVATE(request);
/* setup */ if (priv->renderer) {
render_thread->pool = g_thread_pool_new(render_job, zathura, 1, TRUE, NULL); g_object_unref(priv->renderer);
if (render_thread->pool == NULL) {
goto error_free;
} }
}
/* renderer methods */
render_thread->about_to_close = false; bool
g_thread_pool_set_sort_function(render_thread->pool, render_thread_sort, zathura); zathura_renderer_recolor_enabled(ZathuraRenderer* renderer)
mutex_init(&render_thread->mutex); {
g_return_val_if_fail(ZATHURA_IS_RENDERER(renderer), false);
return render_thread; return GET_PRIVATE(renderer)->recolor.enabled;
}
error_free: void
zathura_renderer_enable_recolor(ZathuraRenderer* renderer, bool enable)
{
g_return_if_fail(ZATHURA_IS_RENDERER(renderer));
render_free(render_thread); GET_PRIVATE(renderer)->recolor.enabled = enable;
return NULL; }
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 void
render_free(render_thread_t* render_thread) zathura_renderer_enable_recolor_hue(ZathuraRenderer* renderer, bool enable)
{ {
if (render_thread == NULL) { g_return_if_fail(ZATHURA_IS_RENDERER(renderer));
return;
} 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);
}
render_thread->about_to_close = true; void
if (render_thread->pool) { zathura_renderer_unlock(ZathuraRenderer* renderer)
g_thread_pool_free(render_thread->pool, TRUE, TRUE); {
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);
} }
}
mutex_free(&(render_thread->mutex)); void
g_free(render_thread); 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;
}
} }
bool
render_page(render_thread_t* render_thread, zathura_page_t* page) static void
render_job(void* data, void* user_data)
{ {
if (render_thread == NULL || page == NULL || render_thread->pool == NULL || render_thread->about_to_close == true) { ZathuraRenderRequest* request = data;
return false; 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;
} }
g_thread_pool_push(render_thread->pool, page, NULL); girara_debug("Rendering page %d ...", zathura_page_get_index(request_priv->page) + 1);
return true; 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 static void
color2double(GdkColor* col, double* v) color2double(GdkColor* col, double* v)
{ {
...@@ -101,7 +318,7 @@ 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 Assumes that l is in the interval l1, l2 and corrects the value to
force u=0 on l1 and l2 */ force u=0 on l1 and l2 */
static double 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; double u, uu, v, vv, lv;
if (h[0] == 0 && h[1] == 0 && h[2] == 0) { if (h[0] == 0 && h[1] == 0 && h[2] == 0) {
...@@ -143,11 +360,11 @@ colorumax(double* h, double l, double l1, double l2) ...@@ -143,11 +360,11 @@ colorumax(double* h, double l, double l1, double l2)
static bool 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) { private_t* priv = GET_PRIVATE(renderer);
return false; request_private_t* request_priv = REQUEST_GET_PRIVATE(request);
} zathura_page_t* page = request_priv->page;
/* create cairo surface */ /* create cairo surface */
unsigned int page_width = 0; unsigned int page_width = 0;
...@@ -155,13 +372,11 @@ render(zathura_t* zathura, zathura_page_t* page) ...@@ -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); 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); cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, page_width, page_height);
if (surface == NULL) { if (surface == NULL) {
return false; return false;
} }
cairo_t* cairo = cairo_create(surface); cairo_t* cairo = cairo_create(surface);
if (cairo == NULL) { if (cairo == NULL) {
cairo_surface_destroy(surface); cairo_surface_destroy(surface);
return false; return false;
...@@ -178,20 +393,24 @@ render(zathura_t* zathura, zathura_page_t* page) ...@@ -178,20 +393,24 @@ render(zathura_t* zathura, zathura_page_t* page)
cairo_scale(cairo, real_scale, real_scale); 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) { if (zathura_page_render(page, cairo, false) != ZATHURA_ERROR_OK) {
render_unlock(zathura->sync.render_thread); zathura_renderer_unlock(renderer);
cairo_destroy(cairo); cairo_destroy(cairo);
cairo_surface_destroy(surface); cairo_surface_destroy(surface);
return false; return false;
} }
render_unlock(zathura->sync.render_thread); zathura_renderer_unlock(renderer);
cairo_restore(cairo); cairo_restore(cairo);
cairo_destroy(cairo); cairo_destroy(cairo);
const int rowstride = cairo_image_surface_get_stride(surface); /* before recoloring, check if we've been aborted */
unsigned char* image = cairo_image_surface_get_data(surface); if (priv->about_to_close == true || request_priv->aborted == true) {
cairo_surface_destroy(surface);
return true;
}
/* recolor */ /* recolor */
/* uses a representation of a rgb color as follows: /* uses a representation of a rgb color as follows:
...@@ -199,72 +418,84 @@ render(zathura_t* zathura, zathura_page_t* page) ...@@ -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 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. - 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) {
/* RGB weights for computing lightness. Must sum to one */ const int rowstride = cairo_image_surface_get_stride(surface);
double a[] = {0.30, 0.59, 0.11};