Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
perf(gpui): copy opaque screenshot rows during compositing
jatmn594d4f3207f9
diff --git a/apps/desktop-gpui/kernels/Cargo.toml b/apps/desktop-gpui/kernels/Cargo.tomlindex 672cc9e61..6950a4c77 100644--- a/apps/desktop-gpui/kernels/Cargo.toml+++ b/apps/desktop-gpui/kernels/Cargo.toml@@ -11,3 +11,7 @@ cap-project = { path = "../../../crates/project" }image = { version = "0.25.1", default-features = false, features = ["jpeg", "png", "webp"] }md-5 = "0.10"tracing = "0.1"++[[bench]]+name = "screenshot_blit"+harness = falsediff --git a/apps/desktop-gpui/kernels/benches/screenshot_blit.rs b/apps/desktop-gpui/kernels/benches/screenshot_blit.rsnew file mode 100644index 000000000..496ecb61e--- /dev/null+++ b/apps/desktop-gpui/kernels/benches/screenshot_blit.rs@@ -0,0 +1,145 @@+use cap_gpui_kernels::screenshot as candidate;+use std::{hint::black_box, time::Instant};++type Blit = fn(&mut [u8], (u32, u32), &[u8], (u32, u32), (i64, i64));++#[derive(Clone, Copy)]+enum Alpha {+ Solid(u8),+ Mixed,+ Edge,+ Sparse,+}++fn main() {+ use Alpha::{Edge, Mixed, Solid, Sparse};++ println!("workload,sample,before_ns,after_ns");+ for (name, size, offset, alpha) in [+ ("opaque-1080p", (1920u32, 1080u32), (0, 0), Solid(255)),+ ("opaque-4k", (3840, 2160), (0, 0), Solid(255)),+ ("opaque-portrait", (2160, 3840), (0, 0), Solid(255)),+ ("opaque-expanded", (3840, 2160), (100, 80), Solid(255)),+ ("opaque-clipped", (3840, 2160), (-2000, -1200), Solid(255)),+ ("translucent-4k", (3840, 2160), (0, 0), Solid(128)),+ ("edge-alpha-4k", (3840, 2160), (0, 0), Edge),+ ("sparse-alpha-4k", (3840, 2160), (0, 0), Sparse),+ ("mixed-4k", (3840, 2160), (0, 0), Mixed),+ ("transparent-4k", (3840, 2160), (0, 0), Solid(0)),+ ("outside-4k", (3840, 2160), (4000, 0), Solid(255)),+ ] {+ let dst_size = if name == "opaque-expanded" {+ (size.0 + 200, size.1 + 160)+ } else {+ size+ };+ let src: Vec<u8> = (0..size.0 * size.1)+ .flat_map(|i| {+ [+ i as u8,+ (i >> 8) as u8,+ i.wrapping_mul(17) as u8,+ match alpha {+ Mixed => i as u8,+ Edge if i % size.0 == size.0 - 1 => 128,+ Sparse if i % 1024 == 1023 => 128,+ Edge | Sparse => 255,+ Solid(alpha) => alpha,+ },+ ]+ })+ .collect();+ let initial: Vec<u8> = (0..dst_size.0 * dst_size.1)+ .flat_map(|i| [53, 71, 89, (i % 256) as u8])+ .collect();+ let mut before = initial.clone();+ let mut after = initial.clone();+ baseline::blit_over_offset(&mut before, dst_size, &src, size, offset);+ candidate::blit_over_offset(&mut after, dst_size, &src, size, offset);+ assert_eq!(before, after, "{name}");+ for sample in 0..35 {+ before.copy_from_slice(&initial);+ after.copy_from_slice(&initial);+ let run = |f: Blit, dst: &mut [u8]| {+ let start = Instant::now();+ f(+ black_box(dst),+ black_box(dst_size),+ black_box(&src),+ black_box(size),+ black_box(offset),+ );+ start.elapsed().as_nanos()+ };+ let (old, new) = if sample % 2 == 0 {+ let old = run(baseline::blit_over_offset, &mut before);+ (old, run(candidate::blit_over_offset, &mut after))+ } else {+ let new = run(candidate::blit_over_offset, &mut after);+ (run(baseline::blit_over_offset, &mut before), new)+ };+ black_box((&before, &after));+ if sample >= 5 {+ println!("{name},{},{old},{new}", sample - 5);+ }+ }+ }+}++// Preserved from 40b06b92ac57b87f9bdb52e4eff8429d0ea319c3 so both paths use the same build and inputs.+mod baseline {+ fn blend_pixel_over(dst: &mut [u8], src: [u8; 4]) {+ if src[3] == 0 {+ return;+ }+ if src[3] == 255 {+ dst.copy_from_slice(&src);+ return;+ }+ let sa = f32::from(src[3]) / 255.;+ let da = f32::from(dst[3]) / 255.;+ let oa = sa + da * (1. - sa);+ if oa <= 0. {+ dst.fill(0);+ return;+ }+ for channel in 0..3 {+ let s = f32::from(src[channel]);+ let d = f32::from(dst[channel]);+ dst[channel] = ((s * sa + d * da * (1. - sa)) / oa).round().clamp(0., 255.) as u8;+ }+ dst[3] = (oa * 255.).round() as u8;+ }++ pub fn blit_over_offset(+ dst: &mut [u8],+ dst_size: (u32, u32),+ src: &[u8],+ src_size: (u32, u32),+ offset: (i64, i64),+ ) {+ let dst_stride = dst_size.0 as usize * 4;+ let src_stride = src_size.0 as usize * 4;+ for src_y in 0..src_size.1 as i64 {+ let dst_y = src_y + offset.1;+ if dst_y < 0 || dst_y >= i64::from(dst_size.1) {+ continue;+ }+ for src_x in 0..src_size.0 as i64 {+ let dst_x = src_x + offset.0;+ if dst_x < 0 || dst_x >= i64::from(dst_size.0) {+ continue;+ }+ let src_start = src_y as usize * src_stride + src_x as usize * 4;+ let dst_start = dst_y as usize * dst_stride + dst_x as usize * 4;+ let pixel = [+ src[src_start],+ src[src_start + 1],+ src[src_start + 2],+ src[src_start + 3],+ ];+ blend_pixel_over(&mut dst[dst_start..dst_start + 4], pixel);+ }+ }+ }+}diff --git a/apps/desktop-gpui/kernels/src/screenshot.rs b/apps/desktop-gpui/kernels/src/screenshot.rsindex f66551271..cd578ea50 100644--- a/apps/desktop-gpui/kernels/src/screenshot.rs+++ b/apps/desktop-gpui/kernels/src/screenshot.rs@@ -52,21 +52,13 @@ fn blend_pixel_over(dst: &mut [u8], src: [u8; 4]) {}pub fn blit_over(canvas: &mut [u8], size: (u32, u32), region: &image::RgbaImage, at: (u32, u32)) {- let stride = size.0 as usize * 4;- for (row_index, row) in region.rows().enumerate() {- let y = at.1 as usize + row_index;- if y >= size.1 as usize {- break;- }- for (column_index, pixel) in row.enumerate() {- let x = at.0 as usize + column_index;- if x >= size.0 as usize {- break;- }- let start = y * stride + x * 4;- blend_pixel_over(&mut canvas[start..start + 4], pixel.0);- }- }+ blit_over_offset(+ canvas,+ size,+ region.as_raw(),+ region.dimensions(),+ (i64::from(at.0), i64::from(at.1)),+ );}pub fn blit_over_offset(@@ -76,27 +68,33 @@ pub fn blit_over_offset(src_size: (u32, u32),offset: (i64, i64),) {+ if offset.0 >= i64::from(dst_size.0)+ || offset.1 >= i64::from(dst_size.1)+ || offset.0 <= -i64::from(src_size.0)+ || offset.1 <= -i64::from(src_size.1)+ {+ return;+ }++ let src_x = (-offset.0).max(0) as usize;+ let src_y = (-offset.1).max(0) as usize;+ let dst_x = offset.0.max(0) as usize;+ let dst_y = offset.1.max(0) as usize;+ let width = (src_size.0 as usize - src_x).min(dst_size.0 as usize - dst_x);+ let height = (src_size.1 as usize - src_y).min(dst_size.1 as usize - dst_y);let dst_stride = dst_size.0 as usize * 4;let src_stride = src_size.0 as usize * 4;- for src_y in 0..src_size.1 as i64 {- let dst_y = src_y + offset.1;- if dst_y < 0 || dst_y >= i64::from(dst_size.1) {- continue;- }- for src_x in 0..src_size.0 as i64 {- let dst_x = src_x + offset.0;- if dst_x < 0 || dst_x >= i64::from(dst_size.0) {- continue;+ for row in 0..height {+ let src_start = (src_y + row) * src_stride + src_x * 4;+ let dst_start = (dst_y + row) * dst_stride + dst_x * 4;+ let source = &src[src_start..src_start + width * 4];+ let target = &mut dst[dst_start..dst_start + width * 4];+ if is_opaque(source) {+ target.copy_from_slice(source);+ } else {+ for (target, source) in target.chunks_exact_mut(4).zip(source.chunks_exact(4)) {+ blend_pixel_over(target, source.try_into().unwrap());}- let src_start = src_y as usize * src_stride + src_x as usize * 4;- let dst_start = dst_y as usize * dst_stride + dst_x as usize * 4;- let pixel = [- src[src_start],- src[src_start + 1],- src[src_start + 2],- src[src_start + 3],- ];- blend_pixel_over(&mut dst[dst_start..dst_start + 4], pixel);}}}@@ -218,6 +216,96 @@ pub fn mask_filter(mod tests {use super::*;+ fn reference_blit(+ dst: &mut [u8],+ dst_size: (u32, u32),+ src: &[u8],+ src_size: (u32, u32),+ offset: (i64, i64),+ ) {+ for y in 0..src_size.1 {+ for x in 0..src_size.0 {+ let dx = i64::from(x) + offset.0;+ let dy = i64::from(y) + offset.1;+ if dx < 0 || dy < 0 || dx >= i64::from(dst_size.0) || dy >= i64::from(dst_size.1) {+ continue;+ }+ let source = (y as usize * src_size.0 as usize + x as usize) * 4;+ let target = (dy as usize * dst_size.0 as usize + dx as usize) * 4;+ blend_pixel_over(+ &mut dst[target..target + 4],+ src[source..source + 4].try_into().unwrap(),+ );+ }+ }+ }++ #[test]+ fn clipped_blits_match_scalar_source_over() {+ for width in [0, 1, 3, 4, 5, 15, 16, 17, 31, 32, 33] {+ let src_size = (width, 4);+ let dst_size = (19, 7);+ for alpha in [0, 128, 255] {+ let mut src: Vec<u8> = (0..width * 4)+ .flat_map(|i| [i as u8, (i * 17) as u8, (i * 29) as u8, alpha])+ .collect();+ for mixed in [false, true] {+ if mixed {+ for (i, pixel) in src.chunks_exact_mut(4).enumerate() {+ pixel[3] = i.wrapping_mul(71) as u8;+ }+ }+ for x in [-34, -17, -1, 0, 1, 18, 19, 34] {+ for y in [-5, -1, 0, 1, 6, 7] {+ let initial: Vec<u8> = (0..19 * 7)+ .flat_map(|i| [31, 127, 231, (i * 43) as u8])+ .collect();+ let mut expected = initial.clone();+ let mut actual = initial;+ reference_blit(&mut expected, dst_size, &src, src_size, (x, y));+ blit_over_offset(&mut actual, dst_size, &src, src_size, (x, y));+ assert_eq!(+ actual, expected,+ "{src_size:?}, {alpha}, {mixed}, {x}, {y}"+ );+ if x >= 0 && y >= 0 {+ let region =+ image::RgbaImage::from_raw(width, 4, src.clone()).unwrap();+ let mut actual: Vec<u8> = (0..19 * 7)+ .flat_map(|i| [31, 127, 231, (i * 43) as u8])+ .collect();+ blit_over(&mut actual, dst_size, ®ion, (x as u32, y as u32));+ assert_eq!(actual, expected);+ }+ }+ }+ }+ }+ }+ }++ #[test]+ fn opaque_row_fast_path_respects_every_alpha_byte() {+ let size = (33, 2);+ for changed in 0..66 {+ let mut src = vec![255; 33 * 2 * 4];+ src[changed * 4 + 3] = 127;+ let mut expected = vec![61; src.len()];+ let mut actual = expected.clone();+ reference_blit(&mut expected, size, &src, size, (0, 0));+ blit_over_offset(&mut actual, size, &src, size, (0, 0));+ assert_eq!(actual, expected);+ }+ }++ #[test]+ fn blits_outside_the_canvas_do_not_touch_buffers() {+ for offset in [(i64::MIN, 0), (i64::MAX, 0), (0, i64::MIN), (0, i64::MAX)] {+ blit_over_offset(&mut [], (1, 1), &[], (1, 1), offset);+ }+ blit_over_offset(&mut [], (0, 0), &[], (0, 0), (0, 0));+ }+fn reference_flatten(rgba: &[u8], width: u32, height: u32) -> Vec<u8> {let mut out = vec![255; rgba.len()];blit_over_offset(&mut out, (width, height), rgba, (width, height), (0, 0));