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 {
36643664 }
36653665 }
36663666
3667- if cast.should_skip_frame(target_presentation_time) {
3667+ if cast.check_time_and_schedule(&self.event_loop, output, target_presentation_time) {
36683668 continue;
36693669 }
36703670
@@ -3726,7 +3726,7 @@ impl Niri {
37263726 }
37273727 }
37283728
3729- if cast.should_skip_frame(target_presentation_time) {
3729+ if cast.check_time_and_schedule(&self.event_loop, output, target_presentation_time) {
37303730 continue;
37313731 }
37323732
@@ -3805,7 +3805,7 @@ impl Niri {
38053805 }
38063806 }
38073807
3808- if cast.should_skip_frame(target_presentation_time) {
3808+ if cast.check_time_and_schedule(&self.event_loop, output, target_presentation_time) {
38093809 continue;
38103810 }
38113811
src/pw_utils.rsmodified+74 -11
@@ -8,6 +8,8 @@ use std::rc::Rc;
88 use std::time::Duration;
99
1010 use anyhow::Context as _;
11+use calloop::timer::{TimeoutAction, Timer};
12+use calloop::RegistrationToken;
1113 use pipewire::context::Context;
1214 use pipewire::core::Core;
1315 use pipewire::main_loop::MainLoop;
@@ -34,7 +36,7 @@ use smithay::backend::drm::DrmDeviceFd;
3436 use smithay::backend::renderer::damage::OutputDamageTracker;
3537 use smithay::backend::renderer::element::RenderElement;
3638 use smithay::backend::renderer::gles::GlesRenderer;
37-use smithay::output::{OutputModeSource, WeakOutput};
39+use smithay::output::{Output, OutputModeSource, WeakOutput};
3840 use smithay::reexports::calloop::generic::Generic;
3941 use smithay::reexports::calloop::{Interest, LoopHandle, Mode, PostAction};
4042 use smithay::reexports::gbm::Modifier;
@@ -44,6 +46,10 @@ use zbus::SignalContext;
4446 use crate::dbus::mutter_screen_cast::{self, CursorMode};
4547 use crate::niri::State;
4648 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);
4753
4854 pub struct PipeWire {
4955 _context: Context,
@@ -70,6 +76,7 @@ pub struct Cast {
7076 pub last_frame_time: Duration,
7177 min_time_between_frames: Rc<Cell<Duration>>,
7278 dmabufs: Rc<RefCell<HashMap<i64, Dmabuf>>>,
79+ scheduled_redraw: Option<RegistrationToken>,
7380 }
7481
7582 #[derive(Debug)]
@@ -315,10 +322,9 @@ impl PipeWire {
315322 };
316323
317324 let max_frame_rate = format.max_framerate();
318- // Subtract 0.5 ms to improve edge cases when equal to refresh rate.
319- let min_frame_time = Duration::from_secs_f64(
320- max_frame_rate.denom as f64 / max_frame_rate.num as f64,
321- ) - Duration::from_micros(500);
325+ let min_frame_time = Duration::from_micros(
326+ 1_000_000 * u64::from(max_frame_rate.denom) / u64::from(max_frame_rate.num),
327+ );
322328 min_time_between_frames.set(min_frame_time);
323329
324330 let object = pod.as_object().unwrap();
@@ -651,6 +657,7 @@ impl PipeWire {
651657 last_frame_time: Duration::ZERO,
652658 min_time_between_frames,
653659 dmabufs,
660+ scheduled_redraw: None,
654661 };
655662 Ok(cast)
656663 }
@@ -711,13 +718,13 @@ impl Cast {
711718 Ok(())
712719 }
713720
714- pub fn should_skip_frame(&self, target_frame_time: Duration) -> bool {
721+ fn compute_extra_delay(&self, target_frame_time: Duration) -> Duration {
715722 let last = self.last_frame_time;
716723 let min = self.min_time_between_frames.get();
717724
718725 if last.is_zero() {
719726 trace!(?target_frame_time, ?last, "last is zero, recording");
720- return false;
727+ return Duration::ZERO;
721728 }
722729
723730 if target_frame_time < last {
@@ -727,20 +734,76 @@ impl Cast {
727734 ?last,
728735 "target frame time is below last, did it overflow or did we mispredict?"
729736 );
730- return false;
737+ return Duration::ZERO;
731738 }
732739
733740 let diff = target_frame_time - last;
734741 if diff < min {
742+ let delay = min - diff;
735743 trace!(
736744 ?target_frame_time,
737745 ?last,
738- "skipping frame because it is too soon: diff={diff:?} < min={min:?}",
746+ "frame is too soon: min={min:?}, delay={:?}",
747+ delay
739748 );
740- return true;
749+ return delay;
750+ } else {
751+ trace!("overshoot={:?}", diff - min);
741752 }
742753
743- false
754+ 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+ }
744807 }
745808
746809 pub fn dequeue_buffer_and_render(