epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 7 | #include "Benchmark.h" |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 8 | #include "SkCanvas.h" |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 9 | #include "SkCommandLineFlags.h" |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 10 | #include "SkPaint.h" |
| 11 | #include "SkRandom.h" |
reed@google.com | 981d479 | 2011-03-09 12:55:47 +0000 | [diff] [blame] | 12 | #include "SkShader.h" |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 13 | #include "SkString.h" |
| 14 | |
| 15 | DEFINE_double(strokeWidth, -1.0, "If set, use this stroke width in RectBench."); |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 16 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 17 | class RectBench : public Benchmark { |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 18 | public: |
mike@reedtribe.org | 2ecead5 | 2011-04-02 13:37:23 +0000 | [diff] [blame] | 19 | int fShift, fStroke; |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 20 | enum { |
| 21 | W = 640, |
| 22 | H = 480, |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 23 | N = 300, |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 24 | }; |
| 25 | SkRect fRects[N]; |
| 26 | SkColor fColors[N]; |
| 27 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 28 | RectBench(int shift, int stroke = 0) |
| 29 | : fShift(shift) |
commit-bot@chromium.org | 000d732 | 2013-12-05 12:07:44 +0000 | [diff] [blame] | 30 | , fStroke(stroke) {} |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 31 | |
reed@android.com | d2c3744 | 2009-01-08 03:00:57 +0000 | [diff] [blame] | 32 | SkString fName; |
| 33 | const char* computeName(const char root[]) { |
mike@reedtribe.org | 2ecead5 | 2011-04-02 13:37:23 +0000 | [diff] [blame] | 34 | fName.printf("%s_%d", root, fShift); |
| 35 | if (fStroke > 0) { |
| 36 | fName.appendf("_stroke_%d", fStroke); |
| 37 | } |
reed@android.com | d2c3744 | 2009-01-08 03:00:57 +0000 | [diff] [blame] | 38 | return fName.c_str(); |
| 39 | } |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 40 | |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 41 | bool isVisual() override { return true; } |
| 42 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 43 | protected: |
| 44 | virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) { |
| 45 | c->drawRect(r, p); |
| 46 | } |
| 47 | |
reed | 57b7671 | 2015-09-25 04:49:43 -0700 | [diff] [blame] | 48 | const char* onGetName() override { return computeName("rects"); } |
commit-bot@chromium.org | 000d732 | 2013-12-05 12:07:44 +0000 | [diff] [blame] | 49 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 50 | void onDelayedSetup() override { |
commit-bot@chromium.org | 000d732 | 2013-12-05 12:07:44 +0000 | [diff] [blame] | 51 | 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 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 69 | void onDraw(int loops, SkCanvas* canvas) override { |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 70 | SkPaint paint; |
mike@reedtribe.org | 2ecead5 | 2011-04-02 13:37:23 +0000 | [diff] [blame] | 71 | if (fStroke > 0) { |
| 72 | paint.setStyle(SkPaint::kStroke_Style); |
| 73 | paint.setStrokeWidth(SkIntToScalar(fStroke)); |
| 74 | } |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 75 | for (int i = 0; i < loops; i++) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 76 | paint.setColor(fColors[i % N]); |
reed@android.com | 4bc1983 | 2009-01-19 20:08:35 +0000 | [diff] [blame] | 77 | this->setupPaint(&paint); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 78 | this->drawThisRect(canvas, fRects[i % N], paint); |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
reed@android.com | e9d0060 | 2009-09-02 21:12:42 +0000 | [diff] [blame] | 81 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 82 | typedef Benchmark INHERITED; |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
reed@google.com | 2d078b6 | 2012-11-15 19:52:07 +0000 | [diff] [blame] | 85 | class SrcModeRectBench : public RectBench { |
| 86 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 87 | SrcModeRectBench() : INHERITED(1, 0) { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 88 | fMode = SkBlendMode::kSrc; |
reed@google.com | 2d078b6 | 2012-11-15 19:52:07 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 92 | void setupPaint(SkPaint* paint) override { |
reed@google.com | 2d078b6 | 2012-11-15 19:52:07 +0000 | [diff] [blame] | 93 | this->INHERITED::setupPaint(paint); |
| 94 | // srcmode is most interesting when we're not opaque |
| 95 | paint->setAlpha(0x80); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 96 | paint->setBlendMode(fMode); |
reed@google.com | 2d078b6 | 2012-11-15 19:52:07 +0000 | [diff] [blame] | 97 | } |
| 98 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 99 | const char* onGetName() override { |
reed@google.com | 2d078b6 | 2012-11-15 19:52:07 +0000 | [diff] [blame] | 100 | fName.set(this->INHERITED::onGetName()); |
| 101 | fName.prepend("srcmode_"); |
| 102 | return fName.c_str(); |
| 103 | } |
| 104 | |
| 105 | private: |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 106 | SkString fName; |
| 107 | SkBlendMode fMode; |
reed@google.com | 2d078b6 | 2012-11-15 19:52:07 +0000 | [diff] [blame] | 108 | |
| 109 | typedef RectBench INHERITED; |
| 110 | }; |
| 111 | |
joshualitt | 8db65a6 | 2015-09-24 12:42:02 -0700 | [diff] [blame] | 112 | class TransparentRectBench : public RectBench { |
| 113 | public: |
| 114 | TransparentRectBench() : INHERITED(1, 0) {} |
| 115 | |
| 116 | protected: |
| 117 | void setupPaint(SkPaint* paint) override { |
| 118 | this->INHERITED::setupPaint(paint); |
| 119 | // draw non opaque rect |
| 120 | paint->setAlpha(0x80); |
| 121 | } |
| 122 | |
| 123 | const char* onGetName() override { |
| 124 | fName.set(this->INHERITED::onGetName()); |
| 125 | fName.prepend("transparent_"); |
| 126 | return fName.c_str(); |
| 127 | } |
| 128 | |
| 129 | private: |
| 130 | SkString fName; |
| 131 | |
| 132 | typedef RectBench INHERITED; |
| 133 | }; |
| 134 | |
| 135 | |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 136 | class OvalBench : public RectBench { |
reed@android.com | d2c3744 | 2009-01-08 03:00:57 +0000 | [diff] [blame] | 137 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 138 | OvalBench(int shift, int stroke = 0) : RectBench(shift, stroke) {} |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 139 | protected: |
reed | 57b7671 | 2015-09-25 04:49:43 -0700 | [diff] [blame] | 140 | void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) override { |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 141 | c->drawOval(r, p); |
| 142 | } |
reed | 57b7671 | 2015-09-25 04:49:43 -0700 | [diff] [blame] | 143 | const char* onGetName() override { return computeName("ovals"); } |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | class RRectBench : public RectBench { |
reed@android.com | d2c3744 | 2009-01-08 03:00:57 +0000 | [diff] [blame] | 147 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 148 | RRectBench(int shift, int stroke = 0) : RectBench(shift, stroke) {} |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 149 | protected: |
reed | 57b7671 | 2015-09-25 04:49:43 -0700 | [diff] [blame] | 150 | void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) override { |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 151 | c->drawRoundRect(r, r.width() / 4, r.height() / 4, p); |
| 152 | } |
reed | 57b7671 | 2015-09-25 04:49:43 -0700 | [diff] [blame] | 153 | const char* onGetName() override { return computeName("rrects"); } |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | class PointsBench : public RectBench { |
| 157 | public: |
| 158 | SkCanvas::PointMode fMode; |
| 159 | const char* fName; |
| 160 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 161 | PointsBench(SkCanvas::PointMode mode, const char* name) |
| 162 | : RectBench(2) |
| 163 | , fMode(mode) { |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 164 | fName = name; |
| 165 | } |
| 166 | |
| 167 | protected: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 168 | void onDraw(int loops, SkCanvas* canvas) override { |
agl@chromium.org | 652807b | 2010-04-27 15:47:34 +0000 | [diff] [blame] | 169 | SkScalar gSizes[] = { |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 170 | SkIntToScalar(7), 0 |
| 171 | }; |
agl@chromium.org | 652807b | 2010-04-27 15:47:34 +0000 | [diff] [blame] | 172 | size_t sizes = SK_ARRAY_COUNT(gSizes); |
| 173 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 174 | if (FLAGS_strokeWidth >= 0) { |
mtklein@google.com | b87bf83 | 2013-09-10 19:51:10 +0000 | [diff] [blame] | 175 | gSizes[0] = (SkScalar)FLAGS_strokeWidth; |
agl@chromium.org | 652807b | 2010-04-27 15:47:34 +0000 | [diff] [blame] | 176 | sizes = 1; |
| 177 | } |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 178 | |
| 179 | SkPaint paint; |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 180 | paint.setStrokeCap(SkPaint::kRound_Cap); |
agl@chromium.org | 652807b | 2010-04-27 15:47:34 +0000 | [diff] [blame] | 181 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 182 | for (int loop = 0; loop < loops; loop++) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 183 | 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.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
reed | 57b7671 | 2015-09-25 04:49:43 -0700 | [diff] [blame] | 191 | const char* onGetName() override { return fName; } |
reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 192 | }; |
| 193 | |
reed@google.com | 981d479 | 2011-03-09 12:55:47 +0000 | [diff] [blame] | 194 | /******************************************************************************* |
| 195 | * to bench BlitMask [Opaque, Black, color, shader] |
| 196 | *******************************************************************************/ |
| 197 | |
| 198 | class BlitMaskBench : public RectBench { |
| 199 | public: |
| 200 | enum kMaskType { |
| 201 | kMaskOpaque = 0, |
| 202 | kMaskBlack, |
| 203 | kMaskColor, |
| 204 | KMaskShader |
| 205 | }; |
| 206 | SkCanvas::PointMode fMode; |
| 207 | const char* fName; |
| 208 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 209 | BlitMaskBench(SkCanvas::PointMode mode, |
reed@google.com | 981d479 | 2011-03-09 12:55:47 +0000 | [diff] [blame] | 210 | BlitMaskBench::kMaskType type, const char* name) : |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 211 | RectBench(2), fMode(mode), _type(type) { |
reed@google.com | 981d479 | 2011-03-09 12:55:47 +0000 | [diff] [blame] | 212 | fName = name; |
| 213 | } |
| 214 | |
| 215 | protected: |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 216 | void onDraw(int loops, SkCanvas* canvas) override { |
reed@google.com | 981d479 | 2011-03-09 12:55:47 +0000 | [diff] [blame] | 217 | SkScalar gSizes[] = { |
| 218 | SkIntToScalar(13), SkIntToScalar(24) |
| 219 | }; |
| 220 | size_t sizes = SK_ARRAY_COUNT(gSizes); |
| 221 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 222 | if (FLAGS_strokeWidth >= 0) { |
mtklein@google.com | b87bf83 | 2013-09-10 19:51:10 +0000 | [diff] [blame] | 223 | gSizes[0] = (SkScalar)FLAGS_strokeWidth; |
reed@google.com | 981d479 | 2011-03-09 12:55:47 +0000 | [diff] [blame] | 224 | sizes = 1; |
| 225 | } |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 226 | SkRandom rand; |
reed@google.com | 981d479 | 2011-03-09 12:55:47 +0000 | [diff] [blame] | 227 | SkColor color = 0xFF000000; |
| 228 | U8CPU alpha = 0xFF; |
| 229 | SkPaint paint; |
| 230 | paint.setStrokeCap(SkPaint::kRound_Cap); |
| 231 | if (_type == KMaskShader) { |
| 232 | SkBitmap srcBM; |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 233 | srcBM.allocN32Pixels(10, 1); |
reed@google.com | 981d479 | 2011-03-09 12:55:47 +0000 | [diff] [blame] | 234 | srcBM.eraseColor(0xFF00FF00); |
| 235 | |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 236 | paint.setShader(SkShader::MakeBitmapShader(srcBM, SkShader::kClamp_TileMode, |
| 237 | SkShader::kClamp_TileMode)); |
reed@google.com | 981d479 | 2011-03-09 12:55:47 +0000 | [diff] [blame] | 238 | } |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 239 | for (int loop = 0; loop < loops; loop++) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 240 | for (size_t i = 0; i < sizes; i++) { |
| 241 | switch (_type) { |
| 242 | case kMaskOpaque: |
| 243 | color = fColors[i]; |
| 244 | alpha = 0xFF; |
| 245 | break; |
| 246 | case kMaskBlack: |
| 247 | alpha = 0xFF; |
| 248 | color = 0xFF000000; |
| 249 | break; |
| 250 | case kMaskColor: |
| 251 | color = fColors[i]; |
| 252 | alpha = rand.nextU() & 255; |
| 253 | break; |
| 254 | case KMaskShader: |
| 255 | break; |
| 256 | } |
| 257 | paint.setStrokeWidth(gSizes[i]); |
| 258 | this->setupPaint(&paint); |
| 259 | paint.setColor(color); |
| 260 | paint.setAlpha(alpha); |
| 261 | canvas->drawPoints(fMode, N * 2, SkTCast<SkPoint*>(fRects), paint); |
| 262 | } |
| 263 | } |
reed@google.com | 981d479 | 2011-03-09 12:55:47 +0000 | [diff] [blame] | 264 | } |
reed | 57b7671 | 2015-09-25 04:49:43 -0700 | [diff] [blame] | 265 | const char* onGetName() override { return fName; } |
reed@google.com | 981d479 | 2011-03-09 12:55:47 +0000 | [diff] [blame] | 266 | private: |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 267 | typedef RectBench INHERITED; |
| 268 | kMaskType _type; |
reed@google.com | 981d479 | 2011-03-09 12:55:47 +0000 | [diff] [blame] | 269 | }; |
| 270 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 271 | DEF_BENCH(return new RectBench(1);) |
| 272 | DEF_BENCH(return new RectBench(1, 4);) |
| 273 | DEF_BENCH(return new RectBench(3);) |
| 274 | DEF_BENCH(return new RectBench(3, 4);) |
| 275 | DEF_BENCH(return new OvalBench(1);) |
| 276 | DEF_BENCH(return new OvalBench(3);) |
| 277 | DEF_BENCH(return new OvalBench(1, 4);) |
| 278 | DEF_BENCH(return new OvalBench(3, 4);) |
| 279 | DEF_BENCH(return new RRectBench(1);) |
| 280 | DEF_BENCH(return new RRectBench(1, 4);) |
| 281 | DEF_BENCH(return new RRectBench(3);) |
| 282 | DEF_BENCH(return new RRectBench(3, 4);) |
| 283 | DEF_BENCH(return new PointsBench(SkCanvas::kPoints_PointMode, "points");) |
| 284 | DEF_BENCH(return new PointsBench(SkCanvas::kLines_PointMode, "lines");) |
| 285 | DEF_BENCH(return new PointsBench(SkCanvas::kPolygon_PointMode, "polygon");) |
reed@google.com | 981d479 | 2011-03-09 12:55:47 +0000 | [diff] [blame] | 286 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 287 | DEF_BENCH(return new SrcModeRectBench();) |
robertphillips@google.com | 370d835 | 2013-05-02 13:57:52 +0000 | [diff] [blame] | 288 | |
joshualitt | 8db65a6 | 2015-09-24 12:42:02 -0700 | [diff] [blame] | 289 | DEF_BENCH(return new TransparentRectBench();) |
| 290 | |
reed@google.com | 981d479 | 2011-03-09 12:55:47 +0000 | [diff] [blame] | 291 | /* init the blitmask bench |
| 292 | */ |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 293 | DEF_BENCH(return new BlitMaskBench(SkCanvas::kPoints_PointMode, |
| 294 | BlitMaskBench::kMaskOpaque, |
| 295 | "maskopaque");) |
| 296 | DEF_BENCH(return new BlitMaskBench(SkCanvas::kPoints_PointMode, |
| 297 | BlitMaskBench::kMaskBlack, |
| 298 | "maskblack");) |
| 299 | DEF_BENCH(return new BlitMaskBench(SkCanvas::kPoints_PointMode, |
| 300 | BlitMaskBench::kMaskColor, |
| 301 | "maskcolor");) |
| 302 | DEF_BENCH(return new BlitMaskBench(SkCanvas::kPoints_PointMode, |
| 303 | BlitMaskBench::KMaskShader, |
| 304 | "maskshader");) |