diff --git a/src/windowed_context/plugin.rs b/src/windowed_context/plugin.rs index bb5a5d4..762eed2 100644 --- a/src/windowed_context/plugin.rs +++ b/src/windowed_context/plugin.rs @@ -68,20 +68,30 @@ fn render_terminal_to_handle( ) { let width = softatui.backend().get_pixmap_width() as u32; let height = softatui.backend().get_pixmap_height() as u32; - let data = softatui.backend().get_pixmap_data_as_rgba(); let image = images.get_mut(&my_handle.0).expect("Image not found"); - *image = Image::new( - Extent3d { + if image.width() != width || image.height() != height { + image.resize(Extent3d { width, height, depth_or_array_layers: 1, - }, - TextureDimension::D2, - data, - TextureFormat::Rgba8UnormSrgb, - RenderAssetUsages::RENDER_WORLD | RenderAssetUsages::MAIN_WORLD, - ); + }); + image.data = Some(softatui.backend().get_pixmap_data_as_rgba()); + } else { + // efficient fast-path copy using chunks + let data_in = softatui.backend().get_pixmap_data(); + let data_out = image.data.as_mut().expect("Image data missing"); + let (pixels_in, _) = data_in.as_chunks::<3>(); + let (pixels_out, _) = data_out.as_chunks_mut::<4>(); + for i in 0..(width * height) as usize { + let px_out = &mut pixels_out[i]; + let px_in = pixels_in[i]; + px_out[0] = px_in[0]; + px_out[1] = px_in[1]; + px_out[2] = px_in[2]; + // skip writing alpha as it is set to 255 by get_pixmap_data_as_rgba + } + } } /// System that reacts to window resize