epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 7 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 9 | #include "Resources.h" |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkPaint.h" |
| 12 | #include "SkRandom.h" |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 13 | #include "SkStream.h" |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 14 | #include "SkString.h" |
| 15 | #include "SkTemplates.h" |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 16 | #include "SkTypeface.h" |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 17 | |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 18 | enum FontQuality { |
| 19 | kBW, |
| 20 | kAA, |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 21 | kLCD, |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | static const char* fontQualityName(const SkPaint& paint) { |
| 25 | if (!paint.isAntiAlias()) { |
| 26 | return "BW"; |
| 27 | } |
| 28 | if (paint.isLCDRenderText()) { |
| 29 | return "LCD"; |
| 30 | } |
| 31 | return "AA"; |
| 32 | } |
| 33 | |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 34 | /* Some considerations for performance: |
| 35 | short -vs- long strings (measuring overhead) |
| 36 | tiny -vs- large pointsize (measure blit -vs- overhead) |
| 37 | 1 -vs- many point sizes (measure cache lookup) |
| 38 | normal -vs- subpixel -vs- lineartext (minor) |
| 39 | force purge after each draw to measure scaler |
| 40 | textencoding? |
| 41 | text -vs- postext - pathtext |
| 42 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 43 | class TextBench : public Benchmark { |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 44 | SkPaint fPaint; |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 45 | SkString fText; |
| 46 | SkString fName; |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 47 | FontQuality fFQ; |
reed@google.com | 055af88 | 2011-11-23 15:52:02 +0000 | [diff] [blame] | 48 | bool fDoPos; |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 49 | bool fDoColorEmoji; |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 50 | sk_sp<SkTypeface> fColorEmojiTypeface; |
reed@google.com | 055af88 | 2011-11-23 15:52:02 +0000 | [diff] [blame] | 51 | SkPoint* fPos; |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 52 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 53 | TextBench(const char text[], int ps, |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 54 | SkColor color, FontQuality fq, bool doColorEmoji = false, bool doPos = false) |
| 55 | : fText(text) |
| 56 | , fFQ(fq) |
| 57 | , fDoPos(doPos) |
| 58 | , fDoColorEmoji(doColorEmoji) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 59 | , fPos(nullptr) { |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 60 | fPaint.setAntiAlias(kBW != fq); |
| 61 | fPaint.setLCDRenderText(kLCD == fq); |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 62 | fPaint.setTextSize(SkIntToScalar(ps)); |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 63 | fPaint.setColor(color); |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 64 | } |
reed@google.com | 055af88 | 2011-11-23 15:52:02 +0000 | [diff] [blame] | 65 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 66 | ~TextBench() override { |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 67 | delete[] fPos; |
| 68 | } |
| 69 | |
| 70 | protected: |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 71 | void onDelayedSetup() override { |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 72 | if (fDoColorEmoji) { |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 73 | SkASSERT(kBW == fFQ); |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 74 | fColorEmojiTypeface = MakeResourceAsTypeface("/fonts/Funkster.ttf"); |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 75 | } |
| 76 | |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 77 | if (fDoPos) { |
| 78 | size_t len = fText.size(); |
reed@google.com | 055af88 | 2011-11-23 15:52:02 +0000 | [diff] [blame] | 79 | SkScalar* adv = new SkScalar[len]; |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 80 | fPaint.getTextWidths(fText.c_str(), len, adv); |
reed@google.com | 055af88 | 2011-11-23 15:52:02 +0000 | [diff] [blame] | 81 | fPos = new SkPoint[len]; |
| 82 | SkScalar x = 0; |
| 83 | for (size_t i = 0; i < len; ++i) { |
| 84 | fPos[i].set(x, SkIntToScalar(50)); |
| 85 | x += adv[i]; |
| 86 | } |
| 87 | delete[] adv; |
| 88 | } |
| 89 | } |
| 90 | |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 91 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 92 | const char* onGetName() override { |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 93 | fName.printf("text_%g", SkScalarToFloat(fPaint.getTextSize())); |
reed@google.com | 055af88 | 2011-11-23 15:52:02 +0000 | [diff] [blame] | 94 | if (fDoPos) { |
vandebo@chromium.org | b9682d3 | 2012-02-21 18:53:39 +0000 | [diff] [blame] | 95 | fName.append("_pos"); |
reed@google.com | 055af88 | 2011-11-23 15:52:02 +0000 | [diff] [blame] | 96 | } |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 97 | fName.appendf("_%s", fontQualityName(fPaint)); |
mtklein | 9c84b11 | 2015-08-26 13:41:31 -0700 | [diff] [blame] | 98 | if (SK_ColorBLACK == fPaint.getColor()) { |
vandebo@chromium.org | b9682d3 | 2012-02-21 18:53:39 +0000 | [diff] [blame] | 99 | fName.append("_BK"); |
mtklein | 9c84b11 | 2015-08-26 13:41:31 -0700 | [diff] [blame] | 100 | } else if (SK_ColorWHITE == fPaint.getColor()) { |
| 101 | fName.append("_WT"); |
| 102 | } else { |
| 103 | fName.appendf("_%02X", fPaint.getAlpha()); |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 104 | } |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 105 | |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 106 | if (fDoColorEmoji) { |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 107 | fName.append("_ColorEmoji"); |
| 108 | } |
| 109 | |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 110 | return fName.c_str(); |
| 111 | } |
| 112 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 113 | void onDraw(int loops, SkCanvas* canvas) override { |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 114 | const SkIPoint dim = this->getSize(); |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 115 | SkRandom rand; |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 116 | |
| 117 | SkPaint paint(fPaint); |
| 118 | this->setupPaint(&paint); |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 119 | // explicitly need these |
| 120 | paint.setColor(fPaint.getColor()); |
| 121 | paint.setAntiAlias(kBW != fFQ); |
| 122 | paint.setLCDRenderText(kLCD == fFQ); |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 123 | |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 124 | if (fDoColorEmoji && fColorEmojiTypeface) { |
| 125 | paint.setTypeface(fColorEmojiTypeface); |
| 126 | } |
| 127 | |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 128 | const SkScalar x0 = SkIntToScalar(-10); |
| 129 | const SkScalar y0 = SkIntToScalar(-10); |
| 130 | |
reed@google.com | 055af88 | 2011-11-23 15:52:02 +0000 | [diff] [blame] | 131 | if (fDoPos) { |
| 132 | // realistically, the matrix is often at least translated, so we |
| 133 | // do that since it exercises different code in drawPosText. |
| 134 | canvas->translate(SK_Scalar1, SK_Scalar1); |
| 135 | } |
| 136 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 137 | for (int i = 0; i < loops; i++) { |
reed@google.com | 055af88 | 2011-11-23 15:52:02 +0000 | [diff] [blame] | 138 | if (fDoPos) { |
| 139 | canvas->drawPosText(fText.c_str(), fText.size(), fPos, paint); |
| 140 | } else { |
| 141 | SkScalar x = x0 + rand.nextUScalar1() * dim.fX; |
| 142 | SkScalar y = y0 + rand.nextUScalar1() * dim.fY; |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 143 | canvas->drawString(fText, x, y, paint); |
reed@google.com | 055af88 | 2011-11-23 15:52:02 +0000 | [diff] [blame] | 144 | } |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 149 | typedef Benchmark INHERITED; |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | /////////////////////////////////////////////////////////////////////////////// |
| 153 | |
| 154 | #define STR "Hamburgefons" |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 155 | |
mtklein | 9c84b11 | 2015-08-26 13:41:31 -0700 | [diff] [blame] | 156 | DEF_BENCH( return new TextBench(STR, 16, 0xFFFFFFFF, kBW); ) |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 157 | DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW); ) |
| 158 | DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kBW); ) |
| 159 | DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kBW); ) |
reed@android.com | eca4836 | 2010-12-20 18:34:17 +0000 | [diff] [blame] | 160 | |
mtklein | 9c84b11 | 2015-08-26 13:41:31 -0700 | [diff] [blame] | 161 | DEF_BENCH( return new TextBench(STR, 16, 0xFFFFFFFF, kAA); ) |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 162 | DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kAA); ) |
| 163 | DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kAA); ) |
| 164 | DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kAA); ) |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 165 | |
mtklein | 9c84b11 | 2015-08-26 13:41:31 -0700 | [diff] [blame] | 166 | DEF_BENCH( return new TextBench(STR, 16, 0xFFFFFFFF, kLCD); ) |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 167 | DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kLCD); ) |
| 168 | DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kLCD); ) |
| 169 | DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kLCD); ) |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 170 | |
mtklein | 9c84b11 | 2015-08-26 13:41:31 -0700 | [diff] [blame] | 171 | DEF_BENCH( return new TextBench(STR, 16, 0xFFFFFFFF, kBW, true); ) |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 172 | DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW, true); ) |
| 173 | DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kBW, true); ) |
| 174 | DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kBW, true); ) |
| 175 | |
| 176 | DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW, true, true); ) |
| 177 | DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kAA, false, true); ) |