// todo("windows"): remove #![cfg_attr(windows, allow(dead_code))] use crate::{ bounds_tree::BoundsTree, point, AtlasTextureId, AtlasTile, Bounds, ContentMask, Corners, Edges, Hsla, Pixels, Point, Radians, ScaledPixels, Size, }; use std::{fmt::Debug, iter::Peekable, ops::Range, slice}; #[allow(non_camel_case_types, unused)] pub(crate) type PathVertex_ScaledPixels = PathVertex; pub(crate) type DrawOrder = u32; #[derive(Default)] pub(crate) struct Scene { pub(crate) paint_operations: Vec, primitive_bounds: BoundsTree, layer_stack: Vec, pub(crate) shadows: Vec, pub(crate) quads: Vec, pub(crate) paths: Vec>, pub(crate) underlines: Vec, pub(crate) monochrome_sprites: Vec, pub(crate) polychrome_sprites: Vec, pub(crate) surfaces: Vec, } impl Scene { pub fn clear(&mut self) { self.paint_operations.clear(); self.primitive_bounds.clear(); self.layer_stack.clear(); self.paths.clear(); self.shadows.clear(); self.quads.clear(); self.underlines.clear(); self.monochrome_sprites.clear(); self.polychrome_sprites.clear(); self.surfaces.clear(); } pub fn paths(&self) -> &[Path] { &self.paths } pub fn len(&self) -> usize { self.paint_operations.len() } pub fn push_layer(&mut self, bounds: Bounds) { let order = self.primitive_bounds.insert(bounds); self.layer_stack.push(order); self.paint_operations .push(PaintOperation::StartLayer(bounds)); } pub fn pop_layer(&mut self) { self.layer_stack.pop(); self.paint_operations.push(PaintOperation::EndLayer); } pub fn insert_primitive(&mut self, primitive: impl Into) { let mut primitive = primitive.into(); let clipped_bounds = primitive .bounds() .intersect(&primitive.content_mask().bounds); if clipped_bounds.is_empty() { return; } let order = self .layer_stack .last() .copied() .unwrap_or_else(|| self.primitive_bounds.insert(clipped_bounds)); match &mut primitive { Primitive::Shadow(shadow) => { shadow.order = order; self.shadows.push(shadow.clone()); } Primitive::Quad(quad) => { quad.order = order; self.quads.push(quad.clone()); } Primitive::Path(path) => { path.order = order; path.id = PathId(self.paths.len()); self.paths.push(path.clone()); } Primitive::Underline(underline) => { underline.order = order; self.underlines.push(underline.clone()); } Primitive::MonochromeSprite(sprite) => { sprite.order = order; self.monochrome_sprites.push(sprite.clone()); } Primitive::PolychromeSprite(sprite) => { sprite.order = order; self.polychrome_sprites.push(sprite.clone()); } Primitive::Surface(surface) => { surface.order = order; self.surfaces.push(surface.clone()); } } self.paint_operations .push(PaintOperation::Primitive(primitive)); } pub fn replay(&mut self, range: Range, prev_scene: &Scene) { for operation in &prev_scene.paint_operations[range] { match operation { PaintOperation::Primitive(primitive) => self.insert_primitive(primitive.clone()), PaintOperation::StartLayer(bounds) => self.push_layer(*bounds), PaintOperation::EndLayer => self.pop_layer(), } } } pub fn finish(&mut self) { self.shadows.sort(); self.quads.sort(); self.paths.sort(); self.underlines.sort(); self.monochrome_sprites.sort(); self.polychrome_sprites.sort(); self.surfaces.sort(); } pub(crate) fn batches(&self) -> impl Iterator { BatchIterator { shadows: &self.shadows, shadows_start: 0, shadows_iter: self.shadows.iter().peekable(), quads: &self.quads, quads_start: 0, quads_iter: self.quads.iter().peekable(), paths: &self.paths, paths_start: 0, paths_iter: self.paths.iter().peekable(), underlines: &self.underlines, underlines_start: 0, underlines_iter: self.underlines.iter().peekable(), monochrome_sprites: &self.monochrome_sprites, monochrome_sprites_start: 0, monochrome_sprites_iter: self.monochrome_sprites.iter().peekable(), polychrome_sprites: &self.polychrome_sprites, polychrome_sprites_start: 0, polychrome_sprites_iter: self.polychrome_sprites.iter().peekable(), surfaces: &self.surfaces, surfaces_start: 0, surfaces_iter: self.surfaces.iter().peekable(), } } } #[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Default)] pub(crate) enum PrimitiveKind { Shadow, #[default] Quad, Path, Underline, MonochromeSprite, PolychromeSprite, Surface, } pub(crate) enum PaintOperation { Primitive(Primitive), StartLayer(Bounds), EndLayer, } #[derive(Clone, Ord, PartialOrd, Eq, PartialEq)] pub(crate) enum Primitive { Shadow(Shadow), Quad(Quad), Path(Path), Underline(Underline), MonochromeSprite(MonochromeSprite), PolychromeSprite(PolychromeSprite), Surface(PaintSurface), } impl Primitive { pub fn bounds(&self) -> &Bounds { match self { Primitive::Shadow(shadow) => &shadow.bounds, Primitive::Quad(quad) => &quad.bounds, Primitive::Path(path) => &path.bounds, Primitive::Underline(underline) => &underline.bounds, Primitive::MonochromeSprite(sprite) => &sprite.bounds, Primitive::PolychromeSprite(sprite) => &sprite.bounds, Primitive::Surface(surface) => &surface.bounds, } } pub fn content_mask(&self) -> &ContentMask { match self { Primitive::Shadow(shadow) => &shadow.content_mask, Primitive::Quad(quad) => &quad.content_mask, Primitive::Path(path) => &path.content_mask, Primitive::Underline(underline) => &underline.content_mask, Primitive::MonochromeSprite(sprite) => &sprite.content_mask, Primitive::PolychromeSprite(sprite) => &sprite.content_mask, Primitive::Surface(surface) => &surface.content_mask, } } } struct BatchIterator<'a> { shadows: &'a [Shadow], shadows_start: usize, shadows_iter: Peekable>, quads: &'a [Quad], quads_start: usize, quads_iter: Peekable>, paths: &'a [Path], paths_start: usize, paths_iter: Peekable>>, underlines: &'a [Underline], underlines_start: usize, underlines_iter: Peekable>, monochrome_sprites: &'a [MonochromeSprite], monochrome_sprites_start: usize, monochrome_sprites_iter: Peekable>, polychrome_sprites: &'a [PolychromeSprite], polychrome_sprites_start: usize, polychrome_sprites_iter: Peekable>, surfaces: &'a [PaintSurface], surfaces_start: usize, surfaces_iter: Peekable>, } impl<'a> Iterator for BatchIterator<'a> { type Item = PrimitiveBatch<'a>; fn next(&mut self) -> Option { let mut orders_and_kinds = [ ( self.shadows_iter.peek().map(|s| s.order), PrimitiveKind::Shadow, ), (self.quads_iter.peek().map(|q| q.order), PrimitiveKind::Quad), (self.paths_iter.peek().map(|q| q.order), PrimitiveKind::Path), ( self.underlines_iter.peek().map(|u| u.order), PrimitiveKind::Underline, ), ( self.monochrome_sprites_iter.peek().map(|s| s.order), PrimitiveKind::MonochromeSprite, ), ( self.polychrome_sprites_iter.peek().map(|s| s.order), PrimitiveKind::PolychromeSprite, ), ( self.surfaces_iter.peek().map(|s| s.order), PrimitiveKind::Surface, ), ]; orders_and_kinds.sort_by_key(|(order, kind)| (order.unwrap_or(u32::MAX), *kind)); let first = orders_and_kinds[0]; let second = orders_and_kinds[1]; let (batch_kind, max_order_and_kind) = if first.0.is_some() { (first.1, (second.0.unwrap_or(u32::MAX), second.1)) } else { return None; }; match batch_kind { PrimitiveKind::Shadow => { let shadows_start = self.shadows_start; let mut shadows_end = shadows_start + 1; self.shadows_iter.next(); while self .shadows_iter .next_if(|shadow| (shadow.order, batch_kind) < max_order_and_kind) .is_some() { shadows_end += 1; } self.shadows_start = shadows_end; Some(PrimitiveBatch::Shadows( &self.shadows[shadows_start..shadows_end], )) } PrimitiveKind::Quad => { let quads_start = self.quads_start; let mut quads_end = quads_start + 1; self.quads_iter.next(); while self .quads_iter .next_if(|quad| (quad.order, batch_kind) < max_order_and_kind) .is_some() { quads_end += 1; } self.quads_start = quads_end; Some(PrimitiveBatch::Quads(&self.quads[quads_start..quads_end])) } PrimitiveKind::Path => { let paths_start = self.paths_start; let mut paths_end = paths_start + 1; self.paths_iter.next(); while self .paths_iter .next_if(|path| (path.order, batch_kind) < max_order_and_kind) .is_some() { paths_end += 1; } self.paths_start = paths_end; Some(PrimitiveBatch::Paths(&self.paths[paths_start..paths_end])) } PrimitiveKind::Underline => { let underlines_start = self.underlines_start; let mut underlines_end = underlines_start + 1; self.underlines_iter.next(); while self .underlines_iter .next_if(|underline| (underline.order, batch_kind) < max_order_and_kind) .is_some() { underlines_end += 1; } self.underlines_start = underlines_end; Some(PrimitiveBatch::Underlines( &self.underlines[underlines_start..underlines_end], )) } PrimitiveKind::MonochromeSprite => { let texture_id = self.monochrome_sprites_iter.peek().unwrap().tile.texture_id; let sprites_start = self.monochrome_sprites_start; let mut sprites_end = sprites_start + 1; self.monochrome_sprites_iter.next(); while self .monochrome_sprites_iter .next_if(|sprite| { (sprite.order, batch_kind) < max_order_and_kind && sprite.tile.texture_id == texture_id }) .is_some() { sprites_end += 1; } self.monochrome_sprites_start = sprites_end; Some(PrimitiveBatch::MonochromeSprites { texture_id, sprites: &self.monochrome_sprites[sprites_start..sprites_end], }) } PrimitiveKind::PolychromeSprite => { let texture_id = self.polychrome_sprites_iter.peek().unwrap().tile.texture_id; let sprites_start = self.polychrome_sprites_start; let mut sprites_end = self.polychrome_sprites_start + 1; self.polychrome_sprites_iter.next(); while self .polychrome_sprites_iter .next_if(|sprite| { (sprite.order, batch_kind) < max_order_and_kind && sprite.tile.texture_id == texture_id }) .is_some() { sprites_end += 1; } self.polychrome_sprites_start = sprites_end; Some(PrimitiveBatch::PolychromeSprites { texture_id, sprites: &self.polychrome_sprites[sprites_start..sprites_end], }) } PrimitiveKind::Surface => { let surfaces_start = self.surfaces_start; let mut surfaces_end = surfaces_start + 1; self.surfaces_iter.next(); while self .surfaces_iter .next_if(|surface| (surface.order, batch_kind) < max_order_and_kind) .is_some() { surfaces_end += 1; } self.surfaces_start = surfaces_end; Some(PrimitiveBatch::Surfaces( &self.surfaces[surfaces_start..surfaces_end], )) } } } } #[derive(Debug)] pub(crate) enum PrimitiveBatch<'a> { Shadows(&'a [Shadow]), Quads(&'a [Quad]), Paths(&'a [Path]), Underlines(&'a [Underline]), MonochromeSprites { texture_id: AtlasTextureId, sprites: &'a [MonochromeSprite], }, PolychromeSprites { texture_id: AtlasTextureId, sprites: &'a [PolychromeSprite], }, Surfaces(&'a [PaintSurface]), } #[derive(Default, Debug, Clone, Eq, PartialEq)] #[repr(C)] pub(crate) struct Quad { pub order: DrawOrder, pub pad: u32, // align to 8 bytes pub bounds: Bounds, pub content_mask: ContentMask, pub background: Hsla, pub border_color: Hsla, pub corner_radii: Corners, pub border_widths: Edges, } impl Ord for Quad { fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.order.cmp(&other.order) } } impl PartialOrd for Quad { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl From for Primitive { fn from(quad: Quad) -> Self { Primitive::Quad(quad) } } #[derive(Debug, Clone, Eq, PartialEq)] #[repr(C)] pub(crate) struct Underline { pub order: DrawOrder, pub pad: u32, // align to 8 bytes pub bounds: Bounds, pub content_mask: ContentMask, pub color: Hsla, pub thickness: ScaledPixels, pub wavy: bool, } impl Ord for Underline { fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.order.cmp(&other.order) } } impl PartialOrd for Underline { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl From for Primitive { fn from(underline: Underline) -> Self { Primitive::Underline(underline) } } #[derive(Debug, Clone, Eq, PartialEq)] #[repr(C)] pub(crate) struct Shadow { pub order: DrawOrder, pub blur_radius: ScaledPixels, pub bounds: Bounds, pub corner_radii: Corners, pub content_mask: ContentMask, pub color: Hsla, } impl Ord for Shadow { fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.order.cmp(&other.order) } } impl PartialOrd for Shadow { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl From for Primitive { fn from(shadow: Shadow) -> Self { Primitive::Shadow(shadow) } } /// A data type representing a 2 dimensional transformation that can be applied to an element. #[derive(Debug, Clone, Copy, PartialEq)] #[repr(C)] pub struct TransformationMatrix { /// 2x2 matrix containing rotation and scale, /// stored row-major pub rotation_scale: [[f32; 2]; 2], /// translation vector pub translation: [f32; 2], } impl Eq for TransformationMatrix {} impl TransformationMatrix { /// The unit matrix, has no effect. pub fn unit() -> Self { Self { rotation_scale: [[1.0, 0.0], [0.0, 1.0]], translation: [0.0, 0.0], } } /// Move the origin by a given point pub fn translate(mut self, point: Point) -> Self { self.compose(Self { rotation_scale: [[1.0, 0.0], [0.0, 1.0]], translation: [point.x.0, point.y.0], }) } /// Clockwise rotation in radians around the origin pub fn rotate(self, angle: Radians) -> Self { self.compose(Self { rotation_scale: [ [angle.0.cos(), -angle.0.sin()], [angle.0.sin(), angle.0.cos()], ], translation: [0.0, 0.0], }) } /// Scale around the origin pub fn scale(self, size: Size) -> Self { self.compose(Self { rotation_scale: [[size.width, 0.0], [0.0, size.height]], translation: [0.0, 0.0], }) } /// Perform matrix multiplication with another transformation /// to produce a new transformation that is the result of /// applying both transformations: first, `other`, then `self`. #[inline] pub fn compose(self, other: TransformationMatrix) -> TransformationMatrix { if other == Self::unit() { return self; } // Perform matrix multiplication TransformationMatrix { rotation_scale: [ [ self.rotation_scale[0][0] * other.rotation_scale[0][0] + self.rotation_scale[0][1] * other.rotation_scale[1][0], self.rotation_scale[0][0] * other.rotation_scale[0][1] + self.rotation_scale[0][1] * other.rotation_scale[1][1], ], [ self.rotation_scale[1][0] * other.rotation_scale[0][0] + self.rotation_scale[1][1] * other.rotation_scale[1][0], self.rotation_scale[1][0] * other.rotation_scale[0][1] + self.rotation_scale[1][1] * other.rotation_scale[1][1], ], ], translation: [ self.translation[0] + self.rotation_scale[0][0] * other.translation[0] + self.rotation_scale[0][1] * other.translation[1], self.translation[1] + self.rotation_scale[1][0] * other.translation[0] + self.rotation_scale[1][1] * other.translation[1], ], } } /// Apply transformation to a point, mainly useful for debugging pub fn apply(&self, point: Point) -> Point { let input = [point.x.0, point.y.0]; let mut output = self.translation; for i in 0..2 { for k in 0..2 { output[i] += self.rotation_scale[i][k] * input[k]; } } Point::new(output[0].into(), output[1].into()) } } impl Default for TransformationMatrix { fn default() -> Self { Self::unit() } } #[derive(Clone, Debug, Eq, PartialEq)] #[repr(C)] pub(crate) struct MonochromeSprite { pub order: DrawOrder, pub pad: u32, // align to 8 bytes pub bounds: Bounds, pub content_mask: ContentMask, pub color: Hsla, pub tile: AtlasTile, pub transformation: TransformationMatrix, } impl Ord for MonochromeSprite { fn cmp(&self, other: &Self) -> std::cmp::Ordering { match self.order.cmp(&other.order) { std::cmp::Ordering::Equal => self.tile.tile_id.cmp(&other.tile.tile_id), order => order, } } } impl PartialOrd for MonochromeSprite { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl From for Primitive { fn from(sprite: MonochromeSprite) -> Self { Primitive::MonochromeSprite(sprite) } } #[derive(Clone, Debug, Eq, PartialEq)] #[repr(C)] pub(crate) struct PolychromeSprite { pub order: DrawOrder, pub grayscale: bool, pub bounds: Bounds, pub content_mask: ContentMask, pub corner_radii: Corners, pub tile: AtlasTile, } impl Ord for PolychromeSprite { fn cmp(&self, other: &Self) -> std::cmp::Ordering { match self.order.cmp(&other.order) { std::cmp::Ordering::Equal => self.tile.tile_id.cmp(&other.tile.tile_id), order => order, } } } impl PartialOrd for PolychromeSprite { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl From for Primitive { fn from(sprite: PolychromeSprite) -> Self { Primitive::PolychromeSprite(sprite) } } #[derive(Clone, Debug, Eq, PartialEq)] pub(crate) struct PaintSurface { pub order: DrawOrder, pub bounds: Bounds, pub content_mask: ContentMask, #[cfg(target_os = "macos")] pub image_buffer: media::core_video::CVImageBuffer, } impl Ord for PaintSurface { fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.order.cmp(&other.order) } } impl PartialOrd for PaintSurface { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl From for Primitive { fn from(surface: PaintSurface) -> Self { Primitive::Surface(surface) } } #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] pub(crate) struct PathId(pub(crate) usize); /// A line made up of a series of vertices and control points. #[derive(Clone, Debug)] pub struct Path { pub(crate) id: PathId, order: DrawOrder, pub(crate) bounds: Bounds

