From 09f1ecbf82461f100e896c89ccecea8bb207ffc8 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Wed, 16 Nov 2022 07:40:56 -0700 Subject: [PATCH] erase generic T from Atlas, Sprite, CachedGlyph etc. This will make it easier to use the same impl for webgpu and opengl for a bunch of this stuff. --- Cargo.lock | 1 + wezterm-gui/src/customglyph.rs | 7 ++- wezterm-gui/src/glyphcache.rs | 57 +++++++++++++------------ wezterm-gui/src/main.rs | 37 ++++++++-------- wezterm-gui/src/renderstate.rs | 5 +-- wezterm-gui/src/shapecache.rs | 30 ++++--------- wezterm-gui/src/termwindow/box_model.rs | 7 ++- wezterm-gui/src/termwindow/mod.rs | 4 +- wezterm-gui/src/termwindow/render.rs | 17 ++++---- wezterm-gui/src/utilsprites.rs | 12 +++--- window/Cargo.toml | 1 + window/src/bitmaps/atlas.rs | 39 ++++++----------- window/src/bitmaps/mod.rs | 4 +- 13 files changed, 98 insertions(+), 123 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ae3d618d6..9090b4932 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6200,6 +6200,7 @@ dependencies = [ "core-foundation 0.7.0", "core-graphics 0.19.2", "dirs-next", + "downcast-rs", "euclid", "filedescriptor", "futures-util", diff --git a/wezterm-gui/src/customglyph.rs b/wezterm-gui/src/customglyph.rs index 7ce8de105..edcf8f8e1 100644 --- a/wezterm-gui/src/customglyph.rs +++ b/wezterm-gui/src/customglyph.rs @@ -7,7 +7,6 @@ use std::ops::Range; use termwiz::surface::CursorShape; use tiny_skia::{FillRule, Paint, Path, PathBuilder, PixmapMut, Stroke, Transform}; use wezterm_font::units::{IntPixelLength, PixelLength}; -use window::bitmaps::Texture2d; use window::{BitmapImage, Image, Point, Rect, Size}; #[derive(Copy, Clone, Eq, PartialEq)] @@ -3660,7 +3659,7 @@ impl BlockKey { } } -impl GlyphCache { +impl GlyphCache { fn draw_polys( &mut self, metrics: &RenderMetrics, @@ -3703,7 +3702,7 @@ impl GlyphCache { shape: Option, metrics: &RenderMetrics, width: u8, - ) -> anyhow::Result> { + ) -> anyhow::Result { if let Some(sprite) = self.cursor_glyphs.get(&(shape, width)) { return Ok(sprite.clone()); } @@ -3789,7 +3788,7 @@ impl GlyphCache { &mut self, render_metrics: &RenderMetrics, key: SizedBlockKey, - ) -> anyhow::Result> { + ) -> anyhow::Result { let metrics = match &key.block { BlockKey::PolyWithCustomMetrics { underline_height, diff --git a/wezterm-gui/src/glyphcache.rs b/wezterm-gui/src/glyphcache.rs index e52353036..198d09591 100644 --- a/wezterm-gui/src/glyphcache.rs +++ b/wezterm-gui/src/glyphcache.rs @@ -134,7 +134,7 @@ impl<'a> std::hash::Hash for (dyn GlyphKeyTrait + 'a) { /// Caches a rendered glyph. /// The image data may be None for whitespace glyphs. -pub struct CachedGlyph { +pub struct CachedGlyph { pub has_color: bool, pub brightness_adjust: f32, pub x_offset: PixelLength, @@ -142,11 +142,11 @@ pub struct CachedGlyph { pub x_advance: PixelLength, pub bearing_x: PixelLength, pub bearing_y: PixelLength, - pub texture: Option>, + pub texture: Option, pub scale: f64, } -impl std::fmt::Debug for CachedGlyph { +impl std::fmt::Debug for CachedGlyph { fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> { fmt.debug_struct("CachedGlyph") .field("has_color", &self.has_color) @@ -247,21 +247,21 @@ impl DecodedImage { /// A number of items here are HashMaps rather than LfuCaches; /// eviction is managed by recreating Self when the Atlas is filled -pub struct GlyphCache { - glyph_cache: HashMap>>, - pub atlas: Atlas, +pub struct GlyphCache { + glyph_cache: HashMap>, + pub atlas: Atlas, pub fonts: Rc, pub image_cache: LfuCacheU64, - frame_cache: HashMap<[u8; 32], Sprite>, - line_glyphs: HashMap>, - pub block_glyphs: HashMap>, - pub cursor_glyphs: HashMap<(Option, u8), Sprite>, - pub color: HashMap<(RgbColor, NotNan), Sprite>, + frame_cache: HashMap<[u8; 32], Sprite>, + line_glyphs: HashMap, + pub block_glyphs: HashMap, + pub cursor_glyphs: HashMap<(Option, u8), Sprite>, + pub color: HashMap<(RgbColor, NotNan), Sprite>, } -impl GlyphCache { +impl GlyphCache { pub fn new_in_memory(fonts: &Rc, size: usize) -> anyhow::Result { - let surface = Rc::new(ImageTexture::new(size, size)); + let surface: Rc = Rc::new(ImageTexture::new(size, size)); let atlas = Atlas::new(&surface).expect("failed to create new texture atlas"); Ok(Self { @@ -283,13 +283,14 @@ impl GlyphCache { } } -impl GlyphCache { +impl GlyphCache { pub fn new_webgpu( state: &WebGpuState, fonts: &Rc, size: usize, ) -> anyhow::Result { - let texture = Rc::new(WebGpuTexture::new(size as u32, size as u32, state)); + let texture: Rc = + Rc::new(WebGpuTexture::new(size as u32, size as u32, state)); let atlas = Atlas::new(&texture).expect("failed to create new texture atlas"); Ok(Self { @@ -311,7 +312,7 @@ impl GlyphCache { } } -impl GlyphCache { +impl GlyphCache { pub fn new_gl( backend: &Rc, fonts: &Rc, @@ -334,7 +335,7 @@ impl GlyphCache { caps.max_texture_size ); } - let surface = Rc::new(SrgbTexture2d::empty_with_format( + let surface: Rc = Rc::new(SrgbTexture2d::empty_with_format( backend, glium::texture::SrgbFormat::U8U8U8U8, glium::texture::MipmapsOption::NoMipmap, @@ -362,7 +363,7 @@ impl GlyphCache { } } -impl GlyphCache { +impl GlyphCache { /// Resolve a glyph from the cache, rendering the glyph on-demand if /// the cache doesn't already hold the desired glyph. pub fn cached_glyph( @@ -373,7 +374,7 @@ impl GlyphCache { font: &Rc, metrics: &RenderMetrics, num_cells: u8, - ) -> anyhow::Result>> { + ) -> anyhow::Result> { let key = BorrowedGlyphKey { font_idx: info.font_idx, glyph_pos: info.glyph_pos, @@ -443,7 +444,7 @@ impl GlyphCache { font: &Rc, followed_by_space: bool, num_cells: u8, - ) -> anyhow::Result>> { + ) -> anyhow::Result> { let base_metrics; let idx_metrics; let brightness_adjust; @@ -610,11 +611,11 @@ impl GlyphCache { } fn cached_image_impl( - frame_cache: &mut HashMap<[u8; 32], Sprite>, - atlas: &mut Atlas, + frame_cache: &mut HashMap<[u8; 32], Sprite>, + atlas: &mut Atlas, decoded: &DecodedImage, padding: Option, - ) -> anyhow::Result<(Sprite, Option)> { + ) -> anyhow::Result<(Sprite, Option)> { let mut handle = DecodedImageHandle { h: decoded.image.data(), current_frame: *decoded.current_frame.borrow(), @@ -683,7 +684,7 @@ impl GlyphCache { &mut self, image_data: &Arc, padding: Option, - ) -> anyhow::Result<(Sprite, Option)> { + ) -> anyhow::Result<(Sprite, Option)> { let id = image_data.id() as u64; if let Some(decoded) = self.image_cache.get(&id) { @@ -697,7 +698,7 @@ impl GlyphCache { } } - pub fn cached_color(&mut self, color: RgbColor, alpha: f32) -> anyhow::Result> { + pub fn cached_color(&mut self, color: RgbColor, alpha: f32) -> anyhow::Result { let key = (color, NotNan::new(alpha).unwrap()); if let Some(s) = self.color.get(&key) { @@ -722,7 +723,7 @@ impl GlyphCache { &mut self, block: BlockKey, metrics: &RenderMetrics, - ) -> anyhow::Result> { + ) -> anyhow::Result { let key = SizedBlockKey { block, size: metrics.into(), @@ -733,7 +734,7 @@ impl GlyphCache { self.block_sprite(metrics, key) } - fn line_sprite(&mut self, key: LineKey, metrics: &RenderMetrics) -> anyhow::Result> { + fn line_sprite(&mut self, key: LineKey, metrics: &RenderMetrics) -> anyhow::Result { let mut buffer = Image::new( metrics.cell_size.width as usize, metrics.cell_size.height as usize, @@ -936,7 +937,7 @@ impl GlyphCache { underline: Underline, overline: bool, metrics: &RenderMetrics, - ) -> anyhow::Result> { + ) -> anyhow::Result { let effective_underline = match (is_highlited_hyperlink, underline) { (true, Underline::None) => Underline::Single, (true, Underline::Single) => Underline::Double, diff --git a/wezterm-gui/src/main.rs b/wezterm-gui/src/main.rs index eebb8cf5c..c57e15b83 100644 --- a/wezterm-gui/src/main.rs +++ b/wezterm-gui/src/main.rs @@ -954,26 +954,29 @@ pub fn run_ls_fonts(config: config::ConfigHandle, cmd: &LsFontsCommand) -> anyho let mut glyph = String::new(); if let Some(texture) = &cached_glyph.texture { - for y in texture.coords.min_y()..texture.coords.max_y() { - for &px in texture.texture.image.borrow().horizontal_pixel_range( - texture.coords.min_x() as usize, - texture.coords.max_x() as usize, - y as usize, - ) { - let px = u32::from_be(px); - let (b, g, r, a) = ( - (px >> 8) as u8, - (px >> 16) as u8, - (px >> 24) as u8, - (px & 0xff) as u8, - ); - // Use regular RGB for other terminals, but then - // set RGBA for wezterm - glyph.push_str(&format!( + use window::bitmaps::ImageTexture; + if let Some(tex) = texture.texture.downcast_ref::() { + for y in texture.coords.min_y()..texture.coords.max_y() { + for &px in tex.image.borrow().horizontal_pixel_range( + texture.coords.min_x() as usize, + texture.coords.max_x() as usize, + y as usize, + ) { + let px = u32::from_be(px); + let (b, g, r, a) = ( + (px >> 8) as u8, + (px >> 16) as u8, + (px >> 24) as u8, + (px & 0xff) as u8, + ); + // Use regular RGB for other terminals, but then + // set RGBA for wezterm + glyph.push_str(&format!( "\x1b[38:2::{r}:{g}:{b}m\x1b[38:6::{r}:{g}:{b}:{a}m\u{2588}\x1b[0m" )); + } + glyph.push('\n'); } - glyph.push('\n'); } } diff --git a/wezterm-gui/src/renderstate.rs b/wezterm-gui/src/renderstate.rs index 341e36f27..f0e687012 100644 --- a/wezterm-gui/src/renderstate.rs +++ b/wezterm-gui/src/renderstate.rs @@ -4,7 +4,6 @@ use super::utilsprites::{RenderMetrics, UtilSprites}; use ::window::bitmaps::atlas::OutOfTextureSpace; use ::window::glium::backend::Context as GliumContext; use ::window::glium::buffer::Mapping; -use ::window::glium::texture::SrgbTexture2d; use ::window::glium::{CapabilitiesSource, IndexBuffer, VertexBuffer}; use ::window::*; use anyhow::Context; @@ -219,8 +218,8 @@ impl<'a> TripleLayerQuadAllocatorTrait for BorrowedLayers<'a> { pub struct RenderState { pub context: Rc, - pub glyph_cache: RefCell>, - pub util_sprites: UtilSprites, + pub glyph_cache: RefCell, + pub util_sprites: UtilSprites, pub glyph_prog: glium::Program, pub layers: RefCell>>, } diff --git a/wezterm-gui/src/shapecache.rs b/wezterm-gui/src/shapecache.rs index 80d443a37..8e1c84f3d 100644 --- a/wezterm-gui/src/shapecache.rs +++ b/wezterm-gui/src/shapecache.rs @@ -1,6 +1,5 @@ use crate::customglyph::BlockKey; use crate::glyphcache::CachedGlyph; -use ::window::bitmaps::Texture2d; use config::TextStyle; use std::rc::Rc; use wezterm_font::shaper::GlyphInfo; @@ -22,25 +21,17 @@ pub struct GlyphPosition { } #[derive(Debug)] -pub struct ShapedInfo -where - T: Texture2d, - T: std::fmt::Debug, -{ - pub glyph: Rc>, +pub struct ShapedInfo { + pub glyph: Rc, pub pos: GlyphPosition, pub block_key: Option, } -impl ShapedInfo -where - T: Texture2d, - T: std::fmt::Debug, -{ +impl ShapedInfo { /// Process the results from the shaper, stitching together glyph /// and positioning information - pub fn process(infos: &[GlyphInfo], glyphs: &[Rc>]) -> Vec> { - let mut pos: Vec> = Vec::with_capacity(infos.len()); + pub fn process(infos: &[GlyphInfo], glyphs: &[Rc]) -> Vec { + let mut pos: Vec = Vec::with_capacity(infos.len()); for (info, glyph) in infos.iter().zip(glyphs.iter()) { pos.push(ShapedInfo { @@ -123,7 +114,6 @@ impl<'a> std::hash::Hash for (dyn ShapeCacheKeyTrait + 'a) { #[cfg(test)] mod test { - use super::*; use crate::glyphcache::GlyphCache; use crate::shapecache::{GlyphPosition, ShapedInfo}; use crate::utilsprites::RenderMetrics; @@ -135,17 +125,13 @@ mod test { use wezterm_font::shaper::PresentationWidth; use wezterm_font::{FontConfiguration, LoadedFont}; - fn cluster_and_shape( + fn cluster_and_shape( render_metrics: &RenderMetrics, - glyph_cache: &mut GlyphCache, + glyph_cache: &mut GlyphCache, style: &TextStyle, font: &Rc, text: &str, - ) -> Vec - where - T: Texture2d, - T: std::fmt::Debug, - { + ) -> Vec { let line = Line::from_text(text, &CellAttributes::default(), SEQ_ZERO, None); eprintln!("{:?}", line); let mut all_infos = vec![]; diff --git a/wezterm-gui/src/termwindow/box_model.rs b/wezterm-gui/src/termwindow/box_model.rs index d903facc8..17699b688 100644 --- a/wezterm-gui/src/termwindow/box_model.rs +++ b/wezterm-gui/src/termwindow/box_model.rs @@ -4,8 +4,7 @@ use crate::customglyph::{BlockKey, Poly}; use crate::glyphcache::CachedGlyph; use crate::quad::{QuadImpl, QuadTrait, TripleLayerQuadAllocator, TripleLayerQuadAllocatorTrait}; use crate::termwindow::{ - BorrowedLayers, ColorEase, MouseCapture, RenderState, SrgbTexture2d, TermWindowNotif, UIItem, - UIItemType, + BorrowedLayers, ColorEase, MouseCapture, RenderState, TermWindowNotif, UIItem, UIItemType, }; use crate::utilsprites::RenderMetrics; use ::window::{RectF, WindowOps}; @@ -486,8 +485,8 @@ pub enum ComputedElementContent { #[derive(Debug, Clone)] pub enum ElementCell { - Sprite(Sprite), - Glyph(Rc>), + Sprite(Sprite), + Glyph(Rc), } struct Rects { diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index a3d0f1220..af5c39701 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -3,7 +3,6 @@ use super::renderstate::*; use super::utilsprites::RenderMetrics; use crate::colorease::ColorEase; use crate::frontend::{front_end, try_front_end}; -use crate::glium::texture::SrgbTexture2d; use crate::inputmap::InputMap; use crate::overlay::{ confirm_close_pane, confirm_close_tab, confirm_close_window, confirm_quit_program, launcher, @@ -404,8 +403,7 @@ pub struct TermWindow { quad_generation: usize, shape_generation: usize, - shape_cache: - RefCell>>>>>, + shape_cache: RefCell>>>>, line_to_ele_shape_cache: RefCell>, line_state_cache: RefCell>>, diff --git a/wezterm-gui/src/termwindow/render.rs b/wezterm-gui/src/termwindow/render.rs index a7cd10091..66f6cd59b 100644 --- a/wezterm-gui/src/termwindow/render.rs +++ b/wezterm-gui/src/termwindow/render.rs @@ -1,7 +1,6 @@ use super::box_model::*; use crate::colorease::{ColorEase, ColorEaseUniform}; use crate::customglyph::{BlockKey, *}; -use crate::glium::texture::SrgbTexture2d; use crate::glyphcache::{CachedGlyph, GlyphCache}; use crate::quad::{ HeapQuadAllocator, QuadAllocator, QuadImpl, QuadTrait, TripleLayerQuadAllocator, @@ -49,7 +48,6 @@ use wezterm_font::units::{IntPixelLength, PixelLength}; use wezterm_font::{ClearShapeCache, GlyphInfo, LoadedFont}; use wezterm_term::color::{ColorAttribute, ColorPalette, RgbColor}; use wezterm_term::{CellAttributes, Line, StableRowIndex}; -use window::bitmaps::Texture2d; use window::color::LinearRgba; pub const TOP_LEFT_ROUNDED_CORNER: &[Poly] = &[Poly { @@ -222,7 +220,7 @@ pub struct LineToElementShape { pub underline_color: LinearRgba, pub x_pos: f32, pub pixel_width: f32, - pub glyph_info: Rc>>, + pub glyph_info: Rc>, pub cluster: CellCluster, } @@ -1900,8 +1898,11 @@ impl super::TermWindow { } fn call_draw(&mut self, frame: &mut glium::Frame) -> anyhow::Result<()> { + use crate::glium::texture::SrgbTexture2d; let gl_state = self.render_state.as_ref().unwrap(); let tex = gl_state.glyph_cache.borrow().atlas.texture(); + let tex = tex.downcast_ref::().unwrap(); + let projection = euclid::Transform3D::::ortho( -(self.dimensions.pixel_width as f32) / 2.0, self.dimensions.pixel_width as f32 / 2.0, @@ -3103,7 +3104,7 @@ impl super::TermWindow { font: Option<&Rc>, gl_state: &RenderState, metrics: &RenderMetrics, - ) -> anyhow::Result>> { + ) -> anyhow::Result> { let fa_lock = "\u{f023}"; let line = Line::from_text(fa_lock, attrs, 0, None); let cluster = line.cluster(None); @@ -3413,11 +3414,11 @@ impl super::TermWindow { fn glyph_infos_to_glyphs( &self, style: &TextStyle, - glyph_cache: &mut GlyphCache, + glyph_cache: &mut GlyphCache, infos: &[GlyphInfo], font: &Rc, metrics: &RenderMetrics, - ) -> anyhow::Result>>> { + ) -> anyhow::Result>> { let mut glyphs = Vec::with_capacity(infos.len()); let mut iter = infos.iter().peekable(); while let Some(info) = iter.next() { @@ -3466,7 +3467,7 @@ impl super::TermWindow { gl_state: &RenderState, font: Option<&Rc>, metrics: &RenderMetrics, - ) -> anyhow::Result>>> { + ) -> anyhow::Result>> { let shape_resolve_start = Instant::now(); let key = BorrowedShapeCacheKey { style, @@ -3532,7 +3533,7 @@ impl super::TermWindow { fn lookup_cached_shape( &self, key: &dyn ShapeCacheKeyTrait, - ) -> Option>>>> { + ) -> Option>>> { match self.shape_cache.borrow_mut().get(key) { Some(Ok(info)) => Some(Ok(Rc::clone(info))), Some(Err(err)) => Some(Err(anyhow!("cached shaper error: {}", err))), diff --git a/wezterm-gui/src/utilsprites.rs b/wezterm-gui/src/utilsprites.rs index b8f68164d..31793118d 100644 --- a/wezterm-gui/src/utilsprites.rs +++ b/wezterm-gui/src/utilsprites.rs @@ -1,6 +1,6 @@ use super::glyphcache::GlyphCache; use ::window::bitmaps::atlas::{OutOfTextureSpace, Sprite}; -use ::window::bitmaps::{BitmapImage, Image, Texture2d}; +use ::window::bitmaps::{BitmapImage, Image}; use ::window::color::SrgbaPixel; use ::window::{Point, Rect, Size}; use anyhow::Context; @@ -132,14 +132,14 @@ impl RenderMetrics { } } -pub struct UtilSprites { - pub white_space: Sprite, - pub filled_box: Sprite, +pub struct UtilSprites { + pub white_space: Sprite, + pub filled_box: Sprite, } -impl UtilSprites { +impl UtilSprites { pub fn new( - glyph_cache: &mut GlyphCache, + glyph_cache: &mut GlyphCache, metrics: &RenderMetrics, ) -> Result { let mut buffer = Image::new( diff --git a/window/Cargo.toml b/window/Cargo.toml index 1268752be..cf6c9f5fe 100644 --- a/window/Cargo.toml +++ b/window/Cargo.toml @@ -25,6 +25,7 @@ async-trait = "0.1" anyhow = "1.0" bytes = "1.0" config = { path = "../config" } +downcast-rs = "1.0" thiserror = "1.0" bitflags = "1.3" euclid = "0.22" diff --git a/window/src/bitmaps/atlas.rs b/window/src/bitmaps/atlas.rs index 900f7e7ef..abe26d42e 100644 --- a/window/src/bitmaps/atlas.rs +++ b/window/src/bitmaps/atlas.rs @@ -18,11 +18,8 @@ pub struct OutOfTextureSpace { /// Atlases are bitmaps of srgba data that are sized as a power of 2. /// We allocate sprites out of the available space, using AtlasAllocator /// to manage the available rectangles. -pub struct Atlas -where - T: Texture2d, -{ - texture: Rc, +pub struct Atlas { + texture: Rc, allocator: SimpleAtlasAllocator, @@ -30,11 +27,8 @@ where side: usize, } -impl Atlas -where - T: Texture2d, -{ - pub fn new(texture: &Rc) -> Fallible { +impl Atlas { + pub fn new(texture: &Rc) -> Fallible { ensure!( texture.width() == texture.height(), "texture must be square!" @@ -56,12 +50,12 @@ where } #[inline] - pub fn texture(&self) -> Rc { + pub fn texture(&self) -> Rc { Rc::clone(&self.texture) } /// Reserve space for a sprite of the given size - pub fn allocate(&mut self, im: &dyn BitmapImage) -> Result, OutOfTextureSpace> { + pub fn allocate(&mut self, im: &dyn BitmapImage) -> Result { self.allocate_with_padding(im, None) } @@ -69,7 +63,7 @@ where &mut self, im: &dyn BitmapImage, padding: Option, - ) -> Result, OutOfTextureSpace> { + ) -> Result { let (width, height) = im.image_dimensions(); // If we can't convert the sizes to i32, then we'll never @@ -136,15 +130,12 @@ where } } -pub struct Sprite -where - T: Texture2d, -{ - pub texture: Rc, +pub struct Sprite { + pub texture: Rc, pub coords: Rect, } -impl std::fmt::Debug for Sprite { +impl std::fmt::Debug for Sprite { fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> { fmt.debug_struct("Sprite") .field("coords", &self.coords) @@ -154,10 +145,7 @@ impl std::fmt::Debug for Sprite { } } -impl Clone for Sprite -where - T: Texture2d, -{ +impl Clone for Sprite { fn clone(&self) -> Self { Self { texture: Rc::clone(&self.texture), @@ -166,10 +154,7 @@ where } } -impl Sprite -where - T: Texture2d, -{ +impl Sprite { /// Returns the texture coordinates of the sprite pub fn texture_coords(&self) -> TextureRect { self.texture.to_texture_coords(self.coords) diff --git a/window/src/bitmaps/mod.rs b/window/src/bitmaps/mod.rs index ca4be2234..ab20478b3 100644 --- a/window/src/bitmaps/mod.rs +++ b/window/src/bitmaps/mod.rs @@ -1,5 +1,6 @@ use crate::color::{LinearRgba, SrgbaPixel}; use crate::{Point, Rect, Size}; +use downcast_rs::{impl_downcast, Downcast}; use glium::texture::SrgbTexture2d; use std::cell::RefCell; @@ -12,7 +13,7 @@ pub type TextureSize = euclid::Size2D; /// Represents a big endian bgra32 bitmap that may not be present /// in local RAM, but may be addressable in eg: video RAM -pub trait Texture2d { +pub trait Texture2d: Downcast { /// Copy the bits from the source bitmap to the texture at the location /// specified by the rectangle. /// The dimensions of the rectangle must match the source image @@ -40,6 +41,7 @@ pub trait Texture2d { ) } } +impl_downcast!(Texture2d); impl Texture2d for SrgbTexture2d { fn write(&self, rect: Rect, im: &dyn BitmapImage) {