reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 1 | #include "SkBenchmark.h" |
| 2 | #include "SkCanvas.h" |
reed@android.com | d6638e6 | 2009-04-08 05:03:52 +0000 | [diff] [blame] | 3 | #include "SkFontHost.h" |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 4 | #include "SkPaint.h" |
| 5 | #include "SkRandom.h" |
reed@android.com | 9781ca5 | 2009-04-14 14:28:22 +0000 | [diff] [blame] | 6 | #include "SkSfntUtils.h" |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 7 | #include "SkString.h" |
| 8 | #include "SkTemplates.h" |
| 9 | |
| 10 | /* Some considerations for performance: |
| 11 | short -vs- long strings (measuring overhead) |
| 12 | tiny -vs- large pointsize (measure blit -vs- overhead) |
| 13 | 1 -vs- many point sizes (measure cache lookup) |
| 14 | normal -vs- subpixel -vs- lineartext (minor) |
| 15 | force purge after each draw to measure scaler |
| 16 | textencoding? |
| 17 | text -vs- postext - pathtext |
| 18 | */ |
| 19 | class TextBench : public SkBenchmark { |
| 20 | SkPaint fPaint; |
| 21 | int fCount; |
| 22 | SkPoint* fPos; |
| 23 | SkString fText; |
| 24 | SkString fName; |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 25 | enum { N = 600 }; |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 26 | public: |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 27 | TextBench(void* param, const char text[], int ps, bool linearText, |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 28 | bool posText, SkColor color = SK_ColorBLACK) : INHERITED(param) { |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 29 | fText.set(text); |
| 30 | |
| 31 | fPaint.setAntiAlias(true); |
| 32 | fPaint.setTextSize(SkIntToScalar(ps)); |
| 33 | fPaint.setLinearText(linearText); |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 34 | fPaint.setColor(color); |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 35 | |
| 36 | if (posText) { |
| 37 | SkAutoTArray<SkScalar> storage(fText.size()); |
| 38 | SkScalar* widths = storage.get(); |
| 39 | fCount = fPaint.getTextWidths(fText.c_str(), fText.size(), widths); |
| 40 | fPos = new SkPoint[fCount]; |
| 41 | SkScalar x = 0; |
| 42 | for (int i = 0; i < fCount; i++) { |
| 43 | fPos[i].set(x, 0); |
| 44 | x += widths[i]; |
| 45 | } |
| 46 | } else { |
| 47 | fCount = 0; |
| 48 | fPos = NULL; |
| 49 | } |
| 50 | } |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 51 | |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 52 | virtual ~TextBench() { |
| 53 | delete[] fPos; |
| 54 | } |
| 55 | |
| 56 | protected: |
| 57 | virtual const char* onGetName() { |
| 58 | fName.printf("text_%g", SkScalarToFloat(fPaint.getTextSize())); |
| 59 | if (fPaint.isLinearText()) { |
| 60 | fName.append("_linear"); |
| 61 | } |
| 62 | if (fPos) { |
| 63 | fName.append("_pos"); |
| 64 | } |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 65 | |
| 66 | if (SK_ColorBLACK != fPaint.getColor()) { |
| 67 | fName.appendf("_%02X", fPaint.getAlpha()); |
| 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 | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 78 | paint.setColor(fPaint.getColor()); // need our specified color |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 79 | |
| 80 | const SkScalar x0 = SkIntToScalar(-10); |
| 81 | const SkScalar y0 = SkIntToScalar(-10); |
| 82 | |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 83 | for (int i = 0; i < N; i++) { |
| 84 | SkScalar x = x0 + rand.nextUScalar1() * dim.fX; |
| 85 | SkScalar y = y0 + rand.nextUScalar1() * dim.fY; |
| 86 | if (fPos) { |
| 87 | canvas->save(SkCanvas::kMatrix_SaveFlag); |
| 88 | canvas->translate(x, y); |
| 89 | canvas->drawPosText(fText.c_str(), fText.size(), fPos, paint); |
| 90 | canvas->restore(); |
| 91 | } else { |
| 92 | canvas->drawText(fText.c_str(), fText.size(), x, y, paint); |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | private: |
| 98 | typedef SkBenchmark INHERITED; |
| 99 | }; |
| 100 | |
| 101 | /////////////////////////////////////////////////////////////////////////////// |
| 102 | |
| 103 | #define STR "Hamburgefons" |
| 104 | #define SMALL 9 |
| 105 | #define BIG 48 |
| 106 | |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 107 | static SkBenchmark* Fact0(void* p) { return new TextBench(p, STR, SMALL, false, false); } |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 108 | static SkBenchmark* Fact01(void* p) { return new TextBench(p, STR, SMALL, false, false, 0xFFFF0000); } |
| 109 | static SkBenchmark* Fact02(void* p) { return new TextBench(p, STR, SMALL, false, false, 0x88FF0000); } |
reed@android.com | eca4836 | 2010-12-20 18:34:17 +0000 | [diff] [blame] | 110 | |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 111 | static SkBenchmark* Fact1(void* p) { return new TextBench(p, STR, SMALL, false, true); } |
| 112 | static SkBenchmark* Fact2(void* p) { return new TextBench(p, STR, SMALL, true, false); } |
| 113 | static SkBenchmark* Fact3(void* p) { return new TextBench(p, STR, SMALL, true, true); } |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 114 | |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 115 | static SkBenchmark* Fact4(void* p) { return new TextBench(p, STR, BIG, false, false); } |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 116 | static SkBenchmark* Fact41(void* p) { return new TextBench(p, STR, BIG, false, false, 0xFFFF0000); } |
| 117 | static SkBenchmark* Fact42(void* p) { return new TextBench(p, STR, BIG, false, false, 0x88FF0000); } |
| 118 | |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 119 | static SkBenchmark* Fact5(void* p) { return new TextBench(p, STR, BIG, false, true); } |
| 120 | static SkBenchmark* Fact6(void* p) { return new TextBench(p, STR, BIG, true, false); } |
| 121 | static SkBenchmark* Fact7(void* p) { return new TextBench(p, STR, BIG, true, true); } |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 122 | |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 123 | static BenchRegistry gReg0(Fact0); |
| 124 | static BenchRegistry gReg01(Fact01); |
| 125 | static BenchRegistry gReg02(Fact02); |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 126 | static BenchRegistry gReg1(Fact1); |
| 127 | static BenchRegistry gReg2(Fact2); |
| 128 | static BenchRegistry gReg3(Fact3); |
| 129 | static BenchRegistry gReg4(Fact4); |
reed@google.com | b9d84f3 | 2011-01-17 19:45:43 +0000 | [diff] [blame] | 130 | static BenchRegistry gReg41(Fact41); |
| 131 | static BenchRegistry gReg42(Fact42); |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 132 | static BenchRegistry gReg5(Fact5); |
| 133 | static BenchRegistry gReg6(Fact6); |
| 134 | static BenchRegistry gReg7(Fact7); |
reed@android.com | eca4836 | 2010-12-20 18:34:17 +0000 | [diff] [blame] | 135 | |