Move xdp-gnome-screencast code into separate module
Browse files at this commitafe27a143b8ae3d8fac70abfd2fedeb4432b66ceParent: fd2916e5 changed files +686 -678
- src/dummy_pw_utils.rs +0 -13
- src/lib.rs +2 -8
- src/niri.rs +7 -655
- src/screencasting/mod.rs +674 -0
- src/pw_utils.rs → src/screencasting/pw_utils.rs +3 -2
src/dummy_pw_utils.rsdeleted+0 -13
| @@ -1,13 +0,0 @@ | |||
| 1 | -use anyhow::bail; | ||
| 2 | -use smithay::reexports::calloop::LoopHandle; | ||
| 3 | - | ||
| 4 | -use crate::niri::State; | ||
| 5 | - | ||
| 6 | -pub struct PipeWire; | ||
| 7 | -pub struct Cast; | ||
| 8 | - | ||
| 9 | -impl PipeWire { | ||
| 10 | - pub fn new(_event_loop: &LoopHandle<'static, State>) -> anyhow::Result<Self> { | ||
| 11 | - bail!("PipeWire support is disabled (see \"xdp-gnome-screencast\" feature)"); | ||
| 12 | - } | ||
| 13 | -} | ||
src/lib.rsmodified+2 -8
| @@ -19,17 +19,11 @@ pub mod niri; | |||
| 19 | pub mod protocols; | 19 | pub mod protocols; |
| 20 | pub mod render_helpers; | 20 | pub mod render_helpers; |
| 21 | pub mod rubber_band; | 21 | pub mod rubber_band; |
| 22 | +#[cfg(feature = "xdp-gnome-screencast")] | ||
| 23 | +pub mod screencasting; | ||
| 22 | pub mod ui; | 24 | pub mod ui; |
| 23 | pub mod utils; | 25 | pub mod utils; |
| 24 | pub mod window; | 26 | pub mod window; |
| 25 | 27 | ||
| 26 | -#[cfg(not(feature = "xdp-gnome-screencast"))] | ||
| 27 | -pub mod dummy_pw_utils; | ||
| 28 | -#[cfg(feature = "xdp-gnome-screencast")] | ||
| 29 | -pub mod pw_utils; | ||
| 30 | - | ||
| 31 | -#[cfg(not(feature = "xdp-gnome-screencast"))] | ||
| 32 | -pub use dummy_pw_utils as pw_utils; | ||
| 33 | - | ||
| 34 | #[cfg(test)] | 28 | #[cfg(test)] |
| 35 | mod tests; | 29 | mod tests; |
src/niri.rsmodified+7 -655
| @@ -123,8 +123,6 @@ use crate::dbus::freedesktop_login1::Login1ToNiri; | |||
| 123 | use crate::dbus::gnome_shell_introspect::{self, IntrospectToNiri, NiriToIntrospect}; | 123 | use crate::dbus::gnome_shell_introspect::{self, IntrospectToNiri, NiriToIntrospect}; |
| 124 | #[cfg(feature = "dbus")] | 124 | #[cfg(feature = "dbus")] |
| 125 | use crate::dbus::gnome_shell_screenshot::{NiriToScreenshot, ScreenshotToNiri}; | 125 | use crate::dbus::gnome_shell_screenshot::{NiriToScreenshot, ScreenshotToNiri}; |
| 126 | -#[cfg(feature = "xdp-gnome-screencast")] | ||
| 127 | -use crate::dbus::mutter_screen_cast::{self, ScreenCastToNiri}; | ||
| 128 | use crate::frame_clock::FrameClock; | 126 | use crate::frame_clock::FrameClock; |
| 129 | use crate::handlers::{configure_lock_surface, XDG_ACTIVATION_TOKEN_TIMEOUT}; | 127 | use crate::handlers::{configure_lock_surface, XDG_ACTIVATION_TOKEN_TIMEOUT}; |
| 130 | use crate::input::pick_color_grab::PickColorGrab; | 128 | use crate::input::pick_color_grab::PickColorGrab; |
| @@ -150,9 +148,6 @@ use crate::protocols::mutter_x11_interop::MutterX11InteropManagerState; | |||
| 150 | use crate::protocols::output_management::OutputManagementManagerState; | 148 | use crate::protocols::output_management::OutputManagementManagerState; |
| 151 | use crate::protocols::screencopy::{Screencopy, ScreencopyBuffer, ScreencopyManagerState}; | 149 | use crate::protocols::screencopy::{Screencopy, ScreencopyBuffer, ScreencopyManagerState}; |
| 152 | use crate::protocols::virtual_pointer::VirtualPointerManagerState; | 150 | use crate::protocols::virtual_pointer::VirtualPointerManagerState; |
| 153 | -use crate::pw_utils::{Cast, PipeWire}; | ||
| 154 | -#[cfg(feature = "xdp-gnome-screencast")] | ||
| 155 | -use crate::pw_utils::{CastSizeChange, CursorData, PwToNiri}; | ||
| 156 | use crate::render_helpers::debug::draw_opaque_regions; | 151 | use crate::render_helpers::debug::draw_opaque_regions; |
| 157 | use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; | 152 | use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; |
| 158 | use crate::render_helpers::renderer::NiriRenderer; | 153 | use crate::render_helpers::renderer::NiriRenderer; |
| @@ -163,6 +158,8 @@ use crate::render_helpers::{ | |||
| 163 | encompassing_geo, render_to_dmabuf, render_to_encompassing_texture, render_to_shm, | 158 | encompassing_geo, render_to_dmabuf, render_to_encompassing_texture, render_to_shm, |
| 164 | render_to_texture, render_to_vec, shaders, RenderTarget, | 159 | render_to_texture, render_to_vec, shaders, RenderTarget, |
| 165 | }; | 160 | }; |
| 161 | +#[cfg(feature = "xdp-gnome-screencast")] | ||
| 162 | +use crate::screencasting::Screencasting; | ||
| 166 | use crate::ui::config_error_notification::ConfigErrorNotification; | 163 | use crate::ui::config_error_notification::ConfigErrorNotification; |
| 167 | use crate::ui::exit_confirm_dialog::{ExitConfirmDialog, ExitConfirmDialogRenderElement}; | 164 | use crate::ui::exit_confirm_dialog::{ExitConfirmDialog, ExitConfirmDialogRenderElement}; |
| 168 | use crate::ui::hotkey_overlay::HotkeyOverlay; | 165 | use crate::ui::hotkey_overlay::HotkeyOverlay; |
| @@ -179,7 +176,7 @@ use crate::utils::{ | |||
| 179 | logical_output, make_screenshot_path, output_matches_name, output_size, panel_orientation, | 176 | logical_output, make_screenshot_path, output_matches_name, output_size, panel_orientation, |
| 180 | send_scale_transform, write_png_rgba8, xwayland, | 177 | send_scale_transform, write_png_rgba8, xwayland, |
| 181 | }; | 178 | }; |
| 182 | -use crate::window::mapped::{MappedId, WindowCastRenderElements}; | 179 | +use crate::window::mapped::MappedId; |
| 183 | use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped, WindowRef}; | 180 | use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped, WindowRef}; |
| 184 | 181 | ||
| 185 | const CLEAR_COLOR_LOCKED: [f32; 4] = [0.3, 0.1, 0.1, 1.]; | 182 | const CLEAR_COLOR_LOCKED: [f32; 4] = [0.3, 0.1, 0.1, 1.]; |
| @@ -409,19 +406,8 @@ pub struct Niri { | |||
| 409 | 406 | ||
| 410 | pub satellite: Option<Satellite>, | 407 | pub satellite: Option<Satellite>, |
| 411 | 408 | ||
| 412 | - // Casts are dropped before PipeWire to prevent a double-free (yay). | ||
| 413 | - pub casts: Vec<Cast>, | ||
| 414 | - pub pipewire: Option<PipeWire>, | ||
| 415 | - #[cfg(feature = "xdp-gnome-screencast")] | ||
| 416 | - pub pw_to_niri: calloop::channel::Sender<PwToNiri>, | ||
| 417 | - | ||
| 418 | - // Screencast output for each mapped window. | ||
| 419 | #[cfg(feature = "xdp-gnome-screencast")] | 409 | #[cfg(feature = "xdp-gnome-screencast")] |
| 420 | - pub mapped_cast_output: HashMap<Window, Output>, | 410 | + pub casting: Screencasting, |
| 421 | - | ||
| 422 | - /// Window ID for the "dynamic cast" special window for the xdp-gnome picker. | ||
| 423 | - #[cfg(feature = "xdp-gnome-screencast")] | ||
| 424 | - pub dynamic_cast_id_for_portal: MappedId, | ||
| 425 | } | 411 | } |
| 426 | 412 | ||
| 427 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 413 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| @@ -1997,326 +1983,9 @@ impl State { | |||
| 1997 | self.niri.queue_redraw_all(); | 1983 | self.niri.queue_redraw_all(); |
| 1998 | } | 1984 | } |
| 1999 | 1985 | ||
| 2000 | - #[cfg(feature = "xdp-gnome-screencast")] | ||
| 2001 | - pub fn on_pw_msg(&mut self, msg: PwToNiri) { | ||
| 2002 | - match msg { | ||
| 2003 | - PwToNiri::StopCast { session_id } => self.niri.stop_cast(session_id), | ||
| 2004 | - PwToNiri::Redraw { stream_id } => self.redraw_cast(stream_id), | ||
| 2005 | - PwToNiri::FatalError => { | ||
| 2006 | - warn!("stopping PipeWire due to fatal error"); | ||
| 2007 | - if let Some(pw) = self.niri.pipewire.take() { | ||
| 2008 | - let ids: Vec<_> = self.niri.casts.iter().map(|cast| cast.session_id).collect(); | ||
| 2009 | - for id in ids { | ||
| 2010 | - self.niri.stop_cast(id); | ||
| 2011 | - } | ||
| 2012 | - self.niri.event_loop.remove(pw.token); | ||
| 2013 | - } | ||
| 2014 | - } | ||
| 2015 | - } | ||
| 2016 | - } | ||
| 2017 | - | ||
| 2018 | - #[cfg(feature = "xdp-gnome-screencast")] | ||
| 2019 | - fn redraw_cast(&mut self, stream_id: usize) { | ||
| 2020 | - let _span = tracy_client::span!("State::redraw_cast"); | ||
| 2021 | - | ||
| 2022 | - let casts = &mut self.niri.casts; | ||
| 2023 | - let Some(idx) = casts.iter().position(|cast| cast.stream_id == stream_id) else { | ||
| 2024 | - warn!("cast to redraw is missing"); | ||
| 2025 | - return; | ||
| 2026 | - }; | ||
| 2027 | - let cast = &mut casts[idx]; | ||
| 2028 | - | ||
| 2029 | - let id = match &cast.target { | ||
| 2030 | - CastTarget::Nothing => { | ||
| 2031 | - self.backend.with_primary_renderer(|renderer| { | ||
| 2032 | - if cast.dequeue_buffer_and_clear(renderer) { | ||
| 2033 | - cast.last_frame_time = get_monotonic_time(); | ||
| 2034 | - } | ||
| 2035 | - }); | ||
| 2036 | - return; | ||
| 2037 | - } | ||
| 2038 | - CastTarget::Output(weak) => { | ||
| 2039 | - if let Some(output) = weak.upgrade() { | ||
| 2040 | - self.niri.queue_redraw(&output); | ||
| 2041 | - } | ||
| 2042 | - return; | ||
| 2043 | - } | ||
| 2044 | - CastTarget::Window { id } => *id, | ||
| 2045 | - }; | ||
| 2046 | - | ||
| 2047 | - // Lack of partial borrowing strikes again... | ||
| 2048 | - let mut casts = mem::take(&mut self.niri.casts); | ||
| 2049 | - let cast = &mut casts[idx]; | ||
| 2050 | - let mut stop = false; | ||
| 2051 | - // Use a loop {} so we can break instead of early-return. | ||
| 2052 | - #[allow(clippy::never_loop)] | ||
| 2053 | - loop { | ||
| 2054 | - let mut windows = self.niri.layout.windows(); | ||
| 2055 | - let Some((_, mapped)) = windows.find(|(_, mapped)| mapped.id().get() == id) else { | ||
| 2056 | - break; | ||
| 2057 | - }; | ||
| 2058 | - | ||
| 2059 | - // Use the cached output since it will be present even if the output was | ||
| 2060 | - // currently disconnected. | ||
| 2061 | - let Some(output) = self.niri.mapped_cast_output.get(&mapped.window) else { | ||
| 2062 | - break; | ||
| 2063 | - }; | ||
| 2064 | - | ||
| 2065 | - let scale = Scale::from(output.current_scale().fractional_scale()); | ||
| 2066 | - let bbox = mapped | ||
| 2067 | - .window | ||
| 2068 | - .bbox_with_popups() | ||
| 2069 | - .to_physical_precise_up(scale); | ||
| 2070 | - | ||
| 2071 | - match cast.ensure_size(bbox.size) { | ||
| 2072 | - Ok(CastSizeChange::Ready) => (), | ||
| 2073 | - Ok(CastSizeChange::Pending) => break, | ||
| 2074 | - Err(err) => { | ||
| 2075 | - warn!("error updating stream size, stopping screencast: {err:?}"); | ||
| 2076 | - stop = true; | ||
| 2077 | - break; | ||
| 2078 | - } | ||
| 2079 | - } | ||
| 2080 | - | ||
| 2081 | - self.backend.with_primary_renderer(|renderer| { | ||
| 2082 | - let mut elements = Vec::new(); | ||
| 2083 | - mapped.render_for_screen_cast(renderer, scale, &mut |elem| { | ||
| 2084 | - elements.push(CastRenderElement::from(elem)) | ||
| 2085 | - }); | ||
| 2086 | - | ||
| 2087 | - let mut pointer_elements = Vec::new(); | ||
| 2088 | - let mut pointer_location = Point::default(); | ||
| 2089 | - | ||
| 2090 | - if self.niri.pointer_visibility.is_visible() { | ||
| 2091 | - if let Some((pointer_pos, win_pos)) = | ||
| 2092 | - self.niri.pointer_pos_for_window_cast(mapped) | ||
| 2093 | - { | ||
| 2094 | - // Pointer location must be relative to the screencast buffer. | ||
| 2095 | - // - win_pos is the position of the main window surface in output-local | ||
| 2096 | - // coordinates | ||
| 2097 | - // - bbox.loc moves us relative to the screencast buffer | ||
| 2098 | - let buf_pos = win_pos + bbox.loc.to_f64().to_logical(scale); | ||
| 2099 | - let output_pos = | ||
| 2100 | - self.niri.global_space.output_geometry(output).unwrap().loc; | ||
| 2101 | - pointer_location = pointer_pos - output_pos.to_f64() - buf_pos; | ||
| 2102 | - | ||
| 2103 | - let pos = buf_pos.to_physical_precise_round(scale).upscale(-1); | ||
| 2104 | - self.niri.render_pointer(renderer, output, &mut |elem| { | ||
| 2105 | - let elem = | ||
| 2106 | - RelocateRenderElement::from_element(elem, pos, Relocate::Relative); | ||
| 2107 | - pointer_elements.push(CastRenderElement::from(elem)); | ||
| 2108 | - }); | ||
| 2109 | - } | ||
| 2110 | - } | ||
| 2111 | - let cursor_data = CursorData::compute(&pointer_elements, pointer_location, scale); | ||
| 2112 | - | ||
| 2113 | - if cast.dequeue_buffer_and_render( | ||
| 2114 | - renderer, | ||
| 2115 | - &elements, | ||
| 2116 | - &cursor_data, | ||
| 2117 | - bbox.size, | ||
| 2118 | - scale, | ||
| 2119 | - ) { | ||
| 2120 | - cast.last_frame_time = get_monotonic_time(); | ||
| 2121 | - } | ||
| 2122 | - }); | ||
| 2123 | - | ||
| 2124 | - break; | ||
| 2125 | - } | ||
| 2126 | - let session_id = cast.session_id; | ||
| 2127 | - self.niri.casts = casts; | ||
| 2128 | - | ||
| 2129 | - if stop { | ||
| 2130 | - self.niri.stop_cast(session_id); | ||
| 2131 | - } | ||
| 2132 | - } | ||
| 2133 | - | ||
| 2134 | #[cfg(not(feature = "xdp-gnome-screencast"))] | 1986 | #[cfg(not(feature = "xdp-gnome-screencast"))] |
| 2135 | pub fn set_dynamic_cast_target(&mut self, _target: CastTarget) {} | 1987 | pub fn set_dynamic_cast_target(&mut self, _target: CastTarget) {} |
| 2136 | 1988 | ||
| 2137 | - #[cfg(feature = "xdp-gnome-screencast")] | ||
| 2138 | - pub fn set_dynamic_cast_target(&mut self, target: CastTarget) { | ||
| 2139 | - let _span = tracy_client::span!("State::set_dynamic_cast_target"); | ||
| 2140 | - | ||
| 2141 | - let mut refresh = None; | ||
| 2142 | - match &target { | ||
| 2143 | - // Leave refresh as is when clearing. Chances are, the next refresh will match it, | ||
| 2144 | - // then we'll avoid reconfiguring. | ||
| 2145 | - CastTarget::Nothing => (), | ||
| 2146 | - CastTarget::Output(output) => { | ||
| 2147 | - if let Some(output) = output.upgrade() { | ||
| 2148 | - refresh = Some(output.current_mode().unwrap().refresh as u32); | ||
| 2149 | - } | ||
| 2150 | - } | ||
| 2151 | - CastTarget::Window { id } => { | ||
| 2152 | - let mut windows = self.niri.layout.windows(); | ||
| 2153 | - if let Some((_, mapped)) = windows.find(|(_, mapped)| mapped.id().get() == *id) { | ||
| 2154 | - if let Some(output) = self.niri.mapped_cast_output.get(&mapped.window) { | ||
| 2155 | - refresh = Some(output.current_mode().unwrap().refresh as u32); | ||
| 2156 | - } | ||
| 2157 | - } | ||
| 2158 | - } | ||
| 2159 | - } | ||
| 2160 | - | ||
| 2161 | - let mut to_redraw = Vec::new(); | ||
| 2162 | - let mut to_stop = Vec::new(); | ||
| 2163 | - for cast in &mut self.niri.casts { | ||
| 2164 | - if !cast.dynamic_target { | ||
| 2165 | - continue; | ||
| 2166 | - } | ||
| 2167 | - | ||
| 2168 | - if let Some(refresh) = refresh { | ||
| 2169 | - if let Err(err) = cast.set_refresh(refresh) { | ||
| 2170 | - warn!("error changing cast FPS: {err:?}"); | ||
| 2171 | - to_stop.push(cast.session_id); | ||
| 2172 | - continue; | ||
| 2173 | - } | ||
| 2174 | - } | ||
| 2175 | - | ||
| 2176 | - cast.target = target.clone(); | ||
| 2177 | - to_redraw.push(cast.stream_id); | ||
| 2178 | - } | ||
| 2179 | - | ||
| 2180 | - for id in to_redraw { | ||
| 2181 | - self.redraw_cast(id); | ||
| 2182 | - } | ||
| 2183 | - } | ||
| 2184 | - | ||
| 2185 | - #[cfg(feature = "xdp-gnome-screencast")] | ||
| 2186 | - pub fn on_screen_cast_msg(&mut self, msg: ScreenCastToNiri) { | ||
| 2187 | - use smithay::reexports::gbm::Modifier; | ||
| 2188 | - | ||
| 2189 | - use crate::dbus::mutter_screen_cast::StreamTargetId; | ||
| 2190 | - | ||
| 2191 | - match msg { | ||
| 2192 | - ScreenCastToNiri::StartCast { | ||
| 2193 | - session_id, | ||
| 2194 | - stream_id, | ||
| 2195 | - target, | ||
| 2196 | - cursor_mode, | ||
| 2197 | - signal_ctx, | ||
| 2198 | - } => { | ||
| 2199 | - let _span = tracy_client::span!("StartCast"); | ||
| 2200 | - | ||
| 2201 | - debug!(session_id, stream_id, "StartCast"); | ||
| 2202 | - | ||
| 2203 | - let Some(gbm) = self.backend.gbm_device() else { | ||
| 2204 | - warn!("error starting screencast: no GBM device available"); | ||
| 2205 | - self.niri.stop_cast(session_id); | ||
| 2206 | - return; | ||
| 2207 | - }; | ||
| 2208 | - | ||
| 2209 | - let pw = if let Some(pw) = &self.niri.pipewire { | ||
| 2210 | - pw | ||
| 2211 | - } else { | ||
| 2212 | - match PipeWire::new(self.niri.event_loop.clone(), self.niri.pw_to_niri.clone()) | ||
| 2213 | - { | ||
| 2214 | - Ok(pipewire) => self.niri.pipewire.insert(pipewire), | ||
| 2215 | - Err(err) => { | ||
| 2216 | - warn!( | ||
| 2217 | - "error starting screencast: PipeWire failed to initialize: {err:?}" | ||
| 2218 | - ); | ||
| 2219 | - self.niri.stop_cast(session_id); | ||
| 2220 | - return; | ||
| 2221 | - } | ||
| 2222 | - } | ||
| 2223 | - }; | ||
| 2224 | - | ||
| 2225 | - let mut dynamic_target = false; | ||
| 2226 | - let (target, size, refresh, alpha) = match target { | ||
| 2227 | - StreamTargetId::Output { name } => { | ||
| 2228 | - let global_space = &self.niri.global_space; | ||
| 2229 | - let output = global_space.outputs().find(|out| out.name() == name); | ||
| 2230 | - let Some(output) = output else { | ||
| 2231 | - warn!("error starting screencast: requested output is missing"); | ||
| 2232 | - self.niri.stop_cast(session_id); | ||
| 2233 | - return; | ||
| 2234 | - }; | ||
| 2235 | - | ||
| 2236 | - let mode = output.current_mode().unwrap(); | ||
| 2237 | - let transform = output.current_transform(); | ||
| 2238 | - let size = transform.transform_size(mode.size); | ||
| 2239 | - let refresh = mode.refresh as u32; | ||
| 2240 | - (CastTarget::Output(output.downgrade()), size, refresh, false) | ||
| 2241 | - } | ||
| 2242 | - StreamTargetId::Window { id } | ||
| 2243 | - if id == self.niri.dynamic_cast_id_for_portal.get() => | ||
| 2244 | - { | ||
| 2245 | - dynamic_target = true; | ||
| 2246 | - | ||
| 2247 | - // All dynamic casts start as Nothing to avoid surprises and exposing | ||
| 2248 | - // sensitive info. | ||
| 2249 | - (CastTarget::Nothing, Size::from((1, 1)), 1000, true) | ||
| 2250 | - } | ||
| 2251 | - StreamTargetId::Window { id } => { | ||
| 2252 | - let Some(window) = self.niri.layout.windows().find_map(|(_, mapped)| { | ||
| 2253 | - (mapped.id().get() == id).then_some(&mapped.window) | ||
| 2254 | - }) else { | ||
| 2255 | - warn!("error starting screencast: requested window is missing"); | ||
| 2256 | - self.niri.stop_cast(session_id); | ||
| 2257 | - return; | ||
| 2258 | - }; | ||
| 2259 | - | ||
| 2260 | - // Use the cached output since it will be present even if the output was | ||
| 2261 | - // currently disconnected. | ||
| 2262 | - let Some(output) = self.niri.mapped_cast_output.get(window) else { | ||
| 2263 | - warn!("error starting screencast: requested window is missing"); | ||
| 2264 | - self.niri.stop_cast(session_id); | ||
| 2265 | - return; | ||
| 2266 | - }; | ||
| 2267 | - | ||
| 2268 | - let scale = Scale::from(output.current_scale().fractional_scale()); | ||
| 2269 | - let bbox = window.bbox_with_popups().to_physical_precise_up(scale); | ||
| 2270 | - let refresh = output.current_mode().unwrap().refresh as u32; | ||
| 2271 | - | ||
| 2272 | - (CastTarget::Window { id }, bbox.size, refresh, true) | ||
| 2273 | - } | ||
| 2274 | - }; | ||
| 2275 | - | ||
| 2276 | - let mut render_formats = self | ||
| 2277 | - .backend | ||
| 2278 | - .with_primary_renderer(|renderer| { | ||
| 2279 | - renderer.egl_context().dmabuf_render_formats().clone() | ||
| 2280 | - }) | ||
| 2281 | - .unwrap_or_default(); | ||
| 2282 | - | ||
| 2283 | - { | ||
| 2284 | - let config = self.niri.config.borrow(); | ||
| 2285 | - if config.debug.force_pipewire_invalid_modifier { | ||
| 2286 | - render_formats = render_formats | ||
| 2287 | - .into_iter() | ||
| 2288 | - .filter(|f| f.modifier == Modifier::Invalid) | ||
| 2289 | - .collect(); | ||
| 2290 | - } | ||
| 2291 | - } | ||
| 2292 | - | ||
| 2293 | - let res = pw.start_cast( | ||
| 2294 | - gbm, | ||
| 2295 | - render_formats, | ||
| 2296 | - session_id, | ||
| 2297 | - stream_id, | ||
| 2298 | - target, | ||
| 2299 | - dynamic_target, | ||
| 2300 | - size, | ||
| 2301 | - refresh, | ||
| 2302 | - alpha, | ||
| 2303 | - cursor_mode, | ||
| 2304 | - signal_ctx, | ||
| 2305 | - ); | ||
| 2306 | - match res { | ||
| 2307 | - Ok(cast) => { | ||
| 2308 | - self.niri.casts.push(cast); | ||
| 2309 | - } | ||
| 2310 | - Err(err) => { | ||
| 2311 | - warn!("error starting screencast: {err:?}"); | ||
| 2312 | - self.niri.stop_cast(session_id); | ||
| 2313 | - } | ||
| 2314 | - } | ||
| 2315 | - } | ||
| 2316 | - ScreenCastToNiri::StopCast { session_id } => self.niri.stop_cast(session_id), | ||
| 2317 | - } | ||
| 2318 | - } | ||
| 2319 | - | ||
| 2320 | #[cfg(feature = "dbus")] | 1989 | #[cfg(feature = "dbus")] |
| 2321 | pub fn on_screen_shot_msg( | 1990 | pub fn on_screen_shot_msg( |
| 2322 | &mut self, | 1991 | &mut self, |
| @@ -2389,7 +2058,7 @@ impl State { | |||
| 2389 | 2058 | ||
| 2390 | #[cfg(feature = "xdp-gnome-screencast")] | 2059 | #[cfg(feature = "xdp-gnome-screencast")] |
| 2391 | windows.insert( | 2060 | windows.insert( |
| 2392 | - self.niri.dynamic_cast_id_for_portal.get(), | 2061 | + self.niri.casting.dynamic_cast_id_for_portal.get(), |
| 2393 | gnome_shell_introspect::WindowProperties { | 2062 | gnome_shell_introspect::WindowProperties { |
| 2394 | title: String::from("niri Dynamic Cast Target"), | 2063 | title: String::from("niri Dynamic Cast Target"), |
| 2395 | app_id: String::from("rs.bxt.niri.desktop"), | 2064 | app_id: String::from("rs.bxt.niri.desktop"), |
| @@ -2682,16 +2351,7 @@ impl Niri { | |||
| 2682 | }; | 2351 | }; |
| 2683 | 2352 | ||
| 2684 | #[cfg(feature = "xdp-gnome-screencast")] | 2353 | #[cfg(feature = "xdp-gnome-screencast")] |
| 2685 | - let pw_to_niri = { | 2354 | + let screencasting = Screencasting::new(&event_loop); |
| 2686 | - let (pw_to_niri, from_pipewire) = calloop::channel::channel(); | ||
| 2687 | - event_loop | ||
| 2688 | - .insert_source(from_pipewire, move |event, _, state| match event { | ||
| 2689 | - calloop::channel::Event::Msg(msg) => state.on_pw_msg(msg), | ||
| 2690 | - calloop::channel::Event::Closed => (), | ||
| 2691 | - }) | ||
| 2692 | - .unwrap(); | ||
| 2693 | - pw_to_niri | ||
| 2694 | - }; | ||
| 2695 | 2355 | ||
| 2696 | let display_source = Generic::new(display, Interest::READ, Mode::Level); | 2356 | let display_source = Generic::new(display, Interest::READ, Mode::Level); |
| 2697 | event_loop | 2357 | event_loop |
| @@ -2858,16 +2518,8 @@ impl Niri { | |||
| 2858 | 2518 | ||
| 2859 | satellite: None, | 2519 | satellite: None, |
| 2860 | 2520 | ||
| 2861 | - pipewire: None, | ||
| 2862 | - casts: vec![], | ||
| 2863 | - #[cfg(feature = "xdp-gnome-screencast")] | ||
| 2864 | - pw_to_niri, | ||
| 2865 | - | ||
| 2866 | - #[cfg(feature = "xdp-gnome-screencast")] | ||
| 2867 | - mapped_cast_output: HashMap::new(), | ||
| 2868 | - | ||
| 2869 | #[cfg(feature = "xdp-gnome-screencast")] | 2521 | #[cfg(feature = "xdp-gnome-screencast")] |
| 2870 | - dynamic_cast_id_for_portal: MappedId::next(), | 2522 | + casting: screencasting, |
| 2871 | }; | 2523 | }; |
| 2872 | 2524 | ||
| 2873 | niri.reset_pointer_inactivity_timer(); | 2525 | niri.reset_pointer_inactivity_timer(); |
| @@ -3145,7 +2797,6 @@ impl Niri { | |||
| 3145 | RedrawState::WaitingForEstimatedVBlankAndQueued(token) => self.event_loop.remove(token), | 2797 | RedrawState::WaitingForEstimatedVBlankAndQueued(token) => self.event_loop.remove(token), |
| 3146 | } | 2798 | } |
| 3147 | 2799 | ||
| 3148 | - #[cfg(feature = "xdp-gnome-screencast")] | ||
| 3149 | self.stop_casts_for_target(CastTarget::Output(output.downgrade())); | 2800 | self.stop_casts_for_target(CastTarget::Output(output.downgrade())); |
| 3150 | 2801 | ||
| 3151 | self.remove_screencopy_output(output); | 2802 | self.remove_screencopy_output(output); |
| @@ -4275,66 +3926,6 @@ impl Niri { | |||
| 4275 | } | 3926 | } |
| 4276 | } | 3927 | } |
| 4277 | 3928 | ||
| 4278 | - #[cfg(feature = "xdp-gnome-screencast")] | ||
| 4279 | - pub fn refresh_mapped_cast_window_rules(&mut self) { | ||
| 4280 | - // O(N^2) but should be fine since there aren't many casts usually. | ||
| 4281 | - self.layout.with_windows_mut(|mapped, _| { | ||
| 4282 | - let id = mapped.id().get(); | ||
| 4283 | - // Find regardless of cast.is_active. | ||
| 4284 | - let value = self | ||
| 4285 | - .casts | ||
| 4286 | - .iter() | ||
| 4287 | - .any(|cast| cast.target == (CastTarget::Window { id })); | ||
| 4288 | - mapped.set_is_window_cast_target(value); | ||
| 4289 | - }); | ||
| 4290 | - } | ||
| 4291 | - | ||
| 4292 | - #[cfg(feature = "xdp-gnome-screencast")] | ||
| 4293 | - pub fn refresh_mapped_cast_outputs(&mut self) { | ||
| 4294 | - use std::collections::hash_map::Entry; | ||
| 4295 | - | ||
| 4296 | - let mut seen = HashSet::new(); | ||
| 4297 | - let mut output_changed = vec![]; | ||
| 4298 | - | ||
| 4299 | - self.layout.with_windows(|mapped, output, _, _| { | ||
| 4300 | - seen.insert(mapped.window.clone()); | ||
| 4301 | - | ||
| 4302 | - let Some(output) = output else { | ||
| 4303 | - return; | ||
| 4304 | - }; | ||
| 4305 | - | ||
| 4306 | - match self.mapped_cast_output.entry(mapped.window.clone()) { | ||
| 4307 | - Entry::Occupied(mut entry) => { | ||
| 4308 | - if entry.get() != output { | ||
| 4309 | - entry.insert(output.clone()); | ||
| 4310 | - output_changed.push((mapped.id(), output.clone())); | ||
| 4311 | - } | ||
| 4312 | - } | ||
| 4313 | - Entry::Vacant(entry) => { | ||
| 4314 | - entry.insert(output.clone()); | ||
| 4315 | - } | ||
| 4316 | - } | ||
| 4317 | - }); | ||
| 4318 | - | ||
| 4319 | - self.mapped_cast_output.retain(|win, _| seen.contains(win)); | ||
| 4320 | - | ||
| 4321 | - let mut to_stop = vec![]; | ||
| 4322 | - for (id, out) in output_changed { | ||
| 4323 | - let refresh = out.current_mode().unwrap().refresh as u32; | ||
| 4324 | - let target = CastTarget::Window { id: id.get() }; | ||
| 4325 | - for cast in self.casts.iter_mut().filter(|cast| cast.target == target) { | ||
| 4326 | - if let Err(err) = cast.set_refresh(refresh) { | ||
| 4327 | - warn!("error changing cast FPS: {err:?}"); | ||
| 4328 | - to_stop.push(cast.session_id); | ||
| 4329 | - }; | ||
| 4330 | - } | ||
| 4331 | - } | ||
| 4332 | - | ||
| 4333 | - for session_id in to_stop { | ||
| 4334 | - self.stop_cast(session_id); | ||
| 4335 | - } | ||
| 4336 | - } | ||
| 4337 | - | ||
| 4338 | pub fn advance_animations(&mut self) { | 3929 | pub fn advance_animations(&mut self) { |
| 4339 | let _span = tracy_client::span!("Niri::advance_animations"); | 3930 | let _span = tracy_client::span!("Niri::advance_animations"); |
| 4340 | 3931 | ||
| @@ -5280,172 +4871,6 @@ impl Niri { | |||
| 5280 | feedback | 4871 | feedback |
| 5281 | } | 4872 | } |
| 5282 | 4873 | ||
| 5283 | - #[cfg(feature = "xdp-gnome-screencast")] | ||
| 5284 | - fn render_for_screen_cast( | ||
| 5285 | - &mut self, | ||
| 5286 | - renderer: &mut GlesRenderer, | ||
| 5287 | - output: &Output, | ||
| 5288 | - target_presentation_time: Duration, | ||
| 5289 | - ) { | ||
| 5290 | - let _span = tracy_client::span!("Niri::render_for_screen_cast"); | ||
| 5291 | - | ||
| 5292 | - let target = CastTarget::Output(output.downgrade()); | ||
| 5293 | - | ||
| 5294 | - let size = output.current_mode().unwrap().size; | ||
| 5295 | - let transform = output.current_transform(); | ||
| 5296 | - let size = transform.transform_size(size); | ||
| 5297 | - | ||
| 5298 | - let scale = Scale::from(output.current_scale().fractional_scale()); | ||
| 5299 | - | ||
| 5300 | - let mut elements = Vec::new(); | ||
| 5301 | - let mut pointer = Vec::new(); | ||
| 5302 | - let mut cursor_data = None; | ||
| 5303 | - | ||
| 5304 | - let mut casts_to_stop = vec![]; | ||
| 5305 | - | ||
| 5306 | - let mut casts = mem::take(&mut self.casts); | ||
| 5307 | - for cast in &mut casts { | ||
| 5308 | - if !cast.is_active() { | ||
| 5309 | - continue; | ||
| 5310 | - } | ||
| 5311 | - | ||
| 5312 | - if cast.target != target { | ||
| 5313 | - continue; | ||
| 5314 | - } | ||
| 5315 | - | ||
| 5316 | - match cast.ensure_size(size) { | ||
| 5317 | - Ok(CastSizeChange::Ready) => (), | ||
| 5318 | - Ok(CastSizeChange::Pending) => continue, | ||
| 5319 | - Err(err) => { | ||
| 5320 | - warn!("error updating stream size, stopping screencast: {err:?}"); | ||
| 5321 | - casts_to_stop.push(cast.session_id); | ||
| 5322 | - } | ||
| 5323 | - } | ||
| 5324 | - | ||
| 5325 | - if cast.check_time_and_schedule(output, target_presentation_time) { | ||
| 5326 | - continue; | ||
| 5327 | - } | ||
| 5328 | - | ||
| 5329 | - if cursor_data.is_none() { | ||
| 5330 | - // FIXME: support debug draw opaque regions. | ||
| 5331 | - self.render_inner( | ||
| 5332 | - renderer, | ||
| 5333 | - output, | ||
| 5334 | - false, | ||
| 5335 | - RenderTarget::Screencast, | ||
| 5336 | - &mut |elem| elements.push(elem.into()), | ||
| 5337 | - ); | ||
| 5338 | - | ||
| 5339 | - if self.pointer_visibility.is_visible() { | ||
| 5340 | - self.render_pointer(renderer, output, &mut |elem| pointer.push(elem.into())); | ||
| 5341 | - } | ||
| 5342 | - | ||
| 5343 | - let output_pos = self.global_space.output_geometry(output).unwrap().loc; | ||
| 5344 | - let pointer_pos = self | ||
| 5345 | - .tablet_cursor_location | ||
| 5346 | - .unwrap_or_else(|| self.seat.get_pointer().unwrap().current_location()); | ||
| 5347 | - let pointer_pos = pointer_pos - output_pos.to_f64(); | ||
| 5348 | - | ||
| 5349 | - cursor_data = Some(CursorData::compute(&pointer, pointer_pos, scale)); | ||
| 5350 | - } | ||
| 5351 | - let cursor_data = cursor_data.as_ref().unwrap(); | ||
| 5352 | - | ||
| 5353 | - if cast.dequeue_buffer_and_render(renderer, &elements, cursor_data, size, scale) { | ||
| 5354 | - cast.last_frame_time = target_presentation_time; | ||
| 5355 | - } | ||
| 5356 | - } | ||
| 5357 | - self.casts = casts; | ||
| 5358 | - | ||
| 5359 | - for id in casts_to_stop { | ||
| 5360 | - self.stop_cast(id); | ||
| 5361 | - } | ||
| 5362 | - } | ||
| 5363 | - | ||
| 5364 | - #[cfg(feature = "xdp-gnome-screencast")] | ||
| 5365 | - fn render_windows_for_screen_cast( | ||
| 5366 | - &mut self, | ||
| 5367 | - renderer: &mut GlesRenderer, | ||
| 5368 | - output: &Output, | ||
| 5369 | - target_presentation_time: Duration, | ||
| 5370 | - ) { | ||
| 5371 | - let _span = tracy_client::span!("Niri::render_windows_for_screen_cast"); | ||
| 5372 | - | ||
| 5373 | - let scale = Scale::from(output.current_scale().fractional_scale()); | ||
| 5374 | - | ||
| 5375 | - let mut casts_to_stop = vec![]; | ||
| 5376 | - | ||
| 5377 | - let mut casts = mem::take(&mut self.casts); | ||
| 5378 | - for cast in &mut casts { | ||
| 5379 | - if !cast.is_active() { | ||
| 5380 | - continue; | ||
| 5381 | - } | ||
| 5382 | - | ||
| 5383 | - let CastTarget::Window { id } = cast.target else { | ||
| 5384 | - continue; | ||
| 5385 | - }; | ||
| 5386 | - | ||
| 5387 | - let mut windows = self.layout.windows_for_output(output); | ||
| 5388 | - let Some(mapped) = windows.find(|win| win.id().get() == id) else { | ||
| 5389 | - continue; | ||
| 5390 | - }; | ||
| 5391 | - | ||
| 5392 | - let bbox = mapped | ||
| 5393 | - .window | ||
| 5394 | - .bbox_with_popups() | ||
| 5395 | - .to_physical_precise_up(scale); | ||
| 5396 | - | ||
| 5397 | - match cast.ensure_size(bbox.size) { | ||
| 5398 | - Ok(CastSizeChange::Ready) => (), | ||
| 5399 | - Ok(CastSizeChange::Pending) => continue, | ||
| 5400 | - Err(err) => { | ||
| 5401 | - warn!("error updating stream size, stopping screencast: {err:?}"); | ||
| 5402 | - casts_to_stop.push(cast.session_id); | ||
| 5403 | - } | ||
| 5404 | - } | ||
| 5405 | - | ||
| 5406 | - if cast.check_time_and_schedule(output, target_presentation_time) { | ||
| 5407 | - continue; | ||
| 5408 | - } | ||
| 5409 | - | ||
| 5410 | - let mut elements = Vec::new(); | ||
| 5411 | - mapped.render_for_screen_cast(renderer, scale, &mut |elem| { | ||
| 5412 | - elements.push(CastRenderElement::from(elem)) | ||
| 5413 | - }); | ||
| 5414 | - | ||
| 5415 | - let mut pointer_elements = Vec::new(); | ||
| 5416 | - let mut pointer_location = Point::default(); | ||
| 5417 | - | ||
| 5418 | - if self.pointer_visibility.is_visible() { | ||
| 5419 | - if let Some((pointer_pos, win_pos)) = self.pointer_pos_for_window_cast(mapped) { | ||
| 5420 | - // Pointer location must be relative to the screencast buffer. | ||
| 5421 | - // - win_pos is the position of the main window surface in output-local | ||
| 5422 | - // coordinates | ||
| 5423 | - // - bbox.loc moves us relative to the screencast buffer | ||
| 5424 | - let buf_pos = win_pos + bbox.loc.to_f64().to_logical(scale); | ||
| 5425 | - let output_pos = self.global_space.output_geometry(output).unwrap().loc; | ||
| 5426 | - pointer_location = pointer_pos - output_pos.to_f64() - buf_pos; | ||
| 5427 | - | ||
| 5428 | - let pos = buf_pos.to_physical_precise_round(scale).upscale(-1); | ||
| 5429 | - self.render_pointer(renderer, output, &mut |elem| { | ||
| 5430 | - let elem = | ||
| 5431 | - RelocateRenderElement::from_element(elem, pos, Relocate::Relative); | ||
| 5432 | - pointer_elements.push(CastRenderElement::from(elem)); | ||
| 5433 | - }); | ||
| 5434 | - } | ||
| 5435 | - } | ||
| 5436 | - let cursor_data = CursorData::compute(&pointer_elements, pointer_location, scale); | ||
| 5437 | - | ||
| 5438 | - if cast.dequeue_buffer_and_render(renderer, &elements, &cursor_data, bbox.size, scale) { | ||
| 5439 | - cast.last_frame_time = target_presentation_time; | ||
| 5440 | - } | ||
| 5441 | - } | ||
| 5442 | - self.casts = casts; | ||
| 5443 | - | ||
| 5444 | - for id in casts_to_stop { | ||
| 5445 | - self.stop_cast(id); | ||
| 5446 | - } | ||
| 5447 | - } | ||
| 5448 | - | ||
| 5449 | pub fn render_for_screencopy_with_damage( | 4874 | pub fn render_for_screencopy_with_damage( |
| 5450 | &mut self, | 4875 | &mut self, |
| 5451 | renderer: &mut GlesRenderer, | 4876 | renderer: &mut GlesRenderer, |
| @@ -5618,73 +5043,9 @@ impl Niri { | |||
| 5618 | Ok((sync, damages)) | 5043 | Ok((sync, damages)) |
| 5619 | } | 5044 | } |
| 5620 | 5045 | ||
| 5621 | - #[cfg(feature = "xdp-gnome-screencast")] | ||
| 5622 | - fn stop_cast(&mut self, session_id: usize) { | ||
| 5623 | - let _span = tracy_client::span!("Niri::stop_cast"); | ||
| 5624 | - | ||
| 5625 | - debug!(session_id, "StopCast"); | ||
| 5626 | - | ||
| 5627 | - for i in (0..self.casts.len()).rev() { | ||
| 5628 | - let cast = &self.casts[i]; | ||
| 5629 | - if cast.session_id != session_id { | ||
| 5630 | - continue; | ||
| 5631 | - } | ||
| 5632 | - | ||
| 5633 | - let cast = self.casts.swap_remove(i); | ||
| 5634 | - if let Err(err) = cast.stream.disconnect() { | ||
| 5635 | - warn!("error disconnecting stream: {err:?}"); | ||
| 5636 | - } | ||
| 5637 | - } | ||
| 5638 | - | ||
| 5639 | - let dbus = &self.dbus.as_ref().unwrap(); | ||
| 5640 | - let server = dbus.conn_screen_cast.as_ref().unwrap().object_server(); | ||
| 5641 | - let path = format!("/org/gnome/Mutter/ScreenCast/Session/u{session_id}"); | ||
| 5642 | - if let Ok(iface) = server.interface::<_, mutter_screen_cast::Session>(path) { | ||
| 5643 | - let _span = tracy_client::span!("invoking Session::stop"); | ||
| 5644 | - | ||
| 5645 | - async_io::block_on(async move { | ||
| 5646 | - iface | ||
| 5647 | - .get() | ||
| 5648 | - .stop(server.inner(), iface.signal_emitter().clone()) | ||
| 5649 | - .await | ||
| 5650 | - }); | ||
| 5651 | - } | ||
| 5652 | - } | ||
| 5653 | - | ||
| 5654 | #[cfg(not(feature = "xdp-gnome-screencast"))] | 5046 | #[cfg(not(feature = "xdp-gnome-screencast"))] |
| 5655 | pub fn stop_casts_for_target(&mut self, _target: CastTarget) {} | 5047 | pub fn stop_casts_for_target(&mut self, _target: CastTarget) {} |
| 5656 | 5048 | ||
| 5657 | - #[cfg(feature = "xdp-gnome-screencast")] | ||
| 5658 | - pub fn stop_casts_for_target(&mut self, target: CastTarget) { | ||
| 5659 | - let _span = tracy_client::span!("Niri::stop_casts_for_target"); | ||
| 5660 | - | ||
| 5661 | - // This is O(N^2) but it shouldn't be a problem I think. | ||
| 5662 | - let mut saw_dynamic = false; | ||
| 5663 | - let mut ids = Vec::new(); | ||
| 5664 | - for cast in &self.casts { | ||
| 5665 | - if cast.target != target { | ||
| 5666 | - continue; | ||
| 5667 | - } | ||
| 5668 | - | ||
| 5669 | - if cast.dynamic_target { | ||
| 5670 | - saw_dynamic = true; | ||
| 5671 | - continue; | ||
| 5672 | - } | ||
| 5673 | - | ||
| 5674 | - ids.push(cast.session_id); | ||
| 5675 | - } | ||
| 5676 | - | ||
| 5677 | - for id in ids { | ||
| 5678 | - self.stop_cast(id); | ||
| 5679 | - } | ||
| 5680 | - | ||
| 5681 | - // We don't stop dynamic casts, instead we switch them to Nothing. | ||
| 5682 | - if saw_dynamic { | ||
| 5683 | - self.event_loop | ||
| 5684 | - .insert_idle(|state| state.set_dynamic_cast_target(CastTarget::Nothing)); | ||
| 5685 | - } | ||
| 5686 | - } | ||
| 5687 | - | ||
| 5688 | pub fn remove_screencopy_output(&mut self, output: &Output) { | 5049 | pub fn remove_screencopy_output(&mut self, output: &Output) { |
| 5689 | let _span = tracy_client::span!("Niri::remove_screencopy_output"); | 5050 | let _span = tracy_client::span!("Niri::remove_screencopy_output"); |
| 5690 | for queue in self.screencopy_state.queues_mut() { | 5051 | for queue in self.screencopy_state.queues_mut() { |
| @@ -6741,15 +6102,6 @@ niri_render_elements! { | |||
| 6741 | } | 6102 | } |
| 6742 | } | 6103 | } |
| 6743 | 6104 | ||
| 6744 | -niri_render_elements! { | ||
| 6745 | - CastRenderElement<R> => { | ||
| 6746 | - Output = OutputRenderElements<R>, | ||
| 6747 | - Window = WindowCastRenderElements<R>, | ||
| 6748 | - Pointer = PointerRenderElements<R>, | ||
| 6749 | - RelocatedPointer = RelocateRenderElement<PointerRenderElements<R>>, | ||
| 6750 | - } | ||
| 6751 | -} | ||
| 6752 | - | ||
| 6753 | niri_render_elements! { | 6105 | niri_render_elements! { |
| 6754 | OutputRenderElements<R> => { | 6106 | OutputRenderElements<R> => { |
| 6755 | Monitor = MonitorRenderElement<R>, | 6107 | Monitor = MonitorRenderElement<R>, |
src/screencasting/mod.rsadded+674 -0
| @@ -0,0 +1,674 @@ | |||
| 1 | +use std::collections::hash_map::Entry; | ||
| 2 | +use std::collections::{HashMap, HashSet}; | ||
| 3 | +use std::mem; | ||
| 4 | +use std::time::Duration; | ||
| 5 | + | ||
| 6 | +use calloop::LoopHandle; | ||
| 7 | +use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement}; | ||
| 8 | +use smithay::backend::renderer::gles::GlesRenderer; | ||
| 9 | +use smithay::desktop::Window; | ||
| 10 | +use smithay::output::Output; | ||
| 11 | +use smithay::reexports::gbm::Modifier; | ||
| 12 | +use smithay::utils::{Point, Scale, Size}; | ||
| 13 | + | ||
| 14 | +use crate::dbus::mutter_screen_cast::{self, ScreenCastToNiri, StreamTargetId}; | ||
| 15 | +use crate::niri::{CastTarget, Niri, OutputRenderElements, PointerRenderElements, State}; | ||
| 16 | +use crate::niri_render_elements; | ||
| 17 | +use crate::render_helpers::RenderTarget; | ||
| 18 | +use crate::utils::get_monotonic_time; | ||
| 19 | +use crate::window::mapped::{MappedId, WindowCastRenderElements}; | ||
| 20 | + | ||
| 21 | +mod pw_utils; | ||
| 22 | +use pw_utils::{Cast, CastSizeChange, CursorData, PipeWire, PwToNiri}; | ||
| 23 | + | ||
| 24 | +pub struct Screencasting { | ||
| 25 | + pub casts: Vec<Cast>, | ||
| 26 | + pub pw_to_niri: calloop::channel::Sender<PwToNiri>, | ||
| 27 | + | ||
| 28 | + /// Screencast output for each mapped window. | ||
| 29 | + pub mapped_cast_output: HashMap<Window, Output>, | ||
| 30 | + | ||
| 31 | + /// Window ID for the "dynamic cast" special window for the xdp-gnome picker. | ||
| 32 | + pub dynamic_cast_id_for_portal: MappedId, | ||
| 33 | + | ||
| 34 | + // Drop PipeWire last, and specifically after casts, to prevent a double-free (yay). | ||
| 35 | + pub pipewire: Option<PipeWire>, | ||
| 36 | +} | ||
| 37 | + | ||
| 38 | +impl Screencasting { | ||
| 39 | + pub fn new(event_loop: &LoopHandle<'static, State>) -> Self { | ||
| 40 | + let pw_to_niri = { | ||
| 41 | + let (pw_to_niri, from_pipewire) = calloop::channel::channel(); | ||
| 42 | + event_loop | ||
| 43 | + .insert_source(from_pipewire, move |event, _, state| match event { | ||
| 44 | + calloop::channel::Event::Msg(msg) => state.on_pw_msg(msg), | ||
| 45 | + calloop::channel::Event::Closed => (), | ||
| 46 | + }) | ||
| 47 | + .unwrap(); | ||
| 48 | + pw_to_niri | ||
| 49 | + }; | ||
| 50 | + | ||
| 51 | + Self { | ||
| 52 | + casts: vec![], | ||
| 53 | + pw_to_niri, | ||
| 54 | + mapped_cast_output: HashMap::new(), | ||
| 55 | + dynamic_cast_id_for_portal: MappedId::next(), | ||
| 56 | + pipewire: None, | ||
| 57 | + } | ||
| 58 | + } | ||
| 59 | +} | ||
| 60 | + | ||
| 61 | +impl State { | ||
| 62 | + pub fn on_pw_msg(&mut self, msg: PwToNiri) { | ||
| 63 | + match msg { | ||
| 64 | + PwToNiri::StopCast { session_id } => self.niri.stop_cast(session_id), | ||
| 65 | + PwToNiri::Redraw { stream_id } => self.redraw_cast(stream_id), | ||
| 66 | + PwToNiri::FatalError => { | ||
| 67 | + warn!("stopping PipeWire due to fatal error"); | ||
| 68 | + let casting = &mut self.niri.casting; | ||
| 69 | + if let Some(pw) = casting.pipewire.take() { | ||
| 70 | + let ids: Vec<_> = casting.casts.iter().map(|cast| cast.session_id).collect(); | ||
| 71 | + for id in ids { | ||
| 72 | + self.niri.stop_cast(id); | ||
| 73 | + } | ||
| 74 | + self.niri.event_loop.remove(pw.token); | ||
| 75 | + } | ||
| 76 | + } | ||
| 77 | + } | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + fn redraw_cast(&mut self, stream_id: usize) { | ||
| 81 | + let _span = tracy_client::span!("State::redraw_cast"); | ||
| 82 | + | ||
| 83 | + let casts = &mut self.niri.casting.casts; | ||
| 84 | + let Some(idx) = casts.iter().position(|cast| cast.stream_id == stream_id) else { | ||
| 85 | + warn!("cast to redraw is missing"); | ||
| 86 | + return; | ||
| 87 | + }; | ||
| 88 | + let cast = &mut casts[idx]; | ||
| 89 | + | ||
| 90 | + let id = match &cast.target { | ||
| 91 | + CastTarget::Nothing => { | ||
| 92 | + self.backend.with_primary_renderer(|renderer| { | ||
| 93 | + if cast.dequeue_buffer_and_clear(renderer) { | ||
| 94 | + cast.last_frame_time = get_monotonic_time(); | ||
| 95 | + } | ||
| 96 | + }); | ||
| 97 | + return; | ||
| 98 | + } | ||
| 99 | + CastTarget::Output(weak) => { | ||
| 100 | + if let Some(output) = weak.upgrade() { | ||
| 101 | + self.niri.queue_redraw(&output); | ||
| 102 | + } | ||
| 103 | + return; | ||
| 104 | + } | ||
| 105 | + CastTarget::Window { id } => *id, | ||
| 106 | + }; | ||
| 107 | + | ||
| 108 | + // Lack of partial borrowing strikes again... | ||
| 109 | + let mut casts = mem::take(&mut self.niri.casting.casts); | ||
| 110 | + let cast = &mut casts[idx]; | ||
| 111 | + let mut stop = false; | ||
| 112 | + // Use a loop {} so we can break instead of early-return. | ||
| 113 | + #[allow(clippy::never_loop)] | ||
| 114 | + loop { | ||
| 115 | + let mut windows = self.niri.layout.windows(); | ||
| 116 | + let Some((_, mapped)) = windows.find(|(_, mapped)| mapped.id().get() == id) else { | ||
| 117 | + break; | ||
| 118 | + }; | ||
| 119 | + | ||
| 120 | + // Use the cached output since it will be present even if the output was | ||
| 121 | + // currently disconnected. | ||
| 122 | + let Some(output) = self.niri.casting.mapped_cast_output.get(&mapped.window) else { | ||
| 123 | + break; | ||
| 124 | + }; | ||
| 125 | + | ||
| 126 | + let scale = Scale::from(output.current_scale().fractional_scale()); | ||
| 127 | + let bbox = mapped | ||
| 128 | + .window | ||
| 129 | + .bbox_with_popups() | ||
| 130 | + .to_physical_precise_up(scale); | ||
| 131 | + | ||
| 132 | + match cast.ensure_size(bbox.size) { | ||
| 133 | + Ok(CastSizeChange::Ready) => (), | ||
| 134 | + Ok(CastSizeChange::Pending) => break, | ||
| 135 | + Err(err) => { | ||
| 136 | + warn!("error updating stream size, stopping screencast: {err:?}"); | ||
| 137 | + stop = true; | ||
| 138 | + break; | ||
| 139 | + } | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + self.backend.with_primary_renderer(|renderer| { | ||
| 143 | + let mut elements = Vec::new(); | ||
| 144 | + mapped.render_for_screen_cast(renderer, scale, &mut |elem| { | ||
| 145 | + elements.push(CastRenderElement::from(elem)) | ||
| 146 | + }); | ||
| 147 | + | ||
| 148 | + let mut pointer_elements = Vec::new(); | ||
| 149 | + let mut pointer_location = Point::default(); | ||
| 150 | + | ||
| 151 | + if self.niri.pointer_visibility.is_visible() { | ||
| 152 | + if let Some((pointer_pos, win_pos)) = | ||
| 153 | + self.niri.pointer_pos_for_window_cast(mapped) | ||
| 154 | + { | ||
| 155 | + // Pointer location must be relative to the screencast buffer. | ||
| 156 | + // - win_pos is the position of the main window surface in output-local | ||
| 157 | + // coordinates | ||
| 158 | + // - bbox.loc moves us relative to the screencast buffer | ||
| 159 | + let buf_pos = win_pos + bbox.loc.to_f64().to_logical(scale); | ||
| 160 | + let output_pos = | ||
| 161 | + self.niri.global_space.output_geometry(output).unwrap().loc; | ||
| 162 | + pointer_location = pointer_pos - output_pos.to_f64() - buf_pos; | ||
| 163 | + | ||
| 164 | + let pos = buf_pos.to_physical_precise_round(scale).upscale(-1); | ||
| 165 | + self.niri.render_pointer(renderer, output, &mut |elem| { | ||
| 166 | + let elem = | ||
| 167 | + RelocateRenderElement::from_element(elem, pos, Relocate::Relative); | ||
| 168 | + pointer_elements.push(CastRenderElement::from(elem)); | ||
| 169 | + }); | ||
| 170 | + } | ||
| 171 | + } | ||
| 172 | + let cursor_data = CursorData::compute(&pointer_elements, pointer_location, scale); | ||
| 173 | + | ||
| 174 | + if cast.dequeue_buffer_and_render( | ||
| 175 | + renderer, | ||
| 176 | + &elements, | ||
| 177 | + &cursor_data, | ||
| 178 | + bbox.size, | ||
| 179 | + scale, | ||
| 180 | + ) { | ||
| 181 | + cast.last_frame_time = get_monotonic_time(); | ||
| 182 | + } | ||
| 183 | + }); | ||
| 184 | + | ||
| 185 | + break; | ||
| 186 | + } | ||
| 187 | + let session_id = cast.session_id; | ||
| 188 | + self.niri.casting.casts = casts; | ||
| 189 | + | ||
| 190 | + if stop { | ||
| 191 | + self.niri.stop_cast(session_id); | ||
| 192 | + } | ||
| 193 | + } | ||
| 194 | + | ||
| 195 | + pub fn set_dynamic_cast_target(&mut self, target: CastTarget) { | ||
| 196 | + let _span = tracy_client::span!("State::set_dynamic_cast_target"); | ||
| 197 | + | ||
| 198 | + let mut refresh = None; | ||
| 199 | + match &target { | ||
| 200 | + // Leave refresh as is when clearing. Chances are, the next refresh will match it, | ||
| 201 | + // then we'll avoid reconfiguring. | ||
| 202 | + CastTarget::Nothing => (), | ||
| 203 | + CastTarget::Output(output) => { | ||
| 204 | + if let Some(output) = output.upgrade() { | ||
| 205 | + refresh = Some(output.current_mode().unwrap().refresh as u32); | ||
| 206 | + } | ||
| 207 | + } | ||
| 208 | + CastTarget::Window { id } => { | ||
| 209 | + let mut windows = self.niri.layout.windows(); | ||
| 210 | + if let Some((_, mapped)) = windows.find(|(_, mapped)| mapped.id().get() == *id) { | ||
| 211 | + if let Some(output) = self.niri.casting.mapped_cast_output.get(&mapped.window) { | ||
| 212 | + refresh = Some(output.current_mode().unwrap().refresh as u32); | ||
| 213 | + } | ||
| 214 | + } | ||
| 215 | + } | ||
| 216 | + } | ||
| 217 | + | ||
| 218 | + let mut to_redraw = Vec::new(); | ||
| 219 | + let mut to_stop = Vec::new(); | ||
| 220 | + for cast in &mut self.niri.casting.casts { | ||
| 221 | + if !cast.dynamic_target { | ||
| 222 | + continue; | ||
| 223 | + } | ||
| 224 | + | ||
| 225 | + if let Some(refresh) = refresh { | ||
| 226 | + if let Err(err) = cast.set_refresh(refresh) { | ||
| 227 | + warn!("error changing cast FPS: {err:?}"); | ||
| 228 | + to_stop.push(cast.session_id); | ||
| 229 | + continue; | ||
| 230 | + } | ||
| 231 | + } | ||
| 232 | + | ||
| 233 | + cast.target = target.clone(); | ||
| 234 | + to_redraw.push(cast.stream_id); | ||
| 235 | + } | ||
| 236 | + | ||
| 237 | + for id in to_redraw { | ||
| 238 | + self.redraw_cast(id); | ||
| 239 | + } | ||
| 240 | + } | ||
| 241 | + | ||
| 242 | + pub fn on_screen_cast_msg(&mut self, msg: ScreenCastToNiri) { | ||
| 243 | + match msg { | ||
| 244 | + ScreenCastToNiri::StartCast { | ||
| 245 | + session_id, | ||
| 246 | + stream_id, | ||
| 247 | + target, | ||
| 248 | + cursor_mode, | ||
| 249 | + signal_ctx, | ||
| 250 | + } => { | ||
| 251 | + let _span = tracy_client::span!("StartCast"); | ||
| 252 | + | ||
| 253 | + debug!(session_id, stream_id, "StartCast"); | ||
| 254 | + | ||
| 255 | + let Some(gbm) = self.backend.gbm_device() else { | ||
| 256 | + warn!("error starting screencast: no GBM device available"); | ||
| 257 | + self.niri.stop_cast(session_id); | ||
| 258 | + return; | ||
| 259 | + }; | ||
| 260 | + | ||
| 261 | + let pw = if let Some(pw) = &self.niri.casting.pipewire { | ||
| 262 | + pw | ||
| 263 | + } else { | ||
| 264 | + match PipeWire::new( | ||
| 265 | + self.niri.event_loop.clone(), | ||
| 266 | + self.niri.casting.pw_to_niri.clone(), | ||
| 267 | + ) { | ||
| 268 | + Ok(pipewire) => self.niri.casting.pipewire.insert(pipewire), | ||
| 269 | + Err(err) => { | ||
| 270 | + warn!( | ||
| 271 | + "error starting screencast: PipeWire failed to initialize: {err:?}" | ||
| 272 | + ); | ||
| 273 | + self.niri.stop_cast(session_id); | ||
| 274 | + return; | ||
| 275 | + } | ||
| 276 | + } | ||
| 277 | + }; | ||
| 278 | + | ||
| 279 | + let mut dynamic_target = false; | ||
| 280 | + let (target, size, refresh, alpha) = match target { | ||
| 281 | + StreamTargetId::Output { name } => { | ||
| 282 | + let global_space = &self.niri.global_space; | ||
| 283 | + let output = global_space.outputs().find(|out| out.name() == name); | ||
| 284 | + let Some(output) = output else { | ||
| 285 | + warn!("error starting screencast: requested output is missing"); | ||
| 286 | + self.niri.stop_cast(session_id); | ||
| 287 | + return; | ||
| 288 | + }; | ||
| 289 | + | ||
| 290 | + let mode = output.current_mode().unwrap(); | ||
| 291 | + let transform = output.current_transform(); | ||
| 292 | + let size = transform.transform_size(mode.size); | ||
| 293 | + let refresh = mode.refresh as u32; | ||
| 294 | + (CastTarget::Output(output.downgrade()), size, refresh, false) | ||
| 295 | + } | ||
| 296 | + StreamTargetId::Window { id } | ||
| 297 | + if id == self.niri.casting.dynamic_cast_id_for_portal.get() => | ||
| 298 | + { | ||
| 299 | + dynamic_target = true; | ||
| 300 | + | ||
| 301 | + // All dynamic casts start as Nothing to avoid surprises and exposing | ||
| 302 | + // sensitive info. | ||
| 303 | + (CastTarget::Nothing, Size::from((1, 1)), 1000, true) | ||
| 304 | + } | ||
| 305 | + StreamTargetId::Window { id } => { | ||
| 306 | + let Some(window) = self.niri.layout.windows().find_map(|(_, mapped)| { | ||
| 307 | + (mapped.id().get() == id).then_some(&mapped.window) | ||
| 308 | + }) else { | ||
| 309 | + warn!("error starting screencast: requested window is missing"); | ||
| 310 | + self.niri.stop_cast(session_id); | ||
| 311 | + return; | ||
| 312 | + }; | ||
| 313 | + | ||
| 314 | + // Use the cached output since it will be present even if the output was | ||
| 315 | + // currently disconnected. | ||
| 316 | + let Some(output) = self.niri.casting.mapped_cast_output.get(window) else { | ||
| 317 | + warn!("error starting screencast: requested window is missing"); | ||
| 318 | + self.niri.stop_cast(session_id); | ||
| 319 | + return; | ||
| 320 | + }; | ||
| 321 | + | ||
| 322 | + let scale = Scale::from(output.current_scale().fractional_scale()); | ||
| 323 | + let bbox = window.bbox_with_popups().to_physical_precise_up(scale); | ||
| 324 | + let refresh = output.current_mode().unwrap().refresh as u32; | ||
| 325 | + | ||
| 326 | + (CastTarget::Window { id }, bbox.size, refresh, true) | ||
| 327 | + } | ||
| 328 | + }; | ||
| 329 | + | ||
| 330 | + let mut render_formats = self | ||
| 331 | + .backend | ||
| 332 | + .with_primary_renderer(|renderer| { | ||
| 333 | + renderer.egl_context().dmabuf_render_formats().clone() | ||
| 334 | + }) | ||
| 335 | + .unwrap_or_default(); | ||
| 336 | + | ||
| 337 | + { | ||
| 338 | + let config = self.niri.config.borrow(); | ||
| 339 | + if config.debug.force_pipewire_invalid_modifier { | ||
| 340 | + render_formats = render_formats | ||
| 341 | + .into_iter() | ||
| 342 | + .filter(|f| f.modifier == Modifier::Invalid) | ||
| 343 | + .collect(); | ||
| 344 | + } | ||
| 345 | + } | ||
| 346 | + | ||
| 347 | + let res = pw.start_cast( | ||
| 348 | + gbm, | ||
| 349 | + render_formats, | ||
| 350 | + session_id, | ||
| 351 | + stream_id, | ||
| 352 | + target, | ||
| 353 | + dynamic_target, | ||
| 354 | + size, | ||
| 355 | + refresh, | ||
| 356 | + alpha, | ||
| 357 | + cursor_mode, | ||
| 358 | + signal_ctx, | ||
| 359 | + ); | ||
| 360 | + match res { | ||
| 361 | + Ok(cast) => { | ||
| 362 | + self.niri.casting.casts.push(cast); | ||
| 363 | + } | ||
| 364 | + Err(err) => { | ||
| 365 | + warn!("error starting screencast: {err:?}"); | ||
| 366 | + self.niri.stop_cast(session_id); | ||
| 367 | + } | ||
| 368 | + } | ||
| 369 | + } | ||
| 370 | + ScreenCastToNiri::StopCast { session_id } => self.niri.stop_cast(session_id), | ||
| 371 | + } | ||
| 372 | + } | ||
| 373 | +} | ||
| 374 | + | ||
| 375 | +impl Niri { | ||
| 376 | + pub fn refresh_mapped_cast_window_rules(&mut self) { | ||
| 377 | + // O(N^2) but should be fine since there aren't many casts usually. | ||
| 378 | + self.layout.with_windows_mut(|mapped, _| { | ||
| 379 | + let id = mapped.id().get(); | ||
| 380 | + // Find regardless of cast.is_active. | ||
| 381 | + let value = self | ||
| 382 | + .casting | ||
| 383 | + .casts | ||
| 384 | + .iter() | ||
| 385 | + .any(|cast| cast.target == (CastTarget::Window { id })); | ||
| 386 | + mapped.set_is_window_cast_target(value); | ||
| 387 | + }); | ||
| 388 | + } | ||
| 389 | + | ||
| 390 | + pub fn refresh_mapped_cast_outputs(&mut self) { | ||
| 391 | + let mut seen = HashSet::new(); | ||
| 392 | + let mut output_changed = vec![]; | ||
| 393 | + | ||
| 394 | + self.layout.with_windows(|mapped, output, _, _| { | ||
| 395 | + seen.insert(mapped.window.clone()); | ||
| 396 | + | ||
| 397 | + let Some(output) = output else { | ||
| 398 | + return; | ||
| 399 | + }; | ||
| 400 | + | ||
| 401 | + match self.casting.mapped_cast_output.entry(mapped.window.clone()) { | ||
| 402 | + Entry::Occupied(mut entry) => { | ||
| 403 | + if entry.get() != output { | ||
| 404 | + entry.insert(output.clone()); | ||
| 405 | + output_changed.push((mapped.id(), output.clone())); | ||
| 406 | + } | ||
| 407 | + } | ||
| 408 | + Entry::Vacant(entry) => { | ||
| 409 | + entry.insert(output.clone()); | ||
| 410 | + } | ||
| 411 | + } | ||
| 412 | + }); | ||
| 413 | + | ||
| 414 | + self.casting | ||
| 415 | + .mapped_cast_output | ||
| 416 | + .retain(|win, _| seen.contains(win)); | ||
| 417 | + | ||
| 418 | + let mut to_stop = vec![]; | ||
| 419 | + for (id, out) in output_changed { | ||
| 420 | + let refresh = out.current_mode().unwrap().refresh as u32; | ||
| 421 | + let target = CastTarget::Window { id: id.get() }; | ||
| 422 | + for cast in self | ||
| 423 | + .casting | ||
| 424 | + .casts | ||
| 425 | + .iter_mut() | ||
| 426 | + .filter(|cast| cast.target == target) | ||
| 427 | + { | ||
| 428 | + if let Err(err) = cast.set_refresh(refresh) { | ||
| 429 | + warn!("error changing cast FPS: {err:?}"); | ||
| 430 | + to_stop.push(cast.session_id); | ||
| 431 | + }; | ||
| 432 | + } | ||
| 433 | + } | ||
| 434 | + | ||
| 435 | + for session_id in to_stop { | ||
| 436 | + self.stop_cast(session_id); | ||
| 437 | + } | ||
| 438 | + } | ||
| 439 | + | ||
| 440 | + pub fn render_for_screen_cast( | ||
| 441 | + &mut self, | ||
| 442 | + renderer: &mut GlesRenderer, | ||
| 443 | + output: &Output, | ||
| 444 | + target_presentation_time: Duration, | ||
| 445 | + ) { | ||
| 446 | + let _span = tracy_client::span!("Niri::render_for_screen_cast"); | ||
| 447 | + | ||
| 448 | + let target = CastTarget::Output(output.downgrade()); | ||
| 449 | + | ||
| 450 | + let size = output.current_mode().unwrap().size; | ||
| 451 | + let transform = output.current_transform(); | ||
| 452 | + let size = transform.transform_size(size); | ||
| 453 | + | ||
| 454 | + let scale = Scale::from(output.current_scale().fractional_scale()); | ||
| 455 | + | ||
| 456 | + let mut elements = Vec::new(); | ||
| 457 | + let mut pointer = Vec::new(); | ||
| 458 | + let mut cursor_data = None; | ||
| 459 | + | ||
| 460 | + let mut casts_to_stop = vec![]; | ||
| 461 | + | ||
| 462 | + let mut casts = mem::take(&mut self.casting.casts); | ||
| 463 | + for cast in &mut casts { | ||
| 464 | + if !cast.is_active() { | ||
| 465 | + continue; | ||
| 466 | + } | ||
| 467 | + | ||
| 468 | + if cast.target != target { | ||
| 469 | + continue; | ||
| 470 | + } | ||
| 471 | + | ||
| 472 | + match cast.ensure_size(size) { | ||
| 473 | + Ok(CastSizeChange::Ready) => (), | ||
| 474 | + Ok(CastSizeChange::Pending) => continue, | ||
| 475 | + Err(err) => { | ||
| 476 | + warn!("error updating stream size, stopping screencast: {err:?}"); | ||
| 477 | + casts_to_stop.push(cast.session_id); | ||
| 478 | + } | ||
| 479 | + } | ||
| 480 | + | ||
| 481 | + if cast.check_time_and_schedule(output, target_presentation_time) { | ||
| 482 | + continue; | ||
| 483 | + } | ||
| 484 | + | ||
| 485 | + if cursor_data.is_none() { | ||
| 486 | + // FIXME: support debug draw opaque regions. | ||
| 487 | + self.render_inner( | ||
| 488 | + renderer, | ||
| 489 | + output, | ||
| 490 | + false, | ||
| 491 | + RenderTarget::Screencast, | ||
| 492 | + &mut |elem| elements.push(elem.into()), | ||
| 493 | + ); | ||
| 494 | + | ||
| 495 | + if self.pointer_visibility.is_visible() { | ||
| 496 | + self.render_pointer(renderer, output, &mut |elem| pointer.push(elem.into())); | ||
| 497 | + } | ||
| 498 | + | ||
| 499 | + let output_pos = self.global_space.output_geometry(output).unwrap().loc; | ||
| 500 | + let pointer_pos = self | ||
| 501 | + .tablet_cursor_location | ||
| 502 | + .unwrap_or_else(|| self.seat.get_pointer().unwrap().current_location()); | ||
| 503 | + let pointer_pos = pointer_pos - output_pos.to_f64(); | ||
| 504 | + | ||
| 505 | + cursor_data = Some(CursorData::compute(&pointer, pointer_pos, scale)); | ||
| 506 | + } | ||
| 507 | + let cursor_data = cursor_data.as_ref().unwrap(); | ||
| 508 | + | ||
| 509 | + if cast.dequeue_buffer_and_render(renderer, &elements, cursor_data, size, scale) { | ||
| 510 | + cast.last_frame_time = target_presentation_time; | ||
| 511 | + } | ||
| 512 | + } | ||
| 513 | + self.casting.casts = casts; | ||
| 514 | + | ||
| 515 | + for id in casts_to_stop { | ||
| 516 | + self.stop_cast(id); | ||
| 517 | + } | ||
| 518 | + } | ||
| 519 | + | ||
| 520 | + pub fn render_windows_for_screen_cast( | ||
| 521 | + &mut self, | ||
| 522 | + renderer: &mut GlesRenderer, | ||
| 523 | + output: &Output, | ||
| 524 | + target_presentation_time: Duration, | ||
| 525 | + ) { | ||
| 526 | + let _span = tracy_client::span!("Niri::render_windows_for_screen_cast"); | ||
| 527 | + | ||
| 528 | + let scale = Scale::from(output.current_scale().fractional_scale()); | ||
| 529 | + | ||
| 530 | + let mut casts_to_stop = vec![]; | ||
| 531 | + | ||
| 532 | + let mut casts = mem::take(&mut self.casting.casts); | ||
| 533 | + for cast in &mut casts { | ||
| 534 | + if !cast.is_active() { | ||
| 535 | + continue; | ||
| 536 | + } | ||
| 537 | + | ||
| 538 | + let CastTarget::Window { id } = cast.target else { | ||
| 539 | + continue; | ||
| 540 | + }; | ||
| 541 | + | ||
| 542 | + let mut windows = self.layout.windows_for_output(output); | ||
| 543 | + let Some(mapped) = windows.find(|win| win.id().get() == id) else { | ||
| 544 | + continue; | ||
| 545 | + }; | ||
| 546 | + | ||
| 547 | + let bbox = mapped | ||
| 548 | + .window | ||
| 549 | + .bbox_with_popups() | ||
| 550 | + .to_physical_precise_up(scale); | ||
| 551 | + | ||
| 552 | + match cast.ensure_size(bbox.size) { | ||
| 553 | + Ok(CastSizeChange::Ready) => (), | ||
| 554 | + Ok(CastSizeChange::Pending) => continue, | ||
| 555 | + Err(err) => { | ||
| 556 | + warn!("error updating stream size, stopping screencast: {err:?}"); | ||
| 557 | + casts_to_stop.push(cast.session_id); | ||
| 558 | + } | ||
| 559 | + } | ||
| 560 | + | ||
| 561 | + if cast.check_time_and_schedule(output, target_presentation_time) { | ||
| 562 | + continue; | ||
| 563 | + } | ||
| 564 | + | ||
| 565 | + let mut elements = Vec::new(); | ||
| 566 | + mapped.render_for_screen_cast(renderer, scale, &mut |elem| { | ||
| 567 | + elements.push(CastRenderElement::from(elem)) | ||
| 568 | + }); | ||
| 569 | + | ||
| 570 | + let mut pointer_elements = Vec::new(); | ||
| 571 | + let mut pointer_location = Point::default(); | ||
| 572 | + | ||
| 573 | + if self.pointer_visibility.is_visible() { | ||
| 574 | + if let Some((pointer_pos, win_pos)) = self.pointer_pos_for_window_cast(mapped) { | ||
| 575 | + // Pointer location must be relative to the screencast buffer. | ||
| 576 | + // - win_pos is the position of the main window surface in output-local | ||
| 577 | + // coordinates | ||
| 578 | + // - bbox.loc moves us relative to the screencast buffer | ||
| 579 | + let buf_pos = win_pos + bbox.loc.to_f64().to_logical(scale); | ||
| 580 | + let output_pos = self.global_space.output_geometry(output).unwrap().loc; | ||
| 581 | + pointer_location = pointer_pos - output_pos.to_f64() - buf_pos; | ||
| 582 | + | ||
| 583 | + let pos = buf_pos.to_physical_precise_round(scale).upscale(-1); | ||
| 584 | + self.render_pointer(renderer, output, &mut |elem| { | ||
| 585 | + let elem = | ||
| 586 | + RelocateRenderElement::from_element(elem, pos, Relocate::Relative); | ||
| 587 | + pointer_elements.push(CastRenderElement::from(elem)); | ||
| 588 | + }); | ||
| 589 | + } | ||
| 590 | + } | ||
| 591 | + let cursor_data = CursorData::compute(&pointer_elements, pointer_location, scale); | ||
| 592 | + | ||
| 593 | + if cast.dequeue_buffer_and_render(renderer, &elements, &cursor_data, bbox.size, scale) { | ||
| 594 | + cast.last_frame_time = target_presentation_time; | ||
| 595 | + } | ||
| 596 | + } | ||
| 597 | + self.casting.casts = casts; | ||
| 598 | + | ||
| 599 | + for id in casts_to_stop { | ||
| 600 | + self.stop_cast(id); | ||
| 601 | + } | ||
| 602 | + } | ||
| 603 | + | ||
| 604 | + fn stop_cast(&mut self, session_id: usize) { | ||
| 605 | + let _span = tracy_client::span!("Niri::stop_cast"); | ||
| 606 | + | ||
| 607 | + debug!(session_id, "StopCast"); | ||
| 608 | + | ||
| 609 | + for i in (0..self.casting.casts.len()).rev() { | ||
| 610 | + let cast = &self.casting.casts[i]; | ||
| 611 | + if cast.session_id != session_id { | ||
| 612 | + continue; | ||
| 613 | + } | ||
| 614 | + | ||
| 615 | + let cast = self.casting.casts.swap_remove(i); | ||
| 616 | + if let Err(err) = cast.stream.disconnect() { | ||
| 617 | + warn!("error disconnecting stream: {err:?}"); | ||
| 618 | + } | ||
| 619 | + } | ||
| 620 | + | ||
| 621 | + let dbus = &self.dbus.as_ref().unwrap(); | ||
| 622 | + let server = dbus.conn_screen_cast.as_ref().unwrap().object_server(); | ||
| 623 | + let path = format!("/org/gnome/Mutter/ScreenCast/Session/u{session_id}"); | ||
| 624 | + if let Ok(iface) = server.interface::<_, mutter_screen_cast::Session>(path) { | ||
| 625 | + let _span = tracy_client::span!("invoking Session::stop"); | ||
| 626 | + | ||
| 627 | + async_io::block_on(async move { | ||
| 628 | + iface | ||
| 629 | + .get() | ||
| 630 | + .stop(server.inner(), iface.signal_emitter().clone()) | ||
| 631 | + .await | ||
| 632 | + }); | ||
| 633 | + } | ||
| 634 | + } | ||
| 635 | + | ||
| 636 | + pub fn stop_casts_for_target(&mut self, target: CastTarget) { | ||
| 637 | + let _span = tracy_client::span!("Niri::stop_casts_for_target"); | ||
| 638 | + | ||
| 639 | + // This is O(N^2) but it shouldn't be a problem I think. | ||
| 640 | + let mut saw_dynamic = false; | ||
| 641 | + let mut ids = Vec::new(); | ||
| 642 | + for cast in &self.casting.casts { | ||
| 643 | + if cast.target != target { | ||
| 644 | + continue; | ||
| 645 | + } | ||
| 646 | + | ||
| 647 | + if cast.dynamic_target { | ||
| 648 | + saw_dynamic = true; | ||
| 649 | + continue; | ||
| 650 | + } | ||
| 651 | + | ||
| 652 | + ids.push(cast.session_id); | ||
| 653 | + } | ||
| 654 | + | ||
| 655 | + for id in ids { | ||
| 656 | + self.stop_cast(id); | ||
| 657 | + } | ||
| 658 | + | ||
| 659 | + // We don't stop dynamic casts, instead we switch them to Nothing. | ||
| 660 | + if saw_dynamic { | ||
| 661 | + self.event_loop | ||
| 662 | + .insert_idle(|state| state.set_dynamic_cast_target(CastTarget::Nothing)); | ||
| 663 | + } | ||
| 664 | + } | ||
| 665 | +} | ||
| 666 | + | ||
| 667 | +niri_render_elements! { | ||
| 668 | + CastRenderElement<R> => { | ||
| 669 | + Output = OutputRenderElements<R>, | ||
| 670 | + Window = WindowCastRenderElements<R>, | ||
| 671 | + Pointer = PointerRenderElements<R>, | ||
| 672 | + RelocatedPointer = RelocateRenderElement<PointerRenderElements<R>>, | ||
| 673 | + } | ||
| 674 | +} | ||
src/pw_utils.rs → src/screencasting/pw_utils.rsrenamed+3 -2
| @@ -50,10 +50,11 @@ use smithay::utils::{Logical, Physical, Point, Scale, Size, Transform}; | |||
| 50 | use zbus::object_server::SignalEmitter; | 50 | use zbus::object_server::SignalEmitter; |
| 51 | 51 | ||
| 52 | use crate::dbus::mutter_screen_cast::{self, CursorMode}; | 52 | use crate::dbus::mutter_screen_cast::{self, CursorMode}; |
| 53 | -use crate::niri::{CastRenderElement, CastTarget, State}; | 53 | +use crate::niri::{CastTarget, State}; |
| 54 | use crate::render_helpers::{ | 54 | use crate::render_helpers::{ |
| 55 | clear_dmabuf, encompassing_geo, render_and_download, render_to_dmabuf, | 55 | clear_dmabuf, encompassing_geo, render_and_download, render_to_dmabuf, |
| 56 | }; | 56 | }; |
| 57 | +use crate::screencasting::CastRenderElement; | ||
| 57 | use crate::utils::get_monotonic_time; | 58 | use crate::utils::get_monotonic_time; |
| 58 | 59 | ||
| 59 | // Give a 0.1 ms allowance for presentation time errors. | 60 | // Give a 0.1 ms allowance for presentation time errors. |
| @@ -1040,7 +1041,7 @@ impl Cast { | |||
| 1040 | let source = Generic::new(sync_fd, Interest::READ, Mode::OneShot); | 1041 | let source = Generic::new(sync_fd, Interest::READ, Mode::OneShot); |
| 1041 | self.event_loop | 1042 | self.event_loop |
| 1042 | .insert_source(source, move |_, _, state| { | 1043 | .insert_source(source, move |_, _, state| { |
| 1043 | - for cast in &mut state.niri.casts { | 1044 | + for cast in &mut state.niri.casting.casts { |
| 1044 | if cast.stream_id == stream_id { | 1045 | if cast.stream_id == stream_id { |
| 1045 | cast.queue_completed_buffers(); | 1046 | cast.queue_completed_buffers(); |
| 1046 | } | 1047 | } |