Render thread

This commit is contained in:
Moritz Lipp 2011-01-24 19:43:39 +08:00
parent 1fc97838e1
commit 55eaca8ec7
5 changed files with 131 additions and 6 deletions

View file

@ -16,6 +16,8 @@ cb_destroy(GtkWidget* widget, gpointer data)
girara_session_destroy(Zathura.UI.session); girara_session_destroy(Zathura.UI.session);
} }
document_close();
return TRUE; return TRUE;
} }
@ -62,7 +64,7 @@ cb_view_vadjustment_value_changed(GtkAdjustment *adjustment, gpointer data)
|| ( (begin <= lower) && (end >= lower) && (end <= upper) ) /* end of the page is in viewport */ || ( (begin <= lower) && (end >= lower) && (end <= upper) ) /* end of the page is in viewport */
|| ( (begin >= lower) && (end >= upper) && (begin <= upper) ) /* begin of the page is in viewport */ || ( (begin >= lower) && (end >= upper) && (begin <= upper) ) /* begin of the page is in viewport */
) { ) {
page_render(Zathura.document->pages[page_id]); render_page(Zathura.Sync.render_thread, Zathura.document->pages[page_id]);
} }
} }
} }

View file

@ -1,27 +1,117 @@
#include "render.h" #include "render.h"
#include "zathura.h" #include "zathura.h"
bool page_render(zathura_page_t* page) void* search(void* data);
bool render(zathura_page_t* page);
void*
search(void* data)
{
render_thread_t* render_thread = (render_thread_t*) data;
while(true) {
g_static_mutex_lock(&(render_thread->lock));
if(girara_list_size(render_thread->list) <= 0) {
g_static_mutex_unlock(&(render_thread->lock));
g_thread_yield();
continue;
}
zathura_page_t* page = (zathura_page_t*) girara_list_nth(render_thread->list, 0);
girara_list_remove(render_thread->list, page);
g_static_mutex_unlock(&(render_thread->lock));
printf("Rendered %d\n", page->number);
render(page);
}
return NULL;
}
render_thread_t*
render_init(void)
{
render_thread_t* render_thread = malloc(sizeof(render_thread_t));
if(!render_thread) {
goto error_ret;
}
render_thread->list = girara_list_new();
if(!render_thread->list) {
goto error_free;
}
render_thread->thread = g_thread_create(search, render_thread, TRUE, NULL);
if(!render_thread->thread) {
goto error_free;
}
g_static_mutex_init(&(render_thread->lock));
return render_thread;
error_free:
free(render_thread);
error_ret:
return NULL;
}
void
render_free(render_thread_t* render_thread)
{
if(!render_thread) {
return;
}
girara_list_free(render_thread->list);
g_static_mutex_free(&(render_thread->lock));
}
bool
render_page(render_thread_t* render_thread, zathura_page_t* page)
{
if(!render_thread || !page || !render_thread->list) {
return false;
}
g_static_mutex_lock(&(render_thread->lock));
if(!girara_list_contains(render_thread->list, page)) {
girara_list_append(render_thread->list, page);
}
g_static_mutex_unlock(&(render_thread->lock));
return true;
}
bool
render(zathura_page_t* page)
{ {
g_static_mutex_lock(&(page->lock)); g_static_mutex_lock(&(page->lock));
GtkWidget* image = zathura_page_render(page); GtkWidget* image = zathura_page_render(page);
g_static_mutex_unlock(&(page->lock));
if(!image) { if(!image) {
g_static_mutex_unlock(&(page->lock));
printf("error: rendering failed\n"); printf("error: rendering failed\n");
return false; return false;
} }
/* add new page */ /* add new page */
g_static_mutex_lock(&(page->lock));
GList* list = gtk_container_get_children(GTK_CONTAINER(Zathura.UI.page_view)); GList* list = gtk_container_get_children(GTK_CONTAINER(Zathura.UI.page_view));
GtkWidget* widget = (GtkWidget*) g_list_nth_data(list, page->number); GtkWidget* widget = (GtkWidget*) g_list_nth_data(list, page->number);
g_list_free(list); g_list_free(list);
if(!widget) { if(!widget) {
g_static_mutex_unlock(&(page->lock));
printf("error: page container does not exist\n"); printf("error: page container does not exist\n");
g_object_unref(image); g_object_unref(image);
g_static_mutex_unlock(&(page->lock));
return false; return false;
} }

View file

@ -1,7 +1,23 @@
/* See LICENSE file for license and copyright information */ /* See LICENSE file for license and copyright information */
#ifndef RENDER_H
#define RENDER_H
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h>
#include <girara-datastructures.h>
#include "ft/document.h" #include "ft/document.h"
bool page_render(zathura_page_t* page); typedef struct render_thread_s
{
girara_list_t* list;
GThread* thread;
GStaticMutex lock;
} render_thread_t;
render_thread_t* render_init(void);
void render_free(render_thread_t* render_thread);
bool render_page(render_thread_t* render_thread, zathura_page_t* page);
#endif // RENDER_H

View file

@ -117,6 +117,13 @@ document_open(const char* path, const char* password)
girara_set_view(Zathura.UI.session, Zathura.UI.page_view); girara_set_view(Zathura.UI.session, Zathura.UI.page_view);
/* threads */
Zathura.Sync.render_thread = render_init();
if(!Zathura.Sync.render_thread) {
goto error_free;
}
/* first page */ /* first page */
if(!page_set(0)) { if(!page_set(0)) {
goto error_free; goto error_free;
@ -140,6 +147,10 @@ document_close()
return false; return false;
} }
if(Zathura.Sync.render_thread) {
render_free(Zathura.Sync.render_thread);
}
zathura_document_free(Zathura.document); zathura_document_free(Zathura.document);
return true; return true;

View file

@ -5,6 +5,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <girara.h> #include <girara.h>
#include <render.h>
#include "ft/document.h" #include "ft/document.h"
@ -40,6 +41,11 @@ struct
GtkWidget *page_view; GtkWidget *page_view;
} UI; } UI;
struct
{
render_thread_t* render_thread;
} Sync;
zathura_document_t* document; /**> The current document */ zathura_document_t* document; /**> The current document */
} Zathura; } Zathura;