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 | |
reed@android.com | d6638e6 | 2009-04-08 05:03:52 +0000 | [diff] [blame] | 10 | static void dump_font(const char name[], SkFontID fontID) { |
| 11 | SkDebugf("Font \"%s\" %x\n", name, fontID); |
| 12 | int count = SkFontHost::CountTables(fontID); |
| 13 | SkAutoTArray<SkFontTableTag> storage(count); |
| 14 | SkFontTableTag* tags = storage.get(); |
| 15 | SkFontHost::GetTableTags(fontID, tags); |
| 16 | for (int i = 0; i < count; i++) { |
| 17 | uint32_t tag = tags[i]; |
| 18 | uint8_t data[4]; |
| 19 | size_t size = SkFontHost::GetTableSize(fontID, tag); |
| 20 | size_t bytes = SkFontHost::GetTableData(fontID, tag, |
| 21 | 0, sizeof(data), data); |
| 22 | SkDebugf(" tag=%c%c%c%c size=%d bytes=%d %x %x %x %x\n", |
| 23 | uint8_t(tag>>24), uint8_t(tag>>16), uint8_t(tag>>8), uint8_t(tag), |
| 24 | size, bytes, data[0], data[1], data[2], data[3]); |
| 25 | } |
reed@android.com | 9781ca5 | 2009-04-14 14:28:22 +0000 | [diff] [blame^] | 26 | |
| 27 | SkSfntTable_head head; |
| 28 | if (SkSfntUtils::ReadTable_head(fontID, &head)) { |
| 29 | SkDebugf("--- head: version=%x magic=%x upem=%d style=%x\n", head.fVersion, |
| 30 | head.fMagicNumber, head.fUnitsPerEm, head.fMacStyle); |
| 31 | } else { |
| 32 | SkDebugf("------- head wasn't read\n"); |
| 33 | } |
| 34 | |
| 35 | SkSfntTable_maxp maxp; |
| 36 | if (SkSfntUtils::ReadTable_maxp(fontID, &maxp)) { |
| 37 | SkDebugf("--- maxp: version=%x glyphs=%d points=%d ctrs=%d\n", maxp.fVersion, |
| 38 | maxp.fNumGlyphs, maxp.fMaxPoints, maxp.fMaxContours); |
| 39 | } else { |
| 40 | SkDebugf("------- maxp wasn't read\n"); |
| 41 | } |
reed@android.com | d6638e6 | 2009-04-08 05:03:52 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static void test_tables() { |
| 45 | static bool gOnce; |
| 46 | if (gOnce) { |
| 47 | return; |
| 48 | } |
| 49 | gOnce = true; |
| 50 | |
| 51 | static const char* gNames[] = { |
| 52 | "Arial", "Times", "Courier" |
| 53 | }; |
| 54 | |
| 55 | for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); i++) { |
| 56 | SkTypeface* tf = SkTypeface::CreateFromName(gNames[i], SkTypeface::kNormal); |
| 57 | if (tf) { |
| 58 | SkFontID fontID = tf->uniqueID(); |
| 59 | dump_font(gNames[i], fontID); |
| 60 | tf->unref(); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 65 | /* Some considerations for performance: |
| 66 | short -vs- long strings (measuring overhead) |
| 67 | tiny -vs- large pointsize (measure blit -vs- overhead) |
| 68 | 1 -vs- many point sizes (measure cache lookup) |
| 69 | normal -vs- subpixel -vs- lineartext (minor) |
| 70 | force purge after each draw to measure scaler |
| 71 | textencoding? |
| 72 | text -vs- postext - pathtext |
| 73 | */ |
| 74 | class TextBench : public SkBenchmark { |
| 75 | SkPaint fPaint; |
| 76 | int fCount; |
| 77 | SkPoint* fPos; |
| 78 | SkString fText; |
| 79 | SkString fName; |
| 80 | enum { N = 300 }; |
| 81 | public: |
| 82 | TextBench(const char text[], int ps, bool linearText, bool posText) { |
reed@android.com | d6638e6 | 2009-04-08 05:03:52 +0000 | [diff] [blame] | 83 | test_tables(); |
| 84 | |
reed@android.com | 3a859a0 | 2009-01-28 00:56:29 +0000 | [diff] [blame] | 85 | fText.set(text); |
| 86 | |
| 87 | fPaint.setAntiAlias(true); |
| 88 | fPaint.setTextSize(SkIntToScalar(ps)); |
| 89 | fPaint.setLinearText(linearText); |
| 90 | |
| 91 | if (posText) { |
| 92 | SkAutoTArray<SkScalar> storage(fText.size()); |
| 93 | SkScalar* widths = storage.get(); |
| 94 | fCount = fPaint.getTextWidths(fText.c_str(), fText.size(), widths); |
| 95 | fPos = new SkPoint[fCount]; |
| 96 | SkScalar x = 0; |
| 97 | for (int i = 0; i < fCount; i++) { |
| 98 | fPos[i].set(x, 0); |
| 99 | x += widths[i]; |
| 100 | } |
| 101 | } else { |
| 102 | fCount = 0; |
| 103 | fPos = NULL; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | virtual ~TextBench() { |
| 108 | delete[] fPos; |
| 109 | } |
| 110 | |
| 111 | protected: |
| 112 | virtual const char* onGetName() { |
| 113 | fName.printf("text_%g", SkScalarToFloat(fPaint.getTextSize())); |
| 114 | if (fPaint.isLinearText()) { |
| 115 | fName.append("_linear"); |
| 116 | } |
| 117 | if (fPos) { |
| 118 | fName.append("_pos"); |
| 119 | } |
| 120 | return fName.c_str(); |
| 121 | } |
| 122 | |
| 123 | virtual void onDraw(SkCanvas* canvas) { |
| 124 | const SkIPoint dim = this->getSize(); |
| 125 | SkRandom rand; |
| 126 | |
| 127 | SkPaint paint(fPaint); |
| 128 | this->setupPaint(&paint); |
| 129 | |
| 130 | const SkScalar x0 = SkIntToScalar(-10); |
| 131 | const SkScalar y0 = SkIntToScalar(-10); |
| 132 | |
| 133 | for (int i = 0; i < N; i++) { |
| 134 | SkScalar x = x0 + rand.nextUScalar1() * dim.fX; |
| 135 | SkScalar y = y0 + rand.nextUScalar1() * dim.fY; |
| 136 | if (fPos) { |
| 137 | canvas->save(SkCanvas::kMatrix_SaveFlag); |
| 138 | canvas->translate(x, y); |
| 139 | canvas->drawPosText(fText.c_str(), fText.size(), fPos, paint); |
| 140 | canvas->restore(); |
| 141 | } else { |
| 142 | canvas->drawText(fText.c_str(), fText.size(), x, y, paint); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | private: |
| 148 | typedef SkBenchmark INHERITED; |
| 149 | }; |
| 150 | |
| 151 | /////////////////////////////////////////////////////////////////////////////// |
| 152 | |
| 153 | #define STR "Hamburgefons" |
| 154 | #define SMALL 9 |
| 155 | #define BIG 48 |
| 156 | |
| 157 | static SkBenchmark* Fact0(void*) { return new TextBench(STR, SMALL, false, false); } |
| 158 | static SkBenchmark* Fact1(void*) { return new TextBench(STR, SMALL, false, true); } |
| 159 | static SkBenchmark* Fact2(void*) { return new TextBench(STR, SMALL, true, false); } |
| 160 | static SkBenchmark* Fact3(void*) { return new TextBench(STR, SMALL, true, true); } |
| 161 | static SkBenchmark* Fact4(void*) { return new TextBench(STR, BIG, false, false); } |
| 162 | static SkBenchmark* Fact5(void*) { return new TextBench(STR, BIG, false, true); } |
| 163 | static SkBenchmark* Fact6(void*) { return new TextBench(STR, BIG, true, false); } |
| 164 | static SkBenchmark* Fact7(void*) { return new TextBench(STR, BIG, true, true); } |
| 165 | |
| 166 | static BenchRegistry gReg0(Fact0); |
| 167 | static BenchRegistry gReg1(Fact1); |
| 168 | static BenchRegistry gReg2(Fact2); |
| 169 | static BenchRegistry gReg3(Fact3); |
| 170 | static BenchRegistry gReg4(Fact4); |
| 171 | static BenchRegistry gReg5(Fact5); |
| 172 | static BenchRegistry gReg6(Fact6); |
| 173 | static BenchRegistry gReg7(Fact7); |