1package drm
2
3import (
4 "context"
5 "errors"
6 "time"
7
8 "github.com/bnema/neferwl/internal/adapters/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, 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, 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 r.UseTarget(o.back)
93 // Captures never show the capture indicator: their frame goes first.
94 took, pdone, perr := pipeline.SubmitPlain(r, composed, surfaces, normal)
95 if perr != nil {
96 o.holdRead(pdone)
97 if pdone != nil {
98 pdone.Close()
99 }
100 ov.close()
101 return false, renderError{perr}
102 }
103 // Not capture admission: an engage during the render retains its client
104 // reads; SubmitScoped already failed the requests of a stale epoch.
105 if !o.sceneCurrent(scene) || o.protected {
106 o.holdRead(pdone)
107 if pdone != nil {
108 pdone.Close()
109 }
110 ov.close()
111 return false, errSecurityScene
112 }
113 if pdone != nil {
114 pdone.Close()
115 }
116 if took {
117 normal = nil // answered from the frame without the indicator
118 }
119 if len(excluded) > 0 {
120 // The excluded frame goes first to the same target and is copied by
121 // the capture; the displayed frame is drawn over it in queue order.
122 // Cost: a second composition on frames with an excluded request.
123 xdone, xerr := r.Render(pipeline.ExcludedScene(composed), surfaces)
124 if xerr != nil {
125 o.holdRead(xdone)
126 if xdone != nil {
127 xdone.Close()
128 }
129 ov.close()
130 return false, renderError{xerr}
131 }
132 // An excluded composition is not capture admission. Engage may have
133 // happened while Render submitted GPU work; retain its client reads
134 // and leave the request credit with the caller on rejection.
135 if !o.sceneCurrent(scene) || o.protected {
136 o.holdRead(xdone)
137 if xdone != nil {
138 xdone.Close()
139 }
140 ov.close()
141 return false, errSecurityScene
142 }
143 if xdone != nil {
144 xdone.Close()
145 }
146 pipeline.SubmitScoped(scene.Security, r, excluded)
147 // Ownership moved to the worker (or an immediate failure reply).
148 clear(excluded)
149 }
150 done, rerr := r.Render(composed, surfaces)
151 if rerr != nil {
152 ov.close()
153 return false, renderError{rerr}
154 }
155 if ov.fb != 0 && !o.testOverlay(o.fbs[o.back], ov) {
156 // Refused: compose the window too (cached per buffer).
157 if done != nil {
158 done.Close()
159 }
160 ov.close()
161 ov = overlayWin{}
162 if done, rerr = r.Render(scene, surfaces); rerr != nil {
163 return false, renderError{rerr}
164 }
165 }
166 if !o.sceneCurrent(scene) || o.protected {
167 o.holdRead(done)
168 if done != nil {
169 done.Close()
170 }
171 ov.close()
172 return false, errSecurityScene
173 }
174 if len(normal) > 0 {
175 // Scanout and the overlay plane were skipped for this frame.
176 o.log.Debug().Str("connector", o.conn.name).Int("captures", len(normal)).Msg("capture frame composed")
177 pipeline.SubmitScoped(scene.Security, r, normal)
178 // Ownership moved to the worker (or an immediate failure reply).
179 // A later commit panic must not fail the same requests again.
180 clear(normal)
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 // The overlay buffer is on screen like a scanned-out one: it is
191 // reported shown, so it is not released under the plane.
192 f.queued, f.zeroCopy, f.composed = ov.buf, ov.id, true
193 // A covering fullscreen window keeps VRR while composed (overlay
194 // layer, subsurfaces, size mismatch).
195 game := o.vrrProp != 0 && fullscreenShown(&scene)
196 err = o.commitWith(o.fbs[o.back], done, false, o.wantVRR(game), f, ov)
197 presented = err == nil
198 if err != nil {
199 // The GPU may still read client buffers for this frame.
200 o.holdRead(done)
201 }
202 if done != nil {
203 done.Close()
204 }
205 ov.close()
206 if errors.Is(err, errColorRefused) {
207 // Cached for the overlay's buffer format: the next frame composes it.
208 o.setOverlayReason("color_refused")
209 return false, errOverlayDropped
210 }
211 if err != nil && o.overlayConflict(err, ov.buf) {
212 return false, errOverlayDropped
213 }
214 if err == nil {
215 o.queued = ov.buf
216 o.back = 1 - o.back
217 }
218 return false, err
219}