1package vulkan
2
3import (
4 "image"
5 "testing"
6
7 "github.com/bnema/neferwl/internal/ports"
8 "github.com/stretchr/testify/require"
9)
10
11func indicatorScene(m ...ports.CaptureIndicator) ports.Scene {
12 return ports.Scene{Seq: 1, Scale: 1, Background: "#101010", CaptureIndicators: m}
13}
14
15// indicatorDraws walks s and returns the physical rects drawn, all in the
16// indicator red.
17func indicatorDraws(t *testing.T, r *Renderer, s ports.Scene) []image.Rectangle {
18 t.Helper()
19 var out []image.Rectangle
20 for _, d := range r.draws(s, nil, newDamage(&target{}, s, image.Rect(0, 0, r.width, r.height))) {
21 require.Equal(t, [4]float32{0xff / 255.0, 0x3b / 255.0, 0x30 / 255.0, 1}, d.pc.color)
22 out = append(out, image.Rect(int(d.pc.rect[0]), int(d.pc.rect[1]), int(d.pc.rect[2]), int(d.pc.rect[3])))
23 }
24 return out
25}
26
27func TestCaptureIndicatorBorderGeometryLogicalScaled(t *testing.T) {
28 target := ports.CaptureIndicator{Rect: ports.Rect{X: 8, Y: 4, W: 16, H: 8}}
29 for _, tc := range []struct {
30 scale float64
31 want []image.Rectangle
32 }{
33 {1, []image.Rectangle{image.Rect(8, 4, 24, 6), image.Rect(8, 10, 24, 12), image.Rect(8, 6, 10, 10), image.Rect(22, 6, 24, 10)}},
34 {2, []image.Rectangle{image.Rect(16, 8, 48, 12), image.Rect(16, 20, 48, 24), image.Rect(16, 12, 20, 20), image.Rect(44, 12, 48, 20)}},
35 } {
36 r := &Renderer{width: 64, height: 64}
37 s := indicatorScene(target)
38 s.Scale = tc.scale
39 require.Equal(t, tc.want, indicatorDraws(t, r, s), "scale %v", tc.scale)
40 }
41 // On the output edge the border is still inside the output.
42 r := &Renderer{width: 64, height: 64}
43 got := indicatorDraws(t, r, indicatorScene(ports.CaptureIndicator{Rect: ports.Rect{W: 64, H: 64}}))
44 require.Equal(t, []image.Rectangle{image.Rect(0, 0, 64, 2), image.Rect(0, 62, 64, 64), image.Rect(0, 2, 2, 62), image.Rect(62, 2, 64, 62)}, got)
45 // A target thinner than the border is clamped: no overdraw.
46 got = indicatorDraws(t, r, indicatorScene(ports.CaptureIndicator{Rect: ports.Rect{X: 4, Y: 4, W: 10, H: 2}}))
47 require.Equal(t, []image.Rectangle{image.Rect(4, 4, 14, 5), image.Rect(4, 5, 14, 6)}, got)
48 // Union of several targets, in order.
49 got = indicatorDraws(t, r, indicatorScene(ports.CaptureIndicator{Rect: ports.Rect{W: 10, H: 10}}, ports.CaptureIndicator{Rect: ports.Rect{X: 20, W: 10, H: 10}}))
50 require.Len(t, got, 8)
51}
52
53// A listed mark always draws something: a target 1 logical pixel wide or
54// tall (an output that small, core inflates the others) is filled whole.
55func TestCaptureIndicatorThinMarkStillDraws(t *testing.T) {
56 r := &Renderer{width: 64, height: 64}
57 got := indicatorDraws(t, r, indicatorScene(ports.CaptureIndicator{Rect: ports.Rect{X: 10, Y: 4, W: 1, H: 20}}))
58 require.Equal(t, []image.Rectangle{image.Rect(10, 4, 11, 24)}, got, "W=1")
59 got = indicatorDraws(t, r, indicatorScene(ports.CaptureIndicator{Rect: ports.Rect{X: 4, Y: 10, W: 20, H: 1}}))
60 require.Equal(t, []image.Rectangle{image.Rect(4, 10, 24, 11)}, got, "H=1")
61 s := indicatorScene(ports.CaptureIndicator{Rect: ports.Rect{X: 5, Y: 5, W: 1, H: 1}})
62 s.Scale = 2
63 require.Equal(t, []image.Rectangle{image.Rect(10, 10, 12, 12)}, indicatorDraws(t, &Renderer{width: 128, height: 128}, s))
64}
65
66func TestCaptureIndicatorPill(t *testing.T) {
67 r := &Renderer{width: 64, height: 64}
68 pill := ports.CaptureIndicator{Pill: true, Rect: ports.Rect{X: 44, Y: 8, W: 12, H: 12}}
69 got := indicatorDraws(t, r, indicatorScene(pill))
70 // Three bands: the corners are cut, the middle is the full width.
71 require.Equal(t, []image.Rectangle{image.Rect(46, 8, 54, 11), image.Rect(44, 11, 56, 17), image.Rect(46, 17, 54, 20)}, got)
72 s := indicatorScene(pill)
73 s.Scale = 2
74 got = indicatorDraws(t, &Renderer{width: 128, height: 128}, s)
75 require.Equal(t, []image.Rectangle{image.Rect(91, 16, 109, 22), image.Rect(88, 22, 112, 34), image.Rect(91, 34, 109, 40)}, got)
76}
77
78// A scene without indicators draws nothing extra, and walking a scene that
79// has them allocates nothing once warm.
80func TestCaptureIndicatorIdleAndAllocs(t *testing.T) {
81 r := &Renderer{width: 64, height: 64}
82 s := indicatorScene()
83 require.Empty(t, r.draws(s, nil, newDamage(&target{}, s, image.Rect(0, 0, 64, 64))))
84 walk := func(s ports.Scene) float64 {
85 dmg := newDamage(&target{}, s, image.Rect(0, 0, 64, 64))
86 r.draws(s, nil, dmg)
87 return testing.AllocsPerRun(50, func() { r.draws(s, nil, dmg) })
88 }
89 idle := walk(s)
90 with := walk(indicatorScene(ports.CaptureIndicator{Rect: ports.Rect{W: 64, H: 64}}, ports.CaptureIndicator{Pill: true, Rect: ports.Rect{X: 44, Y: 8, W: 12, H: 12}}))
91 t.Logf("r.draws allocations: idle %.0f, with indicators %.0f", idle, with)
92 require.LessOrEqual(t, with, idle, "indicator draws allocate")
93}