diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 01:54:09 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | b1311b3d489a8054456cba7054160ed7fb7e70e1 (patch) | |
| tree | b724a13f225f30a9dc6ce8e4b64ec6ffc0ceba32 /src | |
| parent | f3591d48a16086dd4b3b081da00804d2541fefcc (diff) | |
| download | sketchbook-b1311b3d489a8054456cba7054160ed7fb7e70e1.tar.gz sketchbook-b1311b3d489a8054456cba7054160ed7fb7e70e1.zip | |
screen.png: one read, answered when its frame has landed
A screenshot over 9P was a write of `screenshot` to ctl, then reads of
status until screenshot_generation moved, then a read of screen.png (an
open while a capture was pending failed). Now an open of screen.png asks
for the next frame itself and is parked (cloud9's Status.again) until the
capture lands: the service loop answers the engine's retry() each pass,
so a parked open is asked again every frame, and respond (unchanged)
answers it once nothing is pending, pinning that frame's PNG, or with
the capture's error. So `9p read screen.png` or `fs read screen.png` is
the whole recipe. A write of `screenshot` still works and status still
counts, and an open after it waits for a frame of its own.
The frame-thread loop carries the park cleanly: retry() and next() run
on the frame thread between frames like every request, a parked open
keeps its fid, a flush or a hangup drops it, and the four parking slots
a connection has bound how many wait at once (a fifth open is refused
"waiting").
Tests: test-control 46/46 steps, 10/10 tests (the snapshot test's
CapturePending is now what answer() parks). Live, headless, the replay
fixture: three reads of screen.png with no write, 24-26 ms each with the
9p process's start, 640x360 PNGs; the old write-then-read still works.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src')
| -rw-r--r-- | src/host/control.zig | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/src/host/control.zig b/src/host/control.zig index bf1e12d..6fb997b 100644 --- a/src/host/control.zig +++ b/src/host/control.zig @@ -24,7 +24,7 @@ const files = std.enums.EnumArray(File, struct { mode: u16 = 0o400, doc: []const .ctl = .{ .mode = 0o600, .doc = "write a verb a line, of those below: ctl's own, the input a script plays (live, no frame of its own), the console's; any other line goes to the sketch (commands lists its words)" }, .status = .{ .doc = "name, canvas size, playback mode, gpu, and the screenshot's state" }, .log = .{ .doc = "the console's last five lines" }, - .@"screen.png" = .{ .doc = "the frame screenshot captured, as PNG; each open pins its own copy" }, + .@"screen.png" = .{ .doc = "the next frame, as PNG: an open waits for it to be drawn, and pins its own copy" }, .profile = .{ .doc = "with --profile-gpu: label, count, mean, worst, p95 and p99 ms, tab-separated" }, .@"profile-trace" = .{ .doc = "with --profile-gpu: label,sample,ns, the samples kept, as CSV" }, .@"timing-trace" = .{ .doc = "index,frame_ns,work_ns,outside_work_ns, the frames kept, as CSV" }, @@ -37,7 +37,7 @@ const files = std.enums.EnumArray(File, struct { mode: u16 = 0o400, doc: []const /// The verbs `ctl` takes beside the console's lines. const Verb = enum { screenshot, @"profile-reset" }; const verbs = std.enums.EnumArray(Verb, []const u8).init(.{ - .screenshot = "captures the next frame for screen.png", + .screenshot = "captures the next frame (status says when); screen.png's open does it itself", .@"profile-reset" = "clears profile's counters and frame count, keeping its labels", }); @@ -124,7 +124,8 @@ pub const Control = struct { conn.used -= consumed; progressed = consumed != 0; } - while (conn.server.next()) |req| self.answer(host, conn, req); + while (conn.server.retry()) |req| self.answer(host, conn, req, true); + while (conn.server.next()) |req| self.answer(host, conn, req, false); if (conn.server.protocol.dead) return error.Closed; if (conn.server.output().len > 0) { if (try transport.write(conn.fd, conn.server.output())) |n| { @@ -144,11 +145,18 @@ pub const Control = struct { } } - fn answer(self: *Control, host: *Host, conn: *Connection, req: fs.Req) void { + /// Answers a request, or parks it: an open of screen.png asks for the + /// next frame and waits for it, asked again (`retried`) each frame + /// until the capture has landed. + fn answer(self: *Control, host: *Host, conn: *Connection, req: fs.Req, retried: bool) void { var reply: fs.Reply = .{ .tag = req.tag }; var buffer: [msize]u8 = undefined; + if (!retried and req.op == .open and req.node == @backingInt(File.@"screen.png")) { + self.capture_pending = true; + self.capture_error = null; + } const bytes = self.respond(host, conn, req, &reply, &buffer) catch |err| blk: { - reply = .{ .tag = req.tag, .status = .err, .errno = fs.E.INVAL, .ename = @errorName(err) }; + reply = if (err == error.CapturePending) .{ .tag = req.tag, .status = .again } else .{ .tag = req.tag, .status = .err, .errno = fs.E.INVAL, .ename = @errorName(err) }; break :blk &.{}; }; conn.server.reply(&reply, bytes); @@ -594,7 +602,7 @@ test "control writes take input verbs live, and the README and commands read the try std.testing.expectEqual(abi.Event.Kind.key_down, host.pending.items[3].kind); try std.testing.expectError(error.ExpectedPoint, control.respond(&host, &conn, .{ .tag = 1, .op = .write, .node = 2, .data = "click 10" }, &reply, &buf)); for ([_]File{ .README, .commands }) |f| { - _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = @intFromEnum(f) }, &reply, &buf); + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = @backingInt(f) }, &reply, &buf); const handle = reply.handle; const text = conn.snapshots[handle - 1].?; if (f == .README) { @@ -608,6 +616,6 @@ test "control writes take input verbs live, and the README and commands read the try std.testing.expect(std.mem.startsWith(u8, text, "screenshot captures")); try std.testing.expect(std.mem.endsWith(u8, text, "\nscene room\nscene street\n")); } - _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .release, .node = @intFromEnum(f), .handle = handle, .opened = true }, &reply, &buf); + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .release, .node = @backingInt(f), .handle = handle, .opened = true }, &reply, &buf); } } |
