diff options
Diffstat (limited to 'src/pdf_bridge.c')
| -rw-r--r-- | src/pdf_bridge.c | 126 |
1 files changed, 122 insertions, 4 deletions
diff --git a/src/pdf_bridge.c b/src/pdf_bridge.c index b1537107..57711245 100644 --- a/src/pdf_bridge.c +++ b/src/pdf_bridge.c @@ -5,6 +5,8 @@ #include <math.h> #include <limits.h> +#include <pthread.h> +#include <stddef.h> #include <stdint.h> #include <stdatomic.h> #include <stdlib.h> @@ -21,6 +23,18 @@ struct pardes_pdf_document { fz_document *doc; int page_count; + /* + * Threads (pardes_pdf_lane_clone). A base document's context is made + * with MuPDF's locks, so it can be cloned; it owns the mutexes and the + * fz_buffer every lane opens its own fz_document from. A lane has + * `base` set and owns neither. + */ + pthread_mutex_t mutexes[FZ_LOCK_MAX]; + fz_locks_context locks; + fz_buffer *buffer; + pardes_pdf_document *base; + fz_cookie *cookie; + /* One UI thread owns a document. Page switches invalidate this cache. */ int cached_page_number; fz_page *cached_page; @@ -34,6 +48,24 @@ struct pardes_pdf_document { int display_list_candidate_page_number; }; +_Static_assert(sizeof(pardes_pdf_cookie) == sizeof(fz_cookie) && + offsetof(pardes_pdf_cookie, abort) == offsetof(fz_cookie, abort) && + offsetof(pardes_pdf_cookie, progress_max) == offsetof(fz_cookie, progress_max) && + offsetof(pardes_pdf_cookie, incomplete) == offsetof(fz_cookie, incomplete), + "pardes_pdf_cookie must mirror fz_cookie"); + +static void +pardes_pdf_lock(void *user, int lock) +{ + pthread_mutex_lock(&((pthread_mutex_t *)user)[lock]); +} + +static void +pardes_pdf_unlock(void *user, int lock) +{ + pthread_mutex_unlock(&((pthread_mutex_t *)user)[lock]); +} + static pardes_pdf_allocator pardes_pdf_global_allocator; static int pardes_pdf_allocator_installed; static _Atomic size_t pardes_pdf_active_documents; @@ -339,12 +371,19 @@ pardes_pdf_open_memory(const unsigned char *bytes, size_t length, int *page_coun return NULL; state->cached_page_number = -1; state->display_list_candidate_page_number = -1; + for (int i = 0; i < FZ_LOCK_MAX; ++i) + pthread_mutex_init(&state->mutexes[i], NULL); + state->locks.user = state->mutexes; + state->locks.lock = pardes_pdf_lock; + state->locks.unlock = pardes_pdf_unlock; ctx = fz_new_context( state->uses_allocator ? &state->mupdf_allocator : NULL, - NULL, + &state->locks, PARDES_PDF_STORE_LIMIT_BYTES); if (ctx == NULL) { + for (int i = 0; i < FZ_LOCK_MAX; ++i) + pthread_mutex_destroy(&state->mutexes[i]); pardes_pdf_free_document(state); atomic_fetch_sub(&pardes_pdf_active_documents, 1); return NULL; @@ -366,13 +405,15 @@ pardes_pdf_open_memory(const unsigned char *bytes, size_t length, int *page_coun fz_always(ctx) { fz_drop_stream(ctx, stream); - fz_drop_buffer(ctx, buffer); } fz_catch(ctx) { fz_report_error(ctx); fz_drop_document(ctx, doc); + fz_drop_buffer(ctx, buffer); fz_drop_context(ctx); + for (int i = 0; i < FZ_LOCK_MAX; ++i) + pthread_mutex_destroy(&state->mutexes[i]); pardes_pdf_free_document(state); atomic_fetch_sub(&pardes_pdf_active_documents, 1); return NULL; @@ -380,6 +421,7 @@ pardes_pdf_open_memory(const unsigned char *bytes, size_t length, int *page_coun state->ctx = ctx; state->doc = doc; + state->buffer = buffer; state->page_count = pages; *page_count = pages; return state; @@ -392,11 +434,84 @@ pardes_pdf_close(pardes_pdf_document *document) return; pardes_pdf_drop_cached_page(document); fz_drop_document(document->ctx, document->doc); + if (document->base == NULL) + fz_drop_buffer(document->ctx, document->buffer); fz_drop_context(document->ctx); + if (document->base == NULL) + for (int i = 0; i < FZ_LOCK_MAX; ++i) + pthread_mutex_destroy(&document->mutexes[i]); pardes_pdf_free_document(document); atomic_fetch_sub(&pardes_pdf_active_documents, 1); } +pardes_pdf_document * +pardes_pdf_lane_clone(pardes_pdf_document *base) +{ + pardes_pdf_document *lane; + + if (base == NULL || base->base != NULL) + return NULL; + lane = pardes_pdf_allocate_document(); + if (lane == NULL) + return NULL; + lane->ctx = fz_clone_context(base->ctx); + if (lane->ctx == NULL) { + pardes_pdf_free_document(lane); + atomic_fetch_sub(&pardes_pdf_active_documents, 1); + return NULL; + } + lane->base = base; + lane->page_count = base->page_count; + lane->cached_page_number = -1; + lane->display_list_candidate_page_number = -1; + return lane; +} + +/* + * The lane's own document, from the base's bytes: parsed again (the xref, + * the page tree), never shared, so the lane's thread is the only one in it. + * The fz_buffer's count is kept under the shared alloc lock. + */ +int +pardes_pdf_lane_open(pardes_pdf_document *lane) +{ + fz_context *ctx; + fz_stream *stream = NULL; + fz_document *doc = NULL; + + if (lane == NULL || lane->base == NULL) + return PARDES_PDF_ERROR; + if (lane->doc != NULL) + return PARDES_PDF_OK; + ctx = lane->ctx; + fz_var(stream); + fz_var(doc); + fz_try(ctx) + { + stream = fz_open_buffer(ctx, lane->base->buffer); + doc = fz_open_document_with_stream(ctx, "application/pdf", stream); + if (fz_count_pages(ctx, doc) != lane->page_count) + fz_throw(ctx, FZ_ERROR_FORMAT, "PDF lane counts other pages"); + } + fz_always(ctx) + fz_drop_stream(ctx, stream); + fz_catch(ctx) + { + fz_report_error(ctx); + fz_drop_document(ctx, doc); + return PARDES_PDF_ERROR; + } + lane->doc = doc; + return PARDES_PDF_OK; +} + +void +pardes_pdf_set_cookie(pardes_pdf_document *document, pardes_pdf_cookie *cookie) +{ + if (document != NULL) + document->cookie = (fz_cookie *)cookie; +} + /* * A page's SIZE, without building a page. * @@ -735,10 +850,10 @@ pardes_pdf_render_into( pardes_pdf_cache_display_list(document); device = fz_new_draw_device(ctx, ctm, pixmap); fz_run_display_list(ctx, document->cached_display_list, device, - fz_identity, scissor, NULL); + fz_identity, scissor, document->cookie); } else { device = fz_new_draw_device(ctx, ctm, pixmap); - fz_run_page(ctx, document->cached_page, device, fz_identity, NULL); + fz_run_page(ctx, document->cached_page, device, fz_identity, document->cookie); } fz_close_device(ctx, device); fz_drop_device(ctx, device); @@ -761,6 +876,9 @@ pardes_pdf_render_into( fz_report_error(ctx); return PARDES_PDF_ERROR; } + /* An aborted run returns as if done, over half a page: say so. */ + if (document->cookie != NULL && document->cookie->abort) + return PARDES_PDF_ABORTED; /* MuPDF draws premultiplied samples. The public RGBA contract and tint * pass use straight alpha; preserve coverage and undo premultiplication. */ |
