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 embed_ffi;
25pub mod harmony;
26pub mod ios;
27pub mod shm_driver;
28pub mod wayland;
29pub mod winit;
30
31
32pub use crate::renderers::RepaintBufferType;
33
34#[derive(Copy, Clone, PartialEq, Eq, Debug)]
39pub enum DesktopRenderBackend {
40 Wgpu,
42 Software,
44}
45
46impl DesktopRenderBackend {
47 #[inline(always)]
51 pub const fn default_desktop() -> Self {
52 #[cfg(feature = "gpu")]
53 {
54 Self::Wgpu
55 }
56 #[cfg(not(feature = "gpu"))]
57 {
58 Self::Software
59 }
60 }
61
62 #[inline(always)]
64 pub const fn is_gpu(self) -> bool {
65 matches!(self, Self::Wgpu)
66 }
67}
68
69impl Default for DesktopRenderBackend {
70 #[inline(always)]
71 fn default() -> Self {
72 Self::default_desktop()
73 }
74}
75
76#[derive(Copy, Clone, PartialEq, Eq, Debug)]
78pub enum PlatformControlFlow {
79 Poll,
81 Wait,
83 Exit,
85}
86
87#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
89pub struct FlushReport {
90 pub dirty_regions: usize,
92 pub strips_or_buffers: usize,
94 pub pixels_updated: usize,
96}
97
98impl FlushReport {
99 #[inline(always)]
101 pub const fn has_updates(&self) -> bool {
102 self.dirty_regions > 0
103 }
104}
105
106pub trait PlatformSurface {
111 fn surface_size(&self) -> (i16, i16);
113
114 fn flush_frame_dt(&mut self, engine: &mut VeloxEngine, dt_ms: u16) -> FlushReport;
116
117 #[inline]
119 fn flush_frame(&mut self, engine: &mut VeloxEngine) -> FlushReport {
120 self.flush_frame_dt(engine, 16)
121 }
122}
123
124pub trait Platform {
128 fn duration_since_start(&self) -> Duration {
130 Duration::ZERO
131 }
132
133 fn step_platform(&mut self, engine: &mut VeloxEngine, dt_ms: u16) -> PlatformControlFlow;
135
136 fn run_event_loop(mut self, mut engine: VeloxEngine)
138 where
139 Self: Sized,
140 {
141 let mut last_tick = std::time::Instant::now();
142 loop {
143 let dt_ms = {
144 let now = std::time::Instant::now();
145 let elapsed = now.duration_since(last_tick).as_millis();
146 last_tick = now;
147 elapsed.clamp(1, 100) as u16
148 };
149
150 match self.step_platform(&mut engine, dt_ms) {
151 PlatformControlFlow::Poll => {
152 {
153 let frame_budget = Duration::from_millis(16);
154 let spent = last_tick.elapsed();
155 if spent < frame_budget {
156 std::thread::sleep(frame_budget - spent);
157 }
158 }
159 }
160 PlatformControlFlow::Wait => {
161 std::thread::sleep(Duration::from_millis(16));
162 }
163 PlatformControlFlow::Exit => break,
164 }
165 }
166 }
167}
168
169pub trait AsyncPlatformSurface {
171 fn surface_size(&self) -> (i16, i16);
173
174 fn flush_frame_dt_async(
176 &mut self,
177 engine: &mut VeloxEngine,
178 dt_ms: u16,
179 ) -> impl Future<Output = FlushReport>;
180
181 fn flush_frame_async(&mut self, engine: &mut VeloxEngine) -> impl Future<Output = FlushReport> {
183 self.flush_frame_dt_async(engine, 16)
184 }
185}
186
187pub trait AsyncPlatform {
189 fn duration_since_start(&self) -> Duration {
191 Duration::ZERO
192 }
193
194 fn step_platform_async(
196 &mut self,
197 engine: &mut VeloxEngine,
198 dt_ms: u16,
199 ) -> impl Future<Output = PlatformControlFlow>;
200}