Skip to content

Commit 2cd650d

Browse files
Minimal integration with the Skia text shaper module (#9556)
This converts the libtxt Paragraph and ParagraphBuilder classes into interfaces with Minikin and SkShaper/SkParagraph based implementations. Use the --enable-skshaper GN flag to select the Skia shaper implementation at build time.
1 parent f775f5e commit 2cd650d

24 files changed

+1334
-701
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,10 @@ FILE: ../../../flutter/third_party/txt/src/minikin/SparseBitSet.cpp
10681068
FILE: ../../../flutter/third_party/txt/src/minikin/SparseBitSet.h
10691069
FILE: ../../../flutter/third_party/txt/src/minikin/WordBreaker.cpp
10701070
FILE: ../../../flutter/third_party/txt/src/minikin/WordBreaker.h
1071+
FILE: ../../../flutter/third_party/txt/src/skia/paragraph_builder_skia.cc
1072+
FILE: ../../../flutter/third_party/txt/src/skia/paragraph_builder_skia.h
1073+
FILE: ../../../flutter/third_party/txt/src/skia/paragraph_skia.cc
1074+
FILE: ../../../flutter/third_party/txt/src/skia/paragraph_skia.h
10711075
FILE: ../../../flutter/third_party/txt/src/txt/asset_font_manager.cc
10721076
FILE: ../../../flutter/third_party/txt/src/txt/asset_font_manager.h
10731077
FILE: ../../../flutter/third_party/txt/src/txt/font_asset_provider.cc
@@ -1082,12 +1086,15 @@ FILE: ../../../flutter/third_party/txt/src/txt/font_style.h
10821086
FILE: ../../../flutter/third_party/txt/src/txt/font_weight.h
10831087
FILE: ../../../flutter/third_party/txt/src/txt/paint_record.cc
10841088
FILE: ../../../flutter/third_party/txt/src/txt/paint_record.h
1085-
FILE: ../../../flutter/third_party/txt/src/txt/paragraph.cc
10861089
FILE: ../../../flutter/third_party/txt/src/txt/paragraph.h
10871090
FILE: ../../../flutter/third_party/txt/src/txt/paragraph_builder.cc
10881091
FILE: ../../../flutter/third_party/txt/src/txt/paragraph_builder.h
1092+
FILE: ../../../flutter/third_party/txt/src/txt/paragraph_builder_txt.cc
1093+
FILE: ../../../flutter/third_party/txt/src/txt/paragraph_builder_txt.h
10891094
FILE: ../../../flutter/third_party/txt/src/txt/paragraph_style.cc
10901095
FILE: ../../../flutter/third_party/txt/src/txt/paragraph_style.h
1096+
FILE: ../../../flutter/third_party/txt/src/txt/paragraph_txt.cc
1097+
FILE: ../../../flutter/third_party/txt/src/txt/paragraph_txt.h
10911098
FILE: ../../../flutter/third_party/txt/src/txt/placeholder_run.cc
10921099
FILE: ../../../flutter/third_party/txt/src/txt/placeholder_run.h
10931100
FILE: ../../../flutter/third_party/txt/src/txt/styled_runs.cc

common/config.gni

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ if (target_cpu == "arm" || target_cpu == "arm64") {
1717
declare_args() {
1818
# The runtime mode ("debug", "profile", or "release")
1919
flutter_runtime_mode = "debug"
20+
21+
# Whether to use the Skia text shaper module
22+
flutter_enable_skshaper = false
2023
}
2124

2225
# feature_defines_list ---------------------------------------------------------

lib/ui/BUILD.gn

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# found in the LICENSE file.
44

55
import("//build/fuchsia/sdk.gni")
6+
import("$flutter_root/common/config.gni")
67
import("$flutter_root/testing/testing.gni")
78

89
source_set("ui") {
@@ -126,6 +127,10 @@ source_set("ui") {
126127
"$flutter_root/third_party/txt",
127128
]
128129

130+
if (flutter_enable_skshaper) {
131+
defines = [ "FLUTTER_ENABLE_SKSHAPER" ]
132+
}
133+
129134
if (is_fuchsia) {
130135
sources += [
131136
"compositing/scene_host.cc",

lib/ui/text/paragraph_builder.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,16 @@ ParagraphBuilder::ParagraphBuilder(
283283

284284
FontCollection& font_collection =
285285
UIDartState::Current()->window()->client()->GetFontCollection();
286-
m_paragraphBuilder = std::make_unique<txt::ParagraphBuilder>(
287-
style, font_collection.GetFontCollection());
288-
} // namespace flutter
286+
287+
#if FLUTTER_ENABLE_SKSHAPER
288+
#define FLUTTER_PARAGRAPH_BUILDER txt::ParagraphBuilder::CreateSkiaBuilder
289+
#else
290+
#define FLUTTER_PARAGRAPH_BUILDER txt::ParagraphBuilder::CreateTxtBuilder
291+
#endif
292+
293+
m_paragraphBuilder =
294+
FLUTTER_PARAGRAPH_BUILDER(style, font_collection.GetFontCollection());
295+
}
289296

290297
ParagraphBuilder::~ParagraphBuilder() = default;
291298

third_party/txt/BUILD.gn

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import("$flutter_root/common/config.gni")
16+
1517
declare_args() {
1618
flutter_use_fontconfig = false
1719
}
@@ -30,6 +32,9 @@ source_set("txt") {
3032
if (flutter_use_fontconfig) {
3133
defines += [ "FLUTTER_USE_FONTCONFIG" ]
3234
}
35+
if (flutter_enable_skshaper) {
36+
defines += [ "FLUTTER_ENABLE_SKSHAPER" ]
37+
}
3338

3439
sources = [
3540
"src/log/log.cc",
@@ -84,12 +89,15 @@ source_set("txt") {
8489
"src/txt/font_weight.h",
8590
"src/txt/paint_record.cc",
8691
"src/txt/paint_record.h",
87-
"src/txt/paragraph.cc",
8892
"src/txt/paragraph.h",
8993
"src/txt/paragraph_builder.cc",
9094
"src/txt/paragraph_builder.h",
95+
"src/txt/paragraph_builder_txt.cc",
96+
"src/txt/paragraph_builder_txt.h",
9197
"src/txt/paragraph_style.cc",
9298
"src/txt/paragraph_style.h",
99+
"src/txt/paragraph_txt.cc",
100+
"src/txt/paragraph_txt.h",
93101
"src/txt/placeholder_run.cc",
94102
"src/txt/placeholder_run.h",
95103
"src/txt/platform.h",
@@ -130,6 +138,17 @@ source_set("txt") {
130138
deps += [ "//third_party/fontconfig" ]
131139
}
132140

141+
if (flutter_enable_skshaper) {
142+
sources += [
143+
"src/skia/paragraph_builder_skia.cc",
144+
"src/skia/paragraph_builder_skia.h",
145+
"src/skia/paragraph_skia.cc",
146+
"src/skia/paragraph_skia.h",
147+
]
148+
149+
deps += [ "//third_party/skia/modules/skparagraph" ]
150+
}
151+
133152
if (is_mac || is_ios) {
134153
sources += [ "src/txt/platform_mac.mm" ]
135154
deps += [ "$flutter_root/fml" ]

third_party/txt/benchmarks/paragraph_benchmarks.cc

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "txt/font_style.h"
3131
#include "txt/font_weight.h"
3232
#include "txt/paragraph.h"
33-
#include "txt/paragraph_builder.h"
33+
#include "txt/paragraph_builder_txt.h"
3434

3535
namespace txt {
3636

@@ -45,15 +45,15 @@ static void BM_ParagraphShortLayout(benchmark::State& state) {
4545
txt::TextStyle text_style;
4646
text_style.font_families = std::vector<std::string>(1, "Roboto");
4747
text_style.color = SK_ColorBLACK;
48-
txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
48+
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
4949

5050
builder.PushStyle(text_style);
5151
builder.AddText(u16_text);
5252
builder.Pop();
53-
auto paragraph = builder.Build();
53+
auto paragraph = BuildParagraph(builder);
5454
while (state.KeepRunning()) {
5555
paragraph->SetDirty();
56-
paragraph->Layout(300, true);
56+
paragraph->Layout(300);
5757
}
5858
}
5959
BENCHMARK(BM_ParagraphShortLayout);
@@ -87,15 +87,15 @@ static void BM_ParagraphLongLayout(benchmark::State& state) {
8787
text_style.font_families = std::vector<std::string>(1, "Roboto");
8888
text_style.color = SK_ColorBLACK;
8989

90-
txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
90+
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
9191

9292
builder.PushStyle(text_style);
9393
builder.AddText(u16_text);
9494
builder.Pop();
95-
auto paragraph = builder.Build();
95+
auto paragraph = BuildParagraph(builder);
9696
while (state.KeepRunning()) {
9797
paragraph->SetDirty();
98-
paragraph->Layout(300, true);
98+
paragraph->Layout(300);
9999
}
100100
}
101101
BENCHMARK(BM_ParagraphLongLayout);
@@ -130,15 +130,15 @@ static void BM_ParagraphJustifyLayout(benchmark::State& state) {
130130
text_style.font_families = std::vector<std::string>(1, "Roboto");
131131
text_style.color = SK_ColorBLACK;
132132

133-
txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
133+
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
134134

135135
builder.PushStyle(text_style);
136136
builder.AddText(u16_text);
137137
builder.Pop();
138-
auto paragraph = builder.Build();
138+
auto paragraph = BuildParagraph(builder);
139139
while (state.KeepRunning()) {
140140
paragraph->SetDirty();
141-
paragraph->Layout(300, true);
141+
paragraph->Layout(300);
142142
}
143143
}
144144
BENCHMARK(BM_ParagraphJustifyLayout);
@@ -154,15 +154,15 @@ static void BM_ParagraphManyStylesLayout(benchmark::State& state) {
154154
txt::TextStyle text_style;
155155
text_style.font_families = std::vector<std::string>(1, "Roboto");
156156
text_style.color = SK_ColorBLACK;
157-
txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
157+
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
158158
for (int i = 0; i < 1000; ++i) {
159159
builder.PushStyle(text_style);
160160
builder.AddText(u16_text);
161161
}
162-
auto paragraph = builder.Build();
162+
auto paragraph = BuildParagraph(builder);
163163
while (state.KeepRunning()) {
164164
paragraph->SetDirty();
165-
paragraph->Layout(300, true);
165+
paragraph->Layout(300);
166166
}
167167
}
168168
BENCHMARK(BM_ParagraphManyStylesLayout);
@@ -181,15 +181,15 @@ static void BM_ParagraphTextBigO(benchmark::State& state) {
181181
text_style.font_families = std::vector<std::string>(1, "Roboto");
182182
text_style.color = SK_ColorBLACK;
183183

184-
txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
184+
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
185185

186186
builder.PushStyle(text_style);
187187
builder.AddText(u16_text);
188188
builder.Pop();
189-
auto paragraph = builder.Build();
189+
auto paragraph = BuildParagraph(builder);
190190
while (state.KeepRunning()) {
191191
paragraph->SetDirty();
192-
paragraph->Layout(300, true);
192+
paragraph->Layout(300);
193193
}
194194
state.SetComplexityN(state.range(0));
195195
}
@@ -210,16 +210,16 @@ static void BM_ParagraphStylesBigO(benchmark::State& state) {
210210
text_style.font_families = std::vector<std::string>(1, "Roboto");
211211
text_style.color = SK_ColorBLACK;
212212

213-
txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
213+
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
214214

215215
for (int i = 0; i < state.range(0); ++i) {
216216
builder.PushStyle(text_style);
217217
builder.AddText(u16_text);
218218
}
219-
auto paragraph = builder.Build();
219+
auto paragraph = BuildParagraph(builder);
220220
while (state.KeepRunning()) {
221221
paragraph->SetDirty();
222-
paragraph->Layout(300, true);
222+
paragraph->Layout(300);
223223
}
224224
state.SetComplexityN(state.range(0));
225225
}
@@ -239,11 +239,11 @@ static void BM_ParagraphPaintSimple(benchmark::State& state) {
239239
txt::TextStyle text_style;
240240
text_style.font_families = std::vector<std::string>(1, "Roboto");
241241
text_style.color = SK_ColorBLACK;
242-
txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
242+
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
243243
builder.PushStyle(text_style);
244244
builder.AddText(u16_text);
245-
auto paragraph = builder.Build();
246-
paragraph->Layout(300, true);
245+
auto paragraph = BuildParagraph(builder);
246+
paragraph->Layout(300);
247247

248248
std::unique_ptr<SkBitmap> bitmap = std::make_unique<SkBitmap>();
249249
bitmap->allocN32Pixels(1000, 1000);
@@ -286,11 +286,11 @@ static void BM_ParagraphPaintLarge(benchmark::State& state) {
286286
txt::TextStyle text_style;
287287
text_style.font_families = std::vector<std::string>(1, "Roboto");
288288
text_style.color = SK_ColorBLACK;
289-
txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
289+
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
290290
builder.PushStyle(text_style);
291291
builder.AddText(u16_text);
292-
auto paragraph = builder.Build();
293-
paragraph->Layout(300, true);
292+
auto paragraph = BuildParagraph(builder);
293+
paragraph->Layout(300);
294294

295295
std::unique_ptr<SkBitmap> bitmap = std::make_unique<SkBitmap>();
296296
bitmap->allocN32Pixels(1000, 1000);
@@ -322,7 +322,7 @@ static void BM_ParagraphPaintDecoration(benchmark::State& state) {
322322
text_style.decoration_style = TextDecorationStyle(kSolid);
323323
text_style.color = SK_ColorBLACK;
324324

325-
txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
325+
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
326326

327327
builder.PushStyle(text_style);
328328
builder.AddText(u16_text);
@@ -335,8 +335,8 @@ static void BM_ParagraphPaintDecoration(benchmark::State& state) {
335335
builder.PushStyle(text_style);
336336
builder.AddText(u16_text);
337337

338-
auto paragraph = builder.Build();
339-
paragraph->Layout(300, true);
338+
auto paragraph = BuildParagraph(builder);
339+
paragraph->Layout(300);
340340

341341
std::unique_ptr<SkBitmap> bitmap = std::make_unique<SkBitmap>();
342342
bitmap->allocN32Pixels(1000, 1000);

third_party/txt/benchmarks/paragraph_builder_benchmarks.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
#include "txt/font_style.h"
2424
#include "txt/font_weight.h"
2525
#include "txt/paragraph.h"
26-
#include "txt/paragraph_builder.h"
26+
#include "txt/paragraph_builder_txt.h"
2727

2828
namespace txt {
2929

3030
static void BM_ParagraphBuilderConstruction(benchmark::State& state) {
3131
txt::ParagraphStyle paragraph_style;
3232
auto font_collection = GetTestFontCollection();
3333
while (state.KeepRunning()) {
34-
txt::ParagraphBuilder builder(paragraph_style, font_collection);
34+
txt::ParagraphBuilderTxt builder(paragraph_style, font_collection);
3535
}
3636
}
3737
BENCHMARK(BM_ParagraphBuilderConstruction);
@@ -43,15 +43,15 @@ static void BM_ParagraphBuilderPushStyle(benchmark::State& state) {
4343
text_style.color = SK_ColorBLACK;
4444
auto font_collection = GetTestFontCollection();
4545
while (state.KeepRunning()) {
46-
txt::ParagraphBuilder builder(paragraph_style, font_collection);
46+
txt::ParagraphBuilderTxt builder(paragraph_style, font_collection);
4747
builder.PushStyle(text_style);
4848
}
4949
}
5050
BENCHMARK(BM_ParagraphBuilderPushStyle);
5151

5252
static void BM_ParagraphBuilderPushPop(benchmark::State& state) {
5353
txt::ParagraphStyle paragraph_style;
54-
txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
54+
txt::ParagraphBuilderTxt builder(paragraph_style, GetTestFontCollection());
5555

5656
txt::TextStyle text_style;
5757
text_style.color = SK_ColorBLACK;
@@ -70,7 +70,7 @@ static void BM_ParagraphBuilderAddTextString(benchmark::State& state) {
7070
txt::ParagraphStyle paragraph_style;
7171

7272
while (state.KeepRunning()) {
73-
txt::ParagraphBuilder builder(paragraph_style, font_collection);
73+
txt::ParagraphBuilderTxt builder(paragraph_style, font_collection);
7474
builder.AddText(text);
7575
}
7676
}
@@ -82,7 +82,7 @@ static void BM_ParagraphBuilderAddTextChar(benchmark::State& state) {
8282
txt::ParagraphStyle paragraph_style;
8383
auto font_collection = GetTestFontCollection();
8484
while (state.KeepRunning()) {
85-
txt::ParagraphBuilder builder(paragraph_style, font_collection);
85+
txt::ParagraphBuilderTxt builder(paragraph_style, font_collection);
8686
builder.AddText(text);
8787
}
8888
}
@@ -97,7 +97,7 @@ static void BM_ParagraphBuilderAddTextU16stringShort(benchmark::State& state) {
9797
txt::ParagraphStyle paragraph_style;
9898
auto font_collection = GetTestFontCollection();
9999
while (state.KeepRunning()) {
100-
txt::ParagraphBuilder builder(paragraph_style, font_collection);
100+
txt::ParagraphBuilderTxt builder(paragraph_style, font_collection);
101101
builder.AddText(u16_text);
102102
}
103103
}
@@ -131,7 +131,7 @@ static void BM_ParagraphBuilderAddTextU16stringLong(benchmark::State& state) {
131131
txt::ParagraphStyle paragraph_style;
132132

133133
while (state.KeepRunning()) {
134-
txt::ParagraphBuilder builder(paragraph_style, font_collection);
134+
txt::ParagraphBuilderTxt builder(paragraph_style, font_collection);
135135
builder.AddText(u16_text);
136136
}
137137
}
@@ -150,7 +150,7 @@ static void BM_ParagraphBuilderShortParagraphConstruct(
150150
text_style.color = SK_ColorBLACK;
151151
auto font_collection = GetTestFontCollection();
152152
while (state.KeepRunning()) {
153-
txt::ParagraphBuilder builder(paragraph_style, font_collection);
153+
txt::ParagraphBuilderTxt builder(paragraph_style, font_collection);
154154
builder.PushStyle(text_style);
155155
builder.AddText(u16_text);
156156
builder.Pop();
@@ -188,7 +188,7 @@ static void BM_ParagraphBuilderLongParagraphConstruct(benchmark::State& state) {
188188
text_style.color = SK_ColorBLACK;
189189
auto font_collection = GetTestFontCollection();
190190
while (state.KeepRunning()) {
191-
txt::ParagraphBuilder builder(paragraph_style, font_collection);
191+
txt::ParagraphBuilderTxt builder(paragraph_style, font_collection);
192192
builder.PushStyle(text_style);
193193
builder.AddText(u16_text);
194194
builder.Pop();

0 commit comments

Comments
 (0)