, pub(crate) content_mask: ContentMask

, pub(crate) vertices: Vec>, pub(crate) color: Hsla, start: Point

, current: Point

, contour_count: usize, } impl Path { /// Create a new path with the given starting point. pub fn new(start: Point) -> Self { Self { id: PathId(0), order: DrawOrder::default(), vertices: Vec::new(), start, current: start, bounds: Bounds { origin: start, size: Default::default(), }, content_mask: Default::default(), color: Default::default(), contour_count: 0, } } /// Scale this path by the given factor. pub fn scale(&self, factor: f32) -> Path { Path { id: self.id, order: self.order, bounds: self.bounds.scale(factor), content_mask: self.content_mask.scale(factor), vertices: self .vertices .iter() .map(|vertex| vertex.scale(factor)) .collect(), start: self.start.map(|start| start.scale(factor)), current: self.current.scale(factor), contour_count: self.contour_count, color: self.color, } } /// Draw a straight line from the current point to the given point. pub fn line_to(&mut self, to: Point) { self.contour_count += 1; if self.contour_count > 1 { self.push_triangle( (self.start, self.current, to), (point(0., 1.), point(0., 1.), point(0., 1.)), ); } self.current = to; } /// Draw a curve from the current point to the given point, using the given control point. pub fn curve_to(&mut self, to: Point, ctrl: Point) { self.contour_count += 1; if self.contour_count > 1 { self.push_triangle( (self.start, self.current, to), (point(0., 1.), point(0., 1.), point(0., 1.)), ); } self.push_triangle( (self.current, ctrl, to), (point(0., 0.), point(0.5, 0.), point(1., 1.)), ); self.current = to; } fn push_triangle( &mut self, xy: (Point, Point, Point), st: (Point, Point, Point), ) { self.bounds = self .bounds .union(&Bounds { origin: xy.0, size: Default::default(), }) .union(&Bounds { origin: xy.1, size: Default::default(), }) .union(&Bounds { origin: xy.2, size: Default::default(), }); self.vertices.push(PathVertex { xy_position: xy.0, st_position: st.0, content_mask: Default::default(), }); self.vertices.push(PathVertex { xy_position: xy.1, st_position: st.1, content_mask: Default::default(), }); self.vertices.push(PathVertex { xy_position: xy.2, st_position: st.2, content_mask: Default::default(), }); } } impl Eq for Path {} impl PartialEq for Path { fn eq(&self, other: &Self) -> bool { self.order == other.order } } impl Ord for Path { fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.order.cmp(&other.order) } } impl PartialOrd for Path { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl From> for Primitive { fn from(path: Path) -> Self { Primitive::Path(path) } } #[derive(Clone, Debug)] #[repr(C)] pub(crate) struct PathVertex { pub(crate) xy_position: Point

, pub(crate) st_position: Point, pub(crate) content_mask: ContentMask

, } impl PathVertex { pub fn scale(&self, factor: f32) -> PathVertex { PathVertex { xy_position: self.xy_position.scale(factor), st_position: self.st_position, content_mask: self.content_mask.scale(factor), } } }