blob: cef2e22d0635a68f94d135b176135ad0a6e7fb6e [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
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000026 RectBench(void* param, int shift, int stroke = 0) : INHERITED(param), fShift(shift), fStroke(stroke) {
reed@android.combd700c32009-01-05 03:34:50 +000027 SkRandom rand;
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000028 const SkScalar offset = SK_Scalar1/3;
reed@android.combd700c32009-01-05 03:34:50 +000029 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.comd2c37442009-01-08 03:00:57 +000034 w >>= shift;
35 h >>= shift;
reed@android.combd700c32009-01-05 03:34:50 +000036 x -= w/2;
37 y -= h/2;
38 fRects[i].set(SkIntToScalar(x), SkIntToScalar(y),
39 SkIntToScalar(x+w), SkIntToScalar(y+h));
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000040 fRects[i].offset(offset, offset);
reed@android.combd700c32009-01-05 03:34:50 +000041 fColors[i] = rand.nextU() | 0xFF808080;
42 }
43 }
reed@google.com1fcd51e2011-01-05 15:50:27 +000044
reed@android.comd2c37442009-01-08 03:00:57 +000045 SkString fName;
46 const char* computeName(const char root[]) {
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000047 fName.printf("%s_%d", root, fShift);
48 if (fStroke > 0) {
49 fName.appendf("_stroke_%d", fStroke);
50 }
reed@android.comd2c37442009-01-08 03:00:57 +000051 return fName.c_str();
52 }
reed@google.com1fcd51e2011-01-05 15:50:27 +000053
reed@android.combd700c32009-01-05 03:34:50 +000054protected:
55 virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) {
56 c->drawRect(r, p);
57 }
58
reed@android.com4bc19832009-01-19 20:08:35 +000059 virtual const char* onGetName() { return computeName("rects"); }
reed@android.combd700c32009-01-05 03:34:50 +000060 virtual void onDraw(SkCanvas* canvas) {
61 SkPaint paint;
mike@reedtribe.org2ecead52011-04-02 13:37:23 +000062 if (fStroke > 0) {
63 paint.setStyle(SkPaint::kStroke_Style);
64 paint.setStrokeWidth(SkIntToScalar(fStroke));
65 }
reed@android.combd700c32009-01-05 03:34:50 +000066 for (int i = 0; i < N; i++) {
67 paint.setColor(fColors[i]);
reed@android.com4bc19832009-01-19 20:08:35 +000068 this->setupPaint(&paint);
reed@android.combd700c32009-01-05 03:34:50 +000069 this->drawThisRect(canvas, fRects[i], paint);
70 }
71 }
reed@android.come9d00602009-09-02 21:12:42 +000072private:
73 typedef SkBenchmark INHERITED;
reed@android.combd700c32009-01-05 03:34:50 +000074};
75
76class OvalBench : public RectBench {
reed@android.comd2c37442009-01-08 03:00:57 +000077public:
reed@android.come9d00602009-09-02 21:12:42 +000078 OvalBench(void* param, int shift) : RectBench(param, shift) {}
reed@android.combd700c32009-01-05 03:34:50 +000079protected:
80 virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) {
81 c->drawOval(r, p);
82 }
reed@android.comd2c37442009-01-08 03:00:57 +000083 virtual const char* onGetName() { return computeName("ovals"); }
reed@android.combd700c32009-01-05 03:34:50 +000084};
85
86class RRectBench : public RectBench {
reed@android.comd2c37442009-01-08 03:00:57 +000087public:
reed@android.come9d00602009-09-02 21:12:42 +000088 RRectBench(void* param, int shift) : RectBench(param, shift) {}
reed@android.combd700c32009-01-05 03:34:50 +000089protected:
90 virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) {
91 c->drawRoundRect(r, r.width() / 4, r.height() / 4, p);
92 }
reed@android.com4bc19832009-01-19 20:08:35 +000093 virtual const char* onGetName() { return computeName("rrects"); }
reed@android.combd700c32009-01-05 03:34:50 +000094};
95
96class PointsBench : public RectBench {
97public:
98 SkCanvas::PointMode fMode;
99 const char* fName;
100
reed@google.com1fcd51e2011-01-05 15:50:27 +0000101 PointsBench(void* param, SkCanvas::PointMode mode, const char* name) :
reed@android.come9d00602009-09-02 21:12:42 +0000102 RectBench(param, 2), fMode(mode) {
reed@android.combd700c32009-01-05 03:34:50 +0000103 fName = name;
104 }
105
106protected:
107 virtual void onDraw(SkCanvas* canvas) {
agl@chromium.org652807b2010-04-27 15:47:34 +0000108 SkScalar gSizes[] = {
reed@android.combd700c32009-01-05 03:34:50 +0000109 SkIntToScalar(7), 0
110 };
agl@chromium.org652807b2010-04-27 15:47:34 +0000111 size_t sizes = SK_ARRAY_COUNT(gSizes);
112
113 if (this->hasStrokeWidth()) {
114 gSizes[0] = this->getStrokeWidth();
115 sizes = 1;
116 }
reed@android.combd700c32009-01-05 03:34:50 +0000117
118 SkPaint paint;
reed@android.combd700c32009-01-05 03:34:50 +0000119 paint.setStrokeCap(SkPaint::kRound_Cap);
agl@chromium.org652807b2010-04-27 15:47:34 +0000120
121 for (size_t i = 0; i < sizes; i++) {
reed@android.combd700c32009-01-05 03:34:50 +0000122 paint.setStrokeWidth(gSizes[i]);
reed@android.com4bc19832009-01-19 20:08:35 +0000123 this->setupPaint(&paint);
reed@google.com1fcd51e2011-01-05 15:50:27 +0000124 canvas->drawPoints(fMode, N * 2, SkTCast<SkPoint*>(fRects), paint);
reed@android.combd700c32009-01-05 03:34:50 +0000125 paint.setColor(fColors[i]);
126 }
127 }
128 virtual const char* onGetName() { return fName; }
129};
130
reed@google.com981d4792011-03-09 12:55:47 +0000131/*******************************************************************************
132 * to bench BlitMask [Opaque, Black, color, shader]
133 *******************************************************************************/
134
135class BlitMaskBench : public RectBench {
136public:
137 enum kMaskType {
138 kMaskOpaque = 0,
139 kMaskBlack,
140 kMaskColor,
141 KMaskShader
142 };
143 SkCanvas::PointMode fMode;
144 const char* fName;
145
146 BlitMaskBench(void* param, SkCanvas::PointMode mode,
147 BlitMaskBench::kMaskType type, const char* name) :
148 RectBench(param, 2), fMode(mode), _type(type) {
149 fName = name;
150 }
151
152protected:
153 virtual void onDraw(SkCanvas* canvas) {
154 SkScalar gSizes[] = {
155 SkIntToScalar(13), SkIntToScalar(24)
156 };
157 size_t sizes = SK_ARRAY_COUNT(gSizes);
158
159 if (this->hasStrokeWidth()) {
160 gSizes[0] = this->getStrokeWidth();
161 sizes = 1;
162 }
163 SkRandom rand;
164 SkColor color = 0xFF000000;
165 U8CPU alpha = 0xFF;
166 SkPaint paint;
167 paint.setStrokeCap(SkPaint::kRound_Cap);
168 if (_type == KMaskShader) {
169 SkBitmap srcBM;
170 srcBM.setConfig(SkBitmap::kARGB_8888_Config, 10, 1);
171 srcBM.allocPixels();
172 srcBM.eraseColor(0xFF00FF00);
173
174 SkShader* s;
175 s = SkShader::CreateBitmapShader(srcBM, SkShader::kClamp_TileMode,
176 SkShader::kClamp_TileMode);
177 paint.setShader(s)->unref();
178 }
179 for (size_t i = 0; i < sizes; i++) {
180 switch (_type) {
181 case kMaskOpaque:
182 color = fColors[i];
183 alpha = 0xFF;
184 break;
185 case kMaskBlack:
186 alpha = 0xFF;
187 color = 0xFF000000;
188 break;
189 case kMaskColor:
190 color = fColors[i];
191 alpha = rand.nextU() & 255;
192 break;
193 case KMaskShader:
194 break;
195 }
196 paint.setStrokeWidth(gSizes[i]);
197 this->setupPaint(&paint);
198 paint.setColor(color);
199 paint.setAlpha(alpha);
200 canvas->drawPoints(fMode, N * 2, SkTCast<SkPoint*>(fRects), paint);
201 }
202 }
203 virtual const char* onGetName() { return fName; }
204private:
205 typedef RectBench INHERITED;
206 kMaskType _type;
207};
208
209
mike@reedtribe.org2ecead52011-04-02 13:37:23 +0000210static SkBenchmark* RectFactory1F(void* p) { return SkNEW_ARGS(RectBench, (p, 1)); }
211static SkBenchmark* RectFactory1S(void* p) { return SkNEW_ARGS(RectBench, (p, 1, 4)); }
212static SkBenchmark* RectFactory2F(void* p) { return SkNEW_ARGS(RectBench, (p, 3)); }
213static SkBenchmark* RectFactory2S(void* p) { return SkNEW_ARGS(RectBench, (p, 3, 4)); }
reed@android.come9d00602009-09-02 21:12:42 +0000214static SkBenchmark* OvalFactory1(void* p) { return SkNEW_ARGS(OvalBench, (p, 1)); }
215static SkBenchmark* OvalFactory2(void* p) { return SkNEW_ARGS(OvalBench, (p, 3)); }
216static SkBenchmark* RRectFactory1(void* p) { return SkNEW_ARGS(RRectBench, (p, 1)); }
217static SkBenchmark* RRectFactory2(void* p) { return SkNEW_ARGS(RRectBench, (p, 3)); }
218static SkBenchmark* PointsFactory(void* p) {
219 return SkNEW_ARGS(PointsBench, (p, SkCanvas::kPoints_PointMode, "points"));
reed@android.combd700c32009-01-05 03:34:50 +0000220}
reed@android.come9d00602009-09-02 21:12:42 +0000221static SkBenchmark* LinesFactory(void* p) {
222 return SkNEW_ARGS(PointsBench, (p, SkCanvas::kLines_PointMode, "lines"));
reed@android.combd700c32009-01-05 03:34:50 +0000223}
reed@android.come9d00602009-09-02 21:12:42 +0000224static SkBenchmark* PolygonFactory(void* p) {
225 return SkNEW_ARGS(PointsBench, (p, SkCanvas::kPolygon_PointMode, "polygon"));
reed@android.combd700c32009-01-05 03:34:50 +0000226}
227
reed@google.com981d4792011-03-09 12:55:47 +0000228/* init the blitmask bench
229 */
230static SkBenchmark* BlitMaskOpaqueFactory(void* p) {
231 return SkNEW_ARGS(BlitMaskBench,
232 (p, SkCanvas::kPoints_PointMode,
233 BlitMaskBench::kMaskOpaque, "maskopaque")
234 );
235}
236static SkBenchmark* BlitMaskBlackFactory(void* p) {
237 return SkNEW_ARGS(BlitMaskBench,
238 (p, SkCanvas::kPoints_PointMode,
239 BlitMaskBench::kMaskBlack, "maskblack")
240 );
241}
242static SkBenchmark* BlitMaskColorFactory(void* p) {
243 return SkNEW_ARGS(BlitMaskBench,
244 (p, SkCanvas::kPoints_PointMode,
245 BlitMaskBench::kMaskColor, "maskcolor")
246 );
247}
248static SkBenchmark* BlitMaskShaderFactory(void* p) {
249 return SkNEW_ARGS(BlitMaskBench,
250 (p, SkCanvas::kPoints_PointMode,
251 BlitMaskBench::KMaskShader, "maskshader")
252 );
253}
mike@reedtribe.org2ecead52011-04-02 13:37:23 +0000254
255static BenchRegistry gRectReg1F(RectFactory1F);
256static BenchRegistry gRectReg1S(RectFactory1S);
257static BenchRegistry gRectReg2F(RectFactory2F);
258static BenchRegistry gRectReg2S(RectFactory2S);
reed@android.comf523e252009-01-26 23:15:37 +0000259static BenchRegistry gOvalReg1(OvalFactory1);
260static BenchRegistry gOvalReg2(OvalFactory2);
261static BenchRegistry gRRectReg1(RRectFactory1);
262static BenchRegistry gRRectReg2(RRectFactory2);
263static BenchRegistry gPointsReg(PointsFactory);
264static BenchRegistry gLinesReg(LinesFactory);
265static BenchRegistry gPolygonReg(PolygonFactory);
reed@google.com981d4792011-03-09 12:55:47 +0000266static BenchRegistry gRectRegOpaque(BlitMaskOpaqueFactory);
267static BenchRegistry gRectRegBlack(BlitMaskBlackFactory);
268static BenchRegistry gRectRegColor(BlitMaskColorFactory);
269static BenchRegistry gRectRegShader(BlitMaskShaderFactory);