joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "bench/Benchmark.h" |
| 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkFont.h" |
| 11 | #include "include/core/SkPaint.h" |
| 12 | #include "include/core/SkStream.h" |
| 13 | #include "include/core/SkString.h" |
| 14 | #include "include/core/SkTextBlob.h" |
| 15 | #include "include/core/SkTypeface.h" |
| 16 | #include "include/private/SkTemplates.h" |
| 17 | #include "include/utils/SkRandom.h" |
| 18 | #include "tools/Resources.h" |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 19 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "tools/ToolUtils.h" |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | * A trivial test which benchmarks the performance of a textblob with a single run. |
| 24 | */ |
Herb Derby | 430424b | 2018-08-07 13:29:41 -0400 | [diff] [blame] | 25 | class SkTextBlobBench : public Benchmark { |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 26 | public: |
Herb Derby | 430424b | 2018-08-07 13:29:41 -0400 | [diff] [blame] | 27 | SkTextBlobBench() {} |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 28 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 29 | void onDelayedSetup() override { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 30 | fFont.setTypeface(ToolUtils::create_portable_typeface("serif", SkFontStyle())); |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 31 | fFont.setSubpixel(true); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 32 | |
Herb Derby | 430424b | 2018-08-07 13:29:41 -0400 | [diff] [blame] | 33 | // This text seems representative in both length and letter frequency. |
| 34 | const char* text = "Keep your sentences short, but not overly so."; |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 35 | |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 36 | fGlyphs.setCount(fFont.countText(text, strlen(text), SkTextEncoding::kUTF8)); |
Mike Reed | ed33af4 | 2018-12-05 14:15:25 -0500 | [diff] [blame] | 37 | fXPos.setCount(fGlyphs.count()); |
| 38 | |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 39 | fFont.textToGlyphs(text, strlen(text), SkTextEncoding::kUTF8, fGlyphs.begin(), fGlyphs.count()); |
Mike Reed | ed33af4 | 2018-12-05 14:15:25 -0500 | [diff] [blame] | 40 | fFont.getXPos(&fGlyphs[0], fGlyphs.count(), fXPos.begin()); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Herb Derby | 430424b | 2018-08-07 13:29:41 -0400 | [diff] [blame] | 43 | sk_sp<SkTextBlob> makeBlob() { |
| 44 | const SkTextBlobBuilder::RunBuffer& run = |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 45 | fBuilder.allocRunPosH(fFont, fGlyphs.count(), 10, nullptr); |
Mike Reed | c16abee | 2018-11-24 13:27:27 -0500 | [diff] [blame] | 46 | memcpy(run.glyphs, &fGlyphs[0], fGlyphs.count() * sizeof(uint16_t)); |
Mike Reed | ed33af4 | 2018-12-05 14:15:25 -0500 | [diff] [blame] | 47 | memcpy(run.pos, &fXPos[0], fXPos.count() * sizeof(SkScalar)); |
Herb Derby | 430424b | 2018-08-07 13:29:41 -0400 | [diff] [blame] | 48 | return fBuilder.make(); |
| 49 | } |
| 50 | |
| 51 | private: |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 52 | SkTextBlobBuilder fBuilder; |
| 53 | SkFont fFont; |
| 54 | SkTDArray<uint16_t> fGlyphs; |
Mike Reed | ed33af4 | 2018-12-05 14:15:25 -0500 | [diff] [blame] | 55 | SkTDArray<SkScalar> fXPos; |
Herb Derby | 430424b | 2018-08-07 13:29:41 -0400 | [diff] [blame] | 56 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame^] | 57 | using INHERITED = Benchmark; |
Herb Derby | 430424b | 2018-08-07 13:29:41 -0400 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | class TextBlobCachedBench : public SkTextBlobBench { |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 61 | const char* onGetName() override { |
Herb Derby | 430424b | 2018-08-07 13:29:41 -0400 | [diff] [blame] | 62 | return "TextBlobCachedBench"; |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 63 | } |
| 64 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 65 | void onDraw(int loops, SkCanvas* canvas) override { |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 66 | SkPaint paint; |
| 67 | |
Herb Derby | 430424b | 2018-08-07 13:29:41 -0400 | [diff] [blame] | 68 | auto blob = this->makeBlob(); |
Herb Derby | 55bc051 | 2018-10-08 14:06:09 -0400 | [diff] [blame] | 69 | auto bigLoops = loops * 100; |
Herb Derby | 434377a | 2018-10-02 16:48:17 -0400 | [diff] [blame] | 70 | for (int i = 0; i < bigLoops; i++) { |
Herb Derby | 430424b | 2018-08-07 13:29:41 -0400 | [diff] [blame] | 71 | // To ensure maximum caching, we just redraw the blob at the same place everytime |
| 72 | canvas->drawTextBlob(blob, 0, 0, paint); |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 75 | }; |
Mike Reed | ed33af4 | 2018-12-05 14:15:25 -0500 | [diff] [blame] | 76 | DEF_BENCH( return new TextBlobCachedBench(); ) |
joshualitt | eef5b3e | 2015-04-03 08:07:26 -0700 | [diff] [blame] | 77 | |
Herb Derby | 430424b | 2018-08-07 13:29:41 -0400 | [diff] [blame] | 78 | class TextBlobFirstTimeBench : public SkTextBlobBench { |
| 79 | const char* onGetName() override { |
| 80 | return "TextBlobFirstTimeBench"; |
| 81 | } |
| 82 | |
| 83 | void onDraw(int loops, SkCanvas* canvas) override { |
| 84 | SkPaint paint; |
| 85 | |
Herb Derby | 55bc051 | 2018-10-08 14:06:09 -0400 | [diff] [blame] | 86 | auto bigLoops = loops * 100; |
Herb Derby | 434377a | 2018-10-02 16:48:17 -0400 | [diff] [blame] | 87 | for (int i = 0; i < bigLoops; i++) { |
Herb Derby | 430424b | 2018-08-07 13:29:41 -0400 | [diff] [blame] | 88 | canvas->drawTextBlob(this->makeBlob(), 0, 0, paint); |
| 89 | } |
| 90 | } |
| 91 | }; |
Herb Derby | 430424b | 2018-08-07 13:29:41 -0400 | [diff] [blame] | 92 | DEF_BENCH( return new TextBlobFirstTimeBench(); ) |
Mike Reed | ed33af4 | 2018-12-05 14:15:25 -0500 | [diff] [blame] | 93 | |
| 94 | class TextBlobMakeBench : public SkTextBlobBench { |
| 95 | const char* onGetName() override { |
| 96 | return "TextBlobMakeBench"; |
| 97 | } |
| 98 | |
| 99 | bool isSuitableFor(Backend backend) override { |
| 100 | return backend == kNonRendering_Backend; |
| 101 | } |
| 102 | |
| 103 | void onDraw(int loops, SkCanvas*) override { |
| 104 | for (int i = 0; i < loops; i++) { |
| 105 | for (int inner = 0; inner < 1000; ++inner) { |
| 106 | this->makeBlob(); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | }; |
| 111 | DEF_BENCH( return new TextBlobMakeBench(); ) |