use core::future::Future;
use core::time::Duration;
use crate::engine::VeloxEngine;
pub mod clipboard;
pub mod mobile;
pub mod testing;
pub mod web;
pub use clipboard::{clipboard_has_text, clipboard_read_into, clipboard_write_text};
pub use clipboard::clipboard_read_text;
pub mod android;
pub mod harmony;
pub mod ios;
pub mod shm_driver;
pub mod wayland;
pub mod winit;
pub use crate::renderers::RepaintBufferType;
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum DesktopRenderBackend {
Wgpu,
Software,
}
impl DesktopRenderBackend {
#[inline(always)]
pub const fn default_desktop() -> Self {
#[cfg(feature = "gpu")]
{
Self::Wgpu
}
#[cfg(not(feature = "gpu"))]
{
Self::Software
}
}
#[inline(always)]
pub const fn is_gpu(self) -> bool {
matches!(self, Self::Wgpu)
}
}
impl Default for DesktopRenderBackend {
#[inline(always)]
fn default() -> Self {
Self::default_desktop()
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum PlatformControlFlow {
Poll,
Wait,
Exit,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub struct FlushReport {
pub dirty_regions: usize,
pub strips_or_buffers: usize,
pub pixels_updated: usize,
}
impl FlushReport {
#[inline(always)]
pub const fn has_updates(&self) -> bool {
self.dirty_regions > 0
}
}
pub trait PlatformSurface {
fn surface_size(&self) -> (i16, i16);
fn flush_frame_dt(&mut self, engine: &mut VeloxEngine, dt_ms: u16) -> FlushReport;
#[inline]
fn flush_frame(&mut self, engine: &mut VeloxEngine) -> FlushReport {
self.flush_frame_dt(engine, 16)
}
}
pub trait Platform {
fn duration_since_start(&self) -> Duration {
Duration::ZERO
}
fn step_platform(&mut self, engine: &mut VeloxEngine, dt_ms: u16) -> PlatformControlFlow;
fn run_event_loop(mut self, mut engine: VeloxEngine)
where
Self: Sized,
{
let mut last_tick = std::time::Instant::now();
loop {
let dt_ms = {
let now = std::time::Instant::now();
let elapsed = now.duration_since(last_tick).as_millis();
last_tick = now;
elapsed.clamp(1, 100) as u16
};
match self.step_platform(&mut engine, dt_ms) {
PlatformControlFlow::Poll => {
{
let frame_budget = Duration::from_millis(16);
let spent = last_tick.elapsed();
if spent < frame_budget {
std::thread::sleep(frame_budget - spent);
}
}
}
PlatformControlFlow::Wait => {
std::thread::sleep(Duration::from_millis(16));
}
PlatformControlFlow::Exit => break,
}
}
}
}
pub trait AsyncPlatformSurface {
fn surface_size(&self) -> (i16, i16);
fn flush_frame_dt_async(
&mut self,
engine: &mut VeloxEngine,
dt_ms: u16,
) -> impl Future<Output = FlushReport>;
fn flush_frame_async(&mut self, engine: &mut VeloxEngine) -> impl Future<Output = FlushReport> {
self.flush_frame_dt_async(engine, 16)
}
}
pub trait AsyncPlatform {
fn duration_since_start(&self) -> Duration {
Duration::ZERO
}
fn step_platform_async(
&mut self,
engine: &mut VeloxEngine,
dt_ms: u16,
) -> impl Future<Output = PlatformControlFlow>;
}