Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sctk): reduce event spam for redraw requests #180

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions winit/src/platform_specific/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,10 @@
}
}

pub(crate) fn send_ready(&mut self) {
#[cfg(all(feature = "wayland", target_os = "linux"))]
{
self.send_wayland(wayland::Action::Ready);
}
}

pub(crate) fn update_subsurfaces(
&mut self,
id: window::Id,

Check warning on line 64 in winit/src/platform_specific/mod.rs

View workflow job for this annotation

GitHub Actions / web

unused variable: `id`

Check warning on line 64 in winit/src/platform_specific/mod.rs

View workflow job for this annotation

GitHub Actions / wasm

unused variable: `id`

Check warning on line 64 in winit/src/platform_specific/mod.rs

View workflow job for this annotation

GitHub Actions / web

unused variable: `id`

Check warning on line 64 in winit/src/platform_specific/mod.rs

View workflow job for this annotation

GitHub Actions / wasm

unused variable: `id`
window: &dyn winit::window::Window,

Check warning on line 65 in winit/src/platform_specific/mod.rs

View workflow job for this annotation

GitHub Actions / web

unused variable: `window`

Check warning on line 65 in winit/src/platform_specific/mod.rs

View workflow job for this annotation

GitHub Actions / web

unused variable: `window`
) {
#[cfg(all(feature = "wayland", target_os = "linux"))]
{
Expand Down Expand Up @@ -150,7 +143,7 @@

pub(crate) fn handle_event<'a, P, C>(
e: Event,
events: &mut Vec<(Option<window::Id>, iced_runtime::core::Event)>,

Check warning on line 146 in winit/src/platform_specific/mod.rs

View workflow job for this annotation

GitHub Actions / web

unused variable: `events`
platform_specific: &mut PlatformSpecific,
program: &'a P,
compositor: &mut C,
Expand Down
82 changes: 38 additions & 44 deletions winit/src/platform_specific/wayland/event_loop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,50 +103,49 @@ impl SctkEventLoop {
_ = loop_handle
.insert_source(action_rx, |event, _, state| {
match event {
calloop::channel::Event::Msg(e) => match e {
crate::platform_specific::Action::Action(a) => {
if let Err(err) = state.handle_action(a) {
log::warn!("{err:?}");
calloop::channel::Event::Msg(e) => match e {
crate::platform_specific::Action::Action(a) => {
if let Err(err) = state.handle_action(a) {
log::warn!("{err:?}");
}
}
}
crate::platform_specific::Action::TrackWindow(
window,
id,
) => {
state.windows.push(SctkWindow { window, id });
}
crate::Action::RemoveWindow(id) => {
// TODO clean up popups matching the window.
state.windows.retain(|window| id != window.id);
}
crate::platform_specific::Action::SetCursor(icon) => {
if let Some(seat) = state.seats.get_mut(0) {
seat.icon = Some(icon);
seat.set_cursor(&state.connection, icon);
crate::platform_specific::Action::TrackWindow(
window,
id,
) => {
state.windows.push(SctkWindow { window, id });
}
}
crate::platform_specific::Action::RequestRedraw(id) => {
let e = state.frame_status.entry(id).or_insert(FrameStatus::RequestedRedraw);
if matches!(e, FrameStatus::Received) {
*e = FrameStatus::Ready;
crate::Action::RemoveWindow(id) => {
// TODO clean up popups matching the window.
state.windows.retain(|window| id != window.id);
}
crate::platform_specific::Action::SetCursor(
icon,
) => {
if let Some(seat) = state.seats.get_mut(0) {
seat.icon = Some(icon);
seat.set_cursor(&state.connection, icon);
}
}
crate::platform_specific::Action::RequestRedraw(
id,
) => {
let e = state
.frame_status
.entry(id)
.or_insert(FrameStatus::RequestedRedraw);
if matches!(e, FrameStatus::Received) {
*e = FrameStatus::Ready;
}
}
crate::Action::Dropped(id) => {
_ = state.destroyed.remove(&id.inner());
}
},
calloop::channel::Event::Closed => {
log::info!("Calloop channel closed.");
}
crate::platform_specific::Action::PrePresentNotify(
_,
) => {
// TODO
}
crate::platform_specific::Action::Ready => {
state.ready = true;
}
crate::Action::Dropped(id) => {
_ = state.destroyed.remove(&id.inner());
}
},
calloop::channel::Event::Closed => {
log::info!("Calloop channel closed.");
}
}
})
.unwrap();
let wayland_source =
Expand Down Expand Up @@ -227,7 +226,6 @@ impl SctkEventLoop {
proxy,
id_map: Default::default(),
to_commit: HashMap::new(),
ready: true,
destroyed: HashSet::new(),
pending_popup: Default::default(),
activation_token_ctr: 0,
Expand Down Expand Up @@ -305,9 +303,6 @@ impl SctkEventLoop {
}
}
}
if !state.state.ready {
continue;
}

if let Err(err) =
state.event_loop.dispatch(None, &mut state.state)
Expand Down Expand Up @@ -370,7 +365,6 @@ impl SctkEventLoop {
);
}
}

if wake_up {
state.state.proxy.wake_up();
}
Expand Down
1 change: 0 additions & 1 deletion winit/src/platform_specific/wayland/event_loop/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,6 @@ pub struct SctkState {
pub(crate) id_map: HashMap<ObjectId, core::window::Id>,
pub(crate) to_commit: HashMap<core::window::Id, WlSurface>,
pub(crate) destroyed: HashSet<core::window::Id>,
pub(crate) ready: bool,
pub(crate) pending_popup: Option<(SctkPopupSettings, usize)>,

pub(crate) activation_token_ctr: u32,
Expand Down
6 changes: 0 additions & 6 deletions winit/src/platform_specific/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ pub(crate) enum Action {
Action(iced_runtime::platform_specific::wayland::Action),
SetCursor(CursorIcon),
RequestRedraw(ObjectId),
PrePresentNotify(ObjectId),
TrackWindow(Arc<dyn winit::window::Window>, window::Id),
RemoveWindow(window::Id),
Dropped(SurfaceIdWrapper),
Ready,
}

impl std::fmt::Debug for Action {
Expand All @@ -45,16 +43,12 @@ impl std::fmt::Debug for Action {
Self::RequestRedraw(arg0) => {
f.debug_tuple("RequestRedraw").field(arg0).finish()
}
Self::PrePresentNotify(arg0) => {
f.debug_tuple("PrePresentNotify").field(arg0).finish()
}
Self::TrackWindow(_arg0, arg1) => {
f.debug_tuple("TrackWindow").field(arg1).finish()
}
Self::RemoveWindow(arg0) => {
f.debug_tuple("RemoveWindow").field(arg0).finish()
}
Self::Ready => write!(f, "Ready"),
Self::Dropped(_surface_id_wrapper) => write!(f, "Dropped"),
}
}
Expand Down
24 changes: 2 additions & 22 deletions winit/src/platform_specific/wayland/winit_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use crate::platform_specific::wayland::Action;
use raw_window_handle::HandleError;
use sctk::reexports::{
calloop::channel,
client::{
protocol::{wl_display::WlDisplay, wl_surface::WlSurface},
Proxy, QueueHandle,
},
client::{protocol::wl_display::WlDisplay, Proxy, QueueHandle},
};
use std::sync::{Arc, Mutex};
use winit::{
Expand All @@ -16,19 +13,7 @@ use winit::{

use crate::platform_specific::SurfaceIdWrapper;

use super::event_loop::state::{
Common, CommonSurface, SctkLayerSurface, SctkLockSurface, SctkPopup,
SctkState, TOKEN_CTR,
};

#[derive(Debug)]
pub(crate) enum Surface {
Popup(SctkPopup),
Layer(SctkLayerSurface),
Lock(SctkLockSurface),
}

impl Surface {}
use super::event_loop::state::{Common, CommonSurface, SctkState, TOKEN_CTR};

pub struct SctkWinitWindow {
tx: channel::Sender<Action>,
Expand All @@ -37,7 +22,6 @@ pub struct SctkWinitWindow {
common: Arc<Mutex<Common>>,
display: WlDisplay,
pub(crate) queue_handle: QueueHandle<SctkState>,
wait_redraw: bool,
}

impl Drop for SctkWinitWindow {
Expand All @@ -62,7 +46,6 @@ impl SctkWinitWindow {
surface,
display,
queue_handle,
wait_redraw: false,
})
}
}
Expand All @@ -87,9 +70,6 @@ impl winit::window::Window for SctkWinitWindow {
fn pre_present_notify(&self) {
let surface = self.surface.wl_surface();
_ = surface.frame(&self.queue_handle, surface.clone());
_ = self
.tx
.send(Action::PrePresentNotify(self.surface.wl_surface().id()));
}

fn set_cursor(&self, cursor: winit::window::Cursor) {
Expand Down
3 changes: 2 additions & 1 deletion winit/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use crate::futures::{Executor, Runtime};
use crate::graphics;
use crate::graphics::{compositor, Compositor};
use crate::platform_specific;

Check warning on line 29 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

unused import: `crate::platform_specific`

Check warning on line 29 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

unused import: `crate::platform_specific`

Check warning on line 29 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

unused import: `crate::platform_specific`

Check warning on line 29 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

unused import: `crate::platform_specific`
use crate::runtime::user_interface::{self, UserInterface};
use crate::runtime::Debug;
use crate::runtime::{self, Action, Task};
Expand Down Expand Up @@ -378,8 +378,8 @@

#[cfg(target_arch = "wasm32")]
{
use winit::platform::web::WindowExtWebSys;

Check failure on line 381 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

unresolved import `winit::platform::web::WindowExtWebSys`

Check failure on line 381 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

unresolved import `winit::platform::web::WindowExtWebSys`

Check failure on line 381 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

unresolved import `winit::platform::web::WindowExtWebSys`

Check failure on line 381 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

unresolved import `winit::platform::web::WindowExtWebSys`
self.canvas = window.canvas();

Check failure on line 382 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

no method named `canvas` found for struct `std::sync::Arc<dyn winit::window::Window>` in the current scope

Check failure on line 382 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

no method named `canvas` found for struct `std::sync::Arc<dyn winit::window::Window>` in the current scope

Check failure on line 382 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

no method named `canvas` found for struct `std::sync::Arc<dyn winit::window::Window>` in the current scope

Check failure on line 382 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

no method named `canvas` found for struct `std::sync::Arc<dyn winit::window::Window>` in the current scope
}

let finish_boot = async move {
Expand Down Expand Up @@ -495,14 +495,14 @@

#[cfg(target_arch = "wasm32")]
let window_attributes = {
use winit::platform::web::WindowAttributesExtWebSys;

Check failure on line 498 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

unresolved import `winit::platform::web::WindowAttributesExtWebSys`

Check failure on line 498 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

unresolved import `winit::platform::web::WindowAttributesExtWebSys`

Check failure on line 498 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

unresolved import `winit::platform::web::WindowAttributesExtWebSys`

Check failure on line 498 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

unresolved import `winit::platform::web::WindowAttributesExtWebSys`
window_attributes
.with_canvas(self.canvas.take())

Check failure on line 500 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

no method named `with_canvas` found for struct `WindowAttributes` in the current scope

Check failure on line 500 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

no method named `with_canvas` found for struct `WindowAttributes` in the current scope

Check failure on line 500 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

no method named `with_canvas` found for struct `WindowAttributes` in the current scope

Check failure on line 500 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

no method named `with_canvas` found for struct `WindowAttributes` in the current scope
};

log::info!("Window attributes for id `{id:#?}`: {window_attributes:#?}");

let window = Arc::from(

Check failure on line 505 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

type annotations needed for `std::sync::Arc<_, _>`

Check failure on line 505 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

type annotations needed for `std::sync::Arc<_, _>`

Check failure on line 505 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

type annotations needed for `std::sync::Arc<_, _>`

Check failure on line 505 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

type annotations needed for `std::sync::Arc<_, _>`
event_loop
.create_window(window_attributes)
.expect("Create window"),
Expand All @@ -510,7 +510,7 @@

#[cfg(target_arch = "wasm32")]
{
use winit::platform::web::WindowExtWebSys;

Check failure on line 513 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

unresolved import `winit::platform::web::WindowExtWebSys`

Check failure on line 513 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

unresolved import `winit::platform::web::WindowExtWebSys`

Check failure on line 513 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

unresolved import `winit::platform::web::WindowExtWebSys`

Check failure on line 513 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

unresolved import `winit::platform::web::WindowExtWebSys`

let canvas = window
.canvas()
Expand Down Expand Up @@ -630,8 +630,8 @@

#[cfg(target_arch = "wasm32")]
{
use winit::platform::web::EventLoopExtWebSys;

Check failure on line 633 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

unresolved import `winit::platform::web::EventLoopExtWebSys`

Check failure on line 633 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

unresolved import `winit::platform::web::EventLoopExtWebSys`

Check failure on line 633 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

unresolved import `winit::platform::web::EventLoopExtWebSys`

Check failure on line 633 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

unresolved import `winit::platform::web::EventLoopExtWebSys`
let _ = event_loop.spawn_app(runner);

Check failure on line 634 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

no method named `spawn_app` found for struct `EventLoop` in the current scope

Check failure on line 634 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

no method named `spawn_app` found for struct `EventLoop` in the current scope

Check failure on line 634 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

no method named `spawn_app` found for struct `EventLoop` in the current scope

Check failure on line 634 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

no method named `spawn_app` found for struct `EventLoop` in the current scope

Ok(())
}
Expand Down Expand Up @@ -693,7 +693,7 @@
boot: oneshot::Receiver<Boot<C>>,
mut event_receiver: mpsc::UnboundedReceiver<Event<P::Message>>,
mut control_sender: mpsc::UnboundedSender<Control>,
display_handle: OwnedDisplayHandle,

Check warning on line 696 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

unused variable: `display_handle`

Check warning on line 696 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

unused variable: `display_handle`

Check warning on line 696 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

unused variable: `display_handle`

Check warning on line 696 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

unused variable: `display_handle`
is_daemon: bool,
) where
P: Program + 'static,
Expand All @@ -705,7 +705,7 @@

let Boot {
mut compositor,
is_wayland,

Check warning on line 708 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

unused variable: `is_wayland`

Check warning on line 708 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

unused variable: `is_wayland`

Check warning on line 708 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

unused variable: `is_wayland`

Check warning on line 708 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

unused variable: `is_wayland`
} = boot.await.expect("Receive boot");

let mut platform_specific_handler =
Expand Down Expand Up @@ -795,7 +795,6 @@
let event = if let Ok(event) = event_receiver.try_next() {
event
} else {
platform_specific_handler.send_ready();
event_receiver.next().await
};

Expand Down Expand Up @@ -1135,6 +1134,8 @@
continue;
};

window.redraw_requested = false;

// TODO: Avoid redrawing all the time by forcing widgets to
// request redraws on state changes
//
Expand Down Expand Up @@ -1230,7 +1231,7 @@
{
let logical_size = window.state.logical_size();
debug.layout_started();
let mut ui = user_interfaces

Check warning on line 1234 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

variable does not need to be mutable

Check warning on line 1234 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

variable does not need to be mutable

Check warning on line 1234 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

variable does not need to be mutable

Check warning on line 1234 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

variable does not need to be mutable
.remove(&id)
.expect("Remove user interface")
.relayout(logical_size, &mut window.renderer);
Expand Down Expand Up @@ -1500,7 +1501,7 @@
}
}
}
_ => {}

Check warning on line 1504 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

unreachable pattern

Check warning on line 1504 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

unreachable pattern

Check warning on line 1504 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / web

unreachable pattern

Check warning on line 1504 in winit/src/program.rs

View workflow job for this annotation

GitHub Actions / wasm

unreachable pattern
}
}
Event::AboutToWait => {
Expand Down
7 changes: 6 additions & 1 deletion winit/src/program/window_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ where
mouse_interaction: mouse::Interaction::None,
prev_dnd_destination_rectangles_count: 0,
resize_enabled: false,
redraw_requested: false,
},
);

Expand Down Expand Up @@ -167,6 +168,7 @@ where
pub surface: C::Surface,
pub renderer: P::Renderer,
pub resize_enabled: bool,
pub(crate) redraw_requested: bool,
}

impl<P, C> Window<P, C>
Expand All @@ -193,6 +195,9 @@ where
}

pub fn request_redraw(&mut self) {
self.raw.request_redraw();
if !self.redraw_requested {
self.redraw_requested = true;
self.raw.request_redraw();
}
}
}
Loading