Skip to content
Open
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
52 changes: 52 additions & 0 deletions apps/desktop-gpui/kernels/benches/frame_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ fn mixed_rgba(pixels: usize) -> Vec<u8> {
rgba
}

fn transparent_rgba(pixels: usize) -> Vec<u8> {
let mut rgba = Vec::with_capacity(pixels * 4);
for i in 0..pixels {
rgba.extend_from_slice(&[
(i & 0xFF) as u8,
((i >> 8) & 0xFF) as u8,
(i.wrapping_mul(17) & 0xFF) as u8,
0,
]);
}
rgba
}

fn fused_two_step(rgba: &[u8]) -> Vec<u8> {
baseline::rgba_to_rgb(&baseline::flatten_onto_white(rgba))
}
Expand Down Expand Up @@ -73,6 +86,20 @@ fn run_fused(name: &str, rgba: &[u8]) {
);
}

fn run_presized(name: &str, rgba: &[u8]) {
assert_eq!(
baseline::flatten_rgba_to_rgb_over_white(rgba),
candidate_codec::flatten_rgba_to_rgb_over_white(rgba),
"{name}"
);
emit(
name,
baseline::flatten_rgba_to_rgb_over_white,
candidate_codec::flatten_rgba_to_rgb_over_white,
rgba,
);
}

fn emit<T>(name: &str, before: fn(T) -> Vec<u8>, after: fn(T) -> Vec<u8>, input: T)
where
T: Copy,
Expand Down Expand Up @@ -103,13 +130,17 @@ where
fn main() {
let hd = opaque_rgba(1920 * 1080);
let uhd_opaque = opaque_rgba(3840 * 2160);
let uhd_transparent = transparent_rgba(3840 * 2160);
let uhd_mixed = mixed_rgba(3840 * 2160);
println!("workload,sample,before_ns,after_ns");
run_flatten("flatten-opaque-1080p", &hd);
run_flatten("flatten-opaque-4k", &uhd_opaque);
run_flatten("flatten-mixed-4k", &uhd_mixed);
run_strip("rgba-strip-4k", &uhd_opaque);
run_fused("export-fused-4k", &uhd_mixed);
run_presized("flatten-rgb-opaque-4k", &uhd_opaque);
run_presized("flatten-rgb-transparent-4k", &uhd_transparent);
run_presized("flatten-rgb-mixed-4k", &uhd_mixed);
}

mod baseline {
Expand All @@ -130,4 +161,25 @@ mod baseline {
}
rgba
}

pub fn flatten_rgba_to_rgb_over_white(rgba: &[u8]) -> Vec<u8> {
let mut rgb = Vec::with_capacity(rgba.len() / 4 * 3);
for px in rgba.chunks_exact(4) {
let alpha = px[3];
if alpha == 255 {
rgb.extend_from_slice(&[px[0], px[1], px[2]]);
} else if alpha == 0 {
rgb.extend_from_slice(&[255, 255, 255]);
} else {
let a = u32::from(alpha);
let inv = 255 - a;
rgb.extend_from_slice(&[
((u32::from(px[0]) * a + 255 * inv + 127) / 255) as u8,
((u32::from(px[1]) * a + 255 * inv + 127) / 255) as u8,
((u32::from(px[2]) * a + 255 * inv + 127) / 255) as u8,
]);
}
}
rgb
}
}
84 changes: 75 additions & 9 deletions apps/desktop-gpui/kernels/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,29 @@ pub fn rgba_to_rgb(rgba: &[u8]) -> Vec<u8> {
}

pub fn flatten_rgba_to_rgb_over_white(rgba: &[u8]) -> Vec<u8> {
let mut rgb = Vec::with_capacity(rgba.len() / 4 * 3);
let mut dst = vec![0u8; rgba.len() / 4 * 3];
let mut i = 0;
for px in rgba.chunks_exact(4) {
let alpha = px[3];
let out = &mut dst[i..i + 3];
if alpha == 255 {
rgb.extend_from_slice(&[px[0], px[1], px[2]]);
out[0] = px[0];
out[1] = px[1];
out[2] = px[2];
} else if alpha == 0 {
rgb.extend_from_slice(&[255, 255, 255]);
out[0] = 255;
out[1] = 255;
out[2] = 255;
} else {
let a = u32::from(alpha);
let inv = 255 - a;
rgb.extend_from_slice(&[
((u32::from(px[0]) * a + 255 * inv + 127) / 255) as u8,
((u32::from(px[1]) * a + 255 * inv + 127) / 255) as u8,
((u32::from(px[2]) * a + 255 * inv + 127) / 255) as u8,
]);
out[0] = ((u32::from(px[0]) * a + 255 * inv + 127) / 255) as u8;
out[1] = ((u32::from(px[1]) * a + 255 * inv + 127) / 255) as u8;
out[2] = ((u32::from(px[2]) * a + 255 * inv + 127) / 255) as u8;
}
i += 3;
}
rgb
dst
}

pub fn encode_png_rgba(rgba: &[u8], width: u32, height: u32) -> image::ImageResult<Vec<u8>> {
Expand Down Expand Up @@ -246,6 +251,67 @@ mod tests {
);
}

fn extend_flatten_rgba_to_rgb_over_white(rgba: &[u8]) -> Vec<u8> {
let mut rgb = Vec::with_capacity(rgba.len() / 4 * 3);
for px in rgba.chunks_exact(4) {
let alpha = px[3];
if alpha == 255 {
rgb.extend_from_slice(&[px[0], px[1], px[2]]);
} else if alpha == 0 {
rgb.extend_from_slice(&[255, 255, 255]);
} else {
let a = u32::from(alpha);
let inv = 255 - a;
rgb.extend_from_slice(&[
((u32::from(px[0]) * a + 255 * inv + 127) / 255) as u8,
((u32::from(px[1]) * a + 255 * inv + 127) / 255) as u8,
((u32::from(px[2]) * a + 255 * inv + 127) / 255) as u8,
]);
}
}
rgb
}

fn rgba_with_alpha(pixels: usize, alpha_of: impl Fn(usize) -> u8) -> Vec<u8> {
let mut rgba = Vec::with_capacity(pixels * 4);
for i in 0..pixels {
rgba.extend_from_slice(&[
(i & 0xff) as u8,
((i >> 4) & 0xff) as u8,
i.wrapping_mul(17) as u8,
alpha_of(i),
]);
}
rgba
}

#[test]
fn flatten_rgba_to_rgb_over_white_matches_extend_formula() {
assert_eq!(flatten_rgba_to_rgb_over_white(&[1, 2, 3, 255]), [1, 2, 3]);
assert_eq!(
flatten_rgba_to_rgb_over_white(&[1, 2, 3, 0]),
[255, 255, 255]
);
assert_eq!(
flatten_rgba_to_rgb_over_white(&[10, 0, 255, 128]),
[132, 127, 255]
);

let opaque = rgba_with_alpha(256, |_| 255);
let transparent = rgba_with_alpha(256, |_| 0);
let mixed = rgba_with_alpha(256, |i| match i % 3 {
0 => 0,
1 => 255,
_ => (i * 17) as u8,
});
for rgba in [&opaque[..], &transparent[..], &mixed[..]] {
assert_eq!(
flatten_rgba_to_rgb_over_white(rgba),
extend_flatten_rgba_to_rgb_over_white(rgba)
);
}
}

#[test]
fn png_round_trips_rgba() {
let rgba = [10, 20, 30, 40, 50, 60, 70, 255];
Expand Down