#[cfg(feature = "winit")]
mod handler;
pub mod multi_window;
mod nested;
use std::eprintln;
use std::string::String;
use std::time::{Duration, Instant};
#[cfg(feature = "winit")]
use winit::event_loop::EventLoop;
#[cfg(feature = "winit")]
use handler::{WinitAppHandler, WinitUserEvent};
pub use multi_window::{
CsdConfig, CsdHitZone, MultiWindowId, MultiWindowManager, MultiWindowSpec, WindowRole,
WindowSession,
};
pub use nested::run_nested_compositor_window;
pub use super::shm_driver::{
write_uncompressed_png, ShmSlot, ShmSurfaceDriver, SoftwareSurfaceDriver, WaylandSurfaceDriver,
WlBuffer, WlSurface,
};
use crate::prelude::*;
pub type DesktopWindowRunner = WinitPlatform;
pub struct WinitPlatform {
title: String,
width: usize,
height: usize,
dpi_scale: usize,
dpi_scale_explicit: bool,
start_instant: Instant,
driver: SoftwareSurfaceDriver,
wl_surface: WlSurface,
}
impl WinitPlatform {
pub fn new(title: impl Into<String>, width: usize, height: usize) -> Self {
let dpi_scale = 2;
Self {
title: title.into(),
width,
height,
dpi_scale,
dpi_scale_explicit: false,
start_instant: Instant::now(),
driver: SoftwareSurfaceDriver::new(width * dpi_scale, height * dpi_scale),
wl_surface: WlSurface::new(),
}
}
pub fn with_dpi_scale(mut self, scale: usize) -> Self {
self.dpi_scale = scale.max(1);
self.dpi_scale_explicit = true;
self.driver
.resize(self.width * self.dpi_scale, self.height * self.dpi_scale);
self
}
#[inline]
pub fn with_backend(mut self, backend: super::DesktopRenderBackend) -> Self {
self.driver.set_backend(backend);
self
}
#[inline]
pub fn set_backend(&mut self, backend: super::DesktopRenderBackend) {
self.driver.set_backend(backend);
}
#[inline]
pub const fn backend(&self) -> super::DesktopRenderBackend {
self.driver.backend()
}
#[inline]
pub const fn driver(&self) -> &SoftwareSurfaceDriver {
&self.driver
}
pub fn run(self, mut core: VeloxEngine, scroll_list_id: Option<u32>) {
#[cfg(feature = "winit")]
{
#[cfg(not(target_arch = "wasm32"))]
{
let loc = core.locale();
let lang = loc.language();
let preload_cjk = lang == "zh" || lang == "ja" || lang == "ko";
crate::display::ttf_atlas::spawn_font_preload(preload_cjk);
}
let event_loop = match EventLoop::<WinitUserEvent>::with_user_event().build() {
Ok(el) => el,
Err(e) => {
eprintln!("[velox::platform::winit] 无法初始化显示服务器 EventLoop ({e}),回退至无头验证模式。");
self.run_headless(&mut core, 10);
return;
}
};
event_loop.listen_device_events(winit::event_loop::DeviceEvents::Never);
let Self {
title,
width,
height,
dpi_scale,
dpi_scale_explicit,
mut driver,
wl_surface,
..
} = self;
let scale_factor = (dpi_scale.max(1)) as f32;
core.set_scale_factor(scale_factor);
core.set_pointer_kind(PointerKind::Mouse);
let async_proxy = event_loop.create_proxy();
core.set_async_waker(move || {
let _ = async_proxy.send_event(WinitUserEvent::WakeAsync);
});
let phys_w = ((width as f32) * scale_factor).round().max(1.0) as usize;
let phys_h = ((height as f32) * scale_factor).round().max(1.0) as usize;
#[cfg(feature = "gpu")]
let gpu_warmup_in_flight = if driver.backend().is_gpu() {
let proxy = event_loop.create_proxy();
std::thread::Builder::new()
.name(String::from("velox-gpu-warmup"))
.stack_size(512 * 1024)
.spawn(move || {
let prepared = crate::renderers::wgpu::PreparedWgpuContext::init_headless();
let _ = proxy.send_event(WinitUserEvent::GpuReady(prepared));
})
.is_ok()
} else {
false
};
driver.resize(1, 1);
let mut app = WinitAppHandler {
title,
logical_w: width,
logical_h: height,
phys_w,
phys_h,
scale_factor,
dpi_scale_explicit,
core,
scroll_list_id,
driver,
wl_surface,
#[cfg(feature = "gpu")]
gpu_surface: None,
#[cfg(feature = "gpu")]
pending_gpu: None,
#[cfg(feature = "gpu")]
gpu_warmup_in_flight,
surface: None,
window: None,
window_shown: false,
force_present_once: false,
last_window_theme_dark: None,
cursor_pos: (0, 0),
mouse_down: false,
last_tick: Instant::now() - Duration::from_secs(1),
last_ime_area: None,
last_cursor: CursorShape::Default,
};
let _ = event_loop.run_app(&mut app);
}
#[cfg(not(feature = "winit"))]
{
let _ = scroll_list_id;
let _ = &self.title;
eprintln!("[velox::platform::winit] 未启用 winit 特性,以无头模式步进 10 帧。");
self.run_headless(&mut core, 10);
}
}
pub fn run_headless(&self, core: &mut VeloxEngine, frames: usize) {
let scale = self.dpi_scale.max(1);
core.set_dpi_scale(scale as i16);
let mut driver = SoftwareSurfaceDriver::new(self.width * scale, self.height * scale)
.with_backend(self.driver.backend());
let mut wl_surface = WlSurface::new();
for _ in 0..frames {
if let Some(id) = wl_surface.attached_buffer_id {
driver.release_buffer(id);
}
let _ = driver.flush_frame_dt(core, &mut wl_surface, 16);
}
}
pub fn render_to_png(&self, core: &mut VeloxEngine, path: &str) -> std::io::Result<()> {
let scale = self.dpi_scale.max(1);
core.set_dpi_scale(scale as i16);
core.force_full_repaint();
let phys_w = self.width * scale;
let phys_h = self.height * scale;
let mut driver =
SoftwareSurfaceDriver::new(phys_w, phys_h).with_backend(self.driver.backend());
let mut wl_surface = WlSurface::new();
driver.flush_frame_dt(core, &mut wl_surface, 16);
write_uncompressed_png(path, phys_w, phys_h, driver.front_pixels())
}
}
impl PlatformSurface for WinitPlatform {
#[inline(always)]
fn surface_size(&self) -> (i16, i16) {
(self.width as i16, self.height as i16)
}
fn flush_frame_dt(&mut self, engine: &mut VeloxEngine, dt_ms: u16) -> FlushReport {
engine.set_dpi_scale(self.dpi_scale.max(1) as i16);
if let Some(old_id) = self.wl_surface.attached_buffer_id {
self.driver.release_buffer(old_id);
}
if self
.driver
.flush_frame_dt(engine, &mut self.wl_surface, dt_ms)
{
let rects = self.wl_surface.committed_damage.len();
let px: usize = self
.wl_surface
.committed_damage
.iter()
.map(|r| (r.w.max(0) as usize) * (r.h.max(0) as usize))
.sum();
FlushReport {
dirty_regions: rects,
strips_or_buffers: 1,
pixels_updated: px,
}
} else {
FlushReport::default()
}
}
}
impl Platform for WinitPlatform {
#[inline]
fn duration_since_start(&self) -> Duration {
self.start_instant.elapsed()
}
fn step_platform(&mut self, engine: &mut VeloxEngine, dt_ms: u16) -> PlatformControlFlow {
let report = self.flush_frame_dt(engine, dt_ms);
if report.has_updates() || engine.needs_redraw() {
PlatformControlFlow::Poll
} else {
PlatformControlFlow::Wait
}
}
fn run_event_loop(self, engine: VeloxEngine) {
self.run(engine, None);
}
}