oxframe-render 0.20.4

Renderers for oxframe scenes: tiny-skia on the CPU, vello on the GPU
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
use std::collections::BTreeMap;

use kurbo::{Affine, BezPath, PathEl, Shape};
use oxframe_2d::{
    DrawItem, Layer, LineCap, LineJoin, Paint as MPaint, Primitive, RasterImage, SceneGraph,
    StrokeStyle,
};
use oxframe_core::{BlendMode, Color, Gradient, GradientKind};
use tiny_skia::{
    FillRule, FilterQuality, GradientStop, LinearGradient, Mask, Paint, PathBuilder, Pixmap,
    PixmapPaint, RadialGradient, SpreadMode, Stroke, StrokeDash, Transform,
};

use crate::blur::gaussian_blur;
use crate::{Image, RenderOptions, Renderer, scene_to_pixels};

/// tiny-skia backend. Deterministic: same scene, same bytes.
#[derive(Default)]
pub struct CpuRenderer {
    pixmap: Option<Pixmap>,
    images: BTreeMap<u64, Pixmap>,
    /// Reused layer buffers.
    pool: Vec<Pixmap>,
    /// Pre-composite pixels of layers whose vectors did not change.
    layers: Vec<CachedLayer>,
    /// Items left to ignore because their layer was replayed from [`Self::layers`].
    skip: u32,
}

struct CachedLayer {
    key: u64,
    region: Region,
    buf: Pixmap,
    seen: bool,
}

impl CpuRenderer {
    /// A CPU renderer; buffers are reused between frames.
    pub fn new() -> Self {
        Self::default()
    }

    /// Renders draw items (with `view` mapping scene → pixels) into a
    /// transparent pixmap. Used by the GPU renderer for filtered layers.
    pub fn render_items(&mut self, items: &[DrawItem], view: Affine, w: u32, h: u32) -> Pixmap {
        let mut target = Pixmap::new(w.max(1), h.max(1)).expect("valid pixmap size");
        self.draw_items(&mut target, items, view);
        target
    }

    fn take_layer(&mut self, w: u32, h: u32) -> Pixmap {
        let (w, h) = (w.max(1), h.max(1));
        match self
            .pool
            .iter()
            .position(|p| p.width() == w && p.height() == h)
        {
            Some(i) => {
                let mut p = self.pool.swap_remove(i);
                p.fill(tiny_skia::Color::TRANSPARENT);
                p
            }
            None => Pixmap::new(w, h).expect("valid pixmap size"),
        }
    }

    fn draw_items(&mut self, base: &mut Pixmap, items: &[DrawItem], view: Affine) {
        let full = Region {
            x: 0,
            y: 0,
            w: base.width(),
            h: base.height(),
        };
        for slot in &mut self.layers {
            slot.seen = false;
        }
        self.skip = 0;
        // Open layers: buffer, the region it covers, layer, transform, cache key, replayed.
        let mut stack: Vec<(Pixmap, Region, Layer, Affine, u64, bool)> = Vec::new();
        for (i, item) in items.iter().enumerate() {
            let tf = view * item.transform;
            if self.skip > 0 {
                match &item.prim {
                    Primitive::PushLayer(_) => self.skip += 1,
                    Primitive::PopLayer => {
                        self.skip -= 1;
                        if self.skip == 0 {
                            self.pop_layer(&mut stack, base, &full);
                        }
                    }
                    _ => {}
                }
                continue;
            }
            match &item.prim {
                Primitive::PushLayer(layer) => {
                    let parent = stack.last().map_or(full, |(_, r, _, _, _, _)| *r);
                    let region = layer_region(items, i, view, parent).unwrap_or(Region {
                        x: parent.x,
                        y: parent.y,
                        w: 1,
                        h: 1,
                    });
                    let key = cache_key(oxframe_2d::layer_fingerprint(items, i), region);
                    if let Some(slot) = self
                        .layers
                        .iter_mut()
                        .find(|slot| slot.key == key && slot.region == region)
                    {
                        slot.seen = true;
                        stack.push((slot.buf.clone(), region, layer.clone(), tf, key, true));
                        self.skip = 1;
                    } else {
                        let buf = self.take_layer(region.w, region.h);
                        stack.push((buf, region, layer.clone(), tf, key, false));
                    }
                }
                Primitive::PopLayer => self.pop_layer(&mut stack, base, &full),
                prim => {
                    let (dst, region) = match stack.last_mut() {
                        Some((p, r, _, _, _, _)) => (p, *r),
                        None => (&mut *base, full),
                    };
                    draw_prim(dst, prim, region.shift() * tf, &mut self.images);
                }
            }
        }
        while !stack.is_empty() {
            self.pop_layer(&mut stack, base, &full);
        }
        self.layers.retain(|slot| slot.seen);
        self.skip = 0;
    }

