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

72 lines
2.5 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
#![warn(missing_docs)]
//! This module defines a `ComponentFactory` and related code.
use crate::api::ComponentHandle;
use crate::item_tree::{ItemTreeRc, ItemTreeVTable, ItemTreeWeak};
use alloc::boxed::Box;
use alloc::rc::Rc;
use core::fmt::Debug;
/// The `FactoryContext` provides extra information to the ComponentFactory
pub struct FactoryContext {
/// The item tree to embed the factory product into
pub parent_item_tree: ItemTreeWeak,
/// The index in the parent item tree with the dynamic node to connect
/// the factories product to.
pub parent_item_tree_index: u32,
}
#[derive(Clone)]
struct ComponentFactoryInner(Rc<dyn Fn(FactoryContext) -> Option<ItemTreeRc> + 'static>);
impl PartialEq for ComponentFactoryInner {
fn eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.0, &other.0)
}
}
impl Debug for ComponentFactoryInner {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("ComponentFactoryData").finish()
}
}
/// A `ComponentFactory` can be used to create new Components at runtime,
/// taking a factory function returning a [`ComponentHandle`].
///
/// The `FactoryContext` is passed to that factory function.
///
/// A `ComponentFactory` implements the `component-factory` type for
/// properties in the Slint language.
///
/// The `component-factory` is used by an `ComponentContainer` element in Slint
/// files to embed UI elements based on the produced component within the
/// `ComponentContainer` element.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ComponentFactory(Option<ComponentFactoryInner>);
impl ComponentFactory {
/// Create a new `ComponentFactory`
pub fn new<
X: vtable::HasStaticVTable<ItemTreeVTable> + 'static,
T: ComponentHandle<WeakInner = vtable::VWeak<ItemTreeVTable, X>> + 'static,
>(
factory: impl Fn(FactoryContext) -> Option<T> + 'static,
) -> Self {
let factory = Box::new(factory) as Box<dyn Fn(FactoryContext) -> Option<T> + 'static>;
Self(Some(ComponentFactoryInner(Rc::new(move |ctx| -> Option<ItemTreeRc> {
let product = (factory)(ctx);
product.map(|p| vtable::VRc::into_dyn(p.as_weak().inner().upgrade().unwrap()))
}))))
}
/// Build a `Component`
pub(crate) fn build(&self, ctx: FactoryContext) -> Option<ItemTreeRc> {
self.0.as_ref().and_then(move |b| (b.0)(ctx))
}
}