bevy/examples/app
Aevyrie 2d674e7c3e 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:🪟: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 (https://github.com/rust-windowing/winit/issues/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.
2022-03-07 23:32:05 +00:00
..
custom_loop.rs Make get_resource (and friends) infallible (#4047) 2022-02-27 22:37:18 +00:00
drag_and_drop.rs Down with the system! (#2496) 2021-07-27 23:42:36 +00:00
empty.rs Merge AppBuilder into App (#2531) 2021-07-27 20:21:06 +00:00
empty_defaults.rs Merge AppBuilder into App (#2531) 2021-07-27 20:21:06 +00:00
headless.rs Down with the system! (#2496) 2021-07-27 23:42:36 +00:00
headless_defaults.rs default() shorthand (#4071) 2022-03-01 20:52:09 +00:00
logs.rs Down with the system! (#2496) 2021-07-27 23:42:36 +00:00
plugin.rs Down with the system! (#2496) 2021-07-27 23:42:36 +00:00
plugin_group.rs Fix doc_markdown lints in examples (#3486) 2021-12-29 17:25:34 +00:00
return_after_run.rs Reduce power usage with configurable event loop (#3974) 2022-03-07 23:32:05 +00:00
thread_pool_resources.rs Merge AppBuilder into App (#2531) 2021-07-27 20:21:06 +00:00
without_winit.rs Fix crash with disabled winit (#3330) 2021-12-15 00:15:47 +00:00