euv-core 0.44.0

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
use super::*;

/// Fluent builder helpers for `FrameRenderParams`.
impl FrameRenderParams {
    /// Creates frame render parameters for `viewport` with `1.0` scale, white clear color, and full damage.
    ///
    /// # Arguments
    ///
    /// - `Size` - Logical viewport size.
    ///
    /// # Returns
    ///
    /// - `Self` - Ready render parameters.
    #[must_use]
    pub fn new(viewport: Size) -> Self {
        Self {
            viewport,
            scale_factor: 1.0,
            clear_color: COLOR_WHITE,
            layout_stats: LayoutStats::default(),
            damage: DamageRegion::full(),
        }
    }

    /// Sets the active DPI scale factor.
    ///
    /// # Arguments
    ///
    /// - `Self` - Base parameters.
    /// - `f32` - Scale factor.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated parameters.
    #[must_use]
    pub fn with_scale(mut self, scale_factor: f32) -> Self {
        self.set_scale_factor(scale_factor.max(0.5));
        self
    }

    /// Sets the surface clear color.
    ///
    /// # Arguments
    ///
    /// - `Self` - Base parameters.
    /// - `Color` - Clear color.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated parameters.
    #[must_use]
    pub fn with_clear(mut self, clear_color: Color) -> Self {
        self.set_clear_color(clear_color);
        self
    }

    /// Attaches layout telemetry from the current frame.
    ///
    /// # Arguments
    ///
    /// - `Self` - Base parameters.
    /// - `LayoutStats` - Layout pass statistics.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated parameters.
    #[must_use]
    pub fn with_layout_stats(mut self, layout_stats: LayoutStats) -> Self {
        self.set_layout_stats(layout_stats);
        self
    }

    /// Sets the active dirty damage region.
    ///
    /// # Arguments
    ///
    /// - `Self` - Base parameters.
    /// - `DamageRegion` - Damage region.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated parameters.
    #[must_use]
    pub fn with_damage(mut self, damage: DamageRegion) -> Self {
        self.set_damage(damage);
        self
    }
}

/// Multi-mode pipeline construction, live mode switching, and frame rendering for `Renderer`.
impl Renderer {
    /// Creates a `Renderer` resolving the optimal backend for `platform` and `config`.
    ///
    /// # Arguments
    ///
    /// - `PlatformKind` - Active execution platform.
    /// - `PlatformCapabilities` - Hardware capabilities reported by the platform backend.
    /// - `u32` - Initial physical width.
    /// - `u32` - Initial physical height.
    /// - `RenderConfig` - Requested rendering configuration.
    ///
    /// # Returns
    ///
    /// - `Self` - Initialized multi-mode renderer facade.
    #[must_use]
    pub fn new(
        platform: PlatformKind,
        capabilities: PlatformCapabilities,
        width: u32,
        height: u32,
        config: RenderConfig,
    ) -> Self {
        let (active_mode, active_backend): (RenderMode, GraphicsBackend) =
            Self::resolve_mode_and_backend(platform, capabilities, config);
        let readback_mirror: bool = true;
        let compute_pixel_hash: bool =
            platform == PlatformKind::Headless || active_backend == GraphicsBackend::Headless;
        let font_provider: NativeSystemFont = if platform == PlatformKind::Headless {
            NativeSystemFont::deterministic()
        } else {
            NativeSystemFont::system()
        };
        let mut display_list: DisplayList = DisplayList::default();
        display_list.get_mut_commands().reserve(128);
        Self {
            config,
            active_mode,
            active_backend,
            software: SoftwareRenderer::with_font_provider(
                width,
                height,
                config,
                font_provider.clone(),
            ),
            hardware: HardwareRenderer::with_backend_and_font(
                active_backend,
                width,
                height,
                config,
                readback_mirror,
                font_provider.clone(),
            ),
            hybrid: HybridRenderer::with_backend_and_font(
                active_backend,
                width,
                height,
                config,
                font_provider,
            ),
            paint_pass: PaintPass,
            paint_fragment_cache: PaintFragmentCache::default(),
            display_list,
            frame_index: 0,
            compute_pixel_hash,
            last_hashed_mode: None,
            last_hashed_dims: (0, 0),
            last_stats: FrameStats::default(),
        }
    }

