cranpose-render-pixels 0.22.0

Software renderer for Cranpose scenes and RGBA framebuffers
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
use std::{ops::Range, rc::Rc, sync::Arc};

use cranpose_core::NodeId;
pub use cranpose_render_common::graph_scene::{HitRegion, Scene};
use cranpose_render_common::{
    glyph_reach::{InkSpread, glyph_part},
    graph::{InlineNode, LayerChange, LayerNode, ProjectiveTransform, RenderNode},
    layer_shadow::{ShadowLight, ShadowRRect},
    primitive_emit::ShapeDrawParams,
};
use cranpose_ui::TextStyle;
use cranpose_ui_graphics::{
    ArcGeometry, BlendMode, Brush, Color, ColorFilter, ImageBitmap, ImageSampling, LineGeometry,
    Point, Rect, RoundedCornerShape, Stroke, Trapezoid,
};

use crate::{
    draw::RenderItem,
    rewrite::Rewrite,
    scene_item::{ItemRef, SceneItem},
};

#[derive(Clone, Debug)]
pub(crate) struct DrawShape {
    pub rect: Rect,
    pub snap_anchor: Option<Point>,
    pub snap_to_pixel_grid: bool,
    pub brush: Brush,
    pub outline: Outline,
    pub clip: Option<Rect>,
    pub blend_mode: BlendMode,
}

// What a shape draws of its rect. The parameters a shape comes from hold
// one geometry at a time, so one enum keeps it.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum Outline {
    // The rect, or the shape rounded within it, filled.
    Fill(Option<RoundedCornerShape>),
    // The edge of the rect, or of the shape rounded within it.
    Stroke(Option<RoundedCornerShape>, Stroke),
    Arc(ArcGeometry),
    Line(LineGeometry),
    Trapezoid(Trapezoid),
}

impl Outline {
    // The rounded shape a fill or a stroke follows.
    pub(crate) fn rounded(self) -> Option<RoundedCornerShape> {
        match self {
            Self::Fill(shape) | Self::Stroke(shape, _) => shape,
            Self::Arc(_) | Self::Line(_) | Self::Trapezoid(_) => None,
        }
    }

    fn translated(self, dx: f32, dy: f32) -> Self {
        match self {
            Self::Arc(arc) => Self::Arc(ArcGeometry {
                center: shifted(arc.center, dx, dy),
                ..arc
            }),
            Self::Line(line) => {
                Self::Line(line.placed(shifted(line.start, dx, dy), shifted(line.end, dx, dy), 1.0))
            }
            Self::Trapezoid(trapezoid) => Self::Trapezoid(trapezoid.translate(dx, dy)),
            Self::Fill(_) | Self::Stroke(..) => self,
        }
    }

    // The geometry of `params`: a line, a slice and an arc each stand for
    // the whole shape, and a stroke or a fill follows its rounded shape.
    fn of_params(params: &ShapeDrawParams) -> Self {
        match (params.line, params.trapezoid, params.arc, params.stroke) {
            (Some(line), ..) => Self::Line(line),
            (None, Some(trapezoid), ..) => Self::Trapezoid(trapezoid),
            (None, None, Some(arc), _) => Self::Arc(arc),
            (None, None, None, Some(stroke)) => Self::Stroke(params.shape, stroke),
            (None, None, None, None) => Self::Fill(params.shape),
        }
    }
}

/// A point moved by `dx`, `dy`.
fn shifted(point: Point, dx: f32, dy: f32) -> Point {
    Point::new(point.x + dx, point.y + dy)
}

impl DrawShape {
    /// The shape moved by `dx`, `dy`, with no clip.
    pub(crate) fn translated(&self, dx: f32, dy: f32) -> Self {
        Self {
            rect: self.rect.translate(dx, dy),
            snap_anchor: self.snap_anchor.map(|anchor| shifted(anchor, dx, dy)),
            outline: self.outline.translated(dx, dy),
            clip: None,
            ..self.clone()
        }
    }

