1package drm
2
3import (
4 "errors"
5 "os"
6
7 "github.com/bnema/neferwl/internal/ports"
8 "golang.org/x/sys/unix"
9)
10
11// Overlay plane: a window drawn from one opaque dmabuf, with nothing but
12// the cursor above it, goes on an overlay plane at its place on screen;
13// the rest of the scene is composed into the primary plane without it.
14// The client buffer is shown with no GPU pass, like direct scanout, but
15// the window need not cover the output. A refusal (TEST_ONLY) keeps the
16// window composed and is cached per buffer.
17
18// pickOverlay takes an overlay plane above the primary one (zpos), when
19// the output has one.
20func (o *Output) pickOverlay() {
21 pz, ok := o.primary.zpos()
22 if !ok {
23 return
24 }
25 for _, p := range o.stray {
26 if p.typ != planeOverlay {
27 continue
28 }
29 if z, ok := p.zpos(); ok && z > pz {
30 o.overlay = p
31 return
32 }
33 }
34}
35
36// zpos is the plane's zpos value when known.
37func (p *plane) zpos() (uint64, bool) {
38 if p.props["zpos"] == 0 {
39 return 0, false
40 }
41 return p.zposValue, true
42}
43
44// overlayWin is the window on the overlay plane: its buffer and where it
45// shows, in physical pixels.
46type overlayWin struct {
47 id ports.WindowID
48 fb uint32
49 buf uint64
50 rect ports.Rect // CRTC rect
51 w, h int // buffer size
52 // color is how the plane's colour pipeline shows the buffer.
53 color colorUse
54 // acquire is the client's explicit-sync fence, a duplicate owned by
55 // the frame loop (closed after the commit); nil without one.
56 acquire *os.File
57}
58
59// overlayCandidate finds the one window the overlay can show: an opaque
60// dmabuf at integer physical coordinates, drawn 1:1, with no other window
61// or layer above it, and the plane colour mode it needs. reason is why none.
62func overlayCandidate(s ports.Scene, surfaces map[ports.WindowID]ports.SurfaceContent, hdrOn bool, pipeline *colorPipeline) (ports.SceneWindow, ports.SurfaceContent, colorMode, string) {
63 if s.Transform != 0 {
64 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "output_transform"
65 }
66 scale := s.Scale
67 if scale <= 0 {
68 scale = 1
69 }
70 var pick *ports.SceneWindow
71 var mode colorMode
72 for i := range s.Windows {
73 w := &s.Windows[i]
74 if w.Hidden || w.Rect.W <= 0 || w.Rect.H <= 0 {
75 continue
76 }
77 if w.Popup {
78 // Popups draw after every window, wherever they are listed.
79 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "window_above"
80 }
81 c := surfaces[w.ID]
82 // A viewport crop is composed: the plane would show the whole
83 // buffer, and so is a colour the plane cannot show (raw PQ values
84 // on an SDR output, SDR on HDR without a pipeline). A dimmed
85 // window (a peeking stashed one) needs the veil drawn over it, and
86 // one with the focus effect needs its lift; an overview preview is
87 // drawn smaller than its buffer.
88 if w.Dim <= 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) {
89 if m, why := planeColor(c.Color, c.DMABuf.Format, c.Opaque, hdrOn, pipeline); why == "" {
90 pick, mode = w, m
91 continue
92 }
93 }
94 if pick != nil {
95 // Something is drawn above the candidate.
96 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "window_above"
97 }
98 }
99 if pick == nil {
100 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "no_candidate"
101 }
102 if s.Dim > 0 {
103 // The renderer composes the veil (under the first float, or under
104 // every window with DimBehind): a window on the plane would skip it.
105 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "dim"
106 }
107 if len(s.DropHints) > 0 {
108 // Drag hints are drawn over the windows.
109 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "drop_hint"
110 }
111 for _, l := range s.Layers {
112 if l.Layer >= ports.LayerTop && l.Rect.W > 0 && l.Rect.H > 0 {
113 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "layer_above"
114 }
115 }
116 if !pick.Fullscreen && len(s.Separators) > 0 {
117 // The overlay shows the buffer alone; the lines would go with it.
118 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "border"
119 }
120 if clip := s.WorkspaceClip; clip != (ports.Rect{}) &&
121 (pick.Rect.X < clip.X || pick.Rect.Y < clip.Y || pick.Rect.X+pick.Rect.W > clip.X+clip.W || pick.Rect.Y+pick.Rect.H > clip.Y+clip.H) {
122 // The plane cannot apply the workspace's logical viewport crop.
123 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "workspace_clip"
124 }
125 c := surfaces[pick.ID]
126 pw, ph := float64(pick.Rect.W)*scale, float64(pick.Rect.H)*scale
127 if float64(c.Width) != pw || float64(c.Height) != ph || c.Geometry != (ports.Rect{}) && (c.Geometry.X != 0 || c.Geometry.Y != 0) {
128 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "scaled"
129 }
130 x, y := float64(pick.Rect.X)*scale, float64(pick.Rect.Y)*scale
131 if x != float64(int(x)) || y != float64(int(y)) {
132 return ports.SceneWindow{}, ports.SurfaceContent{}, colorBypass, "fractional_position"
133 }
134 return *pick, c, mode, ""
135}
136
137// overlayProps puts ov on the overlay plane, or turns it off.
138func (o *Output) overlayProps(req *atomicReq, ov overlayWin) {
139 p := o.overlay
140 if p == nil {
141 return
142 }
143 if ov.fb == 0 {
144 req.set(p.id, p.prop("FB_ID"), 0)
145 req.set(p.id, p.prop("CRTC_ID"), 0)
146 o.overlayColorProps(req, colorBypass)
147 return
148 }
149 o.overlayColorProps(req, ov.color.mode)
150 req.set(p.id, p.prop("FB_ID"), uint64(ov.fb))
151 req.set(p.id, p.prop("CRTC_ID"), uint64(o.crtc))
152 req.set(p.id, p.prop("SRC_X"), 0)
153 req.set(p.id, p.prop("SRC_Y"), 0)
154 req.set(p.id, p.prop("SRC_W"), uint64(ov.w)<<16)
155 req.set(p.id, p.prop("SRC_H"), uint64(ov.h)<<16)
156 req.set(p.id, p.prop("CRTC_X"), uint64(int64(ov.rect.X)))
157 req.set(p.id, p.prop("CRTC_Y"), uint64(int64(ov.rect.Y)))
158 req.set(p.id, p.prop("CRTC_W"), uint64(ov.rect.W))
159 req.set(p.id, p.prop("CRTC_H"), uint64(ov.rect.H))
160 if ov.acquire != nil {
161 // KMS waits for the client's GPU work before scanning it out.
162 req.set(p.id, p.prop("IN_FENCE_FD"), uint64(ov.acquire.Fd()))
163 }
164}
165
166// close releases the frame's duplicate of the acquire fence.
167func (ov overlayWin) close() {
168 if ov.acquire != nil {
169 ov.acquire.Close()
170 }
171}
172
173// testOverlay asks KMS whether it takes the frame with ov on the overlay,
174// the cursor at its current place included. A refusal is cached on the
175// buffer; one that the plane's colour pipeline causes (the same frame passes
176// with Bypass) is cached on the plane for the buffer format and the cursor
177// state instead.
178func (o *Output) testOverlay(fb uint32, ov overlayWin) bool {
179 err := o.probeOverlay(fb, ov, ov.color.mode)
180 if err == nil {
181 return true
182 }
183 if ov.color.mode != colorBypass && refused(err) && o.probeOverlay(fb, ov, colorBypass) == nil {
184 o.overlay.setVerdict(ov.color.format, o.cursorShown(), false)
185 o.setOverlayReason("color_refused")
186 o.renderLog.Info().Err(err).Uint32("format", ov.color.format).Str("connector", o.conn.name).Msg("overlay colour pipeline refused")
187 return false
188 }
189 if cfb := o.clientFBs[ov.buf]; cfb != nil && refused(err) {
190 cfb.overlayFailed = "overlay_refused"
191 }
192 o.setOverlayReason("overlay_refused")
193 o.renderLog.Info().Err(err).Str("connector", o.conn.name).Msg("overlay refused")
194 return false
195}
196
197// probeOverlay is a TEST_ONLY of the frame: the composed image on the
198// primary plane (Bypass), ov on the overlay with colour mode, the cursor.
199// Both planes' colour state is written whatever they applied: the test must
200// not rely on the state of the last commit.
201func (o *Output) probeOverlay(fb uint32, ov overlayWin, mode colorMode) error {
202 req := &o.probeReq // no frame request is alive: the frame commit follows
203 req.reset()
204 o.primaryProps(req, fb)
205 o.primary.colorProps(req, colorBypass, 0, 0)
206 o.overlayProps(req, ov)
207 o.overlay.colorProps(req, mode, o.colorMult, o.ctmBlob)
208 if o.cursor != nil {
209 o.cursor.props(req, o.crtc, o.cursor.desired())
210 }
211 return o.k.commit(req, atomicTestOnly, 0)
212}
213
214// errOverlayDropped: a commit was refused with the overlay on; the frame is
215// composed again without it.
216var errOverlayDropped = errors.New("overlay dropped")
217
218// overlayConflict handles a commit refused with buffer buf on the overlay:
219// the buffer leaves the overlay for good (e.g. the cursor moved where the
220// plane forbids it) and the frame is composed. It reports whether it did.
221func (o *Output) overlayConflict(err error, buf uint64) bool {
222 if buf == 0 || !errors.Is(err, unix.EINVAL) {
223 return false
224 }
225 if cfb := o.clientFBs[buf]; cfb != nil {
226 cfb.overlayFailed = "cursor_conflict"
227 }
228 o.setOverlayReason("cursor_conflict")
229 return true
230}