Refactor wlr-screencopy state cleanup
Browse files at this commit9c79108afa0250faef1ac0f1da815b4b84ed6effParent: 25712423 changed files +119 -41
- src/handlers/mod.rs +1 -5
- src/niri.rs +6 -14
- src/protocols/screencopy.rs +112 -22
src/handlers/mod.rsmodified+1 -5
| @@ -624,11 +624,7 @@ impl ScreencopyHandler for State { | |||
| 624 | // If with_damage then push it onto the queue for redraw of the output, | 624 | // If with_damage then push it onto the queue for redraw of the output, |
| 625 | // otherwise render it immediately. | 625 | // otherwise render it immediately. |
| 626 | if screencopy.with_damage() { | 626 | if screencopy.with_damage() { |
| 627 | - let Some(queue) = self.niri.screencopy_state.get_queue_mut(manager) else { | 627 | + self.niri.screencopy_state.push(manager, screencopy); |
| 628 | - trace!("screencopy manager destroyed already"); | ||
| 629 | - return; | ||
| 630 | - }; | ||
| 631 | - queue.push(screencopy); | ||
| 632 | } else { | 628 | } else { |
| 633 | self.backend.with_primary_renderer(|renderer| { | 629 | self.backend.with_primary_renderer(|renderer| { |
| 634 | if let Err(err) = self | 630 | if let Err(err) = self |
src/niri.rsmodified+6 -14
| @@ -2817,8 +2817,7 @@ impl Niri { | |||
| 2817 | } | 2817 | } |
| 2818 | 2818 | ||
| 2819 | self.stop_casts_for_target(CastTarget::output(output)); | 2819 | self.stop_casts_for_target(CastTarget::output(output)); |
| 2820 | - | 2820 | + self.screencopy_state.remove_output(output); |
| 2821 | - self.remove_screencopy_output(output); | ||
| 2822 | 2821 | ||
| 2823 | // Disable the output global and remove some time later to give the clients some time to | 2822 | // Disable the output global and remove some time later to give the clients some time to |
| 2824 | // process it. | 2823 | // process it. |
| @@ -4900,7 +4899,7 @@ impl Niri { | |||
| 4900 | let mut screencopy_state = mem::take(&mut self.screencopy_state); | 4899 | let mut screencopy_state = mem::take(&mut self.screencopy_state); |
| 4901 | let elements = OnceCell::new(); | 4900 | let elements = OnceCell::new(); |
| 4902 | 4901 | ||
| 4903 | - for queue in screencopy_state.queues_mut() { | 4902 | + screencopy_state.with_queues_mut(|queue| { |
| 4904 | let (damage_tracker, screencopy) = queue.split(); | 4903 | let (damage_tracker, screencopy) = queue.split(); |
| 4905 | if let Some(screencopy) = screencopy { | 4904 | if let Some(screencopy) = screencopy { |
| 4906 | if screencopy.output() == output { | 4905 | if screencopy.output() == output { |
| @@ -4947,7 +4946,7 @@ impl Niri { | |||
| 4947 | } | 4946 | } |
| 4948 | }; | 4947 | }; |
| 4949 | } | 4948 | } |
| 4950 | - } | 4949 | + }); |
| 4951 | 4950 | ||
| 4952 | self.screencopy_state = screencopy_state; | 4951 | self.screencopy_state = screencopy_state; |
| 4953 | } | 4952 | } |
| @@ -4974,10 +4973,10 @@ impl Niri { | |||
| 4974 | screencopy.overlay_cursor(), | 4973 | screencopy.overlay_cursor(), |
| 4975 | RenderTarget::ScreenCapture, | 4974 | RenderTarget::ScreenCapture, |
| 4976 | ); | 4975 | ); |
| 4977 | - let Some(queue) = self.screencopy_state.get_queue_mut(manager) else { | 4976 | + let Some(damage_tracker) = self.screencopy_state.damage_tracker(manager) else { |
| 4978 | - bail!("screencopy manager destroyed already"); | 4977 | + error!("screencopy queue must not be deleted as long as frames exist"); |
| 4978 | + bail!("screencopy queue missing"); | ||
| 4979 | }; | 4979 | }; |
| 4980 | - let damage_tracker = queue.split().0; | ||
| 4981 | 4980 | ||
| 4982 | let render_result = Self::render_for_screencopy_internal( | 4981 | let render_result = Self::render_for_screencopy_internal( |
| 4983 | renderer, | 4982 | renderer, |
| @@ -5065,13 +5064,6 @@ impl Niri { | |||
| 5065 | #[cfg(not(feature = "xdp-gnome-screencast"))] | 5064 | #[cfg(not(feature = "xdp-gnome-screencast"))] |
| 5066 | pub fn stop_casts_for_target(&mut self, _target: CastTarget) {} | 5065 | pub fn stop_casts_for_target(&mut self, _target: CastTarget) {} |
| 5067 | 5066 | ||
| 5068 | - pub fn remove_screencopy_output(&mut self, output: &Output) { | ||
| 5069 | - let _span = tracy_client::span!("Niri::remove_screencopy_output"); | ||
| 5070 | - for queue in self.screencopy_state.queues_mut() { | ||
| 5071 | - queue.remove_output(output); | ||
| 5072 | - } | ||
| 5073 | - } | ||
| 5074 | - | ||
| 5075 | pub fn debug_toggle_damage(&mut self) { | 5067 | pub fn debug_toggle_damage(&mut self) { |
| 5076 | self.debug_draw_damage = !self.debug_draw_damage; | 5068 | self.debug_draw_damage = !self.debug_draw_damage; |
| 5077 | 5069 | ||
src/protocols/screencopy.rsmodified+112 -22
| @@ -1,4 +1,4 @@ | |||
| 1 | -use std::collections::HashMap; | 1 | +use std::collections::{HashMap, HashSet}; |
| 2 | use std::sync::atomic::{AtomicBool, Ordering}; | 2 | use std::sync::atomic::{AtomicBool, Ordering}; |
| 3 | use std::sync::Arc; | 3 | use std::sync::Arc; |
| 4 | use std::time::Duration; | 4 | use std::time::Duration; |
| @@ -10,10 +10,6 @@ use smithay::backend::allocator::{Buffer, Fourcc}; | |||
| 10 | use smithay::backend::renderer::damage::OutputDamageTracker; | 10 | use smithay::backend::renderer::damage::OutputDamageTracker; |
| 11 | use smithay::backend::renderer::sync::SyncPoint; | 11 | use smithay::backend::renderer::sync::SyncPoint; |
| 12 | use smithay::output::Output; | 12 | use smithay::output::Output; |
| 13 | -use smithay::reexports::wayland_protocols_wlr::screencopy::v1::server::zwlr_screencopy_frame_v1::{ | ||
| 14 | - Flags, ZwlrScreencopyFrameV1, | ||
| 15 | -}; | ||
| 16 | -use smithay::reexports::wayland_protocols_wlr::screencopy::v1::server::zwlr_screencopy_manager_v1::ZwlrScreencopyManagerV1; | ||
| 17 | use smithay::reexports::wayland_protocols_wlr::screencopy::v1::server::{ | 13 | use smithay::reexports::wayland_protocols_wlr::screencopy::v1::server::{ |
| 18 | zwlr_screencopy_frame_v1, zwlr_screencopy_manager_v1, | 14 | zwlr_screencopy_frame_v1, zwlr_screencopy_manager_v1, |
| 19 | }; | 15 | }; |
| @@ -24,6 +20,8 @@ use smithay::reexports::wayland_server::{ | |||
| 24 | }; | 20 | }; |
| 25 | use smithay::utils::{Physical, Point, Rectangle, Size, Transform}; | 21 | use smithay::utils::{Physical, Point, Rectangle, Size, Transform}; |
| 26 | use smithay::wayland::{dmabuf, shm}; | 22 | use smithay::wayland::{dmabuf, shm}; |
| 23 | +use zwlr_screencopy_frame_v1::{Flags, ZwlrScreencopyFrameV1}; | ||
| 24 | +use zwlr_screencopy_manager_v1::ZwlrScreencopyManagerV1; | ||
| 27 | 25 | ||
| 28 | use crate::utils::get_monotonic_time; | 26 | use crate::utils::get_monotonic_time; |
| 29 | 27 | ||
| @@ -31,6 +29,9 @@ const VERSION: u32 = 3; | |||
| 31 | 29 | ||
| 32 | pub struct ScreencopyQueue { | 30 | pub struct ScreencopyQueue { |
| 33 | damage_tracker: OutputDamageTracker, | 31 | damage_tracker: OutputDamageTracker, |
| 32 | + /// Frames waiting for the client to call copy or destroy. | ||
| 33 | + pending_frames: HashSet<ZwlrScreencopyFrameV1>, | ||
| 34 | + /// Queue of screencopies waiting for a corresponding output redraw with damage. | ||
| 34 | screencopies: Vec<Screencopy>, | 35 | screencopies: Vec<Screencopy>, |
| 35 | } | 36 | } |
| 36 | 37 | ||
| @@ -44,30 +45,36 @@ impl ScreencopyQueue { | |||
| 44 | pub fn new() -> Self { | 45 | pub fn new() -> Self { |
| 45 | Self { | 46 | Self { |
| 46 | damage_tracker: OutputDamageTracker::new((0, 0), 1.0, Transform::Normal), | 47 | damage_tracker: OutputDamageTracker::new((0, 0), 1.0, Transform::Normal), |
| 48 | + pending_frames: HashSet::new(), | ||
| 47 | screencopies: Vec::new(), | 49 | screencopies: Vec::new(), |
| 48 | } | 50 | } |
| 49 | } | 51 | } |
| 50 | 52 | ||
| 53 | + pub fn is_empty(&self) -> bool { | ||
| 54 | + self.pending_frames.is_empty() && self.screencopies.is_empty() | ||
| 55 | + } | ||
| 56 | + | ||
| 51 | pub fn split(&mut self) -> (&mut OutputDamageTracker, Option<&Screencopy>) { | 57 | pub fn split(&mut self) -> (&mut OutputDamageTracker, Option<&Screencopy>) { |
| 52 | let ScreencopyQueue { | 58 | let ScreencopyQueue { |
| 53 | damage_tracker, | 59 | damage_tracker, |
| 54 | screencopies, | 60 | screencopies, |
| 61 | + .. | ||
| 55 | } = self; | 62 | } = self; |
| 56 | (damage_tracker, screencopies.first()) | 63 | (damage_tracker, screencopies.first()) |
| 57 | } | 64 | } |
| 58 | 65 | ||
| 59 | pub fn push(&mut self, screencopy: Screencopy) { | 66 | pub fn push(&mut self, screencopy: Screencopy) { |
| 67 | + // Screencopy without damage is rendered immediately without the queue. | ||
| 68 | + if !screencopy.with_damage() { | ||
| 69 | + error!("only screencopy with damage can be pushed in the queue"); | ||
| 70 | + } | ||
| 71 | + | ||
| 60 | self.screencopies.push(screencopy); | 72 | self.screencopies.push(screencopy); |
| 61 | } | 73 | } |
| 62 | 74 | ||
| 63 | pub fn pop(&mut self) -> Screencopy { | 75 | pub fn pop(&mut self) -> Screencopy { |
| 64 | self.screencopies.remove(0) | 76 | self.screencopies.remove(0) |
| 65 | } | 77 | } |
| 66 | - | ||
| 67 | - pub fn remove_output(&mut self, output: &Output) { | ||
| 68 | - self.screencopies | ||
| 69 | - .retain(|screencopy| screencopy.output() != output); | ||
| 70 | - } | ||
| 71 | } | 78 | } |
| 72 | 79 | ||
| 73 | #[derive(Default)] | 80 | #[derive(Default)] |
| @@ -99,23 +106,46 @@ impl ScreencopyManagerState { | |||
| 99 | } | 106 | } |
| 100 | } | 107 | } |
| 101 | 108 | ||
| 102 | - pub fn bind(&mut self, manager: &ZwlrScreencopyManagerV1) { | 109 | + pub fn push(&mut self, manager: &ZwlrScreencopyManagerV1, screencopy: Screencopy) { |
| 103 | - // Clean up all entries if its manager is dead and its queue is empty. | 110 | + let Some(queue) = self.queues.get_mut(manager) else { |
| 104 | - self.queues | 111 | + // Destroying the manager does not invalidate existing frames, so the queue should |
| 105 | - .retain(|k, v| k.is_alive() || !v.screencopies.is_empty()); | 112 | + // keep existing. |
| 113 | + error!("screencopy queue must not be deleted as long as frames exist"); | ||
| 114 | + return; | ||
| 115 | + }; | ||
| 106 | 116 | ||
| 107 | - self.queues.insert(manager.clone(), ScreencopyQueue::new()); | 117 | + queue.push(screencopy); |
| 108 | } | 118 | } |
| 109 | 119 | ||
| 110 | - pub fn get_queue_mut( | 120 | + pub fn damage_tracker( |
| 111 | &mut self, | 121 | &mut self, |
| 112 | manager: &ZwlrScreencopyManagerV1, | 122 | manager: &ZwlrScreencopyManagerV1, |
| 113 | - ) -> Option<&mut ScreencopyQueue> { | 123 | + ) -> Option<&mut OutputDamageTracker> { |
| 114 | - self.queues.get_mut(manager) | 124 | + let queue = self.queues.get_mut(manager)?; |
| 125 | + Some(&mut queue.damage_tracker) | ||
| 126 | + } | ||
| 127 | + | ||
| 128 | + pub fn remove_output(&mut self, output: &Output) { | ||
| 129 | + for queue in self.queues.values_mut() { | ||
| 130 | + queue | ||
| 131 | + .screencopies | ||
| 132 | + .retain(|screencopy| screencopy.output() != output); | ||
| 133 | + } | ||
| 134 | + | ||
| 135 | + self.cleanup_queues(); | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + pub fn with_queues_mut(&mut self, mut f: impl FnMut(&mut ScreencopyQueue)) { | ||
| 139 | + for queue in self.queues.values_mut() { | ||
| 140 | + f(queue); | ||
| 141 | + } | ||
| 142 | + | ||
| 143 | + self.cleanup_queues(); | ||
| 115 | } | 144 | } |
| 116 | 145 | ||
| 117 | - pub fn queues_mut(&mut self) -> impl Iterator<Item = &mut ScreencopyQueue> { | 146 | + fn cleanup_queues(&mut self) { |
| 118 | - self.queues.values_mut() | 147 | + self.queues |
| 148 | + .retain(|manager, queue| manager.is_alive() || !queue.is_empty()); | ||
| 119 | } | 149 | } |
| 120 | } | 150 | } |
| 121 | 151 | ||
| @@ -137,7 +167,9 @@ where | |||
| 137 | data_init: &mut DataInit<'_, D>, | 167 | data_init: &mut DataInit<'_, D>, |
| 138 | ) { | 168 | ) { |
| 139 | let manager = data_init.init(manager, ()); | 169 | let manager = data_init.init(manager, ()); |
| 140 | - state.screencopy_state().bind(&manager); | 170 | + |
| 171 | + let state = state.screencopy_state(); | ||
| 172 | + state.queues.insert(manager.clone(), ScreencopyQueue::new()); | ||
| 141 | } | 173 | } |
| 142 | 174 | ||
| 143 | fn can_view(client: Client, global_data: &ScreencopyManagerGlobalData) -> bool { | 175 | fn can_view(client: Client, global_data: &ScreencopyManagerGlobalData) -> bool { |
| @@ -154,7 +186,7 @@ where | |||
| 154 | D: 'static, | 186 | D: 'static, |
| 155 | { | 187 | { |
| 156 | fn request( | 188 | fn request( |
| 157 | - _state: &mut D, | 189 | + state: &mut D, |
| 158 | _client: &Client, | 190 | _client: &Client, |
| 159 | manager: &ZwlrScreencopyManagerV1, | 191 | manager: &ZwlrScreencopyManagerV1, |
| 160 | request: zwlr_screencopy_manager_v1::Request, | 192 | request: zwlr_screencopy_manager_v1::Request, |
| @@ -273,13 +305,35 @@ where | |||
| 273 | // Notify client that all supported buffers were enumerated. | 305 | // Notify client that all supported buffers were enumerated. |
| 274 | frame.buffer_done(); | 306 | frame.buffer_done(); |
| 275 | } | 307 | } |
| 308 | + | ||
| 309 | + let state = state.screencopy_state(); | ||
| 310 | + let queue = state.queues.get_mut(manager).unwrap(); | ||
| 311 | + queue.pending_frames.insert(frame); | ||
| 312 | + } | ||
| 313 | + | ||
| 314 | + fn destroyed( | ||
| 315 | + state: &mut D, | ||
| 316 | + _client: wayland_backend::server::ClientId, | ||
| 317 | + manager: &ZwlrScreencopyManagerV1, | ||
| 318 | + _data: &(), | ||
| 319 | + ) { | ||
| 320 | + let state = state.screencopy_state(); | ||
| 321 | + let queue = state.queues.get_mut(manager).unwrap(); | ||
| 322 | + // Clean up the queue if this was the last object. | ||
| 323 | + if queue.is_empty() { | ||
| 324 | + state.queues.remove(manager); | ||
| 325 | + } | ||
| 276 | } | 326 | } |
| 277 | } | 327 | } |
| 278 | 328 | ||
| 279 | /// Handler trait for wlr-screencopy. | 329 | /// Handler trait for wlr-screencopy. |
| 280 | pub trait ScreencopyHandler { | 330 | pub trait ScreencopyHandler { |
| 281 | /// Handle new screencopy request. | 331 | /// Handle new screencopy request. |
| 332 | + /// | ||
| 333 | + /// The handler must synchronously either ready/fail the screencopy, or submit it to the | ||
| 334 | + /// manager queue. | ||
| 282 | fn frame(&mut self, manager: &ZwlrScreencopyManagerV1, screencopy: Screencopy); | 335 | fn frame(&mut self, manager: &ZwlrScreencopyManagerV1, screencopy: Screencopy); |
| 336 | + | ||
| 283 | fn screencopy_state(&mut self) -> &mut ScreencopyManagerState; | 337 | fn screencopy_state(&mut self) -> &mut ScreencopyManagerState; |
| 284 | } | 338 | } |
| 285 | 339 | ||
| @@ -405,6 +459,42 @@ where | |||
| 405 | submitted: false, | 459 | submitted: false, |
| 406 | }, | 460 | }, |
| 407 | ); | 461 | ); |
| 462 | + | ||
| 463 | + // By this point the frame should've been either copied or failed or pushed to the queue, | ||
| 464 | + // so remove it from pending frames. | ||
| 465 | + let state = state.screencopy_state(); | ||
| 466 | + let queue = state.queues.get_mut(manager).unwrap(); | ||
| 467 | + queue.pending_frames.remove(frame); | ||
| 468 | + if queue.is_empty() && !manager.is_alive() { | ||
| 469 | + state.queues.remove(manager); | ||
| 470 | + } | ||
| 471 | + } | ||
| 472 | + | ||
| 473 | + fn destroyed( | ||
| 474 | + state: &mut D, | ||
| 475 | + _client: wayland_backend::server::ClientId, | ||
| 476 | + frame: &ZwlrScreencopyFrameV1, | ||
| 477 | + data: &ScreencopyFrameState, | ||
| 478 | + ) { | ||
| 479 | + let ScreencopyFrameState::Pending { manager, .. } = data else { | ||
| 480 | + return; | ||
| 481 | + }; | ||
| 482 | + | ||
| 483 | + let state = state.screencopy_state(); | ||
| 484 | + let Some(queue) = state.queues.get_mut(manager) else { | ||
| 485 | + // I think this can happen when we post_error() on a pending frame? Either way better | ||
| 486 | + // safe than sorry. | ||
| 487 | + return; | ||
| 488 | + }; | ||
| 489 | + | ||
| 490 | + queue.pending_frames.remove(frame); | ||
| 491 | + queue | ||
| 492 | + .screencopies | ||
| 493 | + .retain(|screencopy| screencopy.frame != *frame); | ||
| 494 | + // Clean up the queue if this was the last object. | ||
| 495 | + if queue.is_empty() && !manager.is_alive() { | ||
| 496 | + state.queues.remove(manager); | ||
| 497 | + } | ||
| 408 | } | 498 | } |
| 409 | } | 499 | } |
| 410 | 500 | ||