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