"""Renders every stats overlay style to PNG without a GPU or a window, from made-up numbers (clearly labelled on the picture): `python devtools/render_stats_samples.py OUT_DIR [--ui 1.0]`. For looking at the layout and the colours; the numbers are NOT measurements. tests/test_stats_hud.py builds the same data.""" from __future__ import annotations import argparse import pathlib import sys import types sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) import numpy as np from PIL import Image from framegen import hud, stats as S from framegen.io import MiniText LABEL = "SAMPLE DATA, not measured" def sample_collector(hz: int = 120, stutter: bool = True, seconds: float = 40.0, seed: int = 1) -> S.Collector: """A collector fed 40 s of made-up frames at `hz` (a stutter now and then), stage times and system numbers.""" rng = np.random.default_rng(seed) c = S.Collector(window_s=30.0) c.set_target(hz) c.set_config(model="RIFE 4.22", frame_generation=True, multiplier=f"{hz // 30}x", hz=hz, upscaler="FSR 1", quality="sharpness 0.5", output="2560x1440", source="1920x1080", look="none") t, i = 1000.0, 0 base = 1000.0 / hz c.present(t, False) while t < 1000.0 + seconds: ms = base * (1 + 0.04 * rng.standard_normal()) if stutter and rng.random() < 0.004: ms = base * rng.uniform(2.0, 4.5) t += ms / 1000.0 i += 1 c.present(t, i % 4 != 0) c.tick(i % 2 == 0) c.stage("capture", float(abs(rng.normal(0.4, 0.1))), t) c.stage("interp", float(abs(rng.normal(5.2, 0.6))), t) c.stage("upscale", float(abs(rng.normal(1.3, 0.1))), t) c.stage("look", float(abs(rng.normal(0.2, 0.03))), t) c.stage("present", float(abs(rng.normal(0.8, 0.2))), t) c.drop(3, t - 5) c.repeat(2, t - 8) c.attach(types.SimpleNamespace(real_delay_ms=list(rng.normal(28, 4, 300)))) c.sampler = types.SimpleNamespace(latest={"cpu": 34.0, "gpu": {"util": 87.0, "power": 232.0, "temp": 76.0, "mem": 7340.0, "mem_total": 24576.0, "clock": 1890.0}}, history=[{"cpu": 30.0 + k % 9, "gpu": {"util": 80.0 + k % 14, "power": 220.0 + k % 20, "temp": 70.0 + k % 7, "mem": 7100.0 + k * 3, "mem_total": 24576.0}} for k in range(40)]) return c def text_for(c: S.Collector, style: str, colours: bool = True, items=None) -> MiniText: snap = c.snapshot(game_fps=30.0) items = items or S.wanted(style, lambda k: "auto") parts = [(f"{snap['fps']:.0f} fps", "main"), ("game 30", "dim"), (f"delay {snap['delay']:.0f} ms", "dim")] if style == "detailed": head = [k for k in items if k in ("fps", "game", "delay")] cells, rows = S.all_cells(snap, head, colours, True), S.all_cells(snap, [k for k in items if k not in head], colours, False) else: cells, rows = S.all_cells(snap, items, colours, True), () graph = None if style == "graphs": rng = np.random.default_rng(3) graph = {"hz": c.hz, "tick_ms": 16.7, "busy": list(rng.normal(9, 2, 300)), "delay": list(rng.normal(28, 5, 300)), "show": None, "low1": snap["low1"], "low01": snap["low01"]} return MiniText.make(parts, "good", LABEL, graph, cells=cells, rows=rows) def main() -> None: ap = argparse.ArgumentParser() ap.add_argument("out") ap.add_argument("--ui", type=float, default=1.0) a = ap.parse_args() out = pathlib.Path(a.out) out.mkdir(parents=True, exist_ok=True) c = sample_collector() rng = np.random.default_rng(2) ft = list(np.abs(rng.normal(8.3, 0.4, 300))) bg = np.full((1080, 1920, 4), (46, 70, 52, 255), np.uint8) # a plain "game" colour behind them shots = {"minimal": hud.stats(text_for(c, "minimal"), a.ui, 0.8), "detailed": hud.stats(text_for(c, "detailed"), a.ui, 0.8), "graphs": hud.graphs(text_for(c, "graphs"), ft, a.ui, 0.8), "bar": hud.bar(text_for(c, "bar"), 1920, a.ui, 0.8)} summ = c.summary(reason="sample data") summ["benchmark"] = {"seconds": 40} shots["card"] = hud.card(summ, a.ui, 0.96, "(sample data, not saved)") for name, img in shots.items(): canvas = bg.copy() h, w = img.shape[:2] x, y = (0, 0) if name == "bar" else (16, 16) if name != "card" else ((1920 - w) // 2, (1080 - h) // 2) region = canvas[y:y + h, x:x + w] a_ = img[..., 3:4].astype(np.float32) / 255 region[..., :3] = (img[..., :3] * a_ + region[..., :3] * (1 - a_)).astype(np.uint8) Image.fromarray(canvas[: max(y + h + 16, 120) if name != "card" else 1080, : (1920 if name in ("bar", "card") else x + w + 16)]).save(out / f"sample-{name}.png") print(out / f"sample-{name}.png", img.shape) if __name__ == "__main__": main()