Skip to content

Commit 1839f72

Browse files
committed
Schedule self render if texture is outdated
1 parent 61c5ab6 commit 1839f72

4 files changed

Lines changed: 44 additions & 33 deletions

File tree

desktop/src/app.rs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use winit::event::StartCause;
1212
use winit::event::WindowEvent;
1313
use winit::event_loop::ActiveEventLoop;
1414
use winit::event_loop::ControlFlow;
15+
use winit::event_loop::EventLoopProxy;
1516
use winit::window::Window;
1617
use winit::window::WindowId;
1718

@@ -21,35 +22,24 @@ pub(crate) struct WinitApp {
2122
pub(crate) cef_context: cef::Context<cef::Initialized>,
2223
pub(crate) window: Option<Arc<Window>>,
2324
cef_schedule: Option<Instant>,
24-
ui_dirty: bool,
2525
ui_frame_buffer: Option<FrameBuffer>,
2626
window_size_sender: Sender<WindowSize>,
2727
_viewport_frame_buffer: Option<FrameBuffer>,
2828
graphics_state: Option<GraphicsState>,
29+
event_loop_proxy: EventLoopProxy<CustomEvent>,
2930
}
3031

3132
impl WinitApp {
32-
pub(crate) fn new(cef_context: cef::Context<cef::Initialized>, window_size_sender: Sender<WindowSize>) -> Self {
33+
pub(crate) fn new(cef_context: cef::Context<cef::Initialized>, window_size_sender: Sender<WindowSize>, event_loop_proxy: EventLoopProxy<CustomEvent>) -> Self {
3334
Self {
3435
cef_context,
3536
window: None,
3637
cef_schedule: Some(Instant::now()),
3738
_viewport_frame_buffer: None,
3839
ui_frame_buffer: None,
39-
ui_dirty: false,
4040
graphics_state: None,
4141
window_size_sender,
42-
}
43-
}
44-
fn run_cef(&mut self) {
45-
if self.ui_frame_buffer.is_none() {
46-
self.cef_context.work();
47-
}
48-
if let Some(schedule) = self.cef_schedule
49-
&& schedule < Instant::now()
50-
{
51-
self.cef_schedule = None;
52-
self.cef_context.work();
42+
event_loop_proxy,
5343
}
5444
}
5545
}
@@ -62,7 +52,15 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
6252
}
6353

6454
fn new_events(&mut self, _event_loop: &ActiveEventLoop, _cause: StartCause) {
65-
self.run_cef();
55+
if self.ui_frame_buffer.is_none() {
56+
self.cef_context.work();
57+
}
58+
if let Some(schedule) = self.cef_schedule
59+
&& schedule < Instant::now()
60+
{
61+
self.cef_schedule = None;
62+
self.cef_context.work();
63+
}
6664
}
6765

6866
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
@@ -84,26 +82,30 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
8482
}
8583

8684
fn user_event(&mut self, _: &ActiveEventLoop, event: CustomEvent) {
87-
self.run_cef();
8885
match event {
8986
CustomEvent::UiUpdate(frame_buffer) => {
9087
if let Some(graphics_state) = self.graphics_state.as_mut() {
9188
graphics_state.update_texture(&frame_buffer);
92-
self.ui_dirty = true;
9389
}
9490
self.ui_frame_buffer = Some(frame_buffer);
9591
if let Some(window) = &self.window {
9692
window.request_redraw();
9793
}
9894
}
9995
CustomEvent::ScheduleBrowserWork(instant) => {
96+
if let Some(graphics_state) = self.graphics_state.as_mut()
97+
&& let Some(frame_buffer) = &self.ui_frame_buffer
98+
&& graphics_state.ui_texture_outdated(frame_buffer)
99+
{
100+
self.cef_context.work();
101+
let _ = self.event_loop_proxy.send_event(CustomEvent::ScheduleBrowserWork(Instant::now() + Duration::from_millis(1)));
102+
}
100103
self.cef_schedule = Some(instant);
101104
}
102105
}
103106
}
104107

