scene.rs

  1use crate::{
  2    point, AtlasTextureId, AtlasTile, Bounds, ContentMask, Corners, Edges, EntityId, Hsla, Pixels,
  3    Point, ScaledPixels, StackingOrder,
  4};
  5use collections::{BTreeMap, FxHashSet};
  6use std::{fmt::Debug, iter::Peekable, slice};
  7
  8// Exported to metal
  9pub(crate) type PointF = Point<f32>;
 10#[allow(non_camel_case_types, unused)]
 11pub(crate) type PathVertex_ScaledPixels = PathVertex<ScaledPixels>;
 12
 13pub type LayerId = u32;
 14pub type DrawOrder = u32;
 15
 16#[derive(Default, Copy, Clone, Debug, Eq, PartialEq, Hash)]
 17#[repr(C)]
 18pub struct ViewId {
 19    low_bits: u32,
 20    high_bits: u32,
 21}
 22
 23impl From<EntityId> for ViewId {
 24    fn from(value: EntityId) -> Self {
 25        let value = value.as_u64();
 26        Self {
 27            low_bits: value as u32,
 28            high_bits: (value >> 32) as u32,
 29        }
 30    }
 31}
 32
 33impl From<ViewId> for EntityId {
 34    fn from(value: ViewId) -> Self {
 35        let value = (value.low_bits as u64) | ((value.high_bits as u64) << 32);
 36        value.into()
 37    }
 38}
 39
 40#[derive(Default)]
 41pub struct Scene {
 42    layers_by_order: BTreeMap<StackingOrder, LayerId>,
 43    orders_by_layer: BTreeMap<LayerId, StackingOrder>,
 44    pub(crate) shadows: Vec<Shadow>,
 45    pub(crate) quads: Vec<Quad>,
 46    pub(crate) paths: Vec<Path<ScaledPixels>>,
 47    pub(crate) underlines: Vec<Underline>,
 48    pub(crate) monochrome_sprites: Vec<MonochromeSprite>,
 49    pub(crate) polychrome_sprites: Vec<PolychromeSprite>,
 50    pub(crate) surfaces: Vec<Surface>,
 51}
 52
 53impl Scene {
 54    pub fn clear(&mut self) {
 55        self.layers_by_order.clear();
 56        self.orders_by_layer.clear();
 57        self.shadows.clear();
 58        self.quads.clear();
 59        self.paths.clear();
 60        self.underlines.clear();
 61        self.monochrome_sprites.clear();
 62        self.polychrome_sprites.clear();
 63        self.surfaces.clear();
 64    }
 65
 66    pub fn paths(&self) -> &[Path<ScaledPixels>] {
 67        &self.paths
 68    }
 69
 70    pub(crate) fn batches(&self) -> impl Iterator<Item = PrimitiveBatch> {
 71        BatchIterator {
 72            shadows: &self.shadows,
 73            shadows_start: 0,
 74            shadows_iter: self.shadows.iter().peekable(),
 75            quads: &self.quads,
 76            quads_start: 0,
 77            quads_iter: self.quads.iter().peekable(),
 78            paths: &self.paths,
 79            paths_start: 0,
 80            paths_iter: self.paths.iter().peekable(),
 81            underlines: &self.underlines,
 82            underlines_start: 0,
 83            underlines_iter: self.underlines.iter().peekable(),
 84            monochrome_sprites: &self.monochrome_sprites,
 85            monochrome_sprites_start: 0,
 86            monochrome_sprites_iter: self.monochrome_sprites.iter().peekable(),
 87            polychrome_sprites: &self.polychrome_sprites,
 88            polychrome_sprites_start: 0,
 89            polychrome_sprites_iter: self.polychrome_sprites.iter().peekable(),
 90            surfaces: &self.surfaces,
 91            surfaces_start: 0,
 92            surfaces_iter: self.surfaces.iter().peekable(),
 93        }
 94    }
 95
 96    pub fn insert(&mut self, order: &StackingOrder, primitive: impl Into<Primitive>) {
 97        let primitive = primitive.into();
 98        let clipped_bounds = primitive
 99            .bounds()
100            .intersect(&primitive.content_mask().bounds);
101        if clipped_bounds.size.width <= ScaledPixels(0.)
102            || clipped_bounds.size.height <= ScaledPixels(0.)
103        {
104            return;
105        }
106
107        let layer_id = self.layer_id_for_order(order);
108        match primitive {
109            Primitive::Shadow(mut shadow) => {
110                shadow.layer_id = layer_id;
111                self.shadows.push(shadow);
112            }
113            Primitive::Quad(mut quad) => {
114                quad.layer_id = layer_id;
115                self.quads.push(quad);
116            }
117            Primitive::Path(mut path) => {
118                path.layer_id = layer_id;
119                path.id = PathId(self.paths.len());
120                self.paths.push(path);
121            }
122            Primitive::Underline(mut underline) => {
123                underline.layer_id = layer_id;
124                self.underlines.push(underline);
125            }
126            Primitive::MonochromeSprite(mut sprite) => {
127                sprite.layer_id = layer_id;
128                self.monochrome_sprites.push(sprite);
129            }
130            Primitive::PolychromeSprite(mut sprite) => {
131                sprite.layer_id = layer_id;
132                self.polychrome_sprites.push(sprite);
133            }
134            Primitive::Surface(mut surface) => {
135                surface.layer_id = layer_id;
136                self.surfaces.push(surface);
137            }
138        }
139    }
140
141    fn layer_id_for_order(&mut self, order: &StackingOrder) -> LayerId {
142        if let Some(layer_id) = self.layers_by_order.get(order) {
143            *layer_id
144        } else {
145            let next_id = self.layers_by_order.len() as LayerId;
146            self.layers_by_order.insert(order.clone(), next_id);
147            self.orders_by_layer.insert(next_id, order.clone());
148            next_id
149        }
150    }
151
152    pub fn reuse_views(&mut self, views: &FxHashSet<EntityId>, prev_scene: &mut Self) {
153        for shadow in prev_scene.shadows.drain(..) {
154            if views.contains(&shadow.view_id.into()) {
155                let order = &prev_scene.orders_by_layer[&shadow.layer_id];
156                self.insert(&order, shadow);
157            }
158        }
159
160        for quad in prev_scene.quads.drain(..) {
161            if views.contains(&quad.view_id.into()) {
162                let order = &prev_scene.orders_by_layer[&quad.layer_id];
163                self.insert(&order, quad);
164            }
165        }
166
167        for path in prev_scene.paths.drain(..) {
168            if views.contains(&path.view_id.into()) {
169                let order = &prev_scene.orders_by_layer[&path.layer_id];
170                self.insert(&order, path);
171            }
172        }
173
174        for underline in prev_scene.underlines.drain(..) {
175            if views.contains(&underline.view_id.into()) {
176                let order = &prev_scene.orders_by_layer[&underline.layer_id];
177                self.insert(&order, underline);
178            }
179        }
180
181        for sprite in prev_scene.monochrome_sprites.drain(..) {
182            if views.contains(&sprite.view_id.into()) {
183                let order = &prev_scene.orders_by_layer[&sprite.layer_id];
184                self.insert(&order, sprite);
185            }
186        }
187
188        for sprite in prev_scene.polychrome_sprites.drain(..) {
189            if views.contains(&sprite.view_id.into()) {
190                let order = &prev_scene.orders_by_layer[&sprite.layer_id];
191                self.insert(&order, sprite);
192            }
193        }
194
195        for surface in prev_scene.surfaces.drain(..) {
196            if views.contains(&surface.view_id.into()) {
197                let order = &prev_scene.orders_by_layer[&surface.layer_id];
198                self.insert(&order, surface);
199            }
200        }
201    }
202
203    pub fn finish(&mut self) {
204        let mut orders = vec![0; self.layers_by_order.len()];
205        for (ix, layer_id) in self.layers_by_order.values().enumerate() {
206            orders[*layer_id as usize] = ix as u32;
207        }
208
209        for shadow in &mut self.shadows {
210            shadow.order = orders[shadow.layer_id as usize];
211        }
212        self.shadows.sort_by_key(|shadow| shadow.order);
213
214        for quad in &mut self.quads {
215            quad.order = orders[quad.layer_id as usize];
216        }
217        self.quads.sort_by_key(|quad| quad.order);
218
219        for path in &mut self.paths {
220            path.order = orders[path.layer_id as usize];
221        }
222        self.paths.sort_by_key(|path| path.order);
223
224        for underline in &mut self.underlines {
225            underline.order = orders[underline.layer_id as usize];
226        }
227        self.underlines.sort_by_key(|underline| underline.order);
228
229        for monochrome_sprite in &mut self.monochrome_sprites {
230            monochrome_sprite.order = orders[monochrome_sprite.layer_id as usize];
231        }
232        self.monochrome_sprites.sort_by_key(|sprite| sprite.order);
233
234        for polychrome_sprite in &mut self.polychrome_sprites {
235            polychrome_sprite.order = orders[polychrome_sprite.layer_id as usize];
236        }
237        self.polychrome_sprites.sort_by_key(|sprite| sprite.order);
238
239        for surface in &mut self.surfaces {
240            surface.order = orders[surface.layer_id as usize];
241        }
242        self.surfaces.sort_by_key(|surface| surface.order);
243    }
244}
245
246struct BatchIterator<'a> {
247    shadows: &'a [Shadow],
248    shadows_start: usize,
249    shadows_iter: Peekable<slice::Iter<'a, Shadow>>,
250    quads: &'a [Quad],
251    quads_start: usize,
252    quads_iter: Peekable<slice::Iter<'a, Quad>>,
253    paths: &'a [Path<ScaledPixels>],
254    paths_start: usize,
255    paths_iter: Peekable<slice::Iter<'a, Path<ScaledPixels>>>,
256    underlines: &'a [Underline],
257    underlines_start: usize,
258    underlines_iter: Peekable<slice::Iter<'a, Underline>>,
259    monochrome_sprites: &'a [MonochromeSprite],
260    monochrome_sprites_start: usize,
261    monochrome_sprites_iter: Peekable<slice::Iter<'a, MonochromeSprite>>,
262    polychrome_sprites: &'a [PolychromeSprite],
263    polychrome_sprites_start: usize,
264    polychrome_sprites_iter: Peekable<slice::Iter<'a, PolychromeSprite>>,
265    surfaces: &'a [Surface],
266    surfaces_start: usize,
267    surfaces_iter: Peekable<slice::Iter<'a, Surface>>,
268}
269
270impl<'a> Iterator for BatchIterator<'a> {
271    type Item = PrimitiveBatch<'a>;
272
273    fn next(&mut self) -> Option<Self::Item> {
274        let mut orders_and_kinds = [
275            (
276                self.shadows_iter.peek().map(|s| s.order),
277                PrimitiveKind::Shadow,
278            ),
279            (self.quads_iter.peek().map(|q| q.order), PrimitiveKind::Quad),
280            (self.paths_iter.peek().map(|q| q.order), PrimitiveKind::Path),
281            (
282                self.underlines_iter.peek().map(|u| u.order),
283                PrimitiveKind::Underline,
284            ),
285            (
286                self.monochrome_sprites_iter.peek().map(|s| s.order),
287                PrimitiveKind::MonochromeSprite,
288            ),
289            (
290                self.polychrome_sprites_iter.peek().map(|s| s.order),
291                PrimitiveKind::PolychromeSprite,
292            ),
293            (
294                self.surfaces_iter.peek().map(|s| s.order),
295                PrimitiveKind::Surface,
296            ),
297        ];
298        orders_and_kinds.sort_by_key(|(order, kind)| (order.unwrap_or(u32::MAX), *kind));
299
300        let first = orders_and_kinds[0];
301        let second = orders_and_kinds[1];
302        let (batch_kind, max_order) = if first.0.is_some() {
303            (first.1, second.0.unwrap_or(u32::MAX))
304        } else {
305            return None;
306        };
307
308        match batch_kind {
309            PrimitiveKind::Shadow => {
310                let shadows_start = self.shadows_start;
311                let mut shadows_end = shadows_start + 1;
312                self.shadows_iter.next();
313                while self
314                    .shadows_iter
315                    .next_if(|shadow| shadow.order < max_order)
316                    .is_some()
317                {
318                    shadows_end += 1;
319                }
320                self.shadows_start = shadows_end;
321                Some(PrimitiveBatch::Shadows(
322                    &self.shadows[shadows_start..shadows_end],
323                ))
324            }
325            PrimitiveKind::Quad => {
326                let quads_start = self.quads_start;
327                let mut quads_end = quads_start + 1;
328                self.quads_iter.next();
329                while self
330                    .quads_iter
331                    .next_if(|quad| quad.order < max_order)
332                    .is_some()
333                {
334                    quads_end += 1;
335                }
336                self.quads_start = quads_end;
337                Some(PrimitiveBatch::Quads(&self.quads[quads_start..quads_end]))
338            }
339            PrimitiveKind::Path => {
340                let paths_start = self.paths_start;
341                let mut paths_end = paths_start + 1;
342                self.paths_iter.next();
343                while self
344                    .paths_iter
345                    .next_if(|path| path.order < max_order)
346                    .is_some()
347                {
348                    paths_end += 1;
349                }
350                self.paths_start = paths_end;
351                Some(PrimitiveBatch::Paths(&self.paths[paths_start..paths_end]))
352            }
353            PrimitiveKind::Underline => {
354                let underlines_start = self.underlines_start;
355                let mut underlines_end = underlines_start + 1;
356                self.underlines_iter.next();
357                while self
358                    .underlines_iter
359                    .next_if(|underline| underline.order < max_order)
360                    .is_some()
361                {
362                    underlines_end += 1;
363                }
364                self.underlines_start = underlines_end;
365                Some(PrimitiveBatch::Underlines(
366                    &self.underlines[underlines_start..underlines_end],
367                ))
368            }
369            PrimitiveKind::MonochromeSprite => {
370                let texture_id = self.monochrome_sprites_iter.peek().unwrap().tile.texture_id;
371                let sprites_start = self.monochrome_sprites_start;
372                let mut sprites_end = sprites_start + 1;
373                self.monochrome_sprites_iter.next();
374                while self
375                    .monochrome_sprites_iter
376                    .next_if(|sprite| {
377                        sprite.order < max_order && sprite.tile.texture_id == texture_id
378                    })
379                    .is_some()
380                {
381                    sprites_end += 1;
382                }
383                self.monochrome_sprites_start = sprites_end;
384                Some(PrimitiveBatch::MonochromeSprites {
385                    texture_id,
386                    sprites: &self.monochrome_sprites[sprites_start..sprites_end],
387                })
388            }
389            PrimitiveKind::PolychromeSprite => {
390                let texture_id = self.polychrome_sprites_iter.peek().unwrap().tile.texture_id;
391                let sprites_start = self.polychrome_sprites_start;
392                let mut sprites_end = self.polychrome_sprites_start + 1;
393                self.polychrome_sprites_iter.next();
394                while self
395                    .polychrome_sprites_iter
396                    .next_if(|sprite| {
397                        sprite.order < max_order && sprite.tile.texture_id == texture_id
398                    })
399                    .is_some()
400                {
401                    sprites_end += 1;
402                }
403                self.polychrome_sprites_start = sprites_end;
404                Some(PrimitiveBatch::PolychromeSprites {
405                    texture_id,
406                    sprites: &self.polychrome_sprites[sprites_start..sprites_end],
407                })
408            }
409            PrimitiveKind::Surface => {
410                let surfaces_start = self.surfaces_start;
411                let mut surfaces_end = surfaces_start + 1;
412                self.surfaces_iter.next();
413                while self
414                    .surfaces_iter
415                    .next_if(|surface| surface.order < max_order)
416                    .is_some()
417                {
418                    surfaces_end += 1;
419                }
420                self.surfaces_start = surfaces_end;
421                Some(PrimitiveBatch::Surfaces(
422                    &self.surfaces[surfaces_start..surfaces_end],
423                ))
424            }
425        }
426    }
427}
428
429#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Default)]
430pub enum PrimitiveKind {
431    Shadow,
432    #[default]
433    Quad,
434    Path,
435    Underline,
436    MonochromeSprite,
437    PolychromeSprite,
438    Surface,
439}
440
441pub enum Primitive {
442    Shadow(Shadow),
443    Quad(Quad),
444    Path(Path<ScaledPixels>),
445    Underline(Underline),
446    MonochromeSprite(MonochromeSprite),
447    PolychromeSprite(PolychromeSprite),
448    Surface(Surface),
449}
450
451impl Primitive {
452    pub fn bounds(&self) -> &Bounds<ScaledPixels> {
453        match self {
454            Primitive::Shadow(shadow) => &shadow.bounds,
455            Primitive::Quad(quad) => &quad.bounds,
456            Primitive::Path(path) => &path.bounds,
457            Primitive::Underline(underline) => &underline.bounds,
458            Primitive::MonochromeSprite(sprite) => &sprite.bounds,
459            Primitive::PolychromeSprite(sprite) => &sprite.bounds,
460            Primitive::Surface(surface) => &surface.bounds,
461        }
462    }
463
464    pub fn content_mask(&self) -> &ContentMask<ScaledPixels> {
465        match self {
466            Primitive::Shadow(shadow) => &shadow.content_mask,
467            Primitive::Quad(quad) => &quad.content_mask,
468            Primitive::Path(path) => &path.content_mask,
469            Primitive::Underline(underline) => &underline.content_mask,
470            Primitive::MonochromeSprite(sprite) => &sprite.content_mask,
471            Primitive::PolychromeSprite(sprite) => &sprite.content_mask,
472            Primitive::Surface(surface) => &surface.content_mask,
473        }
474    }
475}
476
477#[derive(Debug)]
478pub(crate) enum PrimitiveBatch<'a> {
479    Shadows(&'a [Shadow]),
480    Quads(&'a [Quad]),
481    Paths(&'a [Path<ScaledPixels>]),
482    Underlines(&'a [Underline]),
483    MonochromeSprites {
484        texture_id: AtlasTextureId,
485        sprites: &'a [MonochromeSprite],
486    },
487    PolychromeSprites {
488        texture_id: AtlasTextureId,
489        sprites: &'a [PolychromeSprite],
490    },
491    Surfaces(&'a [Surface]),
492}
493
494#[derive(Default, Debug, Clone, Eq, PartialEq)]
495#[repr(C)]
496pub struct Quad {
497    pub view_id: ViewId,
498    pub layer_id: LayerId,
499    pub order: DrawOrder,
500    pub bounds: Bounds<ScaledPixels>,
501    pub content_mask: ContentMask<ScaledPixels>,
502    pub background: Hsla,
503    pub border_color: Hsla,
504    pub corner_radii: Corners<ScaledPixels>,
505    pub border_widths: Edges<ScaledPixels>,
506}
507
508impl Ord for Quad {
509    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
510        self.order.cmp(&other.order)
511    }
512}
513
514impl PartialOrd for Quad {
515    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
516        Some(self.cmp(other))
517    }
518}
519
520impl From<Quad> for Primitive {
521    fn from(quad: Quad) -> Self {
522        Primitive::Quad(quad)
523    }
524}
525
526#[derive(Debug, Clone, Eq, PartialEq)]
527#[repr(C)]
528pub struct Underline {
529    pub view_id: ViewId,
530    pub layer_id: LayerId,
531    pub order: DrawOrder,
532    pub bounds: Bounds<ScaledPixels>,
533    pub content_mask: ContentMask<ScaledPixels>,
534    pub thickness: ScaledPixels,
535    pub color: Hsla,
536    pub wavy: bool,
537}
538
539impl Ord for Underline {
540    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
541        self.order.cmp(&other.order)
542    }
543}
544
545impl PartialOrd for Underline {
546    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
547        Some(self.cmp(other))
548    }
549}
550
551impl From<Underline> for Primitive {
552    fn from(underline: Underline) -> Self {
553        Primitive::Underline(underline)
554    }
555}
556
557#[derive(Debug, Clone, Eq, PartialEq)]
558#[repr(C)]
559pub struct Shadow {
560    pub view_id: ViewId,
561    pub layer_id: LayerId,
562    pub order: DrawOrder,
563    pub bounds: Bounds<ScaledPixels>,
564    pub corner_radii: Corners<ScaledPixels>,
565    pub content_mask: ContentMask<ScaledPixels>,
566    pub color: Hsla,
567    pub blur_radius: ScaledPixels,
568}
569
570impl Ord for Shadow {
571    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
572        self.order.cmp(&other.order)
573    }
574}
575
576impl PartialOrd for Shadow {
577    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
578        Some(self.cmp(other))
579    }
580}
581
582impl From<Shadow> for Primitive {
583    fn from(shadow: Shadow) -> Self {
584        Primitive::Shadow(shadow)
585    }
586}
587
588#[derive(Clone, Debug, Eq, PartialEq)]
589#[repr(C)]
590pub struct MonochromeSprite {
591    pub view_id: ViewId,
592    pub layer_id: LayerId,
593    pub order: DrawOrder,
594    pub bounds: Bounds<ScaledPixels>,
595    pub content_mask: ContentMask<ScaledPixels>,
596    pub color: Hsla,
597    pub tile: AtlasTile,
598}
599
600impl Ord for MonochromeSprite {
601    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
602        match self.order.cmp(&other.order) {
603            std::cmp::Ordering::Equal => self.tile.tile_id.cmp(&other.tile.tile_id),
604            order => order,
605        }
606    }
607}
608
609impl PartialOrd for MonochromeSprite {
610    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
611        Some(self.cmp(other))
612    }
613}
614
615impl From<MonochromeSprite> for Primitive {
616    fn from(sprite: MonochromeSprite) -> Self {
617        Primitive::MonochromeSprite(sprite)
618    }
619}
620
621#[derive(Clone, Debug, Eq, PartialEq)]
622#[repr(C)]
623pub struct PolychromeSprite {
624    pub view_id: ViewId,
625    pub layer_id: LayerId,
626    pub order: DrawOrder,
627    pub bounds: Bounds<ScaledPixels>,
628    pub content_mask: ContentMask<ScaledPixels>,
629    pub corner_radii: Corners<ScaledPixels>,
630    pub tile: AtlasTile,
631    pub grayscale: bool,
632}
633
634impl Ord for PolychromeSprite {
635    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
636        match self.order.cmp(&other.order) {
637            std::cmp::Ordering::Equal => self.tile.tile_id.cmp(&other.tile.tile_id),
638            order => order,
639        }
640    }
641}
642
643impl PartialOrd for PolychromeSprite {
644    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
645        Some(self.cmp(other))
646    }
647}
648
649impl From<PolychromeSprite> for Primitive {
650    fn from(sprite: PolychromeSprite) -> Self {
651        Primitive::PolychromeSprite(sprite)
652    }
653}
654
655#[derive(Clone, Debug, Eq, PartialEq)]
656pub struct Surface {
657    pub view_id: ViewId,
658    pub layer_id: LayerId,
659    pub order: DrawOrder,
660    pub bounds: Bounds<ScaledPixels>,
661    pub content_mask: ContentMask<ScaledPixels>,
662    pub image_buffer: media::core_video::CVImageBuffer,
663}
664
665impl Ord for Surface {
666    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
667        self.order.cmp(&other.order)
668    }
669}
670
671impl PartialOrd for Surface {
672    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
673        Some(self.cmp(other))
674    }
675}
676
677impl From<Surface> for Primitive {
678    fn from(surface: Surface) -> Self {
679        Primitive::Surface(surface)
680    }
681}
682
683#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
684pub(crate) struct PathId(pub(crate) usize);
685
686#[derive(Debug)]
687pub struct Path<P: Clone + Default + Debug> {
688    pub(crate) id: PathId,
689    pub(crate) view_id: ViewId,
690    layer_id: LayerId,
691    order: DrawOrder,
692    pub(crate) bounds: Bounds<P>,
693    pub(crate) content_mask: ContentMask<P>,
694    pub(crate) vertices: Vec<PathVertex<P>>,
695    pub(crate) color: Hsla,
696    start: Point<P>,
697    current: Point<P>,
698    contour_count: usize,
699}
700
701impl Path<Pixels> {
702    pub fn new(start: Point<Pixels>) -> Self {
703        Self {
704            id: PathId(0),
705            view_id: ViewId::default(),
706            layer_id: LayerId::default(),
707            order: DrawOrder::default(),
708            vertices: Vec::new(),
709            start,
710            current: start,
711            bounds: Bounds {
712                origin: start,
713                size: Default::default(),
714            },
715            content_mask: Default::default(),
716            color: Default::default(),
717            contour_count: 0,
718        }
719    }
720
721    pub fn scale(&self, factor: f32) -> Path<ScaledPixels> {
722        Path {
723            id: self.id,
724            view_id: self.view_id,
725            layer_id: self.layer_id,
726            order: self.order,
727            bounds: self.bounds.scale(factor),
728            content_mask: self.content_mask.scale(factor),
729            vertices: self
730                .vertices
731                .iter()
732                .map(|vertex| vertex.scale(factor))
733                .collect(),
734            start: self.start.map(|start| start.scale(factor)),
735            current: self.current.scale(factor),
736            contour_count: self.contour_count,
737            color: self.color,
738        }
739    }
740
741    pub fn line_to(&mut self, to: Point<Pixels>) {
742        self.contour_count += 1;
743        if self.contour_count > 1 {
744            self.push_triangle(
745                (self.start, self.current, to),
746                (point(0., 1.), point(0., 1.), point(0., 1.)),
747            );
748        }
749        self.current = to;
750    }
751
752    pub fn curve_to(&mut self, to: Point<Pixels>, ctrl: Point<Pixels>) {
753        self.contour_count += 1;
754        if self.contour_count > 1 {
755            self.push_triangle(
756                (self.start, self.current, to),
757                (point(0., 1.), point(0., 1.), point(0., 1.)),
758            );
759        }
760
761        self.push_triangle(
762            (self.current, ctrl, to),
763            (point(0., 0.), point(0.5, 0.), point(1., 1.)),
764        );
765        self.current = to;
766    }
767
768    fn push_triangle(
769        &mut self,
770        xy: (Point<Pixels>, Point<Pixels>, Point<Pixels>),
771        st: (Point<f32>, Point<f32>, Point<f32>),
772    ) {
773        self.bounds = self
774            .bounds
775            .union(&Bounds {
776                origin: xy.0,
777                size: Default::default(),
778            })
779            .union(&Bounds {
780                origin: xy.1,
781                size: Default::default(),
782            })
783            .union(&Bounds {
784                origin: xy.2,
785                size: Default::default(),
786            });
787
788        self.vertices.push(PathVertex {
789            xy_position: xy.0,
790            st_position: st.0,
791            content_mask: Default::default(),
792        });
793        self.vertices.push(PathVertex {
794            xy_position: xy.1,
795            st_position: st.1,
796            content_mask: Default::default(),
797        });
798        self.vertices.push(PathVertex {
799            xy_position: xy.2,
800            st_position: st.2,
801            content_mask: Default::default(),
802        });
803    }
804}
805
806impl Eq for Path<ScaledPixels> {}
807
808impl PartialEq for Path<ScaledPixels> {
809    fn eq(&self, other: &Self) -> bool {
810        self.order == other.order
811    }
812}
813
814impl Ord for Path<ScaledPixels> {
815    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
816        self.order.cmp(&other.order)
817    }
818}
819
820impl PartialOrd for Path<ScaledPixels> {
821    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
822        Some(self.cmp(other))
823    }
824}
825
826impl From<Path<ScaledPixels>> for Primitive {
827    fn from(path: Path<ScaledPixels>) -> Self {
828        Primitive::Path(path)
829    }
830}
831
832#[derive(Clone, Debug)]
833#[repr(C)]
834pub struct PathVertex<P: Clone + Default + Debug> {
835    pub(crate) xy_position: Point<P>,
836    pub(crate) st_position: Point<f32>,
837    pub(crate) content_mask: ContentMask<P>,
838}
839
840impl PathVertex<Pixels> {
841    pub fn scale(&self, factor: f32) -> PathVertex<ScaledPixels> {
842        PathVertex {
843            xy_position: self.xy_position.scale(factor),
844            st_position: self.st_position,
845            content_mask: self.content_mask.scale(factor),
846        }
847    }
848}
849
850#[derive(Copy, Clone, Debug)]
851pub struct AtlasId(pub(crate) usize);