1#[derive(Clone, Copy, Debug)]
22pub enum PlatformWindowHandle {
23 Win32(*mut core::ffi::c_void),
25 X11(u64),
27 Wayland(*mut core::ffi::c_void),
29 Other(*mut core::ffi::c_void),
32}
33
34#[derive(Clone, Copy, Debug, PartialEq)]
40pub struct GfxInstance {
41 pub pos: [f32; 2],
43 pub size: [f32; 2],
45 pub uv_rect: [f32; 4],
47 pub color: [f32; 4],
49 pub color_end: [f32; 4],
51 pub gradient: [f32; 4],
53 pub params: [f32; 4],
55}
56
57const _: () = assert!(core::mem::size_of::<GfxInstance>() == 96);
62
63#[derive(Clone, Copy, Debug, PartialEq, Eq)]
65pub struct AtlasUpdate {
66 pub x: u32,
68 pub y: u32,
70 pub w: u32,
72 pub h: u32,
74}
75
76#[derive(Clone, Debug, Default, PartialEq)]
78pub struct Frame {
79 pub instances: Vec<GfxInstance>,
81 pub atlas_updates: Vec<AtlasUpdate>,
83 pub atlas_reset: bool,
85}
86
87pub trait RenderBackend {
92 fn create(handle: PlatformWindowHandle, width: u32, height: u32) -> Result<Self, String>
96 where
97 Self: Sized;
98
99 fn rebuild(&mut self, width: u32, height: u32) -> Result<(), String>;
101
102 fn resize(&mut self, width: u32, height: u32) -> Result<(), String>;
104
105 fn present(&mut self, frame: &Frame) -> Result<(), String>;
109
110 fn is_occluded(&self) -> bool;
113
114 fn instance_count(&self) -> usize;
116}
117
118#[derive(Clone, Debug, PartialEq)]
122pub enum BackendCall {
123 Create {
125 width: u32,
127 height: u32,
129 },
130 Rebuild {
132 width: u32,
134 height: u32,
136 },
137 Resize {
139 width: u32,
141 height: u32,
143 },
144 Present {
146 instances: usize,
148 atlas_updates: usize,
150 atlas_reset: bool,
152 },
153}
154
155#[derive(Debug, Default)]
160pub struct RecordingBackend {
161 pub calls: Vec<BackendCall>,
163 pub last_frame: Option<Frame>,
165 pub occluded: bool,
167 pub fail_present: bool,
169}
170
171impl RecordingBackend {
172 pub fn new() -> Self {
178 Self::default()
179 }
180
181 pub fn presents(&self) -> impl Iterator<Item = &BackendCall> {
183 self.calls
184 .iter()
185 .filter(|c| matches!(c, BackendCall::Present { .. }))
186 }
187}
188
189impl RenderBackend for RecordingBackend {
190 fn create(_handle: PlatformWindowHandle, width: u32, height: u32) -> Result<Self, String> {
191 let mut backend = Self::new();
192 backend.calls.push(BackendCall::Create { width, height });
193 Ok(backend)
194 }
195
196 fn rebuild(&mut self, width: u32, height: u32) -> Result<(), String> {
197 self.calls.push(BackendCall::Rebuild { width, height });
198 Ok(())
199 }
200
201 fn resize(&mut self, width: u32, height: u32) -> Result<(), String> {
202 self.calls.push(BackendCall::Resize { width, height });
203 Ok(())
204 }
205
206 fn present(&mut self, frame: &Frame) -> Result<(), String> {
207 self.calls.push(BackendCall::Present {
208 instances: frame.instances.len(),
209 atlas_updates: frame.atlas_updates.len(),
210 atlas_reset: frame.atlas_reset,
211 });
212 self.last_frame = Some(frame.clone());
213 if self.fail_present {
214 return Err("mock: device lost".to_string());
215 }
216 Ok(())
217 }
218
219 fn is_occluded(&self) -> bool {
220 self.occluded
221 }
222
223 fn instance_count(&self) -> usize {
224 self.last_frame
225 .as_ref()
226 .map(|f| f.instances.len())
227 .unwrap_or(0)
228 }
229}
230
231#[cfg(test)]
232mod tests {
233 use super::*;
234
235 fn inst(x: f32) -> GfxInstance {
236 GfxInstance {
237 pos: [x, 0.0],
238 size: [8.0, 16.0],
239 uv_rect: [0.0, 0.0, 0.1, 0.1],
240 color: [1.0, 1.0, 1.0, 1.0],
241 color_end: [1.0, 1.0, 1.0, 1.0],
242 gradient: [0.0, 0.0, 0.0, 0.0],
243 params: [1.0, 1.0, 1.0, 1.0],
244 }
245 }
246
247 #[test]
248 fn recording_backend_records_calls_in_order() {
249 let mut b = RecordingBackend::create(PlatformWindowHandle::X11(7), 800, 600).unwrap();
250 b.resize(1024, 768).unwrap();
251 let frame = Frame {
252 instances: vec![inst(0.0), inst(8.0)],
253 atlas_updates: vec![AtlasUpdate {
254 x: 0,
255 y: 0,
256 w: 4,
257 h: 4,
258 }],
259 atlas_reset: false,
260 };
261 b.present(&frame).unwrap();
262
263 assert_eq!(
264 b.calls,
265 vec![
266 BackendCall::Create {
267 width: 800,
268 height: 600
269 },
270 BackendCall::Resize {
271 width: 1024,
272 height: 768
273 },
274 BackendCall::Present {
275 instances: 2,
276 atlas_updates: 1,
277 atlas_reset: false
278 },
279 ],
280 "调用顺序与内容必须被完整记录"
281 );
282 assert_eq!(b.instance_count(), 2);
283 assert_eq!(b.last_frame.as_ref().unwrap().instances[1].pos[0], 8.0);
284 }
285
286 #[test]
287 fn recording_backend_can_simulate_device_loss() {
288 let mut b =
289 RecordingBackend::create(PlatformWindowHandle::Win32(std::ptr::null_mut()), 1, 1)
290 .unwrap();
291 b.fail_present = true;
292 let err = b.present(&Frame::default()).unwrap_err();
293 assert!(err.contains("device lost"), "错误应表明设备丢失:{err}");
294 b.fail_present = false;
296 b.rebuild(1, 1).unwrap();
297 assert!(b.calls.contains(&BackendCall::Rebuild {
298 width: 1,
299 height: 1
300 }));
301 }
302
303 #[test]
304 fn empty_frame_still_reports_present() {
305 let mut b =
306 RecordingBackend::create(PlatformWindowHandle::Wayland(std::ptr::null_mut()), 10, 10)
307 .unwrap();
308 b.present(&Frame::default()).unwrap();
309 assert_eq!(b.presents().count(), 1, "空帧也必须提交(否则窗口不刷新)");
310 assert_eq!(b.instance_count(), 0);
311 }
312}