Skip to content

Commit

Permalink
Improve the Skia font cache usage
Browse files Browse the repository at this point in the history
A bigger default size with cleanup when idling
  • Loading branch information
fredizzimo committed Apr 14, 2024
1 parent d7e2ffa commit 4c7b0db
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
25 changes: 8 additions & 17 deletions src/renderer/fonts/caching_shaper.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use std::{num::NonZeroUsize, sync::Arc};

use itertools::Itertools;
use log::{debug, error, info, trace, warn};
use log::{debug, error, info, trace};
use lru::LruCache;
use skia_safe::{
graphics::{font_cache_limit, font_cache_used, set_font_cache_limit},
TextBlob, TextBlobBuilder,
};
use skia_safe::{graphics::set_font_cache_limit, TextBlob, TextBlobBuilder};
use swash::{
shape::ShapeContext,
text::{
Expand All @@ -30,6 +27,8 @@ struct ShapeKey {
pub style: CoarseStyle,
}

const FONT_CACHE_SIZE: usize = 8 * 1024 * 1024;

pub struct CachingShaper {
options: FontOptions,
font_loader: FontLoader,
Expand Down Expand Up @@ -352,16 +351,10 @@ impl CachingShaper {
grouped_results
}

pub fn adjust_font_cache_size(&self) {
let current_font_cache_size = font_cache_limit() as f32;
let percent_font_cache_used = font_cache_used() as f32 / current_font_cache_size;
if percent_font_cache_used > 0.9 {
warn!(
"Font cache is {}% full, increasing cache size",
percent_font_cache_used * 100.0
);
set_font_cache_limit((percent_font_cache_used * 1.5) as usize);
}
pub fn cleanup_font_cache(&self) {
tracy_zone!("purge_font_cache");
set_font_cache_limit(FONT_CACHE_SIZE / 2);
set_font_cache_limit(FONT_CACHE_SIZE);
}

pub fn shape(&mut self, text: String, style: CoarseStyle) -> Vec<TextBlob> {
Expand Down Expand Up @@ -420,8 +413,6 @@ impl CachingShaper {
resulting_blobs.push(blob.expect("Could not create textblob"));
}

self.adjust_font_cache_size();

resulting_blobs
}

Expand Down
6 changes: 6 additions & 0 deletions src/window/update_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ impl UpdateLoop {
self.last_dt = self.previous_frame_start.elapsed().as_secs_f32();
self.previous_frame_start = Instant::now();
}
} else {
window_wrapper
.renderer
.grid_renderer
.shaper
.cleanup_font_cache();
}
}
Ok(Event::WindowEvent {
Expand Down
2 changes: 1 addition & 1 deletion src/window/window_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub struct WinitWindowWrapper {
// Don't rearrange this, unless you have a good reason to do so
// The destruction order has to be correct
pub skia_renderer: Box<dyn SkiaRenderer>,
renderer: Renderer,
pub renderer: Renderer,
keyboard_manager: KeyboardManager,
mouse_manager: MouseManager,
title: String,
Expand Down

0 comments on commit 4c7b0db

Please sign in to comment.