Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(desktop-gpui): downsample tray PNG thumbnails at decode

Khushal Bhardwaj6dbce16cfc1f
diff --git a/apps/desktop-gpui/src/library.rs b/apps/desktop-gpui/src/library.rs
index dc57098a7..ee53c102f 100644
--- a/apps/desktop-gpui/src/library.rs
+++ b/apps/desktop-gpui/src/library.rs
@@ -1060,6 +1060,11 @@ impl CacheSlot {
}
}
+#[cfg(test)]
+pub fn thumbnail_cache_file(source: &Path) -> Option<PathBuf> {
+ CacheSlot::for_source(source).map(|slot| slot.file())
+}
+
/// gpui's atlas takes BGRA; `image`'s RgbaImage is just the container (the
/// same swap gpui's own asset loader does after decoding).
pub fn rgba_to_render_image(mut rgba: image::RgbaImage) -> Arc<RenderImage> {
diff --git a/apps/desktop-gpui/src/tray.rs b/apps/desktop-gpui/src/tray.rs
index 001a6d2a8..0232da3d0 100644
--- a/apps/desktop-gpui/src/tray.rs
+++ b/apps/desktop-gpui/src/tray.rs
@@ -173,17 +173,20 @@ pub fn cover_crop_geometry(orig_w: u32, orig_h: u32, size: u32) -> (u32, u32, u3
/// `load_thumbnail_data`, ending in a PNG rather than raw RGBA -- muda hands
/// its icon to AppKit as a PNG too (`PlatformIcon::to_nsimage`), so this is the
-/// same conversion one step earlier. A JPEG source starts from the Recents card
+/// same conversion one step earlier. An opaque source (a recording's JPEG, a
+/// display or area screenshot's RGB PNG) starts from the Recents card
/// thumbnail cache instead of the native-resolution original. That cache is
-/// itself JPEG and drops alpha, so anything else (a transparent screenshot PNG)
-/// still decodes directly.
+/// itself JPEG and drops alpha, so a source that can carry alpha (a window
+/// screenshot's rounded-corner RGBA PNG) still decodes directly.
fn load_thumbnail_png(path: &Path) -> Option<Vec<u8>> {
- use image::{GenericImageView, ImageEncoder, ImageFormat, RgbaImage, imageops::FilterType};
-
- let img = if ImageFormat::from_path(path).ok() == Some(ImageFormat::Jpeg) {
- image::DynamicImage::ImageRgba8(library::thumbnail_rgba(path)?)
- } else {
- image::load_from_memory(&std::fs::read(path).ok()?).ok()?
+ use image::{GenericImageView, ImageEncoder, RgbaImage, imageops::FilterType};
+
+ let cached = (!source_may_have_alpha(path))
+ .then(|| library::thumbnail_rgba(path))
+ .flatten();
+ let img = match cached {
+ Some(rgba) => image::DynamicImage::ImageRgba8(rgba),
+ None => image::load_from_memory(&std::fs::read(path).ok()?).ok()?,
};
let (orig_w, orig_h) = img.dimensions();
if orig_w == 0 || orig_h == 0 {
@@ -211,6 +214,24 @@ fn load_thumbnail_png(path: &Path) -> Option<Vec<u8>> {
Some(png)
}
+/// Reads only the signature and, for a PNG, the header chunks: the color type
+/// says whether the pixels can carry alpha without decoding any of them.
+fn source_may_have_alpha(path: &Path) -> bool {
+ use image::{ImageDecoder, ImageFormat, ImageReader};
+
+ let Ok(reader) = ImageReader::open(path).and_then(ImageReader::with_guessed_format) else {
+ return true;
+ };
+ match reader.format() {
+ Some(ImageFormat::Jpeg) => false,
+ Some(ImageFormat::Png) => reader
+ .into_decoder()
+ .ok()
+ .is_none_or(|decoder| decoder.color_type().has_alpha()),
+ _ => true,
+ }
+}
+
/// `load_all_previous_items` + the background thumbnail pass, in one call.
///
/// Reuses the library scan the main window's Recents runs on, so the two lists
@@ -1261,6 +1282,8 @@ mod tests {
let icon = image::load_from_memory(&load_thumbnail_png(&png).unwrap())
.unwrap()
.into_rgba8();
+ assert_eq!(icon.dimensions(), (THUMBNAIL_SIZE, THUMBNAIL_SIZE));
+ assert!(icon.get_pixel(0, 0)[3] < 8, "{scan} scan lost the corner");
assert!(
icon.get_pixel(2, 16)[3] < 8,
"{scan} scan lost the transparent half"
@@ -1270,6 +1293,50 @@ mod tests {
"{scan} scan lost the opaque half"
);
}
+ let cache = library::thumbnail_cache_file(&png).unwrap();
+ assert!(
+ !cache.exists(),
+ "a transparent PNG never goes through the alpha-dropping cache"
+ );
+ std::fs::remove_dir_all(&root).ok();
+ }
+
+ #[test]
+ fn previous_thumbnails_reuse_the_cache_for_an_opaque_png() {
+ let root = temp_root("opaque-png");
+ std::fs::create_dir_all(&root).unwrap();
+ let png = root.join("screenshot.png");
+ image::RgbImage::from_pixel(1280, 720, image::Rgb([20, 120, 220]))
+ .save_with_format(&png, image::ImageFormat::Png)
+ .unwrap();
+ let cache = library::thumbnail_cache_file(&png).unwrap();
+ std::fs::remove_file(&cache).ok();
+
+ let cold = image::load_from_memory(&load_thumbnail_png(&png).unwrap())
+ .unwrap()
+ .into_rgba8();
+ assert_eq!(cold.dimensions(), (THUMBNAIL_SIZE, THUMBNAIL_SIZE));
+ assert_eq!(cold.get_pixel(0, 0)[3], 255);
+ assert!(cache.exists(), "the first scan fills the shared cache");
+
+ let marker = image::Rgb([200, 30, 90]);
+ image::RgbImage::from_pixel(398, 224, marker)
+ .save_with_format(&cache, image::ImageFormat::Jpeg)
+ .unwrap();
+ let warm = image::load_from_memory(&load_thumbnail_png(&png).unwrap())
+ .unwrap()
+ .into_rgba8();
+ assert_eq!(warm.dimensions(), (THUMBNAIL_SIZE, THUMBNAIL_SIZE));
+ assert!(
+ warm.pixels().all(|pixel| pixel.0[..3]
+ .iter()
+ .zip(marker.0)
+ .all(|(a, b)| a.abs_diff(b) <= 3)
+ && pixel.0[3] == 255),
+ "the warm icon is built from the cached thumbnail, not the original"
+ );
+
+ std::fs::remove_file(&cache).ok();
std::fs::remove_dir_all(&root).ok();
}