blob: 76e7cdd14f8b8682473fb45f2950898517a688e6 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com3a859a02009-01-28 00:56:29 +00008#include "SkBenchmark.h"
9#include "SkCanvas.h"
reed@android.comd6638e62009-04-08 05:03:52 +000010#include "SkFontHost.h"
reed@android.com3a859a02009-01-28 00:56:29 +000011#include "SkPaint.h"
12#include "SkRandom.h"
reed@android.com9781ca52009-04-14 14:28:22 +000013#include "SkSfntUtils.h"
reed@android.com3a859a02009-01-28 00:56:29 +000014#include "SkString.h"
15#include "SkTemplates.h"
16
17/* Some considerations for performance:
18 short -vs- long strings (measuring overhead)
19 tiny -vs- large pointsize (measure blit -vs- overhead)
20 1 -vs- many point sizes (measure cache lookup)
21 normal -vs- subpixel -vs- lineartext (minor)
22 force purge after each draw to measure scaler
23 textencoding?
24 text -vs- postext - pathtext
25 */
26class TextBench : public SkBenchmark {
27 SkPaint fPaint;
28 int fCount;
29 SkPoint* fPos;
30 SkString fText;
31 SkString fName;
reed@google.comb9d84f32011-01-17 19:45:43 +000032 enum { N = 600 };
reed@android.com3a859a02009-01-28 00:56:29 +000033public:
reed@android.come9d00602009-09-02 21:12:42 +000034 TextBench(void* param, const char text[], int ps, bool linearText,
reed@google.comb9d84f32011-01-17 19:45:43 +000035 bool posText, SkColor color = SK_ColorBLACK) : INHERITED(param) {
reed@android.com3a859a02009-01-28 00:56:29 +000036 fText.set(text);
37
38 fPaint.setAntiAlias(true);
39 fPaint.setTextSize(SkIntToScalar(ps));
40 fPaint.setLinearText(linearText);
reed@google.comb9d84f32011-01-17 19:45:43 +000041 fPaint.setColor(color);
reed@android.com3a859a02009-01-28 00:56:29 +000042
43 if (posText) {
44 SkAutoTArray<SkScalar> storage(fText.size());
45 SkScalar* widths = storage.get();
46 fCount = fPaint.getTextWidths(fText.c_str(), fText.size(), widths);
47 fPos = new SkPoint[fCount];
48 SkScalar x = 0;
49 for (int i = 0; i < fCount; i++) {
50 fPos[i].set(x, 0);
51 x += widths[i];
52 }
53 } else {
54 fCount = 0;
55 fPos = NULL;
56 }
57 }
reed@google.comb9d84f32011-01-17 19:45:43 +000058
reed@android.com3a859a02009-01-28 00:56:29 +000059 virtual ~TextBench() {
60 delete[] fPos;
61 }
62
63protected:
64 virtual const char* onGetName() {
65 fName.printf("text_%g", SkScalarToFloat(fPaint.getTextSize()));
66 if (fPaint.isLinearText()) {
67 fName.append("_linear");
68 }
69 if (fPos) {
70 fName.append("_pos");
71 }
reed@google.comb9d84f32011-01-17 19:45:43 +000072
73 if (SK_ColorBLACK != fPaint.getColor()) {
74 fName.appendf("_%02X", fPaint.getAlpha());
75 }
reed@android.com3a859a02009-01-28 00:56:29 +000076 return fName.c_str();
77 }
78
79 virtual void onDraw(SkCanvas* canvas) {
80 const SkIPoint dim = this->getSize();
81 SkRandom rand;
82
83 SkPaint paint(fPaint);
84 this->setupPaint(&paint);
reed@google.comb9d84f32011-01-17 19:45:43 +000085 paint.setColor(fPaint.getColor()); // need our specified color
reed@android.com3a859a02009-01-28 00:56:29 +000086
87 const SkScalar x0 = SkIntToScalar(-10);
88 const SkScalar y0 = SkIntToScalar(-10);
89
reed@google.comb9d84f32011-01-17 19:45:43 +000090 for (int i = 0; i < N; i++) {
91 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
92 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
93 if (fPos) {
94 canvas->save(SkCanvas::kMatrix_SaveFlag);
95 canvas->translate(x, y);
96 canvas->drawPosText(fText.c_str(), fText.size(), fPos, paint);
97 canvas->restore();
98 } else {
99 canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
reed@android.com3a859a02009-01-28 00:56:29 +0000100 }
101 }
102 }
103
104private:
105 typedef SkBenchmark INHERITED;
106};
107
108///////////////////////////////////////////////////////////////////////////////
109
110#define STR "Hamburgefons"
111#define SMALL 9
112#define BIG 48
113
reed@android.come9d00602009-09-02 21:12:42 +0000114static SkBenchmark* Fact0(void* p) { return new TextBench(p, STR, SMALL, false, false); }
reed@google.comb9d84f32011-01-17 19:45:43 +0000115static SkBenchmark* Fact01(void* p) { return new TextBench(p, STR, SMALL, false, false, 0xFFFF0000); }
116static SkBenchmark* Fact02(void* p) { return new TextBench(p, STR, SMALL, false, false, 0x88FF0000); }
reed@android.comeca48362010-12-20 18:34:17 +0000117
reed@android.come9d00602009-09-02 21:12:42 +0000118static SkBenchmark* Fact1(void* p) { return new TextBench(p, STR, SMALL, false, true); }
119static SkBenchmark* Fact2(void* p) { return new TextBench(p, STR, SMALL, true, false); }
120static SkBenchmark* Fact3(void* p) { return new TextBench(p, STR, SMALL, true, true); }
reed@google.comb9d84f32011-01-17 19:45:43 +0000121
reed@android.come9d00602009-09-02 21:12:42 +0000122static SkBenchmark* Fact4(void* p) { return new TextBench(p, STR, BIG, false, false); }
reed@google.comb9d84f32011-01-17 19:45:43 +0000123static SkBenchmark* Fact41(void* p) { return new TextBench(p, STR, BIG, false, false, 0xFFFF0000); }
124static SkBenchmark* Fact42(void* p) { return new TextBench(p, STR, BIG, false, false, 0x88FF0000); }
125
reed@android.come9d00602009-09-02 21:12:42 +0000126static SkBenchmark* Fact5(void* p) { return new TextBench(p, STR, BIG, false, true); }
127static SkBenchmark* Fact6(void* p) { return new TextBench(p, STR, BIG, true, false); }
128static SkBenchmark* Fact7(void* p) { return new TextBench(p, STR, BIG, true, true); }
reed@android.com3a859a02009-01-28 00:56:29 +0000129
reed@google.comb9d84f32011-01-17 19:45:43 +0000130static BenchRegistry gReg0(Fact0);
131static BenchRegistry gReg01(Fact01);
132static BenchRegistry gReg02(Fact02);
reed@android.com3a859a02009-01-28 00:56:29 +0000133static BenchRegistry gReg1(Fact1);
134static BenchRegistry gReg2(Fact2);
135static BenchRegistry gReg3(Fact3);
136static BenchRegistry gReg4(Fact4);
reed@google.comb9d84f32011-01-17 19:45:43 +0000137static BenchRegistry gReg41(Fact41);
138static BenchRegistry gReg42(Fact42);
reed@android.com3a859a02009-01-28 00:56:29 +0000139static BenchRegistry gReg5(Fact5);
140static BenchRegistry gReg6(Fact6);
141static BenchRegistry gReg7(Fact7);
reed@android.comeca48362010-12-20 18:34:17 +0000142