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