summaryrefslogtreecommitdiffstats
path: root/glow/src/image/textures.rs
diff options
context:
space:
mode:
authorLibravatar Ian Douglas Scott <idscott@system76.com>2022-10-24 17:06:02 -0700
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2022-11-05 03:19:38 +0100
commit5575e6ea0897e406674e7e4239808fbf9daa07c3 (patch)
tree888f1b22926a304e7e280710312727fc4137e373 /glow/src/image/textures.rs
parent2c7c42ee93a61f39562590f6a75eb2dd8b220fb8 (diff)
downloadiced-5575e6ea0897e406674e7e4239808fbf9daa07c3.tar.gz
iced-5575e6ea0897e406674e7e4239808fbf9daa07c3.tar.bz2
iced-5575e6ea0897e406674e7e4239808fbf9daa07c3.zip
Add image/svg support to `iced_glow`
https://github.com/iced-rs/iced/issues/674 Uses image/svg support in `iced_graphics`. The is not currently using an atlas, and uses one texture/draw per image. This should be good enough for now; supporting images with glow is better than not supporting them, and if something else performs better, that improvement can be made without any change to the public API.
Diffstat (limited to 'glow/src/image/textures.rs')
-rw-r--r--glow/src/image/textures.rs82
1 files changed, 82 insertions, 0 deletions
diff --git a/glow/src/image/textures.rs b/glow/src/image/textures.rs
new file mode 100644
index 00000000..f43cae1c
--- /dev/null
+++ b/glow/src/image/textures.rs
@@ -0,0 +1,82 @@
+use glow::HasContext;
+use iced_graphics::image::{TextureStore, TextureStoreEntry};
+
+#[derive(Debug)]
+pub struct Textures;
+
+impl Textures {
+ pub fn new() -> Self {
+ Self
+ }
+}
+
+impl TextureStore for Textures {
+ type Entry = Entry;
+ type State<'a> = &'a glow::Context;
+
+ fn upload(
+ &mut self,
+ width: u32,
+ height: u32,
+ data: &[u8],
+ gl: &mut &glow::Context,
+ ) -> Option<Self::Entry> {
+ unsafe {
+ let texture = gl.create_texture().expect("create texture");
+ gl.bind_texture(glow::TEXTURE_2D, Some(texture));
+ gl.tex_image_2d(
+ glow::TEXTURE_2D,
+ 0,
+ glow::SRGB8_ALPHA8 as i32,
+ width as i32,
+ height as i32,
+ 0,
+ glow::BGRA,
+ glow::UNSIGNED_BYTE,
+ Some(data),
+ );
+ gl.tex_parameter_i32(
+ glow::TEXTURE_2D,
+ glow::TEXTURE_WRAP_S,
+ glow::CLAMP_TO_EDGE as _,
+ );
+ gl.tex_parameter_i32(
+ glow::TEXTURE_2D,
+ glow::TEXTURE_WRAP_T,
+ glow::CLAMP_TO_EDGE as _,
+ );
+ gl.tex_parameter_i32(
+ glow::TEXTURE_2D,
+ glow::TEXTURE_MIN_FILTER,
+ glow::LINEAR as _,
+ );
+ gl.tex_parameter_i32(
+ glow::TEXTURE_2D,
+ glow::TEXTURE_MAG_FILTER,
+ glow::LINEAR as _,
+ );
+ gl.bind_texture(glow::TEXTURE_2D, None);
+
+ Some(Entry {
+ size: (width, height),
+ texture,
+ })
+ }
+ }
+
+ fn remove(&mut self, entry: &Entry, gl: &mut &glow::Context) {
+ unsafe { gl.delete_texture(entry.texture) }
+ }
+}
+
+#[derive(Debug)]
+pub struct Entry {
+ size: (u32, u32),
+ pub texture: glow::NativeTexture,
+}
+
+impl TextureStoreEntry for Entry {
+ fn size(&self) -> (u32, u32) {
+ self.size
+ }
+}