blob: f2b96048afd6950b3aad52c139285decd9b795d4 [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;
reed@google.comb9d84f32011-01-17 19:45:43 +000025 enum { N = 600 };
reed@android.com3a859a02009-01-28 00:56:29 +000026public:
reed@android.come9d00602009-09-02 21:12:42 +000027 TextBench(void* param, const char text[], int ps, bool linearText,
reed@google.comb9d84f32011-01-17 19:45:43 +000028 bool posText, SkColor color = SK_ColorBLACK) : 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);
reed@google.comb9d84f32011-01-17 19:45:43 +000034 fPaint.setColor(color);
reed@android.com3a859a02009-01-28 00:56:29 +000035
36 if (posText) {
37 SkAutoTArray<SkScalar> storage(fText.size());
38 SkScalar* widths = storage.get();
39 fCount = fPaint.getTextWidths(fText.c_str(), fText.size(), widths);
40 fPos = new SkPoint[fCount];
41 SkScalar x = 0;
42 for (int i = 0; i < fCount; i++) {
43 fPos[i].set(x, 0);
44 x += widths[i];
45 }
46 } else {
47 fCount = 0;
48 fPos = NULL;
49 }
50 }
reed@google.comb9d84f32011-01-17 19:45:43 +000051
reed@android.com3a859a02009-01-28 00:56:29 +000052 virtual ~TextBench() {
53 delete[] fPos;
54 }
55
56protected:
57 virtual const char* onGetName() {
58 fName.printf("text_%g", SkScalarToFloat(fPaint.getTextSize()));
59 if (fPaint.isLinearText()) {
60 fName.append("_linear");
61 }
62 if (fPos) {
63 fName.append("_pos");
64 }
reed@google.comb9d84f32011-01-17 19:45:43 +000065
66 if (SK_ColorBLACK != fPaint.getColor()) {
67 fName.appendf("_%02X", fPaint.getAlpha());
68 }
reed@android.com3a859a02009-01-28 00:56:29 +000069 return fName.c_str();
70 }
71
72 virtual void onDraw(SkCanvas* canvas) {
73 const SkIPoint dim = this->getSize();
74 SkRandom rand;
75
76 SkPaint paint(fPaint);
77 this->setupPaint(&paint);
reed@google.comb9d84f32011-01-17 19:45:43 +000078 paint.setColor(fPaint.getColor()); // need our specified color
reed@android.com3a859a02009-01-28 00:56:29 +000079
80 const SkScalar x0 = SkIntToScalar(-10);
81 const SkScalar y0 = SkIntToScalar(-10);
82
reed@google.comb9d84f32011-01-17 19:45:43 +000083 for (int i = 0; i < N; i++) {
84 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
85 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
86 if (fPos) {
87 canvas->save(SkCanvas::kMatrix_SaveFlag);
88 canvas->translate(x, y);
89 canvas->drawPosText(fText.c_str(), fText.size(), fPos, paint);
90 canvas->restore();
91 } else {
92 canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
reed@android.com3a859a02009-01-28 00:56:29 +000093 }
94 }
95 }
96
97private:
98 typedef SkBenchmark INHERITED;
99};
100
101///////////////////////////////////////////////////////////////////////////////
102
103#define STR "Hamburgefons"
104#define SMALL 9
105#define BIG 48
106
reed@android.come9d00602009-09-02 21:12:42 +0000107static SkBenchmark* Fact0(void* p) { return new TextBench(p, STR, SMALL, false, false); }
reed@google.comb9d84f32011-01-17 19:45:43 +0000108static SkBenchmark* Fact01(void* p) { return new TextBench(p, STR, SMALL, false, false, 0xFFFF0000); }
109static SkBenchmark* Fact02(void* p) { return new TextBench(p, STR, SMALL, false, false, 0x88FF0000); }
reed@android.comeca48362010-12-20 18:34:17 +0000110
reed@android.come9d00602009-09-02 21:12:42 +0000111static SkBenchmark* Fact1(void* p) { return new TextBench(p, STR, SMALL, false, true); }
112static SkBenchmark* Fact2(void* p) { return new TextBench(p, STR, SMALL, true, false); }
113static SkBenchmark* Fact3(void* p) { return new TextBench(p, STR, SMALL, true, true); }
reed@google.comb9d84f32011-01-17 19:45:43 +0000114
reed@android.come9d00602009-09-02 21:12:42 +0000115static SkBenchmark* Fact4(void* p) { return new TextBench(p, STR, BIG, false, false); }
reed@google.comb9d84f32011-01-17 19:45:43 +0000116static SkBenchmark* Fact41(void* p) { return new TextBench(p, STR, BIG, false, false, 0xFFFF0000); }
117static SkBenchmark* Fact42(void* p) { return new TextBench(p, STR, BIG, false, false, 0x88FF0000); }
118
reed@android.come9d00602009-09-02 21:12:42 +0000119static SkBenchmark* Fact5(void* p) { return new TextBench(p, STR, BIG, false, true); }
120static SkBenchmark* Fact6(void* p) { return new TextBench(p, STR, BIG, true, false); }
121static SkBenchmark* Fact7(void* p) { return new TextBench(p, STR, BIG, true, true); }
reed@android.com3a859a02009-01-28 00:56:29 +0000122
reed@google.comb9d84f32011-01-17 19:45:43 +0000123static BenchRegistry gReg0(Fact0);
124static BenchRegistry gReg01(Fact01);
125static BenchRegistry gReg02(Fact02);
reed@android.com3a859a02009-01-28 00:56:29 +0000126static BenchRegistry gReg1(Fact1);
127static BenchRegistry gReg2(Fact2);
128static BenchRegistry gReg3(Fact3);
129static BenchRegistry gReg4(Fact4);
reed@google.comb9d84f32011-01-17 19:45:43 +0000130static BenchRegistry gReg41(Fact41);
131static BenchRegistry gReg42(Fact42);
reed@android.com3a859a02009-01-28 00:56:29 +0000132static BenchRegistry gReg5(Fact5);
133static BenchRegistry gReg6(Fact6);
134static BenchRegistry gReg7(Fact7);
reed@android.comeca48362010-12-20 18:34:17 +0000135