summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-03-07 23:45:54 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-03-07 23:45:54 +0100
commitb74e7e7353d69ffb54cf0c0f0574ea7abf0f3a68 (patch)
treed05e074ad1a57309ae1ed09f49fee1cbb78a6ea6
parent37f0d97159d81dbd4801e287a06f4e243e483269 (diff)
downloadiced-b74e7e7353d69ffb54cf0c0f0574ea7abf0f3a68.tar.gz
iced-b74e7e7353d69ffb54cf0c0f0574ea7abf0f3a68.tar.bz2
iced-b74e7e7353d69ffb54cf0c0f0574ea7abf0f3a68.zip
Implement `Primitive::Cached`
Diffstat (limited to '')
-rw-r--r--core/src/point.rs11
-rw-r--r--core/src/vector.rs11
-rw-r--r--examples/bezier_tool/src/main.rs5
-rw-r--r--examples/geometry/src/main.rs4
-rw-r--r--wgpu/src/primitive.rs13
-rw-r--r--wgpu/src/renderer.rs112
-rw-r--r--wgpu/src/triangle.rs4
-rw-r--r--wgpu/src/widget/canvas.rs5
-rw-r--r--wgpu/src/widget/canvas/frame.rs59
-rw-r--r--wgpu/src/widget/canvas/layer.rs13
-rw-r--r--wgpu/src/widget/canvas/layer/cache.rs13
-rw-r--r--wgpu/src/widget/canvas/text.rs8
12 files changed, 152 insertions, 106 deletions
diff --git a/core/src/point.rs b/core/src/point.rs
index b9a8149c..b55f5099 100644
--- a/core/src/point.rs
+++ b/core/src/point.rs
@@ -46,3 +46,14 @@ impl std::ops::Add<Vector> for Point {
}
}
}
+
+impl std::ops::Sub<Vector> for Point {
+ type Output = Self;
+
+ fn sub(self, vector: Vector) -> Self {
+ Self {
+ x: self.x - vector.x,
+ y: self.y - vector.y,
+ }
+ }
+}
diff --git a/core/src/vector.rs b/core/src/vector.rs
index 4c1cbfab..a75053a0 100644
--- a/core/src/vector.rs
+++ b/core/src/vector.rs
@@ -32,6 +32,17 @@ where
}
}
+impl<T> std::ops::Sub for Vector<T>
+where
+ T: std::ops::Sub<Output = T>,
+{
+ type Output = Self;
+
+ fn sub(self, b: Self) -> Self {
+ Self::new(self.x - b.x, self.y - b.y)
+ }
+}
+
impl<T> Default for Vector<T>
where
T: Default,
diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs
index 023eb0f7..c3fbf276 100644
--- a/examples/bezier_tool/src/main.rs
+++ b/examples/bezier_tool/src/main.rs
@@ -23,7 +23,6 @@ mod bezier {
basic_shapes, BuffersBuilder, StrokeAttributes, StrokeOptions,
StrokeTessellator, VertexBuffers,
};
- use std::sync::Arc;
pub struct Bezier<'a, Message> {
state: &'a mut State,
@@ -175,10 +174,10 @@ mod bezier {
let mesh = Primitive::Mesh2D {
origin: Point::new(bounds.x, bounds.y),
- buffers: Arc::new(Mesh2D {
+ buffers: Mesh2D {
vertices: buffer.vertices,
indices: buffer.indices,
- }),
+ },
};
(
diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs
index 795c6a71..13a687ab 100644
--- a/examples/geometry/src/main.rs
+++ b/examples/geometry/src/main.rs
@@ -87,7 +87,7 @@ mod rainbow {
(
Primitive::Mesh2D {
origin: Point::new(b.x, b.y),
- buffers: std::sync::Arc::new(Mesh2D {
+ buffers: Mesh2D {
vertices: vec![
Vertex2D {
position: posn_center,
@@ -136,7 +136,7 @@ mod rainbow {
0, 7, 8, // BL
0, 8, 1, // L
],
- }),
+ },
},
MouseCursor::OutOfBounds,
)
diff --git a/wgpu/src/primitive.rs b/wgpu/src/primitive.rs
index 823b4b72..46d9e624 100644
--- a/wgpu/src/primitive.rs
+++ b/wgpu/src/primitive.rs
@@ -78,7 +78,18 @@ pub enum Primitive {
origin: Point,
/// The vertex and index buffers of the mesh
- buffers: Arc<triangle::Mesh2D>,
+ buffers: triangle::Mesh2D,
+ },
+ /// A cached primitive.
+ ///
+ /// This can be useful if you are implementing a widget where primitive
+ /// generation is expensive.
+ Cached {
+ /// The origin of the coordinate system of the cached primitives
+ origin: Point,
+
+ /// The cached primitive
+ cache: Arc<Primitive>,
},
}
diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs
index 1da19b1a..c06af339 100644
--- a/wgpu/src/renderer.rs
+++ b/wgpu/src/renderer.rs
@@ -10,7 +10,6 @@ use iced_native::{
layout, Background, Color, Layout, MouseCursor, Point, Rectangle, Vector,
Widget,
};
-use std::sync::Arc;
mod widget;
@@ -29,9 +28,8 @@ pub struct Renderer {
struct Layer<'a> {
bounds: Rectangle<u32>,
- offset: Vector<u32>,
quads: Vec<Quad>,
- meshes: Vec<(Point, Arc<triangle::Mesh2D>)>,
+ meshes: Vec<(Point, &'a triangle::Mesh2D)>,
text: Vec<wgpu_glyph::Section<'a>>,
#[cfg(any(feature = "image", feature = "svg"))]
@@ -39,10 +37,9 @@ struct Layer<'a> {
}
impl<'a> Layer<'a> {
- pub fn new(bounds: Rectangle<u32>, offset: Vector<u32>) -> Self {
+ pub fn new(bounds: Rectangle<u32>) -> Self {
Self {
bounds,
- offset,
quads: Vec::new(),
text: Vec::new(),
meshes: Vec::new(),
@@ -103,17 +100,14 @@ impl Renderer {
let mut layers = Vec::new();
- layers.push(Layer::new(
- Rectangle {
- x: 0,
- y: 0,
- width: u32::from(width),
- height: u32::from(height),
- },
- Vector::new(0, 0),
- ));
-
- self.draw_primitive(primitive, &mut layers);
+ layers.push(Layer::new(Rectangle {
+ x: 0,
+ y: 0,
+ width: u32::from(width),
+ height: u32::from(height),
+ }));
+
+ self.draw_primitive(Vector::new(0.0, 0.0), primitive, &mut layers);
self.draw_overlay(overlay, &mut layers);
for layer in layers {
@@ -137,17 +131,16 @@ impl Renderer {
fn draw_primitive<'a>(
&mut self,
+ translation: Vector,
primitive: &'a Primitive,
layers: &mut Vec<Layer<'a>>,
) {
- let layer = layers.last_mut().unwrap();
-
match primitive {
Primitive::None => {}
Primitive::Group { primitives } => {
// TODO: Inspect a bit and regroup (?)
for primitive in primitives {
- self.draw_primitive(primitive, layers)
+ self.draw_primitive(translation, primitive, layers)
}
}
Primitive::Text {
@@ -179,12 +172,11 @@ impl Renderer {
}
};
+ let layer = layers.last_mut().unwrap();
+
layer.text.push(wgpu_glyph::Section {
text: &content,
- screen_position: (
- x - layer.offset.x as f32,
- y - layer.offset.y as f32,
- ),
+ screen_position: (x + translation.x, y + translation.y),
bounds: (bounds.width, bounds.height),
scale: wgpu_glyph::Scale { x: *size, y: *size },
color: color.into_linear(),
@@ -222,11 +214,13 @@ impl Renderer {
border_width,
border_color,
} => {
- // TODO: Move some of this computations to the GPU (?)
+ let layer = layers.last_mut().unwrap();
+
+ // TODO: Move some of these computations to the GPU (?)
layer.quads.push(Quad {
position: [
- bounds.x - layer.offset.x as f32,
- bounds.y - layer.offset.y as f32,
+ bounds.x + translation.x,
+ bounds.y + translation.y,
],
scale: [bounds.width, bounds.height],
color: match background {
@@ -238,38 +232,59 @@ impl Renderer {
});
}
Primitive::Mesh2D { origin, buffers } => {
- layer.meshes.push((*origin, buffers.clone()));
+ let layer = layers.last_mut().unwrap();
+
+ layer.meshes.push((*origin + translation, buffers));
}
Primitive::Clip {
bounds,
offset,
content,
} => {
+ let layer = layers.last_mut().unwrap();
+
let layer_bounds: Rectangle<f32> = layer.bounds.into();
let clip = Rectangle {
- x: bounds.x - layer.offset.x as f32,
- y: bounds.y - layer.offset.y as f32,
+ x: bounds.x + translation.x,
+ y: bounds.y + translation.y,
..*bounds
};
// Only draw visible content
if let Some(clip_bounds) = layer_bounds.intersection(&clip) {
- let clip_layer =
- Layer::new(clip_bounds.into(), layer.offset + *offset);
- let new_layer = Layer::new(layer.bounds, layer.offset);
+ let clip_layer = Layer::new(clip_bounds.into());
+ let new_layer = Layer::new(layer.bounds);
layers.push(clip_layer);
- self.draw_primitive(content, layers);
+ self.draw_primitive(
+ translation
+ - Vector::new(offset.x as f32, offset.y as f32),
+ content,
+ layers,
+ );
layers.push(new_layer);
}
}
+ Primitive::Cached { origin, cache } => {
+ self.draw_primitive(
+ translation + Vector::new(origin.x, origin.y),
+ &cache,
+ layers,
+ );
+ }
+
#[cfg(feature = "image")]
Primitive::Image { handle, bounds } => {
+ let layer = layers.last_mut().unwrap();
+
layer.images.push(Image {
handle: image::Handle::Raster(handle.clone()),
- position: [bounds.x, bounds.y],
+ position: [
+ bounds.x + translation.x,
+ bounds.y + translation.y,
+ ],
size: [bounds.width, bounds.height],
});
}
@@ -278,9 +293,14 @@ impl Renderer {
#[cfg(feature = "svg")]
Primitive::Svg { handle, bounds } => {
+ let layer = layers.last_mut().unwrap();
+
layer.images.push(Image {
handle: image::Handle::Vector(handle.clone()),
- position: [bounds.x, bounds.y],
+ position: [
+ bounds.x + translation.x,
+ bounds.y + translation.y,
+ ],
size: [bounds.width, bounds.height],
});
}
@@ -295,7 +315,7 @@ impl Renderer {
layers: &mut Vec<Layer<'a>>,
) {
let first = layers.first().unwrap();
- let mut overlay = Layer::new(first.bounds, Vector::new(0, 0));
+ let mut overlay = Layer::new(first.bounds);
let font_id = self.text_pipeline.overlay_font();
let scale = wgpu_glyph::Scale { x: 20.0, y: 20.0 };
@@ -337,12 +357,8 @@ impl Renderer {
let bounds = layer.bounds * scale_factor;
if layer.meshes.len() > 0 {
- let translated = transformation
- * Transformation::scale(scale_factor, scale_factor)
- * Transformation::translate(
- -(layer.offset.x as f32),
- -(layer.offset.y as f32),
- );
+ let scaled = transformation
+ * Transformation::scale(scale_factor, scale_factor);
self.triangle_pipeline.draw(
device,
@@ -350,7 +366,7 @@ impl Renderer {
target,
target_width,
target_height,
- translated,
+ scaled,
&layer.meshes,
bounds,
);
@@ -371,18 +387,14 @@ impl Renderer {
#[cfg(any(feature = "image", feature = "svg"))]
{
if layer.images.len() > 0 {
- let translated_and_scaled = transformation
- * Transformation::scale(scale_factor, scale_factor)
- * Transformation::translate(
- -(layer.offset.x as f32),
- -(layer.offset.y as f32),
- );
+ let scaled = transformation
+ * Transformation::scale(scale_factor, scale_factor);
self.image_pipeline.draw(
device,
encoder,
&layer.images,
- translated_and_scaled,
+ scaled,
bounds,
target,
scale_factor,
diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs
index 85ed4bd5..be61cc15 100644
--- a/wgpu/src/triangle.rs
+++ b/wgpu/src/triangle.rs
@@ -1,7 +1,7 @@
//! Draw meshes of triangles.
use crate::{settings, Transformation};
use iced_native::{Point, Rectangle};
-use std::{mem, sync::Arc};
+use std::mem;
mod msaa;
@@ -194,7 +194,7 @@ impl Pipeline {
target_width: u32,
target_height: u32,
transformation: Transformation,
- meshes: &Vec<(Point, Arc<Mesh2D>)>,
+ meshes: &Vec<(Point, &Mesh2D)>,
bounds: Rectangle<u32>,
) {
// This looks a bit crazy, but we are just counting how many vertices
diff --git a/wgpu/src/widget/canvas.rs b/wgpu/src/widget/canvas.rs
index 9741ee9d..3a9605c9 100644
--- a/wgpu/src/widget/canvas.rs
+++ b/wgpu/src/widget/canvas.rs
@@ -123,7 +123,10 @@ impl<'a, Message> Widget<Message, Renderer> for Canvas<'a> {
primitives: self
.layers
.iter()
- .map(|layer| layer.draw(origin, size))
+ .map(|layer| Primitive::Cached {
+ origin,
+ cache: layer.draw(size),
+ })
.collect(),
},
MouseCursor::Idle,
diff --git a/wgpu/src/widget/canvas/frame.rs b/wgpu/src/widget/canvas/frame.rs
index 5b82c951..0bf58320 100644
--- a/wgpu/src/widget/canvas/frame.rs
+++ b/wgpu/src/widget/canvas/frame.rs
@@ -1,12 +1,10 @@
-use iced_native::{Point, Size, Vector};
+use iced_native::{Point, Rectangle, Size, Vector};
use crate::{
canvas::{Fill, Path, Stroke, Text},
triangle, Primitive,
};
-use std::sync::Arc;
-
/// The frame of a [`Canvas`].
///
/// [`Canvas`]: struct.Canvas.html
@@ -15,8 +13,8 @@ pub struct Frame {
width: f32,
height: f32,
buffers: lyon::tessellation::VertexBuffers<triangle::Vertex2D, u32>,
+ primitives: Vec<Primitive>,
transforms: Transforms,
- texts: Vec<Text>,
}
#[derive(Debug)]
@@ -43,7 +41,7 @@ impl Frame {
width,
height,
buffers: lyon::tessellation::VertexBuffers::new(),
- texts: Vec::new(),
+ primitives: Vec::new(),
transforms: Transforms {
previous: Vec::new(),
current: Transform {
@@ -165,7 +163,23 @@ impl Frame {
/// [`Frame`]: struct.Frame.html
#[inline]
pub fn fill_text(&mut self, text: Text) {
- self.texts.push(text);
+ use std::f32;
+
+ // TODO: Use vectorial text instead of primitive
+ self.primitives.push(Primitive::Text {
+ content: text.content,
+ bounds: Rectangle {
+ x: text.position.x,
+ y: text.position.y,
+ width: f32::INFINITY,
+ height: f32::INFINITY,
+ },
+ color: text.color,
+ size: text.size,
+ font: text.font,
+ horizontal_alignment: text.horizontal_alignment,
+ vertical_alignment: text.vertical_alignment,
+ });
}
/// Stores the current transform of the [`Frame`] and executes the given
@@ -226,35 +240,18 @@ impl Frame {
/// Produces the primitive representing everything drawn on the [`Frame`].
///
/// [`Frame`]: struct.Frame.html
- pub fn into_primitive(self, origin: Point) -> Primitive {
- let mut primitives: Vec<Primitive> = self
- .texts
- .into_iter()
- .map(|mut t| {
- t.bounds.x += origin.x;
- t.bounds.y += origin.y;
-
- Primitive::Text {
- content: t.content,
- bounds: t.bounds,
- color: t.color,
- size: t.size,
- font: t.font,
- horizontal_alignment: t.horizontal_alignment,
- vertical_alignment: t.vertical_alignment,
- }
- })
- .collect();
-
- primitives.push(Primitive::Mesh2D {
- origin,
- buffers: Arc::new(triangle::Mesh2D {
+ pub fn into_primitive(mut self) -> Primitive {
+ self.primitives.push(Primitive::Mesh2D {
+ origin: Point::ORIGIN,
+ buffers: triangle::Mesh2D {
vertices: self.buffers.vertices,
indices: self.buffers.indices,
- }),
+ },
});
- Primitive::Group { primitives }
+ Primitive::Group {
+ primitives: self.primitives,
+ }
}
}
diff --git a/wgpu/src/widget/canvas/layer.rs b/wgpu/src/widget/canvas/layer.rs
index 95e2d0ee..a46b7fb1 100644
--- a/wgpu/src/widget/canvas/layer.rs
+++ b/wgpu/src/widget/canvas/layer.rs
@@ -4,21 +4,22 @@ mod cache;
pub use cache::Cache;
use crate::Primitive;
+use iced_native::Size;
-use iced_native::{Point, Size};
+use std::sync::Arc;
/// A layer that can be presented at a [`Canvas`].
///
/// [`Canvas`]: ../struct.Canvas.html
pub trait Layer: std::fmt::Debug {
- /// Draws the [`Layer`] in the given bounds and produces [`Mesh2D`] as a
- /// result.
+ /// Draws the [`Layer`] in the given bounds and produces a [`Primitive`] as
+ /// a result.
///
- /// The [`Layer`] may choose to store the produced [`Mesh2D`] locally and
+ /// The [`Layer`] may choose to store the produced [`Primitive`] locally and
/// only recompute it when the bounds change, its contents change, or is
/// otherwise explicitly cleared by other means.
///
/// [`Layer`]: trait.Layer.html
- /// [`Mesh2D`]: ../../../triangle/struct.Mesh2D.html
- fn draw(&self, origin: Point, bounds: Size) -> Primitive;
+ /// [`Primitive`]: ../../../enum.Primitive.html
+ fn draw(&self, bounds: Size) -> Arc<Primitive>;
}
diff --git a/wgpu/src/widget/canvas/layer/cache.rs b/wgpu/src/widget/canvas/layer/cache.rs
index f05028da..6b69f01e 100644
--- a/wgpu/src/widget/canvas/layer/cache.rs
+++ b/wgpu/src/widget/canvas/layer/cache.rs
@@ -3,8 +3,8 @@ use crate::{
Primitive,
};
-use iced_native::{Point, Size};
-use std::{cell::RefCell, marker::PhantomData};
+use iced_native::Size;
+use std::{cell::RefCell, marker::PhantomData, sync::Arc};
/// A simple cache that stores generated geometry to avoid recomputation.
///
@@ -22,7 +22,10 @@ pub struct Cache<T: Drawable> {
#[derive(Debug)]
enum State {
Empty,
- Filled { bounds: Size, primitive: Primitive },
+ Filled {
+ bounds: Size,
+ primitive: Arc<Primitive>,
+ },
}
impl<T> Cache<T>
@@ -70,7 +73,7 @@ impl<'a, T> Layer for Bind<'a, T>
where
T: Drawable + std::fmt::Debug,
{
- fn draw(&self, origin: Point, current_bounds: Size) -> Primitive {
+ fn draw(&self, current_bounds: Size) -> Arc<Primitive> {
use std::ops::Deref;
if let State::Filled { bounds, primitive } =
@@ -84,7 +87,7 @@ where
let mut frame = Frame::new(current_bounds.width, current_bounds.height);
self.input.draw(&mut frame);
- let primitive = frame.into_primitive(origin);
+ let primitive = Arc::new(frame.into_primitive());
*self.cache.state.borrow_mut() = State::Filled {
bounds: current_bounds,
diff --git a/wgpu/src/widget/canvas/text.rs b/wgpu/src/widget/canvas/text.rs
index 241f8503..1f9bdc19 100644
--- a/wgpu/src/widget/canvas/text.rs
+++ b/wgpu/src/widget/canvas/text.rs
@@ -1,14 +1,12 @@
-use iced_native::{
- Color, Font, HorizontalAlignment, Rectangle, VerticalAlignment,
-};
+use iced_native::{Color, Font, HorizontalAlignment, Point, VerticalAlignment};
/// A bunch of text that can be drawn to a canvas
#[derive(Debug, Clone)]
pub struct Text {
/// The contents of the text
pub content: String,
- /// The bounds of the text
- pub bounds: Rectangle,
+ /// The position where to begin drawing the text (top-left corner coordinates)
+ pub position: Point,
/// The color of the text
pub color: Color,
/// The size of the text