Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't poll the event loop on Wayland to reduce the power usage #2149

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ default = []
embed-fonts = []
profiling = ["dep:tracy-client-sys"]
gpu_profiling = ["profiling"]
# Corresponds to https://github.com/nagisa/rust_tracy_client/blob/main/FEATURES.mkd
tracy-fibers = ["tracy-client-sys?/fibers"]
tracy-system-tracing = ["tracy-client-sys?/system-tracing"]
tracy-context-switch-tracing = ["tracy-client-sys?/context-switch-tracing"]
tracy-sampling = ["tracy-client-sys?/sampling"]
tracy-code-transfer = ["tracy-client-sys?/code-transfer"]
tracy-callstack-inlines = ["tracy-client-sys?/callstack-inlines"]

[dependencies]
anyhow = { version = "1.0.75", features = ["backtrace"] }
Expand Down Expand Up @@ -58,7 +65,7 @@ time = "0.3.9"
tokio = { version = "1.25.0", features = ["full"] }
tokio-util = { version = "0.7.4", features = ["compat"] }
toml = "0.7.3"
tracy-client-sys = { version = "0.19.0", optional = true }
tracy-client-sys = { version = "0.22.0", optional = true, default-features = false, features = ["broadcast", "delayed-init", "enable", "manual-lifetime"] }
unicode-segmentation = "1.9.0"
which = "4.2.5"
winit = { version = "=0.29.4", features = ["serde"] }
Expand Down
2 changes: 1 addition & 1 deletion src/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ pub fn main_loop(
break;
}

let (wait_duration, _) = update_loop.get_event_wait_time();
let (wait_duration, _) = update_loop.get_event_wait_time(&window_wrapper.vsync);
let event = rx
.recv_timeout(wait_duration)
.map_err(|e| matches!(e, RecvTimeoutError::Disconnected));
Expand Down
23 changes: 12 additions & 11 deletions src/window/update_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use super::draw_background;
use super::{UserEvent, WindowSettings, WinitWindowWrapper};
use crate::{
profiling::{tracy_create_gpu_context, tracy_zone},
renderer::VSync,
settings::SETTINGS,
};

Expand Down Expand Up @@ -54,17 +55,22 @@ impl UpdateLoop {
}
}

pub fn get_event_wait_time(&self) -> (Duration, Instant) {
pub fn get_event_wait_time(&self, vsync: &VSync) -> (Duration, Instant) {
let refresh_rate = match self.focused {
FocusedState::Focused | FocusedState::UnfocusedNotDrawn => {
// NOTE: Always wait for the idle refresh rate when winit throttling is used to avoid waking up too early
// The winit redraw request will likely happen much before that and wake it up anyway
FocusedState::Focused | FocusedState::UnfocusedNotDrawn
if !vsync.uses_winit_throttling() =>
{
SETTINGS.get::<WindowSettings>().refresh_rate as f32
}
FocusedState::Unfocused => SETTINGS.get::<WindowSettings>().refresh_rate_idle as f32,
_ => SETTINGS.get::<WindowSettings>().refresh_rate_idle as f32,
}
.max(1.0);

let expected_frame_duration = Duration::from_secs_f32(1.0 / refresh_rate);
if self.num_consecutive_rendered > 0 {
if self.num_consecutive_rendered > 0 && !vsync.uses_winit_throttling() {
// Only poll when using native vsync
(Duration::from_nanos(0), Instant::now())
} else {
let deadline = self.previous_frame_start + expected_frame_duration;
Expand Down Expand Up @@ -155,12 +161,7 @@ impl UpdateLoop {
self.should_render |= window_wrapper.handle_event(event);
}

let (_, deadline) = self.get_event_wait_time();

if self.num_consecutive_rendered > 0 {
Ok(ControlFlow::Poll)
} else {
Ok(ControlFlow::WaitUntil(deadline))
}
let (_, deadline) = self.get_event_wait_time(&window_wrapper.vsync);
Ok(ControlFlow::WaitUntil(deadline))
}
}