Flutter Engine
The Flutter Engine
skparagraph_benchmarks.cc
Go to the documentation of this file.
1/*
2 * Copyright 2017 Google, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <sstream>
18
19#include "flutter/fml/command_line.h"
20#include "flutter/fml/logging.h"
21#include "flutter/third_party/txt/tests/txt_test_utils.h"
22#include "third_party/benchmark/include/benchmark/benchmark.h"
23#include "third_party/icu/source/common/unicode/unistr.h"
32
33namespace sktxt = skia::textlayout;
34
35class SkParagraphFixture : public benchmark::Fixture {
36 public:
38 font_collection_ = sk_make_sp<sktxt::TestFontCollection>(txt::GetFontDir());
39
40 bitmap_ = std::make_unique<SkBitmap>();
41 bitmap_->allocN32Pixels(1000, 1000);
42 canvas_ = std::make_unique<SkCanvas>(*bitmap_);
43 canvas_->clear(SK_ColorWHITE);
44 }
45
46 protected:
48 std::unique_ptr<SkCanvas> canvas_;
49 std::unique_ptr<SkBitmap> bitmap_;
50};
51
53 const char* text = "Hello World";
54 sktxt::ParagraphStyle paragraph_style;
55 sktxt::TextStyle text_style;
56 text_style.setFontFamilies({SkString("Roboto")});
57 text_style.setColor(SK_ColorBLACK);
58 auto builder =
59 sktxt::ParagraphBuilder::make(paragraph_style, font_collection_, SkUnicodes::ICU::Make());
60 builder->pushStyle(text_style);
61 builder->addText(text);
62 builder->pop();
63 auto paragraph = builder->Build();
64 while (state.KeepRunning()) {
65 paragraph->markDirty();
66 paragraph->layout(300);
67 }
68}
69
71 const char* text =
72 "This is a very long sentence to test if the text will properly wrap "
73 "around and go to the next line. Sometimes, short sentence. Longer "
74 "sentences are okay too because they are necessary. Very short. "
75 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
76 "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
77 "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
78 "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
79 "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
80 "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
81 "mollit anim id est laborum. "
82 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
83 "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
84 "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
85 "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
86 "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
87 "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
88 "mollit anim id est laborum.";
89 sktxt::ParagraphStyle paragraph_style;
90 sktxt::TextStyle text_style;
91 text_style.setFontFamilies({SkString("Roboto")});
92 text_style.setColor(SK_ColorBLACK);
93 auto builder =
94 sktxt::ParagraphBuilder::make(paragraph_style, font_collection_, SkUnicodes::ICU::Make());
95 builder->pushStyle(text_style);
96 builder->addText(text);
97 builder->pop();
98 auto paragraph = builder->Build();
99 while (state.KeepRunning()) {
100 paragraph->markDirty();
101 paragraph->layout(300);
102 }
103}
104
106 const char* text =
107 "This is a very long sentence to test if the text will properly wrap "
108 "around and go to the next line. Sometimes, short sentence. Longer "
109 "sentences are okay too because they are necessary. Very short. "
110 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
111 "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
112 "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
113 "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
114 "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
115 "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
116 "mollit anim id est laborum. "
117 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
118 "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
119 "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
120 "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
121 "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
122 "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
123 "mollit anim id est laborum.";
124 sktxt::ParagraphStyle paragraph_style;
125 paragraph_style.setTextAlign(sktxt::TextAlign::kJustify);
126 sktxt::TextStyle text_style;
127 text_style.setFontFamilies({SkString("Roboto")});
128 text_style.setColor(SK_ColorBLACK);
129 auto builder =
130 sktxt::ParagraphBuilder::make(paragraph_style, font_collection_, SkUnicodes::ICU::Make());
131 builder->pushStyle(text_style);
132 builder->addText(text);
133 builder->pop();
134 auto paragraph = builder->Build();
135 while (state.KeepRunning()) {
136 paragraph->markDirty();
137 paragraph->layout(300);
138 }
139}
140
142 const char* text = "-";
143 sktxt::ParagraphStyle paragraph_style;
144 sktxt::TextStyle text_style;
145 text_style.setFontFamilies({SkString("Roboto")});
146 text_style.setColor(SK_ColorBLACK);
147 auto builder =
148 sktxt::ParagraphBuilder::make(paragraph_style, font_collection_, SkUnicodes::ICU::Make());
149 for (int i = 0; i < 1000; ++i) {
150 builder->pushStyle(text_style);
151 builder->addText(text);
152 }
153 auto paragraph = builder->Build();
154 while (state.KeepRunning()) {
155 paragraph->markDirty();
156 paragraph->layout(300);
157 }
158}
159
161 std::vector<uint16_t> text;
162 for (uint16_t i = 0; i < state.range(0); ++i) {
163 text.push_back(i % 5 == 0 ? ' ' : i);
164 }
165 std::u16string u16_text(text.data(), text.data() + text.size());
166 sktxt::ParagraphStyle paragraph_style;
167 sktxt::TextStyle text_style;
168 text_style.setFontFamilies({SkString("Roboto")});
169 text_style.setColor(SK_ColorBLACK);
170 auto builder =
171 sktxt::ParagraphBuilder::make(paragraph_style, font_collection_, SkUnicodes::ICU::Make());
172 builder->pushStyle(text_style);
173 builder->addText(u16_text);
174 builder->pop();
175 auto paragraph = builder->Build();
176 while (state.KeepRunning()) {
177 paragraph->markDirty();
178 paragraph->layout(300);
179 }
180 state.SetComplexityN(state.range(0));
181}
182BENCHMARK_REGISTER_F(SkParagraphFixture, TextBigO)
183 ->RangeMultiplier(4)
184 ->Range(1 << 6, 1 << 14)
185 ->Complexity(benchmark::oN);
186
188 const char* text = "vry shrt ";
189 sktxt::ParagraphStyle paragraph_style;
190 sktxt::TextStyle text_style;
191 text_style.setFontFamilies({SkString("Roboto")});
192 text_style.setColor(SK_ColorBLACK);
194 paragraph_style,
195 sk_make_sp<sktxt::TestFontCollection>(txt::GetFontDir()),
197 for (int i = 0; i < 1000; ++i) {
198 builder->pushStyle(text_style);
199 builder->addText(text);
200 }
201 auto paragraph = builder->Build();
202 while (state.KeepRunning()) {
203 paragraph->markDirty();
204 paragraph->layout(300);
205 }
206 state.SetComplexityN(state.range(0));
207}
208BENCHMARK_REGISTER_F(SkParagraphFixture, StylesBigO)
209 ->RangeMultiplier(4)
210 ->Range(1 << 3, 1 << 12)
211 ->Complexity(benchmark::oN);
212
214 const char* text = "This is a simple sentence to test drawing.";
215 sktxt::ParagraphStyle paragraph_style;
216 sktxt::TextStyle text_style;
217 text_style.setFontFamilies({SkString("Roboto")});
218 text_style.setColor(SK_ColorBLACK);
219 auto builder =
220 sktxt::ParagraphBuilder::make(paragraph_style, font_collection_, SkUnicodes::ICU::Make());
221 builder->pushStyle(text_style);
222 builder->addText(text);
223 builder->pop();
224 auto paragraph = builder->Build();
225 paragraph->layout(300);
226 int offset = 0;
227 while (state.KeepRunning()) {
228 paragraph->paint(canvas_.get(), offset % 700, 10);
229 offset++;
230 }
231}
232
234 const char* text =
235 "Hello world! This is a simple sentence 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.Hello world! This is a simple sentence "
241 "to test drawing. Hello world! "
242 "This is a simple sentence to test drawing. Hello world! This is a "
243 "simple sentence to test drawing.Hello world! This is a simple sentence "
244 "to test drawing. Hello world! "
245 "This is a simple sentence to test drawing. Hello world! This is a "
246 "simple sentence to test drawing.Hello world! This is a simple sentence "
247 "to test drawing. Hello world! "
248 "This is a simple sentence to test drawing. Hello world! This is a "
249 "simple sentence to test drawing.Hello world! This is a simple sentence "
250 "to test drawing. Hello world! "
251 "This is a simple sentence to test drawing. Hello world! This is a "
252 "simple sentence to test drawing.";
253 sktxt::ParagraphStyle paragraph_style;
254 sktxt::TextStyle text_style;
255 text_style.setFontFamilies({SkString("Roboto")});
256 text_style.setColor(SK_ColorBLACK);
257 auto builder =
258 sktxt::ParagraphBuilder::make(paragraph_style, font_collection_, SkUnicodes::ICU::Make());
259 builder->pushStyle(text_style);
260 builder->addText(text);
261 builder->pop();
262 auto paragraph = builder->Build();
263 paragraph->layout(300);
264 int offset = 0;
265 while (state.KeepRunning()) {
266 paragraph->paint(canvas_.get(), offset % 700, 10);
267 offset++;
268 }
269}
270
272 const char* text =
273 "Hello world! This is a simple sentence to test drawing. Hello world! "
274 "This is a simple sentence to test drawing.";
275 sktxt::ParagraphStyle paragraph_style;
276 sktxt::TextStyle text_style;
277 text_style.setFontFamilies({SkString("Roboto")});
278 text_style.setColor(SK_ColorBLACK);
279 text_style.setDecoration(static_cast<sktxt::TextDecoration>(
282 auto builder =
283 sktxt::ParagraphBuilder::make(paragraph_style, font_collection_, SkUnicodes::ICU::Make());
285 builder->pushStyle(text_style);
286 builder->addText(text);
287
289 builder->pushStyle(text_style);
290 builder->addText(text);
291
293 builder->pushStyle(text_style);
294 builder->addText(text);
295
296 auto paragraph = builder->Build();
297 paragraph->layout(300);
298 int offset = 0;
299 while (state.KeepRunning()) {
300 paragraph->paint(canvas_.get(), offset % 700, 10);
301 offset++;
302 }
303}
304
306 const char* text = "Hello World";
307 sktxt::ParagraphStyle paragraph_style;
308 sktxt::TextStyle text_style;
309 text_style.setFontFamilies({SkString("Roboto")});
310 text_style.setColor(SK_ColorBLACK);
311 while (state.KeepRunning()) {
312 auto builder =
313 sktxt::ParagraphBuilder::make(paragraph_style, font_collection_, SkUnicodes::ICU::Make());
314 builder->pushStyle(text_style);
315 builder->addText(text);
316 builder->pop();
317 auto paragraph = builder->Build();
318 }
319}
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
std::unique_ptr< SkCanvas > canvas_
void SetUp(const ::benchmark::State &state)
sk_sp< sktxt::TestFontCollection > font_collection_
std::unique_ptr< SkBitmap > bitmap_
static std::unique_ptr< ParagraphBuilder > make(const ParagraphStyle &style, sk_sp< FontCollection > fontCollection, sk_sp< SkUnicode > unicode)
void setDecorationStyle(TextDecorationStyle style)
Definition: TextStyle.h:222
void setFontFamilies(std::vector< SkString > families)
Definition: TextStyle.h:253
void setColor(SkColor color)
Definition: TextStyle.h:166
void setDecoration(TextDecoration decoration)
Definition: TextStyle.h:220
AtkStateType state
std::u16string text
SKUNICODE_API sk_sp< SkUnicode > Make()
const std::string & GetFontDir()
BENCHMARK_DEFINE_F(SkParagraphFixture, TextBigO)(benchmark
BENCHMARK_F(SkParagraphFixture, ShortLayout)(benchmark
SeparatedVector2 offset
void setTextAlign(TextAlign align)