diff options
Diffstat (limited to 'wgpu/src/image')
-rw-r--r-- | wgpu/src/image/atlas.rs | 24 | ||||
-rw-r--r-- | wgpu/src/image/atlas/entry.rs | 2 | ||||
-rw-r--r-- | wgpu/src/image/raster.rs | 8 | ||||
-rw-r--r-- | wgpu/src/image/vector.rs | 42 |
4 files changed, 40 insertions, 36 deletions
diff --git a/wgpu/src/image/atlas.rs b/wgpu/src/image/atlas.rs index 660ebe44..4855fa4a 100644 --- a/wgpu/src/image/atlas.rs +++ b/wgpu/src/image/atlas.rs @@ -4,6 +4,8 @@ mod allocation; mod allocator; mod layer; +use std::num::NonZeroU32; + pub use allocation::Allocation; pub use entry::Entry; pub use layer::Layer; @@ -24,7 +26,7 @@ impl Atlas { let extent = wgpu::Extent3d { width: SIZE, height: SIZE, - depth: 1, + depth_or_array_layers: 1, }; let texture = device.create_texture(&wgpu::TextureDescriptor { @@ -294,19 +296,19 @@ impl Atlas { let extent = wgpu::Extent3d { width, height, - depth: 1, + depth_or_array_layers: 1, }; encoder.copy_buffer_to_texture( - wgpu::BufferCopyView { + wgpu::ImageCopyBuffer { buffer, - layout: wgpu::TextureDataLayout { + layout: wgpu::ImageDataLayout { offset: offset as u64, - bytes_per_row: 4 * image_width + padding, - rows_per_image: image_height, + bytes_per_row: NonZeroU32::new(4 * image_width + padding), + rows_per_image: NonZeroU32::new(image_height), }, }, - wgpu::TextureCopyView { + wgpu::ImageCopyTexture { texture: &self.texture, mip_level: 0, origin: wgpu::Origin3d { @@ -334,7 +336,7 @@ impl Atlas { size: wgpu::Extent3d { width: SIZE, height: SIZE, - depth: self.layers.len() as u32, + depth_or_array_layers: self.layers.len() as u32, }, mip_level_count: 1, sample_count: 1, @@ -355,7 +357,7 @@ impl Atlas { } encoder.copy_texture_to_texture( - wgpu::TextureCopyView { + wgpu::ImageCopyTexture { texture: &self.texture, mip_level: 0, origin: wgpu::Origin3d { @@ -364,7 +366,7 @@ impl Atlas { z: i as u32, }, }, - wgpu::TextureCopyView { + wgpu::ImageCopyTexture { texture: &new_texture, mip_level: 0, origin: wgpu::Origin3d { @@ -376,7 +378,7 @@ impl Atlas { wgpu::Extent3d { width: SIZE, height: SIZE, - depth: 1, + depth_or_array_layers: 1, }, ); } diff --git a/wgpu/src/image/atlas/entry.rs b/wgpu/src/image/atlas/entry.rs index 0310fc54..9b3f16df 100644 --- a/wgpu/src/image/atlas/entry.rs +++ b/wgpu/src/image/atlas/entry.rs @@ -10,7 +10,7 @@ pub enum Entry { } impl Entry { - #[cfg(feature = "image")] + #[cfg(feature = "image_rs")] pub fn size(&self) -> (u32, u32) { match self { Entry::Contiguous(allocation) => allocation.size(), diff --git a/wgpu/src/image/raster.rs b/wgpu/src/image/raster.rs index 25607dab..d5c62545 100644 --- a/wgpu/src/image/raster.rs +++ b/wgpu/src/image/raster.rs @@ -4,7 +4,7 @@ use std::collections::{HashMap, HashSet}; #[derive(Debug)] pub enum Memory { - Host(::image::ImageBuffer<::image::Bgra<u8>, Vec<u8>>), + Host(::image_rs::ImageBuffer<::image_rs::Bgra<u8>, Vec<u8>>), Device(atlas::Entry), NotFound, Invalid, @@ -42,14 +42,14 @@ impl Cache { let memory = match handle.data() { image::Data::Path(path) => { - if let Ok(image) = ::image::open(path) { + if let Ok(image) = ::image_rs::open(path) { Memory::Host(image.to_bgra8()) } else { Memory::NotFound } } image::Data::Bytes(bytes) => { - if let Ok(image) = ::image::load_from_memory(&bytes) { + if let Ok(image) = ::image_rs::load_from_memory(&bytes) { Memory::Host(image.to_bgra8()) } else { Memory::Invalid @@ -60,7 +60,7 @@ impl Cache { height, pixels, } => { - if let Some(image) = ::image::ImageBuffer::from_vec( + if let Some(image) = ::image_rs::ImageBuffer::from_vec( *width, *height, pixels.to_vec(), diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index 95df2e99..cd511a45 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -1,9 +1,10 @@ -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(resvg::usvg::Tree), + Loaded(usvg::Tree), NotFound, } @@ -43,17 +44,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, } @@ -76,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! @@ -101,26 +100,29 @@ 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 mut canvas = - resvg::raqote::DrawTarget::new(width as i32, height as i32); - - resvg::backend_raqote::render_to_canvas( + let img = resvg::render( tree, - &resvg::Options::default(), - screen_size, - &mut canvas, - ); + if width > height { + usvg::FitTo::Width(width) + } else { + usvg::FitTo::Height(height) + }, + None, + )?; + let width = img.width(); + let height = img.height(); + + 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(canvas.get_data()), + bytemuck::cast_slice(rgba.as_slice()), device, encoder, )?; + log::debug!("allocating {} {}x{}", id, width, height); let _ = self.svg_hits.insert(id); let _ = self.rasterized_hits.insert((id, width, height)); |