Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions d3d12/crates/leonida-gpu/src/shown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use std::sync::atomic::{AtomicU64, Ordering::Relaxed};
use std::sync::{Mutex, OnceLock};

use windows::Win32::Foundation::{CloseHandle, HANDLE};
use windows::Win32::Graphics::Direct3D12::*;
use windows::Win32::Graphics::Dxgi::Common::*;
use windows::Win32::Graphics::Dxgi::*;
Expand Down Expand Up @@ -393,17 +394,30 @@ fn copy_shown(this: *mut core::ffi::c_void, flags: u32) {
}
}

struct EventHandle(HANDLE);

impl Drop for EventHandle {
fn drop(&mut self) {
if !self.0.is_invalid() {
// SAFETY: event handle created by `CreateEventW`; closed once on drop.
let _ = unsafe { CloseHandle(self.0) };
self.0 = HANDLE::default();
}
}
}

/// The thread that waits for each copy, takes its bytes out and frees its slot; the file is
/// written by a thread of its own, as the capture at our present does.
fn saver(jobs: std::sync::mpsc::Receiver<Job>, _device: ID3D12Device) {
// SAFETY: a plain auto-reset event of ours.
let Ok(event) = (unsafe { CreateEventW(None, false, false, None) }) else { return };
let event = EventHandle(event);
while let Ok(job) = jobs.recv() {
let Some(hook) = HOOK.get() else { return };
// SAFETY: our fence and event.
unsafe {
if hook.fence.GetCompletedValue() < job.value && hook.fence.SetEventOnCompletion(job.value, event).is_ok() {
WaitForSingleObject(event, INFINITE);
if hook.fence.GetCompletedValue() < job.value && hook.fence.SetEventOnCompletion(job.value, event.0).is_ok() {
WaitForSingleObject(event.0, INFINITE);
}
}
let taken = {
Expand Down Expand Up @@ -465,3 +479,43 @@ fn note_time(n: u64, ours: u64, us: u128) {
let _ = writeln!(file, "{n} {ours} {us}");
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn event_handle_drop_closes_event_handle() {
const CHILD_ENV: &str = "SHOWN_EVENT_DROP_CHILD";
if std::env::var_os(CHILD_ENV).is_some() {
// SAFETY: creates a plain unnamed auto-reset event owned by this guard.
let raw = unsafe { CreateEventW(None, false, false, None) }.unwrap();
let guard = EventHandle(raw);
let mut flags = 0u32;
// SAFETY: valid handle open in this isolated child process.
let open_before = unsafe { windows_sys::Win32::Foundation::GetHandleInformation(raw.0, &mut flags) };
assert_ne!(open_before, 0, "event handle should be open before drop");
drop(guard);
// SAFETY: isolated `--exact` subprocess; verify Drop closed `raw`.
let open_after = unsafe { windows_sys::Win32::Foundation::GetHandleInformation(raw.0, &mut flags) };
assert_eq!(open_after, 0, "EventHandle::drop should close the event handle");
return;
}
let exe = std::env::current_exe().unwrap();
let out = std::process::Command::new(exe)
.env(CHILD_ENV, "1")
.args([
"--exact",
"shown::tests::event_handle_drop_closes_event_handle",
"--nocapture",
])
.output()
.unwrap();
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(
out.status.success() && stdout.contains("1 passed"),
"shown event handle drop check failed (status={:?}, stdout={stdout})",
out.status
);
}
}
Loading