    fn pop_layer(
        &mut self,
        stack: &mut Vec<(Pixmap, Region, Layer, Affine, u64, bool)>,
        base: &mut Pixmap,
        full: &Region,
    ) {
        let Some((buf, region, layer, ltf, key, replay)) = stack.pop() else {
            return;
        };
        if !replay {
            if let Some(slot) = self.layers.iter_mut().find(|slot| slot.key == key) {
                slot.buf = buf.clone();
                slot.region = region;
                slot.seen = true;
            } else {
                self.layers.push(CachedLayer {
                    key,
                    region,
                    buf: buf.clone(),
                    seen: true,
                });
            }
        }
        let (dst, dst_region) = match stack.last_mut() {
            Some((p, r, _, _, _, _)) => (p, *r),
            None => (&mut *base, *full),
        };
        composite(dst, dst_region, buf, region, &layer, ltf, &mut self.pool);
    }
}

/// A pixel rectangle of the output frame.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Region {
    x: i32,
    y: i32,
    w: u32,
    h: u32,
}

impl Region {
    /// Maps frame pixels to this region's buffer.
    fn shift(self) -> Affine {
        Affine::translate((-f64::from(self.x), -f64::from(self.y)))
    }
}

fn cache_key(fingerprint: u64, region: Region) -> u64 {
    fingerprint
        ^ (region.w as u64).wrapping_shl(1)
        ^ (region.h as u64).wrapping_shl(17)
        ^ (region.x as u32 as u64).wrapping_shl(33)
        ^ (region.y as u32 as u64).wrapping_shl(49)
}

/// Pixel bounds of everything between the `PushLayer` at `start` and its
/// `PopLayer`, grown by the reach of blurs and shadows, clipped to `within`.
fn layer_region(items: &[DrawItem], start: usize, view: Affine, within: Region) -> Option<Region> {
    let mut bb: Option<kurbo::Rect> = None;
    let mut grow = 0.0f64;
    let mut depth = 0usize;
    for item in &items[start..] {
        let tf = view * item.transform;
        let r = match &item.prim {
            Primitive::PushLayer(l) => {
                depth += 1;
                let k = scale_of(tf);
                grow += 3.0 * l.blur * k;
                if let Some(sh) = &l.shadow {
                    let off = linear(tf) * sh.offset.to_point();
                    grow += 3.0 * sh.blur * k + off.x.abs().max(off.y.abs());
                }
                None
            }
            Primitive::PopLayer => {
                depth -= 1;
                if depth == 0 {
                    break;
                }
                None
            }
            Primitive::Fill { path, .. } => Some(tf.transform_rect_bbox(path.bounding_box())),
            Primitive::Stroke { path, style, .. } => {
                let pad = style.width * scale_of(tf);
                Some(
                    tf.transform_rect_bbox(path.bounding_box())
                        .inflate(pad, pad),
                )
            }
            Primitive::Image { rect, .. } => Some(tf.transform_rect_bbox(*rect)),
            Primitive::ReadFrame(_) | Primitive::BeginEffect(_) | Primitive::EndEffect => None,
        };
        if let Some(r) = r {
            bb = Some(bb.map_or(r, |b| b.union(r)));
        }
    }
    let r = bb?.inflate(grow + 2.0, grow + 2.0);
    let x0 = r.x0.floor().max(f64::from(within.x)) as i32;
    let y0 = r.y0.floor().max(f64::from(within.y)) as i32;
    let x1 = r.x1.ceil().min(f64::from(within.x) + f64::from(within.w)) as i32;
    let y1 = r.y1.ceil().min(f64::from(within.y) + f64::from(within.h)) as i32;
    (x1 > x0 && y1 > y0).then(|| Region {
        x: x0,
        y: y0,
        w: (x1 - x0) as u32,
        h: (y1 - y0) as u32,
    })
}

fn scale_of(tf: Affine) -> f64 {
    tf.determinant().abs().sqrt()
}

