Browse Source

optimization: don't compare font data each frame

pull/262/head
Emil Ernerfeldt 4 years ago
parent
commit
aeaa54aab1
  1. 29
      egui/src/context.rs
  2. 5
      egui/src/memory.rs

29
egui/src/context.rs

@ -407,7 +407,14 @@ impl Context {
/// Will become active at the start of the next frame.
pub fn set_fonts(&self, font_definitions: FontDefinitions) {
self.memory().options.font_definitions = font_definitions;
if let Some(current_fonts) = &self.fonts {
// NOTE: this comparison is expensive since it checks TTF data for equality
if current_fonts.definitions() == &font_definitions {
return; // no change - save us from reloading font textures
}
}
self.memory().new_font_definitions = Some(font_definitions);
}
/// The [`Style`] used by all subsequent windows, panels etc.
@ -531,21 +538,25 @@ impl Context {
self.input = input.begin_frame(new_raw_input);
self.frame_state.lock().begin_frame(&self.input);
let font_definitions = self.memory().options.font_definitions.clone();
{
// Load new fonts if required:
let new_font_definitions = self.memory().new_font_definitions.take();
let pixels_per_point = self.input.pixels_per_point();
let same_as_current = match &self.fonts {
None => false,
Some(fonts) => {
*fonts.definitions() == font_definitions
&& (fonts.pixels_per_point() - pixels_per_point).abs() < 1e-3
let pixels_per_point_changed = match &self.fonts {
None => true,
Some(current_fonts) => {
(current_fonts.pixels_per_point() - pixels_per_point).abs() > 1e-3
}
};
if !same_as_current {
if self.fonts.is_none() || new_font_definitions.is_some() || pixels_per_point_changed {
self.fonts = Some(Arc::new(Fonts::from_definitions(
pixels_per_point,
font_definitions,
new_font_definitions.unwrap_or_default(),
)));
}
}
// Ensure we register the background area so panels and background ui can catch clicks:
let screen_rect = self.input.screen_rect();

5
egui/src/memory.rs

@ -23,6 +23,9 @@ pub struct Memory {
/// new scale that will be applied at the start of the next frame
pub(crate) new_pixels_per_point: Option<f32>,
/// new fonts that will be applied at the start of the next frame
pub(crate) new_font_definitions: Option<epaint::text::FontDefinitions>,
#[cfg_attr(feature = "persistence", serde(skip))]
pub(crate) interaction: Interaction,
@ -71,8 +74,6 @@ pub struct Options {
pub(crate) style: std::sync::Arc<Style>,
/// Controls the tessellator.
pub(crate) tessellation_options: epaint::TessellationOptions,
/// Font sizes etc.
pub(crate) font_definitions: epaint::text::FontDefinitions,
/// This does not at all change the behavior of egui,
/// but is a signal to any backend that we want the [`crate::Output::events`] read out loud.

Loading…
Cancel
Save