v0.4.0 b6be3a8

damage_delta_test.go RawHistoryFolder

1package vulkan
2
3import (
4	"bytes"
5	"image"
6	"image/color"
7	"reflect"
8	"slices"
9	"testing"
10
11	"github.com/stretchr/testify/assert"
12	"github.com/stretchr/testify/require"
13
14	"github.com/bnema/neferwl/internal/ports"
15)
16
17// TestSceneFieldCount fails when Scene gains or loses a field: sceneDelta
18// lists the fields that force a full redraw.
19func TestSceneFieldCount(t *testing.T) {
20	const fields = 20
21	assert.Equal(t, fields, len(reflect.VisibleFields(reflect.TypeOf(ports.Scene{}))),
22		"Scene fields changed: check sceneDelta (a field it does not compare must force a full redraw or be covered), then this count")
23}
24
25// TestSceneWindowFieldCount fails when SceneWindow gains or loses a field:
26// sceneDelta compares windows by struct equality, so a new field is covered
27// as long as it only changes where or how that window draws (inside Rect).
28func TestSceneWindowFieldCount(t *testing.T) {
29	const fields = 15
30	assert.Equal(t, fields, len(reflect.VisibleFields(reflect.TypeOf(ports.SceneWindow{}))),
31		"SceneWindow fields changed: check sceneDelta (a field that draws outside Rect needs its own rule), then this count")
32}
33
34func deltaScene(seq uint64, ws ...ports.SceneWindow) ports.Scene {
35	return ports.Scene{Seq: seq, OutputWidth: 1000, OutputHeight: 500, Scale: 1, Background: "#000000", Windows: ws}
36}
37
38func win(id ports.WindowID, x, y, w, h int) ports.SceneWindow {
39	return ports.SceneWindow{ID: id, Rect: ports.Rect{X: x, Y: y, W: w, H: h}}
40}
41
42// held is a target that holds s after a full frame of its windows.
43func held(s ports.Scene) *target {
44	tg := &target{}
45	tg.hold(s, &damageRegion{drawn: map[ports.WindowID]heldWindow{}})
46	return tg
47}
48
49func TestDamageOneWindowResizeIsUnionOfRects(t *testing.T) {
50	bounds := image.Rect(0, 0, 1000, 500)
51	old := deltaScene(1, win(1, 0, 0, 300, 500), win(2, 300, 0, 300, 500), win(3, 600, 0, 400, 500))
52	cur := deltaScene(2, win(1, 0, 0, 300, 500), win(2, 300, 0, 300, 500), win(3, 600, 0, 380, 400))
53	d := newDamage(held(old), cur, bounds)
54	require.False(t, d.all())
55	assert.Equal(t, image.Rect(600, 0, 1000, 500), d.area)
56
57	// Window moves: both places.
58	cur = deltaScene(2, win(1, 0, 0, 300, 500), win(2, 310, 0, 300, 500), win(3, 600, 0, 400, 500))
59	d = newDamage(held(old), cur, bounds)
60	require.False(t, d.all())
61	assert.Equal(t, image.Rect(300, 0, 610, 500), d.area)
62}
63
64func TestDamageDeltaAtScale(t *testing.T) {
65	old := deltaScene(1, win(1, 0, 0, 10, 10), win(2, 100, 0, 10, 10))
66	cur := deltaScene(2, win(1, 0, 0, 10, 10), win(2, 100, 0, 20, 10))
67	old.Scale, cur.Scale = 1.5, 1.5
68	d := newDamage(held(old), cur, image.Rect(0, 0, 1500, 750))
69	require.False(t, d.all())
70	assert.Equal(t, image.Rect(150, 0, 180, 15), d.area)
71}
72
73func TestDamageDeltaWindowFieldChange(t *testing.T) {
74	old := deltaScene(1, win(1, 0, 0, 100, 100), win(2, 100, 0, 100, 100))
75	cur := deltaScene(2, win(1, 0, 0, 100, 100), win(2, 100, 0, 100, 100))
76	cur.Windows[0].FocusEffect = 0.3
77	d := newDamage(held(old), cur, image.Rect(0, 0, 1000, 500))
78	require.False(t, d.all())
79	assert.Equal(t, image.Rect(0, 0, 100, 100), d.area)
80}
81
82func TestDamageDeltaFullRedraws(t *testing.T) {
83	base := func() ports.Scene {
84		return deltaScene(1, win(1, 0, 0, 100, 100), win(2, 100, 0, 100, 100))
85	}
86	bounds := image.Rect(0, 0, 1000, 500)
87	for name, mod := range map[string]func(*ports.Scene){
88		"camera shift over half": func(s *ports.Scene) {
89			s.Windows = []ports.SceneWindow{win(1, 0, 0, 600, 500), win(2, 600, 0, 400, 500)}
90			s.Windows[0].Rect.X = 50
91		},
92		"layer":          func(s *ports.Scene) { s.Layers = []ports.SceneLayer{{ID: 9, Rect: ports.Rect{W: 10, H: 10}}} },
93		"output":         func(s *ports.Scene) { s.Output = "DP-2" },
94		"size":           func(s *ports.Scene) { s.OutputWidth = 999 },
95		"scale":          func(s *ports.Scene) { s.Scale = 2 },
96		"transform":      func(s *ports.Scene) { s.Transform = 1 },
97		"off":            func(s *ports.Scene) { s.Off = true },
98		"background":     func(s *ports.Scene) { s.Background = "#111111" },
99		"border":         func(s *ports.Scene) { s.Border.Width = 2 },
100		"workspace clip": func(s *ports.Scene) { s.WorkspaceClip = ports.Rect{W: 10, H: 10} },
101		"dim":            func(s *ports.Scene) { s.Dim = 0.5 },
102		"dim behind":     func(s *ports.Scene) { s.DimBehind = true },
103		"drop hints":     func(s *ports.Scene) { s.DropHints = []ports.Rect{{W: 5, H: 5}} },
104		"indicators":     func(s *ports.Scene) { s.CaptureIndicators = []ports.CaptureIndicator{{Rect: ports.Rect{W: 5, H: 5}}} },
105		"capture":        func(s *ports.Scene) { s.Capture = &ports.SceneCapture{} },
106		"capture scene":  func(s *ports.Scene) { s.CaptureScene = &ports.Scene{} },
107		"security":       func(s *ports.Scene) { s.Security.Protected = true },
108		"window count":   func(s *ports.Scene) { s.Windows = s.Windows[:1] },
109		"window order":   func(s *ports.Scene) { s.Windows[0], s.Windows[1] = s.Windows[1], s.Windows[0] },
110		"window id":      func(s *ports.Scene) { s.Windows[1].ID = 7 },
111	} {
112		t.Run(name, func(t *testing.T) {
113			cur := base()
114			mod(&cur)
115			cur.Seq = 2
116			assert.True(t, newDamage(held(base()), cur, bounds).all())
117		})
118	}
119	t.Run("seq 0 and cold target", func(t *testing.T) {
120		cur := base()
121		cur.Seq = 0
122		assert.True(t, newDamage(held(base()), cur, bounds).all())
123		cur.Seq = 2
124		assert.True(t, newDamage(&target{}, cur, bounds).all())
125	})
126}
127
128func TestDamageDeltaSeparators(t *testing.T) {
129	sep := func(x int) ports.Separator { return ports.Separator{Rect: ports.Rect{X: x, W: 2, H: 500}} }
130	a, b := sep(100), sep(200)
131	old := deltaScene(1, win(1, 0, 0, 500, 500))
132	old.Separators = []ports.Separator{a, b}
133	cur := deltaScene(2, win(1, 0, 0, 500, 500))
134	cur.Separators = []ports.Separator{a, sep(250)}
135	d := newDamage(held(old), cur, image.Rect(0, 0, 1000, 500))
136	require.False(t, d.all())
137	assert.Equal(t, image.Rect(200, 0, 252, 500), d.area)
138
139	// Same lines, another order: nothing differs by value.
140	cur.Separators = []ports.Separator{b, a}
141	d = newDamage(held(old), cur, image.Rect(0, 0, 1000, 500))
142	require.False(t, d.all())
143	assert.True(t, d.area.Empty())
144
145	// A line that turns active is a different value.
146	cur.Separators = []ports.Separator{a, {Rect: b.Rect, Active: true}}
147	d = newDamage(held(old), cur, image.Rect(0, 0, 1000, 500))
148	require.False(t, d.all())
149	assert.Equal(t, image.Rect(200, 0, 202, 500), d.area)
150}
151
152func TestSceneDeltaAllocations(t *testing.T) {
153	// Many separators (twenty tiles' worth): the same-length lists are
154	// compared index by index, and a list that lost a line still allocates
155	// nothing.
156	const seps = 80
157	old := deltaScene(1, win(1, 0, 0, 300, 500), win(2, 300, 0, 300, 500), win(3, 600, 0, 400, 500))
158	cur := deltaScene(2, win(1, 0, 0, 300, 500), win(2, 300, 0, 300, 500), win(3, 600, 0, 380, 500))
159	for i := range seps {
160		s := ports.Separator{Rect: ports.Rect{X: i * 10, W: 2, H: 500}}
161		old.Separators = append(old.Separators, s)
162		cur.Separators = append(cur.Separators, s)
163	}
164	cur.Separators[5].Active = true
165	phys := func(r ports.Rect) image.Rectangle { return image.Rect(r.X, r.Y, r.X+r.W, r.Y+r.H) }
166	var area image.Rectangle
167	var ok bool
168	allocs := testing.AllocsPerRun(50, func() { area, ok = sceneDelta(old, cur, phys, 1<<30) })
169	require.True(t, ok)
170	assert.False(t, area.Empty())
171	assert.True(t, image.Rect(50, 0, 52, 500).In(area), "the changed line is damaged: %v", area)
172	assert.Zero(t, allocs, "sceneDelta must not allocate")
173
174	short := cur
175	short.Separators = cur.Separators[:seps-1]
176	allocs = testing.AllocsPerRun(50, func() { area, ok = sceneDelta(old, short, phys, 1<<30) })
177	require.True(t, ok)
178	assert.True(t, image.Rect((seps-1)*10, 0, (seps-1)*10+2, 500).In(area), "the dropped line is damaged: %v", area)
179	assert.Zero(t, allocs, "sceneDelta must not allocate with lists of different lengths")
180}
181
182// Separators are damaged where a line is in one list more often than in
183// the other, whatever the order: equal lists add nothing, a changed pair
184// adds both rects, a rotated list adds nothing, a duplicated line that
185// replaces another adds both, a line added or dropped adds that line.
186func TestSceneDeltaSeparators(t *testing.T) {
187	phys := func(r ports.Rect) image.Rectangle { return image.Rect(r.X, r.Y, r.X+r.W, r.Y+r.H) }
188	line := func(x int) ports.Separator { return ports.Separator{Rect: ports.Rect{X: x, W: 2, H: 10}} }
189	a, b, c := line(0), line(100), line(200)
190	assert.True(t, separatorDelta([]ports.Separator{a, b}, []ports.Separator{a, b}, phys).Empty())
191	assert.Equal(t, image.Rect(100, 0, 202, 10), separatorDelta([]ports.Separator{a, b}, []ports.Separator{a, c}, phys))
192	assert.True(t, separatorDelta([]ports.Separator{a, b, c}, []ports.Separator{b, c, a}, phys).Empty())
193	assert.Equal(t, image.Rect(0, 0, 102, 10), separatorDelta([]ports.Separator{a, b, c}, []ports.Separator{a, a, c}, phys))
194	assert.Equal(t, image.Rect(200, 0, 202, 10), separatorDelta([]ports.Separator{a, b}, []ports.Separator{a, b, c}, phys))
195	assert.Equal(t, image.Rect(0, 0, 2, 10), separatorDelta([]ports.Separator{a, b, c}, []ports.Separator{b, c}, phys))
196}
197
198// The paint order splits at the first window that opens the floats: when
199// that window changes, the whole output is redrawn.
200func TestSceneDeltaFloatsStartMoves(t *testing.T) {
201	phys := func(r ports.Rect) image.Rectangle { return image.Rect(r.X, r.Y, r.X+r.W, r.Y+r.H) }
202	tile := func(id ports.WindowID, x int) ports.SceneWindow { return win(id, x, 0, 100, 100) }
203	float := func(id ports.WindowID, x int) ports.SceneWindow {
204		w := win(id, x, 0, 100, 100)
205		w.Floating = true
206		return w
207	}
208	delta := func(a, b []ports.SceneWindow) bool {
209		_, ok := sceneDelta(deltaScene(1, a...), deltaScene(2, b...), phys, 1<<30)
210		return ok
211	}
212	// A tile becomes a float: the tile lines and the veil move under it.
213	assert.False(t, delta([]ports.SceneWindow{tile(1, 0), tile(2, 100)}, []ports.SceneWindow{tile(1, 0), float(2, 100)}))
214	// The first float moves in the list.
215	assert.False(t, delta([]ports.SceneWindow{float(1, 0), float(2, 100)}, []ports.SceneWindow{tile(1, 0), float(2, 100)}))
216	// A hidden or popup float opens nothing; showing it does.
217	hidden := float(2, 100)
218	hidden.Hidden = true
219	assert.False(t, delta([]ports.SceneWindow{tile(1, 0), hidden}, []ports.SceneWindow{tile(1, 0), float(2, 100)}))
220	popup := float(2, 100)
221	popup.Popup = true
222	assert.False(t, delta([]ports.SceneWindow{tile(1, 0), popup}, []ports.SceneWindow{tile(1, 0), float(2, 100)}))
223	// A below float or an overview preview does not open the floats.
224	below := float(2, 100)
225	below.Below = true
226	assert.True(t, delta([]ports.SceneWindow{tile(1, 0), tile(2, 100)}, []ports.SceneWindow{tile(1, 0), below}))
227	preview := float(2, 100)
228	preview.Preview = 0.5
229	assert.True(t, delta([]ports.SceneWindow{tile(1, 0), tile(2, 100)}, []ports.SceneWindow{tile(1, 0), preview}))
230	// The same first float with another rect stays a delta.
231	assert.True(t, delta([]ports.SceneWindow{tile(1, 0), float(2, 100)}, []ports.SceneWindow{tile(1, 0), float(2, 130)}))
232}
233
234// The target keeps its own copy of the compared slices: a caller reusing
235// its slices in place (DRM overlay frames, capture) must not change what the
236// target holds.
237func TestTargetHoldCopiesSlices(t *testing.T) {
238	ws := []ports.SceneWindow{win(1, 0, 0, 100, 100), win(2, 100, 0, 100, 100)}
239	seps := []ports.Separator{{Rect: ports.Rect{X: 100, W: 2, H: 100}}}
240	layers := []ports.SceneLayer{{ID: 9, Rect: ports.Rect{W: 10, H: 10}}}
241	hints := []ports.Rect{{W: 5, H: 5}}
242	marks := []ports.CaptureIndicator{{Rect: ports.Rect{W: 5, H: 5}}}
243	s := deltaScene(1)
244	s.Windows, s.Separators, s.Layers, s.DropHints, s.CaptureIndicators = ws, seps, layers, hints, marks
245	tg := &target{}
246	d := &damageRegion{drawn: map[ports.WindowID]heldWindow{}}
247	tg.hold(s, d)
248	ws[1].Rect.X, seps[0].Rect.X, layers[0].ID, hints[0].W, marks[0].Rect.W = 150, 150, 8, 6, 6
249	phys := func(r ports.Rect) image.Rectangle { return image.Rect(r.X, r.Y, r.X+r.W, r.Y+r.H) }
250	// Compared with the mutated slices as the new scene, the held copy
251	// differs in each; with a held reference nothing would differ.
252	cur := s
253	cur.Seq = 2
254	_, ok := sceneDelta(tg.scene, cur, phys, 1<<30)
255	assert.False(t, ok, "layers, hints and indicators changed: full redraw")
256	cur.Layers, cur.DropHints, cur.CaptureIndicators = tg.heldLayers, tg.heldHints, tg.heldIndicators
257	area, ok := sceneDelta(tg.scene, cur, phys, 1<<30)
258	require.True(t, ok)
259	assert.Equal(t, image.Rect(100, 0, 250, 100), area, "window 2, old and new")
260	// Zero allocations once the buffers are warm.
261	allocs := testing.AllocsPerRun(50, func() { tg.hold(s, d) })
262	assert.Zero(t, allocs, "hold must not allocate once warm")
263}
264
265// deltaCase is a frame sequence of two windows whose second one resizes.
266type deltaCase struct {
267	name      string
268	scale     float64
269	out       ports.BufferTransform
270	sceneDim  float64
271	floating  bool // window 2 is a float: the veil and tile lines go under it
272	windowDim bool // window 1 is dimmed on some frames
273}
274
275func (c deltaCase) scene(seq uint64, i, w2 int, windows []ports.SceneWindow, seps []ports.Separator) ports.Scene {
276	windows[0], windows[1] = win(1, 0, 0, 20, 48), win(2, 20, 0, w2, 48)
277	windows[1].Floating = c.floating
278	if c.windowDim && i%3 == 0 {
279		windows[0].Dim = 0.5
280	}
281	seps[0] = ports.Separator{Rect: ports.Rect{X: 20, W: 1, H: 48}}
282	return ports.Scene{Seq: seq, Transform: c.out, OutputWidth: 64, OutputHeight: 48, Scale: c.scale, Background: "#101010",
283		Dim: c.sceneDim, Windows: windows, Separators: seps}
284}
285
286func deltaCases() []deltaCase {
287	return []deltaCase{
288		{name: "scale 1", scale: 1},
289		{name: "scale 1.5", scale: 1.5},
290		{name: "transform 90", scale: 1, out: 1},
291		{name: "scene dim over tiles", scale: 1, sceneDim: 0.4, floating: true},
292		{name: "window dim", scale: 1, windowDim: true},
293		{name: "scale 1.5 rotated, all", scale: 1.5, out: 1, sceneDim: 0.3, floating: true, windowDim: true},
294	}
295}
296
297// A partial redraw after a window resize is pixel-identical to a full
298// redraw, on both targets of a double-buffered output. With reuse the scene
299// slices are rewritten in place for every frame, like an output adapter
300// that reuses them: the target must not have kept a reference.
301func TestRendererSceneDeltaMatchesFullRedraw(t *testing.T) {
302	for _, reuse := range []bool{false, true} {
303		for _, tc := range deltaCases() {
304			name := tc.name
305			if reuse {
306				name += " reused slices"
307			}
308			t.Run(name, func(t *testing.T) { sceneDeltaMatchesFullRedraw(t, tc, reuse) })
309		}
310	}
311}
312
313func sceneDeltaMatchesFullRedraw(t *testing.T, tc deltaCase, reuse bool) {
314	tw, th := tc.out.Size(int(64*tc.scale+0.5), int(48*tc.scale+0.5))
315	newR := func() *Renderer {
316		r, err := New(tw, th)
317		if err != nil {
318			t.Skipf("Vulkan unavailable: %v", err)
319		}
320		t.Cleanup(r.Close)
321		return r
322	}
323	damaged, full := newR(), newR()
324	bufs, err := damaged.ExportTargets(2, nil, false)
325	if err != nil {
326		t.Skipf("no exportable targets: %v", err)
327	}
328	for _, b := range bufs {
329		for _, p := range b.Planes {
330			p.File.Close()
331		}
332	}
333	red := solidContent(t, 8, 8, color.RGBA{R: 255, A: 255})
334	red.ID, red.Surface, red.Seq, red.Version = 1, 1, 1, 1
335	green := solidContent(t, 8, 8, color.RGBA{G: 255, A: 255})
336	green.ID, green.Surface, green.Seq, green.Version = 2, 2, 1, 1
337	contents := map[ports.WindowID]ports.SurfaceContent{1: red, 2: green}
338	shared := make([]ports.SceneWindow, 2)
339	sharedSeps := make([]ports.Separator, 1)
340	var partial int
341	for i, w2 := range []int{20, 20, 30, 24, 24, 40, 20, 22} {
342		windows, seps := shared, sharedSeps
343		if !reuse {
344			windows, seps = make([]ports.SceneWindow, 2), make([]ports.Separator, 1)
345		}
346		if reuse {
347			clear(windows)
348		}
349		s := tc.scene(uint64(10+i), i, w2, windows, seps)
350		damaged.UseTarget(i % 2)
351		before := damaged.redrawn
352		require.NoError(t, render(damaged, s, contents))
353		if damaged.redrawn-before < tw*th {
354			partial++
355		}
356		plain := s
357		plain.Seq = 0
358		plain.Windows = slices.Clone(s.Windows)
359		plain.Separators = slices.Clone(s.Separators)
360		require.NoError(t, render(full, plain, contents))
361		a, b := readPixels(t, damaged), readPixels(t, full)
362		if !bytes.Equal(a.Pix, b.Pix) {
363			t.Fatalf("frame %d (w2=%d) differs from a full redraw", i, w2)
364		}
365	}
366	assert.Positive(t, partial, "no frame was redrawn partially")
367}
368
369// A window's Fade changes only how it draws inside its rect: the delta is
370// that rect. A leaving entry (core keeps a closed or hidden window in the
371// scene while it fades) changes the window list when it appears and when
372// it goes: both redraw everything, like any list change; while it fades,
373// only its rect is redrawn.
374func TestDamageDeltaFadeAndLeavingEntry(t *testing.T) {
375	bounds := image.Rect(0, 0, 1000, 500)
376	settled := deltaScene(1, win(1, 0, 0, 500, 500), win(2, 500, 0, 500, 500))
377
378	faded := deltaScene(2, win(1, 0, 0, 500, 500), win(2, 500, 0, 500, 500))
379	faded.Windows[1].Fade = 0.5
380	d := newDamage(held(settled), faded, bounds)
381	require.False(t, d.all())
382	assert.Equal(t, image.Rect(500, 0, 1000, 500), d.area)
383
384	// Window 2 was unmapped: the layout re-flows and a leaving entry is
385	// appended. The list changed: full redraw.
386	leaving := deltaScene(3, win(1, 0, 0, 1000, 500), win(2, 500, 0, 500, 500))
387	leaving.Windows[1].Fade = 0.1
388	assert.True(t, newDamage(held(settled), leaving, bounds).all())
389
390	// The fade advances: its rect only, the neighbours untouched.
391	next := deltaScene(4, win(1, 0, 0, 1000, 500), win(2, 500, 0, 500, 500))
392	next.Windows[1].Fade = 0.6
393	d = newDamage(held(leaving), next, bounds)
394	require.False(t, d.all())
395	assert.Equal(t, image.Rect(500, 0, 1000, 500), d.area)
396
397	// The entry settled and is dropped: full redraw (its pixels are stale).
398	gone := deltaScene(5, win(1, 0, 0, 1000, 500))
399	assert.True(t, newDamage(held(next), gone, bounds).all())
400
401	// A leaving float opens the floats where it is appended: the tile
402	// lines and the veil move under it, so floatsStart differs.
403	float := deltaScene(6, win(1, 0, 0, 1000, 500), win(2, 100, 100, 200, 200))
404	float.Windows[1].Floating, float.Windows[1].Fade = true, 0.1
405	assert.True(t, newDamage(held(gone), float, bounds).all())
406}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 26

Tags 10

↑↓ moveenter openesc close