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) {
201 reason := "disabled"
202 if o.scanout {
203 c, reason = scanoutCandidate(scene, surfaces, o.Width(), o.Height())
204 if reason == "" && isYUVFormat(c.DMABuf.Format) {
205 reason = "yuv"
206 }
207 if o.hdr.on && reason == "" {
208 switch {
209 case !c.Color.IsPQ2020():
210 reason = "hdr_sdr_content"
211 case !isTenBit(c.DMABuf.Format):
212 reason = "hdr_format"
213 }
214 } else if !o.hdr.on && reason == "" && c.Color.IsPQ2020() {
215 // An SDR connector must never show raw PQ values.
216 reason = "sdr_pq_content"
217 }
218 }
219 if reason == "" {
220 fb, reason = o.scanoutFB(c.DMABuf, time.Now())
221 if reason == "" {
222 rect := scanoutRect(c, o.Width(), o.Height())
223 cfb := o.clientFBs[c.DMABuf.ID]
224 if cfb.scaleRefused && cfb.scaleFailed == rect {
225 reason = "scale_refused"
226 } else if rect != fullPlaneRect(o.Width(), o.Height()) && (!cfb.scaleTestedOK || cfb.scaleTested != rect) {
227 req := &atomicReq{}
228 o.primaryRectProps(req, fb, rect)
229 if err := o.k.commit(req, atomicTestOnly, 0); err != nil {
230 cfb.scaleFailed = rect
231 cfb.scaleRefused = true
232 reason = "scale_refused"
233 o.log.Info().Str("component", "render").Err(err).Str("reason", reason).Str("connector", o.conn.name).Msg("scanout scaling refused")
234 } else {
235 cfb.scaleTested = rect
236 cfb.scaleTestedOK = true
237 }
238 }
239 }
240 }
241 o.setScanoutReason(reason)
242 if reason != "" {
243 return 0, c
244 }
245 return fb, c
246}
247
248// overlayFrame decides the overlay of a frame: the window, and the scene
249// the renderer composes (the window left out). A zero overlayWin means
250// none; the reason is logged on change.
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.hdr.on {
255 reason = "hdr"
256 } else if o.overlay != nil && o.scanout {
257 var w ports.SceneWindow
258 var c ports.SurfaceContent
259 w, c, reason = overlayCandidate(s, surfaces)
260 if reason == "" {
261 var fb uint32
262 if fb, reason = o.overlayFB(c.DMABuf, time.Now()); reason == "" {
263 scale := s.Scale
264 if scale <= 0 {
265 scale = 1
266 }
267 ov = overlayWin{id: w.ID, fb: fb, buf: c.DMABuf.ID, w: c.Width, h: c.Height,
268 rect: ports.Rect{X: int(float64(w.Rect.X) * scale), Y: int(float64(w.Rect.Y) * scale), W: c.Width, H: c.Height},
269 acquire: dupFence(c.Acquire)}
270 }
271 }
272 }
273 o.setOverlayReason(reason)
274 if ov.fb == 0 {
275 return overlayWin{}, s
276 }
277 // The composed frame leaves the window out: the overlay shows it.
278 rest := s
279 rest.Windows = make([]ports.SceneWindow, 0, len(s.Windows))
280 for _, w := range s.Windows {
281 if w.ID != ov.id {
282 rest.Windows = append(rest.Windows, w)
283 }
284 }
285 return ov, rest
286}
287
288// frameDecision selects the zero-copy path or the scene to compose.
289type frameDecision struct {
290 fb uint32
291 content ports.SurfaceContent
292 rect planeRect
293 overlay overlayWin
294 composed ports.Scene
295}
296
297// composeFrame chooses overlay or composition after direct scanout cannot commit.
298// The caller owns the returned overlay fence.
299func (o *Output) composeFrame(s ports.Scene, surfaces map[ports.WindowID]ports.SurfaceContent) frameDecision {
300 ov, rest := o.overlayFrame(s, surfaces)
301 if ov.fb != 0 {
302 return frameDecision{overlay: ov, composed: rest}
303 }
304 return frameDecision{composed: s}
305}
306
307// decideFrame tries direct scanout, then overlay, then composition.
308// Captures require composition; the caller owns the returned overlay fence.
309func (o *Output) decideFrame(s ports.Scene, surfaces map[ports.WindowID]ports.SurfaceContent, capture bool) frameDecision {
310 if capture {
311 o.setScanoutReason("capture")
312 o.setOverlayReason("capture")
313 return frameDecision{composed: s}
314 }
315 fb, c := o.scanoutFrame(s, surfaces)
316 if fb != 0 {
317 return frameDecision{fb: fb, content: c, rect: scanoutRect(c, o.Width(), o.Height())}
318 }
319 return o.composeFrame(s, surfaces)
320}
321
322// setScanoutReason logs a direct-scanout transition once.
323func (o *Output) setScanoutReason(reason string) {
324 if reason != o.reason {
325 o.log.Info().Str("component", "render").Bool("direct_scanout", reason == "").Str("reason", reason).Str("connector", o.conn.name).Msg("scanout")
326 o.reason = reason
327 }
328}
329
330// setOverlayReason logs an overlay transition once.
331func (o *Output) setOverlayReason(reason string) {
332 if reason != o.overlayReason {
333 o.log.Info().Str("component", "render").Bool("overlay", reason == "").Str("reason", reason).Str("connector", o.conn.name).Msg("overlay")
334 o.overlayReason = reason
335 }
336}
337
338// clientFB is a client buffer imported as a KMS framebuffer.
339type clientFB struct {
340 fbID uint32
341 last time.Time // when last wanted on screen
342 // failed is the reason KMS refused the buffer; it is not retried.
343 failed string
344 // noAsync: KMS refused an async flip to it; it flips at vblank.
345 noAsync bool
346 scaleTested, scaleFailed planeRect
347 scaleRefused, scaleTestedOK bool
348 // overlayFailed is why the overlay plane refused it; kept apart from
349 // failed, so a refusal on one plane does not bar the other.
350 overlayFailed string
351 importErr bool
352}
353
354// scanoutFB returns the framebuffer of a client buffer for the primary
355// plane, importing it on first use. A buffer KMS refuses is remembered
356// with its reason.
357func (o *Output) scanoutFB(b *ports.DMABuf, now time.Time) (uint32, string) {
358 return o.planeFB(o.primary, b, now, func(fb *clientFB) *string { return &fb.failed })
359}
360
361// overlayFB is scanoutFB for the overlay plane, with its own formats and
362// refusal reason.
363func (o *Output) overlayFB(b *ports.DMABuf, now time.Time) (uint32, string) {
364 return o.planeFB(o.overlay, b, now, func(fb *clientFB) *string { return &fb.overlayFailed })
365}
366
367// planeFB imports b once as a framebuffer; reason is the plane's refusal
368// field. Both planes share the framebuffer: they show the same format.
369func (o *Output) planeFB(p *plane, b *ports.DMABuf, now time.Time, reason func(*clientFB) *string) (uint32, string) {
370 fb := o.clientFBs[b.ID]
371 if fb == nil {
372 fb = &clientFB{}
373 o.clientFBs[b.ID] = fb
374 }
375 fb.last = now
376 why := reason(fb)
377 if *why != "" {
378 return 0, *why
379 }
380 format, ok := planeFormat(p, b)
381 if !ok {
382 *why = "format"
383 return 0, *why
384 }
385 if fb.fbID == 0 {
386 if fb.importErr {
387 *why = "import_failed"
388 return 0, *why
389 }
390 id, err := o.k.addFB(b, format)
391 if err != nil {
392 o.log.Info().Str("component", "render").Err(err).Uint32("format", b.Format).Uint64("modifier", b.Modifier).Msg("scanout import failed")
393 fb.importErr, *why = true, "import_failed"
394 return 0, *why
395 }
396 fb.fbID = id
397 }
398 return fb.fbID, ""
399}
400
401// scanoutFormat is the framebuffer format a client buffer is scanned out
402// as on the primary plane (see planeFormat).
403func (o *Output) scanoutFormat(b *ports.DMABuf) (uint32, bool) { return planeFormat(o.primary, b) }
404
405// planeFormat is the framebuffer format a client buffer shows as on p,
406// when p lists it with the buffer's modifier. Planes here show no alpha:
407// an alpha format goes as its X variant when the plane has that one.
408func planeFormat(p *plane, b *ports.DMABuf) (uint32, bool) {
409 has := func(f uint32) bool {
410 return slices.Contains(p.formats, ports.DMABufFormat{Format: f, Modifier: b.Modifier})
411 }
412 if x, ok := opaqueVariant[b.Format]; ok && has(x) {
413 return x, true
414 }
415 return b.Format, has(b.Format)
416}
417
418// opaqueVariant maps alpha formats to the same layout without alpha.
419var opaqueVariant = map[uint32]uint32{
420 fourccARGB: fourccXRGB,
421 fourccABGR: fourccXBGR,
422 fourccAR30: fourccXR30,
423 fourccAB30: fourccXB30,
424 fourccAR4H: fourccXR4H,
425 fourccAB4H: fourccXB4H,
426}
427
428// scanoutFormats are the formats a buffer can be scanned out with: in the
429// primary plane's IN_FORMATS (directly or as its opaque variant) and
430// composable by the renderer.
431func (o *Output) scanoutFormats(sampled []ports.DMABufFormat) []ports.DMABufFormat {
432 var out []ports.DMABufFormat
433 for _, f := range sampled {
434 b := ports.DMABuf{Format: f.Format, Modifier: f.Modifier}
435 if _, ok := o.scanoutFormat(&b); ok {
436 out = append(out, f)
437 }
438 }
439 return out
440}
441
442// isYUVFormat identifies video formats which are always composed, never scanned out.
443func isYUVFormat(format uint32) bool {
444 return format == fourccNV12 || format == fourccP010
445}
446
447// isTenBit identifies the packed 2101010 formats supported by the renderer.
448func isTenBit(format uint32) bool {
449 switch format {
450 case fourccXR30, fourccAR30, fourccXB30, fourccAB30:
451 return true
452 }
453 return false
454}
455
456const (
457 fourccNV12 = 'N' | 'V'<<8 | '1'<<16 | '2'<<24
458 fourccP010 = 'P' | '0'<<8 | '1'<<16 | '0'<<24
459 fourccXRGB = 'X' | 'R'<<8 | '2'<<16 | '4'<<24
460 fourccARGB = 'A' | 'R'<<8 | '2'<<16 | '4'<<24
461 fourccXBGR = 'X' | 'B'<<8 | '2'<<16 | '4'<<24
462 fourccABGR = 'A' | 'B'<<8 | '2'<<16 | '4'<<24
463 fourccXR30 = 'X' | 'R'<<8 | '3'<<16 | '0'<<24
464 fourccAR30 = 'A' | 'R'<<8 | '3'<<16 | '0'<<24
465 fourccXB30 = 'X' | 'B'<<8 | '3'<<16 | '0'<<24
466 fourccAB30 = 'A' | 'B'<<8 | '3'<<16 | '0'<<24
467 fourccXR4H = 'X' | 'R'<<8 | '4'<<16 | 'H'<<24
468 fourccAR4H = 'A' | 'R'<<8 | '4'<<16 | 'H'<<24
469 fourccXB4H = 'X' | 'B'<<8 | '4'<<16 | 'H'<<24
470 fourccAB4H = 'A' | 'B'<<8 | '4'<<16 | 'H'<<24
471)
472
473// addFB imports a dmabuf as a framebuffer of the given fourcc: client buffers, the renderer's
474// output images and cursor images. The GEM handle belongs to the card fd
475// and is shared by every import of the same buffer, so import, ADDFB2 and
476// close run under the lock and the handle is closed right away: the
477// framebuffer keeps its own reference to the buffer.
478func (k kmsDevice) addFB(b *ports.DMABuf, format uint32) (uint32, error) {
479 raw, err := b.Planes[0].File.SyscallConn()
480 if err != nil {
481 return 0, err
482 }
483 k.gemMu.Lock()
484 defer k.gemMu.Unlock()
485 var p primeHandle
486 var ioErr error
487 if err := raw.Control(func(fd uintptr) {
488 p.fd = int32(fd)
489 ioErr = ioctl(k.fd, ioctlPrimeFDToHandle, unsafe.Pointer(&p))
490 }); err != nil {
491 return 0, err
492 }
493 if ioErr != nil {
494 return 0, errors.Join(errors.New("prime import"), ioErr)
495 }
496 defer func() {
497 g := gemClose{handle: p.handle}
498 _ = ioctl(k.fd, ioctlGemClose, unsafe.Pointer(&g))
499 }()
500 cmd := fbCmd2{width: uint32(b.Width), height: uint32(b.Height), format: format}
501 useMod := b.Modifier != modInvalid && (b.Modifier != 0 || k.modifiers)
502 if useMod {
503 cmd.flags = fbModifiers
504 }
505 // Planes in the same dmabuf share its GEM handle: each distinct
506 // handle is closed once.
507 closing := map[uint32]bool{p.handle: true}
508 for i, pl := range b.Planes {
509 if i >= len(cmd.handles) {
510 break
511 }
512 h := p.handle
513 if i > 0 {
514 var err error
515 if h, err = k.primeImport(pl.File); err != nil {
516 return 0, err
517 }
518 if !closing[h] {
519 closing[h] = true
520 defer k.gemClose(h)
521 }
522 }
523 cmd.handles[i], cmd.pitches[i], cmd.offsets[i] = h, pl.Stride, pl.Offset
524 if useMod {
525 cmd.modifiers[i] = b.Modifier
526 }
527 }
528 if err := ioctl(k.fd, ioctlAddFB2, unsafe.Pointer(&cmd)); err != nil {
529 return 0, errors.Join(errors.New("add fb2"), err)
530 }
531 return cmd.fbID, nil
532}
533
534// primeImport returns the GEM handle of a dmabuf file. The caller holds
535// gemMu and closes the handle.
536func (k kmsDevice) primeImport(f *os.File) (uint32, error) {
537 raw, err := f.SyscallConn()
538 if err != nil {
539 return 0, err
540 }
541 var p primeHandle
542 var ioErr error
543 if err := raw.Control(func(fd uintptr) {
544 p.fd = int32(fd)
545 ioErr = ioctl(k.fd, ioctlPrimeFDToHandle, unsafe.Pointer(&p))
546 }); err != nil {
547 return 0, err
548 }
549 if ioErr != nil {
550 return 0, errors.Join(errors.New("prime import"), ioErr)
551 }
552 return p.handle, nil
553}
554
555func (k kmsDevice) gemClose(h uint32) {
556 g := gemClose{handle: h}
557 _ = ioctl(k.fd, ioctlGemClose, unsafe.Pointer(&g))
558}
559
560// dropClientFBs frees framebuffers not wanted for scanoutIdleTTL, or all
561// of them, except those on screen or queued.
562func (o *Output) dropClientFBs(now time.Time, all bool) {
563 for id, fb := range o.clientFBs {
564 if id == o.shown || id == o.queued {
565 continue
566 }
567 if !all && now.Sub(fb.last) < scanoutIdleTTL {
568 continue
569 }
570 if fb.fbID != 0 {
571 _ = o.k.rmFB(fb.fbID)
572 }
573 delete(o.clientFBs, id)
574 }
575}
576
577// dupFence duplicates a sync file under its SyscallConn, so a concurrent
578// Close cannot hand us a reused fd; nil when f is nil or closed.
579func dupFence(f *os.File) *os.File {
580 if f == nil {
581 return nil
582 }
583 raw, err := f.SyscallConn()
584 if err != nil {
585 return nil
586 }
587 fd := -1
588 if raw.Control(func(v uintptr) { fd, _ = unix.FcntlInt(v, unix.F_DUPFD_CLOEXEC, 0) }) != nil || fd < 0 {
589 return nil
590 }
591 return os.NewFile(uintptr(fd), "acquire-fence")
592}