1package drm
2
3import (
4 "os"
5 "testing"
6 "time"
7
8 "github.com/bnema/neferwl/internal/ports"
9 "github.com/stretchr/testify/mock"
10 "golang.org/x/sys/unix"
11)
12
13const pZpos = 30
14
15// overlayOutput is a test output with an overlay plane above the primary.
16func overlayOutput(t *testing.T, errs ...error) (*Output, *mockkms, *[]commitRec) {
17 o, k, commits := testOutput(t, errs...)
18 props := map[string]uint32{"zpos": pZpos}
19 for n, v := range planeProps {
20 props[n] = v
21 }
22 o.primary.props = props
23 o.stray = []*plane{{id: tOverlay, typ: planeOverlay, props: props, zposValue: 2}}
24 o.primary.formats = nil // the overlay's own formats decide
25 o.pickOverlay()
26 if o.overlay == nil {
27 t.Fatal("no overlay picked")
28 }
29 return o, k, commits
30}
31
32// A 1:1 opaque dmabuf window with nothing above goes on the overlay.
33func overlayScene() (ports.Scene, map[ports.WindowID]ports.SurfaceContent) {
34 s := ports.Scene{Seq: 1, Scale: 1, OutputWidth: 200, OutputHeight: 100, Windows: []ports.SceneWindow{
35 {ID: 1, Rect: ports.Rect{W: 100, H: 100}},
36 {ID: 2, Rect: ports.Rect{X: 100, W: 100, H: 100}},
37 }}
38 return s, map[ports.WindowID]ports.SurfaceContent{
39 2: {ID: 2, Width: 100, Height: 100, Opaque: true, DMABuf: &ports.DMABuf{ID: 9, Width: 100, Height: 100, Format: fourccXRGB}},
40 }
41}
42
43func TestScanoutWorkspaceClipCannotBeBypassed(t *testing.T) {
44 s, contents := fullscreenScene()
45 s.WorkspaceClip = ports.Rect{X: 50, W: 100, H: 100}
46 if _, reason := scanoutCandidate(s, contents, 200, 100, nil); reason != "workspace_clip" {
47 t.Fatalf("fullscreen bypassed clip: %q", reason)
48 }
49 if fullscreenShown(&s) {
50 t.Fatal("clipped fullscreen counted as output-covering")
51 }
52}
53
54func TestOverlayWorkspaceClip(t *testing.T) {
55 s, contents := overlayScene()
56 s.WorkspaceClip = ports.Rect{X: 50, W: 100, H: 100}
57 if _, _, _, reason := overlayCandidate(s, contents, false, nil, nil); reason != "workspace_clip" {
58 t.Fatalf("overflowing workspace overlay: %q", reason)
59 }
60 s.WorkspaceClip = ports.Rect{X: 100, W: 100, H: 100}
61 if win, _, _, reason := overlayCandidate(s, contents, false, nil, nil); reason != "" || win.ID != 2 {
62 t.Fatalf("contained workspace overlay: %v %q", win.ID, reason)
63 }
64}
65
66func TestOverlayCandidate(t *testing.T) {
67 s, c := overlayScene()
68 if w, _, _, reason := overlayCandidate(s, c, false, nil, nil); reason != "" || w.ID != 2 {
69 t.Fatalf("candidate %v %q", w.ID, reason)
70 }
71 // A rotated output composes: planes are not rotated.
72 rs, rc := overlayScene()
73 rs.Transform = 1
74 if _, _, _, reason := overlayCandidate(rs, rc, false, nil, nil); reason != "output_transform" {
75 t.Fatalf("rotated output reason %q", reason)
76 }
77 c[2] = ports.SurfaceContent{ID: 2, Width: 100, Height: 100, Opaque: true, DMABuf: &ports.DMABuf{Format: fourccNV12}}
78 if _, _, _, reason := overlayCandidate(s, c, false, nil, nil); reason != "no_candidate" {
79 t.Fatalf("YUV overlay reason %q", reason)
80 }
81 // A transformed buffer is composed: the plane would show it unrotated.
82 _, c = overlayScene()
83 rotated := c[2]
84 rotated.Transform = 1
85 c[2] = rotated
86 if _, _, _, reason := overlayCandidate(s, c, false, nil, nil); reason != "no_candidate" {
87 t.Fatalf("transformed overlay reason %q", reason)
88 }
89 _, c = overlayScene()
90 s.Windows = append(s.Windows, ports.SceneWindow{ID: 3, Rect: ports.Rect{W: 10, H: 10}})
91 if _, _, _, reason := overlayCandidate(s, c, false, nil, nil); reason != "window_above" {
92 t.Fatalf("reason %q", reason)
93 }
94 s, _ = overlayScene()
95 s.Scale = 1.5
96 if _, _, _, reason := overlayCandidate(s, c, false, nil, nil); reason != "scaled" {
97 t.Fatalf("reason %q", reason)
98 }
99 s, _ = overlayScene()
100 s.Layers = []ports.SceneLayer{{ID: 5, Layer: ports.LayerOverlay, Rect: ports.Rect{W: 5, H: 5}}}
101 if _, _, _, reason := overlayCandidate(s, c, false, nil, nil); reason != "layer_above" {
102 t.Fatalf("reason %q", reason)
103 }
104 // The dim veil is composed under the float: nothing leaves for the plane.
105 s, _ = overlayScene()
106 s.Dim = 0.3
107 if _, _, _, reason := overlayCandidate(s, c, false, nil, nil); reason != "dim" {
108 t.Fatalf("dim reason %q", reason)
109 }
110 // A dimmed window (a peeking stashed one) needs its veil composed.
111 s, _ = overlayScene()
112 s.Windows[len(s.Windows)-1].Dim = 0.5
113 if _, _, _, reason := overlayCandidate(s, c, false, nil, nil); reason != "no_candidate" {
114 t.Fatalf("dimmed window reason %q", reason)
115 }
116 // A fading window needs its translucency composed.
117 s, _ = overlayScene()
118 s.Windows[len(s.Windows)-1].Fade = 0.5
119 if _, _, _, reason := overlayCandidate(s, c, false, nil, nil); reason != "no_candidate" {
120 t.Fatalf("fading window reason %q", reason)
121 }
122 // A zoomed (animating) window is drawn smaller than its buffer.
123 s, _ = overlayScene()
124 s.Windows[len(s.Windows)-1].Zoom = 0.9
125 if _, _, _, reason := overlayCandidate(s, c, false, nil, nil); reason != "no_candidate" {
126 t.Fatalf("zoomed window reason %q", reason)
127 }
128 // A window with the focus effect needs it composed.
129 s, _ = overlayScene()
130 s.Windows[len(s.Windows)-1].FocusEffect = 0.05
131 if _, _, _, reason := overlayCandidate(s, c, false, nil, nil); reason != "no_candidate" {
132 t.Fatalf("pulsing window reason %q", reason)
133 }
134 // An overview preview is drawn smaller than its buffer: composed.
135 s, _ = overlayScene()
136 s.Windows[len(s.Windows)-1].Preview = 0.5
137 if _, _, _, reason := overlayCandidate(s, c, false, nil, nil); reason != "no_candidate" {
138 t.Fatalf("preview reason %q", reason)
139 }
140 // A viewport crop is composed: the plane would show the whole buffer.
141 s, c = overlayScene()
142 cropped := c[2]
143 cropped.Source = [4]float32{10, 10, 50, 50}
144 c[2] = cropped
145 if _, _, _, reason := overlayCandidate(s, c, false, nil, nil); reason != "no_candidate" {
146 t.Fatalf("cropped overlay reason %q", reason)
147 }
148 // A full-buffer source is no crop.
149 cropped.Source = [4]float32{0, 0, 100, 100}
150 c[2] = cropped
151 if _, _, _, reason := overlayCandidate(s, c, false, nil, nil); reason != "" {
152 t.Fatalf("full source reason %q", reason)
153 }
154}
155
156// The overlay is used only on SDR outputs: PQ content there needs the
157// renderer's tone mapping.
158func TestOverlaySkipsPQContent(t *testing.T) {
159 o, _, _ := overlayOutput(t)
160 o.overlay.formats = []ports.DMABufFormat{{Format: fourccXRGB}}
161 s, c := overlayScene()
162 pq := c[2]
163 pq.Color = ports.SurfaceColor{TF: ports.ColorTFPQ, Primaries: ports.ColorPrimariesBT2020}
164 c[2] = pq
165 if ov, rest := o.overlayFrame(s, c); ov.fb != 0 || len(rest.Windows) != 2 {
166 t.Fatalf("PQ overlay %+v rest %+v", ov, rest.Windows)
167 }
168}
169
170// The overlay is chosen and the composed scene leaves its window out;
171// the frame commit carries the plane at the window's place.
172func TestOverlayChosen(t *testing.T) {
173 o, k, commits := overlayOutput(t)
174 k.EXPECT().addFB(mock.Anything, uint32(fourccXRGB)).Return(88, nil).Once()
175 o.overlay.formats = []ports.DMABufFormat{{Format: fourccXRGB}}
176 s, c := overlayScene()
177 ov, rest := o.overlayFrame(s, c)
178 if ov.fb != 88 || len(rest.Windows) != 1 || rest.Windows[0].ID != 1 {
179 t.Fatalf("overlay %+v rest %+v", ov, rest.Windows)
180 }
181 if !o.testOverlay(70, ov) {
182 t.Fatal("test refused")
183 }
184 if err := o.commitWith(70, nil, false, false, pendingFrame{queued: ov.buf}, ov); err != nil {
185 t.Fatal(err)
186 }
187 last := (*commits)[len(*commits)-1]
188 if v, _ := last.req.value(tOverlay, pFB); v != 88 {
189 t.Fatalf("overlay fb %d", v)
190 }
191 if v, _ := last.req.value(tOverlay, 14); v != 100 {
192 t.Fatalf("overlay x %d", v)
193 }
194 if o.overlayOn != 9 {
195 t.Fatalf("overlayOn %d", o.overlayOn)
196 }
197}
198
199// A TEST_ONLY refusal composes the window and is cached on the buffer.
200func TestOverlayTestRefusedFallsBack(t *testing.T) {
201 o, k, _ := overlayOutput(t, unix.EINVAL)
202 k.EXPECT().addFB(mock.Anything, uint32(fourccXRGB)).Return(88, nil).Once()
203 o.overlay.formats = []ports.DMABufFormat{{Format: fourccXRGB}}
204 s, c := overlayScene()
205 ov, _ := o.overlayFrame(s, c)
206 if o.testOverlay(70, ov) {
207 t.Fatal("refusal taken")
208 }
209 if o.overlayReason != "overlay_refused" {
210 t.Fatalf("refusal reason %q", o.overlayReason)
211 }
212 if ov2, rest := o.overlayFrame(s, c); ov2.fb != 0 || len(rest.Windows) != 2 {
213 t.Fatal("refused buffer tried again")
214 }
215 // The overlay's refusal does not bar direct scanout of the buffer.
216 o.primary.formats = []ports.DMABufFormat{{Format: fourccXRGB}}
217 if fb, reason := o.scanoutFB(c[2].DMABuf, time.Now()); fb != 88 || reason != "" {
218 t.Fatalf("scanout after overlay refusal: %d %q", fb, reason)
219 }
220}
221
222// Popups are drawn above every window: one listed first still blocks the
223// overlay; a bordered window does too.
224func TestOverlayPopupAndBorder(t *testing.T) {
225 s, c := overlayScene()
226 s.Windows = append([]ports.SceneWindow{{ID: 7, Popup: true, Rect: ports.Rect{W: 5, H: 5}}}, s.Windows...)
227 if _, _, _, reason := overlayCandidate(s, c, false, nil, nil); reason != "window_above" {
228 t.Fatalf("popup: %q", reason)
229 }
230 s, _ = overlayScene()
231 s.Separators = []ports.Separator{{Rect: ports.Rect{W: 2, H: 2}}}
232 if _, _, _, reason := overlayCandidate(s, c, false, nil, nil); reason != "border" {
233 t.Fatalf("border: %q", reason)
234 }
235}
236
237// A cursor commit refused with the overlay on drops the overlay: the
238// next frame is composed.
239func TestOverlayCursorConflictDropsOverlay(t *testing.T) {
240 o, k, _ := overlayOutput(t, nil, unix.EINVAL)
241 k.EXPECT().addFB(mock.Anything, uint32(fourccXRGB)).Return(88, nil).Once()
242 o.overlay.formats = []ports.DMABufFormat{{Format: fourccXRGB}}
243 s, c := overlayScene()
244 ov, _ := o.overlayFrame(s, c)
245 if err := o.commitWith(70, nil, false, false, pendingFrame{}, ov); err != nil {
246 t.Fatal(err)
247 }
248 o.frame.endPending()
249 o.cursor.image = true
250 o.cursor.Move(150, 50)
251 if err := o.commitState(false); err != errOverlayDropped {
252 t.Fatalf("err %v", err)
253 }
254 if ov2, _ := o.overlayFrame(s, c); ov2.fb != 0 || o.overlayReason != "cursor_conflict" {
255 t.Fatalf("overlay kept: %+v %q", ov2, o.overlayReason)
256 }
257}
258
259// An explicit-sync window on the overlay: the plane carries a duplicate of
260// the client's acquire fence, closed once the frame is committed.
261func TestOverlayCarriesAcquireFence(t *testing.T) {
262 o, k, commits := overlayOutput(t)
263 k.EXPECT().addFB(mock.Anything, uint32(fourccXRGB)).Return(88, nil).Once()
264 o.overlay.formats = []ports.DMABufFormat{{Format: fourccXRGB}}
265 s, c := overlayScene()
266 r, w, err := os.Pipe()
267 if err != nil {
268 t.Fatal(err)
269 }
270 defer r.Close()
271 defer w.Close()
272 win := c[2]
273 win.Acquire = r
274 c[2] = win
275 ov, _ := o.overlayFrame(s, c)
276 if ov.acquire == nil || ov.acquire.Fd() == r.Fd() {
277 t.Fatal("overlay has no duplicate of the acquire fence")
278 }
279 fd := ov.acquire.Fd()
280 if err := o.commitWith(70, nil, false, false, pendingFrame{queued: ov.buf}, ov); err != nil {
281 t.Fatal(err)
282 }
283 ov.close()
284 last := (*commits)[len(*commits)-1]
285 if v, ok := last.req.value(tOverlay, pFence); !ok || v != uint64(fd) {
286 t.Fatalf("overlay IN_FENCE_FD %d %v, want %d", v, ok, fd)
287 }
288 if _, err := ov.acquire.Stat(); err == nil {
289 t.Fatal("duplicate left open")
290 }
291}