Found live on the VM: the drag ended as a click (back to picking). SUMMIT_SHOT=<prefix> plus a <prefix>.req file makes the window write the frame it is about to show (the shared, composited llvmpipe display gave out-of-order window captures).
353 lines
15 KiB
Python
353 lines
15 KiB
Python
"""Compare's second step (framegen/compare.py): Same area (cells, picking, the picked area in every cell, zoom, the
|
|
free box and the inset map), Freeze, Flip, labels at the bottom, --compare-area. No GPU or window needed."""
|
|
import pathlib
|
|
import sys
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
|
from framegen import compare as C # noqa: E402
|
|
from framegen.settings import Settings # noqa: E402
|
|
|
|
|
|
def live_with(spec: str = "aa:off,fxaa,smaa,cas", **values) -> tuple[Settings, C.Live]:
|
|
st = Settings()
|
|
for k, v in C.settings_for(spec).items():
|
|
st.override(k, v)
|
|
for k, v in values.items():
|
|
st.override(k, v)
|
|
return st, C.Live(st, lambda: C.Caps())
|
|
|
|
|
|
def quadrants(w: int = 64, h: int = 36) -> np.ndarray:
|
|
"""A picture whose four quarters are 10, 20, 30, 40, with a pixel-wide ramp so position shows."""
|
|
img = np.zeros((h, w, 3), np.uint8)
|
|
img[: h // 2, : w // 2], img[: h // 2, w // 2:] = 10, 20
|
|
img[h // 2:, : w // 2], img[h // 2:, w // 2:] = 30, 40
|
|
return img
|
|
|
|
|
|
# ---- cells and areas ------------------------------------------------------------------------------------------
|
|
def test_cells_are_halves_thirds_or_a_2x2():
|
|
assert C.cells(2, 1920, 1080) == [(0, 0, 960, 1080), (960, 0, 1920, 1080)]
|
|
assert C.cells(3, 1920, 1080) == [(0, 0, 640, 1080), (640, 0, 1280, 1080), (1280, 0, 1920, 1080)]
|
|
assert C.cells(4, 1920, 1080) == [(0, 0, 960, 540), (960, 0, 1920, 540), (0, 540, 960, 1080),
|
|
(960, 540, 1920, 1080)]
|
|
assert C.cell_at(4, 0.9, 0.9) == 4 and C.cell_at(4, 0.1, 0.9) == 3 and C.cell_at(3, 0.5, 0.2) == 2
|
|
assert C.dividers("grid", 3, 1920, 1080) == [(640, 0, 640, 1080), (1280, 0, 1280, 1080)]
|
|
|
|
|
|
def test_the_picked_quarter_is_shown_in_every_cell_pixel_for_pixel():
|
|
a = C.area_of("4", 1, 4)
|
|
r = C.rects("grid", 4, 1920, 1080, area=a)
|
|
assert all(src == (960, 540, 1920, 1080) for _, src in r) # the bottom-right quarter, four times
|
|
assert [d for d, _ in r] == C.cells(4, 1920, 1080) # each filling its cell, at 1x
|
|
img = quadrants()
|
|
out = C.compose([img] * 4, C.rects("grid", 4, 64, 36, area=a))
|
|
assert (out == 40).all()
|
|
|
|
|
|
def test_the_old_grid_is_the_middle_at_1x():
|
|
"""Before an area is given, the grid shows the picture's middle, a cell's size (round 2's "same crop")."""
|
|
assert C.rects("grid", 4, 1920, 1080)[0][1] == (480, 270, 1440, 810)
|
|
|
|
|
|
def test_picking_shows_each_cell_as_it_is():
|
|
r = C.rects("grid", 3, 1920, 1080, picking=True)
|
|
assert all(d == s for d, s in r)
|
|
|
|
|
|
def test_zoom_enlarges_by_whole_pixels_with_nearest_neighbour():
|
|
a = C.Area(0.5, 0.5, 4, 0)
|
|
(d, s), *_ = C.rects("grid", 4, 1920, 1080, area=a)
|
|
assert (s[2] - s[0], s[3] - s[1]) == (240, 135) and (d[2] - d[0], d[3] - d[1]) == (960, 540)
|
|
# nearest neighbour: every source pixel becomes a 4x4 block, no new values in between
|
|
img = np.random.default_rng(1).integers(0, 255, (1080, 1920, 3), dtype=np.uint8)
|
|
out = C.compose([img] * 4, C.rects("grid", 4, 1920, 1080, area=a))
|
|
cell = out[:540, :960]
|
|
src = img[s[1]:s[3], s[0]:s[2]]
|
|
assert np.array_equal(cell, np.repeat(np.repeat(src, 4, 0), 4, 1))
|
|
|
|
|
|
def test_a_zoom_that_doesnt_fill_the_cell_is_centred_and_the_rest_is_black():
|
|
st, live = live_with(compare_layout="grid", compare_cell="free", compare_zoom=6)
|
|
p = live.plan()
|
|
d = C.draws(p, 1280, 720)
|
|
assert d[0][0] == -1 # black first, under the zoomed area
|
|
(_, cell, _), = [t for t in d if t[0] == -1][:1]
|
|
_, dst, src = next(t for t in d if t[0] == 0)
|
|
assert (dst[2] - dst[0]) == (src[2] - src[0]) * 6 and dst[0] >= cell[0] and dst[2] <= cell[2]
|
|
out = C.compose([np.full((720, 1280, 3), 200, np.uint8)] * 4, d, np.full((720, 1280, 3), 99, np.uint8))
|
|
assert out[:360, :640].min() in (0, 200) and (out[:360, :640] != 99).all()
|
|
|
|
|
|
def test_compose_scales_torch_the_same_as_numpy():
|
|
torch = pytest.importorskip("torch")
|
|
img = np.random.default_rng(2).integers(0, 255, (72, 128, 3), dtype=np.uint8)
|
|
r = C.rects("grid", 4, 128, 72, area=C.Area(0.3, 0.6, 3, 0))
|
|
a = C.compose([img] * 4, r)
|
|
b = C.compose([torch.from_numpy(img)] * 4, r)
|
|
assert np.array_equal(a, b.numpy())
|
|
|
|
|
|
def test_the_free_box_stays_inside_the_picture_and_snaps_to_cells():
|
|
assert C.clamp_centre(4, 2, 0.0, 1.0) == (0.125, 0.875)
|
|
assert C.snap(4, 0.26, 0.74) == 3 and C.snap(4, 0.4, 0.5) == 0
|
|
r = C.area_rect(C.Area(0.99, 0.99, 2, 0), 4, 1920, 1080)
|
|
assert r[2] == 1920 and r[3] == 1080
|
|
|
|
|
|
def test_the_inset_map_shows_the_whole_picture_in_a_corner():
|
|
st, live = live_with(compare_layout="grid", compare_cell="2")
|
|
d = C.draws(live.plan(), 1920, 1080)
|
|
i, dst, src = d[-1]
|
|
assert i == 0 and src == (0, 0, 1920, 1080)
|
|
assert dst[2] < 1920 and dst[3] < 1080 and dst[0] > 1920 // 2 # bottom right, labels at the top
|
|
st.override("compare_labels_at", "bottom")
|
|
assert C.draws(live.plan(), 1920, 1080)[-1][1][1] < 100 # top right, labels at the bottom
|
|
|
|
|
|
# ---- the plan from the settings, and the keys and mouse --------------------------------------------------------
|
|
def test_same_area_starts_by_picking_and_the_keys_pick():
|
|
st, live = live_with()
|
|
assert live.key("area") is True
|
|
p = live.plan()
|
|
assert p.layout == "grid" and p.picking and p.sel == 0
|
|
live.key("right"), live.key("down")
|
|
assert live.plan().sel == 3
|
|
live.key("enter")
|
|
p = live.plan()
|
|
assert not p.picking and p.area.cell == 4 and p.area.zoom == 1
|
|
assert C.labels(p, C.Timings())[1][0] == "FXAA · 1x" # the zoom, on every label
|
|
live.key("enter") # back to picking, on the cell it was
|
|
assert live.plan().picking and live.plan().sel == 3
|
|
live.key("2")
|
|
assert live.plan().area.cell == 2
|
|
live.key("back") # Esc leaves Same area, back to the layout before
|
|
assert live.plan().layout == "columns"
|
|
|
|
|
|
def test_the_mouse_picks_pans_and_zooms():
|
|
st, live = live_with(compare_layout="grid")
|
|
live.key("area"), live.key("area") # off and on again: picking
|
|
assert live.press(1500, 300, 1920, 1080) # picking: a press picks at once
|
|
assert live.plan().area.cell == 2
|
|
assert live.wheel(1.0) # zoom in
|
|
p = live.plan()
|
|
assert p.area.zoom == 2 and p.area.cell == 2
|
|
live.press(1000, 500, 1920, 1080)
|
|
live.move(1400, 500, 1920, 1080) # a drag: the scene follows the hand, so the box goes left
|
|
live.release()
|
|
p = live.plan()
|
|
assert p.area.cell == 0 and p.area.cx < 0.75 and st.compare_cell == "free"
|
|
live.press(1000, 500, 1920, 1080)
|
|
live.release() # a click: back to picking
|
|
assert live.plan().picking
|
|
|
|
|
|
def test_a_drag_whose_moves_were_missed_is_still_a_drag():
|
|
st, live = live_with(compare_layout="grid", compare_cell="1", compare_zoom=2)
|
|
live.press(400, 300, 1920, 1080)
|
|
live.release(100, 200, 1920, 1080) # (the window saw no motion in between)
|
|
assert st.compare_cell == "free" and live.plan().area.cx > 0.25
|
|
|
|
|
|
def test_the_inset_map_moves_the_box():
|
|
st, live = live_with(compare_layout="grid", compare_cell="1", compare_zoom=4)
|
|
ix0, iy0, ix1, iy1 = C.inset_rect(1920, 1080)
|
|
live.press(ix0 + (ix1 - ix0) * 0.6, iy0 + (iy1 - iy0) * 0.3, 1920, 1080)
|
|
live.release()
|
|
a = live.plan().area
|
|
assert a.cell == 0 and abs(a.cx - 0.6) < 0.01 and abs(a.cy - 0.3) < 0.01
|
|
|
|
|
|
def test_the_overlay_map_click():
|
|
st, live = live_with(compare_layout="grid", compare_cell="pick")
|
|
live.map_click(0.8, 0.8)
|
|
assert live.plan().area.cell == 4 # at 1x: the cell there
|
|
st.override("compare_zoom", 3)
|
|
live.map_click(0.4, 0.45)
|
|
a = live.plan().area
|
|
assert a.cell == 0 and a.zoom == 3 and (a.cx, a.cy) == (0.4, 0.45)
|
|
v = live.area_view()
|
|
assert v["n"] == 4 and v["zoom"] == 3 and v["box"][0] < 0.4 < v["box"][2]
|
|
|
|
|
|
def test_arrow_keys_nudge_a_picked_area():
|
|
st, live = live_with(compare_layout="grid", compare_cell="1", compare_zoom=2)
|
|
live.key("right")
|
|
a = live.plan().area
|
|
assert a.cell == 0 and a.cx > 0.25
|
|
|
|
|
|
def test_fewer_strips_fewer_cells():
|
|
st, live = live_with("aa:off,fxaa", compare_layout="grid")
|
|
assert len(C.cells(live.plan().n, 100, 100)) == 2
|
|
assert not C.allowed("compare_cell", "3", st.values()) and C.allowed("compare_cell", "2", st.values())
|
|
live.key("4")
|
|
assert live.plan().picking # no fourth cell
|
|
|
|
|
|
# ---- Freeze ----------------------------------------------------------------------------------------------------
|
|
def test_freeze_holds_one_frame_for_every_strip():
|
|
st, live = live_with("aa:off,fxaa")
|
|
a, b = quadrants(), quadrants()[::-1].copy()
|
|
live.key("freeze")
|
|
p = live.plan()
|
|
assert p.frozen
|
|
first = live.source_side(a, p, a, 2)
|
|
again = live.source_side(b, p, b, 2) # a new game frame: the still stays
|
|
assert np.array_equal(first, again)
|
|
items = C.overlay_items(p, C.labels(p, C.Timings()), 640, 360, 0.5)
|
|
assert len(items) == 1 + 1 + 2 # a divider, the FROZEN mark, two labels
|
|
live.key("freeze")
|
|
assert not np.array_equal(live.source_side(b, live.plan(), b, 2), first)
|
|
|
|
|
|
def test_frame_generation_cant_be_frozen():
|
|
st, live = live_with("framegen:off,on")
|
|
assert live.key("freeze") == C.FREEZE_WHY
|
|
st.override("compare_freeze", True)
|
|
assert not live.plan().frozen
|
|
assert not C.allowed("compare_freeze", True, st.values())
|
|
|
|
|
|
def test_freeze_works_for_window_side_comparisons_too():
|
|
"""Upscaler and look strips are drawn by the window from the frame the pipeline sends: frozen, that is the
|
|
held one."""
|
|
st, live = live_with("scaler:off,bilinear")
|
|
live.key("freeze")
|
|
p = live.plan()
|
|
a, b = quadrants(), quadrants()[::-1].copy()
|
|
assert np.array_equal(live.source_side(a, p, a, 2), live.source_side(b, p, b, 2))
|
|
|
|
|
|
# ---- Flip ------------------------------------------------------------------------------------------------------
|
|
def test_flip_shows_the_whole_picture_with_a_then_b():
|
|
st, live = live_with("aa:off,fxaa")
|
|
live.key("flip_down")
|
|
p = live.plan()
|
|
assert p.flip == "a" and C.draws(p, 64, 36) == [(0, (0, 0, 64, 36), (0, 0, 64, 36))]
|
|
assert C.labels(p, C.Timings())[0][0] == "A: Off"
|
|
live.key("flip_up")
|
|
p = live.plan()
|
|
assert p.flip == "b" and C.draws(p, 64, 36)[0][0] == 1
|
|
t = C.Timings()
|
|
C.aa_strips(quadrants(), p, t)
|
|
assert t.stat("aa:fxaa") is not None
|
|
items = C.overlay_items(p, C.labels(p, t), 640, 360, 0.5)
|
|
assert len(items) == 1 # one label, no divider
|
|
live.key("back")
|
|
assert live.plan().flip == "off"
|
|
|
|
|
|
def test_flip_needs_two_strips():
|
|
st, live = live_with()
|
|
assert "two" in live.key("flip_down")
|
|
st.override("compare_flip", "a")
|
|
assert live.plan().flip == "off"
|
|
|
|
|
|
def test_flip_runs_only_the_strip_on_screen():
|
|
st, live = live_with("aa:off,fxaa", compare_flip="a")
|
|
t = C.Timings()
|
|
out = C.aa_strips(quadrants(), live.plan(), t)
|
|
assert t.stat("aa:fxaa") is None and np.array_equal(out, quadrants())
|
|
|
|
|
|
# ---- labels at the bottom --------------------------------------------------------------------------------------
|
|
def test_labels_can_go_to_the_bottom():
|
|
st, live = live_with()
|
|
p = live.plan()
|
|
top = C.overlay_items(p, C.labels(p, C.Timings()), 1280, 720, 0.67)
|
|
st.override("compare_labels_at", "bottom")
|
|
p = live.plan()
|
|
bot = C.overlay_items(p, C.labels(p, C.Timings()), 1280, 720, 0.67)
|
|
assert max(y for _, y, _ in top[3:]) < 100 and min(y for _, y, _ in bot[3:]) > 600
|
|
|
|
|
|
def test_picker_marks_and_hint():
|
|
st, live = live_with(compare_layout="grid")
|
|
p = live.plan()
|
|
items = C.overlay_items(p, C.labels(p, C.Timings()), 1280, 720, 0.67, live.hint)
|
|
# two dividers, an outline and a number per cell, the hint, four labels
|
|
assert len(items) == 2 + 8 + 1 + 4
|
|
img = C.blend(np.zeros((720, 1280, 3), np.uint8), items)
|
|
assert img[1:4, 10:600, 2].mean() > 250 # the highlighted cell's outline, in the accent (red channel high)
|
|
|
|
|
|
def test_view_changes_keep_the_timings():
|
|
st, live = live_with()
|
|
live.timings.add("aa:fxaa", 1.0)
|
|
live.key("area")
|
|
live.key("1")
|
|
live.key("freeze")
|
|
st.override("compare_labels_at", "bottom")
|
|
assert live.timings.stat("aa:fxaa") is not None
|
|
st.override("compare_dim", "scaler")
|
|
assert live.timings.stat("aa:fxaa") is None
|
|
|
|
|
|
# ---- --compare-area ----------------------------------------------------------------------------------------------
|
|
def test_compare_area_takes_a_cell_or_fractions():
|
|
s, centre = C.area_settings("3", 4)
|
|
assert s == {"compare_layout": "grid", "compare_cell": "3", "compare_zoom": 1} and centre == (0.25, 0.75)
|
|
s, centre = C.area_settings("0.1,0.1,0.1,0.1", 4)
|
|
assert s["compare_cell"] == "free" and s["compare_zoom"] == 4 and centre == pytest.approx((0.15, 0.15))
|
|
s, _ = C.area_settings("0,0,0.5,0.5", 4)
|
|
assert s["compare_cell"] == "1" and s["compare_zoom"] == 1
|
|
for bad in ("5", "0.9,0.9,0.5,0.5", "a,b", "1,2"):
|
|
with pytest.raises(ValueError):
|
|
C.area_settings(bad, 4)
|
|
|
|
|
|
# ---- the overlay's Compare tab ---------------------------------------------------------------------------------
|
|
def overlay_on(row_key: str, **values):
|
|
from framegen.menu import Menu
|
|
st, live = live_with(**values)
|
|
m = Menu(st, allowed=lambda k, v: C.allowed(k, v, st.values()) if k.startswith("compare_") else True)
|
|
m.area_view, m.area_click = live.area_view, live.map_click
|
|
m.open = True
|
|
m.show("Compare")
|
|
rows = m.rows()
|
|
m._select([r.key for r in rows].index(row_key), rows)
|
|
return st, live, m
|
|
|
|
|
|
def test_every_compare_action_is_a_row_in_the_overlay():
|
|
st, live, m = overlay_on("compare_layout", compare_layout="grid")
|
|
keys = {r.key for r in m.rows()}
|
|
assert {"compare", "compare_dim", "compare_strips", "compare_layout", "compare_cell", "compare_zoom",
|
|
"compare_freeze", "compare_flip", "compare_labels_at"} <= keys
|
|
st.override("compare_layout", "columns")
|
|
st.override("compare_strips", 2)
|
|
assert "compare_split" in {r.key for r in m.rows()} and "compare_cell" not in {r.key for r in m.rows()}
|
|
|
|
|
|
def test_the_area_row_is_buttons_and_its_map_picks():
|
|
st, live, m = overlay_on("compare_cell", compare_layout="grid")
|
|
row = m.rows()[m.index]
|
|
assert m.kind_of(row) == "buttons"
|
|
m.render(0.67, 0.9)
|
|
x0, y0, x1, y1 = m._hit["map"]
|
|
m.click(x0 + (x1 - x0) * 0.8, y0 + (y1 - y0) * 0.2) # the top-right cell
|
|
assert st.compare_cell == "2" and live.plan().area.cell == 2
|
|
m.key("right") # the keys step through Pick, 1..4, Free
|
|
assert st.compare_cell == "3"
|
|
|
|
|
|
def test_the_area_row_greys_out_cells_past_the_strips():
|
|
st, live, m = overlay_on("compare_cell", compare_layout="grid", compare_strips=2)
|
|
row = m.rows()[m.index]
|
|
assert m.can(row, "2") and not m.can(row, "3")
|
|
|
|
|
|
def test_the_overlay_fits_a_1280_wide_window():
|
|
"""Round 2's overlay was cut off at the right in a 1280 px window: render() takes the room there is."""
|
|
st, live, m = overlay_on("compare")
|
|
ui = 720 / 1080 * 1.5 # a bigger overlay (Overlay size 150%) in a 1280 x 720 window
|
|
img = m.render(ui, 0.9, 1280 - 2 * int(16 * ui))
|
|
assert img.shape[1] <= 1280 - 2 * int(16 * ui)
|
|
assert m.render(720 / 1080, 0.9, 1248).shape[1] == round(1100 * 720 / 1080) # room enough: its own width
|