cranpose_render_wgpu/frame_packet.rs
1use cranpose_render_common::scene_store::SceneVersion;
2
3use crate::{
4 collect::{ChildLayer, LayerScene},
5 frame::{Damage, DamageRects, PacketDamage},
6 scene::{
7 BackdropLayer, CompositorScene, DrawOp, EffectLayer, ImageDraw, RunDraw, ShadowDraw,
8 TextDraw,
9 },
10 target_state::TargetDrawn,
11};
12
13pub(crate) const MAX_FRAMES_IN_FLIGHT: usize = 2;
14
15/// Why the present stage refused a packet without drawing it. Each reason
16/// names the expectation the packet no longer matches; the frame is not an
17/// error, its scene travels back through `RenderReturns` for recycling.
18/// `pub` (not `pub(crate)`) because the cancellation-protocol tests in
19/// `tests/` observe outcomes through `#[doc(hidden)]` hooks; the module is
20/// private, so the only public path is the hidden re-export in `lib.rs`.
21#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22pub enum CancelReason {
23 /// The packet was built against a different `GpuRenderer` instance.
24 RendererEpoch,
25 /// The packet was built against a different surface configuration.
26 SurfaceEpoch,
27 /// The packet was lowered for a different surface size.
28 Viewport,
29 /// The present stage had no usable surface to draw the packet on: the
30 /// surface was dropped, or acquire failed past its one reconfigure
31 /// retry. Producer state (epochs, viewport) still matched, only the
32 /// swapchain was missing.
33 SurfaceUnavailable,
34 /// The device reported an uncaptured error (validation/OOM/internal)
35 /// since the previous frame: nothing of this packet is encoded on the
36 /// suspect device. One cancel per poisoning; the gate clears as it
37 /// fires, so the next packet renders (see `DeviceErrorSentry`).
38 DeviceError,
39 /// The device reported its loss: nothing is encoded on it until the
40 /// renderer moves to a new device.
41 DeviceLost,
42 /// The packet holds the draws of a part of the window, and the image it
43 /// went into needs more: the next packet holds every draw.
44 NeedsWhole,
45}
46
47/// What the present stage did with a packet. `NotRun` is the `Default` so a
48/// draw that never happened can never be reported `Presented`.
49/// `pub` like [`CancelReason`], for the same hidden test re-export.
50#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
51pub enum PresentOutcome {
52 /// No packet was consumed (the default of an untouched returns value).
53 #[default]
54 NotRun,
55 /// The packet was drawn and presented.
56 Presented,
57 /// The packet drew what the surface showed: no image, draw or present.
58 Unchanged,
59 /// The packet was refused before any encoding; the scene returned whole.
60 Cancelled(CancelReason),
61}
62
63pub(crate) struct FramePacket {
64 pub(crate) frame_id: u64,
65 pub(crate) viewport: (u32, u32),
66 pub(crate) renderer_epoch: u64,
67 pub(crate) surface_epoch: u64,
68 pub(crate) root_scale: f32,
69 pub(crate) root: Box<LayerScene>,
70 pub(crate) overlay: Option<Box<LayerScene>>,
71 pub(crate) clear: wgpu::Color,
72 // The generation of the debug toggles at the build. The present side
73 // compares frames by the value the producer culled by, so a toggle
74 // flipped before the draw refuses no packet.
75 pub(crate) debug_toggles: u64,
76 // Whether a pointer was held on the app's controls for this frame.
77 pub(crate) pointer_held: bool,
78 // The scene update the packet was collected from: a frame drawn from the
79 // update the frame before drew shows nothing new of the app's.
80 pub(crate) scene_update: SceneVersion,
81 // What the frame changes against the packet before it.
82 pub(crate) damage: PacketDamage,
83 // Whether the frame was one to skip working out what it changes, while
84 // frames keep changing most of the window.
85 pub(crate) skipped: bool,
86 // The window rects the packet holds every draw within; `None` when it
87 // holds every draw of the scene.
88 pub(crate) culled: Option<DamageRects>,
89}
90
91impl FramePacket {
92 // How the packet's scene moved since the frame that drew `drawn`, the
93 // scene update before.
94 pub(crate) fn scene_motion(&self, drawn: Option<SceneVersion>) -> SceneMotion {
95 if self.pointer_held {
96 SceneMotion::Held
97 } else if drawn == Some(self.scene_update) {
98 SceneMotion::Still
99 } else {
100 SceneMotion::Moving
101 }
102 }
103
104 // Whether a backdrop of the packet reads the page beneath it.
105 pub(crate) fn reads_page(&self) -> bool {
106 self.root.contains_backdrop()
107 || self
108 .overlay
109 .as_ref()
110 .is_some_and(|overlay| overlay.contains_backdrop())
111 }
112}
113
114// How a frame's scene moved since the frame before, which on WebGL decides
115// whether a paced build runs at its end (see `PipelineCompiler::paced`).
116#[derive(Clone, Copy, Debug, PartialEq, Eq)]
117pub(crate) enum SceneMotion {
118 // The frame draws the scene update the frame before drew, with no
119 // pointer held.
120 Still,
121 // The scene changed, with no pointer held.
122 Moving,
123 // A pointer is held on the app's controls.
124 Held,
125}
126
127pub(crate) struct FrameSceneStorage {
128 pub(crate) root: Box<LayerScene>,
129 pub(crate) overlay: Option<Box<LayerScene>>,
130}
131
132/// Present-stage timestamps for one consumed packet, in nanoseconds on the
133/// clock the producer injected at runtime start (`0` = stage did not run or
134/// no clock was injected). Carried back in `RenderReturns` so the
135/// producer's frame telemetry keeps recording acquire/render/present phases
136/// after those stages move to the present thread. Plain integers so the
137/// packet types stay clock-library-free on every target.
138#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
139pub struct PresentTimings {
140 /// When the present stage had acquired the surface texture.
141 pub after_acquire_ns: i64,
142 /// When the present stage had rendered the frame's draws.
143 pub after_render_ns: i64,
144 /// When the present stage had presented the frame to the surface.
145 pub after_present_ns: i64,
146}
147
148impl RenderReturns {
149 // What a target that keeps its frames returns for `packet` once it drew
150 // the packet's damage over the frame before.
151 pub(crate) fn drawn_in_place(packet: FramePacket) -> Self {
152 Self {
153 target: Some(TargetDrawn {
154 damage: packet.damage.damage,
155 skipped: packet.skipped,
156 knew_before: packet.damage.since.is_some(),
157 own_sake: None,
158 }),
159 held: Some(HeldWindow {
160 oldest: packet.frame_id,
161 stand_ins: Damage::NONE,
162 landings: None,
163 }),
164 scene: Some(FrameSceneStorage {
165 root: packet.root,
166 overlay: packet.overlay,
167 }),
168 frame_id: packet.frame_id,
169 ..Self::default()
170 }
171 }
172}
173
174// The oldest frame a window's targets hold, the parts of their frames drawn
175// with stand-ins, and the landings counted when those were drawn, `None`
176// when they were drawn at different counts: a window frame draws the
177// damages since that frame and, after a landing, those parts again, or
178// whole when a frame may show one anywhere.
179#[derive(Clone, Copy, Debug, PartialEq)]
180pub(crate) struct HeldWindow {
181 pub(crate) oldest: u64,
182 pub(crate) stand_ins: Damage,
183 pub(crate) landings: Option<u64>,
184}
185
186#[derive(Default)]
187pub(crate) struct RenderReturns {
188 pub(crate) scene: Option<FrameSceneStorage>,
189 pub(crate) frame_id: u64,
190 pub(crate) outcome: PresentOutcome,
191 // How the frame went into a target that keeps its frames; `None` for a
192 // frame drawn into a texture that keeps none.
193 pub(crate) target: Option<TargetDrawn>,
194 // The oldest packet the window's next frame finds in its target, and
195 // the parts it draws again; `None` when a target holds none known.
196 pub(crate) held: Option<HeldWindow>,
197 #[cfg(not(target_arch = "wasm32"))]
198 pub(crate) timings: PresentTimings,
199}
200
201const _: () = {
202 const fn assert_send<T: Send>() {}
203 assert_send::<FramePacket>();
204 assert_send::<FrameSceneStorage>();
205 assert_send::<LayerScene>();
206 assert_send::<ChildLayer>();
207 assert_send::<CompositorScene>();
208 assert_send::<RunDraw>();
209 assert_send::<ImageDraw>();
210 assert_send::<TextDraw>();
211 assert_send::<ShadowDraw>();
212 assert_send::<DrawOp>();
213 assert_send::<EffectLayer>();
214 assert_send::<BackdropLayer>();
215 assert_send::<RenderReturns>();
216 assert_send::<CancelReason>();
217 assert_send::<PresentOutcome>();
218 assert_send::<PresentTimings>();
219};