Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1 | // Copyright 2019 Google LLC. |
| 2 | // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
| 3 | |
| 4 | #include "bench/Benchmark.h" |
| 5 | |
| 6 | #if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && !defined(SK_BUILD_FOR_GOOGLE3) |
| 7 | |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 8 | #include "modules/skparagraph/include/FontCollection.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 9 | #include "modules/skparagraph/include/Paragraph.h" |
| 10 | #include "modules/skparagraph/src/ParagraphBuilderImpl.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 11 | #include "modules/skparagraph/src/ParagraphImpl.h" |
| 12 | #include "tools/Resources.h" |
| 13 | |
| 14 | #include <cfloat> |
| 15 | |
| 16 | using namespace skia::textlayout; |
| 17 | namespace { |
| 18 | struct ParagraphBench : public Benchmark { |
| 19 | ParagraphBench(SkScalar width, const char* r, const char* n) |
| 20 | : fResource(r), fName(n), fWidth(width) {} |
| 21 | sk_sp<SkData> fData; |
| 22 | const char* fResource; |
| 23 | const char* fName; |
| 24 | SkScalar fWidth; |
| 25 | const char* onGetName() override { return fName; } |
| 26 | bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; } |
| 27 | void onDelayedSetup() override { fData = GetResourceAsData(fResource); } |
| 28 | void onDraw(int loops, SkCanvas*) override { |
| 29 | if (!fData) { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | const char* text = (const char*)fData->data(); |
| 34 | |
| 35 | sk_sp<FontCollection> fontCollection = sk_make_sp<FontCollection>(); |
| 36 | ParagraphStyle paragraph_style; |
| 37 | paragraph_style.turnHintingOff(); |
| 38 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 39 | builder.addText(text); |
| 40 | auto paragraph = builder.Build(); |
| 41 | |
| 42 | while (loops-- > 0) { |
| 43 | paragraph->layout(fWidth); |
| 44 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
| 45 | impl->formatLines(fWidth); |
| 46 | } |
| 47 | } |
| 48 | }; |
| 49 | } // namespace |
| 50 | |
| 51 | #define PARAGRAPH_BENCH(X) DEF_BENCH(return new ParagraphBench(500, "text/" #X ".txt", "paragraph_" #X);) |
| 52 | //PARAGRAPH_BENCH(arabic) |
| 53 | //PARAGRAPH_BENCH(emoji) |
| 54 | PARAGRAPH_BENCH(english) |
| 55 | #undef PARAGRAPH_BENCH |
| 56 | |
| 57 | #endif // !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && !defined(SK_BUILD_FOR_GOOGLE3) |