Skip to content

Commit

Permalink
ash-window/examples/winit: Demonstrate proper surface lifetime with `…
Browse files Browse the repository at this point in the history
…Suspended`/`Resumed` events
  • Loading branch information
MarijnS95 committed Mar 24, 2024
1 parent a1df052 commit 9f68701
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions ash-window/examples/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,9 @@ fn main() -> Result<(), Box<dyn Error>> {
.with_inner_size(PhysicalSize::<u32>::from((800, 600)))
.build(&event_loop)?;

// Create a surface from winit window.
let surface = ash_window::create_surface(
&entry,
&instance,
window.display_handle()?.as_raw(),
window.window_handle()?.as_raw(),
None,
)?;
// Load the surface extensions
let surface_fn = ash::extensions::khr::surface::Instance::new(&entry, &instance);
println!("surface: {surface:?}");
let mut surface = None;

let _ = event_loop.run(move |event, elwp| match event {
winit::event::Event::WindowEvent {
Expand All @@ -62,6 +55,33 @@ fn main() -> Result<(), Box<dyn Error>> {
elwp.exit();
}
Event::LoopExiting => {
// This will be the last event before the loop terminates.
// TODO: How does this play with Suspended?
// https://github.com/rust-windowing/winit/issues/3206
if let Some(surface) = surface.take() {
surface_fn.destroy_surface(surface, None);
}
}
Event::Resumed => {
// Create a surface from winit window.
let s = ash_window::create_surface(
&entry,
&instance,
window.display_handle().unwrap().as_raw(),
window.window_handle().unwrap().as_raw(),
None,
)
.unwrap();
println!("surface: {s:?}");
assert!(
surface.replace(s).is_none(),
"Surface must not yet exist when Resumed is called"
);
}
Event::Suspended => {
let surface = surface
.take()
.expect("Surface must have been created in Resumed");
surface_fn.destroy_surface(surface, None);
}
_ => {}
Expand Down

0 comments on commit 9f68701

Please sign in to comment.