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; |
tomhudson@google.com | ca529d3 | 2011-10-28 15:34:49 +0000 | [diff] [blame^] | 47 | enum { N = SkBENCHLOOP(800) }; |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 48 | public: |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 49 | TextBench(void* param, const char text[], int ps, |
| 50 | SkColor color, FontQuality fq) : INHERITED(param) { |
| 51 | fFQ = fq; |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 52 | fText.set(text); |
| 53 | |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 54 | fPaint.setAntiAlias(kBW != fq); |
| 55 | fPaint.setLCDRenderText(kLCD == fq); |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 56 | fPaint.setTextSize(SkIntToScalar(ps)); |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 57 | fPaint.setColor(color); |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | protected: |
| 61 | virtual const char* onGetName() { |
| 62 | fName.printf("text_%g", SkScalarToFloat(fPaint.getTextSize())); |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 63 | fName.appendf("_%s", fontQualityName(fPaint)); |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 64 | if (SK_ColorBLACK != fPaint.getColor()) { |
| 65 | fName.appendf("_%02X", fPaint.getAlpha()); |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 66 | } else { |
| 67 | fName.appendf("_BK"); |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 68 | } |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 69 | return fName.c_str(); |
| 70 | } |
| 71 | |
| 72 | virtual void onDraw(SkCanvas* canvas) { |
| 73 | const SkIPoint dim = this->getSize(); |
| 74 | SkRandom rand; |
| 75 | |
| 76 | SkPaint paint(fPaint); |
| 77 | this->setupPaint(&paint); |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 78 | // explicitly need these |
| 79 | paint.setColor(fPaint.getColor()); |
| 80 | paint.setAntiAlias(kBW != fFQ); |
| 81 | paint.setLCDRenderText(kLCD == fFQ); |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 82 | |
| 83 | const SkScalar x0 = SkIntToScalar(-10); |
| 84 | const SkScalar y0 = SkIntToScalar(-10); |
| 85 | |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 86 | for (int i = 0; i < N; i++) { |
| 87 | SkScalar x = x0 + rand.nextUScalar1() * dim.fX; |
| 88 | SkScalar y = y0 + rand.nextUScalar1() * dim.fY; |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 89 | canvas->drawText(fText.c_str(), fText.size(), x, y, paint); |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | typedef SkBenchmark INHERITED; |
| 95 | }; |
| 96 | |
| 97 | /////////////////////////////////////////////////////////////////////////////// |
| 98 | |
| 99 | #define STR "Hamburgefons" |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 100 | |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 101 | static SkBenchmark* Fact01(void* p) { return new TextBench(p, STR, 16, 0xFF000000, kBW); } |
| 102 | static SkBenchmark* Fact02(void* p) { return new TextBench(p, STR, 16, 0xFFFF0000, kBW); } |
| 103 | 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] | 104 | |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 105 | static SkBenchmark* Fact11(void* p) { return new TextBench(p, STR, 16, 0xFF000000, kAA); } |
| 106 | static SkBenchmark* Fact12(void* p) { return new TextBench(p, STR, 16, 0xFFFF0000, kAA); } |
| 107 | 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] | 108 | |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 109 | static SkBenchmark* Fact21(void* p) { return new TextBench(p, STR, 16, 0xFF000000, kLCD); } |
| 110 | static SkBenchmark* Fact22(void* p) { return new TextBench(p, STR, 16, 0xFFFF0000, kLCD); } |
| 111 | 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] | 112 | |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 113 | static BenchRegistry gReg01(Fact01); |
| 114 | static BenchRegistry gReg02(Fact02); |
reed@google.com | 095186a | 2011-10-18 12:21:50 +0000 | [diff] [blame] | 115 | static BenchRegistry gReg03(Fact03); |
| 116 | |
| 117 | static BenchRegistry gReg11(Fact11); |
| 118 | static BenchRegistry gReg12(Fact12); |
| 119 | static BenchRegistry gReg13(Fact13); |
| 120 | |
| 121 | static BenchRegistry gReg21(Fact21); |
| 122 | static BenchRegistry gReg22(Fact22); |
| 123 | static BenchRegistry gReg23(Fact23); |
reed@android.com | eca4836 | 2010-12-20 18:34:17 +0000 | [diff] [blame] | 124 | |