euv-core 0.48.1

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Default rendering configuration (`Auto` hardware-first, `VSync`, `AnalyticalSdf`, uncapped FPS, `max_frames_in_flight = 1`).
impl Default for RenderConfig {
    /// Creates the recommended cross-platform `RenderConfig`.
    ///
    /// # Returns
    ///
    /// - `Self` - Default `RenderConfig`.
    fn default() -> Self {
        Self {
            mode: RenderMode::Auto,
            preferred_backend: None,
            present_mode: PresentMode::VSync,
            antialiasing: AntialiasingMode::AnalyticalSdf,
            color_space: ColorSpace::Srgb,
            dirty_rect_optimization: true,
            tile_size: DEFAULT_TILE_SIZE,
            max_fps: DEFAULT_MAX_FPS,
            max_frames_in_flight: DEFAULT_MAX_FRAMES_IN_FLIGHT,
            hdr: false,
            direct_gpu_present: false,
            adaptive_switch_mode: AdaptiveSwitchMode::RecommendOnly,
        }
    }
}

/// Fluent builder helpers for `RenderConfig`.
impl RenderConfig {
    /// Creates a software-rendering configuration.
    ///
    /// # Returns
    ///
    /// - `Self` - Software-mode `RenderConfig`.
    #[must_use]
    pub fn software() -> Self {
        let mut cfg: Self = Self::default();
        cfg.set_mode(RenderMode::Software);
        cfg.set_preferred_backend(Some(GraphicsBackend::SoftwareCpu));
        cfg.set_direct_gpu_present(false);
        cfg
    }

    /// Creates an explicit deterministic CPU software reference configuration for headless and visual-parity tests.
    ///
    /// # Returns
    ///
    /// - `Self` - Deterministic CPU reference `RenderConfig`.
    #[must_use]
    pub fn software_reference() -> Self {
        Self::software()
    }

    /// Creates a hardware GPU rendering configuration.
    ///
    /// # Returns
    ///
    /// - `Self` - Hardware-mode `RenderConfig`.
    #[must_use]
    pub fn hardware() -> Self {
        let mut cfg: Self = Self::default();
        cfg.set_mode(RenderMode::Hardware);
        cfg
    }

    /// Creates a hardware GPU direct-draw configuration skipping steady-state CPU RGBA mirror generation.
    ///
    /// # Returns
    ///
    /// - `Self` - Direct-draw hardware-mode `RenderConfig`.
    #[must_use]
    pub fn hardware_direct() -> Self {
        let mut cfg: Self = Self::hardware();
        cfg.set_direct_gpu_present(true);
        cfg
    }

    /// Configures whether `Hardware` mode renders directly to the GPU without steady-state CPU RGBA readback.
    ///
    /// # Arguments
    ///
    /// - `Self` - Base render configuration.
    /// - `bool` - `true` to enable direct GPU presentation with zero steady-state CPU RGBA mirroring.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated render configuration.
    #[must_use]
    pub fn with_direct_gpu_present(mut self, enabled: bool) -> Self {
        self.set_direct_gpu_present(enabled);
        self
    }

    /// Configures an optional explicit frame-rate cap (`None` = uncapped hardware VSync).
    ///
    /// # Arguments
    ///
    /// - `Self` - Base render configuration.
    /// - `Option<u32>` - Optional explicit FPS cap, or `None` for uncapped native refresh.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated render configuration.
    #[must_use]
    pub fn with_max_fps(mut self, max_fps: Option<u32>) -> Self {
        self.set_max_fps(max_fps.unwrap_or(DEFAULT_MAX_FPS));
        self
    }

    /// Returns the configured optional frame-rate cap (`None` when uncapped).
    ///
    /// # Arguments
    ///
    /// - `&Self` - Render config.
    ///
    /// # Returns
    ///
    /// - `Option<u32>` - Explicit FPS cap, or `None` when uncapped.
    #[must_use]
    pub fn max_fps_limit(&self) -> Option<u32> {
        let raw: u32 = self.get_max_fps();
        if raw == 0 { None } else { Some(raw) }
    }

    /// Configures the bounded in-flight frame queue depth clamped to `1..=MAX_ALLOWED_FRAMES_IN_FLIGHT`.
    ///
    /// # Arguments
    ///
    /// - `Self` - Base render configuration.
    /// - `u32` - Desired maximum frames in flight.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated render configuration.
    #[must_use]
    pub fn with_max_frames_in_flight(mut self, max_frames_in_flight: u32) -> Self {
        self.set_max_frames_in_flight(max_frames_in_flight.clamp(1, MAX_ALLOWED_FRAMES_IN_FLIGHT));
        self
    }

    /// Creates a hybrid CPU-tile + GPU-compositor rendering configuration.
    ///
    /// # Returns
    ///
    /// - `Self` - Hybrid-mode `RenderConfig`.
    #[must_use]
    pub fn hybrid() -> Self {
        let mut cfg: Self = Self::default();
        cfg.set_mode(RenderMode::Hybrid);
        cfg
    }

    /// Creates a GPU-first adaptive rendering configuration starting in shadow `RecommendOnly` mode.
    ///
    /// # Returns
    ///
    /// - `Self` - Adaptive GPU-first `RenderConfig` with direct GPU presentation enabled.
    #[must_use]
    pub fn adaptive() -> Self {
        let mut cfg: Self = Self::default();
        cfg.set_mode(RenderMode::Auto);
        cfg.set_direct_gpu_present(true);
        cfg.set_adaptive_switch_mode(AdaptiveSwitchMode::RecommendOnly);
        cfg
    }

    /// Creates a GPU-first adaptive rendering configuration with live frame-boundary `AutoSwitch` enabled.
    ///
    /// # Returns
    ///
    /// - `Self` - Adaptive GPU-first `RenderConfig` with `AutoSwitch` enabled.
    #[must_use]
    pub fn adaptive_auto_switch() -> Self {
        let mut cfg: Self = Self::adaptive();
        cfg.set_adaptive_switch_mode(AdaptiveSwitchMode::AutoSwitch);
        cfg
    }

    /// Configures the adaptive router rollout switch mode (`RecommendOnly` vs `AutoSwitch`).
    ///
    /// # Arguments
    ///
    /// - `Self` - Base render configuration.
    /// - `AdaptiveSwitchMode` - Desired adaptive switch rollout mode.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated render configuration.
    #[must_use]
    pub fn with_adaptive_switch_mode(mut self, mode: AdaptiveSwitchMode) -> Self {
        self.set_adaptive_switch_mode(mode);
        self
    }

    /// Aligns `direct_gpu_present` strictly with whether the adaptive-selected candidate is `GpuDirect`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Render config reference.
    /// - `RenderBackendCandidate` - Candidate selected by `AdaptiveRenderRouter`.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` only when `candidate == RenderBackendCandidate::GpuDirect`.
    #[must_use]
    pub fn resolve_direct_gpu_present(&self, candidate: RenderBackendCandidate) -> bool {
        let _: AdaptiveSwitchMode = self.get_adaptive_switch_mode();
        candidate.is_direct_gpu()
    }

    /// Returns the optional preferred graphics backend.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Render config.
    ///
    /// # Returns
    ///
    /// - `Option<GraphicsBackend>` - Explicit backend preference if set.
    #[must_use]
    pub fn preferred_backend_value(&self) -> Option<GraphicsBackend> {
        let Self {
            preferred_backend, ..
        } = *self;
        preferred_backend
    }
}

/// Present-outcome projection helpers for `FrameStats`.
impl FrameStats {
    /// Returns the unified `PresentOutcome` corresponding to this frame's submission and pipelining state.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Frame statistics reference.
    ///
    /// # Returns
    ///
    /// - `PresentOutcome` - Detailed presentation outcome for this frame tick.
    #[must_use]
    pub fn present_outcome(&self) -> PresentOutcome {
        let submit: FrameSubmitOutcome = self.get_submit_outcome();
        if self.get_wait_in_flight() && submit == FrameSubmitOutcome::SkippedNoDamage {
            PresentOutcome::WaitInFlight
        } else {
            PresentOutcome::from_submit_outcome(submit)
        }
    }

    /// Updates both `submit_outcome` and `wait_in_flight` consistently from `outcome`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable frame statistics.
    /// - `PresentOutcome` - Observed presentation outcome.
    pub fn record_present_outcome(&mut self, outcome: PresentOutcome) {
        self.set_wait_in_flight(outcome == PresentOutcome::WaitInFlight);
        self.set_submit_outcome(outcome.to_submit_outcome());
    }
}