From 1dbfb9a468859fb0e0ce228740731e0876bcdcf6 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 25 Oct 2026 10:15:44 -0300 Subject: pdfrender 1: PDF pages render off the loop: MuPDF's base context gets locks (fz_locks_context, pthread mutexes), a render lane is a cloned context with its own document opened from the base's shared fz_buffer (no fz_context or fz_document is ever touched by two threads); jobs on io.concurrent, a generation per slot (a stale raster never lands), fz_cookie aborts a job nobody waits for; a page not landed shows its paper, the page past the strip is rendered ahead, the second lane starts once the first is warm; pages kept past their pane for a second open or a Restore (64 MiB, oldest out first); a PDF's watch armed over the bytes it read no longer reopens it (every open parsed the document twice); a far jump's first frame no longer falls back to the page's text --- src/pdf_bridge.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/pdf_bridge.h') diff --git a/src/pdf_bridge.h b/src/pdf_bridge.h index 776843de..3ab87376 100644 --- a/src/pdf_bridge.h +++ b/src/pdf_bridge.h @@ -167,6 +167,8 @@ enum { PARDES_PDF_OK = 0, PARDES_PDF_ERROR = -1, PARDES_PDF_LIMIT_EXCEEDED = -2, + /* A render stopped early through its cookie (pardes_pdf_set_cookie). */ + PARDES_PDF_ABORTED = -3, /* Per-document MuPDF eviction cache; render buffers are separate. */ PARDES_PDF_STORE_LIMIT_BYTES = 64 * 1024 * 1024, @@ -192,6 +194,33 @@ enum { pardes_pdf_document *pardes_pdf_open_memory(const unsigned char *bytes, size_t length, int *page_count); void pardes_pdf_close(pardes_pdf_document *document); +/* + * A render lane: a context cloned from `base`'s (fz_clone_context: the store, + * the allocator and the locks shared) and a document of its OWN, opened from + * the bytes `base` holds (the fz_buffer shared, read only). MuPDF allows one + * thread at a time in a context and in a document; a lane is used by one + * thread at a time and never shares either with `base` or another lane, so + * lanes render while `base` serves the UI thread. Clone on the thread that + * owns `base` (the clone reads its context); `pardes_pdf_lane_open` then runs + * on the lane's thread. Close every lane before `base`. NULL: no lane (out of + * memory). + */ +pardes_pdf_document *pardes_pdf_lane_clone(pardes_pdf_document *base); +int pardes_pdf_lane_open(pardes_pdf_document *lane); + +/* Mirrors fz_cookie (checked in the bridge): set `abort` nonzero from any + * thread and a render running under it stops at its next operator. */ +typedef struct pardes_pdf_cookie { + int abort; + int progress; + size_t progress_max; + int errors; + int incomplete; +} pardes_pdf_cookie; + +/* The cookie the document's renders run under (NULL: none), until set again. */ +void pardes_pdf_set_cookie(pardes_pdf_document *document, pardes_pdf_cookie *cookie); + /* Page dimensions in PDF points after crop/rotation, without rasterizing. */ int pardes_pdf_get_page_size( pardes_pdf_document *document, -- cgit v1.3