blob: d32d0fd105f13d8c3f3db8964a3135a30bde860b [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"
13#include "SkString.h"
14#include "SkTemplates.h"
15
reed@google.com095186a2011-10-18 12:21:50 +000016enum FontQuality {
17 kBW,
18 kAA,
19 kLCD
20};
21
22static const char* fontQualityName(const SkPaint& paint) {
23 if (!paint.isAntiAlias()) {
24 return "BW";
25 }
26 if (paint.isLCDRenderText()) {
27 return "LCD";
28 }
29 return "AA";
30}
31
reed@android.com3a859a02009-01-28 00:56:29 +000032/* Some considerations for performance:
33 short -vs- long strings (measuring overhead)
34 tiny -vs- large pointsize (measure blit -vs- overhead)
35 1 -vs- many point sizes (measure cache lookup)
36 normal -vs- subpixel -vs- lineartext (minor)
37 force purge after each draw to measure scaler
38 textencoding?
39 text -vs- postext - pathtext
40 */
41class TextBench : public SkBenchmark {
42 SkPaint fPaint;
reed@android.com3a859a02009-01-28 00:56:29 +000043 SkString fText;
44 SkString fName;
reed@google.com095186a2011-10-18 12:21:50 +000045 FontQuality fFQ;
reed@google.com055af882011-11-23 15:52:02 +000046 bool fDoPos;
47 SkPoint* fPos;
reed@android.com3a859a02009-01-28 00:56:29 +000048public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000049 TextBench(const char text[], int ps,
50 SkColor color, FontQuality fq, bool doPos = false) {
bsalomon@google.com7fbc6042012-08-13 22:10:05 +000051 fPos = NULL;
reed@google.com095186a2011-10-18 12:21:50 +000052 fFQ = fq;
reed@google.com055af882011-11-23 15:52:02 +000053 fDoPos = doPos;
reed@android.com3a859a02009-01-28 00:56:29 +000054 fText.set(text);
55
reed@google.com095186a2011-10-18 12:21:50 +000056 fPaint.setAntiAlias(kBW != fq);
57 fPaint.setLCDRenderText(kLCD == fq);
reed@android.com3a859a02009-01-28 00:56:29 +000058 fPaint.setTextSize(SkIntToScalar(ps));
reed@google.comb9d84f32011-01-17 19:45:43 +000059 fPaint.setColor(color);
reed@google.com055af882011-11-23 15:52:02 +000060
61 if (doPos) {
62 size_t len = strlen(text);
63 SkScalar* adv = new SkScalar[len];
64 fPaint.getTextWidths(text, len, adv);
65 fPos = new SkPoint[len];
66 SkScalar x = 0;
67 for (size_t i = 0; i < len; ++i) {
68 fPos[i].set(x, SkIntToScalar(50));
69 x += adv[i];
70 }
71 delete[] adv;
72 }
73 }
74
75 virtual ~TextBench() {
76 delete[] fPos;
reed@android.com3a859a02009-01-28 00:56:29 +000077 }
78
79protected:
80 virtual const char* onGetName() {
81 fName.printf("text_%g", SkScalarToFloat(fPaint.getTextSize()));
reed@google.com055af882011-11-23 15:52:02 +000082 if (fDoPos) {
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +000083 fName.append("_pos");
reed@google.com055af882011-11-23 15:52:02 +000084 }
reed@google.com095186a2011-10-18 12:21:50 +000085 fName.appendf("_%s", fontQualityName(fPaint));
reed@google.comb9d84f32011-01-17 19:45:43 +000086 if (SK_ColorBLACK != fPaint.getColor()) {
87 fName.appendf("_%02X", fPaint.getAlpha());
reed@google.com095186a2011-10-18 12:21:50 +000088 } else {
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +000089 fName.append("_BK");
reed@google.comb9d84f32011-01-17 19:45:43 +000090 }
reed@android.com3a859a02009-01-28 00:56:29 +000091 return fName.c_str();
92 }
93
commit-bot@chromium.org33614712013-12-03 18:17:16 +000094 virtual void onDraw(const int loops, SkCanvas* canvas) {
reed@android.com3a859a02009-01-28 00:56:29 +000095 const SkIPoint dim = this->getSize();
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000096 SkRandom rand;
reed@android.com3a859a02009-01-28 00:56:29 +000097
98 SkPaint paint(fPaint);
99 this->setupPaint(&paint);
reed@google.com095186a2011-10-18 12:21:50 +0000100 // explicitly need these
101 paint.setColor(fPaint.getColor());
102 paint.setAntiAlias(kBW != fFQ);
103 paint.setLCDRenderText(kLCD == fFQ);
reed@android.com3a859a02009-01-28 00:56:29 +0000104
105 const SkScalar x0 = SkIntToScalar(-10);
106 const SkScalar y0 = SkIntToScalar(-10);
107
reed@google.com055af882011-11-23 15:52:02 +0000108 if (fDoPos) {
109 // realistically, the matrix is often at least translated, so we
110 // do that since it exercises different code in drawPosText.
111 canvas->translate(SK_Scalar1, SK_Scalar1);
112 }
113
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000114 for (int i = 0; i < loops; i++) {
reed@google.com055af882011-11-23 15:52:02 +0000115 if (fDoPos) {
116 canvas->drawPosText(fText.c_str(), fText.size(), fPos, paint);
117 } else {
118 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
119 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
120 canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
121 }
reed@android.com3a859a02009-01-28 00:56:29 +0000122 }
123 }
124
125private:
126 typedef SkBenchmark INHERITED;
127};
128
129///////////////////////////////////////////////////////////////////////////////
130
131#define STR "Hamburgefons"
reed@android.com3a859a02009-01-28 00:56:29 +0000132
mtklein@google.com410e6e82013-09-13 19:52:27 +0000133DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW); )
134DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kBW); )
135DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kBW); )
reed@android.comeca48362010-12-20 18:34:17 +0000136
mtklein@google.com410e6e82013-09-13 19:52:27 +0000137DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kAA); )
138DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kAA); )
139DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kAA); )
reed@google.comb9d84f32011-01-17 19:45:43 +0000140
mtklein@google.com410e6e82013-09-13 19:52:27 +0000141DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kLCD); )
142DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kLCD); )
143DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kLCD); )
reed@google.comb9d84f32011-01-17 19:45:43 +0000144
mtklein@google.com410e6e82013-09-13 19:52:27 +0000145DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kAA, true); )