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/SkData.h" 11 #include "include/core/SkFontMgr.h" 12 #include "include/core/SkPaint.h" 13 #include "include/core/SkString.h" 14 #include "tools/Resources.h" 15 #include "tools/fonts/FontToolUtils.h" 16 17 #if defined(SK_ENABLE_PARAGRAPH) 18 19 #include "modules/skparagraph/include/FontCollection.h" 20 #include "modules/skparagraph/include/ParagraphBuilder.h" 21 #include "modules/skparagraph/include/ParagraphStyle.h" 22 23 class ParagraphBench final : public Benchmark { 24 SkString fName; 25 sk_sp<skia::textlayout::FontCollection> fFontCollection; 26 skia::textlayout::TextStyle fTStyle; 27 std::unique_ptr<skia::textlayout::Paragraph> fParagraph; 28 29 public: ParagraphBench()30 ParagraphBench() { 31 fName.printf("skparagraph"); 32 } 33 34 protected: onGetName()35 const char* onGetName() override { 36 return fName.c_str(); 37 } 38 isSuitableFor(Backend backend)39 bool isSuitableFor(Backend backend) override { 40 // fParagraph might have failed to be created in onDelayedSetup() 41 return backend == Backend::kNonRendering && !!fParagraph; 42 } 43 onDelayedSetup()44 void onDelayedSetup() override { 45 fFontCollection = sk_make_sp<skia::textlayout::FontCollection>(); 46 fFontCollection->setDefaultFontManager(ToolUtils::TestFontMgr()); 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."; 69 skia::textlayout::ParagraphStyle paragraph_style; 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 onDraw(int loops,SkCanvas * canvas)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 94 private: 95 using INHERITED = Benchmark; 96 }; 97 98 DEF_BENCH( return new ParagraphBench; ) 99 100 #endif // SK_ENABLE_PARAGRAPH 101