forge
Explore
Log in

Don't you dare go Hollow

backend/winit: Add DMA-BUF support (#3327)

Browse files at this commit

849d26d646def844e43722fb921db4c861d105bbUnverifiedAustin Riba and Ivan MolodetskikhParent: 9e5716a
* backend/winit: DMA-BUF setup for Nvidia support

Creates a dmabuf global in the same manner as the tty backend. This
fixes applications failing to launch on Nvidia when using the winit
backend.

This code is adapted from Smithay's Anvil compositor.
See smithay/anvil/src/winit.rs

* fixes

---------
src/backend/winit.rsmodified+40 -0
@@ -4,8 +4,10 @@ use std::mem;
44 use std::rc::Rc;
55 use std::sync::{Arc, Mutex};
66
7+use anyhow::Context as _;
78 use niri_config::{Config, OutputName};
89 use smithay::backend::allocator::dmabuf::Dmabuf;
10+use smithay::backend::egl::EGLDevice;
911 use smithay::backend::renderer::damage::OutputDamageTracker;
1012 use smithay::backend::renderer::gles::GlesRenderer;
1113 use smithay::backend::renderer::{DebugFlags, ImportDma, ImportEgl, Renderer};
@@ -16,6 +18,7 @@ use smithay::reexports::wayland_protocols::wp::presentation_time::server::wp_pre
1618 use smithay::reexports::winit::dpi::LogicalSize;
1719 use smithay::reexports::winit::platform::wayland::WindowAttributesExtWayland;
1820 use smithay::reexports::winit::window::Window;
21+use smithay::wayland::dmabuf::{DmabufFeedbackBuilder, DmabufGlobal};
1922 use smithay::wayland::presentation::Refresh;
2023
2124 use super::{IpcOutputMap, OutputId, RenderResult};
@@ -29,6 +32,7 @@ pub struct Winit {
2932 output: Output,
3033 backend: WinitGraphicsBackend<GlesRenderer>,
3134 damage_tracker: OutputDamageTracker,
35+ dmabuf_global: Option<DmabufGlobal>,
3236 ipc_outputs: Arc<Mutex<IpcOutputMap>>,
3337 }
3438
@@ -137,6 +141,7 @@ impl Winit {
137141 output,
138142 backend,
139143 damage_tracker,
144+ dmabuf_global: None,
140145 ipc_outputs,
141146 })
142147 }
@@ -164,9 +169,44 @@ impl Winit {
164169
165170 niri.update_shaders();
166171
172+ self.create_dmabuf_global(niri);
173+
167174 niri.add_output(self.output.clone(), None, false);
168175 }
169176
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+
170210 pub fn seat_name(&self) -> String {
171211 "winit".to_owned()
172212 }