From 71e5a0f4a8685b385fac762995d45575b638179f Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 5 Nov 2026 22:21:45 -0300 Subject: kinds #1, the paint contract: a picture over the cells is one Placement (raster: ptr, w, h, stride; src in pixels; dst in the frame's cells; at, where in them in pixels, or none for the frontend to fit; key, the pixels' identity by pane serial, page and revision; a terminal program's kitty z; dirty, the rows changed since an older key; art, the pane's Petscii, Palette and Ascii), where ImagePlace carried native.{revision, page, fit, pan_x, pan_y, geometry, pixel_offset_y, paper_bg, exact}, cell rects and an ImageCacheKey of three shapes by build; a PDF strip's scroll is its body region's scroll_px, whose fraction the window draws as pixel_offset_y was; glyph art leaves the core for src/Art.zig, the frontends' (the matcher, both palettes and a cache by key, ex image.zig's GlyphArt and TerminalImages' glyph cache): the core always hands an image pane, a PDF's pages and a terminal's kitty images as placements, whatever the frontend shows, and the tty draws them as glyph art without kitty graphics or when the pane asks, the window when the pane asks; the four words set the same pane bytes as before and the frontend reads them off the placement, so config.zig, builtins.zig and exec.zig are untouched; /screen lists a frame's placements as images (only a frame with pictures has the field); no golden changes: the snapshot goldens are the tty's, which draws the same art (images.snap and tagbottomimage.snap pass unchanged, and the tty's bytes for an image, its Palette and its Ascii are byte-identical to the base's), no /screen golden holds a picture, and the window's goldens hash the same; tests: kitty.zig's glyph-art case becomes its core half (a placement over the 8x4 cells, the cells the pane's) with the art in tui.zig's own test, terminal.zig's glyph-art case the same, surface.zig's two ImageCacheKey cases one keyOf case, the GlyphArt cases move to Art.zig with a frame case; gates: core-test 959/959, behaviour-test 244/244, unit-test 1486/1487 tui and 1530/1531 gui, pane-test 171/172, snap all ok, process-test 0 of 37 failed on both, hxdiff 19 and hxparity 6 mismatches by the base's names, replicas, gui-golden, monkey-gui, kitty-test 44/44, kitty-harness and image-harness on both, daemon.py main and window-clients Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/gui/gui.zig | 196 ++++++++++++++++++++++++++------------------------------ 1 file changed, 92 insertions(+), 104 deletions(-) (limited to 'src/gui') diff --git a/src/gui/gui.zig b/src/gui/gui.zig index d6efa27c..f9b757f8 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -15,6 +15,7 @@ const file_watch = @import("../file_watch.zig"); const deck = @import("deck.zig"); const pet = @import("pet.zig"); const JumpTrail = @import("../jump_trail.zig"); +const Art = @import("../Art.zig"); const fonts = @import("../fonts.zig"); // the fallback faces const selection_pipe = @import("../selection_pipe.zig"); const c_heap = @import("c_heap"); @@ -460,7 +461,7 @@ const ImageInstance = extern struct { const opaque_bg_bit: u32 = 0x2000_0000; const PreparedImage = struct { - place: pardes.ImagePlace, + place: pardes.Placement, texture: *c.SDL_GPUTexture, }; @@ -958,8 +959,13 @@ const Gui = struct { image_vbuf: ?*c.SDL_GPUBuffer = null, image_vxfer: ?*c.SDL_GPUTransferBuffer = null, image_capacity: u32 = 0, - native_images: std.AutoHashMapUnmanaged(pardes.ImageCacheKey, *c.SDL_GPUTexture) = .empty, + /// The textures of the placements shown, by key. + native_images: std.AutoHashMapUnmanaged(u64, *c.SDL_GPUTexture) = .empty, prepared_images: std.ArrayListUnmanaged(PreparedImage) = .empty, + /// The glyph art of the placements whose panes ask for it (Petscii + /// on), and the frame copy it is drawn into. + art: Art = .{}, + art_arena: ?std.heap.ArenaAllocator = null, /// renderFrame's cover of the grid (coverFrame). cover: std.ArrayListUnmanaged(Cover) = .empty, /// renderFrame's decor instances, group by group (buildDecor). @@ -1658,6 +1664,8 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di clearNativeImages(&g); g.native_images.deinit(gpa); g.prepared_images.deinit(gpa); + g.art.deinit(gpa); + if (g.art_arena) |*arena| arena.deinit(); g.cover.deinit(gpa); g.decor.deinit(gpa); } @@ -5159,7 +5167,7 @@ fn scrollScissor(g: *const Gui, layout: CellLayout, sw: u32, sh: u32) c.SDL_Rect return .{ .x = x0, .y = y0, .w = x1 - x0, .h = y1 - y0 }; } -fn releaseNativeImage(g: *Gui, key: pardes.ImageCacheKey) void { +fn releaseNativeImage(g: *Gui, key: u64) void { if (g.native_images.fetchRemove(key)) |removed| c.SDL_ReleaseGPUTexture(g.device, removed.value); } @@ -5170,79 +5178,62 @@ fn clearNativeImages(g: *Gui) void { g.native_images.clearRetainingCapacity(); } -fn surfaceHasNativeKey(surface: *const pardes.Surface, key: pardes.ImageCacheKey) bool { - for (surface.images.items) |place| { - if (validImageBytes(place) != null and place.cacheKey().eql(key)) return true; +/// A placement the window shows as a texture: pixels, not glyph art. +fn texturePlacement(place: pardes.Placement) bool { + return !place.art.glyph and validImageBytes(place) != null; +} + +fn surfaceHasNativeKey(surface: *const pardes.Surface, key: u64) bool { + for (surface.placements.items) |place| { + if (texturePlacement(place) and place.key == key) return true; } return false; } -fn validImageBytes(place: pardes.ImagePlace) ?u32 { - if (place.iw == 0 or place.ih == 0 or - place.iw > std.math.maxInt(u32) or place.ih > std.math.maxInt(u32)) return null; - const pixels = std.math.mul(usize, place.iw, place.ih) catch return null; - const bytes = std.math.mul(usize, pixels, 4) catch return null; - if (place.rgba.len != bytes or bytes > std.math.maxInt(u32)) return null; +fn validImageBytes(place: pardes.Placement) ?u32 { + const r = place.raster; + // Rows packed: a texture upload takes no stride. + if (r.w == 0 or r.h == 0 or r.stride != r.w * 4) return null; + const bytes = std.math.mul(usize, r.stride, r.h) catch return null; + if (bytes > std.math.maxInt(u32)) return null; return @intCast(bytes); } -const NativeBounds = struct { w: u32, h: u32 }; - -fn nativeBounds(g: *const Gui, place: pardes.ImagePlace) ?NativeBounds { - const bound_w = std.math.mul(u32, place.w, g.cell_w) catch return null; - const bound_h = std.math.mul(u32, place.h, g.cell_h) catch return null; - return .{ .w = bound_w, .h = bound_h }; -} - -fn nativePlaceGeometry(g: *const Gui, place: pardes.ImagePlace) ?pardes.image.NativeGeometry { - if (comptime pardes.pdf_enabled) { - if (place.native.geometry) |geometry| return geometry; - const bounds = nativeBounds(g, place) orelse return null; - return pardes.image.nativeGeometry( - place.iw, - place.ih, - bounds.w, - bounds.h, - place.native.fit, - place.native.pan_x, - place.native.pan_y, - ); - } - return null; -} - -fn nativePlaceDrawable(g: *const Gui, place: pardes.ImagePlace) bool { - if (comptime pardes.pdf_enabled) return nativePlaceGeometry(g, place) != null; - const bounds = nativeBounds(g, place) orelse return false; - return pardes.image.containGeometry(place.iw, place.ih, bounds.w, bounds.h) != null; +/// Where the placement's pixels go in its cells: where it says, or fitted +/// there (no larger than it is, at the top left). +fn nativePlaceGeometry(g: *const Gui, place: pardes.Placement) ?pardes.image.NativeGeometry { + if (place.at) |at| return .{ .src = place.src, .dst = at }; + const bound_w = std.math.mul(u32, place.dst.w, g.cell_w) catch return null; + const bound_h = std.math.mul(u32, place.dst.h, g.cell_h) catch return null; + const fit = pardes.image.containGeometry(place.src.w, place.src.h, bound_w, bound_h) orelse return null; + return .{ .src = place.src, .dst = fit.dst }; } fn uploadNativeTexture( g: *Gui, gpa: std.mem.Allocator, cmd: *c.SDL_GPUCommandBuffer, - place: pardes.ImagePlace, + place: pardes.Placement, ) !void { const byte_len = validImageBytes(place) orelse return error.BadImage; - const key = place.cacheKey(); + const key = place.key; if (g.native_images.contains(key)) return; - if (comptime pardes.pdf_enabled) if (place.patch) |patch| patch: { - // The core changed only these rows since a revision we still hold: + const r = place.raster; + const row = r.stride; + if (place.dirty) |dirty| dirty: { + // The core changed only these rows since a picture we still hold: // write them into that texture rather than sending the whole page. - var base = key; - base.revision = patch.from; - if (patch.y + patch.h > place.ih) break :patch; - const texture = g.native_images.get(base) orelse break :patch; - const row = place.iw * 4; - try uploadTextureRows(g, cmd, texture, place.rgba[patch.y * row ..][0 .. patch.h * row], place.iw, patch.y, patch.h); + if (dirty.y + dirty.h > r.h) break :dirty; + const texture = g.native_images.get(dirty.from) orelse break :dirty; + try uploadTextureRows(g, cmd, texture, r.bytes()[dirty.y * row ..][0 .. dirty.h * row], r.w, dirty.y, dirty.h); try g.native_images.put(gpa, key, texture); - _ = g.native_images.remove(base); + _ = g.native_images.remove(dirty.from); return; - }; + } - const texture = texture2d(g.device, c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM, c.SDL_GPU_TEXTUREUSAGE_SAMPLER, @intCast(place.iw), @intCast(place.ih)) orelse return error.GpuCreate; + const texture = texture2d(g.device, c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM, c.SDL_GPU_TEXTUREUSAGE_SAMPLER, r.w, r.h) orelse return error.GpuCreate; errdefer c.SDL_ReleaseGPUTexture(g.device, texture); - try uploadTextureRows(g, cmd, texture, place.rgba[0..byte_len], place.iw, 0, place.ih); + try uploadTextureRows(g, cmd, texture, r.bytes()[0..byte_len], r.w, 0, r.h); try g.native_images.put(gpa, key, texture); } @@ -5289,54 +5280,46 @@ fn uploadTextureRows( c.SDL_EndGPUCopyPass(copy); } -fn appendPreparedImage(g: *Gui, gpa: std.mem.Allocator, place: pardes.ImagePlace) void { - const texture = g.native_images.get(place.cacheKey()) orelse return; - if (!nativePlaceDrawable(g, place)) return; +fn appendPreparedImage(g: *Gui, gpa: std.mem.Allocator, place: pardes.Placement) void { + const texture = g.native_images.get(place.key) orelse return; + if (nativePlaceGeometry(g, place) == null) return; g.prepared_images.append(gpa, .{ .place = place, .texture = texture }) catch {}; } fn emitPreparedImage( g: *Gui, + surface: *const pardes.Surface, instance: *ImageInstance, prepared: PreparedImage, win_w: f32, win_h: f32, ) bool { const place = prepared.place; - const body_x = @as(u32, place.x) * g.cell_w; - const body_y = @as(u32, place.y) * g.cell_h; - if (comptime pardes.pdf_enabled) { - const geometry = nativePlaceGeometry(g, place) orelse return false; - const px0 = @as(f32, @floatFromInt(body_x + geometry.dst.x)); - const py0 = @as(f32, @floatFromInt(body_y + geometry.dst.y)) + place.native.pixel_offset_y; - const px1 = px0 + @as(f32, @floatFromInt(geometry.dst.w)); - const py1 = py0 + @as(f32, @floatFromInt(geometry.dst.h)); - const image_w: f32 = @floatFromInt(place.iw); - const image_h: f32 = @floatFromInt(place.ih); - instance.* = .{ - .x0 = (px0 / win_w) * 2.0 - 1.0, - .y0 = 1.0 - (py0 / win_h) * 2.0, - .x1 = (px1 / win_w) * 2.0 - 1.0, - .y1 = 1.0 - (py1 / win_h) * 2.0, - .u0 = @as(f32, @floatFromInt(geometry.src.x)) / image_w, - .v0 = @as(f32, @floatFromInt(geometry.src.y)) / image_h, - .u1 = @as(f32, @floatFromInt(geometry.src.x + geometry.src.w)) / image_w, - .v1 = @as(f32, @floatFromInt(geometry.src.y + geometry.src.h)) / image_h, - }; - } else { - const bounds = nativeBounds(g, place) orelse return false; - const fit = (pardes.image.containGeometry(place.iw, place.ih, bounds.w, bounds.h) orelse return false).dst; - instance.* = .{ - .x0 = (@as(f32, @floatFromInt(body_x)) / win_w) * 2.0 - 1.0, - .y0 = 1.0 - (@as(f32, @floatFromInt(body_y)) / win_h) * 2.0, - .x1 = (@as(f32, @floatFromInt(body_x + fit.w)) / win_w) * 2.0 - 1.0, - .y1 = 1.0 - (@as(f32, @floatFromInt(body_y + fit.h)) / win_h) * 2.0, - .u0 = 0, - .v0 = 0, - .u1 = 1, - .v1 = 1, - }; - } + const body_x = @as(u32, place.dst.x) * g.cell_w; + const body_y = @as(u32, place.dst.y) * g.cell_h; + const geometry = nativePlaceGeometry(g, place) orelse return false; + // A strip's pictures lie at its whole pixel: up by the fraction left. + var scroll: f64 = 0; + for (surface.regionList()) |r| if (r.kind == .body and r.owner == place.pane) { + scroll = r.scroll_px; + break; + }; + const px0 = @as(f32, @floatFromInt(body_x + geometry.dst.x)); + const py0 = @as(f32, @floatFromInt(body_y + geometry.dst.y)) - @as(f32, @floatCast(scroll - @floor(scroll))); + const px1 = px0 + @as(f32, @floatFromInt(geometry.dst.w)); + const py1 = py0 + @as(f32, @floatFromInt(geometry.dst.h)); + const image_w: f32 = @floatFromInt(place.raster.w); + const image_h: f32 = @floatFromInt(place.raster.h); + instance.* = .{ + .x0 = (px0 / win_w) * 2.0 - 1.0, + .y0 = 1.0 - (py0 / win_h) * 2.0, + .x1 = (px1 / win_w) * 2.0 - 1.0, + .y1 = 1.0 - (py1 / win_h) * 2.0, + .u0 = @as(f32, @floatFromInt(geometry.src.x)) / image_w, + .v0 = @as(f32, @floatFromInt(geometry.src.y)) / image_h, + .u1 = @as(f32, @floatFromInt(geometry.src.x + geometry.src.w)) / image_w, + .v1 = @as(f32, @floatFromInt(geometry.src.y + geometry.src.h)) / image_h, + }; return true; } @@ -5349,13 +5332,13 @@ fn prepareNativeImages( sw: u32, sh: u32, ) bool { - for (surface.images.items) |place| { - if (validImageBytes(place) == null) continue; + for (surface.placements.items) |place| { + if (!texturePlacement(place)) continue; uploadNativeTexture(g, gpa, cmd, place) catch continue; } while (true) { - var stale: [pardes.MAX_PANES]pardes.ImageCacheKey = undefined; + var stale: [pardes.MAX_PANES]u64 = undefined; var stale_len: usize = 0; var iterator = g.native_images.iterator(); while (iterator.next()) |entry| { @@ -5370,8 +5353,8 @@ fn prepareNativeImages( g.prepared_images.clearRetainingCapacity(); // The pictures are the page's: the other groups float over them. - for (surface.images.items) |place| - if (validImageBytes(place) != null) appendPreparedImage(g, gpa, place); + for (surface.placements.items) |place| + if (texturePlacement(place)) appendPreparedImage(g, gpa, place); groups.items[0].image_start = 0; groups.items[0].image_count = std.math.cast(u32, g.prepared_images.items.len) orelse return false; @@ -5385,7 +5368,7 @@ fn prepareNativeImages( const win_h: f32 = @floatFromInt(sh); var idx: u32 = 0; for (g.prepared_images.items) |prepared| { - if (!emitPreparedImage(g, &instances[idx], prepared, win_w, win_h)) continue; + if (!emitPreparedImage(g, surface, &instances[idx], prepared, win_w, win_h)) continue; idx += 1; } c.SDL_UnmapGPUTransferBuffer(g.device, g.image_vxfer.?); @@ -5399,12 +5382,12 @@ fn prepareNativeImages( return true; } -fn imageScissor(g: *const Gui, place: pardes.ImagePlace) c.SDL_Rect { +fn imageScissor(g: *const Gui, place: pardes.Placement) c.SDL_Rect { return .{ - .x = @intCast(@as(u32, place.x) * g.cell_w), - .y = @intCast(@as(u32, place.y) * g.cell_h), - .w = @intCast(@as(u32, place.w) * g.cell_w), - .h = @intCast(@as(u32, place.h) * g.cell_h), + .x = @intCast(@as(u32, place.dst.x) * g.cell_w), + .y = @intCast(@as(u32, place.dst.y) * g.cell_h), + .w = @intCast(@as(u32, place.dst.w) * g.cell_w), + .h = @intCast(@as(u32, place.dst.h) * g.cell_h), }; } @@ -5440,8 +5423,13 @@ fn renderFrame( g: *Gui, gpa: std.mem.Allocator, core: *pardes.Pardes, - surface: *const pardes.Surface, + canonical: *const pardes.Surface, ) !bool { + // The pictures a pane asks to see as glyph art, drawn into a copy of + // the frame (Art.zig); the rest are textures. + if (g.art_arena == null) g.art_arena = .init(gpa); + _ = g.art_arena.?.reset(.retain_capacity); + const surface = g.art.frame(gpa, g.art_arena.?.allocator(), canonical, true, g.cell_h) catch canonical; if (surface.pointer_shape != g.pointer_shape) { g.pointer_shape = surface.pointer_shape; const cursor = switch (g.pointer_shape) { -- cgit v1.3