fn composite(
    dst: &mut Pixmap,
    dst_region: Region,
    mut buf: Pixmap,
    region: Region,
    layer: &Layer,
    tf: Affine,
    pool: &mut Vec<Pixmap>,
) {
    let (w, h) = (buf.width() as usize, buf.height() as usize);
    let k = scale_of(tf);
    if let Some(clip) = &layer.clip
        && let Some(path) = to_skia_path(clip)
        && let Some(mut mask) = Mask::new(buf.width(), buf.height())
    {
        mask.fill_path(
            &path,
            FillRule::Winding,
            true,
            to_skia_transform(region.shift() * tf),
        );
        buf.apply_mask(&mask);
    }
    // Where this buffer goes in the destination buffer.
    let (dx, dy) = (region.x - dst_region.x, region.y - dst_region.y);
    if layer.blur > 0.0 {
        gaussian_blur(buf.data_mut(), w, h, layer.blur * k);
    }
    let paint = PixmapPaint {
        opacity: layer.opacity.clamp(0.0, 1.0) as f32,
        blend_mode: blend_mode(layer.blend),
        quality: FilterQuality::Nearest,
    };
    if let Some(shadow) = &layer.shadow {
        // Shadow = the layer's coverage tinted, blurred and offset.
        let mut sh = buf.clone();
        let [r, g, b, a] = shadow.color.to_rgba8().map(u32::from);
        for px in sh.data_mut().chunks_exact_mut(4) {
            let cov = u32::from(px[3]) * a / 255;
            px[0] = (r * cov / 255) as u8;
            px[1] = (g * cov / 255) as u8;
            px[2] = (b * cov / 255) as u8;
            px[3] = cov as u8;
        }
        gaussian_blur(sh.data_mut(), w, h, shadow.blur * k);
        let off = linear(tf) * shadow.offset.to_point();
        dst.draw_pixmap(
            dx,
            dy,
            sh.as_ref(),
            &paint,
            Transform::from_translate(off.x as f32, off.y as f32),
            None,
        );
        pool.push(sh);
    }
    dst.draw_pixmap(dx, dy, buf.as_ref(), &paint, Transform::identity(), None);
    pool.push(buf);
}

/// The linear part of an affine map (for offsets).
fn linear(tf: Affine) -> Affine {
    let [a, b, c, d, _, _] = tf.as_coeffs();
    Affine::new([a, b, c, d, 0.0, 0.0])
}

pub(crate) fn blend_mode(b: BlendMode) -> tiny_skia::BlendMode {
    use tiny_skia::BlendMode as T;
    match b {
        BlendMode::Normal => T::SourceOver,
        BlendMode::Multiply => T::Multiply,
        BlendMode::Screen => T::Screen,
        BlendMode::Overlay => T::Overlay,
        BlendMode::Darken => T::Darken,
        BlendMode::Lighten => T::Lighten,
        BlendMode::ColorDodge => T::ColorDodge,
        BlendMode::ColorBurn => T::ColorBurn,
        BlendMode::HardLight => T::HardLight,
        BlendMode::SoftLight => T::SoftLight,
        BlendMode::Difference => T::Difference,
        BlendMode::Exclusion => T::Exclusion,
        BlendMode::Hue => T::Hue,
        BlendMode::Saturation => T::Saturation,
        BlendMode::Color => T::Color,
        BlendMode::Luminosity => T::Luminosity,
    }
}

fn draw_prim(dst: &mut Pixmap, prim: &Primitive, tf: Affine, images: &mut BTreeMap<u64, Pixmap>) {
    match prim {
        Primitive::Fill { path, paint } => {
            if let (Some(path), Some(p)) = (to_skia_path(path), skia_paint(paint)) {
                dst.fill_path(&path, &p, FillRule::Winding, to_skia_transform(tf), None);
            }
        }
        Primitive::Stroke { path, paint, style } => {
            if let (Some(path), Some(p)) = (to_skia_path(path), skia_paint(paint)) {
                dst.stroke_path(&path, &p, &skia_stroke(style), to_skia_transform(tf), None);
            }
        }
        Primitive::Image {
            image,
            rect,
            opacity,
        } => {
            let src = match images.entry(image.id) {
                std::collections::btree_map::Entry::Occupied(e) => e.into_mut(),
                std::collections::btree_map::Entry::Vacant(e) => {
                    let Some(pm) = to_pixmap(image) else { return };
                    e.insert(pm)
                }
            };
            let place = tf
                * Affine::translate((rect.x0, rect.y0))
                * Affine::scale_non_uniform(
                    rect.width() / f64::from(image.width),
                    rect.height() / f64::from(image.height),
                );
            let paint = PixmapPaint {
                opacity: *opacity as f32,
                quality: FilterQuality::Bicubic,
                ..Default::default()
            };
            dst.draw_pixmap(0, 0, src.as_ref(), &paint, to_skia_transform(place), None);
        }
        Primitive::PushLayer(_)
        | Primitive::PopLayer
        | Primitive::ReadFrame(_)
        | Primitive::BeginEffect(_)
        | Primitive::EndEffect => {}
    }
}

