Skip to content

Commit

Permalink
winit: avoid to poll or call request_redraw too often
Browse files Browse the repository at this point in the history
Otherwise there will be a huge queue of request redraw command that
accumulate within winit.
This makes operation such as resizing a bit more soomth

Tested on X11
  • Loading branch information
ogoffart committed Jan 22, 2024
1 parent c7aae4b commit 9113b87
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
30 changes: 8 additions & 22 deletions internal/backends/winit/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ impl EventLoopState {
fn process_window_event(&mut self, window: Rc<WinitWindowAdapter>, event: WindowEvent) {
let runtime_window = WindowInner::from_pub(window.window());
match event {
WindowEvent::RedrawRequested => self.loop_error = window.draw().err(),
WindowEvent::Resized(size) => {
self.loop_error = window.resize_event(size).err();
}
Expand Down Expand Up @@ -428,14 +429,6 @@ impl EventLoopState {
use winit::event_loop::ControlFlow;

match event {
Event::WindowEvent { event: WindowEvent::RedrawRequested, window_id: id } => {
if let Some(window) = window_by_id(id) {
if let Err(rendering_error) = window.draw() {
self.loop_error = Some(rendering_error)
}
}
}

Event::WindowEvent { event, window_id } => {
if let Some(window) = window_by_id(window_id) {
#[cfg(enable_accesskit)]
Expand Down Expand Up @@ -478,21 +471,14 @@ impl EventLoopState {
}),

Event::AboutToWait => {
if !event_loop_target.exiting()
&& ALL_WINDOWS.with(|windows| {
windows.borrow().iter().any(|(_, w)| {
w.upgrade()
.and_then(|w| {
w.window().has_active_animations().then(|| {
w.request_redraw();
true
})
})
.unwrap_or_default()
})
if !event_loop_target.exiting() {
ALL_WINDOWS.with(|windows| {
for w in windows.borrow().iter().filter_map(|(_, w)| w.upgrade()) {
if w.window().has_active_animations() {
w.request_redraw();
}
}
})
{
event_loop_target.set_control_flow(ControlFlow::Poll);
}

if event_loop_target.control_flow() == ControlFlow::Wait {
Expand Down
5 changes: 3 additions & 2 deletions internal/backends/winit/winitwindowadapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,9 @@ impl WindowAdapter for WinitWindowAdapter {
}

fn request_redraw(&self) {
self.pending_redraw.set(true);
self.with_window_handle(&mut |window| window.request_redraw())
if !self.pending_redraw.replace(true) {
self.winit_window.request_redraw()
}
}

#[allow(clippy::unnecessary_cast)] // Coord is used!
Expand Down

0 comments on commit 9113b87

Please sign in to comment.