Refactor frame scheduling
Browse files at this commita3aa5fca12b59df0c1ee421b482a51a425e25abdParent: 21737ab4 changed files +143 -44
- src/backend/tty.rs +57 -20
- src/backend/winit.rs +13 -1
- src/layout.rs +9 -0
- src/niri.rs +64 -23
src/backend/tty.rsmodified+57 -20
| @@ -1,5 +1,6 @@ | |||
| 1 | use std::cell::RefCell; | 1 | use std::cell::RefCell; |
| 2 | use std::collections::{HashMap, HashSet}; | 2 | use std::collections::{HashMap, HashSet}; |
| 3 | +use std::mem; | ||
| 3 | use std::os::fd::FromRawFd; | 4 | use std::os::fd::FromRawFd; |
| 4 | use std::path::{Path, PathBuf}; | 5 | use std::path::{Path, PathBuf}; |
| 5 | use std::rc::Rc; | 6 | use std::rc::Rc; |
| @@ -38,7 +39,7 @@ use smithay_drm_extras::drm_scanner::{DrmScanEvent, DrmScanner}; | |||
| 38 | use smithay_drm_extras::edid::EdidInfo; | 39 | use smithay_drm_extras::edid::EdidInfo; |
| 39 | 40 | ||
| 40 | use crate::config::Config; | 41 | use crate::config::Config; |
| 41 | -use crate::niri::{OutputRenderElements, State}; | 42 | +use crate::niri::{OutputRenderElements, State, RedrawState}; |
| 42 | use crate::utils::get_monotonic_time; | 43 | use crate::utils::get_monotonic_time; |
| 43 | use crate::Niri; | 44 | use crate::Niri; |
| 44 | 45 | ||
| @@ -703,34 +704,55 @@ impl Tty { | |||
| 703 | .plot(surface.sequence_delta_plot_name, delta); | 704 | .plot(surface.sequence_delta_plot_name, delta); |
| 704 | } | 705 | } |
| 705 | 706 | ||
| 706 | - assert!(output_state.waiting_for_vblank); | ||
| 707 | - assert!(output_state.estimated_vblank_timer.is_none()); | ||
| 708 | - | ||
| 709 | - output_state.waiting_for_vblank = false; | ||
| 710 | output_state.frame_clock.presented(presentation_time); | 707 | output_state.frame_clock.presented(presentation_time); |
| 711 | output_state.current_estimated_sequence = Some(meta.sequence); | 708 | 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 | + } | ||
| 713 | } | 723 | } |
| 714 | 724 | ||
| 715 | - fn on_estimated_vblank_timer(&self, niri: &mut Niri, output: &Output) { | 725 | + fn on_estimated_vblank_timer(&self, niri: &mut Niri, output: Output) { |
| 716 | let span = tracy_client::span!("Tty::on_estimated_vblank_timer"); | 726 | let span = tracy_client::span!("Tty::on_estimated_vblank_timer"); |
| 717 | 727 | ||
| 718 | let name = output.name(); | 728 | let name = output.name(); |
| 719 | span.emit_text(&name); | 729 | span.emit_text(&name); |
| 720 | 730 | ||
| 721 | - let Some(output_state) = niri.output_state.get_mut(output) else { | 731 | + let Some(output_state) = niri.output_state.get_mut(&output) else { |
| 722 | error!("missing output state for {name}"); | 732 | error!("missing output state for {name}"); |
| 723 | return; | 733 | return; |
| 724 | }; | 734 | }; |
| 725 | 735 | ||
| 726 | - assert!(!output_state.waiting_for_vblank); | 736 | + match mem::replace(&mut output_state.redraw_state, RedrawState::Idle) { |
| 727 | - let token = output_state.estimated_vblank_timer.take(); | 737 | + RedrawState::Idle => unreachable!(), |
| 728 | - assert!(token.is_some()); | 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 | + } | ||
| 729 | 747 | ||
| 730 | if let Some(sequence) = output_state.current_estimated_sequence.as_mut() { | 748 | if let Some(sequence) = output_state.current_estimated_sequence.as_mut() { |
| 731 | *sequence = sequence.wrapping_add(1); | 749 | *sequence = sequence.wrapping_add(1); |
| 732 | 750 | ||
| 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 | + } | ||
| 734 | } | 756 | } |
| 735 | } | 757 | } |
| 736 | 758 | ||
| @@ -793,10 +815,18 @@ impl Tty { | |||
| 793 | match drm_compositor.queue_frame(data) { | 815 | match drm_compositor.queue_frame(data) { |
| 794 | Ok(()) => { | 816 | Ok(()) => { |
| 795 | let output_state = niri.output_state.get_mut(output).unwrap(); | 817 | let output_state = niri.output_state.get_mut(output).unwrap(); |
| 796 | - output_state.waiting_for_vblank = true; | 818 | + let new_state = RedrawState::WaitingForVBlank { |
| 797 | - if let Some(token) = output_state.estimated_vblank_timer.take() { | 819 | + redraw_needed: false, |
| 798 | - niri.event_loop.remove(token); | 820 | + }; |
| 799 | - } | 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 | + }; | ||
| 800 | 830 | ||
| 801 | return Some(&surface.dmabuf_feedback); | 831 | return Some(&surface.dmabuf_feedback); |
| 802 | } | 832 | } |
| @@ -904,8 +934,15 @@ fn queue_estimated_vblank_timer( | |||
| 904 | target_presentation_time: Duration, | 934 | target_presentation_time: Duration, |
| 905 | ) { | 935 | ) { |
| 906 | let output_state = niri.output_state.get_mut(&output).unwrap(); | 936 | let output_state = niri.output_state.get_mut(&output).unwrap(); |
| 907 | - if output_state.estimated_vblank_timer.is_some() { | 937 | + match mem::take(&mut output_state.redraw_state) { |
| 908 | - return; | 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 | + } | ||
| 909 | } | 946 | } |
| 910 | 947 | ||
| 911 | let now = get_monotonic_time(); | 948 | let now = get_monotonic_time(); |
| @@ -915,9 +952,9 @@ fn queue_estimated_vblank_timer( | |||
| 915 | .insert_source(timer, move |_, _, data| { | 952 | .insert_source(timer, move |_, _, data| { |
| 916 | data.backend | 953 | data.backend |
| 917 | .tty() | 954 | .tty() |
| 918 | - .on_estimated_vblank_timer(&mut data.niri, &output); | 955 | + .on_estimated_vblank_timer(&mut data.niri, output.clone()); |
| 919 | TimeoutAction::Drop | 956 | TimeoutAction::Drop |
| 920 | }) | 957 | }) |
| 921 | .unwrap(); | 958 | .unwrap(); |
| 922 | - output_state.estimated_vblank_timer = Some(token); | 959 | + output_state.redraw_state = RedrawState::WaitingForEstimatedVBlank(token); |
| 923 | } | 960 | } |
src/backend/winit.rsmodified+13 -1
| @@ -1,5 +1,6 @@ | |||
| 1 | use std::cell::RefCell; | 1 | use std::cell::RefCell; |
| 2 | use std::collections::HashMap; | 2 | use std::collections::HashMap; |
| 3 | +use std::mem; | ||
| 3 | use std::rc::Rc; | 4 | use std::rc::Rc; |
| 4 | use std::sync::{Arc, Mutex}; | 5 | use std::sync::{Arc, Mutex}; |
| 5 | use std::time::Duration; | 6 | use std::time::Duration; |
| @@ -18,7 +19,7 @@ use smithay::utils::Transform; | |||
| 18 | use smithay::wayland::dmabuf::DmabufFeedback; | 19 | use smithay::wayland::dmabuf::DmabufFeedback; |
| 19 | 20 | ||
| 20 | use crate::config::Config; | 21 | use crate::config::Config; |
| 21 | -use crate::niri::{OutputRenderElements, State}; | 22 | +use crate::niri::{OutputRenderElements, RedrawState, State}; |
| 22 | use crate::utils::get_monotonic_time; | 23 | use crate::utils::get_monotonic_time; |
| 23 | use crate::Niri; | 24 | use crate::Niri; |
| 24 | 25 | ||
| @@ -182,7 +183,18 @@ impl Winit { | |||
| 182 | 0, | 183 | 0, |
| 183 | wp_presentation_feedback::Kind::empty(), | 184 | wp_presentation_feedback::Kind::empty(), |
| 184 | ); | 185 | ); |
| 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 | + } | ||
| 185 | 196 | ||
| 197 | + if output_state.unfinished_animations_remain { | ||
| 186 | self.backend.window().request_redraw(); | 198 | self.backend.window().request_redraw(); |
| 187 | } | 199 | } |
| 188 | 200 | ||
src/layout.rsmodified+9 -0
| @@ -1328,6 +1328,11 @@ impl<W: LayoutElement> Monitor<W> { | |||
| 1328 | } | 1328 | } |
| 1329 | } | 1329 | } |
| 1330 | 1330 | ||
| 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 | + | ||
| 1331 | pub fn update_config(&mut self, config: &Config) { | 1336 | pub fn update_config(&mut self, config: &Config) { |
| 1332 | for ws in &mut self.workspaces { | 1337 | for ws in &mut self.workspaces { |
| 1333 | ws.update_config(config); | 1338 | ws.update_config(config); |
| @@ -1464,6 +1469,10 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1464 | } | 1469 | } |
| 1465 | } | 1470 | } |
| 1466 | 1471 | ||
| 1472 | + pub fn are_animations_ongoing(&self) -> bool { | ||
| 1473 | + self.view_offset_anim.is_some() | ||
| 1474 | + } | ||
| 1475 | + | ||
| 1467 | pub fn update_config(&mut self, config: &Config) { | 1476 | pub fn update_config(&mut self, config: &Config) { |
| 1468 | let c = &config.focus_ring; | 1477 | let c = &config.focus_ring; |
| 1469 | self.focus_ring.is_off = c.off; | 1478 | self.focus_ring.is_off = c.off; |
src/niri.rsmodified+64 -23
| @@ -6,7 +6,7 @@ use std::process::Command; | |||
| 6 | use std::rc::Rc; | 6 | use std::rc::Rc; |
| 7 | use std::sync::{Arc, Mutex}; | 7 | use std::sync::{Arc, Mutex}; |
| 8 | use std::time::Duration; | 8 | use std::time::Duration; |
| 9 | -use std::{env, thread}; | 9 | +use std::{env, mem, thread}; |
| 10 | 10 | ||
| 11 | use _server_decoration::server::org_kde_kwin_server_decoration_manager::Mode as KdeDecorationsMode; | 11 | use _server_decoration::server::org_kde_kwin_server_decoration_manager::Mode as KdeDecorationsMode; |
| 12 | use anyhow::Context; | 12 | use anyhow::Context; |
| @@ -132,16 +132,10 @@ pub struct Niri { | |||
| 132 | 132 | ||
| 133 | pub struct OutputState { | 133 | pub struct OutputState { |
| 134 | pub global: GlobalId, | 134 | 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>, | ||
| 144 | pub frame_clock: FrameClock, | 135 | 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, | ||
| 145 | /// Estimated sequence currently displayed on this output. | 139 | /// Estimated sequence currently displayed on this output. |
| 146 | /// | 140 | /// |
| 147 | /// When a frame is presented on this output, this becomes the real sequence from the VBlank | 141 | /// When a frame is presented on this output, this becomes the real sequence from the VBlank |
| @@ -153,6 +147,21 @@ pub struct OutputState { | |||
| 153 | pub current_estimated_sequence: Option<u32>, | 147 | pub current_estimated_sequence: Option<u32>, |
| 154 | } | 148 | } |
| 155 | 149 | ||
| 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 | + | ||
| 156 | // Not related to the one in Smithay. | 165 | // Not related to the one in Smithay. |
| 157 | // | 166 | // |
| 158 | // This state keeps track of when a surface last received a frame callback. | 167 | // This state keeps track of when a surface last received a frame callback. |
| @@ -733,9 +742,8 @@ impl Niri { | |||
| 733 | 742 | ||
| 734 | let state = OutputState { | 743 | let state = OutputState { |
| 735 | global, | 744 | global, |
| 736 | - queued_redraw: None, | 745 | + redraw_state: RedrawState::Idle, |
| 737 | - waiting_for_vblank: false, | 746 | + unfinished_animations_remain: false, |
| 738 | - estimated_vblank_timer: None, | ||
| 739 | frame_clock: FrameClock::new(refresh_interval), | 747 | frame_clock: FrameClock::new(refresh_interval), |
| 740 | current_estimated_sequence: None, | 748 | current_estimated_sequence: None, |
| 741 | }; | 749 | }; |
| @@ -748,9 +756,16 @@ impl Niri { | |||
| 748 | self.global_space.unmap_output(output); | 756 | self.global_space.unmap_output(output); |
| 749 | // FIXME: reposition outputs so they are adjacent. | 757 | // FIXME: reposition outputs so they are adjacent. |
| 750 | 758 | ||
| 751 | - let mut state = self.output_state.remove(output).unwrap(); | 759 | + let state = self.output_state.remove(output).unwrap(); |
| 752 | - if let Some(idle) = state.queued_redraw.take() { | 760 | + match state.redraw_state { |
| 753 | - idle.cancel(); | 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 | + } | ||
| 754 | } | 769 | } |
| 755 | 770 | ||
| 756 | // Disable the output global and remove some time later to give the clients some time to | 771 | // Disable the output global and remove some time later to give the clients some time to |
| @@ -917,15 +932,34 @@ impl Niri { | |||
| 917 | /// Schedules an immediate redraw if one is not already scheduled. | 932 | /// Schedules an immediate redraw if one is not already scheduled. |
| 918 | pub fn queue_redraw(&mut self, output: Output) { | 933 | pub fn queue_redraw(&mut self, output: Output) { |
| 919 | let state = self.output_state.get_mut(&output).unwrap(); | 934 | 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 | + } | ||
| 920 | 945 | ||
| 921 | - if state.queued_redraw.is_some() || state.waiting_for_vblank { | 946 | + // We're waiting for VBlank, request a redraw afterwards. |
| 922 | - return; | 947 | + RedrawState::WaitingForVBlank { .. } => { |
| 923 | - } | 948 | + state.redraw_state = RedrawState::WaitingForVBlank { |
| 949 | + redraw_needed: true, | ||
| 950 | + }; | ||
| 951 | + return; | ||
| 952 | + } | ||
| 953 | + }; | ||
| 924 | 954 | ||
| 925 | let idle = self.event_loop.insert_idle(move |state| { | 955 | let idle = self.event_loop.insert_idle(move |state| { |
| 926 | state.niri.redraw(&mut state.backend, &output); | 956 | state.niri.redraw(&mut state.backend, &output); |
| 927 | }); | 957 | }); |
| 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 | + }; | ||
| 929 | } | 963 | } |
| 930 | 964 | ||
| 931 | pub fn pointer_element( | 965 | pub fn pointer_element( |
| @@ -1070,14 +1104,21 @@ impl Niri { | |||
| 1070 | }; | 1104 | }; |
| 1071 | 1105 | ||
| 1072 | let state = self.output_state.get_mut(output).unwrap(); | 1106 | 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 | + )); | ||
| 1074 | 1111 | ||
| 1075 | - assert!(state.queued_redraw.take().is_some()); | 1112 | + let presentation_time = state.frame_clock.next_presentation_time(); |
| 1076 | - assert!(!state.waiting_for_vblank); | ||
| 1077 | 1113 | ||
| 1078 | // Update from the config and advance the animations. | 1114 | // Update from the config and advance the animations. |
| 1079 | self.monitor_set.update_config(&self.config.borrow()); | 1115 | self.monitor_set.update_config(&self.config.borrow()); |
| 1080 | self.monitor_set.advance_animations(presentation_time); | 1116 | 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(); | ||
| 1081 | 1122 | ||
| 1082 | // Render the elements. | 1123 | // Render the elements. |
| 1083 | let elements = self.render(renderer, output, true); | 1124 | let elements = self.render(renderer, output, true); |