gallery/vendor/i-slint-core/item_focus.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

295 lines
8.3 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
// cSpell: ignore nesw
/*!
This module contains the code moving the keyboard focus between items
*/
use crate::item_tree::ItemTreeNodeArray;
pub fn step_out_of_node(
index: u32,
item_tree: &crate::item_tree::ItemTreeNodeArray,
) -> Option<u32> {
let mut self_or_ancestor = index;
loop {
if let Some(sibling) = item_tree.next_sibling(self_or_ancestor) {
return Some(sibling);
}
self_or_ancestor = item_tree.parent(self_or_ancestor)?;
}
}
pub fn default_next_in_local_focus_chain(
index: u32,
item_tree: &crate::item_tree::ItemTreeNodeArray,
) -> Option<u32> {
if let Some(child) = item_tree.first_child(index) {
return Some(child);
}
step_out_of_node(index, item_tree)
}
fn step_into_node(item_tree: &ItemTreeNodeArray, index: u32) -> u32 {
let mut node = index;
loop {
if let Some(last_child) = item_tree.last_child(node) {
node = last_child;
} else {
return node;
}
}
}
pub fn default_previous_in_local_focus_chain(
index: u32,
item_tree: &crate::item_tree::ItemTreeNodeArray,
) -> Option<u32> {
if let Some(previous) = item_tree.previous_sibling(index) {
Some(step_into_node(item_tree, previous))
} else {
item_tree.parent(index)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::vec;
use crate::item_tree::ItemTreeNode;
fn validate_focus_chains(item_tree: ItemTreeNodeArray<'_>) {
let forward_chain = {
let mut tmp = alloc::vec::Vec::with_capacity(item_tree.node_count());
let mut node = 0;
loop {
tmp.push(node);
if let Some(next_node) = default_next_in_local_focus_chain(node, &item_tree) {
node = next_node;
} else {
break;
}
}
tmp
};
let reverse_backward_chain = {
let mut tmp = alloc::vec::Vec::with_capacity(item_tree.node_count());
let mut node = step_into_node(&item_tree, 0);
loop {
tmp.push(node);
if let Some(next_node) = default_previous_in_local_focus_chain(node, &item_tree) {
node = next_node;
} else {
break;
}
}
tmp.reverse();
tmp
};
assert_eq!(forward_chain, reverse_backward_chain);
assert_eq!(forward_chain.len(), item_tree.node_count());
}
#[test]
fn test_focus_chain_root_only() {
let nodes = vec![ItemTreeNode::Item {
is_accessible: false,
children_count: 0,
children_index: 1,
parent_index: 0,
item_array_index: 0,
}];
let tree: ItemTreeNodeArray = (nodes.as_slice()).into();
validate_focus_chains(tree);
}
#[test]
fn test_focus_chain_one_child() {
let nodes = vec![
ItemTreeNode::Item {
is_accessible: false,
children_count: 1,
children_index: 1,
parent_index: 0,
item_array_index: 0,
},
ItemTreeNode::Item {
is_accessible: false,
children_count: 0,
children_index: 2,
parent_index: 0,
item_array_index: 0,
},
];
let tree: ItemTreeNodeArray = (nodes.as_slice()).into();
validate_focus_chains(tree);
}
#[test]
fn test_focus_chain_three_children() {
let nodes = vec![
ItemTreeNode::Item {
is_accessible: false,
children_count: 3,
children_index: 1,
parent_index: 0,
item_array_index: 0,
},
ItemTreeNode::Item {
is_accessible: false,
children_count: 0,
children_index: 4,
parent_index: 0,
item_array_index: 0,
},
ItemTreeNode::Item {
is_accessible: false,
children_count: 0,
children_index: 4,
parent_index: 0,
item_array_index: 0,
},
ItemTreeNode::Item {
is_accessible: false,
children_count: 0,
children_index: 4,
parent_index: 0,
item_array_index: 0,
},
];
let tree: ItemTreeNodeArray = (nodes.as_slice()).into();
validate_focus_chains(tree);
}
#[test]
fn test_focus_chain_complex_tree() {
let nodes = vec![
ItemTreeNode::Item {
// 0
is_accessible: false,
children_count: 2,
children_index: 1,
parent_index: 0,
item_array_index: 0,
},
ItemTreeNode::Item {
// 1
is_accessible: false,
children_count: 2,
children_index: 3,
parent_index: 0,
item_array_index: 0,
},
ItemTreeNode::Item {
// 2
is_accessible: false,
children_count: 1,
children_index: 11,
parent_index: 0,
item_array_index: 0,
},
ItemTreeNode::Item {
// 3
is_accessible: false,
children_count: 1,
children_index: 5,
parent_index: 1,
item_array_index: 0,
},
ItemTreeNode::Item {
// 4
is_accessible: false,
children_count: 2,
children_index: 6,
parent_index: 1,
item_array_index: 0,
},
ItemTreeNode::Item {
// 5
is_accessible: false,
children_count: 0,
children_index: 0,
parent_index: 3,
item_array_index: 0,
},
ItemTreeNode::Item {
// 6
is_accessible: false,
children_count: 2,
children_index: 8,
parent_index: 4,
item_array_index: 0,
},
ItemTreeNode::Item {
// 7
is_accessible: false,
children_count: 1,
children_index: 10,
parent_index: 4,
item_array_index: 0,
},
ItemTreeNode::Item {
// 8
is_accessible: false,
children_count: 0,
children_index: 0,
parent_index: 6,
item_array_index: 0,
},
ItemTreeNode::Item {
// 9
is_accessible: false,
children_count: 0,
children_index: 0,
parent_index: 6,
item_array_index: 0,
},
ItemTreeNode::Item {
// 10
is_accessible: false,
children_count: 0,
children_index: 0,
parent_index: 7,
item_array_index: 0,
},
ItemTreeNode::Item {
// 11
is_accessible: false,
children_count: 2,
children_index: 12,
parent_index: 2,
item_array_index: 0,
},
ItemTreeNode::Item {
// 12
is_accessible: false,
children_count: 0,
children_index: 0,
parent_index: 11,
item_array_index: 0,
},
ItemTreeNode::Item {
// 13
is_accessible: false,
children_count: 0,
children_index: 0,
parent_index: 11,
item_array_index: 0,
},
];
let tree: ItemTreeNodeArray = (nodes.as_slice()).into();
validate_focus_chains(tree);
}
}