From 514ccf8a72d660d77f26e085b545e5104389c138 Mon Sep 17 00:00:00 2001
From: Héctor Ramón Jiménez <hector0193@gmail.com>
Date: Sun, 15 Dec 2019 07:03:54 +0100
Subject: Cache `Svg` load result properly

This avoids trying to reload the file constantly on every frame.
---
 wgpu/src/image.rs        |  7 ++-----
 wgpu/src/image/vector.rs | 35 ++++++++++++++++++++---------------
 2 files changed, 22 insertions(+), 20 deletions(-)

(limited to 'wgpu/src')

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,
         }
     }
 
-- 
cgit