Commit 10115db6 authored by Sebastian Ramacher's avatar Sebastian Ramacher

Rewrite render thread as request based system

ZathuraRenderer is a thread pool rendering the pages. Every page widget holds a
ZathuraRenderRequest instance to request its page to be rendered. This object
can also be used to abort the request in case the page is not visible anymore.
Signed-off-by: Sebastian Ramacher's avatarSebastian Ramacher <sebastian+dev@ramacher.at>
parent 0afc0e53
......@@ -381,9 +381,9 @@ cmd_search(girara_session_t* session, const char* input, girara_argument_t* argu
GtkWidget* page_widget = zathura_page_get_widget(zathura, page);
g_object_set(page_widget, "draw-links", FALSE, NULL);
render_lock(zathura->sync.render_thread);
zathura_renderer_lock(zathura->sync.render_thread);
girara_list_t* result = zathura_page_search_text(page, input, &error);
render_unlock(zathura->sync.render_thread);
zathura_renderer_unlock(zathura->sync.render_thread);
if (result == NULL || girara_list_size(result) == 0) {
girara_list_free(result);
......
......@@ -15,6 +15,7 @@
#include "utils.h"
#include "shortcuts.h"
#include "synctex.h"
#include "zathura.h"
G_DEFINE_TYPE(ZathuraPage, zathura_page_widget, GTK_TYPE_DRAWING_AREA)
......@@ -22,6 +23,7 @@ typedef struct zathura_page_widget_private_s {
zathura_page_t* page; /**< Page object */
zathura_t* zathura; /**< Zathura object */
cairo_surface_t* surface; /**< Cairo surface */
ZathuraRenderRequest* render_request; /* Request object */
bool render_requested; /**< No surface and rendering has been requested */
gint64 last_view; /**< Last time the page has been viewed */
mutex lock; /**< Lock */
......@@ -144,6 +146,7 @@ zathura_page_widget_init(ZathuraPage* widget)
priv->page = NULL;
priv->surface = NULL;
priv->render_requested = false;
priv->render_request = NULL;
priv->last_view = g_get_real_time();
priv->links.list = NULL;
......@@ -177,7 +180,18 @@ zathura_page_widget_new(zathura_t* zathura, zathura_page_t* page)
{
g_return_val_if_fail(page != NULL, NULL);
return g_object_new(ZATHURA_TYPE_PAGE, "page", page, "zathura", zathura, NULL);
GObject* ret = g_object_new(ZATHURA_TYPE_PAGE, "page", page, "zathura", zathura, NULL);
if (ret == NULL) {
return NULL;
}
ZathuraPage* widget = ZATHURA_PAGE(ret);
zathura_page_widget_private_t* priv = ZATHURA_PAGE_GET_PRIVATE(widget);
priv->render_request = zathura_render_request_new(zathura->sync.render_thread, page);
g_signal_connect_object(priv->render_request, "completed",
G_CALLBACK(zathura_page_widget_update_surface), widget, G_CONNECT_SWAPPED);
return GTK_WIDGET(ret);
}
static void
......@@ -190,6 +204,10 @@ zathura_page_widget_finalize(GObject* object)
cairo_surface_destroy(priv->surface);
}
if (priv->render_request != NULL) {
g_object_unref(priv->render_request);
}
if (priv->search.list != NULL) {
girara_list_free(priv->search.list);
}
......@@ -485,7 +503,7 @@ zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo)
/* render real page */
if (priv->render_requested == false) {
priv->render_requested = true;
render_page(priv->zathura->sync.render_thread, priv->page);
zathura_render_request(priv->render_request);
}
}
mutex_unlock(&(priv->lock));
......
......@@ -109,9 +109,9 @@ cb_print_draw_page(GtkPrintOperation* print_operation, GtkPrintContext*
/* Try to render the page without a temporary surface. This only works with
* plugins that support rendering to any surface. */
girara_debug("printing page %d ...", page_number);
render_lock(zathura->sync.render_thread);
zathura_renderer_lock(zathura->sync.render_thread);
int err = zathura_page_render(page, cairo, true);
render_unlock(zathura->sync.render_thread);
zathura_renderer_unlock(zathura->sync.render_thread);
if (err == ZATHURA_ERROR_OK) {
return;
}
......@@ -144,9 +144,9 @@ cb_print_draw_page(GtkPrintOperation* print_operation, GtkPrintContext*
/* Render the page to the temporary surface */
girara_debug("printing page %d ...", page_number);
render_lock(zathura->sync.render_thread);
zathura_renderer_lock(zathura->sync.render_thread);
err = zathura_page_render(page, temp_cairo, true);
render_unlock(zathura->sync.render_thread);
zathura_renderer_unlock(zathura->sync.render_thread);
if (err != ZATHURA_ERROR_OK) {
cairo_destroy(temp_cairo);
cairo_surface_destroy(surface);
......
This diff is collapsed.
......@@ -6,57 +6,139 @@
#include <stdbool.h>
#include <stdlib.h>
#include <girara/types.h>
#include "types.h"
#include "zathura.h"
#include "callbacks.h"
typedef struct zathura_renderer_class_s ZathuraRendererClass;
struct zathura_renderer_s
{
GObject parent;
};
struct zathura_renderer_class_s
{
GObjectClass parent_class;
};
#define ZATHURA_TYPE_RENDERER \
(zathura_renderer_get_type())
#define ZATHURA_RENDERER(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), ZATHURA_TYPE_RENDERER, ZathuraRenderer))
#define ZATHURA_RENDERER_CLASS(obj) \
(G_TYPE_CHECK_CLASS_CAST((obj), ZATHURA_TYPE_RENDERER, ZathuraRendererClass))
#define ZATHURA_IS_RENDERER(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj), ZATHURA_TYPE_RENDERER))
#define ZATHURA_IS_RENDERER_CLASS(obj) \
(G_TYPE_CHECK_CLASS_TYPE((obj), ZATHURA_TYPE_RENDERER))
#define ZATHURA_RENDERER_GET_CLASS \
(G_TYPE_INSTANCE_GET_CLASS((obj), ZATHURA_TYPE_RENDERER, ZathuraRendererClass))
/**
* This function initializes a render thread
*
* @param zathura object
* @return The render thread object or NULL if an error occured
* Returns the type of the renderer.
* @return the type
*/
GType zathura_renderer_get_type(void);
/**
* Create a page view widget.
* @param zathura the zathura instance
* @param page the page to be displayed
* @return a page view widget
*/
render_thread_t* render_init(zathura_t* zathura);
ZathuraRenderer* zathura_renderer_new(zathura_t* zathura);
bool zathura_renderer_recolor_enabled(ZathuraRenderer* renderer);
void zathura_renderer_enable_recolor(ZathuraRenderer* renderer, bool enable);
bool zathura_renderer_recolor_hue_enabled(ZathuraRenderer* renderer);
void zathura_renderer_enable_recolor_hue(ZathuraRenderer* renderer,
bool enable);
void zathura_renderer_set_recolor_colors(ZathuraRenderer* renderer,
GdkColor* light, GdkColor* dark);
void zathura_renderer_stop(ZathuraRenderer* renderer);
/**
* This function destroys the render thread object
* Lock the render thread. This is useful if you want to render on your own (e.g
* for printing).
*
* @param render_thread The render thread object
* @param render_thread The render thread object.
*/
void render_free(render_thread_t* render_thread);
void zathura_renderer_lock(ZathuraRenderer* renderer);
/**
* This function is used to add a page to the render thread list
* that should be rendered.
* Unlock the render thread.
*
* @param render_thread The render thread object
* @param page The page that should be rendered
* @return true if no error occured
* @param render_thread The render thread object.
*/
bool render_page(render_thread_t* render_thread, zathura_page_t* page);
void zathura_renderer_unlock(ZathuraRenderer* renderer);
typedef struct zathura_render_request_s ZathuraRenderRequest;
typedef struct zathura_render_request_class_s ZathuraRenderRequestClass;
struct zathura_render_request_s
{
GObject parent;
};
struct zathura_render_request_class_s
{
GObjectClass parent_class;
};
#define ZATHURA_TYPE_RENDER_REQUEST \
(zathura_render_request_get_type())
#define ZATHURA_RENDER_REQUEST(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), ZATHURA_TYPE_RENDER_REQUEST, \
ZathuraRenderRequest))
#define ZATHURA_RENDER_REQUEST_CLASS(obj) \
(G_TYPE_CHECK_CLASS_CAST((obj), ZATHURA_TYPE_RENDER_REQUEST, \
ZathuraRenderRequestClass))
#define ZATHURA_IS_RENDER_REQUEST(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj), ZATHURA_TYPE_RENDER_REQUEST))
#define ZATHURA_IS_RENDER_REQUEST_CLASS(obj) \
(G_TYPE_CHECK_CLASS_TYPE((obj), ZATHURA_TYPE_RENDER_REQUEST))
#define ZATHURA_RENDER_REQUEST_GET_CLASS \
(G_TYPE_INSTANCE_GET_CLASS((obj), ZATHURA_TYPE_RENDER_REQUEST, \
ZathuraRenderRequestClass))
/**
* This function is used to unmark all pages as not rendered. This should
* be used if all pages should be rendered again (e.g.: the zoom level or the
* colors have changed)
* Returns the type of the renderer.
* @return the type
*/
GType zathura_page_render_info_get_type(void);
/**
* Create a page view widget.
* @param zathura the zathura instance
* @param page the page to be displayed
* @return a page view widget
*/
ZathuraRenderRequest* zathura_render_request_new(ZathuraRenderer* renderer,
zathura_page_t* page);
/**
* This function is used to add a page to the render thread list
* that should be rendered.
*
* @param zathura Zathura object
* @param request request object of the page that should be renderer
*/
void render_all(zathura_t* zathura);
void zathura_render_request(ZathuraRenderRequest* request);
/**
* Lock the render thread. This is useful if you want to render on your own (e.g
* for printing).
* Abort an existing render request.
*
* @param render_thread The render thread object.
* @param reqeust request that should be aborted
*/
void render_lock(render_thread_t* render_thread);
void zathura_render_request_abort(ZathuraRenderRequest* request);
/**
* Unlock the render thread.
* This function is used to unmark all pages as not rendered. This should
* be used if all pages should be rendered again (e.g.: the zoom level or the
* colors have changed)
*
* @param render_thread The render thread object.
* @param zathura Zathura object
*/
void render_unlock(render_thread_t* render_thread);
void render_all(zathura_t* zathura);
#endif // RENDER_H
......@@ -25,6 +25,11 @@ typedef struct zathura_s zathura_t;
*/
typedef struct zathura_plugin_manager_s zathura_plugin_manager_t;
/**
* Renderer
*/
typedef struct zathura_renderer_s ZathuraRenderer;
/**
* Error types
*/
......
......@@ -711,6 +711,13 @@ document_open(zathura_t* zathura, const char* path, const char* password,
zathura->document = document;
/* threads */
zathura->sync.render_thread = zathura_renderer_new(zathura);
if (zathura->sync.render_thread == NULL) {
goto error_free;
}
/* create blank pages */
zathura->pages = calloc(number_of_pages, sizeof(GtkWidget*));
if (zathura->pages == NULL) {
......@@ -759,13 +766,6 @@ document_open(zathura_t* zathura, const char* path, const char* password,
girara_set_view(zathura->ui.session, zathura->ui.page_widget_alignment);
/* threads */
zathura->sync.render_thread = render_init(zathura);
if (zathura->sync.render_thread == NULL) {
goto error_free;
}
for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) {
gtk_widget_realize(zathura->pages[page_id]);
}
......@@ -883,6 +883,9 @@ document_close(zathura_t* zathura, bool keep_monitor)
return false;
}
/* stop rendering */
zathura_renderer_stop(zathura->sync.render_thread);
/* remove monitor */
if (keep_monitor == false) {
if (zathura->file_monitor.monitor != NULL) {
......@@ -945,7 +948,7 @@ document_close(zathura_t* zathura, bool keep_monitor)
zathura->jumplist.size = 0;
/* release render thread */
render_free(zathura->sync.render_thread);
g_object_unref(zathura->sync.render_thread);
zathura->sync.render_thread = NULL;
/* remove widgets */
......
......@@ -31,10 +31,6 @@ enum {
/* forward declaration for types from database.h */
typedef struct _ZathuraDatabase zathura_database_t;
/* forward declaration for types from render.h */
struct render_thread_s;
typedef struct render_thread_s render_thread_t;
/**
* Jump
*/
......@@ -78,7 +74,7 @@ struct zathura_s
struct
{
render_thread_t* render_thread; /**< The thread responsible for rendering the pages */
ZathuraRenderer* render_thread; /**< The thread responsible for rendering the pages */
} sync;
struct
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment