Skip to main content

euv_core/render/config/
impl.rs

1use super::*;
2
3/// Default rendering configuration (`Auto` hardware-first, `VSync`, `AnalyticalSdf`, uncapped FPS, `max_frames_in_flight = 1`).
4impl Default for RenderConfig {
5    /// Creates the recommended cross-platform `RenderConfig`.
6    ///
7    /// # Returns
8    ///
9    /// - `Self` - Default `RenderConfig`.
10    fn default() -> Self {
11        Self {
12            mode: RenderMode::Auto,
13            preferred_backend: None,
14            present_mode: PresentMode::VSync,
15            antialiasing: AntialiasingMode::AnalyticalSdf,
16            color_space: ColorSpace::Srgb,
17            dirty_rect_optimization: true,
18            tile_size: DEFAULT_TILE_SIZE,
19            max_fps: DEFAULT_MAX_FPS,
20            max_frames_in_flight: DEFAULT_MAX_FRAMES_IN_FLIGHT,
21            hdr: false,
22            direct_gpu_present: false,
23            adaptive_switch_mode: AdaptiveSwitchMode::RecommendOnly,
24        }
25    }
26}
27
28/// Fluent builder helpers for `RenderConfig`.
29impl RenderConfig {
30    /// Creates a software-rendering configuration.
31    ///
32    /// # Returns
33    ///
34    /// - `Self` - Software-mode `RenderConfig`.
35    #[must_use]
36    pub fn software() -> Self {
37        let mut cfg: Self = Self::default();
38        cfg.set_mode(RenderMode::Software);
39        cfg.set_preferred_backend(Some(GraphicsBackend::SoftwareCpu));
40        cfg.set_direct_gpu_present(false);
41        cfg
42    }
43
44    /// Creates an explicit deterministic CPU software reference configuration for headless and visual-parity tests.
45    ///
46    /// # Returns
47    ///
48    /// - `Self` - Deterministic CPU reference `RenderConfig`.
49    #[must_use]
50    pub fn software_reference() -> Self {
51        Self::software()
52    }
53
54    /// Creates a hardware GPU rendering configuration.
55    ///
56    /// # Returns
57    ///
58    /// - `Self` - Hardware-mode `RenderConfig`.
59    #[must_use]
60    pub fn hardware() -> Self {
61        let mut cfg: Self = Self::default();
62        cfg.set_mode(RenderMode::Hardware);
63        cfg
64    }
65
66    /// Creates a hardware GPU direct-draw configuration skipping steady-state CPU RGBA mirror generation.
67    ///
68    /// # Returns
69    ///
70    /// - `Self` - Direct-draw hardware-mode `RenderConfig`.
71    #[must_use]
72    pub fn hardware_direct() -> Self {
73        let mut cfg: Self = Self::hardware();
74        cfg.set_direct_gpu_present(true);
75        cfg
76    }
77
78    /// Configures whether `Hardware` mode renders directly to the GPU without steady-state CPU RGBA readback.
79    ///
80    /// # Arguments
81    ///
82    /// - `Self` - Base render configuration.
83    /// - `bool` - `true` to enable direct GPU presentation with zero steady-state CPU RGBA mirroring.
84    ///
85    /// # Returns
86    ///
87    /// - `Self` - Updated render configuration.
88    #[must_use]
89    pub fn with_direct_gpu_present(mut self, enabled: bool) -> Self {
90        self.set_direct_gpu_present(enabled);
91        self
92    }
93
94    /// Configures an optional explicit frame-rate cap (`None` = uncapped hardware VSync).
95    ///
96    /// # Arguments
97    ///
98    /// - `Self` - Base render configuration.
99    /// - `Option<u32>` - Optional explicit FPS cap, or `None` for uncapped native refresh.
100    ///
101    /// # Returns
102    ///
103    /// - `Self` - Updated render configuration.
104    #[must_use]
105    pub fn with_max_fps(mut self, max_fps: Option<u32>) -> Self {
106        self.set_max_fps(max_fps.unwrap_or(DEFAULT_MAX_FPS));
107        self
108    }
109
110    /// Returns the configured optional frame-rate cap (`None` when uncapped).
111    ///
112    /// # Arguments
113    ///
114    /// - `&Self` - Render config.
115    ///
116    /// # Returns
117    ///
118    /// - `Option<u32>` - Explicit FPS cap, or `None` when uncapped.
119    #[must_use]
120    pub fn max_fps_limit(&self) -> Option<u32> {
121        let raw: u32 = self.get_max_fps();
122        if raw == 0 { None } else { Some(raw) }
123    }
124
125    /// Configures the bounded in-flight frame queue depth clamped to `1..=MAX_ALLOWED_FRAMES_IN_FLIGHT`.
126    ///
127    /// # Arguments
128    ///
129    /// - `Self` - Base render configuration.
130    /// - `u32` - Desired maximum frames in flight.
131    ///
132    /// # Returns
133    ///
134    /// - `Self` - Updated render configuration.
135    #[must_use]
136    pub fn with_max_frames_in_flight(mut self, max_frames_in_flight: u32) -> Self {
137        self.set_max_frames_in_flight(max_frames_in_flight.clamp(1, MAX_ALLOWED_FRAMES_IN_FLIGHT));
138        self
139    }
140
141    /// Creates a hybrid CPU-tile + GPU-compositor rendering configuration.
142    ///
143    /// # Returns
144    ///
145    /// - `Self` - Hybrid-mode `RenderConfig`.
146    #[must_use]
147    pub fn hybrid() -> Self {
148        let mut cfg: Self = Self::default();
149        cfg.set_mode(RenderMode::Hybrid);
150        cfg
151    }
152
153    /// Creates a GPU-first adaptive rendering configuration starting in shadow `RecommendOnly` mode.
154    ///
155    /// # Returns
156    ///
157    /// - `Self` - Adaptive GPU-first `RenderConfig` with direct GPU presentation enabled.
158    #[must_use]
159    pub fn adaptive() -> Self {
160        let mut cfg: Self = Self::default();
161        cfg.set_mode(RenderMode::Auto);
162        cfg.set_direct_gpu_present(true);
163        cfg.set_adaptive_switch_mode(AdaptiveSwitchMode::RecommendOnly);
164        cfg
165    }
166
167    /// Creates a GPU-first adaptive rendering configuration with live frame-boundary `AutoSwitch` enabled.
168    ///
169    /// # Returns
170    ///
171    /// - `Self` - Adaptive GPU-first `RenderConfig` with `AutoSwitch` enabled.
172    #[must_use]
173    pub fn adaptive_auto_switch() -> Self {
174        let mut cfg: Self = Self::adaptive();
175        cfg.set_adaptive_switch_mode(AdaptiveSwitchMode::AutoSwitch);
176        cfg
177    }
178
179    /// Configures the adaptive router rollout switch mode (`RecommendOnly` vs `AutoSwitch`).
180    ///
181    /// # Arguments
182    ///
183    /// - `Self` - Base render configuration.
184    /// - `AdaptiveSwitchMode` - Desired adaptive switch rollout mode.
185    ///
186    /// # Returns
187    ///
188    /// - `Self` - Updated render configuration.
189    #[must_use]
190    pub fn with_adaptive_switch_mode(mut self, mode: AdaptiveSwitchMode) -> Self {
191        self.set_adaptive_switch_mode(mode);
192        self
193    }
194
195    /// Aligns `direct_gpu_present` strictly with whether the adaptive-selected candidate is `GpuDirect`.
196    ///
197    /// # Arguments
198    ///
199    /// - `&Self` - Render config reference.
200    /// - `RenderBackendCandidate` - Candidate selected by `AdaptiveRenderRouter`.
201    ///
202    /// # Returns
203    ///
204    /// - `bool` - `true` only when `candidate == RenderBackendCandidate::GpuDirect`.
205    #[must_use]
206    pub fn resolve_direct_gpu_present(&self, candidate: RenderBackendCandidate) -> bool {
207        let _: AdaptiveSwitchMode = self.get_adaptive_switch_mode();
208        candidate.is_direct_gpu()
209    }
210
211    /// Returns the optional preferred graphics backend.
212    ///
213    /// # Arguments
214    ///
215    /// - `&Self` - Render config.
216    ///
217    /// # Returns
218    ///
219    /// - `Option<GraphicsBackend>` - Explicit backend preference if set.
220    #[must_use]
221    pub fn preferred_backend_value(&self) -> Option<GraphicsBackend> {
222        let Self {
223            preferred_backend, ..
224        } = *self;
225        preferred_backend
226    }
227}
228
229/// Present-outcome projection helpers for `FrameStats`.
230impl FrameStats {
231    /// Returns the unified `PresentOutcome` corresponding to this frame's submission and pipelining state.
232    ///
233    /// # Arguments
234    ///
235    /// - `&Self` - Frame statistics reference.
236    ///
237    /// # Returns
238    ///
239    /// - `PresentOutcome` - Detailed presentation outcome for this frame tick.
240    #[must_use]
241    pub fn present_outcome(&self) -> PresentOutcome {
242        let submit: FrameSubmitOutcome = self.get_submit_outcome();
243        if self.get_wait_in_flight() && submit == FrameSubmitOutcome::SkippedNoDamage {
244            PresentOutcome::WaitInFlight
245        } else {
246            PresentOutcome::from_submit_outcome(submit)
247        }
248    }
249
250    /// Updates both `submit_outcome` and `wait_in_flight` consistently from `outcome`.
251    ///
252    /// # Arguments
253    ///
254    /// - `&Self` - Mutable frame statistics.
255    /// - `PresentOutcome` - Observed presentation outcome.
256    pub fn record_present_outcome(&mut self, outcome: PresentOutcome) {
257        self.set_wait_in_flight(outcome == PresentOutcome::WaitInFlight);
258        self.set_submit_outcome(outcome.to_submit_outcome());
259    }
260}