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

216 lines
7.6 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 crate::BorderRadius;
use crate::Coord;
/// This type is used as a tagging type for use with [`euclid::Scale`] to convert
/// between physical and logical pixels.
pub struct PhysicalPx;
/// This type is used as a tagging type for use with [`euclid::Scale`] to convert
/// between physical and logical pixels.
pub struct LogicalPx;
pub type LogicalLength = euclid::Length<Coord, LogicalPx>;
pub type LogicalRect = euclid::Rect<Coord, LogicalPx>;
pub type LogicalPoint = euclid::Point2D<Coord, LogicalPx>;
pub type LogicalSize = euclid::Size2D<Coord, LogicalPx>;
pub type LogicalVector = euclid::Vector2D<Coord, LogicalPx>;
pub type LogicalBorderRadius = BorderRadius<Coord, LogicalPx>;
pub type PhysicalBorderRadius = BorderRadius<f32, PhysicalPx>;
pub type ItemTransform = euclid::Transform2D<f32, LogicalPx, LogicalPx>;
pub type ScaleFactor = euclid::Scale<f32, LogicalPx, PhysicalPx>;
pub trait SizeLengths {
type LengthType;
fn width_length(&self) -> Self::LengthType;
fn height_length(&self) -> Self::LengthType;
}
impl<T: Copy, U> SizeLengths for euclid::Size2D<T, U> {
type LengthType = euclid::Length<T, U>;
fn width_length(&self) -> Self::LengthType {
euclid::Length::new(self.width)
}
fn height_length(&self) -> Self::LengthType {
euclid::Length::new(self.height)
}
}
pub trait PointLengths {
type LengthType;
fn x_length(&self) -> Self::LengthType;
fn y_length(&self) -> Self::LengthType;
}
impl<T: Copy, U> PointLengths for euclid::Point2D<T, U> {
type LengthType = euclid::Length<T, U>;
fn x_length(&self) -> Self::LengthType {
euclid::Length::new(self.x)
}
fn y_length(&self) -> Self::LengthType {
euclid::Length::new(self.y)
}
}
impl<T: Copy, U> PointLengths for euclid::Vector2D<T, U> {
type LengthType = euclid::Length<T, U>;
fn x_length(&self) -> Self::LengthType {
euclid::Length::new(self.x)
}
fn y_length(&self) -> Self::LengthType {
euclid::Length::new(self.y)
}
}
pub trait RectLengths {
type SizeType;
type LengthType;
fn size_length(&self) -> Self::SizeType;
fn width_length(&self) -> Self::LengthType;
fn height_length(&self) -> Self::LengthType;
/// The typed horizontal range, unlike euclid's unitless [`euclid::Rect::x_range`].
fn x_length_range(&self) -> core::ops::Range<Self::LengthType>;
}
impl<T: Copy + core::ops::Add<Output = T>, U> RectLengths for euclid::Rect<T, U> {
type LengthType = euclid::Length<T, U>;
type SizeType = euclid::Size2D<T, U>;
fn size_length(&self) -> Self::SizeType {
euclid::Size2D::new(self.size.width, self.size.height)
}
fn width_length(&self) -> Self::LengthType {
self.size_length().width_length()
}
fn height_length(&self) -> Self::LengthType {
self.size_length().height_length()
}
fn x_length_range(&self) -> core::ops::Range<Self::LengthType> {
self.origin.x_length()..self.origin.x_length() + self.width_length()
}
}
/// Convert from the api size to the internal size
/// (This doesn't use the `From` trait because it would expose the conversion to euclid in the public API)
pub fn logical_size_from_api(size: crate::api::LogicalSize) -> LogicalSize {
size.to_euclid()
}
pub fn logical_point_from_api(position: crate::api::LogicalPosition) -> LogicalPoint {
position.to_euclid()
}
pub fn logical_position_to_api(pos: LogicalPoint) -> crate::api::LogicalPosition {
crate::api::LogicalPosition::from_euclid(pos)
}
pub fn logical_size_to_api(size: LogicalSize) -> crate::api::LogicalSize {
crate::api::LogicalSize::from_euclid(size)
}
/// Four distances from the edges of a rectangle represented in the coordinate space of logical pixels.
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(C)]
pub struct LogicalEdges {
/// The top distance in logical pixels.
pub top: f32,
/// The bottom distance in logical pixels.
pub bottom: f32,
/// The left distance in logical pixels.
pub left: f32,
/// The right distance in logical pixels.
pub right: f32,
}
impl LogicalEdges {
/// Construct a new logical edges struct from the given border values, that are assumed to be
/// in the logical coordinate space.
pub const fn new(top: f32, bottom: f32, left: f32, right: f32) -> Self {
Self { top, bottom, left, right }
}
/// Converts the top edge to logical pixels.
#[inline]
pub const fn top(&self) -> LogicalLength {
LogicalLength::new(self.top as crate::Coord)
}
/// Converts the bottom edge to logical pixels.
#[inline]
pub const fn bottom(&self) -> LogicalLength {
LogicalLength::new(self.bottom as crate::Coord)
}
/// Converts the left edge to logical pixels.
#[inline]
pub const fn left(&self) -> LogicalLength {
LogicalLength::new(self.left as crate::Coord)
}
/// Converts the right edge to logical pixels.
#[inline]
pub const fn right(&self) -> LogicalLength {
LogicalLength::new(self.right as crate::Coord)
}
}
/// Four distances from the edges of a rectangle represented in the coordinate space of physical pixels.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PhysicalEdges {
/// The top edge in physical pixels.
pub top: i32,
/// The bottom edge in physical pixels.
pub bottom: i32,
/// The left edge in physical pixels.
pub left: i32,
/// The right edge in physical pixels.
pub right: i32,
}
impl PhysicalEdges {
/// Construct a new physical edges struct from the given border values, that are assumed to be
/// in the physical coordinate space.
pub const fn new(top: i32, bottom: i32, left: i32, right: i32) -> Self {
Self { top, bottom, left, right }
}
/// Convert a given logical edges to a physical edges by dividing the lengths by the
/// specified scale factor.
#[inline]
pub const fn to_logical(&self, scale_factor: f32) -> LogicalEdges {
LogicalEdges::new(
self.top_to_logical(scale_factor).0 as f32,
self.bottom_to_logical(scale_factor).0 as f32,
self.left_to_logical(scale_factor).0 as f32,
self.right_to_logical(scale_factor).0 as f32,
)
}
/// Convert the top logical edge to a physical edge by dividing the length by the
/// specified scale factor.
#[inline]
pub const fn top_to_logical(&self, scale_factor: f32) -> LogicalLength {
LogicalLength::new((self.top as f32 / scale_factor) as crate::Coord)
}
/// Convert the bottom logical edge to a physical edge by dividing the length by the
/// specified scale factor.
#[inline]
pub const fn bottom_to_logical(&self, scale_factor: f32) -> LogicalLength {
LogicalLength::new((self.bottom as f32 / scale_factor) as crate::Coord)
}
#[inline]
/// Convert the left logical edge to a physical edge by dividing the length by the
/// specified scale factor.
pub const fn left_to_logical(&self, scale_factor: f32) -> LogicalLength {
LogicalLength::new((self.left as f32 / scale_factor) as crate::Coord)
}
/// Convert the right logical edge to a physical edge by dividing the length by the
/// specified scale factor.
#[inline]
pub const fn right_to_logical(&self, scale_factor: f32) -> LogicalLength {
LogicalLength::new((self.right as f32 / scale_factor) as crate::Coord)
}
}