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