Skip to main content

euv_example/app/
impl.rs

1use super::*;
2
3/// Context-family and CPU-mirror helpers for `ShowcaseWebPresentationPath`.
4impl ShowcaseWebPresentationPath {
5    /// Returns the locked HTML5 `<canvas>` context family required by this presentation path.
6    ///
7    /// # Returns
8    ///
9    /// - `ShowcaseWebCanvasContextFamily` - `WebGl2` for `GpuDirect`/`GpuUpload`, or `Canvas2d` for `Canvas2d`.
10    #[must_use]
11    pub fn context_family(self) -> ShowcaseWebCanvasContextFamily {
12        match self {
13            Self::GpuDirect | Self::GpuUpload => ShowcaseWebCanvasContextFamily::WebGl2,
14            Self::Canvas2d => ShowcaseWebCanvasContextFamily::Canvas2d,
15        }
16    }
17
18    /// Returns whether this presentation path maintains a CPU software framebuffer mirror.
19    ///
20    /// # Returns
21    ///
22    /// - `bool` - `false` for `GpuDirect`; `true` for `GpuUpload` and `Canvas2d`.
23    #[must_use]
24    pub fn uses_cpu_mirror(self) -> bool {
25        self != Self::GpuDirect
26    }
27}
28
29/// Default initialization for `ShowcaseWebDualCanvasCoordinator` starting on WebGL2 `GpuUpload`.
30impl Default for ShowcaseWebDualCanvasCoordinator {
31    /// Creates a dual-canvas coordinator with a single visible WebGL2 foreground canvas.
32    ///
33    /// # Returns
34    ///
35    /// - `Self` - Initialized coordinator with honest `SubmissionProxy` timing.
36    fn default() -> Self {
37        Self::with_initial_path(ShowcaseWebPresentationPath::GpuUpload, 1200, 800)
38    }
39}
40
41/// Independent-canvas pre-warming, atomic frame-boundary handoff, and proxy telemetry methods for `ShowcaseWebDualCanvasCoordinator`.
42impl ShowcaseWebDualCanvasCoordinator {
43    /// Creates a coordinator starting with `initial_path` on canvas ID `1` (`z_index = 1`, `visible = true`).
44    ///
45    /// # Arguments
46    ///
47    /// - `ShowcaseWebPresentationPath` - Initial presentation path.
48    /// - `u32` - Initial physical width in pixels.
49    /// - `u32` - Initial physical height in pixels.
50    ///
51    /// # Returns
52    ///
53    /// - `Self` - Initialized dual-canvas coordinator.
54    #[must_use]
55    pub fn with_initial_path(
56        initial_path: ShowcaseWebPresentationPath,
57        width: u32,
58        height: u32,
59    ) -> Self {
60        let active: ShowcaseWebCanvasSlot = ShowcaseWebCanvasSlot {
61            canvas_id: 1,
62            context_family: initial_path.context_family(),
63            path: initial_path,
64            visible: true,
65            z_index: 1,
66            width: width.max(1),
67            height: height.max(1),
68            scene_version: 0,
69            ready: true,
70        };
71        Self {
72            active,
73            standby: None,
74            cpu_mirror_active: initial_path.uses_cpu_mirror(),
75            timing_source: ShowcaseWebTimingSource::SubmissionProxy,
76            next_canvas_id: 2,
77            completed_swaps: 0,
78            aborted_swaps: 0,
79            raf_proxy_ticks: 0,
80            last_upload_bytes: 0,
81            retired_canvas_ids: Vec::new(),
82        }
83    }
84
85    /// Returns the currently pending hidden standby `<canvas>` slot, if any.
86    ///
87    /// # Returns
88    ///
89    /// - `Option<ShowcaseWebCanvasSlot>` - Pending hidden standby slot.
90    #[must_use]
91    pub fn standby_slot(&self) -> Option<ShowcaseWebCanvasSlot> {
92        let Self { standby, .. } = self;
93        *standby
94    }
95
96    /// Begins switching to `target_path`, allocating an independent hidden sibling `<canvas>` whenever switching context families (`WebGl2` <-> `Canvas2d`).
97    ///
98    /// # Arguments
99    ///
100    /// - `ShowcaseWebPresentationPath` - Candidate presentation path.
101    /// - `u32` - Target physical width in pixels.
102    /// - `u32` - Target physical height in pixels.
103    ///
104    /// # Returns
105    ///
106    /// - `bool` - `true` when an independent hidden standby `<canvas>` was created; `false` when switched in-place within the same context family.
107    pub fn begin_switch(
108        &mut self,
109        target_path: ShowcaseWebPresentationPath,
110        width: u32,
111        height: u32,
112    ) -> bool {
113        let target_family: ShowcaseWebCanvasContextFamily = target_path.context_family();
114        if self.get_active().get_context_family() == target_family {
115            let mut active: ShowcaseWebCanvasSlot = self.get_active();
116            active.set_path(target_path);
117            active.set_width(width.max(1));
118            active.set_height(height.max(1));
119            self.set_active(active);
120            self.set_cpu_mirror_active(target_path.uses_cpu_mirror());
121            if !target_path.uses_cpu_mirror() {
122                self.set_last_upload_bytes(0);
123            }
124            return false;
125        }
126        let next_id: u64 = self.get_next_canvas_id();
127        self.set_next_canvas_id(next_id.saturating_add(1));
128        let slot: ShowcaseWebCanvasSlot = ShowcaseWebCanvasSlot {
129            canvas_id: next_id,
130            context_family: target_family,
131            path: target_path,
132            visible: false,
133            z_index: 0,
134            width: width.max(1),
135            height: height.max(1),
136            scene_version: 0,
137            ready: false,
138        };
139        let Self {
140            standby,
141            retired_canvas_ids,
142            ..
143        } = self;
144        if let Some(prev_standby) = standby.replace(slot) {
145            retired_canvas_ids.push(prev_standby.get_canvas_id());
146        }
147        true
148    }
149
150    /// Marks the hidden standby `<canvas>` as pre-warmed and ready for atomic swap at `(width, height, scene_version)`.
151    ///
152    /// # Arguments
153    ///
154    /// - `u32` - Rendered candidate physical width.
155    /// - `u32` - Rendered candidate physical height.
156    /// - `u64` - Rendered candidate scene revision.
157    ///
158    /// # Returns
159    ///
160    /// - `bool` - `true` if a standby slot was present and marked ready.
161    pub fn mark_standby_ready(&mut self, width: u32, height: u32, scene_version: u64) -> bool {
162        let Self { standby, .. } = self;
163        if let Some(slot) = standby.as_mut() {
164            slot.set_width(width.max(1));
165            slot.set_height(height.max(1));
166            slot.set_scene_version(scene_version);
167            slot.set_ready(true);
168            true
169        } else {
170            false
171        }
172    }
173
174    /// 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.
175    ///
176    /// # Arguments
177    ///
178    /// - `u32` - Expected physical width in pixels.
179    /// - `u32` - Expected physical height in pixels.
180    /// - `u64` - Expected scene revision.
181    ///
182    /// # Returns
183    ///
184    /// - `bool` - `true` when swap committed; `false` when standby was missing or stale (aborting handoff and keeping old canvas active).
185    pub fn commit_at_frame_boundary(
186        &mut self,
187        expected_width: u32,
188        expected_height: u32,
189        expected_scene_version: u64,
190    ) -> bool {
191        let matches_frame: bool = self
192            .standby_slot()
193            .map(|s: ShowcaseWebCanvasSlot| {
194                s.get_ready()
195                    && s.get_width() == expected_width.max(1)
196                    && s.get_height() == expected_height.max(1)
197                    && s.get_scene_version() == expected_scene_version
198            })
199            .unwrap_or(false);
200        if !matches_frame {
201            self.abort_switch();
202            return false;
203        }
204        let Some(mut next_active) = ({
205            let Self { standby, .. } = self;
206            standby.take()
207        }) else {
208            return false;
209        };
210        let old_id: u64 = self.get_active().get_canvas_id();
211        next_active.set_visible(true);
212        next_active.set_z_index(1);
213        let uses_mirror: bool = next_active.get_path().uses_cpu_mirror();
214        self.set_active(next_active);
215        self.set_cpu_mirror_active(uses_mirror);
216        self.set_timing_source(ShowcaseWebTimingSource::SubmissionProxy);
217        if !uses_mirror {
218            self.set_last_upload_bytes(0);
219        }
220        self.set_completed_swaps(self.get_completed_swaps().saturating_add(1));
221        self.get_mut_retired_canvas_ids().push(old_id);
222        true
223    }
224
225    /// Aborts any pending standby handoff without disturbing the visible active `<canvas>`.
226    pub fn abort_switch(&mut self) {
227        let prev_standby: Option<ShowcaseWebCanvasSlot> = {
228            let Self { standby, .. } = self;
229            standby.take()
230        };
231        if let Some(dropped) = prev_standby {
232            self.get_mut_retired_canvas_ids()
233                .push(dropped.get_canvas_id());
234            self.set_aborted_swaps(self.get_aborted_swaps().saturating_add(1));
235        }
236    }
237
238    /// Records a browser `requestAnimationFrame` callback strictly as `ShowcaseWebTimingSource::SubmissionProxy`.
239    ///
240    /// # Arguments
241    ///
242    /// - `u64` - Framebuffer bytes uploaded on this tick (`0` for `GpuDirect`).
243    pub fn record_raf_proxy_tick(&mut self, upload_bytes: u64) {
244        self.set_timing_source(ShowcaseWebTimingSource::SubmissionProxy);
245        self.set_raf_proxy_ticks(self.get_raf_proxy_ticks().saturating_add(1));
246        if self.get_cpu_mirror_active() {
247            self.set_last_upload_bytes(upload_bytes);
248        } else {
249            self.set_last_upload_bytes(0);
250        }
251    }
252}