feat: proper dmabuf import

This commit is contained in:
Nova
2023-06-26 20:09:20 -04:00
parent c05921fd71
commit 59d049e7bd
2 changed files with 20 additions and 7 deletions

View File

@@ -16,8 +16,10 @@ use global_counter::primitive::exact::CounterU32;
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use parking_lot::Mutex; use parking_lot::Mutex;
use sk::StereoKitDraw; use sk::StereoKitDraw;
use smithay::backend::allocator::dmabuf::Dmabuf;
use smithay::backend::egl::EGLContext; use smithay::backend::egl::EGLContext;
use smithay::backend::renderer::gles::GlesRenderer; use smithay::backend::renderer::gles::GlesRenderer;
use smithay::backend::renderer::ImportDma;
use smithay::reexports::wayland_server::{backend::GlobalId, Display, ListeningSocket}; use smithay::reexports::wayland_server::{backend::GlobalId, Display, ListeningSocket};
use std::os::unix::prelude::AsRawFd; use std::os::unix::prelude::AsRawFd;
use std::{ use std::{
@@ -26,6 +28,7 @@ use std::{
sync::Arc, sync::Arc,
}; };
use stereokit as sk; use stereokit as sk;
use tokio::sync::mpsc::UnboundedReceiver;
use tokio::{ use tokio::{
io::unix::AsyncFd, net::UnixListener as AsyncUnixListener, sync::mpsc, task::JoinHandle, io::unix::AsyncFd, net::UnixListener as AsyncUnixListener, sync::mpsc, task::JoinHandle,
}; };
@@ -63,6 +66,7 @@ pub struct Wayland {
pub socket_name: String, pub socket_name: String,
join_handle: JoinHandle<Result<()>>, join_handle: JoinHandle<Result<()>>,
renderer: GlesRenderer, renderer: GlesRenderer,
dmabuf_rx: UnboundedReceiver<Dmabuf>,
state: Arc<Mutex<WaylandState>>, state: Arc<Mutex<WaylandState>>,
} }
impl Wayland { impl Wayland {
@@ -79,8 +83,9 @@ impl Wayland {
let display: Display<WaylandState> = Display::new()?; let display: Display<WaylandState> = Display::new()?;
let display_handle = display.handle(); let display_handle = display.handle();
let (dmabuf_tx, dmabuf_rx) = mpsc::unbounded_channel();
let display = Arc::new(Mutex::new(display)); let display = Arc::new(Mutex::new(display));
let state = WaylandState::new(display.clone(), display_handle, &renderer); let state = WaylandState::new(display.clone(), display_handle, &renderer, dmabuf_tx);
let (global_destroy_queue_in, global_destroy_queue) = mpsc::channel(8); let (global_destroy_queue_in, global_destroy_queue) = mpsc::channel(8);
GLOBAL_DESTROY_QUEUE.set(global_destroy_queue_in).unwrap(); GLOBAL_DESTROY_QUEUE.set(global_destroy_queue_in).unwrap();
@@ -100,6 +105,7 @@ impl Wayland {
socket_name, socket_name,
join_handle, join_handle,
renderer, renderer,
dmabuf_rx,
state, state,
}) })
} }
@@ -150,6 +156,9 @@ impl Wayland {
#[instrument(level = "debug", name = "Wayland frame", skip(self, sk))] #[instrument(level = "debug", name = "Wayland frame", skip(self, sk))]
pub fn update(&mut self, sk: &impl StereoKitDraw) { pub fn update(&mut self, sk: &impl StereoKitDraw) {
while let Ok(dmabuf) = self.dmabuf_rx.try_recv() {
let _ = self.renderer.import_dmabuf(&dmabuf, None);
}
for core_surface in CORE_SURFACES.get_valid_contents() { for core_surface in CORE_SURFACES.get_valid_contents() {
core_surface.process(sk, &mut self.renderer); core_surface.process(sk, &mut self.renderer);
} }

View File

@@ -24,12 +24,13 @@ use smithay::{
wayland::{ wayland::{
buffer::BufferHandler, buffer::BufferHandler,
compositor::{CompositorClientState, CompositorState}, compositor::{CompositorClientState, CompositorState},
dmabuf::{self, DmabufGlobal, DmabufHandler, DmabufState}, dmabuf::{self, DmabufGlobal, DmabufHandler, DmabufState, ImportError},
shell::kde::decoration::KdeDecorationState, shell::kde::decoration::KdeDecorationState,
shm::{ShmHandler, ShmState}, shm::{ShmHandler, ShmState},
}, },
}; };
use std::sync::{Arc, Weak}; use std::sync::{Arc, Weak};
use tokio::sync::mpsc::UnboundedSender;
use tracing::info; use tracing::info;
#[derive(Default)] #[derive(Default)]
@@ -58,8 +59,9 @@ pub struct WaylandState {
// pub xdg_activation_state: XdgActivationState, // pub xdg_activation_state: XdgActivationState,
pub kde_decoration_state: KdeDecorationState, pub kde_decoration_state: KdeDecorationState,
pub shm_state: ShmState, pub shm_state: ShmState,
pub dmabuf_state: DmabufState, dmabuf_state: DmabufState,
pub dmabuf_global: DmabufGlobal, _dmabuf_global: DmabufGlobal,
dmabuf_tx: UnboundedSender<Dmabuf>,
pub output: Output, pub output: Output,
pub seats: FxHashMap<ClientId, Arc<SeatData>>, pub seats: FxHashMap<ClientId, Arc<SeatData>>,
} }
@@ -69,6 +71,7 @@ impl WaylandState {
display: Arc<Mutex<Display<WaylandState>>>, display: Arc<Mutex<Display<WaylandState>>>,
display_handle: DisplayHandle, display_handle: DisplayHandle,
renderer: &GlesRenderer, renderer: &GlesRenderer,
dmabuf_tx: UnboundedSender<Dmabuf>,
) -> Arc<Mutex<Self>> { ) -> Arc<Mutex<Self>> {
let compositor_state = CompositorState::new::<Self>(&display_handle); let compositor_state = CompositorState::new::<Self>(&display_handle);
// let xdg_activation_state = XdgActivationState::new::<Self, _>(&display_handle); // let xdg_activation_state = XdgActivationState::new::<Self, _>(&display_handle);
@@ -118,7 +121,8 @@ impl WaylandState {
kde_decoration_state, kde_decoration_state,
shm_state, shm_state,
dmabuf_state, dmabuf_state,
dmabuf_global, _dmabuf_global: dmabuf_global,
dmabuf_tx,
output, output,
seats: FxHashMap::default(), seats: FxHashMap::default(),
}) })
@@ -151,9 +155,9 @@ impl DmabufHandler for WaylandState {
fn dmabuf_imported( fn dmabuf_imported(
&mut self, &mut self,
_global: &DmabufGlobal, _global: &DmabufGlobal,
_dmabuf: Dmabuf, dmabuf: Dmabuf,
) -> Result<(), dmabuf::ImportError> { ) -> Result<(), dmabuf::ImportError> {
Ok(()) self.dmabuf_tx.send(dmabuf).map_err(|_| ImportError::Failed)
} }
} }
delegate_dmabuf!(WaylandState); delegate_dmabuf!(WaylandState);