1 #include "SkBenchmark.h"
2 #include "SkCanvas.h"
3 #include "SkPaint.h"
4 #include "SkRandom.h"
5 #include "SkString.h"
6 #include "SkShader.h"
7
8 class RectBench : public SkBenchmark {
9 public:
10 int fShift, fStroke;
11 enum {
12 W = 640,
13 H = 480,
14 N = 300
15 };
16 SkRect fRects[N];
17 SkColor fColors[N];
18
RectBench(void * param,int shift,int stroke=0)19 RectBench(void* param, int shift, int stroke = 0) : INHERITED(param), fShift(shift), fStroke(stroke) {
20 SkRandom rand;
21 const SkScalar offset = SK_Scalar1/3;
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;
27 w >>= shift;
28 h >>= shift;
29 x -= w/2;
30 y -= h/2;
31 fRects[i].set(SkIntToScalar(x), SkIntToScalar(y),
32 SkIntToScalar(x+w), SkIntToScalar(y+h));
33 fRects[i].offset(offset, offset);
34 fColors[i] = rand.nextU() | 0xFF808080;
35 }
36 }
37
38 SkString fName;
computeName(const char root[])39 const char* computeName(const char root[]) {
40 fName.printf("%s_%d", root, fShift);
41 if (fStroke > 0) {
42 fName.appendf("_stroke_%d", fStroke);
43 }
44 return fName.c_str();
45 }
46
47 protected:
drawThisRect(SkCanvas * c,const SkRect & r,const SkPaint & p)48 virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) {
49 c->drawRect(r, p);
50 }
51
onGetName()52 virtual const char* onGetName() { return computeName("rects"); }
onDraw(SkCanvas * canvas)53 virtual void onDraw(SkCanvas* canvas) {
54 SkPaint paint;
55 if (fStroke > 0) {
56 paint.setStyle(SkPaint::kStroke_Style);
57 paint.setStrokeWidth(SkIntToScalar(fStroke));
58 }
59 for (int i = 0; i < N; i++) {
60 paint.setColor(fColors[i]);
61 this->setupPaint(&paint);
62 this->drawThisRect(canvas, fRects[i], paint);
63 }
64 }
65 private:
66 typedef SkBenchmark INHERITED;
67 };
68
69 class OvalBench : public RectBench {
70 public:
OvalBench(void * param,int shift)71 OvalBench(void* param, int shift) : RectBench(param, shift) {}
72 protected:
drawThisRect(SkCanvas * c,const SkRect & r,const SkPaint & p)73 virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) {
74 c->drawOval(r, p);
75 }
onGetName()76 virtual const char* onGetName() { return computeName("ovals"); }
77 };
78
79 class RRectBench : public RectBench {
80 public:
RRectBench(void * param,int shift)81 RRectBench(void* param, int shift) : RectBench(param, shift) {}
82 protected:
drawThisRect(SkCanvas * c,const SkRect & r,const SkPaint & p)83 virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) {
84 c->drawRoundRect(r, r.width() / 4, r.height() / 4, p);
85 }
onGetName()86 virtual const char* onGetName() { return computeName("rrects"); }
87 };
88
89 class PointsBench : public RectBench {
90 public:
91 SkCanvas::PointMode fMode;
92 const char* fName;
93
PointsBench(void * param,SkCanvas::PointMode mode,const char * name)94 PointsBench(void* param, SkCanvas::PointMode mode, const char* name) :
95 RectBench(param, 2), fMode(mode) {
96 fName = name;
97 }
98
99 protected:
onDraw(SkCanvas * canvas)100 virtual void onDraw(SkCanvas* canvas) {
101 SkScalar gSizes[] = {
102 SkIntToScalar(7), 0
103 };
104 size_t sizes = SK_ARRAY_COUNT(gSizes);
105
106 if (this->hasStrokeWidth()) {
107 gSizes[0] = this->getStrokeWidth();
108 sizes = 1;
109 }
110
111 SkPaint paint;
112 paint.setStrokeCap(SkPaint::kRound_Cap);
113
114 for (size_t i = 0; i < sizes; i++) {
115 paint.setStrokeWidth(gSizes[i]);
116 this->setupPaint(&paint);
117 canvas->drawPoints(fMode, N * 2, SkTCast<SkPoint*>(fRects), paint);
118 paint.setColor(fColors[i]);
119 }
120 }
onGetName()121 virtual const char* onGetName() { return fName; }
122 };
123
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
BlitMaskBench(void * param,SkCanvas::PointMode mode,BlitMaskBench::kMaskType type,const char * name)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:
onDraw(SkCanvas * canvas)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 }
onGetName()196 virtual const char* onGetName() { return fName; }
197 private:
198 typedef RectBench INHERITED;
199 kMaskType _type;
200 };
201
202
RectFactory1F(void * p)203 static SkBenchmark* RectFactory1F(void* p) { return SkNEW_ARGS(RectBench, (p, 1)); }
RectFactory1S(void * p)204 static SkBenchmark* RectFactory1S(void* p) { return SkNEW_ARGS(RectBench, (p, 1, 4)); }
RectFactory2F(void * p)205 static SkBenchmark* RectFactory2F(void* p) { return SkNEW_ARGS(RectBench, (p, 3)); }
RectFactory2S(void * p)206 static SkBenchmark* RectFactory2S(void* p) { return SkNEW_ARGS(RectBench, (p, 3, 4)); }
OvalFactory1(void * p)207 static SkBenchmark* OvalFactory1(void* p) { return SkNEW_ARGS(OvalBench, (p, 1)); }
OvalFactory2(void * p)208 static SkBenchmark* OvalFactory2(void* p) { return SkNEW_ARGS(OvalBench, (p, 3)); }
RRectFactory1(void * p)209 static SkBenchmark* RRectFactory1(void* p) { return SkNEW_ARGS(RRectBench, (p, 1)); }
RRectFactory2(void * p)210 static SkBenchmark* RRectFactory2(void* p) { return SkNEW_ARGS(RRectBench, (p, 3)); }
PointsFactory(void * p)211 static SkBenchmark* PointsFactory(void* p) {
212 return SkNEW_ARGS(PointsBench, (p, SkCanvas::kPoints_PointMode, "points"));
213 }
LinesFactory(void * p)214 static SkBenchmark* LinesFactory(void* p) {
215 return SkNEW_ARGS(PointsBench, (p, SkCanvas::kLines_PointMode, "lines"));
216 }
PolygonFactory(void * p)217 static SkBenchmark* PolygonFactory(void* p) {
218 return SkNEW_ARGS(PointsBench, (p, SkCanvas::kPolygon_PointMode, "polygon"));
219 }
220
221 /* init the blitmask bench
222 */
BlitMaskOpaqueFactory(void * p)223 static SkBenchmark* BlitMaskOpaqueFactory(void* p) {
224 return SkNEW_ARGS(BlitMaskBench,
225 (p, SkCanvas::kPoints_PointMode,
226 BlitMaskBench::kMaskOpaque, "maskopaque")
227 );
228 }
BlitMaskBlackFactory(void * p)229 static SkBenchmark* BlitMaskBlackFactory(void* p) {
230 return SkNEW_ARGS(BlitMaskBench,
231 (p, SkCanvas::kPoints_PointMode,
232 BlitMaskBench::kMaskBlack, "maskblack")
233 );
234 }
BlitMaskColorFactory(void * p)235 static SkBenchmark* BlitMaskColorFactory(void* p) {
236 return SkNEW_ARGS(BlitMaskBench,
237 (p, SkCanvas::kPoints_PointMode,
238 BlitMaskBench::kMaskColor, "maskcolor")
239 );
240 }
BlitMaskShaderFactory(void * p)241 static SkBenchmark* BlitMaskShaderFactory(void* p) {
242 return SkNEW_ARGS(BlitMaskBench,
243 (p, SkCanvas::kPoints_PointMode,
244 BlitMaskBench::KMaskShader, "maskshader")
245 );
246 }
247
248 static BenchRegistry gRectReg1F(RectFactory1F);
249 static BenchRegistry gRectReg1S(RectFactory1S);
250 static BenchRegistry gRectReg2F(RectFactory2F);
251 static BenchRegistry gRectReg2S(RectFactory2S);
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);
259 static BenchRegistry gRectRegOpaque(BlitMaskOpaqueFactory);
260 static BenchRegistry gRectRegBlack(BlitMaskBlackFactory);
261 static BenchRegistry gRectRegColor(BlitMaskColorFactory);
262 static BenchRegistry gRectRegShader(BlitMaskShaderFactory);
263