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"
|
2011-01-07 09:07:02 +01:00
|
|
|
|
2011-01-24 13:10:06 +01:00
|
|
|
void* render_job(void* data);
|
2011-04-18 18:29:50 +02:00
|
|
|
bool render(zathura_t* zathura, zathura_page_t* page);
|
2011-01-24 12:43:39 +01:00
|
|
|
|
2011-12-10 10:19:14 +01:00
|
|
|
static void
|
2011-12-10 11:44:27 +01:00
|
|
|
page_calc_height_width(zathura_page_t* page, unsigned int* page_height, unsigned int* page_width, bool rotate)
|
2011-12-10 10:19:14 +01:00
|
|
|
{
|
2011-12-10 11:44:27 +01:00
|
|
|
if (rotate && page->document->rotate % 180) {
|
2011-12-11 01:45:27 +01:00
|
|
|
*page_width = ceil(page->height * page->document->scale);
|
|
|
|
*page_height = ceil(page->width * page->document->scale);
|
2011-12-10 11:44:27 +01:00
|
|
|
} else {
|
2011-12-11 01:45:27 +01:00
|
|
|
*page_width = ceil(page->width * page->document->scale);
|
|
|
|
*page_height = ceil(page->height * page->document->scale);
|
2011-12-10 10:19:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-24 12:43:39 +01:00
|
|
|
void*
|
2011-01-24 13:10:06 +01:00
|
|
|
render_job(void* data)
|
2011-01-24 12:43:39 +01:00
|
|
|
{
|
|
|
|
render_thread_t* render_thread = (render_thread_t*) data;
|
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
while (true) {
|
2011-01-26 12:06:57 +01:00
|
|
|
g_mutex_lock(render_thread->lock);
|
2011-01-24 12:43:39 +01:00
|
|
|
|
2011-10-09 00:35:52 +02:00
|
|
|
if (girara_list_size(render_thread->list) == 0) {
|
2011-01-26 12:06:57 +01:00
|
|
|
g_cond_wait(render_thread->cond, render_thread->lock);
|
2011-01-24 12:43:39 +01:00
|
|
|
}
|
|
|
|
|
2011-10-09 00:35:52 +02:00
|
|
|
if (girara_list_size(render_thread->list) == 0) {
|
2011-07-21 14:39:25 +02:00
|
|
|
/*
|
|
|
|
* We've been signaled with g_cond_signal(), but the list
|
|
|
|
* is still empty. This means that the signal came from
|
|
|
|
* render_free() and current document is being closed.
|
|
|
|
* We should unlock the mutex and kill the thread.
|
|
|
|
*/
|
|
|
|
g_mutex_unlock(render_thread->lock);
|
|
|
|
g_thread_exit(0);
|
|
|
|
}
|
|
|
|
|
2011-01-24 12:43:39 +01:00
|
|
|
zathura_page_t* page = (zathura_page_t*) girara_list_nth(render_thread->list, 0);
|
|
|
|
girara_list_remove(render_thread->list, page);
|
2011-01-26 12:06:57 +01:00
|
|
|
g_mutex_unlock(render_thread->lock);
|
2011-01-24 12:43:39 +01:00
|
|
|
|
2011-04-18 18:00:08 +02:00
|
|
|
if (render(render_thread->zathura, page) != true) {
|
2011-04-18 21:22:35 +02:00
|
|
|
girara_error("Rendering failed\n");
|
2011-03-20 02:09:04 +01:00
|
|
|
}
|
2011-01-24 12:43:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
/* init */
|
2011-04-18 18:00:08 +02:00
|
|
|
render_thread->zathura = zathura;
|
2011-01-26 12:06:57 +01:00
|
|
|
|
|
|
|
/* setup */
|
2011-01-24 12:43:39 +01:00
|
|
|
render_thread->list = girara_list_new();
|
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
if (!render_thread->list) {
|
2011-01-24 12:43:39 +01:00
|
|
|
goto error_free;
|
|
|
|
}
|
|
|
|
|
2011-01-26 12:06:57 +01:00
|
|
|
render_thread->cond = g_cond_new();
|
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
if (!render_thread->cond) {
|
2011-01-26 12:06:57 +01:00
|
|
|
goto error_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
render_thread->lock = g_mutex_new();
|
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
if (!render_thread->lock) {
|
2011-01-26 12:06:57 +01:00
|
|
|
goto error_free;
|
|
|
|
}
|
2011-01-24 12:43:39 +01:00
|
|
|
|
2011-04-26 16:39:40 +02:00
|
|
|
render_thread->thread = g_thread_create(render_job, render_thread, TRUE, NULL);
|
|
|
|
|
|
|
|
if (!render_thread->thread) {
|
|
|
|
goto error_free;
|
|
|
|
}
|
|
|
|
|
2011-01-24 12:43:39 +01:00
|
|
|
return render_thread;
|
|
|
|
|
|
|
|
error_free:
|
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
if (render_thread->list) {
|
2011-01-26 12:06:57 +01:00
|
|
|
girara_list_free(render_thread->list);
|
|
|
|
}
|
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
if (render_thread->cond) {
|
2011-01-26 12:06:57 +01:00
|
|
|
g_cond_free(render_thread->cond);
|
|
|
|
}
|
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
if (render_thread->lock) {
|
2011-01-26 12:06:57 +01:00
|
|
|
g_mutex_free(render_thread->lock);
|
|
|
|
}
|
|
|
|
|
2011-10-19 13:55:50 +02:00
|
|
|
g_free(render_thread);
|
2011-01-24 12:43:39 +01:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
render_free(render_thread_t* render_thread)
|
|
|
|
{
|
2011-02-09 12:29:09 +01:00
|
|
|
if (!render_thread) {
|
2011-01-24 12:43:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
if (render_thread->list) {
|
2011-01-26 12:06:57 +01:00
|
|
|
girara_list_free(render_thread->list);
|
|
|
|
}
|
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
if (render_thread->cond) {
|
2011-07-21 14:39:25 +02:00
|
|
|
g_cond_signal(render_thread->cond);
|
|
|
|
g_thread_join(render_thread->thread);
|
2011-01-26 12:06:57 +01:00
|
|
|
g_cond_free(render_thread->cond);
|
|
|
|
}
|
|
|
|
|
2011-02-09 12:29:09 +01:00
|
|
|
if (render_thread->lock) {
|
2011-01-26 12:06:57 +01:00
|
|
|
g_mutex_free(render_thread->lock);
|
|
|
|
}
|
2011-01-24 12:43:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
render_page(render_thread_t* render_thread, zathura_page_t* page)
|
|
|
|
{
|
2011-04-19 18:33:28 +02:00
|
|
|
if (!render_thread || !page || !render_thread->list || page->surface) {
|
2011-01-24 12:43:39 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-26 12:06:57 +01:00
|
|
|
g_mutex_lock(render_thread->lock);
|
2011-02-09 12:29:09 +01:00
|
|
|
if (!girara_list_contains(render_thread->list, page)) {
|
2011-01-24 12:43:39 +01:00
|
|
|
girara_list_append(render_thread->list, page);
|
|
|
|
}
|
2011-01-26 12:06:57 +01:00
|
|
|
g_cond_signal(render_thread->cond);
|
|
|
|
g_mutex_unlock(render_thread->lock);
|
2011-01-24 12:43:39 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2011-04-18 21:22:35 +02:00
|
|
|
if (zathura == NULL || page == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-03-18 18:40:20 +01:00
|
|
|
gdk_threads_enter();
|
2011-01-11 10:57:59 +01:00
|
|
|
g_static_mutex_lock(&(page->lock));
|
|
|
|
|
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-24 12:43:39 +01:00
|
|
|
g_static_mutex_unlock(&(page->lock));
|
2011-03-18 18:40:20 +01:00
|
|
|
gdk_threads_leave();
|
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);
|
|
|
|
g_static_mutex_unlock(&(page->lock));
|
|
|
|
gdk_threads_leave();
|
|
|
|
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
|
|
|
|
2011-09-29 18:49:43 +02:00
|
|
|
if (fabs(zathura->document->scale - 1.0f) > FLT_EPSILON) {
|
|
|
|
cairo_scale(cairo, zathura->document->scale, zathura->document->scale);
|
|
|
|
}
|
|
|
|
|
2011-09-29 12:35:52 +02:00
|
|
|
if (zathura_page_render(page, cairo) == false) {
|
|
|
|
cairo_destroy(cairo);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
g_static_mutex_unlock(&(page->lock));
|
|
|
|
gdk_threads_leave();
|
|
|
|
return false;
|
2011-03-20 02:09:04 +01:00
|
|
|
}
|
2011-01-07 09:07:02 +01:00
|
|
|
|
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 */
|
|
|
|
if (zathura->global.recolor) {
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-20 02:09:04 +01:00
|
|
|
/* draw to gtk widget */
|
2011-09-29 12:35:52 +02:00
|
|
|
page->surface = surface;
|
2011-04-19 15:39:10 +02:00
|
|
|
gtk_widget_queue_draw(page->drawing_area);
|
2011-01-07 09:07:02 +01:00
|
|
|
|
2011-01-11 10:57:59 +01:00
|
|
|
g_static_mutex_unlock(&(page->lock));
|
2011-03-18 18:40:20 +01:00
|
|
|
gdk_threads_leave();
|
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
|
|
|
{
|
2011-04-18 18:00:08 +02:00
|
|
|
if (zathura->document == NULL) {
|
2011-02-09 21:28:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unmark all pages */
|
2011-04-18 18:00:08 +02:00
|
|
|
for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; page_id++) {
|
2011-12-10 10:19:14 +01:00
|
|
|
zathura_page_t* page = zathura->document->pages[page_id];
|
|
|
|
cairo_surface_destroy(page->surface);
|
|
|
|
page->surface = NULL;
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-12-10 10:19:14 +01:00
|
|
|
gtk_widget_set_size_request(page->drawing_area, page_width, page_height);
|
|
|
|
gtk_widget_queue_resize(page->drawing_area);
|
|
|
|
}
|
2011-04-18 21:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2011-09-21 00:46:03 +02:00
|
|
|
page_expose_event(GtkWidget* UNUSED(widget), GdkEventExpose* UNUSED(event),
|
|
|
|
gpointer data)
|
2011-04-18 21:22:35 +02:00
|
|
|
{
|
|
|
|
zathura_page_t* page = data;
|
2011-04-18 22:59:59 +02:00
|
|
|
if (page == NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-04-18 21:22:35 +02:00
|
|
|
g_static_mutex_lock(&(page->lock));
|
|
|
|
|
|
|
|
cairo_t* cairo = gdk_cairo_create(page->drawing_area->window);
|
|
|
|
|
|
|
|
if (cairo == NULL) {
|
2011-04-18 22:59:59 +02:00
|
|
|
girara_error("Could not retreive cairo object");
|
2011-04-18 21:22:35 +02:00
|
|
|
g_static_mutex_unlock(&(page->lock));
|
2011-04-18 22:59:59 +02:00
|
|
|
return FALSE;
|
2011-04-18 21:22:35 +02:00
|
|
|
}
|
|
|
|
|
2011-12-10 11:44:27 +01:00
|
|
|
unsigned int page_height = 0, page_width = 0;
|
|
|
|
page_calc_height_width(page, &page_height, &page_width, true);
|
|
|
|
|
2011-04-19 15:26:26 +02:00
|
|
|
if (page->surface != NULL) {
|
2011-12-10 11:44:27 +01:00
|
|
|
cairo_save(cairo);
|
|
|
|
|
2012-01-19 00:49:08 +01:00
|
|
|
switch (page->document->rotate) {
|
2011-12-10 11:44:27 +01:00
|
|
|
case 90:
|
|
|
|
cairo_translate(cairo, page_width, 0);
|
|
|
|
break;
|
|
|
|
case 180:
|
|
|
|
cairo_translate(cairo, page_width, page_height);
|
|
|
|
break;
|
|
|
|
case 270:
|
|
|
|
cairo_translate(cairo, 0, page_height);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (page->document->rotate != 0) {
|
|
|
|
cairo_rotate(cairo, page->document->rotate * G_PI / 180.0);
|
|
|
|
}
|
|
|
|
|
2011-04-18 22:59:59 +02:00
|
|
|
cairo_set_source_surface(cairo, page->surface, 0, 0);
|
|
|
|
cairo_paint(cairo);
|
2011-12-10 11:44:27 +01:00
|
|
|
|
|
|
|
cairo_restore(cairo);
|
2011-04-19 21:42:18 +02:00
|
|
|
} else {
|
|
|
|
/* set background color */
|
|
|
|
cairo_set_source_rgb(cairo, 255, 255, 255);
|
2011-12-10 11:44:27 +01:00
|
|
|
cairo_rectangle(cairo, 0, 0, page_width, page->height * page->height);
|
2011-04-19 21:42:18 +02:00
|
|
|
cairo_fill(cairo);
|
|
|
|
|
2012-02-03 22:15:29 +01:00
|
|
|
bool render_loading = true;
|
|
|
|
if (page->document != NULL && page->document->zathura != NULL &&
|
2012-02-03 22:32:11 +01:00
|
|
|
page->document->zathura->ui.session != NULL) {
|
2012-02-03 22:15:29 +01:00
|
|
|
girara_setting_get(page->document->zathura->ui.session, "render-loading", &render_loading);
|
2012-02-03 22:32:11 +01:00
|
|
|
}
|
2012-02-01 17:30:43 +01:00
|
|
|
|
2011-04-19 21:42:18 +02:00
|
|
|
/* write text */
|
2012-02-01 17:30:43 +01:00
|
|
|
if (render_loading == true) {
|
|
|
|
cairo_set_source_rgb(cairo, 0, 0, 0);
|
|
|
|
const char* text = "Loading...";
|
|
|
|
cairo_select_font_face(cairo, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
|
|
|
|
cairo_set_font_size(cairo, 16.0);
|
|
|
|
cairo_text_extents_t extents;
|
|
|
|
cairo_text_extents(cairo, text, &extents);
|
|
|
|
double x = page_width * 0.5 - (extents.width * 0.5 + extents.x_bearing);
|
|
|
|
double y = page_height * 0.5 - (extents.height * 0.5 + extents.y_bearing);
|
|
|
|
cairo_move_to(cairo, x, y);
|
|
|
|
cairo_show_text(cairo, text);
|
|
|
|
}
|
2011-04-19 21:42:18 +02:00
|
|
|
|
|
|
|
/* render real page */
|
2011-04-19 18:33:28 +02:00
|
|
|
render_page(page->document->zathura->sync.render_thread, page);
|
2011-04-27 19:57:49 +02:00
|
|
|
|
|
|
|
/* update statusbar */
|
2011-04-18 22:59:59 +02:00
|
|
|
}
|
2011-04-19 15:23:55 +02:00
|
|
|
cairo_destroy(cairo);
|
2011-04-18 21:22:35 +02:00
|
|
|
|
2011-04-27 19:57:49 +02:00
|
|
|
page->document->current_page_number = page->number;
|
|
|
|
statusbar_page_number_update(page->document->zathura);
|
|
|
|
|
2011-04-18 21:22:35 +02:00
|
|
|
g_static_mutex_unlock(&(page->lock));
|
|
|
|
|
2011-04-18 22:59:59 +02:00
|
|
|
return TRUE;
|
2011-02-09 21:28:36 +01:00
|
|
|
}
|