    /// The shape `params` resolved.
    pub(crate) fn of_params(params: ShapeDrawParams) -> Self {
        Self {
            rect: params.rect,
            snap_anchor: None,
            snap_to_pixel_grid: false,
            outline: Outline::of_params(&params),
            brush: params.brush.into_brush(),
            clip: params.clip,
            blend_mode: params.blend_mode,
        }
    }
}

#[derive(Clone, Debug)]
pub(crate) struct TextDraw {
    pub rect: Rect,
    pub snap_anchor: Option<Point>,
    pub text: Rc<cranpose_ui::text::AnnotatedString>,
    // The lines and glyphs, from `rect`'s top left corner.
    pub layout: cranpose_text::TextLayout,
    // Whether the glyphs paint or only their shadows.
    pub ink: cranpose_render_common::text_raster::GlyphInk,
    pub color: Color,
    pub text_style: Arc<TextStyle>,
    pub font_size: f32,
    pub scale: f32,
    pub clip: Option<Rect>,
}

#[derive(Clone)]
pub(crate) struct ImageDraw {
    pub rect: Rect,
    pub snap_anchor: Option<Point>,
    pub image: ImageBitmap,
    pub alpha: f32,
    pub color_filter: Option<ColorFilter>,
    pub sampling: ImageSampling,
    pub clip: Option<Rect>,
    pub blend_mode: BlendMode,
    pub src_rect: Option<Rect>,
    /// Maps `rect`'s space to device pixels for an image drawn turned or
    /// mirrored; `None` when `rect` is in device pixels.
    pub transform: Option<ProjectiveTransform>,
}

/// One pass of a layer's elevation shadow: Skia's round rect shadow in
/// device pixels, its color and the clip it is cut to.
#[derive(Clone, Copy, Debug)]
pub(crate) struct ShadowDraw {
    pub shadow: ShadowRRect,
    pub snap_anchor: Option<Point>,
    pub color: Color,
    pub clip: Option<Rect>,
}

impl TextDraw {
    // The part of `rect`, where the text draws from its top left corner,
    // in which its glyphs and their shadows draw.
    pub(crate) fn glyph_reach(&self, rect: Rect) -> Rect {
        let spread = InkSpread::of_text(&self.text_style, &self.text.span_styles, &self.layout);
        glyph_part(rect, &self.layout, self.scale, spread)
    }

    /// The text moved by `dx`, `dy`, with no clip.
    pub(crate) fn translated(&self, dx: f32, dy: f32) -> Self {
        Self {
            rect: self.rect.translate(dx, dy),
            snap_anchor: self.snap_anchor.map(|anchor| shifted(anchor, dx, dy)),
            clip: None,
            ..self.clone()
        }
    }
}

impl ImageDraw {
    /// The image moved by `dx`, `dy`, with no clip.
    pub(crate) fn translated(&self, dx: f32, dy: f32) -> Self {
        let (rect, transform) = match self.transform {
            Some(transform) => (
                self.rect,
                Some(transform.then(ProjectiveTransform::translation(dx, dy))),
            ),
            None => (self.rect.translate(dx, dy), None),
        };
        Self {
            rect,
            transform,
            snap_anchor: self.snap_anchor.map(|anchor| shifted(anchor, dx, dy)),
            clip: None,
            ..self.clone()
        }
    }
}

impl ShadowDraw {
    /// The shadow moved by `dx`, `dy`, with no clip.
    pub(crate) fn translated(&self, dx: f32, dy: f32) -> Self {
        Self {
            shadow: self.shadow.translated(dx, dy),
            snap_anchor: self.snap_anchor.map(|anchor| shifted(anchor, dx, dy)),
            clip: None,
            ..*self
        }
    }
}

/// How many items of each kind a scene holds where a run of its items starts
/// or stops.
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub(crate) struct ItemCounts {
    pub order: usize,
    pub shapes: usize,
    pub images: usize,
    pub texts: usize,
    pub shadows: usize,
}

impl ItemCounts {
    /// How many items of each kind lie from `start` up to `self`.
    pub(crate) fn since(self, start: Self) -> Self {
        Self {
            order: self.order - start.order,
            shapes: self.shapes - start.shapes,
            images: self.images - start.images,
            texts: self.texts - start.texts,
            shadows: self.shadows - start.shadows,
        }
    }

