diff --git a/src/frontend/software/termwindow.rs b/src/frontend/software/termwindow.rs index 8aaeb0d62..4a46150f5 100644 --- a/src/frontend/software/termwindow.rs +++ b/src/frontend/software/termwindow.rs @@ -187,6 +187,7 @@ struct Vertex { } ::window::glium::implement_vertex!(Vertex, position, tex); +#[derive(Copy, Clone)] struct RenderMetrics { descender: f64, descender_row: isize, @@ -363,6 +364,23 @@ struct OpenGLRenderState { util_sprites: UtilSprites, } +impl OpenGLRenderState { + pub fn new( + context: Rc, + fonts: &Rc, + metrics: &RenderMetrics, + size: usize, + ) -> Fallible { + let glyph_cache = RefCell::new(GlyphCache::new_gl(&context, fonts, size)?); + let util_sprites = UtilSprites::new(&mut *glyph_cache.borrow_mut(), metrics)?; + Ok(Self { + context, + glyph_cache, + util_sprites, + }) + } +} + enum RenderState { Software(SoftwareRenderState), GL(OpenGLRenderState), @@ -651,8 +669,12 @@ impl TermWindow { let width = render_metrics.cell_size.width as usize * physical_cols; let height = render_metrics.cell_size.height as usize * physical_rows; - let render_state = - RenderState::Software(SoftwareRenderState::new(fontconfig, &render_metrics, 4096)?); + const ATLAS_SIZE: usize = 4096; + let render_state = RenderState::Software(SoftwareRenderState::new( + fontconfig, + &render_metrics, + ATLAS_SIZE, + )?); let window = Window::new_window( "wezterm", @@ -698,7 +720,29 @@ impl TermWindow { window.show(); if super::is_opengl_enabled() { - log::error!("I should use opengl"); + window.enable_opengl(|any, window, maybe_ctx| { + let termwindow = any.downcast_ref::().expect("to be TermWindow"); + + log::error!("I should use opengl"); + match maybe_ctx { + Ok(ctx) => { + match OpenGLRenderState::new( + ctx, + &termwindow.fonts, + &termwindow.render_metrics, + ATLAS_SIZE, + ) { + Ok(gl) => { + log::error!("OpenGL initialized!"); + } + Err(err) => { + log::error!("OpenGL init failed: {}", err); + } + } + } + Err(err) => log::error!("OpenGL init failed: {}", err), + } + }); } Ok(()) diff --git a/window/examples/basic_opengl.rs b/window/examples/basic_opengl.rs index dbfc08ef4..2a35a1978 100644 --- a/window/examples/basic_opengl.rs +++ b/window/examples/basic_opengl.rs @@ -42,12 +42,10 @@ fn spawn_window() -> Fallible<()> { )?; #[cfg(feature = "opengl")] - match win.enable_opengl() { - Ok(_ctx) => { - eprintln!("opengl enabled!"); - } + win.enable_opengl(|_any, _window, maybe_ctx| match maybe_ctx { + Ok(_ctx) => eprintln!("opengl enabled!"), Err(err) => eprintln!("opengl fail: {}", err), - }; + }); #[cfg(not(feature = "opengl"))] eprintln!( diff --git a/window/src/lib.rs b/window/src/lib.rs index d1695f920..8971dd097 100644 --- a/window/src/lib.rs +++ b/window/src/lib.rs @@ -171,9 +171,15 @@ pub trait WindowOps { Self: Sized; #[cfg(feature = "opengl")] - fn enable_opengl(&self) -> failure::Fallible> { - failure::bail!("opengl support is not implemented"); - } + fn enable_opengl< + F: Send + + 'static + + Fn(&mut dyn Any, &dyn WindowOps, failure::Fallible>), + >( + &self, + func: F, + ) where + Self: Sized; } pub trait WindowOpsMut { diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index b370689c3..4c0812190 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -271,31 +271,44 @@ impl WindowOps for Window { } #[cfg(feature = "opengl")] - fn enable_opengl(&self) -> Fallible> { - if let Some(handle) = Connection::get().unwrap().window_by_id(self.0) { - let inner = handle.borrow_mut(); - let gl_state = opengl::GlState::create(*inner.view)?; + fn enable_opengl< + F: Send + + 'static + + Fn(&mut dyn Any, &dyn WindowOps, failure::Fallible>), + >( + &self, + func: F, + ) where + Self: Sized, + { + Connection::with_window_inner(self.0, move |inner| { + let window = Window(inner.window_id); - let glium_context = unsafe { - glium::backend::Context::new( - Rc::new(gl_state), - true, - if cfg!(debug_assertions) { - glium::debug::DebugCallbackBehavior::DebugMessageOnError - } else { - glium::debug::DebugCallbackBehavior::Ignore - }, - )? - }; + let glium_context = opengl::GlState::create(*inner.view).and_then(|gl_state| { + Ok(unsafe { + glium::backend::Context::new( + Rc::new(gl_state), + true, + if cfg!(debug_assertions) { + glium::debug::DebugCallbackBehavior::DebugMessageOnError + } else { + glium::debug::DebugCallbackBehavior::Ignore + }, + )? + }) + }); if let Some(window_view) = WindowView::get_this(unsafe { &**inner.view }) { - window_view.inner.borrow_mut().gl_context = Some(Rc::clone(&glium_context)); - } + window_view.inner.borrow_mut().gl_context = + glium_context.as_ref().map(Rc::clone).ok(); - Ok(glium_context) - } else { - failure::bail!("invalid window?"); - } + func( + window_view.inner.borrow_mut().callbacks.as_any(), + &window, + glium_context, + ); + } + }); } }