Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

test(verify): flip-model stimulus presentation and crash snapshots on Windows, Windows camera pacing xfails

Richie McIlroy276749a2b930
diff --git a/Cargo.lock b/Cargo.lock
index 38bfa138d..64f834871 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2207,6 +2207,7 @@ dependencies = [
"scap-screencapturekit",
"serde_json",
"softbuffer",
+ "windows 0.60.0",
"winit",
"workspace-hack",
]
diff --git a/crates/verify-stimulus/Cargo.toml b/crates/verify-stimulus/Cargo.toml
index 8706b3942..54b13281c 100644
--- a/crates/verify-stimulus/Cargo.toml
+++ b/crates/verify-stimulus/Cargo.toml
@@ -29,5 +29,11 @@ winit = "0.30"
[target.'cfg(windows)'.dependencies]
cpal = { workspace = true }
-softbuffer = "0.4"
winit = "0.30"
+windows = { workspace = true, features = [
+ "Win32_Foundation",
+ "Win32_Graphics_Direct3D",
+ "Win32_Graphics_Direct3D11",
+ "Win32_Graphics_Dxgi",
+ "Win32_Graphics_Dxgi_Common",
+] }
diff --git a/crates/verify-stimulus/src/main.rs b/crates/verify-stimulus/src/main.rs
index 84f5e0333..5e332962b 100644
--- a/crates/verify-stimulus/src/main.rs
+++ b/crates/verify-stimulus/src/main.rs
@@ -19,7 +19,6 @@ mod app {
use std::{
fs::File,
io::{BufRead, Write},
- num::NonZeroU32,
path::PathBuf,
sync::{
Arc, Mutex,
@@ -340,7 +339,7 @@ mod app {
epoch: Arc<AtomicU64>,
go_at: Option<(Instant, u64)>,
window: Option<Arc<Window>>,
- surface: Option<softbuffer::Surface<Arc<Window>, Arc<Window>>>,
+ surface: Option<canvas::Canvas>,
size: (u32, u32),
shown: Option<u32>,
ready: bool,
@@ -415,21 +414,10 @@ mod app {
if w == 0 || h == 0 {
return;
}
- if surface
- .resize(NonZeroU32::new(w).unwrap(), NonZeroU32::new(h).unwrap())
- .is_err()
- {
- return;
- }
- let Ok(mut buffer) = surface.buffer_mut() else {
- return;
- };
- let (w, h) = (w as usize, h as usize);
- let band = if self.spec.busy { h / 8 } else { 0 };
- {
- let bytes: &mut [u8] = unsafe {
- std::slice::from_raw_parts_mut(buffer.as_mut_ptr().cast::<u8>(), w * h * 4)
- };
+ let busy = self.spec.busy;
+ let presented = canvas::present(surface, w, h, |bytes| {
+ let (w, h) = (w as usize, h as usize);
+ let band = if busy { h / 8 } else { 0 };
let stride = w * 4;
let marker = FrameMarker::new(StreamKind::Screen, 0, index);
video_code::draw_marker(
@@ -450,8 +438,8 @@ mod app {
(salt << 1) | 1,
);
}
- }
- if buffer.present().is_ok() {
+ });
+ if presented {
let host = host_now();
if self.shown != Some(index) {
self.log.write(&StimulusEvent::Frame {
@@ -468,6 +456,166 @@ mod app {
}
}
+ /// Where the stimulus draws BGRA pictures and presents them.
+ #[cfg(target_os = "macos")]
+ mod canvas {
+ use super::*;
+ use std::num::NonZeroU32;
+
+ pub type Canvas = softbuffer::Surface<Arc<Window>, Arc<Window>>;
+
+ pub fn new(window: &Arc<Window>) -> Result<Canvas, String> {
+ softbuffer::Context::new(window.clone())
+ .and_then(|context| softbuffer::Surface::new(&context, window.clone()))
+ .map_err(|e| e.to_string())
+ }
+
+ pub fn present(canvas: &mut Canvas, w: u32, h: u32, paint: impl FnOnce(&mut [u8])) -> bool {
+ if canvas
+ .resize(NonZeroU32::new(w).unwrap(), NonZeroU32::new(h).unwrap())
+ .is_err()
+ {
+ return false;
+ }
+ let Ok(mut buffer) = canvas.buffer_mut() else {
+ return false;
+ };
+ let bytes: &mut [u8] = unsafe {
+ std::slice::from_raw_parts_mut(
+ buffer.as_mut_ptr().cast::<u8>(),
+ w as usize * h as usize * 4,
+ )
+ };
+ paint(bytes);
+ buffer.present().is_ok()
+ }
+ }
+
+ /// A flip-model DXGI swap chain. softbuffer presents on Windows with a
+ /// GDI blit that the compositor (and so Windows Graphics Capture) can
+ /// sample half done: recordings showed markers whose top rows were the
+ /// new picture and bottom rows the old one (run 39000141528). A flip
+ /// present hands the compositor whole pictures.
+ #[cfg(windows)]
+ mod canvas {
+ use super::*;
+ use windows::Win32::{
+ Foundation::{HMODULE, HWND},
+ Graphics::{
+ Direct3D::D3D_DRIVER_TYPE_HARDWARE,
+ Direct3D11::{
+ D3D11_CREATE_DEVICE_BGRA_SUPPORT, D3D11_SDK_VERSION, D3D11CreateDevice,
+ ID3D11Device, ID3D11DeviceContext, ID3D11Texture2D,
+ },
+ Dxgi::{
+ Common::{
+ DXGI_ALPHA_MODE_IGNORE, DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_UNKNOWN,
+ DXGI_SAMPLE_DESC,
+ },
+ CreateDXGIFactory2, DXGI_CREATE_FACTORY_FLAGS, DXGI_PRESENT, DXGI_SCALING_NONE,
+ DXGI_SWAP_CHAIN_DESC1, DXGI_SWAP_CHAIN_FLAG, DXGI_SWAP_EFFECT_FLIP_DISCARD,
+ DXGI_USAGE_RENDER_TARGET_OUTPUT, IDXGIFactory2, IDXGISwapChain1,
+ },
+ },
+ };
+ use winit::raw_window_handle::{HasWindowHandle, RawWindowHandle};
+
+ pub struct Canvas {
+ context: ID3D11DeviceContext,
+ swap_chain: IDXGISwapChain1,
+ size: (u32, u32),
+ pixels: Vec<u8>,
+ }
+
+ pub fn new(window: &Arc<Window>) -> Result<Canvas, String> {
+ let hwnd = match window.window_handle().map_err(|e| e.to_string())?.as_raw() {
+ RawWindowHandle::Win32(handle) => HWND(handle.hwnd.get() as *mut core::ffi::c_void),
+ other => return Err(format!("not a Win32 window: {other:?}")),
+ };
+ let size = window.inner_size();
+ let size = (size.width.max(1), size.height.max(1));
+ let mut device: Option<ID3D11Device> = None;
+ let mut context: Option<ID3D11DeviceContext> = None;
+ unsafe {
+ D3D11CreateDevice(
+ None,
+ D3D_DRIVER_TYPE_HARDWARE,
+ HMODULE::default(),
+ D3D11_CREATE_DEVICE_BGRA_SUPPORT,
+ None,
+ D3D11_SDK_VERSION,
+ Some(&mut device),
+ None,
+ Some(&mut context),
+ )
+ }
+ .map_err(|e| format!("D3D11CreateDevice: {e}"))?;
+ let device = device.ok_or("no D3D11 device")?;
+ let context = context.ok_or("no D3D11 context")?;
+ let factory: IDXGIFactory2 =
+ unsafe { CreateDXGIFactory2(DXGI_CREATE_FACTORY_FLAGS(0)) }
+ .map_err(|e| format!("CreateDXGIFactory2: {e}"))?;
+ let desc = DXGI_SWAP_CHAIN_DESC1 {
+ Width: size.0,
+ Height: size.1,
+ Format: DXGI_FORMAT_B8G8R8A8_UNORM,
+ SampleDesc: DXGI_SAMPLE_DESC {
+ Count: 1,
+ Quality: 0,
+ },
+ BufferUsage: DXGI_USAGE_RENDER_TARGET_OUTPUT,
+ BufferCount: 2,
+ Scaling: DXGI_SCALING_NONE,
+ SwapEffect: DXGI_SWAP_EFFECT_FLIP_DISCARD,
+ AlphaMode: DXGI_ALPHA_MODE_IGNORE,
+ ..Default::default()
+ };
+ let swap_chain =
+ unsafe { factory.CreateSwapChainForHwnd(&device, hwnd, &desc, None, None) }
+ .map_err(|e| format!("CreateSwapChainForHwnd: {e}"))?;
+ Ok(Canvas {
+ context,
+ swap_chain,
+ size,
+ pixels: Vec::new(),
+ })
+ }
+
+ pub fn present(canvas: &mut Canvas, w: u32, h: u32, paint: impl FnOnce(&mut [u8])) -> bool {
+ if canvas.size != (w, h) {
+ let resized = unsafe {
+ canvas.swap_chain.ResizeBuffers(
+ 0,
+ w,
+ h,
+ DXGI_FORMAT_UNKNOWN,
+ DXGI_SWAP_CHAIN_FLAG(0),
+ )
+ };
+ if resized.is_err() {
+ return false;
+ }
+ canvas.size = (w, h);
+ }
+ canvas.pixels.resize(w as usize * h as usize * 4, 0);
+ paint(&mut canvas.pixels);
+ let Ok(back) = (unsafe { canvas.swap_chain.GetBuffer::<ID3D11Texture2D>(0) }) else {
+ return false;
+ };
+ unsafe {
+ canvas.context.UpdateSubresource(
+ &back,
+ 0,
+ None,
+ canvas.pixels.as_ptr().cast(),
+ w * 4,
+ 0,
+ );
+ }
+ unsafe { canvas.swap_chain.Present(0, DXGI_PRESENT(0)) }.is_ok()
+ }
+ }
+
/// Achromatic 16 px blocks whose levels change every frame.
fn noise_band(data: &mut [u8], w: usize, h: usize, index: u32, salt: u64) {
let stride = w * 4;
@@ -516,9 +664,7 @@ mod app {
};
let size = window.inner_size();
self.size = (size.width, size.height);
- let surface = softbuffer::Context::new(window.clone())
- .and_then(|context| softbuffer::Surface::new(&context, window.clone()));
- match surface {
+ match canvas::new(&window) {
Ok(s) => self.surface = Some(s),
Err(e) => {
self.error = Some(format!("surface: {e}"));
diff --git a/crates/verify/expectations.toml b/crates/verify/expectations.toml
index f59ca5d42..468c37aa4 100644
--- a/crates/verify/expectations.toml
+++ b/crates/verify/expectations.toml
@@ -320,9 +320,33 @@ link = ""
[[xfail]]
case = "recording/instant/late-mic"
class = "sync-screen-mic"
-platforms = ["macos"]
+platforms = ["macos", "windows"]
bound = { count = 1, offset-ms = [900, 1100] }
-reason = "NEW: a microphone whose first sample arrives 1.0 s after the recording starts becomes audio zero, so all audio plays early by its start-up delay minus the screen's (screen late against the mic by 962 to 1040 ms on runners, every run). Bound: the mic's 1.0 s delay within 100 ms"
+reason = "NEW: a microphone whose first sample arrives 1.0 s after the recording starts becomes audio zero, so all audio plays early by its start-up delay minus the screen's (screen late against the mic by 962 to 1040 ms on macOS and Windows runners, every run). Bound: the mic's 1.0 s delay within 100 ms"
+link = ""
+
+[[xfail]]
+case = "recording/studio-camera/*"
+class = "timestamps"
+platforms = ["windows"]
+bound = { count = 2 }
+reason = "NEW: Windows camera encoder frame pacing (WindowsCameraMuxer, crates/recording/src/output_pipeline/win.rs): when no frame arrives within one frame interval it re-encodes the last frame stamped one interval later, the slot the late real frame is stamped for, so the real frame lands on the same timestamp and the muxer moves it by one 90 kHz tick (run 39000141528: 16 duplicated frames, pts 0.266667 then 0.266678). Also in cap-v0.6.0"
+link = ""
+
+[[xfail]]
+case = "recording/studio-camera/*"
+class = "sync-camera-mic"
+platforms = ["windows"]
+bound = { count = 1, camera-mic-ms = [-70, 0] }
+reason = "NEW: same Windows camera frame pacing: the paced duplicates shift the camera track's frames against their capture times (camera vs mic -38.3 ms, run 39000141528). Bound: two frame periods early"
+link = ""
+
+[[xfail]]
+case = "recording/studio-camera/*"
+class = "camera-clock"
+platforms = ["windows"]
+bound = { count = 1, camera-mic-ms = [-70, 0] }
+reason = "NEW: same Windows camera frame pacing, past the drift anchor (camera vs mic -38.3 ms, run 39000141528). Bound: two frame periods early"
link = ""
[[xfail]]
diff --git a/crates/verify/tests/recording.rs b/crates/verify/tests/recording.rs
index 9f430425a..a75ff22d8 100644
--- a/crates/verify/tests/recording.rs
+++ b/crates/verify/tests/recording.rs
@@ -2184,12 +2184,7 @@ async fn record(
let crashed = if spec.mode == Mode::StudioCrashed {
let snapshot = scratch.join("crash-snapshot.cap");
let snapshot_started = host_now();
- let status = std::process::Command::new("cp")
- .arg("-cR")
- .arg(&project)
- .arg(&snapshot)
- .status()?;
- anyhow::ensure!(status.success(), "snapshot of the live recording failed");
+ snapshot_live_project(&project, &snapshot)?;
case.note(format!(
"crash snapshot at {:.3}s, cloned in {:.3}s",
host_secs(snapshot_started, reference),
@@ -2301,6 +2296,40 @@ async fn record(
/// pipeline still in flight.
const CRASH_LOSS_BUDGET: f64 = 3.0;
+/// Copies the live project as a crash would leave it on disk: APFS clones on
+/// macOS; a file-by-file copy on Windows, where the recorder's files are open
+/// for shared reading.
+fn snapshot_live_project(project: &Path, snapshot: &Path) -> anyhow::Result<()> {
+ #[cfg(target_os = "macos")]
+ {
+ let status = std::process::Command::new("cp")
+ .arg("-cR")
+ .arg(project)
+ .arg(snapshot)
+ .status()?;
+ anyhow::ensure!(status.success(), "snapshot of the live recording failed");
+ Ok(())
+ }
+ #[cfg(windows)]
+ {
+ fn copy_dir(from: &Path, to: &Path) -> std::io::Result<()> {
+ std::fs::create_dir_all(to)?;
+ for entry in std::fs::read_dir(from)? {
+ let entry = entry?;
+ let target = to.join(entry.file_name());
+ if entry.file_type()?.is_dir() {
+ copy_dir(&entry.path(), &target)?;
+ } else {
+ std::fs::copy(entry.path(), &target)?;
+ }
+ }
+ Ok(())
+ }
+ copy_dir(project, snapshot)
+ .map_err(|e| anyhow::anyhow!("snapshot of the live recording failed: {e}"))
+ }
+}
+
async fn finalize_instant(
recording: &mut instant_recording::CompletedRecording,
) -> anyhow::Result<()> {