From a9e33e52d789c7433da1da17a8c82093c9114243 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 24 Oct 2026 15:23:45 -0300 Subject: anim 7: what the probe found: the paced wait spins (asleep, the CPU's clock fell and an input's frame took 2-5x as long), a glide measures the core's line count first and emits only the rows it shows, a far jump paints only its view, a long line 16 KB round the caret, the caret's sideways scroll measured from the view's edge, a paint never joined on the loop nor asked while the view stands still, rewritten generated panes do not glide, pictures decode off the loop --- src/image.zig | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) (limited to 'src/image.zig') diff --git a/src/image.zig b/src/image.zig index 5797ce9e..19dbb9e4 100644 --- a/src/image.zig +++ b/src/image.zig @@ -1,5 +1,6 @@ //! Image decoding, pixel placement, and glyph approximation. const std = @import("std"); +const builtin = @import("builtin"); const zstbi = @import("zstbi"); /// The 16-colour ANSI table below is the only thing this file ever wanted from /// the emulator, and a build without one still renders images — see @@ -767,7 +768,9 @@ pub fn readable(bytes: []const u8) bool { /// decode + downscale to RGBA, gpa-owned. Returns null on any failure — the /// pane then simply shows a blank body. -pub fn decode(gpa: std.mem.Allocator, bytes: []const u8) ?struct { rgba: []u8, w: usize, h: usize } { +const Decoded = struct { rgba: []u8, w: usize, h: usize }; + +pub fn decode(gpa: std.mem.Allocator, bytes: []const u8) ?Decoded { if (bytes.len == 0 or bytes.len > std.math.maxInt(c_int)) return null; // Bounded before decoding: a few KB of PNG can name gigapixels. var iw: c_int = 0; @@ -812,8 +815,14 @@ pub const State = struct { raw: []u8 = &.{}, grid: GlyphArt.Grid = .{ .cells = &.{}, .cols = 0, .rows = 0 }, grid_key: GridKey = .{}, + /// The decode under way off the loop (ensureDecoded), and its answer. + decoder: ?std.Thread = null, + decoded: std.atomic.Value(bool) = .init(false), + answer: ?Decoded = null, pub fn deinit(state: *State, gpa: std.mem.Allocator) void { + if (state.decoder) |t| t.join(); + if (state.answer) |a| gpa.free(a.rgba); gpa.free(state.path); if (state.rgba.len > 0) gpa.free(state.rgba); if (state.raw.len > 0) gpa.free(state.raw); @@ -941,15 +950,41 @@ pub fn tagPrefix(arena: std.mem.Allocator, state: *const State, glyphs: bool) ![ ); } +/// The picture decoded off the loop: the first draw starts a thread and +/// draws the pane without it (its tag and frame at once); the frames after +/// it look (Pardes.nextWake asks for them) and the picture lands on the +/// first one after the decode (80 ms in ReleaseSafe, 290 in Debug, for the +/// docs' screenshot: all of it once on the frame before). +/// ponytail: a thread a picture; a pool if many open at once. fn ensureDecoded(p: *pardes.Pardes, state: *State) void { - if (state.tried) return; - state.tried = true; - if (state.raw.len == 0) return; // nothing was readable at open - if (decode(p.image_gpa, state.raw)) |decoded| { + if (state.decoder) |t| { + if (!state.decoded.load(.acquire)) return; + t.join(); + state.decoder = null; + } + if (state.answer) |decoded| { + state.answer = null; state.rgba = decoded.rgba; state.iw = decoded.w; state.ih = decoded.h; } + if (state.tried) return; + state.tried = true; + if (state.raw.len == 0) return; // nothing was readable at open + const Work = struct { + fn run(gpa: std.mem.Allocator, st: *State) void { + st.answer = decode(gpa, st.raw); + st.decoded.store(true, .release); + } + }; + if (comptime pardes.hosted and !builtin.is_test) { + if (std.Thread.spawn(.{}, Work.run, .{ p.image_gpa, state })) |t| { + state.decoder = t; + return; + } else |_| {} + } + Work.run(p.image_gpa, state); + ensureDecoded(p, state); } fn ensureGrid(p: *pardes.Pardes, state: *State, cols: u16, rows: u16) void { -- cgit v1.3