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