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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
use super::*;

/// Sparse-effect hybrid partitioning, tight-tile CPU rasterization, and GPU batch compositing for `HybridRenderer`.
impl HybridRenderer {
    /// Creates a sparse-effect hybrid renderer targeting `gpu_backend` for direct GPU primitives and tight-tile compositing.
    ///
    /// # Arguments
    ///
    /// - `GraphicsBackend` - GPU backend used for direct primitive batches and sparse-tile compositing.
    /// - `u32` - Initial physical width.
    /// - `u32` - Initial physical height.
    /// - `RenderConfig` - Rendering configuration.
    ///
    /// # Returns
    ///
    /// - `Self` - Initialized sparse-effect hybrid renderer.
    #[must_use]
    pub fn with_backend(
        gpu_backend: GraphicsBackend,
        width: u32,
        height: u32,
        config: RenderConfig,
    ) -> Self {
        Self::with_backend_and_font(
            gpu_backend,
            width,
            height,
            config,
            NativeSystemFont::deterministic(),
        )
    }

    /// Creates a sparse-effect hybrid renderer targeting `gpu_backend` with an explicit font provider.
    ///
    /// # Arguments
    ///
    /// - `GraphicsBackend` - GPU backend used for direct primitive batches and sparse-tile compositing.
    /// - `u32` - Initial physical width.
    /// - `u32` - Initial physical height.
    /// - `RenderConfig` - Rendering configuration.
    /// - `NativeSystemFont` - Font provider used only when sparse CPU effect regions require rasterization.
    ///
    /// # Returns
    ///
    /// - `Self` - Initialized sparse-effect hybrid renderer.
    #[must_use]
    pub fn with_backend_and_font(
        gpu_backend: GraphicsBackend,
        width: u32,
        height: u32,
        config: RenderConfig,
        font_provider: NativeSystemFont,
    ) -> Self {
        let mut compositor: RecordingGpuDriver = RecordingGpuDriver::default();
        compositor.set_backend_kind(gpu_backend);
        Self {
            software: SoftwareRenderer::with_font_provider(width, height, config, font_provider),
            compositor,
            last_cpu_effect_regions: Vec::new(),
            last_gpu_native_commands: 0,
            last_cpu_tile_count: 0,
            last_cpu_upload_bytes: 0,
            last_cpu_rasterized_pixels: 0,
        }
    }

