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