blob: 6c3bab13d9145ac4125edd7d678c2abff478494b [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 */
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
reed@android.com3a859a02009-01-28 00:56:29 +00009#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkRandom.h"
12#include "SkString.h"
13#include "SkTemplates.h"
14
reed@google.com095186a2011-10-18 12:21:50 +000015enum FontQuality {
16 kBW,
17 kAA,
18 kLCD
19};
20
21static const char* fontQualityName(const SkPaint& paint) {
22 if (!paint.isAntiAlias()) {
23 return "BW";
24 }
25 if (paint.isLCDRenderText()) {
26 return "LCD";
27 }
28 return "AA";
29}
30
reed@android.com3a859a02009-01-28 00:56:29 +000031/* Some considerations for performance:
32 short -vs- long strings (measuring overhead)
33 tiny -vs- large pointsize (measure blit -vs- overhead)
34 1 -vs- many point sizes (measure cache lookup)
35 normal -vs- subpixel -vs- lineartext (minor)
36 force purge after each draw to measure scaler
37 textencoding?
38 text -vs- postext - pathtext
39 */
tfarinaf168b862014-06-19 12:32:29 -070040class TextBench : public Benchmark {
reed@android.com3a859a02009-01-28 00:56:29 +000041 SkPaint fPaint;
reed@android.com3a859a02009-01-28 00:56:29 +000042 SkString fText;
43 SkString fName;
reed@google.com095186a2011-10-18 12:21:50 +000044 FontQuality fFQ;
reed@google.com055af882011-11-23 15:52:02 +000045 bool fDoPos;
46 SkPoint* fPos;
reed@android.com3a859a02009-01-28 00:56:29 +000047public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000048 TextBench(const char text[], int ps,
49 SkColor color, FontQuality fq, bool doPos = false) {
bsalomon@google.com7fbc6042012-08-13 22:10:05 +000050 fPos = NULL;
reed@google.com095186a2011-10-18 12:21:50 +000051 fFQ = fq;
reed@google.com055af882011-11-23 15:52:02 +000052 fDoPos = doPos;
reed@android.com3a859a02009-01-28 00:56:29 +000053 fText.set(text);
54
reed@google.com095186a2011-10-18 12:21:50 +000055 fPaint.setAntiAlias(kBW != fq);
56 fPaint.setLCDRenderText(kLCD == fq);
reed@android.com3a859a02009-01-28 00:56:29 +000057 fPaint.setTextSize(SkIntToScalar(ps));
reed@google.comb9d84f32011-01-17 19:45:43 +000058 fPaint.setColor(color);
reed@google.com055af882011-11-23 15:52:02 +000059
60 if (doPos) {
61 size_t len = strlen(text);
62 SkScalar* adv = new SkScalar[len];
63 fPaint.getTextWidths(text, len, adv);
64 fPos = new SkPoint[len];
65 SkScalar x = 0;
66 for (size_t i = 0; i < len; ++i) {
67 fPos[i].set(x, SkIntToScalar(50));
68 x += adv[i];
69 }
70 delete[] adv;
71 }
72 }
73
74 virtual ~TextBench() {
75 delete[] fPos;
reed@android.com3a859a02009-01-28 00:56:29 +000076 }
77
78protected:
79 virtual const char* onGetName() {
80 fName.printf("text_%g", SkScalarToFloat(fPaint.getTextSize()));
reed@google.com055af882011-11-23 15:52:02 +000081 if (fDoPos) {
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +000082 fName.append("_pos");
reed@google.com055af882011-11-23 15:52:02 +000083 }
reed@google.com095186a2011-10-18 12:21:50 +000084 fName.appendf("_%s", fontQualityName(fPaint));
reed@google.comb9d84f32011-01-17 19:45:43 +000085 if (SK_ColorBLACK != fPaint.getColor()) {
86 fName.appendf("_%02X", fPaint.getAlpha());
reed@google.com095186a2011-10-18 12:21:50 +000087 } else {
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +000088 fName.append("_BK");
reed@google.comb9d84f32011-01-17 19:45:43 +000089 }
reed@android.com3a859a02009-01-28 00:56:29 +000090 return fName.c_str();
91 }
92
commit-bot@chromium.org33614712013-12-03 18:17:16 +000093 virtual void onDraw(const int loops, SkCanvas* canvas) {
reed@android.com3a859a02009-01-28 00:56:29 +000094 const SkIPoint dim = this->getSize();
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000095 SkRandom rand;
reed@android.com3a859a02009-01-28 00:56:29 +000096
97 SkPaint paint(fPaint);
98 this->setupPaint(&paint);
reed@google.com095186a2011-10-18 12:21:50 +000099 // explicitly need these
100 paint.setColor(fPaint.getColor());
101 paint.setAntiAlias(kBW != fFQ);
102 paint.setLCDRenderText(kLCD == fFQ);
reed@android.com3a859a02009-01-28 00:56:29 +0000103
104 const SkScalar x0 = SkIntToScalar(-10);
105 const SkScalar y0 = SkIntToScalar(-10);
106
reed@google.com055af882011-11-23 15:52:02 +0000107 if (fDoPos) {
108 // realistically, the matrix is often at least translated, so we
109 // do that since it exercises different code in drawPosText.
110 canvas->translate(SK_Scalar1, SK_Scalar1);
111 }
112
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000113 for (int i = 0; i < loops; i++) {
reed@google.com055af882011-11-23 15:52:02 +0000114 if (fDoPos) {
115 canvas->drawPosText(fText.c_str(), fText.size(), fPos, paint);
116 } else {
117 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
118 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
119 canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
120 }
reed@android.com3a859a02009-01-28 00:56:29 +0000121 }
122 }
123
124private:
tfarinaf168b862014-06-19 12:32:29 -0700125 typedef Benchmark INHERITED;
reed@android.com3a859a02009-01-28 00:56:29 +0000126};
127
128///////////////////////////////////////////////////////////////////////////////
129
130#define STR "Hamburgefons"
reed@android.com3a859a02009-01-28 00:56:29 +0000131
mtklein@google.com410e6e82013-09-13 19:52:27 +0000132DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW); )
133DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kBW); )
134DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kBW); )
reed@android.comeca48362010-12-20 18:34:17 +0000135
mtklein@google.com410e6e82013-09-13 19:52:27 +0000136DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kAA); )
137DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kAA); )
138DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kAA); )
reed@google.comb9d84f32011-01-17 19:45:43 +0000139
mtklein@google.com410e6e82013-09-13 19:52:27 +0000140DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kLCD); )
141DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kLCD); )
142DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kLCD); )
reed@google.comb9d84f32011-01-17 19:45:43 +0000143
mtklein@google.com410e6e82013-09-13 19:52:27 +0000144DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kAA, true); )