Skip to content

Commit 2d674e7

Browse files
committed
Reduce power usage with configurable event loop (#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.
1 parent cba9bcc commit 2d674e7

File tree

8 files changed

+435
-43
lines changed

8 files changed

+435
-43
lines changed

Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ bevy_dylib = { path = "crates/bevy_dylib", version = "0.6.0", default-features =
9898
bevy_internal = { path = "crates/bevy_internal", version = "0.6.0", default-features = false }
9999

100100
[target.'cfg(target_arch = "wasm32")'.dependencies]
101-
bevy_internal = { path = "crates/bevy_internal", version = "0.6.0", default-features = false, features = ["webgl"] }
101+
bevy_internal = { path = "crates/bevy_internal", version = "0.6.0", default-features = false, features = [
102+
"webgl",
103+
] }
102104

103105
[dev-dependencies]
104106
anyhow = "1.0.4"
@@ -522,6 +524,10 @@ path = "examples/ui/ui.rs"
522524
name = "clear_color"
523525
path = "examples/window/clear_color.rs"
524526

527+
[[example]]
528+
name = "low_power"
529+
path = "examples/window/low_power.rs"
530+
525531
[[example]]
526532
name = "multiple_windows"
527533
path = "examples/window/multiple_windows.rs"

crates/bevy_window/src/event.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ pub struct CreateWindow {
2020
pub descriptor: WindowDescriptor,
2121
}
2222

23+
/// An event that indicates the window should redraw, even if its control flow is set to `Wait` and
24+
/// there have been no window events.
25+
#[derive(Debug, Clone)]
26+
pub struct RequestRedraw;
27+
2328
/// An event that indicates a window should be closed.
2429
#[derive(Debug, Clone)]
2530
pub struct CloseWindow {

crates/bevy_window/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ impl Plugin for WindowPlugin {
4343
.add_event::<CreateWindow>()
4444
.add_event::<WindowCreated>()
4545
.add_event::<WindowCloseRequested>()
46+
.add_event::<RequestRedraw>()
4647
.add_event::<CloseWindow>()
4748
.add_event::<CursorMoved>()
4849
.add_event::<CursorEntered>()

crates/bevy_winit/src/lib.rs

Lines changed: 110 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@ use bevy_ecs::{
1717
world::World,
1818
};
1919
use bevy_math::{ivec2, DVec2, Vec2};
20-
use bevy_utils::tracing::{error, trace, warn};
20+
use bevy_utils::{
21+
tracing::{error, trace, warn},
22+
Instant,
23+
};
2124
use bevy_window::{
2225
CreateWindow, CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, ReceivedCharacter,
23-
WindowBackendScaleFactorChanged, WindowCloseRequested, WindowCreated, WindowFocused,
24-
WindowMoved, WindowResized, WindowScaleFactorChanged, Windows,
26+
RequestRedraw, WindowBackendScaleFactorChanged, WindowCloseRequested, WindowCreated,
27+
WindowFocused, WindowMoved, WindowResized, WindowScaleFactorChanged, Windows,
2528
};
2629
use winit::{
2730
dpi::PhysicalPosition,
28-
event::{self, DeviceEvent, Event, WindowEvent},
31+
event::{self, DeviceEvent, Event, StartCause, WindowEvent},
2932
event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
3033
};
3134

@@ -37,6 +40,7 @@ pub struct WinitPlugin;
3740
impl Plugin for WinitPlugin {
3841
fn build(&self, app: &mut App) {
3942
app.init_non_send_resource::<WinitWindows>()
43+
.init_resource::<WinitSettings>()
4044
.set_runner(winit_runner)
4145
.add_system_to_stage(CoreStage::PostUpdate, change_window.exclusive_system());
4246
let event_loop = EventLoop::new();
@@ -227,41 +231,71 @@ pub fn winit_runner(app: App) {
227231
// winit_runner_with(app, EventLoop::new_any_thread());
228232
// }
229233

234+
/// Stores state that must persist between frames.
235+
struct WinitPersistentState {
236+
/// Tracks whether or not the application is active or suspended.
237+
active: bool,
238+
/// Tracks whether or not an event has occurred this frame that would trigger an update in low
239+
/// power mode. Should be reset at the end of every frame.
240+
low_power_event: bool,
241+
/// Tracks whether the event loop was started this frame because of a redraw request.
242+
redraw_request_sent: bool,
243+
/// Tracks if the event loop was started this frame because of a `WaitUntil` timeout.
244+
timeout_reached: bool,
245+
last_update: Instant,
246+
}
247+
impl Default for WinitPersistentState {
248+
fn default() -> Self {
249+
Self {
250+
active: true,
251+
low_power_event: false,
252+
redraw_request_sent: false,
253+
timeout_reached: false,
254+
last_update: Instant::now(),
255+
}
256+
}
257+
}
258+
230259
pub fn winit_runner_with(mut app: App) {
231260
let mut event_loop = app
232261
.world
233262
.remove_non_send_resource::<EventLoop<()>>()
234263
.unwrap();
235264
let mut create_window_event_reader = ManualEventReader::<CreateWindow>::default();
236265
let mut app_exit_event_reader = ManualEventReader::<AppExit>::default();
266+
let mut redraw_event_reader = ManualEventReader::<RequestRedraw>::default();
267+
let mut winit_state = WinitPersistentState::default();
237268
app.world
238269
.insert_non_send_resource(event_loop.create_proxy());
239270

271+
let return_from_run = app.world.resource::<WinitSettings>().return_from_run;
240272
trace!("Entering winit event loop");
241273

242-
let should_return_from_run = app
243-
.world
244-
.get_resource::<WinitConfig>()
245-
.map_or(false, |config| config.return_from_run);
246-
247-
let mut active = true;
248-
249274
let event_handler = move |event: Event<()>,
250275
event_loop: &EventLoopWindowTarget<()>,
251276
control_flow: &mut ControlFlow| {
252-
*control_flow = ControlFlow::Poll;
253-
254-
if let Some(app_exit_events) = app.world.get_resource_mut::<Events<AppExit>>() {
255-
if app_exit_event_reader
256-
.iter(&app_exit_events)
257-
.next_back()
258-
.is_some()
259-
{
260-
*control_flow = ControlFlow::Exit;
261-
}
262-
}
263-
264277
match event {
278+
event::Event::NewEvents(start) => {
279+
let winit_config = app.world.resource::<WinitSettings>();
280+
let windows = app.world.resource::<Windows>();
281+
let focused = windows.iter().any(|w| w.is_focused());
282+
// Check if either the `WaitUntil` timeout was triggered by winit, or that same
283+
// amount of time has elapsed since the last app update. This manual check is needed
284+
// because we don't know if the criteria for an app update were met until the end of
285+
// the frame.
286+
let auto_timeout_reached = matches!(start, StartCause::ResumeTimeReached { .. });
287+
let now = Instant::now();
288+
let manual_timeout_reached = match winit_config.update_mode(focused) {
289+
UpdateMode::Continuous => false,
290+
UpdateMode::Reactive { max_wait }
291+
| UpdateMode::ReactiveLowPower { max_wait } => {
292+
now.duration_since(winit_state.last_update) >= *max_wait
293+
}
294+
};
295+
// The low_power_event state and timeout must be reset at the start of every frame.
296+
winit_state.low_power_event = false;
297+
winit_state.timeout_reached = auto_timeout_reached || manual_timeout_reached;
298+
}
265299
event::Event::WindowEvent {
266300
event,
267301
window_id: winit_window_id,
@@ -287,6 +321,7 @@ pub fn winit_runner_with(mut app: App) {
287321
warn!("Skipped event for unknown Window Id {:?}", winit_window_id);
288322
return;
289323
};
324+
winit_state.low_power_event = true;
290325

291326
match event {
292327
WindowEvent::Resized(size) => {
@@ -497,25 +532,73 @@ pub fn winit_runner_with(mut app: App) {
497532
});
498533
}
499534
event::Event::Suspended => {
500-
active = false;
535+
winit_state.active = false;
501536
}
502537
event::Event::Resumed => {
503-
active = true;
538+
winit_state.active = true;
504539
}
505540
event::Event::MainEventsCleared => {
506541
handle_create_window_events(
507542
&mut app.world,
508543
event_loop,
509544
&mut create_window_event_reader,
510545
);
511-
if active {
546+
let winit_config = app.world.resource::<WinitSettings>();
547+
let update = if winit_state.active {
548+
let windows = app.world.resource::<Windows>();
549+
let focused = windows.iter().any(|w| w.is_focused());
550+
match winit_config.update_mode(focused) {
551+
UpdateMode::Continuous | UpdateMode::Reactive { .. } => true,
552+
UpdateMode::ReactiveLowPower { .. } => {
553+
winit_state.low_power_event
554+
|| winit_state.redraw_request_sent
555+
|| winit_state.timeout_reached
556+
}
557+
}
558+
} else {
559+
false
560+
};
561+
if update {
562+
winit_state.last_update = Instant::now();
512563
app.update();
513564
}
514565
}
566+
Event::RedrawEventsCleared => {
567+
{
568+
let winit_config = app.world.resource::<WinitSettings>();
569+
let windows = app.world.resource::<Windows>();
570+
let focused = windows.iter().any(|w| w.is_focused());
571+
let now = Instant::now();
572+
use UpdateMode::*;
573+
*control_flow = match winit_config.update_mode(focused) {
574+
Continuous => ControlFlow::Poll,
575+
Reactive { max_wait } | ReactiveLowPower { max_wait } => {
576+
ControlFlow::WaitUntil(now + *max_wait)
577+
}
578+
};
579+
}
580+
// This block needs to run after `app.update()` in `MainEventsCleared`. Otherwise,
581+
// we won't be able to see redraw requests until the next event, defeating the
582+
// purpose of a redraw request!
583+
let mut redraw = false;
584+
if let Some(app_redraw_events) = app.world.get_resource::<Events<RequestRedraw>>() {
585+
if redraw_event_reader.iter(app_redraw_events).last().is_some() {
586+
*control_flow = ControlFlow::Poll;
587+
redraw = true;
588+
}
589+
}
590+
if let Some(app_exit_events) = app.world.get_resource::<Events<AppExit>>() {
591+
if app_exit_event_reader.iter(app_exit_events).last().is_some() {
592+
*control_flow = ControlFlow::Exit;
593+
}
594+
}
595+
winit_state.redraw_request_sent = redraw;
596+
}
515597
_ => (),
516598
}
517599
};
518-
if should_return_from_run {
600+
601+
if return_from_run {
519602
run_return(&mut event_loop, event_handler);
520603
} else {
521604
run(event_loop, event_handler);

crates/bevy_winit/src/winit_config.rs

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,89 @@
1+
use bevy_utils::Duration;
2+
13
/// A resource for configuring usage of the `rust_winit` library.
2-
#[derive(Debug, Default)]
3-
pub struct WinitConfig {
4-
/// Configures the winit library to return control to the main thread after
5-
/// the [run](bevy_app::App::run) loop is exited. Winit strongly recommends
6-
/// avoiding this when possible. Before using this please read and understand
7-
/// the [caveats](winit::platform::run_return::EventLoopExtRunReturn::run_return)
8-
/// in the winit documentation.
9-
///
10-
/// This feature is only available on desktop `target_os` configurations.
11-
/// Namely `windows`, `macos`, `linux`, `dragonfly`, `freebsd`, `netbsd`, and
12-
/// `openbsd`. If set to true on an unsupported platform
13-
/// [run](bevy_app::App::run) will panic.
4+
#[derive(Debug)]
5+
pub struct WinitSettings {
6+
/// Configures the winit library to return control to the main thread after the
7+
/// [run](bevy_app::App::run) loop is exited. Winit strongly recommends avoiding this when
8+
/// possible. Before using this please read and understand the
9+
/// [caveats](winit::platform::run_return::EventLoopExtRunReturn::run_return) in the winit
10+
/// documentation.
11+
///
12+
/// This feature is only available on desktop `target_os` configurations. Namely `windows`,
13+
/// `macos`, `linux`, `dragonfly`, `freebsd`, `netbsd`, and `openbsd`. If set to true on an
14+
/// unsupported platform [run](bevy_app::App::run) will panic.
1415
pub return_from_run: bool,
16+
/// Configures how the winit event loop updates while the window is focused.
17+
pub focused_mode: UpdateMode,
18+
/// Configures how the winit event loop updates while the window is *not* focused.
19+
pub unfocused_mode: UpdateMode,
20+
}
21+
impl WinitSettings {
22+
/// Configure winit with common settings for a game.
23+
pub fn game() -> Self {
24+
WinitSettings::default()
25+
}
26+
27+
/// Configure winit with common settings for a desktop application.
28+
pub fn desktop_app() -> Self {
29+
WinitSettings {
30+
focused_mode: UpdateMode::Reactive {
31+
max_wait: Duration::from_secs(5),
32+
},
33+
unfocused_mode: UpdateMode::ReactiveLowPower {
34+
max_wait: Duration::from_secs(60),
35+
},
36+
..Default::default()
37+
}
38+
}
39+
40+
/// Gets the configured `UpdateMode` depending on whether the window is focused or not
41+
pub fn update_mode(&self, focused: bool) -> &UpdateMode {
42+
match focused {
43+
true => &self.focused_mode,
44+
false => &self.unfocused_mode,
45+
}
46+
}
47+
}
48+
impl Default for WinitSettings {
49+
fn default() -> Self {
50+
WinitSettings {
51+
return_from_run: false,
52+
focused_mode: UpdateMode::Continuous,
53+
unfocused_mode: UpdateMode::Continuous,
54+
}
55+
}
56+
}
57+
58+
/// Configure how the winit event loop should update.
59+
#[derive(Debug)]
60+
pub enum UpdateMode {
61+
/// The event loop will update continuously, running as fast as possible.
62+
Continuous,
63+
/// The event loop will only update if there is a winit event, a redraw is requested, or the
64+
/// maximum wait time has elapsed.
65+
///
66+
/// ## Note
67+
///
68+
/// Once the app has executed all bevy systems and reaches the end of the event loop, there is
69+
/// no way to force the app to wake and update again, unless a `winit` event (such as user
70+
/// input, or the window being resized) is received or the time limit is reached.
71+
Reactive { max_wait: Duration },
72+
/// The event loop will only update if there is a winit event from direct interaction with the
73+
/// window (e.g. mouseover), a redraw is requested, or the maximum wait time has elapsed.
74+
///
75+
/// ## Note
76+
///
77+
/// Once the app has executed all bevy systems and reaches the end of the event loop, there is
78+
/// no way to force the app to wake and update again, unless a `winit` event (such as user
79+
/// input, or the window being resized) is received or the time limit is reached.
80+
///
81+
/// ## Differences from [`UpdateMode::Reactive`]
82+
///
83+
/// Unlike [`UpdateMode::Reactive`], this mode will ignore winit events that aren't directly
84+
/// caused by interaction with the window. For example, you might want to use this mode when the
85+
/// window is not focused, to only re-draw your bevy app when the cursor is over the window, but
86+
/// not when the mouse moves somewhere else on the screen. This helps to significantly reduce
87+
/// power consumption by only updated the app when absolutely necessary.
88+
ReactiveLowPower { max_wait: Duration },
1589
}

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ Example | File | Description
261261
Example | File | Description
262262
--- | --- | ---
263263
`clear_color` | [`window/clear_color.rs`](./window/clear_color.rs) | Creates a solid color window
264+
`low_power` | [`window/low_power.rs`](./window/low_power.rs) | Demonstrates settings to reduce power use for bevy applications
264265
`multiple_windows` | [`window/multiple_windows.rs`](./window/multiple_windows.rs) | Demonstrates creating multiple windows, and rendering to them
265266
`scale_factor_override` | [`window/scale_factor_override.rs`](./window/scale_factor_override.rs) | Illustrates how to customize the default window settings
266267
`transparent_window` | [`window/transparent_window.rs`](./window/transparent_window.rs) | Illustrates making the window transparent and hiding the window decoration

examples/app/return_after_run.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
use bevy::{prelude::*, winit::WinitConfig};
1+
use bevy::{prelude::*, winit::WinitSettings};
22

33
fn main() {
44
println!("Running first App.");
55
App::new()
6-
.insert_resource(WinitConfig {
6+
.insert_resource(WinitSettings {
77
return_from_run: true,
8+
..default()
89
})
910
.insert_resource(ClearColor(Color::rgb(0.2, 0.2, 0.8)))
1011
.add_plugins(DefaultPlugins)
1112
.add_system(system1)
1213
.run();
1314
println!("Running another App.");
1415
App::new()
15-
.insert_resource(WinitConfig {
16+
.insert_resource(WinitSettings {
1617
return_from_run: true,
18+
..default()
1719
})
1820
.insert_resource(ClearColor(Color::rgb(0.2, 0.8, 0.2)))
1921
.add_plugins_with(DefaultPlugins, |group| {

0 commit comments

Comments
 (0)