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 config_spec: Self = Self::default();
38        config_spec.set_mode(RenderMode::Software);
39        config_spec.set_preferred_backend(Some(GraphicsBackend::SoftwareCpu));
40        config_spec.set_direct_gpu_present(false);
41        config_spec
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 config_spec: Self = Self::default();
62        config_spec.set_mode(RenderMode::Hardware);
63        config_spec
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 config_spec: Self = Self::hardware();
74        config_spec.set_direct_gpu_present(true);
75        config_spec.set_max_frames_in_flight(PIPELINED_MAX_FRAMES_IN_FLIGHT);
76        config_spec
77    }
78
79    /// Configures whether `Hardware` mode renders directly to the GPU without steady-state CPU RGBA readback.
80    ///
81    /// # Arguments
82    ///
83    /// - `Self` - Base render configuration.
84    /// - `bool` - `true` to enable direct GPU presentation with zero steady-state CPU RGBA mirroring.
85    ///
86    /// # Returns
87    ///
88    /// - `Self` - Updated render configuration.
89    #[must_use]
90    pub fn with_direct_gpu_present(mut self, enabled: bool) -> Self {
91        self.set_direct_gpu_present(enabled);
92        self
93    }
94
95    /// Configures an optional explicit frame-rate cap (`None` = uncapped hardware VSync).
96    ///
97    /// # Arguments
98    ///
99    /// - `Self` - Base render configuration.
100    /// - `Option<u32>` - Optional explicit FPS cap, or `None` for uncapped native refresh.
101    ///
102    /// # Returns
103    ///
104    /// - `Self` - Updated render configuration.
105    #[must_use]
106    pub fn with_max_fps(mut self, max_fps: Option<u32>) -> Self {
107        self.set_max_fps(max_fps.unwrap_or(DEFAULT_MAX_FPS));
108        self
109    }
110
111    /// Returns the configured optional frame-rate cap (`None` when uncapped).
112    ///
113    /// # Arguments
114    ///
115    /// - `&Self` - Render config.
116    ///
117    /// # Returns
118    ///
119    /// - `Option<u32>` - Explicit FPS cap, or `None` when uncapped.
120    #[must_use]
121    pub fn max_fps_limit(&self) -> Option<u32> {
122        let raw: u32 = self.get_max_fps();
123        if raw == 0 { None } else { Some(raw) }
124    }
125
126    /// Configures the bounded in-flight frame queue depth clamped to `1..=MAX_ALLOWED_FRAMES_IN_FLIGHT`.
127    ///
128    /// # Arguments
129    ///
130    /// - `Self` - Base render configuration.
131    /// - `u32` - Desired maximum frames in flight.
132    ///
133    /// # Returns
134    ///
135    /// - `Self` - Updated render configuration.
136    #[must_use]
137    pub fn with_max_frames_in_flight(mut self, max_frames_in_flight: u32) -> Self {
138        self.set_max_frames_in_flight(max_frames_in_flight.clamp(1, MAX_ALLOWED_FRAMES_IN_FLIGHT));
139        self
140    }
141
142    /// Creates a hybrid CPU-tile + GPU-compositor rendering configuration.
143    ///
144    /// # Returns
145    ///
146    /// - `Self` - Hybrid-mode `RenderConfig`.
147    #[must_use]
148    pub fn hybrid() -> Self {
149        let mut config_spec: Self = Self::default();
150        config_spec.set_mode(RenderMode::Hybrid);
151        config_spec
152    }
153
154    /// Creates a GPU-first adaptive rendering configuration starting in shadow `RecommendOnly` mode.
155    ///
156    /// # Returns
157    ///
158    /// - `Self` - Adaptive GPU-first `RenderConfig` with direct GPU presentation enabled.
159    #[must_use]
160    pub fn adaptive() -> Self {
161        let mut config_spec: Self = Self::default();
162        config_spec.set_mode(RenderMode::Auto);
163        config_spec.set_direct_gpu_present(true);
164        config_spec.set_max_frames_in_flight(PIPELINED_MAX_FRAMES_IN_FLIGHT);
165        config_spec.set_adaptive_switch_mode(AdaptiveSwitchMode::RecommendOnly);
166        config_spec
167    }
168
169    /// Creates a GPU-first adaptive rendering configuration with live frame-boundary `AutoSwitch` enabled.
170    ///
171    /// # Returns
172    ///
173    /// - `Self` - Adaptive GPU-first `RenderConfig` with `AutoSwitch` enabled.
174    #[must_use]
175    pub fn adaptive_auto_switch() -> Self {
176        let mut config_spec: Self = Self::adaptive();
177        config_spec.set_adaptive_switch_mode(AdaptiveSwitchMode::AutoSwitch);
178        config_spec
179    }
180
181    /// Configures the adaptive router rollout switch mode (`RecommendOnly` vs `AutoSwitch`).
182    ///
183    /// # Arguments
184    ///
185    /// - `Self` - Base render configuration.
186    /// - `AdaptiveSwitchMode` - Desired adaptive switch rollout mode.
187    ///
188    /// # Returns
189    ///
190    /// - `Self` - Updated render configuration.
191    #[must_use]
192    pub fn with_adaptive_switch_mode(mut self, mode: AdaptiveSwitchMode) -> Self {
193        self.set_adaptive_switch_mode(mode);
194        self
195    }
196
197    /// Aligns `direct_gpu_present` strictly with whether the adaptive-selected candidate is `GpuDirect`.
198    ///
199    /// # Arguments
200    ///
201    /// - `&Self` - Render config reference.
202    /// - `RenderBackendCandidate` - Candidate selected by `AdaptiveRenderRouter`.
203    ///
204    /// # Returns
205    ///
206    /// - `bool` - `true` only when `candidate == RenderBackendCandidate::GpuDirect`.
207    #[must_use]
208    pub fn resolve_direct_gpu_present(&self, candidate: RenderBackendCandidate) -> bool {
209        let _: AdaptiveSwitchMode = self.get_adaptive_switch_mode();
210        candidate.is_direct_gpu()
211    }
212
213    /// Returns the optional preferred graphics backend.
214    ///
215    /// # Arguments
216    ///
217    /// - `&Self` - Render config.
218    ///
219    /// # Returns
220    ///
221    /// - `Option<GraphicsBackend>` - Explicit backend preference if set.
222    #[must_use]
223    pub fn preferred_backend_value(&self) -> Option<GraphicsBackend> {
224        let Self {
225            preferred_backend, ..
226        } = *self;
227        preferred_backend
228    }
229}
230
231/// Present-outcome projection helpers for `FrameStats`.
232impl FrameStats {
233    /// Returns the unified `PresentOutcome` corresponding to this frame's submission and pipelining state.
234    ///
235    /// # Arguments
236    ///
237    /// - `&Self` - Frame statistics reference.
238    ///
239    /// # Returns
240    ///
241    /// - `PresentOutcome` - Detailed presentation outcome for this frame tick.
242    #[must_use]
243    pub fn present_outcome(&self) -> PresentOutcome {
244        let submit: FrameSubmitOutcome = self.get_submit_outcome();
245        if self.get_wait_in_flight() && submit == FrameSubmitOutcome::SkippedNoDamage {
246            PresentOutcome::WaitInFlight
247        } else {
248            PresentOutcome::from_submit_outcome(submit)
249        }
250    }
251
252    /// Updates both `submit_outcome` and `wait_in_flight` consistently from `outcome`.
253    ///
254    /// # Arguments
255    ///
256    /// - `&Self` - Mutable frame statistics.
257    /// - `PresentOutcome` - Observed presentation outcome.
258    pub fn record_present_outcome(&mut self, outcome: PresentOutcome) {
259        self.set_wait_in_flight(outcome == PresentOutcome::WaitInFlight);
260        self.set_submit_outcome(outcome.to_submit_outcome());
261    }
262}