1package drm
2
3import (
4 "errors"
5 "math"
6 "os"
7 "slices"
8 "time"
9 "unsafe"
10
11 "github.com/bnema/neferwl/internal/ports"
12 "golang.org/x/sys/unix"
13)
14
15// Direct scanout (ADR 006): when one fullscreen window covers the output
16// with a GPU buffer whose source can fill the plane, the output flips to that buffer
17// instead of compositing it. The buffer becomes a KMS framebuffer once
18// (PRIME import + ADDFB2) and is cached by DMABuf ID while the client
19// keeps using it. KMS waits on the buffer's implicit fences before showing
20// it. Anything else on screen or a buffer KMS refuses falls back to composition.
21
22const (
23 ioctlGemClose = 0x40086409
24 ioctlPrimeFDToHandle = 0xC00C642E
25 ioctlAddFB2 = 0xC06864B8
26
27 fbModifiers = 1 << 1
28 capAddFB2Modifiers = 0x10
29 modInvalid = 0x00ffffffffffffff
30 scanoutIdleTTL = 5 * time.Second // a cached framebuffer survives unused
31)
32
33type primeHandle struct {
34 handle, flags uint32
35 fd int32
36}
37
38type gemClose struct{ handle, pad uint32 }
39
40type fbCmd2 struct {
41 fbID, width, height, format, flags uint32
42 handles, pitches, offsets [4]uint32
43 modifiers [4]uint64
44}
45
46// planeRect contains source coordinates in 16.16 and destination pixels.
47type planeRect struct {
48 src [4]uint64
49 crtc [4]uint64
50}
51
52func fullPlaneRect(w, h int) planeRect {
53 return planeRect{src: [4]uint64{0, 0, uint64(w) << 16, uint64(h) << 16}, crtc: [4]uint64{0, 0, uint64(w), uint64(h)}}
54}
55
56func scanoutRect(c ports.SurfaceContent, w, h int) planeRect {
57 r := fullPlaneRect(w, h)
58 r.src = [4]uint64{0, 0, uint64(c.Width) << 16, uint64(c.Height) << 16}
59 if c.Source[2] != 0 {
60 for i, value := range c.Source {
61 r.src[i] = uint64(float64(value) * 65536)
62 }
63 }
64 return r
65}
66
67// covers reports whether win fills the output of scene s.
68func covers(s *ports.Scene, win *ports.SceneWindow) bool {
69 return (s.WorkspaceClip == (ports.Rect{}) || s.WorkspaceClip == (ports.Rect{W: s.OutputWidth, H: s.OutputHeight})) &&
70 win.Rect.X == 0 && win.Rect.Y == 0 && win.Rect.W == s.OutputWidth && win.Rect.H == s.OutputHeight
71}
72
73// fullscreenShown reports whether a visible fullscreen window covers the
74// output, whether its frame is scanned out or composed.
75func fullscreenShown(s *ports.Scene) bool {
76 for i := range s.Windows {
77 win := &s.Windows[i]
78 if win.Fullscreen && !win.Hidden && covers(s, win) {
79 return true
80 }
81 }
82 return false
83}
84
85// scanoutCandidate returns the window content the output can scan out
86// directly, or a reason why it cannot. w, h are the output's physical size.
87// The plane's formats are checked by scanoutFB.
88func scanoutCandidate(s ports.Scene, surfaces map[ports.WindowID]ports.SurfaceContent, w, h int) (ports.SurfaceContent, string) {
89 if s.Transform != 0 {
90 // The scanout buffer would have to be pre-rotated: rotated outputs compose.
91 return ports.SurfaceContent{}, "output_transform"
92 }
93 if s.WorkspaceClip != (ports.Rect{}) && s.WorkspaceClip != (ports.Rect{W: s.OutputWidth, H: s.OutputHeight}) {
94 return ports.SurfaceContent{}, "workspace_clip"
95 }
96 var full *ports.SceneWindow
97 for i := range s.Windows {
98 win := &s.Windows[i]
99 if win.Hidden || win.Rect.W <= 0 || win.Rect.H <= 0 {
100 continue
101 }
102 if !win.Fullscreen || !covers(&s, win) || full != nil {
103 return ports.SurfaceContent{}, "other_windows"
104 }
105 full = win
106 }
107 if full == nil {
108 return ports.SurfaceContent{}, "no_fullscreen"
109 }
110 if len(s.DropHints) > 0 {
111 // Drag hints are drawn over the window.
112 return ports.SurfaceContent{}, "drop_hint"
113 }
114 if full.Dim > 0 {
115 // Its veil is composed over it: the plane would show it bright.
116 return ports.SurfaceContent{}, "dim"
117 }
118 // Core leaves the layers a fullscreen window hides out of the scene:
119 // one above the windows here is drawn.
120 for _, l := range s.Layers {
121 if l.Layer >= ports.LayerTop && l.Rect.W > 0 && l.Rect.H > 0 {
122 return ports.SurfaceContent{}, "overlay_surface"
123 }
124 }
125 c := surfaces[full.ID]
126 if c.DMABuf == nil && len(c.Children) > 0 {
127 var reason string
128 if c, reason = clientSubsurface(c); reason != "" {
129 return c, reason
130 }
131 }
132 switch {
133 case c.DMABuf == nil:
134 return c, "not_dmabuf"
135 case c.Fade > 0:
136 // The plane would show it unfaded (wp_alpha_modifier_v1).
137 return c, "fade"
138 case len(c.Children) > 0:
139 return c, "subsurfaces"
140 case c.Transform != 0:
141 // Planes show buffers as they are: rotation is composed.
142 return c, "buffer_transform"
143 case c.LogicalW != full.Rect.W || c.LogicalH != full.Rect.H:
144 return c, "letterbox"
145 case c.Width <= 0 || c.Height <= 0 || c.Source[2] < 0 || c.Source[3] < 0 || !finiteSource(c.Source):
146 return c, "source_crop"
147 case c.Geometry != (ports.Rect{}) && (c.Geometry.X != 0 || c.Geometry.Y != 0 || c.Geometry.W != c.LogicalW || c.Geometry.H != c.LogicalH):
148 return c, "geometry_crop"
149 }
150 if c.Source[2] != 0 && (c.Source[0] < 0 || c.Source[1] < 0 || c.Source[2] <= 0 || c.Source[3] <= 0 || c.Source[0]+c.Source[2] > float32(c.Width) || c.Source[1]+c.Source[3] > float32(c.Height)) {
151 return c, "source_crop"
152 }
153 return c, ""
154}
155
156func finiteSource(src [4]float32) bool {
157 for _, n := range src {
158 if math.IsNaN(float64(n)) || math.IsInf(float64(n), 0) {
159 return false
160 }
161 }
162 return true
163}
164
165// cropped reports whether a viewport source shows less than the buffer.
166func cropped(c ports.SurfaceContent) bool {
167 return c.Source[2] != 0 && c.Source != [4]float32{0, 0, float32(c.Width), float32(c.Height)}
168}
169
170// clientSubsurface returns the content to scan out for a window whose
171// root has no GPU buffer but one opaque subsurface over its geometry: Wine
172// draws the frame in the root (wl_shm) and presents Vulkan in a subsurface
173// on the client area. That subsurface hides the root entirely, so it is
174// what the screen shows. The returned content keeps the window's ID and
175// Seq for release and presentation.
176func clientSubsurface(c ports.SurfaceContent) (ports.SurfaceContent, string) {
177 if len(c.Children) != 1 {
178 return c, "subsurfaces"
179 }
180 ch := c.Children[0]
181 switch {
182 case ch.DMABuf == nil:
183 return c, "not_dmabuf"
184 case !ch.Opaque:
185 return c, "subsurface_translucent"
186 case ch.Below:
187 // The root is drawn over it.
188 return c, "subsurface_below"
189 case ch.X != c.Geometry.X || ch.Y != c.Geometry.Y:
190 return c, "subsurface_offset"
191 }
192 out := ch.SurfaceContent
193 out.ID, out.Seq, out.Geometry = c.ID, c.Seq, ports.Rect{}
194 return out, ""
195}
196
197// scanoutFrame picks a fullscreen client buffer to flip directly. It
198// returns fb 0, with the reason logged on change, when the frame must be
199// composed.
200func (o *Output) scanoutFrame(scene ports.Scene, surfaces map[ports.WindowID]ports.SurfaceContent) (fb uint32, c ports.SurfaceContent, mode colorMode) {
201 reason := "disabled"
202 if o.scanout {
203 c, reason = scanoutCandidate(scene, surfaces, o.Width(), o.Height())
204 if reason == "" {
205 mode, reason = planeColor(c.Color, c.DMABuf.Format, c.Opaque, o.hdr.on, o.primary.pipeline)
206 }
207 }
208 if reason == "" {
209 fb, reason = o.scanoutFB(c.DMABuf, time.Now())
210 if reason == "" {
211 rect := scanoutRect(c, o.Width(), o.Height())
212 cfb := o.clientFBs[c.DMABuf.ID]
213 if cfb.scaleRefused && cfb.scaleFailed == rect {
214 reason = "scale_refused"
215 } else if rect != fullPlaneRect(o.Width(), o.Height()) && (!cfb.scaleTestedOK || cfb.scaleTested != rect) {
216 req := &o.probeReq
217 req.reset()
218 o.primaryRectProps(req, fb, rect)
219 if err := o.k.commit(req, atomicTestOnly, 0); err != nil {
220 cfb.scaleFailed = rect
221 cfb.scaleRefused = true
222 reason = "scale_refused"
223 o.renderLog.Info().Err(err).Str("reason", reason).Str("connector", o.conn.name).Msg("scanout scaling refused")
224 } else {
225 cfb.scaleTested = rect
226 cfb.scaleTestedOK = true
227 }
228 }
229 if reason == "" && mode != colorBypass && !o.colorAllowed(o.primary, c.DMABuf.Format, func(req *atomicReq) {
230 o.primaryRectProps(req, fb, rect)
231 if o.overlayOn != 0 {
232 o.overlayProps(req, overlayWin{})
233 }
234 }) {
235 reason = "color_refused"
236 }
237 }
238 }
239 o.setScanoutReason(reason)
240 if reason != "" {
241 return 0, c, colorBypass
242 }
243 return fb, c, mode
244}
245
246// overlayFrame decides the overlay of a frame: the window, and the scene
247// the renderer composes (the window left out). A zero overlayWin means
248// none; the reason is logged on change. The returned scene's Windows is
249// valid until the next call: it reuses o.restWindows (its consumers, Render,
250// ExcludedScene and the capture tracking, run synchronously before then).
251func (o *Output) overlayFrame(s ports.Scene, surfaces map[ports.WindowID]ports.SurfaceContent) (overlayWin, ports.Scene) {
252 reason := "no_plane"
253 var ov overlayWin
254 if o.overlay != nil && o.scanout {
255 var w ports.SceneWindow
256 var c ports.SurfaceContent
257 var mode colorMode
258 w, c, mode, reason = overlayCandidate(s, surfaces, o.hdr.on, o.overlay.pipeline)
259 if reason == "" && mode != colorBypass {
260 if ok, known := o.overlay.verdict(c.DMABuf.Format, o.cursorShown()); known && !ok {
261 reason = "color_refused"
262 }
263 }
264 if reason == "" {
265 var fb uint32
266 if fb, reason = o.overlayFB(c.DMABuf, time.Now()); reason == "" {
267 scale := s.Scale
268 if scale <= 0 {
269 scale = 1
270 }
271 ov = overlayWin{id: w.ID, fb: fb, buf: c.DMABuf.ID, w: c.Width, h: c.Height,
272 rect: ports.Rect{X: int(float64(w.Rect.X) * scale), Y: int(float64(w.Rect.Y) * scale), W: c.Width, H: c.Height},
273 acquire: dupFence(c.Acquire), color: colorUse{mode: mode, format: c.DMABuf.Format}}
274 }
275 }
276 }
277 o.setOverlayReason(reason)
278 if ov.id == 0 {
279 return overlayWin{}, s
280 }
281 // The composed frame leaves the window out: the overlay shows it.
282 rest := s
283 o.restWindows = o.restWindows[:0]
284 for _, w := range s.Windows {
285 if w.ID != ov.id {
286 o.restWindows = append(o.restWindows, w)
287 }
288 }
289 rest.Windows = o.restWindows
290 return ov, rest
291}
292
293// frameDecision selects the zero-copy path or the scene to compose.
294type frameDecision struct {
295 fb uint32
296 content ports.SurfaceContent
297 rect planeRect
298 color colorMode // how the primary plane shows fb
299 overlay overlayWin
300 composed ports.Scene
301}
302
303// composeFrame chooses overlay or composition after direct scanout cannot commit.
304// The caller owns the returned overlay fence.
305func (o *Output) composeFrame(s ports.Scene, surfaces map[ports.WindowID]ports.SurfaceContent) frameDecision {
306 ov, rest := o.overlayFrame(s, surfaces)
307 if ov.fb != 0 {
308 return frameDecision{overlay: ov, composed: rest}
309 }
310 return frameDecision{composed: s}
311}
312
313// decideFrame tries direct scanout, then overlay, then composition.
314// Captures require composition; the caller owns the returned overlay fence.
315func (o *Output) decideFrame(s ports.Scene, surfaces map[ports.WindowID]ports.SurfaceContent, capture bool) frameDecision {
316 if capture {
317 o.setScanoutReason("capture")
318 o.setOverlayReason("capture")
319 return frameDecision{composed: s}
320 }
321 fb, c, mode := o.scanoutFrame(s, surfaces)
322 if fb != 0 {
323 return frameDecision{fb: fb, content: c, rect: scanoutRect(c, o.Width(), o.Height()), color: mode}
324 }
325 return o.composeFrame(s, surfaces)
326}
327
328// setScanoutReason logs a direct-scanout transition once.
329func (o *Output) setScanoutReason(reason string) {
330 if reason != o.reason {
331 o.renderLog.Info().Bool("direct_scanout", reason == "").Str("reason", reason).Str("connector", o.conn.name).Msg("scanout")
332 o.reason = reason
333 }
334}
335
336// setOverlayReason logs an overlay transition once.
337func (o *Output) setOverlayReason(reason string) {
338 if reason != o.overlayReason {
339 o.renderLog.Info().Bool("overlay", reason == "").Str("reason", reason).Str("connector", o.conn.name).Msg("overlay")
340 o.overlayReason = reason
341 }
342}
343
344// clientFB is a client buffer imported as a KMS framebuffer.
345type clientFB struct {
346 fbID uint32
347 last time.Time // when last wanted on screen
348 // failed is the reason KMS refused the buffer; it is not retried.
349 failed string
350 // noAsync: KMS refused an async flip to it; it flips at vblank.
351 noAsync bool
352 scaleTested, scaleFailed planeRect
353 scaleRefused, scaleTestedOK bool
354 // overlayFailed is why the overlay plane refused it; kept apart from
355 // failed, so a refusal on one plane does not bar the other.
356 overlayFailed string
357 importErr bool
358}
359
360// scanoutFB returns the framebuffer of a client buffer for the primary
361// plane, importing it on first use. A buffer KMS refuses is remembered
362// with its reason.
363func (o *Output) scanoutFB(b *ports.DMABuf, now time.Time) (uint32, string) {
364 return o.planeFB(o.primary, b, now, func(fb *clientFB) *string { return &fb.failed })
365}
366
367// overlayFB is scanoutFB for the overlay plane, with its own formats and
368// refusal reason.
369func (o *Output) overlayFB(b *ports.DMABuf, now time.Time) (uint32, string) {
370 return o.planeFB(o.overlay, b, now, func(fb *clientFB) *string { return &fb.overlayFailed })
371}
372
373// planeFB imports b once as a framebuffer; reason is the plane's refusal
374// field. Both planes share the framebuffer: they show the same format.
375func (o *Output) planeFB(p *plane, b *ports.DMABuf, now time.Time, reason func(*clientFB) *string) (uint32, string) {
376 fb := o.clientFBs[b.ID]
377 if fb == nil {
378 fb = &clientFB{}
379 o.clientFBs[b.ID] = fb
380 }
381 fb.last = now
382 why := reason(fb)
383 if *why != "" {
384 return 0, *why
385 }
386 format, ok := planeFormat(p, b)
387 if !ok {
388 *why = "format"
389 return 0, *why
390 }
391 if fb.fbID == 0 {
392 if fb.importErr {
393 *why = "import_failed"
394 return 0, *why
395 }
396 id, err := o.k.addFB(b, format)
397 if err != nil {
398 o.renderLog.Info().Err(err).Uint32("format", b.Format).Uint64("modifier", b.Modifier).Msg("scanout import failed")
399 fb.importErr, *why = true, "import_failed"
400 return 0, *why
401 }
402 fb.fbID = id
403 }
404 return fb.fbID, ""
405}
406
407// scanoutFormat is the framebuffer format a client buffer is scanned out
408// as on the primary plane (see planeFormat).
409func (o *Output) scanoutFormat(b *ports.DMABuf) (uint32, bool) { return planeFormat(o.primary, b) }
410
411// planeFormat is the framebuffer format a client buffer shows as on p,
412// when p lists it with the buffer's modifier. Planes here show no alpha:
413// an alpha format goes as its X variant when the plane has that one.
414func planeFormat(p *plane, b *ports.DMABuf) (uint32, bool) {
415 has := func(f uint32) bool {
416 return slices.Contains(p.formats, ports.DMABufFormat{Format: f, Modifier: b.Modifier})
417 }
418 if x, ok := opaqueVariant[b.Format]; ok && has(x) {
419 return x, true
420 }
421 return b.Format, has(b.Format)
422}
423
424// opaqueVariant maps alpha formats to the same layout without alpha.
425var opaqueVariant = map[uint32]uint32{
426 fourccARGB: fourccXRGB,
427 fourccABGR: fourccXBGR,
428 fourccAR30: fourccXR30,
429 fourccAB30: fourccXB30,
430 fourccAR4H: fourccXR4H,
431 fourccAB4H: fourccXB4H,
432}
433
434// scanoutFormats are the formats a buffer can be scanned out with: in the
435// primary plane's IN_FORMATS (directly or as its opaque variant) and
436// composable by the renderer.
437func (o *Output) scanoutFormats(sampled []ports.DMABufFormat) []ports.DMABufFormat {
438 var out []ports.DMABufFormat
439 for _, f := range sampled {
440 b := ports.DMABuf{Format: f.Format, Modifier: f.Modifier}
441 if _, ok := o.scanoutFormat(&b); ok {
442 out = append(out, f)
443 }
444 }
445 return out
446}
447
448// isYUVFormat identifies video formats which are always composed, never scanned out.
449func isYUVFormat(format uint32) bool {
450 return format == fourccNV12 || format == fourccP010
451}
452
453// isTenBit identifies the packed 2101010 formats supported by the renderer.
454func isTenBit(format uint32) bool {
455 switch format {
456 case fourccXR30, fourccAR30, fourccXB30, fourccAB30:
457 return true
458 }
459 return false
460}
461
462const (
463 fourccNV12 = 'N' | 'V'<<8 | '1'<<16 | '2'<<24
464 fourccP010 = 'P' | '0'<<8 | '1'<<16 | '0'<<24
465 fourccXRGB = 'X' | 'R'<<8 | '2'<<16 | '4'<<24
466 fourccARGB = 'A' | 'R'<<8 | '2'<<16 | '4'<<24
467 fourccXBGR = 'X' | 'B'<<8 | '2'<<16 | '4'<<24
468 fourccABGR = 'A' | 'B'<<8 | '2'<<16 | '4'<<24
469 fourccXR30 = 'X' | 'R'<<8 | '3'<<16 | '0'<<24
470 fourccAR30 = 'A' | 'R'<<8 | '3'<<16 | '0'<<24
471 fourccXB30 = 'X' | 'B'<<8 | '3'<<16 | '0'<<24
472 fourccAB30 = 'A' | 'B'<<8 | '3'<<16 | '0'<<24
473 fourccXR4H = 'X' | 'R'<<8 | '4'<<16 | 'H'<<24
474 fourccAR4H = 'A' | 'R'<<8 | '4'<<16 | 'H'<<24
475 fourccXB4H = 'X' | 'B'<<8 | '4'<<16 | 'H'<<24
476 fourccAB4H = 'A' | 'B'<<8 | '4'<<16 | 'H'<<24
477)
478
479// addFB imports a dmabuf as a framebuffer of the given fourcc: client buffers, the renderer's
480// output images and cursor images. The GEM handle belongs to the card fd
481// and is shared by every import of the same buffer, so import, ADDFB2 and
482// close run under the lock and the handle is closed right away: the
483// framebuffer keeps its own reference to the buffer.
484func (k kmsDevice) addFB(b *ports.DMABuf, format uint32) (uint32, error) {
485 raw, err := b.Planes[0].File.SyscallConn()
486 if err != nil {
487 return 0, err
488 }
489 k.gemMu.Lock()
490 defer k.gemMu.Unlock()
491 var p primeHandle
492 var ioErr error
493 if err := raw.Control(func(fd uintptr) {
494 p.fd = int32(fd)
495 ioErr = ioctl(k.fd, ioctlPrimeFDToHandle, unsafe.Pointer(&p))
496 }); err != nil {
497 return 0, err
498 }
499 if ioErr != nil {
500 return 0, errors.Join(errors.New("prime import"), ioErr)
501 }
502 defer func() {
503 g := gemClose{handle: p.handle}
504 _ = ioctl(k.fd, ioctlGemClose, unsafe.Pointer(&g))
505 }()
506 cmd := fbCmd2{width: uint32(b.Width), height: uint32(b.Height), format: format}
507 useMod := b.Modifier != modInvalid && (b.Modifier != 0 || k.modifiers)
508 if useMod {
509 cmd.flags = fbModifiers
510 }
511 // Planes in the same dmabuf share its GEM handle: each distinct
512 // handle is closed once.
513 closing := map[uint32]bool{p.handle: true}
514 for i, pl := range b.Planes {
515 if i >= len(cmd.handles) {
516 break
517 }
518 h := p.handle
519 if i > 0 {
520 var err error
521 if h, err = k.primeImport(pl.File); err != nil {
522 return 0, err
523 }
524 if !closing[h] {
525 closing[h] = true
526 defer k.gemClose(h)
527 }
528 }
529 cmd.handles[i], cmd.pitches[i], cmd.offsets[i] = h, pl.Stride, pl.Offset
530 if useMod {
531 cmd.modifiers[i] = b.Modifier
532 }
533 }
534 if err := ioctl(k.fd, ioctlAddFB2, unsafe.Pointer(&cmd)); err != nil {
535 return 0, errors.Join(errors.New("add fb2"), err)
536 }
537 return cmd.fbID, nil
538}
539
540// primeImport returns the GEM handle of a dmabuf file. The caller holds
541// gemMu and closes the handle.
542func (k kmsDevice) primeImport(f *os.File) (uint32, error) {
543 raw, err := f.SyscallConn()
544 if err != nil {
545 return 0, err
546 }
547 var p primeHandle
548 var ioErr error
549 if err := raw.Control(func(fd uintptr) {
550 p.fd = int32(fd)
551 ioErr = ioctl(k.fd, ioctlPrimeFDToHandle, unsafe.Pointer(&p))
552 }); err != nil {
553 return 0, err
554 }
555 if ioErr != nil {
556 return 0, errors.Join(errors.New("prime import"), ioErr)
557 }
558 return p.handle, nil
559}
560
561func (k kmsDevice) gemClose(h uint32) {
562 g := gemClose{handle: h}
563 _ = ioctl(k.fd, ioctlGemClose, unsafe.Pointer(&g))
564}
565
566// dropClientFBs frees framebuffers not wanted for scanoutIdleTTL, or all
567// of them, except those on screen or queued.
568func (o *Output) dropClientFBs(now time.Time, all bool) {
569 for id, fb := range o.clientFBs {
570 if id == o.shown || id == o.queued {
571 continue
572 }
573 if !all && now.Sub(fb.last) < scanoutIdleTTL {
574 continue
575 }
576 if fb.fbID != 0 {
577 _ = o.k.rmFB(fb.fbID)
578 }
579 delete(o.clientFBs, id)
580 }
581}
582
583// dupFence duplicates a sync file under its SyscallConn, so a concurrent
584// Close cannot hand us a reused fd; nil when f is nil or closed.
585func dupFence(f *os.File) *os.File {
586 if f == nil {
587 return nil
588 }
589 raw, err := f.SyscallConn()
590 if err != nil {
591 return nil
592 }
593 fd := -1
594 if raw.Control(func(v uintptr) { fd, _ = unix.FcntlInt(v, unix.F_DUPFD_CLOEXEC, 0) }) != nil || fd < 0 {
595 return nil
596 }
597 return os.NewFile(uintptr(fd), "acquire-fence")
598}