summaryrefslogtreecommitdiff
path: root/src/term/TerminalImages.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/term/TerminalImages.zig')
-rw-r--r--src/term/TerminalImages.zig52
1 files changed, 27 insertions, 25 deletions
diff --git a/src/term/TerminalImages.zig b/src/term/TerminalImages.zig
index 6894945c..1420d9e3 100644
--- a/src/term/TerminalImages.zig
+++ b/src/term/TerminalImages.zig
@@ -8,14 +8,17 @@
//! 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 plain = @import("plain");
const ghostty_vt = @import("ghostty-vt");
-const pardes = @import("../pardes.zig");
const zstbi = @import("zstbi");
const wuffs = @import("wuffs");
const TerminalImages = @This();
const gfx = ghostty_vt.kitty.graphics;
-const PixelRect = @import("../surface.zig").RectPx;
+const PixelRect = plain.RectPx;
+/// Pixels one image may decode to (RGBA, 4 bytes each): checked from its
+/// header before it is decoded.
+const image_decode_pixels: usize = 64 * 1024 * 1024;
/// 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.
@@ -140,7 +143,7 @@ fn decodePng(gpa: std.mem.Allocator, bytes: []const u8) ghostty_vt.sys.DecodeErr
if (bytes.len < 24 or !std.mem.startsWith(u8, bytes, "\x89PNG\r\n\x1a\n") or !std.mem.eql(u8, bytes[12..16], "IHDR")) return error.InvalidData;
const w = std.mem.readInt(u32, bytes[16..20], .big);
const h = std.mem.readInt(u32, bytes[20..24], .big);
- if (@as(u64, w) * h > pardes.memory.limits.image_decode_pixels) return error.InvalidData;
+ if (@as(u64, w) * h > image_decode_pixels) return error.InvalidData;
const got = wuffs.png.decode(gpa, bytes) catch return error.InvalidData;
if (got.data.len != @as(usize, got.width) * got.height * 4) {
gpa.free(got.data);
@@ -160,38 +163,36 @@ fn decodePng(gpa: std.mem.Allocator, bytes: []const u8) ghostty_vt.sys.DecodeErr
/// columns the body shows, from `tx`.
pub fn draw(
ti: *TerminalImages,
- p: *pardes.Pardes,
- s: *pardes.Surface,
+ gpa: std.mem.Allocator,
vt: *ghostty_vt.Terminal,
- pane_id: u8,
- serial: u32,
- tx: u16,
- body_y: u16,
- cols: u16,
+ where: plain.term.ImagesAt,
rows_of: []const ?u16,
+ out: *std.ArrayList(plain.Placement),
+ frame: []plain.Cell,
+ frame_cols: u16,
) void {
var conv = ti.converted.valueIterator();
while (conv.next()) |c| c.used = false;
- defer ti.prune(p.gpa);
+ defer ti.prune(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;
+ const now_ms = where.now_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.view().cell_pixels.w);
- const ch: u32 = @max(1, p.view().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 };
+ 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 };
// 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 {
+ const first = out.items.len;
+ defer std.sort.insertion(plain.Placement, out.items[first..], {}, struct {
+ fn lessThan(_: void, a: plain.Placement, b: plain.Placement) bool {
return a.z < b.z;
}
}.lessThan);
@@ -205,8 +206,8 @@ pub fn draw(
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);
+ if (x >= where.cols) break;
+ const cell = &frame[@as(usize, where.body_y + row) * frame_cols + where.tx + x];
cell.text[0] = ' ';
cell.len = 1;
}
@@ -267,7 +268,8 @@ pub fn draw(
const Ctx = struct {
ti: *TerminalImages,
- p: *pardes.Pardes,
+ gpa: std.mem.Allocator,
+ out: *std.ArrayList(plain.Placement),
pane: u8,
serial: u32,
tx: u16,
@@ -285,7 +287,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.p.gpa, img) orelse return;
+ const rgba = ctx.ti.pixelsOf(ctx.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;
@@ -303,7 +305,7 @@ const Ctx = struct {
};
const c0: u16 = @intCast(@divFloor(x0, cw));
const c1: u16 = @intCast(@divFloor(x1 + cw - 1, cw));
- _ = ctx.p.appendPlacement(.{
+ ctx.out.append(ctx.gpa, .{
.pane = ctx.pane,
.raster = .of(rgba, img.width, img.height),
.src = piece,
@@ -314,10 +316,10 @@ const Ctx = struct {
.w = @intCast(x1 - x0),
.h = @intCast(y1 - y0),
},
- .key = pardes.Placement.keyOf(ctx.serial, img.id, @truncate(img.generation)),
+ .key = plain.Placement.keyOf(ctx.serial, img.id, @truncate(img.generation)),
.z = z,
.art = .{ .glyph = ctx.ti.petscii },
- });
+ }) catch {};
}
}
};