1package drm
2
3import (
4 "context"
5 "errors"
6 "time"
7
8 "github.com/bnema/neferwl/internal/adapters/outputkit/capture"
9 "github.com/bnema/neferwl/internal/ports"
10)
11
12// renderError is a frame the renderer failed to draw: the output stops.
13type renderError struct{ err error }
14
15func (e renderError) Error() string { return "render frame: " + e.err.Error() }
16func (e renderError) Unwrap() error { return e.err }
17
18// submitFrame puts one frame of the scene on screen: the fullscreen window
19// scanned out directly when the kernel takes it, else a composed frame with
20// an optional overlay plane, dropped when the kernel refuses it. Captures
21// force composition and are answered from the composed frame; on renderError
22// the caller still owns the requests and must fail them. direct reports scanout.
23//
24// On success the frame is in flight; errOverlayDropped asks for a new frame
25// without the overlay; a renderError stops the output; other errors are
26// commit errors for commitFailed.
27func (o *Output) submitFrame(ctx context.Context, r ports.Renderer, scene ports.Scene, surfaces map[ports.WindowID]ports.SurfaceContent, seen map[ports.WindowID]uint64, requests []ports.CaptureRequest, pipeline *capture.Pipeline) (direct bool, err error) {
28 presented := false // the frame was committed: what it gated is released
29 if !o.sceneCurrent(scene) {
30 return false, errSecurityScene
31 }
32 if o.protected {
33 return false, o.submitProtectedFrame(ctx, r, scene, surfaces, seen)
34 }
35 // shows is what the frame puts on screen: presentation feedback of
36 // windows it does not draw is discarded, not presented.
37 f := pendingFrame{security: scene.Security, animating: scene.Animating, shows: o.shownBy(scene, seen)}
38 o.wantContent(scene, surfaces)
39 // Requests that cannot be served (exclusion of another session, a
40 // workspace that moved) are failed here, never answered from the
41 // displayed frame.
42 // A request whose capture indicator the scene does not show yet is held
43 // (the caller keeps it, see capture.Waiting).
44 ready, _ := pipeline.Hold(scene, requests, time.Now())
45 if len(scene.CaptureIndicators) > 0 {
46 // The captures of this frame are delivered only once the frame that
47 // carries their indicator is committed, whatever the path out (see
48 // capture.Pipeline.EndGate): no failed or dropped frame serves one.
49 pipeline.BeginGate()
50 defer func() {
51 if err == nil && !presented {
52 err = capture.ErrFrameNotPresented // a panic on the way out
53 }
54 pipeline.EndGate(capture.GateVerdict(err))
55 }()
56 }
57 normal, excluded, hidden := pipeline.Split(scene, ready)
58 pipeline.Retire(scene)
59 if len(hidden) > 0 {
60 if !o.sceneCurrent(scene) || o.protected {
61 return false, errSecurityScene
62 }
63 // A hidden workspace: the child renderer draws it, whatever the display
64 // does (scanout included). Failures answer the requests and never stop
65 // the output.
66 pipeline.SubmitHidden(scene, surfaces, hidden)
67 // Publish child holds before queuing a display flip, which may stall
68 // past the stale-report timeout. Do not advance the display's Seen
69 // until its own GPU work finishes.
70 if o.capHidden != nil {
71 o.report(nil, o.reports.Seen())
72 }
73 clear(hidden)
74 }
75 // Displayed-frame captures, Exclude requests and the capture indicator
76 // force composition (scanout and the overlay plane would skip them); the
77 // hidden-workspace child does not.
78 decision := o.decideFrame(scene, surfaces, len(normal) > 0 || len(excluded) > 0 || len(scene.CaptureIndicators) > 0)
79 if !o.sceneCurrent(scene) || o.protected {
80 decision.overlay.close()
81 return false, errSecurityScene
82 }
83 if decision.fb != 0 {
84 c := decision.content
85 if direct, err = o.commitScanoutRect(decision.fb, c, pendingFrame{security: scene.Security, animating: scene.Animating, shows: o.directShownBy(c.ID, seen[c.ID])}, decision.rect, decision.color); direct {
86 presented = err == nil
87 return true, err
88 }
89 decision = o.composeFrame(scene, surfaces)
90 }
91 ov, composed := decision.overlay, decision.composed
92 // A render that was not committed may still read client buffers: only a
93 // new render's fence orders this flip after it, so the frame composes.
94 if ov.fb != 0 && len(normal) == 0 && len(excluded) == 0 && o.readDone() {
95 if front := o.fbs[1-o.back]; o.front.holds(front, composed, surfaces) {
96 // Only the overlay window changed: the image on screen already
97 // holds the rest.
98 if o.testOverlay(front, ov) {
99 err = o.commitOverlayOnly(front, scene, f, ov)
100 presented = err == nil
101 return false, err
102 }
103 // Refused: compose the window too (cached per buffer).
104 ov.close()
105 ov, composed = overlayWin{}, scene
106 }
107 }
108 r.UseTarget(o.back)
109 // Captures never show the capture indicator: their frame goes first.
110 took, pdone, perr := pipeline.SubmitPlain(r, composed, surfaces, normal)
111 if perr != nil {
112 o.holdRead(pdone)
113 if pdone != nil {
114 pdone.Close()
115 }
116 ov.close()
117 return false, renderError{perr}
118 }
119 // Not capture admission: an engage during the render retains its client
120 // reads; SubmitScoped already failed the requests of a stale epoch.
121 if !o.sceneCurrent(scene) || o.protected {
122 o.holdRead(pdone)
123 if pdone != nil {
124 pdone.Close()
125 }
126 ov.close()
127 return false, errSecurityScene
128 }
129 if pdone != nil {
130 pdone.Close()
131 }
132 if took {
133 normal = nil // answered from the frame without the indicator
134 }
135 if len(excluded) > 0 {
136 // The excluded frame goes first to the same target and is copied by
137 // the capture; the displayed frame is drawn over it in queue order.
138 // Cost: a second composition on frames with an excluded request.
139 xdone, xerr := r.Render(pipeline.ExcludedScene(composed), surfaces)
140 if xerr != nil {
141 o.holdRead(xdone)
142 if xdone != nil {
143 xdone.Close()
144 }
145 ov.close()
146 return false, renderError{xerr}
147 }
148 // An excluded composition is not capture admission. Engage may have
149 // happened while Render submitted GPU work; retain its client reads
150 // and leave the request credit with the caller on rejection.
151 if !o.sceneCurrent(scene) || o.protected {
152 o.holdRead(xdone)
153 if xdone != nil {
154 xdone.Close()
155 }
156 ov.close()
157 return false, errSecurityScene
158 }
159 if xdone != nil {
160 xdone.Close()
161 }
162 pipeline.SubmitScoped(scene.Security, r, excluded)
163 // Ownership moved to the worker (or an immediate failure reply).
164 clear(excluded)
165 }
166 done, rerr := r.Render(composed, surfaces)
167 if rerr != nil {
168 ov.close()
169 return false, renderError{rerr}
170 }
171 if ov.fb != 0 && !o.testOverlay(o.fbs[o.back], ov) {
172 // Refused: compose the window too (cached per buffer).
173 if done != nil {
174 done.Close()
175 }
176 ov.close()
177 ov = overlayWin{}
178 if done, rerr = r.Render(scene, surfaces); rerr != nil {
179 return false, renderError{rerr}
180 }
181 }
182 if !o.sceneCurrent(scene) || o.protected {
183 o.holdRead(done)
184 if done != nil {
185 done.Close()
186 }
187 ov.close()
188 return false, errSecurityScene
189 }
190 if len(normal) > 0 {
191 // Scanout and the overlay plane were skipped for this frame.
192 o.log.Debug().Str("connector", o.conn.name).Int("captures", len(normal)).Msg("capture frame composed")
193 pipeline.SubmitScoped(scene.Security, r, normal)
194 // Ownership moved to the worker (or an immediate failure reply).
195 // A later commit panic must not fail the same requests again.
196 clear(normal)
197 }
198 if !o.sceneCurrent(scene) || o.protected {
199 o.holdRead(done)
200 if done != nil {
201 done.Close()
202 }
203 ov.close()
204 return false, errSecurityScene
205 }
206 // The overlay buffer is on screen like a scanned-out one: it is
207 // reported shown, so it is not released under the plane.
208 f.queued, f.zeroCopy, f.composed = ov.buf, ov.id, true
209 // A covering fullscreen window keeps VRR while composed (overlay
210 // layer, subsurfaces, size mismatch).
211 game := o.vrrProp != 0 && fullscreenShown(&scene)
212 err = o.commitWith(o.fbs[o.back], done, false, o.wantVRR(game), f, ov)
213 presented = err == nil
214 if err != nil {
215 // The GPU may still read client buffers for this frame.
216 o.holdRead(done)
217 }
218 if done != nil {
219 done.Close()
220 }
221 ov.close()
222 if err = o.finishOverlayCommit(err, ov.buf); err == nil {
223 // The image now holds what was drawn last: composed, or the whole
224 // scene after an overlay refusal.
225 if ov.fb != 0 {
226 o.front.hold(o.fbs[o.back], composed, surfaces)
227 } else {
228 o.front.hold(o.fbs[o.back], scene, surfaces)
229 }
230 o.back = 1 - o.back
231 }
232 return false, err
233}
234
235// commitOverlayOnly commits front, the image on screen, again with ov on
236// the overlay plane: no render, no GPU fence. It closes ov's fence.
237func (o *Output) commitOverlayOnly(front uint32, scene ports.Scene, f pendingFrame, ov overlayWin) error {
238 defer ov.close()
239 if !o.sceneCurrent(scene) || o.protected {
240 return errSecurityScene
241 }
242 f.queued, f.zeroCopy = ov.buf, ov.id
243 game := o.vrrProp != 0 && fullscreenShown(&scene)
244 return o.finishOverlayCommit(o.commitWith(front, nil, false, o.wantVRR(game), f, ov), ov.buf)
245}
246
247// finishOverlayCommit applies the result of a frame commit with buf on the
248// overlay (0: none): a refusal the overlay causes asks for a frame without
249// it (errOverlayDropped); a commit marks buf queued.
250func (o *Output) finishOverlayCommit(err error, buf uint64) error {
251 if errors.Is(err, errColorRefused) {
252 // Cached for the overlay's buffer format: the next frame composes it.
253 o.setOverlayReason("color_refused")
254 return errOverlayDropped
255 }
256 if err != nil && o.overlayConflict(err, buf) {
257 return errOverlayDropped
258 }
259 if err == nil {
260 o.queued = buf
261 }
262 return err
263}