blob: 04f824c700c055106de209b2f8e6e630edd1a724 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
joshualitt02b05012015-02-11 06:56:30 -08007
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
joshualitt02b05012015-02-11 06:56:30 -08009#include "Resources.h"
reed@android.com3a859a02009-01-28 00:56:29 +000010#include "SkCanvas.h"
11#include "SkPaint.h"
12#include "SkRandom.h"
joshualitt02b05012015-02-11 06:56:30 -080013#include "SkStream.h"
reed@android.com3a859a02009-01-28 00:56:29 +000014#include "SkString.h"
15#include "SkTemplates.h"
joshualitt02b05012015-02-11 06:56:30 -080016#include "SkTypeface.h"
reed@android.com3a859a02009-01-28 00:56:29 +000017
reed@google.com095186a2011-10-18 12:21:50 +000018enum FontQuality {
19 kBW,
20 kAA,
joshualitt02b05012015-02-11 06:56:30 -080021 kLCD,
reed@google.com095186a2011-10-18 12:21:50 +000022};
23
24static const char* fontQualityName(const SkPaint& paint) {
25 if (!paint.isAntiAlias()) {
26 return "BW";
27 }
28 if (paint.isLCDRenderText()) {
29 return "LCD";
30 }
31 return "AA";
32}
33
reed@android.com3a859a02009-01-28 00:56:29 +000034/* Some considerations for performance:
35 short -vs- long strings (measuring overhead)
36 tiny -vs- large pointsize (measure blit -vs- overhead)
37 1 -vs- many point sizes (measure cache lookup)
38 normal -vs- subpixel -vs- lineartext (minor)
39 force purge after each draw to measure scaler
40 textencoding?
41 text -vs- postext - pathtext
42 */
tfarinaf168b862014-06-19 12:32:29 -070043class TextBench : public Benchmark {
reed@android.com3a859a02009-01-28 00:56:29 +000044 SkPaint fPaint;
reed@android.com3a859a02009-01-28 00:56:29 +000045 SkString fText;
46 SkString fName;
reed@google.com095186a2011-10-18 12:21:50 +000047 FontQuality fFQ;
reed@google.com055af882011-11-23 15:52:02 +000048 bool fDoPos;
joshualitt02b05012015-02-11 06:56:30 -080049 bool fDoColorEmoji;
50 SkAutoTUnref<SkTypeface> fColorEmojiTypeface;
reed@google.com055af882011-11-23 15:52:02 +000051 SkPoint* fPos;
reed@android.com3a859a02009-01-28 00:56:29 +000052public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000053 TextBench(const char text[], int ps,
kkinnunenb4a797f2015-05-21 06:15:28 -070054 SkColor color, FontQuality fq, bool doColorEmoji = false, bool doPos = false)
55 : fText(text)
56 , fFQ(fq)
57 , fDoPos(doPos)
58 , fDoColorEmoji(doColorEmoji)
halcanary96fcdcc2015-08-27 07:41:13 -070059 , fPos(nullptr) {
reed@google.com095186a2011-10-18 12:21:50 +000060 fPaint.setAntiAlias(kBW != fq);
61 fPaint.setLCDRenderText(kLCD == fq);
reed@android.com3a859a02009-01-28 00:56:29 +000062 fPaint.setTextSize(SkIntToScalar(ps));
reed@google.comb9d84f32011-01-17 19:45:43 +000063 fPaint.setColor(color);
kkinnunenb4a797f2015-05-21 06:15:28 -070064 }
reed@google.com055af882011-11-23 15:52:02 +000065
kkinnunenb4a797f2015-05-21 06:15:28 -070066 virtual ~TextBench() {
67 delete[] fPos;
68 }
69
70protected:
joshualitt8a6697a2015-09-30 12:11:07 -070071 void onDelayedSetup() override {
kkinnunenb4a797f2015-05-21 06:15:28 -070072 if (fDoColorEmoji) {
joshualitt02b05012015-02-11 06:56:30 -080073 SkASSERT(kBW == fFQ);
bungeman3ffa1262015-04-30 17:12:58 -040074 fColorEmojiTypeface.reset(GetResourceAsTypeface("/fonts/Funkster.ttf"));
joshualitt02b05012015-02-11 06:56:30 -080075 }
76
kkinnunenb4a797f2015-05-21 06:15:28 -070077 if (fDoPos) {
78 size_t len = fText.size();
reed@google.com055af882011-11-23 15:52:02 +000079 SkScalar* adv = new SkScalar[len];
kkinnunenb4a797f2015-05-21 06:15:28 -070080 fPaint.getTextWidths(fText.c_str(), len, adv);
reed@google.com055af882011-11-23 15:52:02 +000081 fPos = new SkPoint[len];
82 SkScalar x = 0;
83 for (size_t i = 0; i < len; ++i) {
84 fPos[i].set(x, SkIntToScalar(50));
85 x += adv[i];
86 }
87 delete[] adv;
88 }
89 }
90
reed@android.com3a859a02009-01-28 00:56:29 +000091
mtkleinf0599002015-07-13 06:18:39 -070092 const char* onGetName() override {
reed@android.com3a859a02009-01-28 00:56:29 +000093 fName.printf("text_%g", SkScalarToFloat(fPaint.getTextSize()));
reed@google.com055af882011-11-23 15:52:02 +000094 if (fDoPos) {
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +000095 fName.append("_pos");
reed@google.com055af882011-11-23 15:52:02 +000096 }
reed@google.com095186a2011-10-18 12:21:50 +000097 fName.appendf("_%s", fontQualityName(fPaint));
mtklein9c84b112015-08-26 13:41:31 -070098 if (SK_ColorBLACK == fPaint.getColor()) {
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +000099 fName.append("_BK");
mtklein9c84b112015-08-26 13:41:31 -0700100 } else if (SK_ColorWHITE == fPaint.getColor()) {
101 fName.append("_WT");
102 } else {
103 fName.appendf("_%02X", fPaint.getAlpha());
reed@google.comb9d84f32011-01-17 19:45:43 +0000104 }
joshualitt02b05012015-02-11 06:56:30 -0800105
kkinnunenb4a797f2015-05-21 06:15:28 -0700106 if (fDoColorEmoji) {
joshualitt02b05012015-02-11 06:56:30 -0800107 fName.append("_ColorEmoji");
108 }
109
reed@android.com3a859a02009-01-28 00:56:29 +0000110 return fName.c_str();
111 }
112
mtkleina1ebeb22015-10-01 09:43:39 -0700113 void onDraw(int loops, SkCanvas* canvas) override {
reed@android.com3a859a02009-01-28 00:56:29 +0000114 const SkIPoint dim = this->getSize();
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000115 SkRandom rand;
reed@android.com3a859a02009-01-28 00:56:29 +0000116
117 SkPaint paint(fPaint);
118 this->setupPaint(&paint);
reed@google.com095186a2011-10-18 12:21:50 +0000119 // explicitly need these
120 paint.setColor(fPaint.getColor());
121 paint.setAntiAlias(kBW != fFQ);
122 paint.setLCDRenderText(kLCD == fFQ);
reed@android.com3a859a02009-01-28 00:56:29 +0000123
joshualitt02b05012015-02-11 06:56:30 -0800124 if (fDoColorEmoji && fColorEmojiTypeface) {
125 paint.setTypeface(fColorEmojiTypeface);
126 }
127
reed@android.com3a859a02009-01-28 00:56:29 +0000128 const SkScalar x0 = SkIntToScalar(-10);
129 const SkScalar y0 = SkIntToScalar(-10);
130
reed@google.com055af882011-11-23 15:52:02 +0000131 if (fDoPos) {
132 // realistically, the matrix is often at least translated, so we
133 // do that since it exercises different code in drawPosText.
134 canvas->translate(SK_Scalar1, SK_Scalar1);
135 }
136
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000137 for (int i = 0; i < loops; i++) {
reed@google.com055af882011-11-23 15:52:02 +0000138 if (fDoPos) {
139 canvas->drawPosText(fText.c_str(), fText.size(), fPos, paint);
140 } else {
141 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
142 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
143 canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
144 }
reed@android.com3a859a02009-01-28 00:56:29 +0000145 }
146 }
147
148private:
tfarinaf168b862014-06-19 12:32:29 -0700149 typedef Benchmark INHERITED;
reed@android.com3a859a02009-01-28 00:56:29 +0000150};
151
152///////////////////////////////////////////////////////////////////////////////
153
154#define STR "Hamburgefons"
reed@android.com3a859a02009-01-28 00:56:29 +0000155
mtklein9c84b112015-08-26 13:41:31 -0700156DEF_BENCH( return new TextBench(STR, 16, 0xFFFFFFFF, kBW); )
mtklein@google.com410e6e82013-09-13 19:52:27 +0000157DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW); )
158DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kBW); )
159DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kBW); )
reed@android.comeca48362010-12-20 18:34:17 +0000160
mtklein9c84b112015-08-26 13:41:31 -0700161DEF_BENCH( return new TextBench(STR, 16, 0xFFFFFFFF, kAA); )
mtklein@google.com410e6e82013-09-13 19:52:27 +0000162DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kAA); )
163DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kAA); )
164DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kAA); )
reed@google.comb9d84f32011-01-17 19:45:43 +0000165
mtklein9c84b112015-08-26 13:41:31 -0700166DEF_BENCH( return new TextBench(STR, 16, 0xFFFFFFFF, kLCD); )
mtklein@google.com410e6e82013-09-13 19:52:27 +0000167DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kLCD); )
168DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kLCD); )
169DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kLCD); )
reed@google.comb9d84f32011-01-17 19:45:43 +0000170
mtklein9c84b112015-08-26 13:41:31 -0700171DEF_BENCH( return new TextBench(STR, 16, 0xFFFFFFFF, kBW, true); )
joshualitt02b05012015-02-11 06:56:30 -0800172DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW, true); )
173DEF_BENCH( return new TextBench(STR, 16, 0xFFFF0000, kBW, true); )
174DEF_BENCH( return new TextBench(STR, 16, 0x88FF0000, kBW, true); )
175
176DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kBW, true, true); )
177DEF_BENCH( return new TextBench(STR, 16, 0xFF000000, kAA, false, true); )