2019-12-15 06:19:07 +01:00
|
|
|
use iced_native::svg;
|
|
|
|
|
use std::{
|
|
|
|
|
collections::{HashMap, HashSet},
|
2020-01-10 14:39:29 +01:00
|
|
|
fmt,
|
2019-12-15 06:19:07 +01:00
|
|
|
};
|
2020-01-10 14:39:29 +01:00
|
|
|
use guillotiere::{Allocation, AtlasAllocator, Size};
|
2020-01-10 16:07:09 +01:00
|
|
|
use debug_stub_derive::*;
|
2019-12-15 06:19:07 +01:00
|
|
|
|
2019-12-15 07:03:54 +01:00
|
|
|
pub enum Svg {
|
|
|
|
|
Loaded { tree: resvg::usvg::Tree },
|
|
|
|
|
NotFound,
|
2019-12-15 06:19:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Svg {
|
|
|
|
|
pub fn viewport_dimensions(&self) -> (u32, u32) {
|
2019-12-15 07:03:54 +01:00
|
|
|
match self {
|
|
|
|
|
Svg::Loaded { tree } => {
|
|
|
|
|
let size = tree.svg_node().size;
|
2019-12-15 06:19:07 +01:00
|
|
|
|
2019-12-15 07:03:54 +01:00
|
|
|
(size.width() as u32, size.height() as u32)
|
|
|
|
|
}
|
|
|
|
|
Svg::NotFound => (1, 1),
|
|
|
|
|
}
|
2019-12-15 06:19:07 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-10 14:39:29 +01:00
|
|
|
impl fmt::Debug for Svg {
|
2019-12-15 06:19:07 +01:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
write!(f, "Svg")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-10 16:07:09 +01:00
|
|
|
#[derive(DebugStub)]
|
2019-12-15 06:19:07 +01:00
|
|
|
pub struct Cache {
|
|
|
|
|
svgs: HashMap<u64, Svg>,
|
2020-01-10 16:07:09 +01:00
|
|
|
#[debug_stub="ReplacementValue"]
|
2020-01-10 14:39:29 +01:00
|
|
|
rasterized: HashMap<(u64, u32, u32), Allocation>,
|
2019-12-15 06:19:07 +01:00
|
|
|
svg_hits: HashSet<u64>,
|
|
|
|
|
rasterized_hits: HashSet<(u64, u32, u32)>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Cache {
|
2020-01-10 16:07:09 +01:00
|
|
|
pub fn new() -> Self {
|
2019-12-15 06:19:07 +01:00
|
|
|
Self {
|
|
|
|
|
svgs: HashMap::new(),
|
|
|
|
|
rasterized: HashMap::new(),
|
|
|
|
|
svg_hits: HashSet::new(),
|
|
|
|
|
rasterized_hits: HashSet::new(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-15 07:03:54 +01:00
|
|
|
pub fn load(&mut self, handle: &svg::Handle) -> &Svg {
|
2019-12-15 06:19:07 +01:00
|
|
|
if self.svgs.contains_key(&handle.id()) {
|
2019-12-15 07:03:54 +01:00
|
|
|
return self.svgs.get(&handle.id()).unwrap();
|
2019-12-15 06:19:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let opt = resvg::Options::default();
|
|
|
|
|
|
2019-12-15 07:03:54 +01:00
|
|
|
let svg = match resvg::usvg::Tree::from_file(handle.path(), &opt.usvg) {
|
|
|
|
|
Ok(tree) => Svg::Loaded { tree },
|
|
|
|
|
Err(_) => Svg::NotFound,
|
2019-12-15 06:19:07 +01:00
|
|
|
};
|
|
|
|
|
|
2019-12-15 07:03:54 +01:00
|
|
|
let _ = self.svgs.insert(handle.id(), svg);
|
|
|
|
|
self.svgs.get(&handle.id()).unwrap()
|
2019-12-15 06:19:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn upload(
|
|
|
|
|
&mut self,
|
|
|
|
|
handle: &svg::Handle,
|
|
|
|
|
[width, height]: [f32; 2],
|
|
|
|
|
scale: f32,
|
|
|
|
|
device: &wgpu::Device,
|
|
|
|
|
encoder: &mut wgpu::CommandEncoder,
|
2020-01-10 16:07:09 +01:00
|
|
|
allocator: &mut AtlasAllocator,
|
|
|
|
|
atlas: &mut wgpu::Texture,
|
2020-01-10 14:39:29 +01:00
|
|
|
) -> Option<&Allocation> {
|
2019-12-15 06:19:07 +01:00
|
|
|
let id = handle.id();
|
|
|
|
|
|
|
|
|
|
let (width, height) = (
|
|
|
|
|
(scale * width).round() as u32,
|
|
|
|
|
(scale * height).round() as u32,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// TODO: Optimize!
|
|
|
|
|
// We currently rerasterize the SVG when its size changes. This is slow
|
|
|
|
|
// as heck. A GPU rasterizer like `pathfinder` may perform better.
|
2019-12-15 06:28:55 +01:00
|
|
|
// It would be cool to be able to smooth resize the `svg` example.
|
2020-01-10 14:39:29 +01:00
|
|
|
if self.rasterized.get(&(id, width, height)).is_some() {
|
2019-12-15 06:19:07 +01:00
|
|
|
let _ = self.svg_hits.insert(id);
|
|
|
|
|
let _ = self.rasterized_hits.insert((id, width, height));
|
|
|
|
|
|
2020-01-10 14:39:29 +01:00
|
|
|
return self.rasterized.get(&(id, width, height));
|
2019-12-15 06:19:07 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-10 14:39:29 +01:00
|
|
|
let _ = self.load(handle);
|
|
|
|
|
|
|
|
|
|
match self.svgs.get(&handle.id()).unwrap() {
|
2019-12-15 07:03:54 +01:00
|
|
|
Svg::Loaded { tree } => {
|
2019-12-21 08:16:36 +01:00
|
|
|
if width == 0 || height == 0 {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-10 14:39:29 +01:00
|
|
|
let size = Size::new(width as i32, height as i32);
|
2020-01-10 16:07:09 +01:00
|
|
|
let old_atlas_size = allocator.size();
|
2020-01-10 14:39:29 +01:00
|
|
|
let allocation;
|
|
|
|
|
|
|
|
|
|
loop {
|
2020-01-10 16:07:09 +01:00
|
|
|
if let Some(a) = allocator.allocate(size) {
|
2020-01-10 14:39:29 +01:00
|
|
|
allocation = a;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-12-15 06:19:07 +01:00
|
|
|
|
2020-01-10 16:07:09 +01:00
|
|
|
allocator.grow(allocator.size() * 2);
|
2020-01-10 14:39:29 +01:00
|
|
|
}
|
2019-12-15 06:19:07 +01:00
|
|
|
|
2020-01-10 16:07:09 +01:00
|
|
|
let new_atlas_size = allocator.size();
|
2020-01-10 14:39:29 +01:00
|
|
|
|
|
|
|
|
if new_atlas_size != old_atlas_size {
|
|
|
|
|
let new_atlas = device.create_texture(&wgpu::TextureDescriptor {
|
|
|
|
|
size: wgpu::Extent3d {
|
|
|
|
|
width: new_atlas_size.width as u32,
|
|
|
|
|
height: new_atlas_size.height as u32,
|
|
|
|
|
depth: 1,
|
|
|
|
|
},
|
|
|
|
|
array_layer_count: 1,
|
|
|
|
|
mip_level_count: 1,
|
|
|
|
|
sample_count: 1,
|
|
|
|
|
dimension: wgpu::TextureDimension::D2,
|
|
|
|
|
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
|
|
|
|
usage: wgpu::TextureUsage::COPY_DST
|
|
|
|
|
| wgpu::TextureUsage::COPY_SRC
|
|
|
|
|
| wgpu::TextureUsage::SAMPLED,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
encoder.copy_texture_to_texture(
|
|
|
|
|
wgpu::TextureCopyView {
|
2020-01-10 16:07:09 +01:00
|
|
|
texture: atlas,
|
2020-01-10 14:39:29 +01:00
|
|
|
array_layer: 0,
|
|
|
|
|
mip_level: 0,
|
|
|
|
|
origin: wgpu::Origin3d {
|
|
|
|
|
x: 0.0,
|
|
|
|
|
y: 0.0,
|
|
|
|
|
z: 0.0,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
wgpu::TextureCopyView {
|
|
|
|
|
texture: &new_atlas,
|
|
|
|
|
array_layer: 0,
|
|
|
|
|
mip_level: 0,
|
|
|
|
|
origin: wgpu::Origin3d {
|
|
|
|
|
x: 0.0,
|
|
|
|
|
y: 0.0,
|
|
|
|
|
z: 0.0,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
wgpu::Extent3d {
|
|
|
|
|
width: old_atlas_size.width as u32,
|
|
|
|
|
height: old_atlas_size.height as u32,
|
|
|
|
|
depth: 1,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2020-01-10 16:07:09 +01:00
|
|
|
*atlas = new_atlas;
|
2020-01-10 14:39:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Optimize!
|
|
|
|
|
// We currently rerasterize the SVG when its size changes. This is slow
|
|
|
|
|
// as heck. A GPU rasterizer like `pathfinder` may perform better.
|
|
|
|
|
// It would be cool to be able to smooth resize the `svg` example.
|
2019-12-15 06:19:07 +01:00
|
|
|
let temp_buf = {
|
|
|
|
|
let screen_size =
|
|
|
|
|
resvg::ScreenSize::new(width, height).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut canvas = resvg::raqote::DrawTarget::new(
|
|
|
|
|
width as i32,
|
|
|
|
|
height as i32,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
resvg::backend_raqote::render_to_canvas(
|
2020-01-10 14:39:29 +01:00
|
|
|
tree,
|
2019-12-15 06:19:07 +01:00
|
|
|
&resvg::Options::default(),
|
|
|
|
|
screen_size,
|
|
|
|
|
&mut canvas,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let slice = canvas.get_data();
|
|
|
|
|
|
|
|
|
|
device
|
|
|
|
|
.create_buffer_mapped(
|
|
|
|
|
slice.len(),
|
|
|
|
|
wgpu::BufferUsage::COPY_SRC,
|
|
|
|
|
)
|
|
|
|
|
.fill_from_slice(slice)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
encoder.copy_buffer_to_texture(
|
|
|
|
|
wgpu::BufferCopyView {
|
|
|
|
|
buffer: &temp_buf,
|
|
|
|
|
offset: 0,
|
|
|
|
|
row_pitch: 4 * width as u32,
|
|
|
|
|
image_height: height as u32,
|
|
|
|
|
},
|
|
|
|
|
wgpu::TextureCopyView {
|
2020-01-10 16:07:09 +01:00
|
|
|
texture: atlas,
|
2019-12-15 06:19:07 +01:00
|
|
|
array_layer: 0,
|
|
|
|
|
mip_level: 0,
|
|
|
|
|
origin: wgpu::Origin3d {
|
2020-01-10 14:39:29 +01:00
|
|
|
x: allocation.rectangle.min.x as f32,
|
|
|
|
|
y: allocation.rectangle.min.y as f32,
|
2019-12-15 06:19:07 +01:00
|
|
|
z: 0.0,
|
|
|
|
|
},
|
|
|
|
|
},
|
2020-01-10 14:39:29 +01:00
|
|
|
wgpu::Extent3d {
|
|
|
|
|
width,
|
|
|
|
|
height,
|
|
|
|
|
depth: 1,
|
|
|
|
|
},
|
2019-12-15 06:19:07 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let _ = self.svg_hits.insert(id);
|
|
|
|
|
let _ = self.rasterized_hits.insert((id, width, height));
|
2020-01-10 14:39:29 +01:00
|
|
|
let _ = self
|
|
|
|
|
.rasterized
|
|
|
|
|
.insert((id, width, height), allocation);
|
2019-12-15 06:19:07 +01:00
|
|
|
|
2020-01-10 14:39:29 +01:00
|
|
|
self.rasterized.get(&(id, width, height))
|
2019-12-15 06:19:07 +01:00
|
|
|
}
|
2020-01-10 14:39:29 +01:00
|
|
|
Svg::NotFound => None
|
2019-12-15 06:19:07 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-10 16:07:09 +01:00
|
|
|
pub fn trim(&mut self, allocator: &mut AtlasAllocator) {
|
2019-12-15 06:19:07 +01:00
|
|
|
let svg_hits = &self.svg_hits;
|
|
|
|
|
let rasterized_hits = &self.rasterized_hits;
|
|
|
|
|
|
2020-01-10 14:39:29 +01:00
|
|
|
for (k, alloc) in &mut self.rasterized {
|
|
|
|
|
if !rasterized_hits.contains(&k) {
|
2020-01-10 16:07:09 +01:00
|
|
|
allocator.deallocate(alloc.id);
|
2020-01-10 14:39:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-15 06:19:07 +01:00
|
|
|
self.svgs.retain(|k, _| svg_hits.contains(k));
|
|
|
|
|
self.rasterized.retain(|k, _| rasterized_hits.contains(k));
|
|
|
|
|
self.svg_hits.clear();
|
|
|
|
|
self.rasterized_hits.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|