    /// Resolves `(RenderMode, GraphicsBackend)` deterministically from platform capabilities and user preference.
    ///
    /// # Arguments
    ///
    /// - `PlatformKind` - Target platform.
    /// - `PlatformCapabilities` - Hardware support flags.
    /// - `RenderConfig` - User configuration.
    ///
    /// # Returns
    ///
    /// - `(RenderMode, GraphicsBackend)` - Concrete resolved mode and backend.
    #[must_use]
    pub fn resolve_mode_and_backend(
        platform: PlatformKind,
        capabilities: PlatformCapabilities,
        config: RenderConfig,
    ) -> (RenderMode, GraphicsBackend) {
        let default_gpu: GraphicsBackend = match platform {
            PlatformKind::MacOs | PlatformKind::Ios | PlatformKind::IpadOs => {
                GraphicsBackend::Metal
            }
            PlatformKind::Windows => GraphicsBackend::Direct3D12,
            PlatformKind::Linux | PlatformKind::Android => GraphicsBackend::Vulkan,
            PlatformKind::Web => GraphicsBackend::WebGpu,
            PlatformKind::Headless => GraphicsBackend::Headless,
        };
        let mode: RenderMode = match config.get_mode() {
            RenderMode::Auto => {
                if capabilities.get_hardware_gpu() {
                    RenderMode::Hardware
                } else {
                    RenderMode::Software
                }
            }
            explicit => explicit,
        };
        let backend: GraphicsBackend = match mode {
            RenderMode::Software => match platform {
                PlatformKind::Web => GraphicsBackend::Canvas2d,
                _ => GraphicsBackend::SoftwareCpu,
            },
            RenderMode::Hardware | RenderMode::Hybrid => {
                config.preferred_backend_value().unwrap_or(default_gpu)
            }
            RenderMode::Auto => default_gpu,
        };
        (mode, backend)
    }

    /// Dynamically switches the active rendering mode at runtime without rebuilding the scene tree.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable renderer.
    /// - `PlatformKind` - Active platform kind.
    /// - `PlatformCapabilities` - Platform hardware capabilities.
    /// - `RenderMode` - Target render mode.
    pub fn switch_mode(
        &mut self,
        platform: PlatformKind,
        capabilities: PlatformCapabilities,
        mode: RenderMode,
    ) {
        let mut next_cfg: RenderConfig = self.get_config();
        next_cfg.set_mode(mode);
        let (resolved_mode, resolved_backend): (RenderMode, GraphicsBackend) =
            Self::resolve_mode_and_backend(platform, capabilities, next_cfg);
        self.set_config(next_cfg);
        self.set_active_mode(resolved_mode);
        self.set_active_backend(resolved_backend);
        let Self {
            hardware,
            hybrid,
            last_hashed_mode,
            ..
        } = self;
        *last_hashed_mode = None;
        hardware.get_mut_driver().switch_backend(resolved_backend);
        hybrid
            .get_mut_compositor()
            .set_backend_kind(resolved_backend);
    }

    /// Compiles `tree` into a `DisplayList`, executes the active render backend, and returns `FrameStats`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable renderer.
    /// - `&SceneTree` - Laid-out scene tree.
    /// - `Size` - Logical viewport dimensions.
    /// - `f32` - Active DPI scale factor.
    /// - `Color` - Surface clear color.
    /// - `LayoutStats` - Telemetry from the layout pass.
    /// - `DamageRegion` - Damaged screen region.
    ///
    /// # Returns
    ///
    /// - `Result<FrameStats, PlatformError>` - Frame performance and pixel-hash telemetry.
    pub fn render_frame(
        &mut self,
        tree: &SceneTree,
        viewport: Size,
        scale_factor: f32,
        clear_color: Color,
        layout_stats: LayoutStats,
        damage: DamageRegion,
    ) -> Result<FrameStats, PlatformError> {
        let scale: f32 = scale_factor.max(0.5);
        let phys_w: u32 = ((viewport.get_width() * scale).round() as u32).max(1);
        let phys_h: u32 = ((viewport.get_height() * scale).round() as u32).max(1);
        let phys_dims: (u32, u32) = (phys_w, phys_h);
        let Self {
            active_mode,
            active_backend,
            software,
            hardware,
            hybrid,
            paint_pass,
            paint_fragment_cache,
            display_list,
            frame_index,
            compute_pixel_hash,
            last_hashed_mode,
            last_hashed_dims,
            last_stats,
            ..
        } = self;
        let mode: RenderMode = *active_mode;
        let backend: GraphicsBackend = *active_backend;
        let do_pixel_hash: bool = *compute_pixel_hash;
        let hash_reusable: bool = *last_hashed_mode == Some(mode) && *last_hashed_dims == phys_dims;
        if !damage.get_full_damage() && damage.bounds_rect().is_none() && hash_reusable {
            *frame_index = frame_index.wrapping_add(1);
            let stats: FrameStats = FrameStats {
                frame_index: *frame_index,
                active_mode: mode,
                active_backend: backend,
                layout_nodes: layout_stats.get_nodes_computed(),
                layout_skipped: layout_stats.get_subtrees_skipped(),
                draw_commands: 0,
                draw_batches: 0,
                pixels_shaded: 0,
                frame_hash: last_stats.get_frame_hash(),
            };
            *last_stats = stats;
            return Ok(stats);
        }
        let _: &PaintPass = paint_pass;
        let layout_changed: bool = layout_stats.get_nodes_computed() > 0;
        PaintPass::build_retained(
            tree,
            viewport,
            scale,
            clear_color,
            layout_changed,
            display_list,
            paint_fragment_cache,
        );
        let cmd_count: usize = display_list.len();
        let (batches, pixels, hash): (usize, usize, u64) = match mode {
            RenderMode::Software => {
                let (b, p): (usize, usize) = software.render(display_list, damage);
                let h: u64 = if p == 0 && hash_reusable {
                    last_stats.get_frame_hash()
                } else if do_pixel_hash {
                    software.get_framebuffer().fnv1a_hash()
                } else {
                    display_list.fast_hash()
                };
                (b, p, h)
            }
            RenderMode::Hybrid => {
                let (b, p): (usize, usize) = hybrid.render(display_list, damage)?;
                let h: u64 = if p == 0 && hash_reusable {
                    last_stats.get_frame_hash()
                } else if do_pixel_hash {
                    hybrid.get_software().get_framebuffer().fnv1a_hash()
                } else {
                    display_list.fast_hash()
                };
                (b, p, h)
            }
            RenderMode::Hardware | RenderMode::Auto => {
                let (b, p): (usize, usize) = hardware.render(display_list, damage)?;
                let h: u64 = if p == 0 && hash_reusable {
                    last_stats.get_frame_hash()
                } else if hardware.get_enable_readback() {
                    if do_pixel_hash {
                        hardware.get_readback().get_framebuffer().fnv1a_hash()
                    } else {
                        display_list.fast_hash()
                    }
                } else {
                    hardware.batch_hash()
                };
                (b, p, h)
            }
        };
        *last_hashed_mode = Some(mode);
        *last_hashed_dims = phys_dims;
        *frame_index = frame_index.wrapping_add(1);
        let stats: FrameStats = FrameStats {
            frame_index: *frame_index,
            active_mode: mode,
            active_backend: backend,
            layout_nodes: layout_stats.get_nodes_computed(),
            layout_skipped: layout_stats.get_subtrees_skipped(),
            draw_commands: cmd_count,
            draw_batches: batches,
            pixels_shaded: pixels,
            frame_hash: hash,
        };
        *last_stats = stats;
        Ok(stats)
    }

    /// Renders `tree` using a compact `FrameRenderParams` descriptor and returns `FrameStats`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable renderer.
    /// - `&SceneTree` - Laid-out scene tree.
    /// - `&FrameRenderParams` - Viewport, DPI, clear color, layout stats, and damage bundle.
    ///
    /// # Returns
    ///
    /// - `Result<FrameStats, PlatformError>` - Frame performance and pixel-hash telemetry.
    #[inline(always)]
    pub fn render_with(
        &mut self,
        tree: &SceneTree,
        params: &FrameRenderParams,
    ) -> Result<FrameStats, PlatformError> {
        self.render_frame(
            tree,
            params.get_viewport(),
            params.get_scale_factor(),
            params.get_clear_color(),
            params.get_layout_stats(),
            params.get_damage(),
        )
    }

    /// Advances the frame counter for an undamaged idle frame without rasterizing or hashing pixels.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable renderer.
    ///
    /// # Returns
    ///
    /// - `FrameStats` - Lightweight telemetry preserving the last rendered `frame_hash` with zero shaded pixels.
    pub fn record_skipped_frame(&mut self) -> FrameStats {
        let next_idx: u64 = self.get_frame_index().wrapping_add(1);
        self.set_frame_index(next_idx);
        let mut stats: FrameStats = self.get_last_stats();
        stats.set_frame_index(next_idx);
        stats.set_layout_nodes(0);
        stats.set_draw_batches(0);
        stats.set_pixels_shaded(0);
        self.set_last_stats(stats);
        stats
    }

    /// Returns cumulative cross-frame `PaintFragmentCache` telemetry `(whole_list_hits, fragment_hits, fragment_misses, fragment_evictions)`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Renderer reference.
    ///
    /// # Returns
    ///
    /// - `(u64, u64, u64, u64)` - Tuple of whole-list hits, fragment hits, misses, and evictions.
    #[must_use]
    pub fn paint_fragment_cache_stats(&self) -> (u64, u64, u64, u64) {
        self.get_paint_fragment_cache().stats()
    }

    /// Returns a reference to the latest CPU-accessible `Framebuffer` for software/hybrid/readback presentation.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Renderer reference.
    ///
    /// # Returns
    ///
    /// - `Option<&Framebuffer>` - Active RGBA8 framebuffer if available in host memory.
    #[must_use]
    pub fn framebuffer(&self) -> Option<&Framebuffer> {
        match self.get_active_mode() {
            RenderMode::Software => Some(self.get_software().get_framebuffer()),
            RenderMode::Hybrid => Some(self.get_hybrid().get_software().get_framebuffer()),
            RenderMode::Hardware | RenderMode::Auto => {
                if self.get_hardware().get_enable_readback() {
                    Some(self.get_hardware().get_readback().get_framebuffer())
                } else {
                    None
                }
            }
        }
    }
}