blob: 752c7ebfcd4a50e3c929328d7416bfbdcd5ee912 [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:
reed@android.come9d00602009-09-02 21:12:42 +000027 TextBench(void* param, const char text[], int ps, bool linearText,
28 bool posText) : INHERITED(param) {
reed@android.com3a859a02009-01-28 00:56:29 +000029 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
55protected:
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.com867ee802009-10-20 13:55:41 +000076 const SkColor colors[] = { SK_ColorBLACK, SK_ColorGRAY };
reed@android.com3a859a02009-01-28 00:56:29 +000077
reed@android.com867ee802009-10-20 13:55:41 +000078 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.com3a859a02009-01-28 00:56:29 +000091 }
92 }
93 }
94
95private:
96 typedef SkBenchmark INHERITED;
97};
98
99///////////////////////////////////////////////////////////////////////////////
100
101#define STR "Hamburgefons"
102#define SMALL 9
103#define BIG 48
104
reed@android.come9d00602009-09-02 21:12:42 +0000105static SkBenchmark* Fact0(void* p) { return new TextBench(p, STR, SMALL, false, false); }
reed@android.comeca48362010-12-20 18:34:17 +0000106static BenchRegistry gReg0(Fact0);
107
reed@android.come9d00602009-09-02 21:12:42 +0000108static SkBenchmark* Fact1(void* p) { return new TextBench(p, STR, SMALL, false, true); }
109static SkBenchmark* Fact2(void* p) { return new TextBench(p, STR, SMALL, true, false); }
110static SkBenchmark* Fact3(void* p) { return new TextBench(p, STR, SMALL, true, true); }
111static SkBenchmark* Fact4(void* p) { return new TextBench(p, STR, BIG, false, false); }
112static SkBenchmark* Fact5(void* p) { return new TextBench(p, STR, BIG, false, true); }
113static SkBenchmark* Fact6(void* p) { return new TextBench(p, STR, BIG, true, false); }
114static SkBenchmark* Fact7(void* p) { return new TextBench(p, STR, BIG, true, true); }
reed@android.com3a859a02009-01-28 00:56:29 +0000115
reed@android.com3a859a02009-01-28 00:56:29 +0000116static BenchRegistry gReg1(Fact1);
117static BenchRegistry gReg2(Fact2);
118static BenchRegistry gReg3(Fact3);
119static BenchRegistry gReg4(Fact4);
120static BenchRegistry gReg5(Fact5);
121static BenchRegistry gReg6(Fact6);
122static BenchRegistry gReg7(Fact7);
reed@android.comeca48362010-12-20 18:34:17 +0000123