blob: 87737379c350fd3dffa14b3977bc470bb9590f65 [file] [log] [blame]
Mike Reedfc324052021-03-17 13:00:08 -04001/*
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
Robert Phillipsbe2c3ee2021-08-31 12:36:25 -040015#if defined(SK_ENABLE_PARAGRAPH)
16
Mike Reedfc324052021-03-17 13:00:08 -040017#include "modules/skparagraph/include/FontCollection.h"
18#include "modules/skparagraph/include/ParagraphBuilder.h"
19#include "modules/skparagraph/include/ParagraphStyle.h"
20
Herb Derby3401d5e2021-03-18 15:12:45 -040021class ParagraphBench final : public Benchmark {
Mike Reedfc324052021-03-17 13:00:08 -040022 SkString fName;
23 sk_sp<skia::textlayout::FontCollection> fFontCollection;
24 skia::textlayout::TextStyle fTStyle;
25 std::unique_ptr<skia::textlayout::Paragraph> fParagraph;
26
27public:
28 ParagraphBench() {
29 fName.printf("skparagraph");
30 }
31
32protected:
33 const char* onGetName() override {
34 return fName.c_str();
35 }
36
37 bool isSuitableFor(Backend backend) override {
38 // fParagraph might have failed to be created in onDelayedSetup()
39 return backend == kNonRendering_Backend && !!fParagraph;
40 }
41
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();
Herb Derby3401d5e2021-03-18 15:12:45 -040078
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);
Mike Reedfc324052021-03-17 13:00:08 -040083 }
84
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
92private:
93 using INHERITED = Benchmark;
94};
95
96DEF_BENCH( return new ParagraphBench; )
Robert Phillipsbe2c3ee2021-08-31 12:36:25 -040097
98#endif // SK_ENABLE_PARAGRAPH