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