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: 4 additions & 0 deletions apps/desktop-gpui/kernels/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ harness = false
name = "frame_unpad"
harness = false

[[bench]]
name = "frame_copy_rows"
harness = false

[[bench]]
name = "screenshot_mask"
harness = false
Expand Down
84 changes: 84 additions & 0 deletions apps/desktop-gpui/kernels/benches/frame_copy_rows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use cap_gpui_kernels::frame as candidate;
use std::{hint::black_box, time::Instant};

type CopyRows = fn(&[u8], usize, usize, usize, bool) -> Option<Vec<u8>>;

fn main() {
println!("workload,sample,before_ns,after_ns");
for (name, width, height, bytes_per_pixel, stride_align, mirrored) in [
(
"camera-720p-mirrored",
1280usize,
720usize,
4usize,
64usize,
true,
),
("camera-1080p-mirrored", 1920, 1080, 4, 64, true),
("camera-720p", 1280, 720, 4, 64, false),
("camera-1080p", 1920, 1080, 4, 64, false),
("import-4k-rgba", 3840, 2160, 4, 64, false),
("library-thumb-rgb24", 1280, 720, 3, 64, false),
("clip-thumb-bgra-padded", 1366, 768, 4, 256, false),
] {
let row_bytes = width * bytes_per_pixel;
let stride = row_bytes.next_multiple_of(stride_align);
let src: Vec<u8> = (0..stride * height)
.map(|i| (i * 31 + i / 7) as u8)
.collect();
let before = baseline::copy_rows(&src, row_bytes, stride, height, mirrored).unwrap();
let after = candidate::copy_rows(&src, row_bytes, stride, height, mirrored).unwrap();
assert_eq!(before, after, "{name}");
for sample in 0..35 {
let run = |f: CopyRows| {
let start = Instant::now();
let _ = black_box(f(
black_box(&src),
black_box(row_bytes),
black_box(stride),
black_box(height),
black_box(mirrored),
));
start.elapsed().as_nanos()
};
let (old, new) = if sample % 2 == 0 {
let old = run(baseline::copy_rows);
(old, run(candidate::copy_rows))
} else {
let new = run(candidate::copy_rows);
(run(baseline::copy_rows), new)
};
if sample >= 5 {
println!("{name},{},{old},{new}", sample - 5);
}
}
}
}

// Inlined from main so before/after share one build and the same inputs.
mod baseline {
pub fn copy_rows(
data: &[u8],
row_bytes: usize,
stride: usize,
rows: usize,
mirrored: bool,
) -> Option<Vec<u8>> {
if stride < row_bytes || data.len() < rows.checked_mul(stride)? {
return None;
}
let mut pixels = vec![0; rows.checked_mul(row_bytes)?];
for (row, output) in pixels.chunks_exact_mut(row_bytes.max(1)).enumerate() {
let input = &data[row * stride..row * stride + row_bytes];
if mirrored {
for (target, source) in output.chunks_exact_mut(4).zip(input.chunks_exact(4).rev())
{
target.copy_from_slice(source);
}
} else {
output.copy_from_slice(input);
}
}
Some(pixels)
}
}
81 changes: 72 additions & 9 deletions apps/desktop-gpui/kernels/src/frame.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::mem::MaybeUninit;

pub fn unpad_rows(data: &[u8], row_bytes: usize, stride: usize, rows: usize) -> Option<Vec<u8>> {
if stride == 0 {
return None;
Expand Down Expand Up @@ -68,17 +70,31 @@ pub fn copy_rows(
if stride < row_bytes || data.len() < rows.checked_mul(stride)? {
return None;
}
let mut pixels = vec![0; rows.checked_mul(row_bytes)?];
for (row, output) in pixels.chunks_exact_mut(row_bytes.max(1)).enumerate() {
let input = &data[row * stride..row * stride + row_bytes];
if mirrored {
for (target, source) in output.chunks_exact_mut(4).zip(input.chunks_exact(4).rev()) {
target.copy_from_slice(source);
}
} else {
output.copy_from_slice(input);
let output_len = rows.checked_mul(row_bytes)?;
if output_len == 0 {
return Some(Vec::new());
}
if !mirrored {
let mut pixels = Vec::with_capacity(output_len);
for input in data.chunks(stride).take(rows) {
pixels.extend_from_slice(&input[..row_bytes]);
}
return Some(pixels);
}
let mut pixels = Vec::with_capacity(output_len);
let spare = &mut pixels.spare_capacity_mut()[..output_len];
for (input, output) in data.chunks(stride).zip(spare.chunks_exact_mut(row_bytes)) {
let (target, padding) = output.as_chunks_mut::<4>();
for (pixel, source) in target
.iter_mut()
.zip(input[..row_bytes].as_chunks::<4>().0.iter().rev())
{
*pixel = source.map(MaybeUninit::new);
}
padding.fill(MaybeUninit::new(0));
}
// SAFETY: every row writes all row_bytes of its chunk, so all output_len bytes are initialized.
unsafe { pixels.set_len(output_len) };
Some(pixels)
}

Expand Down Expand Up @@ -335,6 +351,53 @@ mod tests {
assert_eq!(copy_rows(&PADDED, 8, 10, 3, false), None);
}

fn zeroed_copy_rows(
data: &[u8],
row_bytes: usize,
stride: usize,
rows: usize,
mirrored: bool,
) -> Vec<u8> {
let mut pixels = vec![0; rows * row_bytes];
for (row, output) in pixels.chunks_exact_mut(row_bytes.max(1)).enumerate() {
let input = &data[row * stride..row * stride + row_bytes];
if mirrored {
let source = input.as_chunks::<4>().0.iter().rev();
for (target, source) in output.as_chunks_mut::<4>().0.iter_mut().zip(source) {
*target = *source;
}
} else {
output.copy_from_slice(input);
}
}
pixels
}

#[test]
fn copy_rows_matches_a_zeroed_copy_for_every_shape() {
for (row_bytes, stride, rows) in [
(0, 0, 3),
(0, 8, 3),
(8, 8, 0),
(4, 4, 1),
(12, 12, 5),
(12, 16, 5),
(15, 15, 3),
(15, 64, 3),
(1280 * 3, 1280 * 3 + 64, 7),
(1366 * 4, (1366 * 4usize).next_multiple_of(256), 9),
] {
let data: Vec<u8> = (0..stride * rows + 5).map(|i| (i * 7 + 3) as u8).collect();
for mirrored in [false, true] {
assert_eq!(
copy_rows(&data, row_bytes, stride, rows, mirrored).unwrap(),
zeroed_copy_rows(&data, row_bytes, stride, rows, mirrored),
"{row_bytes} {stride} {rows} {mirrored}"
);
}
}
}

#[test]
fn swaps_into_a_reused_buffer() {
let mut target = vec![0; 99];
Expand Down
Loading