3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include "SkCommandLineFlags.h"
16 DEFINE_double(strokeWidth, -1.0, "If set, use this stroke width in RectBench.");
18 class RectBench : public Benchmark {
29 RectBench(int shift, int stroke = 0)
34 const char* computeName(const char root[]) {
35 fName.printf("%s_%d", root, fShift);
37 fName.appendf("_stroke_%d", fStroke);
43 virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) {
47 virtual const char* onGetName() { return computeName("rects"); }
49 virtual void onPreDraw() {
51 const SkScalar offset = SK_Scalar1/3;
52 for (int i = 0; i < N; i++) {
53 int x = rand.nextU() % W;
54 int y = rand.nextU() % H;
55 int w = rand.nextU() % W;
56 int h = rand.nextU() % H;
61 fRects[i].set(SkIntToScalar(x), SkIntToScalar(y),
62 SkIntToScalar(x+w), SkIntToScalar(y+h));
63 fRects[i].offset(offset, offset);
64 fColors[i] = rand.nextU() | 0xFF808080;
68 virtual void onDraw(const int loops, SkCanvas* canvas) {
71 paint.setStyle(SkPaint::kStroke_Style);
72 paint.setStrokeWidth(SkIntToScalar(fStroke));
74 for (int i = 0; i < loops; i++) {
75 paint.setColor(fColors[i % N]);
76 this->setupPaint(&paint);
77 this->drawThisRect(canvas, fRects[i % N], paint);
81 typedef Benchmark INHERITED;
84 class SrcModeRectBench : public RectBench {
86 SrcModeRectBench() : INHERITED(1, 0) {
87 fMode = SkXfermode::Create(SkXfermode::kSrc_Mode);
90 virtual ~SrcModeRectBench() {
95 void setupPaint(SkPaint* paint) override {
96 this->INHERITED::setupPaint(paint);
97 // srcmode is most interesting when we're not opaque
98 paint->setAlpha(0x80);
99 paint->setXfermode(fMode);
102 const char* onGetName() override {
103 fName.set(this->INHERITED::onGetName());
104 fName.prepend("srcmode_");
105 return fName.c_str();
112 typedef RectBench INHERITED;
115 class OvalBench : public RectBench {
117 OvalBench(int shift, int stroke = 0) : RectBench(shift, stroke) {}
119 virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) {
122 virtual const char* onGetName() { return computeName("ovals"); }
125 class RRectBench : public RectBench {
127 RRectBench(int shift, int stroke = 0) : RectBench(shift, stroke) {}
129 virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) {
130 c->drawRoundRect(r, r.width() / 4, r.height() / 4, p);
132 virtual const char* onGetName() { return computeName("rrects"); }
135 class PointsBench : public RectBench {
137 SkCanvas::PointMode fMode;
140 PointsBench(SkCanvas::PointMode mode, const char* name)
147 virtual void onDraw(const int loops, SkCanvas* canvas) {
148 SkScalar gSizes[] = {
151 size_t sizes = SK_ARRAY_COUNT(gSizes);
153 if (FLAGS_strokeWidth >= 0) {
154 gSizes[0] = (SkScalar)FLAGS_strokeWidth;
159 paint.setStrokeCap(SkPaint::kRound_Cap);
161 for (int loop = 0; loop < loops; loop++) {
162 for (size_t i = 0; i < sizes; i++) {
163 paint.setStrokeWidth(gSizes[i]);
164 this->setupPaint(&paint);
165 canvas->drawPoints(fMode, N * 2, SkTCast<SkPoint*>(fRects), paint);
166 paint.setColor(fColors[i % N]);
170 virtual const char* onGetName() { return fName; }
173 /*******************************************************************************
174 * to bench BlitMask [Opaque, Black, color, shader]
175 *******************************************************************************/
177 class BlitMaskBench : public RectBench {
185 SkCanvas::PointMode fMode;
188 BlitMaskBench(SkCanvas::PointMode mode,
189 BlitMaskBench::kMaskType type, const char* name) :
190 RectBench(2), fMode(mode), _type(type) {
195 virtual void onDraw(const int loops, SkCanvas* canvas) {
196 SkScalar gSizes[] = {
197 SkIntToScalar(13), SkIntToScalar(24)
199 size_t sizes = SK_ARRAY_COUNT(gSizes);
201 if (FLAGS_strokeWidth >= 0) {
202 gSizes[0] = (SkScalar)FLAGS_strokeWidth;
206 SkColor color = 0xFF000000;
209 paint.setStrokeCap(SkPaint::kRound_Cap);
210 if (_type == KMaskShader) {
212 srcBM.allocN32Pixels(10, 1);
213 srcBM.eraseColor(0xFF00FF00);
216 s = SkShader::CreateBitmapShader(srcBM, SkShader::kClamp_TileMode,
217 SkShader::kClamp_TileMode);
218 paint.setShader(s)->unref();
220 for (int loop = 0; loop < loops; loop++) {
221 for (size_t i = 0; i < sizes; i++) {
233 alpha = rand.nextU() & 255;
238 paint.setStrokeWidth(gSizes[i]);
239 this->setupPaint(&paint);
240 paint.setColor(color);
241 paint.setAlpha(alpha);
242 canvas->drawPoints(fMode, N * 2, SkTCast<SkPoint*>(fRects), paint);
246 virtual const char* onGetName() { return fName; }
248 typedef RectBench INHERITED;
253 DEF_BENCH( return SkNEW_ARGS(RectBench, (1)); )
254 DEF_BENCH( return SkNEW_ARGS(RectBench, (1, 4)); )
255 DEF_BENCH( return SkNEW_ARGS(RectBench, (3)); )
256 DEF_BENCH( return SkNEW_ARGS(RectBench, (3, 4)); )
257 DEF_BENCH( return SkNEW_ARGS(OvalBench, (1)); )
258 DEF_BENCH( return SkNEW_ARGS(OvalBench, (3)); )
259 DEF_BENCH( return SkNEW_ARGS(OvalBench, (1, 4)); )
260 DEF_BENCH( return SkNEW_ARGS(OvalBench, (3, 4)); )
261 DEF_BENCH( return SkNEW_ARGS(RRectBench, (1)); )
262 DEF_BENCH( return SkNEW_ARGS(RRectBench, (1, 4)); )
263 DEF_BENCH( return SkNEW_ARGS(RRectBench, (3)); )
264 DEF_BENCH( return SkNEW_ARGS(RRectBench, (3, 4)); )
265 DEF_BENCH( return SkNEW_ARGS(PointsBench, (SkCanvas::kPoints_PointMode, "points")); )
266 DEF_BENCH( return SkNEW_ARGS(PointsBench, (SkCanvas::kLines_PointMode, "lines")); )
267 DEF_BENCH( return SkNEW_ARGS(PointsBench, (SkCanvas::kPolygon_PointMode, "polygon")); )
269 DEF_BENCH( return SkNEW_ARGS(SrcModeRectBench, ()); )
271 /* init the blitmask bench
273 DEF_BENCH( return SkNEW_ARGS(BlitMaskBench,
274 (SkCanvas::kPoints_PointMode,
275 BlitMaskBench::kMaskOpaque, "maskopaque")
277 DEF_BENCH( return SkNEW_ARGS(BlitMaskBench,
278 (SkCanvas::kPoints_PointMode,
279 BlitMaskBench::kMaskBlack, "maskblack")
281 DEF_BENCH( return SkNEW_ARGS(BlitMaskBench,
282 (SkCanvas::kPoints_PointMode,
283 BlitMaskBench::kMaskColor, "maskcolor")
285 DEF_BENCH( return SkNEW_ARGS(BlitMaskBench,
286 (SkCanvas::kPoints_PointMode,
287 BlitMaskBench::KMaskShader, "maskshader")