Redraw on demand rather than continuously
Browse files at this commit9b073b2350d00dfdb32a4983a102c4bde843c92cParent: ed613265 changed files +57 -31
- src/handlers/compositor.rs +2 -0
- src/input.rs +6 -0
- src/niri.rs +34 -1
- src/tty.rs +6 -22
- src/winit.rs +9 -8
src/handlers/compositor.rsmodified+2 -0
| @@ -41,6 +41,8 @@ impl CompositorHandler for Niri { | |||
| 41 | 41 | ||
| 42 | xdg_shell::handle_commit(&self.space, surface); | 42 | xdg_shell::handle_commit(&self.space, surface); |
| 43 | resize_grab::handle_commit(&mut self.space, surface); | 43 | resize_grab::handle_commit(&mut self.space, surface); |
| 44 | + | ||
| 45 | + self.queue_redraw(); | ||
| 44 | } | 46 | } |
| 45 | } | 47 | } |
| 46 | 48 | ||
src/input.rsmodified+6 -0
| @@ -89,6 +89,9 @@ impl Niri { | |||
| 89 | utime: event.time(), | 89 | utime: event.time(), |
| 90 | }, | 90 | }, |
| 91 | ); | 91 | ); |
| 92 | + | ||
| 93 | + // Redraw to update the cursor position. | ||
| 94 | + self.queue_redraw(); | ||
| 92 | } | 95 | } |
| 93 | InputEvent::PointerMotionAbsolute { event, .. } => { | 96 | InputEvent::PointerMotionAbsolute { event, .. } => { |
| 94 | let output = self.space.outputs().next().unwrap(); | 97 | let output = self.space.outputs().next().unwrap(); |
| @@ -112,6 +115,9 @@ impl Niri { | |||
| 112 | time: event.time_msec(), | 115 | time: event.time_msec(), |
| 113 | }, | 116 | }, |
| 114 | ); | 117 | ); |
| 118 | + | ||
| 119 | + // Redraw to update the cursor position. | ||
| 120 | + self.queue_redraw(); | ||
| 115 | } | 121 | } |
| 116 | InputEvent::PointerButton { event, .. } => { | 122 | InputEvent::PointerButton { event, .. } => { |
| 117 | let pointer = self.seat.get_pointer().unwrap(); | 123 | let pointer = self.seat.get_pointer().unwrap(); |
src/niri.rsmodified+34 -1
| @@ -12,6 +12,7 @@ use smithay::input::keyboard::XkbConfig; | |||
| 12 | use smithay::input::{Seat, SeatState}; | 12 | use smithay::input::{Seat, SeatState}; |
| 13 | use smithay::output::Output; | 13 | use smithay::output::Output; |
| 14 | use smithay::reexports::calloop::generic::Generic; | 14 | use smithay::reexports::calloop::generic::Generic; |
| 15 | +use smithay::reexports::calloop::timer::{TimeoutAction, Timer}; | ||
| 15 | use smithay::reexports::calloop::{Interest, LoopHandle, LoopSignal, Mode, PostAction}; | 16 | use smithay::reexports::calloop::{Interest, LoopHandle, LoopSignal, Mode, PostAction}; |
| 16 | use smithay::reexports::wayland_server::backend::{ClientData, ClientId, DisconnectReason}; | 17 | use smithay::reexports::wayland_server::backend::{ClientData, ClientId, DisconnectReason}; |
| 17 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; | 18 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
| @@ -45,6 +46,11 @@ pub struct Niri { | |||
| 45 | 46 | ||
| 46 | pub seat: Seat<Self>, | 47 | pub seat: Seat<Self>, |
| 47 | pub output: Option<Output>, | 48 | pub output: Option<Output>, |
| 49 | + | ||
| 50 | + // Set to `true` if there's a redraw queued on the event loop. Reset to `false` in redraw() | ||
| 51 | + // which means that you cannot queue more than one redraw at once. | ||
| 52 | + pub redraw_queued: bool, | ||
| 53 | + pub waiting_for_vblank: bool, | ||
| 48 | } | 54 | } |
| 49 | 55 | ||
| 50 | impl Niri { | 56 | impl Niri { |
| @@ -119,6 +125,8 @@ impl Niri { | |||
| 119 | 125 | ||
| 120 | seat, | 126 | seat, |
| 121 | output: None, | 127 | output: None, |
| 128 | + redraw_queued: false, | ||
| 129 | + waiting_for_vblank: false, | ||
| 122 | } | 130 | } |
| 123 | } | 131 | } |
| 124 | 132 | ||
| @@ -135,7 +143,32 @@ impl Niri { | |||
| 135 | }) | 143 | }) |
| 136 | } | 144 | } |
| 137 | 145 | ||
| 138 | - pub fn redraw(&mut self, backend: &mut dyn Backend) { | 146 | + /// Schedules an immediate redraw if one is not already scheduled. |
| 147 | + pub fn queue_redraw(&mut self) { | ||
| 148 | + if self.redraw_queued || self.waiting_for_vblank { | ||
| 149 | + return; | ||
| 150 | + } | ||
| 151 | + | ||
| 152 | + self.redraw_queued = true; | ||
| 153 | + | ||
| 154 | + self.event_loop | ||
| 155 | + .insert_source(Timer::immediate(), |_, _, data| { | ||
| 156 | + let backend: &mut dyn Backend = if let Some(tty) = &mut data.tty { | ||
| 157 | + tty | ||
| 158 | + } else { | ||
| 159 | + data.winit.as_mut().unwrap() | ||
| 160 | + }; | ||
| 161 | + data.niri.redraw(backend); | ||
| 162 | + TimeoutAction::Drop | ||
| 163 | + }) | ||
| 164 | + .unwrap(); | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + fn redraw(&mut self, backend: &mut dyn Backend) { | ||
| 168 | + assert!(self.redraw_queued); | ||
| 169 | + assert!(!self.waiting_for_vblank); | ||
| 170 | + self.redraw_queued = false; | ||
| 171 | + | ||
| 139 | let elements = space_render_elements( | 172 | let elements = space_render_elements( |
| 140 | backend.renderer(), | 173 | backend.renderer(), |
| 141 | [&self.space], | 174 | [&self.space], |
src/tty.rsmodified+6 -22
| @@ -1,6 +1,5 @@ | |||
| 1 | use std::os::fd::FromRawFd; | 1 | use std::os::fd::FromRawFd; |
| 2 | use std::path::PathBuf; | 2 | use std::path::PathBuf; |
| 3 | -use std::time::Duration; | ||
| 4 | 3 | ||
| 5 | use anyhow::anyhow; | 4 | use anyhow::anyhow; |
| 6 | use smithay::backend::allocator::dmabuf::Dmabuf; | 5 | use smithay::backend::allocator::dmabuf::Dmabuf; |
| @@ -17,7 +16,6 @@ use smithay::backend::session::libseat::LibSeatSession; | |||
| 17 | use smithay::backend::session::{Event as SessionEvent, Session}; | 16 | use smithay::backend::session::{Event as SessionEvent, Session}; |
| 18 | use smithay::backend::udev::{self, UdevBackend, UdevEvent}; | 17 | use smithay::backend::udev::{self, UdevBackend, UdevEvent}; |
| 19 | use smithay::output::{Mode, Output, OutputModeSource, PhysicalProperties, Subpixel}; | 18 | use smithay::output::{Mode, Output, OutputModeSource, PhysicalProperties, Subpixel}; |
| 20 | -use smithay::reexports::calloop::timer::{TimeoutAction, Timer}; | ||
| 21 | use smithay::reexports::calloop::{LoopHandle, RegistrationToken}; | 19 | use smithay::reexports::calloop::{LoopHandle, RegistrationToken}; |
| 22 | use smithay::reexports::drm::control::connector::{ | 20 | use smithay::reexports::drm::control::connector::{ |
| 23 | Interface as ConnectorInterface, State as ConnectorState, | 21 | Interface as ConnectorInterface, State as ConnectorState, |
| @@ -83,7 +81,7 @@ impl Backend for Tty { | |||
| 83 | assert!(!res.needs_sync()); | 81 | assert!(!res.needs_sync()); |
| 84 | if res.damage.is_some() { | 82 | if res.damage.is_some() { |
| 85 | match output_device.drm_compositor.queue_frame(()) { | 83 | match output_device.drm_compositor.queue_frame(()) { |
| 86 | - Ok(()) => return, | 84 | + Ok(()) => niri.waiting_for_vblank = true, |
| 87 | Err(err) => { | 85 | Err(err) => { |
| 88 | error!("error queueing frame: {err}"); | 86 | error!("error queueing frame: {err}"); |
| 89 | } | 87 | } |
| @@ -95,17 +93,6 @@ impl Backend for Tty { | |||
| 95 | error!("error rendering frame: {err}"); | 93 | error!("error rendering frame: {err}"); |
| 96 | } | 94 | } |
| 97 | } | 95 | } |
| 98 | - | ||
| 99 | - // FIXME: render on demand instead of busy looping. | ||
| 100 | - niri.event_loop | ||
| 101 | - .insert_source( | ||
| 102 | - Timer::from_duration(Duration::from_millis(6)), | ||
| 103 | - |_, _, data| { | ||
| 104 | - data.niri.redraw(data.tty.as_mut().unwrap()); | ||
| 105 | - TimeoutAction::Drop | ||
| 106 | - }, | ||
| 107 | - ) | ||
| 108 | - .unwrap(); | ||
| 109 | } | 96 | } |
| 110 | } | 97 | } |
| 111 | 98 | ||
| @@ -157,7 +144,7 @@ impl Tty { | |||
| 157 | output_device.drm_compositor.reset_buffers(); | 144 | output_device.drm_compositor.reset_buffers(); |
| 158 | } | 145 | } |
| 159 | 146 | ||
| 160 | - niri.redraw(tty); | 147 | + niri.queue_redraw(); |
| 161 | } | 148 | } |
| 162 | } | 149 | } |
| 163 | }) | 150 | }) |
| @@ -190,7 +177,7 @@ impl Tty { | |||
| 190 | if let Err(err) = tty.device_added(device_id, path, niri) { | 177 | if let Err(err) = tty.device_added(device_id, path, niri) { |
| 191 | warn!("error adding device: {err:?}"); | 178 | warn!("error adding device: {err:?}"); |
| 192 | } | 179 | } |
| 193 | - niri.redraw(tty); | 180 | + niri.queue_redraw(); |
| 194 | } | 181 | } |
| 195 | UdevEvent::Changed { device_id } => tty.device_changed(device_id, niri), | 182 | UdevEvent::Changed { device_id } => tty.device_changed(device_id, niri), |
| 196 | UdevEvent::Removed { device_id } => tty.device_removed(device_id, niri), | 183 | UdevEvent::Removed { device_id } => tty.device_removed(device_id, niri), |
| @@ -198,7 +185,7 @@ impl Tty { | |||
| 198 | }) | 185 | }) |
| 199 | .unwrap(); | 186 | .unwrap(); |
| 200 | 187 | ||
| 201 | - niri.redraw(self); | 188 | + niri.queue_redraw(); |
| 202 | } | 189 | } |
| 203 | 190 | ||
| 204 | fn device_added( | 191 | fn device_added( |
| @@ -250,11 +237,8 @@ impl Tty { | |||
| 250 | // .windows | 237 | // .windows |
| 251 | // .mark_presented(&output_device.last_render_states, metadata); | 238 | // .mark_presented(&output_device.last_render_states, metadata); |
| 252 | 239 | ||
| 253 | - // Request redraw before the next VBlank. | 240 | + data.niri.waiting_for_vblank = false; |
| 254 | - // let frame_interval = catacomb.windows.output().frame_interval(); | 241 | + data.niri.queue_redraw(); |
| 255 | - // let duration = frame_interval - RENDER_TIME_OFFSET; | ||
| 256 | - // catacomb.backend.schedule_redraw(duration); | ||
| 257 | - data.niri.redraw(tty); | ||
| 258 | } | 242 | } |
| 259 | DrmEvent::Error(error) => error!("DRM error: {error}"), | 243 | DrmEvent::Error(error) => error!("DRM error: {error}"), |
| 260 | }; | 244 | }; |
src/winit.rsmodified+9 -8
| @@ -115,16 +115,17 @@ impl Winit { | |||
| 115 | ); | 115 | ); |
| 116 | } | 116 | } |
| 117 | WinitEvent::Input(event) => niri.process_input_event(&mut |_| (), event), | 117 | WinitEvent::Input(event) => niri.process_input_event(&mut |_| (), event), |
| 118 | - _ => (), | 118 | + WinitEvent::Focus(_) => (), |
| 119 | + WinitEvent::Refresh => niri.queue_redraw(), | ||
| 119 | }); | 120 | }); |
| 120 | 121 | ||
| 121 | - if let Err(WinitError::WindowClosed) = res { | 122 | + // I want this to stop compiling if more errors are added. |
| 122 | - niri.stop_signal.stop(); | 123 | + #[allow(clippy::single_match)] |
| 123 | - return; | 124 | + match res { |
| 124 | - } else { | 125 | + Err(WinitError::WindowClosed) => { |
| 125 | - res.unwrap(); | 126 | + niri.stop_signal.stop(); |
| 127 | + } | ||
| 128 | + Ok(()) => (), | ||
| 126 | } | 129 | } |
| 127 | - | ||
| 128 | - niri.redraw(self); | ||
| 129 | } | 130 | } |
| 130 | } | 131 | } |