Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with Atlas leaking memory. #721

Merged
merged 3 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/text/atlas.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
get_quad_context,
get_context, get_quad_context,
math::Rect,
texture::{Image, Texture2D},
Color,
Expand Down Expand Up @@ -32,6 +32,13 @@ pub struct Atlas {
unique_id: u64,
}

impl Drop for Atlas {
fn drop(&mut self) {
let ctx = &mut get_context().quad_context;
ctx.delete_texture(self.texture);
}
}

impl Atlas {
// pixel gap between glyphs in the atlas
const GAP: u16 = 2;
Expand Down
25 changes: 15 additions & 10 deletions src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,24 +230,27 @@ impl Image {
bytes,
}
}

/// Blends this image with another image (of identical dimensions)
/// Inspired by OpenCV saturated blending
pub fn blend(&mut self, other: &Image) {
assert!(self.width as usize * self.height as usize == other.width as usize * other.height as usize);
assert!(
self.width as usize * self.height as usize
== other.width as usize * other.height as usize
);

for i in 0..self.bytes.len() / 4 {
let c1: Color = Color {
r: self.bytes[i * 4] as f32 / 255.,
g: self.bytes[i * 4 + 1] as f32 / 255.,
b: self.bytes[i * 4 + 2] as f32 / 255.,
a: self.bytes[i * 4 + 3] as f32 / 255.
a: self.bytes[i * 4 + 3] as f32 / 255.,
};
let c2: Color = Color {
r: other.bytes[i * 4] as f32 / 255.,
g: other.bytes[i * 4 + 1] as f32 / 255.,
b: other.bytes[i * 4 + 2] as f32 / 255.,
a: other.bytes[i * 4 + 3] as f32 / 255.
a: other.bytes[i * 4 + 3] as f32 / 255.,
};
let new_color: Color = Color {
r: f32::min(c1.r * c1.a + c2.r * c2.a, 1.),
Expand All @@ -267,28 +270,31 @@ impl Image {
/// overlaying a completely transparent image has no effect
/// on the original image, though blending them would.
pub fn overlay(&mut self, other: &Image) {
assert!(self.width as usize * self.height as usize == other.width as usize * other.height as usize);
assert!(
self.width as usize * self.height as usize
== other.width as usize * other.height as usize
);

for i in 0..self.bytes.len() / 4 {
let c1: Color = Color {
r: self.bytes[i * 4] as f32 / 255.,
g: self.bytes[i * 4 + 1] as f32 / 255.,
b: self.bytes[i * 4 + 2] as f32 / 255.,
a: self.bytes[i * 4 + 3] as f32 / 255.
a: self.bytes[i * 4 + 3] as f32 / 255.,
};
let c2: Color = Color {
r: other.bytes[i * 4] as f32 / 255.,
g: other.bytes[i * 4 + 1] as f32 / 255.,
b: other.bytes[i * 4 + 2] as f32 / 255.,
a: other.bytes[i * 4 + 3] as f32 / 255.
a: other.bytes[i * 4 + 3] as f32 / 255.,
};
let new_color: Color = Color {
r: f32::min(c1.r * (1. - c2.a) + c2.r * c2.a, 1.),
g: f32::min(c1.g * (1. - c2.a) + c2.g * c2.a, 1.),
b: f32::min(c1.b * (1. - c2.a) + c2.b * c2.a, 1.),
a: f32::min(c1.a + c2.a, 1.)
a: f32::min(c1.a + c2.a, 1.),
};

self.bytes[i * 4] = (new_color.r * 255.) as u8;
self.bytes[i * 4 + 1] = (new_color.g * 255.) as u8;
self.bytes[i * 4 + 2] = (new_color.b * 255.) as u8;
Expand Down Expand Up @@ -671,7 +677,6 @@ impl Texture2D {
ctx.texture_update(self.raw_miniquad_id(), bytes);
}


/// Uploads [Image] data to part of this texture.
pub fn update_part(
&self,
Expand Down
Loading