From 500e404db4471fa7ef9d931fab60692a95dbed78 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 14 Jun 2020 21:47:40 -0700 Subject: [PATCH] window: move opengl post-init to a callback on the trait This allows potentially automatically re-initializing the state if the context is lost. --- src/frontend/gui/termwindow.rs | 71 ++++++++++++++++++--------------- window/src/lib.rs | 32 +++++++-------- window/src/os/macos/window.rs | 28 +++---------- window/src/os/wayland/window.rs | 20 +--------- window/src/os/windows/window.rs | 23 +++-------- window/src/os/x11/window.rs | 24 +++-------- window/src/os/x_and_wayland.rs | 22 ++-------- 7 files changed, 76 insertions(+), 144 deletions(-) diff --git a/src/frontend/gui/termwindow.rs b/src/frontend/gui/termwindow.rs index f094dd6e6..88a82bf20 100644 --- a/src/frontend/gui/termwindow.rs +++ b/src/frontend/gui/termwindow.rs @@ -53,6 +53,8 @@ use wezterm_term::color::ColorPalette; use wezterm_term::input::LastMouseClick; use wezterm_term::{Line, StableRowIndex, Underline}; +const ATLAS_SIZE: usize = 4096; + struct RenderScreenLineOpenGLParams<'a> { line_idx: usize, stable_line_idx: Option, @@ -560,6 +562,41 @@ impl WindowCallbacks for TermWindow { metrics::value!("gui.paint.software", start.elapsed()); } + fn opengl_initialize( + &mut self, + window: &dyn WindowOps, + maybe_ctx: anyhow::Result>, + ) -> anyhow::Result<()> { + match maybe_ctx { + Ok(ctx) => { + match OpenGLRenderState::new( + ctx, + &self.fonts, + &self.render_metrics, + ATLAS_SIZE, + self.dimensions.pixel_width, + self.dimensions.pixel_height, + ) { + Ok(gl) => { + log::info!( + "OpenGL initialized! {} {}", + gl.context.get_opengl_renderer_string(), + gl.context.get_opengl_version_string() + ); + self.render_state = RenderState::GL(gl); + } + Err(err) => { + log::error!("OpenGL init failed: {}", err); + } + } + } + Err(err) => log::error!("OpenGL init failed: {}", err), + }; + + window.show(); + Ok(()) + } + fn paint_opengl(&mut self, frame: &mut glium::Frame) { let tab = match self.get_active_tab_or_overlay() { Some(tab) => tab, @@ -650,7 +687,6 @@ impl TermWindow { dimensions ); - const ATLAS_SIZE: usize = 4096; let render_state = RenderState::Software(SoftwareRenderState::new( fontconfig, &render_metrics, @@ -815,38 +851,7 @@ impl TermWindow { .set_clipboard(&clipboard); if super::is_opengl_enabled() { - window.enable_opengl(|any, window, maybe_ctx| { - let mut termwindow = any.downcast_mut::().expect("to be TermWindow"); - - match maybe_ctx { - Ok(ctx) => { - match OpenGLRenderState::new( - ctx, - &termwindow.fonts, - &termwindow.render_metrics, - ATLAS_SIZE, - termwindow.dimensions.pixel_width, - termwindow.dimensions.pixel_height, - ) { - Ok(gl) => { - log::info!( - "OpenGL initialized! {} {}", - gl.context.get_opengl_renderer_string(), - gl.context.get_opengl_version_string() - ); - termwindow.render_state = RenderState::GL(gl); - } - Err(err) => { - log::error!("OpenGL init failed: {}", err); - } - } - } - Err(err) => log::error!("OpenGL init failed: {}", err), - }; - - window.show(); - Ok(()) - }); + window.enable_opengl(); } else { window.show(); } diff --git a/window/src/lib.rs b/window/src/lib.rs index 6db07b031..eb803aea8 100644 --- a/window/src/lib.rs +++ b/window/src/lib.rs @@ -144,6 +144,18 @@ pub trait WindowCallbacks: Any { frame.clear_color_srgb(0.25, 0.125, 0.375, 1.0); } + /// Called when opengl is initialized by enable_opengl(). + /// (and perhaps also if/when opengl is reinitialized after the + /// context is lost) + #[cfg(feature = "opengl")] + fn opengl_initialize( + &mut self, + _window: &dyn WindowOps, + context: anyhow::Result>, + ) -> anyhow::Result<()> { + context.map(|_| ()) + } + /// Called to handle a key event. /// If your window didn't handle the event, you must return false. /// This is particularly important for eg: ALT keys on windows, @@ -217,23 +229,11 @@ pub trait WindowOps { Self: Sized, R: Send + 'static; + /// Attempt to set up opengl based painting. + /// Will call opengl_initialize() in your WindowCallbacks impl to + /// inform it of the gl context. #[cfg(feature = "opengl")] - fn enable_opengl< - R, - F: Send - + 'static - + Fn( - &mut dyn Any, - &dyn WindowOps, - anyhow::Result>, - ) -> anyhow::Result, - >( - &self, - func: F, - ) -> promise::Future - where - Self: Sized, - R: Send + 'static; + fn enable_opengl(&self) -> promise::Future<()>; /// Initiate textual transfer from the clipboard fn get_clipboard(&self, clipboard: Clipboard) -> Future; diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index 633a5fa38..853eaa8cf 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -442,23 +442,7 @@ impl WindowOps for Window { } #[cfg(feature = "opengl")] - fn enable_opengl< - R, - F: Send - + 'static - + Fn( - &mut dyn Any, - &dyn WindowOps, - anyhow::Result>, - ) -> anyhow::Result, - >( - &self, - func: F, - ) -> promise::Future - where - Self: Sized, - R: Send + 'static, - { + fn enable_opengl(&self) -> promise::Future<()> { Connection::with_window_inner(self.0, move |inner| { let window = Window(inner.window_id); @@ -468,11 +452,11 @@ impl WindowOps for Window { window_view.inner.borrow_mut().gl_context_pair = glium_context.as_ref().map(Clone::clone).ok(); - func( - window_view.inner.borrow_mut().callbacks.as_any(), - &window, - glium_context.map(|pair| pair.context), - ) + window_view + .inner + .borrow_mut() + .callbacks + .opengl_initialize(&window, glium_context.map(|pair| pair.context)) } else { bail!("enable_opengl: window is invalid"); } diff --git a/window/src/os/wayland/window.rs b/window/src/os/wayland/window.rs index 88612f964..8bdbcc3cf 100644 --- a/window/src/os/wayland/window.rs +++ b/window/src/os/wayland/window.rs @@ -710,23 +710,7 @@ impl WindowOps for WaylandWindow { } #[cfg(feature = "opengl")] - fn enable_opengl< - R, - F: Send - + 'static - + Fn( - &mut dyn Any, - &dyn WindowOps, - anyhow::Result>, - ) -> anyhow::Result, - >( - &self, - func: F, - ) -> promise::Future - where - Self: Sized, - R: Send + 'static, - { + fn enable_opengl(&self) -> promise::Future<()> { WaylandConnection::with_window_inner(self.0, move |inner| { let window = Window::Wayland(WaylandWindow(inner.window_id)); let wayland_conn = Connection::get().unwrap().wayland(); @@ -762,7 +746,7 @@ impl WindowOps for WaylandWindow { inner.gl_state = gl_state.as_ref().map(Rc::clone).ok(); inner.wegl_surface = wegl_surface; - func(inner.callbacks.as_any(), &window, gl_state) + inner.callbacks.opengl_initialize(&window, gl_state) }) } diff --git a/window/src/os/windows/window.rs b/window/src/os/windows/window.rs index 89753d284..c4ce41492 100644 --- a/window/src/os/windows/window.rs +++ b/window/src/os/windows/window.rs @@ -394,23 +394,7 @@ impl WindowOps for Window { } #[cfg(feature = "opengl")] - fn enable_opengl< - R, - F: Send - + 'static - + Fn( - &mut dyn Any, - &dyn WindowOps, - anyhow::Result>, - ) -> anyhow::Result, - >( - &self, - func: F, - ) -> promise::Future - where - Self: Sized, - R: Send + 'static, - { + fn enable_opengl(&self) -> promise::Future<()> { Connection::with_window_inner(self.0, move |inner| { let window = Window(inner.hwnd); @@ -430,7 +414,10 @@ impl WindowOps for Window { inner.gl_state = gl_state.as_ref().map(Rc::clone).ok(); - func(inner.callbacks.borrow_mut().as_any(), &window, gl_state) + inner + .callbacks + .borrow_mut() + .opengl_initialize(&window, gl_state) }) } diff --git a/window/src/os/x11/window.rs b/window/src/os/x11/window.rs index 3a7eb51af..3937f4643 100644 --- a/window/src/os/x11/window.rs +++ b/window/src/os/x11/window.rs @@ -122,6 +122,10 @@ impl XWindowInner { #[cfg(feature = "opengl")] { if let Some(gl_context) = self.gl_state.as_ref() { + if gl_context.is_context_lost() { + log::error!("opengl context was lost; should reinit"); + } + self.expose.clear(); let mut frame = glium::Frame::new( @@ -927,23 +931,7 @@ impl WindowOps for XWindow { } #[cfg(feature = "opengl")] - fn enable_opengl< - R, - F: Send - + 'static - + Fn( - &mut dyn Any, - &dyn WindowOps, - anyhow::Result>, - ) -> anyhow::Result, - >( - &self, - func: F, - ) -> promise::Future - where - Self: Sized, - R: Send + 'static, - { + fn enable_opengl(&self) -> promise::Future<()> { XConnection::with_window_inner(self.0, move |inner| { let window = XWindow(inner.window_id); @@ -966,7 +954,7 @@ impl WindowOps for XWindow { inner.gl_state = gl_state.as_ref().map(Rc::clone).ok(); - func(inner.callbacks.as_any(), &window, gl_state) + inner.callbacks.opengl_initialize(&window, gl_state) }) } diff --git a/window/src/os/x_and_wayland.rs b/window/src/os/x_and_wayland.rs index 1d86679e9..51730821c 100644 --- a/window/src/os/x_and_wayland.rs +++ b/window/src/os/x_and_wayland.rs @@ -219,27 +219,11 @@ impl WindowOps for Window { } #[cfg(feature = "opengl")] - fn enable_opengl< - R, - F: Send - + 'static - + Fn( - &mut dyn Any, - &dyn WindowOps, - anyhow::Result>, - ) -> anyhow::Result, - >( - &self, - func: F, - ) -> promise::Future - where - Self: Sized, - R: Send + 'static, - { + fn enable_opengl(&self) -> promise::Future<()> { match self { - Self::X11(x) => x.enable_opengl(func), + Self::X11(x) => x.enable_opengl(), #[cfg(feature = "wayland")] - Self::Wayland(w) => w.enable_opengl(func), + Self::Wayland(w) => w.enable_opengl(), } }