zathura/render.c

195 lines
4.2 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-02-28 04:27:03 +01:00
gdk_threads_enter();
2011-01-24 12:43:39 +01:00
render(page);
2011-02-28 04:27:03 +01:00
printf("Rendered %d\n", page->number);
gdk_threads_leave();
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
{
g_static_mutex_lock(&(page->lock));
zathura_image_buffer_t* buffer = zathura_page_render(page);
if (!buffer) {
2011-01-24 12:43:39 +01:00
g_static_mutex_unlock(&(page->lock));
2011-01-07 09:07:02 +01:00
printf("error: rendering failed\n");
return false;
}
/* add new page */
GList* list = gtk_container_get_children(GTK_CONTAINER(Zathura.UI.page_view));
GtkWidget* widget = (GtkWidget*) g_list_nth_data(list, page->number);
g_list_free(list);
2011-02-09 12:29:09 +01:00
if (!widget) {
2011-01-24 12:43:39 +01:00
g_static_mutex_unlock(&(page->lock));
2011-01-07 09:07:02 +01:00
printf("error: page container does not exist\n");
// TODO: zathura_image_buffer_free(image);
2011-01-07 09:07:02 +01:00
return false;
}
/* child packaging information */
gboolean expand;
gboolean fill;
guint padding;
GtkPackType pack_type;
gtk_box_query_child_packing(GTK_BOX(Zathura.UI.page_view), widget, &expand, &fill, &padding, &pack_type);
/* delete old widget */
gtk_container_remove(GTK_CONTAINER(Zathura.UI.page_view), widget);
/* add new widget */
// TODO: gtk_box_pack_start(GTK_BOX(Zathura.UI.page_view), image, TRUE, TRUE, 0);
2011-01-07 09:07:02 +01:00
/* set old packaging values */
// TODO: gtk_box_set_child_packing(GTK_BOX(Zathura.UI.page_view), image, expand, fill, padding, pack_type);
2011-01-07 09:07:02 +01:00
/* reorder child */
// TODO: gtk_box_reorder_child(GTK_BOX(Zathura.UI.page_view), image, page->number);
g_static_mutex_unlock(&(page->lock));
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);
}