Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Impeller] Reorganize the glyph atlas to improve efficiency when looking up runs of glyphs in the same font #45191

Merged
merged 1 commit into from
Aug 30, 2023

Conversation

jason-simmons
Copy link
Member

The atlas will now store a separate map of glyph positions for each font/scale pair. This avoids the cost of repeated hashing and copying of font objects for each glyph in a text run.

See flutter/flutter#133201

Copy link
Member

@gaaclarke gaaclarke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @jason-simmons, were you able to get positive improvement in the macro benchmarks?

@jonahwilliams
Copy link
Member

Building this now to take a look.

Copy link
Member

@gaaclarke gaaclarke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic looks good. In most places I think we can clearly see this is faster. I called out 2 places where it might not be (one of them I think we can actually remove). It would be nice to have a measurement for this =)

impeller/typographer/font_glyph_pair.h Outdated Show resolved Hide resolved
struct std::equal_to<impeller::ScaledFont> {
constexpr bool operator()(const impeller::ScaledFont& lhs,
const impeller::ScaledFont& rhs) const {
return lhs.font.IsEqual(rhs.font) && lhs.scale == rhs.scale;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to change this, but I'm just realizing that hashing on a floating value might finicky.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We round the scale to two decimal places elsewhere so it should be okay. This definitely caused a problem before if there was animated text

Comment on lines 66 to +73
std::optional<Rect> GlyphAtlas::FindFontGlyphBounds(
const FontGlyphPair& pair) const {
const auto& found = positions_.find(pair);
if (found == positions_.end()) {
const auto& found = font_atlas_map_.find(pair.scaled_font);
if (found == font_atlas_map_.end()) {
return std::nullopt;
}
return found->second;
return found->second.FindGlyphBounds(pair.glyph);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one place where we are replacing one lookup with 2, so FindFontGlyphBounds may be slower. This is where having some benchmark improvement would be nice. Are we even using this anymore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is called when adding new glyphs to the atlas.

The overall intent of the PR is to make rendering faster when no new glyphs are needed in the atlas. The additional overhead when new glyphs are added should not be significant.

impeller/typographer/glyph_atlas.h Outdated Show resolved Hide resolved
@@ -67,12 +67,13 @@ Scalar TextFrame::RoundScaledFontSize(Scalar scale, Scalar point_size) {
return std::round(scale * 100) / 100;
}

void TextFrame::CollectUniqueFontGlyphPairs(FontGlyphPair::Set& set,
void TextFrame::CollectUniqueFontGlyphPairs(FontGlyphMap& glyph_map,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard to know if this is going to be faster. There are multiple inserts happening now.

@jason-simmons
Copy link
Member Author

The main goal here is minimizing the cost of the common case where no new glyphs need to be added to the atlas.

I tested this by scrolling repeatedly through one of the license texts in the Gallery license viewer screen on a Pixel 3.

Without this patch the Android profiler reported that GlyphAtlas::FindFontGlyphBounds and its descendants consumed 11.06% of raster thread time.

With this patch FindGlyphBounds is reduced to 0.83%.

…ing up runs of glyphs in the same font

The atlas will now store a separate map of glyph positions for each
font/scale pair.  This avoids the cost of repeated hashing and copying
of font objects for each glyph in a text run.

See flutter/flutter#133201
Copy link
Member

@gaaclarke gaaclarke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

Copy link
Member

@jonahwilliams jonahwilliams left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@jason-simmons jason-simmons added the autosubmit Merge PR when tree becomes green via auto submit App label Aug 30, 2023
@auto-submit auto-submit bot merged commit b63eee2 into flutter:main Aug 30, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Aug 30, 2023
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Aug 30, 2023
…133692)

flutter/engine@31d5662...b63eee2

2023-08-30 [email protected] [Impeller] Reorganize the glyph atlas to improve efficiency when looking up runs of glyphs in the same font (flutter/engine#45191)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-engine-flutter-autoroll
Please CC [email protected],[email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://bugs.chromium.org/p/skia/issues/entry?template=Autoroller+Bug

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App e: impeller
Projects
No open projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants