summaryrefslogtreecommitdiff
path: root/src/screen_state.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/screen_state.zig')
-rw-r--r--src/screen_state.zig36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/screen_state.zig b/src/screen_state.zig
index eeb1c059..8032e79a 100644
--- a/src/screen_state.zig
+++ b/src/screen_state.zig
@@ -1154,6 +1154,7 @@ const Place = Kind.c.pa_placement;
/// A replica's frame of a pane of a kind: what Kind.draw draws for it, its
/// placements' pixels its peer's `Blobs`.
pub const Shadow = struct {
+ gpu: []const u8 = &.{},
cols: u16 = 0,
rows: u16 = 0,
cells: []Cell = &.{},
@@ -1164,6 +1165,7 @@ pub const Shadow = struct {
wanted: u64 = 0,
pub fn deinit(sh: *Shadow, gpa: std.mem.Allocator) void {
+ gpa.free(sh.gpu);
gpa.free(sh.cells);
gpa.free(sh.hashes);
sh.places.deinit(gpa);
@@ -1216,6 +1218,12 @@ pub const Shadow = struct {
}).ptr;
try sh.places.append(gpa, pl);
}
+ // Older frames have no optional GPU tail.
+ const n = if (r.seek < r.end) try r.takeInt(u32, .little) else 0;
+ if (n > 4 << 20) return error.BadGpuFrame;
+ const gpu = try gpa.dupe(u8, try r.take(n));
+ gpa.free(sh.gpu);
+ sh.gpu = gpu;
if (lacking == sh.wanted) return 0;
sh.wanted = lacking;
return lacking;
@@ -1354,6 +1362,7 @@ pub fn encodeShadow(gpa: std.mem.Allocator, sent: *ShadowSent, f: *const Kind.Fr
const places = f.places[0..@min(f.n, 64)];
var ph = std.hash.Wyhash.init(pointer);
for (places) |*pl| inline for (.{ pl.w, pl.h, pl.src, pl.at, pl.dst, pl.key }) |v| ph.update(std.mem.asBytes(&v));
+ if (f.gpu.len != 0) ph.update(f.gpu);
const places_hash = ph.final();
if (sent.cols == f.cols and std.mem.eql(Hash, sent.hashes.items, hashes.items) and sent.places == places_hash) {
hashes.deinit(gpa);
@@ -1384,6 +1393,9 @@ pub fn encodeShadow(gpa: std.mem.Allocator, sent: *ShadowSent, f: *const Kind.Fr
inline for (.{ pl.dst.x, pl.dst.y, pl.dst.w, pl.dst.h }) |v| try w.writeInt(u16, v, .little);
try w.writeInt(u64, pl.key, .little);
}
+ if (f.gpu.len > 4 << 20) return error.BadGpuFrame;
+ try w.writeInt(u32, @intCast(f.gpu.len), .little);
+ try w.writeAll(f.gpu);
sent.hashes.deinit(gpa);
sent.hashes = hashes;
sent.cols = f.cols;
@@ -1626,3 +1638,27 @@ test "a pane of a kind crosses as its frame, a row the peer holds as its hash, a
try std.testing.expectEqual(@as(usize, 0), sh.places.items.len);
try std.testing.expectEqual(@as(u64, 0), try sh.apply(gpa, out.written(), 1, &blobs));
}
+
+test "optional GPU frames cross shadows, change independently, and disappear" {
+ const gpa = std.testing.allocator;
+ var sent: ShadowSent = .{};
+ defer sent.deinit(gpa);
+ const shadow = try gpa.create(Shadow);
+ shadow.* = .{};
+ defer shadow.deinit(gpa);
+ var blobs: Blobs = .{};
+ defer blobs.deinit(gpa);
+ var cell = std.mem.zeroes(Cell);
+ var frame: Kind.Frame = .{ .cols = 1, .rows = 1, .cells = @as([*]Cell, @ptrCast(&cell))[0..1], .gpu = "first scene" };
+ var out: std.Io.Writer.Allocating = .init(gpa);
+ defer out.deinit();
+ for ([_][]const u8{ "first scene", "second scene", "" }) |bytes| {
+ frame.gpu = bytes;
+ out.clearRetainingCapacity();
+ try std.testing.expect(try encodeShadow(gpa, &sent, &frame, 0, &out.writer));
+ _ = try shadow.apply(gpa, out.written(), 1, &blobs);
+ try std.testing.expectEqualStrings(bytes, shadow.gpu);
+ out.clearRetainingCapacity();
+ try std.testing.expect(!try encodeShadow(gpa, &sent, &frame, 0, &out.writer));
+ }
+}