diff options
Diffstat (limited to 'src/host/Host.zig')
| -rw-r--r-- | src/host/Host.zig | 108 |
1 files changed, 87 insertions, 21 deletions
diff --git a/src/host/Host.zig b/src/host/Host.zig index e579c87..1fe1db2 100644 --- a/src/host/Host.zig +++ b/src/host/Host.zig @@ -9,7 +9,6 @@ const abi = @import("abi"); const memory = @import("memory.zig"); const playback = @import("timeline.zig"); const Timeline = playback.Timeline; -const mapRecordings = playback.mapRecordings; const gfx = @import("gfx.zig"); const sound = @import("audio.zig"); const Gpu = @import("gpu"); @@ -78,7 +77,6 @@ store: memory.StoreFile, /// Images and buffers the sketch made (`newImage`, `decodeImage`, /// `newBuffer`): Session holds their handles, so they go when it resets. made: struct { images: std.ArrayList(abi.Image) = .empty, buffers: std.ArrayList(abi.Buffer) = .empty } = .{}, -recordings: []align(memory.page) u8, timeline: Timeline, /// This run's session and the ones before it (Sessions.zig). sessions: Sessions, @@ -90,8 +88,8 @@ library: ?Library = null, watch: ?Watch = null, reloads: usize = 0, /// Identifies the running build of the sketch (its library or executable -/// file's inode, size and modification time): recordings carry it, and -/// `play` warns on a mismatch. +/// file's inode, size and modification time): sessions' stretches carry it, +/// and `play` warns on a mismatch. build: u64 = 0, /// Copies of the sketch's info strings, replaced on every load. info_arena: std.heap.ArenaAllocator, @@ -169,9 +167,6 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std var state = try memory.StateFile.open(gpa, options.state); errdefer state.close(); - var inputs_buf: [std.fs.max_path_bytes]u8 = undefined; - const recordings = try mapRecordings(try std.mem.print(&inputs_buf, "{s}.inputs", .{options.state})); - errdefer std.posix.munmap(recordings); var store_buf: [std.fs.max_path_bytes]u8 = undefined; var store = memory.StoreFile.open(gpa, try std.mem.print(&store_buf, "{s}.store", .{options.state}), options.fresh) catch |err| { if (err == error.StoreInUse) std.log.err("{s}.store is open in another process", .{options.state}); @@ -193,7 +188,6 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std .state = state, .session = .init(gpa), .store = store, - .recordings = recordings, .timeline = undefined, .sessions = .{ .gpa = gpa, .io = io }, .sketch = sketch, @@ -202,7 +196,7 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std .frame_arena = .init(gpa), .input = .{ .exporting = options.export_to != null }, }; - self.timeline = .{ .bank = .{ .state = &self.state, .inputs = recordings } }; + self.timeline = .{ .gpa = gpa, .state = &self.state, .defaults = sketch.defaults }; self.painter.images = &self.images; if (gpu) |device| self.passes = .init(gpa, device); if (options.profile_gpu) if (self.passes) |*g| { @@ -217,12 +211,12 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std _ = c.SDL_StartTextInput(self.window); self.build = fileId(io, options.library orelse "/proc/self/exe"); try self.adopt(sketch); - if (options.fresh) { - self.state.header().used = 0; - @memset(self.recordings, 0); - self.resetState(); - } self.openSessions(info.record); + // Before --fresh: it never touches the history. + if (importSnapshots(&self.state, &self.sessions)) |n| { + if (n > 0) self.print("{d} snapshots of {s} are marks now", .{ n, options.state }); + } else |err| self.print("snapshots of {s} kept in it: {t}", .{ options.state, err }); + if (options.fresh) self.resetState(); if (!options.headless and options.export_to == null) { self.presentation = Presentation.init(gpa, self.window) catch null; if (options.timing_marker) self.print("presentation tracing: {s}", .{if (self.presentation) |p| p.status else "disabled_or_allocation_failed"}); @@ -250,7 +244,7 @@ pub fn deinit(self: *Host) void { std.posix.munmap(entry.value_ptr.*); } self.files.deinit(self.gpa); - std.posix.munmap(self.recordings); + self.timeline.deinit(); self.session.deinit(self.gpa); self.store.close(); self.made.images.deinit(self.gpa); @@ -294,7 +288,7 @@ pub fn run(self: *Host) !void { frameTime(wall, if (self.presentation) |p| p.sampleNs(wall) else wall, self.last_now, self.timeline.mode, self.pending.items); try self.frame(now); } - self.timeline.stop(c.SDL_GetTicksNS()); + self.timeline.stop(); } // Live animation uses the display clock, but a tick must not predate its input. @@ -352,12 +346,11 @@ pub fn frame(self: *Host, now: u64) !void { const why: ?Sessions.Why = if (self.sessions.frames == 0) .start else if (self.reloaded) .after_reload else if (self.jumped or self.sessions.away) .jump else null; self.sessions.away = false; self.sessions.frame(self.pending.items, self.live(), why) catch |err| self.print("session: {t}", .{err}); - h.capture(self.pending.items) catch |err| self.print("Recording stopped: {t}", .{err}); self.update(self.pending.items); }, } self.pending.clearRetainingCapacity(); - const work = try self.draw(if (h.mode == .live or h.mode == .recording) now else h.position, started); + const work = try self.draw(if (h.mode == .live or h.mode == .recording) now else h.base + h.position, started); const ended = c.SDL_GetTicksNS(); self.timing.add(.{ .frame = ended - (if (self.last_frame_end == 0) started else self.last_frame_end), .work = work }); self.last_frame_end = ended; @@ -398,7 +391,7 @@ fn restoredInput(self: *Host, now: u64) !void { self.input.mouse_y = 0; self.input.buttons = 0; self.input.keys = .empty; - if (h.mode != .recording or h.bank.recording(h.selected).count != 0) return; + if (h.mode != .recording) return; var seed: std.ArrayList(abi.Event) = .empty; defer seed.deinit(self.gpa); if (was.mouse_x != 0 or was.mouse_y != 0) try seed.append(self.gpa, .{ .kind = .mouse_move, .time = now, .x = was.mouse_x, .y = was.mouse_y }); @@ -654,7 +647,9 @@ pub fn runLine(self: *Host, line: []const u8, now: u64) !void { defer zone.end(); const replay = self.console.execute(self, line, now) catch |err| { // The sketch has printed its own error already. - if (err != error.CommandFailed) self.console.print("{t}. Type help for usage.", .{err}); + if (err == error.NotRecording) { + self.console.log("Not recording (a release build: .record = true collects): no marks or clips."); + } else if (err != error.CommandFailed) self.console.print("{t}. Type help for usage.", .{err}); if (self.timeline.restored) self.jumped = true; try self.restoredInput(now); return err; @@ -672,7 +667,7 @@ pub fn runLine(self: *Host, line: []const u8, now: u64) !void { fn movesState(line: []const u8) bool { var words = std.mem.tokenizeAny(u8, line, " \t"); const verb = words.next() orelse return false; - const still = [_][]const u8{ "help", "overlay", "timing", "get", "snapshot", "recording", "diff", "stop", "pause", "save", "quit", "exit", "sessions" }; + const still = [_][]const u8{ "help", "overlay", "timing", "get", "snapshot", "recording", "diff", "stop", "pause", "save", "quit", "exit", "sessions", "marks", "clips" }; for (still) |w| if (std.mem.eql(u8, verb, w)) return false; return true; } @@ -733,6 +728,7 @@ fn adopt(self: *Host, sketch: abi.Sketch) !void { for (completions, info.commands) |*to, from| to.* = try arena.dupe(u8, from); self.completions = completions; self.sketch = sketch; + self.timeline.defaults = sketch.defaults; self.reloaded = true; switch (adoption) { .unchanged => {}, @@ -916,6 +912,19 @@ fn openSessions(self: *Host, record: bool) void { } } +/// Snapshots of a state file from before sessions become marks "1".."7", +/// then the file holds live state only. While nothing records, they stay. +fn importSnapshots(state: *memory.StateFile, sessions: *Sessions) !usize { + if (state.slots == 1 or sessions.mode() == .off) return 0; + var n: usize = 0; + for (1..state.slots) |i| if (state.used(i)) { + _ = try sessions.mark(&.{'0' + @as(u8, @intCast(i))}, .import, state.slot(i)); + n += 1; + }; + try state.dropSnapshots(); + return n; +} + fn wallClock(io: std.Io) u64 { return @intCast(@max(0, std.Io.Timestamp.now(io, .real).nanoseconds)); } @@ -1176,3 +1185,60 @@ test "setup writeBuffer propagates unavailable, unknown and out-of-bounds upload try host.passes.?.buffers.append(a, .{ .buffer = undefined, .size = 4 }); try std.testing.expectError(error.UploadOutOfBounds, client.writeBuffer(@fromBackingInt(@intCast(1)), 2, "data")); } + +test "a state file's snapshots from before sessions become marks, and the file one slot" { + const State = extern struct { v: u32 = 0 }; + const D = struct { + fn f(_: abi.Memory, p: [*]u8) callconv(.c) void { + @as(*State, @ptrCast(@alignCast(p))).* = .{}; + } + }; + const gpa = std.testing.allocator; + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var buf: [std.fs.max_path_bytes]u8 = undefined; + const path = try std.mem.print(&buf, ".zig-cache/tmp/{s}/old.state", .{&tmp.sub_path}); + // An eight-slot SKETCH02 file, slots 0, 1 and 3 used: built from a new + // one-slot file's header. + { + var file = try memory.StateFile.open(gpa, path); + defer file.close(); + _ = try file.adopt(gpa, abi.layout.of(State, .state), D.f); + } + const one = try std.Io.Dir.cwd().readFileAlloc(io, path, gpa, .unlimited); + defer gpa.free(one); + const stride = memory.page; + const old = try gpa.alloc(u8, memory.header_len + memory.old_slot_count * stride); + defer gpa.free(old); + @memset(old, 0); + @memcpy(old[0..memory.header_len], one[0..memory.header_len]); + const h: *memory.Header = @ptrCast(@alignCast(old.ptr)); + h.magic = memory.file_magic_v2; + h.used = 0b1011; + for ([_][2]u32{ .{ 0, 5 }, .{ 1, 11 }, .{ 3, 33 } }) |slot| std.mem.writeInt(u32, old[memory.header_len + slot[0] * stride ..][0..4], slot[1], .little); + try std.Io.Dir.cwd().writeFile(io, .{ .sub_path = path, .data = old }); + + var state = try memory.StateFile.open(gpa, path); + defer state.close(); + try std.testing.expectEqual(memory.old_slot_count, state.slots); + var sessions = try Sessions.open(gpa, io, .memory, "", .{ .build = 1, .source = "", .layout = state.layoutBytes() }, 0); + defer sessions.deinit(); + try std.testing.expectEqual(@as(usize, 2), try importSnapshots(&state, &sessions)); + for ([_][2]u32{ .{ '1', 11 }, .{ '3', 33 } }) |want| { + const m = (try sessions.findMark(&.{@intCast(want[0])})).?; + const key = try sessions.load(m.session, m.key); + defer key.free(gpa); + try std.testing.expectEqual(Sessions.Why.import, key.why); + try std.testing.expectEqual(want[1], std.mem.readInt(u32, key.state[0..4], .little)); + } + try std.testing.expectEqual(@as(?Sessions.Entry, null), try sessions.findMark("2")); + // One slot now, live state kept, and it reopens so. + try std.testing.expectEqual(@as(usize, 1), state.slots); + try std.testing.expectEqual(@as(u32, 5), std.mem.readInt(u32, state.slot(0)[0..4], .little)); + var again = try memory.StateFile.open(gpa, path); + defer again.close(); + try std.testing.expectEqualStrings(&memory.file_magic, &again.header().magic); + try std.testing.expectEqual(@as(usize, 1), again.slots); + try std.testing.expectEqual(@as(u32, 5), std.mem.readInt(u32, again.slot(0)[0..4], .little)); +} |
