diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-18 05:32:59 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-18 05:48:26 -0300 |
| commit | 3f8f66643a1e4840e1cbf0d7a4c5aa8580360ac7 (patch) | |
| tree | acb0c305ed07e229725bfa677d92691a9c4e92fd /src/Kind.zig | |
| parent | 22b5ada4c099618e72af3d33dc274a7c272dd227 (diff) | |
| download | pardes-3f8f66643a1e4840e1cbf0d7a4c5aa8580360ac7.tar.gz pardes-3f8f66643a1e4840e1cbf0d7a4c5aa8580360ac7.zip | |
kinds: a draw (pa_api.draw, ABI 3.2), a kind's job whose answer feeds its pictures alone: job_run over a copy of its input on a worker of the process that shows the pane (the frontend's workers through Kind.drawer, else at once), its answer to job_done with a PA_DRAWN token at the pane's next paint (Kind.deliver), never an event, on the tape, in the digest or sent to another process, each process asking its own from the same call; the header says what a kind promises by it (what job_done keeps of it paint alone reads; never a plain kind's block, nor its token; Debug checks the block is as it was) and addon.zig has draw; a client's pane of a plain kind closed in a speculation leaves its kind's own memory be (its rollback brings the pane back); the window client takes its queue's draws; the addon ABI tests expect 3.2; tui core-test 819/958 (139 skipped), shell-test 993/996, behaviour-test 273/273 (the four addon tests the base failed on its ABI string run again and pass), --replay 20 seeds 0 failures, pane-test 180/181, replicas pass, addon-test 1/1
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src/Kind.zig')
| -rw-r--r-- | src/Kind.zig | 90 |
1 files changed, 85 insertions, 5 deletions
diff --git a/src/Kind.zig b/src/Kind.zig index 42205518..47384227 100644 --- a/src/Kind.zig +++ b/src/Kind.zig @@ -24,6 +24,7 @@ const tape = @import("tape.zig"); const surface = @import("surface.zig"); const views = @import("views.zig"); const Addon = @import("Addon.zig"); +const host_io = @import("host_io.zig"); pub const Loaded = Addon.Loaded; pub const LoadedWord = Addon.LoadedWord; pub const loadedOf = Addon.loadedOf; @@ -41,7 +42,7 @@ comptime { if (@sizeOf(usize) == 8) for (.{ .{ c.pa_cell, 20 }, .{ c.pa_window, 24 }, .{ c.pa_rect, 16 }, .{ c.pa_cells, 8 }, .{ c.pa_placement, 80 }, .{ c.pa_paint, 48 }, .{ c.pa_key, 8 }, .{ c.pa_mouse, 16 }, - .{ c.pa_api, 112 }, .{ c.pa_out, 16 }, .{ c.pa_word, 32 }, .{ c.pa_head, 56 }, + .{ c.pa_api, 120 }, .{ c.pa_out, 16 }, .{ c.pa_word, 32 }, .{ c.pa_head, 56 }, .{ c.pa_ops, 208 }, .{ c.pa_view, 32 }, .{ c.pa_tick, 128 }, }) |r| if (@sizeOf(r[0]) != r[1]) @compileError(std.fmt.comptimePrint("{s} is {d} bytes, the header says {d}", .{ @typeName(r[0]), @sizeOf(r[0]), r[1] })); } @@ -384,13 +385,22 @@ pub fn fileChanged(p: *Pardes, id: usize, _: []const u8) bool { /// Its kind's close, then everything it had. pub fn close(p: *Pardes, pane: *Pane) void { const s = pane.addon orelse return; - if (s.ops) |ops| if (ops.close) |f| { + // A client's pane of a plain kind closed in a speculation: its kind's + // own memory (its pictures) is left be, as the rollback brings the pane + // back; the session's order closes it. + const kept = s.plain != null and s.ops != null and s.ops.?.close != null and tape.authorityOnly("a kind's close"); + if (!kept) { + var i: usize = 0; + while (i < ready.items.len) if (ready.items[i].draw == p and ready.items[i].serial == pane.serial) ready.orderedRemove(i).drop() else { + i += 1; + }; + } + if (s.ops) |ops| if (ops.close) |f| if (!kept) { var cl: Call = .{ .p = null, .id = 0, .slot = s, .quiet = true }; const prev = cl.begin("close"); f(s.st); _ = cl.end(prev); }; - _ = p; const gpa = s.gpa; s.free(); gpa.destroy(s); @@ -422,7 +432,10 @@ pub fn draw(p: *Pardes, s: *surface.Surface, arena: std.mem.Allocator, pane: *Pa const text = if (p.kinds.byName(slot.nameOf()) != null) why else std.fmt.bufPrint(&line, "addon {s} {d} is not loaded: {s}", .{ slot.nameOf(), slot.version, why }) catch &line; _ = s.print(x, y, cols, text, .{ .fg = fg, .bg = bg }); return; - } else frame.cells = (paint(p, slot, id, arena, cols, rows, &frame) orelse return); + } else { + if (ready.items.len > 0) deliver(p, slot, pane, id); + frame.cells = (paint(p, slot, id, arena, cols, rows, &frame) orelse return); + } if (frame.gpu.len != 0) s.gpu_frames[id] = .{ .bytes = frame.gpu, .serial = pane.serial, .dst = .{ .x = x, .y = y, .w = cols, .h = rows } }; for (0..@min(rows, frame.rows)) |r| for (0..@min(cols, frame.cols)) |col| { const cell = &frame.cells[r * frame.cols + col]; @@ -469,7 +482,7 @@ pub fn paint(p: *const Pardes, slot: *Slot, id: usize, arena: std.mem.Allocator, var out: c.pa_paint = std.mem.zeroInit(c.pa_paint, .{ .placements = &frame.buf, .cap = frame.buf.len, .gpu = GpuPaint.emit, .gpu_ctx = &gpu }); const px = p.view().cell_pixels; const win = std.mem.zeroInit(c.pa_window, .{ .cells = cells.ptr, .stride = cols, .rows = rows, .cols = cols, .cell_w = px.w, .cell_h = px.h, .focused = @intFromBool(focusedAnywhere(p, id)), .view = p.live + 1, .glyphs = @intFromBool(!p.view().frontend.native_images) }); - var cl: Call = .{ .p = null, .id = 0, .slot = slot, .quiet = true }; + var cl: Call = .{ .p = null, .id = id, .slot = slot, .quiet = true, .core = p }; const prev = cl.begin("paint"); const before = if (checked) usedHash(slot) else {}; ops.paint.?(slot.st, &win, &out); @@ -914,6 +927,9 @@ const Call = struct { opening: bool = false, /// An address's: what it says of a miss is the Look's to say. missing: ?*Buf(160) = null, + /// Paint's core (its `p` is null: it asks nothing of the step), whose + /// pane `id` a draw is for. + core: ?*const Pardes = null, name: []const u8 = "", ret: i64 = 0, refused: Buf(160) = .{}, @@ -1167,6 +1183,31 @@ pub const api: c.pa_api = .{ cl.cancels[free] = token; } }.f, + .draw = struct { + /// A picture's job, of this process alone: on the frontend's + /// workers (`drawer`), else here at once; its answer to job_done at + /// the pane's next paint (`deliver`), never through the step. + fn f(in: ?*const anyopaque, n: u32) callconv(.c) u32 { + const cl = current orelse return 0; + const p: *const Pardes = cl.p orelse cl.core orelse return 0; + const pane = p.panes[cl.id] orelse return 0; + const ops = cl.slot.ops orelse return 0; + if (ops.job_run == null or ops.job_done == null) return 0; + const copy = heap.dupe(u8, if (in) |b| @as([*]const u8, @ptrCast(b))[0..n] else "") catch return 0; + const j = heap.create(host_io.KindJob) catch { + heap.free(copy); + return 0; + }; + draw_tokens = (draw_tokens +% 1) & ~c.PA_DRAWN; + const token = c.PA_DRAWN | draw_tokens; + j.* = .{ .gpa = heap, .pane = @intCast(cl.id), .serial = pane.serial, .token = token, .gen = cl.slot.gen, .ops = ops, .in = copy, .arena = .init(heap), .draw = p }; + Addon.holdJob(ops, true); + if (drawer) |d| if (d.go(d.ctx, j)) return token; + j.run(); + ready.append(heap, j) catch j.drop(); + return token; + } + }.f, .found = struct { /// Gathered in the call: the search's answer, or onto its +Search after. fn f(rows: [*c]const u8, n: u32) callconv(.c) void { @@ -1178,6 +1219,45 @@ pub const api: c.pa_api = .{ }.f, }; +/// Where this process's draws run (api.draw): its frontend's workers, +/// which post each back to its loop to land (host_io.KindJob.land); false, +/// or none (a test's core), and it runs here at once. +pub var drawer: ?struct { ctx: ?*anyopaque, go: *const fn (ctx: ?*anyopaque, j: *host_io.KindJob) bool } = null; +var draw_tokens: u32 = 0; +/// Draws done, each kept for its pane's next paint (the loop's alone). +var ready: std.ArrayListUnmanaged(*host_io.KindJob) = .empty; + +/// A draw done on a worker, back on the loop: kept for its pane's next +/// paint, a frame owed. One of another core's (a client joined again +/// since) is dropped: the new core's pane asked its own. +pub fn landDraw(p: *Pardes, j: *host_io.KindJob) void { + if (j.draw != p) return j.drop(); + ready.append(heap, j) catch return j.drop(); + p.needs_frame = true; +} + +/// The draws done for pane `id`, to its kind's job_done before it paints: +/// pictures only, so a plain kind's block is as it was (Debug checks). +fn deliver(p: *const Pardes, slot: *Slot, pane: *const Pane, id: usize) void { + var i: usize = 0; + while (i < ready.items.len) { + const j = ready.items[i]; + if (j.draw != p or j.pane != id) { + i += 1; + continue; + } + _ = ready.orderedRemove(i); + defer j.drop(); + if (pane.serial != j.serial or slot.gen != j.gen or slot.ops != j.ops) continue; + var cl: Call = .{ .p = null, .id = id, .slot = slot, .quiet = true, .core = p }; + const prev = cl.begin("job_done"); + const before = if (checked) usedHash(slot); + j.ops.job_done.?(slot.st, j.token, j.out.ptr, @intCast(j.out.len)); + if (checked and slot.plain != null and usedHash(slot) != before) std.debug.panic("addon {s}: a draw's answer changed its state (a draw is pictures alone)", .{slot.nameOf()}); + _ = cl.end(prev); + } +} + /// What `api.heap` hands out: memory that outlives a call, from any /// thread (a document a kind keeps, its pixels, MuPDF's own). A host sets /// it to its region for kinds (memory.zig); the C heap otherwise. |
