forge
Explore
Log in

Don't you dare go Hollow

Rework PW screencast frame timing

Browse files at this commit

b2c7d3ad401574a3db01ed20acd67ebc8d331cbcIvan MolodetskikhParent: 4832924
- Remove the 0.5 ms hack.
- Add redraw scheduling to fix stuck frame if the last redrawn frame
  happened too soon.
src/niri.rsmodified+3 -3
@@ -3664,7 +3664,7 @@ impl Niri {
3664 }3664 }
3665 }3665 }
3666 3666
3667- if cast.should_skip_frame(target_presentation_time) {3667+ if cast.check_time_and_schedule(&self.event_loop, output, target_presentation_time) {
3668 continue;3668 continue;
3669 }3669 }
3670 3670
@@ -3726,7 +3726,7 @@ impl Niri {
3726 }3726 }
3727 }3727 }
3728 3728
3729- if cast.should_skip_frame(target_presentation_time) {3729+ if cast.check_time_and_schedule(&self.event_loop, output, target_presentation_time) {
3730 continue;3730 continue;
3731 }3731 }
3732 3732
@@ -3805,7 +3805,7 @@ impl Niri {
3805 }3805 }
3806 }3806 }
3807 3807
3808- if cast.should_skip_frame(target_presentation_time) {3808+ if cast.check_time_and_schedule(&self.event_loop, output, target_presentation_time) {
3809 continue;3809 continue;
3810 }3810 }
3811 3811
src/pw_utils.rsmodified+74 -11
@@ -8,6 +8,8 @@ use std::rc::Rc;
8 use std::time::Duration;8 use std::time::Duration;
9 9
10 use anyhow::Context as _;10 use anyhow::Context as _;
11+use calloop::timer::{TimeoutAction, Timer};
12+use calloop::RegistrationToken;
11 use pipewire::context::Context;13 use pipewire::context::Context;
12 use pipewire::core::Core;14 use pipewire::core::Core;
13 use pipewire::main_loop::MainLoop;15 use pipewire::main_loop::MainLoop;
@@ -34,7 +36,7 @@ use smithay::backend::drm::DrmDeviceFd;
34 use smithay::backend::renderer::damage::OutputDamageTracker;36 use smithay::backend::renderer::damage::OutputDamageTracker;
35 use smithay::backend::renderer::element::RenderElement;37 use smithay::backend::renderer::element::RenderElement;
36 use smithay::backend::renderer::gles::GlesRenderer;38 use smithay::backend::renderer::gles::GlesRenderer;
37-use smithay::output::{OutputModeSource, WeakOutput};39+use smithay::output::{Output, OutputModeSource, WeakOutput};
38 use smithay::reexports::calloop::generic::Generic;40 use smithay::reexports::calloop::generic::Generic;
39 use smithay::reexports::calloop::{Interest, LoopHandle, Mode, PostAction};41 use smithay::reexports::calloop::{Interest, LoopHandle, Mode, PostAction};
40 use smithay::reexports::gbm::Modifier;42 use smithay::reexports::gbm::Modifier;
@@ -44,6 +46,10 @@ use zbus::SignalContext;
44 use crate::dbus::mutter_screen_cast::{self, CursorMode};46 use crate::dbus::mutter_screen_cast::{self, CursorMode};
45 use crate::niri::State;47 use crate::niri::State;
46 use crate::render_helpers::render_to_dmabuf;48 use crate::render_helpers::render_to_dmabuf;
49+use crate::utils::get_monotonic_time;
50+
51+// Give a 0.1 ms allowance for presentation time errors.
52+const CAST_DELAY_ALLOWANCE: Duration = Duration::from_micros(100);
47 53
48 pub struct PipeWire {54 pub struct PipeWire {
49 _context: Context,55 _context: Context,
@@ -70,6 +76,7 @@ pub struct Cast {
70 pub last_frame_time: Duration,76 pub last_frame_time: Duration,
71 min_time_between_frames: Rc<Cell<Duration>>,77 min_time_between_frames: Rc<Cell<Duration>>,
72 dmabufs: Rc<RefCell<HashMap<i64, Dmabuf>>>,78 dmabufs: Rc<RefCell<HashMap<i64, Dmabuf>>>,
79+ scheduled_redraw: Option<RegistrationToken>,
73 }80 }
74 81
75 #[derive(Debug)]82 #[derive(Debug)]
@@ -315,10 +322,9 @@ impl PipeWire {
315 };322 };
316 323
317 let max_frame_rate = format.max_framerate();324 let max_frame_rate = format.max_framerate();
318- // Subtract 0.5 ms to improve edge cases when equal to refresh rate.325+ let min_frame_time = Duration::from_micros(
319- let min_frame_time = Duration::from_secs_f64(326+ 1_000_000 * u64::from(max_frame_rate.denom) / u64::from(max_frame_rate.num),
320- max_frame_rate.denom as f64 / max_frame_rate.num as f64,327+ );
321- ) - Duration::from_micros(500);
322 min_time_between_frames.set(min_frame_time);328 min_time_between_frames.set(min_frame_time);
323 329
324 let object = pod.as_object().unwrap();330 let object = pod.as_object().unwrap();
@@ -651,6 +657,7 @@ impl PipeWire {
651 last_frame_time: Duration::ZERO,657 last_frame_time: Duration::ZERO,
652 min_time_between_frames,658 min_time_between_frames,
653 dmabufs,659 dmabufs,
660+ scheduled_redraw: None,
654 };661 };
655 Ok(cast)662 Ok(cast)
656 }663 }
@@ -711,13 +718,13 @@ impl Cast {
711 Ok(())718 Ok(())
712 }719 }
713 720
714- pub fn should_skip_frame(&self, target_frame_time: Duration) -> bool {721+ fn compute_extra_delay(&self, target_frame_time: Duration) -> Duration {
715 let last = self.last_frame_time;722 let last = self.last_frame_time;
716 let min = self.min_time_between_frames.get();723 let min = self.min_time_between_frames.get();
717 724
718 if last.is_zero() {725 if last.is_zero() {
719 trace!(?target_frame_time, ?last, "last is zero, recording");726 trace!(?target_frame_time, ?last, "last is zero, recording");
720- return false;727+ return Duration::ZERO;
721 }728 }
722 729
723 if target_frame_time < last {730 if target_frame_time < last {
@@ -727,20 +734,76 @@ impl Cast {
727 ?last,734 ?last,
728 "target frame time is below last, did it overflow or did we mispredict?"735 "target frame time is below last, did it overflow or did we mispredict?"
729 );736 );
730- return false;737+ return Duration::ZERO;
731 }738 }
732 739
733 let diff = target_frame_time - last;740 let diff = target_frame_time - last;
734 if diff < min {741 if diff < min {
742+ let delay = min - diff;
735 trace!(743 trace!(
736 ?target_frame_time,744 ?target_frame_time,
737 ?last,745 ?last,
738- "skipping frame because it is too soon: diff={diff:?} < min={min:?}",746+ "frame is too soon: min={min:?}, delay={:?}",
747+ delay
739 );748 );
740- return true;749+ return delay;
750+ } else {
751+ trace!("overshoot={:?}", diff - min);
741 }752 }
742 753
743- false754+ Duration::ZERO
755+ }
756+
757+ fn schedule_redraw(
758+ &mut self,
759+ event_loop: &LoopHandle<'static, State>,
760+ output: Output,
761+ target_time: Duration,
762+ ) {
763+ if self.scheduled_redraw.is_some() {
764+ return;
765+ }
766+
767+ let now = get_monotonic_time();
768+ let duration = target_time.saturating_sub(now);
769+ let timer = Timer::from_duration(duration);
770+ let token = event_loop
771+ .insert_source(timer, move |_, _, state| {
772+ state.niri.queue_redraw(&output);
773+ TimeoutAction::Drop
774+ })
775+ .unwrap();
776+ self.scheduled_redraw = Some(token);
777+ }
778+
779+ fn remove_scheduled_redraw(&mut self, event_loop: &LoopHandle<'static, State>) {
780+ if let Some(token) = self.scheduled_redraw.take() {
781+ event_loop.remove(token);
782+ }
783+ }
784+
785+ /// Checks whether this frame should be skipped because it's too soon.
786+ ///
787+ /// If the frame should be skipped, schedules a redraw and returns `true`. Otherwise, removes a
788+ /// scheduled redraw, if any, and returns `false`.
789+ ///
790+ /// When this method returns `false`, the calling code is assumed to follow up with
791+ /// [`Cast::dequeue_buffer_and_render()`].
792+ pub fn check_time_and_schedule(
793+ &mut self,
794+ event_loop: &LoopHandle<'static, State>,
795+ output: &Output,
796+ target_frame_time: Duration,
797+ ) -> bool {
798+ let delay = self.compute_extra_delay(target_frame_time);
799+ if delay >= CAST_DELAY_ALLOWANCE {
800+ trace!("delay >= allowance, scheduling redraw");
801+ self.schedule_redraw(event_loop, output.clone(), target_frame_time + delay);
802+ true
803+ } else {
804+ self.remove_scheduled_redraw(event_loop);
805+ false
806+ }
744 }807 }
745 808
746 pub fn dequeue_buffer_and_render(809 pub fn dequeue_buffer_and_render(