gallery/vendor/i-slint-renderer-skia/font_cache.rs
Greg Pomerantz 93b27d724b Root-cause and fix the Mali scroll jank; strip jank-hunt instrumentation
The 19-26 ms mid-scroll stalls (Mali-only, fixed content offsets) were
the Skia renderer's final synchronous GrDirectContext::flush() blocking
the UI thread on Mali driver back-pressure — proven by per-stage
timestamps in the vendored renderer (stall frames: flush 19-24 ms,
walk 1-3 ms, 1 binding re-eval, image imports 0.1 ms each).

- vendor/i-slint-renderer-skia (pinned 1.18.1, [patch.crates-io]):
  remove that one flush. Redundant on the wgpu backend: the surface
  already calls flush_and_submit before queue.present, same Vulkan
  queue, in-order. Phone: 0 stalls across 4 runs (max 3.6-7.4 ms vs
  the 8.3 ms budget), display verified clean.
- docs/BUGS/slint-skia-final-flush-jank.md: full evidence, prior art
  (slint-ui/slint#12030 closest), open question for a future
  upstream filing (not filed yet).
- Profiling also retired the O(n)-walk theory: Slint 1.18 prunes the
  item walk to ~133-152 items; the windowed Flickable built on that
  theory was already reverted (ListView is smooth with the fix).
- Strip all jank-hunt instrumentation: FR/SCR/TICK/APLYD/STG logging,
  stg markers, .gallery_* file knobs, jsamp thread sampler,
  DbgScroll programmatic scroll, nblog perf-file mirroring.
  Also fixes the 'launches into unstoppable fast scroll' bug: a
  leftover .gallery_cmd was re-read on every launch (file never
  deleted) and overrode user swipes — the hook is gone.
- Keep: nblog (logd backpressure is real), zero-dimension decode
  guards, keep-alive (default-off).
- HANDOFF: replace stale windowed-Flickable section with the actual
  root cause/fix; 15 unit tests pass.

Verified on phone (aarch64, 8580 photos) and emulator (x86_64).
2026-10-28 18:23:50 -04:00

109 lines
4.1 KiB
Rust

// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
use clru::CLruCache;
use i_slint_common::sharedfontique::HashedBlob;
use i_slint_core::textlayout::sharedparley::{fontique, parley};
use std::cell::RefCell;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::num::NonZeroUsize;
const FONT_CACHE_CAPACITY: NonZeroUsize = NonZeroUsize::new(64).unwrap();
pub struct FontCache {
font_mgr: skia_safe::FontMgr,
// Use HashedBlob in key to keep strong reference to font data blob,
// preventing eviction from fontique's shared cache (see commit 30a03cf).
// The u64 is a hash of variation settings (0 for base typefaces).
fonts: CLruCache<(HashedBlob, u32, u64), Option<skia_safe::Typeface>>,
}
impl Default for FontCache {
fn default() -> Self {
Self { font_mgr: skia_safe::FontMgr::new(), fonts: CLruCache::new(FONT_CACHE_CAPACITY) }
}
}
impl FontCache {
pub fn font_with_variations(
&mut self,
font: &parley::FontData,
synthesis: &fontique::Synthesis,
) -> Option<skia_safe::Typeface> {
let variation_settings = synthesis.variation_settings();
let mut variations_hash = 0u64;
if !variation_settings.is_empty() {
let mut hasher = DefaultHasher::new();
for &(tag, value) in variation_settings {
tag.to_be_bytes().hash(&mut hasher);
value.to_bits().hash(&mut hasher);
}
variations_hash = hasher.finish();
}
let key = (font.data.clone().into(), font.index, variations_hash);
if let Some(cached) = self.fonts.get(&key) {
return cached.clone();
}
let mut typeface = self.load_typeface_internal(font);
if !variation_settings.is_empty() {
typeface = typeface.and_then(|base| {
let coords: Vec<skia_safe::font_arguments::variation_position::Coordinate> =
variation_settings
.iter()
.map(|&(tag, value)| {
skia_safe::font_arguments::variation_position::Coordinate {
axis: skia_safe::FourByteTag::new(u32::from_be_bytes(
tag.to_be_bytes(),
)),
value,
}
})
.collect();
let position =
skia_safe::font_arguments::VariationPosition { coordinates: &coords };
let args = skia_safe::FontArguments::new().set_variation_design_position(position);
base.clone_with_arguments(&args).or(Some(base))
});
}
self.fonts.put(key, typeface.clone());
typeface
}
fn load_typeface_internal(&self, font: &parley::FontData) -> Option<skia_safe::Typeface> {
let typeface = self.font_mgr.new_from_data(
skia_safe::Data::new_copy(font.data.as_ref()),
if font.index > 0 { Some(font.index as _) } else { None },
);
// Due to https://issues.skia.org/issues/310510989, fonts from true type collections
// with an index > 0 fail to load on macOS. As a workaround, we manually extract the font from the
// collection and load it as a single font.
#[cfg(target_vendor = "apple")]
if font.index > 0
&& typeface.is_none()
&& let Some(typeface) = read_fonts::CollectionRef::new(font.data.as_ref())
.ok()
.and_then(|ttc| ttc.get(font.index).ok())
.map(|ttf| write_fonts::FontBuilder::new().copy_missing_tables(ttf).build())
.and_then(|new_ttf| {
self.font_mgr.new_from_data(skia_safe::Data::new_copy(&new_ttf), None)
})
{
return Some(typeface);
}
typeface
}
}
thread_local! {
pub static FONT_CACHE: RefCell<FontCache> = RefCell::new(Default::default())
}