feat(wayland): core surface

This commit is contained in:
Nova
2022-08-22 10:15:20 -04:00
parent 060bd59d01
commit a8bbfbafdf
9 changed files with 352 additions and 74 deletions

View File

@@ -3,20 +3,15 @@ mod nodes;
mod wayland;
use crate::nodes::model::{MODELS_TO_DROP, MODEL_REGISTRY};
use crate::wayland::{ClientState, WaylandState};
use crate::wayland::WaylandState;
use self::core::eventloop::EventLoop;
use anyhow::{ensure, Result};
use anyhow::Result;
use clap::Parser;
use once_cell::sync::Lazy;
use parking_lot::Mutex;
use slog::Drain;
use smithay::backend::egl::EGLContext;
use smithay::backend::renderer::gles2::Gles2Renderer;
use smithay::reexports::wayland_server::{Display, ListeningSocket};
use std::ffi::c_void;
use std::sync::Arc;
use stereokit as sk;
use stereokit::{lifecycle::DisplayMode, Settings};
use tokio::{runtime::Handle, sync::oneshot};
@@ -34,27 +29,6 @@ struct CliArgs {
overlay: bool,
}
struct EGLRawHandles {
display: *const c_void,
config: *const c_void,
context: *const c_void,
}
fn get_sk_egl() -> Result<EGLRawHandles> {
ensure!(
unsafe { sk::sys::backend_graphics_get() }
== sk::sys::backend_graphics__backend_graphics_opengles_egl,
"StereoKit is not running using EGL!"
);
Ok(unsafe {
EGLRawHandles {
display: sk::sys::backend_opengl_egl_get_display() as *const c_void,
config: sk::sys::backend_opengl_egl_get_config() as *const c_void,
context: sk::sys::backend_opengl_egl_get_context() as *const c_void,
}
})
}
fn main() -> Result<()> {
let cli_args = Arc::new(CliArgs::parse());
let log = ::slog::Logger::root(::slog_stdlog::StdLog.fuse(), slog::o!());
@@ -72,41 +46,18 @@ fn main() -> Result<()> {
})
.init()
.expect("StereoKit failed to initialize");
println!("Init StereoKit");
let (event_stop_tx, event_stop_rx) = oneshot::channel::<()>();
let event_thread = std::thread::Builder::new()
.name("event_loop".to_owned())
.spawn(move || event_loop(event_stop_rx))?;
let egl_raw_handles = get_sk_egl()?;
let renderer = unsafe {
Gles2Renderer::new(
EGLContext::from_raw(
egl_raw_handles.display,
egl_raw_handles.config,
egl_raw_handles.context,
log.clone(),
)?,
log.clone(),
)?
};
let mut display: Display<WaylandState> = Display::new()?;
let socket = ListeningSocket::bind_auto("wayland", 0..33)?;
if let Some(socket_name) = socket.socket_name() {
println!("Wayland compositor {:?} active", socket_name);
}
let mut wayland_state = WaylandState::new(&display, renderer, log)?;
let mut wayland = WaylandState::new(log)?;
println!("Stardust ready!");
stereokit.run(
|draw_ctx| {
if let Ok(Some(client)) = socket.accept() {
let _ = display
.handle()
.insert_client(client, Arc::new(ClientState));
}
display.dispatch_clients(&mut wayland_state).unwrap();
display.flush_clients().unwrap();
wayland.frame(&stereokit);
nodes::root::Root::logic_step(stereokit.time_elapsed());
for model in MODEL_REGISTRY.get_valid_contents() {
@@ -114,17 +65,14 @@ fn main() -> Result<()> {
}
MODELS_TO_DROP.lock().clear();
unsafe { wayland_state.renderer.egl_context().make_current().unwrap() };
unsafe { wayland.renderer.egl_context().make_current().unwrap() };
},
|| {
println!("Cleanly shut down StereoKit");
},
);
drop(wayland_state);
drop(socket);
drop(display);
println!("Cleanly shut down the Wayland compositor");
drop(wayland);
let _ = event_stop_tx.send(());
event_thread