summaryrefslogtreecommitdiffstats
path: root/glow
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2022-11-05 03:13:04 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2022-11-05 03:19:38 +0100
commit8ce8d374b1e8d1d394a42a5ee2bca8af790f0b71 (patch)
tree40cf3587e2a08f845c9a8d89108b1b3d8cd86213 /glow
parent5575e6ea0897e406674e7e4239808fbf9daa07c3 (diff)
downloadiced-8ce8d374b1e8d1d394a42a5ee2bca8af790f0b71.tar.gz
iced-8ce8d374b1e8d1d394a42a5ee2bca8af790f0b71.tar.bz2
iced-8ce8d374b1e8d1d394a42a5ee2bca8af790f0b71.zip
Refactor some `image` traits a bit
- Use `Size<u32>` were applicable. - Rename `TextureStore` to `image::Storage`. - Rename `TextureStoreEntry` to `image::storage::Entry`. - Wire up `viewport_dimensions` to `iced_glow` for `Svg`.
Diffstat (limited to 'glow')
-rw-r--r--glow/src/backend.rs8
-rw-r--r--glow/src/image.rs64
-rw-r--r--glow/src/image/storage.rs (renamed from glow/src/image/textures.rs)26
3 files changed, 56 insertions, 42 deletions
diff --git a/glow/src/backend.rs b/glow/src/backend.rs
index 1ba70a49..35a82c0f 100644
--- a/glow/src/backend.rs
+++ b/glow/src/backend.rs
@@ -258,7 +258,7 @@ impl backend::Text for Backend {
#[cfg(feature = "image_rs")]
impl backend::Image for Backend {
- fn dimensions(&self, handle: &iced_native::image::Handle) -> (u32, u32) {
+ fn dimensions(&self, handle: &iced_native::image::Handle) -> Size<u32> {
self.image_pipeline.dimensions(handle)
}
}
@@ -267,8 +267,8 @@ impl backend::Image for Backend {
impl backend::Svg for Backend {
fn viewport_dimensions(
&self,
- _handle: &iced_native::svg::Handle,
- ) -> (u32, u32) {
- (50, 50)
+ handle: &iced_native::svg::Handle,
+ ) -> Size<u32> {
+ self.image_pipeline.viewport_dimensions(handle)
}
}
diff --git a/glow/src/image.rs b/glow/src/image.rs
index 51e3016e..66620537 100644
--- a/glow/src/image.rs
+++ b/glow/src/image.rs
@@ -1,20 +1,24 @@
-use crate::program::{self, Shader};
-use crate::Transformation;
-use glow::HasContext;
-use iced_graphics::layer;
-#[cfg(feature = "image_rs")]
-use std::cell::RefCell;
+mod storage;
+
+use storage::Storage;
pub use iced_graphics::triangle::{Mesh2D, Vertex2D};
+use crate::program::{self, Shader};
+use crate::Transformation;
+
#[cfg(feature = "image_rs")]
use iced_graphics::image::raster;
#[cfg(feature = "svg")]
use iced_graphics::image::vector;
-mod textures;
-use textures::{Entry, Textures};
+use iced_graphics::layer;
+use iced_graphics::Size;
+
+use glow::HasContext;
+
+use std::cell::RefCell;
#[derive(Debug)]
pub(crate) struct Pipeline {
@@ -22,11 +26,11 @@ pub(crate) struct Pipeline {
vertex_array: <glow::Context as HasContext>::VertexArray,
vertex_buffer: <glow::Context as HasContext>::Buffer,
transform_location: <glow::Context as HasContext>::UniformLocation,
- textures: Textures,
+ storage: Storage,
#[cfg(feature = "image_rs")]
- raster_cache: RefCell<raster::Cache<Textures>>,
+ raster_cache: RefCell<raster::Cache<Storage>>,
#[cfg(feature = "svg")]
- vector_cache: vector::Cache<Textures>,
+ vector_cache: RefCell<vector::Cache<Storage>>,
}
impl Pipeline {
@@ -110,22 +114,30 @@ impl Pipeline {
vertex_array,
vertex_buffer,
transform_location,
- textures: Textures::new(),
+ storage: Storage::default(),
#[cfg(feature = "image_rs")]
raster_cache: RefCell::new(raster::Cache::default()),
#[cfg(feature = "svg")]
- vector_cache: vector::Cache::default(),
+ vector_cache: RefCell::new(vector::Cache::default()),
}
}
#[cfg(feature = "image_rs")]
- pub fn dimensions(
- &self,
- handle: &iced_native::image::Handle,
- ) -> (u32, u32) {
+ pub fn dimensions(&self, handle: &iced_native::image::Handle) -> Size<u32> {
self.raster_cache.borrow_mut().load(handle).dimensions()
}
+ #[cfg(feature = "svg")]
+ pub fn viewport_dimensions(
+ &self,
+ handle: &iced_native::svg::Handle,
+ ) -> Size<u32> {
+ let mut cache = self.vector_cache.borrow_mut();
+ let svg = cache.load(handle);
+
+ svg.viewport_dimensions()
+ }
+
pub fn draw(
&mut self,
mut gl: &glow::Context,
@@ -141,11 +153,15 @@ impl Pipeline {
#[cfg(feature = "image_rs")]
let mut raster_cache = self.raster_cache.borrow_mut();
+
+ #[cfg(feature = "svg")]
+ let mut vector_cache = self.vector_cache.borrow_mut();
+
for image in images {
let (entry, bounds) = match &image {
#[cfg(feature = "image_rs")]
layer::Image::Raster { handle, bounds } => (
- raster_cache.upload(handle, &mut gl, &mut self.textures),
+ raster_cache.upload(handle, &mut gl, &mut self.storage),
bounds,
),
#[cfg(not(feature = "image_rs"))]
@@ -155,12 +171,12 @@ impl Pipeline {
layer::Image::Vector { handle, bounds } => {
let size = [bounds.width, bounds.height];
(
- self.vector_cache.upload(
+ vector_cache.upload(
handle,
size,
_scale_factor,
&mut gl,
- &mut self.textures,
+ &mut self.storage,
),
bounds,
)
@@ -171,7 +187,7 @@ impl Pipeline {
};
unsafe {
- if let Some(Entry { texture, .. }) = entry {
+ if let Some(storage::Entry { texture, .. }) = entry {
gl.bind_texture(glow::TEXTURE_2D, Some(*texture))
} else {
continue;
@@ -204,9 +220,11 @@ impl Pipeline {
#[cfg(feature = "image_rs")]
self.raster_cache
.borrow_mut()
- .trim(&mut self.textures, &mut gl);
+ .trim(&mut self.storage, &mut gl);
#[cfg(feature = "svg")]
- self.vector_cache.trim(&mut self.textures, &mut gl);
+ self.vector_cache
+ .borrow_mut()
+ .trim(&mut self.storage, &mut gl);
}
}
diff --git a/glow/src/image/textures.rs b/glow/src/image/storage.rs
index f43cae1c..e2171fb5 100644
--- a/glow/src/image/textures.rs
+++ b/glow/src/image/storage.rs
@@ -1,16 +1,12 @@
-use glow::HasContext;
-use iced_graphics::image::{TextureStore, TextureStoreEntry};
+use iced_graphics::image;
+use iced_graphics::Size;
-#[derive(Debug)]
-pub struct Textures;
+use glow::HasContext;
-impl Textures {
- pub fn new() -> Self {
- Self
- }
-}
+#[derive(Debug, Default)]
+pub struct Storage;
-impl TextureStore for Textures {
+impl image::Storage for Storage {
type Entry = Entry;
type State<'a> = &'a glow::Context;
@@ -58,7 +54,7 @@ impl TextureStore for Textures {
gl.bind_texture(glow::TEXTURE_2D, None);
Some(Entry {
- size: (width, height),
+ size: Size::new(width, height),
texture,
})
}
@@ -71,12 +67,12 @@ impl TextureStore for Textures {
#[derive(Debug)]
pub struct Entry {
- size: (u32, u32),
- pub texture: glow::NativeTexture,
+ size: Size<u32>,
+ pub(super) texture: glow::NativeTexture,
}
-impl TextureStoreEntry for Entry {
- fn size(&self) -> (u32, u32) {
+impl image::storage::Entry for Entry {
+ fn size(&self) -> Size<u32> {
self.size
}
}