gallery/vendor/i-slint-renderer-skia/cached_image.rs
Greg Pomerantz 93b27d724b Root-cause and fix the Mali scroll jank; strip jank-hunt instrumentation
The 19-26 ms mid-scroll stalls (Mali-only, fixed content offsets) were
the Skia renderer's final synchronous GrDirectContext::flush() blocking
the UI thread on Mali driver back-pressure — proven by per-stage
timestamps in the vendored renderer (stall frames: flush 19-24 ms,
walk 1-3 ms, 1 binding re-eval, image imports 0.1 ms each).

- vendor/i-slint-renderer-skia (pinned 1.18.1, [patch.crates-io]):
  remove that one flush. Redundant on the wgpu backend: the surface
  already calls flush_and_submit before queue.present, same Vulkan
  queue, in-order. Phone: 0 stalls across 4 runs (max 3.6-7.4 ms vs
  the 8.3 ms budget), display verified clean.
- docs/BUGS/slint-skia-final-flush-jank.md: full evidence, prior art
  (slint-ui/slint#12030 closest), open question for a future
  upstream filing (not filed yet).
- Profiling also retired the O(n)-walk theory: Slint 1.18 prunes the
  item walk to ~133-152 items; the windowed Flickable built on that
  theory was already reverted (ListView is smooth with the fix).
- Strip all jank-hunt instrumentation: FR/SCR/TICK/APLYD/STG logging,
  stg markers, .gallery_* file knobs, jsamp thread sampler,
  DbgScroll programmatic scroll, nblog perf-file mirroring.
  Also fixes the 'launches into unstoppable fast scroll' bug: a
  leftover .gallery_cmd was re-read on every launch (file never
  deleted) and overrode user swipes — the hook is gone.
- Keep: nblog (logd backpressure is real), zero-dimension decode
  guards, keep-alive (default-off).
- HANDOFF: replace stale windowed-Flickable section with the actual
  root cause/fix; 15 unit tests pass.

Verified on phone (aarch64, 8580 photos) and emulator (x86_64).
2026-10-28 18:23:50 -04:00

157 lines
5.9 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 i_slint_core::graphics::{
Image, ImageCacheKey, ImageInner, IntSize, OpaqueImage, OpaqueImageVTable, SharedImageBuffer,
cache as core_cache,
};
use i_slint_core::items::ImageFit;
use i_slint_core::lengths::{LogicalSize, ScaleFactor};
struct SkiaCachedImage {
image: skia_safe::Image,
cache_key: ImageCacheKey,
}
i_slint_core::OpaqueImageVTable_static! {
static SKIA_CACHED_IMAGE_VT for SkiaCachedImage
}
impl OpaqueImage for SkiaCachedImage {
fn size(&self) -> IntSize {
IntSize::new(self.image.width() as u32, self.image.height() as u32)
}
fn cache_key(&self) -> ImageCacheKey {
self.cache_key.clone()
}
}
pub(crate) fn as_skia_image(
image: Image,
target_size_fn: &dyn Fn() -> LogicalSize,
image_fit: ImageFit,
scale_factor: ScaleFactor,
canvas: &skia_safe::Canvas,
surface: Option<&dyn crate::Surface>,
) -> Option<skia_safe::Image> {
let image_inner: &ImageInner = (&image).into();
match image_inner {
ImageInner::None => None,
ImageInner::EmbeddedImage { buffer, cache_key } => {
let result = image_buffer_to_skia_image(buffer);
if let Some(img) = result.as_ref() {
core_cache::replace_cached_image(
cache_key.clone(),
ImageInner::BackendStorage(vtable::VRc::into_dyn(vtable::VRc::new(
SkiaCachedImage { image: img.clone(), cache_key: cache_key.clone() },
))),
)
}
result
}
ImageInner::Svg(svg) => {
// Query target_width/height here again to ensure that changes will invalidate the item rendering cache.
let target_size = i_slint_core::graphics::scalable_render_size(
svg.size(),
image_fit,
target_size_fn() * scale_factor,
scale_factor,
Default::default(),
)?;
let pixels = match svg.render(Some(target_size)).ok()? {
SharedImageBuffer::RGB8(_) => unreachable!(),
SharedImageBuffer::RGBA8(_) => unreachable!(),
SharedImageBuffer::RGBA8Premultiplied(pixels) => pixels,
};
let image_info = crate::image_info(
skia_safe::ISize::new(pixels.width() as i32, pixels.height() as i32),
skia_safe::ColorType::RGBA8888,
skia_safe::AlphaType::Premul,
);
skia_safe::images::raster_from_data(
&image_info,
skia_safe::Data::new_copy(pixels.as_bytes()),
pixels.width() as usize * 4,
)
}
ImageInner::StaticTextures(_) => todo!(),
ImageInner::BackendStorage(x) => {
vtable::VRc::borrow(x).downcast::<SkiaCachedImage>().map(|x| x.image.clone())
}
ImageInner::BorrowedOpenGLTexture(texture) => {
surface.and_then(|surface| surface.import_opengl_texture(canvas, texture))
}
ImageInner::NineSlice(n) => as_skia_image(
n.image(),
target_size_fn,
ImageFit::Preserve,
scale_factor,
canvas,
surface,
),
#[cfg(any(feature = "unstable-wgpu-29", feature = "unstable-wgpu-30"))]
ImageInner::WGPUTexture(any_wgpu_texture) => {
surface.and_then(|surface| surface.import_wgpu_texture(canvas, any_wgpu_texture))
}
#[allow(unreachable_patterns)]
_ => None,
}
}
fn image_buffer_to_skia_image(buffer: &SharedImageBuffer) -> Option<skia_safe::Image> {
// Report fully opaque images as such to Skia. This allows Skia to skip
// reading destination pixels back when blending, which is particularly
// important for the software renderer when it renders directly into
// write-combined (uncached) frame buffer memory, where CPU reads are an
// order of magnitude slower than writes. The scan happens once per image;
// the resulting skia_safe::Image is cached.
fn opaque_or(rgba_bytes: &[u8], alpha_type: skia_safe::AlphaType) -> skia_safe::AlphaType {
if rgba_bytes.as_chunks::<4>().0.iter().all(|px| px[3] == 255) {
skia_safe::AlphaType::Opaque
} else {
alpha_type
}
}
let (data, bpl, size, color_type, alpha_type) = match buffer {
SharedImageBuffer::RGB8(pixels) => {
// RGB888 with one byte per component is not supported by Skia right now. Convert once to RGBA8 :-(
let rgba = pixels
.as_bytes()
.chunks(3)
.flat_map(|rgb| IntoIterator::into_iter([rgb[0], rgb[1], rgb[2], 255]))
.collect::<Vec<u8>>();
(
skia_safe::Data::new_copy(&rgba),
pixels.width() as usize * 4,
pixels.size(),
skia_safe::ColorType::RGBA8888,
skia_safe::AlphaType::Opaque,
)
}
SharedImageBuffer::RGBA8(pixels) => (
skia_safe::Data::new_copy(pixels.as_bytes()),
pixels.width() as usize * 4,
pixels.size(),
skia_safe::ColorType::RGBA8888,
opaque_or(pixels.as_bytes(), skia_safe::AlphaType::Unpremul),
),
SharedImageBuffer::RGBA8Premultiplied(pixels) => (
skia_safe::Data::new_copy(pixels.as_bytes()),
pixels.width() as usize * 4,
pixels.size(),
skia_safe::ColorType::RGBA8888,
opaque_or(pixels.as_bytes(), skia_safe::AlphaType::Premul),
),
};
let image_info = crate::image_info(
skia_safe::ISize::new(size.width as i32, size.height as i32),
color_type,
alpha_type,
);
skia_safe::images::raster_from_data(&image_info, data, bpl)
}