Add initial dmabuf feedback implementation
Browse files at this commitcc1c9d93254df120fc1041e121cb3b5edd00ffc9Parent: 03a9fd83 changed files +86 -6
- src/backend/tty.rs +22 -2
- src/handlers/compositor.rs +38 -2
- src/handlers/mod.rs +26 -2
src/backend/tty.rsmodified+22 -2
| @@ -12,7 +12,7 @@ use smithay::backend::drm::{DrmDevice, DrmDeviceFd, DrmEvent, DrmEventTime}; | |||
| 12 | use smithay::backend::egl::{EGLContext, EGLDisplay}; | 12 | use smithay::backend::egl::{EGLContext, EGLDisplay}; |
| 13 | use smithay::backend::libinput::{LibinputInputBackend, LibinputSessionInterface}; | 13 | use smithay::backend::libinput::{LibinputInputBackend, LibinputSessionInterface}; |
| 14 | use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; | 14 | use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; |
| 15 | -use smithay::backend::renderer::{Bind, DebugFlags, ImportEgl}; | 15 | +use smithay::backend::renderer::{Bind, DebugFlags, ImportDma, ImportEgl}; |
| 16 | use smithay::backend::session::libseat::LibSeatSession; | 16 | use smithay::backend::session::libseat::LibSeatSession; |
| 17 | use smithay::backend::session::{Event as SessionEvent, Session}; | 17 | use smithay::backend::session::{Event as SessionEvent, Session}; |
| 18 | use smithay::backend::udev::{self, UdevBackend, UdevEvent}; | 18 | use smithay::backend::udev::{self, UdevBackend, UdevEvent}; |
| @@ -27,10 +27,11 @@ use smithay::reexports::nix::fcntl::OFlag; | |||
| 27 | use smithay::reexports::nix::libc::dev_t; | 27 | use smithay::reexports::nix::libc::dev_t; |
| 28 | use smithay::reexports::wayland_protocols::wp::presentation_time::server::wp_presentation_feedback; | 28 | use smithay::reexports::wayland_protocols::wp::presentation_time::server::wp_presentation_feedback; |
| 29 | use smithay::utils::DeviceFd; | 29 | use smithay::utils::DeviceFd; |
| 30 | +use smithay::wayland::dmabuf::{DmabufFeedbackBuilder, DmabufGlobal, DmabufState}; | ||
| 30 | use smithay_drm_extras::drm_scanner::{DrmScanEvent, DrmScanner}; | 31 | use smithay_drm_extras::drm_scanner::{DrmScanEvent, DrmScanner}; |
| 31 | use smithay_drm_extras::edid::EdidInfo; | 32 | use smithay_drm_extras::edid::EdidInfo; |
| 32 | 33 | ||
| 33 | -use crate::niri::OutputRenderElements; | 34 | +use crate::niri::{OutputRenderElements, State}; |
| 34 | use crate::{LoopData, Niri}; | 35 | use crate::{LoopData, Niri}; |
| 35 | 36 | ||
| 36 | const BACKGROUND_COLOR: [f32; 4] = [0.1, 0.1, 0.1, 1.]; | 37 | const BACKGROUND_COLOR: [f32; 4] = [0.1, 0.1, 0.1, 1.]; |
| @@ -59,6 +60,8 @@ struct OutputDevice { | |||
| 59 | formats: HashSet<DrmFormat>, | 60 | formats: HashSet<DrmFormat>, |
| 60 | drm_scanner: DrmScanner, | 61 | drm_scanner: DrmScanner, |
| 61 | surfaces: HashMap<crtc::Handle, GbmDrmCompositor>, | 62 | surfaces: HashMap<crtc::Handle, GbmDrmCompositor>, |
| 63 | + dmabuf_state: DmabufState, | ||
| 64 | + dmabuf_global: DmabufGlobal, | ||
| 62 | } | 65 | } |
| 63 | 66 | ||
| 64 | #[derive(Debug, Clone, Copy)] | 67 | #[derive(Debug, Clone, Copy)] |
| @@ -331,6 +334,13 @@ impl Tty { | |||
| 331 | 334 | ||
| 332 | let formats = Bind::<Dmabuf>::supported_formats(&gles).unwrap_or_default(); | 335 | let formats = Bind::<Dmabuf>::supported_formats(&gles).unwrap_or_default(); |
| 333 | 336 | ||
| 337 | + let mut dmabuf_state = DmabufState::new(); | ||
| 338 | + let default_feedback = DmabufFeedbackBuilder::new(device_id, gles.dmabuf_formats()) | ||
| 339 | + .build() | ||
| 340 | + .unwrap(); | ||
| 341 | + let dmabuf_global = dmabuf_state | ||
| 342 | + .create_global_with_default_feedback::<State>(&niri.display_handle, &default_feedback); | ||
| 343 | + | ||
| 334 | self.output_device = Some(OutputDevice { | 344 | self.output_device = Some(OutputDevice { |
| 335 | id: device_id, | 345 | id: device_id, |
| 336 | token, | 346 | token, |
| @@ -340,6 +350,8 @@ impl Tty { | |||
| 340 | formats, | 350 | formats, |
| 341 | drm_scanner: DrmScanner::new(), | 351 | drm_scanner: DrmScanner::new(), |
| 342 | surfaces: HashMap::new(), | 352 | surfaces: HashMap::new(), |
| 353 | + dmabuf_state, | ||
| 354 | + dmabuf_global, | ||
| 343 | }); | 355 | }); |
| 344 | 356 | ||
| 345 | self.device_changed(device_id, niri); | 357 | self.device_changed(device_id, niri); |
| @@ -394,7 +406,11 @@ impl Tty { | |||
| 394 | } | 406 | } |
| 395 | 407 | ||
| 396 | let mut device = self.output_device.take().unwrap(); | 408 | let mut device = self.output_device.take().unwrap(); |
| 409 | + device | ||
| 410 | + .dmabuf_state | ||
| 411 | + .destroy_global::<State>(&niri.display_handle, device.dmabuf_global); | ||
| 397 | device.gles.unbind_wl_display(); | 412 | device.gles.unbind_wl_display(); |
| 413 | + | ||
| 398 | niri.event_loop.remove(device.token); | 414 | niri.event_loop.remove(device.token); |
| 399 | } | 415 | } |
| 400 | 416 | ||
| @@ -582,6 +598,10 @@ impl Tty { | |||
| 582 | } | 598 | } |
| 583 | } | 599 | } |
| 584 | } | 600 | } |
| 601 | + | ||
| 602 | + pub fn dmabuf_state(&mut self) -> &mut DmabufState { | ||
| 603 | + &mut self.output_device.as_mut().unwrap().dmabuf_state | ||
| 604 | + } | ||
| 585 | } | 605 | } |
| 586 | 606 | ||
| 587 | fn refresh_interval(mode: DrmMode) -> Duration { | 607 | fn refresh_interval(mode: DrmMode) -> Duration { |
src/handlers/compositor.rsmodified+38 -2
| @@ -2,13 +2,16 @@ use std::collections::hash_map::Entry; | |||
| 2 | 2 | ||
| 3 | use smithay::backend::renderer::utils::{on_commit_buffer_handler, with_renderer_surface_state}; | 3 | use smithay::backend::renderer::utils::{on_commit_buffer_handler, with_renderer_surface_state}; |
| 4 | use smithay::desktop::find_popup_root_surface; | 4 | use smithay::desktop::find_popup_root_surface; |
| 5 | +use smithay::reexports::calloop::Interest; | ||
| 5 | use smithay::reexports::wayland_server::protocol::wl_buffer; | 6 | use smithay::reexports::wayland_server::protocol::wl_buffer; |
| 6 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; | 7 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
| 7 | -use smithay::reexports::wayland_server::Client; | 8 | +use smithay::reexports::wayland_server::{Client, Resource}; |
| 8 | use smithay::wayland::buffer::BufferHandler; | 9 | use smithay::wayland::buffer::BufferHandler; |
| 9 | use smithay::wayland::compositor::{ | 10 | use smithay::wayland::compositor::{ |
| 10 | - get_parent, is_sync_subsurface, CompositorClientState, CompositorHandler, CompositorState, | 11 | + add_blocker, add_pre_commit_hook, get_parent, is_sync_subsurface, with_states, |
| 12 | + BufferAssignment, CompositorClientState, CompositorHandler, CompositorState, SurfaceAttributes, | ||
| 11 | }; | 13 | }; |
| 14 | +use smithay::wayland::dmabuf::get_dmabuf; | ||
| 12 | use smithay::wayland::shm::{ShmHandler, ShmState}; | 15 | use smithay::wayland::shm::{ShmHandler, ShmState}; |
| 13 | use smithay::{delegate_compositor, delegate_shm}; | 16 | use smithay::{delegate_compositor, delegate_shm}; |
| 14 | 17 | ||
| @@ -24,6 +27,39 @@ impl CompositorHandler for State { | |||
| 24 | &client.get_data::<ClientState>().unwrap().compositor_state | 27 | &client.get_data::<ClientState>().unwrap().compositor_state |
| 25 | } | 28 | } |
| 26 | 29 | ||
| 30 | + fn new_surface(&mut self, surface: &WlSurface) { | ||
| 31 | + add_pre_commit_hook::<Self, _>(surface, move |state, _dh, surface| { | ||
| 32 | + let maybe_dmabuf = with_states(surface, |surface_data| { | ||
| 33 | + surface_data | ||
| 34 | + .cached_state | ||
| 35 | + .pending::<SurfaceAttributes>() | ||
| 36 | + .buffer | ||
| 37 | + .as_ref() | ||
| 38 | + .and_then(|assignment| match assignment { | ||
| 39 | + BufferAssignment::NewBuffer(buffer) => get_dmabuf(buffer).ok(), | ||
| 40 | + _ => None, | ||
| 41 | + }) | ||
| 42 | + }); | ||
| 43 | + if let Some(dmabuf) = maybe_dmabuf { | ||
| 44 | + if let Ok((blocker, source)) = dmabuf.generate_blocker(Interest::READ) { | ||
| 45 | + let client = surface.client().unwrap(); | ||
| 46 | + let res = state | ||
| 47 | + .niri | ||
| 48 | + .event_loop | ||
| 49 | + .insert_source(source, move |_, _, data| { | ||
| 50 | + data.state | ||
| 51 | + .client_compositor_state(&client) | ||
| 52 | + .blocker_cleared(&mut data.state, &data.display.handle()); | ||
| 53 | + Ok(()) | ||
| 54 | + }); | ||
| 55 | + if res.is_ok() { | ||
| 56 | + add_blocker(surface, blocker); | ||
| 57 | + } | ||
| 58 | + } | ||
| 59 | + } | ||
| 60 | + }) | ||
| 61 | + } | ||
| 62 | + | ||
| 27 | fn commit(&mut self, surface: &WlSurface) { | 63 | fn commit(&mut self, surface: &WlSurface) { |
| 28 | let _span = tracy_client::span!("CompositorHandler::commit"); | 64 | let _span = tracy_client::span!("CompositorHandler::commit"); |
| 29 | 65 | ||
src/handlers/mod.rsmodified+26 -2
| @@ -2,6 +2,8 @@ mod compositor; | |||
| 2 | mod layer_shell; | 2 | mod layer_shell; |
| 3 | mod xdg_shell; | 3 | mod xdg_shell; |
| 4 | 4 | ||
| 5 | +use smithay::backend::allocator::dmabuf::Dmabuf; | ||
| 6 | +use smithay::backend::renderer::ImportDma; | ||
| 5 | use smithay::input::pointer::CursorImageStatus; | 7 | use smithay::input::pointer::CursorImageStatus; |
| 6 | use smithay::input::{Seat, SeatHandler, SeatState}; | 8 | use smithay::input::{Seat, SeatHandler, SeatState}; |
| 7 | use smithay::reexports::wayland_server::protocol::wl_data_source::WlDataSource; | 9 | use smithay::reexports::wayland_server::protocol::wl_data_source::WlDataSource; |
| @@ -11,9 +13,10 @@ use smithay::wayland::data_device::{ | |||
| 11 | set_data_device_focus, ClientDndGrabHandler, DataDeviceHandler, DataDeviceState, | 13 | set_data_device_focus, ClientDndGrabHandler, DataDeviceHandler, DataDeviceState, |
| 12 | ServerDndGrabHandler, | 14 | ServerDndGrabHandler, |
| 13 | }; | 15 | }; |
| 16 | +use smithay::wayland::dmabuf::{DmabufGlobal, DmabufHandler, DmabufState, ImportError}; | ||
| 14 | use smithay::{ | 17 | use smithay::{ |
| 15 | - delegate_data_device, delegate_output, delegate_pointer_gestures, delegate_presentation, | 18 | + delegate_data_device, delegate_dmabuf, delegate_output, delegate_pointer_gestures, |
| 16 | - delegate_seat, delegate_tablet_manager, | 19 | + delegate_presentation, delegate_seat, delegate_tablet_manager, |
| 17 | }; | 20 | }; |
| 18 | 21 | ||
| 19 | use crate::niri::State; | 22 | use crate::niri::State; |
| @@ -75,3 +78,24 @@ delegate_data_device!(State); | |||
| 75 | delegate_output!(State); | 78 | delegate_output!(State); |
| 76 | 79 | ||
| 77 | delegate_presentation!(State); | 80 | delegate_presentation!(State); |
| 81 | + | ||
| 82 | +impl DmabufHandler for State { | ||
| 83 | + fn dmabuf_state(&mut self) -> &mut DmabufState { | ||
| 84 | + self.backend.tty().dmabuf_state() | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + fn dmabuf_imported( | ||
| 88 | + &mut self, | ||
| 89 | + _global: &DmabufGlobal, | ||
| 90 | + dmabuf: Dmabuf, | ||
| 91 | + ) -> Result<(), ImportError> { | ||
| 92 | + match self.backend.renderer().import_dmabuf(&dmabuf, None) { | ||
| 93 | + Ok(_texture) => Ok(()), | ||
| 94 | + Err(err) => { | ||
| 95 | + warn!("error importing dmabuf: {err:?}"); | ||
| 96 | + Err(ImportError::Failed) | ||
| 97 | + } | ||
| 98 | + } | ||
| 99 | + } | ||
| 100 | +} | ||
| 101 | +delegate_dmabuf!(State); | ||