use windows::Win32::{
Foundation::HANDLE,
Graphics::{
Direct3D12::{
D3D12_CPU_PAGE_PROPERTY_UNKNOWN, D3D12_HEAP_FLAG_NONE, D3D12_HEAP_PROPERTIES, D3D12_HEAP_TYPE_DEFAULT,
D3D12_MEMORY_POOL_UNKNOWN, D3D12_RESOURCE_DESC, D3D12_RESOURCE_DIMENSION_TEXTURE2D, D3D12_RESOURCE_FLAG_NONE,
D3D12_RESOURCE_STATE_COPY_DEST, D3D12_TEXTURE_LAYOUT_UNKNOWN, ID3D12Device, ID3D12Fence, ID3D12Resource,
},
Dxgi::Common::{
DXGI_FORMAT, DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_NV12, DXGI_FORMAT_R8_UNORM, DXGI_FORMAT_R8G8_UNORM,
DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_SAMPLE_DESC,
},
},
};
use crate::{
app::card_read::{Read, Readers},
images::{CardFrame, CardFrames, CardFramesWay, CardLease, CardSurface},
};
pub(super) fn announce(device: &ID3D12Device) {
let card = unsafe { device.GetAdapterLuid() };
crate::images::set_card_frames_adapter(Some((card.LowPart, card.HighPart)));
crate::images::set_card_frames_nv12(true);
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Kind {
Nv12,
Planes,
Rgba(DXGI_FORMAT),
}
pub(super) struct Slot {
pub first: ID3D12Resource,
pub second: Option<ID3D12Resource>,
pub kind: Kind,
pub size: (u32, u32),
}
fn opened(
device: &ID3D12Device,
handle: isize,
formats: &[DXGI_FORMAT],
) -> Option<(ID3D12Resource, DXGI_FORMAT, (u32, u32))> {
let mut resource: Option<ID3D12Resource> = None;
unsafe { device.OpenSharedHandle(HANDLE(handle as _), &mut resource) }.ok()?;
let resource = resource?;
let said = unsafe { resource.GetDesc() };
if !formats.contains(&said.Format) {
return None;
}
Some((resource, said.Format, (u32::try_from(said.Width).ok()?, said.Height)))
}
fn slots(device: &ID3D12Device, frames: &CardFrames) -> Option<Vec<Slot>> {
frames
.surfaces()
.iter()
.map(|surface| {
Some(match *surface {
CardSurface::Nv12(handle) => {
let (first, _, size) = opened(device, handle, &[DXGI_FORMAT_NV12])?;
Slot {
first,
second: None,
kind: Kind::Nv12,
size,
}
}
CardSurface::Planes { gray, color } => {
let (first, _, size) = opened(device, gray, &[DXGI_FORMAT_R8_UNORM])?;
let (second, ..) = opened(device, color, &[DXGI_FORMAT_R8G8_UNORM])?;
Slot {
first,
second: Some(second),
kind: Kind::Planes,
size,
}
}
CardSurface::Rgba(handle) => {
let (first, format, size) = opened(
device,
handle,
&[DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_B8G8R8A8_UNORM],
)?;
Slot {
first,
second: None,
kind: Kind::Rgba(format),
size,
}
}
})
})
.collect()
}
fn fence(device: &ID3D12Device, handle: isize) -> Option<ID3D12Fence> {
let mut fence: Option<ID3D12Fence> = None;
unsafe { device.OpenSharedHandle(HANDLE(handle as _), &mut fence) }.ok()?;
fence
}
fn own_texture(device: &ID3D12Device, format: DXGI_FORMAT, wide: u32, high: u32) -> Option<ID3D12Resource> {
let heap = D3D12_HEAP_PROPERTIES {
Type: D3D12_HEAP_TYPE_DEFAULT,
CPUPageProperty: D3D12_CPU_PAGE_PROPERTY_UNKNOWN,
MemoryPoolPreference: D3D12_MEMORY_POOL_UNKNOWN,
CreationNodeMask: 1,
VisibleNodeMask: 1,
};
let said = D3D12_RESOURCE_DESC {
Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D,
Alignment: 0,
Width: u64::from(wide),
Height: high,
DepthOrArraySize: 1,
MipLevels: 1,
Format: format,
SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
Layout: D3D12_TEXTURE_LAYOUT_UNKNOWN,
Flags: D3D12_RESOURCE_FLAG_NONE,
};
let mut texture: Option<ID3D12Resource> = None;
unsafe {
device.CreateCommittedResource(
&heap,
D3D12_HEAP_FLAG_NONE,
&said,
D3D12_RESOURCE_STATE_COPY_DEST,
None,
&mut texture,
)
}
.ok()?;
texture
}
fn bytes_of(format: DXGI_FORMAT) -> usize {
match format {
DXGI_FORMAT_R8_UNORM => 1,
DXGI_FORMAT_R8G8_UNORM => 2,
_ => 4,
}
}
pub(super) struct Part {
pub texture: ID3D12Resource,
pub format: DXGI_FORMAT,
pub wide: u32,
pub high: u32,
pub bytes: Vec<u8>,
}
pub(super) struct Arrived {
pub parts: Vec<Part>,
pub written: bool,
}
struct Copied {
read: Read,
slot: Slot,
written: bool,
kept: Vec<Vec<u8>>,
}
impl Copied {
fn open(device: &ID3D12Device, frames: &CardFrames, readers: &mut Readers) -> Option<Self> {
let read = Read::open(frames, readers)?;
let parts = read.parts();
let &(format, wide, high) = parts.first()?;
let first = own_texture(device, format, wide, high)?;
let second = match parts.get(1) {
Some(&(format, wide, high)) => Some(own_texture(device, format, wide, high)?),
None => None,
};
let kind = if second.is_some() {
Kind::Planes
} else {
Kind::Rgba(format)
};
Some(Self {
read,
slot: Slot {
first,
second,
kind,
size: (wide, high),
},
written: false,
kept: Vec::new(),
})
}
fn arrived(&mut self) -> Option<((u64, CardFrame), Arrived)> {
let said: Vec<(DXGI_FORMAT, u32, u32)> = self.read.parts().to_vec();
let mut kept = std::mem::take(&mut self.kept);
kept.resize_with(said.len(), Vec::new);
let frame = self.read.arrived(|part, rows, pitch| {
let (Some(&(format, wide, high)), Some(bytes)) = (said.get(part), kept.get_mut(part)) else {
return;
};
let row = wide as usize * bytes_of(format);
bytes.clear();
bytes.reserve(row * high as usize);
for line in 0..high as usize {
bytes.extend_from_slice(unsafe { std::slice::from_raw_parts(rows.add(line * pitch as usize), row) });
}
});
let Some(frame) = frame else {
self.kept = kept;
return None;
};
let textures = [Some(self.slot.first.clone()), self.slot.second.clone()];
let parts = said
.into_iter()
.zip(kept)
.zip(textures.into_iter().flatten())
.map(|(((format, wide, high), bytes), texture)| Part {
texture,
format,
wide,
high,
bytes,
})
.collect();
Some((
frame,
Arrived {
parts,
written: self.written,
},
))
}
}
enum Way {
Card {
slots: Vec<Slot>,
fence: Option<ID3D12Fence>,
},
Memory(Copied),
Nowhere,
}
#[derive(Default)]
pub(super) struct Picked {
pub lease: Option<CardLease>,
pub arrived: Option<Arrived>,
}
pub(super) struct Opened {
way: Way,
frames: CardFrames,
drawn: Option<(u64, CardFrame)>,
width: u32,
height: u32,
}
impl Opened {
pub(super) fn open(
device: &ID3D12Device,
frames: &CardFrames,
(width, height): (u32, u32),
readers: &mut Readers,
) -> Self {
let on_card = slots(device, frames).and_then(|slots| {
let fence = match frames.fence() {
Some(handle) => Some(fence(device, handle)?),
None => None,
};
Some(Way::Card { slots, fence })
});
let way = on_card
.or_else(|| Copied::open(device, frames, readers).map(Way::Memory))
.unwrap_or(Way::Nowhere);
frames.found(match way {
Way::Card { .. } => CardFramesWay::Card,
Way::Memory(_) => CardFramesWay::Memory,
Way::Nowhere => CardFramesWay::Nowhere,
});
Self {
way,
frames: frames.clone(),
drawn: None,
width,
height,
}
}
pub(super) fn is_of(&self, frames: &CardFrames, (width, height): (u32, u32)) -> bool {
self.frames.same(frames) && (self.width, self.height) == (width, height)
}
pub(super) fn pick(&mut self) -> Picked {
let mut picked = Picked::default();
if let Way::Memory(copied) = &mut self.way {
if let Some(((count, mut frame), arrived)) = copied.arrived() {
drop(frame.lease.take());
self.drawn = Some((count, frame));
picked.arrived = Some(arrived);
}
if copied.read.is_copying() {
return picked;
}
}
let Some((count, frame)) = self.frames.latest() else {
return picked;
};
if self.drawn.as_ref().is_some_and(|(drawn, _)| *drawn == count) || frame.surface >= self.frames.surfaces().len() {
return picked;
}
let reached = match &self.way {
Way::Card { fence, .. } => fence.as_ref().map(|fence| unsafe { fence.GetCompletedValue() }),
Way::Memory(copied) => copied.read.reached(),
Way::Nowhere => return picked,
};
let written = match (frame.ready, reached) {
(Some(value), Some(reached)) => reached >= value,
_ => true,
};
if !written {
return picked;
}
match &mut self.way {
Way::Card { .. } => {
picked.lease = self.drawn.replace((count, frame)).and_then(|(_, before)| before.lease);
}
Way::Memory(copied) => copied.read.copy(count, frame),
Way::Nowhere => {}
}
picked
}
pub(super) fn sent(&mut self, arrived: Arrived, written: bool) {
if let Way::Memory(copied) = &mut self.way {
copied.written |= written;
copied.kept = arrived.parts.into_iter().map(|part| part.bytes).collect();
}
}
pub(super) fn is_shared(&self) -> bool {
matches!(self.way, Way::Card { .. })
}
pub(super) fn drawn(&self) -> Option<&Slot> {
let (_, frame) = self.drawn.as_ref()?;
match &self.way {
Way::Card { slots, .. } => slots.get(frame.surface),
Way::Memory(copied) => copied.written.then_some(&copied.slot),
Way::Nowhere => None,
}
}
pub(super) fn part(&self, min: [f32; 2], max: [f32; 2]) -> ([f32; 2], [f32; 2]) {
let Some(((_, frame), slot)) = self.drawn.as_ref().zip(self.drawn()) else {
return (min, max);
};
crate::images::part_of(
frame.crop,
(self.width, self.height),
slot.size,
self.frames.is_rgba(),
min,
max,
)
}
pub(super) fn colors(&self) -> crate::images::YuvColors {
self
.drawn
.as_ref()
.map_or(crate::images::YuvColors::BT709, |(_, frame)| frame.colors)
}
pub(super) fn closed(mut self) -> Option<CardLease> {
self.drawn.take().and_then(|(_, frame)| frame.lease)
}
}
#[cfg(test)]
mod tests {
use std::sync::{
Arc,
atomic::{AtomicBool, Ordering},
};
use windows::{
Win32::Graphics::{
Direct3D::D3D_FEATURE_LEVEL_11_0,
Direct3D12::{
D3D12_FENCE_FLAG_SHARED, D3D12_HEAP_FLAG_SHARED, D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS,
D3D12_RESOURCE_STATE_COMMON, D3D12CreateDevice, D3D12GetDebugInterface, ID3D12Debug,
},
},
core::PCWSTR,
};
use super::*;
use crate::images::CardCrop;
pub(super) fn device() -> ID3D12Device {
static DEBUG_LAYER: std::sync::Once = std::sync::Once::new();
DEBUG_LAYER.call_once(|| {
let mut debug: Option<ID3D12Debug> = None;
unsafe {
if D3D12GetDebugInterface(&mut debug).is_ok()
&& let Some(debug) = debug
{
debug.EnableDebugLayer();
}
}
});
let mut device: Option<ID3D12Device> = None;
unsafe { D3D12CreateDevice(None, D3D_FEATURE_LEVEL_11_0, &mut device) }.expect("a Direct3D 12 device");
device.expect("a Direct3D 12 device")
}
fn shared(device: &ID3D12Device, wide: u32, high: u32, format: DXGI_FORMAT) -> isize {
const GENERIC_ALL: u32 = 0x1000_0000;
let heap = D3D12_HEAP_PROPERTIES {
Type: D3D12_HEAP_TYPE_DEFAULT,
CPUPageProperty: D3D12_CPU_PAGE_PROPERTY_UNKNOWN,
MemoryPoolPreference: D3D12_MEMORY_POOL_UNKNOWN,
CreationNodeMask: 1,
VisibleNodeMask: 1,
};
let said = D3D12_RESOURCE_DESC {
Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D,
Alignment: 0,
Width: u64::from(wide),
Height: high,
DepthOrArraySize: 1,
MipLevels: 1,
Format: format,
SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
Layout: D3D12_TEXTURE_LAYOUT_UNKNOWN,
Flags: D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS,
};
let mut texture: Option<ID3D12Resource> = None;
unsafe {
device
.CreateCommittedResource(
&heap,
D3D12_HEAP_FLAG_SHARED,
&said,
D3D12_RESOURCE_STATE_COMMON,
None,
&mut texture,
)
.expect("a texture");
let texture = texture.expect("a texture");
device
.CreateSharedHandle(&texture, None, GENERIC_ALL, PCWSTR::null())
.expect("a handle of the texture")
.0 as isize
}
}
fn shared_fence(device: &ID3D12Device) -> (ID3D12Fence, isize) {
const GENERIC_ALL: u32 = 0x1000_0000;
unsafe {
let fence: ID3D12Fence = device.CreateFence(0, D3D12_FENCE_FLAG_SHARED).expect("a fence");
let handle = device
.CreateSharedHandle(&fence, None, GENERIC_ALL, PCWSTR::null())
.expect("a handle of the fence");
(fence, handle.0 as isize)
}
}
struct Lease(Arc<AtomicBool>);
impl Drop for Lease {
fn drop(&mut self) {
self.0.store(true, Ordering::Release);
}
}
fn lease() -> (Lease, Arc<AtomicBool>) {
let gone = Arc::new(AtomicBool::new(false));
(Lease(gone.clone()), gone)
}
fn nothing(picked: Picked) -> bool {
picked.lease.is_none() && picked.arrived.is_none()
}
fn near(part: ([f32; 2], [f32; 2]), other: ([f32; 2], [f32; 2])) -> bool {
let close = |a: [f32; 2], b: [f32; 2]| (a[0] - b[0]).abs() < 1e-5 && (a[1] - b[1]).abs() < 1e-5;
close(part.0, other.0) && close(part.1, other.1)
}
#[test]
fn the_frame_drawn_is_the_newest_one_written_and_the_lease_of_the_one_before_is_given() {
let _alone = crate::app::card_read::ON_THE_CARD.lock();
let device = device();
let planes = || CardSurface::Planes {
gray: shared(&device, 64, 48, DXGI_FORMAT_R8_UNORM),
color: shared(&device, 32, 24, DXGI_FORMAT_R8G8_UNORM),
};
let (fence, fence_handle) = shared_fence(&device);
let frames = unsafe { CardFrames::with_fence(vec![planes(), planes()], fence_handle) };
assert_eq!(frames.way(), None);
let mut opened = Opened::open(&device, &frames, (64, 48), &mut Readers::default());
assert_eq!(frames.way(), Some(CardFramesWay::Card));
assert!(opened.is_of(&frames, (64, 48)) && !opened.is_of(&frames, (64, 40)));
assert!(nothing(opened.pick()) && opened.drawn().is_none());
let (first, first_gone) = lease();
frames.show(CardFrame::of(0).ready_at(1).lease(first));
assert!(nothing(opened.pick()) && opened.drawn().is_none());
unsafe { fence.Signal(1) }.expect("the fence is moved");
assert!(nothing(opened.pick()), "no frame was drawn before the first");
let slot = opened.drawn().expect("the first frame is drawn");
assert_eq!(
(slot.kind, slot.size, slot.second.is_some()),
(Kind::Planes, (64, 48), true)
);
let (second, second_gone) = lease();
frames.show(CardFrame::of(1).ready_at(2).lease(second));
assert!(nothing(opened.pick()));
assert!(!first_gone.load(Ordering::Acquire));
unsafe { fence.Signal(2) }.expect("the fence is moved");
let before = opened
.pick()
.lease
.expect("the lease of the frame that is drawn no more");
assert!(
!first_gone.load(Ordering::Acquire),
"the card may still draw the first frame"
);
drop(before);
assert!(first_gone.load(Ordering::Acquire));
assert!(nothing(opened.pick()));
let last = opened.closed().expect("the lease of the last frame");
assert!(!second_gone.load(Ordering::Acquire));
drop(last);
drop(frames);
assert!(second_gone.load(Ordering::Acquire));
}
#[test]
fn frames_that_are_not_what_they_say_or_that_the_device_does_not_open_are_drawn_nowhere() {
let _alone = crate::app::card_read::ON_THE_CARD.lock();
let device = device();
let frames = unsafe { CardFrames::new(vec![CardSurface::Rgba(shared(&device, 64, 48, DXGI_FORMAT_R8_UNORM))]) };
let mut opened = Opened::open(&device, &frames, (64, 48), &mut Readers::default());
assert_eq!(frames.way(), Some(CardFramesWay::Nowhere));
frames.show(CardFrame::of(0));
assert!(nothing(opened.pick()) && opened.drawn().is_none());
assert_eq!(opened.part([0.0, 0.0], [1.0, 1.0]), ([0.0, 0.0], [1.0, 1.0]));
}
#[test]
fn a_texture_of_red_green_and_blue_is_drawn_as_it_is_and_the_picture_is_the_part_it_says() {
let _alone = crate::app::card_read::ON_THE_CARD.lock();
let device = device();
let frames = unsafe {
CardFrames::new(vec![CardSurface::Rgba(shared(
&device,
64,
48,
DXGI_FORMAT_B8G8R8A8_UNORM,
))])
};
let mut opened = Opened::open(&device, &frames, (60, 40), &mut Readers::default());
assert_eq!(frames.way(), Some(CardFramesWay::Card));
frames.show(CardFrame::of(0));
assert!(nothing(opened.pick()));
assert!(opened.is_shared());
let slot = opened.drawn().expect("the frame is drawn");
assert_eq!(
(slot.kind, slot.size, slot.second.is_some()),
(Kind::Rgba(DXGI_FORMAT_B8G8R8A8_UNORM), (64, 48), false)
);
assert!(near(
opened.part([0.0, 0.0], [1.0, 1.0]),
([0.0, 0.0], [59.5 / 64.0, 39.5 / 48.0])
));
frames.show(CardFrame::of(0).crop(CardCrop {
left: 4,
top: 8,
width: 32,
height: 16,
}));
assert!(nothing(opened.pick()));
assert!(near(
opened.part([0.0, 0.0], [1.0, 1.0]),
([4.5 / 64.0, 8.5 / 48.0], [35.5 / 64.0, 23.5 / 48.0])
));
assert!(near(
opened.part([0.5, 0.0], [1.0, 0.5]),
([20.0 / 64.0, 8.5 / 48.0], [35.5 / 64.0, 16.0 / 48.0])
));
}
}
#[cfg(test)]
mod other_card_tests {
use std::time::{Duration, Instant};
use windows::{
Win32::{
Foundation::HMODULE,
Graphics::{
Direct3D::{D3D_DRIVER_TYPE_WARP, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_11_1},
Direct3D11::{
D3D11_BIND_SHADER_RESOURCE, D3D11_CREATE_DEVICE_FLAG, D3D11_RESOURCE_MISC_SHARED,
D3D11_RESOURCE_MISC_SHARED_NTHANDLE, D3D11_SDK_VERSION, D3D11_SUBRESOURCE_DATA, D3D11_TEXTURE2D_DESC,
D3D11_USAGE_DEFAULT, D3D11CreateDevice, ID3D11Device, ID3D11DeviceContext, ID3D11Texture2D,
},
Dxgi::{DXGI_SHARED_RESOURCE_READ, IDXGIResource1},
},
},
core::{Interface, PCWSTR},
};
use super::*;
fn written(
device: &ID3D11Device,
format: DXGI_FORMAT,
wide: u32,
high: u32,
bytes: &[u8],
) -> (isize, ID3D11Texture2D) {
let said = D3D11_TEXTURE2D_DESC {
Width: wide,
Height: high,
MipLevels: 1,
ArraySize: 1,
Format: format,
SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
Usage: D3D11_USAGE_DEFAULT,
BindFlags: D3D11_BIND_SHADER_RESOURCE.0 as u32,
CPUAccessFlags: 0,
MiscFlags: (D3D11_RESOURCE_MISC_SHARED_NTHANDLE.0 | D3D11_RESOURCE_MISC_SHARED.0) as u32,
};
let holds = D3D11_SUBRESOURCE_DATA {
pSysMem: bytes.as_ptr().cast(),
SysMemPitch: (bytes.len() / high as usize) as u32,
SysMemSlicePitch: 0,
};
let mut texture: Option<ID3D11Texture2D> = None;
unsafe {
device
.CreateTexture2D(&said, Some(&holds), Some(&mut texture))
.expect("a texture of the software card");
let texture = texture.expect("a texture of the software card");
let handle = texture
.cast::<IDXGIResource1>()
.expect("a texture is shared")
.CreateSharedHandle(None, DXGI_SHARED_RESOURCE_READ.0, PCWSTR::null())
.expect("a handle of the texture");
(handle.0 as isize, texture)
}
}
#[test]
fn the_frames_of_a_decoder_on_another_card_come_through_memory_as_they_were_written() {
let _alone = crate::app::card_read::ON_THE_CARD.lock();
let device = super::tests::device();
let (mut decoder, mut context): (Option<ID3D11Device>, Option<ID3D11DeviceContext>) = (None, None);
unsafe {
D3D11CreateDevice(
None,
D3D_DRIVER_TYPE_WARP,
HMODULE::default(),
D3D11_CREATE_DEVICE_FLAG(0),
Some(&[D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0]),
D3D11_SDK_VERSION,
Some(&mut decoder),
None,
Some(&mut context),
)
}
.expect("a device of the software card");
let (decoder, context) = (decoder.expect("the device"), context.expect("its context"));
const WIDE: u32 = 64;
const HIGH: u32 = 48;
let gray: Vec<u8> = (0..WIDE * HIGH).map(|at| (at % 251) as u8).collect();
let color: Vec<u8> = (0..WIDE * HIGH / 2).map(|at| (at % 241) as u8).collect();
let (gray_handle, _gray) = written(&decoder, DXGI_FORMAT_R8_UNORM, WIDE, HIGH, &gray);
let (color_handle, _color) = written(&decoder, DXGI_FORMAT_R8G8_UNORM, WIDE / 2, HIGH / 2, &color);
unsafe { context.Flush() };
let frames = unsafe {
CardFrames::new(vec![CardSurface::Planes {
gray: gray_handle,
color: color_handle,
}])
};
let mut readers = Readers::default();
let mut opened = Opened::open(&device, &frames, (WIDE, HIGH), &mut readers);
if frames.way() == Some(CardFramesWay::Card) {
eprintln!("the renderer's card opens the software card's textures: nothing comes through memory here");
return;
}
assert_eq!(frames.way(), Some(CardFramesWay::Memory));
assert!(!opened.is_shared());
frames.show(CardFrame::of(0));
let started = Instant::now();
let arrived = loop {
let picked = opened.pick();
assert!(picked.lease.is_none());
if let Some(arrived) = picked.arrived {
break arrived;
}
assert!(opened.drawn().is_none());
assert!(
started.elapsed() < Duration::from_secs(5),
"the frame never comes through memory"
);
std::thread::sleep(Duration::from_millis(2));
};
assert!(!arrived.written, "the renderer's textures hold no frame yet");
assert_eq!(arrived.parts.len(), 2);
let (first, second) = (&arrived.parts[0], &arrived.parts[1]);
assert_eq!(
(first.format, first.wide, first.high),
(DXGI_FORMAT_R8_UNORM, WIDE, HIGH)
);
assert_eq!(
(second.format, second.wide, second.high),
(DXGI_FORMAT_R8G8_UNORM, WIDE / 2, HIGH / 2)
);
assert!(first.bytes == gray, "the gray is the gray that was written");
assert!(second.bytes == color, "the color is the color that was written");
assert!(
opened.drawn().is_none(),
"nothing is drawn of textures nothing was sent to"
);
opened.sent(arrived, true);
let slot = opened.drawn().expect("the frame is drawn");
assert_eq!(
(slot.kind, slot.size, slot.second.is_some()),
(Kind::Planes, (WIDE, HIGH), true)
);
frames.show(CardFrame::of(0));
let started = Instant::now();
let arrived = loop {
if let Some(arrived) = opened.pick().arrived {
break arrived;
}
assert!(
started.elapsed() < Duration::from_secs(5),
"the second frame never comes through memory"
);
std::thread::sleep(Duration::from_millis(2));
};
assert!(arrived.written);
assert!(arrived.parts[0].bytes == gray && arrived.parts[1].bytes == color);
}
}