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 shm_driver;
29pub mod wayland;
30pub mod winit;
31
32
33pub use crate::renderers::RepaintBufferType;
34
35#[derive(Copy, Clone, PartialEq, Eq, Debug)]
40pub enum DesktopRenderBackend {
41 Wgpu,
43 Software,
45}
46
47impl DesktopRenderBackend {
48 #[inline(always)]
52 pub const fn default_desktop() -> Self {
53 #[cfg(feature = "gpu")]
54 {
55 Self::Wgpu
56 }
57 #[cfg(not(feature = "gpu"))]
58 {
59 Self::Software
60 }
61 }
62
63 #[inline(always)]
65 pub const fn is_gpu(self) -> bool {
66 matches!(self, Self::Wgpu)
67 }
68}
69
70impl Default for DesktopRenderBackend {
71 #[inline(always)]
72 fn default() -> Self {
73 Self::default_desktop()
74 }
75}
76
77#[derive(Copy, Clone, PartialEq, Eq, Debug)]
79pub enum PlatformControlFlow {
80 Poll,
82 Wait,
84 Exit,
86}
87
88#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
90pub struct FlushReport {
91 pub dirty_regions: usize,
93 pub strips_or_buffers: usize,
95 pub pixels_updated: usize,
97}
98
99impl FlushReport {
100 #[inline(always)]
102 pub const fn has_updates(&self) -> bool {
103 self.dirty_regions > 0
104 }
105}
106
107pub trait PlatformSurface {
112 fn surface_size(&self) -> (i16, i16);
114
115 fn flush_frame_dt(&mut self, engine: &mut VeloxEngine, dt_ms: u16) -> FlushReport;
117
118 #[inline]
120 fn flush_frame(&mut self, engine: &mut VeloxEngine) -> FlushReport {
121 self.flush_frame_dt(engine, 16)
122 }
123}
124
125pub trait Platform {
129 fn duration_since_start(&self) -> Duration {
131 Duration::ZERO
132 }
133
134 fn step_platform(&mut self, engine: &mut VeloxEngine, dt_ms: u16) -> PlatformControlFlow;
136
137 fn run_event_loop(mut self, mut engine: VeloxEngine)
139 where
140 Self: Sized,
141 {
142 let mut last_tick = std::time::Instant::now();
143 loop {
144 let dt_ms = {
145 let now = std::time::Instant::now();
146 let elapsed = now.duration_since(last_tick).as_millis();
147 last_tick = now;
148 elapsed.clamp(1, 100) as u16
149 };
150
151 match self.step_platform(&mut engine, dt_ms) {
152 PlatformControlFlow::Poll => {
153 {
154 let frame_budget = Duration::from_millis(16);
155 let spent = last_tick.elapsed();
156 if spent < frame_budget {
157 std::thread::sleep(frame_budget - spent);
158 }
159 }
160 }
161 PlatformControlFlow::Wait => {
162 std::thread::sleep(Duration::from_millis(16));
163 }
164 PlatformControlFlow::Exit => break,
165 }
166 }
167 }
168}
169
170pub trait AsyncPlatformSurface {
172 fn surface_size(&self) -> (i16, i16);
174
175 fn flush_frame_dt_async(
177 &mut self,
178 engine: &mut VeloxEngine,
179 dt_ms: u16,
180 ) -> impl Future<Output = FlushReport>;
181
182 fn flush_frame_async(&mut self, engine: &mut VeloxEngine) -> impl Future<Output = FlushReport> {
184 self.flush_frame_dt_async(engine, 16)
185 }
186}
187
188pub trait AsyncPlatform {
190 fn duration_since_start(&self) -> Duration {
192 Duration::ZERO
193 }
194
195 fn step_platform_async(
197 &mut self,
198 engine: &mut VeloxEngine,
199 dt_ms: u16,
200 ) -> impl Future<Output = PlatformControlFlow>;
201}