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