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
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);
......
......@@ -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));
......
......@@ -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);
......
......@@ -5,7 +5,6 @@
#include <girara/utils.h>
#include <girara/session.h>
#include <girara/settings.h>
#include "glib-compat.h"
#include "render.h"
#include "zathura.h"
......@@ -14,81 +13,299 @@
#include "page-widget.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 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 void color2double(GdkColor* col, double* v);
struct render_thread_s {
/* 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
render_job(void* data, void* user_data)
zathura_renderer_class_init(ZathuraRendererClass* class)
{
zathura_page_t* page = data;
zathura_t* zathura = user_data;
if (page == NULL || zathura == NULL) {
return;
/* 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
};
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);
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;
}
render_thread_t*
render_init(zathura_t* zathura)
static void
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 */
render_thread->pool = g_thread_pool_new(render_job, zathura, 1, TRUE, NULL);
if (render_thread->pool == NULL) {
goto error_free;
if (priv->renderer) {
g_object_unref(priv->renderer);
}
}
/* renderer methods */
render_thread->about_to_close = false;
g_thread_pool_set_sort_function(render_thread->pool, render_thread_sort, zathura);
mutex_init(&render_thread->mutex);
bool
zathura_renderer_recolor_enabled(ZathuraRenderer* renderer)
{
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);
return NULL;
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
render_free(render_thread_t* render_thread)
zathura_renderer_enable_recolor_hue(ZathuraRenderer* renderer, bool enable)
{
if (render_thread == NULL) {
return;
}
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);
}
render_thread->about_to_close = true;
if (render_thread->pool) {
g_thread_pool_free(render_thread->pool, TRUE, TRUE);
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);
}
}
mutex_free(&(render_thread->mutex));
g_free(render_thread);
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;
}
}
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) {
return false;
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;
}
g_thread_pool_push(render_thread->pool, page, NULL);
return true;
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;
}
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) {
/* RGB weights for computing lightness. Must sum to one */
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];
if (priv->recolor.enabled == true) {
const int rowstride = cairo_image_surface_get_stride(surface);
unsigned char* image = cairo_image_surface_get_data(surface);
color2double(&zathura->ui.colors.recolor_dark_color, rgb1);
color2double(&zathura->ui.colors.recolor_light_color, rgb2);
/* RGB weights for computing lightness. Must sum to one */
static const double a[] = {0.30, 0.59, 0.11};
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];
h[0] = rgb[0] - l;
h[1] = rgb[1] - l;
h[2] = 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);
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. */
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->