Skip to content

Commit f184e4a

Browse files
authored
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
1 parent 9f4f368 commit f184e4a

File tree

7 files changed

+165
-247
lines changed

7 files changed

+165
-247
lines changed

desktop/src/app.rs

Lines changed: 63 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,53 @@
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;
89
use winit::application::ApplicationHandler;
10+
use winit::dpi::PhysicalSize;
911
use winit::event::StartCause;
1012
use winit::event::WindowEvent;
1113
use winit::event_loop::ActiveEventLoop;
1214
use winit::event_loop::ControlFlow;
15+
use winit::event_loop::EventLoopProxy;
1316
use winit::window::Window;
1417
use winit::window::WindowId;
1518

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_frame_buffer: Option<FrameBuffer>,
26+
window_size_sender: Sender<WindowSize>,
27+
_viewport_frame_buffer: Option<FrameBuffer>,
28+
graphics_state: Option<GraphicsState>,
29+
event_loop_proxy: EventLoopProxy<CustomEvent>,
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>, event_loop_proxy: EventLoopProxy<CustomEvent>) -> 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+
graphics_state: None,
41+
window_size_sender,
42+
event_loop_proxy,
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+
// Set a timeout in case we miss any cef schedule requests
50+
let timeout = Instant::now() + Duration::from_millis(100);
3951
let wait_until = timeout.min(self.cef_schedule.unwrap_or(timeout));
4052
event_loop.set_control_flow(ControlFlow::WaitUntil(wait_until));
4153
}
@@ -50,37 +62,42 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
5062
}
5163

5264
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()));
65+
let window = Arc::new(
66+
event_loop
67+
.create_window(
68+
Window::default_attributes()
69+
.with_title("CEF Offscreen Rendering")
70+
.with_inner_size(winit::dpi::LogicalSize::new(1200, 800)),
71+
)
72+
.unwrap(),
73+
);
74+
let graphics_state = pollster::block_on(GraphicsState::new(window.clone()));
6675

67-
self.window = Some(window.clone());
68-
s.graphics_state = Some(graphics_state);
76+
self.window = Some(window);
77+
self.graphics_state = Some(graphics_state);
6978

70-
tracing::info!("Winit window created and ready");
71-
}
72-
})
73-
.unwrap();
79+
tracing::info!("Winit window created and ready");
7480
}
7581

7682
fn user_event(&mut self, _: &ActiveEventLoop, event: CustomEvent) {
7783
match event {
78-
CustomEvent::UiUpdate => {
84+
CustomEvent::UiUpdate(frame_buffer) => {
85+
if let Some(graphics_state) = self.graphics_state.as_mut() {
86+
graphics_state.update_texture(&frame_buffer);
87+
}
88+
self.ui_frame_buffer = Some(frame_buffer);
7989
if let Some(window) = &self.window {
8090
window.request_redraw();
8191
}
8292
}
8393
CustomEvent::ScheduleBrowserWork(instant) => {
94+
if let Some(graphics_state) = self.graphics_state.as_mut()
95+
&& let Some(frame_buffer) = &self.ui_frame_buffer
96+
&& graphics_state.ui_texture_outdated(frame_buffer)
97+
{
98+
self.cef_context.work();
99+
let _ = self.event_loop_proxy.send_event(CustomEvent::ScheduleBrowserWork(Instant::now() + Duration::from_millis(1)));
100+
}
84101
self.cef_schedule = Some(instant);
85102
}
86103
}
@@ -94,58 +111,33 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
94111
tracing::info!("The close button was pressed; stopping");
95112
event_loop.exit();
96113
}
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();
114+
WindowEvent::Resized(PhysicalSize { width, height }) => {
115+
let _ = self.window_size_sender.send(WindowSize::new(width as usize, height as usize));
116+
if let Some(ref mut graphics_state) = self.graphics_state {
117+
graphics_state.resize(width, height);
118+
}
109119
self.cef_context.notify_of_resize();
110120
}
111121

112122
WindowEvent::RedrawRequested => {
113-
self.cef_context.work();
123+
let Some(ref mut graphics_state) = self.graphics_state else { return };
124+
// Only rerender once we have a new ui texture to display
114125

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-
}
144-
}
145-
})
146-
.unwrap();
126+
match graphics_state.render() {
127+
Ok(_) => {}
128+
Err(wgpu::SurfaceError::Lost) => {
129+
tracing::warn!("lost surface");
130+
}
131+
Err(wgpu::SurfaceError::OutOfMemory) => {
132+
event_loop.exit();
133+
}
134+
Err(e) => tracing::error!("{:?}", e),
135+
}
147136
}
148137
_ => {}
149138
}
139+
140+
// Notify cef of possible input events
141+
self.cef_context.work();
150142
}
151143
}

