diff options
Diffstat (limited to 'src/screen_state.zig')
| -rw-r--r-- | src/screen_state.zig | 94 |
1 files changed, 78 insertions, 16 deletions
diff --git a/src/screen_state.zig b/src/screen_state.zig index 1c86c216..bcf4b137 100644 --- a/src/screen_state.zig +++ b/src/screen_state.zig @@ -1408,19 +1408,48 @@ pub const ShadowSent = struct { /// Frame `f` of a pane (its prefix and `placed` from its slot) for a peer /// that was last sent `sent`: false, and nothing written, when it holds -/// this frame already. -pub fn encodeShadow(gpa: std.mem.Allocator, sent: *ShadowSent, f: *const Kind.Frame, prefix: []const u8, placed: bool, pointer: u8, w: *std.Io.Writer) !bool { +/// this frame already. The message is `budget` bytes at most (the wire's +/// bound, which a peer's reader takes as the link's end past it): each +/// placement the peer holds crosses as its key, and a new one's raster +/// only while it fits; one that does not is left out of the frame (the +/// peer shows the pane without it) and is not tried again until it moves. +pub fn encodeShadow(gpa: std.mem.Allocator, sent: *ShadowSent, f: *const Kind.Frame, prefix: []const u8, placed: bool, pointer: u8, budget: usize, w: *std.Io.Writer) !bool { var hashes: std.ArrayList(Hash) = .empty; errdefer hashes.deinit(gpa); for (0..f.rows) |y| try hashes.append(gpa, std.hash.Wyhash.hash(0, std.mem.sliceAsBytes(f.cells[y * f.cols ..][0..f.cols]))); const places = f.places[0..f.n]; + const prefix_len = @min(prefix.len, 96); // Whether it placed a picture, and the pointer it asks for, one byte. const flags: u8 = @intFromBool(placed) | @as(u8, @min(pointer, 127)) << 1; const prefix_hash = std.hash.Wyhash.hash(flags, prefix); - var same = sent.cols == f.cols and std.mem.eql(Hash, sent.hashes.items, hashes.items) and sent.prefix == prefix_hash and sent.keys.items.len == places.len; - if (same) for (places, sent.keys.items) |pl, k| { - if (pl.key != k) same = false; - }; + const rowHeld = struct { + fn at(s: *const ShadowSent, cols: u16, hs: []const Hash, y: usize) bool { + return s.cols == cols and y < s.hashes.items.len and s.hashes.items[y] == hs[y]; + } + }.at; + // Its size but for the placements: the head, each row's hash and the + // rows the peer does not hold, the placements' count. + var size: usize = 6 + prefix_len + 1; + for (0..f.rows) |y| size += 9 + if (rowHeld(sent, f.cols, hashes.items, y)) 0 else @as(usize, f.cols) * 20; + const place_head = 57; + var crosses: [64]bool = @splat(false); + var keys: usize = 0; + for (places, 0..) |pl, i| { + const held = std.mem.indexOfScalar(u64, sent.keys.items, pl.key) != null; + const more = place_head + if (held) 0 else @as(usize, pl.w) * pl.h * 4; + if (i >= crosses.len or size + more > budget) continue; + crosses[i] = true; + size += more; + keys += 1; + } + var same = sent.cols == f.cols and std.mem.eql(Hash, sent.hashes.items, hashes.items) and sent.prefix == prefix_hash and sent.keys.items.len == keys; + if (same) { + var k: usize = 0; + for (places, 0..) |pl, i| if (i < crosses.len and crosses[i]) { + if (pl.key != sent.keys.items[k]) same = false; + k += 1; + }; + } if (same) { hashes.deinit(gpa); return false; @@ -1428,11 +1457,11 @@ pub fn encodeShadow(gpa: std.mem.Allocator, sent: *ShadowSent, f: *const Kind.Fr try w.writeInt(u16, f.cols, .little); try w.writeInt(u16, f.rows, .little); try w.writeByte(flags); - try w.writeByte(@intCast(@min(prefix.len, 96))); - try w.writeAll(prefix[0..@min(prefix.len, 96)]); + try w.writeByte(@intCast(prefix_len)); + try w.writeAll(prefix[0..prefix_len]); for (hashes.items, 0..) |h, y| { try w.writeInt(u64, h, .little); - const held = sent.cols == f.cols and y < sent.hashes.items.len and sent.hashes.items[y] == h; + const held = rowHeld(sent, f.cols, hashes.items, y); try w.writeByte(@intFromBool(!held)); if (!held) for (f.cells[y * f.cols ..][0..f.cols]) |*c| { try w.writeAll(&c.text); @@ -1443,8 +1472,8 @@ pub fn encodeShadow(gpa: std.mem.Allocator, sent: *ShadowSent, f: *const Kind.Fr try w.writeByte(c.flags); }; } - try w.writeByte(@intCast(places.len)); - for (places) |pl| { + try w.writeByte(@intCast(keys)); + for (places, 0..) |pl, i| if (i < crosses.len and crosses[i]) { try w.writeInt(u32, pl.w, .little); try w.writeInt(u32, pl.h, .little); inline for (.{ pl.src.x, pl.src.y, pl.src.w, pl.src.h, pl.at.x, pl.at.y, pl.at.w, pl.at.h }) |v| try w.writeInt(u32, v, .little); @@ -1453,13 +1482,13 @@ pub fn encodeShadow(gpa: std.mem.Allocator, sent: *ShadowSent, f: *const Kind.Fr const held = std.mem.indexOfScalar(u64, sent.keys.items, pl.key) != null; try w.writeByte(@intFromBool(!held)); if (!held) try w.writeAll(pl.rgba[0 .. @as(usize, pl.w) * pl.h * 4]); - } + }; sent.hashes.deinit(gpa); sent.hashes = hashes; sent.cols = f.cols; sent.prefix = prefix_hash; sent.keys.clearRetainingCapacity(); - for (places) |pl| try sent.keys.append(gpa, pl.key); + for (places, 0..) |pl, i| if (i < crosses.len and crosses[i]) try sent.keys.append(gpa, pl.key); return true; } @@ -1478,19 +1507,52 @@ test "a pane of a kind crosses as its frame, a row or a raster the peer holds as defer sh.deinit(gpa); var out: std.Io.Writer.Allocating = .init(gpa); defer out.deinit(); - try std.testing.expect(try encodeShadow(gpa, &sent, &f, "fake", true, 0, &out.writer)); + try std.testing.expect(try encodeShadow(gpa, &sent, &f, "fake", true, 0, 1 << 20, &out.writer)); try sh.apply(gpa, out.written()); try std.testing.expectEqualStrings("a", sh.cells[0].text[0..1]); try std.testing.expectEqualStrings("fake", sh.prefix.slice()); try std.testing.expectEqual(@as(u8, 3), sh.rasters.items[0][2]); // The same frame again: nothing to send. out.clearRetainingCapacity(); - try std.testing.expect(!try encodeShadow(gpa, &sent, &f, "fake", true, 0, &out.writer)); + try std.testing.expect(!try encodeShadow(gpa, &sent, &f, "fake", true, 0, 1 << 20, &out.writer)); // A cell of the second row moves: the first row and the raster go as hashes. cells[4].flags = Kind.c.PA_PAINTED; - try std.testing.expect(try encodeShadow(gpa, &sent, &f, "fake", true, 0, &out.writer)); + try std.testing.expect(try encodeShadow(gpa, &sent, &f, "fake", true, 0, 1 << 20, &out.writer)); try std.testing.expect(out.written().len < 20 + 2 * 9 + 3 * 20 + 60); try sh.apply(gpa, out.written()); try std.testing.expectEqual(@as(u8, Kind.c.PA_PAINTED), sh.cells[4].flags); try std.testing.expectEqual(@as(u8, 3), sh.rasters.items[0][2]); } + +test "a pane of a kind's raster past the message's bound is left out of its frame, the frame within it" { + const gpa = std.testing.allocator; + var cells: [2 * 1]Cell = @splat(std.mem.zeroes(Cell)); + var small: [4]u8 = .{ 1, 2, 3, 4 }; + var big: [4 * 64]u8 = @splat(7); + var f: Kind.Frame = .{ .cells = &cells, .cols = 2, .rows = 1, .n = 2 }; + f.buf[0] = std.mem.zeroInit(Place, .{ .rgba = &big, .w = 8, .h = 8, .key = 1 }); + f.buf[1] = std.mem.zeroInit(Place, .{ .rgba = &small, .w = 1, .h = 1, .key = 2 }); + f.places = &f.buf; + var sent: ShadowSent = .{}; + defer sent.deinit(gpa); + const sh = try gpa.create(Shadow); + sh.* = .{}; + defer sh.deinit(gpa); + var out: std.Io.Writer.Allocating = .init(gpa); + defer out.deinit(); + // Room for the frame and the small raster, not the 256-byte one. + const budget = 200; + try std.testing.expect(try encodeShadow(gpa, &sent, &f, "p", false, 0, budget, &out.writer)); + try std.testing.expect(out.written().len <= budget); + try sh.apply(gpa, out.written()); + try std.testing.expectEqual(@as(usize, 1), sh.places.items.len); + try std.testing.expectEqual(@as(u64, 2), sh.places.items[0].key); + // Nothing moved: nothing to send, the big one not tried again. + out.clearRetainingCapacity(); + try std.testing.expect(!try encodeShadow(gpa, &sent, &f, "p", false, 0, budget, &out.writer)); + // With room, it crosses, the small one as its key. + try std.testing.expect(try encodeShadow(gpa, &sent, &f, "p", false, 0, 1 << 20, &out.writer)); + try sh.apply(gpa, out.written()); + try std.testing.expectEqual(@as(usize, 2), sh.places.items.len); + try std.testing.expectEqual(@as(u8, 7), sh.rasters.items[0][5]); +} |
