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}
99
100impl FlushReport {
101 #[inline(always)]
103 pub const fn has_updates(&self) -> bool {
104 self.dirty_regions > 0
105 }
106}
107
108pub trait PlatformSurface {
113 fn surface_size(&self) -> (i16, i16);
115
116 fn flush_frame_dt(&mut self, engine: &mut VeloxEngine, dt_ms: u16) -> FlushReport;
118
119 #[inline]
121 fn flush_frame(&mut self, engine: &mut VeloxEngine) -> FlushReport {
122 self.flush_frame_dt(engine, 16)
123 }
124}
125
126pub trait Platform {
130 fn duration_since_start(&self) -> Duration {
132 Duration::ZERO
133 }
134
135 fn step_platform(&mut self, engine: &mut VeloxEngine, dt_ms: u16) -> PlatformControlFlow;
137
138 fn run_event_loop(mut self, mut engine: VeloxEngine)
140 where
141 Self: Sized,
142 {
143 let mut last_tick = std::time::Instant::now();
144 loop {
145 let dt_ms = {
146 let now = std::time::Instant::now();
147 let elapsed = now.duration_since(last_tick).as_millis();
148 last_tick = now;
149 elapsed.clamp(1, 100) as u16
150 };
151
152 match self.step_platform(&mut engine, dt_ms) {
153 PlatformControlFlow::Poll => {
154 {
155 let frame_budget = Duration::from_millis(16);
156 let spent = last_tick.elapsed();
157 if spent < frame_budget {
158 std::thread::sleep(frame_budget - spent);
159 }
160 }
161 }
162 PlatformControlFlow::Wait => {
163 std::thread::sleep(Duration::from_millis(16));
164 }
165 PlatformControlFlow::Exit => break,
166 }
167 }
168 }
169}
170
171pub trait AsyncPlatformSurface {
173 fn surface_size(&self) -> (i16, i16);
175
176 fn flush_frame_dt_async(
178 &mut self,
179 engine: &mut VeloxEngine,
180 dt_ms: u16,
181 ) -> impl Future<Output = FlushReport>;
182
183 fn flush_frame_async(&mut self, engine: &mut VeloxEngine) -> impl Future<Output = FlushReport> {
185 self.flush_frame_dt_async(engine, 16)
186 }
187}
188
189pub trait AsyncPlatform {
191 fn duration_since_start(&self) -> Duration {
193 Duration::ZERO
194 }
195
196 fn step_platform_async(
198 &mut self,
199 engine: &mut VeloxEngine,
200 dt_ms: u16,
201 ) -> impl Future<Output = PlatformControlFlow>;
202}