Struct etagere::bucketed::BucketedAtlasAllocator
source · pub struct BucketedAtlasAllocator {
shelves: Vec<Shelf>,
buckets: Vec<Bucket>,
available_height: u16,
width: u16,
height: u16,
first_unallocated_bucket: BucketIndex,
flip_xy: bool,
alignment: Size,
current_column: u16,
column_width: u16,
num_columns: u16,
allocated_space: i32,
}
Expand description
A faster but less precise Shelf-packing dynamic texture atlas allocator, inspired by https://github.com/mapbox/shelf-pack/
Items are accumulated into buckets which are laid out in rows (shelves) of variable height. When allocating we first look for a suitable bucket. If none is found, a new shelf of the desired height is pushed.
Lifetime isn’t tracked at item granularity. Instead, items are grouped into buckets and deallocation happens per bucket when all items of the buckets are removed. When the top-most shelf is empty, it is removed, potentially cascading into garbage-collecting the next shelf, etc.
This allocator works well when there are a lot of small items with similar sizes (typically, glyph atlases).
Fields§
§shelves: Vec<Shelf>
§buckets: Vec<Bucket>
§available_height: u16
§width: u16
§height: u16
§first_unallocated_bucket: BucketIndex
§flip_xy: bool
§alignment: Size
§current_column: u16
§column_width: u16
§num_columns: u16
§allocated_space: i32
Implementations§
source§impl BucketedAtlasAllocator
impl BucketedAtlasAllocator
sourcepub fn with_options(size: Size, options: &AllocatorOptions) -> Self
pub fn with_options(size: Size, options: &AllocatorOptions) -> Self
Create an atlas allocator with provided options.
pub fn clear(&mut self)
pub fn size(&self) -> Size
pub fn grow(&mut self, new_size: Size)
pub fn is_empty(&self) -> bool
sourcepub fn allocate(&mut self, requested_size: Size) -> Option<Allocation>
pub fn allocate(&mut self, requested_size: Size) -> Option<Allocation>
Allocate a rectangle in the atlas.
sourcepub fn deallocate(&mut self, id: AllocId)
pub fn deallocate(&mut self, id: AllocId)
Deallocate a rectangle in the atlas.
Space is only reclaimed when all items of the same bucket are deallocated.
sourcepub fn allocated_space(&self) -> i32
pub fn allocated_space(&self) -> i32
Amount of occupied space in the atlas.
sourcepub fn free_space(&self) -> i32
pub fn free_space(&self) -> i32
How much space is available for future allocations.
fn alloc_from_bucket( &mut self, shelf_index: usize, bucket_index: BucketIndex, width: u16, ) -> Option<Allocation>
fn add_bucket(&mut self, bucket: Bucket) -> BucketIndex
fn add_shelf(&mut self, width: u16, height: u16) -> usize
sourcefn coalesce_shelves(&mut self, w: u16, h: u16) -> (usize, BucketIndex)
fn coalesce_shelves(&mut self, w: u16, h: u16) -> (usize, BucketIndex)
Find a sequence of consecutive shelves that can be coalesced into a single one tall enough to fit the provided size.
If such a sequence is found, grow the height of first shelf and squash the other ones to zero. The squashed shelves are not removed, their height is just set to zero so no item can go in, and they will be garbage-collected whenever there’s no shelf above them. For simplicity, the bucket width is not modified.
fn num_buckets(&self, width: u16, height: u16) -> u16
sourcefn deallocate_from_bucket(&mut self, id: AllocId) -> bool
fn deallocate_from_bucket(&mut self, id: AllocId) -> bool
Returns true if we should garbage-collect the shelves as a result of removing this element (we deallocated the last item from the bucket on the top-most shelf).
fn cleanup_shelves(&mut self)
fn shelf_is_empty(&self, idx: usize) -> bool
sourcepub fn dump_svg(&self, output: &mut dyn Write) -> Result<()>
pub fn dump_svg(&self, output: &mut dyn Write) -> Result<()>
Dump a visual representation of the atlas in SVG format.
fn check(&self)
sourcepub fn dump_into_svg(
&self,
rect: Option<&Rectangle>,
output: &mut dyn Write,
) -> Result<()>
pub fn dump_into_svg( &self, rect: Option<&Rectangle>, output: &mut dyn Write, ) -> Result<()>
Dump a visual representation of the atlas in SVG, omitting the beginning and end of the SVG document, so that it can be included in a larger document.
If a rectangle is provided, translate and scale the output to fit it.
Trait Implementations§
source§impl Clone for BucketedAtlasAllocator
impl Clone for BucketedAtlasAllocator
source§fn clone(&self) -> BucketedAtlasAllocator
fn clone(&self) -> BucketedAtlasAllocator
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more