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, |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 55 | SkColor color, FontQuality fq, bool doColorEmoji = false, bool doPos = false) { |
bsalomon@google.com | 7fbc604 | 2012-08-13 22:10:05 +0000 | [diff] [blame] | 56 | fPos = NULL; |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 57 | fFQ = fq; |
reed@google.com | 055af88 | 2011-11-23 15:52:02 +0000 | [diff] [blame] | 58 | fDoPos = doPos; |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 59 | fDoColorEmoji = doColorEmoji; |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 60 | fText.set(text); |
| 61 | |
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); |
reed@google.com | 055af88 | 2011-11-23 15:52:02 +0000 | [diff] [blame] | 66 | |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 67 | if (doColorEmoji) { |
| 68 | SkASSERT(kBW == fFQ); |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 69 | fColorEmojiTypeface.reset(GetResourceAsTypeface("/fonts/Funkster.ttf")); |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 70 | } |
| 71 | |
reed@google.com | 055af88 | 2011-11-23 15:52:02 +0000 | [diff] [blame] | 72 | if (doPos) { |
| 73 | size_t len = strlen(text); |
| 74 | SkScalar* adv = new SkScalar[len]; |
| 75 | fPaint.getTextWidths(text, len, adv); |
| 76 | fPos = new SkPoint[len]; |
| 77 | SkScalar x = 0; |
| 78 | for (size_t i = 0; i < len; ++i) { |
| 79 | fPos[i].set(x, SkIntToScalar(50)); |
| 80 | x += adv[i]; |
| 81 | } |
| 82 | delete[] adv; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | virtual ~TextBench() { |
| 87 | delete[] fPos; |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | protected: |
| 91 | virtual const char* onGetName() { |
| 92 | fName.printf("text_%g", SkScalarToFloat(fPaint.getTextSize())); |
reed@google.com | 055af88 | 2011-11-23 15:52:02 +0000 | [diff] [blame] | 93 | if (fDoPos) { |
vandebo@chromium.org | b9682d3 | 2012-02-21 18:53:39 +0000 | [diff] [blame] | 94 | fName.append("_pos"); |
reed@google.com | 055af88 | 2011-11-23 15:52:02 +0000 | [diff] [blame] | 95 | } |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 96 | fName.appendf("_%s", fontQualityName(fPaint)); |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 97 | if (SK_ColorBLACK != fPaint.getColor()) { |
| 98 | fName.appendf("_%02X", fPaint.getAlpha()); |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 99 | } else { |
vandebo@chromium.org | b9682d3 | 2012-02-21 18:53:39 +0000 | [diff] [blame] | 100 | fName.append("_BK"); |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 101 | } |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 102 | |
| 103 | if (fDoColorEmoji && fColorEmojiTypeface) { |
| 104 | fName.append("_ColorEmoji"); |
| 105 | } |
| 106 | |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 107 | return fName.c_str(); |
| 108 | } |
| 109 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 110 | virtual void onDraw(const int loops, SkCanvas* canvas) { |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 111 | const SkIPoint dim = this->getSize(); |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 112 | SkRandom rand; |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 113 | |
| 114 | SkPaint paint(fPaint); |
| 115 | this->setupPaint(&paint); |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 116 | // explicitly need these |
| 117 | paint.setColor(fPaint.getColor()); |
| 118 | paint.setAntiAlias(kBW != fFQ); |
| 119 | paint.setLCDRenderText(kLCD == fFQ); |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 120 | |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 121 | if (fDoColorEmoji && fColorEmojiTypeface) { |
| 122 | paint.setTypeface(fColorEmojiTypeface); |
| 123 | } |
| 124 | |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 125 | const SkScalar x0 = SkIntToScalar(-10); |
| 126 | const SkScalar y0 = SkIntToScalar(-10); |
| 127 | |
reed@google.com | 055af88 | 2011-11-23 15:52:02 +0000 | [diff] [blame] | 128 | if (fDoPos) { |
| 129 | // realistically, the matrix is often at least translated, so we |
| 130 | // do that since it exercises different code in drawPosText. |
| 131 | canvas->translate(SK_Scalar1, SK_Scalar1); |
| 132 | } |
| 133 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 134 | for (int i = 0; i < loops; i++) { |
reed@google.com | 055af88 | 2011-11-23 15:52:02 +0000 | [diff] [blame] | 135 | if (fDoPos) { |
| 136 | canvas->drawPosText(fText.c_str(), fText.size(), fPos, paint); |
| 137 | } else { |
| 138 | SkScalar x = x0 + rand.nextUScalar1() * dim.fX; |
| 139 | SkScalar y = y0 + rand.nextUScalar1() * dim.fY; |
| 140 | canvas->drawText(fText.c_str(), fText.size(), x, y, paint); |
| 141 | } |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 146 | typedef Benchmark INHERITED; |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | /////////////////////////////////////////////////////////////////////////////// |
| 150 | |
| 151 | #define STR "Hamburgefons" |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 152 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 153 | DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW); ) |
| 154 | DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kBW); ) |
| 155 | DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kBW); ) |
reed@android.com | eca4836 | 2010-12-20 18:34:17 +0000 | [diff] [blame] | 156 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 157 | DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kAA); ) |
| 158 | DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kAA); ) |
| 159 | DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kAA); ) |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 160 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 161 | DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kLCD); ) |
| 162 | DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kLCD); ) |
| 163 | DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kLCD); ) |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 164 | |
joshualitt | 02b0501 | 2015-02-11 06:56:30 -0800 | [diff] [blame] | 165 | DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW, true); ) |
| 166 | DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kBW, true); ) |
| 167 | DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kBW, true); ) |
| 168 | |
| 169 | DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW, true, true); ) |
| 170 | DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kAA, false, true); ) |