Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix: improve rendering stability
Richie McIlroy530f81ef8faf
diff --git a/apps/desktop/src-tauri/src/export.rs b/apps/desktop/src-tauri/src/export.rsindex 1f1b39d69..e75669a8d 100644--- a/apps/desktop/src-tauri/src/export.rs+++ b/apps/desktop/src-tauri/src/export.rs@@ -298,9 +298,10 @@ async fn run_out_of_process_export_attempt(}if mode.is_software_safe() {- command- .env("CAP_RENDER_FORCE_SOFTWARE_ADAPTER", "1")- .env("CAP_EXPORT_FORCE_SOFTWARE_ENCODER", "1");+ command.env("CAP_EXPORT_FORCE_SOFTWARE_ENCODER", "1");+ if cfg!(windows) {+ command.env("CAP_RENDER_FORCE_SOFTWARE_ADAPTER", "1");+ }}configure_exporter_command(&mut command);diff --git a/crates/enc-ffmpeg/src/video/h264.rs b/crates/enc-ffmpeg/src/video/h264.rsindex 7f844ee35..f8065b7bd 100644--- a/crates/enc-ffmpeg/src/video/h264.rs+++ b/crates/enc-ffmpeg/src/video/h264.rs@@ -183,7 +183,7 @@ impl H264EncoderBuilder {);} else {let is_high_throughput =- requires_software_encoder(&input_config, self.preset);+ requires_software_encoder(&input_config, self.preset, self.is_export);if is_high_throughput {warn!(encoder = %codec_name,@@ -776,7 +776,11 @@ fn estimate_hw_encoder_max_fps(encoder_name: &str, width: u32, height: u32) -> f}}-fn requires_software_encoder(config: &VideoInfo, preset: H264Preset) -> bool {+fn requires_software_encoder(config: &VideoInfo, preset: H264Preset, is_export: bool) -> bool {+ if is_export {+ return false;+ }+if preset == H264Preset::HighThroughput {return true;}@@ -848,11 +852,15 @@ fn get_default_encoder_priority(_config: &VideoInfo) -> &'static [&'static str]static ENCODER_PRIORITY_DEFAULT: &[&str] =&["h264_nvenc", "h264_qsv", "h264_amf", "h264_mf", "libx264"];- match detect_primary_gpu().map(|info| info.vendor) {- Some(GpuVendor::Nvidia) => ENCODER_PRIORITY_NVIDIA,- Some(GpuVendor::Amd) => ENCODER_PRIORITY_AMD,- Some(GpuVendor::Intel) => ENCODER_PRIORITY_INTEL,- _ => ENCODER_PRIORITY_DEFAULT,+ match detect_primary_gpu() {+ Some(info) if !info.supports_hardware_encoding() => &["libx264"],+ Some(info) => match info.vendor {+ GpuVendor::Nvidia => ENCODER_PRIORITY_NVIDIA,+ GpuVendor::Amd => ENCODER_PRIORITY_AMD,+ GpuVendor::Intel => ENCODER_PRIORITY_INTEL,+ _ => ENCODER_PRIORITY_DEFAULT,+ },+ None => &["libx264"],}}@@ -866,12 +874,13 @@ fn get_encoder_priority_with_override(config: &VideoInfo,preset: H264Preset,override_priority: Option<&'static [&'static str]>,+ is_export: bool,) -> &'static [&'static str] {if force_software_encoder() {return &["libx264"];}- if requires_software_encoder(config, preset) {+ if requires_software_encoder(config, preset, is_export) {return &["libx264"];}@@ -888,13 +897,9 @@ fn force_software_encoder() -> bool {}fn export_encoder_priority_override(- config: &VideoInfo,- preset: H264Preset,+ _config: &VideoInfo,+ _preset: H264Preset,) -> Option<&'static [&'static str]> {- if requires_software_encoder(config, preset) {- return None;- }-#[cfg(target_os = "windows")]{use cap_frame_converter::{GpuVendor, detect_primary_gpu};@@ -902,7 +907,10 @@ fn export_encoder_priority_override(static ENCODER_PRIORITY_AMD_EXPORT: &[&str] =&["h264_amf", "h264_mf", "h264_nvenc", "h264_qsv", "libx264"];- if let Some(GpuVendor::Amd) = detect_primary_gpu().map(|info| info.vendor) {+ if let Some(info) = detect_primary_gpu()+ && info.supports_hardware_encoding()+ && info.vendor == GpuVendor::Amd+ {return Some(ENCODER_PRIORITY_AMD_EXPORT);}}@@ -931,7 +939,7 @@ fn get_codec_and_options(let encoder_priority = if crf.is_some() {&["libx264"] as &[&str]} else {- get_encoder_priority_with_override(config, preset, encoder_priority_override)+ get_encoder_priority_with_override(config, preset, encoder_priority_override, is_export)};let mut encoders = Vec::new();diff --git a/crates/frame-converter/src/d3d11.rs b/crates/frame-converter/src/d3d11.rsindex 1b8cb34c2..5a2abac0e 100644--- a/crates/frame-converter/src/d3d11.rs+++ b/crates/frame-converter/src/d3d11.rs@@ -51,6 +51,29 @@ pub enum GpuVendor {Unknown(u32),}+const NON_HARDWARE_ADAPTER_MARKERS: &[&str] = &[+ "parsec",+ "displaylink",+ "splashtop",+ "synergy",+ "virtual display",+ "microsoft basic render",+ "microsoft basic",+ "warp",+];++fn matches_non_hardware_adapter_marker(description: &str) -> bool {+ let description = description.to_ascii_lowercase();+ NON_HARDWARE_ADAPTER_MARKERS+ .iter()+ .any(|marker| description.contains(marker))+}++fn is_non_hardware_adapter(vendor_id: u32, description: &str) -> bool {+ (vendor_id == 0x1414 && description.contains("Basic Render"))+ || matches_non_hardware_adapter_marker(description)+}+impl GpuVendor {pub fn from_id(vendor_id: u32) -> Self {match vendor_id {@@ -85,12 +108,13 @@ impl GpuInfo {}pub fn is_warp(&self) -> bool {- self.description.contains("Microsoft Basic Render Driver")- || self.description.contains("WARP")+ matches_non_hardware_adapter_marker(&self.description)}pub fn supports_hardware_encoding(&self) -> bool {- !self.is_software_adapter && !self.is_basic_render_driver()+ !self.is_software_adapter+ && !self.is_basic_render_driver()+ && !matches_non_hardware_adapter_marker(&self.description)}}@@ -174,10 +198,7 @@ fn enumerate_all_gpus() -> Vec<GpuInfo> {.collect::<Vec<_>>(),);- let is_software = desc.VendorId == 0x1414- && (description.contains("Basic Render")- || description.contains("WARP")- || description.contains("Microsoft Basic"));+ let is_software = is_non_hardware_adapter(desc.VendorId, &description);let gpu_info = GpuInfo {vendor: GpuVendor::from_id(desc.VendorId),@@ -219,7 +240,10 @@ fn select_best_gpu(gpus: &[GpuInfo]) -> Option<GpuInfo> {return None;}- let hardware_gpus: Vec<&GpuInfo> = gpus.iter().filter(|g| !g.is_software_adapter).collect();+ let hardware_gpus: Vec<&GpuInfo> = gpus+ .iter()+ .filter(|g| g.supports_hardware_encoding())+ .collect();if hardware_gpus.is_empty() {tracing::warn!("No hardware GPUs found, falling back to software adapter");@@ -316,10 +340,7 @@ fn get_gpu_info(device: &ID3D11Device) -> Result<GpuInfo, ConvertError> {.collect::<Vec<_>>(),);- let is_software = desc.VendorId == 0x1414- && (description.contains("Basic Render")- || description.contains("WARP")- || description.contains("Microsoft Basic"));+ let is_software = is_non_hardware_adapter(desc.VendorId, &description);Ok(GpuInfo {vendor: GpuVendor::from_id(desc.VendorId),@@ -360,10 +381,7 @@ impl D3D11Converter {.collect::<Vec<_>>(),);- let is_software = desc.VendorId == 0x1414- && (description.contains("Basic Render")- || description.contains("WARP")- || description.contains("Microsoft Basic"));+ let is_software = is_non_hardware_adapter(desc.VendorId, &description);if !is_software {let vendor = GpuVendor::from_id(desc.VendorId);diff --git a/crates/rendering/src/layers/display.rs b/crates/rendering/src/layers/display.rsindex bd5a367d2..0e970b277 100644--- a/crates/rendering/src/layers/display.rs+++ b/crates/rendering/src/layers/display.rs@@ -14,6 +14,24 @@ struct PendingTextureCopy {dst_texture_index: usize,}+fn uniforms_for_source_frame(+ mut uniforms: CompositeVideoFrameUniforms,+ base_size: XY<u32>,+ source_size: XY<u32>,+) -> CompositeVideoFrameUniforms {+ let scale_x = source_size.x as f32 / base_size.x.max(1) as f32;+ let scale_y = source_size.y as f32 / base_size.y.max(1) as f32;++ uniforms.crop_bounds = [+ uniforms.crop_bounds[0] * scale_x,+ uniforms.crop_bounds[1] * scale_y,+ uniforms.crop_bounds[2] * scale_x,+ uniforms.crop_bounds[3] * scale_y,+ ];+ uniforms.frame_size = [source_size.x as f32, source_size.y as f32];+ uniforms+}+pub struct DisplayLayer {frame_textures: [wgpu::Texture; 2],frame_texture_views: [wgpu::TextureView; 2],@@ -149,6 +167,8 @@ impl DisplayLayer {let frame_data = screen_frame.data();let actual_width = screen_frame.width();let actual_height = screen_frame.height();+ let source_size = XY::new(actual_width, actual_height);+ let uniforms = uniforms_for_source_frame(uniforms, frame_size, source_size);let format = screen_frame.format();let current_recording_time = segment_frames.recording_time;@@ -159,14 +179,14 @@ impl DisplayLayer {if !skipped {let next_texture = 1 - self.current_texture;- if self.frame_textures[next_texture].width() != frame_size.x- || self.frame_textures[next_texture].height() != frame_size.y+ if self.frame_textures[next_texture].width() != source_size.x+ || self.frame_textures[next_texture].height() != source_size.y{self.frame_textures[next_texture] =CompositeVideoFramePipeline::create_frame_texture(device,- frame_size.x,- frame_size.y,+ source_size.x,+ source_size.y,);self.frame_texture_views[next_texture] =self.frame_textures[next_texture].create_view(&Default::default());@@ -180,7 +200,7 @@ impl DisplayLayer {let frame_uploaded = match format {PixelFormat::Rgba => {- let src_bytes_per_row = frame_size.x * 4;+ let src_bytes_per_row = source_size.x * 4;queue.write_texture(wgpu::TexelCopyTextureInfo {@@ -193,11 +213,11 @@ impl DisplayLayer {wgpu::TexelCopyBufferLayout {offset: 0,bytes_per_row: Some(src_bytes_per_row),- rows_per_image: Some(frame_size.y),+ rows_per_image: Some(source_size.y),},wgpu::Extent3d {- width: frame_size.x,- height: frame_size.y,+ width: source_size.x,+ height: source_size.y,depth_or_array_layers: 1,},);@@ -216,8 +236,8 @@ impl DisplayLayer {queue,y_data,uv_data,- frame_size.x,- frame_size.y,+ source_size.x,+ source_size.y,y_stride,uv_stride,);@@ -226,8 +246,8 @@ impl DisplayLayer {Ok(_) => {if self.yuv_converter.output_texture().is_some() {self.pending_copy = Some(PendingTextureCopy {- width: frame_size.x,- height: frame_size.y,+ width: source_size.x,+ height: source_size.y,dst_texture_index: next_texture,});true@@ -250,8 +270,8 @@ impl DisplayLayer {queue,y_data,uv_data,- frame_size.x,- frame_size.y,+ source_size.x,+ source_size.y,y_stride,uv_stride,);@@ -260,8 +280,8 @@ impl DisplayLayer {Ok(_) => {if self.yuv_converter.output_texture().is_some() {self.pending_copy = Some(PendingTextureCopy {- width: frame_size.x,- height: frame_size.y,+ width: source_size.x,+ height: source_size.y,dst_texture_index: next_texture,});true@@ -378,8 +398,8 @@ impl DisplayLayer {queue,y_data,uv_data,- frame_size.x,- frame_size.y,+ source_size.x,+ source_size.y,y_stride,uv_stride,)@@ -389,8 +409,8 @@ impl DisplayLayer {queue,y_data,uv_data,- frame_size.x,- frame_size.y,+ source_size.x,+ source_size.y,y_stride,uv_stride,)@@ -400,8 +420,8 @@ impl DisplayLayer {Ok(_) => {if self.yuv_converter.output_texture().is_some() {self.pending_copy = Some(PendingTextureCopy {- width: frame_size.x,- height: frame_size.y,+ width: source_size.x,+ height: source_size.y,dst_texture_index: next_texture,});true@@ -429,8 +449,8 @@ impl DisplayLayer {y_data,u_data,v_data,- frame_size.x,- frame_size.y,+ source_size.x,+ source_size.y,screen_frame.y_stride(),screen_frame.uv_stride(),)@@ -441,8 +461,8 @@ impl DisplayLayer {y_data,u_data,v_data,- frame_size.x,- frame_size.y,+ source_size.x,+ source_size.y,screen_frame.y_stride(),screen_frame.uv_stride(),)@@ -452,8 +472,8 @@ impl DisplayLayer {Ok(_) => {if self.yuv_converter.output_texture().is_some() {self.pending_copy = Some(PendingTextureCopy {- width: frame_size.x,- height: frame_size.y,+ width: source_size.x,+ height: source_size.y,dst_texture_index: next_texture,});true@@ -500,6 +520,8 @@ impl DisplayLayer {let actual_width = screen_frame.width();let actual_height = screen_frame.height();+ let source_size = XY::new(actual_width, actual_height);+ let uniforms = uniforms_for_source_frame(uniforms, frame_size, source_size);let format = screen_frame.format();let current_recording_time = segment_frames.recording_time;@@ -510,14 +532,14 @@ impl DisplayLayer {if !skipped {let next_texture = 1 - self.current_texture;- if self.frame_textures[next_texture].width() != frame_size.x- || self.frame_textures[next_texture].height() != frame_size.y+ if self.frame_textures[next_texture].width() != source_size.x+ || self.frame_textures[next_texture].height() != source_size.y{self.frame_textures[next_texture] =CompositeVideoFramePipeline::create_frame_texture(device,- frame_size.x,- frame_size.y,+ source_size.x,+ source_size.y,);self.frame_texture_views[next_texture] =self.frame_textures[next_texture].create_view(&Default::default());@@ -532,7 +554,7 @@ impl DisplayLayer {let frame_uploaded = match format {PixelFormat::Rgba => {let frame_data = screen_frame.data();- let src_bytes_per_row = frame_size.x * 4;+ let src_bytes_per_row = source_size.x * 4;queue.write_texture(wgpu::TexelCopyTextureInfo {@@ -545,11 +567,11 @@ impl DisplayLayer {wgpu::TexelCopyBufferLayout {offset: 0,bytes_per_row: Some(src_bytes_per_row),- rows_per_image: Some(frame_size.y),+ rows_per_image: Some(source_size.y),},wgpu::Extent3d {- width: frame_size.x,- height: frame_size.y,+ width: source_size.x,+ height: source_size.y,depth_or_array_layers: 1,},);@@ -691,8 +713,8 @@ impl DisplayLayer {encoder,y_data,uv_data,- frame_size.x,- frame_size.y,+ source_size.x,+ source_size.y,y_stride,uv_stride,);@@ -701,8 +723,8 @@ impl DisplayLayer {Ok(_) => {if self.yuv_converter.output_texture().is_some() {self.pending_copy = Some(PendingTextureCopy {- width: frame_size.x,- height: frame_size.y,+ width: source_size.x,+ height: source_size.y,dst_texture_index: next_texture,});true@@ -725,8 +747,8 @@ impl DisplayLayer {queue,y_data,uv_data,- frame_size.x,- frame_size.y,+ source_size.x,+ source_size.y,y_stride,uv_stride,);@@ -735,8 +757,8 @@ impl DisplayLayer {Ok(_) => {if self.yuv_converter.output_texture().is_some() {self.pending_copy = Some(PendingTextureCopy {- width: frame_size.x,- height: frame_size.y,+ width: source_size.x,+ height: source_size.y,dst_texture_index: next_texture,});true@@ -764,8 +786,8 @@ impl DisplayLayer {y_data,u_data,v_data,- frame_size.x,- frame_size.y,+ source_size.x,+ source_size.y,screen_frame.y_stride(),screen_frame.uv_stride(),)@@ -777,8 +799,8 @@ impl DisplayLayer {y_data,u_data,v_data,- frame_size.x,- frame_size.y,+ source_size.x,+ source_size.y,screen_frame.y_stride(),screen_frame.uv_stride(),)@@ -788,8 +810,8 @@ impl DisplayLayer {Ok(_) => {if self.yuv_converter.output_texture().is_some() {self.pending_copy = Some(PendingTextureCopy {- width: frame_size.x,- height: frame_size.y,+ width: source_size.x,+ height: source_size.y,dst_texture_index: next_texture,});truediff --git a/crates/rendering/src/lib.rs b/crates/rendering/src/lib.rsindex b79db1557..227ca3cab 100644--- a/crates/rendering/src/lib.rs+++ b/crates/rendering/src/lib.rs@@ -100,12 +100,27 @@ impl Nv12RenderStartupBreakdownMs {}}+const NON_HARDWARE_WGPU_ADAPTER_MARKERS: &[&str] = &[+ "parsec",+ "displaylink",+ "splashtop",+ "synergy",+ "virtual display",+ "microsoft basic render",+ "microsoft basic",+ "warp",+];+pub fn is_software_wgpu_adapter(info: &wgpu::AdapterInfo) -> bool {- matches!(info.device_type, wgpu::DeviceType::Cpu)- || info- .name- .to_lowercase()- .contains("microsoft basic render driver")+ matches!(+ info.device_type,+ wgpu::DeviceType::Cpu | wgpu::DeviceType::VirtualGpu+ ) || {+ let name = info.name.to_ascii_lowercase();+ NON_HARDWARE_WGPU_ADAPTER_MARKERS+ .iter()+ .any(|marker| name.contains(marker))+ }}fn force_software_wgpu_adapter() -> bool {diff --git a/crates/rendering/src/yuv_converter.rs b/crates/rendering/src/yuv_converter.rsindex 2319781a8..c1750dbbf 100644--- a/crates/rendering/src/yuv_converter.rs+++ b/crates/rendering/src/yuv_converter.rs@@ -25,6 +25,12 @@ pub enum YuvConversionError {expected: usize,actual: usize,},+ #[error("{plane} plane stride too small: expected at least {expected}, got {actual}")]+ PlaneStrideTooSmall {+ plane: &'static str,+ expected: u32,+ actual: u32,+ },#[error("{dimension} dimension ({value}) exceeds maximum allowed ({max})")]DimensionExceedsLimit {dimension: &'static str,@@ -51,6 +57,14 @@ fn upload_plane_with_stride(stride: u32,plane_name: &'static str,) -> Result<(), YuvConversionError> {+ if stride < width {+ return Err(YuvConversionError::PlaneStrideTooSmall {+ plane: plane_name,+ expected: width,+ actual: stride,+ });+ }+let expected_data_size = (stride * height) as usize;if data.len() < expected_data_size {return Err(YuvConversionError::PlaneSizeMismatch {@@ -697,6 +711,14 @@ impl YuvToRgbaConverter {upload_plane_with_stride(queue, &self.y_texture, y_data, width, height, y_stride, "Y")?;let half_height = height / 2;+ if uv_stride < width {+ return Err(YuvConversionError::PlaneStrideTooSmall {+ plane: "UV",+ expected: width,+ actual: uv_stride,+ });+ }+let expected_uv_size = (uv_stride * half_height) as usize;if uv_data.len() < expected_uv_size {return Err(YuvConversionError::PlaneSizeMismatch {@@ -778,6 +800,14 @@ impl YuvToRgbaConverter {upload_plane_with_stride(queue, &self.y_texture, y_data, width, height, y_stride, "Y")?;let half_height = height / 2;+ if uv_stride < width {+ return Err(YuvConversionError::PlaneStrideTooSmall {+ plane: "UV",+ expected: width,+ actual: uv_stride,+ });+ }+let expected_uv_size = (uv_stride * half_height) as usize;if uv_data.len() < expected_uv_size {return Err(YuvConversionError::PlaneSizeMismatch {