blob: fb54640c064dd2fac5f9ac20ce30b03d2b4a412f [file] [log] [blame]
reed@android.combd700c32009-01-05 03:34:50 +00001#include "SkBenchmark.h"
2#include "SkCanvas.h"
3#include "SkPaint.h"
4#include "SkRandom.h"
reed@android.comd2c37442009-01-08 03:00:57 +00005#include "SkString.h"
reed@google.com981d4792011-03-09 12:55:47 +00006#include "SkShader.h"
reed@android.combd700c32009-01-05 03:34:50 +00007
8class RectBench : public SkBenchmark {
9public:
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000010 int fShift, fStroke;
reed@android.combd700c32009-01-05 03:34:50 +000011 enum {
12 W = 640,
13 H = 480,
reed@android.comd2c37442009-01-08 03:00:57 +000014 N = 300
reed@android.combd700c32009-01-05 03:34:50 +000015 };
16 SkRect fRects[N];
17 SkColor fColors[N];
18
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000019 RectBench(void* param, int shift, int stroke = 0) : INHERITED(param), fShift(shift), fStroke(stroke) {
reed@android.combd700c32009-01-05 03:34:50 +000020 SkRandom rand;
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000021 const SkScalar offset = SK_Scalar1/3;
reed@android.combd700c32009-01-05 03:34:50 +000022 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.comd2c37442009-01-08 03:00:57 +000027 w >>= shift;
28 h >>= shift;
reed@android.combd700c32009-01-05 03:34:50 +000029 x -= w/2;
30 y -= h/2;
31 fRects[i].set(SkIntToScalar(x), SkIntToScalar(y),
32 SkIntToScalar(x+w), SkIntToScalar(y+h));
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000033 fRects[i].offset(offset, offset);
reed@android.combd700c32009-01-05 03:34:50 +000034 fColors[i] = rand.nextU() | 0xFF808080;
35 }
36 }
reed@google.com1fcd51e2011-01-05 15:50:27 +000037
reed@android.comd2c37442009-01-08 03:00:57 +000038 SkString fName;
39 const char* computeName(const char root[]) {
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000040 fName.printf("%s_%d", root, fShift);
41 if (fStroke > 0) {
42 fName.appendf("_stroke_%d", fStroke);
43 }
reed@android.comd2c37442009-01-08 03:00:57 +000044 return fName.c_str();
45 }
reed@google.com1fcd51e2011-01-05 15:50:27 +000046
reed@android.combd700c32009-01-05 03:34:50 +000047protected:
48 virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) {
49 c->drawRect(r, p);
50 }
51
reed@android.com4bc19832009-01-19 20:08:35 +000052 virtual const char* onGetName() { return computeName("rects"); }
reed@android.combd700c32009-01-05 03:34:50 +000053 virtual void onDraw(SkCanvas* canvas) {
54 SkPaint paint;
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000055 if (fStroke > 0) {
56 paint.setStyle(SkPaint::kStroke_Style);
57 paint.setStrokeWidth(SkIntToScalar(fStroke));
58 }
reed@android.combd700c32009-01-05 03:34:50 +000059 for (int i = 0; i < N; i++) {
60 paint.setColor(fColors[i]);
reed@android.com4bc19832009-01-19 20:08:35 +000061 this->setupPaint(&paint);
reed@android.combd700c32009-01-05 03:34:50 +000062 this->drawThisRect(canvas, fRects[i], paint);
63 }
64 }
reed@android.come9d00602009-09-02 21:12:42 +000065private:
66 typedef SkBenchmark INHERITED;
reed@android.combd700c32009-01-05 03:34:50 +000067};
68
69class OvalBench : public RectBench {
reed@android.comd2c37442009-01-08 03:00:57 +000070public:
reed@android.come9d00602009-09-02 21:12:42 +000071 OvalBench(void* param, int shift) : RectBench(param, shift) {}
reed@android.combd700c32009-01-05 03:34:50 +000072protected:
73 virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) {
74 c->drawOval(r, p);
75 }
reed@android.comd2c37442009-01-08 03:00:57 +000076 virtual const char* onGetName() { return computeName("ovals"); }
reed@android.combd700c32009-01-05 03:34:50 +000077};
78
79class RRectBench : public RectBench {
reed@android.comd2c37442009-01-08 03:00:57 +000080public:
reed@android.come9d00602009-09-02 21:12:42 +000081 RRectBench(void* param, int shift) : RectBench(param, shift) {}
reed@android.combd700c32009-01-05 03:34:50 +000082protected:
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.com4bc19832009-01-19 20:08:35 +000086 virtual const char* onGetName() { return computeName("rrects"); }
reed@android.combd700c32009-01-05 03:34:50 +000087};
88
89class PointsBench : public RectBench {
90public:
91 SkCanvas::PointMode fMode;
92 const char* fName;
93
reed@google.com1fcd51e2011-01-05 15:50:27 +000094 PointsBench(void* param, SkCanvas::PointMode mode, const char* name) :
reed@android.come9d00602009-09-02 21:12:42 +000095 RectBench(param, 2), fMode(mode) {
reed@android.combd700c32009-01-05 03:34:50 +000096 fName = name;
97 }
98
99protected:
100 virtual void onDraw(SkCanvas* canvas) {
agl@chromium.org652807b2010-04-27 15:47:34 +0000101 SkScalar gSizes[] = {
reed@android.combd700c32009-01-05 03:34:50 +0000102 SkIntToScalar(7), 0
103 };
agl@chromium.org652807b2010-04-27 15:47:34 +0000104 size_t sizes = SK_ARRAY_COUNT(gSizes);
105
106 if (this->hasStrokeWidth()) {
107 gSizes[0] = this->getStrokeWidth();
108 sizes = 1;
109 }
reed@android.combd700c32009-01-05 03:34:50 +0000110
111 SkPaint paint;
reed@android.combd700c32009-01-05 03:34:50 +0000112 paint.setStrokeCap(SkPaint::kRound_Cap);
agl@chromium.org652807b2010-04-27 15:47:34 +0000113
114 for (size_t i = 0; i < sizes; i++) {
reed@android.combd700c32009-01-05 03:34:50 +0000115 paint.setStrokeWidth(gSizes[i]);
reed@android.com4bc19832009-01-19 20:08:35 +0000116 this->setupPaint(&paint);
reed@google.com1fcd51e2011-01-05 15:50:27 +0000117 canvas->drawPoints(fMode, N * 2, SkTCast<SkPoint*>(fRects), paint);
reed@android.combd700c32009-01-05 03:34:50 +0000118 paint.setColor(fColors[i]);
119 }
120 }
121 virtual const char* onGetName() { return fName; }
122};
123
reed@google.com981d4792011-03-09 12:55:47 +0000124/*******************************************************************************
125 * to bench BlitMask [Opaque, Black, color, shader]
126 *******************************************************************************/
127
128class BlitMaskBench : public RectBench {
129public:
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
145protected:
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; }
197private:
198 typedef RectBench INHERITED;
199 kMaskType _type;
200};
201
202
mike@reedtribe.org2ecead52011-04-02 13:37:23 +0000203static SkBenchmark* RectFactory1F(void* p) { return SkNEW_ARGS(RectBench, (p, 1)); }
204static SkBenchmark* RectFactory1S(void* p) { return SkNEW_ARGS(RectBench, (p, 1, 4)); }
205static SkBenchmark* RectFactory2F(void* p) { return SkNEW_ARGS(RectBench, (p, 3)); }
206static SkBenchmark* RectFactory2S(void* p) { return SkNEW_ARGS(RectBench, (p, 3, 4)); }
reed@android.come9d00602009-09-02 21:12:42 +0000207static SkBenchmark* OvalFactory1(void* p) { return SkNEW_ARGS(OvalBench, (p, 1)); }
208static SkBenchmark* OvalFactory2(void* p) { return SkNEW_ARGS(OvalBench, (p, 3)); }
209static SkBenchmark* RRectFactory1(void* p) { return SkNEW_ARGS(RRectBench, (p, 1)); }
210static SkBenchmark* RRectFactory2(void* p) { return SkNEW_ARGS(RRectBench, (p, 3)); }
211static SkBenchmark* PointsFactory(void* p) {
212 return SkNEW_ARGS(PointsBench, (p, SkCanvas::kPoints_PointMode, "points"));
reed@android.combd700c32009-01-05 03:34:50 +0000213}
reed@android.come9d00602009-09-02 21:12:42 +0000214static SkBenchmark* LinesFactory(void* p) {
215 return SkNEW_ARGS(PointsBench, (p, SkCanvas::kLines_PointMode, "lines"));
reed@android.combd700c32009-01-05 03:34:50 +0000216}
reed@android.come9d00602009-09-02 21:12:42 +0000217static SkBenchmark* PolygonFactory(void* p) {
218 return SkNEW_ARGS(PointsBench, (p, SkCanvas::kPolygon_PointMode, "polygon"));
reed@android.combd700c32009-01-05 03:34:50 +0000219}
220
reed@google.com981d4792011-03-09 12:55:47 +0000221/* init the blitmask bench
222 */
223static SkBenchmark* BlitMaskOpaqueFactory(void* p) {
224 return SkNEW_ARGS(BlitMaskBench,
225 (p, SkCanvas::kPoints_PointMode,
226 BlitMaskBench::kMaskOpaque, "maskopaque")
227 );
228}
229static SkBenchmark* BlitMaskBlackFactory(void* p) {
230 return SkNEW_ARGS(BlitMaskBench,
231 (p, SkCanvas::kPoints_PointMode,
232 BlitMaskBench::kMaskBlack, "maskblack")
233 );
234}
235static SkBenchmark* BlitMaskColorFactory(void* p) {
236 return SkNEW_ARGS(BlitMaskBench,
237 (p, SkCanvas::kPoints_PointMode,
238 BlitMaskBench::kMaskColor, "maskcolor")
239 );
240}
241static SkBenchmark* BlitMaskShaderFactory(void* p) {
242 return SkNEW_ARGS(BlitMaskBench,
243 (p, SkCanvas::kPoints_PointMode,
244 BlitMaskBench::KMaskShader, "maskshader")
245 );
246}
mike@reedtribe.org2ecead52011-04-02 13:37:23 +0000247
248static BenchRegistry gRectReg1F(RectFactory1F);
249static BenchRegistry gRectReg1S(RectFactory1S);
250static BenchRegistry gRectReg2F(RectFactory2F);
251static BenchRegistry gRectReg2S(RectFactory2S);
reed@android.comf523e252009-01-26 23:15:37 +0000252static BenchRegistry gOvalReg1(OvalFactory1);
253static BenchRegistry gOvalReg2(OvalFactory2);
254static BenchRegistry gRRectReg1(RRectFactory1);
255static BenchRegistry gRRectReg2(RRectFactory2);
256static BenchRegistry gPointsReg(PointsFactory);
257static BenchRegistry gLinesReg(LinesFactory);
258static BenchRegistry gPolygonReg(PolygonFactory);
reed@google.com981d4792011-03-09 12:55:47 +0000259static BenchRegistry gRectRegOpaque(BlitMaskOpaqueFactory);
260static BenchRegistry gRectRegBlack(BlitMaskBlackFactory);
261static BenchRegistry gRectRegColor(BlitMaskColorFactory);
262static BenchRegistry gRectRegShader(BlitMaskShaderFactory);