Skip to content

Commit

Permalink
desktop: Don't render while minimized
Browse files Browse the repository at this point in the history
On desktop, don't render while the window is minimized.

In the wgpu backend, don't panic if swap chain fails to grab a
texture. Instead recreate the swap chain and bail on the current
frame.

Fixes panic when minimizing with the wgpu vulkan backend (#2065).
  • Loading branch information
Herschel committed Dec 29, 2020
1 parent 268ed44 commit 6aafb46
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
12 changes: 10 additions & 2 deletions desktop/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ fn run_player(opt: Opt) -> Result<(), Box<dyn std::error::Error>> {
let mut mouse_pos = PhysicalPosition::new(0.0, 0.0);
let mut time = Instant::now();
let mut next_frame_time = Instant::now();

let mut minimized = false;
loop {
// Poll UI events
event_loop.run(move |event, _window_target, control_flow| {
Expand All @@ -273,10 +273,18 @@ fn run_player(opt: Opt) -> Result<(), Box<dyn std::error::Error>> {
}

// Render
winit::event::Event::RedrawRequested(_) => player.lock().unwrap().render(),
winit::event::Event::RedrawRequested(_) => {
// Don't render when minimized to avoid potential swap chain errors in `wgpu`.
if !minimized {
player.lock().unwrap().render();
}
}

winit::event::Event::WindowEvent { event, .. } => match event {
WindowEvent::Resized(size) => {
// TODO: Change this when winit adds a `Window::minimzed` or `WindowEvent::Minimize`.
minimized = size.width == 0 && size.height == 0;

let mut player_lock = player.lock().unwrap();
player_lock.set_viewport_dimensions(size.width, size.height);
player_lock
Expand Down
8 changes: 7 additions & 1 deletion render/wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,13 @@ impl<T: RenderTarget + 'static> RenderBackend for WgpuRenderBackend<T> {
Ok(frame) => frame,
Err(e) => {
log::warn!("Couldn't begin new render frame: {}", e);
panic!();
// Attemp to recreate the swap chain in this case.
self.target.resize(
&self.descriptors.device,
self.target.width(),
self.target.height(),
);
return;
}
};

Expand Down

0 comments on commit 6aafb46

Please sign in to comment.