velox_engine/platform/
mod.rs1use core::future::Future;
11use core::time::Duration;
12
13use crate::engine::VeloxEngine;
14
15pub mod clipboard;
16pub mod mobile;
17pub mod testing;
18pub mod web;
19
20pub use clipboard::{clipboard_has_text, clipboard_read_into, clipboard_write_text};
21pub use clipboard::clipboard_read_text;
22
23pub mod a11y_bridge;
24pub mod android;
25pub mod embed_ffi;
26pub mod harmony;
27pub mod ios;
28pub mod os_integration;
29pub mod shm_driver;
30pub mod wayland;
31pub mod winit;
32
33
34pub use crate::renderers::RepaintBufferType;
35
36#[derive(Copy, Clone, PartialEq, Eq, Debug)]
41pub enum DesktopRenderBackend {
42 Wgpu,
44 Software,
46}
47
48impl DesktopRenderBackend {
49 #[inline(always)]
53 pub const fn default_desktop() -> Self {
54 #[cfg(feature = "gpu")]
55 {
56 Self::Wgpu
57 }
58 #[cfg(not(feature = "gpu"))]
59 {
60 Self::Software
61 }
62 }
63
64 #[inline(always)]
66 pub const fn is_gpu(self) -> bool {
67 matches!(self, Self::Wgpu)
68 }
69}
70
71impl Default for DesktopRenderBackend {
72 #[inline(always)]
73 fn default() -> Self {
74 Self::default_desktop()
75 }
76}
77
78#[derive(Copy, Clone, PartialEq, Eq, Debug)]
80pub enum PlatformControlFlow {
81 Poll,
83 Wait,
85 Exit,
87}
88
89#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
91pub struct FlushReport {
92 pub dirty_regions: usize,
94 pub strips_or_buffers: usize,
96 pub pixels_updated: usize,
98 pub arena_peak_bytes: usize,
100 pub arena_truncations: u32,
102}
103
104impl FlushReport {
105 #[inline(always)]
107 pub const fn const_empty() -> Self {
108 Self {
109 dirty_regions: 0,
110 strips_or_buffers: 0,
111 pixels_updated: 0,
112 arena_peak_bytes: 0,
113 arena_truncations: 0,
114 }
115 }
116
117 #[inline(always)]
119 pub fn with_engine(
120 dirty_regions: usize,
121 strips_or_buffers: usize,
122 pixels_updated: usize,
123 engine: &VeloxEngine,
124 ) -> Self {
125 Self {
126 dirty_regions,
127 strips_or_buffers,
128 pixels_updated,
129 arena_peak_bytes: engine.last_arena_peak_bytes(),
130 arena_truncations: engine.last_arena_truncations(),
131 }
132 }
133
134 #[inline(always)]
136 pub const fn has_updates(&self) -> bool {
137 self.dirty_regions > 0
138 }
139}
140
141pub trait PlatformSurface {
146 fn surface_size(&self) -> (i16, i16);
148
149 fn flush_frame_dt(&mut self, engine: &mut VeloxEngine, dt_ms: u16) -> FlushReport;
151
152 #[inline]
154 fn flush_frame(&mut self, engine: &mut VeloxEngine) -> FlushReport {
155 self.flush_frame_dt(engine, 16)
156 }
157}
158
159pub trait Platform {
163 fn duration_since_start(&self) -> Duration {
165 Duration::ZERO
166 }
167
168 fn step_platform(&mut self, engine: &mut VeloxEngine, dt_ms: u16) -> PlatformControlFlow;
170
171 fn run_event_loop(mut self, mut engine: VeloxEngine)
173 where
174 Self: Sized,
175 {
176 let mut last_tick = std::time::Instant::now();
177 loop {
178 let dt_ms = {
179 let now = std::time::Instant::now();
180 let elapsed = now.duration_since(last_tick).as_millis();
181 last_tick = now;
182 elapsed.clamp(1, 100) as u16
183 };
184
185 match self.step_platform(&mut engine, dt_ms) {
186 PlatformControlFlow::Poll => {
187 {
188 let frame_budget = Duration::from_millis(16);
189 let spent = last_tick.elapsed();
190 if spent < frame_budget {
191 std::thread::sleep(frame_budget - spent);
192 }
193 }
194 }
195 PlatformControlFlow::Wait => {
196 std::thread::sleep(Duration::from_millis(16));
197 }
198 PlatformControlFlow::Exit => break,
199 }
200 }
201 }
202}
203
204pub trait AsyncPlatformSurface {
206 fn surface_size(&self) -> (i16, i16);
208
209 fn flush_frame_dt_async(
211 &mut self,
212 engine: &mut VeloxEngine,
213 dt_ms: u16,
214 ) -> impl Future<Output = FlushReport>;
215
216 fn flush_frame_async(&mut self, engine: &mut VeloxEngine) -> impl Future<Output = FlushReport> {
218 self.flush_frame_dt_async(engine, 16)
219 }
220}
221
222pub trait AsyncPlatform {
224 fn duration_since_start(&self) -> Duration {
226 Duration::ZERO
227 }
228
229 fn step_platform_async(
231 &mut self,
232 engine: &mut VeloxEngine,
233 dt_ms: u16,
234 ) -> impl Future<Output = PlatformControlFlow>;
235}