use { crate::{ format::Format, gfx_api::{GfxError, GfxTexture, ShmGfxTexture}, gfx_apis::gl::{ gl::texture::GlTexture, renderer::{context::GlRenderContext, framebuffer::Framebuffer}, RenderError, }, video::dmabuf::DmaBuf, }, std::{ any::Any, cell::Cell, fmt::{Debug, Formatter}, rc::Rc, }, }; pub struct Texture { pub(in crate::gfx_apis::gl) ctx: Rc, pub(in crate::gfx_apis::gl) gl: GlTexture, pub(in crate::gfx_apis::gl) format: &'static Format, } impl Debug for Texture { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.debug_struct("Texture").finish_non_exhaustive() } } impl Texture { pub fn width(&self) -> i32 { self.gl.width } pub fn height(&self) -> i32 { self.gl.height } pub fn to_framebuffer(&self) -> Result, RenderError> { match &self.gl.img { Some(img) => self.ctx.image_to_fb(img), _ => Err(RenderError::ShmTextureToFb), } } } impl GfxTexture for Texture { fn size(&self) -> (i32, i32) { (self.width(), self.height()) } fn as_any(&self) -> &dyn Any { self } fn into_any(self: Rc) -> Rc { self } fn read_pixels( self: Rc, x: i32, y: i32, width: i32, height: i32, _stride: i32, format: &Format, shm: &[Cell], ) -> Result<(), GfxError> { self.to_framebuffer()? .copy_to_shm(x, y, width, height, format, shm) .map_err(|e| e.into()) } fn dmabuf(&self) -> Option<&DmaBuf> { self.gl.img.as_ref().map(|i| &i.dmabuf) } fn format(&self) -> &'static Format { self.format } } impl ShmGfxTexture for Texture { fn into_texture(self: Rc) -> Rc { self } }