euv-example 0.48.1

Cross-platform native showcase application demonstrating euv fine-grained reactivity, multi-mode rendering, window controls, and euv-ui components.
Documentation
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);
        }
    }
}