1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 21:30:03 -04:00

Add opt-in support for the 'puffin' profiler in eframe (#1483)

This commit is contained in:
Emil Ernerfeldt
2022-04-13 11:06:13 +02:00
committed by GitHub
parent 973c3f22d1
commit 170b21b63e
20 changed files with 304 additions and 9 deletions

View File

@@ -11,6 +11,7 @@ fn create_display(
glutin::WindowedContext<glutin::PossiblyCurrent>,
glow::Context,
) {
crate::profile_function!();
let gl_window = unsafe {
glutin::ContextBuilder::new()
.with_depth_buffer(native_options.depth_buffer)
@@ -77,15 +78,20 @@ pub fn run(app_name: &str, native_options: &epi::NativeOptions, app_creator: epi
event_loop.run(move |event, _, control_flow| {
let mut redraw = || {
#[cfg(feature = "puffin")]
puffin::GlobalProfiler::lock().new_frame();
if !is_focused {
// On Mac, a minimized Window uses up all CPU: https://github.com/emilk/egui/issues/325
// We can't know if we are minimized: https://github.com/rust-windowing/winit/issues/208
// But we know if we are focused (in foreground). When minimized, we are not focused.
// However, a user may want an egui with an animation in the background,
// so we still need to repaint quite fast.
crate::profile_scope!("bg_sleep");
std::thread::sleep(std::time::Duration::from_millis(10));
}
crate::profile_scope!("frame");
let screen_size_in_pixels: [u32; 2] = gl_window.window().inner_size().into();
crate::painter::clear(&gl, screen_size_in_pixels, app.clear_color());
@@ -99,7 +105,10 @@ pub fn run(app_name: &str, native_options: &epi::NativeOptions, app_creator: epi
integration.handle_platform_output(gl_window.window(), platform_output);
let clipped_primitives = integration.egui_ctx.tessellate(shapes);
let clipped_primitives = {
crate::profile_scope!("tessellate");
integration.egui_ctx.tessellate(shapes)
};
painter.paint_and_update_textures(
screen_size_in_pixels,
@@ -108,7 +117,10 @@ pub fn run(app_name: &str, native_options: &epi::NativeOptions, app_creator: epi
&textures_delta,
);
gl_window.swap_buffers().unwrap();
{
crate::profile_scope!("swap_buffers");
gl_window.swap_buffers().unwrap();
}
{
*control_flow = if integration.should_quit() {

View File

@@ -85,3 +85,25 @@ pub fn check_for_gl_error_impl(gl: &glow::Context, file: &str, line: u32, contex
}
}
}
// ---------------------------------------------------------------------------
/// Profiling macro for feature "puffin"
#[doc(hidden)]
#[macro_export]
macro_rules! profile_function {
($($arg: tt)*) => {
#[cfg(feature = "puffin")]
puffin::profile_function!($($arg)*);
};
}
/// Profiling macro for feature "puffin"
#[doc(hidden)]
#[macro_export]
macro_rules! profile_scope {
($($arg: tt)*) => {
#[cfg(feature = "puffin")]
puffin::profile_scope!($($arg)*);
};
}

View File

@@ -96,6 +96,7 @@ impl Painter {
pp_fb_extent: Option<[i32; 2]>,
shader_prefix: &str,
) -> Result<Painter, String> {
crate::profile_function!();
check_for_gl_error!(&gl, "before Painter::new");
let max_texture_side = unsafe { gl.get_parameter_i32(glow::MAX_TEXTURE_SIZE) } as usize;
@@ -297,6 +298,7 @@ impl Painter {
clipped_primitives: &[egui::ClippedPrimitive],
textures_delta: &egui::TexturesDelta,
) {
crate::profile_function!();
for (id, image_delta) in &textures_delta.set {
self.set_texture(*id, image_delta);
}
@@ -333,6 +335,7 @@ impl Painter {
pixels_per_point: f32,
clipped_primitives: &[egui::ClippedPrimitive],
) {
crate::profile_function!();
self.assert_not_destroyed();
if let Some(ref mut post_process) = self.post_process {
@@ -355,6 +358,7 @@ impl Painter {
}
Primitive::Callback(callback) => {
if callback.rect.is_positive() {
crate::profile_scope!("callback");
// Transform callback rect to physical pixels:
let rect_min_x = pixels_per_point * callback.rect.min.x;
let rect_min_y = pixels_per_point * callback.rect.min.y;
@@ -456,6 +460,8 @@ impl Painter {
// ------------------------------------------------------------------------
pub fn set_texture(&mut self, tex_id: egui::TextureId, delta: &egui::epaint::ImageDelta) {
crate::profile_function!();
self.assert_not_destroyed();
let glow_texture = *self