diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/Term.zig | 11 | ||||
| -rw-r--r-- | core/replica.zig | 106 | ||||
| -rw-r--r-- | core/term/TerminalImages.zig | 13 | ||||
| -rw-r--r-- | core/term/terminal.zig | 11 |
4 files changed, 121 insertions, 20 deletions
diff --git a/core/Term.zig b/core/Term.zig index 116c1118..ce462d81 100644 --- a/core/Term.zig +++ b/core/Term.zig @@ -79,8 +79,8 @@ pub const State = struct { } /// What `dumpRead` made, freed: a load that failed after it. - pub fn dumpFree(s: *State, gpa: std.mem.Allocator) void { - the().close(s.impl, gpa); + pub fn dumpFree(s: *State, _: std.mem.Allocator) void { + the().close(s.impl); } pub fn dumpRead(out: *State, gpa: std.mem.Allocator, bytes: []const u8) !void { @@ -115,10 +115,11 @@ pub fn open(gpa: std.mem.Allocator, cols: u16, rows: u16) !*State { return s; } -/// It and what it holds, freed; the kitty images' caches were `gpa`'s. -pub fn close(s: *State, gpa: std.mem.Allocator) void { +/// It and what it holds, freed, all of it with its own allocator (the +/// one it was opened with): the allocator a caller hands is not used. +pub fn close(s: *State, _: std.mem.Allocator) void { const own = the().gpa(s.impl); - the().close(s.impl, gpa); + the().close(s.impl); own.destroy(s); } diff --git a/core/replica.zig b/core/replica.zig index b8d8f436..e4ed4e0a 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -740,10 +740,12 @@ pub const Replica = struct { /// A replica of a session elsewhere, as its walker dump (core/dump.zig) /// is: what a client of a socket joins with. `opts` are this process's /// (a replica's own allocators and names); its core's allocator is the - /// rollback epoch's, but for images and PDFs, whose workers allocate on - /// threads of their own. ponytail: a picture's pixels freed while a copy - /// is held are not put off; a rollback over one shows an image a frame - /// late at worst... or reads freed pixels, which no test session has. + /// rollback epoch's, its `image_allocator` too (a kind's slot whose + /// state is not plain: a speculation that closes one frees it into the + /// epoch, put off, as any). ponytail: a picture's pixels (`Kind.heap`) + /// freed while a copy is held are not put off; a rollback over one shows + /// an image a frame late at worst... or reads freed pixels, which no test + /// session has. pub fn initDump(gpa: Allocator, id: u16, view: u8, bytes: []const u8, opts: pardes.Options) !*Replica { const r = try gpa.create(Replica); r.* = .{ .gpa = gpa, .id = id, .view = view, .t = tape.memory(gpa), .guess = true }; @@ -761,7 +763,7 @@ pub const Replica = struct { var o = opts; o.frame_allocator = epoch; o.tree_sitter_allocator = epoch; - o.image_allocator = o.image_allocator orelse std.heap.c_allocator; + o.image_allocator = epoch; // The session's closed panes and Recent are the authority's. o.state = .off; // Its emulators off the epoch, as `terminal.create` makes a screen @@ -4929,3 +4931,97 @@ test "a kind's job answered at once inside its host's call (no worker free) is t try std.testing.expectEqual(@as(u32, 1), std.mem.bytesToValue(u32, a.core.panes[id].?.addon.?.plain.?[0..4])); try std.testing.expectEqualSlices(u8, a.core.panes[id].?.addon.?.plain.?, r.core().panes[id].?.addon.?.plain.?); } + +test "a closed terminal with a kitty image brought back on a client: its image drawn under a speculation, rolled back by another's unit, is drawn again from what the emulator holds" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true); + defer a.deinit(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + defer r.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + // A 2x2 picture in RGB (f=24): drawn, the emulator keeps it as RGBA + // of its own (TerminalImages.converted). + a.output(.{ .output = .{ .pane = shell, .bytes = "\x1b_Ga=T,i=7,f=24,s=2,v=2;ESIzESIzESIzESIz\x1b\\" } }); + r.confirm((try a.pending()).?); + // Closed and brought back: the client loads the session's dump of it, + // its image with it. + a.core.view().active = shell; + r.confirm(try a.local(.{ .command = "Del" })); + var line: [32]u8 = undefined; + r.confirm(try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look @p{d}", .{a.core.store.closed.items[0].serial}) })); + const back = for (r.core().panes) |slot| { + if (slot) |pane| if (pane.terminal) |state| break state; + } else return error.NotBack; + try std.testing.expect(pardes.Term.hasImages(back)); + // Typing goes to the text pane (the host's write of `active`, taped). + a.core.view().active = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.file != null) break id; + } else return error.NoTextPane; + r.confirm(try a.local(.{ .focus = true })); + const Seen = struct { + px: ?[4]u8 = null, + fn take(ctx: ?*anyopaque, s: *const pardes.Surface) void { + const seen: *@This() = @ptrCast(@alignCast(ctx.?)); + for (s.placements.items) |pl| seen.px = pl.raster.bytes()[0..4].*; + } + }; + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + // Its own key, at once, a copy held; a frame drawn over it. + try key(r, 'o', &sent); + try std.testing.expect(r.copy.held); + var seen: Seen = .{}; + r.draw(.{ .ctx = &seen, .f = Seen.take }); + try std.testing.expectEqual([4]u8{ 0x11, 0x22, 0x33, 0xff }, seen.px.?); + // The session's own key first: back to the copy, the key applied again. + r.confirm(try a.local(.{ .key = .{ .cp = 'l', .text = "l" } })); + r.settle(); + seen = .{}; + r.draw(.{ .ctx = &seen, .f = Seen.take }); + try std.testing.expectEqual([4]u8{ 0x11, 0x22, 0x33, 0xff }, seen.px.?); + r.confirm(try a.take(sent.items[0])); + r.settle(); + try agrees(a, r); +} + +test "a far client's own Del of a pane whose kind's state is not plain, rolled back by another's unit: the pane comes back whole, and is closed again with the session's order" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + var words: [64]pardes.Kind.LoadedWord = undefined; + const counter = pardes.Kind.loadedOf(&Counter.ops, "/x/counter.so", @splat(7), &words); + _ = try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &counter } }); + _ = try a.local(.{ .command = "Counter" }); + // Joined from the dump: the kind's pane is a line here (no code), its + // slot the dump's. + const r = try joinedNow(a, 1, 0); + defer r.deinit(); + const id = r.core().view().active; + try std.testing.expect(r.core().panes[id].?.addon != null); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + // Its own Del at once, and a key after it: the pane's teardown (its + // slot freed) is that key's step's (`reapPanes`). + try sent.append(gpa, try gpa.dupe(u8, try r.input(.{ .command = "Del" }))); + try key(r, 'j', &sent); + try std.testing.expectEqual(@as(usize, 2), r.applied); + try std.testing.expect(r.core().panes[id] == null); + // The session's own key first: back to the copy, the pane with it. + r.confirm(try a.local(.{ .key = .{ .cp = 'l', .text = "l" } })); + r.settle(); + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + } + try agrees(a, r); + try std.testing.expect(r.core().panes[id] == null); +} diff --git a/core/term/TerminalImages.zig b/core/term/TerminalImages.zig index 1420d9e3..feba5ddc 100644 --- a/core/term/TerminalImages.zig +++ b/core/term/TerminalImages.zig @@ -29,6 +29,8 @@ pub const storage_limit = 64 * 1024 * 1024; petscii: bool = false, /// Pixels in RGBA for images ghostty-vt holds in another format (RGB, /// grey): only for images on screen, dropped the frame they are not. +/// The emulator's own allocator's, as the emulator is (`draw`'s `own`): +/// never a client core's, whose rollback would free it under the emulator. converted: std.AutoHashMapUnmanaged(u32, Converted) = .empty, /// When the next frame of a playing animation (a=a) is due, in ms on the /// core's clock (`Pardes.stepped_ns`): the core wakes for it (nextWake). @@ -160,9 +162,11 @@ fn decodePng(gpa: std.mem.Allocator, bytes: []const u8) ghostty_vt.sys.DecodeErr /// Draw the active screen's images. `rows_of[v]` is the body row the /// viewport's row `v` is shown on, if it is shown; `cols` how many of its -/// columns the body shows, from `tx`. +/// columns the body shows, from `tx`. The caches are `own`'s (the +/// emulator's), `out` is `gpa`'s. pub fn draw( ti: *TerminalImages, + own: std.mem.Allocator, gpa: std.mem.Allocator, vt: *ghostty_vt.Terminal, where: plain.term.ImagesAt, @@ -173,7 +177,7 @@ pub fn draw( ) void { var conv = ti.converted.valueIterator(); while (conv.next()) |c| c.used = false; - defer ti.prune(gpa); + defer ti.prune(own); const screen = vt.screens.active; const storage = &screen.kitty_images; @@ -187,7 +191,7 @@ pub fn draw( const bottom = pages.getBottomRight(.viewport) orelse return; const cw: u32 = @max(1, where.cell_w); const ch: u32 = @max(1, where.cell_h); - const ctx: Ctx = .{ .ti = ti, .gpa = gpa, .out = out, .pane = where.pane, .serial = where.serial, .tx = where.tx, .body_y = where.body_y, .cols = where.cols, .rows_of = rows_of, .cw = cw, .ch = ch }; + const ctx: Ctx = .{ .ti = ti, .own = own, .gpa = gpa, .out = out, .pane = where.pane, .serial = where.serial, .tx = where.tx, .body_y = where.body_y, .cols = where.cols, .rows_of = rows_of, .cw = cw, .ch = ch }; // Handed on lowest z first (the storage's order is a hash map's), the // order a shell draws them in; equal z keeps the order they came in. const first = out.items.len; @@ -268,6 +272,7 @@ pub fn draw( const Ctx = struct { ti: *TerminalImages, + own: std.mem.Allocator, gpa: std.mem.Allocator, out: *std.ArrayList(plain.Placement), pane: u8, @@ -287,7 +292,7 @@ const Ctx = struct { const x0 = @max(dst.x, 0); const x1 = @min(dst.x + dst.w, @as(i64, ctx.cols) * cw); if (x1 <= x0) return; - const rgba = ctx.ti.pixelsOf(ctx.gpa, img) orelse return; + const rgba = ctx.ti.pixelsOf(ctx.own, img) orelse return; const first_row = @divFloor(@max(dst.y, 0), ch); const last_row = @min(@divFloor(dst.y + dst.h - 1, ch), @as(i64, @intCast(ctx.rows_of.len)) - 1); var v = first_row; diff --git a/core/term/terminal.zig b/core/term/terminal.zig index d328748e..d99cfb79 100644 --- a/core/term/terminal.zig +++ b/core/term/terminal.zig @@ -721,12 +721,11 @@ pub fn scrollbackLen(state: ?*State) usize { return @intCast(counting.fullCount()); } -/// Its emulator and what it holds, freed; its kitty images' caches were -/// `gpa`'s. -pub fn close(state: *State, gpa: std.mem.Allocator) void { - if (comptime images_enabled) state.images.deinit(gpa); - state.stream.deinit(); +/// Its emulator and what it holds, freed. +pub fn close(state: *State) void { const own = state.gpa; + if (comptime images_enabled) state.images.deinit(own); + state.stream.deinit(); state.vt.deinit(own); own.destroy(state); } @@ -1502,7 +1501,7 @@ pub fn images(state: *State, gpa: std.mem.Allocator, at: ImagesAt, out: *std.Arr // Nothing to draw, no placeholder to blank and nothing cached to let // go: the common case costs the rows' flags. if (!any_placeholder and screen.kitty_images.placements.count() == 0 and state.images.converted.count() == 0) return; - state.images.draw(gpa, &state.vt, at, rows_of[0..vp_rows], out, frame, frame_cols); + state.images.draw(state.gpa, gpa, &state.vt, at, rows_of[0..vp_rows], out, frame, frame_cols); } test "the luminance table answers exactly what ghostty computes" { |
