diff --git a/engine/src/flutter/impeller/entity/contents/text_contents.cc b/engine/src/flutter/impeller/entity/contents/text_contents.cc index c2818de9fa..eb9a1c739f 100644 --- a/engine/src/flutter/impeller/entity/contents/text_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/text_contents.cc @@ -166,7 +166,7 @@ bool TextContents::Render(const ContentContext& renderer, Scalar rounded_scale = TextFrame::RoundScaledFontSize( scale_, font.GetMetrics().point_size); const FontGlyphAtlas* font_atlas = - atlas->GetFontGlyphAtlas(font, rounded_scale); + atlas->GetFontGlyphAtlas(font, rounded_scale, frame_->GetColor()); if (!font_atlas) { VALIDATION_LOG << "Could not find font in the atlas."; continue; diff --git a/engine/src/flutter/impeller/geometry/color.h b/engine/src/flutter/impeller/geometry/color.h index c90c696066..2107142f64 100644 --- a/engine/src/flutter/impeller/geometry/color.h +++ b/engine/src/flutter/impeller/geometry/color.h @@ -253,6 +253,16 @@ struct Color { return {r, g, b, a}; } + /** + * @brief Convert to ARGB 32 bit color. + * + * @return constexpr uint32_t + */ + constexpr uint32_t ToARGB() const { + std::array result = ToR8G8B8A8(); + return result[3] << 24 | result[0] << 16 | result[1] << 8 | result[2]; + } + static constexpr Color White() { return {1.0f, 1.0f, 1.0f, 1.0f}; } static constexpr Color Black() { return {0.0f, 0.0f, 0.0f, 1.0f}; } diff --git a/engine/src/flutter/impeller/typographer/backends/skia/BUILD.gn b/engine/src/flutter/impeller/typographer/backends/skia/BUILD.gn index cf344bff3d..be13150037 100644 --- a/engine/src/flutter/impeller/typographer/backends/skia/BUILD.gn +++ b/engine/src/flutter/impeller/typographer/backends/skia/BUILD.gn @@ -15,6 +15,7 @@ impeller_component("typographer_skia_backend") { ] public_deps = [ + "//flutter/display_list", "//flutter/impeller/typographer", "//flutter/skia", ] diff --git a/engine/src/flutter/impeller/typographer/backends/skia/text_frame_skia.cc b/engine/src/flutter/impeller/typographer/backends/skia/text_frame_skia.cc index fdf2b3d68a..5167cdd680 100644 --- a/engine/src/flutter/impeller/typographer/backends/skia/text_frame_skia.cc +++ b/engine/src/flutter/impeller/typographer/backends/skia/text_frame_skia.cc @@ -6,6 +6,7 @@ #include +#include "display_list/dl_color.h" #include "flutter/fml/logging.h" #include "impeller/typographer/backends/skia/typeface_skia.h" #include "include/core/SkRect.h" @@ -16,6 +17,15 @@ namespace impeller { +static Color ToColor(const flutter::DlColor& color) { + return { + static_cast(color.getRedF()), // + static_cast(color.getGreenF()), // + static_cast(color.getBlueF()), // + static_cast(color.getAlphaF()) // + }; +} + static Font ToFont(const SkTextBlobRunIterator& run) { auto& font = run.font(); auto typeface = std::make_shared(font.refTypeface()); @@ -39,8 +49,10 @@ static Rect ToRect(const SkRect& rect) { static constexpr Scalar kScaleSize = 64.0f; std::shared_ptr MakeTextFrameFromTextBlobSkia( - const sk_sp& blob) { + const sk_sp& blob, + flutter::DlColor dl_color) { bool has_color = false; + Color color = ToColor(dl_color); std::vector runs; for (SkTextBlobRunIterator run(blob.get()); !run.done(); run.next()) { // TODO(jonahwilliams): ask Skia for a public API to look this up. @@ -89,7 +101,8 @@ std::shared_ptr MakeTextFrameFromTextBlobSkia( continue; } } - return std::make_shared(runs, ToRect(blob->bounds()), has_color); + return std::make_shared(runs, ToRect(blob->bounds()), has_color, + has_color ? color : Color::Black()); } } // namespace impeller diff --git a/engine/src/flutter/impeller/typographer/backends/skia/text_frame_skia.h b/engine/src/flutter/impeller/typographer/backends/skia/text_frame_skia.h index fdb9d7693e..9586fcb24b 100644 --- a/engine/src/flutter/impeller/typographer/backends/skia/text_frame_skia.h +++ b/engine/src/flutter/impeller/typographer/backends/skia/text_frame_skia.h @@ -5,13 +5,15 @@ #ifndef FLUTTER_IMPELLER_TYPOGRAPHER_BACKENDS_SKIA_TEXT_FRAME_SKIA_H_ #define FLUTTER_IMPELLER_TYPOGRAPHER_BACKENDS_SKIA_TEXT_FRAME_SKIA_H_ +#include "display_list/dl_color.h" #include "impeller/typographer/text_frame.h" #include "third_party/skia/include/core/SkTextBlob.h" namespace impeller { std::shared_ptr MakeTextFrameFromTextBlobSkia( - const sk_sp& blob); + const sk_sp& blob, + flutter::DlColor color = flutter::DlColor::kBlack()); } // namespace impeller diff --git a/engine/src/flutter/impeller/typographer/backends/skia/typographer_context_skia.cc b/engine/src/flutter/impeller/typographer/backends/skia/typographer_context_skia.cc index 7593ef09c2..14485f4dd8 100644 --- a/engine/src/flutter/impeller/typographer/backends/skia/typographer_context_skia.cc +++ b/engine/src/flutter/impeller/typographer/backends/skia/typographer_context_skia.cc @@ -242,7 +242,7 @@ static void DrawGlyph(SkCanvas* canvas, sk_font.setHinting(SkFontHinting::kSlight); sk_font.setEmbolden(metrics.embolden); - auto glyph_color = has_color ? SK_ColorWHITE : SK_ColorBLACK; + auto glyph_color = has_color ? scaled_font.color.ToARGB() : SK_ColorBLACK; SkPaint glyph_paint; glyph_paint.setColor(glyph_color); @@ -331,8 +331,8 @@ std::shared_ptr TypographerContextSkia::CreateGlyphAtlas( std::vector new_glyphs; for (const auto& font_value : font_glyph_map) { const ScaledFont& scaled_font = font_value.first; - const FontGlyphAtlas* font_glyph_atlas = - last_atlas->GetFontGlyphAtlas(scaled_font.font, scaled_font.scale); + const FontGlyphAtlas* font_glyph_atlas = last_atlas->GetFontGlyphAtlas( + scaled_font.font, scaled_font.scale, scaled_font.color); if (font_glyph_atlas) { for (const Glyph& glyph : font_value.second) { if (!font_glyph_atlas->FindGlyphBounds(glyph)) { diff --git a/engine/src/flutter/impeller/typographer/backends/stb/text_frame_stb.cc b/engine/src/flutter/impeller/typographer/backends/stb/text_frame_stb.cc index f312588b8d..d4e3985ab6 100644 --- a/engine/src/flutter/impeller/typographer/backends/stb/text_frame_stb.cc +++ b/engine/src/flutter/impeller/typographer/backends/stb/text_frame_stb.cc @@ -63,7 +63,7 @@ std::shared_ptr MakeTextFrameSTB( std::vector runs = {run}; return std::make_shared( - runs, result.value_or(Rect::MakeLTRB(0, 0, 0, 0)), false); + runs, result.value_or(Rect::MakeLTRB(0, 0, 0, 0)), false, Color::Black()); } } // namespace impeller diff --git a/engine/src/flutter/impeller/typographer/backends/stb/typographer_context_stb.cc b/engine/src/flutter/impeller/typographer/backends/stb/typographer_context_stb.cc index 45803cd8d2..c8d6affcbd 100644 --- a/engine/src/flutter/impeller/typographer/backends/stb/typographer_context_stb.cc +++ b/engine/src/flutter/impeller/typographer/backends/stb/typographer_context_stb.cc @@ -412,8 +412,8 @@ std::shared_ptr TypographerContextSTB::CreateGlyphAtlas( std::vector new_glyphs; for (const auto& font_value : font_glyph_map) { const ScaledFont& scaled_font = font_value.first; - const FontGlyphAtlas* font_glyph_atlas = - last_atlas->GetFontGlyphAtlas(scaled_font.font, scaled_font.scale); + const FontGlyphAtlas* font_glyph_atlas = last_atlas->GetFontGlyphAtlas( + scaled_font.font, scaled_font.scale, scaled_font.color); if (font_glyph_atlas) { for (const Glyph& glyph : font_value.second) { if (!font_glyph_atlas->FindGlyphBounds(glyph)) { diff --git a/engine/src/flutter/impeller/typographer/font_glyph_pair.h b/engine/src/flutter/impeller/typographer/font_glyph_pair.h index e504295b08..01767fbebe 100644 --- a/engine/src/flutter/impeller/typographer/font_glyph_pair.h +++ b/engine/src/flutter/impeller/typographer/font_glyph_pair.h @@ -20,6 +20,7 @@ namespace impeller { struct ScaledFont { Font font; Scalar scale; + Color color; }; using FontGlyphMap = std::unordered_map>; @@ -40,7 +41,7 @@ struct FontGlyphPair { template <> struct std::hash { constexpr std::size_t operator()(const impeller::ScaledFont& sf) const { - return fml::HashCombine(sf.font.GetHash(), sf.scale); + return fml::HashCombine(sf.font.GetHash(), sf.scale, sf.color.ToARGB()); } }; @@ -48,7 +49,8 @@ template <> struct std::equal_to { constexpr bool operator()(const impeller::ScaledFont& lhs, const impeller::ScaledFont& rhs) const { - return lhs.font.IsEqual(rhs.font) && lhs.scale == rhs.scale; + return lhs.font.IsEqual(rhs.font) && lhs.scale == rhs.scale && + lhs.color == rhs.color; } }; diff --git a/engine/src/flutter/impeller/typographer/glyph_atlas.cc b/engine/src/flutter/impeller/typographer/glyph_atlas.cc index a678e77bb2..6e9b46a4e2 100644 --- a/engine/src/flutter/impeller/typographer/glyph_atlas.cc +++ b/engine/src/flutter/impeller/typographer/glyph_atlas.cc @@ -78,8 +78,9 @@ std::optional GlyphAtlas::FindFontGlyphBounds( } const FontGlyphAtlas* GlyphAtlas::GetFontGlyphAtlas(const Font& font, - Scalar scale) const { - const auto& found = font_atlas_map_.find({font, scale}); + Scalar scale, + Color color) const { + const auto& found = font_atlas_map_.find({font, scale, color}); if (found == font_atlas_map_.end()) { return nullptr; } diff --git a/engine/src/flutter/impeller/typographer/glyph_atlas.h b/engine/src/flutter/impeller/typographer/glyph_atlas.h index 8b5f22c893..605e70d753 100644 --- a/engine/src/flutter/impeller/typographer/glyph_atlas.h +++ b/engine/src/flutter/impeller/typographer/glyph_atlas.h @@ -126,7 +126,9 @@ class GlyphAtlas { /// scale are not available in the atlas. The pointer is only /// valid for the lifetime of the GlyphAtlas. /// - const FontGlyphAtlas* GetFontGlyphAtlas(const Font& font, Scalar scale) const; + const FontGlyphAtlas* GetFontGlyphAtlas(const Font& font, + Scalar scale, + Color color) const; private: const Type type_; diff --git a/engine/src/flutter/impeller/typographer/text_frame.cc b/engine/src/flutter/impeller/typographer/text_frame.cc index 75955bf30b..e597e54b7c 100644 --- a/engine/src/flutter/impeller/typographer/text_frame.cc +++ b/engine/src/flutter/impeller/typographer/text_frame.cc @@ -3,13 +3,20 @@ // found in the LICENSE file. #include "impeller/typographer/text_frame.h" +#include "impeller/typographer/font_glyph_pair.h" namespace impeller { TextFrame::TextFrame() = default; -TextFrame::TextFrame(std::vector& runs, Rect bounds, bool has_color) - : runs_(std::move(runs)), bounds_(bounds), has_color_(has_color) {} +TextFrame::TextFrame(std::vector& runs, + Rect bounds, + bool has_color, + Color color) + : runs_(std::move(runs)), + bounds_(bounds), + has_color_(has_color), + color_(color) {} TextFrame::~TextFrame() = default; @@ -30,6 +37,10 @@ GlyphAtlas::Type TextFrame::GetAtlasType() const { : GlyphAtlas::Type::kAlphaBitmap; } +Color TextFrame::GetColor() const { + return color_; +} + bool TextFrame::MaybeHasOverlapping() const { if (runs_.size() > 1) { return true; @@ -78,7 +89,7 @@ void TextFrame::CollectUniqueFontGlyphPairs(FontGlyphMap& glyph_map, const Font& font = run.GetFont(); auto rounded_scale = RoundScaledFontSize(scale, font.GetMetrics().point_size); - auto& set = glyph_map[{font, rounded_scale}]; + auto& set = glyph_map[ScaledFont{font, rounded_scale, color_}]; for (const TextRun::GlyphPosition& glyph_position : run.GetGlyphPositions()) { #if false diff --git a/engine/src/flutter/impeller/typographer/text_frame.h b/engine/src/flutter/impeller/typographer/text_frame.h index c92bd9a5ca..97cf52b37a 100644 --- a/engine/src/flutter/impeller/typographer/text_frame.h +++ b/engine/src/flutter/impeller/typographer/text_frame.h @@ -20,7 +20,10 @@ class TextFrame { public: TextFrame(); - TextFrame(std::vector& runs, Rect bounds, bool has_color); + TextFrame(std::vector& runs, + Rect bounds, + bool has_color, + Color color); ~TextFrame(); @@ -60,6 +63,15 @@ class TextFrame { /// to apply opacity peephole optimizations to text blobs. bool MaybeHasOverlapping() const; + //---------------------------------------------------------------------------- + /// @brief Returns the paint color this text frame was recorded with. + /// + /// Non-bitmap/COLR fonts always use a black text color here, but + /// COLR fonts can potentially use the paint color in the glyph + /// atlas, so this color must be considered as part of the cache + /// key. + Color GetColor() const; + //---------------------------------------------------------------------------- /// @brief The type of atlas this run should be emplaced in. GlyphAtlas::Type GetAtlasType() const; @@ -71,7 +83,8 @@ class TextFrame { private: std::vector runs_; Rect bounds_; - bool has_color_ = false; + bool has_color_; + Color color_; }; } // namespace impeller diff --git a/engine/src/flutter/impeller/typographer/typographer_unittests.cc b/engine/src/flutter/impeller/typographer/typographer_unittests.cc index fb6e6fa654..37f53c4350 100644 --- a/engine/src/flutter/impeller/typographer/typographer_unittests.cc +++ b/engine/src/flutter/impeller/typographer/typographer_unittests.cc @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "display_list/dl_color.h" #include "flutter/display_list/testing/dl_test_snippets.h" #include "flutter/testing/testing.h" #include "gtest/gtest.h" @@ -10,6 +11,7 @@ #include "impeller/playground/playground_test.h" #include "impeller/typographer/backends/skia/text_frame_skia.h" #include "impeller/typographer/backends/skia/typographer_context_skia.h" +#include "impeller/typographer/font_glyph_pair.h" #include "impeller/typographer/lazy_glyph_atlas.h" #include "impeller/typographer/rectangle_packer.h" #include "third_party/skia/include/core/SkFont.h" @@ -42,6 +44,22 @@ static std::shared_ptr CreateGlyphAtlas( atlas_context, font_glyph_map); } +static std::shared_ptr CreateGlyphAtlas( + Context& context, + const TypographerContext* typographer_context, + HostBuffer& host_buffer, + GlyphAtlas::Type type, + Scalar scale, + const std::shared_ptr& atlas_context, + const std::vector>& frames) { + FontGlyphMap font_glyph_map; + for (auto& frame : frames) { + frame->CollectUniqueFontGlyphPairs(font_glyph_map, scale); + } + return typographer_context->CreateGlyphAtlas(context, type, host_buffer, + atlas_context, font_glyph_map); +} + TEST_P(TypographerTest, CanConvertTextBlob) { SkFont font = flutter::testing::CreateTestFontOfSize(12); auto blob = SkTextBlob::MakeFromString( @@ -281,6 +299,61 @@ TEST_P(TypographerTest, MaybeHasOverlapping) { ASSERT_FALSE(frame_2->MaybeHasOverlapping()); } +TEST_P(TypographerTest, GlyphColorIsPartOfCacheKey) { + auto host_buffer = HostBuffer::Create(GetContext()->GetResourceAllocator()); +#if FML_OS_MACOSX + auto mapping = flutter::testing::OpenFixtureAsSkData("Apple Color Emoji.ttc"); +#else + auto mapping = flutter::testing::OpenFixtureAsSkData("NotoColorEmoji.ttf"); +#endif + ASSERT_TRUE(mapping); + sk_sp font_mgr = txt::GetDefaultFontManager(); + SkFont emoji_font(font_mgr->makeFromData(mapping), 50.0); + + auto context = TypographerContextSkia::Make(); + auto atlas_context = + context->CreateGlyphAtlasContext(GlyphAtlas::Type::kColorBitmap); + + // Create two frames with the same character and a different color, expect + // that it adds a character. + auto frame = MakeTextFrameFromTextBlobSkia( + SkTextBlob::MakeFromString("😂", emoji_font), flutter::DlColor::kCyan()); + auto frame_2 = MakeTextFrameFromTextBlobSkia( + SkTextBlob::MakeFromString("😂", emoji_font), + flutter::DlColor::kMagenta()); + + auto next_atlas = CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer, + GlyphAtlas::Type::kColorBitmap, 1.0f, + atlas_context, {frame, frame_2}); + + EXPECT_EQ(next_atlas->GetGlyphCount(), 2u); +} + +TEST_P(TypographerTest, GlyphColorIsIgnoredForNonEmojiFonts) { + auto host_buffer = HostBuffer::Create(GetContext()->GetResourceAllocator()); + sk_sp font_mgr = txt::GetDefaultFontManager(); + sk_sp typeface = + font_mgr->matchFamilyStyle("Arial", SkFontStyle::Normal()); + SkFont sk_font(typeface, 0.5f); + + auto context = TypographerContextSkia::Make(); + auto atlas_context = + context->CreateGlyphAtlasContext(GlyphAtlas::Type::kColorBitmap); + + // Create two frames with the same character and a different color, but as a + // non-emoji font the text frame constructor will ignore it. + auto frame = MakeTextFrameFromTextBlobSkia( + SkTextBlob::MakeFromString("A", sk_font), flutter::DlColor::kCyan()); + auto frame_2 = MakeTextFrameFromTextBlobSkia( + SkTextBlob::MakeFromString("A", sk_font), flutter::DlColor::kMagenta()); + + auto next_atlas = CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer, + GlyphAtlas::Type::kColorBitmap, 1.0f, + atlas_context, {frame, frame_2}); + + EXPECT_EQ(next_atlas->GetGlyphCount(), 1u); +} + TEST_P(TypographerTest, RectanglePackerAddsNonoverlapingRectangles) { auto packer = RectanglePacker::Factory(200, 100); ASSERT_NE(packer, nullptr); diff --git a/engine/src/flutter/third_party/txt/src/skia/paragraph_skia.cc b/engine/src/flutter/third_party/txt/src/skia/paragraph_skia.cc index ca03a4a62b..2c3b588ad1 100644 --- a/engine/src/flutter/third_party/txt/src/skia/paragraph_skia.cc +++ b/engine/src/flutter/third_party/txt/src/skia/paragraph_skia.cc @@ -93,7 +93,7 @@ class DisplayListParagraphPainter : public skt::ParagraphPainter { return; } - builder_->DrawTextFrame(impeller::MakeTextFrameFromTextBlobSkia(blob), x, + builder_->DrawTextFrame(impeller::MakeTextFrameFromTextBlobSkia(blob, dl_paints_[paint_id].getColor()), x, y, dl_paints_[paint_id]); return; } @@ -116,7 +116,7 @@ class DisplayListParagraphPainter : public skt::ParagraphPainter { paint.setMaskFilter(&filter); } if (impeller_enabled_) { - builder_->DrawTextFrame(impeller::MakeTextFrameFromTextBlobSkia(blob), x, + builder_->DrawTextFrame(impeller::MakeTextFrameFromTextBlobSkia(blob, paint.getColor()), x, y, paint); return; }