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