Skip to content

Commit 4450536

Browse files
committed
Replace window state with channels and improve resize performance
1 parent e7b8b5a commit 4450536

7 files changed

Lines changed: 111 additions & 205 deletions

File tree

desktop/src/app.rs

Lines changed: 58 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
use crate::CustomEvent;
2-
use crate::WindowState;
3-
use crate::WindowStateHandle;
2+
use crate::FrameBuffer;
3+
use crate::WindowSize;
44
use crate::render::GraphicsState;
55
use std::sync::Arc;
6+
use std::sync::mpsc::Sender;
67
use std::time::Duration;
78
use std::time::Instant;
9+
use tracing::instrument::WithSubscriber;
810
use winit::application::ApplicationHandler;
11+
use winit::dpi::PhysicalSize;
912
use winit::event::StartCause;
1013
use winit::event::WindowEvent;
1114
use winit::event_loop::ActiveEventLoop;
@@ -16,31 +19,42 @@ use winit::window::WindowId;
1619
use crate::cef;
1720

1821
pub(crate) struct WinitApp {
19-
pub(crate) window_state: WindowStateHandle,
2022
pub(crate) cef_context: cef::Context<cef::Initialized>,
2123
pub(crate) window: Option<Arc<Window>>,
2224
cef_schedule: Option<Instant>,
25+
ui_dirty: bool,
26+
ui_frame_buffer: Option<FrameBuffer>,
27+
window_size_sender: Sender<WindowSize>,
28+
_viewport_frame_buffer: Option<FrameBuffer>,
29+
graphics_state: Option<GraphicsState>,
2330
}
2431

