diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index cd0aa0f907..3d56c397a0 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -18,6 +18,7 @@ #include "impeller/aiks/color_filter.h" #include "impeller/aiks/image.h" #include "impeller/aiks/image_filter.h" +#include "impeller/aiks/paint_pass_delegate.h" #include "impeller/aiks/testing/context_spy.h" #include "impeller/core/device_buffer.h" #include "impeller/entity/contents/solid_color_contents.h" @@ -631,7 +632,6 @@ TEST_P(AiksTest, CanRenderRoundedRectWithNonUniformRadii) { } struct TextRenderOptions { - bool stroke = false; Scalar font_size = 50; Color color = Color::Yellow(); Point position = Vector2(100, 200); @@ -671,9 +671,6 @@ bool RenderTextInCanvasSkia(const std::shared_ptr& context, Paint text_paint; text_paint.color = options.color; text_paint.mask_blur_descriptor = options.mask_blur_descriptor; - text_paint.stroke_width = 1; - text_paint.style = - options.stroke ? Paint::Style::kStroke : Paint::Style::kFill; canvas.DrawTextFrame(frame, options.position, text_paint); return true; } @@ -717,18 +714,6 @@ TEST_P(AiksTest, CanRenderTextFrame) { ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } -TEST_P(AiksTest, CanRenderStrokedTextFrame) { - Canvas canvas; - canvas.DrawPaint({.color = Color(0.1, 0.1, 0.1, 1.0)}); - ASSERT_TRUE(RenderTextInCanvasSkia( - GetContext(), canvas, "the quick brown fox jumped over the lazy dog!.?", - "Roboto-Regular.ttf", - { - .stroke = true, - })); - ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); -} - TEST_P(AiksTest, CanRenderTextFrameWithHalfScaling) { Canvas canvas; canvas.DrawPaint({.color = Color(0.1, 0.1, 0.1, 1.0)}); diff --git a/engine/src/flutter/impeller/aiks/canvas.cc b/engine/src/flutter/impeller/aiks/canvas.cc index f396c42e25..bc8d1af742 100644 --- a/engine/src/flutter/impeller/aiks/canvas.cc +++ b/engine/src/flutter/impeller/aiks/canvas.cc @@ -889,16 +889,9 @@ void Canvas::DrawTextFrame(const std::shared_ptr& text_frame, auto text_contents = std::make_shared(); text_contents->SetTextFrame(text_frame); + text_contents->SetColor(paint.color); text_contents->SetForceTextColor(paint.mask_blur_descriptor.has_value()); text_contents->SetOffset(position); - text_contents->SetColor(paint.color); - text_contents->SetTextProperties(paint.color, // - paint.style == Paint::Style::kStroke, // - paint.stroke_width, // - paint.stroke_cap, // - paint.stroke_join, // - paint.stroke_miter // - ); entity.SetTransform(GetCurrentTransform() * Matrix::MakeTranslation(position)); diff --git a/engine/src/flutter/impeller/aiks/experimental_canvas.cc b/engine/src/flutter/impeller/aiks/experimental_canvas.cc index d3c547bb3a..27e27c15b0 100644 --- a/engine/src/flutter/impeller/aiks/experimental_canvas.cc +++ b/engine/src/flutter/impeller/aiks/experimental_canvas.cc @@ -388,16 +388,9 @@ void ExperimentalCanvas::DrawTextFrame( auto text_contents = std::make_shared(); text_contents->SetTextFrame(text_frame); + text_contents->SetColor(paint.color); text_contents->SetForceTextColor(paint.mask_blur_descriptor.has_value()); text_contents->SetScale(GetCurrentTransform().GetMaxBasisLengthXY()); - text_contents->SetColor(paint.color); - text_contents->SetTextProperties(paint.color, // - paint.style == Paint::Style::kStroke, // - paint.stroke_width, // - paint.stroke_cap, // - paint.stroke_join, // - paint.stroke_miter // - ); entity.SetTransform(GetCurrentTransform() * Matrix::MakeTranslation(position)); diff --git a/engine/src/flutter/impeller/display_list/dl_dispatcher.cc b/engine/src/flutter/impeller/display_list/dl_dispatcher.cc index 5c5e30b53a..1cef9d590f 100644 --- a/engine/src/flutter/impeller/display_list/dl_dispatcher.cc +++ b/engine/src/flutter/impeller/display_list/dl_dispatcher.cc @@ -28,7 +28,6 @@ #include "impeller/geometry/path_builder.h" #include "impeller/geometry/scalar.h" #include "impeller/geometry/sigma.h" -#include "impeller/typographer/font_glyph_pair.h" #if IMPELLER_ENABLE_3D #include "impeller/entity/contents/scene_contents.h" @@ -1267,22 +1266,8 @@ void TextFrameDispatcher::drawTextFrame( const std::shared_ptr& text_frame, SkScalar x, SkScalar y) { - GlyphProperties properties; - if (paint_.style == Paint::Style::kStroke) { - properties.stroke = true; - properties.stroke_cap = paint_.stroke_cap; - properties.stroke_join = paint_.stroke_join; - properties.stroke_miter = paint_.stroke_miter; - properties.stroke_width = paint_.stroke_width; - } - if (text_frame->HasColor()) { - properties.color = paint_.color; - } - renderer_.GetLazyGlyphAtlas()->AddTextFrame(*text_frame, // - matrix_.GetMaxBasisLengthXY(), // - Point(x, y), // - properties // - ); + renderer_.GetLazyGlyphAtlas()->AddTextFrame( + *text_frame, matrix_.GetMaxBasisLengthXY(), Point(x, y)); } void TextFrameDispatcher::drawDisplayList( @@ -1295,59 +1280,4 @@ void TextFrameDispatcher::drawDisplayList( FML_DCHECK(stack_depth == stack_.size()); } -// |flutter::DlOpReceiver| -void TextFrameDispatcher::setDrawStyle(flutter::DlDrawStyle style) { - paint_.style = ToStyle(style); -} - -// |flutter::DlOpReceiver| -void TextFrameDispatcher::setColor(flutter::DlColor color) { - paint_.color = { - color.getRedF(), - color.getGreenF(), - color.getBlueF(), - color.getAlphaF(), - }; -} - -// |flutter::DlOpReceiver| -void TextFrameDispatcher::setStrokeWidth(SkScalar width) { - paint_.stroke_width = width; -} - -// |flutter::DlOpReceiver| -void TextFrameDispatcher::setStrokeMiter(SkScalar limit) { - paint_.stroke_miter = limit; -} - -// |flutter::DlOpReceiver| -void TextFrameDispatcher::setStrokeCap(flutter::DlStrokeCap cap) { - switch (cap) { - case flutter::DlStrokeCap::kButt: - paint_.stroke_cap = Cap::kButt; - break; - case flutter::DlStrokeCap::kRound: - paint_.stroke_cap = Cap::kRound; - break; - case flutter::DlStrokeCap::kSquare: - paint_.stroke_cap = Cap::kSquare; - break; - } -} - -// |flutter::DlOpReceiver| -void TextFrameDispatcher::setStrokeJoin(flutter::DlStrokeJoin join) { - switch (join) { - case flutter::DlStrokeJoin::kMiter: - paint_.stroke_join = Join::kMiter; - break; - case flutter::DlStrokeJoin::kRound: - paint_.stroke_join = Join::kRound; - break; - case flutter::DlStrokeJoin::kBevel: - paint_.stroke_join = Join::kBevel; - break; - } -} - } // namespace impeller diff --git a/engine/src/flutter/impeller/display_list/dl_dispatcher.h b/engine/src/flutter/impeller/display_list/dl_dispatcher.h index b089b1e0dc..3382af0971 100644 --- a/engine/src/flutter/impeller/display_list/dl_dispatcher.h +++ b/engine/src/flutter/impeller/display_list/dl_dispatcher.h @@ -368,29 +368,10 @@ class TextFrameDispatcher : public flutter::IgnoreAttributeDispatchHelper, void drawDisplayList(const sk_sp display_list, SkScalar opacity) override; - // |flutter::DlOpReceiver| - void setDrawStyle(flutter::DlDrawStyle style) override; - - // |flutter::DlOpReceiver| - void setColor(flutter::DlColor color) override; - - // |flutter::DlOpReceiver| - void setStrokeWidth(SkScalar width) override; - - // |flutter::DlOpReceiver| - void setStrokeMiter(SkScalar limit) override; - - // |flutter::DlOpReceiver| - void setStrokeCap(flutter::DlStrokeCap cap) override; - - // |flutter::DlOpReceiver| - void setStrokeJoin(flutter::DlStrokeJoin join) override; - private: const ContentContext& renderer_; Matrix matrix_; std::vector stack_; - Paint paint_; }; } // namespace impeller diff --git a/engine/src/flutter/impeller/entity/contents/text_contents.cc b/engine/src/flutter/impeller/entity/contents/text_contents.cc index b4cbdbabf1..d4b0d813a2 100644 --- a/engine/src/flutter/impeller/entity/contents/text_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/text_contents.cc @@ -66,30 +66,10 @@ std::optional TextContents::GetCoverage(const Entity& entity) const { void TextContents::PopulateGlyphAtlas( const std::shared_ptr& lazy_glyph_atlas, Scalar scale) { - lazy_glyph_atlas->AddTextFrame(*frame_, scale, offset_, properties_); + lazy_glyph_atlas->AddTextFrame(*frame_, scale, offset_); scale_ = scale; } -void TextContents::SetTextProperties(Color color, - bool stroke, - Scalar stroke_width, - Cap stroke_cap, - Join stroke_join, - Scalar stroke_miter) { - if (frame_->HasColor()) { - // Alpha is always applied when rendering, remove it here so - // we do not double-apply the alpha. - properties_.color = color.WithAlpha(1.0); - } - if (stroke) { - properties_.stroke = true; - properties_.stroke_width = stroke_width; - properties_.stroke_cap = stroke_cap; - properties_.stroke_join = stroke_join; - properties_.stroke_miter = stroke_miter; - } -} - bool TextContents::Render(const ContentContext& renderer, const Entity& entity, RenderPass& pass) const { @@ -191,7 +171,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; @@ -223,7 +203,7 @@ bool TextContents::Render(const ContentContext& renderer, glyph_position, font.GetAxisAlignment(), offset_, scale_); std::optional> maybe_atlas_glyph_bounds = font_atlas->FindGlyphBounds( - SubpixelGlyph{glyph_position.glyph, subpixel, properties_}); + SubpixelGlyph{glyph_position.glyph, subpixel}); if (!maybe_atlas_glyph_bounds.has_value()) { VALIDATION_LOG << "Could not find glyph position in the atlas."; continue; diff --git a/engine/src/flutter/impeller/entity/contents/text_contents.h b/engine/src/flutter/impeller/entity/contents/text_contents.h index 807b02267e..f076e09f37 100644 --- a/engine/src/flutter/impeller/entity/contents/text_contents.h +++ b/engine/src/flutter/impeller/entity/contents/text_contents.h @@ -9,7 +9,7 @@ #include "impeller/entity/contents/contents.h" #include "impeller/geometry/color.h" -#include "impeller/typographer/font_glyph_pair.h" +#include "impeller/typographer/glyph_atlas.h" #include "impeller/typographer/text_frame.h" namespace impeller { @@ -33,14 +33,6 @@ class TextContents final : public Contents { /// This is used to ensure that mask blurs work correctly on emoji. void SetForceTextColor(bool value); - /// Must be set after text frame. - void SetTextProperties(Color color, - bool stroke, - Scalar stroke_width, - Cap stroke_cap, - Join stroke_join, - Scalar stroke_miter); - Color GetColor() const; // |Contents| @@ -72,11 +64,10 @@ class TextContents final : public Contents { private: std::shared_ptr frame_; Scalar scale_ = 1.0; + Color color_; Scalar inherited_opacity_ = 1.0; Vector2 offset_; bool force_text_color_ = false; - Color color_; - GlyphProperties properties_; TextContents(const TextContents&) = delete; 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 982fdd7017..cd79684c7b 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 "impeller/typographer/font.h" @@ -38,6 +39,15 @@ static AxisAlignment ToAxisAligment(SkAxisAlignment aligment) { FML_UNREACHABLE(); } +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, AxisAlignment alignment) { auto& font = run.font(); auto typeface = std::make_shared(font.refTypeface()); @@ -59,8 +69,10 @@ static Rect ToRect(const SkRect& rect) { } 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. @@ -102,7 +114,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 fb69bab359..99ff93f854 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,6 +5,7 @@ #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" @@ -12,7 +13,8 @@ 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 c743d00307..5002c67df5 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 @@ -33,7 +33,6 @@ #include "impeller/typographer/typographer_context.h" #include "include/core/SkColor.h" #include "include/core/SkImageInfo.h" -#include "include/core/SkPaint.h" #include "include/core/SkSize.h" #include "third_party/skia/include/core/SkBitmap.h" @@ -49,32 +48,6 @@ namespace impeller { // https://github.com/flutter/flutter/issues/114563 constexpr auto kPadding = 2; -namespace { -SkPaint::Cap ToSkiaCap(Cap cap) { - switch (cap) { - case Cap::kButt: - return SkPaint::Cap::kButt_Cap; - case Cap::kRound: - return SkPaint::Cap::kRound_Cap; - case Cap::kSquare: - return SkPaint::Cap::kSquare_Cap; - } - FML_UNREACHABLE(); -} - -SkPaint::Join ToSkiaJoin(Join join) { - switch (join) { - case Join::kMiter: - return SkPaint::Join::kMiter_Join; - case Join::kRound: - return SkPaint::Join::kRound_Join; - case Join::kBevel: - return SkPaint::Join::kBevel_Join; - } - FML_UNREACHABLE(); -} -} // namespace - std::shared_ptr TypographerContextSkia::Make() { return std::make_shared(); } @@ -210,7 +183,6 @@ static void DrawGlyph(SkCanvas* canvas, const ScaledFont& scaled_font, const SubpixelGlyph& glyph, const Rect& scaled_bounds, - const GlyphProperties& prop, bool has_color) { const auto& metrics = scaled_font.font.GetMetrics(); SkPoint position = SkPoint::Make(1, 1); @@ -225,19 +197,11 @@ static void DrawGlyph(SkCanvas* canvas, sk_font.setSubpixel(true); sk_font.setSize(sk_font.getSize() * scaled_font.scale); - auto glyph_color = - has_color ? glyph.properties.color.ToARGB() : SK_ColorBLACK; + auto glyph_color = has_color ? scaled_font.color.ToARGB() : SK_ColorBLACK; SkPaint glyph_paint; glyph_paint.setColor(glyph_color); glyph_paint.setBlendMode(SkBlendMode::kSrc); - if (prop.stroke) { - glyph_paint.setStroke(true); - glyph_paint.setStrokeWidth(prop.stroke_width * scaled_font.scale); - glyph_paint.setStrokeCap(ToSkiaCap(glyph.properties.stroke_cap)); - glyph_paint.setStrokeJoin(ToSkiaJoin(glyph.properties.stroke_join)); - glyph_paint.setStrokeMiter(prop.stroke_miter * scaled_font.scale); - } canvas->translate(glyph.subpixel_offset.x, glyph.subpixel_offset.y); canvas->drawGlyphs(1u, // count &glyph_id, // glyphs @@ -291,8 +255,7 @@ static bool UpdateAtlasBitmap(const GlyphAtlas& atlas, return false; } - DrawGlyph(canvas, pair.scaled_font, pair.glyph, bounds, - pair.glyph.properties, has_color); + DrawGlyph(canvas, pair.scaled_font, pair.glyph, bounds, has_color); // Writing to a malloc'd buffer and then copying to the staging buffers // benchmarks as substantially faster on a number of Android devices. @@ -319,19 +282,9 @@ static bool UpdateAtlasBitmap(const GlyphAtlas& atlas, return blit_pass->ConvertTextureToShaderRead(texture); } -static Rect ComputeGlyphSize(const SkFont& font, - const SubpixelGlyph& glyph, - Scalar scale) { +static Rect ComputeGlyphSize(const SkFont& font, const SubpixelGlyph& glyph) { SkRect scaled_bounds; - SkPaint glyph_paint; - if (glyph.properties.stroke) { - glyph_paint.setStroke(true); - glyph_paint.setStrokeWidth(glyph.properties.stroke_width * scale); - glyph_paint.setStrokeCap(ToSkiaCap(glyph.properties.stroke_cap)); - glyph_paint.setStrokeJoin(ToSkiaJoin(glyph.properties.stroke_join)); - glyph_paint.setStrokeMiter(glyph.properties.stroke_miter * scale); - } - font.getBounds(&glyph.glyph.index, 1, &scaled_bounds, &glyph_paint); + font.getBounds(&glyph.glyph.index, 1, &scaled_bounds, nullptr); // Expand the bounds of glyphs at subpixel offsets by 2 in the x direction. Scalar adjustment = 0.0; @@ -369,8 +322,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); auto metrics = scaled_font.font.GetMetrics(); @@ -390,15 +343,13 @@ std::shared_ptr TypographerContextSkia::CreateGlyphAtlas( for (const SubpixelGlyph& glyph : font_value.second) { if (!font_glyph_atlas->FindGlyphBounds(glyph)) { new_glyphs.emplace_back(scaled_font, glyph); - glyph_sizes.push_back( - ComputeGlyphSize(sk_font, glyph, scaled_font.scale)); + glyph_sizes.push_back(ComputeGlyphSize(sk_font, glyph)); } } } else { for (const SubpixelGlyph& glyph : font_value.second) { new_glyphs.emplace_back(scaled_font, glyph); - glyph_sizes.push_back( - ComputeGlyphSize(sk_font, glyph, scaled_font.scale)); + glyph_sizes.push_back(ComputeGlyphSize(sk_font, 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 773948bf6a..5ec1defc97 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 @@ -65,7 +65,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 c73cd47148..75f5e2c2df 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 @@ -428,8 +428,8 @@ std::shared_ptr TypographerContextSTB::CreateGlyphAtlas( std::vector new_sizes; 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 SubpixelGlyph& 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 c5d3c508f4..2e79ade535 100644 --- a/engine/src/flutter/impeller/typographer/font_glyph_pair.h +++ b/engine/src/flutter/impeller/typographer/font_glyph_pair.h @@ -9,22 +9,12 @@ #include #include "impeller/geometry/color.h" -#include "impeller/geometry/path.h" #include "impeller/geometry/point.h" #include "impeller/typographer/font.h" #include "impeller/typographer/glyph.h" namespace impeller { -struct GlyphProperties { - Color color = Color::Black(); - Scalar stroke_width = 0.0; - Cap stroke_cap = Cap::kButt; - Join stroke_join = Join::kMiter; - Scalar stroke_miter = 4.0; - bool stroke = false; -}; - //------------------------------------------------------------------------------ /// @brief A font and a scale. Used as a key that represents a typeface /// within a glyph atlas. @@ -32,6 +22,7 @@ struct GlyphProperties { struct ScaledFont { Font font; Scalar scale; + Color color; }; //------------------------------------------------------------------------------ @@ -40,14 +31,9 @@ struct ScaledFont { struct SubpixelGlyph { Glyph glyph; Point subpixel_offset; - GlyphProperties properties; - SubpixelGlyph(Glyph p_glyph, - Point p_subpixel_offset, - GlyphProperties p_properties) - : glyph(p_glyph), - subpixel_offset(p_subpixel_offset), - properties(p_properties) {} + SubpixelGlyph(Glyph p_glyph, Point p_subpixel_offset) + : glyph(p_glyph), subpixel_offset(p_subpixel_offset) {} }; using FontGlyphMap = @@ -69,7 +55,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()); } }; @@ -77,18 +63,16 @@ 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; } }; template <> struct std::hash { constexpr std::size_t operator()(const impeller::SubpixelGlyph& sg) const { - return fml::HashCombine( - sg.glyph.index, sg.subpixel_offset.x, sg.subpixel_offset.y, - sg.properties.color.ToARGB(), sg.properties.stroke, - sg.properties.stroke_cap, sg.properties.stroke_join, - sg.properties.stroke_miter, sg.properties.stroke_width); + return fml::HashCombine(sg.glyph.index, sg.subpixel_offset.x, + sg.subpixel_offset.y); } }; @@ -98,13 +82,7 @@ struct std::equal_to { const impeller::SubpixelGlyph& rhs) const { return lhs.glyph.index == rhs.glyph.index && lhs.glyph.type == rhs.glyph.type && - lhs.subpixel_offset == rhs.subpixel_offset && - lhs.properties.color.ToARGB() == rhs.properties.color.ToARGB() && - lhs.properties.stroke == rhs.properties.stroke && - lhs.properties.stroke_cap == rhs.properties.stroke_cap && - lhs.properties.stroke_join == rhs.properties.stroke_join && - lhs.properties.stroke_miter == rhs.properties.stroke_miter && - lhs.properties.stroke_width == rhs.properties.stroke_width; + lhs.subpixel_offset == rhs.subpixel_offset; } }; diff --git a/engine/src/flutter/impeller/typographer/glyph_atlas.cc b/engine/src/flutter/impeller/typographer/glyph_atlas.cc index 77b8dfd923..59a3f3276b 100644 --- a/engine/src/flutter/impeller/typographer/glyph_atlas.cc +++ b/engine/src/flutter/impeller/typographer/glyph_atlas.cc @@ -80,8 +80,9 @@ std::optional> GlyphAtlas::FindFontGlyphBounds( } const FontGlyphAtlas* GlyphAtlas::GetFontGlyphAtlas(const Font& font, - Scalar scale) const { - const auto& found = font_atlas_map_.find(ScaledFont{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 b94fe90e6a..98a6556da2 100644 --- a/engine/src/flutter/impeller/typographer/glyph_atlas.h +++ b/engine/src/flutter/impeller/typographer/glyph_atlas.h @@ -130,7 +130,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/lazy_glyph_atlas.cc b/engine/src/flutter/impeller/typographer/lazy_glyph_atlas.cc index be5688f6a7..5e6c164e9a 100644 --- a/engine/src/flutter/impeller/typographer/lazy_glyph_atlas.cc +++ b/engine/src/flutter/impeller/typographer/lazy_glyph_atlas.cc @@ -32,15 +32,12 @@ LazyGlyphAtlas::~LazyGlyphAtlas() = default; void LazyGlyphAtlas::AddTextFrame(const TextFrame& frame, Scalar scale, - Point offset, - const GlyphProperties& properties) { + Point offset) { FML_DCHECK(alpha_atlas_ == nullptr && color_atlas_ == nullptr); if (frame.GetAtlasType() == GlyphAtlas::Type::kAlphaBitmap) { - frame.CollectUniqueFontGlyphPairs(alpha_glyph_map_, scale, offset, - properties); + frame.CollectUniqueFontGlyphPairs(alpha_glyph_map_, scale, offset); } else { - frame.CollectUniqueFontGlyphPairs(color_glyph_map_, scale, offset, - properties); + frame.CollectUniqueFontGlyphPairs(color_glyph_map_, scale, offset); } } diff --git a/engine/src/flutter/impeller/typographer/lazy_glyph_atlas.h b/engine/src/flutter/impeller/typographer/lazy_glyph_atlas.h index bead4bda98..6fa2e543ee 100644 --- a/engine/src/flutter/impeller/typographer/lazy_glyph_atlas.h +++ b/engine/src/flutter/impeller/typographer/lazy_glyph_atlas.h @@ -19,10 +19,7 @@ class LazyGlyphAtlas { ~LazyGlyphAtlas(); - void AddTextFrame(const TextFrame& frame, - Scalar scale, - Point offset, - const GlyphProperties& properties); + void AddTextFrame(const TextFrame& frame, Scalar scale, Point offset); void ResetTextFrames(); diff --git a/engine/src/flutter/impeller/typographer/text_frame.cc b/engine/src/flutter/impeller/typographer/text_frame.cc index a0ce36d683..a209647d33 100644 --- a/engine/src/flutter/impeller/typographer/text_frame.cc +++ b/engine/src/flutter/impeller/typographer/text_frame.cc @@ -10,8 +10,14 @@ 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; @@ -32,8 +38,8 @@ GlyphAtlas::Type TextFrame::GetAtlasType() const { : GlyphAtlas::Type::kAlphaBitmap; } -bool TextFrame::HasColor() const { - return has_color_; +Color TextFrame::GetColor() const { + return color_; } // static @@ -84,21 +90,19 @@ Point TextFrame::ComputeSubpixelPosition( } } -void TextFrame::CollectUniqueFontGlyphPairs( - FontGlyphMap& glyph_map, - Scalar scale, - Point offset, - const GlyphProperties& properties) const { +void TextFrame::CollectUniqueFontGlyphPairs(FontGlyphMap& glyph_map, + Scalar scale, + Point offset) const { for (const TextRun& run : GetRuns()) { const Font& font = run.GetFont(); auto rounded_scale = RoundScaledFontSize(scale, font.GetMetrics().point_size); - auto& set = glyph_map[ScaledFont{font, rounded_scale}]; + auto& set = glyph_map[ScaledFont{font, rounded_scale, color_}]; for (const TextRun::GlyphPosition& glyph_position : run.GetGlyphPositions()) { Point subpixel = ComputeSubpixelPosition( glyph_position, font.GetAxisAlignment(), offset, scale); - set.emplace(glyph_position.glyph, subpixel, properties); + set.emplace(glyph_position.glyph, subpixel); } } } diff --git a/engine/src/flutter/impeller/typographer/text_frame.h b/engine/src/flutter/impeller/typographer/text_frame.h index b742e96e2a..7a9b89017d 100644 --- a/engine/src/flutter/impeller/typographer/text_frame.h +++ b/engine/src/flutter/impeller/typographer/text_frame.h @@ -20,14 +20,16 @@ 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(); void CollectUniqueFontGlyphPairs(FontGlyphMap& glyph_map, Scalar scale, - Point offset, - const GlyphProperties& properties) const; + Point offset) const; static Point ComputeSubpixelPosition( const TextRun::GlyphPosition& glyph_position, @@ -66,7 +68,7 @@ class TextFrame { /// COLR fonts can potentially use the paint color in the glyph /// atlas, so this color must be considered as part of the cache /// key. - bool HasColor() const; + Color GetColor() const; //---------------------------------------------------------------------------- /// @brief The type of atlas this run should be emplaced in. @@ -80,6 +82,7 @@ class TextFrame { std::vector runs_; Rect bounds_; 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 928cba18f0..a2d1813d37 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" @@ -38,7 +39,7 @@ static std::shared_ptr CreateGlyphAtlas( const std::shared_ptr& atlas_context, const TextFrame& frame) { FontGlyphMap font_glyph_map; - frame.CollectUniqueFontGlyphPairs(font_glyph_map, scale, {0, 0}, {}); + frame.CollectUniqueFontGlyphPairs(font_glyph_map, scale, {0, 0}); return typographer_context->CreateGlyphAtlas(context, type, host_buffer, atlas_context, font_glyph_map); } @@ -50,13 +51,10 @@ static std::shared_ptr CreateGlyphAtlas( GlyphAtlas::Type type, Scalar scale, const std::shared_ptr& atlas_context, - const std::vector>& frames, - const std::vector& properties) { + const std::vector>& frames) { FontGlyphMap font_glyph_map; - size_t offset = 0; for (auto& frame : frames) { - frame->CollectUniqueFontGlyphPairs(font_glyph_map, scale, {0, 0}, - properties[offset++]); + frame->CollectUniqueFontGlyphPairs(font_glyph_map, scale, {0, 0}); } return typographer_context->CreateGlyphAtlas(context, type, host_buffer, atlas_context, font_glyph_map); @@ -137,14 +135,14 @@ TEST_P(TypographerTest, LazyAtlasTracksColor) { LazyGlyphAtlas lazy_atlas(TypographerContextSkia::Make()); - lazy_atlas.AddTextFrame(*frame, 1.0f, {0, 0}, {}); + lazy_atlas.AddTextFrame(*frame, 1.0f, {0, 0}); frame = MakeTextFrameFromTextBlobSkia( SkTextBlob::MakeFromString("😀 ", emoji_font)); ASSERT_TRUE(frame->GetAtlasType() == GlyphAtlas::Type::kColorBitmap); - lazy_atlas.AddTextFrame(*frame, 1.0f, {0, 0}, {}); + lazy_atlas.AddTextFrame(*frame, 1.0f, {0, 0}); // Creates different atlases for color and red bitmap. auto color_atlas = lazy_atlas.CreateOrGetGlyphAtlas( @@ -224,7 +222,7 @@ TEST_P(TypographerTest, GlyphAtlasWithLotsOfdUniqueGlyphSize) { size_t size_count = 8; for (size_t index = 0; index < size_count; index += 1) { MakeTextFrameFromTextBlobSkia(blob)->CollectUniqueFontGlyphPairs( - font_glyph_map, 0.6 * index, {0, 0}, {}); + font_glyph_map, 0.6 * index, {0, 0}); }; auto atlas = context->CreateGlyphAtlas(*GetContext(), GlyphAtlas::Type::kAlphaBitmap, @@ -305,18 +303,14 @@ TEST_P(TypographerTest, GlyphColorIsPartOfCacheKey) { // Create two frames with the same character and a different color, expect // that it adds a character. auto frame = MakeTextFrameFromTextBlobSkia( - SkTextBlob::MakeFromString("😂", emoji_font)); + SkTextBlob::MakeFromString("😂", emoji_font), flutter::DlColor::kCyan()); auto frame_2 = MakeTextFrameFromTextBlobSkia( - SkTextBlob::MakeFromString("😂", emoji_font)); - auto properties = { - GlyphProperties{.color = Color::Red()}, - GlyphProperties{.color = Color::Blue()}, - }; + 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}, properties); + 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); } @@ -334,19 +328,14 @@ TEST_P(TypographerTest, GlyphColorIsIgnoredForNonEmojiFonts) { // 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)); - auto frame_2 = - MakeTextFrameFromTextBlobSkia(SkTextBlob::MakeFromString("A", sk_font)); - auto properties = { - GlyphProperties{}, - GlyphProperties{}, - }; + 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}, properties); + 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); } diff --git a/engine/src/flutter/testing/impeller_golden_tests_output.txt b/engine/src/flutter/testing/impeller_golden_tests_output.txt index 79a7aff48b..c3c1caba83 100644 --- a/engine/src/flutter/testing/impeller_golden_tests_output.txt +++ b/engine/src/flutter/testing/impeller_golden_tests_output.txt @@ -406,9 +406,6 @@ impeller_Play_AiksTest_CanRenderStrokePathThatEndsAtSharpTurn_Vulkan.png impeller_Play_AiksTest_CanRenderStrokePathWithCubicLine_Metal.png impeller_Play_AiksTest_CanRenderStrokePathWithCubicLine_OpenGLES.png impeller_Play_AiksTest_CanRenderStrokePathWithCubicLine_Vulkan.png -impeller_Play_AiksTest_CanRenderStrokedTextFrame_Metal.png -impeller_Play_AiksTest_CanRenderStrokedTextFrame_OpenGLES.png -impeller_Play_AiksTest_CanRenderStrokedTextFrame_Vulkan.png impeller_Play_AiksTest_CanRenderStrokes_Metal.png impeller_Play_AiksTest_CanRenderStrokes_OpenGLES.png impeller_Play_AiksTest_CanRenderStrokes_Vulkan.png 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 ec5da9c63c..1465289a09 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 @@ -102,8 +102,9 @@ class DisplayListParagraphPainter : public skt::ParagraphPainter { builder_->DrawPath(transformed, dl_paints_[paint_id]); return; } - builder_->DrawTextFrame(impeller::MakeTextFrameFromTextBlobSkia(blob), x, - y, dl_paints_[paint_id]); + builder_->DrawTextFrame(impeller::MakeTextFrameFromTextBlobSkia( + blob, dl_paints_[paint_id].getColor()), + x, y, dl_paints_[paint_id]); return; } #endif // IMPELLER_SUPPORTS_RENDERING @@ -125,8 +126,9 @@ class DisplayListParagraphPainter : public skt::ParagraphPainter { paint.setMaskFilter(&filter); } if (impeller_enabled_) { - builder_->DrawTextFrame(impeller::MakeTextFrameFromTextBlobSkia(blob), x, - y, paint); + builder_->DrawTextFrame( + impeller::MakeTextFrameFromTextBlobSkia(blob, paint.getColor()), x, y, + paint); return; } builder_->DrawTextBlob(blob, x, y, paint); @@ -217,11 +219,12 @@ class DisplayListParagraphPainter : public skt::ParagraphPainter { bool ShouldRenderAsPath(const DlPaint& paint) const { FML_DCHECK(impeller_enabled_); - // Text with non-trivial color sources should be rendered as a path when - // running on Impeller for correctness. These filters rely on having the - // glyph coverage, whereas regular text is drawn as rectangular texture - // samples. - return (paint.getColorSource() && !paint.getColorSource()->asColor()); + // Text with non-trivial color sources or stroke paint mode should be + // rendered as a path when running on Impeller for correctness. These + // filters rely on having the glyph coverage, whereas regular text is + // drawn as rectangular texture samples. + return ((paint.getColorSource() && !paint.getColorSource()->asColor()) || + paint.getDrawStyle() != DlDrawStyle::kFill); } DlPaint toDlPaint(const DecorationStyle& decor_style, diff --git a/engine/src/flutter/third_party/txt/tests/paragraph_unittests.cc b/engine/src/flutter/third_party/txt/tests/paragraph_unittests.cc index dd5446aa12..181a7ef035 100644 --- a/engine/src/flutter/third_party/txt/tests/paragraph_unittests.cc +++ b/engine/src/flutter/third_party/txt/tests/paragraph_unittests.cc @@ -253,9 +253,9 @@ TEST_F(PainterTest, DrawStrokedTextImpeller) { auto recorder = DlOpRecorder(); draw(style)->Dispatch(recorder); - EXPECT_EQ(recorder.textFrameCount(), 1); + EXPECT_EQ(recorder.textFrameCount(), 0); EXPECT_EQ(recorder.blobCount(), 0); - EXPECT_EQ(recorder.pathCount(), 0); + EXPECT_EQ(recorder.pathCount(), 1); } TEST_F(PainterTest, DrawTextWithGradientImpeller) {