From 188db4da48954b95a3fe79bcd22689ffc3a661e0 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 21 Mar 2024 05:52:48 +0100 Subject: Draft support for dynamic custom renderer injection --- renderer/src/geometry/cache.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'renderer/src/geometry') diff --git a/renderer/src/geometry/cache.rs b/renderer/src/geometry/cache.rs index 3aff76b9..6a43ddb6 100644 --- a/renderer/src/geometry/cache.rs +++ b/renderer/src/geometry/cache.rs @@ -1,4 +1,5 @@ use crate::core::Size; +use crate::custom; use crate::geometry::{Frame, Geometry}; use crate::Renderer; @@ -29,6 +30,7 @@ enum Internal { TinySkia(Arc), #[cfg(feature = "wgpu")] Wgpu(Arc), + Custom(Arc), } impl Cache { @@ -82,6 +84,9 @@ impl Cache { content: primitive.clone(), }); } + Internal::Custom(geometry) => { + return Geometry::Custom(geometry.clone().load()) + } } } } @@ -100,6 +105,9 @@ impl Cache { Geometry::Wgpu(primitive) => { Internal::Wgpu(Arc::new(primitive)) } + Geometry::Custom(geometry) => { + Internal::Custom(geometry.cache()) + } } }; @@ -120,6 +128,7 @@ impl Cache { content: primitive, }) } + Internal::Custom(geometry) => Geometry::Custom(geometry.load()), } } } -- cgit From 9171df1e356530410a7ceadadd78fd3dcf150dfd Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 21 Mar 2024 06:00:55 +0100 Subject: Gate `Custom` variants in `iced_renderer` behind `custom` feature --- renderer/src/geometry/cache.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'renderer/src/geometry') diff --git a/renderer/src/geometry/cache.rs b/renderer/src/geometry/cache.rs index 6a43ddb6..20f73f22 100644 --- a/renderer/src/geometry/cache.rs +++ b/renderer/src/geometry/cache.rs @@ -1,5 +1,4 @@ use crate::core::Size; -use crate::custom; use crate::geometry::{Frame, Geometry}; use crate::Renderer; @@ -30,7 +29,8 @@ enum Internal { TinySkia(Arc), #[cfg(feature = "wgpu")] Wgpu(Arc), - Custom(Arc), + #[cfg(feature = "custom")] + Custom(Arc), } impl Cache { @@ -84,6 +84,7 @@ impl Cache { content: primitive.clone(), }); } + #[cfg(feature = "custom")] Internal::Custom(geometry) => { return Geometry::Custom(geometry.clone().load()) } @@ -105,6 +106,7 @@ impl Cache { Geometry::Wgpu(primitive) => { Internal::Wgpu(Arc::new(primitive)) } + #[cfg(feature = "custom")] Geometry::Custom(geometry) => { Internal::Custom(geometry.cache()) } @@ -128,6 +130,7 @@ impl Cache { content: primitive, }) } + #[cfg(feature = "custom")] Internal::Custom(geometry) => Geometry::Custom(geometry.load()), } } -- cgit From 53a183fe0d6aed460fbb8155ac9541757277aab3 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 22 Mar 2024 01:35:14 +0100 Subject: Restore `canvas::Frame` API --- renderer/src/geometry/cache.rs | 137 ----------------------------------------- 1 file changed, 137 deletions(-) delete mode 100644 renderer/src/geometry/cache.rs (limited to 'renderer/src/geometry') diff --git a/renderer/src/geometry/cache.rs b/renderer/src/geometry/cache.rs deleted file mode 100644 index 20f73f22..00000000 --- a/renderer/src/geometry/cache.rs +++ /dev/null @@ -1,137 +0,0 @@ -use crate::core::Size; -use crate::geometry::{Frame, Geometry}; -use crate::Renderer; - -use std::cell::RefCell; -use std::sync::Arc; - -/// 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. -#[derive(Debug, Default)] -pub struct Cache { - state: RefCell, -} - -#[derive(Debug, Default)] -enum State { - #[default] - Empty, - Filled { - bounds: Size, - primitive: Internal, - }, -} - -#[derive(Debug, Clone)] -enum Internal { - TinySkia(Arc), - #[cfg(feature = "wgpu")] - Wgpu(Arc), - #[cfg(feature = "custom")] - Custom(Arc), -} - -impl Cache { - /// Creates a new empty [`Cache`]. - pub fn new() -> Self { - Cache { - state: RefCell::default(), - } - } - - /// Clears the [`Cache`], forcing a redraw the next time it is used. - pub fn clear(&self) { - *self.state.borrow_mut() = State::Empty; - } - - /// 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. - pub fn draw( - &self, - renderer: &Renderer, - bounds: Size, - draw_fn: impl FnOnce(&mut Frame), - ) -> Geometry { - use std::ops::Deref; - - if let State::Filled { - bounds: cached_bounds, - primitive, - } = self.state.borrow().deref() - { - if *cached_bounds == bounds { - match primitive { - Internal::TinySkia(primitive) => { - return Geometry::TinySkia( - iced_tiny_skia::Primitive::Cache { - content: primitive.clone(), - }, - ); - } - #[cfg(feature = "wgpu")] - Internal::Wgpu(primitive) => { - return Geometry::Wgpu(iced_wgpu::Primitive::Cache { - content: primitive.clone(), - }); - } - #[cfg(feature = "custom")] - Internal::Custom(geometry) => { - return Geometry::Custom(geometry.clone().load()) - } - } - } - } - - let mut frame = Frame::new(renderer, bounds); - draw_fn(&mut frame); - - let primitive = { - let geometry = frame.into_geometry(); - - match geometry { - Geometry::TinySkia(primitive) => { - Internal::TinySkia(Arc::new(primitive)) - } - #[cfg(feature = "wgpu")] - Geometry::Wgpu(primitive) => { - Internal::Wgpu(Arc::new(primitive)) - } - #[cfg(feature = "custom")] - Geometry::Custom(geometry) => { - Internal::Custom(geometry.cache()) - } - } - }; - - *self.state.borrow_mut() = State::Filled { - bounds, - primitive: primitive.clone(), - }; - - match primitive { - Internal::TinySkia(primitive) => { - Geometry::TinySkia(iced_tiny_skia::Primitive::Cache { - content: primitive, - }) - } - #[cfg(feature = "wgpu")] - Internal::Wgpu(primitive) => { - Geometry::Wgpu(iced_wgpu::Primitive::Cache { - content: primitive, - }) - } - #[cfg(feature = "custom")] - Internal::Custom(geometry) => Geometry::Custom(geometry.load()), - } - } -} -- cgit