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).
162 lines
5.3 KiB
Rust
162 lines
5.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
|
|
|
|
/*!
|
|
Callback that can be connected to one single handler.
|
|
|
|
TODO: reconsider if we should rename that to `Event`
|
|
but then it should also be renamed everywhere, including in the language grammar
|
|
*/
|
|
|
|
#![warn(missing_docs)]
|
|
|
|
use alloc::boxed::Box;
|
|
use core::cell::Cell;
|
|
|
|
/// A Callback that can be connected to a handler.
|
|
///
|
|
/// The Arg represents the argument. It should always be a tuple
|
|
///
|
|
#[repr(C)]
|
|
pub struct Callback<Arg: ?Sized, Ret = ()> {
|
|
/// FIXME: `Box<dyn>` is a fat object and we probably want to put an erased type in there
|
|
handler: Cell<Option<Box<dyn FnMut(&Arg, &mut Ret)>>>,
|
|
}
|
|
|
|
impl<Arg: ?Sized, Ret> Default for Callback<Arg, Ret> {
|
|
fn default() -> Self {
|
|
Self { handler: Default::default() }
|
|
}
|
|
}
|
|
|
|
impl<Arg: ?Sized, Ret: Default> Callback<Arg, Ret> {
|
|
/// Call the callback with the given argument.
|
|
pub fn call(&self, a: &Arg) -> Ret {
|
|
let mut r = Ret::default();
|
|
if let Some(mut h) = self.handler.take() {
|
|
h(a, &mut r);
|
|
assert!(self.handler.take().is_none(), "Callback Handler set while called");
|
|
self.handler.set(Some(h));
|
|
}
|
|
r
|
|
}
|
|
|
|
/// Return whether a callback is registered or not.
|
|
pub fn has_handler(&self) -> bool {
|
|
let handler = self.handler.take();
|
|
let result = handler.is_some();
|
|
self.handler.set(handler);
|
|
result
|
|
}
|
|
|
|
/// Set an handler to be called when the callback is called
|
|
///
|
|
/// There can only be one single handler per callback.
|
|
pub fn set_handler(&self, mut f: impl FnMut(&Arg) -> Ret + 'static) {
|
|
self.handler.set(Some(Box::new(move |a: &Arg, r: &mut Ret| *r = f(a))));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn callback_simple_test() {
|
|
use std::rc::Rc;
|
|
#[derive(Default)]
|
|
struct Component {
|
|
pressed: core::cell::Cell<bool>,
|
|
clicked: Callback<()>,
|
|
}
|
|
let c = Rc::new(Component::default());
|
|
let weak = Rc::downgrade(&c);
|
|
c.clicked.set_handler(move |()| weak.upgrade().unwrap().pressed.set(true));
|
|
c.clicked.call(&());
|
|
assert!(c.pressed.get());
|
|
}
|
|
|
|
#[cfg(feature = "ffi")]
|
|
pub(crate) mod ffi {
|
|
#![allow(unsafe_code)]
|
|
|
|
use super::*;
|
|
use core::ffi::c_void;
|
|
|
|
#[repr(C)]
|
|
/// Has the same layout as Callback<_>
|
|
pub struct CallbackOpaque(*const c_void, *const c_void);
|
|
|
|
static_assertions::assert_eq_align!(CallbackOpaque, Callback<(), c_void>);
|
|
static_assertions::assert_eq_size!(CallbackOpaque, Callback<(), c_void>);
|
|
static_assertions::assert_eq_align!(CallbackOpaque, Callback<(alloc::string::String,), c_void>);
|
|
static_assertions::assert_eq_size!(CallbackOpaque, Callback<(alloc::string::String,), c_void>);
|
|
|
|
/// Initialize the callback.
|
|
/// slint_callback_drop must be called.
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn slint_callback_init(out: *mut CallbackOpaque) {
|
|
assert_eq!(core::mem::size_of::<CallbackOpaque>(), core::mem::size_of::<Callback<()>>());
|
|
unsafe { core::ptr::write(out as *mut Callback<()>, Default::default()) };
|
|
}
|
|
|
|
/// Emit the callback
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn slint_callback_call(
|
|
sig: *const CallbackOpaque,
|
|
arg: *const c_void,
|
|
ret: *mut c_void,
|
|
) {
|
|
unsafe {
|
|
let sig = &*(sig as *const Callback<c_void, c_void>);
|
|
if let Some(mut h) = sig.handler.take() {
|
|
h(&*arg, &mut *ret);
|
|
assert!(sig.handler.take().is_none(), "Callback Handler set while called");
|
|
sig.handler.set(Some(h));
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Set callback handler.
|
|
///
|
|
/// The binding has signature fn(user_data)
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn slint_callback_set_handler(
|
|
sig: *const CallbackOpaque,
|
|
binding: extern "C" fn(user_data: *mut c_void, arg: *const c_void, ret: *mut c_void),
|
|
user_data: *mut c_void,
|
|
drop_user_data: Option<extern "C" fn(*mut c_void)>,
|
|
) {
|
|
unsafe {
|
|
let sig = &mut *(sig as *mut Callback<c_void, c_void>);
|
|
|
|
struct UserData {
|
|
user_data: *mut c_void,
|
|
drop_user_data: Option<extern "C" fn(*mut c_void)>,
|
|
binding:
|
|
extern "C" fn(user_data: *mut c_void, arg: *const c_void, ret: *mut c_void),
|
|
}
|
|
|
|
impl Drop for UserData {
|
|
fn drop(&mut self) {
|
|
if let Some(x) = self.drop_user_data {
|
|
x(self.user_data)
|
|
}
|
|
}
|
|
}
|
|
|
|
impl UserData {
|
|
/// Safety: the arguments must be valid pointers
|
|
unsafe fn call(&self, arg: *const c_void, ret: *mut c_void) {
|
|
(self.binding)(self.user_data, arg, ret)
|
|
}
|
|
}
|
|
|
|
let ud = UserData { user_data, drop_user_data, binding };
|
|
sig.handler.set(Some(Box::new(move |a, r| ud.call(a, r))));
|
|
}
|
|
}
|
|
|
|
/// Destroy callback
|
|
#[unsafe(no_mangle)]
|
|
pub unsafe extern "C" fn slint_callback_drop(handle: *mut CallbackOpaque) {
|
|
unsafe { core::ptr::drop_in_place(handle as *mut Callback<c_void, c_void>) };
|
|
}
|
|
}
|