#[derive(Clone, Copy, Debug)]
pub enum PlatformWindowHandle {
Win32(*mut core::ffi::c_void),
X11(u64),
Wayland(*mut core::ffi::c_void),
Other(*mut core::ffi::c_void),
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct GfxInstance {
pub pos: [f32; 2],
pub size: [f32; 2],
pub uv_rect: [f32; 4],
pub color: [f32; 4],
pub color_end: [f32; 4],
pub gradient: [f32; 4],
pub params: [f32; 4],
}
const _: () = assert!(core::mem::size_of::<GfxInstance>() == 96);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct AtlasUpdate {
pub x: u32,
pub y: u32,
pub w: u32,
pub h: u32,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Frame {
pub instances: Vec<GfxInstance>,
pub atlas_updates: Vec<AtlasUpdate>,
pub atlas_reset: bool,
}
pub trait RenderBackend {
fn create(handle: PlatformWindowHandle, width: u32, height: u32) -> Result<Self, String>
where
Self: Sized;
fn rebuild(&mut self, width: u32, height: u32) -> Result<(), String>;
fn resize(&mut self, width: u32, height: u32) -> Result<(), String>;
fn present(&mut self, frame: &Frame) -> Result<(), String>;
fn is_occluded(&self) -> bool;
fn instance_count(&self) -> usize;
}
#[derive(Clone, Debug, PartialEq)]
pub enum BackendCall {
Create {
width: u32,
height: u32,
},
Rebuild {
width: u32,
height: u32,
},
Resize {
width: u32,
height: u32,
},
Present {
instances: usize,
atlas_updates: usize,
atlas_reset: bool,
},
}
#[derive(Debug, Default)]
pub struct RecordingBackend {
pub calls: Vec<BackendCall>,
pub last_frame: Option<Frame>,
pub occluded: bool,
pub fail_present: bool,
}
impl RecordingBackend {
pub fn new() -> Self {
Self::default()
}
pub fn presents(&self) -> impl Iterator<Item = &BackendCall> {
self.calls
.iter()
.filter(|c| matches!(c, BackendCall::Present { .. }))
}
}
impl RenderBackend for RecordingBackend {
fn create(_handle: PlatformWindowHandle, width: u32, height: u32) -> Result<Self, String> {
let mut backend = Self::new();
backend.calls.push(BackendCall::Create { width, height });
Ok(backend)
}
fn rebuild(&mut self, width: u32, height: u32) -> Result<(), String> {
self.calls.push(BackendCall::Rebuild { width, height });
Ok(())
}
fn resize(&mut self, width: u32, height: u32) -> Result<(), String> {
self.calls.push(BackendCall::Resize { width, height });
Ok(())
}
fn present(&mut self, frame: &Frame) -> Result<(), String> {
self.calls.push(BackendCall::Present {
instances: frame.instances.len(),
atlas_updates: frame.atlas_updates.len(),
atlas_reset: frame.atlas_reset,
});
self.last_frame = Some(frame.clone());
if self.fail_present {
return Err("mock: device lost".to_string());
}
Ok(())
}
fn is_occluded(&self) -> bool {
self.occluded
}
fn instance_count(&self) -> usize {
self.last_frame
.as_ref()
.map(|f| f.instances.len())
.unwrap_or(0)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn inst(x: f32) -> GfxInstance {
GfxInstance {
pos: [x, 0.0],
size: [8.0, 16.0],
uv_rect: [0.0, 0.0, 0.1, 0.1],
color: [1.0, 1.0, 1.0, 1.0],
color_end: [1.0, 1.0, 1.0, 1.0],
gradient: [0.0, 0.0, 0.0, 0.0],
params: [1.0, 1.0, 1.0, 1.0],
}
}
#[test]
fn recording_backend_records_calls_in_order() {
let mut b = RecordingBackend::create(PlatformWindowHandle::X11(7), 800, 600).unwrap();
b.resize(1024, 768).unwrap();
let frame = Frame {
instances: vec![inst(0.0), inst(8.0)],
atlas_updates: vec![AtlasUpdate {
x: 0,
y: 0,
w: 4,
h: 4,
}],
atlas_reset: false,
};
b.present(&frame).unwrap();
assert_eq!(
b.calls,
vec![
BackendCall::Create {
width: 800,
height: 600
},
BackendCall::Resize {
width: 1024,
height: 768
},
BackendCall::Present {
instances: 2,
atlas_updates: 1,
atlas_reset: false
},
],
"调用顺序与内容必须被完整记录"
);
assert_eq!(b.instance_count(), 2);
assert_eq!(b.last_frame.as_ref().unwrap().instances[1].pos[0], 8.0);
}
#[test]
fn recording_backend_can_simulate_device_loss() {
let mut b =
RecordingBackend::create(PlatformWindowHandle::Win32(std::ptr::null_mut()), 1, 1)
.unwrap();
b.fail_present = true;
let err = b.present(&Frame::default()).unwrap_err();
assert!(err.contains("device lost"), "错误应表明设备丢失:{err}");
b.fail_present = false;
b.rebuild(1, 1).unwrap();
assert!(b.calls.contains(&BackendCall::Rebuild {
width: 1,
height: 1
}));
}
#[test]
fn empty_frame_still_reports_present() {
let mut b =
RecordingBackend::create(PlatformWindowHandle::Wayland(std::ptr::null_mut()), 10, 10)
.unwrap();
b.present(&Frame::default()).unwrap();
assert_eq!(b.presents().count(), 1, "空帧也必须提交(否则窗口不刷新)");
assert_eq!(b.instance_count(), 0);
}
}