niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·64a9351·Ivan Molodetskikh·2 years ago·1319 commits
| 1 | use std::collections::HashMap; |
| 2 | use std::sync::atomic::{AtomicBool, Ordering}; |
| 3 | use std::sync::Arc; |
| 4 | use std::time::Duration; |
| 5 | |
| 6 | use calloop::generic::Generic; |
| 7 | use calloop::{Interest, LoopHandle, Mode, PostAction}; |
| 8 | use smithay::backend::allocator::dmabuf::Dmabuf; |
| 9 | use smithay::backend::allocator::{Buffer, Fourcc}; |
| 10 | use smithay::backend::renderer::damage::OutputDamageTracker; |
| 11 | use smithay::backend::renderer::sync::SyncPoint; |
| 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::{ |
| 18 | zwlr_screencopy_frame_v1, zwlr_screencopy_manager_v1, |
| 19 | }; |
| 20 | use smithay::reexports::wayland_server::protocol::wl_buffer::WlBuffer; |
| 21 | use smithay::reexports::wayland_server::protocol::wl_shm::Format; |
| 22 | use smithay::reexports::wayland_server::{ |
| 23 | Client, DataInit, Dispatch, DisplayHandle, GlobalDispatch, New, Resource, |
| 24 | }; |
| 25 | use smithay::utils::{Physical, Point, Rectangle, Size, Transform}; |
| 26 | use smithay::wayland::{dmabuf, shm}; |
| 27 | |
| 28 | use crate::utils::get_monotonic_time; |
| 29 | |
| 30 | const VERSION: u32 = 3; |
| 31 | |
| 32 | pub struct ScreencopyQueue { |
| 33 | damage_tracker: OutputDamageTracker, |
| 34 | screencopies: Vec<Screencopy>, |
| 35 | } |
| 36 | |
| 37 | impl Default for ScreencopyQueue { |
| 38 | fn default() -> Self { |
| 39 | Self::new() |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | impl ScreencopyQueue { |
| 44 | pub fn new() -> Self { |
| 45 | Self { |
| 46 | damage_tracker: OutputDamageTracker::new((0, 0), 1.0, Transform::Normal), |
| 47 | screencopies: Vec::new(), |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | pub fn split(&mut self) -> (&mut OutputDamageTracker, Option<&Screencopy>) { |
| 52 | let ScreencopyQueue { |
| 53 | damage_tracker, |
| 54 | screencopies, |
| 55 | } = self; |
| 56 | (damage_tracker, screencopies.first()) |
| 57 | } |
| 58 | |
| 59 | pub fn push(&mut self, screencopy: Screencopy) { |
| 60 | self.screencopies.push(screencopy); |
| 61 | } |
| 62 | |
| 63 | pub fn pop(&mut self) -> Screencopy { |
| 64 | self.screencopies.pop().unwrap() |
| 65 | } |
| 66 | |
| 67 | pub fn remove_output(&mut self, output: &Output) { |
| 68 | self.screencopies |
| 69 | .retain(|screencopy| screencopy.output() != output); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | #[derive(Default)] |
| 74 | pub struct ScreencopyManagerState { |
| 75 | queues: HashMap<ZwlrScreencopyManagerV1, ScreencopyQueue>, |
| 76 | } |
| 77 | |
| 78 | pub struct ScreencopyManagerGlobalData { |
| 79 | filter: Box<dyn for<'c> Fn(&'c Client) -> bool + Send + Sync>, |
| 80 | } |
| 81 | |
| 82 | impl ScreencopyManagerState { |
| 83 | pub fn new<D, F>(display: &DisplayHandle, filter: F) -> Self |
| 84 | where |
| 85 | D: GlobalDispatch<ZwlrScreencopyManagerV1, ScreencopyManagerGlobalData>, |
| 86 | D: Dispatch<ZwlrScreencopyManagerV1, ()>, |
| 87 | D: Dispatch<ZwlrScreencopyFrameV1, ScreencopyFrameState>, |
| 88 | D: ScreencopyHandler, |
| 89 | D: 'static, |
| 90 | F: for<'c> Fn(&'c Client) -> bool + Send + Sync + 'static, |
| 91 | { |
| 92 | let global_data = ScreencopyManagerGlobalData { |
| 93 | filter: Box::new(filter), |
| 94 | }; |
| 95 | display.create_global::<D, ZwlrScreencopyManagerV1, _>(VERSION, global_data); |
| 96 | |
| 97 | Self { |
| 98 | queues: HashMap::new(), |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | pub fn bind(&mut self, manager: &ZwlrScreencopyManagerV1) { |
| 103 | // Clean up all entries if its manager is dead and its queue is empty. |
| 104 | self.queues |
| 105 | .retain(|k, v| k.is_alive() || !v.screencopies.is_empty()); |
| 106 | |
| 107 | self.queues.insert(manager.clone(), ScreencopyQueue::new()); |
| 108 | } |
| 109 | |
| 110 | pub fn get_queue_mut( |
| 111 | &mut self, |
| 112 | manager: &ZwlrScreencopyManagerV1, |
| 113 | ) -> Option<&mut ScreencopyQueue> { |
| 114 | self.queues.get_mut(manager) |
| 115 | } |
| 116 | |
| 117 | pub fn queues_mut(&mut self) -> impl Iterator<Item = &mut ScreencopyQueue> { |
| 118 | self.queues.values_mut() |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | impl<D> GlobalDispatch<ZwlrScreencopyManagerV1, ScreencopyManagerGlobalData, D> |
| 123 | for ScreencopyManagerState |
| 124 | where |
| 125 | D: GlobalDispatch<ZwlrScreencopyManagerV1, ScreencopyManagerGlobalData>, |
| 126 | D: Dispatch<ZwlrScreencopyManagerV1, ()>, |
| 127 | D: Dispatch<ZwlrScreencopyFrameV1, ScreencopyFrameState>, |
| 128 | D: ScreencopyHandler, |
| 129 | D: 'static, |
| 130 | { |
| 131 | fn bind( |
| 132 | state: &mut D, |
| 133 | _display: &DisplayHandle, |
| 134 | _client: &Client, |
| 135 | manager: New<ZwlrScreencopyManagerV1>, |
| 136 | _manager_state: &ScreencopyManagerGlobalData, |
| 137 | data_init: &mut DataInit<'_, D>, |
| 138 | ) { |
| 139 | let manager = data_init.init(manager, ()); |
| 140 | state.screencopy_state().bind(&manager); |
| 141 | } |
| 142 | |
| 143 | fn can_view(client: Client, global_data: &ScreencopyManagerGlobalData) -> bool { |
| 144 | (global_data.filter)(&client) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | impl<D> Dispatch<ZwlrScreencopyManagerV1, (), D> for ScreencopyManagerState |
| 149 | where |
| 150 | D: GlobalDispatch<ZwlrScreencopyManagerV1, ScreencopyManagerGlobalData>, |
| 151 | D: Dispatch<ZwlrScreencopyManagerV1, ()>, |
| 152 | D: Dispatch<ZwlrScreencopyFrameV1, ScreencopyFrameState>, |
| 153 | D: ScreencopyHandler, |
| 154 | D: 'static, |
| 155 | { |
| 156 | fn request( |
| 157 | _state: &mut D, |
| 158 | _client: &Client, |
| 159 | manager: &ZwlrScreencopyManagerV1, |
| 160 | request: zwlr_screencopy_manager_v1::Request, |
| 161 | _data: &(), |
| 162 | _display: &DisplayHandle, |
| 163 | data_init: &mut DataInit<'_, D>, |
| 164 | ) { |
| 165 | let (frame, overlay_cursor, buffer_size, region_loc, output) = match request { |
| 166 | zwlr_screencopy_manager_v1::Request::CaptureOutput { |
| 167 | frame, |
| 168 | overlay_cursor, |
| 169 | output, |
| 170 | } => { |
| 171 | let Some(output) = Output::from_resource(&output) else { |
| 172 | trace!("screencopy client requested non-existent output"); |
| 173 | let frame = data_init.init(frame, ScreencopyFrameState::Failed); |
| 174 | frame.failed(); |
| 175 | return; |
| 176 | }; |
| 177 | |
| 178 | let buffer_size = output.current_mode().unwrap().size; |
| 179 | let region_loc = Point::from((0, 0)); |
| 180 | |
| 181 | (frame, overlay_cursor, buffer_size, region_loc, output) |
| 182 | } |
| 183 | zwlr_screencopy_manager_v1::Request::CaptureOutputRegion { |
| 184 | frame, |
| 185 | overlay_cursor, |
| 186 | x, |
| 187 | y, |
| 188 | width, |
| 189 | height, |
| 190 | output, |
| 191 | } => { |
| 192 | if width <= 0 || height <= 0 { |
| 193 | trace!("screencopy client requested invalid sized region"); |
| 194 | let frame = data_init.init(frame, ScreencopyFrameState::Failed); |
| 195 | frame.failed(); |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | let Some(output) = Output::from_resource(&output) else { |
| 200 | trace!("screencopy client requested non-existent output"); |
| 201 | let frame = data_init.init(frame, ScreencopyFrameState::Failed); |
| 202 | frame.failed(); |
| 203 | return; |
| 204 | }; |
| 205 | |
| 206 | let output_transform = output.current_transform(); |
| 207 | let output_physical_size = |
| 208 | output_transform.transform_size(output.current_mode().unwrap().size); |
| 209 | let output_rect = Rectangle::from_loc_and_size((0, 0), output_physical_size); |
| 210 | |
| 211 | let rect = Rectangle::from_loc_and_size((x, y), (width, height)); |
| 212 | |
| 213 | let output_scale = output.current_scale().fractional_scale(); |
| 214 | let physical_rect = rect.to_physical_precise_round(output_scale); |
| 215 | |
| 216 | // Clamp captured region to the output. |
| 217 | let Some(clamped_rect) = physical_rect.intersection(output_rect) else { |
| 218 | trace!("screencopy client requested region outside of output"); |
| 219 | let frame = data_init.init(frame, ScreencopyFrameState::Failed); |
| 220 | frame.failed(); |
| 221 | return; |
| 222 | }; |
| 223 | |
| 224 | let untransformed_rect = output_transform |
| 225 | .invert() |
| 226 | .transform_rect_in(clamped_rect, &output_physical_size); |
| 227 | |
| 228 | ( |
| 229 | frame, |
| 230 | overlay_cursor, |
| 231 | untransformed_rect.size, |
| 232 | clamped_rect.loc, |
| 233 | output, |
| 234 | ) |
| 235 | } |
| 236 | zwlr_screencopy_manager_v1::Request::Destroy => return, |
| 237 | _ => unreachable!(), |
| 238 | }; |
| 239 | |
| 240 | // Create the frame. |
| 241 | let overlay_cursor = overlay_cursor != 0; |
| 242 | let info = ScreencopyFrameInfo { |
| 243 | output, |
| 244 | overlay_cursor, |
| 245 | buffer_size, |
| 246 | region_loc, |
| 247 | }; |
| 248 | let frame = data_init.init( |
| 249 | frame, |
| 250 | ScreencopyFrameState::Pending { |
| 251 | manager: manager.clone(), |
| 252 | info, |
| 253 | copied: Arc::new(AtomicBool::new(false)), |
| 254 | }, |
| 255 | ); |
| 256 | |
| 257 | // Send desired SHM buffer parameters. |
| 258 | frame.buffer( |
| 259 | Format::Xrgb8888, |
| 260 | buffer_size.w as u32, |
| 261 | buffer_size.h as u32, |
| 262 | buffer_size.w as u32 * 4, |
| 263 | ); |
| 264 | |
| 265 | if frame.version() >= 3 { |
| 266 | // Send desired DMA buffer parameters. |
| 267 | frame.linux_dmabuf( |
| 268 | Fourcc::Xrgb8888 as u32, |
| 269 | buffer_size.w as u32, |
| 270 | buffer_size.h as u32, |
| 271 | ); |
| 272 | |
| 273 | // Notify client that all supported buffers were enumerated. |
| 274 | frame.buffer_done(); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /// Handler trait for wlr-screencopy. |
| 280 | pub trait ScreencopyHandler { |
| 281 | /// Handle new screencopy request. |
| 282 | fn frame(&mut self, manager: &ZwlrScreencopyManagerV1, screencopy: Screencopy); |
| 283 | fn screencopy_state(&mut self) -> &mut ScreencopyManagerState; |
| 284 | } |
| 285 | |
| 286 | #[allow(missing_docs)] |
| 287 | #[macro_export] |
| 288 | macro_rules! delegate_screencopy { |
| 289 | ($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty) => { |
| 290 | smithay::reexports::wayland_server::delegate_global_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 291 | smithay::reexports::wayland_protocols_wlr::screencopy::v1::server::zwlr_screencopy_manager_v1::ZwlrScreencopyManagerV1: $crate::protocols::screencopy::ScreencopyManagerGlobalData |
| 292 | ] => $crate::protocols::screencopy::ScreencopyManagerState); |
| 293 | |
| 294 | smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 295 | smithay::reexports::wayland_protocols_wlr::screencopy::v1::server::zwlr_screencopy_manager_v1::ZwlrScreencopyManagerV1: () |
| 296 | ] => $crate::protocols::screencopy::ScreencopyManagerState); |
| 297 | |
| 298 | smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 299 | smithay::reexports::wayland_protocols_wlr::screencopy::v1::server::zwlr_screencopy_frame_v1::ZwlrScreencopyFrameV1: $crate::protocols::screencopy::ScreencopyFrameState |
| 300 | ] => $crate::protocols::screencopy::ScreencopyManagerState); |
| 301 | }; |
| 302 | } |
| 303 | |
| 304 | #[derive(Clone)] |
| 305 | pub struct ScreencopyFrameInfo { |
| 306 | output: Output, |
| 307 | buffer_size: Size<i32, Physical>, |
| 308 | region_loc: Point<i32, Physical>, |
| 309 | overlay_cursor: bool, |
| 310 | } |
| 311 | |
| 312 | pub enum ScreencopyFrameState { |
| 313 | Failed, |
| 314 | Pending { |
| 315 | manager: ZwlrScreencopyManagerV1, |
| 316 | info: ScreencopyFrameInfo, |
| 317 | copied: Arc<AtomicBool>, |
| 318 | }, |
| 319 | } |
| 320 | |
| 321 | impl<D> Dispatch<ZwlrScreencopyFrameV1, ScreencopyFrameState, D> for ScreencopyManagerState |
| 322 | where |
| 323 | D: Dispatch<ZwlrScreencopyFrameV1, ScreencopyFrameState>, |
| 324 | D: ScreencopyHandler, |
| 325 | D: 'static, |
| 326 | { |
| 327 | fn request( |
| 328 | state: &mut D, |
| 329 | _client: &Client, |
| 330 | frame: &ZwlrScreencopyFrameV1, |
| 331 | request: zwlr_screencopy_frame_v1::Request, |
| 332 | data: &ScreencopyFrameState, |
| 333 | _display: &DisplayHandle, |
| 334 | _data_init: &mut DataInit<'_, D>, |
| 335 | ) { |
| 336 | if matches!(request, zwlr_screencopy_frame_v1::Request::Destroy) { |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | let ScreencopyFrameState::Pending { |
| 341 | manager, |
| 342 | info, |
| 343 | copied, |
| 344 | } = data |
| 345 | else { |
| 346 | return; |
| 347 | }; |
| 348 | |
| 349 | if copied.load(Ordering::SeqCst) { |
| 350 | frame.post_error( |
| 351 | zwlr_screencopy_frame_v1::Error::AlreadyUsed, |
| 352 | "copy was already requested", |
| 353 | ); |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | let (buffer, with_damage) = match request { |
| 358 | zwlr_screencopy_frame_v1::Request::Copy { buffer } => (buffer, false), |
| 359 | zwlr_screencopy_frame_v1::Request::CopyWithDamage { buffer } => (buffer, true), |
| 360 | _ => unreachable!(), |
| 361 | }; |
| 362 | |
| 363 | let size = info.buffer_size; |
| 364 | |
| 365 | let buffer = if let Ok(dmabuf) = dmabuf::get_dmabuf(&buffer) { |
| 366 | if dmabuf.format().code == Fourcc::Xrgb8888 |
| 367 | && dmabuf.width() == size.w as u32 |
| 368 | && dmabuf.height() == size.h as u32 |
| 369 | { |
| 370 | ScreencopyBuffer::Dmabuf(dmabuf.clone()) |
| 371 | } else { |
| 372 | frame.post_error( |
| 373 | zwlr_screencopy_frame_v1::Error::InvalidBuffer, |
| 374 | "invalid dmabuf parameters", |
| 375 | ); |
| 376 | return; |
| 377 | } |
| 378 | } else if shm::with_buffer_contents(&buffer, |_, shm_len, buffer_data| { |
| 379 | buffer_data.format == Format::Xrgb8888 |
| 380 | && buffer_data.width == size.w |
| 381 | && buffer_data.height == size.h |
| 382 | && buffer_data.stride == size.w * 4 |
| 383 | && shm_len == buffer_data.stride as usize * buffer_data.height as usize |
| 384 | }) |
| 385 | .unwrap_or(false) |
| 386 | { |
| 387 | ScreencopyBuffer::Shm(buffer) |
| 388 | } else { |
| 389 | frame.post_error( |
| 390 | zwlr_screencopy_frame_v1::Error::InvalidBuffer, |
| 391 | "invalid buffer", |
| 392 | ); |
| 393 | return; |
| 394 | }; |
| 395 | |
| 396 | copied.store(true, Ordering::SeqCst); |
| 397 | |
| 398 | state.frame( |
| 399 | manager, |
| 400 | Screencopy { |
| 401 | buffer, |
| 402 | frame: frame.clone(), |
| 403 | info: info.clone(), |
| 404 | with_damage, |
| 405 | submitted: false, |
| 406 | }, |
| 407 | ); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | /// Screencopy buffer. |
| 412 | #[derive(Clone)] |
| 413 | pub enum ScreencopyBuffer { |
| 414 | Dmabuf(Dmabuf), |
| 415 | Shm(WlBuffer), |
| 416 | } |
| 417 | |
| 418 | /// Screencopy frame. |
| 419 | pub struct Screencopy { |
| 420 | info: ScreencopyFrameInfo, |
| 421 | frame: ZwlrScreencopyFrameV1, |
| 422 | buffer: ScreencopyBuffer, |
| 423 | with_damage: bool, |
| 424 | submitted: bool, |
| 425 | } |
| 426 | |
| 427 | impl Drop for Screencopy { |
| 428 | fn drop(&mut self) { |
| 429 | if !self.submitted { |
| 430 | self.frame.failed(); |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | impl Screencopy { |
| 436 | /// Get the target buffer to copy to. |
| 437 | pub fn buffer(&self) -> &ScreencopyBuffer { |
| 438 | &self.buffer |
| 439 | } |
| 440 | |
| 441 | pub fn region_loc(&self) -> Point<i32, Physical> { |
| 442 | self.info.region_loc |
| 443 | } |
| 444 | |
| 445 | pub fn buffer_size(&self) -> Size<i32, Physical> { |
| 446 | self.info.buffer_size |
| 447 | } |
| 448 | |
| 449 | pub fn output(&self) -> &Output { |
| 450 | &self.info.output |
| 451 | } |
| 452 | |
| 453 | pub fn overlay_cursor(&self) -> bool { |
| 454 | self.info.overlay_cursor |
| 455 | } |
| 456 | |
| 457 | pub fn with_damage(&self) -> bool { |
| 458 | self.with_damage |
| 459 | } |
| 460 | |
| 461 | pub fn damage(&self, damages: impl Iterator<Item = Rectangle<i32, smithay::utils::Buffer>>) { |
| 462 | for Rectangle { loc, size } in damages { |
| 463 | self.frame |
| 464 | .damage(loc.x as u32, loc.y as u32, size.w as u32, size.h as u32); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | /// Submit the copied content. |
| 469 | fn submit(mut self, y_invert: bool, timestamp: Duration) { |
| 470 | // Notify client that buffer is ordinary. |
| 471 | self.frame.flags(if y_invert { |
| 472 | Flags::YInvert |
| 473 | } else { |
| 474 | Flags::empty() |
| 475 | }); |
| 476 | |
| 477 | // Notify client about successful copy. |
| 478 | let tv_sec_hi = (timestamp.as_secs() >> 32) as u32; |
| 479 | let tv_sec_lo = (timestamp.as_secs() & 0xFFFFFFFF) as u32; |
| 480 | let tv_nsec = timestamp.subsec_nanos(); |
| 481 | self.frame.ready(tv_sec_hi, tv_sec_lo, tv_nsec); |
| 482 | |
| 483 | // Mark frame as submitted to ensure destructor isn't run. |
| 484 | self.submitted = true; |
| 485 | } |
| 486 | |
| 487 | pub fn submit_after_sync<T>( |
| 488 | self, |
| 489 | y_invert: bool, |
| 490 | sync_point: Option<SyncPoint>, |
| 491 | event_loop: &LoopHandle<'_, T>, |
| 492 | ) { |
| 493 | let timestamp = get_monotonic_time(); |
| 494 | match sync_point.and_then(|s| s.export()) { |
| 495 | None => self.submit(y_invert, timestamp), |
| 496 | Some(sync_fd) => { |
| 497 | let source = Generic::new(sync_fd, Interest::READ, Mode::OneShot); |
| 498 | let mut screencopy = Some(self); |
| 499 | event_loop |
| 500 | .insert_source(source, move |_, _, _| { |
| 501 | screencopy.take().unwrap().submit(y_invert, timestamp); |
| 502 | Ok(PostAction::Remove) |
| 503 | }) |
| 504 | .unwrap(); |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | } |