pw_utils: Wait for frame completion before queueing
Browse files at this commitf49ecc31c4d2a187c4c479e84fc6e6acaf5231f2Parent: 15b4acc5 changed files +101 -80
- niri-config/src/lib.rs +0 -3
- src/niri.rs +4 -27
- src/pw_utils.rs +94 -32
- wiki/Configuration:-Debug-Options.md +0 -17
- wiki/Nvidia.md +3 -1
niri-config/src/lib.rsmodified+0 -3
| @@ -2331,8 +2331,6 @@ pub struct DebugConfig { | ||
| 2331 | 2331 | #[knuffel(child)] |
| 2332 | 2332 | pub wait_for_frame_completion_before_queueing: bool, |
| 2333 | 2333 | #[knuffel(child)] |
| 2334 | - pub wait_for_frame_completion_in_pipewire: bool, | |
| 2335 | - #[knuffel(child)] | |
| 2336 | 2334 | pub enable_overlay_planes: bool, |
| 2337 | 2335 | #[knuffel(child)] |
| 2338 | 2336 | pub disable_cursor_plane: bool, |
| @@ -5327,7 +5325,6 @@ mod tests { | ||
| 5327 | 5325 | preview_render: None, |
| 5328 | 5326 | dbus_interfaces_in_non_session_instances: false, |
| 5329 | 5327 | wait_for_frame_completion_before_queueing: false, |
| 5330 | - wait_for_frame_completion_in_pipewire: false, | |
| 5331 | 5328 | enable_overlay_planes: false, |
| 5332 | 5329 | disable_cursor_plane: false, |
| 5333 | 5330 | disable_direct_scanout: false, |
src/niri.rsmodified+4 -27
| @@ -1881,12 +1881,8 @@ impl State { | ||
| 1881 | 1881 | |
| 1882 | 1882 | match &cast.target { |
| 1883 | 1883 | CastTarget::Nothing => { |
| 1884 | - let config = self.niri.config.borrow(); | |
| 1885 | - let wait_for_sync = config.debug.wait_for_frame_completion_in_pipewire; | |
| 1886 | - drop(config); | |
| 1887 | - | |
| 1888 | 1884 | self.backend.with_primary_renderer(|renderer| { |
| 1889 | - if cast.dequeue_buffer_and_clear(renderer, wait_for_sync) { | |
| 1885 | + if cast.dequeue_buffer_and_clear(renderer) { | |
| 1890 | 1886 | cast.last_frame_time = get_monotonic_time(); |
| 1891 | 1887 | } |
| 1892 | 1888 | }); |
| @@ -1926,10 +1922,6 @@ impl State { | ||
| 1926 | 1922 | } |
| 1927 | 1923 | } |
| 1928 | 1924 | |
| 1929 | - let config = self.niri.config.borrow(); | |
| 1930 | - let wait_for_sync = config.debug.wait_for_frame_completion_in_pipewire; | |
| 1931 | - drop(config); | |
| 1932 | - | |
| 1933 | 1925 | self.backend.with_primary_renderer(|renderer| { |
| 1934 | 1926 | // FIXME: pointer. |
| 1935 | 1927 | let elements = mapped |
| @@ -1937,13 +1929,7 @@ impl State { | ||
| 1937 | 1929 | .rev() |
| 1938 | 1930 | .collect::<Vec<_>>(); |
| 1939 | 1931 | |
| 1940 | - if cast.dequeue_buffer_and_render( | |
| 1941 | - renderer, | |
| 1942 | - &elements, | |
| 1943 | - bbox.size, | |
| 1944 | - scale, | |
| 1945 | - wait_for_sync, | |
| 1946 | - ) { | |
| 1932 | + if cast.dequeue_buffer_and_render(renderer, &elements, bbox.size, scale) { | |
| 1947 | 1933 | cast.last_frame_time = get_monotonic_time(); |
| 1948 | 1934 | } |
| 1949 | 1935 | }); |
| @@ -4994,10 +4980,6 @@ impl Niri { | ||
| 4994 | 4980 | |
| 4995 | 4981 | let scale = Scale::from(output.current_scale().fractional_scale()); |
| 4996 | 4982 | |
| 4997 | - let config = self.config.borrow(); | |
| 4998 | - let wait_for_sync = config.debug.wait_for_frame_completion_in_pipewire; | |
| 4999 | - drop(config); | |
| 5000 | - | |
| 5001 | 4983 | let mut elements = None; |
| 5002 | 4984 | let mut casts_to_stop = vec![]; |
| 5003 | 4985 | |
| @@ -5029,7 +5011,7 @@ impl Niri { | ||
| 5029 | 5011 | self.render(renderer, output, true, RenderTarget::Screencast) |
| 5030 | 5012 | }); |
| 5031 | 5013 | |
| 5032 | - if cast.dequeue_buffer_and_render(renderer, elements, size, scale, wait_for_sync) { | |
| 5014 | + if cast.dequeue_buffer_and_render(renderer, elements, size, scale) { | |
| 5033 | 5015 | cast.last_frame_time = target_presentation_time; |
| 5034 | 5016 | } |
| 5035 | 5017 | } |
| @@ -5051,10 +5033,6 @@ impl Niri { | ||
| 5051 | 5033 | |
| 5052 | 5034 | let scale = Scale::from(output.current_scale().fractional_scale()); |
| 5053 | 5035 | |
| 5054 | - let config = self.config.borrow(); | |
| 5055 | - let wait_for_sync = config.debug.wait_for_frame_completion_in_pipewire; | |
| 5056 | - drop(config); | |
| 5057 | - | |
| 5058 | 5036 | let mut casts_to_stop = vec![]; |
| 5059 | 5037 | |
| 5060 | 5038 | let mut casts = mem::take(&mut self.casts); |
| @@ -5093,8 +5071,7 @@ impl Niri { | ||
| 5093 | 5071 | // FIXME: pointer. |
| 5094 | 5072 | let elements: Vec<_> = mapped.render_for_screen_cast(renderer, scale).collect(); |
| 5095 | 5073 | |
| 5096 | - if cast.dequeue_buffer_and_render(renderer, &elements, bbox.size, scale, wait_for_sync) | |
| 5097 | - { | |
| 5074 | + if cast.dequeue_buffer_and_render(renderer, &elements, bbox.size, scale) { | |
| 5098 | 5075 | cast.last_frame_time = target_presentation_time; |
| 5099 | 5076 | } |
| 5100 | 5077 | } |
src/pw_utils.rsmodified+94 -32
| @@ -38,6 +38,7 @@ use smithay::backend::drm::DrmDeviceFd; | ||
| 38 | 38 | use smithay::backend::renderer::damage::OutputDamageTracker; |
| 39 | 39 | use smithay::backend::renderer::element::RenderElement; |
| 40 | 40 | use smithay::backend::renderer::gles::GlesRenderer; |
| 41 | +use smithay::backend::renderer::sync::SyncPoint; | |
| 41 | 42 | use smithay::output::{Output, OutputModeSource}; |
| 42 | 43 | use smithay::reexports::calloop::generic::Generic; |
| 43 | 44 | use smithay::reexports::calloop::{Interest, LoopHandle, Mode, PostAction}; |
| @@ -92,6 +93,13 @@ struct CastInner { | ||
| 92 | 93 | refresh: u32, |
| 93 | 94 | min_time_between_frames: Duration, |
| 94 | 95 | dmabufs: HashMap<i64, Dmabuf>, |
| 96 | + /// Buffers dequeued from PipeWire in process of rendering. | |
| 97 | + /// | |
| 98 | + /// This is an ordered list of buffers that we started rendering to and waiting for the | |
| 99 | + /// rendering to complete. The completion can be checked from the `SyncPoint`s. The buffers are | |
| 100 | + /// stored in order from oldest to newest, and the same ordering should be preserved when | |
| 101 | + /// submitting completed buffers to PipeWire. | |
| 102 | + rendering_buffers: Vec<(NonNull<pw_buffer>, SyncPoint)>, | |
| 95 | 103 | } |
| 96 | 104 | |
| 97 | 105 | #[allow(clippy::large_enum_variant)] |
| @@ -236,6 +244,7 @@ impl PipeWire { | ||
| 236 | 244 | refresh, |
| 237 | 245 | min_time_between_frames: Duration::ZERO, |
| 238 | 246 | dmabufs: HashMap::new(), |
| 247 | + rendering_buffers: Vec::new(), | |
| 239 | 248 | })); |
| 240 | 249 | |
| 241 | 250 | let listener = stream |
| @@ -655,6 +664,10 @@ impl PipeWire { | ||
| 655 | 664 | trace!("pw stream: remove_buffer"); |
| 656 | 665 | let mut inner = inner.borrow_mut(); |
| 657 | 666 | |
| 667 | + inner | |
| 668 | + .rendering_buffers | |
| 669 | + .retain(|(buf, _)| buf.as_ptr() != buffer); | |
| 670 | + | |
| 658 | 671 | unsafe { |
| 659 | 672 | let spa_buffer = (*buffer).buffer; |
| 660 | 673 | let spa_data = (*spa_buffer).datas; |
| @@ -856,13 +869,84 @@ impl Cast { | ||
| 856 | 869 | unsafe { NonNull::new(self.stream.dequeue_raw_buffer()) } |
| 857 | 870 | } |
| 858 | 871 | |
| 872 | + fn queue_completed_buffers(&mut self) { | |
| 873 | + let mut inner = self.inner.borrow_mut(); | |
| 874 | + | |
| 875 | + // We want to queue buffers in order, so find the first still-rendering buffer, and queue | |
| 876 | + // everything up to that. Even if there are completed buffers past the first | |
| 877 | + // still-rendering buffer, we do not want to queue them, since that would send frames out | |
| 878 | + // of order. | |
| 879 | + let first_in_progress_idx = inner | |
| 880 | + .rendering_buffers | |
| 881 | + .iter() | |
| 882 | + .position(|(_, sync)| !sync.is_reached()) | |
| 883 | + .unwrap_or(inner.rendering_buffers.len()); | |
| 884 | + | |
| 885 | + for (buffer, _) in inner.rendering_buffers.drain(..first_in_progress_idx) { | |
| 886 | + trace!("queueing completed buffer"); | |
| 887 | + unsafe { | |
| 888 | + pw_stream_queue_buffer(self.stream.as_raw_ptr(), buffer.as_ptr()); | |
| 889 | + } | |
| 890 | + } | |
| 891 | + } | |
| 892 | + | |
| 893 | + unsafe fn queue_after_sync(&mut self, pw_buffer: NonNull<pw_buffer>, sync_point: SyncPoint) { | |
| 894 | + let mut inner = self.inner.borrow_mut(); | |
| 895 | + | |
| 896 | + let mut sync_point = sync_point; | |
| 897 | + let sync_fd = match sync_point.export() { | |
| 898 | + Some(sync_fd) => Some(sync_fd), | |
| 899 | + None => { | |
| 900 | + // There are two main ways this can happen. First is that the SyncPoint is | |
| 901 | + // pre-signalled, then the buffer is already ready and no waiting is needed. Second | |
| 902 | + // is that the SyncPoint is potentially still not signalled, but exporting a fence | |
| 903 | + // fd had failed. In this case, there's not much we can do (perhaps do a blocking | |
| 904 | + // wait for the SyncPoint, which itself might fail). | |
| 905 | + // | |
| 906 | + // So let's hope for the best and mark the buffer as submittable. We do not reuse | |
| 907 | + // the original SyncPoint because if we do hit the second case (when it's not | |
| 908 | + // signalled), then without a sync fd we cannot schedule a queue upon its | |
| 909 | + // completion, effectively going stuck. It's better to queue an incomplete buffer | |
| 910 | + // than getting stuck. | |
| 911 | + sync_point = SyncPoint::signaled(); | |
| 912 | + None | |
| 913 | + } | |
| 914 | + }; | |
| 915 | + | |
| 916 | + inner.rendering_buffers.push((pw_buffer, sync_point)); | |
| 917 | + drop(inner); | |
| 918 | + | |
| 919 | + match sync_fd { | |
| 920 | + None => { | |
| 921 | + trace!("sync_fd is None, queueing completed buffers"); | |
| 922 | + // In case this is the only buffer in the list, we will queue it right away. | |
| 923 | + self.queue_completed_buffers(); | |
| 924 | + } | |
| 925 | + Some(sync_fd) => { | |
| 926 | + trace!("scheduling buffer to queue"); | |
| 927 | + let stream_id = self.stream_id; | |
| 928 | + let source = Generic::new(sync_fd, Interest::READ, Mode::OneShot); | |
| 929 | + self.event_loop | |
| 930 | + .insert_source(source, move |_, _, state| { | |
| 931 | + for cast in &mut state.niri.casts { | |
| 932 | + if cast.stream_id == stream_id { | |
| 933 | + cast.queue_completed_buffers(); | |
| 934 | + } | |
| 935 | + } | |
| 936 | + | |
| 937 | + Ok(PostAction::Remove) | |
| 938 | + }) | |
| 939 | + .unwrap(); | |
| 940 | + } | |
| 941 | + } | |
| 942 | + } | |
| 943 | + | |
| 859 | 944 | pub fn dequeue_buffer_and_render( |
| 860 | 945 | &mut self, |
| 861 | 946 | renderer: &mut GlesRenderer, |
| 862 | 947 | elements: &[impl RenderElement<GlesRenderer>], |
| 863 | 948 | size: Size<i32, Physical>, |
| 864 | 949 | scale: Scale<f64>, |
| 865 | - wait_for_sync: bool, | |
| 866 | 950 | ) -> bool { |
| 867 | 951 | let mut inner = self.inner.borrow_mut(); |
| 868 | 952 | |
| @@ -909,33 +993,20 @@ impl Cast { | ||
| 909 | 993 | elements.iter().rev(), |
| 910 | 994 | ) { |
| 911 | 995 | Ok(sync_point) => { |
| 912 | - // FIXME: implement PipeWire explicit sync, and at the very least async wait. | |
| 913 | - if wait_for_sync { | |
| 914 | - let _span = tracy_client::span!("wait for completion"); | |
| 915 | - if let Err(err) = sync_point.wait() { | |
| 916 | - warn!("error waiting for pw frame completion: {err:?}"); | |
| 917 | - } | |
| 918 | - } | |
| 996 | + mark_buffer_as_good(pw_buffer); | |
| 997 | + self.queue_after_sync(pw_buffer, sync_point); | |
| 998 | + true | |
| 919 | 999 | } |
| 920 | 1000 | Err(err) => { |
| 921 | 1001 | warn!("error rendering to dmabuf: {err:?}"); |
| 922 | 1002 | return_unused_buffer(&self.stream, pw_buffer); |
| 923 | - return false; | |
| 1003 | + false | |
| 924 | 1004 | } |
| 925 | 1005 | } |
| 926 | - | |
| 927 | - mark_buffer_as_good(pw_buffer); | |
| 928 | - pw_stream_queue_buffer(self.stream.as_raw_ptr(), buffer); | |
| 929 | 1006 | } |
| 930 | - | |
| 931 | - true | |
| 932 | 1007 | } |
| 933 | 1008 | |
| 934 | - pub fn dequeue_buffer_and_clear( | |
| 935 | - &mut self, | |
| 936 | - renderer: &mut GlesRenderer, | |
| 937 | - wait_for_sync: bool, | |
| 938 | - ) -> bool { | |
| 1009 | + pub fn dequeue_buffer_and_clear(&mut self, renderer: &mut GlesRenderer) -> bool { | |
| 939 | 1010 | let mut inner = self.inner.borrow_mut(); |
| 940 | 1011 | |
| 941 | 1012 | // Clear out the damage tracker if we're in Ready state. |
| @@ -958,26 +1029,17 @@ impl Cast { | ||
| 958 | 1029 | |
| 959 | 1030 | match clear_dmabuf(renderer, dmabuf) { |
| 960 | 1031 | Ok(sync_point) => { |
| 961 | - // FIXME: implement PipeWire explicit sync, and at the very least async wait. | |
| 962 | - if wait_for_sync { | |
| 963 | - let _span = tracy_client::span!("wait for completion"); | |
| 964 | - if let Err(err) = sync_point.wait() { | |
| 965 | - warn!("error waiting for pw frame completion: {err:?}"); | |
| 966 | - } | |
| 967 | - } | |
| 1032 | + mark_buffer_as_good(pw_buffer); | |
| 1033 | + self.queue_after_sync(pw_buffer, sync_point); | |
| 1034 | + true | |
| 968 | 1035 | } |
| 969 | 1036 | Err(err) => { |
| 970 | 1037 | warn!("error clearing dmabuf: {err:?}"); |
| 971 | 1038 | return_unused_buffer(&self.stream, pw_buffer); |
| 972 | - return false; | |
| 1039 | + false | |
| 973 | 1040 | } |
| 974 | 1041 | } |
| 975 | - | |
| 976 | - mark_buffer_as_good(pw_buffer); | |
| 977 | - pw_stream_queue_buffer(self.stream.as_raw_ptr(), buffer); | |
| 978 | 1042 | } |
| 979 | - | |
| 980 | - true | |
| 981 | 1043 | } |
| 982 | 1044 | } |
| 983 | 1045 |
wiki/Configuration:-Debug-Options.mdmodified+0 -17
| @@ -21,7 +21,6 @@ debug { | ||
| 21 | 21 | force-pipewire-invalid-modifier |
| 22 | 22 | dbus-interfaces-in-non-session-instances |
| 23 | 23 | wait-for-frame-completion-before-queueing |
| 24 | - wait-for-frame-completion-in-pipewire | |
| 25 | 24 | emulate-zero-presentation-time |
| 26 | 25 | disable-resize-throttling |
| 27 | 26 | disable-transactions |
| @@ -155,22 +154,6 @@ debug { | ||
| 155 | 154 | } |
| 156 | 155 | ``` |
| 157 | 156 | |
| 158 | -### `wait-for-frame-completion-in-pipewire` | |
| 159 | - | |
| 160 | -<sup>Since: 25.05</sup> | |
| 161 | - | |
| 162 | -Wait until every screencast frame is done rendering before handing it over to PipeWire. | |
| 163 | - | |
| 164 | -Sometimes helps on NVIDIA to prevent glitched frames when screencasting. | |
| 165 | - | |
| 166 | -This debug flag will eventually be removed once we handle this properly (via explicit sync in PipeWire). | |
| 167 | - | |
| 168 | -```kdl | |
| 169 | -debug { | |
| 170 | - wait-for-frame-completion-in-pipewire | |
| 171 | -} | |
| 172 | -``` | |
| 173 | - | |
| 174 | 157 | ### `emulate-zero-presentation-time` |
| 175 | 158 | |
| 176 | 159 | Emulate zero (unknown) presentation time returned from DRM. |
wiki/Nvidia.mdmodified+3 -1
| @@ -42,9 +42,11 @@ The fix shipped in the driver at the time of writing uses a value of 0, while th | ||
| 42 | 42 | |
| 43 | 43 | ### Screencast flickering fix |
| 44 | 44 | |
| 45 | +<sup>Until: next release</sup> | |
| 46 | + | |
| 45 | 47 | If you have screencast glitches or flickering on NVIDIA, set this in the niri config: |
| 46 | 48 | |
| 47 | -```kdl | |
| 49 | +```kdl,must-fail | |
| 48 | 50 | debug { |
| 49 | 51 | wait-for-frame-completion-in-pipewire |
| 50 | 52 | } |