    /// Partitions `display_list` into GPU-native primitives versus sparse CPU-only effect regions without rasterizing the full frame.
    ///
    /// # Arguments
    ///
    /// - `&DisplayList` - Frame display list to inspect.
    ///
    /// # Returns
    ///
    /// - `HybridCommandPartition` - Partition counts and tight physical bounds of sparse CPU-only effect regions.
    #[must_use]
    pub fn partition_commands(display_list: &DisplayList) -> HybridCommandPartition {
        let scale: f32 = display_list.get_scale_factor().max(0.5);
        let mut gpu_native_count: usize = 0;
        let mut gpu_covered_pixels: usize = 0;
        let mut cpu_effect_regions: Vec<Rect> = Vec::new();
        for cmd in display_list.get_commands().iter() {
            match cmd {
                DrawCommand::BackdropBlur { rect, .. } => {
                    let phys: Rect = Rect::new(
                        rect.get_x() * scale,
                        rect.get_y() * scale,
                        (rect.get_width() * scale).max(1.0),
                        (rect.get_height() * scale).max(1.0),
                    );
                    cpu_effect_regions.push(phys);
                }
                DrawCommand::FillRoundedRect { rect, .. }
                | DrawCommand::LinearGradientRect { rect, .. }
                | DrawCommand::BoxShadow { rect, .. }
                | DrawCommand::TextRun { rect, .. }
                | DrawCommand::Image { rect, .. }
                | DrawCommand::SharedImage { rect, .. }
                | DrawCommand::SpriteRegion { rect, .. } => {
                    gpu_native_count = gpu_native_count.saturating_add(1);
                    let pw: usize = ((rect.get_width() * scale).ceil() as usize).max(1);
                    let ph: usize = ((rect.get_height() * scale).ceil() as usize).max(1);
                    gpu_covered_pixels = gpu_covered_pixels.saturating_add(pw.saturating_mul(ph));
                }
                DrawCommand::Circle { radius, .. } => {
                    gpu_native_count = gpu_native_count.saturating_add(1);
                    let d: usize = (((*radius) * 2.0 * scale).ceil() as usize).max(1);
                    gpu_covered_pixels = gpu_covered_pixels.saturating_add(d.saturating_mul(d));
                }
                DrawCommand::Line {
                    from, to, width, ..
                } => {
                    gpu_native_count = gpu_native_count.saturating_add(1);
                    let lw: usize = (((to.get_x() - from.get_x()).abs().max(*width) * scale).ceil()
                        as usize)
                        .max(1);
                    let lh: usize = (((to.get_y() - from.get_y()).abs().max(*width) * scale).ceil()
                        as usize)
                        .max(1);
                    gpu_covered_pixels = gpu_covered_pixels.saturating_add(lw.saturating_mul(lh));
                }
                DrawCommand::Triangle { p0, p1, p2, .. } => {
                    gpu_native_count = gpu_native_count.saturating_add(1);
                    let w: f32 = p0.get_x().max(p1.get_x()).max(p2.get_x())
                        - p0.get_x().min(p1.get_x()).min(p2.get_x());
                    let h: f32 = p0.get_y().max(p1.get_y()).max(p2.get_y())
                        - p0.get_y().min(p1.get_y()).min(p2.get_y());
                    let pw: usize = ((w * scale).ceil() as usize).max(1);
                    let ph: usize = ((h * scale).ceil() as usize).max(1);
                    gpu_covered_pixels = gpu_covered_pixels.saturating_add(pw.saturating_mul(ph));
                }
                DrawCommand::Clear(_)
                | DrawCommand::TextCaret { .. }
                | DrawCommand::PushClip(_)
                | DrawCommand::PushRoundedClip { .. }
                | DrawCommand::PopClip => {
                    gpu_native_count = gpu_native_count.saturating_add(1);
                }
            }
        }
        HybridCommandPartition {
            gpu_native_count,
            cpu_effect_regions,
            gpu_covered_pixels,
        }
    }

    /// Evaluates whether the sparse CPU-only effect regions from the most recent frame justify `Hybrid` over pure GPU or pure CPU.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Hybrid renderer reference.
    /// - `f64` - Pure GPU frame time in milliseconds.
    /// - `f64` - Pure CPU frame time in milliseconds.
    /// - `f64` - Sparse CPU tile raster time in milliseconds.
    /// - `f64` - GPU composite + native primitive time in milliseconds.
    ///
    /// # Returns
    ///
    /// - `HybridNetGainEstimate` - Net gain verdict for `AdaptiveRenderRouter`.
    #[must_use]
    pub fn evaluate_last_frame_net_gain(
        &self,
        gpu_full_ms: f64,
        cpu_full_ms: f64,
        cpu_tile_ms: f64,
        gpu_composite_ms: f64,
    ) -> HybridNetGainEstimate {
        let fb: &Framebuffer = self.get_software().get_framebuffer();
        estimate_hybrid_net_gain(
            fb.get_width(),
            fb.get_height(),
            self.get_last_cpu_effect_regions().as_slice(),
            gpu_full_ms,
            cpu_full_ms,
            cpu_tile_ms,
            gpu_composite_ms,
        )
    }