105108
fn window_event(&mut self, event_loop: &ActiveEventLoop, _window_id: WindowId, event: WindowEvent) {
106-
self.run_cef();
107109
let Some(event) = self.cef_context.handle_window_event(event) else { return };
108110

109111
match event {
@@ -113,25 +115,25 @@ impl ApplicationHandler<CustomEvent> for WinitApp {
113115
}
114116
WindowEvent::Resized(PhysicalSize { width, height }) => {
115117
let _ = self.window_size_sender.send(WindowSize::new(width as usize, height as usize));
118+
if let Some(ref mut graphics_state) = self.graphics_state {
119+
graphics_state.resize(width, height);
120+
}
116121
self.cef_context.notify_of_resize();
117122
}
118123

119124
WindowEvent::RedrawRequested => {
120125
let Some(ref mut graphics_state) = self.graphics_state else { return };
121126
// Only rerender once we have a new ui texture to display
122-
if self.ui_dirty {
123-
self.ui_dirty = false;
124127

125-
match graphics_state.render() {
126-
Ok(_) => {}
127-
Err(wgpu::SurfaceError::Lost) => {
128-
tracing::warn!("lost surface");
129-
}
130-
Err(wgpu::SurfaceError::OutOfMemory) => {
131-
event_loop.exit();
132-
}
133-
Err(e) => tracing::error!("{:?}", e),
128+
match graphics_state.render() {
129+
Ok(_) => {}
130+
Err(wgpu::SurfaceError::Lost) => {
131+
tracing::warn!("lost surface");
132+
}
133+
Err(wgpu::SurfaceError::OutOfMemory) => {
134+
event_loop.exit();
134135
}
136+
Err(e) => tracing::error!("{:?}", e),
135137
}
136138
}
137139
_ => {}

desktop/src/cef.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,5 @@ impl CefEventHandler for CefHandler {
7676

7777
fn schedule_cef_message_loop_work(&self, scheduled_time: std::time::Instant) {
7878
let _ = self.event_loop_proxy.send_event(CustomEvent::ScheduleBrowserWork(scheduled_time));
79-
println!("browser scheduled work");
8079
}
8180
}

desktop/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn main() {
4646

4747
tracing::info!("Cef initialized successfully");
4848

49-
let mut winit_app = WinitApp::new(cef_context, send);
49+
let mut winit_app = WinitApp::new(cef_context, send, event_loop.create_proxy());
5050

5151
event_loop.run_app(&mut winit_app).unwrap();
5252
}

desktop/src/render.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,13 @@ impl GraphicsState {
223223
graphics_state
224224
}
225225

226-
pub(crate) fn update_texture(&mut self, frame_buffer: &FrameBuffer) {
227-
let data = frame_buffer.buffer();
226+
pub(crate) fn ui_texture_outdated(&self, frame_buffer: &FrameBuffer) -> bool {
228227
let width = frame_buffer.width() as u32;
229228
let height = frame_buffer.height() as u32;
230229

230+
self.config.width != width || self.config.height != height
231+
}
232+
pub(crate) fn resize(&mut self, width: u32, height: u32) {
231233
if width > 0 && height > 0 && (self.config.width != width || self.config.height != height) {
232234
self.config.width = width;
233235
self.config.height = height;
@@ -248,6 +250,14 @@ impl GraphicsState {
248250
});
249251
self.texture = Some(texture);
250252
}
253+
}
254+
255+
pub(crate) fn update_texture(&mut self, frame_buffer: &FrameBuffer) {
256+
let data = frame_buffer.buffer();
257+
let width = frame_buffer.width() as u32;
258+
let height = frame_buffer.height() as u32;
259+
260+
self.resize(width, height);
251261

252262
let Some(ref texture) = self.texture else { return };
253263

0 commit comments

Comments
 (0)