From b763678f03963bb74c8ae657abe9898f45d38cd7 Mon Sep 17 00:00:00 2001 From: Kaylee Lubick Date: Mon, 29 Apr 2024 11:59:33 -0400 Subject: [PATCH] Make SkUnicode explicitly instead of relying on SkParagraph to make it for us (flutter/engine#52086) In https://skia-review.googlesource.com/c/skia/+/838417 Skia staged the removal of automatically creating an SkUnicode as part of the effort to make Skia more modular. Clients will now have to construct and SkUnicode and pass it to SkParagraph to provide the appropriate data. Flutter sometimes uses ICU and sometimes uses a "client" SkUnicode which makes use of the browser APIs to get the data. We should come back to the skwasm code especially, because right now skwasm gets the data necessary to make an SkUnicode::Client, but stores it in the ParagraphBuilder, just to take it out again later to make the SkUnicode::Client and pass that back into the ParagraphBuilder. skwasm should just create the SkUnicode::Client directly and pass it to the ParagraphBuilder::make(). ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide] and the [C++, Objective-C, Java style guides]. - [ ] I listed at least one issue that this PR fixes in the description above. - [x] I added new tests to check the change I am making or feature I am adding, or the PR is [test-exempt]. See [testing the engine] for instructions on writing and running engine tests. - [ ] I updated/added relevant documentation (doc comments with `///`). - [x] I signed the [CLA]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style [testing the engine]: https://github.com/flutter/flutter/wiki/Testing-the-engine [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat --- .../web_ui/skwasm/text/paragraph_builder.cpp | 9 +++++++- engine/src/flutter/skia/flutter_defines.gni | 1 + .../txt/benchmarks/skparagraph_benchmarks.cc | 22 ++++++++++--------- .../txt/src/skia/paragraph_builder_skia.cc | 5 ++++- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/engine/src/flutter/lib/web_ui/skwasm/text/paragraph_builder.cpp b/engine/src/flutter/lib/web_ui/skwasm/text/paragraph_builder.cpp index 29e659dd91..b573d31e02 100644 --- a/engine/src/flutter/lib/web_ui/skwasm/text/paragraph_builder.cpp +++ b/engine/src/flutter/lib/web_ui/skwasm/text/paragraph_builder.cpp @@ -5,6 +5,7 @@ #include "../export.h" #include "../wrappers.h" #include "third_party/skia/modules/skparagraph/include/ParagraphBuilder.h" +#include "third_party/skia/modules/skunicode/include/SkUnicode_client.h" using namespace skia::textlayout; using namespace Skwasm; @@ -12,7 +13,8 @@ using namespace Skwasm; SKWASM_EXPORT ParagraphBuilder* paragraphBuilder_create( ParagraphStyle* style, FlutterFontCollection* collection) { - return ParagraphBuilder::make(*style, collection->collection).release(); + return ParagraphBuilder::make(*style, collection->collection, nullptr) + .release(); } SKWASM_EXPORT void paragraphBuilder_dispose(ParagraphBuilder* builder) { @@ -52,6 +54,11 @@ SKWASM_EXPORT void paragraphBuilder_pop(ParagraphBuilder* builder) { } SKWASM_EXPORT Paragraph* paragraphBuilder_build(ParagraphBuilder* builder) { + auto [words, graphemeBreaks, lineBreaks] = builder->getClientICUData(); + auto text = builder->getText(); + sk_sp clientICU = + SkUnicodes::Client::Make(text, words, graphemeBreaks, lineBreaks); + builder->SetUnicode(clientICU); return builder->Build().release(); } diff --git a/engine/src/flutter/skia/flutter_defines.gni b/engine/src/flutter/skia/flutter_defines.gni index 2c08f76cba..f3a5fc623b 100644 --- a/engine/src/flutter/skia/flutter_defines.gni +++ b/engine/src/flutter/skia/flutter_defines.gni @@ -13,6 +13,7 @@ flutter_defines = [ "SK_LEGACY_IGNORE_DRAW_VERTICES_BLEND_WITH_NO_SHADER", "SK_RESOLVE_FILTERS_BEFORE_RESTORE", "SK_DISABLE_LEGACY_METAL_BACKEND_SURFACE", + "SK_DISABLE_LEGACY_PARAGRAPH_UNICODE", # Fast low-precision software rendering isn't a priority for Flutter. "SK_DISABLE_LEGACY_SHADERCONTEXT", diff --git a/engine/src/flutter/third_party/txt/benchmarks/skparagraph_benchmarks.cc b/engine/src/flutter/third_party/txt/benchmarks/skparagraph_benchmarks.cc index 7dd27bdb6a..7f9fd9520f 100644 --- a/engine/src/flutter/third_party/txt/benchmarks/skparagraph_benchmarks.cc +++ b/engine/src/flutter/third_party/txt/benchmarks/skparagraph_benchmarks.cc @@ -28,6 +28,7 @@ #include "third_party/skia/modules/skparagraph/include/ParagraphBuilder.h" #include "third_party/skia/modules/skparagraph/include/TypefaceFontProvider.h" #include "third_party/skia/modules/skparagraph/utils/TestFontCollection.h" +#include "third_party/skia/modules/skunicode/include/SkUnicode_icu.h" namespace sktxt = skia::textlayout; @@ -55,7 +56,7 @@ BENCHMARK_F(SkParagraphFixture, ShortLayout)(benchmark::State& state) { text_style.setFontFamilies({SkString("Roboto")}); text_style.setColor(SK_ColorBLACK); auto builder = - sktxt::ParagraphBuilder::make(paragraph_style, font_collection_); + sktxt::ParagraphBuilder::make(paragraph_style, font_collection_, SkUnicodes::ICU::Make()); builder->pushStyle(text_style); builder->addText(text); builder->pop(); @@ -90,7 +91,7 @@ BENCHMARK_F(SkParagraphFixture, LongLayout)(benchmark::State& state) { text_style.setFontFamilies({SkString("Roboto")}); text_style.setColor(SK_ColorBLACK); auto builder = - sktxt::ParagraphBuilder::make(paragraph_style, font_collection_); + sktxt::ParagraphBuilder::make(paragraph_style, font_collection_, SkUnicodes::ICU::Make()); builder->pushStyle(text_style); builder->addText(text); builder->pop(); @@ -126,7 +127,7 @@ BENCHMARK_F(SkParagraphFixture, JustifyLayout)(benchmark::State& state) { text_style.setFontFamilies({SkString("Roboto")}); text_style.setColor(SK_ColorBLACK); auto builder = - sktxt::ParagraphBuilder::make(paragraph_style, font_collection_); + sktxt::ParagraphBuilder::make(paragraph_style, font_collection_, SkUnicodes::ICU::Make()); builder->pushStyle(text_style); builder->addText(text); builder->pop(); @@ -144,7 +145,7 @@ BENCHMARK_F(SkParagraphFixture, ManyStylesLayout)(benchmark::State& state) { text_style.setFontFamilies({SkString("Roboto")}); text_style.setColor(SK_ColorBLACK); auto builder = - sktxt::ParagraphBuilder::make(paragraph_style, font_collection_); + sktxt::ParagraphBuilder::make(paragraph_style, font_collection_, SkUnicodes::ICU::Make()); for (int i = 0; i < 1000; ++i) { builder->pushStyle(text_style); builder->addText(text); @@ -167,7 +168,7 @@ BENCHMARK_DEFINE_F(SkParagraphFixture, TextBigO)(benchmark::State& state) { text_style.setFontFamilies({SkString("Roboto")}); text_style.setColor(SK_ColorBLACK); auto builder = - sktxt::ParagraphBuilder::make(paragraph_style, font_collection_); + sktxt::ParagraphBuilder::make(paragraph_style, font_collection_, SkUnicodes::ICU::Make()); builder->pushStyle(text_style); builder->addText(u16_text); builder->pop(); @@ -191,7 +192,8 @@ BENCHMARK_DEFINE_F(SkParagraphFixture, StylesBigO)(benchmark::State& state) { text_style.setColor(SK_ColorBLACK); auto builder = sktxt::ParagraphBuilder::make( paragraph_style, - sk_make_sp(txt::GetFontDir())); + sk_make_sp(txt::GetFontDir()), + SkUnicodes::ICU::Make()); for (int i = 0; i < 1000; ++i) { builder->pushStyle(text_style); builder->addText(text); @@ -215,7 +217,7 @@ BENCHMARK_F(SkParagraphFixture, PaintSimple)(benchmark::State& state) { text_style.setFontFamilies({SkString("Roboto")}); text_style.setColor(SK_ColorBLACK); auto builder = - sktxt::ParagraphBuilder::make(paragraph_style, font_collection_); + sktxt::ParagraphBuilder::make(paragraph_style, font_collection_, SkUnicodes::ICU::Make()); builder->pushStyle(text_style); builder->addText(text); builder->pop(); @@ -253,7 +255,7 @@ BENCHMARK_F(SkParagraphFixture, PaintLarge)(benchmark::State& state) { text_style.setFontFamilies({SkString("Roboto")}); text_style.setColor(SK_ColorBLACK); auto builder = - sktxt::ParagraphBuilder::make(paragraph_style, font_collection_); + sktxt::ParagraphBuilder::make(paragraph_style, font_collection_, SkUnicodes::ICU::Make()); builder->pushStyle(text_style); builder->addText(text); builder->pop(); @@ -278,7 +280,7 @@ BENCHMARK_F(SkParagraphFixture, PaintDecoration)(benchmark::State& state) { sktxt::TextDecoration::kLineThrough | sktxt::TextDecoration::kOverline | sktxt::TextDecoration::kUnderline)); auto builder = - sktxt::ParagraphBuilder::make(paragraph_style, font_collection_); + sktxt::ParagraphBuilder::make(paragraph_style, font_collection_, SkUnicodes::ICU::Make()); text_style.setDecorationStyle(sktxt::TextDecorationStyle::kSolid); builder->pushStyle(text_style); builder->addText(text); @@ -308,7 +310,7 @@ BENCHMARK_F(SkParagraphFixture, SimpleBuilder)(benchmark::State& state) { text_style.setColor(SK_ColorBLACK); while (state.KeepRunning()) { auto builder = - sktxt::ParagraphBuilder::make(paragraph_style, font_collection_); + sktxt::ParagraphBuilder::make(paragraph_style, font_collection_, SkUnicodes::ICU::Make()); builder->pushStyle(text_style); builder->addText(text); builder->pop(); diff --git a/engine/src/flutter/third_party/txt/src/skia/paragraph_builder_skia.cc b/engine/src/flutter/third_party/txt/src/skia/paragraph_builder_skia.cc index 46f6d4aa60..0468e5f554 100644 --- a/engine/src/flutter/third_party/txt/src/skia/paragraph_builder_skia.cc +++ b/engine/src/flutter/third_party/txt/src/skia/paragraph_builder_skia.cc @@ -19,6 +19,7 @@ #include "third_party/skia/modules/skparagraph/include/ParagraphStyle.h" #include "third_party/skia/modules/skparagraph/include/TextStyle.h" +#include "third_party/skia/modules/skunicode/include/SkUnicode_icu.h" #include "txt/paragraph_style.h" namespace skt = skia::textlayout; @@ -50,7 +51,9 @@ ParagraphBuilderSkia::ParagraphBuilderSkia( const bool impeller_enabled) : base_style_(style.GetTextStyle()), impeller_enabled_(impeller_enabled) { builder_ = skt::ParagraphBuilder::make( - TxtToSkia(style), font_collection->CreateSktFontCollection()); + TxtToSkia(style), + font_collection->CreateSktFontCollection(), + SkUnicodes::ICU::Make()); } ParagraphBuilderSkia::~ParagraphBuilderSkia() = default;