blob: b5fcacf8d87c5cc122788a6656206b9f062419e3 [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 */
tfarinaf168b862014-06-19 12:32:29 -07007#include "Benchmark.h"
reed@android.combd700c32009-01-05 03:34:50 +00008#include "SkCanvas.h"
mtklein@google.comc2897432013-09-10 19:23:38 +00009#include "SkCommandLineFlags.h"
reed@android.combd700c32009-01-05 03:34:50 +000010#include "SkPaint.h"
11#include "SkRandom.h"
reed@google.com981d4792011-03-09 12:55:47 +000012#include "SkShader.h"
mtklein@google.comc2897432013-09-10 19:23:38 +000013#include "SkString.h"
14
15DEFINE_double(strokeWidth, -1.0, "If set, use this stroke width in RectBench.");
reed@android.combd700c32009-01-05 03:34:50 +000016
tfarinaf168b862014-06-19 12:32:29 -070017class RectBench : public Benchmark {
reed@android.combd700c32009-01-05 03:34:50 +000018public:
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000019 int fShift, fStroke;
reed@android.combd700c32009-01-05 03:34:50 +000020 enum {
21 W = 640,
22 H = 480,
mtklein@google.comc2897432013-09-10 19:23:38 +000023 N = 300,
reed@android.combd700c32009-01-05 03:34:50 +000024 };
25 SkRect fRects[N];
26 SkColor fColors[N];
27
mtklein@google.com410e6e82013-09-13 19:52:27 +000028 RectBench(int shift, int stroke = 0)
29 : fShift(shift)
commit-bot@chromium.org000d7322013-12-05 12:07:44 +000030 , fStroke(stroke) {}
reed@google.com1fcd51e2011-01-05 15:50:27 +000031
reed@android.comd2c37442009-01-08 03:00:57 +000032 const char* computeName(const char root[]) {
Brian Salomon3349c6f2017-03-01 09:49:58 -050033 fBaseName.printf("%s_%d", root, fShift);
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000034 if (fStroke > 0) {
Brian Salomon3349c6f2017-03-01 09:49:58 -050035 fBaseName.appendf("_stroke_%d", fStroke);
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000036 }
Brian Salomon3349c6f2017-03-01 09:49:58 -050037 return fBaseName.c_str();
reed@android.comd2c37442009-01-08 03:00:57 +000038 }
reed@google.com1fcd51e2011-01-05 15:50:27 +000039
joshualitt7d4b4582015-09-24 08:08:23 -070040 bool isVisual() override { return true; }
41
reed@android.combd700c32009-01-05 03:34:50 +000042protected:
Brian Salomondac5f6b2017-02-28 16:11:04 -050043
reed@android.combd700c32009-01-05 03:34:50 +000044 virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) {
45 c->drawRect(r, p);
46 }
47
reed57b76712015-09-25 04:49:43 -070048 const char* onGetName() override { return computeName("rects"); }
commit-bot@chromium.org000d7322013-12-05 12:07:44 +000049
joshualitt8a6697a2015-09-30 12:11:07 -070050 void onDelayedSetup() override {
commit-bot@chromium.org000d7322013-12-05 12:07:44 +000051 SkRandom rand;
52 const SkScalar offset = SK_Scalar1/3;
53 for (int i = 0; i < N; i++) {
54 int x = rand.nextU() % W;
55 int y = rand.nextU() % H;
56 int w = rand.nextU() % W;
57 int h = rand.nextU() % H;
58 w >>= fShift;
59 h >>= fShift;
60 x -= w/2;
61 y -= h/2;
62 fRects[i].set(SkIntToScalar(x), SkIntToScalar(y),
63 SkIntToScalar(x+w), SkIntToScalar(y+h));
64 fRects[i].offset(offset, offset);
65 fColors[i] = rand.nextU() | 0xFF808080;
66 }
67 }
68
mtkleina1ebeb22015-10-01 09:43:39 -070069 void onDraw(int loops, SkCanvas* canvas) override {
reed@android.combd700c32009-01-05 03:34:50 +000070 SkPaint paint;
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000071 if (fStroke > 0) {
72 paint.setStyle(SkPaint::kStroke_Style);
73 paint.setStrokeWidth(SkIntToScalar(fStroke));
74 }
commit-bot@chromium.org33614712013-12-03 18:17:16 +000075 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +000076 paint.setColor(fColors[i % N]);
reed@android.com4bc19832009-01-19 20:08:35 +000077 this->setupPaint(&paint);
mtklein@google.comc2897432013-09-10 19:23:38 +000078 this->drawThisRect(canvas, fRects[i % N], paint);
reed@android.combd700c32009-01-05 03:34:50 +000079 }
80 }
Brian Salomon3349c6f2017-03-01 09:49:58 -050081
reed@android.come9d00602009-09-02 21:12:42 +000082private:
Brian Salomon3349c6f2017-03-01 09:49:58 -050083 SkString fBaseName;
tfarinaf168b862014-06-19 12:32:29 -070084 typedef Benchmark INHERITED;
reed@android.combd700c32009-01-05 03:34:50 +000085};
86
reed@google.com2d078b62012-11-15 19:52:07 +000087class SrcModeRectBench : public RectBench {
88public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000089 SrcModeRectBench() : INHERITED(1, 0) {
reed374772b2016-10-05 17:33:02 -070090 fMode = SkBlendMode::kSrc;
reed@google.com2d078b62012-11-15 19:52:07 +000091 }
92
93protected:
mtklein36352bf2015-03-25 18:17:31 -070094 void setupPaint(SkPaint* paint) override {
reed@google.com2d078b62012-11-15 19:52:07 +000095 this->INHERITED::setupPaint(paint);
96 // srcmode is most interesting when we're not opaque
97 paint->setAlpha(0x80);
reed374772b2016-10-05 17:33:02 -070098 paint->setBlendMode(fMode);
reed@google.com2d078b62012-11-15 19:52:07 +000099 }
100
mtklein36352bf2015-03-25 18:17:31 -0700101 const char* onGetName() override {
reed@google.com2d078b62012-11-15 19:52:07 +0000102 fName.set(this->INHERITED::onGetName());
103 fName.prepend("srcmode_");
104 return fName.c_str();
105 }
106
107private:
reed374772b2016-10-05 17:33:02 -0700108 SkBlendMode fMode;
Brian Salomon3349c6f2017-03-01 09:49:58 -0500109 SkString fName;
reed@google.com2d078b62012-11-15 19:52:07 +0000110
111 typedef RectBench INHERITED;
112};
113
joshualitt8db65a62015-09-24 12:42:02 -0700114class TransparentRectBench : public RectBench {
115public:
116 TransparentRectBench() : INHERITED(1, 0) {}
117
118protected:
119 void setupPaint(SkPaint* paint) override {
120 this->INHERITED::setupPaint(paint);
121 // draw non opaque rect
122 paint->setAlpha(0x80);
123 }
124
125 const char* onGetName() override {
126 fName.set(this->INHERITED::onGetName());
127 fName.prepend("transparent_");
128 return fName.c_str();
129 }
130
131private:
Brian Salomon3349c6f2017-03-01 09:49:58 -0500132 SkString fName;
joshualitt8db65a62015-09-24 12:42:02 -0700133 typedef RectBench INHERITED;
134};
135
136
reed@android.combd700c32009-01-05 03:34:50 +0000137class OvalBench : public RectBench {
reed@android.comd2c37442009-01-08 03:00:57 +0000138public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000139 OvalBench(int shift, int stroke = 0) : RectBench(shift, stroke) {}
reed@android.combd700c32009-01-05 03:34:50 +0000140protected:
reed57b76712015-09-25 04:49:43 -0700141 void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) override {
reed@android.combd700c32009-01-05 03:34:50 +0000142 c->drawOval(r, p);
143 }
reed57b76712015-09-25 04:49:43 -0700144 const char* onGetName() override { return computeName("ovals"); }
reed@android.combd700c32009-01-05 03:34:50 +0000145};
146
147class RRectBench : public RectBench {
reed@android.comd2c37442009-01-08 03:00:57 +0000148public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000149 RRectBench(int shift, int stroke = 0) : RectBench(shift, stroke) {}
reed@android.combd700c32009-01-05 03:34:50 +0000150protected:
reed57b76712015-09-25 04:49:43 -0700151 void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) override {
reed@android.combd700c32009-01-05 03:34:50 +0000152 c->drawRoundRect(r, r.width() / 4, r.height() / 4, p);
153 }
reed57b76712015-09-25 04:49:43 -0700154 const char* onGetName() override { return computeName("rrects"); }
reed@android.combd700c32009-01-05 03:34:50 +0000155};
156
157class PointsBench : public RectBench {
158public:
159 SkCanvas::PointMode fMode;
reed@android.combd700c32009-01-05 03:34:50 +0000160
mtklein@google.com410e6e82013-09-13 19:52:27 +0000161 PointsBench(SkCanvas::PointMode mode, const char* name)
162 : RectBench(2)
163 , fMode(mode) {
reed@android.combd700c32009-01-05 03:34:50 +0000164 fName = name;
165 }
166
167protected:
mtkleina1ebeb22015-10-01 09:43:39 -0700168 void onDraw(int loops, SkCanvas* canvas) override {
agl@chromium.org652807b2010-04-27 15:47:34 +0000169 SkScalar gSizes[] = {
reed@android.combd700c32009-01-05 03:34:50 +0000170 SkIntToScalar(7), 0
171 };
agl@chromium.org652807b2010-04-27 15:47:34 +0000172 size_t sizes = SK_ARRAY_COUNT(gSizes);
173
mtklein@google.comc2897432013-09-10 19:23:38 +0000174 if (FLAGS_strokeWidth >= 0) {
mtklein@google.comb87bf832013-09-10 19:51:10 +0000175 gSizes[0] = (SkScalar)FLAGS_strokeWidth;
agl@chromium.org652807b2010-04-27 15:47:34 +0000176 sizes = 1;
177 }
reed@android.combd700c32009-01-05 03:34:50 +0000178
179 SkPaint paint;
reed@android.combd700c32009-01-05 03:34:50 +0000180 paint.setStrokeCap(SkPaint::kRound_Cap);
agl@chromium.org652807b2010-04-27 15:47:34 +0000181
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000182 for (int loop = 0; loop < loops; loop++) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000183 for (size_t i = 0; i < sizes; i++) {
184 paint.setStrokeWidth(gSizes[i]);
185 this->setupPaint(&paint);
186 canvas->drawPoints(fMode, N * 2, SkTCast<SkPoint*>(fRects), paint);
187 paint.setColor(fColors[i % N]);
188 }
reed@android.combd700c32009-01-05 03:34:50 +0000189 }
190 }
Brian Salomondac5f6b2017-02-28 16:11:04 -0500191 const char* onGetName() override { return fName.c_str(); }
Brian Salomon3349c6f2017-03-01 09:49:58 -0500192
193private:
194 SkString fName;
195
reed@android.combd700c32009-01-05 03:34:50 +0000196};
197
reed@google.com981d4792011-03-09 12:55:47 +0000198/*******************************************************************************
199 * to bench BlitMask [Opaque, Black, color, shader]
200 *******************************************************************************/
201
202class BlitMaskBench : public RectBench {
203public:
204 enum kMaskType {
205 kMaskOpaque = 0,
206 kMaskBlack,
207 kMaskColor,
208 KMaskShader
209 };
210 SkCanvas::PointMode fMode;
reed@google.com981d4792011-03-09 12:55:47 +0000211
mtklein@google.com410e6e82013-09-13 19:52:27 +0000212 BlitMaskBench(SkCanvas::PointMode mode,
reed@google.com981d4792011-03-09 12:55:47 +0000213 BlitMaskBench::kMaskType type, const char* name) :
mtklein@google.com410e6e82013-09-13 19:52:27 +0000214 RectBench(2), fMode(mode), _type(type) {
reed@google.com981d4792011-03-09 12:55:47 +0000215 fName = name;
216 }
217
218protected:
mtkleina1ebeb22015-10-01 09:43:39 -0700219 void onDraw(int loops, SkCanvas* canvas) override {
reed@google.com981d4792011-03-09 12:55:47 +0000220 SkScalar gSizes[] = {
221 SkIntToScalar(13), SkIntToScalar(24)
222 };
223 size_t sizes = SK_ARRAY_COUNT(gSizes);
224
mtklein@google.comc2897432013-09-10 19:23:38 +0000225 if (FLAGS_strokeWidth >= 0) {
mtklein@google.comb87bf832013-09-10 19:51:10 +0000226 gSizes[0] = (SkScalar)FLAGS_strokeWidth;
reed@google.com981d4792011-03-09 12:55:47 +0000227 sizes = 1;
228 }
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000229 SkRandom rand;
reed@google.com981d4792011-03-09 12:55:47 +0000230 SkColor color = 0xFF000000;
231 U8CPU alpha = 0xFF;
232 SkPaint paint;
233 paint.setStrokeCap(SkPaint::kRound_Cap);
234 if (_type == KMaskShader) {
235 SkBitmap srcBM;
reed6c225732014-06-09 19:52:07 -0700236 srcBM.allocN32Pixels(10, 1);
reed@google.com981d4792011-03-09 12:55:47 +0000237 srcBM.eraseColor(0xFF00FF00);
238
reedc6f28f72016-03-14 12:22:10 -0700239 paint.setShader(SkShader::MakeBitmapShader(srcBM, SkShader::kClamp_TileMode,
240 SkShader::kClamp_TileMode));
reed@google.com981d4792011-03-09 12:55:47 +0000241 }
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000242 for (int loop = 0; loop < loops; loop++) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000243 for (size_t i = 0; i < sizes; i++) {
244 switch (_type) {
245 case kMaskOpaque:
246 color = fColors[i];
247 alpha = 0xFF;
248 break;
249 case kMaskBlack:
250 alpha = 0xFF;
251 color = 0xFF000000;
252 break;
253 case kMaskColor:
254 color = fColors[i];
255 alpha = rand.nextU() & 255;
256 break;
257 case KMaskShader:
258 break;
259 }
260 paint.setStrokeWidth(gSizes[i]);
261 this->setupPaint(&paint);
262 paint.setColor(color);
263 paint.setAlpha(alpha);
264 canvas->drawPoints(fMode, N * 2, SkTCast<SkPoint*>(fRects), paint);
265 }
266 }
reed@google.com981d4792011-03-09 12:55:47 +0000267 }
Brian Salomondac5f6b2017-02-28 16:11:04 -0500268 const char* onGetName() override { return fName.c_str(); }
Brian Salomon3349c6f2017-03-01 09:49:58 -0500269
reed@google.com981d4792011-03-09 12:55:47 +0000270private:
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000271 typedef RectBench INHERITED;
272 kMaskType _type;
Brian Salomon3349c6f2017-03-01 09:49:58 -0500273 SkString fName;
reed@google.com981d4792011-03-09 12:55:47 +0000274};
275
halcanary385fe4d2015-08-26 13:07:48 -0700276DEF_BENCH(return new RectBench(1);)
277DEF_BENCH(return new RectBench(1, 4);)
278DEF_BENCH(return new RectBench(3);)
279DEF_BENCH(return new RectBench(3, 4);)
280DEF_BENCH(return new OvalBench(1);)
281DEF_BENCH(return new OvalBench(3);)
282DEF_BENCH(return new OvalBench(1, 4);)
283DEF_BENCH(return new OvalBench(3, 4);)
284DEF_BENCH(return new RRectBench(1);)
285DEF_BENCH(return new RRectBench(1, 4);)
286DEF_BENCH(return new RRectBench(3);)
287DEF_BENCH(return new RRectBench(3, 4);)
288DEF_BENCH(return new PointsBench(SkCanvas::kPoints_PointMode, "points");)
289DEF_BENCH(return new PointsBench(SkCanvas::kLines_PointMode, "lines");)
290DEF_BENCH(return new PointsBench(SkCanvas::kPolygon_PointMode, "polygon");)
reed@google.com981d4792011-03-09 12:55:47 +0000291
halcanary385fe4d2015-08-26 13:07:48 -0700292DEF_BENCH(return new SrcModeRectBench();)
robertphillips@google.com370d8352013-05-02 13:57:52 +0000293
joshualitt8db65a62015-09-24 12:42:02 -0700294DEF_BENCH(return new TransparentRectBench();)
295
reed@google.com981d4792011-03-09 12:55:47 +0000296/* init the blitmask bench
297 */
halcanary385fe4d2015-08-26 13:07:48 -0700298DEF_BENCH(return new BlitMaskBench(SkCanvas::kPoints_PointMode,
299 BlitMaskBench::kMaskOpaque,
300 "maskopaque");)
301DEF_BENCH(return new BlitMaskBench(SkCanvas::kPoints_PointMode,
302 BlitMaskBench::kMaskBlack,
303 "maskblack");)
304DEF_BENCH(return new BlitMaskBench(SkCanvas::kPoints_PointMode,
305 BlitMaskBench::kMaskColor,
306 "maskcolor");)
307DEF_BENCH(return new BlitMaskBench(SkCanvas::kPoints_PointMode,
308 BlitMaskBench::KMaskShader,
309 "maskshader");)