diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-25 10:59:53 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-25 10:59:53 -0300 |
| commit | 53539a43789d402a9f774d8cf30c0a102331b5b3 (patch) | |
| tree | 641057065b9f82504c96bd720156dc4e6cb00f61 /src/pdf_view.zig | |
| parent | 1dbfb9a468859fb0e0ce228740731e0876bcdcf6 (diff) | |
| download | pardes-53539a43789d402a9f774d8cf30c0a102331b5b3.tar.gz pardes-53539a43789d402a9f774d8cf30c0a102331b5b3.zip | |
pdfrender 2: a PDF opens without its page-size pass: the strip is laid out on the first page's size, the last lane measures every page (its first job) and the sizes land as a relayout that keeps the view's page and offset and the strip's glide (a retarget, not a reset; a one-size document never relayouts); MuPDF's page-tree map is off in every document (its first lookup parsed all 5363 page objects of the SDM: 735 ms in Debug, the first page's size alone and every lane's first page)
Diffstat (limited to 'src/pdf_view.zig')
| -rw-r--r-- | src/pdf_view.zig | 149 |
1 files changed, 146 insertions, 3 deletions
diff --git a/src/pdf_view.zig b/src/pdf_view.zig index b45c40c9..bcca11b4 100644 --- a/src/pdf_view.zig +++ b/src/pdf_view.zig @@ -337,6 +337,45 @@ test "off the loop: pages land from lanes, a stale request's raster never lands, for (state.rasters[0..state.rasters_len]) |r| try std.testing.expect(!r.failed); } +test "page sizes off the loop: the strip opens on the first page's size, and when a lane's sizes land it keeps its page and offset" { + if (comptime !enabled) return; + const gpa = std.testing.allocator; + var exact = try State.open(gpa, "docs/design.pdf", 1); + defer exact.deinit(gpa); + workers = std.testing.io; + defer workers = null; + defer dropKept(); + var state = try State.open(gpa, "docs/design.pdf", 1); + defer state.deinit(gpa); + try std.testing.expect(state.sizes_pending); + for (state.page_sizes) |size| try std.testing.expectEqual(exact.page_sizes[0], size); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const view: Viewport = .{ .pixel_w = 400, .pixel_h = 300 }; + // Assumed wrong (half as tall) so that the landing lays out again. + for (state.page_sizes) |*size| size.height /= 2; + ensureLayout(&state, view); + state.document_scroll_y = @as(f64, @floatFromInt(state.page_starts[2])) + 0.25 * @as(f64, @floatFromInt(state.page_heights[2])); + state.drawn_y = state.document_scroll_y; + const none: HighlightInput = .{ .hover_color = .{ 0, 0, 0 }, .selection_color = .{ 0, 0, 0 } }; + const key: TintKey = .{ .mode = .disabled, .colors = undefined }; + var frames: usize = 0; + while (state.sizes_pending) : (frames += 1) { + _ = renderFrame(&state, gpa, arena.allocator(), view, pardes.pdf_raster_policy, key, none, 0, false); + try std.testing.expect(frames < 100_000); + std.Thread.yield() catch {}; + } + applySizes(&state, gpa); + ensureLayout(&state, view); + try std.testing.expect(std.mem.eql(u8, std.mem.sliceAsBytes(exact.page_sizes), std.mem.sliceAsBytes(state.page_sizes))); + try std.testing.expectEqual(@as(usize, 2), pageAtOffset(&state, state.document_scroll_y)); + const offset = (state.document_scroll_y - @as(f64, @floatFromInt(state.page_starts[2]))) / @as(f64, @floatFromInt(state.page_heights[2])); + try std.testing.expectApproxEqAbs(@as(f64, 0.25), offset, 1e-9); + // The strip moved with its place: no kick, the motion carries on. + try std.testing.expectEqual(state.document_scroll_y, state.drawn_y); + try Wait.idle(&state, &arena, view); +} + 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; @@ -372,6 +411,20 @@ test "kept pages: a second open shows the page it left, bounded in bytes, the ol for (kept) |k| if (k) |e| try std.testing.expect(e.page != 4); } +/// TEST-ONLY: frames until every lane is idle. +const Wait = struct { + fn idle(st: *State, a: *std.heap.ArenaAllocator, v: Viewport) !void { + const none: HighlightInput = .{ .hover_color = .{ 0, 0, 0 }, .selection_color = .{ 0, 0, 0 } }; + const key: TintKey = .{ .mode = .disabled, .colors = undefined }; + var frames: usize = 0; + while (rendering(st)) : (frames += 1) { + _ = renderFrame(st, std.testing.allocator, a.allocator(), v, pardes.pdf_raster_policy, key, none, 0, false); + try std.testing.expect(frames < 100_000); + std.Thread.yield() catch {}; + } + } +}; + pub const Raster = if (enabled) struct { valid: bool = false, page: usize = 0, @@ -427,6 +480,10 @@ const lane_count = 2; /// 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 { + /// A page's raster, or every page's size (`submitSizes`). + kind: enum { render, sizes } = .render, + /// The sizes pass's answer, gpa-owned. + sizes: []pdf.PageSize = &.{}, gen: u32, page: usize, request: RenderRequest, @@ -595,6 +652,15 @@ pub const State = if (enabled) struct { blank: [4]u8 = .{ 255, 255, 255, 255 }, /// Which way the strip last went: the page past it is rendered ahead. ahead: i2 = 1, + /// `page_sizes` past the first are assumed (the first's) until a + /// lane's pass lands, generation `sizes_gen` (0: not asked yet). + sizes_pending: bool = false, + sizes_gen: u32 = 0, + /// The pass's sizes, landed, for the next draw (gpa-owned). + sizes_ready: []pdf.PageSize = &.{}, + /// The next relayout keeps the strip's motion (a retarget, not a + /// reset): the sizes landed under it, nothing the user did. + relayout_keeps_motion: bool = false, /// Which file this is, for the kept pages (`fileKey`). file: u64 = 0, spare: [raster_spare][]u8 = @splat(&.{}), @@ -655,7 +721,14 @@ pub const State = if (enabled) struct { errdefer document.deinit(); const page_sizes = try gpa.alloc(pdf.PageSize, document.pages); errdefer gpa.free(page_sizes); - for (page_sizes, 0..) |*size, page| size.* = try document.pageSize(page); + // With lanes, only the first page is measured here and the strip + // is laid out as if every page had its size, until a lane's pass + // over the rest lands (`submitSizes`): 5363 pages of the SDM took + // the open 700 ms in Debug, 26 in ReleaseSafe, on the loop. + const assumed = workers != null and document.pages > 1; + if (assumed) { + @memset(page_sizes, try document.pageSize(0)); + } else for (page_sizes, 0..) |*size, page| size.* = try document.pageSize(page); const page_starts = try gpa.alloc(u64, document.pages); errdefer gpa.free(page_starts); const page_heights = try gpa.alloc(u32, document.pages); @@ -665,6 +738,7 @@ pub const State = if (enabled) struct { return .{ .path = owned_path, .document = document, + .sizes_pending = assumed, .page = if (page_one_based > 0) @min(page_one_based - 1, document.pages - 1) else @@ -1022,6 +1096,7 @@ pub const State = if (enabled) struct { gpa.free(raster.baked); } for (state.spare[0..state.spare_len]) |rgba| gpa.free(rgba); + if (state.sizes_ready.len > 0) gpa.free(state.sizes_ready); gpa.free(state.page_sizes); gpa.free(state.page_starts); gpa.free(state.page_heights); @@ -1610,6 +1685,7 @@ fn reconcile( defer tz.end(); collect(state, gpa); defer if (workers != null) sweep(state); + if (state.sizes_pending and state.sizes_gen == 0) if (workers) |io| submitSizes(state, gpa, io); // 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. @@ -1830,6 +1906,44 @@ fn submit( return .sent; } +/// The sizes landed: laid out again where the view is, by page and offset +/// in it (a Look made meanwhile landed on the assumed layout and stays on +/// its page), and the strip's glide carries on through it (`ensureLayout`). +/// A document whose pages are all one size never gets here. +pub fn applySizes(state: *State, gpa: std.mem.Allocator) void { + if (comptime !enabled) return; + if (state.sizes_ready.len == 0) return; + if (state.layout_valid and !state.scroll_to_page_pending and !state.layout_anchor_pending) captureLayoutAnchor(state); + @memcpy(state.page_sizes, state.sizes_ready); + gpa.free(state.sizes_ready); + state.sizes_ready = &.{}; + state.layout_valid = false; + state.relayout_keeps_motion = true; +} + +/// The sizes of every page, measured on the last lane (its first job, so +/// its document's page tree is loaded when it starts rendering). +fn submitSizes(state: *State, gpa: std.mem.Allocator, io: std.Io) void { + const lane = &state.lanes[lane_count - 1]; + if (lane.job != null) return; + if (lane.doc == null) lane.doc = state.document.laneClone() catch return; + const job = gpa.create(Job) catch return; + state.render_gen +%= 1; + if (state.render_gen == 0) state.render_gen = 1; + job.* = .{ .kind = .sizes, .gen = state.render_gen, .page = 0, .request = .{}, .tint_key = .{ .mode = .disabled, .colors = undefined }, .highlights = &.{}, .keep_clean = false, .rgba = &.{} }; + lane.future = io.concurrent(renderJob, .{ lane.doc.?, job, gpa }) catch { + gpa.destroy(job); + // No lane: measured here after all. + for (state.page_sizes, 0..) |*size, page| size.* = state.document.pageSize(page) catch size.*; + state.sizes_pending = false; + state.layout_valid = false; + return; + }; + lane.job = job; + lane.io = io; + state.sizes_gen = job.gen; +} + /// 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 @@ -1840,6 +1954,15 @@ fn renderJob(lane_doc: Document, job: *Job, gpa: std.mem.Allocator) void { var doc = lane_doc; defer job.done.store(true, .release); doc.laneOpen() catch return; + if (job.kind == .sizes) { + job.sizes = gpa.alloc(pdf.PageSize, doc.pages) catch return; + for (job.sizes, 0..) |*size, page| { + if (page % 64 == 0 and @atomicLoad(c_int, &job.cookie.abort, .acquire) != 0) return; + size.* = doc.pageSize(page) catch return; + } + job.ok = true; + return; + } doc.setCookie(&job.cookie); defer doc.setCookie(null); // Measured here, on the lane's own document: loading a page (its links @@ -1884,6 +2007,19 @@ 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); + if (job.kind == .sizes) { + defer if (job.sizes.len > 0) gpa.free(job.sizes); + if (job.gen == 0 or job.gen != state.sizes_gen) return; + state.sizes_pending = false; + if (!job.ok or job.sizes.len != state.page_count) return; // ponytail: the assumed sizes stay + if (std.mem.eql(u8, std.mem.sliceAsBytes(job.sizes), std.mem.sliceAsBytes(state.page_sizes))) return; + // Applied by the next draw, before it lays out (`applySizes`): here + // the strip may stand where it is drawn, not where the core is. + if (state.sizes_ready.len > 0) gpa.free(state.sizes_ready); + state.sizes_ready = job.sizes; + job.sizes = &.{}; + return; + } 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; @@ -1933,7 +2069,7 @@ fn land(state: *State, gpa: std.mem.Allocator, job: *Job) void { /// 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| { + for (state.lanes) |lane| if (lane.job) |job| if (job.kind == .render) { const wanted = for (state.rasters[0..state.rasters_len]) |r| { if (r.valid and r.pending == job.gen) break true; } else false; @@ -2491,6 +2627,7 @@ pub fn ensureLayout(state: *State, viewport: Viewport) void { const tz = tracy.zone(@src(), "pdf.ensure_layout"); defer tz.end(); var relaid = false; + const before = state.document_scroll_y; if (state.layout_valid and !state.scroll_to_page_pending and !state.layout_anchor_pending and (state.layout_viewport_w != viewport.pixel_w or @@ -2528,7 +2665,12 @@ pub fn ensureLayout(state: *State, viewport: Viewport) void { const max_scroll = @as(f64, @floatFromInt(state.document_height -| viewport.pixel_h)); state.document_scroll_y = std.math.clamp(state.document_scroll_y, 0, max_scroll); // A new layout is not a scroll: the picture is drawn there at once. - if (relaid) { + // Unless the sizes landed under it: then the strip, wherever it is in + // its glide, moves with the place it is heading for. + if (relaid and state.relayout_keeps_motion) { + state.drawn_y += state.document_scroll_y - before; + state.relayout_keeps_motion = false; + } else if (relaid) { state.follow = .{}; state.drawn_y = state.document_scroll_y; state.direct_y = 0; @@ -3374,6 +3516,7 @@ pub fn draw( const state = &(pane.pdf orelse return false); const view = paneViewport(core, pane) orelse return false; const key = TintKey{ .mode = state.tint, .colors = tintColors(core) }; + applySizes(state, core.pdf_gpa); ensureLayout(state, view); // Drawn where the strip's follow is; the core's position comes back // after (`drawn_y`), unless a search's reveal moved it as it drew, |
