renderer: Add importing bitmaps as textures

This commit is contained in:
Victor Brekenfeld 2021-04-26 18:08:51 +02:00
parent 43d3e9cd14
commit 3d8cf0e458
4 changed files with 42 additions and 8 deletions

View File

@ -231,6 +231,11 @@ where
type Error = Error<E1, E2, E3>; type Error = Error<E1, E2, E3>;
type Texture = T; type Texture = T;
#[cfg(feature = "image")]
fn import_bitmap<C: std::ops::Deref<Target=[u8]>>(&mut self, image: &image::ImageBuffer<image::Rgba<u8>, C>) -> Result<Self::Texture, Self::Error> {
self.renderer.import_bitmap(image).map_err(Error::RenderError)
}
#[cfg(feature = "wayland_frontend")] #[cfg(feature = "wayland_frontend")]
fn shm_formats(&self) -> &[wl_shm::Format] { fn shm_formats(&self) -> &[wl_shm::Format] {
self.renderer.shm_formats() self.renderer.shm_formats()

View File

@ -438,6 +438,34 @@ impl Renderer for Gles2Renderer {
] ]
} }
#[cfg(feature = "image")]
fn import_bitmap<C: std::ops::Deref<Target=[u8]>>(&mut self, image: &image::ImageBuffer<image::Rgba<u8>, C>) -> Result<Self::Texture, Self::Error> {
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")] #[cfg(feature = "wayland_frontend")]
fn import_shm(&mut self, buffer: &wl_buffer::WlBuffer) -> Result<Self::Texture, Self::Error> { fn import_shm(&mut self, buffer: &wl_buffer::WlBuffer) -> Result<Self::Texture, Self::Error> {
use crate::wayland::shm::with_buffer_contents; use crate::wayland::shm::with_buffer_contents;
@ -502,13 +530,6 @@ impl Renderer for Gles2Renderer {
self.gl.BlendFunc(ffi::ONE, ffi::ONE_MINUS_SRC_ALPHA); self.gl.BlendFunc(ffi::ONE, ffi::ONE_MINUS_SRC_ALPHA);
} }
// output transformation passed in by the user
let mut projection = Matrix3::<f32>::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 // replicate https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glOrtho.xml
// glOrtho(0, width, 0, height, 1, 1); // glOrtho(0, width, 0, height, 1, 1);
let mut renderer = Matrix3::<f32>::identity(); let mut renderer = Matrix3::<f32>::identity();
@ -526,7 +547,8 @@ impl Renderer for Gles2Renderer {
renderer[2][0] = -(1.0f32.copysign(renderer[0][0] + renderer[1][0])); renderer[2][0] = -(1.0f32.copysign(renderer[0][0] + renderer[1][0]));
renderer[2][1] = -(1.0f32.copysign(renderer[0][1] + renderer[1][1])); 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(()) Ok(())
} }

View File

@ -134,6 +134,8 @@ pub trait Renderer {
type Error: Error; type Error: Error;
type Texture: Texture; type Texture: Texture;
#[cfg(feature = "image")]
fn import_bitmap<C: std::ops::Deref<Target=[u8]>>(&mut self, image: &image::ImageBuffer<image::Rgba<u8>, C>) -> Result<Self::Texture, Self::Error>;
#[cfg(feature = "wayland_frontend")] #[cfg(feature = "wayland_frontend")]
fn shm_formats(&self) -> &[wl_shm::Format] { fn shm_formats(&self) -> &[wl_shm::Format] {
// Mandatory // Mandatory

View File

@ -260,6 +260,11 @@ impl Renderer for WinitGraphicsBackend {
type Error = Gles2Error; type Error = Gles2Error;
type Texture = Gles2Texture; type Texture = Gles2Texture;
#[cfg(feature = "image")]
fn import_bitmap<C: std::ops::Deref<Target=[u8]>>(&mut self, image: &image::ImageBuffer<image::Rgba<u8>, C>) -> Result<Self::Texture, Self::Error> {
self.renderer.import_bitmap(image)
}
#[cfg(feature = "wayland_frontend")] #[cfg(feature = "wayland_frontend")]
fn shm_formats(&self) -> &[wl_shm::Format] { fn shm_formats(&self) -> &[wl_shm::Format] {
Renderer::shm_formats(&self.renderer) Renderer::shm_formats(&self.renderer)