diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-25 10:15:44 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-25 10:42:37 -0300 |
| commit | 1dbfb9a468859fb0e0ce228740731e0876bcdcf6 (patch) | |
| tree | e59b662ef4e0d5223a2559122d2ff62e042236a4 /src/pdf.zig | |
| parent | b0773dc68491b0eab1b8030731b063cba3dd2b40 (diff) | |
| download | pardes-1dbfb9a468859fb0e0ce228740731e0876bcdcf6.tar.gz pardes-1dbfb9a468859fb0e0ce228740731e0876bcdcf6.zip | |
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
Diffstat (limited to 'src/pdf.zig')
| -rw-r--r-- | src/pdf.zig | 62 |
1 files changed, 60 insertions, 2 deletions
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 |