    // The counts after a run of `removed` items before them gave way to a
    // run of `added`.
    pub(crate) fn shifted(self, removed: Self, added: Self) -> Self {
        Self {
            order: self.order - removed.order + added.order,
            shapes: self.shapes - removed.shapes + added.shapes,
            images: self.images - removed.images + added.images,
            texts: self.texts - removed.texts + added.texts,
            shadows: self.shadows - removed.shadows + added.shadows,
        }
    }

    /// The counts of a run copied from where `from` began in one scene to
    /// where `to` begins in another.
    pub(crate) fn moved(self, from: Self, to: Self) -> Self {
        Self {
            order: to.order + self.order.saturating_sub(from.order),
            shapes: to.shapes + self.shapes.saturating_sub(from.shapes),
            images: to.images + self.images.saturating_sub(from.images),
            texts: to.texts + self.texts.saturating_sub(from.texts),
            shadows: to.shadows + self.shadows.saturating_sub(from.shadows),
        }
    }
}

/// How much of a scene a layer's subtree holds: its records, its items of
/// each kind, and the entries of what its layers passed down. Subtrees of
/// one extent fit each other's places wherever each lies in its scene.
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) struct SubtreeExtent {
    pub records: usize,
    pub items: ItemCounts,
    pub passed_down: usize,
}

/// What a layer passes down to the walks of its child layers.
#[derive(Clone, PartialEq)]
pub(crate) struct PassedDown {
    pub transform: ProjectiveTransform,
    pub alpha: f32,
    pub color_filter: Option<ColorFilter>,
    pub clip: Option<Rect>,
    pub snap_anchor: Option<Point>,
    pub translated: bool,
}

