1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
//! A terminal pane's kitty graphics: the images its program sent (yazi's
//! 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 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.
const std = @import("std");
const ghostty_vt = @import("ghostty-vt");
const pardes = @import("pardes.zig");
const image = @import("image.zig");
const TerminalImages = @This();
const gfx = ghostty_vt.kitty.graphics;
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 asks for its images as glyph art even where pixels could be
/// drawn (the Petscii builtin).
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,
/// 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 };
/// One image's rectangle in the pane, in pixels from the viewport's top
/// left corner (above or left of it is negative).
const Rect = struct { x: i64, y: i64, w: i64, h: i64 };
/// The runs of Kitty placeholder cells between two pins, one row's run at a
/// time, as ghostty's own `kitty.graphics.unicode.placementIterator` gave
/// them before it moved into its RenderState, which pardes does not keep.
const PlaceholderRuns = struct {
rows: ghostty_vt.PageList.RowIterator,
row: ?ghostty_vt.Pin,
/// Where the scan of `row` resumes: a run ended by an incompatible
/// placeholder starts the next one there.
x: usize = 0,
fn init(top: ghostty_vt.Pin, bottom: ghostty_vt.Pin) PlaceholderRuns {
var rows = top.rowIterator(.right_down, bottom);
const row = rows.next();
return .{ .rows = rows, .row = row };
}
fn next(self: *PlaceholderRuns) ?gfx.unicode.Placement {
while (self.row) |row| {
if (!row.rowAndCell().row.kitty_virtual_placeholder) {
self.advance();
continue;
}
var run: ?gfx.unicode.IncompletePlacement = null;
var start: usize = 0;
const cells = row.cells(.all);
while (self.x < cells.len) : (self.x += 1) {
const cell = &cells[self.x];
if (cell.codepoint() != gfx.unicode.placeholder) {
if (run) |done| return complete(row, start, done);
continue;
}
const style = row.style(cell);
const curr: gfx.unicode.IncompletePlacement = .init(&style, row.grapheme(cell) orelse &.{});
if (run) |*prev| {
if (!prev.append(&curr)) return complete(row, start, prev.*);
} else {
run = curr.start();
start = self.x;
}
}
self.advance();
if (run) |done| return complete(row, start, done);
}
return null;
}
fn advance(self: *PlaceholderRuns) void {
self.row = self.rows.next();
self.x = 0;
}
fn complete(row: ghostty_vt.Pin, x: usize, run: gfx.unicode.IncompletePlacement) gfx.unicode.Placement {
var pin = row;
pin.x = @intCast(x);
return .{
.pin = pin,
.image_id = run.imageId(),
.placement_id = run.placement_id orelse 0,
.col = run.col.?,
.row = run.row.?,
.width = run.width,
.height = 1,
};
}
};
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);
}
/// Accept PNG (`f=100`) as well as raw pixels: ghostty-vt built as a
/// library decodes none itself. stb_image does it, as for image panes.
pub fn installPngDecoder() void {
ghostty_vt.sys.decode_png = &decodePng;
}
fn decodePng(gpa: std.mem.Allocator, bytes: []const u8) ghostty_vt.sys.DecodeError!ghostty_vt.sys.Image {
const decoded = image.decode(gpa, bytes) orelse return error.InvalidData;
return .{ .width = @intCast(decoded.w), .height = @intCast(decoded.h), .data = decoded.rgba };
}
/// 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`.
pub fn draw(
ti: *TerminalImages,
p: *pardes.Pardes,
s: *pardes.Surface,
vt: *ghostty_vt.Terminal,
pane_id: u8,
serial: u32,
tx: u16,
body_y: u16,
cols: u16,
rows_of: []const ?u16,
) void {
var conv = ti.converted.valueIterator();
while (conv.next()) |c| c.used = false;
defer ti.prune(p.gpa);
const screen = vt.screens.active;
const storage = &screen.kitty_images;
// Animations step on the core's clock, as this frame shows them; the
// shown frame is `renderData()` (a new `generation` each step, which
// the caches key on).
const now_ms = p.clock.stepped_ns / std.time.ns_per_ms;
ti.animation_due_ms = if (storage.animationTick(vt.io(), now_ms)) |delay| now_ms +| delay else null;
const pages = &screen.pages;
const top = pages.getTopLeft(.viewport);
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 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.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);
// Virtual placements: shown through the placeholder cells, one run of
// them on a row at a time. The placeholders themselves are not text.
var runs: PlaceholderRuns = .init(top, bottom);
while (runs.next()) |run| {
const at = pages.pointFromPin(.viewport, run.pin) orelse continue;
const v = at.viewport.y;
if (v >= rows_of.len) continue;
const row = rows_of[v] orelse continue;
for (@as(usize, at.viewport.x)..@as(usize, at.viewport.x) + run.width) |x| {
if (x >= cols) break;
const cell = s.at(tx + @as(u16, @intCast(x)), body_y + row);
cell.text[0] = ' ';
cell.len = 1;
}
const img = storage.imageById(run.image_id) orelse continue;
const target = storage.placeholderTarget(run.image_id, run.placement_id) orelse continue;
const rp = run.renderPlacement(&target.placement, &img, cw, ch) catch continue;
if (rp.dest_width == 0 or rp.dest_height == 0 or rp.source_width == 0 or rp.source_height == 0) continue;
ctx.place(img, target.placement.z, .{ .x = rp.source_x, .y = rp.source_y, .w = rp.source_width, .h = rp.source_height }, .{
.x = @as(i64, at.viewport.x) * cw + rp.offset_x,
.y = @as(i64, v) * ch + rp.offset_y,
.w = rp.dest_width,
.h = rp.dest_height,
});
}
// Placements on a cell: they move with the text they were put on, and
// go where it goes, above the viewport or off the screen.
const origin = pages.pointFromPin(.screen, top) orelse return;
var it = storage.placements.iterator();
while (it.next()) |entry| {
const placed = entry.value_ptr.*;
// A placement relative to another (P=/Q=) sits at its chain's root
// plus the chain's cell offsets (H=/V=), as ghostty's renderer puts it.
var dx: i64 = 0;
var dy: i64 = 0;
const pin = switch (placed.location) {
.pin => |pin| pin,
.virtual => continue,
.relative => |rel| root: {
const chain = storage.resolveChain(rel) orelse continue;
dx = chain.horizontal_offset;
dy = chain.vertical_offset;
break :root switch (chain.root.location) {
.pin => |pin| pin,
// ponytail: a chain rooted on a virtual placement (shown
// through placeholders) is not drawn; ghostty anchors it at
// the placeholder run.
.virtual, .relative => continue,
};
},
};
const img = storage.images.get(entry.key_ptr.image_id) orelse continue;
const at = pages.pointFromPin(.screen, pin.*) orelse continue;
const size = placed.pixelSize(img, vt);
const sx = @min(placed.source_x, img.width);
const sy = @min(placed.source_y, img.height);
const sw = if (placed.source_width > 0) @min(placed.source_width, img.width - sx) else img.width - sx;
const sh = if (placed.source_height > 0) @min(placed.source_height, img.height - sy) else img.height - sy;
if (size.width == 0 or size.height == 0 or sw == 0 or sh == 0) continue;
ctx.place(img, placed.z, .{ .x = sx, .y = sy, .w = sw, .h = sh }, .{
.x = (@as(i64, at.screen.x) + dx) * cw + placed.x_offset,
.y = (@as(i64, at.screen.y) - @as(i64, origin.screen.y) + dy) * ch + placed.y_offset,
.w = size.width,
.h = size.height,
});
}
}
const Ctx = struct {
ti: *TerminalImages,
p: *pardes.Pardes,
pane: u8,
serial: u32,
tx: u16,
body_y: u16,
cols: u16,
rows_of: []const ?u16,
cw: u32,
ch: u32,
/// 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.
fn place(ctx: Ctx, img: gfx.Image, z: i32, src: PixelRect, dst: Rect) void {
const cw: i64 = ctx.cw;
const ch: i64 = ctx.ch;
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.p.gpa, 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;
while (v <= last_row) : (v += 1) {
const row = ctx.rows_of[@intCast(v)] orelse continue;
const y0 = @max(dst.y, v * ch);
const y1 = @min(dst.y + dst.h, (v + 1) * ch);
if (y1 <= y0) continue;
// The source pixels under this piece, by the same scale.
const piece: PixelRect = .{
.x = src.x + @as(u32, @intCast(@divFloor((x0 - dst.x) * src.w, dst.w))),
.y = src.y + @as(u32, @intCast(@divFloor((y0 - dst.y) * src.h, dst.h))),
.w = @max(1, @as(u32, @intCast(@divFloor((x1 - x0) * src.w, dst.w)))),
.h = @max(1, @as(u32, @intCast(@divFloor((y1 - y0) * src.h, dst.h)))),
};
const c0: u16 = @intCast(@divFloor(x0, cw));
const c1: u16 = @intCast(@divFloor(x1 + cw - 1, cw));
_ = 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 },
});
}
}
};
/// The image's pixels as RGBA: its own bytes when it is held so.
fn pixelsOf(ti: *TerminalImages, gpa: std.mem.Allocator, img: gfx.Image) ?[]const u8 {
const n = @as(usize, img.width) * img.height;
if (n == 0) return null;
const data = img.renderData().bytes() orelse return null;
if (img.format == .rgba) return if (data.len == n * 4) data else null;
const gop = ti.converted.getOrPut(gpa, img.id) catch return null;
if (gop.found_existing) {
if (gop.value_ptr.generation == img.generation) {
gop.value_ptr.used = true;
return gop.value_ptr.rgba;
}
gpa.free(gop.value_ptr.rgba);
_ = ti.converted.remove(img.id);
return ti.pixelsOf(gpa, img);
}
const out = convert(gpa, img, n) orelse {
_ = ti.converted.remove(img.id);
return null;
};
gop.value_ptr.* = .{ .generation = img.generation, .rgba = out };
return out;
}
fn convert(gpa: std.mem.Allocator, img: gfx.Image, n: usize) ?[]u8 {
const depth: usize = switch (img.format) {
.rgb => 3,
.gray_alpha => 2,
.gray => 1,
else => return null,
};
const data = img.renderData().bytes() orelse return null;
if (data.len != n * depth) return null;
const out = gpa.alloc(u8, n * 4) catch return null;
for (0..n) |i| {
const px = data[i * depth ..][0..depth];
out[i * 4 ..][0..4].* = switch (depth) {
3 => .{ px[0], px[1], px[2], 255 },
2 => .{ px[0], px[0], px[0], px[1] },
else => .{ px[0], px[0], px[0], 255 },
};
}
return out;
}
/// 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) {
var conv = ti.converted.iterator();
const stale = while (conv.next()) |entry| {
if (!entry.value_ptr.used) break entry.key_ptr.*;
} else break;
gpa.free(ti.converted.get(stale).?.rgba);
_ = ti.converted.remove(stale);
}
}
|