summaryrefslogtreecommitdiffstats
path: root/wgpu/src/texture/atlas/allocator.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-02-26 20:10:19 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-02-26 20:10:19 +0100
commitd06d06e05096e0145be74fd02d67eada0a1665a1 (patch)
tree7e1d5e256d553a4d98c10b7751441d6fa541990f /wgpu/src/texture/atlas/allocator.rs
parent48d70280eb4f5908f1c9339bebdfbab856d55ae1 (diff)
downloadiced-d06d06e05096e0145be74fd02d67eada0a1665a1.tar.gz
iced-d06d06e05096e0145be74fd02d67eada0a1665a1.tar.bz2
iced-d06d06e05096e0145be74fd02d67eada0a1665a1.zip
Deallocate atlas entries and remove padding
Diffstat (limited to 'wgpu/src/texture/atlas/allocator.rs')
-rw-r--r--wgpu/src/texture/atlas/allocator.rs57
1 files changed, 20 insertions, 37 deletions
diff --git a/wgpu/src/texture/atlas/allocator.rs b/wgpu/src/texture/atlas/allocator.rs
index ad111212..7a4ff5b1 100644
--- a/wgpu/src/texture/atlas/allocator.rs
+++ b/wgpu/src/texture/atlas/allocator.rs
@@ -2,70 +2,54 @@ use guillotiere::{AtlasAllocator, Size};
pub struct Allocator {
raw: AtlasAllocator,
- size: u32,
+ allocations: usize,
}
impl Allocator {
- const PADDING: u32 = 1;
-
pub fn new(size: u32) -> Allocator {
let raw = AtlasAllocator::new(Size::new(size as i32, size as i32));
- Allocator { raw, size }
+ Allocator {
+ raw,
+ allocations: 0,
+ }
}
pub fn allocate(&mut self, width: u32, height: u32) -> Option<Region> {
- let padding = (
- if width + Self::PADDING * 2 < self.size {
- Self::PADDING
- } else {
- 0
- },
- if height + Self::PADDING * 2 < self.size {
- Self::PADDING
- } else {
- 0
- },
- );
-
- let allocation = self.raw.allocate(Size::new(
- (width + padding.0 * 2) as i32,
- (height + padding.1 * 2) as i32,
- ))?;
-
- Some(Region {
- allocation,
- padding,
- })
+ let allocation =
+ self.raw.allocate(Size::new(width as i32, height as i32))?;
+
+ self.allocations += 1;
+
+ Some(Region { allocation })
}
- pub fn deallocate(&mut self, region: Region) {
+ pub fn deallocate(&mut self, region: &Region) {
self.raw.deallocate(region.allocation.id);
+
+ self.allocations = self.allocations.saturating_sub(1);
+ }
+
+ pub fn is_empty(&self) -> bool {
+ self.allocations == 0
}
}
pub struct Region {
allocation: guillotiere::Allocation,
- padding: (u32, u32),
}
impl Region {
pub fn position(&self) -> (u32, u32) {
let rectangle = &self.allocation.rectangle;
- (
- rectangle.min.x as u32 + self.padding.0,
- rectangle.min.y as u32 + self.padding.1,
- )
+ (rectangle.min.x as u32, rectangle.min.y as u32)
}
pub fn size(&self) -> (u32, u32) {
let size = self.allocation.rectangle.size();
- (
- size.width as u32 - self.padding.0 * 2,
- size.height as u32 - self.padding.1 * 2,
- )
+ (size.width as u32, size.height as u32)
}
}
@@ -80,7 +64,6 @@ impl std::fmt::Debug for Region {
f.debug_struct("Region")
.field("id", &self.allocation.id)
.field("rectangle", &self.allocation.rectangle)
- .field("padding", &self.padding)
.finish()
}
}