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