From 592cc685067c36cbba87e4db14f4ebc71d65b951 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 19 Apr 2020 21:55:23 +0200 Subject: Remove `Layer` trait and simplify `Canvas` --- wgpu/src/widget/canvas/cache.rs | 110 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 wgpu/src/widget/canvas/cache.rs (limited to 'wgpu/src/widget/canvas/cache.rs') diff --git a/wgpu/src/widget/canvas/cache.rs b/wgpu/src/widget/canvas/cache.rs new file mode 100644 index 00000000..c88239af --- /dev/null +++ b/wgpu/src/widget/canvas/cache.rs @@ -0,0 +1,110 @@ +use crate::{ + canvas::{Drawable, Frame, Geometry}, + Primitive, +}; + +use iced_native::Size; +use std::{cell::RefCell, sync::Arc}; + +enum State { + Empty, + Filled { + bounds: Size, + primitive: Arc, + }, +} + +impl Default for State { + fn default() -> Self { + State::Empty + } +} +/// A simple cache that stores generated geometry to avoid recomputation. +/// +/// A [`Cache`] will not redraw its geometry unless the dimensions of its layer +/// change or it is explicitly cleared. +/// +/// [`Layer`]: ../trait.Layer.html +/// [`Cache`]: struct.Cache.html +#[derive(Debug, Default)] +pub struct Cache { + state: RefCell, +} + +impl Cache { + /// Creates a new empty [`Cache`]. + /// + /// [`Cache`]: struct.Cache.html + pub fn new() -> Self { + Cache { + state: Default::default(), + } + } + + /// Clears the cache, forcing a redraw the next time it is used. + /// + /// [`Cached`]: struct.Cached.html + pub fn clear(&mut self) { + *self.state.borrow_mut() = State::Empty; + } + + pub fn draw(&self, new_bounds: Size, input: T) -> Geometry + where + T: Drawable + std::fmt::Debug, + { + use std::ops::Deref; + + if let State::Filled { bounds, primitive } = self.state.borrow().deref() + { + if *bounds == new_bounds { + return Geometry::from_primitive(primitive.clone()); + } + } + + let mut frame = Frame::new(new_bounds.width, new_bounds.height); + input.draw(&mut frame); + + let primitive = Arc::new(frame.into_primitive()); + + *self.state.borrow_mut() = State::Filled { + bounds: new_bounds, + primitive: primitive.clone(), + }; + + Geometry::from_primitive(primitive) + } + + pub fn with<'a, T>(&'a self, input: T) -> impl crate::canvas::State + 'a + where + T: Drawable + std::fmt::Debug + 'a, + { + Bind { cache: self, input } + } +} + +struct Bind<'a, T> { + cache: &'a Cache, + input: T, +} + +impl<'a, T> crate::canvas::State for Bind<'a, T> +where + T: Drawable + std::fmt::Debug + 'a, +{ + fn draw(&self, bounds: Size) -> Vec { + vec![self.cache.draw(bounds, &self.input)] + } +} + +impl std::fmt::Debug for State { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + State::Empty => write!(f, "Empty"), + State::Filled { primitive, bounds } => f + .debug_struct("Filled") + .field("primitive", primitive) + .field("bounds", bounds) + .finish(), + } + } +} -- cgit From 2381a9310c56f60698653f5fd13f5a0d80fa4f67 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Apr 2020 03:16:18 +0200 Subject: Ask for a `Size` in `Frame::new` --- wgpu/src/widget/canvas/cache.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'wgpu/src/widget/canvas/cache.rs') diff --git a/wgpu/src/widget/canvas/cache.rs b/wgpu/src/widget/canvas/cache.rs index c88239af..310bc1d3 100644 --- a/wgpu/src/widget/canvas/cache.rs +++ b/wgpu/src/widget/canvas/cache.rs @@ -61,7 +61,7 @@ impl Cache { } } - let mut frame = Frame::new(new_bounds.width, new_bounds.height); + let mut frame = Frame::new(new_bounds); input.draw(&mut frame); let primitive = Arc::new(frame.into_primitive()); -- cgit From 59b1e90661ee9e479f404bae71029db824cc7b46 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Apr 2020 03:18:31 +0200 Subject: Introduce `Translate` primitive in `iced_wgpu` --- wgpu/src/widget/canvas/cache.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'wgpu/src/widget/canvas/cache.rs') diff --git a/wgpu/src/widget/canvas/cache.rs b/wgpu/src/widget/canvas/cache.rs index 310bc1d3..12cc6442 100644 --- a/wgpu/src/widget/canvas/cache.rs +++ b/wgpu/src/widget/canvas/cache.rs @@ -57,21 +57,27 @@ impl Cache { if let State::Filled { bounds, primitive } = self.state.borrow().deref() { if *bounds == new_bounds { - return Geometry::from_primitive(primitive.clone()); + return Geometry::from_primitive(Primitive::Cached { + cache: primitive.clone(), + }); } } let mut frame = Frame::new(new_bounds); input.draw(&mut frame); - let primitive = Arc::new(frame.into_primitive()); + let primitive = { + let geometry = frame.into_geometry(); + + Arc::new(geometry.into_primitive()) + }; *self.state.borrow_mut() = State::Filled { bounds: new_bounds, primitive: primitive.clone(), }; - Geometry::from_primitive(primitive) + Geometry::from_primitive(Primitive::Cached { cache: primitive }) } pub fn with<'a, T>(&'a self, input: T) -> impl crate::canvas::State + 'a -- cgit From e4eb0553de13053c9828fd5454c281e27e598d65 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Apr 2020 03:46:03 +0200 Subject: Allow `canvas::State` to produce messages --- wgpu/src/widget/canvas/cache.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'wgpu/src/widget/canvas/cache.rs') diff --git a/wgpu/src/widget/canvas/cache.rs b/wgpu/src/widget/canvas/cache.rs index 12cc6442..2beed0f7 100644 --- a/wgpu/src/widget/canvas/cache.rs +++ b/wgpu/src/widget/canvas/cache.rs @@ -80,7 +80,10 @@ impl Cache { Geometry::from_primitive(Primitive::Cached { cache: primitive }) } - pub fn with<'a, T>(&'a self, input: T) -> impl crate::canvas::State + 'a + pub fn with<'a, T, Message>( + &'a self, + input: T, + ) -> impl crate::canvas::State + 'a where T: Drawable + std::fmt::Debug + 'a, { @@ -93,7 +96,7 @@ struct Bind<'a, T> { input: T, } -impl<'a, T> crate::canvas::State for Bind<'a, T> +impl<'a, T, Message> crate::canvas::State for Bind<'a, T> where T: Drawable + std::fmt::Debug + 'a, { -- cgit From 2539042b71d70afd4d8f262783d441e768811ee9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Apr 2020 06:24:12 +0200 Subject: Remove `Drawable` and rename `State` to `Program` --- wgpu/src/widget/canvas/cache.rs | 37 +++++++------------------------------ 1 file changed, 7 insertions(+), 30 deletions(-) (limited to 'wgpu/src/widget/canvas/cache.rs') diff --git a/wgpu/src/widget/canvas/cache.rs b/wgpu/src/widget/canvas/cache.rs index 2beed0f7..03643f74 100644 --- a/wgpu/src/widget/canvas/cache.rs +++ b/wgpu/src/widget/canvas/cache.rs @@ -1,5 +1,5 @@ use crate::{ - canvas::{Drawable, Frame, Geometry}, + canvas::{Frame, Geometry}, Primitive, }; @@ -48,10 +48,11 @@ impl Cache { *self.state.borrow_mut() = State::Empty; } - pub fn draw(&self, new_bounds: Size, input: T) -> Geometry - where - T: Drawable + std::fmt::Debug, - { + pub fn draw( + &self, + new_bounds: Size, + draw_fn: impl Fn(&mut Frame), + ) -> Geometry { use std::ops::Deref; if let State::Filled { bounds, primitive } = self.state.borrow().deref() @@ -64,7 +65,7 @@ impl Cache { } let mut frame = Frame::new(new_bounds); - input.draw(&mut frame); + draw_fn(&mut frame); let primitive = { let geometry = frame.into_geometry(); @@ -79,30 +80,6 @@ impl Cache { Geometry::from_primitive(Primitive::Cached { cache: primitive }) } - - pub fn with<'a, T, Message>( - &'a self, - input: T, - ) -> impl crate::canvas::State + 'a - where - T: Drawable + std::fmt::Debug + 'a, - { - Bind { cache: self, input } - } -} - -struct Bind<'a, T> { - cache: &'a Cache, - input: T, -} - -impl<'a, T, Message> crate::canvas::State for Bind<'a, T> -where - T: Drawable + std::fmt::Debug + 'a, -{ - fn draw(&self, bounds: Size) -> Vec { - vec![self.cache.draw(bounds, &self.input)] - } } impl std::fmt::Debug for State { -- cgit From d4c4198f7242f168de65146e0ca339e0c1cbfe9b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 30 Apr 2020 07:38:46 +0200 Subject: Write documentation for the new `canvas` API --- wgpu/src/widget/canvas/cache.rs | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'wgpu/src/widget/canvas/cache.rs') diff --git a/wgpu/src/widget/canvas/cache.rs b/wgpu/src/widget/canvas/cache.rs index 03643f74..4b28d164 100644 --- a/wgpu/src/widget/canvas/cache.rs +++ b/wgpu/src/widget/canvas/cache.rs @@ -19,13 +19,14 @@ impl Default for State { State::Empty } } -/// A simple cache that stores generated geometry to avoid recomputation. +/// A simple cache that stores generated [`Geometry`] to avoid recomputation. /// /// A [`Cache`] will not redraw its geometry unless the dimensions of its layer /// change or it is explicitly cleared. /// /// [`Layer`]: ../trait.Layer.html /// [`Cache`]: struct.Cache.html +/// [`Geometry`]: struct.Geometry.html #[derive(Debug, Default)] pub struct Cache { state: RefCell, @@ -41,30 +42,41 @@ impl Cache { } } - /// Clears the cache, forcing a redraw the next time it is used. + /// Clears the [`Cache`], forcing a redraw the next time it is used. /// - /// [`Cached`]: struct.Cached.html + /// [`Cache`]: struct.Cache.html pub fn clear(&mut self) { *self.state.borrow_mut() = State::Empty; } - pub fn draw( - &self, - new_bounds: Size, - draw_fn: impl Fn(&mut Frame), - ) -> Geometry { + /// Draws [`Geometry`] using the provided closure and stores it in the + /// [`Cache`]. + /// + /// The closure will only be called when + /// - the bounds have changed since the previous draw call. + /// - the [`Cache`] is empty or has been explicitly cleared. + /// + /// Otherwise, the previously stored [`Geometry`] will be returned. The + /// [`Cache`] is not cleared in this case. In other words, it will keep + /// returning the stored [`Geometry`] if needed. + /// + /// [`Cache`]: struct.Cache.html + pub fn draw(&self, bounds: Size, draw_fn: impl Fn(&mut Frame)) -> Geometry { use std::ops::Deref; - if let State::Filled { bounds, primitive } = self.state.borrow().deref() + if let State::Filled { + bounds: cached_bounds, + primitive, + } = self.state.borrow().deref() { - if *bounds == new_bounds { + if *cached_bounds == bounds { return Geometry::from_primitive(Primitive::Cached { cache: primitive.clone(), }); } } - let mut frame = Frame::new(new_bounds); + let mut frame = Frame::new(bounds); draw_fn(&mut frame); let primitive = { @@ -74,7 +86,7 @@ impl Cache { }; *self.state.borrow_mut() = State::Filled { - bounds: new_bounds, + bounds, primitive: primitive.clone(), }; -- cgit