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; |
| 25 | enum { N = 300 }; |
| 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, |
| 28 | bool posText) : 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); |
| 34 | |
| 35 | if (posText) { |
| 36 | SkAutoTArray<SkScalar> storage(fText.size()); |
| 37 | SkScalar* widths = storage.get(); |
| 38 | fCount = fPaint.getTextWidths(fText.c_str(), fText.size(), widths); |
| 39 | fPos = new SkPoint[fCount]; |
| 40 | SkScalar x = 0; |
| 41 | for (int i = 0; i < fCount; i++) { |
| 42 | fPos[i].set(x, 0); |
| 43 | x += widths[i]; |
| 44 | } |
| 45 | } else { |
| 46 | fCount = 0; |
| 47 | fPos = NULL; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | virtual ~TextBench() { |
| 52 | delete[] fPos; |
| 53 | } |
| 54 | |
| 55 | protected: |
| 56 | virtual const char* onGetName() { |
| 57 | fName.printf("text_%g", SkScalarToFloat(fPaint.getTextSize())); |
| 58 | if (fPaint.isLinearText()) { |
| 59 | fName.append("_linear"); |
| 60 | } |
| 61 | if (fPos) { |
| 62 | fName.append("_pos"); |
| 63 | } |
| 64 | return fName.c_str(); |
| 65 | } |
| 66 | |
| 67 | virtual void onDraw(SkCanvas* canvas) { |
| 68 | const SkIPoint dim = this->getSize(); |
| 69 | SkRandom rand; |
| 70 | |
| 71 | SkPaint paint(fPaint); |
| 72 | this->setupPaint(&paint); |
| 73 | |
| 74 | const SkScalar x0 = SkIntToScalar(-10); |
| 75 | const SkScalar y0 = SkIntToScalar(-10); |
reed@android.com | 867ee80 | 2009-10-20 13:55:41 +0000 | [diff] [blame] | 76 | const SkColor colors[] = { SK_ColorBLACK, SK_ColorGRAY }; |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 77 | |
reed@android.com | 867ee80 | 2009-10-20 13:55:41 +0000 | [diff] [blame] | 78 | for (size_t j = 0; j < SK_ARRAY_COUNT(colors); j++) { |
| 79 | paint.setColor(colors[j]); |
| 80 | for (int i = 0; i < N; i++) { |
| 81 | SkScalar x = x0 + rand.nextUScalar1() * dim.fX; |
| 82 | SkScalar y = y0 + rand.nextUScalar1() * dim.fY; |
| 83 | if (fPos) { |
| 84 | canvas->save(SkCanvas::kMatrix_SaveFlag); |
| 85 | canvas->translate(x, y); |
| 86 | canvas->drawPosText(fText.c_str(), fText.size(), fPos, paint); |
| 87 | canvas->restore(); |
| 88 | } else { |
| 89 | canvas->drawText(fText.c_str(), fText.size(), x, y, paint); |
| 90 | } |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | typedef SkBenchmark INHERITED; |
| 97 | }; |
| 98 | |
| 99 | /////////////////////////////////////////////////////////////////////////////// |
| 100 | |
| 101 | #define STR "Hamburgefons" |
| 102 | #define SMALL 9 |
| 103 | #define BIG 48 |
| 104 | |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 105 | static SkBenchmark* Fact0(void* p) { return new TextBench(p, STR, SMALL, false, false); } |
reed@android.com | eca4836 | 2010-12-20 18:34:17 +0000 | [diff] [blame^] | 106 | static BenchRegistry gReg0(Fact0); |
| 107 | |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 108 | static SkBenchmark* Fact1(void* p) { return new TextBench(p, STR, SMALL, false, true); } |
| 109 | static SkBenchmark* Fact2(void* p) { return new TextBench(p, STR, SMALL, true, false); } |
| 110 | static SkBenchmark* Fact3(void* p) { return new TextBench(p, STR, SMALL, true, true); } |
| 111 | static SkBenchmark* Fact4(void* p) { return new TextBench(p, STR, BIG, false, false); } |
| 112 | static SkBenchmark* Fact5(void* p) { return new TextBench(p, STR, BIG, false, true); } |
| 113 | static SkBenchmark* Fact6(void* p) { return new TextBench(p, STR, BIG, true, false); } |
| 114 | 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] | 115 | |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 116 | static BenchRegistry gReg1(Fact1); |
| 117 | static BenchRegistry gReg2(Fact2); |
| 118 | static BenchRegistry gReg3(Fact3); |
| 119 | static BenchRegistry gReg4(Fact4); |
| 120 | static BenchRegistry gReg5(Fact5); |
| 121 | static BenchRegistry gReg6(Fact6); |
| 122 | static BenchRegistry gReg7(Fact7); |
reed@android.com | eca4836 | 2010-12-20 18:34:17 +0000 | [diff] [blame^] | 123 | |