desktop/src/cef.rs

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
use crate::FrameBuffer;
2-
use std::time::Instant;
1+
use crate::{CustomEvent, FrameBuffer};
2+
use std::{
3+
sync::{Arc, Mutex, mpsc::Receiver},
4+
time::Instant,
5+
};
36

47
mod context;
58
mod dirs;
@@ -8,16 +11,17 @@ mod internal;
811
mod scheme_handler;
912

1013
pub(crate) use context::{Context, InitError, Initialized, Setup, SetupError};
14+
use winit::event_loop::EventLoopProxy;
1115

1216
pub(crate) trait CefEventHandler: Clone {
1317
fn window_size(&self) -> WindowSize;
14-
fn draw(&self, frame_buffer: FrameBuffer) -> bool;
18+
fn draw(&self, frame_buffer: FrameBuffer);
1519
/// Scheudule the main event loop to run the cef event loop after the timeout
1620
/// [`_cef_browser_process_handler_t::on_schedule_message_pump_work`] for more documentation.
1721
fn schedule_cef_message_loop_work(&self, scheduled_time: Instant);
1822
}
1923

20-
#[derive(Clone)]
24+
#[derive(Clone, Copy)]
2125
pub(crate) struct WindowSize {
2226
pub(crate) width: usize,
2327
pub(crate) height: usize,
@@ -28,3 +32,50 @@ impl WindowSize {
2832
Self { width, height }
2933
}
3034
}
35+
36+
#[derive(Clone)]
37+
pub(crate) struct CefHandler {
38+
window_size_receiver: Arc<Mutex<WindowSizeReceiver>>,
39+
event_loop_proxy: EventLoopProxy<CustomEvent>,
40+
}
41+
struct WindowSizeReceiver {
42+
receiver: Receiver<WindowSize>,
43+
window_size: WindowSize,
44+
}
45+
impl WindowSizeReceiver {
46+
fn new(window_size_receiver: Receiver<WindowSize>) -> Self {
47+
Self {
48+
window_size: WindowSize { width: 1, height: 1 },
49+
receiver: window_size_receiver,
50+
}
51+
}
52+
}
53+
impl CefHandler {
54+
pub(crate) fn new(window_size_receiver: Receiver<WindowSize>, event_loop_proxy: EventLoopProxy<CustomEvent>) -> Self {
55+
Self {
56+
window_size_receiver: Arc::new(Mutex::new(WindowSizeReceiver::new(window_size_receiver))),
57+
event_loop_proxy,
58+
}
59+
}
60+
}
61+
62+
impl CefEventHandler for CefHandler {
63+
fn window_size(&self) -> WindowSize {
64+
let Ok(mut guard) = self.window_size_receiver.lock() else {
65+
tracing::error!("Failed to lock window_size_receiver");
66+
return WindowSize::new(1, 1);
67+
};
68+
let WindowSizeReceiver { receiver, window_size } = &mut *guard;
69+
for new_window_size in receiver.try_iter() {
70+
*window_size = new_window_size;
71+
}
72+
*window_size
73+
}
74+
fn draw(&self, frame_buffer: FrameBuffer) {
75+
let _ = self.event_loop_proxy.send_event(CustomEvent::UiUpdate(frame_buffer));
76+
}
77+
78+
fn schedule_cef_message_loop_work(&self, scheduled_time: std::time::Instant) {
79+
let _ = self.event_loop_proxy.send_event(CustomEvent::ScheduleBrowserWork(scheduled_time));
80+
}
81+
}

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)