zathura/render.c

201 lines
4.3 KiB
C
Raw Normal View History

2011-01-07 09:07:02 +01:00
#include "render.h"
#include "zathura.h"
2011-01-24 13:10:06 +01:00
void* render_job(void* data);
2011-01-24 12:43:39 +01:00
bool render(zathura_page_t* page);
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-02-09 12:29:09 +01: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
}
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-03-20 02:09:04 +01:00
if (render(page) != true) {
fprintf(stderr, "rendering failed\n");
}
2011-02-28 04:27:03 +01:00
printf("Rendered %d\n", page->number);
2011-01-24 12:43:39 +01:00
}
return NULL;
}
render_thread_t*
render_init(void)
{
render_thread_t* render_thread = malloc(sizeof(render_thread_t));
2011-02-09 12:29:09 +01:00
if (!render_thread) {
2011-01-24 12:43:39 +01:00
goto error_ret;
}
2011-01-26 12:06:57 +01:00
/* init */
render_thread->list = NULL;
render_thread->thread = NULL;
render_thread->cond = NULL;
/* 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-24 13:10:06 +01:00
render_thread->thread = g_thread_create(render_job, render_thread, TRUE, NULL);
2011-01-24 12:43:39 +01:00
2011-02-09 12:29:09 +01:00
if (!render_thread->thread) {
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
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-01-24 12:43:39 +01:00
free(render_thread);
error_ret:
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-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-02-09 12:29:09 +01:00
if (!render_thread || !page || !render_thread->list || page->rendered) {
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
render(zathura_page_t* page)
2011-01-07 09:07:02 +01:00
{
2011-03-18 18:40:20 +01:00
gdk_threads_enter();
g_static_mutex_lock(&(page->lock));
2011-03-20 02:09:04 +01:00
zathura_image_buffer_t* image_buffer = zathura_page_render(page);
2011-03-20 02:09:04 +01:00
if (image_buffer == 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-03-20 02:09:04 +01:00
/* remove old image */
GtkWidget* widget = gtk_bin_get_child(GTK_BIN(page->event_box));
if (widget != NULL) {
gtk_container_remove(GTK_CONTAINER(page->event_box), widget);
}
/* create cairo surface */
unsigned int page_width = page->width * Zathura.document->scale;
unsigned int page_height = page->height * Zathura.document->scale;
cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, page_width, page_height);
int rowstride = cairo_image_surface_get_stride(surface);
unsigned char* image = cairo_image_surface_get_data(surface);
for (unsigned int y = 0; y < page_height; y++) {
unsigned char* dst = image + y * rowstride;
unsigned char* src = image_buffer->data + y * image_buffer->rowstride;
for (unsigned int x = 0; x < page_width; x++) {
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst += 3;
}
}
2011-01-07 09:07:02 +01:00
2011-03-20 02:09:04 +01:00
/* draw to gtk widget */
2011-03-20 02:53:24 +01:00
cairo_t* cairo = gdk_cairo_create(page->drawing_area->window);
2011-03-20 02:09:04 +01:00
cairo_set_source_surface(cairo, surface, 0, 0);
cairo_paint(cairo);
cairo_destroy(cairo);
2011-01-07 09:07:02 +01:00
2011-03-20 02:09:04 +01:00
zathura_image_buffer_free(image_buffer);
g_static_mutex_unlock(&(page->lock));
2011-01-07 09:07:02 +01:00
2011-03-18 18:40:20 +01:00
gdk_threads_leave();
2011-01-07 09:07:02 +01:00
return true;
}
2011-02-09 21:28:36 +01:00
void
render_all(void)
{
if (Zathura.document == NULL) {
return;
}
/* unmark all pages */
for (unsigned int page_id = 0; page_id < Zathura.document->number_of_pages; page_id++) {
Zathura.document->pages[page_id]->rendered = false;
}
/* redraw current page */
GtkAdjustment* view_vadjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(Zathura.UI.session->gtk.view));
cb_view_vadjustment_value_changed(view_vadjustment, NULL);
}