window: move opengl post-init to a callback on the trait

This allows potentially automatically re-initializing the
state if the context is lost.
This commit is contained in:
Wez Furlong committed 2020-06-14 21:47:40 -07:00
1 parent 90fa968a34
commit 500e404db4
7 files changed
+76 -144

No files matched your search

+38 -33
View File
@@ -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<StableRowIndex>,
@@ -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<std::rc::Rc<glium::backend::Context>>,
) -> 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::<TermWindow>().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();
}
+16 -16
View File
@@ -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<std::rc::Rc<glium::backend::Context>>,
) -> 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<std::rc::Rc<glium::backend::Context>>,
) -> anyhow::Result<R>,
>(
&self,
func: F,
) -> promise::Future<R>
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<String>;
+6 -22
View File
@@ -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<std::rc::Rc<glium::backend::Context>>,
) -> anyhow::Result<R>,
>(
&self,
func: F,
) -> promise::Future<R>
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");
}
+2 -18
View File
@@ -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<std::rc::Rc<glium::backend::Context>>,
) -> anyhow::Result<R>,
>(
&self,
func: F,
) -> promise::Future<R>
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)
})
}
+5 -18
View File
@@ -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<std::rc::Rc<glium::backend::Context>>,
) -> anyhow::Result<R>,
>(
&self,
func: F,
) -> promise::Future<R>
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)
})
}
+6 -18
View File
@@ -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<std::rc::Rc<glium::backend::Context>>,
) -> anyhow::Result<R>,
>(
&self,
func: F,
) -> promise::Future<R>
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)
})
}
+3 -19
View File
@@ -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<std::rc::Rc<glium::backend::Context>>,
) -> anyhow::Result<R>,
>(
&self,
func: F,
) -> promise::Future<R>
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(),
}
}