forge
Explore
Log in

Don't you dare go Hollow

Refactor frame scheduling

Browse files at this commit

a3aa5fca12b59df0c1ee421b482a51a425e25abdIvan MolodetskikhParent: 21737ab
Combine the redraw state variables into one enum, and refactor to get
rid of the requirement that a VBlank must queue a subsequent redraw.
Also fix the bug where ongoing animations that produced no damage could
stall the redrawing.
src/backend/tty.rsmodified+57 -20
@@ -1,5 +1,6 @@
11 use std::cell::RefCell;
22 use std::collections::{HashMap, HashSet};
3+use std::mem;
34 use std::os::fd::FromRawFd;
45 use std::path::{Path, PathBuf};
56 use std::rc::Rc;
@@ -38,7 +39,7 @@ use smithay_drm_extras::drm_scanner::{DrmScanEvent, DrmScanner};
3839 use smithay_drm_extras::edid::EdidInfo;
3940
4041 use crate::config::Config;
41-use crate::niri::{OutputRenderElements, State};
42+use crate::niri::{OutputRenderElements, State, RedrawState};
4243 use crate::utils::get_monotonic_time;
4344 use crate::Niri;
4445
@@ -703,34 +704,55 @@ impl Tty {
703704 .plot(surface.sequence_delta_plot_name, delta);
704705 }
705706
706- assert!(output_state.waiting_for_vblank);
707- assert!(output_state.estimated_vblank_timer.is_none());
708-
709- output_state.waiting_for_vblank = false;
710707 output_state.frame_clock.presented(presentation_time);
711708 output_state.current_estimated_sequence = Some(meta.sequence);
712- niri.queue_redraw(output);
709+
710+ let redraw_needed = match mem::replace(&mut output_state.redraw_state, RedrawState::Idle) {
711+ RedrawState::Idle => unreachable!(),
712+ RedrawState::Queued(_) => unreachable!(),
713+ RedrawState::WaitingForVBlank { redraw_needed } => redraw_needed,
714+ RedrawState::WaitingForEstimatedVBlank(_) => unreachable!(),
715+ RedrawState::WaitingForEstimatedVBlankAndQueued(_) => unreachable!(),
716+ };
717+
718+ if redraw_needed || output_state.unfinished_animations_remain {
719+ niri.queue_redraw(output);
720+ } else {
721+ niri.send_frame_callbacks(&output);
722+ }
713723 }
714724
715- fn on_estimated_vblank_timer(&self, niri: &mut Niri, output: &Output) {
725+ fn on_estimated_vblank_timer(&self, niri: &mut Niri, output: Output) {
716726 let span = tracy_client::span!("Tty::on_estimated_vblank_timer");
717727
718728 let name = output.name();
719729 span.emit_text(&name);
720730
721- let Some(output_state) = niri.output_state.get_mut(output) else {
731+ let Some(output_state) = niri.output_state.get_mut(&output) else {
722732 error!("missing output state for {name}");
723733 return;
724734 };
725735
726- assert!(!output_state.waiting_for_vblank);
727- let token = output_state.estimated_vblank_timer.take();
728- assert!(token.is_some());
736+ match mem::replace(&mut output_state.redraw_state, RedrawState::Idle) {
737+ RedrawState::Idle => unreachable!(),
738+ RedrawState::Queued(_) => unreachable!(),
739+ RedrawState::WaitingForVBlank { .. } => unreachable!(),
740+ RedrawState::WaitingForEstimatedVBlank(_) => (),
741+ // The timer fired just in front of a redraw.
742+ RedrawState::WaitingForEstimatedVBlankAndQueued((_, idle)) => {
743+ output_state.redraw_state = RedrawState::Queued(idle);
744+ return;
745+ }
746+ }
729747
730748 if let Some(sequence) = output_state.current_estimated_sequence.as_mut() {
731749 *sequence = sequence.wrapping_add(1);
732750
733- niri.send_frame_callbacks(output);
751+ if output_state.unfinished_animations_remain {
752+ niri.queue_redraw(output);
753+ } else {
754+ niri.send_frame_callbacks(&output);
755+ }
734756 }
735757 }
736758
@@ -793,10 +815,18 @@ impl Tty {
793815 match drm_compositor.queue_frame(data) {
794816 Ok(()) => {
795817 let output_state = niri.output_state.get_mut(output).unwrap();
796- output_state.waiting_for_vblank = true;
797- if let Some(token) = output_state.estimated_vblank_timer.take() {
798- niri.event_loop.remove(token);
799- }
818+ let new_state = RedrawState::WaitingForVBlank {
819+ redraw_needed: false,
820+ };
821+ match mem::replace(&mut output_state.redraw_state, new_state) {
822+ RedrawState::Idle => unreachable!(),
823+ RedrawState::Queued(_) => (),
824+ RedrawState::WaitingForVBlank { .. } => unreachable!(),
825+ RedrawState::WaitingForEstimatedVBlank(_) => unreachable!(),
826+ RedrawState::WaitingForEstimatedVBlankAndQueued((token, _)) => {
827+ niri.event_loop.remove(token);
828+ }
829+ };
800830
801831 return Some(&surface.dmabuf_feedback);
802832 }
@@ -904,8 +934,15 @@ fn queue_estimated_vblank_timer(
904934 target_presentation_time: Duration,
905935 ) {
906936 let output_state = niri.output_state.get_mut(&output).unwrap();
907- if output_state.estimated_vblank_timer.is_some() {
908- return;
937+ match mem::take(&mut output_state.redraw_state) {
938+ RedrawState::Idle => unreachable!(),
939+ RedrawState::Queued(_) => (),
940+ RedrawState::WaitingForVBlank { .. } => unreachable!(),
941+ RedrawState::WaitingForEstimatedVBlank(token)
942+ | RedrawState::WaitingForEstimatedVBlankAndQueued((token, _)) => {
943+ output_state.redraw_state = RedrawState::WaitingForEstimatedVBlank(token);
944+ return;
945+ }
909946 }
910947
911948 let now = get_monotonic_time();
@@ -915,9 +952,9 @@ fn queue_estimated_vblank_timer(
915952 .insert_source(timer, move |_, _, data| {
916953 data.backend
917954 .tty()
918- .on_estimated_vblank_timer(&mut data.niri, &output);
955+ .on_estimated_vblank_timer(&mut data.niri, output.clone());
919956 TimeoutAction::Drop
920957 })
921958 .unwrap();
922- output_state.estimated_vblank_timer = Some(token);
959+ output_state.redraw_state = RedrawState::WaitingForEstimatedVBlank(token);
923960 }
src/backend/winit.rsmodified+13 -1
@@ -1,5 +1,6 @@
11 use std::cell::RefCell;
22 use std::collections::HashMap;
3+use std::mem;
34 use std::rc::Rc;
45 use std::sync::{Arc, Mutex};
56 use std::time::Duration;
@@ -18,7 +19,7 @@ use smithay::utils::Transform;
1819 use smithay::wayland::dmabuf::DmabufFeedback;
1920
2021 use crate::config::Config;
21-use crate::niri::{OutputRenderElements, State};
22+use crate::niri::{OutputRenderElements, RedrawState, State};
2223 use crate::utils::get_monotonic_time;
2324 use crate::Niri;
2425
@@ -182,7 +183,18 @@ impl Winit {
182183 0,
183184 wp_presentation_feedback::Kind::empty(),
184185 );
186+ }
187+
188+ let output_state = niri.output_state.get_mut(output).unwrap();
189+ match mem::replace(&mut output_state.redraw_state, RedrawState::Idle) {
190+ RedrawState::Idle => unreachable!(),
191+ RedrawState::Queued(_) => (),
192+ RedrawState::WaitingForVBlank { .. } => unreachable!(),
193+ RedrawState::WaitingForEstimatedVBlank(_) => unreachable!(),
194+ RedrawState::WaitingForEstimatedVBlankAndQueued(_) => unreachable!(),
195+ }
185196
197+ if output_state.unfinished_animations_remain {
186198 self.backend.window().request_redraw();
187199 }
188200
src/layout.rsmodified+9 -0
@@ -1328,6 +1328,11 @@ impl<W: LayoutElement> Monitor<W> {
13281328 }
13291329 }
13301330
1331+ pub fn are_animations_ongoing(&self) -> bool {
1332+ self.workspace_idx_anim.is_some()
1333+ || self.workspaces.iter().any(|ws| ws.are_animations_ongoing())
1334+ }
1335+
13311336 pub fn update_config(&mut self, config: &Config) {
13321337 for ws in &mut self.workspaces {
13331338 ws.update_config(config);
@@ -1464,6 +1469,10 @@ impl<W: LayoutElement> Workspace<W> {
14641469 }
14651470 }
14661471
1472+ pub fn are_animations_ongoing(&self) -> bool {
1473+ self.view_offset_anim.is_some()
1474+ }
1475+
14671476 pub fn update_config(&mut self, config: &Config) {
14681477 let c = &config.focus_ring;
14691478 self.focus_ring.is_off = c.off;
src/niri.rsmodified+64 -23
@@ -6,7 +6,7 @@ use std::process::Command;
66 use std::rc::Rc;
77 use std::sync::{Arc, Mutex};
88 use std::time::Duration;
9-use std::{env, thread};
9+use std::{env, mem, thread};
1010
1111 use _server_decoration::server::org_kde_kwin_server_decoration_manager::Mode as KdeDecorationsMode;
1212 use anyhow::Context;
@@ -132,16 +132,10 @@ pub struct Niri {
132132
133133 pub struct OutputState {
134134 pub global: GlobalId,
135- // Set if there's a redraw queued on the event loop. Reset in redraw() which means that you
136- // cannot queue more than one redraw at once.
137- pub queued_redraw: Option<Idle<'static>>,
138- // Set to `true` when the output was redrawn and is waiting for a VBlank. Upon VBlank a redraw
139- // will always be queued, so you cannot queue a redraw while waiting for a VBlank.
140- pub waiting_for_vblank: bool,
141- // When we did a redraw which did not result in a KMS frame getting queued, this is a timer
142- // that will fire at the estimated VBlank time.
143- pub estimated_vblank_timer: Option<RegistrationToken>,
144135 pub frame_clock: FrameClock,
136+ pub redraw_state: RedrawState,
137+ // After the last redraw, some ongoing animations still remain.
138+ pub unfinished_animations_remain: bool,
145139 /// Estimated sequence currently displayed on this output.
146140 ///
147141 /// When a frame is presented on this output, this becomes the real sequence from the VBlank
@@ -153,6 +147,21 @@ pub struct OutputState {
153147 pub current_estimated_sequence: Option<u32>,
154148 }
155149
150+#[derive(Default)]
151+pub enum RedrawState {
152+ /// The compositor is idle.
153+ #[default]
154+ Idle,
155+ /// A redraw is queued.
156+ Queued(Idle<'static>),
157+ /// We submitted a frame to the KMS and waiting for it to be presented.
158+ WaitingForVBlank { redraw_needed: bool },
159+ /// We did not submit anything to KMS and made a timer to fire at the estimated VBlank.
160+ WaitingForEstimatedVBlank(RegistrationToken),
161+ /// A redraw is queued on top of the above.
162+ WaitingForEstimatedVBlankAndQueued((RegistrationToken, Idle<'static>)),
163+}
164+
156165 // Not related to the one in Smithay.
157166 //
158167 // This state keeps track of when a surface last received a frame callback.
@@ -733,9 +742,8 @@ impl Niri {
733742
734743 let state = OutputState {
735744 global,
736- queued_redraw: None,
737- waiting_for_vblank: false,
738- estimated_vblank_timer: None,
745+ redraw_state: RedrawState::Idle,
746+ unfinished_animations_remain: false,
739747 frame_clock: FrameClock::new(refresh_interval),
740748 current_estimated_sequence: None,
741749 };
@@ -748,9 +756,16 @@ impl Niri {
748756 self.global_space.unmap_output(output);
749757 // FIXME: reposition outputs so they are adjacent.
750758
751- let mut state = self.output_state.remove(output).unwrap();
752- if let Some(idle) = state.queued_redraw.take() {
753- idle.cancel();
759+ let state = self.output_state.remove(output).unwrap();
760+ match state.redraw_state {
761+ RedrawState::Idle => (),
762+ RedrawState::Queued(idle) => idle.cancel(),
763+ RedrawState::WaitingForVBlank { .. } => (),
764+ RedrawState::WaitingForEstimatedVBlank(token) => self.event_loop.remove(token),
765+ RedrawState::WaitingForEstimatedVBlankAndQueued((token, idle)) => {
766+ self.event_loop.remove(token);
767+ idle.cancel();
768+ }
754769 }
755770
756771 // Disable the output global and remove some time later to give the clients some time to
@@ -917,15 +932,34 @@ impl Niri {
917932 /// Schedules an immediate redraw if one is not already scheduled.
918933 pub fn queue_redraw(&mut self, output: Output) {
919934 let state = self.output_state.get_mut(&output).unwrap();
935+ let token = match mem::take(&mut state.redraw_state) {
936+ RedrawState::Idle => None,
937+ RedrawState::WaitingForEstimatedVBlank(token) => Some(token),
938+
939+ // A redraw is already queued, put it back and do nothing.
940+ value @ (RedrawState::Queued(_)
941+ | RedrawState::WaitingForEstimatedVBlankAndQueued(_)) => {
942+ state.redraw_state = value;
943+ return;
944+ }
920945
921- if state.queued_redraw.is_some() || state.waiting_for_vblank {
922- return;
923- }
946+ // We're waiting for VBlank, request a redraw afterwards.
947+ RedrawState::WaitingForVBlank { .. } => {
948+ state.redraw_state = RedrawState::WaitingForVBlank {
949+ redraw_needed: true,
950+ };
951+ return;
952+ }
953+ };
924954
925955 let idle = self.event_loop.insert_idle(move |state| {
926956 state.niri.redraw(&mut state.backend, &output);
927957 });
928- state.queued_redraw = Some(idle);
958+
959+ state.redraw_state = match token {
960+ Some(token) => RedrawState::WaitingForEstimatedVBlankAndQueued((token, idle)),
961+ None => RedrawState::Queued(idle),
962+ };
929963 }
930964
931965 pub fn pointer_element(
@@ -1070,14 +1104,21 @@ impl Niri {
10701104 };
10711105
10721106 let state = self.output_state.get_mut(output).unwrap();
1073- let presentation_time = state.frame_clock.next_presentation_time();
1107+ assert!(matches!(
1108+ state.redraw_state,
1109+ RedrawState::Queued(_) | RedrawState::WaitingForEstimatedVBlankAndQueued(_)
1110+ ));
10741111
1075- assert!(state.queued_redraw.take().is_some());
1076- assert!(!state.waiting_for_vblank);
1112+ let presentation_time = state.frame_clock.next_presentation_time();
10771113
10781114 // Update from the config and advance the animations.
10791115 self.monitor_set.update_config(&self.config.borrow());
10801116 self.monitor_set.advance_animations(presentation_time);
1117+ state.unfinished_animations_remain = self
1118+ .monitor_set
1119+ .monitor_for_output(output)
1120+ .unwrap()
1121+ .are_animations_ongoing();
10811122
10821123 // Render the elements.
10831124 let elements = self.render(renderer, output, true);