// A node of the graph a scene keeps a record of: a layer with its subtree,
// or a node drawn in its parent's layer.
#[derive(Clone, Copy)]
pub(crate) enum Subtree<'a> {
    Layer(&'a LayerNode),
    Inline(&'a InlineNode),
}

impl<'a> Subtree<'a> {
    // The node `child`, a child of a layer, draws, when it draws one.
    pub(crate) fn of_child(child: &'a RenderNode) -> Option<Self> {
        match child {
            RenderNode::Layer(layer) => Some(Self::Layer(layer)),
            RenderNode::Inline(inline) => Some(Self::Inline(inline)),
            RenderNode::Primitive(_) | RenderNode::DrawRun(_) => None,
        }
    }

    // The node and the node whose outer draws it wraps.
    pub(crate) fn identity(self) -> (Option<NodeId>, Option<NodeId>) {
        match self {
            Self::Layer(layer) => (layer.node_id, layer.wraps),
            Self::Inline(inline) => (Some(inline.node_id), None),
        }
    }

    // The change number, which covers the subtree.
    pub(crate) fn change(self) -> LayerChange {
        match self {
            Self::Layer(layer) => layer.change,
            Self::Inline(inline) => inline.change,
        }
    }

    // The node's own change number, which a change beneath it leaves.
    pub(crate) fn own_change(self) -> LayerChange {
        match self {
            Self::Layer(layer) => layer.own_change,
            Self::Inline(inline) => inline.change,
        }
    }

    // The node's placement in its parent's layer.
    pub(crate) fn transform_to_parent(self) -> ProjectiveTransform {
        match self {
            Self::Layer(layer) => layer.transform_to_parent,
            Self::Inline(inline) => inline.transform_to_parent(),
        }
    }
}

/// The items a layer's subtree pushed into a scene, in a run of each list,
/// so the next scene can copy them when nothing they depend on changed.
#[derive(Clone, Copy)]
pub(crate) struct SubtreeItems {
    /// The layer's node and the node whose outer draws it wraps.
    pub identity: (Option<NodeId>, Option<NodeId>),
    /// The layer's change number, which covers its subtree, when it pushed
    /// the items.
    pub change: LayerChange,
    /// The layer's own change number, which a change beneath it leaves.
    pub own_change: LayerChange,
    /// The layer's placement in its parent, which the number leaves out.
    pub transform: ProjectiveTransform,
    /// Where [`RasterScene::passed_down`] holds what the layer passed to its
    /// child layers, when it has any.
    pub passed_down: Option<usize>,
    /// The index of the first record after the subtree's, its descendants'
    /// included.
    pub end: usize,
    pub start: ItemCounts,
    pub stop: ItemCounts,
}

pub(crate) struct RasterScene {
    pub shapes: Vec<DrawShape>,
    pub images: Vec<ImageDraw>,
    pub texts: Vec<TextDraw>,
    pub shadows: Vec<ShadowDraw>,
    /// Every item in the order it was pushed, which is the order it draws in.
    pub order: Vec<RenderItem>,
    /// A record for each layer the scene was built from, in the order the
    /// layers were visited: a layer's descendants follow it.
    pub subtrees: Vec<SubtreeItems>,
    /// What the layers with child layers passed down to them.
    pub passed_down: Vec<PassedDown>,
    /// The scale and the shadow light the walk started from.
    pub start: Option<(f32, ShadowLight)>,
    /// The runs of `order` copied in place from the scene the walk started
    /// from, ascending: their items equal that scene's at the same indices.
    pub(crate) kept: Vec<Range<usize>>,
    // The snap anchor of the layer whose draws push now, which their items
    // take.
    pub(crate) snap_anchor: Option<Point>,
    // While a changed subtree pushes again over its old items: where.
    pub(crate) rewrite: Option<Box<Rewrite>>,
}

#[expect(clippy::multiple_inherent_impl, reason = "item reach in draw/reach")]
impl RasterScene {
    pub(crate) fn new() -> Self {
        Self {
            shapes: Vec::new(),
            images: Vec::new(),
            texts: Vec::new(),
            shadows: Vec::new(),
            order: Vec::new(),
            subtrees: Vec::new(),
            passed_down: Vec::new(),
            start: None,
            kept: Vec::new(),
            snap_anchor: None,
            rewrite: None,
        }
    }

    /// Empties the scene and keeps its storage.
    pub(crate) fn clear(&mut self) {
        self.shapes.clear();
        self.images.clear();
        self.texts.clear();
        self.shadows.clear();
        self.order.clear();
        self.subtrees.clear();
        self.passed_down.clear();
        self.start = None;
        self.kept.clear();
    }

    pub(crate) fn counts(&self) -> ItemCounts {
        if let Some(rewrite) = &self.rewrite {
            return rewrite.counts();
        }
        ItemCounts {
            order: self.order.len(),
            shapes: self.shapes.len(),
            images: self.images.len(),
            texts: self.texts.len(),
            shadows: self.shadows.len(),
        }
    }

    // The item `item` of the draw order stands for.
    pub(crate) fn item(&self, item: RenderItem) -> ItemRef<'_> {
        match item {
            RenderItem::Shape(index) => ItemRef::Shape(&self.shapes[index]),
            RenderItem::Image(index) => ItemRef::Image(&self.images[index]),
            RenderItem::Text(index) => ItemRef::Text(&self.texts[index]),
            RenderItem::Shadow(index) => ItemRef::Shadow(&self.shadows[index]),
        }
    }

    /// Opens the record of `subtree`, whose items come next, and returns its
    /// index for [`Self::close_subtree`].
    pub(crate) fn open_subtree(&mut self, subtree: Subtree<'_>) -> usize {
        let counts = self.counts();
        let record = SubtreeItems {
            identity: subtree.identity(),
            change: subtree.change(),
            own_change: subtree.own_change(),
            transform: subtree.transform_to_parent(),
            passed_down: None,
            end: 0,
            start: counts,
            stop: counts,
        };
        if let Some(rewrite) = self.rewrite.as_deref_mut() {
            return rewrite.place_record(&mut self.subtrees, record);
        }
        let index = self.subtrees.len();
        self.subtrees.push(SubtreeItems {
            end: index + 1,
            ..record
        });
        index
    }

    /// Notes in the record `index` what its layer passes down.
    pub(crate) fn note_passed_down(&mut self, index: usize, passed_down: PassedDown) {
        let at = match self.rewrite.as_deref_mut() {
            Some(rewrite) => rewrite.place_passed_down(&mut self.passed_down, passed_down),
            None => {
                self.passed_down.push(passed_down);
                self.passed_down.len() - 1
            }
        };
        if let Some(record) = self.record_mut(index) {
            record.passed_down = Some(at);
        }
    }

    /// Closes the record `index`: the subtree pushed everything since it
    /// opened.
    pub(crate) fn close_subtree(&mut self, index: usize) {
        let end = self
            .rewrite
            .as_deref()
            .map_or(self.subtrees.len(), Rewrite::record_count);
        let stop = self.counts();
        if let Some(record) = self.record_mut(index) {
            record.end = end;
            record.stop = stop;
        }
    }

    // The record `index`, wherever a rewrite put it.
    fn record_mut(&mut self, index: usize) -> Option<&mut SubtreeItems> {
        match self.rewrite.as_deref_mut() {
            Some(rewrite) => rewrite.record_mut(&mut self.subtrees, index),
            None => self.subtrees.get_mut(index),
        }
    }

    /// Pushes the items and records of the subtrees `old` recorded at
    /// `records`, siblings one after another, which pushed their items there
    /// up to `stop`.
    pub(crate) fn copy_subtrees(&mut self, old: &Self, records: Range<usize>, stop: ItemCounts) {
        let Some(from) = old.subtrees.get(records.start).map(|first| first.start) else {
            return;
        };
        let to = self.counts();
        let run = |start: usize, stop: usize| start..stop.max(start);
        self.shapes.extend_from_slice(
            old.shapes
                .get(run(from.shapes, stop.shapes))
                .unwrap_or_default(),
        );
        self.images.extend_from_slice(
            old.images
                .get(run(from.images, stop.images))
                .unwrap_or_default(),
        );
        self.texts.extend_from_slice(
            old.texts
                .get(run(from.texts, stop.texts))
                .unwrap_or_default(),
        );
        self.shadows.extend_from_slice(
            old.shadows
                .get(run(from.shadows, stop.shadows))
                .unwrap_or_default(),
        );
        let moved = |index: usize, from: usize, to: usize| to + index.saturating_sub(from);
        // A run that lands where it was in the old scene, as every run
        // before the first change does, keeps its indices.
        let base = self.subtrees.len();
        let first = records.start;
        let in_place = from == to && first == base;
        let order = old
            .order
            .get(run(from.order, stop.order))
            .unwrap_or_default();
        if in_place {
            let kept = self.order.len()..self.order.len() + order.len();
            match self.kept.last_mut() {
                Some(last) if last.end == kept.start => last.end = kept.end,
                _ if !kept.is_empty() => self.kept.push(kept),
                _ => {}
            }
            self.order.extend_from_slice(order);
        } else {
            self.order.extend(order.iter().map(|item| match *item {
                RenderItem::Shape(index) => RenderItem::Shape(moved(index, from.shapes, to.shapes)),
                RenderItem::Image(index) => RenderItem::Image(moved(index, from.images, to.images)),
                RenderItem::Text(index) => RenderItem::Text(moved(index, from.texts, to.texts)),
                RenderItem::Shadow(index) => {
                    RenderItem::Shadow(moved(index, from.shadows, to.shadows))
                }
            }));
        }
        self.subtrees
            .extend_from_slice(old.subtrees.get(records).unwrap_or_default());
        let passed_down = &mut self.passed_down;
        for record in self.subtrees.get_mut(base..).unwrap_or_default() {
            if !in_place {
                record.end = moved(record.end, first, base);
                record.start = record.start.moved(from, to);
                record.stop = record.stop.moved(from, to);
            }
            record.passed_down = record
                .passed_down
                .and_then(|at| old.passed_down.get(at))
                .map(|copied| {
                    passed_down.push(copied.clone());
                    passed_down.len() - 1
                });
        }
    }

    /// The extent of the subtree whose record is `record`, and the index of
    /// its first entry of what its layers passed down; `None` when its
    /// layers' entries of what they passed down do not lie in one run.
    pub(crate) fn subtree_span(&self, record: usize) -> Option<(SubtreeExtent, usize)> {
        let head = self.subtrees.get(record)?;
        let records = self.subtrees.get(record..head.end)?;
        let mut passed_down = records.iter().filter_map(|record| record.passed_down);
        let first = passed_down.next();
        let count = usize::from(first.is_some()) + passed_down.count();
        let last = records.iter().rev().find_map(|record| record.passed_down);
        if let (Some(first), Some(last)) = (first, last)
            && last + 1 != first + count
        {
            return None;
        }
        let extent = SubtreeExtent {
            records: records.len(),
            items: head.stop.since(head.start),
            passed_down: count,
        };
        Some((extent, first.unwrap_or(0)))
    }

    /// Queues `shape` to draw over everything queued before.
    pub(crate) fn push_draw_shape(&mut self, shape: DrawShape) {
        self.push_item(shape);
    }

    // Queues `item` to draw over everything queued before, snapped as the
    // layer whose draws push now; while a subtree is rewritten, in the
    // place it takes there.
    pub(crate) fn push_item<T: SceneItem>(&mut self, mut item: T) {
        if let Some(anchor) = self.snap_anchor {
            item.snap_to(anchor);
        }
        if let Some(mut rewrite) = self.rewrite.take() {
            rewrite.place(self, item);
            self.rewrite = Some(rewrite);
            return;
        }
        let (list, order) = T::lists(self);
        order.push(T::item(list.len()));
        list.push(item);
    }

    pub(crate) fn push_shape(
        &mut self,
        rect: Rect,
        brush: Brush,
        shape: Option<RoundedCornerShape>,
        clip: Option<Rect>,
        blend_mode: BlendMode,
    ) {
        self.push_draw_shape(DrawShape {
            rect,
            snap_anchor: None,
            snap_to_pixel_grid: false,
            brush,
            outline: Outline::Fill(shape),
            clip,
            blend_mode,
        });
    }

    pub(crate) fn push_shadow(&mut self, shadow: ShadowRRect, color: Color, clip: Option<Rect>) {
        self.push_item(ShadowDraw {
            shadow,
            snap_anchor: None,
            color,
            clip,
        });
    }

    pub(crate) fn push_pixel_snapped_shape(
        &mut self,
        rect: Rect,
        brush: Brush,
        shape: Option<RoundedCornerShape>,
        clip: Option<Rect>,
        blend_mode: BlendMode,
    ) {
        self.push_draw_shape(DrawShape {
            rect,
            snap_anchor: None,
            snap_to_pixel_grid: true,
            brush,
            outline: Outline::Fill(shape),
            clip,
            blend_mode,
        });
    }

    pub(crate) fn push_shape_params(&mut self, params: ShapeDrawParams) {
        self.push_draw_shape(DrawShape::of_params(params));
    }

    pub(crate) fn push_image(&mut self, draw: ImageDraw) {
        self.push_item(ImageDraw {
            alpha: draw.alpha.clamp(0.0, 1.0),
            ..draw
        });
    }

    /// Queues the pixels a layer drew into a surface of its own: `image`
    /// over `rect`, which `transform` maps to device pixels when the surface
    /// is turned, cut to `clip` and drawn at `alpha`.
    pub(crate) fn push_surface_image(
        &mut self,
        image: ImageBitmap,
        rect: Rect,
        transform: Option<ProjectiveTransform>,
        clip: Option<Rect>,
        alpha: f32,
    ) {
        let sampling = if transform.is_some() {
            ImageSampling::Linear
        } else {
            ImageSampling::Nearest
        };
        self.push_item(ImageDraw {
            rect,
            snap_anchor: None,
            image,
            alpha,
            color_filter: None,
            sampling,
            clip,
            blend_mode: BlendMode::SrcOver,
            src_rect: None,
            transform,
        });
    }

    pub(crate) fn push_text(&mut self, draw: TextDraw) {
        self.push_item(draw);
    }
}

impl Default for RasterScene {
    fn default() -> Self {
        Self::new()
    }
}