Skip to content

Commit

Permalink
Make frame.info.cpu_usage take more into account
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jan 29, 2024
1 parent 1d1b07c commit 7ac72d2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
5 changes: 4 additions & 1 deletion crates/eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,10 @@ pub struct IntegrationInfo {
/// `None` means "don't know".
pub system_theme: Option<Theme>,

/// 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<f32>,
}
Expand Down
6 changes: 3 additions & 3 deletions crates/eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 5 additions & 3 deletions crates/eframe/src/native/glow_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions crates/eframe/src/native/wgpu_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -628,8 +628,6 @@ impl WgpuWinitRunning {
return EventResult::Wait;
};

integration.post_update();

let FullOutput {
platform_output,
textures_delta,
Expand All @@ -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,
Expand Down

0 comments on commit 7ac72d2

Please sign in to comment.