diff options
Diffstat (limited to 'src/gui')
| -rw-r--r-- | src/gui/gui.zig | 118 |
1 files changed, 109 insertions, 9 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 125f53cf..0a3f3340 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -1475,8 +1475,10 @@ const Gui = struct { /// owns the wheel (terminal.ownsWheel). program_wheel: f32 = 0, glide: Glide = .{}, - /// PARDES_FRAMES_DEMO: the scroll timedemo's next step and when it - /// started (FrameStats.demo); null when none runs. + /// PARDES_FRAMES_DEMO: the timedemo (FrameStats.named), its next step + /// and when it started; null when none runs. + demo: []const pardes.FrameStats.Step = &.{}, + demo_name: []const u8 = "", demo_next: ?usize = null, demo_start_ns: u64 = 0, /// G9 and G11: the selection's and the hovered word's glow blocks as @@ -2160,7 +2162,9 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, attach: ?[]const u .capture_series = test_mode and env.get("PARDES_TEST_SERIES") != null, .capture_dir = capture_dir orelse "", .soft_present = soft_present, - .demo_next = if (env.get("PARDES_FRAMES_DEMO") != null) 0 else null, + .demo = if (env.get("PARDES_FRAMES_DEMO")) |name| pardes.FrameStats.named(name) orelse &.{} else &.{}, + .demo_name = env.get("PARDES_FRAMES_DEMO") orelse "", + .demo_next = if (env.get("PARDES_FRAMES_DEMO")) |name| (if (pardes.FrameStats.named(name) != null) @as(?usize, 0) else null) else null, .surface_opacity = surface_opacity, .soft_renderer = soft_renderer, }; @@ -2323,6 +2327,8 @@ fn localSession( defer shell.shaders.deinit(gpa, inotify_fd, &watches); // A virtual clock never blinks: goldens and feel series hold still. g.no_blink = shell.test_clock != null; + frame_probe = &core.frames; + frame_capture_dir = if (std.c.getenv("PARDES_FRAMES_CAPTURE")) |dir| std.mem.span(dir) else null; const host = shell.host(); core.host = host; @@ -3128,6 +3134,13 @@ const Input = struct { }; fn dispatch(g: *Gui, in: *Input, sev: *const c.SDL_Event) void { + // An input's time, for its latency to the frame that answers it + // (FrameStats): SDL's stamp, moved onto the monotonic clock. + switch (sev.type) { + c.SDL_EVENT_KEY_DOWN, c.SDL_EVENT_TEXT_INPUT, c.SDL_EVENT_MOUSE_BUTTON_DOWN, c.SDL_EVENT_MOUSE_WHEEL => if (frame_probe) |probe| + probe.input(monoNs() -| (c.SDL_GetTicksNS() -| sev.common.timestamp)), + else => {}, + } // What the post chain sees of the pointer and the focus. switch (sev.type) { c.SDL_EVENT_MOUSE_MOTION => { @@ -4202,11 +4215,36 @@ fn pollFrame(ctx: ?*anyopaque) void { /// Frames drawn for a blink alone, with no core render (tests, tracing). var blink_redraws: u64 = 0; +/// PARDES_FRAMES_CAPTURE's directory, and how many frames it has. +var frame_capture_dir: ?[]const u8 = null; +var frame_capture_count: u32 = 0; + +fn captureSeries(dir: []const u8, pixels: []const u8, w: u32, h: u32) void { + var path_buf: [4096]u8 = undefined; + const path = std.fmt.bufPrintSentinel(&path_buf, "{s}/{d:0>6}.bgra", .{ dir, frame_capture_count }, 0) catch return; + const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o644)); + if (fd < 0) return; + _ = libc.write(fd, pixels.ptr, pixels.len); + _ = libc.close(fd); + const times = std.fmt.bufPrintSentinel(&path_buf, "{s}/times.txt", .{dir}, 0) catch return; + const tf = libc.open(times, .{ .ACCMODE = .WRONLY, .CREAT = true, .APPEND = true }, @as(libc.mode_t, 0o644)); + if (tf < 0) return; + var line_buf: [96]u8 = undefined; + const line = std.fmt.bufPrint(&line_buf, "{d} {d} {d} {d}\n", .{ frame_capture_count, monoNs(), w, h }) catch ""; + _ = libc.write(tf, line.ptr, line.len); + _ = libc.close(tf); + frame_capture_count += 1; +} + +/// The core's frame probe (FrameStats), for the latency of the inputs the +/// shell reads and the zones its own work goes in; null in tests. +var frame_probe: ?*pardes.FrameStats = null; + /// How long until the timedemo's next step, ms; null when none runs. fn demoDue(g: *const Gui) ?c_int { const next = g.demo_next orelse return null; - if (g.demo_start_ns == 0 or next >= pardes.FrameStats.demo.len) return 0; - const at = g.demo_start_ns + @as(u64, pardes.FrameStats.demo[next].at_ms) * std.time.ns_per_ms; + if (g.demo_start_ns == 0 or next >= g.demo.len) return 0; + const at = g.demo_start_ns + @as(u64, g.demo[next].at_ms) * std.time.ns_per_ms; return @intCast(@min(std.math.maxInt(c_int), (at -| monoNs()) / std.time.ns_per_ms)); } @@ -4215,16 +4253,56 @@ fn demoDue(g: *const Gui) ?c_int { /// focused pane's body; its clock starts at the first loop. Its end /// ends the session (PARDES_FRAMES has the timings then). fn runDemo(g: *Gui, in: *Input, core: *pardes.Pardes) void { - const steps = pardes.FrameStats.demo; + const steps = g.demo; var next = g.demo_next orelse return; const now = monoNs(); - if (g.demo_start_ns == 0) g.demo_start_ns = now; + if (g.demo_start_ns == 0) { + g.demo_start_ns = now; + core.frames.demo_ns = now; + core.frames.demo_name = g.demo_name; + } defer g.demo_next = next; while (next < steps.len and g.demo_start_ns + @as(u64, steps[next].at_ms) * std.time.ns_per_ms <= now) : (next += 1) { const r = core.rects[core.active]; switch (steps[next].act) { - .wheel => |notches| wheelAt(g, in, .{ .y = @floatFromInt(-notches), .integer_y = -notches }, .{ .col = r.x + r.w / 2, .row = r.y + r.h / 2 }), - .key => |key| in.post(.{ .key = key }), + .wheel => |notches| { + core.frames.input(now); + wheelAt(g, in, .{ .y = @floatFromInt(-notches), .integer_y = -notches }, .{ .col = r.x + r.w / 2, .row = r.y + r.h / 2 }); + }, + .key => |key| { + core.frames.input(now); + in.post(.{ .key = key }); + }, + .phase => {}, + .click => |click| { + core.frames.input(now); + const pane = core.panes[core.active] orelse continue; + const top = core.bodyTop(pane, r); + const row: u16 = top + @as(u16, @intFromFloat(click.at * @as(f32, @floatFromInt(r.h -| pane.tag_rows -| 1)))); + const col: u16 = if (click.rail) r.x else r.x + config.GUTTER + 4; + for ([_]pardes.Mouse.Kind{ .press, .release }) |kind| in.post(.{ .mouse = .{ .button = click.button, .kind = kind, .col = col, .row = row } }); + }, + .rail_wheel => |notches| { + core.frames.input(now); + wheelAt(g, in, .{ .y = @floatFromInt(-notches), .integer_y = -notches }, .{ .col = r.x, .row = r.y + r.h / 2 }); + }, + .paste => |text| { + core.frames.input(now); + in.post(.{ .paste = text }); + }, + .exec => |line| { + core.frames.input(now); + var newest: u32 = 0; + for (core.panes) |slot| if (slot) |pane| { + newest = @max(newest, pane.serial); + }; + _ = pardes.exec.executeFrom(core, core.active, line, false); + // What it opened is what the next steps scroll. + for (core.panes, 0..) |slot, id| if (slot) |pane| if (pane.serial > newest) { + core.active = id; + core.needs_frame = true; + }; + }, .end => in.close(), } } @@ -5922,6 +6000,7 @@ fn renderFrame( // The frame's CPU side: groups, decor, cover and every instance, up to // their upload. const build_zone = tracy.zone(@src(), "gui frame build"); + const probe_build = monoNs(); var building = true; defer if (building) build_zone.end(); const layout = fixedCellLayout(g); @@ -6265,6 +6344,7 @@ fn renderFrame( build_zone.end(); building = false; + if (frame_probe) |probe| probe.zone(.gui_build, probe_build); const rp = c.SDL_BeginGPURenderPass(cmd, &color_target, 1, null); pushLayerOpacity(cmd, rp, background_opacity); const whole = c.SDL_Rect{ .x = 0, .y = 0, .w = @intCast(sw), .h = @intCast(sh) }; @@ -7572,6 +7652,11 @@ fn cellSlot(g: *Gui, line: []const pardes.Cell, at: usize, cursor: ?usize, role: // A new word (a hash collision takes the map entry over). Runs of one // face are shaped in that face; fallback faces shape like the primary. + const probe_shape = monoNs(); + defer if (frame_probe) |probe| { + probe.zone(.gui_shape, probe_shape); + probe.shaped +|= 1; + }; const n = text.len; words.slots.ensureUnusedCapacity(gpa, n) catch { words.text.shrinkRetainingCapacity(text_start); @@ -7769,6 +7854,11 @@ fn cachedGlyph(g: *Gui, key: GlyphKey) ?Slot { g.pen_y += g.cell_h; } if (g.pen_y + g.cell_h > atlas_h) return null; // atlas full: fall back + const probe_raster = monoNs(); + defer if (frame_probe) |probe| { + probe.zone(.gui_raster, probe_raster); + probe.rastered +|= 1; + }; const s = Slot{ .u = g.pen_x, .v = g.pen_y }; const out = g.atlas_stage.ptr + @as(usize, s.v) * atlas_w + s.u; const face = if (key.face == 0) g.font else g.fallbacks[key.face - 1].?.face; @@ -8367,21 +8457,31 @@ fn softPresentFrame(g: *Gui, cmd: *c.SDL_GPUCommandBuffer, target: *c.SDL_GPUTex const fence = c.SDL_SubmitGPUCommandBufferAndAcquireFence(cmd) orelse return error.GpuSubmit; defer c.SDL_ReleaseGPUFence(g.device, fence); var fences = [_]*c.SDL_GPUFence{fence}; + const probe_wait = monoNs(); if (!c.SDL_WaitForGPUFences(g.device, true, &fences, 1)) return error.GpuSubmit; + if (frame_probe) |probe| probe.zone(.gui_wait, probe_wait); const mapped = c.SDL_MapGPUTransferBuffer(g.device, xfer, false) orelse return error.GpuMap; defer c.SDL_UnmapGPUTransferBuffer(g.device, xfer); const texture = try ensureSoftTexture(g, sw, sh); const renderer = g.soft_renderer orelse return error.GpuCreate; + const probe_upload = monoNs(); + defer if (frame_probe) |probe| probe.zone(.gui_upload, probe_upload); if (!c.SDL_UpdateTexture(texture, null, mapped, @intCast(sw * bpp))) return error.GpuMap; + // PARDES_FRAMES_CAPTURE: every presented frame's pixels as they went + // out, and when, for a recording at the frames' own times (the + // samples of docs/next-anim.md). One write a frame; nothing else. + if (frame_capture_dir) |dir| captureSeries(dir, @as([*]const u8, @ptrCast(mapped))[0..size], sw, sh); // The cell shader already produced premultiplied backgrounds with opaque // foreground ink. Copy that alpha unchanged; never modulate the full frame. _ = c.SDL_SetRenderDrawBlendMode(renderer, c.SDL_BLENDMODE_NONE); _ = c.SDL_SetRenderDrawColor(renderer, 0, 0, 0, if (g.surface_opacity) 0 else 255); _ = c.SDL_RenderClear(renderer); _ = c.SDL_RenderTexture(renderer, texture, null, null); + const probe_flip = monoNs(); _ = c.SDL_RenderPresent(renderer); + if (frame_probe) |probe| probe.zone(.gui_flip, probe_flip); } fn backgroundOpacity(percent: u8) f32 { |
