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