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_view.zig | 552 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 546 insertions(+), 6 deletions(-) (limited to 'src/pdf_view.zig') 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; -- cgit v1.3