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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use super::*;
/// Context-family and CPU-mirror helpers for `ShowcaseWebPresentationPath`.
impl ShowcaseWebPresentationPath {
/// Returns the locked HTML5 `<canvas>` context family required by this presentation path.
///
/// # Returns
///
/// - `ShowcaseWebCanvasContextFamily` - `WebGl2` for `GpuDirect`/`GpuUpload`, or `Canvas2d` for `Canvas2d`.
#[must_use]
pub fn context_family(self) -> ShowcaseWebCanvasContextFamily {
match self {
Self::GpuDirect | Self::GpuUpload => ShowcaseWebCanvasContextFamily::WebGl2,
Self::Canvas2d => ShowcaseWebCanvasContextFamily::Canvas2d,
}
}
/// Returns whether this presentation path maintains a CPU software framebuffer mirror.
///
/// # Returns
///
/// - `bool` - `false` for `GpuDirect`; `true` for `GpuUpload` and `Canvas2d`.
#[must_use]
pub fn uses_cpu_mirror(self) -> bool {
self != Self::GpuDirect
}
}
/// Default initialization for `ShowcaseWebDualCanvasCoordinator` starting on WebGL2 `GpuUpload`.
impl Default for ShowcaseWebDualCanvasCoordinator {
/// Creates a dual-canvas coordinator with a single visible WebGL2 foreground canvas.
///
/// # Returns
///
/// - `Self` - Initialized coordinator with honest `SubmissionProxy` timing.
fn default() -> Self {
Self::with_initial_path(ShowcaseWebPresentationPath::GpuUpload, 1200, 800)
}
}
/// Independent-canvas pre-warming, atomic frame-boundary handoff, and proxy telemetry methods for `ShowcaseWebDualCanvasCoordinator`.
impl ShowcaseWebDualCanvasCoordinator {
/// Creates a coordinator starting with `initial_path` on canvas ID `1` (`z_index = 1`, `visible = true`).
///
/// # Arguments
///
/// - `ShowcaseWebPresentationPath` - Initial presentation path.
/// - `u32` - Initial physical width in pixels.
/// - `u32` - Initial physical height in pixels.
///
/// # Returns
///
/// - `Self` - Initialized dual-canvas coordinator.
#[must_use]
pub fn with_initial_path(
initial_path: ShowcaseWebPresentationPath,
width: u32,
height: u32,
) -> Self {
let active: ShowcaseWebCanvasSlot = ShowcaseWebCanvasSlot {
canvas_id: 1,
context_family: initial_path.context_family(),
path: initial_path,
visible: true,
z_index: 1,
width: width.max(1),
height: height.max(1),
scene_version: 0,
ready: true,
};
Self {
active,
standby: None,
cpu_mirror_active: initial_path.uses_cpu_mirror(),
timing_source: ShowcaseWebTimingSource::SubmissionProxy,
next_canvas_id: 2,
completed_swaps: 0,
aborted_swaps: 0,
raf_proxy_ticks: 0,
last_upload_bytes: 0,
retired_canvas_ids: Vec::new(),
}
}
/// Returns the currently pending hidden standby `<canvas>` slot, if any.
///
/// # Returns
///
/// - `Option<ShowcaseWebCanvasSlot>` - Pending hidden standby slot.
#[must_use]
pub fn standby_slot(&self) -> Option<ShowcaseWebCanvasSlot> {
let Self { standby, .. } = self;
*standby
}
/// Begins switching to `target_path`, allocating an independent hidden sibling `<canvas>` whenever switching context families (`WebGl2` <-> `Canvas2d`).
///
/// # Arguments
///
/// - `ShowcaseWebPresentationPath` - Candidate presentation path.
/// - `u32` - Target physical width in pixels.
/// - `u32` - Target physical height in pixels.
///
/// # Returns
///
/// - `bool` - `true` when an independent hidden standby `<canvas>` was created; `false` when switched in-place within the same context family.
pub fn begin_switch(
&mut self,
target_path: ShowcaseWebPresentationPath,
width: u32,
height: u32,
) -> bool {
let target_family: ShowcaseWebCanvasContextFamily = target_path.context_family();
if self.get_active().get_context_family() == target_family {
let mut active: ShowcaseWebCanvasSlot = self.get_active();
active.set_path(target_path);
active.set_width(width.max(1));
active.set_height(height.max(1));
self.set_active(active);
self.set_cpu_mirror_active(target_path.uses_cpu_mirror());
if !target_path.uses_cpu_mirror() {
self.set_last_upload_bytes(0);
}
return false;
}
let next_id: u64 = self.get_next_canvas_id();
self.set_next_canvas_id(next_id.saturating_add(1));
let slot: ShowcaseWebCanvasSlot = ShowcaseWebCanvasSlot {
canvas_id: next_id,
context_family: target_family,
path: target_path,
visible: false,
z_index: 0,
width: width.max(1),
height: height.max(1),
scene_version: 0,
ready: false,
};
let Self {
standby,
retired_canvas_ids,
..
} = self;
if let Some(prev_standby) = standby.replace(slot) {
retired_canvas_ids.push(prev_standby.get_canvas_id());
}
true
}
/// Marks the hidden standby `<canvas>` as pre-warmed and ready for atomic swap at `(width, height, scene_version)`.
///
/// # Arguments
///
/// - `u32` - Rendered candidate physical width.
/// - `u32` - Rendered candidate physical height.
/// - `u64` - Rendered candidate scene revision.
///
/// # Returns
///
/// - `bool` - `true` if a standby slot was present and marked ready.
pub fn mark_standby_ready(&mut self, width: u32, height: u32, scene_version: u64) -> bool {
let Self { standby, .. } = self;
if let Some(slot) = standby.as_mut() {
slot.set_width(width.max(1));
slot.set_height(height.max(1));
slot.set_scene_version(scene_version);
slot.set_ready(true);
true
} else {
false
}
}
/// Atomically swaps visibility and `z_index` at a frame boundary once the standby `<canvas>` matches `(expected_width, expected_height, expected_scene_version)`, retiring the old canvas only after commit.
///
/// # Arguments
///
/// - `u32` - Expected physical width in pixels.
/// - `u32` - Expected physical height in pixels.
/// - `u64` - Expected scene revision.
///
/// # Returns
///
/// - `bool` - `true` when swap committed; `false` when standby was missing or stale (aborting handoff and keeping old canvas active).
pub fn commit_at_frame_boundary(
&mut self,
expected_width: u32,
expected_height: u32,
expected_scene_version: u64,
) -> bool {
let matches_frame: bool = self
.standby_slot()
.map(|s: ShowcaseWebCanvasSlot| {
s.get_ready()
&& s.get_width() == expected_width.max(1)
&& s.get_height() == expected_height.max(1)
&& s.get_scene_version() == expected_scene_version
})
.unwrap_or(false);
if !matches_frame {
self.abort_switch();
return false;
}
let Some(mut next_active) = ({
let Self { standby, .. } = self;
standby.take()
}) else {
return false;
};
let old_id: u64 = self.get_active().get_canvas_id();
next_active.set_visible(true);
next_active.set_z_index(1);
let uses_mirror: bool = next_active.get_path().uses_cpu_mirror();
self.set_active(next_active);
self.set_cpu_mirror_active(uses_mirror);
self.set_timing_source(ShowcaseWebTimingSource::SubmissionProxy);
if !uses_mirror {
self.set_last_upload_bytes(0);
}
self.set_completed_swaps(self.get_completed_swaps().saturating_add(1));
self.get_mut_retired_canvas_ids().push(old_id);
true
}
/// Aborts any pending standby handoff without disturbing the visible active `<canvas>`.
pub fn abort_switch(&mut self) {
let prev_standby: Option<ShowcaseWebCanvasSlot> = {
let Self { standby, .. } = self;
standby.take()
};
if let Some(dropped) = prev_standby {
self.get_mut_retired_canvas_ids()
.push(dropped.get_canvas_id());
self.set_aborted_swaps(self.get_aborted_swaps().saturating_add(1));
}
}
/// Records a browser `requestAnimationFrame` callback strictly as `ShowcaseWebTimingSource::SubmissionProxy`.
///
/// # Arguments
///
/// - `u64` - Framebuffer bytes uploaded on this tick (`0` for `GpuDirect`).
pub fn record_raf_proxy_tick(&mut self, upload_bytes: u64) {
self.set_timing_source(ShowcaseWebTimingSource::SubmissionProxy);
self.set_raf_proxy_ticks(self.get_raf_proxy_ticks().saturating_add(1));
if self.get_cpu_mirror_active() {
self.set_last_upload_bytes(upload_bytes);
} else {
self.set_last_upload_bytes(0);
}
}
}