blob: 386fb43dfb281a8591dfd55f66674bd0764eec66 [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 */
Mike Reed75ae4212018-01-23 11:24:08 -05007
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
Mike Reed75ae4212018-01-23 11:24:08 -05009#include "SkBitmap.h"
reed@android.combd700c32009-01-05 03:34:50 +000010#include "SkCanvas.h"
mtklein@google.comc2897432013-09-10 19:23:38 +000011#include "SkCommandLineFlags.h"
reed@android.combd700c32009-01-05 03:34:50 +000012#include "SkPaint.h"
13#include "SkRandom.h"
reed@google.com981d4792011-03-09 12:55:47 +000014#include "SkShader.h"
mtklein@google.comc2897432013-09-10 19:23:38 +000015#include "SkString.h"
16
17DEFINE_double(strokeWidth, -1.0, "If set, use this stroke width in RectBench.");
reed@android.combd700c32009-01-05 03:34:50 +000018
tfarinaf168b862014-06-19 12:32:29 -070019class RectBench : public Benchmark {
reed@android.combd700c32009-01-05 03:34:50 +000020public:
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000021 int fShift, fStroke;
reed@android.combd700c32009-01-05 03:34:50 +000022 enum {
23 W = 640,
24 H = 480,
mtklein@google.comc2897432013-09-10 19:23:38 +000025 N = 300,
reed@android.combd700c32009-01-05 03:34:50 +000026 };
27 SkRect fRects[N];
28 SkColor fColors[N];
29
mtklein@google.com410e6e82013-09-13 19:52:27 +000030 RectBench(int shift, int stroke = 0)
31 : fShift(shift)
commit-bot@chromium.org000d7322013-12-05 12:07:44 +000032 , fStroke(stroke) {}
reed@google.com1fcd51e2011-01-05 15:50:27 +000033
reed@android.comd2c37442009-01-08 03:00:57 +000034 const char* computeName(const char root[]) {
Brian Salomon3349c6f2017-03-01 09:49:58 -050035 fBaseName.printf("%s_%d", root, fShift);
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000036 if (fStroke > 0) {
Brian Salomon3349c6f2017-03-01 09:49:58 -050037 fBaseName.appendf("_stroke_%d", fStroke);
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000038 }
Brian Salomon3349c6f2017-03-01 09:49:58 -050039 return fBaseName.c_str();
reed@android.comd2c37442009-01-08 03:00:57 +000040 }
reed@google.com1fcd51e2011-01-05 15:50:27 +000041
joshualitt7d4b4582015-09-24 08:08:23 -070042 bool isVisual() override { return true; }
43
reed@android.combd700c32009-01-05 03:34:50 +000044protected:
Brian Salomondac5f6b2017-02-28 16:11:04 -050045
reed@android.combd700c32009-01-05 03:34:50 +000046 virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) {
47 c->drawRect(r, p);
48 }
49
reed57b76712015-09-25 04:49:43 -070050 const char* onGetName() override { return computeName("rects"); }
commit-bot@chromium.org000d7322013-12-05 12:07:44 +000051
joshualitt8a6697a2015-09-30 12:11:07 -070052 void onDelayedSetup() override {
commit-bot@chromium.org000d7322013-12-05 12:07:44 +000053 SkRandom rand;
54 const SkScalar offset = SK_Scalar1/3;
55 for (int i = 0; i < N; i++) {
56 int x = rand.nextU() % W;
57 int y = rand.nextU() % H;
58 int w = rand.nextU() % W;
59 int h = rand.nextU() % H;
60 w >>= fShift;
61 h >>= fShift;
62 x -= w/2;
63 y -= h/2;
64 fRects[i].set(SkIntToScalar(x), SkIntToScalar(y),
65 SkIntToScalar(x+w), SkIntToScalar(y+h));
66 fRects[i].offset(offset, offset);
67 fColors[i] = rand.nextU() | 0xFF808080;
68 }
69 }
70
mtkleina1ebeb22015-10-01 09:43:39 -070071 void onDraw(int loops, SkCanvas* canvas) override {
reed@android.combd700c32009-01-05 03:34:50 +000072 SkPaint paint;
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000073 if (fStroke > 0) {
74 paint.setStyle(SkPaint::kStroke_Style);
75 paint.setStrokeWidth(SkIntToScalar(fStroke));
76 }
commit-bot@chromium.org33614712013-12-03 18:17:16 +000077 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +000078 paint.setColor(fColors[i % N]);
reed@android.com4bc19832009-01-19 20:08:35 +000079 this->setupPaint(&paint);
mtklein@google.comc2897432013-09-10 19:23:38 +000080 this->drawThisRect(canvas, fRects[i % N], paint);
reed@android.combd700c32009-01-05 03:34:50 +000081 }
82 }
Brian Salomon3349c6f2017-03-01 09:49:58 -050083
reed@android.come9d00602009-09-02 21:12:42 +000084private:
Brian Salomon3349c6f2017-03-01 09:49:58 -050085 SkString fBaseName;
tfarinaf168b862014-06-19 12:32:29 -070086 typedef Benchmark INHERITED;
reed@android.combd700c32009-01-05 03:34:50 +000087};
88
reed@google.com2d078b62012-11-15 19:52:07 +000089class SrcModeRectBench : public RectBench {
90public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000091 SrcModeRectBench() : INHERITED(1, 0) {
reed374772b2016-10-05 17:33:02 -070092 fMode = SkBlendMode::kSrc;
reed@google.com2d078b62012-11-15 19:52:07 +000093 }
94
95protected:
mtklein36352bf2015-03-25 18:17:31 -070096 void setupPaint(SkPaint* paint) override {
reed@google.com2d078b62012-11-15 19:52:07 +000097 this->INHERITED::setupPaint(paint);
98 // srcmode is most interesting when we're not opaque
99 paint->setAlpha(0x80);
reed374772b2016-10-05 17:33:02 -0700100 paint->setBlendMode(fMode);
reed@google.com2d078b62012-11-15 19:52:07 +0000101 }
102
mtklein36352bf2015-03-25 18:17:31 -0700103 const char* onGetName() override {
reed@google.com2d078b62012-11-15 19:52:07 +0000104 fName.set(this->INHERITED::onGetName());
105 fName.prepend("srcmode_");
106 return fName.c_str();
107 }
108
109private:
reed374772b2016-10-05 17:33:02 -0700110 SkBlendMode fMode;
Brian Salomon3349c6f2017-03-01 09:49:58 -0500111 SkString fName;
reed@google.com2d078b62012-11-15 19:52:07 +0000112
113 typedef RectBench INHERITED;
114};
115
joshualitt8db65a62015-09-24 12:42:02 -0700116class TransparentRectBench : public RectBench {
117public:
118 TransparentRectBench() : INHERITED(1, 0) {}
119
120protected:
121 void setupPaint(SkPaint* paint) override {
122 this->INHERITED::setupPaint(paint);
123 // draw non opaque rect
124 paint->setAlpha(0x80);
125 }
126
127 const char* onGetName() override {
128 fName.set(this->INHERITED::onGetName());
129 fName.prepend("transparent_");
130 return fName.c_str();
131 }
132
133private:
Brian Salomon3349c6f2017-03-01 09:49:58 -0500134 SkString fName;
joshualitt8db65a62015-09-24 12:42:02 -0700135 typedef RectBench INHERITED;
136};
137
138
reed@android.combd700c32009-01-05 03:34:50 +0000139class OvalBench : public RectBench {
reed@android.comd2c37442009-01-08 03:00:57 +0000140public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000141 OvalBench(int shift, int stroke = 0) : RectBench(shift, stroke) {}
reed@android.combd700c32009-01-05 03:34:50 +0000142protected:
reed57b76712015-09-25 04:49:43 -0700143 void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) override {
reed@android.combd700c32009-01-05 03:34:50 +0000144 c->drawOval(r, p);
145 }
reed57b76712015-09-25 04:49:43 -0700146 const char* onGetName() override { return computeName("ovals"); }
reed@android.combd700c32009-01-05 03:34:50 +0000147};
148
149class RRectBench : public RectBench {
reed@android.comd2c37442009-01-08 03:00:57 +0000150public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000151 RRectBench(int shift, int stroke = 0) : RectBench(shift, stroke) {}
reed@android.combd700c32009-01-05 03:34:50 +0000152protected:
reed57b76712015-09-25 04:49:43 -0700153 void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) override {
reed@android.combd700c32009-01-05 03:34:50 +0000154 c->drawRoundRect(r, r.width() / 4, r.height() / 4, p);
155 }
reed57b76712015-09-25 04:49:43 -0700156 const char* onGetName() override { return computeName("rrects"); }
reed@android.combd700c32009-01-05 03:34:50 +0000157};
158
159class PointsBench : public RectBench {
160public:
161 SkCanvas::PointMode fMode;
reed@android.combd700c32009-01-05 03:34:50 +0000162
mtklein@google.com410e6e82013-09-13 19:52:27 +0000163 PointsBench(SkCanvas::PointMode mode, const char* name)
164 : RectBench(2)
165 , fMode(mode) {
reed@android.combd700c32009-01-05 03:34:50 +0000166 fName = name;
167 }
168
169protected:
mtkleina1ebeb22015-10-01 09:43:39 -0700170 void onDraw(int loops, SkCanvas* canvas) override {
agl@chromium.org652807b2010-04-27 15:47:34 +0000171 SkScalar gSizes[] = {
reed@android.combd700c32009-01-05 03:34:50 +0000172 SkIntToScalar(7), 0
173 };
agl@chromium.org652807b2010-04-27 15:47:34 +0000174 size_t sizes = SK_ARRAY_COUNT(gSizes);
175
mtklein@google.comc2897432013-09-10 19:23:38 +0000176 if (FLAGS_strokeWidth >= 0) {
mtklein@google.comb87bf832013-09-10 19:51:10 +0000177 gSizes[0] = (SkScalar)FLAGS_strokeWidth;
agl@chromium.org652807b2010-04-27 15:47:34 +0000178 sizes = 1;
179 }
reed@android.combd700c32009-01-05 03:34:50 +0000180
181 SkPaint paint;
reed@android.combd700c32009-01-05 03:34:50 +0000182 paint.setStrokeCap(SkPaint::kRound_Cap);
agl@chromium.org652807b2010-04-27 15:47:34 +0000183
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000184 for (int loop = 0; loop < loops; loop++) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000185 for (size_t i = 0; i < sizes; i++) {
186 paint.setStrokeWidth(gSizes[i]);
187 this->setupPaint(&paint);
188 canvas->drawPoints(fMode, N * 2, SkTCast<SkPoint*>(fRects), paint);
189 paint.setColor(fColors[i % N]);
190 }
reed@android.combd700c32009-01-05 03:34:50 +0000191 }
192 }
Brian Salomondac5f6b2017-02-28 16:11:04 -0500193 const char* onGetName() override { return fName.c_str(); }
Brian Salomon3349c6f2017-03-01 09:49:58 -0500194
195private:
196 SkString fName;
197
reed@android.combd700c32009-01-05 03:34:50 +0000198};
199
reed@google.com981d4792011-03-09 12:55:47 +0000200/*******************************************************************************
201 * to bench BlitMask [Opaque, Black, color, shader]
202 *******************************************************************************/
203
204class BlitMaskBench : public RectBench {
205public:
206 enum kMaskType {
207 kMaskOpaque = 0,
208 kMaskBlack,
209 kMaskColor,
210 KMaskShader
211 };
212 SkCanvas::PointMode fMode;
reed@google.com981d4792011-03-09 12:55:47 +0000213
mtklein@google.com410e6e82013-09-13 19:52:27 +0000214 BlitMaskBench(SkCanvas::PointMode mode,
reed@google.com981d4792011-03-09 12:55:47 +0000215 BlitMaskBench::kMaskType type, const char* name) :
mtklein@google.com410e6e82013-09-13 19:52:27 +0000216 RectBench(2), fMode(mode), _type(type) {
reed@google.com981d4792011-03-09 12:55:47 +0000217 fName = name;
218 }
219
220protected:
mtkleina1ebeb22015-10-01 09:43:39 -0700221 void onDraw(int loops, SkCanvas* canvas) override {
reed@google.com981d4792011-03-09 12:55:47 +0000222 SkScalar gSizes[] = {
223 SkIntToScalar(13), SkIntToScalar(24)
224 };
225 size_t sizes = SK_ARRAY_COUNT(gSizes);
226
mtklein@google.comc2897432013-09-10 19:23:38 +0000227 if (FLAGS_strokeWidth >= 0) {
mtklein@google.comb87bf832013-09-10 19:51:10 +0000228 gSizes[0] = (SkScalar)FLAGS_strokeWidth;
reed@google.com981d4792011-03-09 12:55:47 +0000229 sizes = 1;
230 }
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000231 SkRandom rand;
reed@google.com981d4792011-03-09 12:55:47 +0000232 SkColor color = 0xFF000000;
233 U8CPU alpha = 0xFF;
234 SkPaint paint;
235 paint.setStrokeCap(SkPaint::kRound_Cap);
236 if (_type == KMaskShader) {
237 SkBitmap srcBM;
reed6c225732014-06-09 19:52:07 -0700238 srcBM.allocN32Pixels(10, 1);
reed@google.com981d4792011-03-09 12:55:47 +0000239 srcBM.eraseColor(0xFF00FF00);
240
reedc6f28f72016-03-14 12:22:10 -0700241 paint.setShader(SkShader::MakeBitmapShader(srcBM, SkShader::kClamp_TileMode,
242 SkShader::kClamp_TileMode));
reed@google.com981d4792011-03-09 12:55:47 +0000243 }
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000244 for (int loop = 0; loop < loops; loop++) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000245 for (size_t i = 0; i < sizes; i++) {
246 switch (_type) {
247 case kMaskOpaque:
248 color = fColors[i];
249 alpha = 0xFF;
250 break;
251 case kMaskBlack:
252 alpha = 0xFF;
253 color = 0xFF000000;
254 break;
255 case kMaskColor:
256 color = fColors[i];
257 alpha = rand.nextU() & 255;
258 break;
259 case KMaskShader:
260 break;
261 }
262 paint.setStrokeWidth(gSizes[i]);
263 this->setupPaint(&paint);
264 paint.setColor(color);
265 paint.setAlpha(alpha);
266 canvas->drawPoints(fMode, N * 2, SkTCast<SkPoint*>(fRects), paint);
267 }
268 }
reed@google.com981d4792011-03-09 12:55:47 +0000269 }
Brian Salomondac5f6b2017-02-28 16:11:04 -0500270 const char* onGetName() override { return fName.c_str(); }
Brian Salomon3349c6f2017-03-01 09:49:58 -0500271
reed@google.com981d4792011-03-09 12:55:47 +0000272private:
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000273 typedef RectBench INHERITED;
274 kMaskType _type;
Brian Salomon3349c6f2017-03-01 09:49:58 -0500275 SkString fName;
reed@google.com981d4792011-03-09 12:55:47 +0000276};
277
halcanary385fe4d2015-08-26 13:07:48 -0700278DEF_BENCH(return new RectBench(1);)
279DEF_BENCH(return new RectBench(1, 4);)
280DEF_BENCH(return new RectBench(3);)
281DEF_BENCH(return new RectBench(3, 4);)
282DEF_BENCH(return new OvalBench(1);)
283DEF_BENCH(return new OvalBench(3);)
284DEF_BENCH(return new OvalBench(1, 4);)
285DEF_BENCH(return new OvalBench(3, 4);)
286DEF_BENCH(return new RRectBench(1);)
287DEF_BENCH(return new RRectBench(1, 4);)
288DEF_BENCH(return new RRectBench(3);)
289DEF_BENCH(return new RRectBench(3, 4);)
290DEF_BENCH(return new PointsBench(SkCanvas::kPoints_PointMode, "points");)
291DEF_BENCH(return new PointsBench(SkCanvas::kLines_PointMode, "lines");)
292DEF_BENCH(return new PointsBench(SkCanvas::kPolygon_PointMode, "polygon");)
reed@google.com981d4792011-03-09 12:55:47 +0000293
halcanary385fe4d2015-08-26 13:07:48 -0700294DEF_BENCH(return new SrcModeRectBench();)
robertphillips@google.com370d8352013-05-02 13:57:52 +0000295
joshualitt8db65a62015-09-24 12:42:02 -0700296DEF_BENCH(return new TransparentRectBench();)
297
reed@google.com981d4792011-03-09 12:55:47 +0000298/* init the blitmask bench
299 */
halcanary385fe4d2015-08-26 13:07:48 -0700300DEF_BENCH(return new BlitMaskBench(SkCanvas::kPoints_PointMode,
301 BlitMaskBench::kMaskOpaque,
302 "maskopaque");)
303DEF_BENCH(return new BlitMaskBench(SkCanvas::kPoints_PointMode,
304 BlitMaskBench::kMaskBlack,
305 "maskblack");)
306DEF_BENCH(return new BlitMaskBench(SkCanvas::kPoints_PointMode,
307 BlitMaskBench::kMaskColor,
308 "maskcolor");)
309DEF_BENCH(return new BlitMaskBench(SkCanvas::kPoints_PointMode,
310 BlitMaskBench::KMaskShader,
311 "maskshader");)