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