[Impeller] Cache Skia text bounds computation. (flutter/engine#45150)

Fixes https://github.com/flutter/flutter/issues/133388

Just use the TextBounds that Skia gives us. These are slightly larger but already computed. If the larger bounds are a problem we can go back to Impeller computed bounds, but using a cached computation.

### Skia
![flutter_02](https://github.com/flutter/engine/assets/8975114/c1824eb3-bd82-47b0-a6af-282e51419a00)

### Impeller
![flutter_03](https://github.com/flutter/engine/assets/8975114/bb2eff8c-ae6e-4c59-90a5-228f8a115dca)
This commit is contained in:
Jonah Williams
2023-08-28 14:49:41 -07:00
committed by GitHub
parent ec6964204d
commit 089cb17e97
12 changed files with 82 additions and 95 deletions

View File

@@ -1278,7 +1278,11 @@ bool RenderTextInCanvasSkia(const std::shared_ptr<Context>& context,
}
// Create the Impeller text frame and draw it at the designated baseline.
auto frame = MakeTextFrameFromTextBlobSkia(blob);
auto maybe_frame = MakeTextFrameFromTextBlobSkia(blob);
if (!maybe_frame.has_value()) {
return false;
}
auto frame = maybe_frame.value();
Paint text_paint;
text_paint.color = Color::Yellow().WithAlpha(options.alpha);
@@ -1467,7 +1471,7 @@ TEST_P(AiksTest, CanRenderTextOutsideBoundaries) {
{
auto blob = SkTextBlob::MakeFromString(t.text, sk_font);
ASSERT_NE(blob, nullptr);
auto frame = MakeTextFrameFromTextBlobSkia(blob);
auto frame = MakeTextFrameFromTextBlobSkia(blob).value();
canvas.DrawTextFrame(frame, Point(), text_paint);
}
canvas.Restore();
@@ -3060,7 +3064,12 @@ TEST_P(AiksTest, TextForegroundShaderWithTransform) {
auto blob = SkTextBlob::MakeFromString("Hello", sk_font);
ASSERT_NE(blob, nullptr);
auto frame = MakeTextFrameFromTextBlobSkia(blob);
auto maybe_frame = MakeTextFrameFromTextBlobSkia(blob);
ASSERT_TRUE(maybe_frame.has_value());
if (!maybe_frame.has_value()) {
return;
}
auto frame = maybe_frame.value();
canvas.DrawTextFrame(frame, Point(), text_paint);
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));

View File

@@ -1111,7 +1111,11 @@ void DlDispatcher::drawDisplayList(
void DlDispatcher::drawTextBlob(const sk_sp<SkTextBlob>& blob,
SkScalar x,
SkScalar y) {
const auto text_frame = MakeTextFrameFromTextBlobSkia(blob);
const auto maybe_text_frame = MakeTextFrameFromTextBlobSkia(blob);
if (!maybe_text_frame.has_value()) {
return;
}
const auto text_frame = maybe_text_frame.value();
if (paint_.style == Paint::Style::kStroke ||
paint_.color_source.GetType() != ColorSource::Type::kColor) {
auto path = skia_conversions::PathDataFromTextBlob(blob);

View File

@@ -59,16 +59,8 @@ void TextContents::SetOffset(Vector2 offset) {
offset_ = offset;
}
std::optional<Rect> TextContents::GetTextFrameBounds() const {
return frame_.GetBounds();
}
std::optional<Rect> TextContents::GetCoverage(const Entity& entity) const {
auto bounds = frame_.GetBounds();
if (!bounds.has_value()) {
return std::nullopt;
}
return bounds->TransformBounds(entity.GetTransformation());
return frame_.GetBounds().TransformBounds(entity.GetTransformation());
}
void TextContents::PopulateGlyphAtlas(

View File

@@ -2174,7 +2174,7 @@ TEST_P(EntityTest, InheritOpacityTest) {
SkFont font;
font.setSize(30);
auto blob = SkTextBlob::MakeFromString("A", font);
auto frame = MakeTextFrameFromTextBlobSkia(blob);
auto frame = MakeTextFrameFromTextBlobSkia(blob).value();
auto lazy_glyph_atlas =
std::make_shared<LazyGlyphAtlas>(TypographerContextSkia::Make());
lazy_glyph_atlas->AddTextFrame(frame, 1.0f);

View File

@@ -39,16 +39,17 @@ static Rect ToRect(const SkRect& rect) {
static constexpr Scalar kScaleSize = 100000.0f;
TextFrame MakeTextFrameFromTextBlobSkia(const sk_sp<SkTextBlob>& blob) {
std::optional<TextFrame> MakeTextFrameFromTextBlobSkia(
const sk_sp<SkTextBlob>& blob) {
// Handling nullptr text blobs feels overly defensive here, as I don't
// actually know if this happens.
if (!blob) {
return {};
}
TextFrame frame;
std::vector<TextRun> runs;
bool has_color = false;
for (SkTextBlobRunIterator run(blob.get()); !run.done(); run.next()) {
TextRun text_run(ToFont(run));
// TODO(jonahwilliams): ask Skia for a public API to look this up.
// https://github.com/flutter/flutter/issues/112005
SkStrikeSpec strikeSpec = SkStrikeSpec::MakeWithNoDevice(run.font());
@@ -57,12 +58,6 @@ TextFrame MakeTextFrameFromTextBlobSkia(const sk_sp<SkTextBlob>& blob) {
const auto glyph_count = run.glyphCount();
const auto* glyphs = run.glyphs();
switch (run.positioning()) {
case SkTextBlobRunIterator::kDefault_Positioning:
FML_DLOG(ERROR) << "Unimplemented.";
break;
case SkTextBlobRunIterator::kHorizontal_Positioning:
FML_DLOG(ERROR) << "Unimplemented.";
break;
case SkTextBlobRunIterator::kFull_Positioning: {
std::vector<SkRect> glyph_bounds;
glyph_bounds.resize(glyph_count);
@@ -75,6 +70,8 @@ TextFrame MakeTextFrameFromTextBlobSkia(const sk_sp<SkTextBlob>& blob) {
font.setSize(kScaleSize);
font.getBounds(glyphs, glyph_count, glyph_bounds.data(), nullptr);
std::vector<TextRun::GlyphPosition> positions;
positions.reserve(glyph_count);
for (auto i = 0u; i < glyph_count; i++) {
// kFull_Positioning has two scalars per glyph.
const SkPoint* glyph_points = run.points();
@@ -82,25 +79,23 @@ TextFrame MakeTextFrameFromTextBlobSkia(const sk_sp<SkTextBlob>& blob) {
Glyph::Type type = paths.glyph(glyphs[i])->isColor()
? Glyph::Type::kBitmap
: Glyph::Type::kPath;
has_color |= type == Glyph::Type::kBitmap;
text_run.AddGlyph(
positions.emplace_back(TextRun::GlyphPosition{
Glyph{glyphs[i], type,
ToRect(glyph_bounds[i]).Scale(font_size / kScaleSize)},
Point{point->x(), point->y()});
Point{point->x(), point->y()}});
}
TextRun text_run(ToFont(run), positions);
runs.emplace_back(text_run);
break;
}
case SkTextBlobRunIterator::kRSXform_Positioning:
FML_DLOG(ERROR) << "Unimplemented.";
break;
default:
FML_DLOG(ERROR) << "Unimplemented.";
continue;
}
frame.AddTextRun(std::move(text_run));
}
return frame;
return TextFrame(runs, ToRect(blob->bounds()), has_color);
}
} // namespace impeller

View File

@@ -10,6 +10,7 @@
namespace impeller {
TextFrame MakeTextFrameFromTextBlobSkia(const sk_sp<SkTextBlob>& blob);
std::optional<TextFrame> MakeTextFrameFromTextBlobSkia(
const sk_sp<SkTextBlob>& blob);
} // namespace impeller

View File

@@ -52,10 +52,16 @@ TextFrame MakeTextFrameSTB(const std::shared_ptr<TypefaceSTB>& typeface_stb,
}
}
TextFrame frame;
frame.AddTextRun(std::move(run));
std::optional<Rect> result;
for (const auto& glyph_position : run.GetGlyphPositions()) {
Rect glyph_rect =
Rect(glyph_position.position + glyph_position.glyph.bounds.origin,
glyph_position.glyph.bounds.size);
result = result.has_value() ? result->Union(glyph_rect) : glyph_rect;
}
return frame;
std::vector<TextRun> runs = {run};
return TextFrame(runs, result.value_or(Rect::MakeLTRB(0, 0, 0, 0)), false);
}
} // namespace impeller

View File

@@ -8,30 +8,13 @@ namespace impeller {
TextFrame::TextFrame() = default;
TextFrame::TextFrame(std::vector<TextRun>& runs, Rect bounds, bool has_color)
: runs_(std::move(runs)), bounds_(bounds), has_color_(has_color) {}
TextFrame::~TextFrame() = default;
std::optional<Rect> TextFrame::GetBounds() const {
std::optional<Rect> result;
for (const auto& run : runs_) {
for (const auto& glyph_position : run.GetGlyphPositions()) {
Rect glyph_rect =
Rect(glyph_position.position + glyph_position.glyph.bounds.origin,
glyph_position.glyph.bounds.size);
result = result.has_value() ? result->Union(glyph_rect) : glyph_rect;
}
}
return result;
}
bool TextFrame::AddTextRun(TextRun&& run) {
if (!run.IsValid()) {
return false;
}
has_color_ |= run.HasColor();
runs_.emplace_back(std::move(run));
return true;
Rect TextFrame::GetBounds() const {
return bounds_;
}
size_t TextFrame::GetRunCount() const {

View File

@@ -20,6 +20,8 @@ class TextFrame {
public:
TextFrame();
TextFrame(std::vector<TextRun>& runs, Rect bounds, bool has_color);
~TextFrame();
void CollectUniqueFontGlyphPairs(FontGlyphPair::Set& set, Scalar scale) const;
@@ -30,9 +32,9 @@ class TextFrame {
/// @brief The conservative bounding box for this text frame.
///
/// @return The bounds rectangle. If there are no glyphs in this text
/// frame, std::nullopt is returned.
/// frame and empty Rectangle is returned instead.
///
std::optional<Rect> GetBounds() const;
Rect GetBounds() const;
//----------------------------------------------------------------------------
/// @brief The number of runs in this text frame.
@@ -41,15 +43,6 @@ class TextFrame {
///
size_t GetRunCount() const;
//----------------------------------------------------------------------------
/// @brief Adds a new text run to the text frame.
///
/// @param[in] run The run
///
/// @return If the text run could be added to this frame.
///
bool AddTextRun(TextRun&& run);
//----------------------------------------------------------------------------
/// @brief Returns a reference to all the text runs in this frame.
///
@@ -77,6 +70,7 @@ class TextFrame {
private:
std::vector<TextRun> runs_;
Rect bounds_;
bool has_color_ = false;
};

View File

@@ -13,11 +13,18 @@ TextRun::TextRun(const Font& font) : font_(font) {
is_valid_ = true;
}
TextRun::TextRun(const Font& font, std::vector<GlyphPosition>& glyphs)
: font_(font), glyphs_(std::move(glyphs)) {
if (!font_.IsValid()) {
return;
}
is_valid_ = true;
}
TextRun::~TextRun() = default;
bool TextRun::AddGlyph(Glyph glyph, Point position) {
glyphs_.emplace_back(GlyphPosition{glyph, position});
has_color_ |= glyph.type == Glyph::Type::kBitmap;
return true;
}
@@ -37,8 +44,4 @@ const Font& TextRun::GetFont() const {
return font_;
}
bool TextRun::HasColor() const {
return has_color_;
}
} // namespace impeller

View File

@@ -31,7 +31,9 @@ class TextRun {
///
/// @param[in] font The font
///
TextRun(const Font& font);
explicit TextRun(const Font& font);
TextRun(const Font& font, std::vector<GlyphPosition>& glyphs);
~TextRun();
@@ -68,15 +70,10 @@ class TextRun {
///
const Font& GetFont() const;
//----------------------------------------------------------------------------
/// @brief Whether any glyph in this run has color.
bool HasColor() const;
private:
Font font_;
std::vector<GlyphPosition> glyphs_;
bool is_valid_ = false;
bool has_color_ = false;
};
} // namespace impeller

View File

@@ -40,7 +40,7 @@ TEST_P(TypographerTest, CanConvertTextBlob) {
auto blob = SkTextBlob::MakeFromString(
"the quick brown fox jumped over the lazy dog.", font);
ASSERT_TRUE(blob);
auto frame = MakeTextFrameFromTextBlobSkia(blob);
auto frame = MakeTextFrameFromTextBlobSkia(blob).value();
ASSERT_EQ(frame.GetRunCount(), 1u);
for (const auto& run : frame.GetRuns()) {
ASSERT_TRUE(run.IsValid());
@@ -62,7 +62,7 @@ TEST_P(TypographerTest, CanCreateGlyphAtlas) {
ASSERT_TRUE(blob);
auto atlas = CreateGlyphAtlas(
*GetContext(), context.get(), GlyphAtlas::Type::kAlphaBitmap, 1.0f,
atlas_context, MakeTextFrameFromTextBlobSkia(blob));
atlas_context, MakeTextFrameFromTextBlobSkia(blob).value());
ASSERT_NE(atlas, nullptr);
ASSERT_NE(atlas->GetTexture(), nullptr);
ASSERT_EQ(atlas->GetType(), GlyphAtlas::Type::kAlphaBitmap);
@@ -108,7 +108,7 @@ TEST_P(TypographerTest, LazyAtlasTracksColor) {
auto blob = SkTextBlob::MakeFromString("hello", sk_font);
ASSERT_TRUE(blob);
auto frame = MakeTextFrameFromTextBlobSkia(blob);
auto frame = MakeTextFrameFromTextBlobSkia(blob).value();
ASSERT_FALSE(frame.GetAtlasType() == GlyphAtlas::Type::kColorBitmap);
@@ -117,7 +117,8 @@ TEST_P(TypographerTest, LazyAtlasTracksColor) {
lazy_atlas.AddTextFrame(frame, 1.0f);
frame = MakeTextFrameFromTextBlobSkia(
SkTextBlob::MakeFromString("😀 ", emoji_font));
SkTextBlob::MakeFromString("😀 ", emoji_font))
.value();
ASSERT_TRUE(frame.GetAtlasType() == GlyphAtlas::Type::kColorBitmap);
@@ -142,7 +143,7 @@ TEST_P(TypographerTest, GlyphAtlasWithOddUniqueGlyphSize) {
ASSERT_TRUE(blob);
auto atlas = CreateGlyphAtlas(
*GetContext(), context.get(), GlyphAtlas::Type::kAlphaBitmap, 1.0f,
atlas_context, MakeTextFrameFromTextBlobSkia(blob));
atlas_context, MakeTextFrameFromTextBlobSkia(blob).value());
ASSERT_NE(atlas, nullptr);
ASSERT_NE(atlas->GetTexture(), nullptr);
@@ -159,7 +160,7 @@ TEST_P(TypographerTest, GlyphAtlasIsRecycledIfUnchanged) {
ASSERT_TRUE(blob);
auto atlas = CreateGlyphAtlas(
*GetContext(), context.get(), GlyphAtlas::Type::kAlphaBitmap, 1.0f,
atlas_context, MakeTextFrameFromTextBlobSkia(blob));
atlas_context, MakeTextFrameFromTextBlobSkia(blob).value());
ASSERT_NE(atlas, nullptr);
ASSERT_NE(atlas->GetTexture(), nullptr);
ASSERT_EQ(atlas, atlas_context->GetGlyphAtlas());
@@ -168,7 +169,7 @@ TEST_P(TypographerTest, GlyphAtlasIsRecycledIfUnchanged) {
auto next_atlas = CreateGlyphAtlas(
*GetContext(), context.get(), GlyphAtlas::Type::kAlphaBitmap, 1.0f,
atlas_context, MakeTextFrameFromTextBlobSkia(blob));
atlas_context, MakeTextFrameFromTextBlobSkia(blob).value());
ASSERT_EQ(atlas, next_atlas);
ASSERT_EQ(atlas_context->GetGlyphAtlas(), atlas);
}
@@ -191,7 +192,7 @@ TEST_P(TypographerTest, GlyphAtlasWithLotsOfdUniqueGlyphSize) {
FontGlyphPair::Set set;
size_t size_count = 8;
for (size_t index = 0; index < size_count; index += 1) {
MakeTextFrameFromTextBlobSkia(blob).CollectUniqueFontGlyphPairs(
MakeTextFrameFromTextBlobSkia(blob).value().CollectUniqueFontGlyphPairs(
set, 0.6 * index);
};
auto atlas =
@@ -225,7 +226,7 @@ TEST_P(TypographerTest, GlyphAtlasTextureIsRecycledIfUnchanged) {
ASSERT_TRUE(blob);
auto atlas = CreateGlyphAtlas(
*GetContext(), context.get(), GlyphAtlas::Type::kAlphaBitmap, 1.0f,
atlas_context, MakeTextFrameFromTextBlobSkia(blob));
atlas_context, MakeTextFrameFromTextBlobSkia(blob).value());
auto old_packer = atlas_context->GetRectPacker();
ASSERT_NE(atlas, nullptr);
@@ -239,7 +240,7 @@ TEST_P(TypographerTest, GlyphAtlasTextureIsRecycledIfUnchanged) {
auto blob2 = SkTextBlob::MakeFromString("spooky 2", sk_font);
auto next_atlas = CreateGlyphAtlas(
*GetContext(), context.get(), GlyphAtlas::Type::kAlphaBitmap, 1.0f,
atlas_context, MakeTextFrameFromTextBlobSkia(blob2));
atlas_context, MakeTextFrameFromTextBlobSkia(blob2).value());
ASSERT_EQ(atlas, next_atlas);
auto* second_texture = next_atlas->GetTexture().get();
@@ -258,7 +259,7 @@ TEST_P(TypographerTest, GlyphAtlasTextureIsRecreatedIfTypeChanges) {
ASSERT_TRUE(blob);
auto atlas = CreateGlyphAtlas(
*GetContext(), context.get(), GlyphAtlas::Type::kAlphaBitmap, 1.0f,
atlas_context, MakeTextFrameFromTextBlobSkia(blob));
atlas_context, MakeTextFrameFromTextBlobSkia(blob).value());
auto old_packer = atlas_context->GetRectPacker();
ASSERT_NE(atlas, nullptr);
@@ -273,7 +274,7 @@ TEST_P(TypographerTest, GlyphAtlasTextureIsRecreatedIfTypeChanges) {
auto blob2 = SkTextBlob::MakeFromString("spooky 1", sk_font);
auto next_atlas = CreateGlyphAtlas(
*GetContext(), context.get(), GlyphAtlas::Type::kColorBitmap, 1.0f,
atlas_context, MakeTextFrameFromTextBlobSkia(blob2));
atlas_context, MakeTextFrameFromTextBlobSkia(blob2).value());
ASSERT_NE(atlas, next_atlas);
auto* second_texture = next_atlas->GetTexture().get();
@@ -308,12 +309,14 @@ TEST_P(TypographerTest, MaybeHasOverlapping) {
SkFont sk_font(typeface, 0.5f);
auto frame =
MakeTextFrameFromTextBlobSkia(SkTextBlob::MakeFromString("1", sk_font));
MakeTextFrameFromTextBlobSkia(SkTextBlob::MakeFromString("1", sk_font))
.value();
// Single character has no overlapping
ASSERT_FALSE(frame.MaybeHasOverlapping());
auto frame_2 = MakeTextFrameFromTextBlobSkia(
SkTextBlob::MakeFromString("123456789", sk_font));
SkTextBlob::MakeFromString("123456789", sk_font))
.value();
ASSERT_FALSE(frame_2.MaybeHasOverlapping());
}