1
0
Fork 0
forked from wry/wry

render: store underlying DmaBufs in textures

This commit is contained in:
Julian Orth 2024-02-18 15:23:10 +01:00
parent 1ac4f3dc52
commit 3635ae0104
10 changed files with 38 additions and 35 deletions

View file

@ -267,6 +267,7 @@ pub trait GfxTexture: Debug {
format: &'static Format,
shm: &[Cell<u8>],
) -> Result<(), GfxError>;
fn dmabuf(&self) -> Option<&DmaBuf>;
}
pub trait GfxContext: Debug {

View file

@ -259,10 +259,8 @@ impl EglDisplay {
Ok(Rc::new(EglImage {
dpy: self.clone(),
img,
width: buf.width,
height: buf.height,
external_only: format.external_only,
format: buf.format,
dmabuf: buf.clone(),
}))
}
}

View file

@ -1,11 +1,11 @@
use {
crate::{
format::Format,
gfx_apis::gl::egl::{
display::EglDisplay,
sys::{EGLImageKHR, EGL_FALSE},
PROCS,
},
video::dmabuf::DmaBuf,
},
std::rc::Rc,
};
@ -13,10 +13,8 @@ use {
pub struct EglImage {
pub dpy: Rc<EglDisplay>,
pub img: EGLImageKHR,
pub width: i32,
pub height: i32,
pub external_only: bool,
pub format: &'static Format,
pub dmabuf: DmaBuf,
}
impl Drop for EglImage {

View file

@ -60,8 +60,8 @@ impl GlRenderBuffer {
_tex: None,
ctx: self.ctx.clone(),
fbo,
width: self.img.width,
height: self.img.height,
width: self.img.dmabuf.width,
height: self.img.dmabuf.height,
};
if status != GL_FRAMEBUFFER_COMPLETE {
return Err(RenderError::CreateFramebuffer);

View file

@ -56,10 +56,10 @@ impl GlTexture {
ctx: ctx.clone(),
img: Some(img.clone()),
tex,
width: img.width,
height: img.height,
width: img.dmabuf.width,
height: img.dmabuf.height,
external_only: img.external_only,
format: img.format,
format: img.dmabuf.format,
})
}

View file

@ -120,6 +120,6 @@ impl GfxFramebuffer for Framebuffer {
}
fn format(&self) -> &'static Format {
self.gl.rb.img.format
self.gl.rb.img.dmabuf.format
}
}

View file

