Redraw continuously instead of busy-waiting at 100% CPU
Previously, Pteropus would only redraw when the window was resized, but it would continuously poll for events, keeping a CPU core at 100% even when not doing anything. It now automatically queues a new RedrawRequested event after each draw finishes. The view() call will also limit the frame rate to the video refresh rate and block when there are more than three frames queued up, so we're not busywaiting any more.
This commit is contained in:
parent
b870e03812
commit
35785dbf01
|
|
@ -106,6 +106,7 @@ impl MvuApp<Model> for App {
|
||||||
let mut config = surface
|
let mut config = surface
|
||||||
.get_default_config(&adapter, size.width, size.height)
|
.get_default_config(&adapter, size.width, size.height)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
config.present_mode = wgpu::PresentMode::AutoVsync;
|
||||||
config.view_formats.push(config.format);
|
config.view_formats.push(config.format);
|
||||||
surface.configure(&device, &config);
|
surface.configure(&device, &config);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,10 @@ where
|
||||||
block_on(self.app.init(&instance, surface, Size2i { width, height }));
|
block_on(self.app.init(&instance, surface, Size2i { width, height }));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: The idea with these `block_on()` calls is that eventually I'll
|
||||||
|
// write an async executor that runs on top of the winit even loop and then
|
||||||
|
// this stuff can be properly async.
|
||||||
fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
|
fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
|
||||||
match event {
|
match event {
|
||||||
WindowEvent::Resized(new_size) => block_on(self.app.resize(Size2i {
|
WindowEvent::Resized(new_size) => block_on(self.app.resize(Size2i {
|
||||||
|
|
@ -55,7 +59,10 @@ where
|
||||||
println!("The close button was pressed; stopping");
|
println!("The close button was pressed; stopping");
|
||||||
event_loop.exit();
|
event_loop.exit();
|
||||||
}
|
}
|
||||||
WindowEvent::RedrawRequested => block_on(self.app.view(self.model.clone())).unwrap(),
|
WindowEvent::RedrawRequested => {
|
||||||
|
block_on(self.app.view(self.model.clone())).unwrap();
|
||||||
|
self.window.as_ref().unwrap().request_redraw();
|
||||||
|
},
|
||||||
WindowEvent::DroppedFile(file_path) => {
|
WindowEvent::DroppedFile(file_path) => {
|
||||||
self.model = block_on(
|
self.model = block_on(
|
||||||
self.app
|
self.app
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue