blob: a4ed9f23dc3efb71110acd5a6871bb71af78d8bd [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"
reed@android.com9781ca52009-04-14 14:28:22 +00006#include "SkSfntUtils.h"
reed@android.com3a859a02009-01-28 00:56:29 +00007#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 */
19class TextBench : public SkBenchmark {
20 SkPaint fPaint;
21 int fCount;
22 SkPoint* fPos;
23 SkString fText;
24 SkString fName;
25 enum { N = 300 };
26public:
27 TextBench(const char text[], int ps, bool linearText, bool posText) {
reed@android.com3a859a02009-01-28 00:56:29 +000028 fText.set(text);
29
30 fPaint.setAntiAlias(true);
31 fPaint.setTextSize(SkIntToScalar(ps));
32 fPaint.setLinearText(linearText);
33
34 if (posText) {
35 SkAutoTArray<SkScalar> storage(fText.size());
36 SkScalar* widths = storage.get();
37 fCount = fPaint.getTextWidths(fText.c_str(), fText.size(), widths);
38 fPos = new SkPoint[fCount];
39 SkScalar x = 0;
40 for (int i = 0; i < fCount; i++) {
41 fPos[i].set(x, 0);
42 x += widths[i];
43 }
44 } else {
45 fCount = 0;
46 fPos = NULL;
47 }
48 }
49
50 virtual ~TextBench() {
51 delete[] fPos;
52 }
53
54protected:
55 virtual const char* onGetName() {
56 fName.printf("text_%g", SkScalarToFloat(fPaint.getTextSize()));
57 if (fPaint.isLinearText()) {
58 fName.append("_linear");
59 }
60 if (fPos) {
61 fName.append("_pos");
62 }
63 return fName.c_str();
64 }
65
66 virtual void onDraw(SkCanvas* canvas) {
67 const SkIPoint dim = this->getSize();
68 SkRandom rand;
69
70 SkPaint paint(fPaint);
71 this->setupPaint(&paint);
72
73 const SkScalar x0 = SkIntToScalar(-10);
74 const SkScalar y0 = SkIntToScalar(-10);
75
76 for (int i = 0; i < N; i++) {
77 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
78 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
79 if (fPos) {
80 canvas->save(SkCanvas::kMatrix_SaveFlag);
81 canvas->translate(x, y);
82 canvas->drawPosText(fText.c_str(), fText.size(), fPos, paint);
83 canvas->restore();
84 } else {
85 canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
86 }
87 }
88 }
89
90private:
91 typedef SkBenchmark INHERITED;
92};
93
94///////////////////////////////////////////////////////////////////////////////
95
96#define STR "Hamburgefons"
97#define SMALL 9
98#define BIG 48
99
100static SkBenchmark* Fact0(void*) { return new TextBench(STR, SMALL, false, false); }
101static SkBenchmark* Fact1(void*) { return new TextBench(STR, SMALL, false, true); }
102static SkBenchmark* Fact2(void*) { return new TextBench(STR, SMALL, true, false); }
103static SkBenchmark* Fact3(void*) { return new TextBench(STR, SMALL, true, true); }
104static SkBenchmark* Fact4(void*) { return new TextBench(STR, BIG, false, false); }
105static SkBenchmark* Fact5(void*) { return new TextBench(STR, BIG, false, true); }
106static SkBenchmark* Fact6(void*) { return new TextBench(STR, BIG, true, false); }
107static SkBenchmark* Fact7(void*) { return new TextBench(STR, BIG, true, true); }
108
109static BenchRegistry gReg0(Fact0);
110static BenchRegistry gReg1(Fact1);
111static BenchRegistry gReg2(Fact2);
112static BenchRegistry gReg3(Fact3);
113static BenchRegistry gReg4(Fact4);
114static BenchRegistry gReg5(Fact5);
115static BenchRegistry gReg6(Fact6);
116static BenchRegistry gReg7(Fact7);