forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce power usage with configurable event loop (bevyengine#3974)
# Objective - Reduce power usage for games when not focused. - Reduce power usage to ~0 when a desktop application is minimized (opt-in). - Reduce power usage when focused, only updating on a `winit` event, or the user sends a redraw request. (opt-in) https://user-images.githubusercontent.com/2632925/156904387-ec47d7de-7f06-4c6f-8aaf-1e952c1153a2.mp4 Note resource usage in the Task Manager in the above video. ## Solution - Added a type `UpdateMode` that allows users to specify how the winit event loop is updated, without exposing winit types. - Added two fields to `WinitConfig`, both with the `UpdateMode` type. One configures how the application updates when focused, and the other configures how the application behaves when it is not focused. Users can modify this resource manually to set the type of event loop control flow they want. - For convenience, two functions were added to `WinitConfig`, that provide reasonable presets: `game()` (default) and `desktop_app()`. - The `game()` preset, which is used by default, is unchanged from current behavior with one exception: when the app is out of focus the app updates at a minimum of 10fps, or every time a winit event is received. This has a huge positive impact on power use and responsiveness on my machine, which will otherwise continue running the app at many hundreds of fps when out of focus or minimized. - The `desktop_app()` preset is fully reactive, only updating when user input (winit event) is supplied or a `RedrawRequest` event is sent. When the app is out of focus, it only updates on `Window` events - i.e. any winit event that directly interacts with the window. What this means in practice is that the app uses *zero* resources when minimized or not interacted with, but still updates fluidly when the app is out of focus and the user mouses over the application. - Added a `RedrawRequest` event so users can force an update even if there are no events. This is useful in an application when you want to, say, run an animation even when the user isn't providing input. - Added an example `low_power` to demonstrate these changes ## Usage Configuring the event loop: ```rs use bevy::winit::{WinitConfig}; // ... .insert_resource(WinitConfig::desktop_app()) // preset // or .insert_resource(WinitConfig::game()) // preset // or .insert_resource(WinitConfig{ .. }) // manual ``` Requesting a redraw: ```rs use bevy::window::RequestRedraw; // ... fn request_redraw(mut event: EventWriter<RequestRedraw>) { event.send(RequestRedraw); } ``` ## Other details - Because we have a single event loop for multiple windows, every time I've mentioned "focused" above, I more precisely mean, "if at least one bevy window is focused". - Due to a platform bug in winit (rust-windowing/winit#1619), we can't simply use `Window::request_redraw()`. As a workaround, this PR will temporarily set the window mode to `Poll` when a redraw is requested. This is then reset to the user's `WinitConfig` setting on the next frame.
- Loading branch information
Showing
8 changed files
with
435 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,89 @@ | ||
use bevy_utils::Duration; | ||
|
||
/// A resource for configuring usage of the `rust_winit` library. | ||
#[derive(Debug, Default)] | ||
pub struct WinitConfig { | ||
/// Configures the winit library to return control to the main thread after | ||
/// the [run](bevy_app::App::run) loop is exited. Winit strongly recommends | ||
/// avoiding this when possible. Before using this please read and understand | ||
/// the [caveats](winit::platform::run_return::EventLoopExtRunReturn::run_return) | ||
/// in the winit documentation. | ||
/// | ||
/// This feature is only available on desktop `target_os` configurations. | ||
/// Namely `windows`, `macos`, `linux`, `dragonfly`, `freebsd`, `netbsd`, and | ||
/// `openbsd`. If set to true on an unsupported platform | ||
/// [run](bevy_app::App::run) will panic. | ||
#[derive(Debug)] | ||
pub struct WinitSettings { | ||
/// Configures the winit library to return control to the main thread after the | ||
/// [run](bevy_app::App::run) loop is exited. Winit strongly recommends avoiding this when | ||
/// possible. Before using this please read and understand the | ||
/// [caveats](winit::platform::run_return::EventLoopExtRunReturn::run_return) in the winit | ||
/// documentation. | ||
/// | ||
/// This feature is only available on desktop `target_os` configurations. Namely `windows`, | ||
/// `macos`, `linux`, `dragonfly`, `freebsd`, `netbsd`, and `openbsd`. If set to true on an | ||
/// unsupported platform [run](bevy_app::App::run) will panic. | ||
pub return_from_run: bool, | ||
/// Configures how the winit event loop updates while the window is focused. | ||
pub focused_mode: UpdateMode, | ||
/// Configures how the winit event loop updates while the window is *not* focused. | ||
pub unfocused_mode: UpdateMode, | ||
} | ||
impl WinitSettings { | ||
/// Configure winit with common settings for a game. | ||
pub fn game() -> Self { | ||
WinitSettings::default() | ||
} | ||
|
||
/// Configure winit with common settings for a desktop application. | ||
pub fn desktop_app() -> Self { | ||
WinitSettings { | ||
focused_mode: UpdateMode::Reactive { | ||
max_wait: Duration::from_secs(5), | ||
}, | ||
unfocused_mode: UpdateMode::ReactiveLowPower { | ||
max_wait: Duration::from_secs(60), | ||
}, | ||
..Default::default() | ||
} | ||
} | ||
|
||
/// Gets the configured `UpdateMode` depending on whether the window is focused or not | ||
pub fn update_mode(&self, focused: bool) -> &UpdateMode { | ||
match focused { | ||
true => &self.focused_mode, | ||
false => &self.unfocused_mode, | ||
} | ||
} | ||
} | ||
impl Default for WinitSettings { | ||
fn default() -> Self { | ||
WinitSettings { | ||
return_from_run: false, | ||
focused_mode: UpdateMode::Continuous, | ||
unfocused_mode: UpdateMode::Continuous, | ||
} | ||
} | ||
} | ||
|
||
/// Configure how the winit event loop should update. | ||
#[derive(Debug)] | ||
pub enum UpdateMode { | ||
/// The event loop will update continuously, running as fast as possible. | ||
Continuous, | ||
/// The event loop will only update if there is a winit event, a redraw is requested, or the | ||
/// maximum wait time has elapsed. | ||
/// | ||
/// ## Note | ||
/// | ||
/// Once the app has executed all bevy systems and reaches the end of the event loop, there is | ||
/// no way to force the app to wake and update again, unless a `winit` event (such as user | ||
/// input, or the window being resized) is received or the time limit is reached. | ||
Reactive { max_wait: Duration }, | ||
/// The event loop will only update if there is a winit event from direct interaction with the | ||
/// window (e.g. mouseover), a redraw is requested, or the maximum wait time has elapsed. | ||
/// | ||
/// ## Note | ||
/// | ||
/// Once the app has executed all bevy systems and reaches the end of the event loop, there is | ||
/// no way to force the app to wake and update again, unless a `winit` event (such as user | ||
/// input, or the window being resized) is received or the time limit is reached. | ||
/// | ||
/// ## Differences from [`UpdateMode::Reactive`] | ||
/// | ||
/// Unlike [`UpdateMode::Reactive`], this mode will ignore winit events that aren't directly | ||
/// caused by interaction with the window. For example, you might want to use this mode when the | ||
/// window is not focused, to only re-draw your bevy app when the cursor is over the window, but | ||
/// not when the mouse moves somewhere else on the screen. This helps to significantly reduce | ||
/// power consumption by only updated the app when absolutely necessary. | ||
ReactiveLowPower { max_wait: Duration }, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.