backend/winit: Add DMA-BUF support (#3327)
Browse files at this commit1 changed file +40 -0
- src/backend/winit.rs +40 -0
src/backend/winit.rsmodified+40 -0
| @@ -4,8 +4,10 @@ use std::mem; | ||
| 4 | 4 | use std::rc::Rc; |
| 5 | 5 | use std::sync::{Arc, Mutex}; |
| 6 | 6 | |
| 7 | +use anyhow::Context as _; | |
| 7 | 8 | use niri_config::{Config, OutputName}; |
| 8 | 9 | use smithay::backend::allocator::dmabuf::Dmabuf; |
| 10 | +use smithay::backend::egl::EGLDevice; | |
| 9 | 11 | use smithay::backend::renderer::damage::OutputDamageTracker; |
| 10 | 12 | use smithay::backend::renderer::gles::GlesRenderer; |
| 11 | 13 | use smithay::backend::renderer::{DebugFlags, ImportDma, ImportEgl, Renderer}; |
| @@ -16,6 +18,7 @@ use smithay::reexports::wayland_protocols::wp::presentation_time::server::wp_pre | ||
| 16 | 18 | use smithay::reexports::winit::dpi::LogicalSize; |
| 17 | 19 | use smithay::reexports::winit::platform::wayland::WindowAttributesExtWayland; |
| 18 | 20 | use smithay::reexports::winit::window::Window; |
| 21 | +use smithay::wayland::dmabuf::{DmabufFeedbackBuilder, DmabufGlobal}; | |
| 19 | 22 | use smithay::wayland::presentation::Refresh; |
| 20 | 23 | |
| 21 | 24 | use super::{IpcOutputMap, OutputId, RenderResult}; |
| @@ -29,6 +32,7 @@ pub struct Winit { | ||
| 29 | 32 | output: Output, |
| 30 | 33 | backend: WinitGraphicsBackend<GlesRenderer>, |
| 31 | 34 | damage_tracker: OutputDamageTracker, |
| 35 | + dmabuf_global: Option<DmabufGlobal>, | |
| 32 | 36 | ipc_outputs: Arc<Mutex<IpcOutputMap>>, |
| 33 | 37 | } |
| 34 | 38 | |
| @@ -137,6 +141,7 @@ impl Winit { | ||
| 137 | 141 | output, |
| 138 | 142 | backend, |
| 139 | 143 | damage_tracker, |
| 144 | + dmabuf_global: None, | |
| 140 | 145 | ipc_outputs, |
| 141 | 146 | }) |
| 142 | 147 | } |
| @@ -164,9 +169,44 @@ impl Winit { | ||
| 164 | 169 | |
| 165 | 170 | niri.update_shaders(); |
| 166 | 171 | |
| 172 | + self.create_dmabuf_global(niri); | |
| 173 | + | |
| 167 | 174 | niri.add_output(self.output.clone(), None, false); |
| 168 | 175 | } |
| 169 | 176 | |
| 177 | + pub fn create_dmabuf_global(&mut self, niri: &mut Niri) { | |
| 178 | + let renderer = self.backend.renderer(); | |
| 179 | + | |
| 180 | + let default_feedback = || { | |
| 181 | + let display = renderer.egl_context().display(); | |
| 182 | + let device = | |
| 183 | + EGLDevice::device_for_display(display).context("error getting EGL device")?; | |
| 184 | + let node = device | |
| 185 | + .try_get_render_node() | |
| 186 | + .context("error getting EGL device render node")? | |
| 187 | + .context("failed to query EGL device render node")?; | |
| 188 | + | |
| 189 | + let primary_formats = renderer.dmabuf_formats(); | |
| 190 | + DmabufFeedbackBuilder::new(node.dev_id(), primary_formats) | |
| 191 | + .build() | |
| 192 | + .context("error building dmabuf feedback") | |
| 193 | + }; | |
| 194 | + | |
| 195 | + // Fallback to dmabuf v3 if we failed to build feedback. | |
| 196 | + let dmabuf_global = match default_feedback() { | |
| 197 | + Ok(feedback) => niri | |
| 198 | + .dmabuf_state | |
| 199 | + .create_global_with_default_feedback::<State>(&niri.display_handle, &feedback), | |
| 200 | + Err(err) => { | |
| 201 | + debug!("failed building default dmabuf feedback, falling back to v3: {err:?}"); | |
| 202 | + let primary_formats = renderer.dmabuf_formats(); | |
| 203 | + niri.dmabuf_state | |
| 204 | + .create_global::<State>(&niri.display_handle, primary_formats) | |
| 205 | + } | |
| 206 | + }; | |
| 207 | + assert!(self.dmabuf_global.replace(dmabuf_global).is_none()); | |
| 208 | + } | |
| 209 | + | |
| 170 | 210 | pub fn seat_name(&self) -> String { |
| 171 | 211 | "winit".to_owned() |
| 172 | 212 | } |