Skip to content

Commit 9243131

Browse files
committed
Retain pixel_bounds sections in the glyph cache
Optimises performance in the case sections are being bound checked each frame, but not actually drawn. Custom positioning use case.
1 parent 35ef0d9 commit 9243131

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/lib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ pub struct GlyphBrush<'a, R: gfx::Resources, F: gfx::Factory<R>>{
214214
// to be rendered on the next `draw_queued` call
215215
section_buffer: Vec<u64>,
216216

217+
// Set of section hashs to keep in the glyph cache this frame even if they haven't been drawn
218+
keep_in_cache: HashSet<u64>,
219+
217220
// config
218221
gpu_cache_scale_tolerance: f32,
219222
gpu_cache_position_tolerance: f32,
@@ -241,6 +244,7 @@ impl<'font, R: gfx::Resources, F: gfx::Factory<R>> GlyphBrush<'font, R, F> {
241244
let mut no_match = true;
242245

243246
let section_hash = self.cache_glyphs(&section, custom_layout);
247+
self.keep_in_cache.insert(section_hash);
244248

245249
for g in &self.calculate_glyph_cache[&section_hash].glyphs {
246250
if let Some(Rect{ min, max }) = g.pixel_bounding_box() {
@@ -521,15 +525,22 @@ impl<'font, R: gfx::Resources, F: gfx::Factory<R>> GlyphBrush<'font, R, F> {
521525
fn clear_section_buffer(&mut self) {
522526
if self.cache_glyph_positioning {
523527
// clear section_buffer & trim calculate_glyph_cache to active sections
524-
let mut active = HashSet::with_capacity(self.section_buffer.len());
528+
let mut active = HashSet::with_capacity(
529+
self.section_buffer.len() + self.keep_in_cache.len()
530+
);
531+
525532
for h in self.section_buffer.drain(..) {
526533
active.insert(h);
527534
}
535+
for h in self.keep_in_cache.drain() {
536+
active.insert(h);
537+
}
528538
self.calculate_glyph_cache.retain(|key, _| active.contains(key));
529539
}
530540
else {
531541
self.section_buffer.clear();
532542
self.calculate_glyph_cache.clear();
543+
self.keep_in_cache.clear();
533544
}
534545
}
535546

@@ -707,6 +718,7 @@ impl<'a> GlyphBrushBuilder<'a> {
707718
draw_cache: None,
708719
section_buffer: Vec::new(),
709720
calculate_glyph_cache: HashMap::new(),
721+
keep_in_cache: HashSet::new(),
710722

711723
gpu_cache_scale_tolerance: self.gpu_cache_scale_tolerance,
712724
gpu_cache_position_tolerance: self.gpu_cache_position_tolerance,

0 commit comments

Comments
 (0)