Skip to content

Commit

Permalink
Merge pull request #86 from mBornand/memory_fix
Browse files Browse the repository at this point in the history
ensure textures are always destroyed
  • Loading branch information
danyspin97 authored Aug 26, 2024
2 parents 0e23789 + 36c9708 commit 132a01d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
30 changes: 16 additions & 14 deletions daemon/src/render/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub enum TransitionStatus {
}

pub struct Renderer {
gl: gl::Gl,
gl: Rc<gl::Gl>,
pub program: gl::types::GLuint,
vbo: gl::types::GLuint,
eab: gl::types::GLuint,
Expand All @@ -55,17 +55,17 @@ impl Renderer {
transition: Transition,
transform: Transform,
) -> Result<Self> {
let gl = gl::Gl::load_with(|name| {
let gl = Rc::new(gl::Gl::load_with(|name| {
egl.get_proc_address(name)
.expect("egl.get_proc_address to work") as *const std::ffi::c_void
});
}));

let program = create_program(&gl, transition)
.context("unable to create program during openGL ES initialization")?;

let (vbo, eab) = initialize_objects(&gl)?;

let current_wallpaper = Wallpaper::new();
let current_wallpaper = Wallpaper::new(gl.clone());

let transparent_texture = load_texture(&gl, transparent_image().into())?;

Expand Down Expand Up @@ -152,8 +152,11 @@ impl Renderer {
mode: BackgroundMode,
offset: Option<f32>,
) -> Result<()> {
self.prev_wallpaper = Some(std::mem::take(&mut self.current_wallpaper));
self.current_wallpaper.load_image(&self.gl, image)?;
self.prev_wallpaper = Some(std::mem::replace(
&mut self.current_wallpaper,
Wallpaper::new(self.gl.clone()),
));
self.current_wallpaper.load_image(image)?;

self.bind_wallpapers(mode, offset)?;

Expand All @@ -169,7 +172,7 @@ impl Renderer {
self.prev_wallpaper
.as_ref()
.expect("previous wallpaper to be set")
.bind(&self.gl)?;
.bind()?;

// current_wallpaper is already binded to TEXTURE1, as load_texture loads the image
// there
Expand Down Expand Up @@ -230,11 +233,14 @@ impl Renderer {
})
};
let texture_scale = gen_texture_scale(
self.current_wallpaper.image_width as f32,
self.current_wallpaper.image_height as f32,
self.current_wallpaper.get_image_width() as f32,
self.current_wallpaper.get_image_height() as f32,
);
let (prev_image_width, prev_image_height) = if let Some(prev_wp) = &self.prev_wallpaper {
(prev_wp.image_width as f32, prev_wp.image_height as f32)
(
prev_wp.get_image_width() as f32,
prev_wp.get_image_height() as f32,
)
} else {
(1.0, 1.0)
};
Expand Down Expand Up @@ -543,10 +549,6 @@ impl Deref for Renderer {
impl Drop for Renderer {
fn drop(&mut self) {
unsafe {
self.gl.DeleteTextures(1, &self.current_wallpaper.texture);
if let Some(wp) = &self.prev_wallpaper {
self.gl.DeleteTextures(1, &wp.texture);
}
self.gl.DeleteBuffers(1, &self.eab);
self.gl.DeleteBuffers(1, &self.vbo);
self.gl.DeleteProgram(self.program);
Expand Down
39 changes: 27 additions & 12 deletions daemon/src/render/wallpaper.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ffi::CStr;
use std::{ffi::CStr, rc::Rc};

use color_eyre::{
eyre::{bail, ensure},
Expand All @@ -10,43 +10,58 @@ use crate::{gl_check, render::gl};

use super::load_texture;

#[derive(Default)]
pub struct Wallpaper {
pub texture: gl::types::GLuint,
pub image_width: u32,
pub image_height: u32,
gl: Rc<gl::Gl>,
texture: gl::types::GLuint,
image_width: u32,
image_height: u32,
}

impl Wallpaper {
pub const fn new() -> Self {
pub const fn new(gl: Rc<gl::Gl>) -> Self {
Self {
gl,
texture: 0,
image_width: 10,
image_height: 10,
}
}

pub fn bind(&self, gl: &gl::Gl) -> Result<()> {
pub fn bind(&self) -> Result<()> {
unsafe {
gl.BindTexture(gl::TEXTURE_2D, self.texture);
gl_check!(gl, "binding textures");
self.gl.BindTexture(gl::TEXTURE_2D, self.texture);
gl_check!(self.gl, "binding textures");
}

Ok(())
}

pub fn load_image(&mut self, gl: &gl::Gl, image: DynamicImage) -> Result<()> {
pub fn load_image(&mut self, image: DynamicImage) -> Result<()> {
self.image_width = image.width();
self.image_height = image.height();

let texture = load_texture(gl, image)?;
let texture = load_texture(&self.gl, image)?;

unsafe {
// Delete from memory the previous texture
gl.DeleteTextures(1, &self.texture);
self.gl.DeleteTextures(1, &self.texture);
}
self.texture = texture;

Ok(())
}

pub fn get_image_height(&self) -> u32 {
self.image_height
}

pub fn get_image_width(&self) -> u32 {
self.image_width
}
}

impl Drop for Wallpaper {
fn drop(&mut self) {
unsafe { self.gl.DeleteTextures(1, &self.texture) };
}
}

0 comments on commit 132a01d

Please sign in to comment.