Skip to content

Commit

Permalink
examples: Reuse not_current_context in resumed() handler
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MarijnS95 authored Jul 3, 2024
1 parent 3296c9a commit 58afc41
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions glutin_examples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
|_| {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 58afc41

Please sign in to comment.