reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame^] | 1 | #include "SkBenchmark.h" |
| 2 | #include "SkCanvas.h" |
| 3 | #include "SkPaint.h" |
| 4 | #include "SkRandom.h" |
| 5 | #include "SkString.h" |
| 6 | #include "SkTemplates.h" |
| 7 | |
| 8 | /* Some considerations for performance: |
| 9 | short -vs- long strings (measuring overhead) |
| 10 | tiny -vs- large pointsize (measure blit -vs- overhead) |
| 11 | 1 -vs- many point sizes (measure cache lookup) |
| 12 | normal -vs- subpixel -vs- lineartext (minor) |
| 13 | force purge after each draw to measure scaler |
| 14 | textencoding? |
| 15 | text -vs- postext - pathtext |
| 16 | */ |
| 17 | class TextBench : public SkBenchmark { |
| 18 | SkPaint fPaint; |
| 19 | int fCount; |
| 20 | SkPoint* fPos; |
| 21 | SkString fText; |
| 22 | SkString fName; |
| 23 | enum { N = 300 }; |
| 24 | public: |
| 25 | TextBench(const char text[], int ps, bool linearText, bool posText) { |
| 26 | fText.set(text); |
| 27 | |
| 28 | fPaint.setAntiAlias(true); |
| 29 | fPaint.setTextSize(SkIntToScalar(ps)); |
| 30 | fPaint.setLinearText(linearText); |
| 31 | |
| 32 | if (posText) { |
| 33 | SkAutoTArray<SkScalar> storage(fText.size()); |
| 34 | SkScalar* widths = storage.get(); |
| 35 | fCount = fPaint.getTextWidths(fText.c_str(), fText.size(), widths); |
| 36 | fPos = new SkPoint[fCount]; |
| 37 | SkScalar x = 0; |
| 38 | for (int i = 0; i < fCount; i++) { |
| 39 | fPos[i].set(x, 0); |
| 40 | x += widths[i]; |
| 41 | } |
| 42 | } else { |
| 43 | fCount = 0; |
| 44 | fPos = NULL; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | virtual ~TextBench() { |
| 49 | delete[] fPos; |
| 50 | } |
| 51 | |
| 52 | protected: |
| 53 | virtual const char* onGetName() { |
| 54 | fName.printf("text_%g", SkScalarToFloat(fPaint.getTextSize())); |
| 55 | if (fPaint.isLinearText()) { |
| 56 | fName.append("_linear"); |
| 57 | } |
| 58 | if (fPos) { |
| 59 | fName.append("_pos"); |
| 60 | } |
| 61 | return fName.c_str(); |
| 62 | } |
| 63 | |
| 64 | virtual void onDraw(SkCanvas* canvas) { |
| 65 | const SkIPoint dim = this->getSize(); |
| 66 | SkRandom rand; |
| 67 | |
| 68 | SkPaint paint(fPaint); |
| 69 | this->setupPaint(&paint); |
| 70 | |
| 71 | const SkScalar x0 = SkIntToScalar(-10); |
| 72 | const SkScalar y0 = SkIntToScalar(-10); |
| 73 | |
| 74 | for (int i = 0; i < N; i++) { |
| 75 | SkScalar x = x0 + rand.nextUScalar1() * dim.fX; |
| 76 | SkScalar y = y0 + rand.nextUScalar1() * dim.fY; |
| 77 | if (fPos) { |
| 78 | canvas->save(SkCanvas::kMatrix_SaveFlag); |
| 79 | canvas->translate(x, y); |
| 80 | canvas->drawPosText(fText.c_str(), fText.size(), fPos, paint); |
| 81 | canvas->restore(); |
| 82 | } else { |
| 83 | canvas->drawText(fText.c_str(), fText.size(), x, y, paint); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | private: |
| 89 | typedef SkBenchmark INHERITED; |
| 90 | }; |
| 91 | |
| 92 | /////////////////////////////////////////////////////////////////////////////// |
| 93 | |
| 94 | #define STR "Hamburgefons" |
| 95 | #define SMALL 9 |
| 96 | #define BIG 48 |
| 97 | |
| 98 | static SkBenchmark* Fact0(void*) { return new TextBench(STR, SMALL, false, false); } |
| 99 | static SkBenchmark* Fact1(void*) { return new TextBench(STR, SMALL, false, true); } |
| 100 | static SkBenchmark* Fact2(void*) { return new TextBench(STR, SMALL, true, false); } |
| 101 | static SkBenchmark* Fact3(void*) { return new TextBench(STR, SMALL, true, true); } |
| 102 | static SkBenchmark* Fact4(void*) { return new TextBench(STR, BIG, false, false); } |
| 103 | static SkBenchmark* Fact5(void*) { return new TextBench(STR, BIG, false, true); } |
| 104 | static SkBenchmark* Fact6(void*) { return new TextBench(STR, BIG, true, false); } |
| 105 | static SkBenchmark* Fact7(void*) { return new TextBench(STR, BIG, true, true); } |
| 106 | |
| 107 | static BenchRegistry gReg0(Fact0); |
| 108 | static BenchRegistry gReg1(Fact1); |
| 109 | static BenchRegistry gReg2(Fact2); |
| 110 | static BenchRegistry gReg3(Fact3); |
| 111 | static BenchRegistry gReg4(Fact4); |
| 112 | static BenchRegistry gReg5(Fact5); |
| 113 | static BenchRegistry gReg6(Fact6); |
| 114 | static BenchRegistry gReg7(Fact7); |