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/file_watch.zig | 12 +- src/gui/gui.zig | 6 + src/pardes.zig | 2 + src/pdf.zig | 62 +++++- src/pdf_bridge.c | 126 +++++++++++- src/pdf_bridge.h | 29 +++ src/pdf_view.zig | 552 ++++++++++++++++++++++++++++++++++++++++++++++++++++- src/tty/tty.zig | 3 + 8 files changed, 779 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/file_watch.zig b/src/file_watch.zig index 4aba6c39..ab902e5b 100644 --- a/src/file_watch.zig +++ b/src/file_watch.zig @@ -496,7 +496,17 @@ pub fn reloadPane( const path = pane.pdfPath() orelse return false; const before = identify(io, path) catch return false; switch (watched.generation) { - .pdf => |accepted| if (accepted) |identity| if (identity.eql(before)) return false, + .pdf => |accepted| if (accepted) |identity| { + if (identity.eql(before)) return false; + } else if (pardes.panes.Pdf.sameBytes(core, pane)) { + // Armed over the bytes the pane opened (a text's baseline is its + // content, a PDF's is what it read): nothing to reload. Parsing + // the document again doubled every PDF's open. + if (watches[id]) |*live| if (live.serial == watched.serial) { + live.generation = .{ .pdf = before }; + }; + return false; + }, .text => {}, } if (!core.reloadWatchedFile(@intCast(id), &.{})) return false; diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 13fd7905..71676194 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -2238,9 +2238,12 @@ fn localSession( defer pardes.memory.deinit(); pardes.image.start(io, allocs.image); // stb_image allocator for image panes if (comptime pardes.pdf_enabled) pardes.pdf.start(allocs.pdf); + // Pages render on lanes off the loop (pdf_view.workers). + if (comptime pardes.pdf_enabled) pardes.panes.Pdf.workers = io; pardes.syntax.start(allocs.tree_sitter); defer { pardes.image.stop(); + if (comptime pardes.pdf_enabled) pardes.panes.Pdf.dropKept(); if (comptime pardes.pdf_enabled) pardes.pdf.stop(); pardes.syntax.stop(); } @@ -2622,9 +2625,12 @@ fn runGrid(init: std.process.Init, opts_in: pardes.Options) !void { defer pardes.memory.deinit(); pardes.image.start(io, allocs.image); if (comptime pardes.pdf_enabled) pardes.pdf.start(allocs.pdf); + // Pages render on lanes off the loop (pdf_view.workers). + if (comptime pardes.pdf_enabled) pardes.panes.Pdf.workers = io; pardes.syntax.start(allocs.tree_sitter); defer { pardes.image.stop(); + if (comptime pardes.pdf_enabled) pardes.panes.Pdf.dropKept(); if (comptime pardes.pdf_enabled) pardes.pdf.stop(); pardes.syntax.stop(); } diff --git a/src/pardes.zig b/src/pardes.zig index a1d1eef8..26b167d5 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -7704,6 +7704,8 @@ pub const Pardes = struct { // decoded is looked for. for (p.panes) |slot| if (slot) |pane| if (pane.places.busy(pane)) return next; for (p.panes) |slot| if (slot) |pane| if (pane.image) |iv| if (iv.decoder != null) return next; + // A PDF page being rendered on a lane lands on the frame after it is done. + if (comptime pdf_enabled) for (p.panes) |slot| if (slot) |pane| if (pane.pdf) |*st| if (panes.Pdf.rendering(st)) return next; // A markdown text's tables still being read from the top (Tables.zig). for (p.panes) |slot| if (slot) |pane| if (pane.file) |f| if (f.tables.busy()) return next; if (panes.Query.busy(p)) return next; diff --git a/src/pdf.zig b/src/pdf.zig index 5aedcb50..18037bb7 100644 --- a/src/pdf.zig +++ b/src/pdf.zig @@ -63,6 +63,42 @@ test "custom allocator owns a complete PDF document lifecycle" { try std.testing.expect(raster.rgba.len > 0); } +test "two lanes render two pages of one document at once, each as the base renders it, and a cookie aborts" { + const gpa = std.testing.allocator; + var base = try openTestFile("docs/design.pdf"); + defer base.deinit(); + try std.testing.expect(base.pages >= 2); + const Lane = struct { + doc: Document, + page: usize, + out: ?Render = null, + fn run(lane: *@This()) void { + lane.doc.laneOpen() catch return; + lane.out = lane.doc.render(std.testing.allocator, lane.page, .{}, &.{}) catch null; + } + }; + // Cloned on the base's thread; each lane's document opened on its own. + var lanes = [2]Lane{ .{ .doc = try base.laneClone(), .page = 0 }, .{ .doc = try base.laneClone(), .page = 1 } }; + defer for (&lanes) |*lane| { + if (lane.out) |r| gpa.free(r.rgba); + lane.doc.deinit(); + }; + var threads: [2]std.Thread = undefined; + for (&threads, &lanes) |*t, *lane| t.* = try std.Thread.spawn(.{}, Lane.run, .{lane}); + for (threads) |t| t.join(); + for (lanes) |lane| { + const want = try base.render(gpa, lane.page, .{}, &.{}); + defer gpa.free(want.rgba); + try std.testing.expect(std.mem.eql(u8, want.rgba, lane.out.?.rgba)); + } + // An abort set before the run stops it at its first operator. + var cookie = std.mem.zeroes(Cookie); + cookie.abort = 1; + lanes[0].doc.setCookie(&cookie); + try std.testing.expectError(error.Aborted, lanes[0].doc.render(gpa, 1, .{}, &.{})); + lanes[0].doc.setCookie(null); +} + /// One raster-quality request. `minimum_*` asks MuPDF for enough source pixels /// to avoid backend upscaling; `max_dimension` caps a raster row always, and /// the page's height unless that would draw it below `display_*`, the size @@ -102,6 +138,7 @@ test "MuPDF document cache has an explicit conservative ceiling" { /// Page-space geometry is normalized to the page bounds, not a render. It /// therefore survives DPI changes and can be shared by Kitty and SDL. +pub const Cookie = c.pardes_pdf_cookie; pub const Point = c.pardes_pdf_point; pub const Quad = c.pardes_pdf_quad; @@ -678,6 +715,25 @@ pub const Document = struct { return .{ .handle = handle, .pages = @intCast(page_count) }; } + /// A render lane of this document (`pardes_pdf_lane_clone`): its own + /// cloned context, its own document opened (`laneOpen`) on the thread + /// that renders with it. Deinit every lane before `base`. + pub fn laneClone(base: *Document) !Document { + const handle = c.pardes_pdf_lane_clone(base.handle) orelse return error.OpenFailed; + return .{ .handle = handle, .pages = base.pages }; + } + + pub fn laneOpen(lane: *Document) !void { + if (c.pardes_pdf_lane_open(lane.handle) != c.PARDES_PDF_OK) return error.OpenFailed; + } + + /// The cookie this document's renders run under until set again: set + /// its `abort` from any thread and a render stops at its next operator + /// (`error.Aborted`). + pub fn setCookie(document: *Document, cookie: ?*Cookie) void { + c.pardes_pdf_set_cookie(document.handle, cookie); + } + /// Link annotations are loaded only for the pointed page, separately from /// text/raster work. Returned URIs survive cache eviction and page changes. pub fn linkAt(document: *Document, gpa: std.mem.Allocator, page: usize, point: Point) !?Link { @@ -897,7 +953,7 @@ pub const Document = struct { if (highlights.len > c.PARDES_PDF_MAX_RESULT_QUADS) return error.RenderFailed; const highlight_ptr: ?[*]const c.pardes_pdf_highlight = if (highlights.len == 0) null else @ptrCast(highlights.ptr); - if (c.pardes_pdf_render_into( + const status = c.pardes_pdf_render_into( document.handle, @intCast(page), bounded.dpi, @@ -916,7 +972,9 @@ pub const Document = struct { @intCast(band.y), @intCast(band.height), @intFromBool(transparent_paper), - ) != c.PARDES_PDF_OK) return error.RenderFailed; + ); + if (status == c.PARDES_PDF_ABORTED) return error.Aborted; + if (status != c.PARDES_PDF_OK) return error.RenderFailed; } /// Only the highlight pass of `renderIntoAtWithPaper`, over `rgba`, which 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 #include +#include +#include #include #include #include @@ -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. */ 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, diff --git a/src/pdf_view.zig b/src/pdf_view.zig index 15e7dac5..b45c40c9 100644 --- a/src/pdf_view.zig +++ b/src/pdf_view.zig @@ -272,6 +272,106 @@ test "a PDF glide renders the pages it crosses in its input's frame, but a far j try std.testing.expect(rasterForPage(&state, 2) == null); } +test "off the loop: pages land from lanes, a stale request's raster never lands, and a job nobody waits for is aborted" { + if (comptime !enabled) return; + const gpa = std.testing.allocator; + workers = std.testing.io; + defer workers = null; + defer dropKept(); + var state = try State.open(gpa, "docs/design.pdf", 1); + defer state.deinit(gpa); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const key: TintKey = .{ .mode = .disabled, .colors = undefined }; + const none: HighlightInput = .{ .hover_color = .{ 0, 0, 0 }, .selection_color = .{ 0, 0, 0 } }; + const policy = pardes.pdf_raster_policy; + const Run = struct { + /// Frames until every lane is idle; no slot ever holds a raster of + /// the `stale` request. + fn settle(st: *State, a: *std.heap.ArenaAllocator, v: Viewport, k: TintKey, h: HighlightInput, stale: ?RenderRequest) !void { + var frames: usize = 0; + while (true) : (frames += 1) { + _ = renderFrame(st, std.testing.allocator, a.allocator(), v, pardes.pdf_raster_policy, k, h, 0, false); + if (stale) |old| for (st.rasters[0..st.rasters_len]) |r| if (r.rgba.len > 0 and r.request_valid) + try std.testing.expect(!r.request.eql(old)); + if (!rendering(st)) break; + try std.testing.expect(frames < 100_000); + std.Thread.yield() catch {}; + } + } + }; + const small: Viewport = .{ .pixel_w = 400, .pixel_h = 300 }; + ensureLayout(&state, small); + // The first frame asks; nothing has landed, so the loop drew nothing. + _ = renderFrame(&state, gpa, arena.allocator(), small, policy, key, none, 0, false); + try std.testing.expect(rendering(&state)); + try std.testing.expect(!pageReady(&state, small, 0)); + try Run.settle(&state, &arena, small, key, none, null); + try std.testing.expect(pageReady(&state, small, 0)); + // The page past the view was rendered ahead. + const shown = visiblePages(&state, small); + try std.testing.expect(rasterForPage(&state, shown.first + shown.len) != null); + // A new request while the old one's jobs are out, many times over: + // only the newest request's rasters are ever in a slot. + var round: u32 = 0; + while (round < 8) : (round += 1) { + const v: Viewport = .{ .pixel_w = 300 + 37 * round, .pixel_h = 300 }; + ensureLayout(&state, v); + _ = renderFrame(&state, gpa, arena.allocator(), v, policy, key, none, 0, false); + const next: Viewport = .{ .pixel_w = 301 + 37 * round, .pixel_h = 300 }; + ensureLayout(&state, next); + try Run.settle(&state, &arena, next, key, none, renderRequest(v, policy, state.fit)); + try std.testing.expect(pageReady(&state, next, state.page)); + try std.testing.expect(rasterForPage(&state, state.page).?.request.eql(renderRequest(next, policy, state.fit))); + } + // A job whose page left the strip is aborted through its cookie, and an + // aborted job is not a failure: the page is asked again when it returns. + state.invalidateAllRasters(); + const last: Viewport = .{ .pixel_w = 301 + 37 * 7, .pixel_h = 300 }; + _ = renderFrame(&state, gpa, arena.allocator(), last, policy, key, none, 0, false); + state.document_scroll_y = @floatFromInt(state.page_starts[state.page_count - 1]); + _ = renderFrame(&state, gpa, arena.allocator(), last, policy, key, none, 0, false); + for (state.lanes) |lane| if (lane.job) |job| if (job.page < state.page_count - 2) + try std.testing.expectEqual(@as(c_int, 1), @atomicLoad(c_int, &job.cookie.abort, .acquire)); + try Run.settle(&state, &arena, last, key, none, null); + for (state.rasters[0..state.rasters_len]) |r| try std.testing.expect(!r.failed); +} + +test "kept pages: a second open shows the page it left, bounded in bytes, the oldest going first" { + if (comptime !enabled) return; + const gpa = std.testing.allocator; + workers = std.testing.io; + defer workers = null; + defer dropKept(); + const key: TintKey = .{ .mode = .disabled, .colors = undefined }; + const request: RenderRequest = .{}; + // Five pages of 20 MiB kept: only what fits in the bound stays, newest. + { + var state = try State.open(gpa, "docs/design.pdf", 1); + defer state.deinit(gpa); + for (0..5) |page| { + state.rasters[page] = .{ .valid = true, .page = page, .rgba = try gpa.alloc(u8, 20 << 20), .iw = 1, .ih = 1, .band_h = 1, .request = request, .request_valid = true, .tint_key = key }; + state.rasters_len += 1; + } + } + var total: usize = 0; + var pages: [5]bool = @splat(false); + for (kept) |k| if (k) |e| { + total += e.rgba.len; + pages[e.page] = true; + }; + try std.testing.expect(total <= kept_limit); + try std.testing.expect(pages[4] and !pages[0]); + // Opened again: page 4 is there before any render. + var again = try State.open(gpa, "docs/design.pdf", 1); + defer again.deinit(gpa); + again.rasters[0] = .{ .valid = true, .page = 4 }; + again.rasters_len = 1; + try std.testing.expect(unkeep(&again, &again.rasters[0], request, key)); + try std.testing.expectEqual(@as(usize, 20 << 20), again.rasters[0].rgba.len); + for (kept) |k| if (k) |e| try std.testing.expect(e.page != 4); +} + pub const Raster = if (enabled) struct { valid: bool = false, page: usize = 0, @@ -300,6 +400,157 @@ pub const Raster = if (enabled) struct { /// to a shell; 0 when the pixels were replaced wholesale since. patch_from: u32 = 0, patch_rows: pdf.Document.Rows = .{ .start = 0, .end = 0 }, + /// The render job this slot waits for (its generation; 0: none). Only + /// that job's raster lands here: an older one, or one for a slot since + /// retired, is dropped when it comes (`land`). + pending: u32 = 0, + /// The last job for `request` failed (not aborted): not asked again + /// until the raster is invalidated or the request changes. + failed: bool = false, + /// A lane found the page too tall for one buffer: it is rendered on the + /// loop, in chunks, as before. + banded: bool = false, +} else void; + +/// Where page renders run off the loop: set by a shell that has threads +/// (gui, tty) to its Io. Null (tests, benches, hosts without threads): the +/// loop renders as it always did. +pub var workers: ?std.Io = null; + +/// Two lanes: the page in view and the next one, or the two a strip shows +/// at once; a page asked for while both are out waits a frame. +/// ponytail: two lanes, fixed; more only if a profile shows pages queuing +/// behind a slow one (a third costs another parse of the document). +const lane_count = 2; + +/// One page render, run on a lane's thread: what the loop asked for, and +/// what the lane leaves for `land` (rgba and clean are gpa-owned, handed +/// over). The lane's thread touches only this and the lane's document. +const Job = if (enabled) struct { + gen: u32, + page: usize, + request: RenderRequest, + /// Measured by the lane. + shape: pdf.Raster = .{ .width = 0, .height = 0, .stride = 0, .len = 0 }, + /// Too tall for one buffer: the loop renders it in bands (`slot.banded`). + banded: bool = false, + tint_key: TintKey, + /// The highlights baked in (gpa-owned): the slot's `baked` once it lands. + highlights: []Highlight, + keep_clean: bool, + /// A spare of the page's size, or empty: the lane allocates it. + rgba: []u8, + clean: []u8 = &.{}, + ok: bool = false, + /// Set from the loop when nothing waits for this page any more. + cookie: pdf.Cookie = std.mem.zeroes(pdf.Cookie), + done: std.atomic.Value(bool) = .init(false), +} else void; + +/// Pages kept past their pane: a second open of the same file, a Restore +/// (the old core's panes close, the new one's open), shows them on its +/// first frame instead of waiting for a lane. Only with workers (a test's +/// State leaves nothing behind). Bound: `kept_limit` bytes and +/// `kept.len` pages, the least recently kept evicted first. +/// ponytail: the file is known by its path, length and a hash of its first +/// and last 64 KiB (a PDF's trailer and xref are at its end); a change +/// that keeps all three shows the old page until it is next rendered. +const Kept = if (enabled) struct { + file: u64, + page: usize, + request: RenderRequest, + tint_key: TintKey, + rgba: []u8, + iw: usize, + ih: usize, + gpa: std.mem.Allocator, + age: u64, +} else void; +const kept_limit: usize = 64 << 20; +var kept: [16]?Kept = @splat(null); +var kept_age: u64 = 0; + +fn fileKey(path: []const u8, bytes: []const u8) u64 { + var h: std.hash.Wyhash = .init(bytes.len); + h.update(path); + h.update(bytes[0..@min(bytes.len, 64 << 10)]); + h.update(bytes[bytes.len -| (64 << 10)..]); + return h.final(); +} + +/// A slot's page as it stands (whole, unhighlighted) into the kept pages, +/// its buffer handed over; the oldest go until it fits. +fn keepPage(state: *State, gpa: std.mem.Allocator, slot: *Raster) void { + if (workers == null or slot.rgba.len == 0 or slot.baked.len != 0 or !slot.request_valid or + slot.tint_key == null or slot.band_y != 0 or slot.band_h != slot.ih or slot.rgba.len > kept_limit) return; + kept_age += 1; + for (&kept) |*k| if (k.*) |e| if (e.file == state.file and e.page == slot.page) { + e.gpa.free(e.rgba); + k.* = null; + }; + while (true) { + var total: usize = slot.rgba.len; + var oldest: ?*?Kept = null; + var free: ?*?Kept = null; + for (&kept) |*k| if (k.*) |e| { + total += e.rgba.len; + if (oldest == null or e.age < oldest.?.*.?.age) oldest = k; + } else if (free == null) { + free = k; + }; + if (total <= kept_limit) if (free) |f| { + f.* = .{ .file = state.file, .page = slot.page, .request = slot.request, .tint_key = slot.tint_key.?, .rgba = slot.rgba, .iw = slot.iw, .ih = slot.ih, .gpa = gpa, .age = kept_age }; + slot.rgba = &.{}; + return; + }; + const o = oldest.?; + o.*.?.gpa.free(o.*.?.rgba); + o.* = null; + } +} + +/// The kept page for this file, page, request and tint, into `slot`. +fn unkeep(state: *State, slot: *Raster, request: RenderRequest, tint_key: TintKey) bool { + for (&kept) |*k| if (k.*) |e| if (e.file == state.file and e.page == slot.page and e.request.eql(request) and e.tint_key.eql(tint_key)) { + k.* = null; + slot.rgba = e.rgba; + slot.iw = e.iw; + slot.ih = e.ih; + slot.band_y = 0; + slot.band_h = e.ih; + slot.request = request; + slot.request_valid = true; + slot.tint_key = tint_key; + slot.tried = true; + slot.baked_valid = true; + state.next_raster_revision +%= 1; + if (state.next_raster_revision == 0) state.next_raster_revision = 1; + slot.revision = state.next_raster_revision; + state.landed +|= 1; + return true; + }; + return false; +} + +/// Every kept page freed: a shell's end, before its allocators' leak check. +pub fn dropKept() void { + if (comptime !enabled) return; + for (&kept) |*k| if (k.*) |e| { + e.gpa.free(e.rgba); + k.* = null; + }; +} + +const Lane = if (enabled) struct { + /// The lane's cloned context and its own document (`Document.laneClone`): + /// made on the loop at its first job, opened by its first job, used by + /// one job at a time, closed with the State. + doc: ?Document = null, + job: ?*Job = null, + future: ?std.Io.Future(void) = null, + io: std.Io = undefined, + /// Its document is open and its fonts loaded by a job (`collect`). + warm: bool = false, } else void; pub const SectionsOutput = if (enabled) struct { @@ -338,6 +589,14 @@ pub const State = if (enabled) struct { rasters_len: usize = 0, /// Rasters installed since the last draw (the probe's `landed`). landed: u16 = 0, + lanes: [lane_count]Lane = @splat(.{}), + render_gen: u32 = 0, + /// The paper a page shows until its raster lands, tinted (`draw`). + blank: [4]u8 = .{ 255, 255, 255, 255 }, + /// Which way the strip last went: the page past it is rendered ahead. + ahead: i2 = 1, + /// Which file this is, for the kept pages (`fileKey`). + file: u64 = 0, spare: [raster_spare][]u8 = @splat(&.{}), spare_len: usize = 0, layout_anchor_pending: bool = false, @@ -382,11 +641,13 @@ pub const State = if (enabled) struct { const local = filesystem.localPath(path) orelse return error.NonLocalPath; const bytes = try filesystem.readFile(gpa, local); defer gpa.free(bytes); - return initDocument(gpa, path, try Document.openBytes(bytes), page_one_based); + return openBytes(gpa, path, bytes, page_one_based); } pub fn openBytes(gpa: std.mem.Allocator, path: []const u8, bytes: []const u8, page_one_based: usize) !@This() { - return initDocument(gpa, path, try Document.openBytes(bytes), page_one_based); + var state = try initDocument(gpa, path, try Document.openBytes(bytes), page_one_based); + state.file = fileKey(path, bytes); + return state; } fn initDocument(gpa: std.mem.Allocator, path: []const u8, opened: Document, page_one_based: usize) !@This() { @@ -739,7 +1000,22 @@ pub const State = if (enabled) struct { } pub fn deinit(state: *@This(), gpa: std.mem.Allocator) void { + // Every lane stopped and joined, its job dropped, its document + // closed, before the document its context was cloned from. + for (&state.lanes) |*lane| { + if (lane.job) |job| { + @atomicStore(c_int, &job.cookie.abort, 1, .release); + pardes.turn.yield(); + lane.future.?.await(lane.io); + pardes.turn.back(); + job.gen = 0; + land(state, gpa, job); + } + if (lane.doc) |*doc| doc.deinit(); + lane.* = .{}; + } gpa.free(state.path); + for (state.rasters[0..state.rasters_len]) |*raster| keepPage(state, gpa, raster); for (state.rasters[0..state.rasters_len]) |raster| { if (raster.rgba.len > 0) gpa.free(raster.rgba); if (raster.clean.len > 0) gpa.free(raster.clean); @@ -1332,6 +1608,8 @@ fn reconcile( if (comptime !enabled) return; const tz = tracy.zone(@src(), "pdf.reconcile"); defer tz.end(); + collect(state, gpa); + defer if (workers != null) sweep(state); // Remove first so arriving pages can claim departing page buffers. Only // the final visible set can be seen, so a fling skips crossed-over pages. @@ -1357,6 +1635,7 @@ fn reconcile( state.rasters[state.rasters_len] = .{ .valid = true, .page = page }; state.rasters_len += 1; raster = &state.rasters[state.rasters_len - 1]; + if (workers != null) _ = unkeep(state, raster.?, request, tint_key); } const slot = raster.?; const page_highlights = highlights.forPage(page); @@ -1386,6 +1665,23 @@ fn reconcile( if (repaintHighlights(state, gpa, slot, request, tint_key, page_highlights)) continue; } + // Off the loop: a lane renders the page whole and it lands on a + // later frame (`collect`); until then the slot shows what it has. + // A page too tall for one buffer is banded and stays on the loop. + if (workers) |io| { + if (pendingJob(state, slot)) |job| { + if (job.request.eql(request) and job.tint_key.eql(tint_key)) continue; + @atomicStore(c_int, &job.cookie.abort, 1, .release); + slot.pending = 0; + } + if (slot.failed and slot.tried and slot.request_valid and slot.request.eql(request)) continue; + const keep = pardes.platform != .macos and (decorated or slot.clean.len > 0); + switch (submit(state, gpa, io, slot, request, tint_key, page_highlights, keep)) { + .sent, .busy => continue, + .loop => {}, + } + } + slot.tried = true; slot.request = request; slot.request_valid = true; @@ -1472,6 +1768,187 @@ fn reconcile( state.trimSpares(gpa); } +fn pendingJob(state: *State, slot: *const Raster) ?*Job { + if (slot.pending == 0) return null; + for (state.lanes) |lane| if (lane.job) |job| if (job.gen == slot.pending) return job; + return null; +} + +/// Hands `slot`'s page to a free lane: `busy` when both are out (asked +/// again next frame), `loop` when the page is banded or no lane can be had +/// (the loop renders it as before). +fn submit( + state: *State, + gpa: std.mem.Allocator, + io: std.Io, + slot: *Raster, + request: RenderRequest, + tint_key: TintKey, + page_highlights: []const Highlight, + keep_clean: bool, +) enum { sent, busy, loop } { + if (slot.banded) return .loop; + // A lane's first job parses the document again and loads its fonts: + // a second cold lane beside the first only slowed both (the SDM's + // first page 1.7 -> 2.5 s in Debug), so a lane starts once the one + // before it is warm. + const lane = for (&state.lanes, 0..) |*l, i| { + if (l.job == null and (l.doc != null or i == 0 or state.lanes[i - 1].warm)) break l; + } else return .busy; + if (lane.doc == null) lane.doc = state.document.laneClone() catch return .loop; + const highlights = gpa.dupe(Highlight, page_highlights) catch return .loop; + const job = gpa.create(Job) catch { + gpa.free(highlights); + return .loop; + }; + state.render_gen +%= 1; + if (state.render_gen == 0) state.render_gen = 1; + // A spare is handed over: the lane keeps it if it is the page's size + // (most pages of a document are), else frees it and allocates. + const spare: []u8 = if (state.spare_len > 0) spare: { + state.spare_len -= 1; + break :spare state.spare[state.spare_len]; + } else &.{}; + job.* = .{ + .gen = state.render_gen, + .page = slot.page, + .request = request, + .tint_key = tint_key, + .highlights = highlights, + .keep_clean = keep_clean, + .rgba = spare, + }; + lane.future = io.concurrent(renderJob, .{ lane.doc.?, job, gpa }) catch { + state.retireRgba(gpa, spare); + gpa.free(highlights); + gpa.destroy(job); + return .loop; + }; + lane.job = job; + lane.io = io; + slot.pending = job.gen; + return .sent; +} + +/// A lane's thread: the page rendered whole into the job's buffer, its +/// clean copy kept, highlighted and tinted, all as `reconcile` does on the +/// loop, under the job's cookie. It touches the job and the lane's own +/// document (`doc` is a copy of its handle), nothing of the State. +fn renderJob(lane_doc: Document, job: *Job, gpa: std.mem.Allocator) void { + const tz = tracy.zone(@src(), "pdf.lane_render"); + defer tz.end(); + var doc = lane_doc; + defer job.done.store(true, .release); + doc.laneOpen() catch return; + doc.setCookie(&job.cookie); + defer doc.setCookie(null); + // Measured here, on the lane's own document: loading a page (its links + // and annotations) cost the loop 6-7 ms a page of the SDM in Debug. + job.shape = doc.measureRenderAt(job.page, job.request) catch return; + if (isBanded(job.shape)) { + job.banded = true; + return; + } + if (job.rgba.len != job.shape.len) { + if (job.rgba.len > 0) gpa.free(job.rgba); + job.rgba = &.{}; + job.rgba = pardes.memory.unfilled(gpa, job.shape.len) catch return; + } + const band = job.shape.wholePage(); + doc.renderIntoAtWithPaper(job.page, job.request, job.shape, band, if (job.keep_clean) &.{} else job.highlights, job.rgba, pardes.platform == .macos) catch return; + if (job.keep_clean) { + job.clean = pardes.memory.dupeUnfilled(gpa, job.rgba) catch return; + doc.paintHighlightsAt(job.page, job.request, job.shape, band, job.highlights, job.rgba) catch return; + } + pdf.tintRgba(job.rgba, job.tint_key.mode, job.tint_key.colors) catch return; + job.ok = true; +} + +/// The jobs that are done, joined (a return away) and landed. +fn collect(state: *State, gpa: std.mem.Allocator) void { + for (&state.lanes) |*lane| { + const job = lane.job orelse continue; + if (!job.done.load(.acquire)) continue; + lane.future.?.await(lane.io); + lane.future = null; + lane.job = null; + lane.warm = true; + land(state, gpa, job); + } +} + +/// A job's raster into the slot waiting for its generation, swapped in +/// whole (no pixel is copied on the loop); a stale, failed or aborted one +/// is dropped. Frees the job. +fn land(state: *State, gpa: std.mem.Allocator, job: *Job) void { + const tz = tracy.zone(@src(), "pdf.land"); + defer tz.end(); + defer gpa.destroy(job); + const slot: ?*Raster = if (job.gen == 0) null else for (state.rasters[0..state.rasters_len]) |*r| { + if (r.valid and r.pending == job.gen) break r; + } else null; + if (slot == null or !job.ok) { + if (slot) |s| { + s.pending = 0; + s.banded = job.banded; + if (@atomicLoad(c_int, &job.cookie.abort, .acquire) == 0 and !job.banded) { + s.failed = true; + s.tried = true; + s.request = job.request; + s.request_valid = true; + } + } + state.retireRgba(gpa, job.rgba); + if (job.clean.len > 0) gpa.free(job.clean); + gpa.free(job.highlights); + return; + } + const s = slot.?; + s.pending = 0; + s.failed = false; + s.tried = true; + state.retireRgba(gpa, s.rgba); + s.rgba = job.rgba; + if (s.clean.len > 0) gpa.free(s.clean); + s.clean = job.clean; + s.iw = job.shape.width; + s.ih = job.shape.height; + s.band_y = 0; + s.band_h = job.shape.height; + s.request = job.request; + s.request_valid = true; + s.tint_key = job.tint_key; + s.decorated = job.highlights.len > 0; + gpa.free(s.baked); + s.baked = job.highlights; + s.baked_valid = true; + s.patch_from = 0; + s.patch_rows = .{ .start = 0, .end = 0 }; + state.next_raster_revision +%= 1; + if (state.next_raster_revision == 0) state.next_raster_revision = 1; + s.revision = state.next_raster_revision; + state.landed +|= 1; +} + +/// A job no slot waits for any more (its page left the strip, its request +/// changed) is told to stop: MuPDF sees the cookie at its next operator. +fn sweep(state: *State) void { + for (state.lanes) |lane| if (lane.job) |job| { + const wanted = for (state.rasters[0..state.rasters_len]) |r| { + if (r.valid and r.pending == job.gen) break true; + } else false; + if (!wanted) @atomicStore(c_int, &job.cookie.abort, 1, .release); + }; +} + +/// Some page of this State is being rendered on a lane (the frames after +/// look for it, as a picture's decode is looked for). +pub fn rendering(state: *const State) bool { + if (comptime !enabled) return false; + for (state.lanes) |lane| if (lane.job != null) return true; + return false; +} + /// Repaint only the rows where the old and new highlights differ: the /// clean rows kept from the page's last render, MuPDF's highlight pass /// (the whole new set) over them, the per-pixel tint — the operations a @@ -1607,13 +2084,23 @@ pub fn renderFrame( return if (from_rest) pagesOver(st, st.document_scroll_y + along, h) else pagesOver(st, st.document_scroll_y + @min(0, along), h + @abs(along)); } }.f; + // Off the loop, the page past the strip the way it last went is asked + // for too, after the ones in view: rendered ahead, as paint-ahead does. + const ahead = struct { + fn f(st: *const State, v: VisiblePages) VisiblePages { + if (workers == null or v.len == 0) return v; + if (st.ahead > 0 and v.first + v.len < st.page_count) return .{ .first = v.first, .len = v.len + 1 }; + if (st.ahead < 0 and v.first > 0) return .{ .first = v.first - 1, .len = v.len + 1 }; + return v; + } + }.f; var visible = span(state, viewport, path, far); - reconcile(state, gpa, request, tint_key, highlights, visible, viewport); + reconcile(state, gpa, request, tint_key, highlights, ahead(state, visible), viewport); rearmSearchReveal(state, viewport); revealSearch(state, viewport, activeGeometry(state, viewport)); visible = span(state, viewport, path, far); - reconcile(state, gpa, request, tint_key, highlights, visible, viewport); + reconcile(state, gpa, request, tint_key, highlights, ahead(state, visible), viewport); return visible; } @@ -2186,6 +2673,17 @@ pub fn openPane( return pane; } +/// The file on disk is still the one the pane read (`fileKey`): a watch +/// armed over it has nothing to reload. +pub fn sameBytes(core: *pardes.Pardes, pane: *const pardes.Pane) bool { + if (comptime !enabled) return false; + const state = if (pane.pdf) |*pv| pv else return false; + if (state.file == 0) return false; + const bytes = filesystem.read(core, state.path) catch return false; + defer core.gpa.free(bytes); + return fileKey(state.path, bytes) == state.file; +} + /// Release a PDF payload while its pane slot is still installed, so the host /// can retire the corresponding directory watch before that id is reused. pub fn deinitPane(core: *pardes.Pardes, pane: *pardes.Pane, state: *State) void { @@ -2831,6 +3329,7 @@ fn stripAt(state: *State, view: Viewport, motion: pardes.animation.Motion, now_n // the pages between are rendered for the slide, not crossed. const body: f64 = @floatFromInt(view.pixel_h); state.far = @abs(moved) > body; + state.ahead = if (moved > 0) 1 else -1; state.follow.kick(@floatCast(std.math.clamp(moved, -body, body)), now_ns, pace); } else state.follow.advance(now_ns, pace); const max_scroll = @as(f64, @floatFromInt(state.document_height -| view.pixel_h)); @@ -2905,8 +3404,45 @@ pub fn draw( var placed_any = false; var page = visible.first; const visible_end = visible.first + visible.len; + state.blank = .{ 255, 255, 255, 255 }; + pdf.tintRgba(&state.blank, key.mode, key.colors) catch {}; while (page < visible_end) : (page += 1) { - const placed = placedRaster(state, view, page) orelse continue; + const placed = placedRaster(state, view, page) orelse { + // Not landed yet (a lane renders it): its paper, where it will + // be, one pixel drawn the page's size. Never a stall. + // ponytail: the blank's texture is keyed revision 0, so a tint + // change shows the old paper until the page lands. + if (workers == null or pardes.platform == .macos) continue; + const size = state.page_sizes[page]; + const rows = visibleRows(state, view, page, @intFromFloat(@max(1, @round(size.width * 8))), @intFromFloat(@max(1, @round(size.height * 8)))) orelse continue; + if (!core.appendImagePlace(.{ + .pane = @intCast(pane_id), + .serial = pane.serial, + .native = .{ + .revision = 0, + .page = @intCast(page), + .fit = switch (state.fit) { + .width => .width, + .height => .height, + }, + .pan_x = state.pan_x, + .geometry = .{ + .src = .{ .x = 0, .y = 0, .w = 1, .h = 1 }, + .dst = .{ .x = rows.base.dst.x, .y = rows.dst_y, .w = rows.base.dst.w, .h = rows.dst_h }, + }, + .pixel_offset_y = rows.pixel_offset_y, + }, + .x = text_x, + .y = core.bodyTop(pane, rect) + pane.notices.span(), + .w = text_width, + .h = (rect.h -| pane.tag_rows) -| pane.notices.span(), + .rgba = &state.blank, + .iw = 1, + .ih = 1, + })) break; + placed_any = true; + continue; + }; const slot = rasterForPage(state, page).?; const patch: ?pardes.ImagePatch = if (slot.patch_from != 0 and slot.patch_rows.start < slot.patch_rows.end) @@ -2952,7 +3488,11 @@ pub fn draw( slot.patch_rows = .{ .start = 0, .end = 0 }; placed_any = true; } - if (!placed_any) return false; + // Nothing placed while pages are there (a far jump's first frame: its + // slide starts a body short of the pages it renders) is still the PDF's + // frame: falling back to the page's text extracted it on the loop + // (9 ms RS, 110 ms Debug on the SDM) to show it for one frame. + if (!placed_any and visible.len == 0) return false; // This frame has spent the motion used to choose its raster band. state.scroll_travel = 0; diff --git a/src/tty/tty.zig b/src/tty/tty.zig index c97ba4bf..5ad02c28 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -1298,9 +1298,12 @@ fn localSession( pardes.image.start(io, allocs.image); if (comptime pardes.pdf_enabled) pardes.pdf.start(allocs.pdf); + // Pages render on lanes off the loop (pdf_view.workers). + if (comptime pardes.pdf_enabled) pardes.panes.Pdf.workers = io; pardes.syntax.start(allocs.tree_sitter); defer { pardes.image.stop(); + if (comptime pardes.pdf_enabled) pardes.panes.Pdf.dropKept(); if (comptime pardes.pdf_enabled) pardes.pdf.stop(); pardes.syntax.stop(); } -- cgit v1.3