    /// Compiles GPU-native primitives directly into GPU batches and rasterizes ONLY sparse CPU-only effect regions into tight tiles.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable hybrid renderer.
    /// - `&DisplayList` - Frame display list.
    /// - `DamageRegion` - Dirty damage bounds.
    ///
    /// # Returns
    ///
    /// - `Result<(usize, usize), PlatformError>` - Tuple of `(batches_submitted, pixels_shaded)`.
    pub fn render(
        &mut self,
        display_list: &DisplayList,
        damage: DamageRegion,
    ) -> Result<(usize, usize), PlatformError> {
        let scale: f32 = display_list.get_scale_factor().max(0.5);
        let vp: Size = display_list.get_viewport();
        let phys_w: u32 = if vp.get_width() > 0.0 {
            ((vp.get_width() * scale).round() as u32).max(1)
        } else {
            self.get_software().get_framebuffer().get_width().max(1)
        };
        let phys_h: u32 = if vp.get_height() > 0.0 {
            ((vp.get_height() * scale).round() as u32).max(1)
        } else {
            self.get_software().get_framebuffer().get_height().max(1)
        };
        if self.get_software().get_framebuffer().get_width() != phys_w
            || self.get_software().get_framebuffer().get_height() != phys_h
        {
            let sw: &mut SoftwareRenderer = self.get_mut_software();
            if let Some(fb) = sw.framebuffer.as_mut() {
                fb.resize(phys_w, phys_h);
            }
        }
        let full_rect: Rect = Rect::new(0.0, 0.0, phys_w as f32, phys_h as f32);
        let partition: HybridCommandPartition = Self::partition_commands(display_list);
        let mut batch: GpuBatch = GpuBatch::default();
        batch.set_scissor(full_rect);
        let mut clear_color: Color = COLOR_TRANSPARENT;
        for cmd in display_list.get_commands().iter() {
            match cmd {
                DrawCommand::Clear(c) => {
                    clear_color = *c;
                }
                DrawCommand::FillRoundedRect {
                    rect,
                    color,
                    radius,
                    border,
                } => {
                    let quad: GpuQuadInstance = GpuQuadInstance {
                        rect: [
                            rect.get_x() * scale,
                            rect.get_y() * scale,
                            rect.get_width() * scale,
                            rect.get_height() * scale,
                        ],
                        clip: [0.0, 0.0, phys_w as f32, phys_h as f32],
                        color: color.to_f32_array(),
                        secondary_color: border.get_color().to_f32_array(),
                        radii: [
                            radius.get_top_left() * scale,
                            radius.get_top_right() * scale,
                            radius.get_bottom_right() * scale,
                            radius.get_bottom_left() * scale,
                        ],
                        stroke_or_blur: border.get_width() * scale,
                        angle_rad: 0.0,
                        kind: GpuPrimitiveKind::RoundedRect as u32,
                        reserved: 0,
                    };
                    let prim: GpuPrimitiveInstance =
                        GpuPrimitiveInstance::from_quad_instance(&quad, 0, 0);
                    batch.get_mut_instances().push(quad);
                    batch.get_mut_primitives().push(prim);
                }
                DrawCommand::LinearGradientRect {
                    rect,
                    gradient,
                    radius,
                } => {
                    let quad: GpuQuadInstance = GpuQuadInstance {
                        rect: [
                            rect.get_x() * scale,
                            rect.get_y() * scale,
                            rect.get_width() * scale,
                            rect.get_height() * scale,
                        ],
                        clip: [0.0, 0.0, phys_w as f32, phys_h as f32],
                        color: gradient.get_start_color().to_f32_array(),
                        secondary_color: gradient.get_end_color().to_f32_array(),
                        radii: [
                            radius.get_top_left() * scale,
                            radius.get_top_right() * scale,
                            radius.get_bottom_right() * scale,
                            radius.get_bottom_left() * scale,
                        ],
                        stroke_or_blur: 0.0,
                        angle_rad: gradient.get_angle_deg().to_radians(),
                        kind: GpuPrimitiveKind::Gradient as u32,
                        reserved: 0,
                    };
                    let prim: GpuPrimitiveInstance =
                        GpuPrimitiveInstance::from_quad_instance(&quad, 0, 0);
                    batch.get_mut_instances().push(quad);
                    batch.get_mut_primitives().push(prim);
                }
                DrawCommand::BoxShadow {
                    rect,
                    radius,
                    shadow,
                } => {
                    let pad: f32 = (shadow.get_blur_radius() + shadow.get_spread_radius()) * scale;
                    let quad: GpuQuadInstance = GpuQuadInstance {
                        rect: [
                            (rect.get_x() + shadow.get_offset_x()) * scale - pad,
                            (rect.get_y() + shadow.get_offset_y()) * scale - pad,
                            rect.get_width() * scale + pad * 2.0,
                            rect.get_height() * scale + pad * 2.0,
                        ],
                        clip: [0.0, 0.0, phys_w as f32, phys_h as f32],
                        color: shadow.get_color().to_f32_array(),
                        secondary_color: [0.0; 4],
                        radii: [
                            radius.get_top_left() * scale,
                            radius.get_top_right() * scale,
                            radius.get_bottom_right() * scale,
                            radius.get_bottom_left() * scale,
                        ],
                        stroke_or_blur: shadow.get_blur_radius() * scale,
                        angle_rad: pad,
                        kind: GpuPrimitiveKind::Shadow as u32,
                        reserved: 0,
                    };
                    let prim: GpuPrimitiveInstance =
                        GpuPrimitiveInstance::from_quad_instance(&quad, 0, 0);
                    batch.get_mut_instances().push(quad);
                    batch.get_mut_primitives().push(prim);
                }
                DrawCommand::TextRun { rect, style, .. } => {
                    let quad: GpuQuadInstance = GpuQuadInstance {
                        rect: [
                            rect.get_x() * scale,
                            rect.get_y() * scale,
                            rect.get_width() * scale,
                            rect.get_height() * scale,
                        ],
                        clip: [0.0, 0.0, phys_w as f32, phys_h as f32],
                        color: style.get_color().to_f32_array(),
                        secondary_color: [0.0; 4],
                        radii: [0.0; 4],
                        stroke_or_blur: 0.0,
                        angle_rad: 0.0,
                        kind: GpuPrimitiveKind::Glyph as u32,
                        reserved: 0,
                    };
                    let prim: GpuPrimitiveInstance =
                        GpuPrimitiveInstance::from_quad_instance(&quad, 0, 0);
                    batch.get_mut_instances().push(quad);
                    batch.get_mut_primitives().push(prim);
                }
                DrawCommand::Image { rect, radius, .. }
                | DrawCommand::SharedImage { rect, radius, .. } => {
                    let quad: GpuQuadInstance = GpuQuadInstance {
                        rect: [
                            rect.get_x() * scale,
                            rect.get_y() * scale,
                            rect.get_width() * scale,
                            rect.get_height() * scale,
                        ],
                        clip: [0.0, 0.0, phys_w as f32, phys_h as f32],
                        color: [1.0, 1.0, 1.0, 1.0],
                        secondary_color: [0.0; 4],
                        radii: [
                            radius.get_top_left() * scale,
                            radius.get_top_right() * scale,
                            radius.get_bottom_right() * scale,
                            radius.get_bottom_left() * scale,
                        ],
                        stroke_or_blur: 0.0,
                        angle_rad: 0.0,
                        kind: GpuPrimitiveKind::TexturedQuad as u32,
                        reserved: 0,
                    };
                    let prim: GpuPrimitiveInstance =
                        GpuPrimitiveInstance::from_quad_instance(&quad, 0, 0);
                    batch.get_mut_instances().push(quad);
                    batch.get_mut_primitives().push(prim);
                }
                DrawCommand::Circle {
                    center,
                    radius,
                    color,
                } => {
                    let r: f32 = radius * scale;
                    let quad: GpuQuadInstance = GpuQuadInstance {
                        rect: [
                            center.get_x() * scale - r,
                            center.get_y() * scale - r,
                            r * 2.0,
                            r * 2.0,
                        ],
                        clip: [0.0, 0.0, phys_w as f32, phys_h as f32],
                        color: color.to_f32_array(),
                        secondary_color: [0.0; 4],
                        radii: [r; 4],
                        stroke_or_blur: 0.0,
                        angle_rad: 0.0,
                        kind: GpuPrimitiveKind::Circle as u32,
                        reserved: 0,
                    };
                    let prim: GpuPrimitiveInstance =
                        GpuPrimitiveInstance::from_quad_instance(&quad, 0, 0);
                    batch.get_mut_instances().push(quad);
                    batch.get_mut_primitives().push(prim);
                }
                DrawCommand::BackdropBlur { .. }
                | DrawCommand::TextCaret { .. }
                | DrawCommand::Line { .. }
                | DrawCommand::Triangle { .. }
                | DrawCommand::SpriteRegion { .. }
                | DrawCommand::PushClip(_)
                | DrawCommand::PushRoundedClip { .. }
                | DrawCommand::PopClip => {}
            }
        }
        let mut cpu_tiles: usize = 0;
        let mut cpu_pixels: usize = 0;
        let mut cpu_upload_bytes: usize = 0;
        if !partition.get_cpu_effect_regions().is_empty() {
            let mut sparse_damage: DamageRegion = DamageRegion::default();
            for region in partition.get_cpu_effect_regions().iter() {
                let clipped_x: f32 = region.get_x().clamp(0.0, phys_w as f32);
                let clipped_y: f32 = region.get_y().clamp(0.0, phys_h as f32);
                let clipped_w: f32 = region
                    .get_width()
                    .min((phys_w as f32 - clipped_x).max(0.0))
                    .max(0.0);
                let clipped_h: f32 = region
                    .get_height()
                    .min((phys_h as f32 - clipped_y).max(0.0))
                    .max(0.0);
                if clipped_w <= 0.0 || clipped_h <= 0.0 {
                    continue;
                }
                let tight_rect: Rect = Rect::new(clipped_x, clipped_y, clipped_w, clipped_h);
                if !damage.get_full_damage()
                    && let Some(dmg_rect) = damage.bounds_rect()
                    && tight_rect.intersect(dmg_rect).is_none()
                {
                    continue;
                }
                sparse_damage.add_rect(tight_rect);
                let tile_w: usize = clipped_w.ceil() as usize;
                let tile_h: usize = clipped_h.ceil() as usize;
                let tile_bytes: usize = tile_w
                    .saturating_mul(tile_h)
                    .saturating_mul(HYBRID_RGBA8_BYTES_PER_PIXEL);
                cpu_upload_bytes = cpu_upload_bytes.saturating_add(tile_bytes);
                let tile_quad: GpuQuadInstance = GpuQuadInstance {
                    rect: [clipped_x, clipped_y, clipped_w, clipped_h],
                    clip: [0.0, 0.0, phys_w as f32, phys_h as f32],
                    color: [1.0, 1.0, 1.0, 1.0],
                    secondary_color: [0.0; 4],
                    radii: [0.0; 4],
                    stroke_or_blur: 0.0,
                    angle_rad: 0.0,
                    kind: GpuPrimitiveKind::TexturedQuad as u32,
                    reserved: 1,
                };
                let tile_prim: GpuPrimitiveInstance =
                    GpuPrimitiveInstance::from_quad_instance(&tile_quad, 0, 0);
                batch.get_mut_instances().push(tile_quad);
                batch.get_mut_primitives().push(tile_prim);
            }
            if sparse_damage.bounds_rect().is_some() {
                let (tiles, pixels): (usize, usize) =
                    self.get_mut_software().render(display_list, sparse_damage);
                cpu_tiles = tiles;
                cpu_pixels = pixels;
            }
        }
        self.set_last_cpu_effect_regions(partition.get_cpu_effect_regions().clone());
        self.set_last_gpu_native_commands(partition.get_gpu_native_count());
        self.set_last_cpu_tile_count(cpu_tiles);
        self.set_last_cpu_upload_bytes(cpu_upload_bytes);
        self.set_last_cpu_rasterized_pixels(cpu_pixels);
        let submitted: usize =
            self.get_mut_compositor()
                .submit_batches(phys_w, phys_h, clear_color, &[batch])?;
        let total_pixels: usize = if cpu_pixels > 0 {
            partition
                .get_gpu_covered_pixels()
                .saturating_add(cpu_pixels)
        } else {
            partition.get_gpu_covered_pixels().max(1)
        };
        Ok((submitted.max(1), total_pixels))
    }
}