summaryrefslogtreecommitdiff
path: root/src/TerminalImages.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/TerminalImages.zig')
-rw-r--r--src/TerminalImages.zig99
1 files changed, 21 insertions, 78 deletions
diff --git a/src/TerminalImages.zig b/src/TerminalImages.zig
index 0131568d..86c20d2c 100644
--- a/src/TerminalImages.zig
+++ b/src/TerminalImages.zig
@@ -2,8 +2,8 @@
//! previews, `kitten icat`) drawn where it placed them. ghostty-vt keeps
//! the images and placements; this reads them each frame, cuts each one
//! into the cell rows it covers, clipped to the rows the pane shows, and
-//! hands them on: as pixels (`ImagePlace`, exact geometry) to a shell that
-//! draws them, else as glyph art in the cells, as an image pane's Petscii.
+//! hands them on as placements, each at its exact pixels: a frontend draws
+//! them, or draws them as glyph art (Art.zig), as an image pane's.
//! Both kinds of placement are drawn: one on a cell (`a=T`/`a=p`), which
//! moves with the text it was put on, and a virtual one (`U=1`) shown
//! through the U+10EEEE placeholder cells the program prints.
@@ -14,28 +14,23 @@ const image = @import("image.zig");
const TerminalImages = @This();
const gfx = ghostty_vt.kitty.graphics;
-const GlyphArt = image.GlyphArt;
const PixelRect = image.PixelRect;
/// The bytes ghostty-vt may hold for one screen's images (it evicts the
/// oldest past this): a 4K picture in RGBA, with room for a second.
pub const storage_limit = 64 * 1024 * 1024;
-/// This pane draws its images as glyph art even where pixels could be
+/// This pane asks for its images as glyph art even where pixels could be
/// drawn (the Petscii builtin; TermImages sets it for a new pane).
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.
converted: std.AutoHashMapUnmanaged(u32, Converted) = .empty,
-/// Glyph art for the fragments drawn as cells, kept while on screen.
-glyphs: std.AutoHashMapUnmanaged(GlyphKey, Glyphs) = .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).
animation_due_ms: ?u64 = null,
const Converted = struct { generation: u64, rgba: []u8, used: bool = true };
-const Glyphs = struct { cells: []GlyphArt.Cell, used: bool = true };
-const GlyphKey = struct { id: u32, generation: u64, src: PixelRect, cols: u16 };
/// One image's rectangle in the pane, in pixels from the viewport's top
/// left corner (above or left of it is negative).
@@ -111,9 +106,6 @@ pub fn deinit(ti: *TerminalImages, gpa: std.mem.Allocator) void {
var conv = ti.converted.valueIterator();
while (conv.next()) |c| gpa.free(c.rgba);
ti.converted.deinit(gpa);
- var gl = ti.glyphs.valueIterator();
- while (gl.next()) |g| gpa.free(g.cells);
- ti.glyphs.deinit(gpa);
}
/// Accept PNG (`f=100`) as well as raw pixels: ghostty-vt built as a
@@ -144,8 +136,6 @@ pub fn draw(
) void {
var conv = ti.converted.valueIterator();
while (conv.next()) |c| c.used = false;
- var gl = ti.glyphs.valueIterator();
- while (gl.next()) |g| g.used = false;
defer ti.prune(p.gpa);
const screen = vt.screens.active;
@@ -160,13 +150,12 @@ pub fn draw(
const bottom = pages.getBottomRight(.viewport) orelse return;
const cw: u32 = @max(1, p.cell_pixels.w);
const ch: u32 = @max(1, p.cell_pixels.h);
- const pixels = p.frontend.native_images and !ti.petscii;
- const ctx: Ctx = .{ .ti = ti, .p = p, .s = s, .pane = pane_id, .serial = serial, .tx = tx, .body_y = body_y, .cols = cols, .rows_of = rows_of, .cw = cw, .ch = ch, .pixels = pixels };
+ const ctx: Ctx = .{ .ti = ti, .p = p, .pane = pane_id, .serial = serial, .tx = tx, .body_y = body_y, .cols = 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 = s.images.items.len;
- defer std.sort.insertion(pardes.ImagePlace, s.images.items[first..], {}, struct {
- fn lessThan(_: void, a: pardes.ImagePlace, b: pardes.ImagePlace) bool {
+ const first = s.placements.items.len;
+ defer std.sort.insertion(pardes.Placement, s.placements.items[first..], {}, struct {
+ fn lessThan(_: void, a: pardes.Placement, b: pardes.Placement) bool {
return a.z < b.z;
}
}.lessThan);
@@ -243,7 +232,6 @@ pub fn draw(
const Ctx = struct {
ti: *TerminalImages,
p: *pardes.Pardes,
- s: *pardes.Surface,
pane: u8,
serial: u32,
tx: u16,
@@ -252,7 +240,6 @@ const Ctx = struct {
rows_of: []const ?u16,
cw: u32,
ch: u32,
- pixels: bool,
/// Draw `src` of `img` over `dst`, one cell row of it at a time: a row
/// is shown, or not, or shown elsewhere, as the body lays it out.
@@ -280,38 +267,20 @@ const Ctx = struct {
};
const c0: u16 = @intCast(@divFloor(x0, cw));
const c1: u16 = @intCast(@divFloor(x1 + cw - 1, cw));
- if (ctx.pixels) {
- _ = ctx.p.appendImagePlace(.{
- .pane = ctx.pane,
- .serial = ctx.serial,
- .native = .{
- .page = img.id,
- .revision = @truncate(img.generation),
- .exact = true,
- .geometry = .{ .src = piece, .dst = .{
- .x = @intCast(x0 - @as(i64, c0) * cw),
- .y = @intCast(y0 - v * ch),
- .w = @intCast(x1 - x0),
- .h = @intCast(y1 - y0),
- } },
- },
- .x = ctx.tx + c0,
- .y = ctx.body_y + row,
- .w = c1 - c0,
- .h = 1,
- .rgba = rgba,
- .iw = img.width,
- .ih = img.height,
- .z = z,
- });
- continue;
- }
- // Glyph art fills whole cells: a sliver of a row is left out.
- if ((y1 - y0) * 2 < ch) continue;
- const cells = ctx.ti.glyphsOf(ctx.p.gpa, img, rgba, piece, c1 - c0) orelse continue;
- for (cells, c0..) |*cell, x| ctx.s.set(ctx.tx + @as(u16, @intCast(x)), ctx.body_y + row, cell.glyph[0..cell.glyph_len], .{
- .fg = .{ .rgb = GlyphArt.commodore[cell.fg] },
- .bg = .{ .rgb = GlyphArt.commodore[cell.bg] },
+ _ = ctx.p.appendPlacement(.{
+ .pane = ctx.pane,
+ .raster = .of(rgba, img.width, img.height),
+ .src = piece,
+ .dst = .{ .x = ctx.tx + c0, .y = ctx.body_y + row, .w = c1 - c0, .h = 1 },
+ .at = .{
+ .x = @intCast(x0 - @as(i64, c0) * cw),
+ .y = @intCast(y0 - v * ch),
+ .w = @intCast(x1 - x0),
+ .h = @intCast(y1 - y0),
+ },
+ .key = pardes.Placement.keyOf(ctx.serial, img.id, @truncate(img.generation)),
+ .z = z,
+ .art = .{ .glyph = ctx.ti.petscii },
});
}
}
@@ -362,24 +331,6 @@ fn convert(gpa: std.mem.Allocator, img: gfx.Image, n: usize) ?[]u8 {
return out;
}
-fn glyphsOf(ti: *TerminalImages, gpa: std.mem.Allocator, img: gfx.Image, rgba: []const u8, piece: PixelRect, cols: u16) ?[]const GlyphArt.Cell {
- const key: GlyphKey = .{ .id = img.id, .generation = img.generation, .src = piece, .cols = cols };
- if (ti.glyphs.getPtr(key)) |hit| {
- hit.used = true;
- return hit.cells;
- }
- const grid = GlyphArt.renderRect(gpa, rgba, img.width, img.height, piece, cols, 1, GlyphArt.commodore, true) catch return null;
- if (grid.cells.len != cols) {
- gpa.free(grid.cells);
- return null;
- }
- ti.glyphs.put(gpa, key, .{ .cells = grid.cells }) catch {
- gpa.free(grid.cells);
- return null;
- };
- return grid.cells;
-}
-
/// Drop what this frame did not draw: the caches hold only what is shown.
fn prune(ti: *TerminalImages, gpa: std.mem.Allocator) void {
while (true) {
@@ -390,12 +341,4 @@ fn prune(ti: *TerminalImages, gpa: std.mem.Allocator) void {
gpa.free(ti.converted.get(stale).?.rgba);
_ = ti.converted.remove(stale);
}
- while (true) {
- var gl = ti.glyphs.iterator();
- const stale = while (gl.next()) |entry| {
- if (!entry.value_ptr.used) break entry.key_ptr.*;
- } else break;
- gpa.free(ti.glyphs.get(stale).?.cells);
- _ = ti.glyphs.remove(stale);
- }
}