Skip to content

Commit b764c12

Browse files
only21milclaude
andcommitted
perf(desktop): encode Linux capture PNGs at a fast deflate level
The Hyprland and KDE capture helpers wrote capture.png at the png crate's default level 6, and that encode was most of the helper's time. Level 2 is lossless. On a 4K Hyprland window the bundled helper went from 1914 ms to 301 ms for a photo and from 236 ms to 136 ms for the T3 Code window, for 4-11% larger PNGs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 05696c6 commit b764c12

4 files changed

Lines changed: 22 additions & 0 deletions

File tree

‎native/hyprland-snap-shot/src/capture.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ pub(super) fn capture_on(connection: Connection, address: u64, directory: &Path)
148148
let mut encoder = png::Encoder::new(BufWriter::new(file), pixels.width, pixels.height);
149149
encoder.set_color(png::ColorType::Rgba);
150150
encoder.set_depth(png::BitDepth::Eight);
151+
// The default level 6 dominated capture time. Level 2 is lossless and much faster.
152+
encoder.set_deflate_compression(png::DeflateCompression::Level(2));
151153
encoder.write_header()?.write_image_data(&rgba)?;
152154
Ok(())
153155
}

‎native/hyprland-snap-shot/src/transport_tests.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,13 @@ impl Dispatch<Frame, ()> for State {
350350
}
351351
}
352352

353+
/// FLEVEL from the IDAT zlib header: 1 for levels 2-5, 2 for the slow default level 6.
354+
const FAST_ZLIB_LEVEL: u8 = 1;
355+
fn zlib_level(png: &[u8]) -> u8 {
356+
let idat = png.windows(4).position(|tag| tag == b"IDAT").unwrap();
357+
png[idat + 5] >> 6
358+
}
359+
353360
#[test]
354361
fn exports_exact_window_over_real_fd_transport() {
355362
let (fixture, connection) = Fixture::start(Mode::Success);
@@ -364,6 +371,8 @@ fn exports_exact_window_over_real_fd_transport() {
364371
let mut pixels = vec![0; reader.output_buffer_size().unwrap()];
365372
reader.next_frame(&mut pixels).unwrap();
366373
assert_eq!(pixels, [10, 20, 30, 255, 40, 50, 60, 255]);
374+
let png = std::fs::read(directory.path().join("capture.png")).unwrap();
375+
assert_eq!(zlib_level(&png), FAST_ZLIB_LEVEL);
367376
}
368377
#[test]
369378
fn failed_export_produces_no_image_and_no_second_capture() {

‎native/kde-snap-shot/src/main.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ fn capture(connection: &Connection, directory: &Path) -> Result<Option<Window>>
306306
let mut encoder = png::Encoder::new(image, width as u32, height as u32);
307307
encoder.set_color(png::ColorType::Rgba);
308308
encoder.set_depth(png::BitDepth::Eight);
309+
// The default level 6 dominated capture time. Level 2 is lossless and much faster.
310+
encoder.set_deflate_compression(png::DeflateCompression::Level(2));
309311
let mut image = encoder.write_header()?;
310312
image.write_image_data(&pixels)?;
311313
image.finish()?;

‎native/kde-snap-shot/src/transport_tests.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ impl Drop for Fixture {
207207
}
208208
}
209209

210+
/// FLEVEL from the IDAT zlib header: 1 for levels 2-5, 2 for the slow default level 6.
211+
const FAST_ZLIB_LEVEL: u8 = 1;
212+
fn zlib_level(png: &[u8]) -> u8 {
213+
let idat = png.windows(4).position(|tag| tag == b"IDAT").unwrap();
214+
png[idat + 5] >> 6
215+
}
216+
210217
#[test]
211218
fn permission_probe_never_captures_and_rejects_denial() {
212219
let f = Fixture::new();
@@ -248,6 +255,8 @@ fn captures_pinned_window_through_real_fd_and_cleans_scripts() {
248255
let mut pixels = vec![0; decoder.output_buffer_size().unwrap()];
249256
decoder.next_frame(&mut pixels).unwrap();
250257
assert_eq!(pixels, [0x11, 0x22, 0x33, 255, 0x44, 0x55, 0x66, 255]);
258+
let png = std::fs::read(f.directory.join("capture.png")).unwrap();
259+
assert_eq!(zlib_level(&png), FAST_ZLIB_LEVEL);
251260
}
252261

253262
#[test]

0 commit comments

Comments
 (0)