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)
11
12func TestScanoutCandidate(t *testing.T) {
13 full := ports.SceneWindow{ID: 1, Rect: ports.Rect{W: 100, H: 50}, Fullscreen: true}
14 scene := ports.Scene{OutputWidth: 100, OutputHeight: 50, Windows: []ports.SceneWindow{full}}
15 buf := &ports.DMABuf{ID: 9, Width: 200, Height: 100, Format: fourccARGB, Planes: []ports.DMABufPlane{{}}}
16 game := ports.SurfaceContent{ID: 1, Width: 200, Height: 100, LogicalW: 100, LogicalH: 50, DMABuf: buf}
17 for _, tc := range []struct {
18 name string
19 scene func(s *ports.Scene)
20 cont func(c *ports.SurfaceContent)
21 reason string
22 }{
23 {"scanout", nil, nil, ""},
24 {"hidden others ignored", func(s *ports.Scene) {
25 s.Windows = append(s.Windows, ports.SceneWindow{ID: 2, Hidden: true})
26 }, nil, ""},
27 {"no window", func(s *ports.Scene) { s.Windows = nil }, nil, "no_fullscreen"},
28 {"rotated output", func(s *ports.Scene) { s.Transform = 1 }, nil, "output_transform"},
29 {"dimmed", func(s *ports.Scene) { s.Windows[0].Dim = 0.5 }, nil, "dim"},
30 {"faded", nil, func(c *ports.SurfaceContent) { c.Fade = 0.5 }, "fade"},
31 {"tiled", func(s *ports.Scene) { s.Windows[0].Fullscreen = false }, nil, "other_windows"},
32 {"float above", func(s *ports.Scene) {
33 s.Windows = append(s.Windows, ports.SceneWindow{ID: 2, Rect: ports.Rect{W: 10, H: 10}})
34 }, nil, "other_windows"},
35 {"not covering", func(s *ports.Scene) { s.Windows[0].Rect.W = 90 }, nil, "other_windows"},
36 {"overlay layer", func(s *ports.Scene) {
37 s.Layers = []ports.SceneLayer{{ID: 3, Layer: ports.LayerOverlay, Rect: ports.Rect{W: 10, H: 10}}}
38 }, nil, "overlay_surface"},
39 {"top layer", func(s *ports.Scene) {
40 s.Layers = []ports.SceneLayer{{ID: 3, Layer: ports.LayerTop, Rect: ports.Rect{W: 10, H: 10}}}
41 }, nil, "overlay_surface"},
42 {"background layer", func(s *ports.Scene) {
43 s.Layers = []ports.SceneLayer{{ID: 3, Layer: ports.LayerBackground, Rect: ports.Rect{W: 10, H: 10}}}
44 }, nil, ""},
45 {"shm", nil, func(c *ports.SurfaceContent) { c.DMABuf, c.SHM = nil, &ports.SHMBuffer{} }, "not_dmabuf"},
46 {"subsurface", nil, func(c *ports.SurfaceContent) { c.Children = []ports.Subsurface{{}} }, "subsurfaces"},
47 {"buffer transform", nil, func(c *ports.SurfaceContent) { c.Transform = 2 }, "buffer_transform"},
48 {"scaled", nil, func(c *ports.SurfaceContent) { c.Width = 100 }, ""},
49 {"cropped", nil, func(c *ports.SurfaceContent) { c.Geometry = ports.Rect{X: 5, W: 90, H: 50} }, "geometry_crop"},
50 {"client ignores scale", nil, func(c *ports.SurfaceContent) { c.LogicalW = 200 }, "letterbox"},
51 {"whole geometry", nil, func(c *ports.SurfaceContent) { c.Geometry = ports.Rect{W: 100, H: 50} }, ""},
52 {"wine client subsurface", nil, wineTree(nil), ""},
53 {"client subsurface translucent", nil, wineTree(func(ch *ports.Subsurface) { ch.Opaque = false }), "subsurface_translucent"},
54 {"client subsurface shm", nil, wineTree(func(ch *ports.Subsurface) { ch.DMABuf, ch.SHM = nil, &ports.SHMBuffer{} }), "not_dmabuf"},
55 {"client subsurface offset", nil, wineTree(func(ch *ports.Subsurface) { ch.X = 4 }), "subsurface_offset"},
56 {"client subsurface below root", nil, wineTree(func(ch *ports.Subsurface) { ch.Below = true }), "subsurface_below"},
57 {"client subsurface cropped", nil, wineTree(func(ch *ports.Subsurface) { ch.Source = [4]float32{0, 0, 100, 50} }), ""},
58 {"client subsurface full source", nil, wineTree(func(ch *ports.Subsurface) { ch.Source = [4]float32{0, 0, 200, 100} }), ""},
59 {"root cropped", nil, func(c *ports.SurfaceContent) { c.Source = [4]float32{10, 0, 190, 100} }, ""},
60 {"client subsurface scaled", nil, wineTree(func(ch *ports.Subsurface) { ch.Width = 100 }), ""},
61 {"client subsurface small", nil, wineTree(func(ch *ports.Subsurface) { ch.LogicalW = 90 }), "letterbox"},
62 {"two subsurfaces", nil, func(c *ports.SurfaceContent) {
63 wineTree(nil)(c)
64 c.Children = append(c.Children, c.Children[0])
65 }, "subsurfaces"},
66 } {
67 s := scene
68 s.Windows = append([]ports.SceneWindow(nil), scene.Windows...)
69 c := game
70 if tc.scene != nil {
71 tc.scene(&s)
72 }
73 if tc.cont != nil {
74 tc.cont(&c)
75 }
76 if _, reason := scanoutCandidate(s, map[ports.WindowID]ports.SurfaceContent{1: c}, 200, 100); reason != tc.reason {
77 t.Errorf("%s: reason %q, want %q", tc.name, reason, tc.reason)
78 }
79 }
80}
81
82// wineTree turns the test content into Wine's layout: the root is a wl_shm
83// frame and the game is one opaque GPU subsurface over the client area.
84func wineTree(edit func(ch *ports.Subsurface)) func(c *ports.SurfaceContent) {
85 return func(c *ports.SurfaceContent) {
86 ch := ports.Subsurface{SurfaceContent: *c}
87 ch.Opaque = true
88 if edit != nil {
89 edit(&ch)
90 }
91 c.DMABuf, c.SHM = nil, &ports.SHMBuffer{}
92 c.Children = []ports.Subsurface{ch}
93 }
94}
95
96// Wine's game subsurface is scanned out in place of the window: the
97// content keeps the window's ID and Seq and takes the subsurface buffer.
98func TestScanoutCandidateClientSubsurface(t *testing.T) {
99 scene := ports.Scene{OutputWidth: 100, OutputHeight: 50, Windows: []ports.SceneWindow{{ID: 1, Rect: ports.Rect{W: 100, H: 50}, Fullscreen: true}}}
100 game := &ports.DMABuf{ID: 9, Width: 200, Height: 100, Format: fourccXRGB, Planes: []ports.DMABufPlane{{}}}
101 acquire := &os.File{}
102 root := ports.SurfaceContent{ID: 1, Seq: 7, Width: 200, Height: 100, LogicalW: 100, LogicalH: 50, SHM: &ports.SHMBuffer{},
103 Geometry: ports.Rect{W: 100, H: 50},
104 Children: []ports.Subsurface{{SurfaceContent: ports.SurfaceContent{Seq: 3, Width: 200, Height: 100, LogicalW: 100, LogicalH: 50, Opaque: true, DMABuf: game, Async: true, Acquire: acquire}}}}
105 c, reason := scanoutCandidate(scene, map[ports.WindowID]ports.SurfaceContent{1: root}, 200, 100)
106 if reason != "" || c.DMABuf != game || c.ID != 1 || c.Seq != 7 || !c.Async || c.Acquire != acquire || c.Geometry != (ports.Rect{}) {
107 t.Fatalf("candidate %+v, reason %q", c, reason)
108 }
109}
110
111// VRR follows a visible fullscreen window covering the output, even
112// when other windows or layers force composition.
113func TestFullscreenShown(t *testing.T) {
114 full := ports.SceneWindow{ID: 1, Rect: ports.Rect{W: 100, H: 50}, Fullscreen: true}
115 for _, tc := range []struct {
116 name string
117 wins []ports.SceneWindow
118 want bool
119 }{
120 {"fullscreen", []ports.SceneWindow{full}, true},
121 {"with dialog above", []ports.SceneWindow{full, {ID: 2, Rect: ports.Rect{W: 10, H: 10}, Floating: true}}, true},
122 {"hidden", []ports.SceneWindow{{ID: 1, Rect: full.Rect, Fullscreen: true, Hidden: true}}, false},
123 {"off screen", []ports.SceneWindow{{ID: 1, Rect: ports.Rect{X: 100, W: 100, H: 50}, Fullscreen: true}}, false},
124 {"tiled covering", []ports.SceneWindow{{ID: 1, Rect: full.Rect}}, false},
125 {"none", nil, false},
126 } {
127 s := ports.Scene{OutputWidth: 100, OutputHeight: 50, Windows: tc.wins}
128 if got := fullscreenShown(&s); got != tc.want {
129 t.Errorf("%s: %v, want %v", tc.name, got, tc.want)
130 }
131 }
132}
133
134// fullscreenShown runs on every composed frame of a VRR output.
135func TestFullscreenShownAllocations(t *testing.T) {
136 s := tiledFullscreenScene()
137 if allocs := testing.AllocsPerRun(100, func() { fullscreenShown(&s) }); allocs != 0 {
138 t.Errorf("fullscreenShown: %.1f allocs, want 0", allocs)
139 }
140}
141
142func BenchmarkFullscreenShown(b *testing.B) {
143 s := tiledFullscreenScene()
144 b.ReportAllocs()
145 for b.Loop() {
146 fullscreenShown(&s)
147 }
148}
149
150// tiledFullscreenScene is 64 tiled windows with the fullscreen one last
151// (worst case for the walk).
152func tiledFullscreenScene() ports.Scene {
153 s := ports.Scene{OutputWidth: 3840, OutputHeight: 2160}
154 for i := range 64 {
155 s.Windows = append(s.Windows, ports.SceneWindow{ID: ports.WindowID(i + 1), Rect: ports.Rect{X: i * 60, W: 60, H: 2160}})
156 }
157 s.Windows = append(s.Windows, ports.SceneWindow{ID: 99, Rect: ports.Rect{W: 3840, H: 2160}, Fullscreen: true})
158 return s
159}
160
161// A buffer scans out when the primary plane lists its format (or the
162// opaque variant) with its modifier; the tranche offers only formats the
163// renderer also composes.
164func TestScanoutFormatFollowsInFormats(t *testing.T) {
165 const tiled = 0x0200000000000001
166 o, k, _ := testOutput(t)
167 o.primary.formats = []ports.DMABufFormat{{Format: fourccXRGB, Modifier: tiled}, {Format: fourccXRGB, Modifier: 0}, {Format: fourccXR30, Modifier: tiled}}
168 nv12 := uint32('N' | 'V'<<8 | '1'<<16 | '2'<<24)
169 for _, tc := range []struct {
170 name string
171 format uint32
172 mod uint64
173 as uint32
174 ok bool
175 }{
176 {"argb as xrgb", fourccARGB, tiled, fourccXRGB, true},
177 {"xrgb linear", fourccXRGB, 0, fourccXRGB, true},
178 {"10 bit alpha as opaque", fourccAR30, tiled, fourccXR30, true},
179 {"modifier not listed", fourccXRGB, 0x0200000000000002, 0, false},
180 {"format not listed", nv12, tiled, 0, false},
181 } {
182 got, ok := o.scanoutFormat(&ports.DMABuf{Format: tc.format, Modifier: tc.mod})
183 if ok != tc.ok || ok && got != tc.as {
184 t.Errorf("%s: %#x %v", tc.name, got, ok)
185 }
186 }
187 sampled := []ports.DMABufFormat{{Format: fourccARGB, Modifier: tiled}, {Format: fourccARGB, Modifier: 0x0200000000000002}, {Format: nv12, Modifier: tiled}}
188 if got := o.scanoutFormats(sampled); len(got) != 1 || got[0] != sampled[0] {
189 t.Fatalf("tranche %v", got)
190 }
191 // A buffer not in IN_FORMATS is refused before any import.
192 o.clientFBs = map[uint64]*clientFB{}
193 if fb, reason := o.scanoutFB(&ports.DMABuf{ID: 4, Format: nv12, Modifier: tiled}, time.Now()); fb != 0 || reason != "format" {
194 t.Fatalf("fb %d reason %q", fb, reason)
195 }
196 // A listed one is imported with every plane (addFB builds ADDFB2).
197 k.EXPECT().addFB(mock.Anything, uint32(fourccXRGB)).Return(88, nil).Once()
198 if fb, reason := o.scanoutFB(&ports.DMABuf{ID: 5, Format: fourccARGB, Modifier: tiled, Planes: []ports.DMABufPlane{{}, {}}}, time.Now()); fb != 88 || reason != "" {
199 t.Fatalf("fb %d reason %q", fb, reason)
200 }
201}
202
203// Each modeset reports the formats clients may allocate for scanout;
204// with direct scanout off the list is empty.
205func TestModesetSendsScanoutFormats(t *testing.T) {
206 const tiled = 0x0200000000000001
207 for _, on := range []bool{true, false} {
208 o, k, _ := testOutput(t)
209 o.cursor = nil
210 o.scanout, o.device = on, 9
211 o.primary.formats = []ports.DMABufFormat{{Format: fourccXRGB, Modifier: tiled}, {Format: fourccNV12, Modifier: tiled}, {Format: fourccP010, Modifier: tiled}}
212 o.sampled = []ports.DMABufFormat{{Format: fourccARGB, Modifier: tiled}, {Format: fourccARGB, Modifier: 0}, {Format: fourccNV12, Modifier: tiled}, {Format: fourccP010, Modifier: tiled}}
213 ch := make(chan ports.OutputFormats, 1)
214 o.formats = ch
215 k.EXPECT().createBlob(mock.Anything).Return(99, nil).Once()
216 if err := o.modeset(); err != nil {
217 t.Fatal(err)
218 }
219 f := <-ch
220 if f.Output != "DP-1" || f.Device != 9 {
221 t.Fatalf("report %+v", f)
222 }
223 if on != (len(f.Formats) == 1 && f.Formats[0] == o.sampled[0]) || !on && len(f.Formats) != 0 {
224 t.Fatalf("scanout=%v formats %v", on, f.Formats)
225 }
226 }
227}
228
229func TestHDRScanoutDecision(t *testing.T) {
230 o, k, _ := testOutput(t)
231 o.hdr.on = true
232 o.primary.formats = []ports.DMABufFormat{{Format: fourccXR30}}
233 scene := ports.Scene{OutputWidth: 200, OutputHeight: 100, Windows: []ports.SceneWindow{{ID: 1, Fullscreen: true, Rect: ports.Rect{W: 200, H: 100}}}}
234 c := ports.SurfaceContent{ID: 1, Width: 200, Height: 100, LogicalW: 200, LogicalH: 100, DMABuf: &ports.DMABuf{ID: 11, Format: fourccXR30}}
235 check := func(want string) {
236 t.Helper()
237 fb, _ := o.scanoutFrame(scene, map[ports.WindowID]ports.SurfaceContent{1: c})
238 if o.reason != want || (fb != 0) != (want == "") {
239 t.Fatalf("fb %d reason %q, want %q", fb, o.reason, want)
240 }
241 }
242 check("hdr_sdr_content")
243 c.Color = ports.SurfaceColor{TF: ports.ColorTFPQ, Primaries: ports.ColorPrimariesBT2020}
244 c.DMABuf.Format = fourccXRGB
245 check("hdr_format")
246 c.DMABuf.Format = fourccP010
247 check("yuv")
248 c.DMABuf.Format = fourccXR30
249 k.EXPECT().addFB(mock.Anything, uint32(fourccXR30)).Return(uint32(77), nil).Once()
250 check("")
251 o.hdr.on = false
252 check("sdr_pq_content")
253}
254
255func TestModesetReportsConfirmedHDR(t *testing.T) {
256 o, k, _ := testOutput(t)
257 o.cursor = nil
258 o.hdr.on = true
259 o.hdr.cap = hdrCapability{MaxLuminance: 1000, MaxFrameAverage: 400, MinLuminance: 0.005}
260 o.primary.formats = []ports.DMABufFormat{{Format: fourccXR30}, {Format: fourccXRGB}}
261 o.sampled = []ports.DMABufFormat{{Format: fourccXR30}, {Format: fourccXRGB}}
262 ch := make(chan ports.OutputFormats, 2)
263 o.formats = ch
264 k.EXPECT().createBlob(mock.Anything).Return(99, nil).Twice()
265 k.EXPECT().destroyBlob(uint32(99)).Return(nil).Once()
266 if err := o.modeset(); err != nil {
267 t.Fatal(err)
268 }
269 if f := <-ch; f.HDR == nil || f.HDR.MaxLuminance != 1000 || f.HDR.MaxFrameAverage != 400 || f.HDR.MinLuminance != 0.005 || len(f.Formats) != 1 || f.Formats[0].Format != fourccXR30 {
270 t.Fatalf("HDR report: %+v", f)
271 }
272 o.hdr.on = false // fallback to SDR
273 if err := o.modeset(); err != nil {
274 t.Fatal(err)
275 }
276 if f := <-ch; f.HDR != nil || len(f.Formats) != 1 || f.Formats[0].Format != fourccXRGB {
277 t.Fatalf("SDR fallback report: %+v", f)
278 }
279}
280
281// Power off withdraws HDR and scanout formats: clients must not target an
282// inactive output.
283func TestPowerOffWithdrawsHDRAndFormats(t *testing.T) {
284 o, _, _ := testOutput(t)
285 o.cursor = nil
286 o.hdr.on, o.scanout = true, true
287 o.hdr.cap = hdrCapability{MaxLuminance: 1000}
288 o.primary.formats = []ports.DMABufFormat{{Format: fourccXR30}}
289 o.sampled = []ports.DMABufFormat{{Format: fourccXR30}}
290 ch := make(chan ports.OutputFormats, 1)
291 o.formats = ch
292 if err := o.powerOff(); err != nil {
293 t.Fatal(err)
294 }
295 select {
296 case f := <-ch:
297 if f.HDR != nil || len(f.Formats) != 0 {
298 t.Fatalf("power-off report: %+v", f)
299 }
300 default:
301 t.Fatal("no report after power off")
302 }
303}
304
305// A plane without IN_FORMATS (driver without modifiers) scans out its
306// GETPLANE formats, linear.
307func TestPlaneWithoutInFormatsIsLinear(t *testing.T) {
308 k := newMockkms(t)
309 k.EXPECT().planes().Return([]planeRes{{id: 1, possible: 1, formats: []uint32{fourccXRGB}}}, nil)
310 k.EXPECT().objProps(uint32(1), uint32(objPlane)).Return(map[string][2]uint64{"type": {20, planePrimary}}, nil)
311 ps, err := readPlanes(k, 0)
312 if err != nil || len(ps) != 1 {
313 t.Fatalf("%v %v", ps, err)
314 }
315 o := &Output{primary: ps[0]}
316 if f, ok := o.scanoutFormat(&ports.DMABuf{Format: fourccARGB}); !ok || f != fourccXRGB {
317 t.Fatalf("linear argb: %#x %v", f, ok)
318 }
319 if _, ok := o.scanoutFormat(&ports.DMABuf{Format: fourccARGB, Modifier: 0x0200000000000001}); ok {
320 t.Fatal("tiled accepted without IN_FORMATS")
321 }
322}
323
324// A flip shows only the windows its scene draws: presentation feedback of
325// a window on another workspace, hidden or scrolled off the output is
326// discarded, not presented.
327func TestShownByScene(t *testing.T) {
328 s := ports.Scene{OutputWidth: 10, OutputHeight: 10, Windows: []ports.SceneWindow{{ID: 1, Rect: ports.Rect{X: 5, W: 10, H: 10}}, {ID: 2, Hidden: true}, {ID: 3, Rect: ports.Rect{X: 10, W: 10, H: 10}}}, Layers: []ports.SceneLayer{{ID: 5}}}
329 got := (&Output{}).shownBy(s, map[ports.WindowID]uint64{1: 3, 2: 4, 3: 8, 5: 6, 9: 7})
330 if len(got) != 2 || got[1] != 3 || got[5] != 6 {
331 t.Fatalf("shows %v", got)
332 }
333}
334
335// The decision is callable without Run and keeps the zero-allocation
336// composition and cached direct-scanout paths cheap.
337func TestFrameDecisionAllocations(t *testing.T) {
338 o, _, _ := testOutput(t)
339 o.scanout = true
340 s := ports.Scene{OutputWidth: 200, OutputHeight: 100, Windows: []ports.SceneWindow{{ID: 1, Fullscreen: true, Rect: ports.Rect{W: 200, H: 100}}}}
341 c := ports.SurfaceContent{ID: 1, Width: 200, Height: 100, LogicalW: 200, LogicalH: 100, DMABuf: &ports.DMABuf{ID: 17, Format: fourccXRGB}}
342 surfaces := map[ports.WindowID]ports.SurfaceContent{1: c}
343 o.clientFBs[17] = &clientFB{fbID: 77}
344 o.primary.formats = []ports.DMABufFormat{{Format: fourccXRGB}}
345 for _, tc := range []struct {
346 name string
347 capture bool
348 wantFB uint32
349 wantReason string
350 }{
351 {"direct", false, 77, ""}, {"capture", true, 0, "capture"},
352 } {
353 t.Run(tc.name, func(t *testing.T) {
354 d := o.decideFrame(s, surfaces, tc.capture)
355 if d.fb != tc.wantFB || o.reason != tc.wantReason {
356 t.Fatalf("decision: fb=%d reason=%q", d.fb, o.reason)
357 }
358 if tc.capture && o.overlayReason != "capture" {
359 t.Fatalf("capture overlay reason %q", o.overlayReason)
360 }
361 if n := testing.AllocsPerRun(100, func() { o.decideFrame(s, surfaces, tc.capture) }); n != 0 {
362 t.Fatalf("decision: %.1f allocs", n)
363 }
364 })
365 }
366 o.primary.formats = nil
367 d := o.decideFrame(s, surfaces, false)
368 if d.fb != 0 || o.overlayReason != "no_plane" || len(d.composed.Windows) != 1 {
369 t.Fatalf("composition: %+v", d)
370 }
371 if n := testing.AllocsPerRun(100, func() { o.decideFrame(s, surfaces, false) }); n != 0 {
372 t.Fatalf("composition decision: %.1f allocs", n)
373 }
374}
375
376func TestFrameDecisionOverlayFallback(t *testing.T) {
377 o, k, _ := overlayOutput(t)
378 o.overlay.formats = []ports.DMABufFormat{{Format: fourccXRGB}}
379 s, surfaces := overlayScene()
380 k.EXPECT().addFB(mock.Anything, uint32(fourccXRGB)).Return(88, nil).Once()
381 d := o.decideFrame(s, surfaces, false)
382 if d.fb != 0 || d.overlay.fb != 88 || o.reason != "other_windows" || len(d.composed.Windows) != 1 {
383 t.Fatalf("overlay decision: %+v", d)
384 }
385 d.overlay.close()
386 o.clientFBs[9].overlayFailed = "overlay_refused"
387 d = o.decideFrame(s, surfaces, false)
388 if d.overlay.fb != 0 || o.overlayReason != "overlay_refused" || len(d.composed.Windows) != 2 {
389 t.Fatalf("fallback decision: %+v", d)
390 }
391}