Skip to content

Commit

Permalink
Reuse TextLayoutInfo in queue_text (#14997)
Browse files Browse the repository at this point in the history
# Objective

Don't reallocate `TextLayoutInfo` every time it needs to be updated.

## Solution

Reuse existing allocation.
  • Loading branch information
UkoeHB authored Sep 2, 2024
1 parent f02d76a commit 2b94a10
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
24 changes: 13 additions & 11 deletions crates/bevy_text/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ impl TextPipeline {
#[allow(clippy::too_many_arguments)]
pub fn queue_text(
&mut self,
layout_info: &mut TextLayoutInfo,
fonts: &Assets<Font>,
sections: &[TextSection],
scale_factor: f64,
Expand All @@ -156,9 +157,12 @@ impl TextPipeline {
textures: &mut Assets<Image>,
y_axis_orientation: YAxisOrientation,
buffer: &mut CosmicBuffer,
) -> Result<TextLayoutInfo, TextError> {
) -> Result<(), TextError> {
layout_info.glyphs.clear();
layout_info.size = Default::default();

if sections.is_empty() {
return Ok(TextLayoutInfo::default());
return Ok(());
}

self.update_buffer(
Expand All @@ -175,14 +179,14 @@ impl TextPipeline {
let font_system = &mut self.font_system.0;
let swash_cache = &mut self.swash_cache.0;

let glyphs = buffer
buffer
.layout_runs()
.flat_map(|run| {
run.glyphs
.iter()
.map(move |layout_glyph| (layout_glyph, run.line_y))
})
.map(|(layout_glyph, line_y)| {
.try_for_each(|(layout_glyph, line_y)| {
let section_index = layout_glyph.metadata;

let font_handle = sections[section_index].style.font.clone_weak();
Expand Down Expand Up @@ -224,14 +228,12 @@ impl TextPipeline {
// when glyphs are not limited to single byte representation, relevant for #1319
let pos_glyph =
PositionedGlyph::new(position, glyph_size.as_vec2(), atlas_info, section_index);
Ok(pos_glyph)
})
.collect::<Result<Vec<_>, _>>()?;
layout_info.glyphs.push(pos_glyph);
Ok(())
})?;

Ok(TextLayoutInfo {
glyphs,
size: box_size,
})
layout_info.size = box_size;
Ok(())
}

/// Queues text for measurement
Expand Down
13 changes: 8 additions & 5 deletions crates/bevy_text/src/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ pub fn update_text2d_layout(

let inverse_scale_factor = scale_factor.recip();

for (entity, text, bounds, mut text_layout_info, mut buffer) in &mut text_query {
for (entity, text, bounds, text_layout_info, mut buffer) in &mut text_query {
if factor_changed || text.is_changed() || bounds.is_changed() || queue.remove(&entity) {
let text_bounds = TextBounds {
width: if text.linebreak_behavior == BreakLineOn::NoWrap {
Expand All @@ -183,7 +183,9 @@ pub fn update_text2d_layout(
.map(|height| scale_value(height, scale_factor)),
};

let text_layout_info = text_layout_info.into_inner();
match text_pipeline.queue_text(
text_layout_info,
&fonts,
&text.sections,
scale_factor.into(),
Expand All @@ -204,10 +206,11 @@ pub fn update_text2d_layout(
Err(e @ (TextError::FailedToAddGlyph(_) | TextError::FailedToGetGlyphImage(_))) => {
panic!("Fatal error when processing text: {e}.");
}
Ok(mut info) => {
info.size.x = scale_value(info.size.x, inverse_scale_factor);
info.size.y = scale_value(info.size.y, inverse_scale_factor);
*text_layout_info = info;
Ok(()) => {
text_layout_info.size.x =
scale_value(text_layout_info.size.x, inverse_scale_factor);
text_layout_info.size.y =
scale_value(text_layout_info.size.y, inverse_scale_factor);
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions crates/bevy_ui/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ fn queue_text(
text: &Text,
node: Ref<Node>,
mut text_flags: Mut<TextFlags>,
mut text_layout_info: Mut<TextLayoutInfo>,
text_layout_info: Mut<TextLayoutInfo>,
buffer: &mut CosmicBuffer,
) {
// Skip the text node if it is waiting for a new measure func
Expand All @@ -240,7 +240,9 @@ fn queue_text(
)
};

let text_layout_info = text_layout_info.into_inner();
match text_pipeline.queue_text(
text_layout_info,
fonts,
&text.sections,
scale_factor.into(),
Expand All @@ -260,10 +262,11 @@ fn queue_text(
Err(e @ (TextError::FailedToAddGlyph(_) | TextError::FailedToGetGlyphImage(_))) => {
panic!("Fatal error when processing text: {e}.");
}
Ok(mut info) => {
info.size.x = scale_value(info.size.x, inverse_scale_factor);
info.size.y = scale_value(info.size.y, inverse_scale_factor);
*text_layout_info = info;
Ok(()) => {
text_layout_info.size.x =
scale_value(text_layout_info.size.x, inverse_scale_factor);
text_layout_info.size.y =
scale_value(text_layout_info.size.y, inverse_scale_factor);
text_flags.needs_recompute = false;
}
}
Expand Down

0 comments on commit 2b94a10

Please sign in to comment.