@ -17,11 +17,11 @@ pub struct Image {
impl Image {
pub fn width(&self) -> i32 {
self.gl.width
self.gl.dmabuf.width
}
pub fn height(&self) -> i32 {
self.gl.height
self.gl.dmabuf.height
}
fn to_texture(self: &Rc<Self>) -> Result<Rc<Texture>, RenderError> {

View file

@ -3,6 +3,7 @@ use {
format::Format,
gfx_api::{GfxError, GfxTexture},
gfx_apis::gl::{gl::texture::GlTexture, renderer::context::GlRenderContext, RenderError},
video::dmabuf::DmaBuf,
},
std::{
any::Any,
@ -58,4 +59,8 @@ impl GfxTexture for Texture {
) -> Result<(), GfxError> {
Err(RenderError::UnsupportedOperation.into())
}
fn dmabuf(&self) -> Option<&DmaBuf> {
self.gl.img.as_ref().map(|i| &i.dmabuf)
}
}

View file

@ -8,10 +8,7 @@ use {
},
theme::Color,
utils::clonecell::CloneCell,
video::{
dmabuf::{DmaBuf, DmaBufPlane, PlaneVec},
Modifier,
},
video::dmabuf::{DmaBuf, PlaneVec},
},
ash::vk::{
BindImageMemoryInfo, BindImagePlaneMemoryInfo, ComponentMapping, ComponentSwizzle,
@ -36,12 +33,10 @@ use {
pub struct VulkanDmaBufImageTemplate {
pub(super) renderer: Rc<VulkanRenderer>,
pub(super) format: &'static Format,
pub(super) width: u32,
pub(super) height: u32,
pub(super) modifier: Modifier,
pub(super) disjoint: bool,
pub(super) planes: PlaneVec<DmaBufPlane>,
pub(super) dmabuf: DmaBuf,
pub(super) render_max_extents: Option<VulkanMaxExtents>,
pub(super) texture_max_extents: Option<VulkanMaxExtents>,
}
@ -260,12 +255,10 @@ impl VulkanRenderer {
}
Ok(Rc::new(VulkanDmaBufImageTemplate {
renderer: self.clone(),
format: dmabuf.format,
width,
height,
modifier: dmabuf.modifier,
disjoint,
planes: dmabuf.planes.clone(),
dmabuf: dmabuf.clone(),
render_max_extents: modifier.render_max_extents,
texture_max_extents: modifier.texture_max_extents,
}))
@ -332,6 +325,7 @@ impl VulkanDmaBufImageTemplate {
}
let image = {
let plane_layouts: PlaneVec<_> = self
.dmabuf
.planes
.iter()
.map(|p| SubresourceLayout {
@ -343,7 +337,7 @@ impl VulkanDmaBufImageTemplate {
})
.collect();
let mut mod_info = ImageDrmFormatModifierExplicitCreateInfoEXT::builder()
.drm_format_modifier(self.modifier)
.drm_format_modifier(self.dmabuf.modifier)
.plane_layouts(&plane_layouts)
.build();
let mut memory_image_create_info = ExternalMemoryImageCreateInfo::builder()
@ -361,7 +355,7 @@ impl VulkanDmaBufImageTemplate {
};
let create_info = ImageCreateInfo::builder()
.image_type(ImageType::TYPE_2D)
.format(self.format.vk_format)
.format(self.dmabuf.format.vk_format)
.mip_levels(1)
.array_layers(1)
.tiling(ImageTiling::DRM_FORMAT_MODIFIER_EXT)
@ -383,14 +377,14 @@ impl VulkanDmaBufImageTemplate {
};
let destroy_image = OnDrop(|| unsafe { device.device.destroy_image(image, None) });
let num_device_memories = match self.disjoint {
true => self.planes.len(),
true => self.dmabuf.planes.len(),
false => 1,
};
let mut device_memories = PlaneVec::new();
let mut free_device_memories = PlaneVec::new();
let mut bind_image_plane_memory_infos = PlaneVec::new();
for plane_idx in 0..num_device_memories {
let dma_buf_plane = &self.planes[plane_idx];
let dma_buf_plane = &self.dmabuf.planes[plane_idx];
let memory_fd_properties = unsafe {
device.external_memory_fd.get_memory_fd_properties(
ExternalMemoryHandleTypeFlags::DMA_BUF_EXT,
@ -467,8 +461,8 @@ impl VulkanDmaBufImageTemplate {
}
let res = unsafe { device.device.bind_image_memory2(&bind_image_memory_infos) };
res.map_err(VulkanError::BindImageMemory)?;
let texture_view = device.create_image_view(image, self.format, false)?;
let render_view = device.create_image_view(image, self.format, true)?;
let texture_view = device.create_image_view(image, self.dmabuf.format, false)?;
let render_view = device.create_image_view(image, self.dmabuf.format, true)?;
free_device_memories.drain(..).for_each(mem::forget);
mem::forget(destroy_image);
Ok(Rc::new(VulkanImage {
@ -484,7 +478,7 @@ impl VulkanDmaBufImageTemplate {
template: self.clone(),
mems: device_memories,
}),
format: self.format,
format: self.dmabuf.format,
is_undefined: Cell::new(true),
}))
}
@ -579,4 +573,11 @@ impl GfxTexture for VulkanImage {
.read_pixels(&self, x, y, width, height, stride, format, shm)
.map_err(|e| e.into())
}
fn dmabuf(&self) -> Option<&DmaBuf> {
match &self.ty {
VulkanImageMemory::DmaBuf(b) => Some(&b.template.dmabuf),
VulkanImageMemory::Internal(_) => None,
}
}
}

View file

@ -532,7 +532,7 @@ impl VulkanRenderer {
flag: u32|
-> Result<(), VulkanError> {
if let VulkanImageMemory::DmaBuf(buf) = &img.ty {
for plane in &buf.template.planes {
for plane in &buf.template.dmabuf.planes {
let fd = dma_buf_export_sync_file(&plane.fd, flag)
.map_err(VulkanError::IoctlExportSyncFile)?;
let semaphore = self.allocate_semaphore()?;
@ -573,7 +573,7 @@ impl VulkanRenderer {
};
let import = |img: &VulkanImage, flag: u32| {
if let VulkanImageMemory::DmaBuf(buf) = &img.ty {
for plane in &buf.template.planes {
for plane in &buf.template.dmabuf.planes {
let res = dma_buf_import_sync_file(&plane.fd, flag, &syncfile)
.map_err(VulkanError::IoctlImportSyncFile);
if let Err(e) = res {
@ -764,7 +764,7 @@ impl VulkanRenderer {
let mut semaphores = vec![];
let mut semaphore_infos = vec![];
if let VulkanImageMemory::DmaBuf(buf) = &tex.ty {
for plane in &buf.template.planes {
for plane in &buf.template.dmabuf.planes {
let fd = dma_buf_export_sync_file(&plane.fd, DMA_BUF_SYNC_READ)
.map_err(VulkanError::IoctlExportSyncFile)?;
let semaphore = self.allocate_semaphore()?;