Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion native/engine/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ pub fn png(data: &[u8], stride: usize, x: usize, y: usize, w: usize, h: usize, a
let mut encoder = png::Encoder::new(&mut out, w as u32, h as u32);
encoder.set_color(if alpha { png::ColorType::Rgba } else { png::ColorType::Rgb });
encoder.set_depth(png::BitDepth::Eight);
encoder.set_compression(png::Compression::Fast);
// fdeflate's fast modes skip LZ matching, so text and UI barely shrink.
// zlib level 1 costs a few ms more per frame and sends about a quarter.
encoder.set_deflate_compression(png::DeflateCompression::Level(1));
encoder.set_filter(png::Filter::Adaptive);
let Ok(mut writer) = encoder.write_header() else {
return Vec::new();
Expand Down
Binary file modified src/backend/engine/wasm/engine_bg.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion src/backend/engine/wasm/source-hash.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
435f311bd716cf0f7cacbe3788858e32e9f807bc86ef0a6fdf2c535fe2bdcec5
5b669dae3affcb6e713ba7dadc0be397234812a07c369e9cedc8d72e58c9d49f
20 changes: 20 additions & 0 deletions tests/backend/engine/wasm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,24 @@ describe("engine module", () => {
surface.copy_rect(0, 0, 2, 2, 2, 2);
surface.free();
});

it("compresses repeating screen content in PNG frames", () => {
const wasm = loadWasm(new WebAssembly.Module(wasmBytes as BufferSource));
const size = 256;
const surface = new wasm.Surface(size, size);
const pixels = surfaceView(wasm, surface);
// Dark marks repeating on a light background, like lines of text.
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
const i = (y * size + x) * 4;
const value = ((x % 8) * 3 + (y % 12) * 5) % 7 < 2 ? 20 : 235;
pixels.fill(value, i, i + 3);
pixels[i + 3] = 255;
}
}
const png = surface.encode_png(0, 0, size, size, false);
// Huffman-only deflate kept this near raw size, a frame's worth per repaint.
expect(png.length).toBeLessThan((size * size * 3) / 4);
surface.free();
});
});