blob: 99ff0a94f1befdb953a70c9d2b32a002ca40ac64 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.combd700c32009-01-05 03:34:50 +00008#include "SkBenchmark.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkRandom.h"
reed@android.comd2c37442009-01-08 03:00:57 +000012#include "SkString.h"
reed@google.com981d4792011-03-09 12:55:47 +000013#include "SkShader.h"
reed@android.combd700c32009-01-05 03:34:50 +000014
15class RectBench : public SkBenchmark {
16public:
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000017 int fShift, fStroke;
reed@android.combd700c32009-01-05 03:34:50 +000018 enum {
19 W = 640,
20 H = 480,
tomhudson@google.comca529d32011-10-28 15:34:49 +000021 N = SkBENCHLOOP(300)
reed@android.combd700c32009-01-05 03:34:50 +000022 };
23 SkRect fRects[N];
24 SkColor fColors[N];
25
skia.committer@gmail.coma7aedfe2012-12-15 02:03:10 +000026 RectBench(void* param, int shift, int stroke = 0)
robertphillips@google.com12310e42012-12-14 22:10:30 +000027 : INHERITED(param)
28 , fShift(shift)
29 , fStroke(stroke) {
reed@android.combd700c32009-01-05 03:34:50 +000030 SkRandom rand;
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000031 const SkScalar offset = SK_Scalar1/3;
reed@android.combd700c32009-01-05 03:34:50 +000032 for (int i = 0; i < N; i++) {
33 int x = rand.nextU() % W;
34 int y = rand.nextU() % H;
35 int w = rand.nextU() % W;
36 int h = rand.nextU() % H;
reed@android.comd2c37442009-01-08 03:00:57 +000037 w >>= shift;
38 h >>= shift;
reed@android.combd700c32009-01-05 03:34:50 +000039 x -= w/2;
40 y -= h/2;
41 fRects[i].set(SkIntToScalar(x), SkIntToScalar(y),
42 SkIntToScalar(x+w), SkIntToScalar(y+h));
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000043 fRects[i].offset(offset, offset);
reed@android.combd700c32009-01-05 03:34:50 +000044 fColors[i] = rand.nextU() | 0xFF808080;
45 }
46 }
reed@google.com1fcd51e2011-01-05 15:50:27 +000047
reed@android.comd2c37442009-01-08 03:00:57 +000048 SkString fName;
49 const char* computeName(const char root[]) {
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000050 fName.printf("%s_%d", root, fShift);
51 if (fStroke > 0) {
52 fName.appendf("_stroke_%d", fStroke);
53 }
reed@android.comd2c37442009-01-08 03:00:57 +000054 return fName.c_str();
55 }
reed@google.com1fcd51e2011-01-05 15:50:27 +000056
reed@android.combd700c32009-01-05 03:34:50 +000057protected:
58 virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) {
59 c->drawRect(r, p);
60 }
61
reed@android.com4bc19832009-01-19 20:08:35 +000062 virtual const char* onGetName() { return computeName("rects"); }
reed@android.combd700c32009-01-05 03:34:50 +000063 virtual void onDraw(SkCanvas* canvas) {
64 SkPaint paint;
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000065 if (fStroke > 0) {
66 paint.setStyle(SkPaint::kStroke_Style);
67 paint.setStrokeWidth(SkIntToScalar(fStroke));
68 }
reed@android.combd700c32009-01-05 03:34:50 +000069 for (int i = 0; i < N; i++) {
70 paint.setColor(fColors[i]);
reed@android.com4bc19832009-01-19 20:08:35 +000071 this->setupPaint(&paint);
reed@android.combd700c32009-01-05 03:34:50 +000072 this->drawThisRect(canvas, fRects[i], paint);
73 }
74 }
reed@android.come9d00602009-09-02 21:12:42 +000075private:
76 typedef SkBenchmark INHERITED;
reed@android.combd700c32009-01-05 03:34:50 +000077};
78
reed@google.com2d078b62012-11-15 19:52:07 +000079class SrcModeRectBench : public RectBench {
80public:
81 SrcModeRectBench(void* param) : INHERITED(param, 1, 0) {
82 fMode = SkXfermode::Create(SkXfermode::kSrc_Mode);
83 }
skia.committer@gmail.com884e60b2012-11-16 02:01:17 +000084
reed@google.com2d078b62012-11-15 19:52:07 +000085 virtual ~SrcModeRectBench() {
86 SkSafeUnref(fMode);
87 }
88
89protected:
90 virtual void setupPaint(SkPaint* paint) SK_OVERRIDE {
91 this->INHERITED::setupPaint(paint);
92 // srcmode is most interesting when we're not opaque
93 paint->setAlpha(0x80);
94 paint->setXfermode(fMode);
95 }
96
97 virtual const char* onGetName() SK_OVERRIDE {
98 fName.set(this->INHERITED::onGetName());
99 fName.prepend("srcmode_");
100 return fName.c_str();
101 }
102
103private:
104 SkString fName;
105 SkXfermode* fMode;
106
107 typedef RectBench INHERITED;
108};
109
reed@android.combd700c32009-01-05 03:34:50 +0000110class OvalBench : public RectBench {
reed@android.comd2c37442009-01-08 03:00:57 +0000111public:
jvanverth@google.comce0ce4f2013-05-07 13:03:21 +0000112 OvalBench(void* param, int shift, int stroke = 0) : RectBench(param, shift, stroke) {}
reed@android.combd700c32009-01-05 03:34:50 +0000113protected:
114 virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) {
115 c->drawOval(r, p);
116 }
reed@android.comd2c37442009-01-08 03:00:57 +0000117 virtual const char* onGetName() { return computeName("ovals"); }
reed@android.combd700c32009-01-05 03:34:50 +0000118};
119
120class RRectBench : public RectBench {
reed@android.comd2c37442009-01-08 03:00:57 +0000121public:
jvanverth@google.com190260e2013-04-23 19:14:03 +0000122 RRectBench(void* param, int shift, int stroke = 0) : RectBench(param, shift, stroke) {}
reed@android.combd700c32009-01-05 03:34:50 +0000123protected:
124 virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) {
125 c->drawRoundRect(r, r.width() / 4, r.height() / 4, p);
126 }
reed@android.com4bc19832009-01-19 20:08:35 +0000127 virtual const char* onGetName() { return computeName("rrects"); }
reed@android.combd700c32009-01-05 03:34:50 +0000128};
129
130class PointsBench : public RectBench {
131public:
132 SkCanvas::PointMode fMode;
133 const char* fName;
134
reed@google.com1fcd51e2011-01-05 15:50:27 +0000135 PointsBench(void* param, SkCanvas::PointMode mode, const char* name) :
reed@android.come9d00602009-09-02 21:12:42 +0000136 RectBench(param, 2), fMode(mode) {
reed@android.combd700c32009-01-05 03:34:50 +0000137 fName = name;
138 }
139
140protected:
141 virtual void onDraw(SkCanvas* canvas) {
agl@chromium.org652807b2010-04-27 15:47:34 +0000142 SkScalar gSizes[] = {
reed@android.combd700c32009-01-05 03:34:50 +0000143 SkIntToScalar(7), 0
144 };
agl@chromium.org652807b2010-04-27 15:47:34 +0000145 size_t sizes = SK_ARRAY_COUNT(gSizes);
146
147 if (this->hasStrokeWidth()) {
148 gSizes[0] = this->getStrokeWidth();
149 sizes = 1;
150 }
reed@android.combd700c32009-01-05 03:34:50 +0000151
152 SkPaint paint;
reed@android.combd700c32009-01-05 03:34:50 +0000153 paint.setStrokeCap(SkPaint::kRound_Cap);
agl@chromium.org652807b2010-04-27 15:47:34 +0000154
155 for (size_t i = 0; i < sizes; i++) {
reed@android.combd700c32009-01-05 03:34:50 +0000156 paint.setStrokeWidth(gSizes[i]);
reed@android.com4bc19832009-01-19 20:08:35 +0000157 this->setupPaint(&paint);
reed@google.com1fcd51e2011-01-05 15:50:27 +0000158 canvas->drawPoints(fMode, N * 2, SkTCast<SkPoint*>(fRects), paint);
reed@android.combd700c32009-01-05 03:34:50 +0000159 paint.setColor(fColors[i]);
160 }
161 }
162 virtual const char* onGetName() { return fName; }
163};
164
robertphillips@google.com12310e42012-12-14 22:10:30 +0000165class AARectBench : public SkBenchmark {
166public:
167 enum {
168 W = 640,
169 H = 480,
170 };
171
robertphillips@google.com370d8352013-05-02 13:57:52 +0000172 AARectBench(void* param, bool rotate) : INHERITED(param), fRotate(rotate) {}
robertphillips@google.com12310e42012-12-14 22:10:30 +0000173
174protected:
175
skia.committer@gmail.com2fd42c42013-05-03 07:01:00 +0000176 virtual const char* onGetName() {
robertphillips@google.com370d8352013-05-02 13:57:52 +0000177 if (fRotate) {
skia.committer@gmail.com2fd42c42013-05-03 07:01:00 +0000178 return "aarects_rotated";
robertphillips@google.com370d8352013-05-02 13:57:52 +0000179 }
skia.committer@gmail.com2fd42c42013-05-03 07:01:00 +0000180 return "aarects";
robertphillips@google.com370d8352013-05-02 13:57:52 +0000181 }
robertphillips@google.com12310e42012-12-14 22:10:30 +0000182
183 virtual void onDraw(SkCanvas* canvas) {
robertphillips@google.com370d8352013-05-02 13:57:52 +0000184 static const SkScalar kHalfRectSize = SkFloatToScalar(0.75f);
185
robertphillips@google.com12310e42012-12-14 22:10:30 +0000186 SkPaint paint;
187 this->setupPaint(&paint);
188 paint.setAntiAlias(true);
189 paint.setColor(SK_ColorBLACK);
robertphillips@google.com370d8352013-05-02 13:57:52 +0000190 SkRect r = { -kHalfRectSize, -kHalfRectSize, kHalfRectSize, kHalfRectSize };
191 int rot = 0;
robertphillips@google.com12310e42012-12-14 22:10:30 +0000192
193 // Draw small aa rects in a grid across the screen
robertphillips@google.com370d8352013-05-02 13:57:52 +0000194 for (SkScalar y = kHalfRectSize+SK_Scalar1; y < H; y += 2*kHalfRectSize+2) {
195 for (SkScalar x = kHalfRectSize+SK_Scalar1; x < W; x += 2*kHalfRectSize+2) {
196 canvas->save();
197 canvas->translate(x, y);
198
199 if (fRotate) {
200 SkMatrix rotate;
201 rotate.setRotate(SkIntToScalar(rot));
202 canvas->concat(rotate);
203 rot += 10;
204 }
205
robertphillips@google.com12310e42012-12-14 22:10:30 +0000206 canvas->drawRect(r, paint);
robertphillips@google.com370d8352013-05-02 13:57:52 +0000207 canvas->restore();
robertphillips@google.com12310e42012-12-14 22:10:30 +0000208 }
209 }
210
211 }
212private:
robertphillips@google.com370d8352013-05-02 13:57:52 +0000213 bool fRotate;
robertphillips@google.com12310e42012-12-14 22:10:30 +0000214 typedef SkBenchmark INHERITED;
215};
216
reed@google.com981d4792011-03-09 12:55:47 +0000217/*******************************************************************************
218 * to bench BlitMask [Opaque, Black, color, shader]
219 *******************************************************************************/
220
221class BlitMaskBench : public RectBench {
222public:
223 enum kMaskType {
224 kMaskOpaque = 0,
225 kMaskBlack,
226 kMaskColor,
227 KMaskShader
228 };
229 SkCanvas::PointMode fMode;
230 const char* fName;
231
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000232 BlitMaskBench(void* param, SkCanvas::PointMode mode,
reed@google.com981d4792011-03-09 12:55:47 +0000233 BlitMaskBench::kMaskType type, const char* name) :
234 RectBench(param, 2), fMode(mode), _type(type) {
235 fName = name;
236 }
237
238protected:
239 virtual void onDraw(SkCanvas* canvas) {
240 SkScalar gSizes[] = {
241 SkIntToScalar(13), SkIntToScalar(24)
242 };
243 size_t sizes = SK_ARRAY_COUNT(gSizes);
244
245 if (this->hasStrokeWidth()) {
246 gSizes[0] = this->getStrokeWidth();
247 sizes = 1;
248 }
249 SkRandom rand;
250 SkColor color = 0xFF000000;
251 U8CPU alpha = 0xFF;
252 SkPaint paint;
253 paint.setStrokeCap(SkPaint::kRound_Cap);
254 if (_type == KMaskShader) {
255 SkBitmap srcBM;
256 srcBM.setConfig(SkBitmap::kARGB_8888_Config, 10, 1);
257 srcBM.allocPixels();
258 srcBM.eraseColor(0xFF00FF00);
259
260 SkShader* s;
261 s = SkShader::CreateBitmapShader(srcBM, SkShader::kClamp_TileMode,
262 SkShader::kClamp_TileMode);
263 paint.setShader(s)->unref();
264 }
265 for (size_t i = 0; i < sizes; i++) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000266 switch (_type) {
267 case kMaskOpaque:
268 color = fColors[i];
269 alpha = 0xFF;
reed@google.com981d4792011-03-09 12:55:47 +0000270 break;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000271 case kMaskBlack:
reed@google.com981d4792011-03-09 12:55:47 +0000272 alpha = 0xFF;
273 color = 0xFF000000;
274 break;
275 case kMaskColor:
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000276 color = fColors[i];
reed@google.com981d4792011-03-09 12:55:47 +0000277 alpha = rand.nextU() & 255;
278 break;
279 case KMaskShader:
280 break;
281 }
282 paint.setStrokeWidth(gSizes[i]);
283 this->setupPaint(&paint);
284 paint.setColor(color);
285 paint.setAlpha(alpha);
286 canvas->drawPoints(fMode, N * 2, SkTCast<SkPoint*>(fRects), paint);
287 }
288 }
289 virtual const char* onGetName() { return fName; }
290private:
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000291 typedef RectBench INHERITED;
292 kMaskType _type;
reed@google.com981d4792011-03-09 12:55:47 +0000293};
294
295
reed@google.com60712f32012-11-15 18:06:00 +0000296DEF_BENCH( return SkNEW_ARGS(RectBench, (p, 1)); )
297DEF_BENCH( return SkNEW_ARGS(RectBench, (p, 1, 4)); )
298DEF_BENCH( return SkNEW_ARGS(RectBench, (p, 3)); )
299DEF_BENCH( return SkNEW_ARGS(RectBench, (p, 3, 4)); )
300DEF_BENCH( return SkNEW_ARGS(OvalBench, (p, 1)); )
301DEF_BENCH( return SkNEW_ARGS(OvalBench, (p, 3)); )
jvanverth@google.comce0ce4f2013-05-07 13:03:21 +0000302DEF_BENCH( return SkNEW_ARGS(OvalBench, (p, 1, 4)); )
303DEF_BENCH( return SkNEW_ARGS(OvalBench, (p, 3, 4)); )
reed@google.com60712f32012-11-15 18:06:00 +0000304DEF_BENCH( return SkNEW_ARGS(RRectBench, (p, 1)); )
jvanverth@google.com190260e2013-04-23 19:14:03 +0000305DEF_BENCH( return SkNEW_ARGS(RRectBench, (p, 1, 4)); )
reed@google.com60712f32012-11-15 18:06:00 +0000306DEF_BENCH( return SkNEW_ARGS(RRectBench, (p, 3)); )
jvanverth@google.com190260e2013-04-23 19:14:03 +0000307DEF_BENCH( return SkNEW_ARGS(RRectBench, (p, 3, 4)); )
reed@google.com60712f32012-11-15 18:06:00 +0000308DEF_BENCH( return SkNEW_ARGS(PointsBench, (p, SkCanvas::kPoints_PointMode, "points")); )
309DEF_BENCH( return SkNEW_ARGS(PointsBench, (p, SkCanvas::kLines_PointMode, "lines")); )
310DEF_BENCH( return SkNEW_ARGS(PointsBench, (p, SkCanvas::kPolygon_PointMode, "polygon")); )
reed@android.combd700c32009-01-05 03:34:50 +0000311
reed@google.com2d078b62012-11-15 19:52:07 +0000312DEF_BENCH( return SkNEW_ARGS(SrcModeRectBench, (p)); )
robertphillips@google.com370d8352013-05-02 13:57:52 +0000313
314DEF_BENCH( return SkNEW_ARGS(AARectBench, (p, false)); )
315DEF_BENCH( return SkNEW_ARGS(AARectBench, (p, true)); )
reed@google.com2d078b62012-11-15 19:52:07 +0000316
reed@google.com981d4792011-03-09 12:55:47 +0000317/* init the blitmask bench
318 */
reed@google.com60712f32012-11-15 18:06:00 +0000319DEF_BENCH( return SkNEW_ARGS(BlitMaskBench,
reed@google.com981d4792011-03-09 12:55:47 +0000320 (p, SkCanvas::kPoints_PointMode,
321 BlitMaskBench::kMaskOpaque, "maskopaque")
reed@google.com60712f32012-11-15 18:06:00 +0000322 ); )
323DEF_BENCH( return SkNEW_ARGS(BlitMaskBench,
reed@google.com981d4792011-03-09 12:55:47 +0000324 (p, SkCanvas::kPoints_PointMode,
325 BlitMaskBench::kMaskBlack, "maskblack")
reed@google.com60712f32012-11-15 18:06:00 +0000326 ); )
327DEF_BENCH( return SkNEW_ARGS(BlitMaskBench,
reed@google.com981d4792011-03-09 12:55:47 +0000328 (p, SkCanvas::kPoints_PointMode,
329 BlitMaskBench::kMaskColor, "maskcolor")
reed@google.com60712f32012-11-15 18:06:00 +0000330 ); )
331DEF_BENCH( return SkNEW_ARGS(BlitMaskBench,
reed@google.com981d4792011-03-09 12:55:47 +0000332 (p, SkCanvas::kPoints_PointMode,
333 BlitMaskBench::KMaskShader, "maskshader")
reed@google.com60712f32012-11-15 18:06:00 +0000334 ); )