• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkFontMgr.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkString.h"
13 #include "tools/Resources.h"
14 
15 #if defined(SK_ENABLE_PARAGRAPH)
16 
17 #include "modules/skparagraph/include/FontCollection.h"
18 #include "modules/skparagraph/include/ParagraphBuilder.h"
19 #include "modules/skparagraph/include/ParagraphStyle.h"
20 
21 class ParagraphBench final : public Benchmark {
22     SkString fName;
23     sk_sp<skia::textlayout::FontCollection> fFontCollection;
24     skia::textlayout::TextStyle fTStyle;
25     std::unique_ptr<skia::textlayout::Paragraph> fParagraph;
26 
27 public:
ParagraphBench()28     ParagraphBench() {
29         fName.printf("skparagraph");
30     }
31 
32 protected:
onGetName()33     const char* onGetName() override {
34         return fName.c_str();
35     }
36 
isSuitableFor(Backend backend)37     bool isSuitableFor(Backend backend) override {
38         // fParagraph might have failed to be created in onDelayedSetup()
39         return backend == kNonRendering_Backend && !!fParagraph;
40     }
41 
onDelayedSetup()42     void onDelayedSetup() override {
43         fFontCollection = sk_make_sp<skia::textlayout::FontCollection>();
44         fFontCollection->setDefaultFontManager(SkFontMgr::RefDefault());
45 
46         fTStyle.setFontFamilies({SkString("Roboto")});
47         fTStyle.setColor(SK_ColorBLACK);
48 
49         const char* text =
50             "This is a very long sentence to test if the text will properly wrap "
51             "around and go to the next line. Sometimes, short sentence. Longer "
52             "sentences are okay too because they are necessary. Very short. "
53             "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
54             "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
55             "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
56             "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
57             "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
58             "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
59             "mollit anim id est laborum. "
60             "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
61             "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
62             "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
63             "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
64             "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
65             "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
66             "mollit anim id est laborum.";
67         skia::textlayout::ParagraphStyle paragraph_style;
68         auto builder =
69             skia::textlayout::ParagraphBuilder::make(paragraph_style, fFontCollection);
70         if (!builder) {
71             return;
72         }
73 
74         builder->pushStyle(fTStyle);
75         builder->addText(text);
76         builder->pop();
77         fParagraph = builder->Build();
78 
79         // Call onDraw once to warm up the glyph cache otherwise nanobench will mis-calculate the
80         // loop count.
81         SkCanvas canvas;
82         this->onDraw(1, &canvas);
83     }
84 
onDraw(int loops,SkCanvas * canvas)85     void onDraw(int loops, SkCanvas* canvas) override {
86         for (int i = 0; i < loops; ++i) {
87             fParagraph->markDirty();
88             fParagraph->layout(300);
89         }
90     }
91 
92 private:
93     using INHERITED = Benchmark;
94 };
95 
96 DEF_BENCH( return new ParagraphBench; )
97 
98 #endif // SK_ENABLE_PARAGRAPH
99