Mike Reed | fc32405 | 2021-03-17 13:00:08 -0400 | [diff] [blame] | 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 | |
Robert Phillips | be2c3ee | 2021-08-31 12:36:25 -0400 | [diff] [blame] | 15 | #if defined(SK_ENABLE_PARAGRAPH) |
| 16 | |
Mike Reed | fc32405 | 2021-03-17 13:00:08 -0400 | [diff] [blame] | 17 | #include "modules/skparagraph/include/FontCollection.h" |
| 18 | #include "modules/skparagraph/include/ParagraphBuilder.h" |
| 19 | #include "modules/skparagraph/include/ParagraphStyle.h" |
| 20 | |
Herb Derby | 3401d5e | 2021-03-18 15:12:45 -0400 | [diff] [blame] | 21 | class ParagraphBench final : public Benchmark { |
Mike Reed | fc32405 | 2021-03-17 13:00:08 -0400 | [diff] [blame] | 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: |
| 28 | ParagraphBench() { |
| 29 | fName.printf("skparagraph"); |
| 30 | } |
| 31 | |
| 32 | protected: |
| 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 Derby | 3401d5e | 2021-03-18 15:12:45 -0400 | [diff] [blame] | 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); |
Mike Reed | fc32405 | 2021-03-17 13:00:08 -0400 | [diff] [blame] | 83 | } |
| 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 | |
| 92 | private: |
| 93 | using INHERITED = Benchmark; |
| 94 | }; |
| 95 | |
| 96 | DEF_BENCH( return new ParagraphBench; ) |
Robert Phillips | be2c3ee | 2021-08-31 12:36:25 -0400 | [diff] [blame] | 97 | |
| 98 | #endif // SK_ENABLE_PARAGRAPH |