From ee3a8d0775901281e18c5e697303c722346ed3a8 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Wed, 5 Jun 2024 16:54:48 -0700 Subject: [PATCH] [Impeller] Use Skia software renderer to draw stroked text. (flutter/engine#53198) We need to match the rounding of glyphs to fix https://github.com/flutter/flutter/issues/138670 . We also don't have sufficient AA quality with 4x MSAA for high quality strokes. Fixes https://github.com/flutter/flutter/issues/138670 Fixes https://github.com/flutter/flutter/issues/136688 --- .../flutter/impeller/aiks/aiks_unittests.cc | 17 ++++- engine/src/flutter/impeller/aiks/canvas.cc | 9 ++- .../impeller/aiks/experimental_canvas.cc | 9 ++- .../impeller/display_list/dl_dispatcher.cc | 74 ++++++++++++++++++- .../impeller/display_list/dl_dispatcher.h | 19 +++++ .../impeller/entity/contents/text_contents.cc | 26 ++++++- .../impeller/entity/contents/text_contents.h | 13 +++- .../backends/skia/text_frame_skia.cc | 17 +---- .../backends/skia/text_frame_skia.h | 4 +- .../backends/skia/typographer_context_skia.cc | 65 ++++++++++++++-- .../backends/stb/text_frame_stb.cc | 2 +- .../backends/stb/typographer_context_stb.cc | 4 +- .../impeller/typographer/font_glyph_pair.h | 40 +++++++--- .../impeller/typographer/glyph_atlas.cc | 5 +- .../impeller/typographer/glyph_atlas.h | 4 +- .../impeller/typographer/lazy_glyph_atlas.cc | 9 ++- .../impeller/typographer/lazy_glyph_atlas.h | 5 +- .../impeller/typographer/text_frame.cc | 26 +++---- .../flutter/impeller/typographer/text_frame.h | 11 +-- .../typographer/typographer_unittests.cc | 51 ++++++++----- .../testing/impeller_golden_tests_output.txt | 3 + .../txt/src/skia/paragraph_skia.cc | 21 +++--- .../txt/tests/paragraph_unittests.cc | 4 +- 23 files changed, 324 insertions(+), 114 deletions(-) diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index 3d56c397a0..cd0aa0f907 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -18,7 +18,6 @@ #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" @@ -632,6 +631,7 @@ TEST_P(AiksTest, CanRenderRoundedRectWithNonUniformRadii) { } struct TextRenderOptions { + bool stroke = false; Scalar font_size = 50; Color color = Color::Yellow(); Point position = Vector2(100, 200); @@ -671,6 +671,9 @@ 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; } @@ -714,6 +717,18 @@ 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 bc8d1af742..f396c42e25 100644 --- a/engine/src/flutter/impeller/aiks/canvas.cc +++ b/engine/src/flutter/impeller/aiks/canvas.cc @@ -889,9 +889,16 @@ 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 27e27c15b0..d3c547bb3a 100644 --- a/engine/src/flutter/impeller/aiks/experimental_canvas.cc +++ b/engine/src/flutter/impeller/aiks/experimental_canvas.cc @@ -388,9 +388,16 @@ 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 1cef9d590f..5c5e30b53a 100644 --- a/engine/src/flutter/impeller/display_list/dl_dispatcher.cc +++ b/engine/src/flutter/impeller/display_list/dl_dispatcher.cc @@ -28,6 +28,7 @@ #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" @@ -1266,8 +1267,22 @@ void TextFrameDispatcher::drawTextFrame( const std::shared_ptr& text_frame, SkScalar x, SkScalar y) { - renderer_.GetLazyGlyphAtlas()->AddTextFrame( - *text_frame, matrix_.GetMaxBasisLengthXY(), Point(x, 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 // + ); } void TextFrameDispatcher::drawDisplayList( @@ -1280,4 +1295,59 @@ 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 3382af0971..b089b1e0dc 100644 --- a/engine/src/flutter/impeller/display_list/dl_dispatcher.h +++ b/engine/src/flutter/impeller/display_list/dl_dispatcher.h @@ -368,10 +368,29 @@ 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 d4b0d813a2..b4cbdbabf1 100644 --- a/engine/src/flutter/impeller/entity/contents/text_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/text_contents.cc @@ -66,10 +66,30 @@ 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_); + lazy_glyph_atlas->AddTextFrame(*frame_, scale, offset_, properties_); 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 { @@ -171,7 +191,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, frame_->GetColor()); + atlas->GetFontGlyphAtlas(font, rounded_scale); if (!font_atlas) { VALIDATION_LOG << "Could not find font in the atlas."; continue; @@ -203,7 +223,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}); + SubpixelGlyph{glyph_position.glyph, subpixel, properties_}); 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 f076e09f37..807b02267e 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/glyph_atlas.h" +#include "impeller/typographer/font_glyph_pair.h" #include "impeller/typographer/text_frame.h" namespace impeller { @@ -33,6 +33,14 @@ 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| @@ -64,10 +72,11 @@ 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 cd79684c7b..982fdd7017 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,7 +6,6 @@ #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" @@ -39,15 +38,6 @@ 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()); @@ -69,10 +59,8 @@ static Rect ToRect(const SkRect& rect) { } std::shared_ptr MakeTextFrameFromTextBlobSkia( - const sk_sp& blob, - flutter::DlColor dl_color) { + const sk_sp& blob) { 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. @@ -114,8 +102,7 @@ std::shared_ptr MakeTextFrameFromTextBlobSkia( continue; } } - return std::make_shared(runs, ToRect(blob->bounds()), has_color, - has_color ? color : Color::Black()); + return std::make_shared(runs, ToRect(blob->bounds()), has_color); } } // 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 99ff93f854..fb69bab359 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,7 +5,6 @@ #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" @@ -13,8 +12,7 @@ namespace impeller { std::shared_ptr MakeTextFrameFromTextBlobSkia( - const sk_sp& blob, - flutter::DlColor color = flutter::DlColor::kBlack()); + const sk_sp& blob); } // 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 5002c67df5..c743d00307 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,6 +33,7 @@ #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" @@ -48,6 +49,32 @@ 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(); } @@ -183,6 +210,7 @@ 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); @@ -197,11 +225,19 @@ static void DrawGlyph(SkCanvas* canvas, sk_font.setSubpixel(true); sk_font.setSize(sk_font.getSize() * scaled_font.scale); - auto glyph_color = has_color ? scaled_font.color.ToARGB() : SK_ColorBLACK; + auto glyph_color = + has_color ? glyph.properties.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 @@ -255,7 +291,8 @@ static bool UpdateAtlasBitmap(const GlyphAtlas& atlas, return false; } - DrawGlyph(canvas, pair.scaled_font, pair.glyph, bounds, has_color); + DrawGlyph(canvas, pair.scaled_font, pair.glyph, bounds, + pair.glyph.properties, 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. @@ -282,9 +319,19 @@ static bool UpdateAtlasBitmap(const GlyphAtlas& atlas, return blit_pass->ConvertTextureToShaderRead(texture); } -static Rect ComputeGlyphSize(const SkFont& font, const SubpixelGlyph& glyph) { +static Rect ComputeGlyphSize(const SkFont& font, + const SubpixelGlyph& glyph, + Scalar scale) { SkRect scaled_bounds; - font.getBounds(&glyph.glyph.index, 1, &scaled_bounds, nullptr); + 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); // Expand the bounds of glyphs at subpixel offsets by 2 in the x direction. Scalar adjustment = 0.0; @@ -322,8 +369,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, scaled_font.color); + const FontGlyphAtlas* font_glyph_atlas = + last_atlas->GetFontGlyphAtlas(scaled_font.font, scaled_font.scale); auto metrics = scaled_font.font.GetMetrics(); @@ -343,13 +390,15 @@ 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)); + glyph_sizes.push_back( + ComputeGlyphSize(sk_font, glyph, scaled_font.scale)); } } } else { for (const SubpixelGlyph& glyph : font_value.second) { new_glyphs.emplace_back(scaled_font, glyph); - glyph_sizes.push_back(ComputeGlyphSize(sk_font, glyph)); + glyph_sizes.push_back( + ComputeGlyphSize(sk_font, glyph, scaled_font.scale)); } } } 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 5ec1defc97..773948bf6a 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, Color::Black()); + runs, result.value_or(Rect::MakeLTRB(0, 0, 0, 0)), false); } } // 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 75f5e2c2df..c73cd47148 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, scaled_font.color); + const FontGlyphAtlas* font_glyph_atlas = + last_atlas->GetFontGlyphAtlas(scaled_font.font, scaled_font.scale); 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 2e79ade535..c5d3c508f4 100644 --- a/engine/src/flutter/impeller/typographer/font_glyph_pair.h +++ b/engine/src/flutter/impeller/typographer/font_glyph_pair.h @@ -9,12 +9,22 @@ #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. @@ -22,7 +32,6 @@ namespace impeller { struct ScaledFont { Font font; Scalar scale; - Color color; }; //------------------------------------------------------------------------------ @@ -31,9 +40,14 @@ struct ScaledFont { struct SubpixelGlyph { Glyph glyph; Point subpixel_offset; + GlyphProperties properties; - SubpixelGlyph(Glyph p_glyph, Point p_subpixel_offset) - : glyph(p_glyph), subpixel_offset(p_subpixel_offset) {} + SubpixelGlyph(Glyph p_glyph, + Point p_subpixel_offset, + GlyphProperties p_properties) + : glyph(p_glyph), + subpixel_offset(p_subpixel_offset), + properties(p_properties) {} }; using FontGlyphMap = @@ -55,7 +69,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, sf.color.ToARGB()); + return fml::HashCombine(sf.font.GetHash(), sf.scale); } }; @@ -63,16 +77,18 @@ 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 && - lhs.color == rhs.color; + return lhs.font.IsEqual(rhs.font) && lhs.scale == rhs.scale; } }; 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); + 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); } }; @@ -82,7 +98,13 @@ 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.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; } }; diff --git a/engine/src/flutter/impeller/typographer/glyph_atlas.cc b/engine/src/flutter/impeller/typographer/glyph_atlas.cc index 59a3f3276b..77b8dfd923 100644 --- a/engine/src/flutter/impeller/typographer/glyph_atlas.cc +++ b/engine/src/flutter/impeller/typographer/glyph_atlas.cc @@ -80,9 +80,8 @@ std::optional> GlyphAtlas::FindFontGlyphBounds( } const FontGlyphAtlas* GlyphAtlas::GetFontGlyphAtlas(const Font& font, - Scalar scale, - Color color) const { - const auto& found = font_atlas_map_.find({font, scale, color}); + Scalar scale) const { + const auto& found = font_atlas_map_.find(ScaledFont{font, scale}); 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 98a6556da2..b94fe90e6a 100644 --- a/engine/src/flutter/impeller/typographer/glyph_atlas.h +++ b/engine/src/flutter/impeller/typographer/glyph_atlas.h @@ -130,9 +130,7 @@ 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, - Color color) const; + const FontGlyphAtlas* GetFontGlyphAtlas(const Font& font, Scalar scale) 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 5e6c164e9a..be5688f6a7 100644 --- a/engine/src/flutter/impeller/typographer/lazy_glyph_atlas.cc +++ b/engine/src/flutter/impeller/typographer/lazy_glyph_atlas.cc @@ -32,12 +32,15 @@ LazyGlyphAtlas::~LazyGlyphAtlas() = default; void LazyGlyphAtlas::AddTextFrame(const TextFrame& frame, Scalar scale, - Point offset) { + Point offset, + const GlyphProperties& properties) { FML_DCHECK(alpha_atlas_ == nullptr && color_atlas_ == nullptr); if (frame.GetAtlasType() == GlyphAtlas::Type::kAlphaBitmap) { - frame.CollectUniqueFontGlyphPairs(alpha_glyph_map_, scale, offset); + frame.CollectUniqueFontGlyphPairs(alpha_glyph_map_, scale, offset, + properties); } else { - frame.CollectUniqueFontGlyphPairs(color_glyph_map_, scale, offset); + frame.CollectUniqueFontGlyphPairs(color_glyph_map_, scale, offset, + properties); } } diff --git a/engine/src/flutter/impeller/typographer/lazy_glyph_atlas.h b/engine/src/flutter/impeller/typographer/lazy_glyph_atlas.h index 6fa2e543ee..bead4bda98 100644 --- a/engine/src/flutter/impeller/typographer/lazy_glyph_atlas.h +++ b/engine/src/flutter/impeller/typographer/lazy_glyph_atlas.h @@ -19,7 +19,10 @@ class LazyGlyphAtlas { ~LazyGlyphAtlas(); - void AddTextFrame(const TextFrame& frame, Scalar scale, Point offset); + void AddTextFrame(const TextFrame& frame, + Scalar scale, + Point offset, + const GlyphProperties& properties); void ResetTextFrames(); diff --git a/engine/src/flutter/impeller/typographer/text_frame.cc b/engine/src/flutter/impeller/typographer/text_frame.cc index a209647d33..a0ce36d683 100644 --- a/engine/src/flutter/impeller/typographer/text_frame.cc +++ b/engine/src/flutter/impeller/typographer/text_frame.cc @@ -10,14 +10,8 @@ namespace impeller { TextFrame::TextFrame() = default; -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(std::vector& runs, Rect bounds, bool has_color) + : runs_(std::move(runs)), bounds_(bounds), has_color_(has_color) {} TextFrame::~TextFrame() = default; @@ -38,8 +32,8 @@ GlyphAtlas::Type TextFrame::GetAtlasType() const { : GlyphAtlas::Type::kAlphaBitmap; } -Color TextFrame::GetColor() const { - return color_; +bool TextFrame::HasColor() const { + return has_color_; } // static @@ -90,19 +84,21 @@ Point TextFrame::ComputeSubpixelPosition( } } -void TextFrame::CollectUniqueFontGlyphPairs(FontGlyphMap& glyph_map, - Scalar scale, - Point offset) const { +void TextFrame::CollectUniqueFontGlyphPairs( + FontGlyphMap& glyph_map, + Scalar scale, + Point offset, + const GlyphProperties& properties) 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, color_}]; + auto& set = glyph_map[ScaledFont{font, rounded_scale}]; for (const TextRun::GlyphPosition& glyph_position : run.GetGlyphPositions()) { Point subpixel = ComputeSubpixelPosition( glyph_position, font.GetAxisAlignment(), offset, scale); - set.emplace(glyph_position.glyph, subpixel); + set.emplace(glyph_position.glyph, subpixel, properties); } } } diff --git a/engine/src/flutter/impeller/typographer/text_frame.h b/engine/src/flutter/impeller/typographer/text_frame.h index 7a9b89017d..b742e96e2a 100644 --- a/engine/src/flutter/impeller/typographer/text_frame.h +++ b/engine/src/flutter/impeller/typographer/text_frame.h @@ -20,16 +20,14 @@ class TextFrame { public: TextFrame(); - TextFrame(std::vector& runs, - Rect bounds, - bool has_color, - Color color); + TextFrame(std::vector& runs, Rect bounds, bool has_color); ~TextFrame(); void CollectUniqueFontGlyphPairs(FontGlyphMap& glyph_map, Scalar scale, - Point offset) const; + Point offset, + const GlyphProperties& properties) const; static Point ComputeSubpixelPosition( const TextRun::GlyphPosition& glyph_position, @@ -68,7 +66,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. - Color GetColor() const; + bool HasColor() const; //---------------------------------------------------------------------------- /// @brief The type of atlas this run should be emplaced in. @@ -82,7 +80,6 @@ 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 a2d1813d37..928cba18f0 100644 --- a/engine/src/flutter/impeller/typographer/typographer_unittests.cc +++ b/engine/src/flutter/impeller/typographer/typographer_unittests.cc @@ -2,7 +2,6 @@ // 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" @@ -39,7 +38,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); } @@ -51,10 +50,13 @@ static std::shared_ptr CreateGlyphAtlas( GlyphAtlas::Type type, Scalar scale, const std::shared_ptr& atlas_context, - const std::vector>& frames) { + const std::vector>& frames, + const std::vector& properties) { FontGlyphMap font_glyph_map; + size_t offset = 0; for (auto& frame : frames) { - frame->CollectUniqueFontGlyphPairs(font_glyph_map, scale, {0, 0}); + frame->CollectUniqueFontGlyphPairs(font_glyph_map, scale, {0, 0}, + properties[offset++]); } return typographer_context->CreateGlyphAtlas(context, type, host_buffer, atlas_context, font_glyph_map); @@ -135,14 +137,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( @@ -222,7 +224,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, @@ -303,14 +305,18 @@ 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), flutter::DlColor::kCyan()); + SkTextBlob::MakeFromString("😂", emoji_font)); auto frame_2 = MakeTextFrameFromTextBlobSkia( - SkTextBlob::MakeFromString("😂", emoji_font), - flutter::DlColor::kMagenta()); + SkTextBlob::MakeFromString("😂", emoji_font)); + auto properties = { + GlyphProperties{.color = Color::Red()}, + GlyphProperties{.color = Color::Blue()}, + }; - auto next_atlas = CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer, - GlyphAtlas::Type::kColorBitmap, 1.0f, - atlas_context, {frame, frame_2}); + auto next_atlas = + CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer, + GlyphAtlas::Type::kColorBitmap, 1.0f, atlas_context, + {frame, frame_2}, properties); EXPECT_EQ(next_atlas->GetGlyphCount(), 2u); } @@ -328,14 +334,19 @@ 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), flutter::DlColor::kCyan()); - auto frame_2 = MakeTextFrameFromTextBlobSkia( - SkTextBlob::MakeFromString("A", sk_font), flutter::DlColor::kMagenta()); + auto frame = + MakeTextFrameFromTextBlobSkia(SkTextBlob::MakeFromString("A", sk_font)); + auto frame_2 = + MakeTextFrameFromTextBlobSkia(SkTextBlob::MakeFromString("A", sk_font)); + auto properties = { + GlyphProperties{}, + GlyphProperties{}, + }; - auto next_atlas = CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer, - GlyphAtlas::Type::kColorBitmap, 1.0f, - atlas_context, {frame, frame_2}); + auto next_atlas = + CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer, + GlyphAtlas::Type::kColorBitmap, 1.0f, atlas_context, + {frame, frame_2}, properties); 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 c3c1caba83..79a7aff48b 100644 --- a/engine/src/flutter/testing/impeller_golden_tests_output.txt +++ b/engine/src/flutter/testing/impeller_golden_tests_output.txt @@ -406,6 +406,9 @@ 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 1465289a09..ec5da9c63c 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,9 +102,8 @@ class DisplayListParagraphPainter : public skt::ParagraphPainter { builder_->DrawPath(transformed, dl_paints_[paint_id]); return; } - builder_->DrawTextFrame(impeller::MakeTextFrameFromTextBlobSkia( - blob, dl_paints_[paint_id].getColor()), - x, y, dl_paints_[paint_id]); + builder_->DrawTextFrame(impeller::MakeTextFrameFromTextBlobSkia(blob), x, + y, dl_paints_[paint_id]); return; } #endif // IMPELLER_SUPPORTS_RENDERING @@ -126,9 +125,8 @@ class DisplayListParagraphPainter : public skt::ParagraphPainter { paint.setMaskFilter(&filter); } if (impeller_enabled_) { - builder_->DrawTextFrame( - impeller::MakeTextFrameFromTextBlobSkia(blob, paint.getColor()), x, y, - paint); + builder_->DrawTextFrame(impeller::MakeTextFrameFromTextBlobSkia(blob), x, + y, paint); return; } builder_->DrawTextBlob(blob, x, y, paint); @@ -219,12 +217,11 @@ class DisplayListParagraphPainter : public skt::ParagraphPainter { bool ShouldRenderAsPath(const DlPaint& paint) const { FML_DCHECK(impeller_enabled_); - // 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); + // 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()); } 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 181a7ef035..dd5446aa12 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(), 0); + EXPECT_EQ(recorder.textFrameCount(), 1); EXPECT_EQ(recorder.blobCount(), 0); - EXPECT_EQ(recorder.pathCount(), 1); + EXPECT_EQ(recorder.pathCount(), 0); } TEST_F(PainterTest, DrawTextWithGradientImpeller) {