mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-30 14:28:12 +08:00
Desktop: Refactor window state to not require locking (#2928)
* Replace window state with channels and improve resize performance * Move Cef Handler into the cef module * Reuse textures * Test cef scheduling * Schedule self render if texture is outdated * Address review comments
This commit is contained in:
+63
-71
@@ -1,41 +1,53 @@
|
|||||||
use crate::CustomEvent;
|
use crate::CustomEvent;
|
||||||
use crate::WindowState;
|
use crate::FrameBuffer;
|
||||||
use crate::WindowStateHandle;
|
use crate::WindowSize;
|
||||||
use crate::render::GraphicsState;
|
use crate::render::GraphicsState;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::sync::mpsc::Sender;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use winit::application::ApplicationHandler;
|
use winit::application::ApplicationHandler;
|
||||||
|
use winit::dpi::PhysicalSize;
|
||||||
use winit::event::StartCause;
|
use winit::event::StartCause;
|
||||||
use winit::event::WindowEvent;
|
use winit::event::WindowEvent;
|
||||||
use winit::event_loop::ActiveEventLoop;
|
use winit::event_loop::ActiveEventLoop;
|
||||||
use winit::event_loop::ControlFlow;
|
use winit::event_loop::ControlFlow;
|
||||||
|
use winit::event_loop::EventLoopProxy;
|
||||||
use winit::window::Window;
|
use winit::window::Window;
|
||||||
use winit::window::WindowId;
|
use winit::window::WindowId;
|
||||||
|
|
||||||
use crate::cef;
|
use crate::cef;
|
||||||
|
|
||||||
pub(crate) struct WinitApp {
|
pub(crate) struct WinitApp {
|
||||||
pub(crate) window_state: WindowStateHandle,
|
|
||||||
pub(crate) cef_context: cef::Context<cef::Initialized>,
|
pub(crate) cef_context: cef::Context<cef::Initialized>,
|
||||||
pub(crate) window: Option<Arc<Window>>,
|
pub(crate) window: Option<Arc<Window>>,
|
||||||
cef_schedule: Option<Instant>,
|
cef_schedule: Option<Instant>,
|
||||||
|
ui_frame_buffer: Option<FrameBuffer>,
|
||||||
|
window_size_sender: Sender<WindowSize>,
|
||||||
|
_viewport_frame_buffer: Option<FrameBuffer>,
|
||||||
|
graphics_state: Option<GraphicsState>,
|
||||||
|
event_loop_proxy: EventLoopProxy<CustomEvent>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WinitApp {
|
impl WinitApp {
|
||||||
pub(crate) fn new(window_state: WindowStateHandle, cef_context: cef::Context<cef::Initialized>) -> Self {
|
pub(crate) fn new(cef_context: cef::Context<cef::Initialized>, window_size_sender: Sender<WindowSize>, event_loop_proxy: EventLoopProxy<CustomEvent>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
window_state,
|
|
||||||
cef_context,
|
cef_context,
|
||||||
window: None,
|
window: None,
|
||||||
cef_schedule: Some(Instant::now()),
|
cef_schedule: Some(Instant::now()),
|
||||||
|
_viewport_frame_buffer: None,
|
||||||
|
ui_frame_buffer: None,
|
||||||
|
graphics_state: None,
|
||||||
|
window_size_sender,
|
||||||
|
event_loop_proxy,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ApplicationHandler<CustomEvent> for WinitApp {
|
impl ApplicationHandler<CustomEvent> for WinitApp {
|
||||||
fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
|
fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
|
||||||
let timeout = Instant::now() + Duration::from_millis(10);
|
// Set a timeout in case we miss any cef schedule requests
|
||||||
|
let timeout = Instant::now() + Duration::from_millis(100);
|
||||||
let wait_until = timeout.min(self.cef_schedule.unwrap_or(timeout));
|
let wait_until = timeout.min(self.cef_schedule.unwrap_or(timeout));
|
||||||
event_loop.set_control_flow(ControlFlow::WaitUntil(wait_until));
|
event_loop.set_control_flow(ControlFlow::WaitUntil(wait_until));
|
||||||
}
|
}
|
||||||
@@ -50,37 +62,42 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||||
self.window_state
|
let window = Arc::new(
|
||||||
.with(|s| {
|
event_loop
|
||||||
if let WindowState { width: Some(w), height: Some(h), .. } = s {
|
.create_window(
|
||||||
let window = Arc::new(
|
Window::default_attributes()
|
||||||
event_loop
|
.with_title("CEF Offscreen Rendering")
|
||||||
.create_window(
|
.with_inner_size(winit::dpi::LogicalSize::new(1200, 800)),
|
||||||
Window::default_attributes()
|
)
|
||||||
.with_title("CEF Offscreen Rendering")
|
.unwrap(),
|
||||||
.with_inner_size(winit::dpi::LogicalSize::new(*w as u32, *h as u32)),
|
);
|
||||||
)
|
let graphics_state = pollster::block_on(GraphicsState::new(window.clone()));
|
||||||
.unwrap(),
|
|
||||||
);
|
|
||||||
let graphics_state = pollster::block_on(GraphicsState::new(window.clone()));
|
|
||||||
|
|
||||||
self.window = Some(window.clone());
|
self.window = Some(window);
|
||||||
s.graphics_state = Some(graphics_state);
|
self.graphics_state = Some(graphics_state);
|
||||||
|
|
||||||
tracing::info!("Winit window created and ready");
|
tracing::info!("Winit window created and ready");
|
||||||
}
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn user_event(&mut self, _: &ActiveEventLoop, event: CustomEvent) {
|
fn user_event(&mut self, _: &ActiveEventLoop, event: CustomEvent) {
|
||||||
match event {
|
match event {
|
||||||
CustomEvent::UiUpdate => {
|
CustomEvent::UiUpdate(frame_buffer) => {
|
||||||
|
if let Some(graphics_state) = self.graphics_state.as_mut() {
|
||||||
|
graphics_state.update_texture(&frame_buffer);
|
||||||
|
}
|
||||||
|
self.ui_frame_buffer = Some(frame_buffer);
|
||||||
if let Some(window) = &self.window {
|
if let Some(window) = &self.window {
|
||||||
window.request_redraw();
|
window.request_redraw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CustomEvent::ScheduleBrowserWork(instant) => {
|
CustomEvent::ScheduleBrowserWork(instant) => {
|
||||||
|
if let Some(graphics_state) = self.graphics_state.as_mut()
|
||||||
|
&& let Some(frame_buffer) = &self.ui_frame_buffer
|
||||||
|
&& graphics_state.ui_texture_outdated(frame_buffer)
|
||||||
|
{
|
||||||
|
self.cef_context.work();
|
||||||
|
let _ = self.event_loop_proxy.send_event(CustomEvent::ScheduleBrowserWork(Instant::now() + Duration::from_millis(1)));
|
||||||
|
}
|
||||||
self.cef_schedule = Some(instant);
|
self.cef_schedule = Some(instant);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -94,58 +111,33 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
|
|||||||
tracing::info!("The close button was pressed; stopping");
|
tracing::info!("The close button was pressed; stopping");
|
||||||
event_loop.exit();
|
event_loop.exit();
|
||||||
}
|
}
|
||||||
WindowEvent::Resized(physical_size) => {
|
WindowEvent::Resized(PhysicalSize { width, height }) => {
|
||||||
self.window_state
|
let _ = self.window_size_sender.send(WindowSize::new(width as usize, height as usize));
|
||||||
.with(|s| {
|
if let Some(ref mut graphics_state) = self.graphics_state {
|
||||||
let width = physical_size.width as usize;
|
graphics_state.resize(width, height);
|
||||||
let height = physical_size.height as usize;
|
}
|
||||||
s.width = Some(width);
|
|
||||||
s.height = Some(height);
|
|
||||||
if let Some(graphics_state) = &mut s.graphics_state {
|
|
||||||
graphics_state.resize(width, height);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
self.cef_context.notify_of_resize();
|
self.cef_context.notify_of_resize();
|
||||||
}
|
}
|
||||||
|
|
||||||
WindowEvent::RedrawRequested => {
|
WindowEvent::RedrawRequested => {
|
||||||
self.cef_context.work();
|
let Some(ref mut graphics_state) = self.graphics_state else { return };
|
||||||
|
// Only rerender once we have a new ui texture to display
|
||||||
|
|
||||||
self.window_state
|
match graphics_state.render() {
|
||||||
.with(|s| {
|
Ok(_) => {}
|
||||||
if let WindowState {
|
Err(wgpu::SurfaceError::Lost) => {
|
||||||
width: Some(width),
|
tracing::warn!("lost surface");
|
||||||
height: Some(height),
|
}
|
||||||
graphics_state: Some(graphics_state),
|
Err(wgpu::SurfaceError::OutOfMemory) => {
|
||||||
ui_frame_buffer: ui_fb,
|
event_loop.exit();
|
||||||
..
|
}
|
||||||
} = s
|
Err(e) => tracing::error!("{:?}", e),
|
||||||
{
|
}
|
||||||
if let Some(fb) = &*ui_fb {
|
|
||||||
graphics_state.update_texture(fb);
|
|
||||||
if fb.width() != *width && fb.height() != *height {
|
|
||||||
graphics_state.resize(*width, *height);
|
|
||||||
}
|
|
||||||
} else if let Some(window) = &self.window {
|
|
||||||
window.request_redraw();
|
|
||||||
}
|
|
||||||
|
|
||||||
match graphics_state.render() {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(wgpu::SurfaceError::Lost) => {
|
|
||||||
graphics_state.resize(*width, *height);
|
|
||||||
}
|
|
||||||
Err(wgpu::SurfaceError::OutOfMemory) => {
|
|
||||||
event_loop.exit();
|
|
||||||
}
|
|
||||||
Err(e) => tracing::error!("{:?}", e),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Notify cef of possible input events
|
||||||
|
self.cef_context.work();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+55
-4
@@ -1,5 +1,8 @@
|
|||||||
use crate::FrameBuffer;
|
use crate::{CustomEvent, FrameBuffer};
|
||||||
use std::time::Instant;
|
use std::{
|
||||||
|
sync::{Arc, Mutex, mpsc::Receiver},
|
||||||
|
time::Instant,
|
||||||
|
};
|
||||||
|
|
||||||
mod context;
|
mod context;
|
||||||
mod dirs;
|
mod dirs;
|
||||||
@@ -8,16 +11,17 @@ mod internal;
|
|||||||
mod scheme_handler;
|
mod scheme_handler;
|
||||||
|
|
||||||
pub(crate) use context::{Context, InitError, Initialized, Setup, SetupError};
|
pub(crate) use context::{Context, InitError, Initialized, Setup, SetupError};
|
||||||
|
use winit::event_loop::EventLoopProxy;
|
||||||
|
|
||||||
pub(crate) trait CefEventHandler: Clone {
|
pub(crate) trait CefEventHandler: Clone {
|
||||||
fn window_size(&self) -> WindowSize;
|
fn window_size(&self) -> WindowSize;
|
||||||
fn draw(&self, frame_buffer: FrameBuffer) -> bool;
|
fn draw(&self, frame_buffer: FrameBuffer);
|
||||||
/// Scheudule the main event loop to run the cef event loop after the timeout
|
/// Scheudule the main event loop to run the cef event loop after the timeout
|
||||||
/// [`_cef_browser_process_handler_t::on_schedule_message_pump_work`] for more documentation.
|
/// [`_cef_browser_process_handler_t::on_schedule_message_pump_work`] for more documentation.
|
||||||
fn schedule_cef_message_loop_work(&self, scheduled_time: Instant);
|
fn schedule_cef_message_loop_work(&self, scheduled_time: Instant);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Copy)]
|
||||||
pub(crate) struct WindowSize {
|
pub(crate) struct WindowSize {
|
||||||
pub(crate) width: usize,
|
pub(crate) width: usize,
|
||||||
pub(crate) height: usize,
|
pub(crate) height: usize,
|
||||||
@@ -28,3 +32,50 @@ impl WindowSize {
|
|||||||
Self { width, height }
|
Self { width, height }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub(crate) struct CefHandler {
|
||||||
|
window_size_receiver: Arc<Mutex<WindowSizeReceiver>>,
|
||||||
|
event_loop_proxy: EventLoopProxy<CustomEvent>,
|
||||||
|
}
|
||||||
|
struct WindowSizeReceiver {
|
||||||
|
receiver: Receiver<WindowSize>,
|
||||||
|
window_size: WindowSize,
|
||||||
|
}
|
||||||
|
impl WindowSizeReceiver {
|
||||||
|
fn new(window_size_receiver: Receiver<WindowSize>) -> Self {
|
||||||
|
Self {
|
||||||
|
window_size: WindowSize { width: 1, height: 1 },
|
||||||
|
receiver: window_size_receiver,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl CefHandler {
|
||||||
|
pub(crate) fn new(window_size_receiver: Receiver<WindowSize>, event_loop_proxy: EventLoopProxy<CustomEvent>) -> Self {
|
||||||
|
Self {
|
||||||
|
window_size_receiver: Arc::new(Mutex::new(WindowSizeReceiver::new(window_size_receiver))),
|
||||||
|
event_loop_proxy,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CefEventHandler for CefHandler {
|
||||||
|
fn window_size(&self) -> WindowSize {
|
||||||
|
let Ok(mut guard) = self.window_size_receiver.lock() else {
|
||||||
|
tracing::error!("Failed to lock window_size_receiver");
|
||||||
|
return WindowSize::new(1, 1);
|
||||||
|
};
|
||||||
|
let WindowSizeReceiver { receiver, window_size } = &mut *guard;
|
||||||
|
for new_window_size in receiver.try_iter() {
|
||||||
|
*window_size = new_window_size;
|
||||||
|
}
|
||||||
|
*window_size
|
||||||
|
}
|
||||||
|
fn draw(&self, frame_buffer: FrameBuffer) {
|
||||||
|
let _ = self.event_loop_proxy.send_event(CustomEvent::UiUpdate(frame_buffer));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn schedule_cef_message_loop_work(&self, scheduled_time: std::time::Instant) {
|
||||||
|
let _ = self.event_loop_proxy.send_event(CustomEvent::ScheduleBrowserWork(scheduled_time));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ pub(crate) struct AppImpl<H: CefEventHandler> {
|
|||||||
object: *mut RcImpl<_cef_app_t, Self>,
|
object: *mut RcImpl<_cef_app_t, Self>,
|
||||||
event_handler: H,
|
event_handler: H,
|
||||||
}
|
}
|
||||||
impl<H: CefEventHandler> AppImpl<H> {
|
impl<H: CefEventHandler + Clone> AppImpl<H> {
|
||||||
pub(crate) fn new(event_handler: H) -> Self {
|
pub(crate) fn new(event_handler: H) -> Self {
|
||||||
Self {
|
Self {
|
||||||
object: std::ptr::null_mut(),
|
object: std::ptr::null_mut(),
|
||||||
@@ -20,7 +20,7 @@ impl<H: CefEventHandler> AppImpl<H> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<H: CefEventHandler> ImplApp for AppImpl<H> {
|
impl<H: CefEventHandler + Clone> ImplApp for AppImpl<H> {
|
||||||
fn browser_process_handler(&self) -> Option<BrowserProcessHandler> {
|
fn browser_process_handler(&self) -> Option<BrowserProcessHandler> {
|
||||||
Some(BrowserProcessHandler::new(BrowserProcessHandlerImpl::new(self.event_handler.clone())))
|
Some(BrowserProcessHandler::new(BrowserProcessHandlerImpl::new(self.event_handler.clone())))
|
||||||
}
|
}
|
||||||
@@ -34,7 +34,7 @@ impl<H: CefEventHandler> ImplApp for AppImpl<H> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<H: CefEventHandler> Clone for AppImpl<H> {
|
impl<H: CefEventHandler + Clone> Clone for AppImpl<H> {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
unsafe {
|
unsafe {
|
||||||
let rc_impl = &mut *self.object;
|
let rc_impl = &mut *self.object;
|
||||||
@@ -54,7 +54,7 @@ impl<H: CefEventHandler> Rc for AppImpl<H> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<H: CefEventHandler> WrapApp for AppImpl<H> {
|
impl<H: CefEventHandler + Clone> WrapApp for AppImpl<H> {
|
||||||
fn wrap_rc(&mut self, object: *mut RcImpl<_cef_app_t, Self>) {
|
fn wrap_rc(&mut self, object: *mut RcImpl<_cef_app_t, Self>) {
|
||||||
self.object = object;
|
self.object = object;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ impl<H: CefEventHandler> BrowserProcessHandlerImpl<H> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<H: CefEventHandler> ImplBrowserProcessHandler for BrowserProcessHandlerImpl<H> {
|
impl<H: CefEventHandler + Clone> ImplBrowserProcessHandler for BrowserProcessHandlerImpl<H> {
|
||||||
fn on_context_initialized(&self) {
|
fn on_context_initialized(&self) {
|
||||||
cef::register_scheme_handler_factory(Some(&CefString::from(GRAPHITE_SCHEME)), None, Some(&mut SchemeHandlerFactory::new(GraphiteSchemeHandlerFactory::new())));
|
cef::register_scheme_handler_factory(Some(&CefString::from(GRAPHITE_SCHEME)), None, Some(&mut SchemeHandlerFactory::new(GraphiteSchemeHandlerFactory::new())));
|
||||||
}
|
}
|
||||||
@@ -34,7 +34,7 @@ impl<H: CefEventHandler> ImplBrowserProcessHandler for BrowserProcessHandlerImpl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<H: CefEventHandler> Clone for BrowserProcessHandlerImpl<H> {
|
impl<H: CefEventHandler + Clone> Clone for BrowserProcessHandlerImpl<H> {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
unsafe {
|
unsafe {
|
||||||
let rc_impl = &mut *self.object;
|
let rc_impl = &mut *self.object;
|
||||||
@@ -54,7 +54,7 @@ impl<H: CefEventHandler> Rc for BrowserProcessHandlerImpl<H> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<H: CefEventHandler> WrapBrowserProcessHandler for BrowserProcessHandlerImpl<H> {
|
impl<H: CefEventHandler + Clone> WrapBrowserProcessHandler for BrowserProcessHandlerImpl<H> {
|
||||||
fn wrap_rc(&mut self, object: *mut RcImpl<_cef_browser_process_handler_t, Self>) {
|
fn wrap_rc(&mut self, object: *mut RcImpl<_cef_browser_process_handler_t, Self>) {
|
||||||
self.object = object;
|
self.object = object;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use cef::rc::{Rc, RcImpl};
|
use cef::rc::{Rc, RcImpl};
|
||||||
use cef::sys::{_cef_render_handler_t, cef_base_ref_counted_t};
|
use cef::sys::{_cef_render_handler_t, cef_base_ref_counted_t};
|
||||||
use cef::{Browser, ImplBrowser, ImplBrowserHost, ImplRenderHandler, PaintElementType, Rect, WrapRenderHandler};
|
use cef::{Browser, ImplRenderHandler, PaintElementType, Rect, WrapRenderHandler};
|
||||||
|
|
||||||
use crate::FrameBuffer;
|
use crate::FrameBuffer;
|
||||||
use crate::cef::CefEventHandler;
|
use crate::cef::CefEventHandler;
|
||||||
@@ -32,7 +32,7 @@ impl<H: CefEventHandler> ImplRenderHandler for RenderHandlerImpl<H> {
|
|||||||
|
|
||||||
fn on_paint(
|
fn on_paint(
|
||||||
&self,
|
&self,
|
||||||
browser: Option<&mut Browser>,
|
_browser: Option<&mut Browser>,
|
||||||
_type_: PaintElementType,
|
_type_: PaintElementType,
|
||||||
_dirty_rect_count: usize,
|
_dirty_rect_count: usize,
|
||||||
_dirty_rects: Option<&Rect>,
|
_dirty_rects: Option<&Rect>,
|
||||||
@@ -44,12 +44,7 @@ impl<H: CefEventHandler> ImplRenderHandler for RenderHandlerImpl<H> {
|
|||||||
let buffer_slice = unsafe { std::slice::from_raw_parts(buffer, buffer_size) };
|
let buffer_slice = unsafe { std::slice::from_raw_parts(buffer, buffer_size) };
|
||||||
let frame_buffer = FrameBuffer::new(buffer_slice.to_vec(), width as usize, height as usize).expect("Failed to create frame buffer");
|
let frame_buffer = FrameBuffer::new(buffer_slice.to_vec(), width as usize, height as usize).expect("Failed to create frame buffer");
|
||||||
|
|
||||||
let draw_successful = self.event_handler.draw(frame_buffer);
|
self.event_handler.draw(frame_buffer)
|
||||||
if !draw_successful {
|
|
||||||
if let Some(browser) = browser {
|
|
||||||
browser.host().unwrap().was_resized();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_raw(&self) -> *mut _cef_render_handler_t {
|
fn get_raw(&self) -> *mut _cef_render_handler_t {
|
||||||
|
|||||||
+7
-130
@@ -1,16 +1,15 @@
|
|||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
|
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
use tracing_subscriber::EnvFilter;
|
use tracing_subscriber::EnvFilter;
|
||||||
use winit::event_loop::{EventLoop, EventLoopProxy};
|
use winit::event_loop::EventLoop;
|
||||||
|
|
||||||
mod cef;
|
mod cef;
|
||||||
use cef::Setup;
|
use cef::{Setup, WindowSize};
|
||||||
|
|
||||||
mod render;
|
mod render;
|
||||||
use render::{FrameBuffer, GraphicsState};
|
use render::FrameBuffer;
|
||||||
|
|
||||||
mod app;
|
mod app;
|
||||||
use app::WinitApp;
|
use app::WinitApp;
|
||||||
@@ -19,123 +18,10 @@ mod dirs;
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) enum CustomEvent {
|
pub(crate) enum CustomEvent {
|
||||||
UiUpdate,
|
UiUpdate(FrameBuffer),
|
||||||
ScheduleBrowserWork(Instant),
|
ScheduleBrowserWork(Instant),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub(crate) struct WindowState {
|
|
||||||
width: Option<usize>,
|
|
||||||
height: Option<usize>,
|
|
||||||
ui_frame_buffer: Option<FrameBuffer>,
|
|
||||||
_viewport_frame_buffer: Option<FrameBuffer>,
|
|
||||||
graphics_state: Option<GraphicsState>,
|
|
||||||
event_loop_proxy: Option<EventLoopProxy<CustomEvent>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl WindowState {
|
|
||||||
fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
width: None,
|
|
||||||
height: None,
|
|
||||||
ui_frame_buffer: None,
|
|
||||||
_viewport_frame_buffer: None,
|
|
||||||
graphics_state: None,
|
|
||||||
event_loop_proxy: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn handle(self) -> WindowStateHandle {
|
|
||||||
WindowStateHandle { inner: Arc::new(Mutex::new(self)) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) struct WindowStateHandle {
|
|
||||||
inner: Arc<Mutex<WindowState>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl WindowStateHandle {
|
|
||||||
fn with<'a, P>(&self, p: P) -> Result<(), PoisonError<MutexGuard<'a, WindowState>>>
|
|
||||||
where
|
|
||||||
P: FnOnce(&mut WindowState),
|
|
||||||
{
|
|
||||||
match self.inner.lock() {
|
|
||||||
Ok(mut guard) => {
|
|
||||||
p(&mut guard);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
Err(_) => todo!("not error handling yet"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Clone for WindowStateHandle {
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Self { inner: self.inner.clone() }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
struct CefHandler {
|
|
||||||
window_state: WindowStateHandle,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CefHandler {
|
|
||||||
fn new(window_state: WindowStateHandle) -> Self {
|
|
||||||
Self { window_state }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl cef::CefEventHandler for CefHandler {
|
|
||||||
fn window_size(&self) -> cef::WindowSize {
|
|
||||||
let mut w = 1;
|
|
||||||
let mut h = 1;
|
|
||||||
|
|
||||||
self.window_state
|
|
||||||
.with(|s| {
|
|
||||||
if let WindowState {
|
|
||||||
width: Some(width),
|
|
||||||
height: Some(height),
|
|
||||||
..
|
|
||||||
} = s
|
|
||||||
{
|
|
||||||
w = *width;
|
|
||||||
h = *height;
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
cef::WindowSize::new(w, h)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw(&self, frame_buffer: FrameBuffer) -> bool {
|
|
||||||
let mut correct_size = true;
|
|
||||||
self.window_state
|
|
||||||
.with(|s| {
|
|
||||||
if let Some(event_loop_proxy) = &s.event_loop_proxy {
|
|
||||||
let _ = event_loop_proxy.send_event(CustomEvent::UiUpdate);
|
|
||||||
}
|
|
||||||
if frame_buffer.width() != s.width.unwrap_or(1) || frame_buffer.height() != s.height.unwrap_or(1) {
|
|
||||||
correct_size = false;
|
|
||||||
} else {
|
|
||||||
s.ui_frame_buffer = Some(frame_buffer);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
correct_size
|
|
||||||
}
|
|
||||||
|
|
||||||
fn schedule_cef_message_loop_work(&self, scheduled_time: std::time::Instant) {
|
|
||||||
self.window_state
|
|
||||||
.with(|s| {
|
|
||||||
let Some(event_loop_proxy) = &mut s.event_loop_proxy else { return };
|
|
||||||
let _ = event_loop_proxy.send_event(CustomEvent::ScheduleBrowserWork(scheduled_time));
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).init();
|
tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()).init();
|
||||||
|
|
||||||
@@ -148,20 +34,11 @@ fn main() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let window_state = WindowState::new().handle();
|
|
||||||
|
|
||||||
window_state
|
|
||||||
.with(|s| {
|
|
||||||
s.width = Some(1200);
|
|
||||||
s.height = Some(800);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let event_loop = EventLoop::<CustomEvent>::with_user_event().build().unwrap();
|
let event_loop = EventLoop::<CustomEvent>::with_user_event().build().unwrap();
|
||||||
|
|
||||||
window_state.with(|s| s.event_loop_proxy = Some(event_loop.create_proxy())).unwrap();
|
let (window_size_sender, window_size_receiver) = std::sync::mpsc::channel();
|
||||||
|
|
||||||
let cef_context = match cef_context.init(CefHandler::new(window_state.clone())) {
|
let cef_context = match cef_context.init(cef::CefHandler::new(window_size_receiver, event_loop.create_proxy())) {
|
||||||
Ok(c) => c,
|
Ok(c) => c,
|
||||||
Err(cef::InitError::InitializationFailed) => {
|
Err(cef::InitError::InitializationFailed) => {
|
||||||
tracing::error!("Cef initialization failed");
|
tracing::error!("Cef initialization failed");
|
||||||
@@ -171,7 +48,7 @@ fn main() {
|
|||||||
|
|
||||||
tracing::info!("Cef initialized successfully");
|
tracing::info!("Cef initialized successfully");
|
||||||
|
|
||||||
let mut winit_app = WinitApp::new(window_state, cef_context);
|
let mut winit_app = WinitApp::new(cef_context, window_size_sender, event_loop.create_proxy());
|
||||||
|
|
||||||
event_loop.run_app(&mut winit_app).unwrap();
|
event_loop.run_app(&mut winit_app).unwrap();
|
||||||
}
|
}
|
||||||
|
|||||||
+30
-27
@@ -10,7 +10,7 @@ pub(crate) struct FrameBuffer {
|
|||||||
}
|
}
|
||||||
impl std::fmt::Debug for FrameBuffer {
|
impl std::fmt::Debug for FrameBuffer {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
f.debug_struct("WindowState")
|
f.debug_struct("FrameBuffer")
|
||||||
.field("width", &self.width)
|
.field("width", &self.width)
|
||||||
.field("height", &self.height)
|
.field("height", &self.height)
|
||||||
.field("len", &self.buffer.len())
|
.field("len", &self.buffer.len())
|
||||||
@@ -214,7 +214,7 @@ impl GraphicsState {
|
|||||||
|
|
||||||
let fb = FrameBuffer::new(initial_data, width, height)
|
let fb = FrameBuffer::new(initial_data, width, height)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
panic!("Failed to create initial FrameBuffer: {}", e);
|
panic!("Failed to create initial FrameBuffer: {e}");
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@@ -223,11 +223,32 @@ impl GraphicsState {
|
|||||||
graphics_state
|
graphics_state
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn resize(&mut self, width: usize, height: usize) {
|
pub(crate) fn ui_texture_outdated(&self, frame_buffer: &FrameBuffer) -> bool {
|
||||||
if width > 0 && height > 0 && (self.config.width != width as u32 || self.config.height != height as u32) {
|
let width = frame_buffer.width() as u32;
|
||||||
self.config.width = width as u32;
|
let height = frame_buffer.height() as u32;
|
||||||
self.config.height = height as u32;
|
|
||||||
|
self.config.width != width || self.config.height != height
|
||||||
|
}
|
||||||
|
pub(crate) fn resize(&mut self, width: u32, height: u32) {
|
||||||
|
if width > 0 && height > 0 && (self.config.width != width || self.config.height != height) {
|
||||||
|
self.config.width = width;
|
||||||
|
self.config.height = height;
|
||||||
self.surface.configure(&self.device, &self.config);
|
self.surface.configure(&self.device, &self.config);
|
||||||
|
let texture = self.device.create_texture(&wgpu::TextureDescriptor {
|
||||||
|
label: Some("CEF Texture"),
|
||||||
|
size: wgpu::Extent3d {
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
depth_or_array_layers: 1,
|
||||||
|
},
|
||||||
|
mip_level_count: 1,
|
||||||
|
sample_count: 1,
|
||||||
|
dimension: wgpu::TextureDimension::D2,
|
||||||
|
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
||||||
|
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
|
||||||
|
view_formats: &[],
|
||||||
|
});
|
||||||
|
self.texture = Some(texture);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -236,30 +257,13 @@ impl GraphicsState {
|
|||||||
let width = frame_buffer.width() as u32;
|
let width = frame_buffer.width() as u32;
|
||||||
let height = frame_buffer.height() as u32;
|
let height = frame_buffer.height() as u32;
|
||||||
|
|
||||||
if width > 0 && height > 0 && (self.config.width != width || self.config.height != height) {
|
self.resize(width, height);
|
||||||
self.config.width = width;
|
|
||||||
self.config.height = height;
|
|
||||||
self.surface.configure(&self.device, &self.config);
|
|
||||||
}
|
|
||||||
|
|
||||||
let texture = self.device.create_texture(&wgpu::TextureDescriptor {
|
let Some(ref texture) = self.texture else { return };
|
||||||
label: Some("CEF Texture"),
|
|
||||||
size: wgpu::Extent3d {
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
depth_or_array_layers: 1,
|
|
||||||
},
|
|
||||||
mip_level_count: 1,
|
|
||||||
sample_count: 1,
|
|
||||||
dimension: wgpu::TextureDimension::D2,
|
|
||||||
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
|
||||||
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
|
|
||||||
view_formats: &[],
|
|
||||||
});
|
|
||||||
|
|
||||||
self.queue.write_texture(
|
self.queue.write_texture(
|
||||||
wgpu::TexelCopyTextureInfo {
|
wgpu::TexelCopyTextureInfo {
|
||||||
texture: &texture,
|
texture,
|
||||||
mip_level: 0,
|
mip_level: 0,
|
||||||
origin: wgpu::Origin3d::ZERO,
|
origin: wgpu::Origin3d::ZERO,
|
||||||
aspect: wgpu::TextureAspect::All,
|
aspect: wgpu::TextureAspect::All,
|
||||||
@@ -294,7 +298,6 @@ impl GraphicsState {
|
|||||||
label: Some("texture_bind_group"),
|
label: Some("texture_bind_group"),
|
||||||
});
|
});
|
||||||
|
|
||||||
self.texture = Some(texture);
|
|
||||||
self.bind_group = Some(bind_group);
|
self.bind_group = Some(bind_group);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user