gallery/vendor/i-slint-core/textlayout/glyphclusters.rs
Greg Pomerantz 2f4768f354 Fix 120Hz scroll jank end-to-end: render-ahead present + full-cache eviction off the UI thread
Three root causes, all fixed and verified on the Pixel 9 Pro:

1. Present phase (backend + renderer): VSYNC_LOCKED_RENDER defers the
   render to the vsync wake; RENDER_AHEAD presents the previous frame at
   the start of render() (phase = wake + ~1.1 ms) and holds the new
   frame one slot. Crash fix: a held wgpu frame must be PRESENTED, never
   merely dropped, before the next acquire (AlreadyAcquired panic at
   launch otherwise); discard_pending_frame presents too.

2. Full-res prefetch churn (src/lib.rs): the 8-slot full cache cycled
   through a ~20-cell viewport forever (137 decodes/20 s) and each
   eviction free()'d a 50 MB Vec on the UI thread (20-52 ms CPU spikes,
   ~7/s = the residual SLOWEV NonMain jank). Fix: 60 s tombstone
   cooldown on evicted paths (idle prefetch skips, urgent tap bypasses)
   + off-thread reaper for the freed blobs (insert: 21 ms -> 4 us).

3. (earlier) Skia final flush + Mailbox present mode (vendor, M3).

Instrumentation (all behind --features perf): OTHR/INVK/INVK_TAG/FLCK
lines for cross-thread closure timing and push-site tagging.

Warm measured swipe, 8580-file grid: cadence median 8.33 ms (exact
120 Hz), p99 15.5 ms, 5/552 gaps >16.7 ms (0.9%, max 28.3 ms, zero
>33 ms); PRES phase median ~994 us, zero late frames. Docs updated
(docs/BUGS/scroll-jank-120hz.md, full scorecard + line reference).

Also: vendored i-slint-core + wgpu-hal (vkshim interposition patch),
vkshim profiling tooling, backend made a non-optional dep, debuggable
flag for simpleperf (REMOVE before shipping).
2026-10-30 02:49:23 -04:00

104 lines
3.2 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 core::{marker::PhantomData, ops::Range};
use euclid::num::Zero;
use super::ShapeBuffer;
#[derive(Clone)]
pub struct GlyphCluster<Length: Clone> {
pub byte_range: Range<usize>,
pub glyph_range: Range<usize>,
pub width: Length,
pub is_whitespace: bool,
pub is_line_or_paragraph_separator: bool,
}
#[derive(Clone)]
pub struct GlyphClusterIterator<'a, Length> {
text: &'a str,
shaped_text: &'a ShapeBuffer<Length>,
current_run: usize,
// absolute byte offset in the entire text
byte_offset: usize,
glyph_index: usize,
marker: PhantomData<Length>,
}
impl<'a, Length> GlyphClusterIterator<'a, Length> {
pub fn new(text: &'a str, shaped_text: &'a ShapeBuffer<Length>) -> Self {
Self {
text,
shaped_text,
current_run: 0,
byte_offset: 0,
glyph_index: 0,
marker: Default::default(),
}
}
}
impl<Length: Copy + Clone + Zero + core::ops::AddAssign> Iterator
for GlyphClusterIterator<'_, Length>
{
type Item = GlyphCluster<Length>;
fn next(&mut self) -> Option<Self::Item> {
if self.current_run >= self.shaped_text.text_runs.len() {
return None;
}
let current_run =
if self.byte_offset < self.shaped_text.text_runs[self.current_run].byte_range.end {
&self.shaped_text.text_runs[self.current_run]
} else {
self.current_run += 1;
self.shaped_text.text_runs.get(self.current_run)?
};
let mut cluster_width: Length = Length::zero();
let cluster_start = self.glyph_index;
let mut cluster_byte_offset;
loop {
let glyph = &self.shaped_text.glyphs[self.glyph_index];
// ShapeBuffer::new folds the run's start into the cluster index
// the shaper reports, so this offset is already absolute.
cluster_byte_offset = glyph.text_byte_offset;
if cluster_byte_offset != self.byte_offset {
break;
}
cluster_width += glyph.advance;
self.glyph_index += 1;
if self.glyph_index >= current_run.glyph_range.end {
cluster_byte_offset = current_run.byte_range.end;
break;
}
}
let byte_range = self.byte_offset..cluster_byte_offset;
let (is_whitespace, is_line_or_paragraph_separator) = self.text[self.byte_offset..]
.chars()
.next()
.map(|ch| {
let is_line_or_paragraph_separator =
ch == '\n' || ch == '\u{2028}' || ch == '\u{2029}';
(ch.is_whitespace(), is_line_or_paragraph_separator)
})
.unwrap_or_default();
self.byte_offset = cluster_byte_offset;
Some(GlyphCluster {
byte_range,
glyph_range: Range { start: cluster_start, end: self.glyph_index },
width: cluster_width,
is_whitespace,
is_line_or_paragraph_separator,
})
}
}