diff --git a/crates/recording/src/output_pipeline/win.rs b/crates/recording/src/output_pipeline/win.rs index 2ce521fb6b3..a1343df8d1b 100644 --- a/crates/recording/src/output_pipeline/win.rs +++ b/crates/recording/src/output_pipeline/win.rs @@ -358,7 +358,7 @@ impl Muxer for WindowsMuxer { _media_foundation_session = Some(session); trace!("Running native encoder with frame pacing"); let mut pacer = FramePacer::new(config.frame_rate); - let mut last_texture: Option = None; + let mut last_frame: Option = None; let mut first_timestamp: Option = None; let mut frame_count: u64 = 0; let mut frames_reused: u64 = 0; @@ -369,11 +369,12 @@ impl Muxer for WindowsMuxer { let (texture, timestamp) = match pacer.next(&video_rx, &pause_flag) { Paced::Frame(frame, timestamp) => { let texture = frame.texture().clone(); - last_texture = Some(texture.clone()); + // Retain the pool lease through the encoder's copy and any paced repeats. + last_frame = Some(frame); (texture, timestamp) } Paced::Repeat(timestamp) => { - let Some(texture) = &last_texture else { + let Some(frame) = &last_frame else { continue; }; frames_reused += 1; @@ -384,7 +385,7 @@ impl Muxer for WindowsMuxer { "Frame pacing: reusing frames due to slow capture" ); } - (texture.clone(), timestamp) + (frame.texture().clone(), timestamp) } Paced::End => { trace!("End of stream signal received"); diff --git a/crates/recording/src/output_pipeline/win_segmented.rs b/crates/recording/src/output_pipeline/win_segmented.rs index 1edaf97daf1..69417772681 100644 --- a/crates/recording/src/output_pipeline/win_segmented.rs +++ b/crates/recording/src/output_pipeline/win_segmented.rs @@ -531,6 +531,7 @@ impl WindowsSegmentedMuxer { either::Left((mut encoder, mut muxer)) => { trace!("Running native encoder for segment"); let mut first_timestamp: Option = None; + let mut last_frame: Option = None; let result = encoder.run( Arc::new(AtomicBool::default()), || { @@ -547,6 +548,8 @@ impl WindowsSegmentedMuxer { }; let frame_time = duration_to_timespan(relative); + // The encoder queues its texture copy before requesting the next frame. + let frame = last_frame.insert(frame); Ok(Some((frame.texture().clone(), frame_time))) }, |output_sample| { diff --git a/crates/recording/src/sources/screen_capture/windows.rs b/crates/recording/src/sources/screen_capture/windows.rs index ef8a9dbd343..72976398322 100644 --- a/crates/recording/src/sources/screen_capture/windows.rs +++ b/crates/recording/src/sources/screen_capture/windows.rs @@ -5,10 +5,21 @@ use crate::{ }, }; use ::windows::Win32::Graphics::Direct3D11::{ - D3D11_BIND_SHADER_RESOURCE, D3D11_BOX, D3D11_SUBRESOURCE_DATA, D3D11_TEXTURE2D_DESC, - D3D11_USAGE_DEFAULT, ID3D11Device, ID3D11Texture2D, + D3D11_BIND_RENDER_TARGET, D3D11_BIND_SHADER_RESOURCE, D3D11_BOX, D3D11_CPU_ACCESS_READ, + D3D11_MAP_READ, D3D11_MAPPED_SUBRESOURCE, D3D11_SUBRESOURCE_DATA, D3D11_TEX2D_VPIV, + D3D11_TEX2D_VPOV, D3D11_TEXTURE2D_DESC, D3D11_USAGE_DEFAULT, D3D11_USAGE_STAGING, + D3D11_VIDEO_PROCESSOR_CONTENT_DESC, D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC, + D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC, D3D11_VIDEO_PROCESSOR_STREAM, + D3D11_VPIV_DIMENSION_TEXTURE2D, D3D11_VPOV_DIMENSION_TEXTURE2D, ID3D11Device, + ID3D11DeviceContext, ID3D11Texture2D, ID3D11VideoContext, ID3D11VideoDevice, + ID3D11VideoProcessor, ID3D11VideoProcessorEnumerator, ID3D11VideoProcessorInputView, + ID3D11VideoProcessorOutputView, }; -use ::windows::Win32::Graphics::Dxgi::Common::DXGI_SAMPLE_DESC; +use ::windows::Win32::Graphics::Dxgi::Common::{ + DXGI_FORMAT, DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_RATIONAL, + DXGI_SAMPLE_DESC, +}; +use ::windows::core::Interface; use anyhow::anyhow; use cap_media_info::{AudioInfo, VideoInfo}; use cap_timestamp::{PerformanceCounterTimestamp, Timestamp}; @@ -19,7 +30,9 @@ use futures::{ }; use scap_ffmpeg::*; use scap_targets::{Display, DisplayId, Window, WindowId, bounds::PhysicalSize}; +use std::mem::ManuallyDrop; use std::{ + borrow::Cow, sync::{ Arc, Mutex, atomic::{self, AtomicBool, AtomicU32}, @@ -101,8 +114,9 @@ pub enum ScreenFrame { } pub struct ScaledScreenFrame { - texture: ID3D11Texture2D, - pixel_data: Vec, + texture: Arc, + pixel_data: Option>, + readback: Option>, width: u32, height: u32, pixel_format: scap_direct3d::PixelFormat, @@ -110,7 +124,342 @@ pub struct ScaledScreenFrame { unsafe impl Send for ScaledScreenFrame {} +struct D3D11FrameReadback { + device: ID3D11Device, + context: ID3D11DeviceContext, + staging: Mutex>, + width: u32, + height: u32, + pixel_format: scap_direct3d::PixelFormat, +} + +impl D3D11FrameReadback { + fn read(&self, texture: &ID3D11Texture2D) -> Result, ::windows::core::Error> { + let mut staging = self + .staging + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()); + if staging.is_none() { + let desc = D3D11_TEXTURE2D_DESC { + Width: self.width, + Height: self.height, + MipLevels: 1, + ArraySize: 1, + Format: pixel_format_dxgi(self.pixel_format), + SampleDesc: DXGI_SAMPLE_DESC { + Count: 1, + Quality: 0, + }, + Usage: D3D11_USAGE_STAGING, + BindFlags: 0, + CPUAccessFlags: D3D11_CPU_ACCESS_READ.0 as u32, + MiscFlags: 0, + }; + let mut created = None; + unsafe { + self.device + .CreateTexture2D(&desc, None, Some(&mut created))?; + } + *staging = Some(created.ok_or_else(::windows::core::Error::from_win32)?); + } + let staging = staging + .as_ref() + .ok_or_else(::windows::core::Error::from_win32)?; + + unsafe { + self.context.CopyResource(staging, texture); + } + let mut mapped = D3D11_MAPPED_SUBRESOURCE::default(); + unsafe { + self.context + .Map(staging, 0, D3D11_MAP_READ, 0, Some(&mut mapped))?; + } + + let row_length = self.width as usize * 4; + let source_stride = mapped.RowPitch as usize; + let mut pixels = vec![0; row_length * self.height as usize]; + for row in 0..self.height as usize { + let source_start = row * source_stride; + let destination_start = row * row_length; + let copy_length = row_length.min(source_stride); + unsafe { + // SAFETY: Map keeps pData valid until the matching Unmap. + let source = std::slice::from_raw_parts( + (mapped.pData as *const u8).add(source_start), + copy_length, + ); + pixels[destination_start..destination_start + copy_length].copy_from_slice(source); + } + } + unsafe { + self.context.Unmap(staging, 0); + } + + Ok(pixels) + } +} + +const D3D11_SCALE_TARGET_POOL_SIZE: usize = 3; + +struct D3D11ScaleTarget { + texture: Arc, + view: ID3D11VideoProcessorOutputView, +} + +impl D3D11ScaleTarget { + fn new( + device: &ID3D11Device, + video_device: &ID3D11VideoDevice, + enumerator: &ID3D11VideoProcessorEnumerator, + texture_desc: &D3D11_TEXTURE2D_DESC, + ) -> Result { + let mut texture = None; + unsafe { + device.CreateTexture2D(texture_desc, None, Some(&mut texture))?; + } + let texture = texture.ok_or_else(::windows::core::Error::from_win32)?; + let output_view_desc = D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC { + ViewDimension: D3D11_VPOV_DIMENSION_TEXTURE2D, + Anonymous: + ::windows::Win32::Graphics::Direct3D11::D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC_0 { + Texture2D: D3D11_TEX2D_VPOV { MipSlice: 0 }, + }, + }; + let mut view = None; + unsafe { + video_device.CreateVideoProcessorOutputView( + &texture, + enumerator, + &output_view_desc, + Some(&mut view), + )?; + } + Ok(Self { + texture: Arc::new(texture), + view: view.ok_or_else(::windows::core::Error::from_win32)?, + }) + } +} + +struct D3D11VideoScaler { + device: ID3D11Device, + video_device: ID3D11VideoDevice, + enumerator: ID3D11VideoProcessorEnumerator, + target_desc: D3D11_TEXTURE2D_DESC, + context: ID3D11DeviceContext, + video_context: ID3D11VideoContext, + processor: ID3D11VideoProcessor, + input_texture: ID3D11Texture2D, + input_view: ID3D11VideoProcessorInputView, + targets: Vec, + next_target: usize, + target_width: u32, + target_height: u32, + pixel_format: scap_direct3d::PixelFormat, + readback: Arc, + #[cfg(test)] + targets_created: usize, +} + +impl D3D11VideoScaler { + fn new( + device: ID3D11Device, + source_width: u32, + source_height: u32, + target_width: u32, + target_height: u32, + pixel_format: scap_direct3d::PixelFormat, + ) -> Result { + let context = unsafe { device.GetImmediateContext()? }; + let video_device: ID3D11VideoDevice = device.cast()?; + let video_context: ID3D11VideoContext = context.cast()?; + let content_desc = D3D11_VIDEO_PROCESSOR_CONTENT_DESC { + InputFrameFormat: + ::windows::Win32::Graphics::Direct3D11::D3D11_VIDEO_FRAME_FORMAT_PROGRESSIVE, + InputFrameRate: DXGI_RATIONAL { + Numerator: 30, + Denominator: 1, + }, + InputWidth: source_width, + InputHeight: source_height, + OutputFrameRate: DXGI_RATIONAL { + Numerator: 30, + Denominator: 1, + }, + OutputWidth: target_width, + OutputHeight: target_height, + Usage: ::windows::Win32::Graphics::Direct3D11::D3D11_VIDEO_USAGE_PLAYBACK_NORMAL, + }; + let enumerator = unsafe { video_device.CreateVideoProcessorEnumerator(&content_desc)? }; + let processor = unsafe { video_device.CreateVideoProcessor(&enumerator, 0)? }; + let input_texture_desc = D3D11_TEXTURE2D_DESC { + Width: source_width, + Height: source_height, + MipLevels: 1, + ArraySize: 1, + Format: pixel_format_dxgi(pixel_format), + SampleDesc: DXGI_SAMPLE_DESC { + Count: 1, + Quality: 0, + }, + Usage: D3D11_USAGE_DEFAULT, + BindFlags: (D3D11_BIND_RENDER_TARGET.0 | D3D11_BIND_SHADER_RESOURCE.0) as u32, + CPUAccessFlags: 0, + MiscFlags: 0, + }; + let mut input_texture = None; + unsafe { + device.CreateTexture2D(&input_texture_desc, None, Some(&mut input_texture))?; + } + let input_texture = input_texture.ok_or_else(::windows::core::Error::from_win32)?; + let input_view_desc = D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC { + FourCC: 0, + ViewDimension: D3D11_VPIV_DIMENSION_TEXTURE2D, + Anonymous: + ::windows::Win32::Graphics::Direct3D11::D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC_0 { + Texture2D: D3D11_TEX2D_VPIV { + MipSlice: 0, + ArraySlice: 0, + }, + }, + }; + let mut input_view = None; + unsafe { + video_device.CreateVideoProcessorInputView( + &input_texture, + &enumerator, + &input_view_desc, + Some(&mut input_view), + )?; + } + let input_view: ID3D11VideoProcessorInputView = + input_view.ok_or_else(::windows::core::Error::from_win32)?; + let texture_desc = D3D11_TEXTURE2D_DESC { + Width: target_width, + Height: target_height, + MipLevels: 1, + ArraySize: 1, + Format: pixel_format_dxgi(pixel_format), + SampleDesc: DXGI_SAMPLE_DESC { + Count: 1, + Quality: 0, + }, + Usage: D3D11_USAGE_DEFAULT, + BindFlags: (D3D11_BIND_RENDER_TARGET.0 | D3D11_BIND_SHADER_RESOURCE.0) as u32, + CPUAccessFlags: 0, + MiscFlags: 0, + }; + let mut targets = Vec::with_capacity(D3D11_SCALE_TARGET_POOL_SIZE); + for _ in 0..D3D11_SCALE_TARGET_POOL_SIZE { + targets.push(D3D11ScaleTarget::new( + &device, + &video_device, + &enumerator, + &texture_desc, + )?); + } + let readback = Arc::new(D3D11FrameReadback { + device: device.clone(), + context: context.clone(), + staging: Mutex::new(None), + width: target_width, + height: target_height, + pixel_format, + }); + + Ok(Self { + device, + video_device, + enumerator, + target_desc: texture_desc, + context, + video_context, + processor, + input_texture, + input_view, + targets, + next_target: 0, + target_width, + target_height, + pixel_format, + readback, + #[cfg(test)] + targets_created: D3D11_SCALE_TARGET_POOL_SIZE, + }) + } + + fn scale_frame( + &mut self, + input: &ID3D11Texture2D, + ) -> Result { + unsafe { + self.context.CopyResource(&self.input_texture, input); + } + let free_target = (0..self.targets.len()) + .map(|offset| (self.next_target + offset) % self.targets.len()) + .find(|&index| Arc::strong_count(&self.targets[index].texture) == 1); + let overflow; + let target = if let Some(index) = free_target { + self.next_target = (index + 1) % self.targets.len(); + &self.targets[index] + } else { + overflow = D3D11ScaleTarget::new( + &self.device, + &self.video_device, + &self.enumerator, + &self.target_desc, + )?; + #[cfg(test)] + { + self.targets_created += 1; + } + &overflow + }; + let mut stream = D3D11_VIDEO_PROCESSOR_STREAM { + Enable: true.into(), + pInputSurface: ManuallyDrop::new(Some(self.input_view.clone())), + ..Default::default() + }; + let blt = unsafe { + self.video_context.VideoProcessorBlt( + &self.processor, + &target.view, + 0, + std::slice::from_ref(&stream), + ) + }; + unsafe { + ManuallyDrop::drop(&mut stream.pInputSurface); + } + blt?; + + Ok(ScaledScreenFrame { + texture: target.texture.clone(), + pixel_data: None, + readback: Some(self.readback.clone()), + width: self.target_width, + height: self.target_height, + pixel_format: self.pixel_format, + }) + } +} + +fn pixel_format_ffmpeg(pixel_format: scap_direct3d::PixelFormat) -> ffmpeg::format::Pixel { + match pixel_format { + scap_direct3d::PixelFormat::R8G8B8A8Unorm => ffmpeg::format::Pixel::RGBA, + scap_direct3d::PixelFormat::B8G8R8A8Unorm => ffmpeg::format::Pixel::BGRA, + } +} + +fn pixel_format_dxgi(pixel_format: scap_direct3d::PixelFormat) -> DXGI_FORMAT { + match pixel_format { + scap_direct3d::PixelFormat::R8G8B8A8Unorm => DXGI_FORMAT_R8G8B8A8_UNORM, + scap_direct3d::PixelFormat::B8G8R8A8Unorm => DXGI_FORMAT_B8G8R8A8_UNORM, + } +} + impl ScreenFrame { + // COM clones do not retain the pool lease; keep the frame until GPU reads are queued. pub fn texture(&self) -> &ID3D11Texture2D { match self { ScreenFrame::Captured(frame) => frame.texture(), @@ -139,9 +488,18 @@ impl ScreenFrame { frame.as_ffmpeg() } ScreenFrame::Scaled(scaled) => { - let ffmpeg_pixel = match scaled.pixel_format { - scap_direct3d::PixelFormat::R8G8B8A8Unorm => ffmpeg::format::Pixel::RGBA, - scap_direct3d::PixelFormat::B8G8R8A8Unorm => ffmpeg::format::Pixel::BGRA, + let ffmpeg_pixel = pixel_format_ffmpeg(scaled.pixel_format); + let readback; + let pixel_data = match &scaled.pixel_data { + Some(pixel_data) => Cow::Borrowed(pixel_data.as_slice()), + None => { + readback = scaled + .readback + .as_ref() + .ok_or_else(::windows::core::Error::from_win32)? + .read(&scaled.texture)?; + Cow::Borrowed(readback.as_slice()) + } }; let mut ff_frame = ffmpeg::frame::Video::new(ffmpeg_pixel, scaled.width, scaled.height); @@ -153,15 +511,14 @@ impl ScreenFrame { let src_start = row * row_length; let dst_start = row * dest_stride; let copy_len = row_length.min( - scaled - .pixel_data + pixel_data .len() .saturating_sub(src_start) .min(dest_bytes.len().saturating_sub(dst_start)), ); if copy_len > 0 { dest_bytes[dst_start..dst_start + copy_len] - .copy_from_slice(&scaled.pixel_data[src_start..src_start + copy_len]); + .copy_from_slice(&pixel_data[src_start..src_start + copy_len]); } } @@ -172,7 +529,8 @@ impl ScreenFrame { } struct FrameScalerState { - context: ffmpeg::software::scaling::Context, + context: Option, + video_scaler: Option, source_width: u32, source_height: u32, } @@ -213,11 +571,55 @@ impl WindowsFrameScaler { .is_none_or(|s| s.source_width != src_width || s.source_height != src_height); if needs_reinit { - let src_pixel = match self.pixel_format { - scap_direct3d::PixelFormat::R8G8B8A8Unorm => ffmpeg::format::Pixel::RGBA, - scap_direct3d::PixelFormat::B8G8R8A8Unorm => ffmpeg::format::Pixel::BGRA, + let video_scaler = match D3D11VideoScaler::new( + self.d3d_device.clone(), + src_width, + src_height, + self.target_width, + self.target_height, + self.pixel_format, + ) { + Ok(scaler) => Some(scaler), + Err(error) => { + warn!(?error, "D3D11 screen scaling unavailable; using CPU scaler"); + None + } }; + self.state = Some(FrameScalerState { + context: None, + video_scaler, + source_width: src_width, + source_height: src_height, + }); + } + + let gpu_result = self + .state + .as_mut()? + .video_scaler + .as_mut() + .map(|scaler| scaler.scale_frame(frame.texture())); + if let Some(result) = gpu_result { + match result { + Ok(scaled) => return Some(ScreenFrame::Scaled(scaled)), + Err(error) => { + warn!(?error, "D3D11 screen scaling failed; using CPU scaler"); + self.state.as_mut()?.video_scaler = None; + } + } + } + + self.scale_frame_cpu(frame, src_width, src_height) + } + fn scale_frame_cpu( + &mut self, + frame: &scap_direct3d::Frame, + src_width: u32, + src_height: u32, + ) -> Option { + if self.state.as_ref()?.context.is_none() { + let src_pixel = pixel_format_ffmpeg(self.pixel_format); let context = ffmpeg::software::scaling::Context::get( src_pixel, src_width, @@ -228,23 +630,14 @@ impl WindowsFrameScaler { ffmpeg::software::scaling::Flags::BILINEAR, ) .ok()?; - - self.state = Some(FrameScalerState { - context, - source_width: src_width, - source_height: src_height, - }); + self.state.as_mut()?.context = Some(context); } let buffer = frame.as_buffer().ok()?; let src_data = buffer.data(); let src_stride = buffer.stride() as usize; let row_length = (src_width * 4) as usize; - - let src_pixel = match self.pixel_format { - scap_direct3d::PixelFormat::R8G8B8A8Unorm => ffmpeg::format::Pixel::RGBA, - scap_direct3d::PixelFormat::B8G8R8A8Unorm => ffmpeg::format::Pixel::BGRA, - }; + let src_pixel = pixel_format_ffmpeg(self.pixel_format); let mut src_frame = ffmpeg::frame::Video::new(src_pixel, src_width, src_height); let ff_stride = src_frame.stride(0); @@ -270,7 +663,11 @@ impl WindowsFrameScaler { let state = self.state.as_mut()?; let mut dst_frame = ffmpeg::frame::Video::new(src_pixel, self.target_width, self.target_height); - state.context.run(&src_frame, &mut dst_frame).ok()?; + state + .context + .as_mut()? + .run(&src_frame, &mut dst_frame) + .ok()?; let dst_stride = dst_frame.stride(0); let dst_row_length = (self.target_width * 4) as usize; @@ -293,21 +690,12 @@ impl WindowsFrameScaler { } } - let dxgi_format = match self.pixel_format { - scap_direct3d::PixelFormat::R8G8B8A8Unorm => { - ::windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R8G8B8A8_UNORM - } - scap_direct3d::PixelFormat::B8G8R8A8Unorm => { - ::windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_B8G8R8A8_UNORM - } - }; - let texture_desc = D3D11_TEXTURE2D_DESC { Width: self.target_width, Height: self.target_height, MipLevels: 1, ArraySize: 1, - Format: dxgi_format, + Format: pixel_format_dxgi(self.pixel_format), SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0, @@ -333,8 +721,9 @@ impl WindowsFrameScaler { }; Some(ScreenFrame::Scaled(ScaledScreenFrame { - texture, - pixel_data, + texture: Arc::new(texture), + pixel_data: Some(pixel_data), + readback: None, width: self.target_width, height: self.target_height, pixel_format: self.pixel_format, @@ -1676,6 +2065,568 @@ mod first_screen_frame_tests { assert!(error.to_string().contains("closed before its first frame")); } } +#[cfg(test)] +mod windows_screen_scale_tests { + use super::*; + use ::windows::Win32::Foundation::{E_NOINTERFACE, HMODULE}; + use ::windows::Win32::Graphics::Direct3D::{D3D_DRIVER_TYPE_UNKNOWN, D3D_DRIVER_TYPE_WARP}; + use ::windows::Win32::Graphics::Direct3D11::{ + D3D11_CREATE_DEVICE_BGRA_SUPPORT, D3D11_SDK_VERSION, D3D11CreateDevice, + }; + use ::windows::Win32::Graphics::Dxgi::{CreateDXGIFactory1, IDXGIDevice, IDXGIFactory1}; + use std::time::Instant; + + fn create_device(selector: &str) -> ID3D11Device { + let mut device = None; + if selector == "warp" { + unsafe { + D3D11CreateDevice( + None, + D3D_DRIVER_TYPE_WARP, + HMODULE::default(), + D3D11_CREATE_DEVICE_BGRA_SUPPORT, + None, + D3D11_SDK_VERSION, + Some(&mut device), + None, + None, + ) + .expect("create WARP device"); + } + } else { + let adapter = if selector == "hardware" { + cap_d3d_adapter::select_capture_adapter(None) + .expect("physical hardware adapter") + .adapter + } else { + let index = selector.parse::().expect( + "CAP_D3D11_SCALE_ADAPTER must be hardware, warp, or a DXGI adapter index", + ); + let factory: IDXGIFactory1 = unsafe { CreateDXGIFactory1().unwrap() }; + unsafe { factory.EnumAdapters1(index).expect("DXGI adapter index") } + .cast() + .unwrap() + }; + unsafe { + D3D11CreateDevice( + &adapter, + D3D_DRIVER_TYPE_UNKNOWN, + HMODULE::default(), + D3D11_CREATE_DEVICE_BGRA_SUPPORT, + None, + D3D11_SDK_VERSION, + Some(&mut device), + None, + None, + ) + .expect("create device on specified adapter"); + } + } + let device = device.unwrap(); + let dxgi_device: IDXGIDevice = device.cast().unwrap(); + let desc = unsafe { dxgi_device.GetAdapter().unwrap().GetDesc().unwrap() }; + let length = desc.Description.iter().take_while(|&&c| c != 0).count(); + println!( + "ADAPTER selector={selector} description={} vendor_id=0x{:04X} device_id=0x{:04X}", + String::from_utf16_lossy(&desc.Description[..length]), + desc.VendorId, + desc.DeviceId, + ); + device + } + + fn selected_device() -> ID3D11Device { + create_device(&std::env::var("CAP_D3D11_SCALE_ADAPTER").unwrap_or("hardware".into())) + } + + struct CpuScaler { + device: ID3D11Device, + context: ::windows::Win32::Graphics::Direct3D11::ID3D11DeviceContext, + staging: ID3D11Texture2D, + scaler: ffmpeg::software::scaling::Context, + source_width: u32, + source_height: u32, + target_width: u32, + target_height: u32, + pixel_format: scap_direct3d::PixelFormat, + } + + impl CpuScaler { + fn new( + device: ID3D11Device, + source_width: u32, + source_height: u32, + target_width: u32, + target_height: u32, + pixel_format: scap_direct3d::PixelFormat, + ) -> Self { + let context = unsafe { device.GetImmediateContext().unwrap() }; + let staging_desc = D3D11_TEXTURE2D_DESC { + Width: source_width, + Height: source_height, + MipLevels: 1, + ArraySize: 1, + Format: pixel_format_dxgi(pixel_format), + SampleDesc: DXGI_SAMPLE_DESC { + Count: 1, + Quality: 0, + }, + Usage: D3D11_USAGE_STAGING, + BindFlags: 0, + CPUAccessFlags: D3D11_CPU_ACCESS_READ.0 as u32, + MiscFlags: 0, + }; + let mut staging = None; + unsafe { + device + .CreateTexture2D(&staging_desc, None, Some(&mut staging)) + .unwrap(); + } + let scaler = ffmpeg::software::scaling::Context::get( + pixel_format_ffmpeg(pixel_format), + source_width, + source_height, + pixel_format_ffmpeg(pixel_format), + target_width, + target_height, + ffmpeg::software::scaling::Flags::BILINEAR, + ) + .unwrap(); + Self { + device, + context, + staging: staging.unwrap(), + scaler, + source_width, + source_height, + target_width, + target_height, + pixel_format, + } + } + + fn scale(&mut self, texture: &ID3D11Texture2D) -> ScreenFrame { + unsafe { + self.context.CopyResource(&self.staging, texture); + } + let mut mapped = D3D11_MAPPED_SUBRESOURCE::default(); + unsafe { + self.context + .Map(&self.staging, 0, D3D11_MAP_READ, 0, Some(&mut mapped)) + .unwrap(); + } + let source_stride = mapped.RowPitch as usize; + let source_row_length = self.source_width as usize * 4; + let mut source_frame = ffmpeg::frame::Video::new( + pixel_format_ffmpeg(self.pixel_format), + self.source_width, + self.source_height, + ); + let source_ffmpeg_stride = source_frame.stride(0); + let source_ffmpeg_data = source_frame.data_mut(0); + for row in 0..self.source_height as usize { + let source_start = row * source_stride; + let destination_start = row * source_ffmpeg_stride; + let copy_length = source_row_length + .min(source_ffmpeg_data.len().saturating_sub(destination_start)); + unsafe { + // SAFETY: Map keeps pData valid until the matching Unmap. + let source = std::slice::from_raw_parts( + (mapped.pData as *const u8).add(source_start), + copy_length, + ); + source_ffmpeg_data[destination_start..destination_start + copy_length] + .copy_from_slice(source); + } + } + unsafe { + self.context.Unmap(&self.staging, 0); + } + + let mut output_frame = ffmpeg::frame::Video::new( + pixel_format_ffmpeg(self.pixel_format), + self.target_width, + self.target_height, + ); + self.scaler.run(&source_frame, &mut output_frame).unwrap(); + let row_length = self.target_width as usize * 4; + let mut pixel_data = vec![0; row_length * self.target_height as usize]; + let output_stride = output_frame.stride(0); + let output_data = output_frame.data(0); + for row in 0..self.target_height as usize { + let source_start = row * output_stride; + let destination_start = row * row_length; + pixel_data[destination_start..destination_start + row_length] + .copy_from_slice(&output_data[source_start..source_start + row_length]); + } + + let texture_desc = D3D11_TEXTURE2D_DESC { + Width: self.target_width, + Height: self.target_height, + MipLevels: 1, + ArraySize: 1, + Format: pixel_format_dxgi(self.pixel_format), + SampleDesc: DXGI_SAMPLE_DESC { + Count: 1, + Quality: 0, + }, + Usage: D3D11_USAGE_DEFAULT, + BindFlags: D3D11_BIND_SHADER_RESOURCE.0 as u32, + CPUAccessFlags: 0, + MiscFlags: 0, + }; + let data = D3D11_SUBRESOURCE_DATA { + pSysMem: pixel_data.as_ptr().cast(), + SysMemPitch: row_length as u32, + SysMemSlicePitch: 0, + }; + let mut output_texture = None; + unsafe { + self.device + .CreateTexture2D(&texture_desc, Some(&data), Some(&mut output_texture)) + .unwrap(); + } + ScreenFrame::Scaled(ScaledScreenFrame { + texture: Arc::new(output_texture.unwrap()), + pixel_data: Some(pixel_data), + readback: None, + width: self.target_width, + height: self.target_height, + pixel_format: self.pixel_format, + }) + } + } + + fn create_source_texture( + device: &ID3D11Device, + width: u32, + height: u32, + pixel_format: scap_direct3d::PixelFormat, + solid: Option<[u8; 4]>, + ) -> ID3D11Texture2D { + let mut pixels = vec![0u8; width as usize * height as usize * 4]; + for y in 0..height { + for x in 0..width { + let index = (y as usize * width as usize + x as usize) * 4; + let rgba = solid.unwrap_or([ + (x * 255 / (width - 1)) as u8, + (y * 255 / (height - 1)) as u8, + ((x + y) * 255 / (width + height - 2)) as u8, + 255, + ]); + let channels = match pixel_format { + scap_direct3d::PixelFormat::R8G8B8A8Unorm => rgba, + scap_direct3d::PixelFormat::B8G8R8A8Unorm => { + [rgba[2], rgba[1], rgba[0], rgba[3]] + } + }; + pixels[index..index + 4].copy_from_slice(&channels); + } + } + let desc = D3D11_TEXTURE2D_DESC { + Width: width, + Height: height, + MipLevels: 1, + ArraySize: 1, + Format: pixel_format_dxgi(pixel_format), + SampleDesc: DXGI_SAMPLE_DESC { + Count: 1, + Quality: 0, + }, + Usage: D3D11_USAGE_DEFAULT, + BindFlags: (D3D11_BIND_RENDER_TARGET.0 | D3D11_BIND_SHADER_RESOURCE.0) as u32, + CPUAccessFlags: 0, + MiscFlags: 0, + }; + let data = D3D11_SUBRESOURCE_DATA { + pSysMem: pixels.as_ptr().cast(), + SysMemPitch: width * 4, + SysMemSlicePitch: 0, + }; + let mut texture = None; + unsafe { + device + .CreateTexture2D(&desc, Some(&data), Some(&mut texture)) + .unwrap(); + } + texture.unwrap() + } + + fn run_scenario( + device: &ID3D11Device, + source: (u32, u32), + target: (u32, u32), + pixel_format: scap_direct3d::PixelFormat, + solid: Option<[u8; 4]>, + benchmark: bool, + ) { + ffmpeg::init().unwrap(); + let mut video_scaler = D3D11VideoScaler::new( + device.clone(), + source.0, + source.1, + target.0, + target.1, + pixel_format, + ) + .unwrap_or_else(|error| { + panic!( + "D3D11 video scaling unavailable: {error:?}. WARP does not expose ID3D11VideoDevice; pixel parity and video-processor benchmarks require a supporting hardware adapter." + ) + }); + let source_texture = create_source_texture(device, source.0, source.1, pixel_format, solid); + let mut cpu_scaler = CpuScaler::new( + device.clone(), + source.0, + source.1, + target.0, + target.1, + pixel_format, + ); + + let cpu_output = cpu_scaler.scale(&source_texture).as_ffmpeg().unwrap(); + let gpu_output = ScreenFrame::Scaled(video_scaler.scale_frame(&source_texture).unwrap()) + .as_ffmpeg() + .unwrap(); + let mut max_error = 0u8; + let mut total_error = 0u64; + let mut compared_channels = 0u64; + let mut channel_max = [0u8; 4]; + let mut channel_total = [0u64; 4]; + let row_length = target.0 as usize * 4; + for row in 0..target.1 as usize { + let cpu_start = row * cpu_output.stride(0); + let gpu_start = row * gpu_output.stride(0); + let cpu_row = &cpu_output.data(0)[cpu_start..cpu_start + row_length]; + let gpu_row = &gpu_output.data(0)[gpu_start..gpu_start + row_length]; + for (index, (cpu, gpu)) in cpu_row.iter().zip(gpu_row).enumerate() { + let error = cpu.abs_diff(*gpu); + max_error = max_error.max(error); + total_error += error as u64; + compared_channels += 1; + channel_max[index % 4] = channel_max[index % 4].max(error); + channel_total[index % 4] += error as u64; + } + } + let mean_error = total_error as f64 / compared_channels as f64; + let channel_mean = channel_total.map(|total| total as f64 / (target.0 * target.1) as f64); + println!( + "PIXELS {:?} solid={solid:?} {}x{} -> {}x{} max delta {} mean delta {:.6}; channel max {:?} mean {:.6?}", + pixel_format, + source.0, + source.1, + target.0, + target.1, + max_error, + mean_error, + channel_max, + channel_mean, + ); + assert!(mean_error <= 4.0 && max_error <= 20); + + if !benchmark { + return; + } + for _ in 0..3 { + std::hint::black_box(cpu_scaler.scale(&source_texture)); + let scaled = ScreenFrame::Scaled(video_scaler.scale_frame(&source_texture).unwrap()); + std::hint::black_box(scaled.as_ffmpeg().unwrap()); + } + let mut scale_samples = Vec::new(); + let mut software_samples = Vec::new(); + let mut cpu_samples = Vec::new(); + for _ in 0..3 { + let started = Instant::now(); + for _ in 0..12 { + std::hint::black_box(video_scaler.scale_frame(&source_texture).unwrap()); + } + std::hint::black_box( + video_scaler + .readback + .read(&video_scaler.targets[0].texture) + .unwrap(), + ); + scale_samples.push(started.elapsed().as_secs_f64() * 1000.0 / 12.0); + + let started = Instant::now(); + for _ in 0..12 { + let scaled = + ScreenFrame::Scaled(video_scaler.scale_frame(&source_texture).unwrap()); + std::hint::black_box(scaled.as_ffmpeg().unwrap()); + } + software_samples.push(started.elapsed().as_secs_f64() * 1000.0 / 12.0); + + let started = Instant::now(); + for _ in 0..12 { + std::hint::black_box(cpu_scaler.scale(&source_texture)); + } + cpu_samples.push(started.elapsed().as_secs_f64() * 1000.0 / 12.0); + } + scale_samples.sort_by(f64::total_cmp); + software_samples.sort_by(f64::total_cmp); + cpu_samples.sort_by(f64::total_cmp); + println!( + "D3D11 {}x{} -> {}x{} scale median {:.3} ms/frame (runs {:.3}, {:.3}, {:.3}); software-output median {:.3} ms/frame (runs {:.3}, {:.3}, {:.3})", + source.0, + source.1, + target.0, + target.1, + scale_samples[1], + scale_samples[0], + scale_samples[1], + scale_samples[2], + software_samples[1], + software_samples[0], + software_samples[1], + software_samples[2] + ); + println!( + "CPU {}x{} -> {}x{} median {:.3} ms/frame (runs {:.3}, {:.3}, {:.3}); output textures created {}", + source.0, + source.1, + target.0, + target.1, + cpu_samples[1], + cpu_samples[0], + cpu_samples[1], + cpu_samples[2], + video_scaler.targets_created, + ); + assert_eq!(video_scaler.targets_created, D3D11_SCALE_TARGET_POOL_SIZE); + } + + #[test] + #[ignore = "requires a video-capable adapter; CAP_D3D11_SCALE_ADAPTER=hardware|warp|"] + fn d3d11_scale_pixel_parity() { + let device = selected_device(); + for format in [ + scap_direct3d::PixelFormat::R8G8B8A8Unorm, + scap_direct3d::PixelFormat::B8G8R8A8Unorm, + ] { + run_scenario(&device, (1920, 1080), (1280, 720), format, None, false); + run_scenario(&device, (3840, 2160), (1920, 1080), format, None, false); + run_scenario(&device, (638, 358), (318, 178), format, None, false); + for color in [ + [255, 0, 0, 255], + [0, 255, 0, 255], + [0, 0, 255, 255], + [128, 128, 128, 255], + ] { + run_scenario(&device, (64, 48), (32, 24), format, Some(color), false); + } + } + } + + #[test] + fn d3d11_scale_warp_reports_unsupported_video_processing() { + let device = create_device("warp"); + let result = D3D11VideoScaler::new( + device.clone(), + 64, + 48, + 32, + 24, + scap_direct3d::PixelFormat::R8G8B8A8Unorm, + ); + let Err(error) = result else { + panic!("WARP unexpectedly supports ID3D11VideoDevice"); + }; + println!("WARP video processor unsupported: {error:?}; CPU fallback required"); + assert_eq!(error.code(), E_NOINTERFACE); + for format in [ + scap_direct3d::PixelFormat::R8G8B8A8Unorm, + scap_direct3d::PixelFormat::B8G8R8A8Unorm, + ] { + let source = create_source_texture(&device, 64, 48, format, Some([255, 0, 0, 255])); + let mut cpu_scaler = CpuScaler::new(device.clone(), 64, 48, 32, 24, format); + let frame = cpu_scaler.scale(&source).as_ffmpeg().unwrap(); + let expected = match format { + scap_direct3d::PixelFormat::R8G8B8A8Unorm => [255, 0, 0, 255], + scap_direct3d::PixelFormat::B8G8R8A8Unorm => [0, 0, 255, 255], + }; + for row in 0..24 { + let start = row * frame.stride(0); + assert!( + frame.data(0)[start..start + 32 * 4] + .chunks_exact(4) + .all(|pixel| pixel == expected) + ); + } + println!("WARP CPU scaler {format:?} 64x48 -> 32x24: solid red preserved"); + } + } + + #[test] + #[ignore = "requires a video-capable adapter; CAP_D3D11_SCALE_ADAPTER=hardware|"] + fn d3d11_scale_pool_reuses_targets_without_allocating() { + let device = selected_device(); + let format = scap_direct3d::PixelFormat::R8G8B8A8Unorm; + let source = create_source_texture(&device, 64, 48, format, None); + let mut scaler = D3D11VideoScaler::new(device, 64, 48, 32, 24, format).unwrap(); + for index in 0..120 { + let frame = scaler.scale_frame(&source).unwrap(); + assert!(Arc::ptr_eq( + &frame.texture, + &scaler.targets[index % D3D11_SCALE_TARGET_POOL_SIZE].texture, + )); + drop(frame); + } + println!( + "POOL 120 promptly dropped frames: {} output textures created", + scaler.targets_created + ); + assert_eq!(scaler.targets_created, D3D11_SCALE_TARGET_POOL_SIZE); + } + + #[test] + #[ignore = "requires a video-capable adapter; CAP_D3D11_SCALE_ADAPTER=hardware|"] + fn d3d11_scale_pool_preserves_in_flight_frames() { + let device = selected_device(); + let format = scap_direct3d::PixelFormat::R8G8B8A8Unorm; + let mut scaler = D3D11VideoScaler::new(device.clone(), 64, 48, 32, 24, format).unwrap(); + let mut held = Vec::new(); + let mut expected = Vec::new(); + for color in [[255, 0, 0, 255], [0, 255, 0, 255], [0, 0, 255, 255]] { + let source = create_source_texture(&device, 64, 48, format, Some(color)); + let frame = scaler.scale_frame(&source).unwrap(); + expected.push(scaler.readback.read(&frame.texture).unwrap()); + held.push(frame); + } + let source = create_source_texture(&device, 64, 48, format, Some([255; 4])); + for _ in 0..12 { + let overflow = scaler.scale_frame(&source).unwrap(); + assert!( + held.iter() + .all(|frame| !Arc::ptr_eq(&frame.texture, &overflow.texture)) + ); + drop(overflow); + } + assert_eq!(scaler.targets_created, D3D11_SCALE_TARGET_POOL_SIZE + 12); + for (frame, pixels) in held.iter().zip(&expected) { + assert_eq!(&scaler.readback.read(&frame.texture).unwrap(), pixels); + } + let released = held.remove(1); + let released_pointer = Arc::as_ptr(&released.texture); + drop(released); + let reused = scaler.scale_frame(&source).unwrap(); + assert_eq!(Arc::as_ptr(&reused.texture), released_pointer); + assert_eq!(scaler.targets_created, D3D11_SCALE_TARGET_POOL_SIZE + 12); + println!("POOL held frames preserved through 12 overflows; released slot reused"); + drop(scaler); + let frame = ScreenFrame::Scaled(held.remove(0)); + let pixels = frame.as_ffmpeg().unwrap(); + assert_eq!(&pixels.data(0)[..32 * 4], &expected[0][..32 * 4]); + } + + #[test] + #[ignore = "requires a video-capable adapter; CAP_D3D11_SCALE_ADAPTER=hardware|warp|"] + fn run_windows_screen_scale_benchmark() { + let device = selected_device(); + let format = scap_direct3d::PixelFormat::R8G8B8A8Unorm; + run_scenario(&device, (1920, 1080), (1280, 720), format, None, true); + run_scenario(&device, (3840, 2160), (1920, 1080), format, None, true); + } +} #[cfg(test)] mod capture_target_tests {