From 81f37123ad23ec5c0812604f1bb8e93fcdf4f6a0 Mon Sep 17 00:00:00 2001 From: Greg V Date: Wed, 25 Nov 2020 22:25:15 +0300 Subject: Update resvg to `0.12` --- wgpu/src/image/vector.rs | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) (limited to 'wgpu/src/image/vector.rs') diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index 95df2e99..ab0f67d0 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -3,7 +3,7 @@ use iced_native::svg; use std::collections::{HashMap, HashSet}; pub enum Svg { - Loaded(resvg::usvg::Tree), + Loaded(usvg::Tree), NotFound, } @@ -43,17 +43,15 @@ impl Cache { return self.svgs.get(&handle.id()).unwrap(); } - let opt = resvg::Options::default(); - let svg = match handle.data() { svg::Data::Path(path) => { - match resvg::usvg::Tree::from_file(path, &opt.usvg) { + match usvg::Tree::from_file(path, &Default::default()) { Ok(tree) => Svg::Loaded(tree), Err(_) => Svg::NotFound, } } svg::Data::Bytes(bytes) => { - match resvg::usvg::Tree::from_data(&bytes, &opt.usvg) { + match usvg::Tree::from_data(&bytes, &Default::default()) { Ok(tree) => Svg::Loaded(tree), Err(_) => Svg::NotFound, } @@ -101,23 +99,38 @@ impl Cache { // 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. - let screen_size = - resvg::ScreenSize::new(width, height).unwrap(); + let img = resvg::render( + tree, + if width > height { + usvg::FitTo::Width(width) + } else { + usvg::FitTo::Height(height) + }, + None, + )?; + let width = img.width(); + let height = img.height(); - let mut canvas = - resvg::raqote::DrawTarget::new(width as i32, height as i32); + let mut rgba = img.take().into_iter(); - resvg::backend_raqote::render_to_canvas( - tree, - &resvg::Options::default(), - screen_size, - &mut canvas, - ); + // TODO: Perform conversion in the GPU + let bgra: Vec = std::iter::from_fn(move || { + use std::iter::once; + + let r = rgba.next()?; + let g = rgba.next()?; + let b = rgba.next()?; + let a = rgba.next()?; + + Some(once(b).chain(once(g)).chain(once(r)).chain(once(a))) + }) + .flatten() + .collect(); let allocation = texture_atlas.upload( width, height, - bytemuck::cast_slice(canvas.get_data()), + bytemuck::cast_slice(bgra.as_slice()), device, encoder, )?; -- cgit From 77a17cde83c9bdb8a61b871c7a15303f13e8d781 Mon Sep 17 00:00:00 2001 From: Zak Date: Tue, 11 May 2021 22:41:55 +0100 Subject: This commit optimizes the function used to converg rgba pixels into bgra pixels. --- wgpu/src/image/vector.rs | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) (limited to 'wgpu/src/image/vector.rs') diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index ab0f67d0..8c7de617 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -1,7 +1,8 @@ -use crate::image::atlas::{self, Atlas}; use iced_native::svg; use std::collections::{HashMap, HashSet}; +use crate::image::atlas::{self, Atlas}; + pub enum Svg { Loaded(usvg::Tree), NotFound, @@ -111,26 +112,13 @@ impl Cache { let width = img.width(); let height = img.height(); - let mut rgba = img.take().into_iter(); - - // TODO: Perform conversion in the GPU - let bgra: Vec = std::iter::from_fn(move || { - use std::iter::once; - - let r = rgba.next()?; - let g = rgba.next()?; - let b = rgba.next()?; - let a = rgba.next()?; - - Some(once(b).chain(once(g)).chain(once(r)).chain(once(a))) - }) - .flatten() - .collect(); + let mut rgba = img.take(); + rgba.chunks_exact_mut(4).for_each(|rgba| rgba.swap(0, 2)); let allocation = texture_atlas.upload( width, height, - bytemuck::cast_slice(bgra.as_slice()), + bytemuck::cast_slice(rgba.as_slice()), device, encoder, )?; -- cgit From 665422e256b5eb8fed23f1a13900838e0e3bcb44 Mon Sep 17 00:00:00 2001 From: aentity Date: Tue, 13 Jul 2021 22:27:48 -0400 Subject: Use ceil on svg dimensions, fix svg memory usage Calls ceil() on dimension bounds as this appears fix svg memory unbounded usage because no longer cache miss. The height and width return from resvg seem to always be ceiling of float dimensions, so we try to match. --- wgpu/src/image/vector.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'wgpu/src/image/vector.rs') diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index 8c7de617..cd511a45 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -75,8 +75,8 @@ impl Cache { let id = handle.id(); let (width, height) = ( - (scale * width).round() as u32, - (scale * height).round() as u32, + (scale * width).ceil() as u32, + (scale * height).ceil() as u32, ); // TODO: Optimize! @@ -122,6 +122,7 @@ impl Cache { device, encoder, )?; + log::debug!("allocating {} {}x{}", id, width, height); let _ = self.svg_hits.insert(id); let _ = self.rasterized_hits.insert((id, width, height)); -- cgit