From 403d002d0a81d264b00611ee8f3f7591ad41b492 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 1 Nov 2020 10:28:17 -0800 Subject: [PATCH] window: fix potential crash with multiple egl windows on windows I didn't actually see this crash, but the same potential problem was present, so adjust the code as per 9712d4d03c4f304ff1c03a1100f9d13ae2669948 refs: #316 --- window/src/os/windows/connection.rs | 4 ++++ window/src/os/windows/window.rs | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/window/src/os/windows/connection.rs b/window/src/os/windows/connection.rs index 24a8ecd51..2f43508ec 100644 --- a/window/src/os/windows/connection.rs +++ b/window/src/os/windows/connection.rs @@ -18,6 +18,8 @@ pub struct Connection { event_handle: HANDLE, pub(crate) windows: RefCell>>>, timers: RefCell>, + #[cfg(feature = "opengl")] + pub(crate) gl_connection: RefCell>>, } impl ConnectionOps for Connection { @@ -97,6 +99,8 @@ impl Connection { event_handle, windows: RefCell::new(HashMap::new()), timers: RefCell::new(HashMap::new()), + #[cfg(feature = "opengl")] + gl_connection: RefCell::new(None), }) } diff --git a/window/src/os/windows/window.rs b/window/src/os/windows/window.rs index bb1564e7b..905b469e2 100644 --- a/window/src/os/windows/window.rs +++ b/window/src/os/windows/window.rs @@ -132,14 +132,23 @@ impl WindowInner { #[cfg(feature = "opengl")] fn enable_opengl(&mut self) -> anyhow::Result<()> { let window = Window(self.hwnd); + let conn = Connection::get().unwrap(); let gl_state = if is_egl_preferred() { - crate::egl::GlState::create(None, self.hwnd.0) + match conn.gl_connection.borrow().as_ref() { + None => crate::egl::GlState::create(None, self.hwnd.0), + Some(glconn) => { + crate::egl::GlState::create_with_existing_connection(glconn, self.hwnd.0) + } + } } else { Err(anyhow::anyhow!("Config says to avoid EGL")) } .and_then(|egl| unsafe { log::error!("Initialized EGL!"); + conn.gl_connection + .borrow_mut() + .replace(Rc::clone(egl.get_connection())); let backend = Rc::new(egl); Ok(glium::backend::Context::new( backend,