Skip to content

Commit

Permalink
Use PresentMode::Mailbox when available.
Browse files Browse the repository at this point in the history
This provides much better latency by default on X11 and Wayland.
  • Loading branch information
xorgy committed Oct 22, 2024
1 parent e03dfdd commit 45d3bce
Showing 1 changed file with 43 additions and 37 deletions.
80 changes: 43 additions & 37 deletions masonry/src/event_loop_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,43 @@ impl MasonryState<'_> {
}
}

fn configure_window(&mut self, window: Arc<winit::window::Window>, accesskit_adapter: Adapter) {
// https://github.com/rust-windowing/winit/issues/2308
let size = if cfg!(target_os = "ios") {
window.outer_size()
} else {
window.inner_size()
};

let mut surface = pollster::block_on(self.render_cx.create_surface(
window.clone(),
size.width,
size.height,
PresentMode::AutoVsync,
))
.unwrap();

let adapter = pollster::block_on(wgpu::util::initialize_adapter_from_env_or_default(
&self.render_cx.instance,
Some(&surface.surface),
))
.unwrap();
let capabilities = surface.surface.get_capabilities(&adapter);
if capabilities
.present_modes
.contains(&wgpu::PresentMode::Mailbox)
{
self.render_cx
.set_present_mode(&mut surface, wgpu::PresentMode::Mailbox);
}

self.window = WindowState::Rendering {
window,
surface,
accesskit_adapter,
}
}

// --- MARK: RESUMED ---
pub fn handle_resumed(&mut self, event_loop: &ActiveEventLoop) {
match std::mem::replace(
Expand All @@ -265,50 +302,19 @@ impl MasonryState<'_> {

let adapter = Adapter::with_event_loop_proxy(&window, self.proxy.clone());
window.set_visible(visible);
let window = Arc::new(window);
// https://github.com/rust-windowing/winit/issues/2308
#[cfg(target_os = "ios")]
let size = window.outer_size();
#[cfg(not(target_os = "ios"))]
let size = window.inner_size();
let surface = pollster::block_on(self.render_cx.create_surface(
window.clone(),
size.width,
size.height,
PresentMode::AutoVsync,
))
.unwrap();
let scale_factor = window.scale_factor();
self.window = WindowState::Rendering {
window,
surface,
accesskit_adapter: adapter,
};

let window = Arc::new(window);

self.configure_window(window, adapter);

self.render_root
.handle_window_event(WindowEvent::Rescale(scale_factor));
}
WindowState::Suspended {
window,
accesskit_adapter,
} => {
// https://github.com/rust-windowing/winit/issues/2308
#[cfg(target_os = "ios")]
let size = window.outer_size();
#[cfg(not(target_os = "ios"))]
let size = window.inner_size();
let surface = pollster::block_on(self.render_cx.create_surface(
window.clone(),
size.width,
size.height,
PresentMode::AutoVsync,
))
.unwrap();
self.window = WindowState::Rendering {
window,
surface,
accesskit_adapter,
}
}
} => self.configure_window(window, accesskit_adapter),
_ => {
// We have received a redundant resumed event. That's allowed by winit
}
Expand Down

0 comments on commit 45d3bce

Please sign in to comment.