mod frame;
mod gles;
mod vulkan;
pub use frame::{DRM_FORMAT_MOD_INVALID, DmaBufFormat, DmaBufFrame, DmaBufLease, DmaBufPlane};
use gles::GlesInterop;
use vulkan::ImportedVulkanImage;
#[derive(Debug)]
enum Backend {
Vulkan,
Gles(Box<GlesInterop>),
}
fn create_backend(backend: wgpu::Backend) -> Backend {
match backend {
wgpu::Backend::Vulkan => Backend::Vulkan,
wgpu::Backend::Gl => Backend::Gles(Box::new(GlesInterop::new())),
backend => {
panic!("DMA-BUF import requires a Vulkan or EGL/GLES wgpu device, received {backend:?}")
}
}
}
#[derive(Debug)]
pub struct DmaBufImportGuard {
#[expect(
dead_code,
reason = "the import is held only so that dropping the guard destroys it, which dead-code analysis does not count as a read"
)]
imported: Option<ImportedVulkanImage>,
}
#[derive(Debug)]
pub struct DmaBufImport {
pub encoder: wgpu::CommandEncoder,
pub guard: DmaBufImportGuard,
}
#[derive(Debug)]
pub struct DmaBufImporter {
device: wgpu::Device,
queue: wgpu::Queue,
backend: Backend,
}
impl DmaBufImporter {
#[must_use]
pub fn new(device: &wgpu::Device, queue: &wgpu::Queue, adapter: &wgpu::Adapter) -> Self {
Self {
device: device.clone(),
queue: queue.clone(),
backend: create_backend(adapter.get_info().backend),
}
}
#[must_use]
pub fn copy_to_texture(&self, mut frame: DmaBufFrame) -> wgpu::Texture {
assert!(
frame.is_render_ready(),
"a DMA-BUF must be ready before a synchronous GPU copy"
);
let (visible_width, visible_height) = frame.visible_size();
let size = wgpu::Extent3d {
width: visible_width,
height: visible_height,
depth_or_array_layers: 1,
};
let destination = self.device.create_texture(&wgpu::TextureDescriptor {
label: Some("wgpu_external_frame_owned_dma_buf"),
size,
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: frame.format.texture_format(),
usage: wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: &[],
});
match &self.backend {
Backend::Vulkan => {
let mut encoder =
self.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("wgpu_external_frame_owned_dma_buf_copy"),
});
encoder.clear_texture(&destination, &wgpu::ImageSubresourceRange::default());
let imported = vulkan::import_dma_buf(&self.device, &mut frame);
imported.record_copy(&mut encoder, &destination, visible_width, visible_height);
frame.presented();
let submission = self.queue.submit([encoder.finish()]);
self.device
.poll(wgpu::PollType::Wait {
submission_index: Some(submission),
timeout: None,
})
.expect("the Vulkan DMA-BUF copy failed");
drop(imported);
}
Backend::Gles(gles) => {
gles.copy_dma_buf(&frame, &destination);
gles.finish();
frame.presented();
}
}
frame.release(None);
destination
}
#[must_use]
pub fn copy_into(&self, frame: &mut DmaBufFrame, destination: &wgpu::Texture) -> DmaBufImport {
let (visible_width, visible_height) = frame.visible_size();
let mut encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("wgpu_external_frame_dma_buf_copy"),
});
encoder.clear_texture(destination, &wgpu::ImageSubresourceRange::default());
let imported = match &self.backend {
Backend::Vulkan => {
let imported = vulkan::import_dma_buf(&self.device, frame);
imported.record_copy(&mut encoder, destination, visible_width, visible_height);
Some(imported)
}
Backend::Gles(gles) => {
self.queue.submit([encoder.finish()]);
gles.copy_dma_buf(frame, destination);
encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("wgpu_external_frame_dma_buf_follow_up"),
});
None
}
};
DmaBufImport {
encoder,
guard: DmaBufImportGuard { imported },
}
}
}