feat: wl_drm

This commit is contained in:
Nova
2025-07-30 23:24:39 -07:00
parent 72c5312c5e
commit f4c75c5705
12 changed files with 251 additions and 97 deletions

View File

@@ -17,10 +17,8 @@ use waynest::server::protocol::stable::linux_dmabuf_v1::zwp_linux_buffer_params_
/// Parameters for a shared memory buffer
pub struct DmabufBacking {
params: Arc<BufferParams>,
size: Vector2<u32>,
format: DrmFourcc,
_flags: Flags,
tex: OnceLock<Handle<Image>>,
pending_imported_dmatex: Mutex<Option<ImportedTexture>>,
}
@@ -28,10 +26,8 @@ pub struct DmabufBacking {
impl std::fmt::Debug for DmabufBacking {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DmabufBacking")
.field("params", &self.params)
.field("size", &self.size)
.field("format", &self.format)
.field("_flags", &self._flags)
.field("tex", &self.tex)
.finish()
}
@@ -39,7 +35,23 @@ impl std::fmt::Debug for DmabufBacking {
impl DmabufBacking {
#[tracing::instrument(level = "debug", skip_all)]
pub fn new(
pub fn new(dmatex: Dmatex) -> Result<Self, ImportError> {
let dev = RENDER_DEVICE.wait();
Ok(Self {
size: [dmatex.res.x, dmatex.res.y].into(),
format: DrmFourcc::try_from(dmatex.format).unwrap(),
tex: OnceLock::new(),
pending_imported_dmatex: Mutex::new(Some(import_texture(
dev,
dmatex,
DropCallback(None),
)?)),
})
}
#[tracing::instrument(level = "debug", skip_all)]
pub fn from_params(
params: Arc<BufferParams>,
size: Vector2<u32>,
format: DrmFourcc,
@@ -60,17 +72,8 @@ impl DmabufBacking {
flip_y: flags.contains(Flags::YInvert),
srgb: true,
};
let dev = RENDER_DEVICE.wait();
let imported_tex = import_texture(dev, dmatex, DropCallback(None))?;
Ok(Self {
params,
size,
format,
_flags: flags,
tex: OnceLock::new(),
pending_imported_dmatex: Mutex::new(Some(imported_tex)),
})
DmabufBacking::new(dmatex)
}
#[tracing::instrument(level = "debug", skip_all)]

View File

@@ -104,7 +104,7 @@ impl ZwpLinuxBufferParamsV1 for BufferParams {
tracing::info!("Creating buffer from BufferParams {:?}", self.id);
// Create the buffer with DMA-BUF backing using self as the backing
let size = [width as u32, height as u32].into();
let buffer = DmabufBacking::new(
let buffer = DmabufBacking::from_params(
client.get::<Self>(self.id).unwrap(),
size,
DrmFourcc::try_from(format).unwrap(),
@@ -138,7 +138,7 @@ impl ZwpLinuxBufferParamsV1 for BufferParams {
) -> Result<()> {
// TODO: terminate client on fail, or send a fail event or something
// Create the buffer with DMA-BUF backing using self as the backing
_ = DmabufBacking::new(
_ = DmabufBacking::from_params(
client.get::<Self>(self.id).unwrap(),
[width as u32, height as u32].into(),
DrmFourcc::try_from(format).unwrap(),

View File

@@ -2,6 +2,8 @@ pub mod buffer_backing;
pub mod buffer_params;
pub mod feedback;
use std::sync::LazyLock;
use super::{util::ClientExt, vulkano_data::VULKANO_CONTEXT};
use crate::core::registry::Registry;
use bevy_dmabuf::{format_mapping::drm_fourcc_to_vk_format, wgpu_init::vulkan_to_wgpu};
@@ -20,6 +22,30 @@ use waynest::{
wire::ObjectId,
};
pub static DMABUF_FORMATS: LazyLock<FxHashSet<(DrmFourcc, u64)>> = LazyLock::new(|| {
let vk = VULKANO_CONTEXT.wait();
ALL_DRM_FOURCCS
.iter()
.copied()
.filter_map(|f| Some((f, drm_fourcc_to_vk_format(f)?)))
.filter(|(_, vk_format)| vulkan_to_wgpu(*vk_format).is_some())
.filter_map(|(f, vk_format)| {
Some((
f,
vk.phys_dev
.format_properties(vk_format.try_into().unwrap())
.ok()?
.drm_format_modifier_properties
.into_iter()
.map(|v| v.drm_format_modifier)
.collect::<Vec<_>>(),
))
})
.flat_map(|(f, mods)| mods.into_iter().map(move |modifier| (f, modifier)))
.collect()
});
/// Main DMA-BUF interface implementation
///
/// This interface allows clients to create wl_buffers from DMA-BUFs.
@@ -45,31 +71,10 @@ pub struct Dmabuf {
impl Dmabuf {
/// Create a new DMA-BUF interface instance
pub async fn new(client: &mut Client, id: ObjectId, version: u32) -> Result<Self> {
let vk = VULKANO_CONTEXT.wait();
let formats: FxHashSet<(DrmFourcc, u64)> = ALL_DRM_FOURCCS
.iter()
.copied()
.filter_map(|f| Some((f, drm_fourcc_to_vk_format(f)?)))
.filter(|(_, vk_format)| vulkan_to_wgpu(*vk_format).is_some())
.filter_map(|(f, vk_format)| {
Some((
f,
vk.phys_dev
.format_properties(vk_format.try_into().unwrap())
.ok()?
.drm_format_modifier_properties
.into_iter()
.map(|v| v.drm_format_modifier)
.collect::<Vec<_>>(),
))
})
.flat_map(|(f, mods)| mods.into_iter().map(move |modifier| (f, modifier)))
.collect();
let dmabuf = Self {
active_params: Registry::new(),
version,
formats,
formats: DMABUF_FORMATS.clone(),
};
if version < 3 {