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 | |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 93 | virtual const char* onGetName() { |
| 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)); |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 99 | if (SK_ColorBLACK != fPaint.getColor()) { |
| 100 | fName.appendf("_%02X", fPaint.getAlpha()); |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 101 | } else { |
vandebo@chromium.org | b9682d3 | 2012-02-21 18:53:39 +0000 | [diff] [blame] | 102 | fName.append("_BK"); |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 103 | } |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 104 | |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 105 | if (fDoColorEmoji) { |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 106 | fName.append("_ColorEmoji"); |
| 107 | } |
| 108 | |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 109 | return fName.c_str(); |
| 110 | } |
| 111 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 112 | virtual void onDraw(const int loops, SkCanvas* canvas) { |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 113 | const SkIPoint dim = this->getSize(); |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 114 | SkRandom rand; |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 115 | |
| 116 | SkPaint paint(fPaint); |
| 117 | this->setupPaint(&paint); |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 118 | // explicitly need these |
| 119 | paint.setColor(fPaint.getColor()); |
| 120 | paint.setAntiAlias(kBW != fFQ); |
| 121 | paint.setLCDRenderText(kLCD == fFQ); |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 122 | |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 123 | if (fDoColorEmoji && fColorEmojiTypeface) { |
| 124 | paint.setTypeface(fColorEmojiTypeface); |
| 125 | } |
| 126 | |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 127 | const SkScalar x0 = SkIntToScalar(-10); |
| 128 | const SkScalar y0 = SkIntToScalar(-10); |
| 129 | |
reed@google.com | 055af88 | 2011-11-23 15:52:02 +0000 | [diff] [blame] | 130 | if (fDoPos) { |
| 131 | // realistically, the matrix is often at least translated, so we |
| 132 | // do that since it exercises different code in drawPosText. |
| 133 | canvas->translate(SK_Scalar1, SK_Scalar1); |
| 134 | } |
| 135 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 136 | for (int i = 0; i < loops; i++) { |
reed@google.com | 055af88 | 2011-11-23 15:52:02 +0000 | [diff] [blame] | 137 | if (fDoPos) { |
| 138 | canvas->drawPosText(fText.c_str(), fText.size(), fPos, paint); |
| 139 | } else { |
| 140 | SkScalar x = x0 + rand.nextUScalar1() * dim.fX; |
| 141 | SkScalar y = y0 + rand.nextUScalar1() * dim.fY; |
| 142 | canvas->drawText(fText.c_str(), fText.size(), x, y, paint); |
| 143 | } |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 148 | typedef Benchmark INHERITED; |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | /////////////////////////////////////////////////////////////////////////////// |
| 152 | |
| 153 | #define STR "Hamburgefons" |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 154 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 155 | DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW); ) |
| 156 | DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kBW); ) |
| 157 | DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kBW); ) |
reed@android.com | eca4836 | 2010-12-20 18:34:17 +0000 | [diff] [blame] | 158 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 159 | DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kAA); ) |
| 160 | DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kAA); ) |
| 161 | DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kAA); ) |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 162 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 163 | DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kLCD); ) |
| 164 | DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kLCD); ) |
| 165 | DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kLCD); ) |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 166 | |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 167 | DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW, true); ) |
| 168 | DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kBW, true); ) |
| 169 | DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kBW, true); ) |
| 170 | |
| 171 | DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW, true, true); ) |
| 172 | DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kAA, false, true); ) |