1package drm
2
3import (
4 "errors"
5 "os"
6 "slices"
7
8 "github.com/bnema/neferwl/internal/ports"
9 "golang.org/x/sys/unix"
10)
11
12// Overlay plane: a window drawn from one opaque dmabuf, with nothing but
13// the cursor above it, goes on an overlay plane at its place on screen;
14// the rest of the scene is composed into the primary plane without it.
15// The client buffer is shown with no GPU pass, like direct scanout, but
16// the window need not cover the output. A refusal (TEST_ONLY) keeps the
17// window composed and is cached per buffer.
18
19// pickOverlay takes an overlay plane above the primary one (zpos), when
20// the output has one.
21func (o *Output) pickOverlay() {
22 pz, ok := o.primary.zpos()
23 if !ok {
24 return
25 }
26 for _, p := range o.stray {
27 if p.typ != planeOverlay {
28 continue
29 }
30 if z, ok := p.zpos(); ok && z > pz {
31 o.overlay = p
32 return
33 }
34 }
35}
36
37// zpos is the plane's zpos value when known.
38func (p *plane) zpos() (uint64, bool) {
39 if p.props["zpos"] == 0 {
40 return 0, false
41 }
42 return p.zposValue, true
43}
44
45// overlayWin is the window on the overlay plane: its buffer and where it
46// shows, in physical pixels.
47type overlayWin struct {
48 id ports.WindowID
49 fb uint32
50 buf uint64
51 rect ports.Rect // CRTC rect
52 w, h int // buffer size
53 // color is how the plane's colour pipeline shows the buffer.
54 color colorUse
55 // acquire is the client's explicit-sync fence, a duplicate owned by
56 // the frame loop (closed after the commit); nil without one.
57 acquire *os.File
58}
59
60// overlayCandidate finds the one window the overlay can show: an opaque
61// dmabuf at integer physical coordinates, drawn 1:1, with no other window,
62// popup, border line or layer drawn over it, and the plane colour mode it
63// needs. What is above it elsewhere on screen stays on the primary plane,
64// which the overlay covers only over the window. reason is why none.
65//
66// kept is scanoutCandidate's: a window drawn from a withdrawn content is
67// composed, never put on the plane.
68func overlayCandidate(s ports.Scene, surfaces map[ports.WindowID]ports.SurfaceContent, hdrOn bool, pipeline *colorPipeline, kept func(ports.WindowID) bool) (ports.SceneWindow, ports.SurfaceContent, colorMode, string) {
69 if s.Transform != 0 {
70 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "output_transform"
71 }
72 scale := s.Scale
73 if scale <= 0 {
74 scale = 1
75 }
76 var pick *ports.SceneWindow
77 var pickAt int
78 var mode colorMode
79 for i := range s.Windows {
80 w := &s.Windows[i]
81 if !s.Draws(*w) {
82 // Not drawn (a tile wholly under a panel included): neither
83 // a candidate nor over one.
84 continue
85 }
86 if w.Popup {
87 // Popups draw after every window, wherever they are listed:
88 // checked once the candidate is known.
89 continue
90 }
91 c := surfaces[w.ID]
92 // A viewport crop is composed: the plane would show the whole
93 // buffer, and so is a colour the plane cannot show (raw PQ values
94 // on an SDR output, SDR on HDR without a pipeline). A dimmed
95 // window (a peeking stashed one) needs the veil drawn over it, a
96 // fading one its translucency, and one with the focus effect its
97 // lift; an overview preview is drawn smaller than its buffer.
98 if w.Dim <= 0 && w.Fade <= 0 && w.Zoom <= 0 && w.FocusEffect <= 0 && w.Preview <= 0 && c.DMABuf != nil && !isYUVFormat(c.DMABuf.Format) && c.Opaque && len(c.Children) == 0 && c.Transform == 0 && !cropped(c) && (kept == nil || !kept(w.ID)) {
99 if m, why := planeColor(c.Color, c.DMABuf.Format, c.Opaque, hdrOn, pipeline); why == "" {
100 pick, pickAt, mode = w, i, m
101 continue
102 }
103 }
104 if pick != nil && w.Rect.Overlaps(pick.Rect) {
105 // Something is drawn over the candidate.
106 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "window_above"
107 }
108 }
109 if pick != nil {
110 for i := range s.Windows {
111 if w := &s.Windows[i]; w.Popup && s.Draws(*w) && w.Rect.Overlaps(pick.Rect) {
112 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "window_above"
113 }
114 }
115 }
116 if pick == nil {
117 if kept != nil && len(s.Windows) > 0 {
118 // The one window that could have gone on the plane fades out
119 // after its close: composed from its kept content.
120 for i := range s.Windows {
121 if w := &s.Windows[i]; s.Draws(*w) && kept(w.ID) {
122 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "leaving"
123 }
124 }
125 }
126 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "no_candidate"
127 }
128 if s.Dim > 0 {
129 // The renderer composes the veil (under the first float, or under
130 // every window with DimBehind): a window on the plane would skip it.
131 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "dim"
132 }
133 if len(s.DropHints) > 0 {
134 // Drag hints are drawn over the windows.
135 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "drop_hint"
136 }
137 for _, l := range s.Layers {
138 if l.Layer >= ports.LayerTop && l.Rect.Overlaps(pick.Rect) {
139 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "layer_above"
140 }
141 }
142 if !pick.Fullscreen {
143 if pick.Inset != 0 && s.Border.Width > 0 {
144 // The content sits inside the lines, not at the window's rect.
145 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "border"
146 }
147 for _, sep := range s.Separators {
148 if sep.Rect.Overlaps(pick.Rect) && !linesUnder(s, sep, pick, pickAt) {
149 // The overlay shows the buffer alone; the lines would go
150 // under it.
151 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "border"
152 }
153 }
154 }
155 if clip := s.WorkspaceClip; clip != (ports.Rect{}) && !clip.Contains(pick.Rect) {
156 // The plane cannot apply the workspace's logical viewport crop.
157 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "workspace_clip"
158 }
159 if a, ok := s.TileArea(); ok && pick.Tile() && !a.Contains(pick.Rect) {
160 // Nor the cut of a tile at a panel.
161 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "tile_clip"
162 }
163 c := surfaces[pick.ID]
164 pw, ph := float64(pick.Rect.W)*scale, float64(pick.Rect.H)*scale
165 if float64(c.Width) != pw || float64(c.Height) != ph || c.Geometry != (ports.Rect{}) && (c.Geometry.X != 0 || c.Geometry.Y != 0) {
166 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "scaled"
167 }
168 x, y := float64(pick.Rect.X)*scale, float64(pick.Rect.Y)*scale
169 if x != float64(int(x)) || y != float64(int(y)) {
170 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "fractional_position"
171 }
172 return *pick, c, mode, ""
173}
174
175// linesUnder reports whether sep is painted before the candidate at index
176// pickAt, so stays composed under the plane: the tile lines and the lines
177// of windows listed before it, when the candidate is a float drawn above
178// the tiles.
179func linesUnder(s ports.Scene, sep ports.Separator, pick *ports.SceneWindow, pickAt int) bool {
180 if !pick.Floating || pick.Below {
181 return false
182 }
183 if sep.Window == 0 {
184 return true
185 }
186 for _, w := range s.Windows[:pickAt] {
187 if w.ID == sep.Window {
188 return true
189 }
190 }
191 return false
192}
193
194// overlayProps puts ov on the overlay plane, or turns it off.
195func (o *Output) overlayProps(req *atomicReq, ov overlayWin) {
196 p := o.overlay
197 if p == nil {
198 return
199 }
200 if ov.fb == 0 {
201 req.set(p.id, p.prop("FB_ID"), 0)
202 req.set(p.id, p.prop("CRTC_ID"), 0)
203 o.overlayColorProps(req, colorBypass)
204 return
205 }
206 o.overlayColorProps(req, ov.color.mode)
207 req.set(p.id, p.prop("FB_ID"), uint64(ov.fb))
208 req.set(p.id, p.prop("CRTC_ID"), uint64(o.crtc))
209 req.set(p.id, p.prop("SRC_X"), 0)
210 req.set(p.id, p.prop("SRC_Y"), 0)
211 req.set(p.id, p.prop("SRC_W"), uint64(ov.w)<<16)
212 req.set(p.id, p.prop("SRC_H"), uint64(ov.h)<<16)
213 req.set(p.id, p.prop("CRTC_X"), uint64(int64(ov.rect.X)))
214 req.set(p.id, p.prop("CRTC_Y"), uint64(int64(ov.rect.Y)))
215 req.set(p.id, p.prop("CRTC_W"), uint64(ov.rect.W))
216 req.set(p.id, p.prop("CRTC_H"), uint64(ov.rect.H))
217 if ov.acquire != nil {
218 // KMS waits for the client's GPU work before scanning it out.
219 req.set(p.id, p.prop("IN_FENCE_FD"), uint64(ov.acquire.Fd()))
220 }
221}
222
223// close releases the frame's duplicate of the acquire fence.
224func (ov overlayWin) close() {
225 if ov.acquire != nil {
226 ov.acquire.Close()
227 }
228}
229
230// testOverlay asks KMS whether it takes the frame with ov on the overlay,
231// the cursor at its current place included. A refusal is cached on the
232// buffer; one that the plane's colour pipeline causes (the same frame passes
233// with Bypass) is cached on the plane for the buffer format and the cursor
234// state instead.
235//
236// A frame identical to one KMS took (same images, place, colour mode and
237// cursor; the fence does not count) is not probed again: a refusal of the
238// commit itself still drops the overlay (overlayConflict).
239func (o *Output) testOverlay(fb uint32, ov overlayWin) bool {
240 // One cursor snapshot: the input goroutine may move it meanwhile, and
241 // what is remembered must be what KMS was asked.
242 key := o.overlayProbeOf(fb, ov)
243 if slices.Contains(o.overlayPassed[:], key) {
244 return true
245 }
246 err := o.probeOverlay(fb, ov, ov.color.mode, key.cursor)
247 if err == nil {
248 o.overlayPassed[o.overlayNext] = key
249 o.overlayNext = (o.overlayNext + 1) % len(o.overlayPassed)
250 return true
251 }
252 if ov.color.mode != colorBypass && refused(err) && o.probeOverlay(fb, ov, colorBypass, key.cursor) == nil {
253 o.overlay.setVerdict(ov.color.format, key.cursor.on, false)
254 o.setOverlayReason("color_refused")
255 o.renderLog.Info().Err(err).Uint32("format", ov.color.format).Str("connector", o.conn.name).Msg("overlay colour pipeline refused")
256 return false
257 }
258 if cfb := o.clientFBs[ov.buf]; cfb != nil && refused(err) {
259 cfb.overlayFailed = "overlay_refused"
260 }
261 o.setOverlayReason("overlay_refused")
262 o.renderLog.Info().Err(err).Str("connector", o.conn.name).Msg("overlay refused")
263 return false
264}
265
266// forgetOverlayProbes drops the remembered probes: KMS is asked again.
267func (o *Output) forgetOverlayProbes() { clear(o.overlayPassed[:]) }
268
269// overlayProbe is what an overlay probe depends on, fences aside.
270type overlayProbe struct {
271 primary, overlay uint32
272 rect ports.Rect
273 w, h int
274 color colorUse
275 cursor cursorState
276 mult uint64
277 ctm uint32
278}
279
280func (o *Output) overlayProbeOf(fb uint32, ov overlayWin) overlayProbe {
281 p := overlayProbe{primary: fb, overlay: ov.fb, rect: ov.rect, w: ov.w, h: ov.h, color: ov.color, mult: o.colorMult, ctm: o.ctmBlob}
282 if o.cursor != nil && !o.protected {
283 p.cursor = o.cursor.desired()
284 }
285 return p
286}
287
288// probeOverlay is a TEST_ONLY of the frame: the composed image on the
289// primary plane (Bypass), ov on the overlay with colour mode, the cursor in
290// state cur. Both planes' colour state is written whatever they applied:
291// the test must not rely on the state of the last commit.
292func (o *Output) probeOverlay(fb uint32, ov overlayWin, mode colorMode, cur cursorState) error {
293 req := &o.probeReq // no frame request is alive: the frame commit follows
294 req.reset()
295 o.primaryProps(req, fb)
296 o.primary.colorProps(req, colorBypass, 0, 0)
297 o.overlayProps(req, ov)
298 o.overlay.colorProps(req, mode, o.colorMult, o.ctmBlob)
299 if o.cursor != nil {
300 o.cursor.props(req, o.crtc, cur)
301 }
302 return o.k.commit(req, atomicTestOnly, 0)
303}
304
305// errOverlayDropped: a commit was refused with the overlay on; the frame is
306// composed again without it.
307var errOverlayDropped = errors.New("overlay dropped")
308
309// overlayConflict handles a commit refused with buffer buf on the overlay:
310// the buffer leaves the overlay for good (e.g. the cursor moved where the
311// plane forbids it) and the frame is composed. It reports whether it did.
312func (o *Output) overlayConflict(err error, buf uint64) bool {
313 if buf == 0 || !errors.Is(err, unix.EINVAL) {
314 return false
315 }
316 if cfb := o.clientFBs[buf]; cfb != nil {
317 cfb.overlayFailed = "cursor_conflict"
318 }
319 o.setOverlayReason("cursor_conflict")
320 return true
321}