Skip to content
Merged
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
111 changes: 82 additions & 29 deletions apps/desktop-gpui/kernels/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ pub fn swap_red_blue(pixels: &mut [u8]) {
}

pub fn swap_red_blue_into(source: &[u8], target: &mut Vec<u8>) {
target.clear();
target.extend_from_slice(source);
swap_red_blue(target);
target.resize(source.len(), 0);
let pixels_end = source.len() / 4 * 4;
reorder_32bit_row::<2, 1, 0, 3>(&source[..pixels_end], &mut target[..pixels_end]);
target[pixels_end..].copy_from_slice(&source[pixels_end..]);
}

pub fn copy_rows(
Expand Down Expand Up @@ -115,40 +116,60 @@ pub fn convert_32bit_rows(
height: usize,
order: ChannelOrder,
) -> Option<Vec<u8>> {
let mut rgba_data = Vec::with_capacity(width * height * 4);
if height == 0 {
return Some(Vec::new());
}
let row_bytes = width.checked_mul(4)?;
let readable_rows = match raw_data.len().checked_sub(row_bytes) {
None => 0,
Some(_) if bytes_per_row == 0 => height,
Some(remaining) => remaining / bytes_per_row + 1,
Comment on lines +123 to +126

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 New row checks lack tests

convert_32bit_rows now checks all row bounds before copying, but the retained tests only use one row. They cannot catch mistakes in the new row-count calculation or rejection of a truncated later row. The comparisons described in the PR are kept outside the repository, so CI cannot repeat them.

Add kernel tests for multiple padded rows, a truncated second row, and zero stride with both sufficient and short input.

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/desktop-gpui/kernels/src/frame.rs
Line: 123-126

Comment:
**New row checks lack tests**

`convert_32bit_rows` now checks all row bounds before copying, but the retained tests only use one row. They cannot catch mistakes in the new row-count calculation or rejection of a truncated later row. The comparisons described in the PR are kept outside the repository, so CI cannot repeat them.

Add kernel tests for multiple padded rows, a truncated second row, and zero stride with both sufficient and short input.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 74bf2ec

};
if readable_rows < height {
let row_start = readable_rows.checked_mul(bytes_per_row)?;
let row_end = row_start.checked_add(row_bytes)?;
tracing::warn!(
row_start,
row_end,
raw_len = raw_data.len(),
"Row bounds exceeded raw data length during thumbnail capture",
);
return None;
}
if row_bytes == 0 {
return Some(Vec::new());
}

let mut rgba_data = vec![0; row_bytes.checked_mul(height)?];
for y in 0..height {
let row_start = y * bytes_per_row;
let row_end = row_start + width * 4;
if row_end > raw_data.len() {
tracing::warn!(
row_start,
row_end,
raw_len = raw_data.len(),
"Row bounds exceeded raw data length during thumbnail capture",
);
return None;
}

let row = &raw_data[row_start..row_end];
for chunk in row.chunks_exact(4) {
match order {
ChannelOrder::Bgra => {
rgba_data.extend_from_slice(&[chunk[2], chunk[1], chunk[0], chunk[3]])
}
ChannelOrder::Rgba => rgba_data.extend_from_slice(chunk),
ChannelOrder::Argb => {
rgba_data.extend_from_slice(&[chunk[1], chunk[2], chunk[3], chunk[0]])
}
ChannelOrder::Abgr => {
rgba_data.extend_from_slice(&[chunk[3], chunk[2], chunk[1], chunk[0]])
}
}
let row = &raw_data[row_start..row_start + row_bytes];
let output = &mut rgba_data[y * row_bytes..(y + 1) * row_bytes];
match order {
ChannelOrder::Bgra => reorder_32bit_row::<2, 1, 0, 3>(row, output),
ChannelOrder::Rgba => output.copy_from_slice(row),
ChannelOrder::Argb => reorder_32bit_row::<1, 2, 3, 0>(row, output),
ChannelOrder::Abgr => reorder_32bit_row::<3, 2, 1, 0>(row, output),
}
}

Some(rgba_data)
}

fn reorder_32bit_row<const R: usize, const G: usize, const B: usize, const A: usize>(
source: &[u8],
target: &mut [u8],
) {
for (source, target) in source
.as_chunks::<4>()
.0
.iter()
.zip(target.as_chunks_mut::<4>().0)
{
*target = [source[R], source[G], source[B], source[A]];
}
}

#[derive(Copy, Clone)]
pub enum Nv12Range {
Video,
Expand Down Expand Up @@ -293,6 +314,38 @@ mod tests {
assert_eq!(target, vec![3, 2, 1, 4]);
}

#[test]
fn converts_multiple_padded_rows_without_last_row_padding() {
let expected = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
for raw in [&PADDED[..], &PADDED[..18]] {
assert_eq!(
convert_32bit_rows(raw, 10, 2, 2, ChannelOrder::Rgba),
Some(expected.clone())
);
}
}

#[test]
fn rejects_a_truncated_second_row() {
assert_eq!(
convert_32bit_rows(&PADDED[..17], 10, 2, 2, ChannelOrder::Rgba),
None
);
}

#[test]
fn zero_stride_reuses_a_complete_row_and_rejects_short_input() {
let raw = [1, 2, 3, 4, 5, 6, 7, 8];
assert_eq!(
convert_32bit_rows(&raw, 0, 2, 2, ChannelOrder::Bgra),
Some(vec![3, 2, 1, 4, 7, 6, 5, 8, 3, 2, 1, 4, 7, 6, 5, 8])
);
assert_eq!(
convert_32bit_rows(&raw[..7], 0, 2, 2, ChannelOrder::Bgra),
None
);
}

#[test]
fn thumbnail_channel_orders_and_nv12_match_the_picker() {
let raw = vec![1, 2, 3, 4, 5, 6, 7, 8, 0xff, 0xff, 0xff, 0xff];
Expand Down
Loading