From 6b0083aa396edcd4d2debd2ed0b3f43d40f51055 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Fri, 16 Jun 2023 19:43:54 +0200 Subject: [PATCH] examples/winit: Implement proper `Event::Resumed` semantics On Android the backing buffer (`NativeWindow`) disappears when the application is not focussed and/or the screen is locked. Winit handles this by requiring apps to create their `raw_window_handle()` consumers _after_ `Event::Resumed` and to clean it up _before_ returning from `Event::Suspended`. For consistency Winit also sends `Resumed` on all other platforms during init. --- examples/winit.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/examples/winit.rs b/examples/winit.rs index 7c903e2..192523a 100644 --- a/examples/winit.rs +++ b/examples/winit.rs @@ -23,18 +23,29 @@ fn main() { .unwrap(); } - let context = softbuffer::Context::new(window.clone()).unwrap(); - let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap(); + let mut state = None; event_loop .run(move |event, elwt| { elwt.set_control_flow(ControlFlow::Wait); match event { + Event::Resumed => { + let context = softbuffer::Context::new(window.clone()).unwrap(); + let surface = softbuffer::Surface::new(&context, window.clone()).unwrap(); + state = Some((context, surface)); + } + Event::Suspended => { + state = None; + } Event::WindowEvent { window_id, event: WindowEvent::RedrawRequested, } if window_id == window.id() => { + let Some((_, surface)) = state.as_mut() else { + eprintln!("RedrawRequested fired before Resumed or after Suspended"); + return; + }; if let (Some(width), Some(height)) = { let size = window.inner_size(); (NonZeroU32::new(size.width), NonZeroU32::new(size.height))