From f4878a1a66a2e95e9430a8ccee8da823d2cb17ff Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 6 Oct 2022 08:04:43 -0700 Subject: Changed tesselation functions to take Vertex2D builder instead of using lyon's builtin Point type to avoid extra copies. --- graphics/src/widget/canvas/frame.rs | 48 ++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/graphics/src/widget/canvas/frame.rs b/graphics/src/widget/canvas/frame.rs index 6517d62a..427a2e2a 100644 --- a/graphics/src/widget/canvas/frame.rs +++ b/graphics/src/widget/canvas/frame.rs @@ -9,7 +9,6 @@ use crate::Primitive; use crate::layer::mesh; use crate::triangle::Vertex2D; use lyon::tessellation; -use lyon::tessellation::geometry_builder::Positions; /// The frame of a [`Canvas`]. /// @@ -17,10 +16,7 @@ use lyon::tessellation::geometry_builder::Positions; #[allow(missing_debug_implementations)] pub struct Frame { size: Size, - buffers: Vec<( - tessellation::VertexBuffers, - mesh::Style, - )>, + buffers: Vec<(tessellation::VertexBuffers, mesh::Style)>, primitives: Vec, transforms: Transforms, fill_tessellator: tessellation::FillTessellator, @@ -93,7 +89,7 @@ impl Frame { let mut buf = tessellation::VertexBuffers::new(); let mut buffers = - tessellation::BuffersBuilder::new(&mut buf, Positions); + tessellation::BuffersBuilder::new(&mut buf, Vertex2DBuilder); let options = tessellation::FillOptions::default().with_fill_rule(rule.into()); @@ -131,7 +127,7 @@ impl Frame { let mut buf = tessellation::VertexBuffers::new(); let mut buffers = - tessellation::BuffersBuilder::new(&mut buf, Positions); + tessellation::BuffersBuilder::new(&mut buf, Vertex2DBuilder); let top_left = self.transforms.current.raw.transform_point( @@ -165,7 +161,7 @@ impl Frame { let mut buf = tessellation::VertexBuffers::new(); let mut buffers = - tessellation::BuffersBuilder::new(&mut buf, Positions); + tessellation::BuffersBuilder::new(&mut buf, Vertex2DBuilder); let mut options = tessellation::StrokeOptions::default(); options.line_width = stroke.width; @@ -342,7 +338,7 @@ impl Frame { if !buffer.indices.is_empty() { self.primitives.push(Primitive::Mesh2D { buffers: triangle::Mesh2D { - vertices: vertices_from(buffer.vertices), + vertices: buffer.vertices, indices: buffer.indices, }, size: self.size, @@ -355,12 +351,30 @@ impl Frame { } } -/// Converts from [`lyon::math::Point`] to [`Vertex2D`]. Used for generating primitives. -fn vertices_from(points: Vec) -> Vec { - points - .iter() - .map(|p| Vertex2D { - position: [p.x, p.y], - }) - .collect() +struct Vertex2DBuilder; + +impl tessellation::FillVertexConstructor for Vertex2DBuilder { + fn new_vertex( + &mut self, + vertex: tessellation::FillVertex<'_>, + ) -> Vertex2D { + let position = vertex.position(); + + Vertex2D { + position: [position.x, position.y], + } + } } + +impl tessellation::StrokeVertexConstructor for Vertex2DBuilder { + fn new_vertex( + &mut self, + vertex: tessellation::StrokeVertex<'_, '_>, + ) -> Vertex2D { + let position = vertex.position(); + + Vertex2D { + position: [position.x, position.y], + } + } +} \ No newline at end of file -- cgit