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