From 7ac72d23d88c86a31deaeada5b390ea508474077 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 29 Jan 2024 16:19:39 +0100 Subject: [PATCH] Make `frame.info.cpu_usage` take more into account --- crates/eframe/src/epi.rs | 5 ++++- crates/eframe/src/native/epi_integration.rs | 6 +++--- crates/eframe/src/native/glow_integration.rs | 8 +++++--- crates/eframe/src/native/wgpu_integration.rs | 8 ++++---- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/crates/eframe/src/epi.rs b/crates/eframe/src/epi.rs index d34b1aecae50..9a20a757e6d9 100644 --- a/crates/eframe/src/epi.rs +++ b/crates/eframe/src/epi.rs @@ -757,7 +757,10 @@ pub struct IntegrationInfo { /// `None` means "don't know". pub system_theme: Option, - /// Seconds of cpu usage (in seconds) of UI code on the previous frame. + /// Seconds of cpu usage (in seconds) on the previous frame. + /// + /// This includes [`App::update`] as well as rendering (except for vsync waiting). + /// /// `None` if this is the first frame. pub cpu_usage: Option, } diff --git a/crates/eframe/src/native/epi_integration.rs b/crates/eframe/src/native/epi_integration.rs index e91278a39e80..e00e3b23a274 100644 --- a/crates/eframe/src/native/epi_integration.rs +++ b/crates/eframe/src/native/epi_integration.rs @@ -258,9 +258,9 @@ impl EpiIntegration { egui_winit.on_window_event(window, event) } - pub fn pre_update(&mut self) { + pub fn start_frame_timer(&mut self) { self.frame_start = Instant::now(); - self.app_icon_setter.update(); + self.app_icon_setter.update(); // TODO(emilk): this is probably not the right place for this call } /// Run user code - this can create immediate viewports, so hold no locks over this! @@ -304,7 +304,7 @@ impl EpiIntegration { std::mem::take(&mut self.pending_full_output) } - pub fn post_update(&mut self) { + pub fn end_frame_timer(&mut self) { let frame_time = self.frame_start.elapsed().as_secs_f64() as f32; self.frame.info.cpu_usage = Some(frame_time); } diff --git a/crates/eframe/src/native/glow_integration.rs b/crates/eframe/src/native/glow_integration.rs index ea193ee87528..c21158a823b0 100644 --- a/crates/eframe/src/native/glow_integration.rs +++ b/crates/eframe/src/native/glow_integration.rs @@ -493,6 +493,8 @@ impl GlowWinitRunning { #[cfg(feature = "puffin")] puffin::GlobalProfiler::lock().new_frame(); + self.integration.start_frame_timer(); + { let glutin = self.glutin.borrow(); let viewport = &glutin.viewports[&viewport_id]; @@ -522,8 +524,6 @@ impl GlowWinitRunning { let mut raw_input = egui_winit.take_egui_input(window); let viewport_ui_cb = viewport.viewport_ui_cb.clone(); - self.integration.pre_update(); - raw_input.time = Some(self.integration.beginning.elapsed().as_secs_f64()); raw_input.viewports = glutin .viewports @@ -604,7 +604,6 @@ impl GlowWinitRunning { let gl_surface = viewport.gl_surface.as_ref().unwrap(); let egui_winit = viewport.egui_winit.as_mut().unwrap(); - integration.post_update(); egui_winit.handle_platform_output(window, platform_output); let clipped_primitives = integration.egui_ctx.tessellate(shapes, pixels_per_point); @@ -640,7 +639,10 @@ impl GlowWinitRunning { integration.post_rendering(window); } + integration.end_frame_timer(); // Must be called before vsync. + { + // vsync crate::profile_scope!("swap_buffers"); if let Err(err) = gl_surface.swap_buffers( current_gl_context diff --git a/crates/eframe/src/native/wgpu_integration.rs b/crates/eframe/src/native/wgpu_integration.rs index 56847b92124b..d53362886652 100644 --- a/crates/eframe/src/native/wgpu_integration.rs +++ b/crates/eframe/src/native/wgpu_integration.rs @@ -528,6 +528,8 @@ impl WgpuWinitRunning { shared, } = self; + integration.start_frame_timer(); + let (viewport_ui_cb, raw_input) = { crate::profile_scope!("Prepare"); let mut shared_lock = shared.borrow_mut(); @@ -584,8 +586,6 @@ impl WgpuWinitRunning { let egui_winit = egui_winit.as_mut().unwrap(); let mut raw_input = egui_winit.take_egui_input(window); - integration.pre_update(); - raw_input.time = Some(integration.beginning.elapsed().as_secs_f64()); raw_input.viewports = viewports .iter() @@ -628,8 +628,6 @@ impl WgpuWinitRunning { return EventResult::Wait; }; - integration.post_update(); - let FullOutput { platform_output, textures_delta, @@ -643,6 +641,8 @@ impl WgpuWinitRunning { { let clipped_primitives = egui_ctx.tessellate(shapes, pixels_per_point); + integration.end_frame_timer(); // TODO(emilk): include everything in rendering except for vsync + let screenshot_requested = std::mem::take(&mut viewport.screenshot_requested); let screenshot = painter.paint_and_update_textures( viewport_id,