From 62541b14343a99b7f194856399e7c78bf54a2d31 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Wed, 28 Aug 2024 09:13:22 -0700 Subject: [PATCH] [Impeller] fall back to path rendering on thick paths. (flutter/engine#54822) For very thin stroked text we switch to skia font rasterization for higher fidelity. However and very large stroke widths, the fidelity loss doesn't matter and may be much slower due to software rasterization. As several users have noticed, there is a maximum size limit of skia stroked text that we hit as well. This only seems to happen on large stroke widths. Fixes https://github.com/flutter/flutter/issues/153784 --- .../third_party/txt/src/skia/paragraph_skia.cc | 11 ++++++++++- .../txt/tests/paragraph_unittests.cc | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) 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 4855e24c6d..b3521dd529 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 @@ -22,6 +22,7 @@ #include "fml/logging.h" #include "impeller/typographer/backends/skia/text_frame_skia.h" #include "include/core/SkMatrix.h" +#include "third_party/skia/src/core/SkTextBlobPriv.h" // nogncheck namespace txt { @@ -85,6 +86,7 @@ class DisplayListParagraphPainter : public skt::ParagraphPainter { #ifdef IMPELLER_SUPPORTS_RENDERING if (impeller_enabled_) { + SkTextBlobRunIterator run(blob.get()); if (ShouldRenderAsPath(dl_paints_[paint_id])) { auto path = skia::textlayout::Paragraph::GetPath(blob.get()); // If there is no path, this is an emoji and should be drawn as is, @@ -184,7 +186,14 @@ class DisplayListParagraphPainter : public skt::ParagraphPainter { // 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()); + // If the text is stroked and the stroke width is large enough, use path + // rendering anyway, as the fidelity problems won't be as noticable and + // rendering will be faster as it avoids software rasterization. A stroke + // width of four was chosen by eyeballing the point at which the path + // text looks good enough, with some room for error. + return (paint.getColorSource() && !paint.getColorSource()->asColor()) || + (paint.getDrawStyle() == DlDrawStyle::kStroke && + paint.getStrokeWidth() > 4); } 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 f34b83b533..3bbc625042 100644 --- a/engine/src/flutter/third_party/txt/tests/paragraph_unittests.cc +++ b/engine/src/flutter/third_party/txt/tests/paragraph_unittests.cc @@ -259,6 +259,23 @@ TEST_F(PainterTest, DrawStrokedTextImpeller) { EXPECT_EQ(recorder.pathCount(), 0); } +TEST_F(PainterTest, DrawStrokedTextWithLargeWidthImpeller) { + PretendImpellerIsEnabled(true); + + auto style = makeStyle(); + DlPaint foreground; + foreground.setDrawStyle(DlDrawStyle::kStroke); + foreground.setStrokeWidth(10); + style.foreground = foreground; + + auto recorder = DlOpRecorder(); + draw(style)->Dispatch(recorder); + + EXPECT_EQ(recorder.textFrameCount(), 0); + EXPECT_EQ(recorder.blobCount(), 0); + EXPECT_EQ(recorder.pathCount(), 1); +} + TEST_F(PainterTest, DrawTextWithGradientImpeller) { PretendImpellerIsEnabled(true);