2532
impl WinitApp {
26-
pub(crate) fn new(window_state: WindowStateHandle, cef_context: cef::Context<cef::Initialized>) -> Self {
33+
pub(crate) fn new(cef_context: cef::Context<cef::Initialized>, window_size_sender: Sender<WindowSize>) -> Self {
2734
Self {
28-
window_state,
2935
cef_context,
3036
window: None,
3137
cef_schedule: Some(Instant::now()),
38+
_viewport_frame_buffer: None,
39+
ui_frame_buffer: None,
40+
ui_dirty: false,
41+
graphics_state: None,
42+
window_size_sender,
3243
}
3344
}
3445
}
3546

3647
impl ApplicationHandler<CustomEvent> for WinitApp {
3748
fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
38-
let timeout = Instant::now() + Duration::from_millis(10);
49+
let timeout = Instant::now() + Duration::from_millis(1000);
3950
let wait_until = timeout.min(self.cef_schedule.unwrap_or(timeout));
4051
event_loop.set_control_flow(ControlFlow::WaitUntil(wait_until));
4152
}
4253

4354
fn new_events(&mut self, _event_loop: &ActiveEventLoop, _cause: StartCause) {
55+
if self.ui_frame_buffer.is_none() {
56+
self.cef_context.work();
57+
}
4458
if let Some(schedule) = self.cef_schedule
4559
&& schedule < Instant::now()
4660
{
@@ -50,32 +64,31 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
5064
}
5165

5266
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
53-
self.window_state
54-
.with(|s| {
55-
if let WindowState { width: Some(w), height: Some(h), .. } = s {
56-
let window = Arc::new(
57-
event_loop
58-
.create_window(
59-
Window::default_attributes()
60-
.with_title("CEF Offscreen Rendering")
61-
.with_inner_size(winit::dpi::LogicalSize::new(*w as u32, *h as u32)),
62-
)
63-
.unwrap(),
64-
);
65-
let graphics_state = pollster::block_on(GraphicsState::new(window.clone()));
67+
let window = Arc::new(
68+
event_loop
69+
.create_window(
70+
Window::default_attributes()
71+
.with_title("CEF Offscreen Rendering")
72+
.with_inner_size(winit::dpi::LogicalSize::new(1200, 800)),
73+
)
74+
.unwrap(),
75+
);
76+
let graphics_state = pollster::block_on(GraphicsState::new(window.clone()));
6677

67-
self.window = Some(window.clone());
68-
s.graphics_state = Some(graphics_state);
78+
self.window = Some(window);
79+
self.graphics_state = Some(graphics_state);
6980

70-
tracing::info!("Winit window created and ready");
71-
}
72-
})
73-
.unwrap();
81+
tracing::info!("Winit window created and ready");
7482
}
7583

7684
fn user_event(&mut self, _: &ActiveEventLoop, event: CustomEvent) {
7785
match event {
78-
CustomEvent::UiUpdate => {
86+
CustomEvent::UiUpdate(frame_buffer) => {
87+
if let Some(graphics_state) = self.graphics_state.as_mut() {
88+
graphics_state.update_texture(&frame_buffer);
89+
self.ui_dirty = true;
90+
}
91+
self.ui_frame_buffer = Some(frame_buffer);
7992
if let Some(window) = &self.window {
8093
window.request_redraw();
8194
}
@@ -94,58 +107,33 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
94107
tracing::info!("The close button was pressed; stopping");
95108
event_loop.exit();
96109
}
97-
WindowEvent::Resized(physical_size) => {
98-
self.window_state
99-
.with(|s| {
100-
let width = physical_size.width as usize;
101-
let height = physical_size.height as usize;
102-
s.width = Some(width);
103-
s.height = Some(height);
104-
if let Some(graphics_state) = &mut s.graphics_state {
105-
graphics_state.resize(width, height);
106-
}
107-
})
108-
.unwrap();
110+
WindowEvent::Resized(PhysicalSize { width, height }) => {
111+
let _ = self.window_size_sender.send(WindowSize::new(width as usize, height as usize));
109112
self.cef_context.notify_of_resize();
110113
}
111114

112115
WindowEvent::RedrawRequested => {
113-
self.cef_context.work();
116+
let Some(ref mut graphics_state) = self.graphics_state else { return };
117+
// Only rerender once we have a new ui texture to display
118+
if self.ui_dirty {
119+
self.ui_dirty = false;
114120

115-
self.window_state
116-
.with(|s| {
117-
if let WindowState {
118-
width: Some(width),
119-
height: Some(height),
120-
graphics_state: Some(graphics_state),
121-
ui_frame_buffer: ui_fb,
122-
..
123-
} = s
124-
{
125-
if let Some(fb) = &*ui_fb {
126-
graphics_state.update_texture(fb);
127-
if fb.width() != *width && fb.height() != *height {
128-
graphics_state.resize(*width, *height);
129-
}
130-
} else if let Some(window) = &self.window {
131-
window.request_redraw();
132-
}
133-
134-
match graphics_state.render() {
135-
Ok(_) => {}
136-
Err(wgpu::SurfaceError::Lost) => {
137-
graphics_state.resize(*width, *height);
138-
}
139-
Err(wgpu::SurfaceError::OutOfMemory) => {
140-
event_loop.exit();
141-
}
142-
Err(e) => tracing::error!("{:?}", e),
143-
}
121+
match graphics_state.render() {
122+
Ok(_) => {}
123+
Err(wgpu::SurfaceError::Lost) => {
124+
tracing::warn!("lost surface");
125+
}
126+
Err(wgpu::SurfaceError::OutOfMemory) => {
127+
event_loop.exit();
144128
}
145-
})
146-
.unwrap();
129+
Err(e) => tracing::error!("{:?}", e),
130+
}
131+
}
147132
}
148133
_ => {}
149134
}
135+
136+
// Notify cef of possible input events
137+
self.cef_context.work();
150138
}
151139
}

desktop/src/cef.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ pub(crate) use context::{Context, InitError, Initialized, Setup, SetupError};
1010

1111
pub(crate) trait CefEventHandler: Clone {
1212
fn window_size(&self) -> WindowSize;
13-
fn draw(&self, frame_buffer: FrameBuffer) -> bool;
13+
fn draw(&self, frame_buffer: FrameBuffer);
1414
/// Scheudule the main event loop to run the cef event loop after the timeout
1515
/// [`_cef_browser_process_handler_t::on_schedule_message_pump_work`] for more documentation.
1616
fn schedule_cef_message_loop_work(&self, scheduled_time: Instant);
1717
}
1818

19-
#[derive(Clone)]
19+
#[derive(Clone, Copy)]
2020
pub(crate) struct WindowSize {
2121
pub(crate) width: usize,
2222
pub(crate) height: usize,

desktop/src/cef/internal/app.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub(crate) struct AppImpl<H: CefEventHandler> {
1111
object: *mut RcImpl<_cef_app_t, Self>,
1212
event_handler: H,
1313
}
14-
impl<H: CefEventHandler> AppImpl<H> {
14+
impl<H: CefEventHandler + Clone> AppImpl<H> {
1515
pub(crate) fn new(event_handler: H) -> Self {
1616
Self {
1717
object: std::ptr::null_mut(),
@@ -20,7 +20,7 @@ impl<H: CefEventHandler> AppImpl<H> {
2020
}
2121
}
2222

23-
impl<H: CefEventHandler> ImplApp for AppImpl<H> {
23+
impl<H: CefEventHandler + Clone> ImplApp for AppImpl<H> {
2424
fn browser_process_handler(&self) -> Option<BrowserProcessHandler> {
2525
Some(BrowserProcessHandler::new(BrowserProcessHandlerImpl::new(self.event_handler.clone())))
2626
}
@@ -34,7 +34,7 @@ impl<H: CefEventHandler> ImplApp for AppImpl<H> {
3434
}
3535
}
3636

37-
impl<H: CefEventHandler> Clone for AppImpl<H> {
37+
impl<H: CefEventHandler + Clone> Clone for AppImpl<H> {
3838
fn clone(&self) -> Self {
3939
unsafe {
4040
let rc_impl = &mut *self.object;
@@ -54,7 +54,7 @@ impl<H: CefEventHandler> Rc for AppImpl<H> {
5454
}
5555
}
5656
}
57-
impl<H: CefEventHandler> WrapApp for AppImpl<H> {
57+
impl<H: CefEventHandler + Clone> WrapApp for AppImpl<H> {
5858
fn wrap_rc(&mut self, object: *mut RcImpl<_cef_app_t, Self>) {
5959
self.object = object;
6060
}

desktop/src/cef/internal/browser_process_handler.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl<H: CefEventHandler> BrowserProcessHandlerImpl<H> {
2020
}
2121
}
2222

23-
impl<H: CefEventHandler> ImplBrowserProcessHandler for BrowserProcessHandlerImpl<H> {
23+
impl<H: CefEventHandler + Clone> ImplBrowserProcessHandler for BrowserProcessHandlerImpl<H> {
2424
fn on_context_initialized(&self) {
2525
cef::register_scheme_handler_factory(Some(&CefString::from(GRAPHITE_SCHEME)), None, Some(&mut SchemeHandlerFactory::new(GraphiteSchemeHandlerFactory::new())));
2626
}
@@ -34,7 +34,7 @@ impl<H: CefEventHandler> ImplBrowserProcessHandler for BrowserProcessHandlerImpl
3434
}
3535
}
3636

37-
impl<H: CefEventHandler> Clone for BrowserProcessHandlerImpl<H> {
37+
impl<H: CefEventHandler + Clone> Clone for BrowserProcessHandlerImpl<H> {
3838
fn clone(&self) -> Self {
3939
unsafe {
4040
let rc_impl = &mut *self.object;
@@ -54,7 +54,7 @@ impl<H: CefEventHandler> Rc for BrowserProcessHandlerImpl<H> {
5454
}
5555
}
5656
}
57-
impl<H: CefEventHandler> WrapBrowserProcessHandler for BrowserProcessHandlerImpl<H> {
57+
impl<H: CefEventHandler + Clone> WrapBrowserProcessHandler for BrowserProcessHandlerImpl<H> {
5858
fn wrap_rc(&mut self, object: *mut RcImpl<_cef_browser_process_handler_t, Self>) {
5959
self.object = object;
6060
}

desktop/src/cef/internal/render_handler.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cef::rc::{Rc, RcImpl};
22
use cef::sys::{_cef_render_handler_t, cef_base_ref_counted_t};
3-
use cef::{Browser, ImplBrowser, ImplBrowserHost, ImplRenderHandler, PaintElementType, Rect, WrapRenderHandler};
3+
use cef::{Browser, ImplRenderHandler, PaintElementType, Rect, WrapRenderHandler};
44

55
use crate::FrameBuffer;
66
use crate::cef::CefEventHandler;
@@ -32,7 +32,7 @@ impl<H: CefEventHandler> ImplRenderHandler for RenderHandlerImpl<H> {
3232

3333
fn on_paint(
3434
&self,
35-
browser: Option<&mut Browser>,
35+
_browser: Option<&mut Browser>,
3636
_type_: PaintElementType,
3737
_dirty_rect_count: usize,
3838
_dirty_rects: Option<&Rect>,
@@ -44,12 +44,7 @@ impl<H: CefEventHandler> ImplRenderHandler for RenderHandlerImpl<H> {
4444
let buffer_slice = unsafe { std::slice::from_raw_parts(buffer, buffer_size) };
4545
let frame_buffer = FrameBuffer::new(buffer_slice.to_vec(), width as usize, height as usize).expect("Failed to create frame buffer");
4646

47-
let draw_successful = self.event_handler.draw(frame_buffer);
48-
if !draw_successful {
49-
if let Some(browser) = browser {
50-
browser.host().unwrap().was_resized();
51-
}
52-
}
47+
self.event_handler.draw(frame_buffer)
5348
}
5449

5550
fn get_raw(&self) -> *mut _cef_render_handler_t {

0 commit comments

Comments
 (0)