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 android;
24pub mod harmony;
25pub mod ios;
26pub mod shm_driver;
27pub mod wayland;
28pub mod winit;
29
30
31pub use crate::renderers::RepaintBufferType;
32
33#[derive(Copy, Clone, PartialEq, Eq, Debug)]
38pub enum DesktopRenderBackend {
39 Wgpu,
41 Software,
43}
44
45impl DesktopRenderBackend {
46 #[inline(always)]
50 pub const fn default_desktop() -> Self {
51 #[cfg(feature = "gpu")]
52 {
53 Self::Wgpu
54 }
55 #[cfg(not(feature = "gpu"))]
56 {
57 Self::Software
58 }
59 }
60
61 #[inline(always)]
63 pub const fn is_gpu(self) -> bool {
64 matches!(self, Self::Wgpu)
65 }
66}
67
68impl Default for DesktopRenderBackend {
69 #[inline(always)]
70 fn default() -> Self {
71 Self::default_desktop()
72 }
73}
74
75#[derive(Copy, Clone, PartialEq, Eq, Debug)]
77pub enum PlatformControlFlow {
78 Poll,
80 Wait,
82 Exit,
84}
85
86#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
88pub struct FlushReport {
89 pub dirty_regions: usize,
91 pub strips_or_buffers: usize,
93 pub pixels_updated: usize,
95}
96
97impl FlushReport {
98 #[inline(always)]
100 pub const fn has_updates(&self) -> bool {
101 self.dirty_regions > 0
102 }
103}
104
105pub trait PlatformSurface {
110 fn surface_size(&self) -> (i16, i16);
112
113 fn flush_frame_dt(&mut self, engine: &mut VeloxEngine, dt_ms: u16) -> FlushReport;
115
116 #[inline]
118 fn flush_frame(&mut self, engine: &mut VeloxEngine) -> FlushReport {
119 self.flush_frame_dt(engine, 16)
120 }
121}
122
123pub trait Platform {
127 fn duration_since_start(&self) -> Duration {
129 Duration::ZERO
130 }
131
132 fn step_platform(&mut self, engine: &mut VeloxEngine, dt_ms: u16) -> PlatformControlFlow;
134
135 fn run_event_loop(mut self, mut engine: VeloxEngine)
137 where
138 Self: Sized,
139 {
140 let mut last_tick = std::time::Instant::now();
141 loop {
142 let dt_ms = {
143 let now = std::time::Instant::now();
144 let elapsed = now.duration_since(last_tick).as_millis();
145 last_tick = now;
146 elapsed.clamp(1, 100) as u16
147 };
148
149 match self.step_platform(&mut engine, dt_ms) {
150 PlatformControlFlow::Poll => {
151 {
152 let frame_budget = Duration::from_millis(16);
153 let spent = last_tick.elapsed();
154 if spent < frame_budget {
155 std::thread::sleep(frame_budget - spent);
156 }
157 }
158 }
159 PlatformControlFlow::Wait => {
160 std::thread::sleep(Duration::from_millis(16));
161 }
162 PlatformControlFlow::Exit => break,
163 }
164 }
165 }
166}
167
168pub trait AsyncPlatformSurface {
170 fn surface_size(&self) -> (i16, i16);
172
173 fn flush_frame_dt_async(
175 &mut self,
176 engine: &mut VeloxEngine,
177 dt_ms: u16,
178 ) -> impl Future<Output = FlushReport>;
179
180 fn flush_frame_async(&mut self, engine: &mut VeloxEngine) -> impl Future<Output = FlushReport> {
182 self.flush_frame_dt_async(engine, 16)
183 }
184}
185
186pub trait AsyncPlatform {
188 fn duration_since_start(&self) -> Duration {
190 Duration::ZERO
191 }
192
193 fn step_platform_async(
195 &mut self,
196 engine: &mut VeloxEngine,
197 dt_ms: u16,
198 ) -> impl Future<Output = PlatformControlFlow>;
199}