diff options
author | 2019-12-15 07:03:54 +0100 | |
---|---|---|
committer | 2019-12-15 07:07:49 +0100 | |
commit | 514ccf8a72d660d77f26e085b545e5104389c138 (patch) | |
tree | 9919f2ee30ab29c83a2455f838313ab5bfb2f146 /wgpu | |
parent | 232d4873ba0fb9b87d08c8d70b117e81aa7489b5 (diff) | |
download | iced-514ccf8a72d660d77f26e085b545e5104389c138.tar.gz iced-514ccf8a72d660d77f26e085b545e5104389c138.tar.bz2 iced-514ccf8a72d660d77f26e085b545e5104389c138.zip |
Cache `Svg` load result properly
This avoids trying to reload the file constantly on every frame.
Diffstat (limited to 'wgpu')
-rw-r--r-- | wgpu/src/image.rs | 7 | ||||
-rw-r--r-- | wgpu/src/image/vector.rs | 35 |
2 files changed, 22 insertions, 20 deletions
diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index e30f70a7..4558ffb0 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -215,12 +215,9 @@ impl Pipeline { #[cfg(feature = "svg")] pub fn viewport_dimensions(&self, handle: &svg::Handle) -> (u32, u32) { let mut cache = self.vector_cache.borrow_mut(); + let svg = cache.load(&handle); - if let Some(svg) = cache.load(&handle) { - svg.viewport_dimensions() - } else { - (1, 1) - } + svg.viewport_dimensions() } pub fn draw( diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index 097223a5..aa712372 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -4,15 +4,21 @@ use std::{ rc::Rc, }; -pub struct Svg { - tree: resvg::usvg::Tree, +pub enum Svg { + Loaded { tree: resvg::usvg::Tree }, + NotFound, } impl Svg { pub fn viewport_dimensions(&self) -> (u32, u32) { - let size = self.tree.svg_node().size; + match self { + Svg::Loaded { tree } => { + let size = tree.svg_node().size; - (size.width() as u32, size.height() as u32) + (size.width() as u32, size.height() as u32) + } + Svg::NotFound => (1, 1), + } } } @@ -40,21 +46,20 @@ impl Cache { } } - pub fn load(&mut self, handle: &svg::Handle) -> Option<&Svg> { + pub fn load(&mut self, handle: &svg::Handle) -> &Svg { if self.svgs.contains_key(&handle.id()) { - return self.svgs.get(&handle.id()); + return self.svgs.get(&handle.id()).unwrap(); } let opt = resvg::Options::default(); - match resvg::usvg::Tree::from_file(handle.path(), &opt.usvg) { - Ok(tree) => { - let _ = self.svgs.insert(handle.id(), Svg { tree }); - } - Err(_) => {} + let svg = match resvg::usvg::Tree::from_file(handle.path(), &opt.usvg) { + Ok(tree) => Svg::Loaded { tree }, + Err(_) => Svg::NotFound, }; - self.svgs.get(&handle.id()) + let _ = self.svgs.insert(handle.id(), svg); + self.svgs.get(&handle.id()).unwrap() } pub fn upload( @@ -85,7 +90,7 @@ impl Cache { } match self.load(handle) { - Some(svg) => { + Svg::Loaded { tree } => { let extent = wgpu::Extent3d { width, height, @@ -113,7 +118,7 @@ impl Cache { ); resvg::backend_raqote::render_to_canvas( - &svg.tree, + &tree, &resvg::Options::default(), screen_size, &mut canvas, @@ -171,7 +176,7 @@ impl Cache { Some(bind_group) } - None => None, + Svg::NotFound => None, } } |