Store cast Stream ID, use it for Redraw request
Browse files at this commit8c4ebb00a1e7512dc00ac3418a9cf67322e7000cParent: f6aa8c13 changed files +99 -117
- src/dbus/mutter_screen_cast.rs +29 -13
- src/niri.rs +65 -101
- src/pw_utils.rs +5 -3
src/dbus/mutter_screen_cast.rsmodified+29 -13
| @@ -62,6 +62,8 @@ static STREAM_ID: AtomicUsize = AtomicUsize::new(0); | ||
| 62 | 62 | |
| 63 | 63 | #[derive(Clone)] |
| 64 | 64 | pub struct Stream { |
| 65 | + id: usize, | |
| 66 | + session_id: usize, | |
| 65 | 67 | target: StreamTarget, |
| 66 | 68 | cursor_mode: CursorMode, |
| 67 | 69 | was_started: Arc<AtomicBool>, |
| @@ -93,6 +95,7 @@ struct StreamParameters { | ||
| 93 | 95 | pub enum ScreenCastToNiri { |
| 94 | 96 | StartCast { |
| 95 | 97 | session_id: usize, |
| 98 | + stream_id: usize, | |
| 96 | 99 | target: StreamTargetId, |
| 97 | 100 | cursor_mode: CursorMode, |
| 98 | 101 | signal_ctx: SignalEmitter<'static>, |
| @@ -149,7 +152,7 @@ impl Session { | ||
| 149 | 152 | debug!("start"); |
| 150 | 153 | |
| 151 | 154 | for (stream, iface) in &*self.streams.lock().unwrap() { |
| 152 | - stream.start(self.id, iface.signal_emitter().clone()); | |
| 155 | + stream.start(iface.signal_emitter().clone()); | |
| 153 | 156 | } |
| 154 | 157 | } |
| 155 | 158 | |
| @@ -204,16 +207,20 @@ impl Session { | ||
| 204 | 207 | return Err(fdo::Error::Failed("monitor is disabled".to_owned())); |
| 205 | 208 | } |
| 206 | 209 | |
| 207 | - let path = format!( | |
| 208 | - "/org/gnome/Mutter/ScreenCast/Stream/u{}", | |
| 209 | - STREAM_ID.fetch_add(1, Ordering::SeqCst) | |
| 210 | - ); | |
| 210 | + let stream_id = STREAM_ID.fetch_add(1, Ordering::SeqCst); | |
| 211 | + let path = format!("/org/gnome/Mutter/ScreenCast/Stream/u{stream_id}"); | |
| 211 | 212 | let path = OwnedObjectPath::try_from(path).unwrap(); |
| 212 | 213 | |
| 213 | 214 | let cursor_mode = properties.cursor_mode.unwrap_or_default(); |
| 214 | 215 | |
| 215 | 216 | let target = StreamTarget::Output(output); |
| 216 | - let stream = Stream::new(target, cursor_mode, self.to_niri.clone()); | |
| 217 | + let stream = Stream::new( | |
| 218 | + stream_id, | |
| 219 | + self.id, | |
| 220 | + target, | |
| 221 | + cursor_mode, | |
| 222 | + self.to_niri.clone(), | |
| 223 | + ); | |
| 217 | 224 | match server.at(&path, stream.clone()).await { |
| 218 | 225 | Ok(true) => { |
| 219 | 226 | let iface = server.interface(&path).await.unwrap(); |
| @@ -237,10 +244,8 @@ impl Session { | ||
| 237 | 244 | ) -> fdo::Result<OwnedObjectPath> { |
| 238 | 245 | debug!(?properties, "record_window"); |
| 239 | 246 | |
| 240 | - let path = format!( | |
| 241 | - "/org/gnome/Mutter/ScreenCast/Stream/u{}", | |
| 242 | - STREAM_ID.fetch_add(1, Ordering::SeqCst) | |
| 243 | - ); | |
| 247 | + let stream_id = STREAM_ID.fetch_add(1, Ordering::SeqCst); | |
| 248 | + let path = format!("/org/gnome/Mutter/ScreenCast/Stream/u{stream_id}"); | |
| 244 | 249 | let path = OwnedObjectPath::try_from(path).unwrap(); |
| 245 | 250 | |
| 246 | 251 | let cursor_mode = properties.cursor_mode.unwrap_or_default(); |
| @@ -248,7 +253,13 @@ impl Session { | ||
| 248 | 253 | let target = StreamTarget::Window { |
| 249 | 254 | id: properties.window_id, |
| 250 | 255 | }; |
| 251 | - let stream = Stream::new(target, cursor_mode, self.to_niri.clone()); | |
| 256 | + let stream = Stream::new( | |
| 257 | + stream_id, | |
| 258 | + self.id, | |
| 259 | + target, | |
| 260 | + cursor_mode, | |
| 261 | + self.to_niri.clone(), | |
| 262 | + ); | |
| 252 | 263 | match server.at(&path, stream.clone()).await { |
| 253 | 264 | Ok(true) => { |
| 254 | 265 | let iface = server.interface(&path).await.unwrap(); |
| @@ -350,11 +361,15 @@ impl Drop for Session { | ||
| 350 | 361 | |
| 351 | 362 | impl Stream { |
| 352 | 363 | fn new( |
| 364 | + id: usize, | |
| 365 | + session_id: usize, | |
| 353 | 366 | target: StreamTarget, |
| 354 | 367 | cursor_mode: CursorMode, |
| 355 | 368 | to_niri: calloop::channel::Sender<ScreenCastToNiri>, |
| 356 | 369 | ) -> Self { |
| 357 | 370 | Self { |
| 371 | + id, | |
| 372 | + session_id, | |
| 358 | 373 | target, |
| 359 | 374 | cursor_mode, |
| 360 | 375 | was_started: Arc::new(AtomicBool::new(false)), |
| @@ -362,13 +377,14 @@ impl Stream { | ||
| 362 | 377 | } |
| 363 | 378 | } |
| 364 | 379 | |
| 365 | - fn start(&self, session_id: usize, ctxt: SignalEmitter<'static>) { | |
| 380 | + fn start(&self, ctxt: SignalEmitter<'static>) { | |
| 366 | 381 | if self.was_started.load(Ordering::SeqCst) { |
| 367 | 382 | return; |
| 368 | 383 | } |
| 369 | 384 | |
| 370 | 385 | let msg = ScreenCastToNiri::StartCast { |
| 371 | - session_id, | |
| 386 | + session_id: self.session_id, | |
| 387 | + stream_id: self.id, | |
| 372 | 388 | target: self.target.make_id(), |
| 373 | 389 | cursor_mode: self.cursor_mode, |
| 374 | 390 | signal_ctx: ctxt, |
src/niri.rsmodified+65 -101
| @@ -1592,20 +1592,7 @@ impl State { | ||
| 1592 | 1592 | pub fn on_pw_msg(&mut self, msg: PwToNiri) { |
| 1593 | 1593 | match msg { |
| 1594 | 1594 | PwToNiri::StopCast { session_id } => self.niri.stop_cast(session_id), |
| 1595 | - PwToNiri::Redraw(target) => match target { | |
| 1596 | - CastTarget::Output(weak) => { | |
| 1597 | - if let Some(output) = weak.upgrade() { | |
| 1598 | - self.niri.queue_redraw(&output); | |
| 1599 | - } | |
| 1600 | - } | |
| 1601 | - CastTarget::Window { id } => { | |
| 1602 | - self.backend.with_primary_renderer(|renderer| { | |
| 1603 | - // FIXME: target presentation time at the time of window commit? | |
| 1604 | - self.niri | |
| 1605 | - .render_window_for_screen_cast(renderer, id, get_monotonic_time()); | |
| 1606 | - }); | |
| 1607 | - } | |
| 1608 | - }, | |
| 1595 | + PwToNiri::Redraw { stream_id } => self.redraw_cast(stream_id), | |
| 1609 | 1596 | PwToNiri::FatalError => { |
| 1610 | 1597 | warn!("stopping PipeWire due to fatal error"); |
| 1611 | 1598 | if let Some(pw) = self.niri.pipewire.take() { |
| @@ -1619,6 +1606,67 @@ impl State { | ||
| 1619 | 1606 | } |
| 1620 | 1607 | } |
| 1621 | 1608 | |
| 1609 | + #[cfg(feature = "xdp-gnome-screencast")] | |
| 1610 | + fn redraw_cast(&mut self, stream_id: usize) { | |
| 1611 | + let _span = tracy_client::span!("State::redraw_cast"); | |
| 1612 | + | |
| 1613 | + let casts = &mut self.niri.casts; | |
| 1614 | + let Some(cast) = casts.iter_mut().find(|cast| cast.stream_id == stream_id) else { | |
| 1615 | + warn!("cast to redraw is missing"); | |
| 1616 | + return; | |
| 1617 | + }; | |
| 1618 | + | |
| 1619 | + match &cast.target { | |
| 1620 | + CastTarget::Output(weak) => { | |
| 1621 | + if let Some(output) = weak.upgrade() { | |
| 1622 | + self.niri.queue_redraw(&output); | |
| 1623 | + } | |
| 1624 | + } | |
| 1625 | + CastTarget::Window { id } => { | |
| 1626 | + let mut windows = self.niri.layout.windows(); | |
| 1627 | + let Some((_, mapped)) = windows.find(|(_, mapped)| mapped.id().get() == *id) else { | |
| 1628 | + return; | |
| 1629 | + }; | |
| 1630 | + | |
| 1631 | + // Use the cached output since it will be present even if the output was | |
| 1632 | + // currently disconnected. | |
| 1633 | + let Some(output) = self.niri.mapped_cast_output.get(&mapped.window) else { | |
| 1634 | + return; | |
| 1635 | + }; | |
| 1636 | + | |
| 1637 | + let scale = Scale::from(output.current_scale().fractional_scale()); | |
| 1638 | + let bbox = mapped | |
| 1639 | + .window | |
| 1640 | + .bbox_with_popups() | |
| 1641 | + .to_physical_precise_up(scale); | |
| 1642 | + | |
| 1643 | + match cast.ensure_size(bbox.size) { | |
| 1644 | + Ok(CastSizeChange::Ready) => (), | |
| 1645 | + Ok(CastSizeChange::Pending) => return, | |
| 1646 | + Err(err) => { | |
| 1647 | + warn!("error updating stream size, stopping screencast: {err:?}"); | |
| 1648 | + drop(windows); | |
| 1649 | + let session_id = cast.session_id; | |
| 1650 | + self.niri.stop_cast(session_id); | |
| 1651 | + return; | |
| 1652 | + } | |
| 1653 | + } | |
| 1654 | + | |
| 1655 | + self.backend.with_primary_renderer(|renderer| { | |
| 1656 | + // FIXME: pointer. | |
| 1657 | + let elements = mapped | |
| 1658 | + .render_for_screen_cast(renderer, scale) | |
| 1659 | + .rev() | |
| 1660 | + .collect::<Vec<_>>(); | |
| 1661 | + | |
| 1662 | + if cast.dequeue_buffer_and_render(renderer, &elements, bbox.size, scale) { | |
| 1663 | + cast.last_frame_time = get_monotonic_time(); | |
| 1664 | + } | |
| 1665 | + }); | |
| 1666 | + } | |
| 1667 | + } | |
| 1668 | + } | |
| 1669 | + | |
| 1622 | 1670 | #[cfg(feature = "xdp-gnome-screencast")] |
| 1623 | 1671 | pub fn on_screen_cast_msg(&mut self, msg: ScreenCastToNiri) { |
| 1624 | 1672 | use smithay::reexports::gbm::Modifier; |
| @@ -1628,13 +1676,14 @@ impl State { | ||
| 1628 | 1676 | match msg { |
| 1629 | 1677 | ScreenCastToNiri::StartCast { |
| 1630 | 1678 | session_id, |
| 1679 | + stream_id, | |
| 1631 | 1680 | target, |
| 1632 | 1681 | cursor_mode, |
| 1633 | 1682 | signal_ctx, |
| 1634 | 1683 | } => { |
| 1635 | 1684 | let _span = tracy_client::span!("StartCast"); |
| 1636 | 1685 | |
| 1637 | - debug!(session_id, "StartCast"); | |
| 1686 | + debug!(session_id, stream_id, "StartCast"); | |
| 1638 | 1687 | |
| 1639 | 1688 | let Some(gbm) = self.backend.gbm_device() else { |
| 1640 | 1689 | warn!("error starting screencast: no GBM device available"); |
| @@ -1726,6 +1775,7 @@ impl State { | ||
| 1726 | 1775 | gbm, |
| 1727 | 1776 | render_formats, |
| 1728 | 1777 | session_id, |
| 1778 | + stream_id, | |
| 1729 | 1779 | target, |
| 1730 | 1780 | size, |
| 1731 | 1781 | refresh, |
| @@ -4336,92 +4386,6 @@ impl Niri { | ||
| 4336 | 4386 | } |
| 4337 | 4387 | } |
| 4338 | 4388 | |
| 4339 | - #[cfg(feature = "xdp-gnome-screencast")] | |
| 4340 | - fn render_window_for_screen_cast( | |
| 4341 | - &mut self, | |
| 4342 | - renderer: &mut GlesRenderer, | |
| 4343 | - window_id: u64, | |
| 4344 | - target_presentation_time: Duration, | |
| 4345 | - ) { | |
| 4346 | - let _span = tracy_client::span!("Niri::render_window_for_screen_cast"); | |
| 4347 | - | |
| 4348 | - let mut window = None; | |
| 4349 | - self.layout.with_windows(|mapped, _, _| { | |
| 4350 | - if mapped.id().get() != window_id { | |
| 4351 | - return; | |
| 4352 | - } | |
| 4353 | - | |
| 4354 | - window = Some(mapped.window.clone()); | |
| 4355 | - }); | |
| 4356 | - | |
| 4357 | - let Some(window) = window else { | |
| 4358 | - return; | |
| 4359 | - }; | |
| 4360 | - | |
| 4361 | - // Use the cached output since it will be present even if the output was | |
| 4362 | - // currently disconnected. | |
| 4363 | - let Some(output) = self.mapped_cast_output.get(&window) else { | |
| 4364 | - return; | |
| 4365 | - }; | |
| 4366 | - | |
| 4367 | - let mut windows = self.layout.windows_for_output(output); | |
| 4368 | - let mapped = windows | |
| 4369 | - .find(|mapped| mapped.id().get() == window_id) | |
| 4370 | - .unwrap(); | |
| 4371 | - | |
| 4372 | - let scale = Scale::from(output.current_scale().fractional_scale()); | |
| 4373 | - let bbox = mapped | |
| 4374 | - .window | |
| 4375 | - .bbox_with_popups() | |
| 4376 | - .to_physical_precise_up(scale); | |
| 4377 | - | |
| 4378 | - let mut elements = None; | |
| 4379 | - let mut casts_to_stop = vec![]; | |
| 4380 | - | |
| 4381 | - let mut casts = mem::take(&mut self.casts); | |
| 4382 | - for cast in &mut casts { | |
| 4383 | - if !cast.is_active.get() { | |
| 4384 | - continue; | |
| 4385 | - } | |
| 4386 | - | |
| 4387 | - if cast.target != (CastTarget::Window { id: window_id }) { | |
| 4388 | - continue; | |
| 4389 | - } | |
| 4390 | - | |
| 4391 | - match cast.ensure_size(bbox.size) { | |
| 4392 | - Ok(CastSizeChange::Ready) => (), | |
| 4393 | - Ok(CastSizeChange::Pending) => continue, | |
| 4394 | - Err(err) => { | |
| 4395 | - warn!("error updating stream size, stopping screencast: {err:?}"); | |
| 4396 | - casts_to_stop.push(cast.session_id); | |
| 4397 | - } | |
| 4398 | - } | |
| 4399 | - | |
| 4400 | - if cast.check_time_and_schedule(&self.event_loop, output, target_presentation_time) { | |
| 4401 | - continue; | |
| 4402 | - } | |
| 4403 | - | |
| 4404 | - let elements = elements.get_or_insert_with(|| { | |
| 4405 | - // FIXME: pointer. | |
| 4406 | - mapped | |
| 4407 | - .render_for_screen_cast(renderer, scale) | |
| 4408 | - .rev() | |
| 4409 | - .collect::<Vec<_>>() | |
| 4410 | - }); | |
| 4411 | - | |
| 4412 | - if cast.dequeue_buffer_and_render(renderer, elements, bbox.size, scale) { | |
| 4413 | - cast.last_frame_time = target_presentation_time; | |
| 4414 | - } | |
| 4415 | - } | |
| 4416 | - self.casts = casts; | |
| 4417 | - | |
| 4418 | - drop(windows); | |
| 4419 | - | |
| 4420 | - for id in casts_to_stop { | |
| 4421 | - self.stop_cast(id); | |
| 4422 | - } | |
| 4423 | - } | |
| 4424 | - | |
| 4425 | 4389 | pub fn render_for_screencopy_with_damage( |
| 4426 | 4390 | &mut self, |
| 4427 | 4391 | renderer: &mut GlesRenderer, |
src/pw_utils.rsmodified+5 -3
| @@ -60,12 +60,13 @@ pub struct PipeWire { | ||
| 60 | 60 | |
| 61 | 61 | pub enum PwToNiri { |
| 62 | 62 | StopCast { session_id: usize }, |
| 63 | - Redraw(CastTarget), | |
| 63 | + Redraw { stream_id: usize }, | |
| 64 | 64 | FatalError, |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | pub struct Cast { |
| 68 | 68 | pub session_id: usize, |
| 69 | + pub stream_id: usize, | |
| 69 | 70 | pub stream: Stream, |
| 70 | 71 | _listener: StreamListener<()>, |
| 71 | 72 | pub is_active: Rc<Cell<bool>>, |
| @@ -189,6 +190,7 @@ impl PipeWire { | ||
| 189 | 190 | gbm: GbmDevice<DrmDeviceFd>, |
| 190 | 191 | formats: FormatSet, |
| 191 | 192 | session_id: usize, |
| 193 | + stream_id: usize, | |
| 192 | 194 | target: CastTarget, |
| 193 | 195 | size: Size<i32, Physical>, |
| 194 | 196 | refresh: u32, |
| @@ -204,10 +206,9 @@ impl PipeWire { | ||
| 204 | 206 | warn!("error sending StopCast to niri: {err:?}"); |
| 205 | 207 | } |
| 206 | 208 | }; |
| 207 | - let target_ = target.clone(); | |
| 208 | 209 | let to_niri_ = self.to_niri.clone(); |
| 209 | 210 | let redraw = move || { |
| 210 | - if let Err(err) = to_niri_.send(PwToNiri::Redraw(target_.clone())) { | |
| 211 | + if let Err(err) = to_niri_.send(PwToNiri::Redraw { stream_id }) { | |
| 211 | 212 | warn!("error sending Redraw to niri: {err:?}"); |
| 212 | 213 | } |
| 213 | 214 | }; |
| @@ -651,6 +652,7 @@ impl PipeWire { | ||
| 651 | 652 | |
| 652 | 653 | let cast = Cast { |
| 653 | 654 | session_id, |
| 655 | + stream_id, | |
| 654 | 656 | stream, |
| 655 | 657 | _listener: listener, |
| 656 | 658 | is_active, |