From 09396e1b62b4709f373aa047905903df0377dd37 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Tue, 2 Jul 2024 22:37:40 +0200 Subject: [PATCH] examples: Reuse `not_current_context` in `resumed()` handler When testing the `glutin` example on Android, after suspending and resuming the app no triangle appears. As it turns out the `winit 0.30` bump in #1678 never reuses the `not_current_context` that was set apart in `suspend()`, but creates a new one, without re-running initialization code such as creating the `Renderer`. Partially restore original code before the `winit 0.30` bump, which never created a context in `resume()` because it was already available before the loop started (and moved back in place in `suspend()`). Note also that more initialization code that now (unnecessarily) lives in `resume()` used to be outside of the loop. --- glutin_examples/src/lib.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/glutin_examples/src/lib.rs b/glutin_examples/src/lib.rs index 4763cf670e..e997dd01d3 100644 --- a/glutin_examples/src/lib.rs +++ b/glutin_examples/src/lib.rs @@ -92,7 +92,10 @@ impl ApplicationHandler for App { .with_context_api(ContextApi::OpenGl(Some(Version::new(2, 1)))) .build(raw_window_handle); - self.not_current_gl_context.replace(unsafe { + // Reuse the uncurrented context from a suspended() call if it exists, otherwise + // this is the first time resumed() is called, where the context still + // has to be created. + let not_current_gl_context = self.not_current_gl_context.take().unwrap_or_else(|| unsafe { gl_display.create_context(&gl_config, &context_attributes).unwrap_or_else(|_| { gl_display.create_context(&gl_config, &fallback_context_attributes).unwrap_or_else( |_| { @@ -121,8 +124,7 @@ impl ApplicationHandler for App { unsafe { gl_config.display().create_window_surface(&gl_config, &attrs).unwrap() }; // Make it current. - let gl_context = - self.not_current_gl_context.take().unwrap().make_current(&gl_surface).unwrap(); + let gl_context = not_current_gl_context.make_current(&gl_surface).unwrap(); // The context needs to be current for the Renderer to set up shaders and // buffers. It also performs function loading, which needs a current context on