pub(crate) fn to_skia_path(path: &BezPath) -> Option<tiny_skia::Path> {
    let mut pb = PathBuilder::new();
    for el in path.elements() {
        match *el {
            PathEl::MoveTo(p) => pb.move_to(p.x as f32, p.y as f32),
            PathEl::LineTo(p) => pb.line_to(p.x as f32, p.y as f32),
            PathEl::QuadTo(a, p) => pb.quad_to(a.x as f32, a.y as f32, p.x as f32, p.y as f32),
            PathEl::CurveTo(a, b, p) => pb.cubic_to(
                a.x as f32, a.y as f32, b.x as f32, b.y as f32, p.x as f32, p.y as f32,
            ),
            PathEl::ClosePath => pb.close(),
        }
    }
    pb.finish()
}

fn to_skia_transform(a: Affine) -> Transform {
    let [a, b, c, d, e, f] = a.as_coeffs().map(|v| v as f32);
    Transform::from_row(a, b, c, d, e, f)
}

fn skia_color(c: Color) -> Option<tiny_skia::Color> {
    tiny_skia::Color::from_rgba(
        c.r.clamp(0.0, 1.0),
        c.g.clamp(0.0, 1.0),
        c.b.clamp(0.0, 1.0),
        c.a.clamp(0.0, 1.0),
    )
}

fn shader(g: &Gradient) -> Option<tiny_skia::Shader<'static>> {
    let stops: Vec<GradientStop> = g
        .stops
        .iter()
        .filter_map(|(o, c)| Some(GradientStop::new(*o as f32, skia_color(*c)?)))
        .collect();
    let pt = |p: kurbo::Point| tiny_skia::Point::from_xy(p.x as f32, p.y as f32);
    match g.kind {
        GradientKind::Linear { from, to } => LinearGradient::new(
            pt(from),
            pt(to),
            stops,
            SpreadMode::Pad,
            Transform::identity(),
        ),
        GradientKind::Radial { center, radius } => RadialGradient::new(
            pt(center),
            0.0,
            pt(center),
            radius.max(1e-3) as f32,
            stops,
            SpreadMode::Pad,
            Transform::identity(),
        ),
    }
}

fn skia_paint(p: &MPaint) -> Option<Paint<'static>> {
    let mut paint = Paint::default();
    match p {
        MPaint::Solid(c) => paint.set_color(skia_color(*c)?),
        MPaint::Gradient(g) => paint.shader = shader(g)?,
    }
    paint.anti_alias = true;
    Some(paint)
}

fn skia_stroke(s: &StrokeStyle) -> Stroke {
    Stroke {
        width: s.width as f32,
        line_cap: match s.cap {
            LineCap::Butt => tiny_skia::LineCap::Butt,
            LineCap::Round => tiny_skia::LineCap::Round,
            LineCap::Square => tiny_skia::LineCap::Square,
        },
        line_join: match s.join {
            LineJoin::Miter => tiny_skia::LineJoin::Miter,
            LineJoin::Round => tiny_skia::LineJoin::Round,
            LineJoin::Bevel => tiny_skia::LineJoin::Bevel,
        },
        dash: (s.dash.len() >= 2 && s.dash.iter().any(|d| *d > 0.0))
            .then(|| {
                // tiny-skia needs an even count.
                let mut d: Vec<f32> = s.dash.iter().map(|v| *v as f32).collect();
                if d.len() % 2 == 1 {
                    d.extend_from_within(..);
                }
                StrokeDash::new(d, s.dash_offset as f32)
            })
            .flatten(),
        ..Default::default()
    }
}

