Flutter Engine
The Flutter Engine
ParagraphBench.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "bench/Benchmark.h"
10#include "include/core/SkData.h"
14#include "tools/Resources.h"
16
17#if defined(SK_ENABLE_PARAGRAPH)
18
22
23class ParagraphBench final : public Benchmark {
27 std::unique_ptr<skia::textlayout::Paragraph> fParagraph;
28
29public:
30 ParagraphBench() {
31 fName.printf("skparagraph");
32 }
33
34protected:
35 const char* onGetName() override {
36 return fName.c_str();
37 }
38
39 bool isSuitableFor(Backend backend) override {
40 // fParagraph might have failed to be created in onDelayedSetup()
41 return backend == Backend::kNonRendering && !!fParagraph;
42 }
43
44 void onDelayedSetup() override {
45 fFontCollection = sk_make_sp<skia::textlayout::FontCollection>();
47
48 fTStyle.setFontFamilies({SkString("Roboto")});
49 fTStyle.setColor(SK_ColorBLACK);
50
51 const char* text =
52 "This is a very long sentence to test if the text will properly wrap "
53 "around and go to the next line. Sometimes, short sentence. Longer "
54 "sentences are okay too because they are necessary. Very short. "
55 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
56 "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
57 "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
58 "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
59 "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
60 "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
61 "mollit anim id est laborum. "
62 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
63 "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
64 "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
65 "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
66 "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
67 "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
68 "mollit anim id est laborum.";
70 auto builder =
71 skia::textlayout::ParagraphBuilder::make(paragraph_style, fFontCollection);
72 if (!builder) {
73 return;
74 }
75
76 builder->pushStyle(fTStyle);
77 builder->addText(text);
78 builder->pop();
79 fParagraph = builder->Build();
80
81 // Call onDraw once to warm up the glyph cache otherwise nanobench will mis-calculate the
82 // loop count.
83 SkCanvas canvas;
84 this->onDraw(1, &canvas);
85 }
86
87 void onDraw(int loops, SkCanvas* canvas) override {
88 for (int i = 0; i < loops; ++i) {
89 fParagraph->markDirty();
90 fParagraph->layout(300);
91 }
92 }
93
94private:
95 using INHERITED = Benchmark;
96};
97
98DEF_BENCH( return new ParagraphBench; )
99
100#endif // SK_ENABLE_PARAGRAPH
#define DEF_BENCH(code)
Definition: Benchmark.h:20
const char * backend
const char * fName
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
virtual void onDraw(int loops, SkCanvas *)=0
virtual bool isSuitableFor(Backend backend)
Definition: Benchmark.h:55
virtual void onDelayedSetup()
Definition: Benchmark.h:101
virtual const char * onGetName()=0
void setDefaultFontManager(sk_sp< SkFontMgr > fontManager)
static std::unique_ptr< ParagraphBuilder > make(const ParagraphStyle &style, sk_sp< FontCollection > fontCollection, sk_sp< SkUnicode > unicode)
void setFontFamilies(std::vector< SkString > families)
Definition: TextStyle.h:253
void setColor(SkColor color)
Definition: TextStyle.h:166
std::u16string text
sk_sp< SkFontMgr > TestFontMgr()