2011-09-29 18:51:24 +02:00
|
|
|
/* See LICENSE file for license and copyright information */
|
|
|
|
|
|
|
|
#include <math.h>
|
2011-10-23 17:01:15 +02:00
|
|
|
#include <girara/datastructures.h>
|
|
|
|
#include <girara/utils.h>
|
|
|
|
#include <girara/session.h>
|
2012-02-01 17:30:43 +01:00
|
|
|
#include <girara/settings.h>
|
2011-09-29 18:51:24 +02:00
|
|
|
|
2011-01-07 09:07:02 +01:00
|
|
|
#include "render.h"
|
|
|
|
#include "zathura.h"
|
2011-04-18 18:29:50 +02:00
|
|
|
#include "document.h"
|
2012-03-26 14:44:56 +02:00
|
|
|
#include "page.h"
|
2012-03-16 14:37:54 +01:00
|
|
|
#include "page-widget.h"
|
2012-02-08 22:23:45 +01:00
|
|
|
#include "utils.h"
|
2011-01-07 09:07:02 +01:00
|
|
|
|
2012-03-02 17:51:48 +01:00
|
|
|
static void render_job(void* data, void* user_data);
|
|
|
|
static bool render(zathura_t* zathura, zathura_page_t* page);
|
2012-05-15 07:52:23 +02:00
|
|
|
static gint render_thread_sort(gconstpointer a, gconstpointer b, gpointer data);
|
2011-01-24 12:43:39 +01:00
|
|
|
|
2012-03-02 17:51:48 +01:00
|
|
|
struct render_thread_s
|
2011-01-24 12:43:39 +01:00
|
|
|
{
|
2012-03-02 17:51:48 +01:00
|
|
|
GThreadPool* pool; /**< Pool of threads */
|
2012-06-03 22:06:57 +02:00
|
|
|
GStaticMutex mutex; /**< Render lock */
|
2012-06-20 15:55:32 +02:00
|
|
|
bool about_to_close; /**< Render thread is to be freed */
|
2012-03-02 17:51:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
render_job(void* data, void* user_data)
|
|
|
|
{
|
2012-06-03 21:54:10 +02:00
|
|
|
zathura_page_t* page = data;
|
2012-03-02 17:51:48 +01:00
|
|
|
zathura_t* zathura = user_data;
|
2012-06-03 21:54:10 +02:00
|
|
|
if (page == NULL || zathura == NULL) {
|
2012-03-02 17:51:48 +01:00
|
|
|
return;
|
2011-01-24 12:43:39 +01:00
|
|
|
}
|
|
|
|
|
2012-06-20 16:41:01 +02:00
|
|
|
girara_debug("rendering page %d ...", zathura_page_get_index(page));
|
2012-06-03 21:54:10 +02:00
|
|
|
if (render(zathura, page) != true) {
|
|
|
|
girara_error("Rendering failed (page %d)\n", zathura_page_get_index(page));
|
2012-03-02 17:51:48 +01:00
|
|
|
}
|
2011-01-24 12:43:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
render_thread_t*
|
2011-04-18 18:00:08 +02:00
|
|
|
render_init(zathura_t* zathura)
|
2011-01-24 12:43:39 +01:00
|
|
|
{
|
2011-10-19 13:55:50 +02:00
|
|
|
render_thread_t* render_thread = g_malloc0(sizeof(render_thread_t));
|
2011-01-24 12:43:39 +01:00
|
|
|
|
2011-01-26 12:06:57 +01:00
|
|
|
/* setup */
|
2012-03-02 17:51:48 +01:00
|
|
|
render_thread->pool = g_thread_pool_new(render_job, zathura, 1, TRUE, NULL);
|
|
|
|
if (render_thread->pool == NULL) {
|
2011-04-26 16:39:40 +02:00
|
|
|
goto error_free;
|
|
|
|
}
|
|
|
|
|
2012-06-20 15:55:32 +02:00
|
|
|
render_thread->about_to_close = false;
|
2012-05-15 07:52:23 +02:00
|
|
|
g_thread_pool_set_sort_function(render_thread->pool, render_thread_sort, zathura);
|
2012-06-03 22:06:57 +02:00
|
|
|
g_static_mutex_init(&render_thread->mutex);
|
2012-05-15 07:52:23 +02:00
|
|
|
|
2011-01-24 12:43:39 +01:00
|
|
|
return render_thread;
|
|
|
|
|
|
|
|
error_free:
|
|
|
|
|
2012-03-02 17:51:48 +01:00
|
|
|
render_free(render_thread);
|
2011-01-24 12:43:39 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
render_free(render_thread_t* render_thread)
|
|
|
|
{
|
2012-03-02 17:51:48 +01:00
|
|
|
if (render_thread == NULL) {
|
2011-01-24 12:43:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-20 15:55:32 +02:00
|
|
|
render_thread->about_to_close = true;
|
2012-03-02 17:51:48 +01:00
|
|
|
if (render_thread->pool) {
|
|
|
|
g_thread_pool_free(render_thread->pool, TRUE, TRUE);
|
2011-01-26 12:06:57 +01:00
|
|
|
}
|
2012-03-02 17:34:13 +01:00
|
|
|
|
2012-06-03 22:06:57 +02:00
|
|
|
g_static_mutex_free(&render_thread->mutex);
|
2012-03-02 17:34:13 +01:00
|
|
|
g_free(render_thread);
|
2011-01-24 12:43:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-06-03 21:54:10 +02:00
|
|
|
render_page(render_thread_t* render_thread, zathura_page_t* page)
|
2011-01-24 12:43:39 +01:00
|
|
|
{
|
2012-06-20 15:55:32 +02:00
|
|
|
if (render_thread == NULL || page == NULL || render_thread->pool == NULL || render_thread->about_to_close == true) {
|
2011-01-24 12:43:39 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-03 21:54:10 +02:00
|
|
|
g_thread_pool_push(render_thread->pool, page, NULL);
|
2011-01-24 12:43:39 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-02 17:51:48 +01:00
|
|
|
static bool
|
2011-04-18 18:00:08 +02:00
|
|
|
render(zathura_t* zathura, zathura_page_t* page)
|
2011-01-07 09:07:02 +01:00
|
|
|
{
|
2012-06-20 16:10:34 +02:00
|
|
|
if (zathura == NULL || page == NULL || zathura->sync.render_thread->about_to_close == true) {
|
2011-04-18 21:22:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-29 12:35:52 +02:00
|
|
|
/* create cairo surface */
|
|
|
|
unsigned int page_width = 0;
|
|
|
|
unsigned int page_height = 0;
|
2011-12-10 11:44:27 +01:00
|
|
|
page_calc_height_width(page, &page_height, &page_width, false);
|
2011-09-29 12:35:52 +02:00
|
|
|
|
|
|
|
cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, page_width, page_height);
|
|
|
|
|
|
|
|
if (surface == NULL) {
|
2011-01-07 09:07:02 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-29 12:35:52 +02:00
|
|
|
cairo_t* cairo = cairo_create(surface);
|
2011-03-20 02:09:04 +01:00
|
|
|
|
2011-09-29 12:35:52 +02:00
|
|
|
if (cairo == NULL) {
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
return false;
|
|
|
|
}
|
2011-03-20 02:09:04 +01:00
|
|
|
|
2011-09-29 12:35:52 +02:00
|
|
|
cairo_save(cairo);
|
|
|
|
cairo_set_source_rgb(cairo, 1, 1, 1);
|
|
|
|
cairo_rectangle(cairo, 0, 0, page_width, page_height);
|
|
|
|
cairo_fill(cairo);
|
|
|
|
cairo_restore(cairo);
|
|
|
|
cairo_save(cairo);
|
2011-03-20 02:09:04 +01:00
|
|
|
|
2012-03-30 18:24:00 +02:00
|
|
|
double scale = zathura_document_get_scale(zathura->document);
|
2012-03-27 21:59:35 +02:00
|
|
|
if (fabs(scale - 1.0f) > FLT_EPSILON) {
|
|
|
|
cairo_scale(cairo, scale, scale);
|
2011-09-29 18:49:43 +02:00
|
|
|
}
|
|
|
|
|
2012-06-03 22:06:57 +02:00
|
|
|
render_lock(zathura->sync.render_thread);
|
2012-03-27 13:30:04 +02:00
|
|
|
if (zathura_page_render(page, cairo, false) != ZATHURA_ERROR_OK) {
|
2012-06-03 22:06:57 +02:00
|
|
|
render_unlock(zathura->sync.render_thread);
|
2011-09-29 12:35:52 +02:00
|
|
|
cairo_destroy(cairo);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
return false;
|
2011-03-20 02:09:04 +01:00
|
|
|
}
|
2011-01-07 09:07:02 +01:00
|
|
|
|
2012-06-03 22:06:57 +02:00
|
|
|
render_unlock(zathura->sync.render_thread);
|
2011-09-29 12:35:52 +02:00
|
|
|
cairo_restore(cairo);
|
|
|
|
cairo_destroy(cairo);
|
|
|
|
|
2011-10-09 00:35:52 +02:00
|
|
|
const int rowstride = cairo_image_surface_get_stride(surface);
|
2011-09-29 12:35:52 +02:00
|
|
|
unsigned char* image = cairo_image_surface_get_data(surface);
|
|
|
|
|
2011-04-30 13:27:27 +02:00
|
|
|
/* recolor */
|
2012-03-14 17:33:35 +01:00
|
|
|
if (zathura->global.recolor == true) {
|
2011-04-30 13:27:27 +02:00
|
|
|
/* recolor code based on qimageblitz library flatten() function
|
|
|
|
(http://sourceforge.net/projects/qimageblitz/) */
|
|
|
|
|
|
|
|
int r1 = zathura->ui.colors.recolor_dark_color.red / 257;
|
|
|
|
int g1 = zathura->ui.colors.recolor_dark_color.green / 257;
|
|
|
|
int b1 = zathura->ui.colors.recolor_dark_color.blue / 257;
|
|
|
|
int r2 = zathura->ui.colors.recolor_light_color.red / 257;
|
|
|
|
int g2 = zathura->ui.colors.recolor_light_color.green / 257;
|
|
|
|
int b2 = zathura->ui.colors.recolor_light_color.blue / 257;
|
|
|
|
|
|
|
|
int min = 0x00;
|
|
|
|
int max = 0xFF;
|
|
|
|
int mean = 0x00;
|
|
|
|
|
|
|
|
float sr = ((float) r2 - r1) / (max - min);
|
|
|
|
float sg = ((float) g2 - g1) / (max - min);
|
|
|
|
float sb = ((float) b2 - b1) / (max - min);
|
|
|
|
|
|
|
|
for (unsigned int y = 0; y < page_height; y++) {
|
|
|
|
unsigned char* data = image + y * rowstride;
|
|
|
|
|
|
|
|
for (unsigned int x = 0; x < page_width; x++) {
|
|
|
|
mean = (data[0] + data[1] + data[2]) / 3;
|
|
|
|
data[2] = sr * (mean - min) + r1 + 0.5;
|
|
|
|
data[1] = sg * (mean - min) + g1 + 0.5;
|
|
|
|
data[0] = sb * (mean - min) + b1 + 0.5;
|
|
|
|
data += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-20 15:55:32 +02:00
|
|
|
if (zathura->sync.render_thread->about_to_close == 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();
|
|
|
|
} else {
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
}
|
2011-09-29 12:35:52 +02:00
|
|
|
|
2011-01-07 09:07:02 +01:00
|
|
|
return true;
|
|
|
|
}
|
2011-02-09 21:28:36 +01:00
|
|
|
|
|
|
|
void
|
2011-04-18 18:00:08 +02:00
|
|
|
render_all(zathura_t* zathura)
|
2011-02-09 21:28:36 +01:00
|
|
|
{
|
2012-04-03 09:02:45 +02:00
|
|
|
if (zathura == NULL || zathura->document == NULL) {
|
2011-02-09 21:28:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unmark all pages */
|
2012-03-27 21:59:35 +02:00
|
|
|
unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document);
|
|
|
|
for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) {
|
|
|
|
zathura_page_t* page = zathura_document_get_page(zathura->document, page_id);
|
2011-12-10 10:19:14 +01:00
|
|
|
unsigned int page_height = 0, page_width = 0;
|
2011-12-10 11:44:27 +01:00
|
|
|
page_calc_height_width(page, &page_height, &page_width, true);
|
2011-02-09 21:28:36 +01:00
|
|
|
|
2012-04-03 09:02:45 +02:00
|
|
|
GtkWidget* widget = zathura_page_get_widget(zathura, page);
|
2012-03-26 14:44:56 +02:00
|
|
|
gtk_widget_set_size_request(widget, page_width, page_height);
|
|
|
|
gtk_widget_queue_resize(widget);
|
2011-12-10 10:19:14 +01:00
|
|
|
}
|
2011-04-18 21:22:35 +02:00
|
|
|
}
|
2012-05-15 07:52:23 +02:00
|
|
|
|
|
|
|
static gint
|
|
|
|
render_thread_sort(gconstpointer a, gconstpointer b, gpointer data)
|
|
|
|
{
|
|
|
|
if (a == NULL || b == NULL || data == 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);
|
|
|
|
|
|
|
|
unsigned int last_view_a = 0;
|
|
|
|
unsigned int 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);
|
|
|
|
|
2012-06-16 09:29:15 +02:00
|
|
|
if (last_view_a < last_view_b) {
|
2012-05-15 07:52:23 +02:00
|
|
|
return -1;
|
2012-06-16 09:29:15 +02:00
|
|
|
} else if (last_view_a > last_view_b) {
|
2012-05-15 07:52:23 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-06-03 22:06:57 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
render_lock(render_thread_t* render_thread)
|
|
|
|
{
|
|
|
|
if (render_thread == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_static_mutex_lock(&render_thread->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
render_unlock(render_thread_t* render_thread)
|
|
|
|
{
|
|
|
|
if (render_thread == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_static_mutex_unlock(&render_thread->mutex);
|
|
|
|
}
|