fn to_pixmap(img: &RasterImage) -> Option<Pixmap> {
    let mut pm = Pixmap::new(img.width, img.height)?;
    for (dst, src) in pm.pixels_mut().iter_mut().zip(img.rgba.chunks_exact(4)) {
        *dst = tiny_skia::ColorU8::from_rgba(src[0], src[1], src[2], src[3]).premultiply();
    }
    Some(pm)
}

/// Converts a premultiplied pixmap to straight-alpha RGBA8.
pub(crate) fn demultiply(pixmap: &Pixmap) -> Vec<u8> {
    let mut data = Vec::with_capacity(pixmap.data().len());
    for px in pixmap.pixels() {
        let c = px.demultiply();
        data.extend_from_slice(&[c.red(), c.green(), c.blue(), c.alpha()]);
    }
    data
}

impl CpuRenderer {
    /// Drops decoded images that no item in `used` draws.
    pub(crate) fn keep_images(&mut self, used: &std::collections::BTreeSet<u64>) {
        self.images.retain(|id, _| used.contains(id));
    }
}

impl Renderer for CpuRenderer {
    fn render(&mut self, graph: &SceneGraph, options: &RenderOptions) -> Image {
        let items = graph.draw_list();
        self.render_list(graph, &items, options)
    }

    fn render_list(
        &mut self,
        graph: &SceneGraph,
        items: &[DrawItem],
        options: &RenderOptions,
    ) -> Image {
        let (w, h) = (options.width.max(1), options.height.max(1));
        let mut base = match self.pixmap.take() {
            Some(p) if p.width() == w && p.height() == h => p,
            _ => Pixmap::new(w, h).expect("valid pixmap size"),
        };
        let bg = options.background;
        base.fill(skia_color(bg).unwrap_or(tiny_skia::Color::BLACK));
        let view = scene_to_pixels(graph.size(), (w, h));
        self.draw_items(&mut base, items, view);
        self.keep_images(&crate::image_ids(items));
        let data = demultiply(&base);
        self.pixmap = Some(base);
        Image {
            width: w,
            height: h,
            data,
        }
    }
}

#[cfg(test)]
mod tests {
    use oxframe_2d::{NodeBuilder, Rect, View};

    use super::*;

    #[test]
    fn draws_centered_rect() {
        let graph = SceneGraph::new(100, 100);
        graph.enter(|| {
            View::current().add(
                Rect::new()
                    .size(50.0)
                    .fill(Color::from_hex("#ff0000").unwrap()),
            );
        });
        let img = CpuRenderer::new().render(
            &graph,
            &RenderOptions {
                width: 100,
                height: 100,
                background: Color::BLACK,
            },
        );
        let px = |x: usize, y: usize| &img.data[(y * 100 + x) * 4..][..4];
        assert_eq!(px(50, 50), [255, 0, 0, 255]);
        assert_eq!(px(10, 10), [0, 0, 0, 255]);
        assert_eq!(px(74, 50), [255, 0, 0, 255]);
        assert_eq!(px(76, 50), [0, 0, 0, 255]);
    }

    #[test]
    fn a_static_blur_replays_the_same_bytes() {
        let graph = SceneGraph::new(64, 64);
        graph.enter(|| {
            View::current().add(
                Rect::new()
                    .size(24.0)
                    .fill(Color::from_hex("#ff0000").unwrap())
                    .blur(1.5),
            );
        });
        let opts = RenderOptions {
            width: 64,
            height: 64,
            background: Color::BLACK,
        };
        let mut cached = CpuRenderer::new();
        let first = cached.render(&graph, &opts);
        let second = cached.render(&graph, &opts);
        let fresh = CpuRenderer::new().render(&graph, &opts);
        assert_eq!(first.data, fresh.data);
        assert_eq!(second.data, fresh.data);
    }

    #[test]
    fn a_moved_blur_is_not_served_from_the_old_buffer() {
        let opts = RenderOptions {
            width: 64,
            height: 64,
            background: Color::BLACK,
        };
        let at = |x: f64| {
            let graph = SceneGraph::new(64, 64);
            graph.enter(|| {
                View::current().add(Rect::new().size(16.0).x(x).fill(Color::WHITE).blur(1.0));
            });
            graph
        };
        let mut renderer = CpuRenderer::new();
        let before = renderer.render(&at(0.0), &opts);
        let after = renderer.render(&at(18.0), &opts);
        assert_ne!(before.data, after.data);
    }
}