From 0158e8e49a1b8ec49ea3a0adf38852651e3b45de Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 6 Aug 2021 13:06:01 -0700 Subject: [PATCH] gui: fixup window transparency --- termwiz/src/image.rs | 9 ++++++--- wezterm-gui/src/glyphcache.rs | 26 ++++++++++++++++++++++++++ wezterm-gui/src/termwindow/mod.rs | 3 ++- wezterm-gui/src/termwindow/render.rs | 14 +++++++++----- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/termwiz/src/image.rs b/termwiz/src/image.rs index b6cc8d185..a7a689709 100644 --- a/termwiz/src/image.rs +++ b/termwiz/src/image.rs @@ -270,7 +270,10 @@ impl ImageDataType { Self::EncodedFile(data) => { let format = match image::guess_format(&data) { Ok(format) => format, - _ => return Self::EncodedFile(data), + Err(err) => { + log::warn!("Unable to decode raw image data: {:#}", err); + return Self::EncodedFile(data); + } }; match format { ImageFormat::Gif => image::gif::GifDecoder::new(&*data) @@ -297,7 +300,7 @@ impl ImageDataType { Self::decode_single(data) } } - _ => Self::EncodedFile(data), + _ => Self::decode_single(data), } } data => data, @@ -370,7 +373,7 @@ impl PartialEq for ImageData { impl ImageData { /// Create a new ImageData struct with the provided raw data. pub fn with_raw_data(data: Vec) -> Self { - Self::with_data(ImageDataType::EncodedFile(data)) + Self::with_data(ImageDataType::EncodedFile(data).decode()) } pub fn with_data(data: ImageDataType) -> Self { diff --git a/wezterm-gui/src/glyphcache.rs b/wezterm-gui/src/glyphcache.rs index 284ba4be7..bfe907132 100644 --- a/wezterm-gui/src/glyphcache.rs +++ b/wezterm-gui/src/glyphcache.rs @@ -14,11 +14,13 @@ use ::window::{Point, Rect}; use anyhow::Context; use config::{AllowSquareGlyphOverflow, TextStyle}; use euclid::num::Zero; +use ordered_float::NotNan; use std::collections::HashMap; use std::convert::TryInto; use std::rc::Rc; use std::sync::{Arc, MutexGuard}; use std::time::Instant; +use termwiz::color::RgbColor; use termwiz::image::{ImageData, ImageDataType}; use termwiz::surface::CursorShape; use wezterm_font::units::*; @@ -217,6 +219,7 @@ pub struct GlyphCache { line_glyphs: HashMap>, pub block_glyphs: HashMap>, pub cursor_glyphs: HashMap, Sprite>, + pub color: HashMap<(RgbColor, NotNan), Sprite>, pub metrics: RenderMetrics, } @@ -244,6 +247,7 @@ impl GlyphCache { line_glyphs: HashMap::new(), block_glyphs: HashMap::new(), cursor_glyphs: HashMap::new(), + color: HashMap::new(), }) } } @@ -296,6 +300,7 @@ impl GlyphCache { line_glyphs: HashMap::new(), block_glyphs: HashMap::new(), cursor_glyphs: HashMap::new(), + color: HashMap::new(), }) } } @@ -627,6 +632,27 @@ impl GlyphCache { } } + 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) { + return Ok(s.clone()); + } + + let (red, green, blue) = color.to_tuple_rgb8(); + let alpha = (alpha * 255.0) as u8; + + let data = vec![ + red, green, blue, alpha, red, green, blue, alpha, red, green, blue, alpha, red, green, + blue, alpha, + ]; + let image = Image::from_raw(2, 2, data); + + let sprite = self.atlas.allocate(&image)?; + self.color.insert(key, sprite.clone()); + Ok(sprite) + } + pub fn cached_block(&mut self, block: BlockKey) -> anyhow::Result> { if let Some(s) = self.block_glyphs.get(&block) { return Ok(s.clone()); diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index 0a9639a86..d738e6880 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -343,7 +343,8 @@ fn load_background_image(config: &ConfigHandle) -> Option> { Some(p) => match std::fs::read(p) { Ok(data) => { log::info!("loaded {}", p.display()); - Some(Arc::new(ImageData::with_raw_data(data))) + let data = ImageDataType::EncodedFile(data).decode(); + Some(Arc::new(ImageData::with_data(data))) } Err(err) => { log::error!( diff --git a/wezterm-gui/src/termwindow/render.rs b/wezterm-gui/src/termwindow/render.rs index 66cec7b3c..34b76ac94 100644 --- a/wezterm-gui/src/termwindow/render.rs +++ b/wezterm-gui/src/termwindow/render.rs @@ -282,6 +282,7 @@ impl super::TermWindow { self.dimensions.pixel_height as f32 / 2., ); quad.set_texture_adjust(0., 0., 0., 0.); + quad.set_is_background_image(); match (self.window_background.as_ref(), self.allow_images) { (Some(im), true) => { @@ -295,7 +296,6 @@ impl super::TermWindow { gl_state.glyph_cache.borrow_mut().cached_image(im, None)?; self.update_next_frame_time(next_due); quad.set_texture(sprite.texture_coords()); - quad.set_is_background_image(); quad.set_hsv(config.window_background_image_hsb); quad.set_fg_color(color); } @@ -303,12 +303,16 @@ impl super::TermWindow { // Regular window background color let background = rgbcolor_alpha_to_window_color( global_bg_color, - self.config.window_background_opacity, + config.window_background_opacity, + ); + quad.set_texture( + gl_state + .glyph_cache + .borrow_mut() + .cached_color(global_bg_color, config.window_background_opacity)? + .texture_coords(), ); - quad.set_texture(filled_box); quad.set_fg_color(background); - quad.set_bg_color(background); - quad.set_has_color(false); quad.set_hsv(None); } }