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> |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 15 | #include "include/core/SkPictureRecorder.h" |
Mike Klein | 52337de | 2019-07-25 09:00:52 -0500 | [diff] [blame] | 16 | #include "modules/skparagraph/utils/TestFontCollection.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 17 | |
| 18 | using namespace skia::textlayout; |
| 19 | namespace { |
| 20 | struct ParagraphBench : public Benchmark { |
| 21 | ParagraphBench(SkScalar width, const char* r, const char* n) |
| 22 | : fResource(r), fName(n), fWidth(width) {} |
| 23 | sk_sp<SkData> fData; |
| 24 | const char* fResource; |
| 25 | const char* fName; |
| 26 | SkScalar fWidth; |
| 27 | const char* onGetName() override { return fName; } |
| 28 | bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; } |
| 29 | void onDelayedSetup() override { fData = GetResourceAsData(fResource); } |
| 30 | void onDraw(int loops, SkCanvas*) override { |
| 31 | if (!fData) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | const char* text = (const char*)fData->data(); |
| 36 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 37 | auto fontCollection = sk_make_sp<FontCollection>(); |
| 38 | fontCollection->setDefaultFontManager(SkFontMgr::RefDefault()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 39 | ParagraphStyle paragraph_style; |
| 40 | paragraph_style.turnHintingOff(); |
| 41 | ParagraphBuilderImpl builder(paragraph_style, fontCollection); |
| 42 | builder.addText(text); |
| 43 | auto paragraph = builder.Build(); |
| 44 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 45 | SkPictureRecorder rec; |
| 46 | SkCanvas* canvas = rec.beginRecording({0,0, 2000,3000}); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 47 | while (loops-- > 0) { |
| 48 | paragraph->layout(fWidth); |
| 49 | auto impl = static_cast<ParagraphImpl*>(paragraph.get()); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 50 | paragraph->paint(canvas, 0, 0); |
| 51 | paragraph->markDirty(); |
| 52 | impl->resetCache(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | }; |
| 56 | } // namespace |
| 57 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 58 | #define PARAGRAPH_BENCH(X) DEF_BENCH(return new ParagraphBench(50000, "text/" #X ".txt", "paragraph_" #X);) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 59 | //PARAGRAPH_BENCH(arabic) |
| 60 | //PARAGRAPH_BENCH(emoji) |
| 61 | PARAGRAPH_BENCH(english) |
| 62 | #undef PARAGRAPH_BENCH |
| 63 | |
| 64 | #endif // !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && !defined(SK_BUILD_FOR_GOOGLE3) |