Flutter Engine
 
Loading...
Searching...
No Matches
skparagraph_benchmarks.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <sstream>
6
10#include "third_party/benchmark/include/benchmark/benchmark.h"
11#include "third_party/icu/source/common/unicode/unistr.h"
12#include "third_party/skia/include/core/SkBitmap.h"
13#include "third_party/skia/include/core/SkCanvas.h"
14#include "third_party/skia/include/core/SkColor.h"
15#include "third_party/skia/modules/skparagraph/include/Paragraph.h"
16#include "third_party/skia/modules/skparagraph/include/ParagraphBuilder.h"
17#include "third_party/skia/modules/skparagraph/include/TypefaceFontProvider.h"
18#include "third_party/skia/modules/skparagraph/utils/TestFontCollection.h"
19#include "third_party/skia/modules/skunicode/include/SkUnicode_icu.h"
20
21namespace sktxt = skia::textlayout;
22
23class SkParagraphFixture : public benchmark::Fixture {
24 public:
25 void SetUp(const ::benchmark::State& state) {
26 font_collection_ = sk_make_sp<sktxt::TestFontCollection>(txt::GetFontDir());
27
28 bitmap_ = std::make_unique<SkBitmap>();
29 bitmap_->allocN32Pixels(1000, 1000);
30 canvas_ = std::make_unique<SkCanvas>(*bitmap_);
31 canvas_->clear(SK_ColorWHITE);
32 }
33
34 protected:
35 sk_sp<sktxt::TestFontCollection> font_collection_;
36 std::unique_ptr<SkCanvas> canvas_;
37 std::unique_ptr<SkBitmap> bitmap_;
38};
39
40BENCHMARK_F(SkParagraphFixture, ShortLayout)(benchmark::State& state) {
41 const char* text = "Hello World";
42 sktxt::ParagraphStyle paragraph_style;
43 sktxt::TextStyle text_style;
44 text_style.setFontFamilies({SkString("Roboto")});
45 text_style.setColor(SK_ColorBLACK);
46 auto builder = sktxt::ParagraphBuilder::make(
47 paragraph_style, font_collection_, SkUnicodes::ICU::Make());
48 builder->pushStyle(text_style);
49 builder->addText(text);
50 builder->pop();
51 auto paragraph = builder->Build();
52 while (state.KeepRunning()) {
53 paragraph->markDirty();
54 paragraph->layout(300);
55 }
56}
57
58BENCHMARK_F(SkParagraphFixture, LongLayout)(benchmark::State& state) {
59 const char* text =
60 "This is a very long sentence to test if the text will properly wrap "
61 "around and go to the next line. Sometimes, short sentence. Longer "
62 "sentences are okay too because they are necessary. Very short. "
63 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
64 "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
65 "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
66 "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
67 "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
68 "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
69 "mollit anim id est laborum. "
70 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
71 "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
72 "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
73 "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
74 "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
75 "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
76 "mollit anim id est laborum.";
77 sktxt::ParagraphStyle paragraph_style;
78 sktxt::TextStyle text_style;
79 text_style.setFontFamilies({SkString("Roboto")});
80 text_style.setColor(SK_ColorBLACK);
81 auto builder = sktxt::ParagraphBuilder::make(
82 paragraph_style, font_collection_, SkUnicodes::ICU::Make());
83 builder->pushStyle(text_style);
84 builder->addText(text);
85 builder->pop();
86 auto paragraph = builder->Build();
87 while (state.KeepRunning()) {
88 paragraph->markDirty();
89 paragraph->layout(300);
90 }
91}
92
93BENCHMARK_F(SkParagraphFixture, JustifyLayout)(benchmark::State& state) {
94 const char* text =
95 "This is a very long sentence to test if the text will properly wrap "
96 "around and go to the next line. Sometimes, short sentence. Longer "
97 "sentences are okay too because they are necessary. Very short. "
98 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
99 "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
100 "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
101 "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
102 "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
103 "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
104 "mollit anim id est laborum. "
105 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
106 "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
107 "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
108 "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
109 "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
110 "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
111 "mollit anim id est laborum.";
112 sktxt::ParagraphStyle paragraph_style;
113 paragraph_style.setTextAlign(sktxt::TextAlign::kJustify);
114 sktxt::TextStyle text_style;
115 text_style.setFontFamilies({SkString("Roboto")});
116 text_style.setColor(SK_ColorBLACK);
117 auto builder = sktxt::ParagraphBuilder::make(
118 paragraph_style, font_collection_, SkUnicodes::ICU::Make());
119 builder->pushStyle(text_style);
120 builder->addText(text);
121 builder->pop();
122 auto paragraph = builder->Build();
123 while (state.KeepRunning()) {
124 paragraph->markDirty();
125 paragraph->layout(300);
126 }
127}
128
129BENCHMARK_F(SkParagraphFixture, ManyStylesLayout)(benchmark::State& state) {
130 const char* text = "-";
131 sktxt::ParagraphStyle paragraph_style;
132 sktxt::TextStyle text_style;
133 text_style.setFontFamilies({SkString("Roboto")});
134 text_style.setColor(SK_ColorBLACK);
135 auto builder = sktxt::ParagraphBuilder::make(
136 paragraph_style, font_collection_, SkUnicodes::ICU::Make());
137 for (int i = 0; i < 1000; ++i) {
138 builder->pushStyle(text_style);
139 builder->addText(text);
140 }
141 auto paragraph = builder->Build();
142 while (state.KeepRunning()) {
143 paragraph->markDirty();
144 paragraph->layout(300);
145 }
146}
147
148BENCHMARK_DEFINE_F(SkParagraphFixture, TextBigO)(benchmark::State& state) {
149 std::vector<uint16_t> text;
150 text.reserve(state.range(0));
151 for (uint16_t i = 0; i < state.range(0); ++i) {
152 text.push_back(i % 5 == 0 ? ' ' : i);
153 }
154 std::u16string u16_text(text.data(), text.data() + text.size());
155 sktxt::ParagraphStyle paragraph_style;
156 sktxt::TextStyle text_style;
157 text_style.setFontFamilies({SkString("Roboto")});
158 text_style.setColor(SK_ColorBLACK);
159 auto builder = sktxt::ParagraphBuilder::make(
160 paragraph_style, font_collection_, SkUnicodes::ICU::Make());
161 builder->pushStyle(text_style);
162 builder->addText(u16_text);
163 builder->pop();
164 auto paragraph = builder->Build();
165 while (state.KeepRunning()) {
166 paragraph->markDirty();
167 paragraph->layout(300);
168 }
169 state.SetComplexityN(state.range(0));
170}
171BENCHMARK_REGISTER_F(SkParagraphFixture, TextBigO)
172 ->RangeMultiplier(4)
173 ->Range(1 << 6, 1 << 14)
174 ->Complexity(benchmark::oN);
175
176BENCHMARK_DEFINE_F(SkParagraphFixture, StylesBigO)(benchmark::State& state) {
177 const char* text = "vry shrt ";
178 sktxt::ParagraphStyle paragraph_style;
179 sktxt::TextStyle text_style;
180 text_style.setFontFamilies({SkString("Roboto")});
181 text_style.setColor(SK_ColorBLACK);
182 auto builder = sktxt::ParagraphBuilder::make(
183 paragraph_style, sk_make_sp<sktxt::TestFontCollection>(txt::GetFontDir()),
184 SkUnicodes::ICU::Make());
185 for (int i = 0; i < 1000; ++i) {
186 builder->pushStyle(text_style);
187 builder->addText(text);
188 }
189 auto paragraph = builder->Build();
190 while (state.KeepRunning()) {
191 paragraph->markDirty();
192 paragraph->layout(300);
193 }
194 state.SetComplexityN(state.range(0));
195}
196BENCHMARK_REGISTER_F(SkParagraphFixture, StylesBigO)
197 ->RangeMultiplier(4)
198 ->Range(1 << 3, 1 << 12)
199 ->Complexity(benchmark::oN);
200
201BENCHMARK_F(SkParagraphFixture, PaintSimple)(benchmark::State& state) {
202 const char* text = "This is a simple sentence to test drawing.";
203 sktxt::ParagraphStyle paragraph_style;
204 sktxt::TextStyle text_style;
205 text_style.setFontFamilies({SkString("Roboto")});
206 text_style.setColor(SK_ColorBLACK);
207 auto builder = sktxt::ParagraphBuilder::make(
208 paragraph_style, font_collection_, SkUnicodes::ICU::Make());
209 builder->pushStyle(text_style);
210 builder->addText(text);
211 builder->pop();
212 auto paragraph = builder->Build();
213 paragraph->layout(300);
214 int offset = 0;
215 while (state.KeepRunning()) {
216 paragraph->paint(canvas_.get(), offset % 700, 10);
217 offset++;
218 }
219}
220
221BENCHMARK_F(SkParagraphFixture, PaintLarge)(benchmark::State& state) {
222 const char* text =
223 "Hello world! This is a simple sentence to test drawing. Hello world! "
224 "This is a simple sentence to test drawing. Hello world! This is a "
225 "simple sentence to test drawing.Hello world! This is a simple sentence "
226 "to test drawing. Hello world! "
227 "This is a simple sentence to test drawing. Hello world! This is a "
228 "simple sentence to test drawing.Hello world! This is a simple sentence "
229 "to test drawing. Hello world! "
230 "This is a simple sentence to test drawing. Hello world! This is a "
231 "simple sentence to test drawing.Hello world! This is a simple sentence "
232 "to test drawing. Hello world! "
233 "This is a simple sentence to test drawing. Hello world! This is a "
234 "simple sentence to test drawing.Hello world! This is a simple sentence "
235 "to test drawing. Hello world! "
236 "This is a simple sentence to test drawing. Hello world! This is a "
237 "simple sentence to test drawing.Hello world! This is a simple sentence "
238 "to test drawing. Hello world! "
239 "This is a simple sentence to test drawing. Hello world! This is a "
240 "simple sentence to test drawing.";
241 sktxt::ParagraphStyle paragraph_style;
242 sktxt::TextStyle text_style;
243 text_style.setFontFamilies({SkString("Roboto")});
244 text_style.setColor(SK_ColorBLACK);
245 auto builder = sktxt::ParagraphBuilder::make(
246 paragraph_style, font_collection_, SkUnicodes::ICU::Make());
247 builder->pushStyle(text_style);
248 builder->addText(text);
249 builder->pop();
250 auto paragraph = builder->Build();
251 paragraph->layout(300);
252 int offset = 0;
253 while (state.KeepRunning()) {
254 paragraph->paint(canvas_.get(), offset % 700, 10);
255 offset++;
256 }
257}
258
259BENCHMARK_F(SkParagraphFixture, PaintDecoration)(benchmark::State& state) {
260 const char* text =
261 "Hello world! This is a simple sentence to test drawing. Hello world! "
262 "This is a simple sentence to test drawing.";
263 sktxt::ParagraphStyle paragraph_style;
264 sktxt::TextStyle text_style;
265 text_style.setFontFamilies({SkString("Roboto")});
266 text_style.setColor(SK_ColorBLACK);
267 // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
268 text_style.setDecoration(static_cast<sktxt::TextDecoration>(
269 sktxt::TextDecoration::kLineThrough | sktxt::TextDecoration::kOverline |
270 sktxt::TextDecoration::kUnderline));
271 auto builder = sktxt::ParagraphBuilder::make(
272 paragraph_style, font_collection_, SkUnicodes::ICU::Make());
273 text_style.setDecorationStyle(sktxt::TextDecorationStyle::kSolid);
274 builder->pushStyle(text_style);
275 builder->addText(text);
276
277 text_style.setDecorationStyle(sktxt::TextDecorationStyle::kDotted);
278 builder->pushStyle(text_style);
279 builder->addText(text);
280
281 text_style.setDecorationStyle(sktxt::TextDecorationStyle::kWavy);
282 builder->pushStyle(text_style);
283 builder->addText(text);
284
285 auto paragraph = builder->Build();
286 paragraph->layout(300);
287 int offset = 0;
288 while (state.KeepRunning()) {
289 paragraph->paint(canvas_.get(), offset % 700, 10);
290 offset++;
291 }
292}
293
294BENCHMARK_F(SkParagraphFixture, SimpleBuilder)(benchmark::State& state) {
295 const char* text = "Hello World";
296 sktxt::ParagraphStyle paragraph_style;
297 sktxt::TextStyle text_style;
298 text_style.setFontFamilies({SkString("Roboto")});
299 text_style.setColor(SK_ColorBLACK);
300 while (state.KeepRunning()) {
301 auto builder = sktxt::ParagraphBuilder::make(
302 paragraph_style, font_collection_, SkUnicodes::ICU::Make());
303 builder->pushStyle(text_style);
304 builder->addText(text);
305 builder->pop();
306 auto paragraph = builder->Build();
307 }
308}
std::unique_ptr< SkCanvas > canvas_
void SetUp(const ::benchmark::State &state)
sk_sp< sktxt::TestFontCollection > font_collection_
std::unique_ptr< SkBitmap > bitmap_
std::u16string text
const std::string & GetFontDir()
BENCHMARK_DEFINE_F(SkParagraphFixture, TextBigO)(benchmark
BENCHMARK_F(SkParagraphFixture, ShortLayout)(benchmark