From cf17cb206551a39b0bf2ee61706c23f3d647816e Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 20 Sep 2021 22:52:29 +0200 Subject: [PATCH] egui_glium: allow sharing a native glium texture using Rc --- egui_glium/examples/native_texture.rs | 6 +++-- egui_glium/src/painter.rs | 35 ++++++++++++--------------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/egui_glium/examples/native_texture.rs b/egui_glium/examples/native_texture.rs index e85e0dbf..ae6ac1cd 100644 --- a/egui_glium/examples/native_texture.rs +++ b/egui_glium/examples/native_texture.rs @@ -47,9 +47,11 @@ fn main() { let image = load_glium_image(png_data); let image_size = egui::Vec2::new(image.width as f32, image.height as f32); // Load to gpu memory - let native_texture = glium::texture::SrgbTexture2d::new(&display, image).unwrap(); + let glium_texture = glium::texture::SrgbTexture2d::new(&display, image).unwrap(); + // Allow us to share the texture with egui: + let glium_texture = std::rc::Rc::new(glium_texture); // Allocate egui's texture id for GL texture - let texture_id = egui.painter_mut().register_native_texture(native_texture); + let texture_id = egui.painter_mut().register_native_texture(glium_texture); event_loop.run(move |event, _, control_flow| { let mut redraw = || { diff --git a/egui_glium/src/painter.rs b/egui_glium/src/painter.rs index fcd95d1b..58d3b6bd 100644 --- a/egui_glium/src/painter.rs +++ b/egui_glium/src/painter.rs @@ -13,6 +13,7 @@ use { uniform, uniforms::{MagnifySamplerFilter, SamplerWrapFunction}, }, + std::rc::Rc, }; pub struct Painter { @@ -30,8 +31,9 @@ struct UserTexture { /// This is the format glium likes. pixels: Vec>, - /// Lazily uploaded - gl_texture: Option, + /// Lazily uploaded from [`Self::pixels`], + /// or owned by the user via `register_native_texture`. + gl_texture: Option>, } impl Painter { @@ -242,20 +244,9 @@ impl Painter { } #[deprecated = "Use: `NativeTexture::register_native_texture` instead"] - pub fn register_glium_texture( - &mut self, - texture: glium::texture::SrgbTexture2d, - ) -> egui::TextureId { - let id = self.alloc_user_texture(); - if let egui::TextureId::User(id) = id { - if let Some(Some(user_texture)) = self.user_textures.get_mut(id as usize) { - *user_texture = UserTexture { - pixels: vec![], - gl_texture: Some(texture), - } - } - } - id + pub fn register_glium_texture(&mut self, texture: Rc) -> egui::TextureId { + use epi::NativeTexture as _; + self.register_native_texture(texture) } pub fn set_user_texture( @@ -302,7 +293,8 @@ impl Painter { .get(id as usize)? .as_ref()? .gl_texture - .as_ref(), + .as_ref() + .map(|rc| rc.as_ref()), } } @@ -312,15 +304,18 @@ impl Painter { let pixels = std::mem::take(&mut user_texture.pixels); let format = texture::SrgbFormat::U8U8U8U8; let mipmaps = texture::MipmapsOption::NoMipmap; - user_texture.gl_texture = - Some(SrgbTexture2d::with_format(facade, pixels, format, mipmaps).unwrap()); + user_texture.gl_texture = Some( + SrgbTexture2d::with_format(facade, pixels, format, mipmaps) + .unwrap() + .into(), + ); } } } } impl epi::NativeTexture for Painter { - type Texture = glium::texture::srgb_texture2d::SrgbTexture2d; + type Texture = Rc; fn register_native_texture(&mut self, native: Self::Texture) -> egui::TextureId { let id = self.alloc_user_texture();