Cache Svg load result properly
This avoids trying to reload the file constantly on every frame.
This commit is contained in:
parent
232d4873ba
commit
514ccf8a72
2 changed files with 22 additions and 20 deletions
|
|
@ -215,12 +215,9 @@ impl Pipeline {
|
||||||
#[cfg(feature = "svg")]
|
#[cfg(feature = "svg")]
|
||||||
pub fn viewport_dimensions(&self, handle: &svg::Handle) -> (u32, u32) {
|
pub fn viewport_dimensions(&self, handle: &svg::Handle) -> (u32, u32) {
|
||||||
let mut cache = self.vector_cache.borrow_mut();
|
let mut cache = self.vector_cache.borrow_mut();
|
||||||
|
let svg = cache.load(&handle);
|
||||||
|
|
||||||
if let Some(svg) = cache.load(&handle) {
|
svg.viewport_dimensions()
|
||||||
svg.viewport_dimensions()
|
|
||||||
} else {
|
|
||||||
(1, 1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(
|
pub fn draw(
|
||||||
|
|
|
||||||
|
|
@ -4,15 +4,21 @@ use std::{
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Svg {
|
pub enum Svg {
|
||||||
tree: resvg::usvg::Tree,
|
Loaded { tree: resvg::usvg::Tree },
|
||||||
|
NotFound,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Svg {
|
impl Svg {
|
||||||
pub fn viewport_dimensions(&self) -> (u32, u32) {
|
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()) {
|
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();
|
let opt = resvg::Options::default();
|
||||||
|
|
||||||
match resvg::usvg::Tree::from_file(handle.path(), &opt.usvg) {
|
let svg = match resvg::usvg::Tree::from_file(handle.path(), &opt.usvg) {
|
||||||
Ok(tree) => {
|
Ok(tree) => Svg::Loaded { tree },
|
||||||
let _ = self.svgs.insert(handle.id(), Svg { tree });
|
Err(_) => Svg::NotFound,
|
||||||
}
|
|
||||||
Err(_) => {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
self.svgs.get(&handle.id())
|
let _ = self.svgs.insert(handle.id(), svg);
|
||||||
|
self.svgs.get(&handle.id()).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn upload(
|
pub fn upload(
|
||||||
|
|
@ -85,7 +90,7 @@ impl Cache {
|
||||||
}
|
}
|
||||||
|
|
||||||
match self.load(handle) {
|
match self.load(handle) {
|
||||||
Some(svg) => {
|
Svg::Loaded { tree } => {
|
||||||
let extent = wgpu::Extent3d {
|
let extent = wgpu::Extent3d {
|
||||||
width,
|
width,
|
||||||
height,
|
height,
|
||||||
|
|
@ -113,7 +118,7 @@ impl Cache {
|
||||||
);
|
);
|
||||||
|
|
||||||
resvg::backend_raqote::render_to_canvas(
|
resvg::backend_raqote::render_to_canvas(
|
||||||
&svg.tree,
|
&tree,
|
||||||
&resvg::Options::default(),
|
&resvg::Options::default(),
|
||||||
screen_size,
|
screen_size,
|
||||||
&mut canvas,
|
&mut canvas,
|
||||||
|
|
@ -171,7 +176,7 @@ impl Cache {
|
||||||
|
|
||||||
Some(bind_group)
|
Some(bind_group)
|
||||||
}
|
}
|
||||||
None => None,
|
Svg::NotFound => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue