optimization: don't compare font data each frame
This commit is contained in:
parent
def09c2455
commit
aeaa54aab1
2 changed files with 28 additions and 16 deletions
|
@ -407,7 +407,14 @@ impl Context {
|
||||||
|
|
||||||
/// Will become active at the start of the next frame.
|
/// Will become active at the start of the next frame.
|
||||||
pub fn set_fonts(&self, font_definitions: FontDefinitions) {
|
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.
|
/// The [`Style`] used by all subsequent windows, panels etc.
|
||||||
|
@ -531,20 +538,24 @@ impl Context {
|
||||||
self.input = input.begin_frame(new_raw_input);
|
self.input = input.begin_frame(new_raw_input);
|
||||||
self.frame_state.lock().begin_frame(&self.input);
|
self.frame_state.lock().begin_frame(&self.input);
|
||||||
|
|
||||||
let font_definitions = self.memory().options.font_definitions.clone();
|
{
|
||||||
let pixels_per_point = self.input.pixels_per_point();
|
// Load new fonts if required:
|
||||||
let same_as_current = match &self.fonts {
|
let new_font_definitions = self.memory().new_font_definitions.take();
|
||||||
None => false,
|
let pixels_per_point = self.input.pixels_per_point();
|
||||||
Some(fonts) => {
|
|
||||||
*fonts.definitions() == font_definitions
|
let pixels_per_point_changed = match &self.fonts {
|
||||||
&& (fonts.pixels_per_point() - pixels_per_point).abs() < 1e-3
|
None => true,
|
||||||
|
Some(current_fonts) => {
|
||||||
|
(current_fonts.pixels_per_point() - pixels_per_point).abs() > 1e-3
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
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,
|
||||||
|
new_font_definitions.unwrap_or_default(),
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
};
|
|
||||||
if !same_as_current {
|
|
||||||
self.fonts = Some(Arc::new(Fonts::from_definitions(
|
|
||||||
pixels_per_point,
|
|
||||||
font_definitions,
|
|
||||||
)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure we register the background area so panels and background ui can catch clicks:
|
// Ensure we register the background area so panels and background ui can catch clicks:
|
||||||
|
|
|
@ -23,6 +23,9 @@ pub struct Memory {
|
||||||
/// new scale that will be applied at the start of the next frame
|
/// new scale that will be applied at the start of the next frame
|
||||||
pub(crate) new_pixels_per_point: Option<f32>,
|
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))]
|
#[cfg_attr(feature = "persistence", serde(skip))]
|
||||||
pub(crate) interaction: Interaction,
|
pub(crate) interaction: Interaction,
|
||||||
|
|
||||||
|
@ -71,8 +74,6 @@ pub struct Options {
|
||||||
pub(crate) style: std::sync::Arc<Style>,
|
pub(crate) style: std::sync::Arc<Style>,
|
||||||
/// Controls the tessellator.
|
/// Controls the tessellator.
|
||||||
pub(crate) tessellation_options: epaint::TessellationOptions,
|
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,
|
/// 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.
|
/// but is a signal to any backend that we want the [`crate::Output::events`] read out loud.
|
||||||
|
|
Loading…
Reference in a new issue