Implement multi-GPU support
Browse files at this commit5cd31e5730160f9162502d2388dc00400c7a87aeParent: de3fc2d6 changed files +609 -200
- src/backend/mod.rs +9 -1
- src/backend/tty.rs +435 -192
- src/handlers/compositor.rs +1 -0
- src/niri.rs +96 -7
- src/render_helpers.rs +39 -0
- src/screenshot_ui.rs +29 -0
src/backend/mod.rsmodified+9 -1
| @@ -5,6 +5,7 @@ use std::time::Duration; | |||
| 5 | use smithay::backend::allocator::dmabuf::Dmabuf; | 5 | use smithay::backend::allocator::dmabuf::Dmabuf; |
| 6 | use smithay::backend::renderer::gles::GlesRenderer; | 6 | use smithay::backend::renderer::gles::GlesRenderer; |
| 7 | use smithay::output::Output; | 7 | use smithay::output::Output; |
| 8 | +use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; | ||
| 8 | 9 | ||
| 9 | use crate::input::CompositorMod; | 10 | use crate::input::CompositorMod; |
| 10 | use crate::Niri; | 11 | use crate::Niri; |
| @@ -102,6 +103,13 @@ impl Backend { | |||
| 102 | } | 103 | } |
| 103 | } | 104 | } |
| 104 | 105 | ||
| 106 | + pub fn early_import(&mut self, surface: &WlSurface) { | ||
| 107 | + match self { | ||
| 108 | + Backend::Tty(tty) => tty.early_import(surface), | ||
| 109 | + Backend::Winit(_) => (), | ||
| 110 | + } | ||
| 111 | + } | ||
| 112 | + | ||
| 105 | #[cfg_attr(not(feature = "dbus"), allow(unused))] | 113 | #[cfg_attr(not(feature = "dbus"), allow(unused))] |
| 106 | pub fn connectors(&self) -> Arc<Mutex<HashMap<String, Output>>> { | 114 | pub fn connectors(&self) -> Arc<Mutex<HashMap<String, Output>>> { |
| 107 | match self { | 115 | match self { |
| @@ -116,7 +124,7 @@ impl Backend { | |||
| 116 | ) -> Option<smithay::backend::allocator::gbm::GbmDevice<smithay::backend::drm::DrmDeviceFd>> | 124 | ) -> Option<smithay::backend::allocator::gbm::GbmDevice<smithay::backend::drm::DrmDeviceFd>> |
| 117 | { | 125 | { |
| 118 | match self { | 126 | match self { |
| 119 | - Backend::Tty(tty) => tty.gbm_device(), | 127 | + Backend::Tty(tty) => tty.primary_gbm_device(), |
| 120 | Backend::Winit(_) => None, | 128 | Backend::Winit(_) => None, |
| 121 | } | 129 | } |
| 122 | } | 130 | } |
src/backend/tty.rsmodified+435 -192
| @@ -1,23 +1,27 @@ | |||
| 1 | use std::cell::RefCell; | 1 | use std::cell::RefCell; |
| 2 | use std::collections::{HashMap, HashSet}; | 2 | use std::collections::{HashMap, HashSet}; |
| 3 | -use std::mem; | 3 | +use std::path::Path; |
| 4 | -use std::path::{Path, PathBuf}; | ||
| 5 | use std::rc::Rc; | 4 | use std::rc::Rc; |
| 6 | use std::sync::{Arc, Mutex}; | 5 | use std::sync::{Arc, Mutex}; |
| 7 | use std::time::Duration; | 6 | use std::time::Duration; |
| 7 | +use std::{io, mem}; | ||
| 8 | 8 | ||
| 9 | use anyhow::{anyhow, Context}; | 9 | use anyhow::{anyhow, Context}; |
| 10 | use libc::dev_t; | 10 | use libc::dev_t; |
| 11 | -use smithay::backend::allocator::dmabuf::Dmabuf; | 11 | +use smithay::backend::allocator::dmabuf::{Dmabuf, DmabufAllocator}; |
| 12 | use smithay::backend::allocator::gbm::{GbmAllocator, GbmBufferFlags, GbmDevice}; | 12 | use smithay::backend::allocator::gbm::{GbmAllocator, GbmBufferFlags, GbmDevice}; |
| 13 | -use smithay::backend::allocator::Fourcc; | 13 | +use smithay::backend::allocator::{Format, Fourcc}; |
| 14 | use smithay::backend::drm::compositor::{DrmCompositor, PrimaryPlaneElement}; | 14 | use smithay::backend::drm::compositor::{DrmCompositor, PrimaryPlaneElement}; |
| 15 | -use smithay::backend::drm::{DrmDevice, DrmDeviceFd, DrmEvent, DrmEventMetadata, DrmEventTime}; | 15 | +use smithay::backend::drm::{ |
| 16 | + DrmDevice, DrmDeviceFd, DrmEvent, DrmEventMetadata, DrmEventTime, DrmNode, NodeType, | ||
| 17 | +}; | ||
| 16 | use smithay::backend::egl::context::ContextPriority; | 18 | use smithay::backend::egl::context::ContextPriority; |
| 17 | -use smithay::backend::egl::{EGLContext, EGLDisplay}; | 19 | +use smithay::backend::egl::{EGLContext, EGLDevice, EGLDisplay}; |
| 18 | use smithay::backend::libinput::{LibinputInputBackend, LibinputSessionInterface}; | 20 | use smithay::backend::libinput::{LibinputInputBackend, LibinputSessionInterface}; |
| 19 | use smithay::backend::renderer::gles::{Capability, GlesRenderer, GlesTexture}; | 21 | use smithay::backend::renderer::gles::{Capability, GlesRenderer, GlesTexture}; |
| 20 | -use smithay::backend::renderer::{DebugFlags, ImportDma, ImportEgl}; | 22 | +use smithay::backend::renderer::multigpu::gbm::GbmGlesBackend; |
| 23 | +use smithay::backend::renderer::multigpu::{GpuManager, MultiFrame, MultiRenderer}; | ||
| 24 | +use smithay::backend::renderer::{DebugFlags, ImportDma, ImportEgl, Renderer}; | ||
| 21 | use smithay::backend::session::libseat::LibSeatSession; | 25 | use smithay::backend::session::libseat::LibSeatSession; |
| 22 | use smithay::backend::session::{Event as SessionEvent, Session}; | 26 | use smithay::backend::session::{Event as SessionEvent, Session}; |
| 23 | use smithay::backend::udev::{self, UdevBackend, UdevEvent}; | 27 | use smithay::backend::udev::{self, UdevBackend, UdevEvent}; |
| @@ -31,6 +35,7 @@ use smithay::reexports::drm::control::{ | |||
| 31 | use smithay::reexports::input::Libinput; | 35 | use smithay::reexports::input::Libinput; |
| 32 | use smithay::reexports::rustix::fs::OFlags; | 36 | use smithay::reexports::rustix::fs::OFlags; |
| 33 | use smithay::reexports::wayland_protocols; | 37 | use smithay::reexports::wayland_protocols; |
| 38 | +use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; | ||
| 34 | use smithay::utils::DeviceFd; | 39 | use smithay::utils::DeviceFd; |
| 35 | use smithay::wayland::dmabuf::{DmabufFeedback, DmabufFeedbackBuilder, DmabufGlobal}; | 40 | use smithay::wayland::dmabuf::{DmabufFeedback, DmabufFeedbackBuilder, DmabufGlobal}; |
| 36 | use smithay_drm_extras::drm_scanner::{DrmScanEvent, DrmScanner}; | 41 | use smithay_drm_extras::drm_scanner::{DrmScanEvent, DrmScanner}; |
| @@ -41,6 +46,7 @@ use wayland_protocols::wp::presentation_time::server::wp_presentation_feedback; | |||
| 41 | use super::RenderResult; | 46 | use super::RenderResult; |
| 42 | use crate::config::Config; | 47 | use crate::config::Config; |
| 43 | use crate::niri::{RedrawState, State}; | 48 | use crate::niri::{RedrawState, State}; |
| 49 | +use crate::render_helpers::AsGlesRenderer; | ||
| 44 | use crate::utils::get_monotonic_time; | 50 | use crate::utils::get_monotonic_time; |
| 45 | use crate::Niri; | 51 | use crate::Niri; |
| 46 | 52 | ||
| @@ -51,14 +57,42 @@ pub struct Tty { | |||
| 51 | session: LibSeatSession, | 57 | session: LibSeatSession, |
| 52 | udev_dispatcher: Dispatcher<'static, UdevBackend, State>, | 58 | udev_dispatcher: Dispatcher<'static, UdevBackend, State>, |
| 53 | libinput: Libinput, | 59 | libinput: Libinput, |
| 54 | - primary_gpu_path: PathBuf, | 60 | + gpu_manager: GpuManager<GbmGlesBackend<GlesRenderer>>, |
| 61 | + // DRM node corresponding to the primary GPU. May or may not be the same as | ||
| 62 | + // primary_render_node. | ||
| 63 | + primary_node: DrmNode, | ||
| 64 | + // DRM render node corresponding to the primary GPU. | ||
| 65 | + primary_render_node: DrmNode, | ||
| 66 | + // Devices indexed by DRM node (not necessarily the render node). | ||
| 67 | + devices: HashMap<DrmNode, OutputDevice>, | ||
| 55 | // The dma-buf global corresponds to the output device (the primary GPU). It is only `Some()` | 68 | // The dma-buf global corresponds to the output device (the primary GPU). It is only `Some()` |
| 56 | // if we have a device corresponding to the primary GPU. | 69 | // if we have a device corresponding to the primary GPU. |
| 57 | dmabuf_global: Option<DmabufGlobal>, | 70 | dmabuf_global: Option<DmabufGlobal>, |
| 58 | - output_device: Option<OutputDevice>, | 71 | + // The allocator for the primary GPU. It is only `Some()` if we have a device corresponding to |
| 72 | + // the primary GPU. | ||
| 73 | + primary_allocator: Option<DmabufAllocator<GbmAllocator<DrmDeviceFd>>>, | ||
| 59 | connectors: Arc<Mutex<HashMap<String, Output>>>, | 74 | connectors: Arc<Mutex<HashMap<String, Output>>>, |
| 60 | } | 75 | } |
| 61 | 76 | ||
| 77 | +pub type TtyRenderer<'render, 'alloc> = MultiRenderer< | ||
| 78 | + 'render, | ||
| 79 | + 'render, | ||
| 80 | + 'alloc, | ||
| 81 | + GbmGlesBackend<GlesRenderer>, | ||
| 82 | + GbmGlesBackend<GlesRenderer>, | ||
| 83 | +>; | ||
| 84 | + | ||
| 85 | +pub type TtyFrame<'render, 'alloc, 'frame> = MultiFrame< | ||
| 86 | + 'render, | ||
| 87 | + 'render, | ||
| 88 | + 'alloc, | ||
| 89 | + 'frame, | ||
| 90 | + GbmGlesBackend<GlesRenderer>, | ||
| 91 | + GbmGlesBackend<GlesRenderer>, | ||
| 92 | +>; | ||
| 93 | + | ||
| 94 | +pub type TtyRendererError<'render, 'alloc> = <TtyRenderer<'render, 'alloc> as Renderer>::Error; | ||
| 95 | + | ||
| 62 | type GbmDrmCompositor = DrmCompositor< | 96 | type GbmDrmCompositor = DrmCompositor< |
| 63 | GbmAllocator<DrmDeviceFd>, | 97 | GbmAllocator<DrmDeviceFd>, |
| 64 | GbmDevice<DrmDeviceFd>, | 98 | GbmDevice<DrmDeviceFd>, |
| @@ -67,9 +101,8 @@ type GbmDrmCompositor = DrmCompositor< | |||
| 67 | >; | 101 | >; |
| 68 | 102 | ||
| 69 | struct OutputDevice { | 103 | struct OutputDevice { |
| 70 | - id: dev_t, | ||
| 71 | token: RegistrationToken, | 104 | token: RegistrationToken, |
| 72 | - gles: GlesRenderer, | 105 | + render_node: DrmNode, |
| 73 | drm_scanner: DrmScanner, | 106 | drm_scanner: DrmScanner, |
| 74 | surfaces: HashMap<crtc::Handle, Surface>, | 107 | surfaces: HashMap<crtc::Handle, Surface>, |
| 75 | // SAFETY: drop after all the objects used with them are dropped. | 108 | // SAFETY: drop after all the objects used with them are dropped. |
| @@ -80,14 +113,14 @@ struct OutputDevice { | |||
| 80 | 113 | ||
| 81 | #[derive(Debug, Clone, Copy)] | 114 | #[derive(Debug, Clone, Copy)] |
| 82 | struct TtyOutputState { | 115 | struct TtyOutputState { |
| 83 | - device_id: dev_t, | 116 | + node: DrmNode, |
| 84 | crtc: crtc::Handle, | 117 | crtc: crtc::Handle, |
| 85 | } | 118 | } |
| 86 | 119 | ||
| 87 | struct Surface { | 120 | struct Surface { |
| 88 | name: String, | 121 | name: String, |
| 89 | compositor: GbmDrmCompositor, | 122 | compositor: GbmDrmCompositor, |
| 90 | - dmabuf_feedback: DmabufFeedback, | 123 | + dmabuf_feedback: Option<SurfaceDmabufFeedback>, |
| 91 | /// Tracy frame that goes from vblank to vblank. | 124 | /// Tracy frame that goes from vblank to vblank. |
| 92 | vblank_frame: Option<tracy_client::Frame>, | 125 | vblank_frame: Option<tracy_client::Frame>, |
| 93 | /// Frame name for the VBlank frame. | 126 | /// Frame name for the VBlank frame. |
| @@ -99,6 +132,11 @@ struct Surface { | |||
| 99 | sequence_delta_plot_name: tracy_client::PlotName, | 132 | sequence_delta_plot_name: tracy_client::PlotName, |
| 100 | } | 133 | } |
| 101 | 134 | ||
| 135 | +pub struct SurfaceDmabufFeedback { | ||
| 136 | + pub render: DmabufFeedback, | ||
| 137 | + pub scanout: DmabufFeedback, | ||
| 138 | +} | ||
| 139 | + | ||
| 102 | impl Tty { | 140 | impl Tty { |
| 103 | pub fn new(config: Rc<RefCell<Config>>, event_loop: LoopHandle<'static, State>) -> Self { | 141 | pub fn new(config: Rc<RefCell<Config>>, event_loop: LoopHandle<'static, State>) -> Self { |
| 104 | let (session, notifier) = LibSeatSession::new().unwrap(); | 142 | let (session, notifier) = LibSeatSession::new().unwrap(); |
| @@ -129,16 +167,46 @@ impl Tty { | |||
| 129 | }) | 167 | }) |
| 130 | .unwrap(); | 168 | .unwrap(); |
| 131 | 169 | ||
| 170 | + let config_ = config.clone(); | ||
| 171 | + let create_renderer = move |display: &EGLDisplay| { | ||
| 172 | + let color_transforms = config_ | ||
| 173 | + .borrow() | ||
| 174 | + .debug | ||
| 175 | + .enable_color_transformations_capability; | ||
| 176 | + | ||
| 177 | + let egl_context = EGLContext::new_with_priority(display, ContextPriority::High)?; | ||
| 178 | + let gles = if color_transforms { | ||
| 179 | + unsafe { GlesRenderer::new(egl_context)? } | ||
| 180 | + } else { | ||
| 181 | + let capabilities = unsafe { GlesRenderer::supported_capabilities(&egl_context) }? | ||
| 182 | + .into_iter() | ||
| 183 | + .filter(|c| *c != Capability::ColorTransformations); | ||
| 184 | + unsafe { GlesRenderer::with_capabilities(egl_context, capabilities)? } | ||
| 185 | + }; | ||
| 186 | + Ok(gles) | ||
| 187 | + }; | ||
| 188 | + let api = GbmGlesBackend::with_factory(Box::new(create_renderer)); | ||
| 189 | + let gpu_manager = GpuManager::new(api).unwrap(); | ||
| 190 | + | ||
| 132 | let primary_gpu_path = udev::primary_gpu(&seat_name).unwrap().unwrap(); | 191 | let primary_gpu_path = udev::primary_gpu(&seat_name).unwrap().unwrap(); |
| 192 | + let primary_node = DrmNode::from_path(primary_gpu_path).unwrap(); | ||
| 193 | + let primary_render_node = primary_node | ||
| 194 | + .node_with_type(NodeType::Render) | ||
| 195 | + .unwrap() | ||
| 196 | + .unwrap(); | ||
| 197 | + info!("using {} as the primary GPU", primary_render_node); | ||
| 133 | 198 | ||
| 134 | Self { | 199 | Self { |
| 135 | config, | 200 | config, |
| 136 | session, | 201 | session, |
| 137 | udev_dispatcher, | 202 | udev_dispatcher, |
| 138 | libinput, | 203 | libinput, |
| 139 | - primary_gpu_path, | 204 | + gpu_manager, |
| 205 | + primary_node, | ||
| 206 | + primary_render_node, | ||
| 207 | + devices: HashMap::new(), | ||
| 140 | dmabuf_global: None, | 208 | dmabuf_global: None, |
| 141 | - output_device: None, | 209 | + primary_allocator: None, |
| 142 | connectors: Arc::new(Mutex::new(HashMap::new())), | 210 | connectors: Arc::new(Mutex::new(HashMap::new())), |
| 143 | } | 211 | } |
| 144 | } | 212 | } |
| @@ -193,8 +261,8 @@ impl Tty { | |||
| 193 | 261 | ||
| 194 | self.libinput.suspend(); | 262 | self.libinput.suspend(); |
| 195 | 263 | ||
| 196 | - if let Some(output_device) = &self.output_device { | 264 | + for device in self.devices.values() { |
| 197 | - output_device.drm.pause(); | 265 | + device.drm.pause(); |
| 198 | } | 266 | } |
| 199 | } | 267 | } |
| 200 | SessionEvent::ActivateSession => { | 268 | SessionEvent::ActivateSession => { |
| @@ -204,69 +272,85 @@ impl Tty { | |||
| 204 | error!("error resuming libinput"); | 272 | error!("error resuming libinput"); |
| 205 | } | 273 | } |
| 206 | 274 | ||
| 207 | - if let Some(output_device) = &mut self.output_device { | 275 | + let mut device_list = self |
| 208 | - // We had an output device, check if it's been removed. | 276 | + .udev_dispatcher |
| 209 | - let output_device_id = output_device.id; | 277 | + .as_source_ref() |
| 210 | - if !self | 278 | + .device_list() |
| 211 | - .udev_dispatcher | 279 | + .map(|(device_id, path)| (device_id, path.to_owned())) |
| 212 | - .as_source_ref() | 280 | + .collect::<HashMap<_, _>>(); |
| 213 | - .device_list() | 281 | + |
| 214 | - .any(|(device_id, _)| device_id == output_device_id) | 282 | + let removed_devices = self |
| 215 | - { | 283 | + .devices |
| 216 | - // The output device, if we had any, has been removed. | 284 | + .keys() |
| 217 | - self.device_removed(output_device_id, niri); | 285 | + .filter(|node| !device_list.contains_key(&node.dev_id())) |
| 218 | - } else { | 286 | + .copied() |
| 219 | - // It hasn't been removed, update its state as usual. | 287 | + .collect::<Vec<_>>(); |
| 220 | - output_device.drm.activate(); | 288 | + |
| 221 | - | 289 | + let remained_devices = self |
| 222 | - // HACK: force reset the connectors to make resuming work across | 290 | + .devices |
| 223 | - // sleep. | 291 | + .keys() |
| 224 | - let output_device = self.output_device.as_mut().unwrap(); | 292 | + .filter(|node| device_list.contains_key(&node.dev_id())) |
| 225 | - let crtcs: Vec<_> = output_device | 293 | + .copied() |
| 226 | - .drm_scanner | 294 | + .collect::<Vec<_>>(); |
| 227 | - .crtcs() | 295 | + |
| 228 | - .map(|(conn, crtc)| (conn.clone(), crtc)) | 296 | + // Remove removed devices. |
| 229 | - .collect(); | 297 | + for node in removed_devices { |
| 230 | - for (conn, crtc) in crtcs { | 298 | + device_list.remove(&node.dev_id()); |
| 231 | - self.connector_disconnected(niri, conn, crtc); | 299 | + self.device_removed(node.dev_id(), niri); |
| 232 | - } | 300 | + } |
| 233 | 301 | ||
| 234 | - let output_device = self.output_device.as_mut().unwrap(); | 302 | + // Update remained devices. |
| 235 | - let _ = output_device | 303 | + for node in remained_devices { |
| 236 | - .drm_scanner | 304 | + device_list.remove(&node.dev_id()); |
| 237 | - .scan_connectors(&output_device.drm); | 305 | + |
| 238 | - let crtcs: Vec<_> = output_device | 306 | + // It hasn't been removed, update its state as usual. |
| 239 | - .drm_scanner | 307 | + let device = &self.devices[&node]; |
| 240 | - .crtcs() | 308 | + device.drm.activate(); |
| 241 | - .map(|(conn, crtc)| (conn.clone(), crtc)) | 309 | + |
| 242 | - .collect(); | 310 | + // HACK: force reset the connectors to make resuming work across sleep. |
| 243 | - for (conn, crtc) in crtcs { | 311 | + let device = &self.devices[&node]; |
| 244 | - if let Err(err) = self.connector_connected(niri, conn, crtc) { | 312 | + let crtcs: Vec<_> = device |
| 245 | - warn!("error connecting connector: {err:?}"); | 313 | + .drm_scanner |
| 246 | - } | 314 | + .crtcs() |
| 315 | + .map(|(conn, crtc)| (conn.clone(), crtc)) | ||
| 316 | + .collect(); | ||
| 317 | + for (conn, crtc) in crtcs { | ||
| 318 | + self.connector_disconnected(niri, node, conn, crtc); | ||
| 319 | + } | ||
| 320 | + | ||
| 321 | + let device = self.devices.get_mut(&node).unwrap(); | ||
| 322 | + let _ = device.drm_scanner.scan_connectors(&device.drm); | ||
| 323 | + let crtcs: Vec<_> = device | ||
| 324 | + .drm_scanner | ||
| 325 | + .crtcs() | ||
| 326 | + .map(|(conn, crtc)| (conn.clone(), crtc)) | ||
| 327 | + .collect(); | ||
| 328 | + for (conn, crtc) in crtcs { | ||
| 329 | + if let Err(err) = self.connector_connected(niri, node, conn, crtc) { | ||
| 330 | + warn!("error connecting connector: {err:?}"); | ||
| 247 | } | 331 | } |
| 332 | + } | ||
| 248 | 333 | ||
| 249 | - // // Refresh the connectors. | 334 | + // // Refresh the connectors. |
| 250 | - // self.device_changed(output_device_id, niri); | 335 | + // self.device_changed(node.dev_id(), niri); |
| 251 | 336 | ||
| 252 | - // // Refresh the state on unchanged connectors. | 337 | + // // Refresh the state on unchanged connectors. |
| 253 | - // let output_device = self.output_device.as_mut().unwrap(); | 338 | + // let device = self.devices.get_mut(&node).unwrap(); |
| 254 | - // for drm_compositor in output_device.surfaces.values_mut() { | 339 | + // for surface in device.surfaces.values_mut() { |
| 255 | - // if let Err(err) = drm_compositor.surface().reset_state() { | 340 | + // let compositor = &mut surface.compositor; |
| 256 | - // warn!("error resetting DRM surface state: {err}"); | 341 | + // if let Err(err) = compositor.surface().reset_state() { |
| 257 | - // } | 342 | + // warn!("error resetting DRM surface state: {err}"); |
| 258 | - // drm_compositor.reset_buffers(); | 343 | + // } |
| 259 | - // } | 344 | + // compositor.reset_buffers(); |
| 345 | + // } | ||
| 260 | 346 | ||
| 261 | - // niri.queue_redraw_all(); | 347 | + // niri.queue_redraw_all(); |
| 262 | - } | 348 | + } |
| 263 | - } else { | 349 | + |
| 264 | - // We didn't have an output device, check if it's been added. | 350 | + // Add new devices. |
| 265 | - let udev_dispatcher = self.udev_dispatcher.clone(); | 351 | + for (device_id, path) in device_list.into_iter() { |
| 266 | - for (device_id, path) in udev_dispatcher.as_source_ref().device_list() { | 352 | + if let Err(err) = self.device_added(device_id, &path, niri) { |
| 267 | - if let Err(err) = self.device_added(device_id, path, niri) { | 353 | + warn!("error adding device: {err:?}"); |
| 268 | - warn!("error adding device: {err:?}"); | ||
| 269 | - } | ||
| 270 | } | 354 | } |
| 271 | } | 355 | } |
| 272 | } | 356 | } |
| @@ -279,13 +363,9 @@ impl Tty { | |||
| 279 | path: &Path, | 363 | path: &Path, |
| 280 | niri: &mut Niri, | 364 | niri: &mut Niri, |
| 281 | ) -> anyhow::Result<()> { | 365 | ) -> anyhow::Result<()> { |
| 282 | - if path != self.primary_gpu_path { | 366 | + debug!("device added: {device_id} {path:?}"); |
| 283 | - debug!("skipping non-primary device {path:?}"); | ||
| 284 | - return Ok(()); | ||
| 285 | - } | ||
| 286 | 367 | ||
| 287 | - debug!("adding device {path:?}"); | 368 | + let node = DrmNode::from_dev_id(device_id)?; |
| 288 | - assert!(self.output_device.is_none()); | ||
| 289 | 369 | ||
| 290 | let open_flags = OFlags::RDWR | OFlags::CLOEXEC | OFlags::NOCTTY | OFlags::NONBLOCK; | 370 | let open_flags = OFlags::RDWR | OFlags::CLOEXEC | OFlags::NOCTTY | OFlags::NONBLOCK; |
| 291 | let fd = self.session.open(path, open_flags)?; | 371 | let fd = self.session.open(path, open_flags)?; |
| @@ -295,24 +375,70 @@ impl Tty { | |||
| 295 | let gbm = GbmDevice::new(device_fd)?; | 375 | let gbm = GbmDevice::new(device_fd)?; |
| 296 | 376 | ||
| 297 | let display = EGLDisplay::new(gbm.clone())?; | 377 | let display = EGLDisplay::new(gbm.clone())?; |
| 298 | - let egl_context = EGLContext::new_with_priority(&display, ContextPriority::High)?; | 378 | + let egl_device = EGLDevice::device_for_display(&display)?; |
| 299 | - | 379 | + |
| 300 | - // ColorTransformations is disabled by default as it makes rendering slightly slower. | 380 | + // HACK: There's an issue in Smithay where the display created by GpuManager will be the |
| 301 | - let mut gles = if self | 381 | + // same as the one we just created here, so when ours is dropped at the end of the scope, |
| 302 | - .config | 382 | + // it will also close the long-lived display in GpuManager. Thus, we need to drop ours |
| 303 | - .borrow() | 383 | + // beforehand. |
| 304 | - .debug | 384 | + drop(display); |
| 305 | - .enable_color_transformations_capability | 385 | + |
| 306 | - { | 386 | + let render_node = egl_device |
| 307 | - unsafe { GlesRenderer::new(egl_context)? } | 387 | + .try_get_render_node()? |
| 308 | - } else { | 388 | + .context("no render node")?; |
| 309 | - let capabilities = unsafe { GlesRenderer::supported_capabilities(&egl_context) }? | 389 | + self.gpu_manager |
| 310 | - .into_iter() | 390 | + .as_mut() |
| 311 | - .filter(|c| *c != Capability::ColorTransformations); | 391 | + .add_node(render_node, gbm.clone()) |
| 312 | - unsafe { GlesRenderer::with_capabilities(egl_context, capabilities)? } | 392 | + .context("error adding render node to GPU manager")?; |
| 313 | - }; | 393 | + |
| 314 | - | 394 | + if node == self.primary_node { |
| 315 | - gles.bind_wl_display(&niri.display_handle)?; | 395 | + debug!("this is the primary node"); |
| 396 | + | ||
| 397 | + let mut renderer = self | ||
| 398 | + .gpu_manager | ||
| 399 | + .single_renderer(&render_node) | ||
| 400 | + .context("error creating renderer")?; | ||
| 401 | + | ||
| 402 | + renderer.bind_wl_display(&niri.display_handle)?; | ||
| 403 | + | ||
| 404 | + // Create the dmabuf global. | ||
| 405 | + let primary_formats = renderer.dmabuf_formats().collect::<HashSet<_>>(); | ||
| 406 | + let default_feedback = | ||
| 407 | + DmabufFeedbackBuilder::new(render_node.dev_id(), primary_formats.clone()) | ||
| 408 | + .build() | ||
| 409 | + .context("error building default dmabuf feedback")?; | ||
| 410 | + let dmabuf_global = niri | ||
| 411 | + .dmabuf_state | ||
| 412 | + .create_global_with_default_feedback::<State>( | ||
| 413 | + &niri.display_handle, | ||
| 414 | + &default_feedback, | ||
| 415 | + ); | ||
| 416 | + assert!(self.dmabuf_global.replace(dmabuf_global).is_none()); | ||
| 417 | + | ||
| 418 | + // Create the primary allocator. | ||
| 419 | + let primary_allocator = | ||
| 420 | + DmabufAllocator(GbmAllocator::new(gbm.clone(), GbmBufferFlags::RENDERING)); | ||
| 421 | + assert!(self.primary_allocator.replace(primary_allocator).is_none()); | ||
| 422 | + | ||
| 423 | + // Update the dmabuf feedbacks for all surfaces. | ||
| 424 | + for device in self.devices.values_mut() { | ||
| 425 | + for surface in device.surfaces.values_mut() { | ||
| 426 | + match surface_dmabuf_feedback( | ||
| 427 | + &surface.compositor, | ||
| 428 | + primary_formats.clone(), | ||
| 429 | + self.primary_render_node, | ||
| 430 | + device.render_node, | ||
| 431 | + ) { | ||
| 432 | + Ok(feedback) => { | ||
| 433 | + surface.dmabuf_feedback = Some(feedback); | ||
| 434 | + } | ||
| 435 | + Err(err) => { | ||
| 436 | + warn!("error building dmabuf feedback: {err:?}"); | ||
| 437 | + } | ||
| 438 | + } | ||
| 439 | + } | ||
| 440 | + } | ||
| 441 | + } | ||
| 316 | 442 | ||
| 317 | let token = niri | 443 | let token = niri |
| 318 | .event_loop | 444 | .event_loop |
| @@ -321,30 +447,22 @@ impl Tty { | |||
| 321 | match event { | 447 | match event { |
| 322 | DrmEvent::VBlank(crtc) => { | 448 | DrmEvent::VBlank(crtc) => { |
| 323 | let meta = meta.expect("VBlank events must have metadata"); | 449 | let meta = meta.expect("VBlank events must have metadata"); |
| 324 | - tty.on_vblank(&mut state.niri, crtc, meta); | 450 | + tty.on_vblank(&mut state.niri, node, crtc, meta); |
| 325 | } | 451 | } |
| 326 | DrmEvent::Error(error) => error!("DRM error: {error}"), | 452 | DrmEvent::Error(error) => error!("DRM error: {error}"), |
| 327 | }; | 453 | }; |
| 328 | }) | 454 | }) |
| 329 | .unwrap(); | 455 | .unwrap(); |
| 330 | 456 | ||
| 331 | - let default_feedback = DmabufFeedbackBuilder::new(device_id, gles.dmabuf_formats()) | 457 | + let device = OutputDevice { |
| 332 | - .build() | ||
| 333 | - .context("error building default dmabuf feedback")?; | ||
| 334 | - let dmabuf_global = niri | ||
| 335 | - .dmabuf_state | ||
| 336 | - .create_global_with_default_feedback::<State>(&niri.display_handle, &default_feedback); | ||
| 337 | - | ||
| 338 | - self.output_device = Some(OutputDevice { | ||
| 339 | - id: device_id, | ||
| 340 | token, | 458 | token, |
| 459 | + render_node, | ||
| 341 | drm, | 460 | drm, |
| 342 | gbm, | 461 | gbm, |
| 343 | - gles, | ||
| 344 | drm_scanner: DrmScanner::new(), | 462 | drm_scanner: DrmScanner::new(), |
| 345 | surfaces: HashMap::new(), | 463 | surfaces: HashMap::new(), |
| 346 | - }); | 464 | + }; |
| 347 | - self.dmabuf_global = Some(dmabuf_global); | 465 | + assert!(self.devices.insert(node, device).is_none()); |
| 348 | 466 | ||
| 349 | self.device_changed(device_id, niri); | 467 | self.device_changed(device_id, niri); |
| 350 | 468 | ||
| @@ -352,13 +470,17 @@ impl Tty { | |||
| 352 | } | 470 | } |
| 353 | 471 | ||
| 354 | fn device_changed(&mut self, device_id: dev_t, niri: &mut Niri) { | 472 | fn device_changed(&mut self, device_id: dev_t, niri: &mut Niri) { |
| 355 | - let Some(device) = &mut self.output_device else { | 473 | + debug!("device changed: {device_id}"); |
| 474 | + | ||
| 475 | + let Ok(node) = DrmNode::from_dev_id(device_id) else { | ||
| 476 | + warn!("error creating DrmNode"); | ||
| 356 | return; | 477 | return; |
| 357 | }; | 478 | }; |
| 358 | - if device.id != device_id { | 479 | + |
| 480 | + let Some(device) = self.devices.get_mut(&node) else { | ||
| 481 | + warn!("no such device"); | ||
| 359 | return; | 482 | return; |
| 360 | - } | 483 | + }; |
| 361 | - debug!("output device changed"); | ||
| 362 | 484 | ||
| 363 | for event in device.drm_scanner.scan_connectors(&device.drm) { | 485 | for event in device.drm_scanner.scan_connectors(&device.drm) { |
| 364 | match event { | 486 | match event { |
| @@ -366,26 +488,31 @@ impl Tty { | |||
| 366 | connector, | 488 | connector, |
| 367 | crtc: Some(crtc), | 489 | crtc: Some(crtc), |
| 368 | } => { | 490 | } => { |
| 369 | - if let Err(err) = self.connector_connected(niri, connector, crtc) { | 491 | + if let Err(err) = self.connector_connected(niri, node, connector, crtc) { |
| 370 | warn!("error connecting connector: {err:?}"); | 492 | warn!("error connecting connector: {err:?}"); |
| 371 | } | 493 | } |
| 372 | } | 494 | } |
| 373 | DrmScanEvent::Disconnected { | 495 | DrmScanEvent::Disconnected { |
| 374 | connector, | 496 | connector, |
| 375 | crtc: Some(crtc), | 497 | crtc: Some(crtc), |
| 376 | - } => self.connector_disconnected(niri, connector, crtc), | 498 | + } => self.connector_disconnected(niri, node, connector, crtc), |
| 377 | _ => (), | 499 | _ => (), |
| 378 | } | 500 | } |
| 379 | } | 501 | } |
| 380 | } | 502 | } |
| 381 | 503 | ||
| 382 | fn device_removed(&mut self, device_id: dev_t, niri: &mut Niri) { | 504 | fn device_removed(&mut self, device_id: dev_t, niri: &mut Niri) { |
| 383 | - let Some(device) = &mut self.output_device else { | 505 | + debug!("device removed: {device_id}"); |
| 506 | + | ||
| 507 | + let Ok(node) = DrmNode::from_dev_id(device_id) else { | ||
| 508 | + warn!("error creating DrmNode"); | ||
| 384 | return; | 509 | return; |
| 385 | }; | 510 | }; |
| 386 | - if device_id != device.id { | 511 | + |
| 512 | + let Some(device) = self.devices.get_mut(&node) else { | ||
| 513 | + warn!("no such device"); | ||
| 387 | return; | 514 | return; |
| 388 | - } | 515 | + }; |
| 389 | 516 | ||
| 390 | let crtcs: Vec<_> = device | 517 | let crtcs: Vec<_> = device |
| 391 | .drm_scanner | 518 | .drm_scanner |
| @@ -394,34 +521,54 @@ impl Tty { | |||
| 394 | .collect(); | 521 | .collect(); |
| 395 | 522 | ||
| 396 | for (connector, crtc) in crtcs { | 523 | for (connector, crtc) in crtcs { |
| 397 | - self.connector_disconnected(niri, connector, crtc); | 524 | + self.connector_disconnected(niri, node, connector, crtc); |
| 398 | } | 525 | } |
| 399 | 526 | ||
| 400 | - let mut device = self.output_device.take().unwrap(); | 527 | + let device = self.devices.remove(&node).unwrap(); |
| 401 | - device.gles.unbind_wl_display(); | 528 | + |
| 402 | - | 529 | + if node == self.primary_node { |
| 403 | - let global = self.dmabuf_global.take().unwrap(); | 530 | + match self.gpu_manager.single_renderer(&device.render_node) { |
| 404 | - niri.dmabuf_state | 531 | + Ok(mut renderer) => renderer.unbind_wl_display(), |
| 405 | - .disable_global::<State>(&niri.display_handle, &global); | 532 | + Err(err) => { |
| 406 | - niri.event_loop | 533 | + error!("error creating renderer during device removal: {err}"); |
| 407 | - .insert_source( | 534 | + } |
| 408 | - Timer::from_duration(Duration::from_secs(10)), | 535 | + } |
| 409 | - move |_, _, state| { | 536 | + |
| 410 | - state | 537 | + // Disable and destroy the dmabuf global. |
| 411 | - .niri | 538 | + let global = self.dmabuf_global.take().unwrap(); |
| 412 | - .dmabuf_state | 539 | + niri.dmabuf_state |
| 413 | - .destroy_global::<State>(&state.niri.display_handle, global); | 540 | + .disable_global::<State>(&niri.display_handle, &global); |
| 414 | - TimeoutAction::Drop | 541 | + niri.event_loop |
| 415 | - }, | 542 | + .insert_source( |
| 416 | - ) | 543 | + Timer::from_duration(Duration::from_secs(10)), |
| 417 | - .unwrap(); | 544 | + move |_, _, state| { |
| 545 | + state | ||
| 546 | + .niri | ||
| 547 | + .dmabuf_state | ||
| 548 | + .destroy_global::<State>(&state.niri.display_handle, global); | ||
| 549 | + TimeoutAction::Drop | ||
| 550 | + }, | ||
| 551 | + ) | ||
| 552 | + .unwrap(); | ||
| 553 | + | ||
| 554 | + self.primary_allocator = None; | ||
| 555 | + | ||
| 556 | + // Clear the dmabuf feedbacks for all surfaces. | ||
| 557 | + for device in self.devices.values_mut() { | ||
| 558 | + for surface in device.surfaces.values_mut() { | ||
| 559 | + surface.dmabuf_feedback = None; | ||
| 560 | + } | ||
| 561 | + } | ||
| 562 | + } | ||
| 418 | 563 | ||
| 564 | + self.gpu_manager.as_mut().remove_node(&device.render_node); | ||
| 419 | niri.event_loop.remove(device.token); | 565 | niri.event_loop.remove(device.token); |
| 420 | } | 566 | } |
| 421 | 567 | ||
| 422 | fn connector_connected( | 568 | fn connector_connected( |
| 423 | &mut self, | 569 | &mut self, |
| 424 | niri: &mut Niri, | 570 | niri: &mut Niri, |
| 571 | + node: DrmNode, | ||
| 425 | connector: connector::Info, | 572 | connector: connector::Info, |
| 426 | crtc: crtc::Handle, | 573 | crtc: crtc::Handle, |
| 427 | ) -> anyhow::Result<()> { | 574 | ) -> anyhow::Result<()> { |
| @@ -446,10 +593,7 @@ impl Tty { | |||
| 446 | return Ok(()); | 593 | return Ok(()); |
| 447 | } | 594 | } |
| 448 | 595 | ||
| 449 | - let device = self | 596 | + let device = self.devices.get_mut(&node).context("missing device")?; |
| 450 | - .output_device | ||
| 451 | - .as_mut() | ||
| 452 | - .context("missing output device")?; | ||
| 453 | 597 | ||
| 454 | // FIXME: print modes here until we have a better way to list all modes. | 598 | // FIXME: print modes here until we have a better way to list all modes. |
| 455 | for m in connector.modes() { | 599 | for m in connector.modes() { |
| @@ -559,10 +703,9 @@ impl Tty { | |||
| 559 | output.change_current_state(Some(wl_mode), None, Some(Scale::Integer(scale)), None); | 703 | output.change_current_state(Some(wl_mode), None, Some(Scale::Integer(scale)), None); |
| 560 | output.set_preferred(wl_mode); | 704 | output.set_preferred(wl_mode); |
| 561 | 705 | ||
| 562 | - output.user_data().insert_if_missing(|| TtyOutputState { | 706 | + output |
| 563 | - device_id: device.id, | 707 | + .user_data() |
| 564 | - crtc, | 708 | + .insert_if_missing(|| TtyOutputState { node, crtc }); |
| 565 | - }); | ||
| 566 | 709 | ||
| 567 | let mut planes = surface.planes().clone(); | 710 | let mut planes = surface.planes().clone(); |
| 568 | 711 | ||
| @@ -581,19 +724,10 @@ impl Tty { | |||
| 581 | Some(device.gbm.clone()) | 724 | Some(device.gbm.clone()) |
| 582 | }; | 725 | }; |
| 583 | 726 | ||
| 584 | - let egl_context = device.gles.egl_context(); | 727 | + let renderer = self.gpu_manager.single_renderer(&device.render_node)?; |
| 585 | - let texture_formats = egl_context.dmabuf_texture_formats(); | 728 | + let egl_context = renderer.as_ref().egl_context(); |
| 586 | let render_formats = egl_context.dmabuf_render_formats(); | 729 | let render_formats = egl_context.dmabuf_render_formats(); |
| 587 | 730 | ||
| 588 | - let scanout_formats = planes | ||
| 589 | - .primary | ||
| 590 | - .formats | ||
| 591 | - .iter() | ||
| 592 | - .chain(planes.overlay.iter().flat_map(|p| &p.formats)) | ||
| 593 | - .copied() | ||
| 594 | - .collect::<HashSet<_>>(); | ||
| 595 | - let scanout_formats = scanout_formats.intersection(texture_formats).copied(); | ||
| 596 | - | ||
| 597 | // Create the compositor. | 731 | // Create the compositor. |
| 598 | let compositor = DrmCompositor::new( | 732 | let compositor = DrmCompositor::new( |
| 599 | OutputModeSource::Auto(output.clone()), | 733 | OutputModeSource::Auto(output.clone()), |
| @@ -602,15 +736,31 @@ impl Tty { | |||
| 602 | allocator, | 736 | allocator, |
| 603 | device.gbm.clone(), | 737 | device.gbm.clone(), |
| 604 | SUPPORTED_COLOR_FORMATS, | 738 | SUPPORTED_COLOR_FORMATS, |
| 739 | + // This is only used to pick a good internal format, so it can use the surface's render | ||
| 740 | + // formats, even though we only ever render on the primary GPU. | ||
| 605 | render_formats.clone(), | 741 | render_formats.clone(), |
| 606 | device.drm.cursor_size(), | 742 | device.drm.cursor_size(), |
| 607 | cursor_plane_gbm, | 743 | cursor_plane_gbm, |
| 608 | )?; | 744 | )?; |
| 609 | 745 | ||
| 610 | - let dmabuf_feedback = DmabufFeedbackBuilder::new(device.id, texture_formats.clone()) | 746 | + let mut dmabuf_feedback = None; |
| 611 | - .add_preference_tranche(device.id, Some(TrancheFlags::Scanout), scanout_formats) | 747 | + if let Ok(primary_renderer) = self.gpu_manager.single_renderer(&self.primary_render_node) { |
| 612 | - .build() | 748 | + let primary_formats = primary_renderer.dmabuf_formats().collect::<HashSet<_>>(); |
| 613 | - .context("error building dmabuf feedback")?; | 749 | + |
| 750 | + match surface_dmabuf_feedback( | ||
| 751 | + &compositor, | ||
| 752 | + primary_formats, | ||
| 753 | + self.primary_render_node, | ||
| 754 | + device.render_node, | ||
| 755 | + ) { | ||
| 756 | + Ok(feedback) => { | ||
| 757 | + dmabuf_feedback = Some(feedback); | ||
| 758 | + } | ||
| 759 | + Err(err) => { | ||
| 760 | + warn!("error building dmabuf feedback: {err:?}"); | ||
| 761 | + } | ||
| 762 | + } | ||
| 763 | + } | ||
| 614 | 764 | ||
| 615 | let vblank_frame_name = | 765 | let vblank_frame_name = |
| 616 | tracy_client::FrameName::new_leak(format!("vblank on {output_name}")); | 766 | tracy_client::FrameName::new_leak(format!("vblank on {output_name}")); |
| @@ -654,12 +804,14 @@ impl Tty { | |||
| 654 | fn connector_disconnected( | 804 | fn connector_disconnected( |
| 655 | &mut self, | 805 | &mut self, |
| 656 | niri: &mut Niri, | 806 | niri: &mut Niri, |
| 807 | + node: DrmNode, | ||
| 657 | connector: connector::Info, | 808 | connector: connector::Info, |
| 658 | crtc: crtc::Handle, | 809 | crtc: crtc::Handle, |
| 659 | ) { | 810 | ) { |
| 660 | debug!("disconnecting connector: {connector:?}"); | 811 | debug!("disconnecting connector: {connector:?}"); |
| 661 | - let Some(device) = self.output_device.as_mut() else { | 812 | + |
| 662 | - error!("missing output device"); | 813 | + let Some(device) = self.devices.get_mut(&node) else { |
| 814 | + error!("missing device"); | ||
| 663 | return; | 815 | return; |
| 664 | }; | 816 | }; |
| 665 | 817 | ||
| @@ -673,7 +825,7 @@ impl Tty { | |||
| 673 | .outputs() | 825 | .outputs() |
| 674 | .find(|output| { | 826 | .find(|output| { |
| 675 | let tty_state: &TtyOutputState = output.user_data().get().unwrap(); | 827 | let tty_state: &TtyOutputState = output.user_data().get().unwrap(); |
| 676 | - tty_state.device_id == device.id && tty_state.crtc == crtc | 828 | + tty_state.node == node && tty_state.crtc == crtc |
| 677 | }) | 829 | }) |
| 678 | .cloned(); | 830 | .cloned(); |
| 679 | if let Some(output) = output { | 831 | if let Some(output) = output { |
| @@ -685,14 +837,20 @@ impl Tty { | |||
| 685 | self.connectors.lock().unwrap().remove(&surface.name); | 837 | self.connectors.lock().unwrap().remove(&surface.name); |
| 686 | } | 838 | } |
| 687 | 839 | ||
| 688 | - fn on_vblank(&mut self, niri: &mut Niri, crtc: crtc::Handle, meta: DrmEventMetadata) { | 840 | + fn on_vblank( |
| 841 | + &mut self, | ||
| 842 | + niri: &mut Niri, | ||
| 843 | + node: DrmNode, | ||
| 844 | + crtc: crtc::Handle, | ||
| 845 | + meta: DrmEventMetadata, | ||
| 846 | + ) { | ||
| 689 | let span = tracy_client::span!("Tty::on_vblank"); | 847 | let span = tracy_client::span!("Tty::on_vblank"); |
| 690 | 848 | ||
| 691 | let now = get_monotonic_time(); | 849 | let now = get_monotonic_time(); |
| 692 | 850 | ||
| 693 | - let Some(device) = self.output_device.as_mut() else { | 851 | + let Some(device) = self.devices.get_mut(&node) else { |
| 694 | // I've seen it happen. | 852 | // I've seen it happen. |
| 695 | - error!("missing output device in vblank callback for crtc {crtc:?}"); | 853 | + error!("missing device in vblank callback for crtc {crtc:?}"); |
| 696 | return; | 854 | return; |
| 697 | }; | 855 | }; |
| 698 | 856 | ||
| @@ -744,7 +902,7 @@ impl Tty { | |||
| 744 | .outputs() | 902 | .outputs() |
| 745 | .find(|output| { | 903 | .find(|output| { |
| 746 | let tty_state: &TtyOutputState = output.user_data().get().unwrap(); | 904 | let tty_state: &TtyOutputState = output.user_data().get().unwrap(); |
| 747 | - tty_state.device_id == device.id && tty_state.crtc == crtc | 905 | + tty_state.node == node && tty_state.crtc == crtc |
| 748 | }) | 906 | }) |
| 749 | .cloned() | 907 | .cloned() |
| 750 | else { | 908 | else { |
| @@ -864,7 +1022,11 @@ impl Tty { | |||
| 864 | &mut self, | 1022 | &mut self, |
| 865 | f: impl FnOnce(&mut GlesRenderer) -> T, | 1023 | f: impl FnOnce(&mut GlesRenderer) -> T, |
| 866 | ) -> Option<T> { | 1024 | ) -> Option<T> { |
| 867 | - self.output_device.as_mut().map(|d| f(&mut d.gles)) | 1025 | + let mut renderer = self |
| 1026 | + .gpu_manager | ||
| 1027 | + .single_renderer(&self.primary_render_node) | ||
| 1028 | + .ok()?; | ||
| 1029 | + Some(f(renderer.as_gles_renderer())) | ||
| 868 | } | 1030 | } |
| 869 | 1031 | ||
| 870 | pub fn render( | 1032 | pub fn render( |
| @@ -877,32 +1039,48 @@ impl Tty { | |||
| 877 | 1039 | ||
| 878 | let mut rv = RenderResult::Skipped; | 1040 | let mut rv = RenderResult::Skipped; |
| 879 | 1041 | ||
| 880 | - let Some(device) = self.output_device.as_mut() else { | 1042 | + let tty_state: &TtyOutputState = output.user_data().get().unwrap(); |
| 1043 | + let Some(device) = self.devices.get_mut(&tty_state.node) else { | ||
| 881 | error!("missing output device"); | 1044 | error!("missing output device"); |
| 882 | return rv; | 1045 | return rv; |
| 883 | }; | 1046 | }; |
| 884 | 1047 | ||
| 1048 | + let Some(surface) = device.surfaces.get_mut(&tty_state.crtc) else { | ||
| 1049 | + error!("missing surface"); | ||
| 1050 | + return rv; | ||
| 1051 | + }; | ||
| 1052 | + | ||
| 1053 | + span.emit_text(&surface.name); | ||
| 1054 | + | ||
| 885 | if !device.drm.is_active() { | 1055 | if !device.drm.is_active() { |
| 886 | warn!("device is inactive"); | 1056 | warn!("device is inactive"); |
| 887 | return rv; | 1057 | return rv; |
| 888 | } | 1058 | } |
| 889 | 1059 | ||
| 890 | - let tty_state: &TtyOutputState = output.user_data().get().unwrap(); | 1060 | + let Some(allocator) = self.primary_allocator.as_mut() else { |
| 891 | - let Some(surface) = device.surfaces.get_mut(&tty_state.crtc) else { | 1061 | + warn!("no primary allocator"); |
| 892 | - error!("missing surface"); | ||
| 893 | return rv; | 1062 | return rv; |
| 894 | }; | 1063 | }; |
| 895 | 1064 | ||
| 896 | - span.emit_text(&surface.name); | 1065 | + let mut renderer = match self.gpu_manager.renderer( |
| 897 | - | 1066 | + &self.primary_render_node, |
| 898 | - let renderer = &mut device.gles; | 1067 | + &device.render_node, |
| 1068 | + allocator, | ||
| 1069 | + surface.compositor.format(), | ||
| 1070 | + ) { | ||
| 1071 | + Ok(renderer) => renderer, | ||
| 1072 | + Err(err) => { | ||
| 1073 | + error!("error creating renderer for primary GPU: {err:?}"); | ||
| 1074 | + return rv; | ||
| 1075 | + } | ||
| 1076 | + }; | ||
| 899 | 1077 | ||
| 900 | // Render the elements. | 1078 | // Render the elements. |
| 901 | - let elements = niri.render::<GlesRenderer>(renderer, output, true); | 1079 | + let elements = niri.render::<TtyRenderer>(&mut renderer, output, true); |
| 902 | 1080 | ||
| 903 | // Hand them over to the DRM. | 1081 | // Hand them over to the DRM. |
| 904 | let drm_compositor = &mut surface.compositor; | 1082 | let drm_compositor = &mut surface.compositor; |
| 905 | - match drm_compositor.render_frame::<_, _, GlesTexture>(renderer, &elements, [0.; 4]) { | 1083 | + match drm_compositor.render_frame::<_, _, GlesTexture>(&mut renderer, &elements, [0.; 4]) { |
| 906 | Ok(res) => { | 1084 | Ok(res) => { |
| 907 | if self | 1085 | if self |
| 908 | .config | 1086 | .config |
| @@ -917,7 +1095,9 @@ impl Tty { | |||
| 917 | } | 1095 | } |
| 918 | 1096 | ||
| 919 | niri.update_primary_scanout_output(output, &res.states); | 1097 | niri.update_primary_scanout_output(output, &res.states); |
| 920 | - niri.send_dmabuf_feedbacks(output, &surface.dmabuf_feedback); | 1098 | + if let Some(dmabuf_feedback) = surface.dmabuf_feedback.as_ref() { |
| 1099 | + niri.send_dmabuf_feedbacks(output, dmabuf_feedback, &res.states); | ||
| 1100 | + } | ||
| 921 | 1101 | ||
| 922 | if res.damage.is_some() { | 1102 | if res.damage.is_some() { |
| 923 | let presentation_feedbacks = | 1103 | let presentation_feedbacks = |
| @@ -979,7 +1159,7 @@ impl Tty { | |||
| 979 | } | 1159 | } |
| 980 | 1160 | ||
| 981 | pub fn toggle_debug_tint(&mut self) { | 1161 | pub fn toggle_debug_tint(&mut self) { |
| 982 | - if let Some(device) = self.output_device.as_mut() { | 1162 | + for device in self.devices.values_mut() { |
| 983 | for surface in device.surfaces.values_mut() { | 1163 | for surface in device.surfaces.values_mut() { |
| 984 | let compositor = &mut surface.compositor; | 1164 | let compositor = &mut surface.compositor; |
| 985 | compositor.set_debug_flags(compositor.debug_flags() ^ DebugFlags::TINT); | 1165 | compositor.set_debug_flags(compositor.debug_flags() ^ DebugFlags::TINT); |
| @@ -988,8 +1168,15 @@ impl Tty { | |||
| 988 | } | 1168 | } |
| 989 | 1169 | ||
| 990 | pub fn import_dmabuf(&mut self, dmabuf: &Dmabuf) -> Result<(), ()> { | 1170 | pub fn import_dmabuf(&mut self, dmabuf: &Dmabuf) -> Result<(), ()> { |
| 991 | - let device = self.output_device.as_mut().ok_or(())?; | 1171 | + let mut renderer = match self.gpu_manager.single_renderer(&self.primary_render_node) { |
| 992 | - match device.gles.import_dmabuf(dmabuf, None) { | 1172 | + Ok(renderer) => renderer, |
| 1173 | + Err(err) => { | ||
| 1174 | + debug!("error creating renderer for primary GPU: {err:?}"); | ||
| 1175 | + return Err(()); | ||
| 1176 | + } | ||
| 1177 | + }; | ||
| 1178 | + | ||
| 1179 | + match renderer.import_dmabuf(dmabuf, None) { | ||
| 993 | Ok(_texture) => Ok(()), | 1180 | Ok(_texture) => Ok(()), |
| 994 | Err(err) => { | 1181 | Err(err) => { |
| 995 | debug!("error importing dmabuf: {err:?}"); | 1182 | debug!("error importing dmabuf: {err:?}"); |
| @@ -998,26 +1185,82 @@ impl Tty { | |||
| 998 | } | 1185 | } |
| 999 | } | 1186 | } |
| 1000 | 1187 | ||
| 1188 | + pub fn early_import(&mut self, surface: &WlSurface) { | ||
| 1189 | + if let Err(err) = self.gpu_manager.early_import( | ||
| 1190 | + // We always advertise the primary GPU in dmabuf feedback. | ||
| 1191 | + Some(self.primary_render_node), | ||
| 1192 | + // We always render on the primary GPU. | ||
| 1193 | + self.primary_render_node, | ||
| 1194 | + surface, | ||
| 1195 | + ) { | ||
| 1196 | + warn!("error doing early import: {err:?}"); | ||
| 1197 | + } | ||
| 1198 | + } | ||
| 1199 | + | ||
| 1001 | pub fn connectors(&self) -> Arc<Mutex<HashMap<String, Output>>> { | 1200 | pub fn connectors(&self) -> Arc<Mutex<HashMap<String, Output>>> { |
| 1002 | self.connectors.clone() | 1201 | self.connectors.clone() |
| 1003 | } | 1202 | } |
| 1004 | 1203 | ||
| 1005 | #[cfg(feature = "xdp-gnome-screencast")] | 1204 | #[cfg(feature = "xdp-gnome-screencast")] |
| 1006 | - pub fn gbm_device(&self) -> Option<GbmDevice<DrmDeviceFd>> { | 1205 | + pub fn primary_gbm_device(&self) -> Option<GbmDevice<DrmDeviceFd>> { |
| 1007 | - self.output_device.as_ref().map(|d| d.gbm.clone()) | 1206 | + self.devices.get(&self.primary_node).map(|d| d.gbm.clone()) |
| 1008 | } | 1207 | } |
| 1009 | 1208 | ||
| 1010 | pub fn set_monitors_active(&self, active: bool) { | 1209 | pub fn set_monitors_active(&self, active: bool) { |
| 1011 | - let Some(device) = &self.output_device else { | 1210 | + for device in self.devices.values() { |
| 1012 | - return; | 1211 | + for crtc in device.surfaces.keys() { |
| 1013 | - }; | 1212 | + set_crtc_active(&device.drm, *crtc, active); |
| 1014 | - | 1213 | + } |
| 1015 | - for crtc in device.surfaces.keys() { | ||
| 1016 | - set_crtc_active(&device.drm, *crtc, active); | ||
| 1017 | } | 1214 | } |
| 1018 | } | 1215 | } |
| 1019 | } | 1216 | } |
| 1020 | 1217 | ||
| 1218 | +fn surface_dmabuf_feedback( | ||
| 1219 | + compositor: &GbmDrmCompositor, | ||
| 1220 | + primary_formats: HashSet<Format>, | ||
| 1221 | + primary_render_node: DrmNode, | ||
| 1222 | + surface_render_node: DrmNode, | ||
| 1223 | +) -> Result<SurfaceDmabufFeedback, io::Error> { | ||
| 1224 | + let surface = compositor.surface(); | ||
| 1225 | + let planes = surface.planes(); | ||
| 1226 | + | ||
| 1227 | + let plane_formats = planes | ||
| 1228 | + .primary | ||
| 1229 | + .formats | ||
| 1230 | + .iter() | ||
| 1231 | + .chain(planes.overlay.iter().flat_map(|p| p.formats.iter())) | ||
| 1232 | + .copied() | ||
| 1233 | + .collect::<HashSet<_>>(); | ||
| 1234 | + | ||
| 1235 | + // We limit the scan-out trache to formats we can also render from so that there is always a | ||
| 1236 | + // fallback render path available in case the supplied buffer can not be scanned out directly. | ||
| 1237 | + let scanout_formats = plane_formats | ||
| 1238 | + .intersection(&primary_formats) | ||
| 1239 | + .copied() | ||
| 1240 | + .collect::<Vec<_>>(); | ||
| 1241 | + | ||
| 1242 | + let builder = DmabufFeedbackBuilder::new(primary_render_node.dev_id(), primary_formats); | ||
| 1243 | + | ||
| 1244 | + let scanout = builder | ||
| 1245 | + .clone() | ||
| 1246 | + .add_preference_tranche( | ||
| 1247 | + surface_render_node.dev_id(), | ||
| 1248 | + Some(TrancheFlags::Scanout), | ||
| 1249 | + scanout_formats, | ||
| 1250 | + ) | ||
| 1251 | + .build()?; | ||
| 1252 | + | ||
| 1253 | + // If this is the primary node surface, send scanout formats in both tranches to avoid | ||
| 1254 | + // duplication. | ||
| 1255 | + let render = if primary_render_node == surface_render_node { | ||
| 1256 | + scanout.clone() | ||
| 1257 | + } else { | ||
| 1258 | + builder.build()? | ||
| 1259 | + }; | ||
| 1260 | + | ||
| 1261 | + Ok(SurfaceDmabufFeedback { render, scanout }) | ||
| 1262 | +} | ||
| 1263 | + | ||
| 1021 | fn find_drm_property(drm: &DrmDevice, crtc: crtc::Handle, name: &str) -> Option<property::Handle> { | 1264 | fn find_drm_property(drm: &DrmDevice, crtc: crtc::Handle, name: &str) -> Option<property::Handle> { |
| 1022 | let props = match drm.get_properties(crtc) { | 1265 | let props = match drm.get_properties(crtc) { |
| 1023 | Ok(props) => props, | 1266 | Ok(props) => props, |
src/handlers/compositor.rsmodified+1 -0
| @@ -82,6 +82,7 @@ impl CompositorHandler for State { | |||
| 82 | let _span = tracy_client::span!("CompositorHandler::commit"); | 82 | let _span = tracy_client::span!("CompositorHandler::commit"); |
| 83 | 83 | ||
| 84 | on_commit_buffer_handler::<Self>(surface); | 84 | on_commit_buffer_handler::<Self>(surface); |
| 85 | + self.backend.early_import(surface); | ||
| 85 | 86 | ||
| 86 | if is_sync_subsurface(surface) { | 87 | if is_sync_subsurface(surface) { |
| 87 | return; | 88 | return; |
src/niri.rsmodified+96 -7
| @@ -16,6 +16,7 @@ use smithay::backend::renderer::element::surface::{ | |||
| 16 | render_elements_from_surface_tree, WaylandSurfaceRenderElement, | 16 | render_elements_from_surface_tree, WaylandSurfaceRenderElement, |
| 17 | }; | 17 | }; |
| 18 | use smithay::backend::renderer::element::texture::TextureRenderElement; | 18 | use smithay::backend::renderer::element::texture::TextureRenderElement; |
| 19 | +use smithay::backend::renderer::element::utils::select_dmabuf_feedback; | ||
| 19 | use smithay::backend::renderer::element::{ | 20 | use smithay::backend::renderer::element::{ |
| 20 | default_primary_scanout_output_compare, AsRenderElements, Element, Id, Kind, RenderElement, | 21 | default_primary_scanout_output_compare, AsRenderElements, Element, Id, Kind, RenderElement, |
| 21 | RenderElementStates, UnderlyingStorage, | 22 | RenderElementStates, UnderlyingStorage, |
| @@ -61,7 +62,7 @@ use smithay::wayland::compositor::{ | |||
| 61 | CompositorState, SurfaceData, TraversalAction, | 62 | CompositorState, SurfaceData, TraversalAction, |
| 62 | }; | 63 | }; |
| 63 | use smithay::wayland::cursor_shape::CursorShapeManagerState; | 64 | use smithay::wayland::cursor_shape::CursorShapeManagerState; |
| 64 | -use smithay::wayland::dmabuf::{DmabufFeedback, DmabufState}; | 65 | +use smithay::wayland::dmabuf::DmabufState; |
| 65 | use smithay::wayland::input_method::InputMethodManagerState; | 66 | use smithay::wayland::input_method::InputMethodManagerState; |
| 66 | use smithay::wayland::output::OutputManagerState; | 67 | use smithay::wayland::output::OutputManagerState; |
| 67 | use smithay::wayland::pointer_constraints::{with_pointer_constraint, PointerConstraintsState}; | 68 | use smithay::wayland::pointer_constraints::{with_pointer_constraint, PointerConstraintsState}; |
| @@ -83,6 +84,7 @@ use smithay::wayland::text_input::TextInputManagerState; | |||
| 83 | use smithay::wayland::virtual_keyboard::VirtualKeyboardManagerState; | 84 | use smithay::wayland::virtual_keyboard::VirtualKeyboardManagerState; |
| 84 | 85 | ||
| 85 | use crate::animation; | 86 | use crate::animation; |
| 87 | +use crate::backend::tty::{SurfaceDmabufFeedback, TtyFrame, TtyRenderer, TtyRendererError}; | ||
| 86 | use crate::backend::{Backend, RenderResult, Tty, Winit}; | 88 | use crate::backend::{Backend, RenderResult, Tty, Winit}; |
| 87 | use crate::config::{Config, TrackLayout}; | 89 | use crate::config::{Config, TrackLayout}; |
| 88 | use crate::cursor::{CursorManager, CursorTextureCache, RenderCursor, XCursor}; | 90 | use crate::cursor::{CursorManager, CursorTextureCache, RenderCursor, XCursor}; |
| @@ -1829,18 +1831,45 @@ impl Niri { | |||
| 1829 | } | 1831 | } |
| 1830 | } | 1832 | } |
| 1831 | 1833 | ||
| 1832 | - pub fn send_dmabuf_feedbacks(&self, output: &Output, feedback: &DmabufFeedback) { | 1834 | + pub fn send_dmabuf_feedbacks( |
| 1835 | + &self, | ||
| 1836 | + output: &Output, | ||
| 1837 | + feedback: &SurfaceDmabufFeedback, | ||
| 1838 | + render_element_states: &RenderElementStates, | ||
| 1839 | + ) { | ||
| 1833 | let _span = tracy_client::span!("Niri::send_dmabuf_feedbacks"); | 1840 | let _span = tracy_client::span!("Niri::send_dmabuf_feedbacks"); |
| 1834 | 1841 | ||
| 1835 | // We can unconditionally send the current output's feedback to regular and layer-shell | 1842 | // We can unconditionally send the current output's feedback to regular and layer-shell |
| 1836 | // surfaces, as they can only be displayed on a single output at a time. Even if a surface | 1843 | // surfaces, as they can only be displayed on a single output at a time. Even if a surface |
| 1837 | // is currently invisible, this is the DMABUF feedback that it should know about. | 1844 | // is currently invisible, this is the DMABUF feedback that it should know about. |
| 1838 | for win in self.layout.windows_for_output(output) { | 1845 | for win in self.layout.windows_for_output(output) { |
| 1839 | - win.send_dmabuf_feedback(output, |_, _| Some(output.clone()), |_, _| feedback); | 1846 | + win.send_dmabuf_feedback( |
| 1847 | + output, | ||
| 1848 | + |_, _| Some(output.clone()), | ||
| 1849 | + |surface, _| { | ||
| 1850 | + select_dmabuf_feedback( | ||
| 1851 | + surface, | ||
| 1852 | + render_element_states, | ||
| 1853 | + &feedback.render, | ||
| 1854 | + &feedback.scanout, | ||
| 1855 | + ) | ||
| 1856 | + }, | ||
| 1857 | + ); | ||
| 1840 | } | 1858 | } |
| 1841 | 1859 | ||
| 1842 | for surface in layer_map_for_output(output).layers() { | 1860 | for surface in layer_map_for_output(output).layers() { |
| 1843 | - surface.send_dmabuf_feedback(output, |_, _| Some(output.clone()), |_, _| feedback); | 1861 | + surface.send_dmabuf_feedback( |
| 1862 | + output, | ||
| 1863 | + |_, _| Some(output.clone()), | ||
| 1864 | + |surface, _| { | ||
| 1865 | + select_dmabuf_feedback( | ||
| 1866 | + surface, | ||
| 1867 | + render_element_states, | ||
| 1868 | + &feedback.render, | ||
| 1869 | + &feedback.scanout, | ||
| 1870 | + ) | ||
| 1871 | + }, | ||
| 1872 | + ); | ||
| 1844 | } | 1873 | } |
| 1845 | 1874 | ||
| 1846 | if let Some(surface) = &self.output_state[output].lock_surface { | 1875 | if let Some(surface) = &self.output_state[output].lock_surface { |
| @@ -1848,7 +1877,14 @@ impl Niri { | |||
| 1848 | surface.wl_surface(), | 1877 | surface.wl_surface(), |
| 1849 | output, | 1878 | output, |
| 1850 | |_, _| Some(output.clone()), | 1879 | |_, _| Some(output.clone()), |
| 1851 | - |_, _| feedback, | 1880 | + |surface, _| { |
| 1881 | + select_dmabuf_feedback( | ||
| 1882 | + surface, | ||
| 1883 | + render_element_states, | ||
| 1884 | + &feedback.render, | ||
| 1885 | + &feedback.scanout, | ||
| 1886 | + ) | ||
| 1887 | + }, | ||
| 1852 | ); | 1888 | ); |
| 1853 | } | 1889 | } |
| 1854 | 1890 | ||
| @@ -1857,7 +1893,14 @@ impl Niri { | |||
| 1857 | surface, | 1893 | surface, |
| 1858 | output, | 1894 | output, |
| 1859 | surface_primary_scanout_output, | 1895 | surface_primary_scanout_output, |
| 1860 | - |_, _| feedback, | 1896 | + |surface, _| { |
| 1897 | + select_dmabuf_feedback( | ||
| 1898 | + surface, | ||
| 1899 | + render_element_states, | ||
| 1900 | + &feedback.render, | ||
| 1901 | + &feedback.scanout, | ||
| 1902 | + ) | ||
| 1903 | + }, | ||
| 1861 | ); | 1904 | ); |
| 1862 | } | 1905 | } |
| 1863 | 1906 | ||
| @@ -1866,7 +1909,14 @@ impl Niri { | |||
| 1866 | surface, | 1909 | surface, |
| 1867 | output, | 1910 | output, |
| 1868 | surface_primary_scanout_output, | 1911 | surface_primary_scanout_output, |
| 1869 | - |_, _| feedback, | 1912 | + |surface, _| { |
| 1913 | + select_dmabuf_feedback( | ||
| 1914 | + surface, | ||
| 1915 | + render_element_states, | ||
| 1916 | + &feedback.render, | ||
| 1917 | + &feedback.scanout, | ||
| 1918 | + ) | ||
| 1919 | + }, | ||
| 1870 | ); | 1920 | ); |
| 1871 | } | 1921 | } |
| 1872 | } | 1922 | } |
| @@ -2642,6 +2692,45 @@ impl RenderElement<GlesRenderer> for OutputRenderElements<GlesRenderer> { | |||
| 2642 | } | 2692 | } |
| 2643 | } | 2693 | } |
| 2644 | 2694 | ||
| 2695 | +impl<'render, 'alloc> RenderElement<TtyRenderer<'render, 'alloc>> | ||
| 2696 | + for OutputRenderElements<TtyRenderer<'render, 'alloc>> | ||
| 2697 | +{ | ||
| 2698 | + fn draw( | ||
| 2699 | + &self, | ||
| 2700 | + frame: &mut TtyFrame<'render, 'alloc, '_>, | ||
| 2701 | + src: Rectangle<f64, Buffer>, | ||
| 2702 | + dst: Rectangle<i32, Physical>, | ||
| 2703 | + damage: &[Rectangle<i32, Physical>], | ||
| 2704 | + ) -> Result<(), TtyRendererError<'render, 'alloc>> { | ||
| 2705 | + match self { | ||
| 2706 | + Self::Monitor(elem) => elem.draw(frame, src, dst, damage), | ||
| 2707 | + Self::Wayland(elem) => elem.draw(frame, src, dst, damage), | ||
| 2708 | + Self::NamedPointer(elem) => { | ||
| 2709 | + RenderElement::<TtyRenderer<'render, 'alloc>>::draw(&elem, frame, src, dst, damage) | ||
| 2710 | + } | ||
| 2711 | + Self::SolidColor(elem) => { | ||
| 2712 | + RenderElement::<TtyRenderer<'render, 'alloc>>::draw(&elem, frame, src, dst, damage) | ||
| 2713 | + } | ||
| 2714 | + Self::ScreenshotUi(elem) => { | ||
| 2715 | + RenderElement::<TtyRenderer<'render, 'alloc>>::draw(&elem, frame, src, dst, damage) | ||
| 2716 | + } | ||
| 2717 | + } | ||
| 2718 | + } | ||
| 2719 | + | ||
| 2720 | + fn underlying_storage( | ||
| 2721 | + &self, | ||
| 2722 | + renderer: &mut TtyRenderer<'render, 'alloc>, | ||
| 2723 | + ) -> Option<UnderlyingStorage> { | ||
| 2724 | + match self { | ||
| 2725 | + Self::Monitor(elem) => elem.underlying_storage(renderer), | ||
| 2726 | + Self::Wayland(elem) => elem.underlying_storage(renderer), | ||
| 2727 | + Self::NamedPointer(elem) => elem.underlying_storage(renderer), | ||
| 2728 | + Self::SolidColor(elem) => elem.underlying_storage(renderer), | ||
| 2729 | + Self::ScreenshotUi(elem) => elem.underlying_storage(renderer), | ||
| 2730 | + } | ||
| 2731 | + } | ||
| 2732 | +} | ||
| 2733 | + | ||
| 2645 | impl<R: NiriRenderer> From<MonitorRenderElement<R>> for OutputRenderElements<R> { | 2734 | impl<R: NiriRenderer> From<MonitorRenderElement<R>> for OutputRenderElements<R> { |
| 2646 | fn from(x: MonitorRenderElement<R>) -> Self { | 2735 | fn from(x: MonitorRenderElement<R>) -> Self { |
| 2647 | Self::Monitor(x) | 2736 | Self::Monitor(x) |
src/render_helpers.rsmodified+39 -0
| @@ -6,6 +6,8 @@ use smithay::backend::renderer::utils::CommitCounter; | |||
| 6 | use smithay::backend::renderer::{Bind, ExportMem, ImportAll, Offscreen, Renderer, Texture}; | 6 | use smithay::backend::renderer::{Bind, ExportMem, ImportAll, Offscreen, Renderer, Texture}; |
| 7 | use smithay::utils::{Buffer, Physical, Rectangle, Scale, Transform}; | 7 | use smithay::utils::{Buffer, Physical, Rectangle, Scale, Transform}; |
| 8 | 8 | ||
| 9 | +use crate::backend::tty::{TtyFrame, TtyRenderer, TtyRendererError}; | ||
| 10 | + | ||
| 9 | /// Trait with our main renderer requirements to save on the typing. | 11 | /// Trait with our main renderer requirements to save on the typing. |
| 10 | pub trait NiriRenderer: | 12 | pub trait NiriRenderer: |
| 11 | ImportAll | 13 | ImportAll |
| @@ -45,6 +47,12 @@ impl AsGlesRenderer for GlesRenderer { | |||
| 45 | } | 47 | } |
| 46 | } | 48 | } |
| 47 | 49 | ||
| 50 | +impl<'render, 'alloc> AsGlesRenderer for TtyRenderer<'render, 'alloc> { | ||
| 51 | + fn as_gles_renderer(&mut self) -> &mut GlesRenderer { | ||
| 52 | + self.as_mut() | ||
| 53 | + } | ||
| 54 | +} | ||
| 55 | + | ||
| 48 | /// Trait for getting the underlying `GlesFrame`. | 56 | /// Trait for getting the underlying `GlesFrame`. |
| 49 | pub trait AsGlesFrame<'frame> | 57 | pub trait AsGlesFrame<'frame> |
| 50 | where | 58 | where |
| @@ -59,6 +67,12 @@ impl<'frame> AsGlesFrame<'frame> for GlesFrame<'frame> { | |||
| 59 | } | 67 | } |
| 60 | } | 68 | } |
| 61 | 69 | ||
| 70 | +impl<'render, 'alloc, 'frame> AsGlesFrame<'frame> for TtyFrame<'render, 'alloc, 'frame> { | ||
| 71 | + fn as_gles_frame(&mut self) -> &mut GlesFrame<'frame> { | ||
| 72 | + self.as_mut() | ||
| 73 | + } | ||
| 74 | +} | ||
| 75 | + | ||
| 62 | /// Wrapper for a texture from the primary GPU for rendering with the primary GPU. | 76 | /// Wrapper for a texture from the primary GPU for rendering with the primary GPU. |
| 63 | #[derive(Debug)] | 77 | #[derive(Debug)] |
| 64 | pub struct PrimaryGpuTextureRenderElement(pub TextureRenderElement<GlesTexture>); | 78 | pub struct PrimaryGpuTextureRenderElement(pub TextureRenderElement<GlesTexture>); |
| @@ -124,3 +138,28 @@ impl RenderElement<GlesRenderer> for PrimaryGpuTextureRenderElement { | |||
| 124 | None | 138 | None |
| 125 | } | 139 | } |
| 126 | } | 140 | } |
| 141 | + | ||
| 142 | +impl<'render, 'alloc> RenderElement<TtyRenderer<'render, 'alloc>> | ||
| 143 | + for PrimaryGpuTextureRenderElement | ||
| 144 | +{ | ||
| 145 | + fn draw( | ||
| 146 | + &self, | ||
| 147 | + frame: &mut TtyFrame<'_, '_, '_>, | ||
| 148 | + src: Rectangle<f64, Buffer>, | ||
| 149 | + dst: Rectangle<i32, Physical>, | ||
| 150 | + damage: &[Rectangle<i32, Physical>], | ||
| 151 | + ) -> Result<(), TtyRendererError<'render, 'alloc>> { | ||
| 152 | + let gles_frame = frame.as_gles_frame(); | ||
| 153 | + RenderElement::<GlesRenderer>::draw(&self.0, gles_frame, src, dst, damage)?; | ||
| 154 | + Ok(()) | ||
| 155 | + } | ||
| 156 | + | ||
| 157 | + fn underlying_storage( | ||
| 158 | + &self, | ||
| 159 | + _renderer: &mut TtyRenderer<'render, 'alloc>, | ||
| 160 | + ) -> Option<UnderlyingStorage> { | ||
| 161 | + // If scanout for things other than Wayland buffers is implemented, this will need to take | ||
| 162 | + // the target GPU into account. | ||
| 163 | + None | ||
| 164 | + } | ||
| 165 | +} | ||
src/screenshot_ui.rsmodified+29 -0
| @@ -17,6 +17,7 @@ use smithay::input::keyboard::{Keysym, ModifiersState}; | |||
| 17 | use smithay::output::{Output, WeakOutput}; | 17 | use smithay::output::{Output, WeakOutput}; |
| 18 | use smithay::utils::{Buffer, Physical, Point, Rectangle, Scale, Size, Transform}; | 18 | use smithay::utils::{Buffer, Physical, Point, Rectangle, Scale, Size, Transform}; |
| 19 | 19 | ||
| 20 | +use crate::backend::tty::{TtyFrame, TtyRenderer, TtyRendererError}; | ||
| 20 | use crate::config::Action; | 21 | use crate::config::Action; |
| 21 | use crate::render_helpers::PrimaryGpuTextureRenderElement; | 22 | use crate::render_helpers::PrimaryGpuTextureRenderElement; |
| 22 | 23 | ||
| @@ -539,6 +540,34 @@ impl RenderElement<GlesRenderer> for ScreenshotUiRenderElement { | |||
| 539 | } | 540 | } |
| 540 | } | 541 | } |
| 541 | 542 | ||
| 543 | +impl<'render, 'alloc> RenderElement<TtyRenderer<'render, 'alloc>> for ScreenshotUiRenderElement { | ||
| 544 | + fn draw( | ||
| 545 | + &self, | ||
| 546 | + frame: &mut TtyFrame<'render, 'alloc, '_>, | ||
| 547 | + src: Rectangle<f64, Buffer>, | ||
| 548 | + dst: Rectangle<i32, Physical>, | ||
| 549 | + damage: &[Rectangle<i32, Physical>], | ||
| 550 | + ) -> Result<(), TtyRendererError<'render, 'alloc>> { | ||
| 551 | + match self { | ||
| 552 | + Self::Screenshot(elem) => { | ||
| 553 | + RenderElement::<TtyRenderer<'render, 'alloc>>::draw(&elem, frame, src, dst, damage) | ||
| 554 | + } | ||
| 555 | + Self::SolidColor(elem) => { | ||
| 556 | + RenderElement::<TtyRenderer<'render, 'alloc>>::draw(&elem, frame, src, dst, damage) | ||
| 557 | + } | ||
| 558 | + } | ||
| 559 | + } | ||
| 560 | + | ||
| 561 | + fn underlying_storage( | ||
| 562 | + &self, | ||
| 563 | + _renderer: &mut TtyRenderer<'render, 'alloc>, | ||
| 564 | + ) -> Option<UnderlyingStorage> { | ||
| 565 | + // If scanout for things other than Wayland buffers is implemented, this will need to take | ||
| 566 | + // the target GPU into account. | ||
| 567 | + None | ||
| 568 | + } | ||
| 569 | +} | ||
| 570 | + | ||
| 542 | impl From<SolidColorRenderElement> for ScreenshotUiRenderElement { | 571 | impl From<SolidColorRenderElement> for ScreenshotUiRenderElement { |
| 543 | fn from(x: SolidColorRenderElement) -> Self { | 572 | fn from(x: SolidColorRenderElement) -> Self { |
| 544 | Self::SolidColor(x) | 573 | Self::SolidColor(x) |