From 3d8cf0e4587fc5e7daf80e75dda36d9212cee8b4 Mon Sep 17 00:00:00 2001 From: Victor Brekenfeld Date: Mon, 26 Apr 2021 18:08:51 +0200 Subject: [PATCH] renderer: Add importing bitmaps as textures --- src/backend/drm/render.rs | 5 ++++ src/backend/renderer/gles2/mod.rs | 38 ++++++++++++++++++++++++------- src/backend/renderer/mod.rs | 2 ++ src/backend/winit.rs | 5 ++++ 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/backend/drm/render.rs b/src/backend/drm/render.rs index 954c877..e58e381 100644 --- a/src/backend/drm/render.rs +++ b/src/backend/drm/render.rs @@ -231,6 +231,11 @@ where type Error = Error; type Texture = T; + #[cfg(feature = "image")] + fn import_bitmap>(&mut self, image: &image::ImageBuffer, C>) -> Result { + self.renderer.import_bitmap(image).map_err(Error::RenderError) + } + #[cfg(feature = "wayland_frontend")] fn shm_formats(&self) -> &[wl_shm::Format] { self.renderer.shm_formats() diff --git a/src/backend/renderer/gles2/mod.rs b/src/backend/renderer/gles2/mod.rs index e910677..e448a88 100644 --- a/src/backend/renderer/gles2/mod.rs +++ b/src/backend/renderer/gles2/mod.rs @@ -437,6 +437,34 @@ impl Renderer for Gles2Renderer { wl_shm::Format::Xrgb8888, ] } + + #[cfg(feature = "image")] + fn import_bitmap>(&mut self, image: &image::ImageBuffer, C>) -> Result { + self.make_current()?; + + let mut tex = 0; + unsafe { + self.gl.GenTextures(1, &mut tex); + self.gl.BindTexture(ffi::TEXTURE_2D, tex); + self.gl.TexParameteri(ffi::TEXTURE_2D, ffi::TEXTURE_WRAP_S, ffi::CLAMP_TO_EDGE as i32); + self.gl.TexParameteri(ffi::TEXTURE_2D, ffi::TEXTURE_WRAP_T, ffi::CLAMP_TO_EDGE as i32); + self.gl.TexImage2D(ffi::TEXTURE_2D, 0, ffi::RGBA as i32, image.width() as i32, image.height() as i32, 0, ffi::RGBA, ffi::UNSIGNED_BYTE as u32, image.as_ptr() as *const _); + self.gl.BindTexture(ffi::TEXTURE_2D, 0); + } + + let texture = Gles2Texture { + texture: tex, + texture_kind: 2, + is_external: false, + y_inverted: false, + width: image.width(), + height: image.height(), + }; + self.egl.unbind()?; + + Ok(texture) + } + #[cfg(feature = "wayland_frontend")] fn import_shm(&mut self, buffer: &wl_buffer::WlBuffer) -> Result { @@ -502,13 +530,6 @@ impl Renderer for Gles2Renderer { self.gl.BlendFunc(ffi::ONE, ffi::ONE_MINUS_SRC_ALPHA); } - // output transformation passed in by the user - let mut projection = Matrix3::::identity(); - //projection = projection * Matrix3::from_translation(Vector2::new(width as f32 / 2.0, height as f32 / 2.0)); - projection = projection * transform.matrix(); - //let (transformed_width, transformed_height) = transform.transform_size(width, height); - //projection = projection * Matrix3::from_translation(Vector2::new(-(transformed_width as f32) / 2.0, -(transformed_height as f32) / 2.0)); - // replicate https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glOrtho.xml // glOrtho(0, width, 0, height, 1, 1); let mut renderer = Matrix3::::identity(); @@ -526,7 +547,8 @@ impl Renderer for Gles2Renderer { renderer[2][0] = -(1.0f32.copysign(renderer[0][0] + renderer[1][0])); renderer[2][1] = -(1.0f32.copysign(renderer[0][1] + renderer[1][1])); - self.current_projection = Some(projection * renderer); + // output transformation passed in by the user + self.current_projection = Some(transform.matrix() * renderer); Ok(()) } diff --git a/src/backend/renderer/mod.rs b/src/backend/renderer/mod.rs index 35de5e8..72dab5b 100644 --- a/src/backend/renderer/mod.rs +++ b/src/backend/renderer/mod.rs @@ -134,6 +134,8 @@ pub trait Renderer { type Error: Error; type Texture: Texture; + #[cfg(feature = "image")] + fn import_bitmap>(&mut self, image: &image::ImageBuffer, C>) -> Result; #[cfg(feature = "wayland_frontend")] fn shm_formats(&self) -> &[wl_shm::Format] { // Mandatory diff --git a/src/backend/winit.rs b/src/backend/winit.rs index b1450ad..6e31082 100644 --- a/src/backend/winit.rs +++ b/src/backend/winit.rs @@ -260,6 +260,11 @@ impl Renderer for WinitGraphicsBackend { type Error = Gles2Error; type Texture = Gles2Texture; + #[cfg(feature = "image")] + fn import_bitmap>(&mut self, image: &image::ImageBuffer, C>) -> Result { + self.renderer.import_bitmap(image) + } + #[cfg(feature = "wayland_frontend")] fn shm_formats(&self) -> &[wl_shm::Format] { Renderer::shm_formats(&self.renderer)