pub struct GlyphCache { /* private fields */ }Expand description
Glyph cache with LRU eviction.
Tracks a frame generation counter. Each get marks the glyph as used
in the current generation. evict_unused removes glyphs not used
for max_idle_frames generations and deallocates their atlas space.
Implementations§
Source§impl GlyphCache
impl GlyphCache
pub fn new() -> Self
Sourcepub fn advance_generation(&mut self)
pub fn advance_generation(&mut self)
Advance the frame generation counter. Call once per render frame.
pub fn generation(&self) -> u64
Sourcepub fn get(&mut self, key: &GlyphCacheKey) -> Option<&CachedGlyph>
pub fn get(&mut self, key: &GlyphCacheKey) -> Option<&CachedGlyph>
Look up a cached glyph, marking it as used in the current generation.
Sourcepub fn peek(&self, key: &GlyphCacheKey) -> Option<&CachedGlyph>
pub fn peek(&self, key: &GlyphCacheKey) -> Option<&CachedGlyph>
Look up without marking as used (for read-only queries).
pub fn insert(&mut self, key: GlyphCacheKey, glyph: CachedGlyph)
Sourcepub fn evict_unused(&mut self) -> Vec<EvictedGlyph>
pub fn evict_unused(&mut self) -> Vec<EvictedGlyph>
Evict glyphs unused for MAX_IDLE_FRAMES generations.
Returns the evicted entries — each carries the AllocId to
deallocate from the atlas plus the atlas rectangle it occupied
(the bucketed allocator cannot resolve a rectangle from an
AllocId after the fact, so the rect is captured here, before
the entry is dropped; debug builds use it to poison-fill the
freed pixels).
Only runs the actual eviction scan every 60 calls (~1 second at 60fps)
to avoid iterating the entire cache on every render.
Sourcepub fn evict_for_pressure(&mut self) -> Vec<EvictedGlyph>
pub fn evict_for_pressure(&mut self) -> Vec<EvictedGlyph>
Emergency eviction when the atlas is full: drop every glyph not used in the current generation, regardless of the idle window.
Unlike evict_unused this runs
unconditionally (no every-60-generations gate) — it is only
called when an allocation has already failed at the atlas size
cap, where the alternative is silently dropping the new glyph.
Glyphs touched this generation are still referenced by quads in
the frame being built and must survive.
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
Sourcepub fn touch(&mut self, keys: &[GlyphCacheKey])
pub fn touch(&mut self, keys: &[GlyphCacheKey])
Mark multiple glyphs as used in the current generation without returning their data. Used by callers that cache glyph output externally (e.g. per-widget paint caches) and need to keep the glyphs alive in the atlas even though they don’t re-measure them every frame.