From a91475032c06c9659877f35e5461297299ffbb48 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 13 Sep 2023 08:28:54 +0200 Subject: [PATCH] More profile scopes --- crates/eframe/src/native/run.rs | 68 ++++++++++++++++++++++----------- crates/egui_glow/src/painter.rs | 14 +++++-- 2 files changed, 55 insertions(+), 27 deletions(-) diff --git a/crates/eframe/src/native/run.rs b/crates/eframe/src/native/run.rs index ba1aba14997..226db22c7f3 100644 --- a/crates/eframe/src/native/run.rs +++ b/crates/eframe/src/native/run.rs @@ -104,7 +104,10 @@ fn create_event_loop_builder( fn create_event_loop(native_options: &mut epi::NativeOptions) -> EventLoop { crate::profile_function!(); - create_event_loop_builder(native_options).build() + let mut builder = create_event_loop_builder(native_options); + + crate::profile_scope!("EventLoopBuilder::build"); + builder.build() } /// Access a thread-local event loop. @@ -142,7 +145,7 @@ fn run_and_return( let mut returned_result = Ok(()); event_loop.run_return(|event, event_loop, control_flow| { - crate::profile_scope!("on_winit_event", short_event_description(&event)); + crate::profile_scope!("winit_event", short_event_description(&event)); let event_result = match &event { winit::event::Event::LoopDestroyed => { @@ -273,7 +276,7 @@ fn run_and_exit(event_loop: EventLoop, mut winit_app: impl WinitApp + let mut next_repaint_time = Instant::now(); event_loop.run(move |event, event_loop, control_flow| { - crate::profile_scope!("on_winit_event", short_event_description(&event)); + crate::profile_scope!("winit_event", short_event_description(&event)); let event_result = match event { winit::event::Event::LoopDestroyed => { @@ -429,6 +432,8 @@ mod glow_integration { native_options: &epi::NativeOptions, event_loop: &EventLoopWindowTarget, ) -> Result { + crate::profile_function!(); + use glutin::prelude::*; // convert native options to glutin options let hardware_acceleration = match native_options.hardware_acceleration { @@ -469,26 +474,35 @@ mod glow_integration { "trying to create glutin Display with config: {:?}", &config_template_builder ); - // create gl display. this may probably create a window too on most platforms. definitely on `MS windows`. never on android. - let (window, gl_config) = glutin_winit::DisplayBuilder::new() + + // Create GL display. This may probably create a window too on most platforms. Definitely on `MS windows`. Never on Android. + let display_builder = glutin_winit::DisplayBuilder::new() // we might want to expose this option to users in the future. maybe using an env var or using native_options. .with_preference(glutin_winit::ApiPrefence::FallbackEgl) // https://github.com/emilk/egui/issues/2520#issuecomment-1367841150 - .with_window_builder(Some(winit_window_builder.clone())) - .build( - event_loop, - config_template_builder.clone(), - |mut config_iterator| { - let config = config_iterator.next().expect( + .with_window_builder(Some(winit_window_builder.clone())); + + let (window, gl_config) = { + crate::profile_scope!("DisplayBuilder::build"); + + display_builder + .build( + event_loop, + config_template_builder.clone(), + |mut config_iterator| { + let config = config_iterator.next().expect( "failed to find a matching configuration for creating glutin config", ); - log::debug!( - "using the first config from config picker closure. config: {:?}", - &config - ); - config - }, - ) - .map_err(|e| crate::Error::NoGlutinConfigs(config_template_builder.build(), e))?; + log::debug!( + "using the first config from config picker closure. config: {:?}", + &config + ); + config + }, + ) + .map_err(|e| { + crate::Error::NoGlutinConfigs(config_template_builder.build(), e) + })? + }; let gl_display = gl_config.display(); log::debug!( @@ -508,10 +522,15 @@ mod glow_integration { let fallback_context_attributes = glutin::context::ContextAttributesBuilder::new() .with_context_api(glutin::context::ContextApi::Gles(None)) .build(raw_window_handle); - let gl_context = match gl_config - .display() - .create_context(&gl_config, &context_attributes) - { + + let gl_context_result = { + crate::profile_scope!("create_context"); + gl_config + .display() + .create_context(&gl_config, &context_attributes) + }; + + let gl_context = match gl_context_result { Ok(it) => it, Err(err) => { log::warn!("failed to create context using default context attributes {context_attributes:?} due to error: {err}"); @@ -703,6 +722,7 @@ mod glow_integration { } let gl = unsafe { + crate::profile_scope!("glow::Context::from_loader_function"); glow::Context::from_loader_function(|s| { let s = std::ffi::CString::new(s) .expect("failed to construct C string from string for gl proc address"); @@ -1204,6 +1224,7 @@ mod wgpu_integration { window: winit::window::Window, ) -> std::result::Result<(), egui_wgpu::WgpuError> { crate::profile_function!(); + #[allow(unsafe_code, unused_mut, unused_unsafe)] let mut painter = egui_wgpu::winit::Painter::new( self.native_options.wgpu_options.clone(), @@ -1582,6 +1603,7 @@ fn short_event_description(event: &winit::event::Event<'_, UserEvent>) -> &'stat Event::LoopDestroyed => "Event::LoopDestroyed", Event::UserEvent(user_event) => match user_event { UserEvent::RequestRepaint { .. } => "UserEvent::RequestRepaint", + #[cfg(feature = "accesskit")] UserEvent::AccessKitActionRequest(_) => "UserEvent::AccessKitActionRequest", }, Event::DeviceEvent { event, .. } => match event { diff --git a/crates/egui_glow/src/painter.rs b/crates/egui_glow/src/painter.rs index e6ac79ecaf2..64a8be0be6a 100644 --- a/crates/egui_glow/src/painter.rs +++ b/crates/egui_glow/src/painter.rs @@ -494,10 +494,13 @@ impl Painter { "Mismatch between texture size and texel count" ); - let data: Vec = image - .srgba_pixels(None) - .flat_map(|a| a.to_array()) - .collect(); + let data: Vec = { + crate::profile_scope!("font -> sRGBA"); + image + .srgba_pixels(None) + .flat_map(|a| a.to_array()) + .collect() + }; self.upload_texture_srgb(delta.pos, image.size, delta.options, &data); } @@ -511,6 +514,7 @@ impl Painter { options: egui::TextureOptions, data: &[u8], ) { + crate::profile_function!(); assert_eq!(data.len(), w * h * 4); assert!( w <= self.max_texture_side && h <= self.max_texture_side, @@ -561,6 +565,7 @@ impl Painter { let level = 0; if let Some([x, y]) = pos { + crate::profile_scope!("gl.tex_sub_image_2d"); self.gl.tex_sub_image_2d( glow::TEXTURE_2D, level, @@ -575,6 +580,7 @@ impl Painter { check_for_gl_error!(&self.gl, "tex_sub_image_2d"); } else { let border = 0; + crate::profile_scope!("gl.tex_image_2d"); self.gl.tex_image_2d( glow::TEXTURE_2D, level,