From c96802490577f8e45c8fe30bab8d554bcd0b762d Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Sat, 7 Sep 2024 20:22:30 +0200 Subject: [PATCH] gfx: add ShmGfxTexture --- src/cursor.rs | 3 ++- src/gfx_api.rs | 8 ++++++-- src/gfx_apis/gl/renderer/context.rs | 8 ++++---- src/gfx_apis/gl/renderer/texture.rs | 8 +++++++- src/gfx_apis/vulkan.rs | 8 ++++---- src/gfx_apis/vulkan/image.rs | 10 +++++++++- src/ifs/wl_buffer.rs | 2 +- src/ifs/wl_surface.rs | 5 +++-- src/it/test_gfx_api.rs | 13 ++++++++++--- src/text.rs | 8 +++++--- 10 files changed, 51 insertions(+), 22 deletions(-) diff --git a/src/cursor.rs b/src/cursor.rs index 6d8e1d58..8f8d1775 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -328,7 +328,8 @@ impl CursorImageScaled { extents: Rect::new_sized(-xhot, -yhot, width, height).unwrap(), tex: ctx .clone() - .shmem_texture(None, data, ARGB8888, width, height, width * 4, None)?, + .shmem_texture(None, data, ARGB8888, width, height, width * 4, None)? + .into_texture(), })) } } diff --git a/src/gfx_api.rs b/src/gfx_api.rs index d8100264..659821bf 100644 --- a/src/gfx_api.rs +++ b/src/gfx_api.rs @@ -531,6 +531,10 @@ pub trait GfxTexture: Debug { fn format(&self) -> &'static Format; } +pub trait ShmGfxTexture: GfxTexture { + fn into_texture(self: Rc) -> Rc; +} + pub trait GfxContext: Debug { fn reset_status(&self) -> Option; @@ -546,14 +550,14 @@ pub trait GfxContext: Debug { fn shmem_texture( self: Rc, - old: Option>, + old: Option>, data: &[Cell], format: &'static Format, width: i32, height: i32, stride: i32, damage: Option<&[Rect]>, - ) -> Result, GfxError>; + ) -> Result, GfxError>; fn allocator(&self) -> Rc; diff --git a/src/gfx_apis/gl/renderer/context.rs b/src/gfx_apis/gl/renderer/context.rs index 4d9568af..1948ac16 100644 --- a/src/gfx_apis/gl/renderer/context.rs +++ b/src/gfx_apis/gl/renderer/context.rs @@ -4,7 +4,7 @@ use { format::{Format, XRGB8888}, gfx_api::{ BufferResvUser, GfxApiOpt, GfxContext, GfxError, GfxFormat, GfxFramebuffer, GfxImage, - GfxTexture, ResetStatus, + ResetStatus, ShmGfxTexture, }, gfx_apis::gl::{ egl::{context::EglContext, display::EglDisplay, image::EglImage}, @@ -265,17 +265,17 @@ impl GfxContext for GlRenderContext { fn shmem_texture( self: Rc, - _old: Option>, + _old: Option>, data: &[Cell], format: &'static Format, width: i32, height: i32, stride: i32, _damage: Option<&[Rect]>, - ) -> Result, GfxError> { + ) -> Result, GfxError> { (&self) .shmem_texture(data, format, width, height, stride) - .map(|w| w as Rc) + .map(|w| w as Rc) .map_err(|e| e.into()) } diff --git a/src/gfx_apis/gl/renderer/texture.rs b/src/gfx_apis/gl/renderer/texture.rs index 003fe6be..e4a97575 100644 --- a/src/gfx_apis/gl/renderer/texture.rs +++ b/src/gfx_apis/gl/renderer/texture.rs @@ -1,7 +1,7 @@ use { crate::{ format::Format, - gfx_api::{GfxError, GfxTexture}, + gfx_api::{GfxError, GfxTexture, ShmGfxTexture}, gfx_apis::gl::{ gl::texture::GlTexture, renderer::{context::GlRenderContext, framebuffer::Framebuffer}, @@ -82,3 +82,9 @@ impl GfxTexture for Texture { self.format } } + +impl ShmGfxTexture for Texture { + fn into_texture(self: Rc) -> Rc { + self + } +} diff --git a/src/gfx_apis/vulkan.rs b/src/gfx_apis/vulkan.rs index 49f3dac0..09827d4e 100644 --- a/src/gfx_apis/vulkan.rs +++ b/src/gfx_apis/vulkan.rs @@ -21,7 +21,7 @@ use { async_engine::AsyncEngine, format::Format, gfx_api::{ - GfxContext, GfxError, GfxFormat, GfxFramebuffer, GfxImage, GfxTexture, ResetStatus, + GfxContext, GfxError, GfxFormat, GfxFramebuffer, GfxImage, ResetStatus, ShmGfxTexture, }, gfx_apis::vulkan::{ image::VulkanImageMemory, instance::VulkanInstance, renderer::VulkanRenderer, @@ -249,16 +249,16 @@ impl GfxContext for Context { fn shmem_texture( self: Rc, - old: Option>, + old: Option>, data: &[Cell], format: &'static Format, width: i32, height: i32, stride: i32, damage: Option<&[Rect]>, - ) -> Result, GfxError> { + ) -> Result, GfxError> { if let Some(old) = old { - let old = old.into_vk(&self.0.device.device); + let old = old.into_texture().into_vk(&self.0.device.device); let shm = match &old.ty { VulkanImageMemory::DmaBuf(_) => unreachable!(), VulkanImageMemory::Internal(shm) => shm, diff --git a/src/gfx_apis/vulkan/image.rs b/src/gfx_apis/vulkan/image.rs index ee0555a5..29469a79 100644 --- a/src/gfx_apis/vulkan/image.rs +++ b/src/gfx_apis/vulkan/image.rs @@ -1,7 +1,9 @@ use { crate::{ format::Format, - gfx_api::{GfxApiOpt, GfxError, GfxFramebuffer, GfxImage, GfxTexture, SyncFile}, + gfx_api::{ + GfxApiOpt, GfxError, GfxFramebuffer, GfxImage, GfxTexture, ShmGfxTexture, SyncFile, + }, gfx_apis::vulkan::{ allocator::VulkanAllocation, device::VulkanDevice, format::VulkanModifierLimits, renderer::VulkanRenderer, shm_image::VulkanShmImage, VulkanError, @@ -530,3 +532,9 @@ impl GfxTexture for VulkanImage { self.format } } + +impl ShmGfxTexture for VulkanImage { + fn into_texture(self: Rc) -> Rc { + self + } +} diff --git a/src/ifs/wl_buffer.rs b/src/ifs/wl_buffer.rs index ffe03415..268f72a8 100644 --- a/src/ifs/wl_buffer.rs +++ b/src/ifs/wl_buffer.rs @@ -201,7 +201,7 @@ impl WlBuffer { match &*self.storage.borrow() { None => None, Some(s) => match s { - WlBufferStorage::Shm { .. } => surface.shm_texture.get(), + WlBufferStorage::Shm { .. } => surface.shm_texture.get().map(|t| t.into_texture()), WlBufferStorage::Dmabuf { tex, .. } => tex.clone(), }, } diff --git a/src/ifs/wl_surface.rs b/src/ifs/wl_surface.rs index d04b82b2..48422742 100644 --- a/src/ifs/wl_surface.rs +++ b/src/ifs/wl_surface.rs @@ -23,7 +23,8 @@ use { drm_feedback::DrmFeedback, fixed::Fixed, gfx_api::{ - AcquireSync, BufferResv, BufferResvUser, GfxTexture, ReleaseSync, SampleRect, SyncFile, + AcquireSync, BufferResv, BufferResvUser, ReleaseSync, SampleRect, ShmGfxTexture, + SyncFile, }, ifs::{ wl_buffer::WlBuffer, @@ -271,7 +272,7 @@ pub struct WlSurface { pub buffer: CloneCell>>, buffer_presented: Cell, buffer_had_frame_request: Cell, - pub shm_texture: CloneCell>>, + pub shm_texture: CloneCell>>, pub buf_x: NumCell, pub buf_y: NumCell, pub children: RefCell>>, diff --git a/src/it/test_gfx_api.rs b/src/it/test_gfx_api.rs index 19f55018..ef37188c 100644 --- a/src/it/test_gfx_api.rs +++ b/src/it/test_gfx_api.rs @@ -4,7 +4,8 @@ use { format::{Format, ARGB8888, XRGB8888}, gfx_api::{ CopyTexture, FillRect, FramebufferRect, GfxApiOpt, GfxContext, GfxError, GfxFormat, - GfxFramebuffer, GfxImage, GfxTexture, GfxWriteModifier, ResetStatus, SyncFile, + GfxFramebuffer, GfxImage, GfxTexture, GfxWriteModifier, ResetStatus, ShmGfxTexture, + SyncFile, }, rect::Rect, theme::Color, @@ -108,14 +109,14 @@ impl GfxContext for TestGfxCtx { fn shmem_texture( self: Rc, - _old: Option>, + _old: Option>, data: &[Cell], format: &'static Format, width: i32, height: i32, stride: i32, _damage: Option<&[Rect]>, - ) -> Result, GfxError> { + ) -> Result, GfxError> { assert!(stride >= width * 4); let size = (stride * height) as usize; assert!(data.len() >= size); @@ -301,6 +302,12 @@ impl GfxTexture for TestGfxImage { } } +impl ShmGfxTexture for TestGfxImage { + fn into_texture(self: Rc) -> Rc { + self + } +} + impl GfxImage for TestGfxImage { fn to_framebuffer(self: Rc) -> Result, GfxError> { Ok(Rc::new(TestGfxFb { diff --git a/src/text.rs b/src/text.rs index 77dfbacf..104e8776 100644 --- a/src/text.rs +++ b/src/text.rs @@ -1,7 +1,7 @@ use { crate::{ format::ARGB8888, - gfx_api::{GfxContext, GfxError, GfxTexture}, + gfx_api::{GfxContext, GfxError, GfxTexture, ShmGfxTexture}, pango::{ consts::{ CAIRO_FORMAT_ARGB32, CAIRO_OPERATOR_SOURCE, PANGO_ELLIPSIZE_END, PANGO_SCALE, @@ -73,6 +73,7 @@ impl<'a> Config<'a> { #[derive(Clone)] pub struct TextTexture { config: Rc>, + shm_texture: Rc, pub texture: Rc, } @@ -210,7 +211,7 @@ fn render2( Ok(d) => d, Err(e) => return Err(TextError::ImageData(e)), }; - let old = old.map(|o| o.texture); + let old = old.map(|o| o.shm_texture); match ctx.clone().shmem_texture( old, bytes, @@ -222,7 +223,8 @@ fn render2( ) { Ok(t) => Ok(TextTexture { config: Rc::new(config.to_static()), - texture: t, + texture: t.clone().into_texture(), + shm_texture: t, }), Err(e) => Err(TextError::RenderError(e)), }