blob: ea4f6da5ac8d57f03da909e773a677b852948591 [file] [log] [blame]
reed@google.com57c49572011-10-31 14:33:35 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkBenchmark.h"
9#include "SkAAClip.h"
10#include "SkPath.h"
11#include "SkRegion.h"
12#include "SkString.h"
robertphillips@google.com6d62df42012-05-07 18:07:36 +000013#include "SkCanvas.h"
reed@google.com57c49572011-10-31 14:33:35 +000014
robertphillips@google.com6d62df42012-05-07 18:07:36 +000015
16////////////////////////////////////////////////////////////////////////////////
17// This bench tests out AA/BW clipping via canvas' clipPath and clipRect calls
18class AAClipBench : public SkBenchmark {
19 SkString fName;
20 SkPath fClipPath;
21 SkRect fClipRect;
22 SkRect fDrawRect;
23 bool fDoPath;
24 bool fDoAA;
25
26 enum {
27 N = SkBENCHLOOP(200),
28 };
29
30public:
31 AAClipBench(void* param, bool doPath, bool doAA)
32 : INHERITED(param) {
33 fDoPath = doPath;
34 fDoAA = doAA;
35
36 fName.printf("aaclip_%s_%s",
37 doPath ? "path" : "rect",
38 doAA ? "AA" : "BW");
39
40 fClipRect.set(SkFloatToScalar(10.5), SkFloatToScalar(10.5),
41 SkFloatToScalar(50.5), SkFloatToScalar(50.5));
42 fClipPath.addRoundRect(fClipRect, SkIntToScalar(10), SkIntToScalar(10));
43 fDrawRect.set(SkIntToScalar(0), SkIntToScalar(0),
44 SkIntToScalar(100), SkIntToScalar(100));
45
46 SkASSERT(fClipPath.isConvex());
47 }
48
49protected:
50 virtual const char* onGetName() { return fName.c_str(); }
51 virtual void onDraw(SkCanvas* canvas) {
52
53 SkPaint paint;
54 this->setupPaint(&paint);
55
56 for (int i = 0; i < N; ++i) {
57 // jostle the clip regions each time to prevent caching
58 fClipRect.offset((i % 2) == 0 ? SkIntToScalar(10) : SkIntToScalar(-10), 0);
59 fClipPath.reset();
60 fClipPath.addRoundRect(fClipRect,
61 SkIntToScalar(5), SkIntToScalar(5));
62 SkASSERT(fClipPath.isConvex());
63
64 canvas->save();
65#if 1
66 if (fDoPath) {
67 canvas->clipPath(fClipPath, SkRegion::kReplace_Op, fDoAA);
68 } else {
69 canvas->clipRect(fClipRect, SkRegion::kReplace_Op, fDoAA);
70 }
71
72 canvas->drawRect(fDrawRect, paint);
73#else
74 // this path tests out directly draw the clip primitive
75 // use it to comparing just drawing the clip vs. drawing using
76 // the clip
77 if (fDoPath) {
78 canvas->drawPath(fClipPath, paint);
79 } else {
80 canvas->drawRect(fClipRect, paint);
81 }
82#endif
83 canvas->restore();
84 }
85 }
86private:
87 typedef SkBenchmark INHERITED;
88};
89
90////////////////////////////////////////////////////////////////////////////////
reed@google.com57c49572011-10-31 14:33:35 +000091class AAClipBuilderBench : public SkBenchmark {
92 SkString fName;
93 SkPath fPath;
94 SkRect fRect;
95 SkRegion fRegion;
96 bool fDoPath;
97 bool fDoAA;
98
99 enum {
100 N = SkBENCHLOOP(200),
101 };
102
103public:
104 AAClipBuilderBench(void* param, bool doPath, bool doAA) : INHERITED(param) {
105 fDoPath = doPath;
106 fDoAA = doAA;
107
108 fName.printf("aaclip_build_%s_%s", doPath ? "path" : "rect",
109 doAA ? "AA" : "BW");
110
111 fRegion.setRect(0, 0, 640, 480);
112 fRect.set(fRegion.getBounds());
113 fRect.inset(SK_Scalar1/4, SK_Scalar1/4);
114 fPath.addRoundRect(fRect, SkIntToScalar(20), SkIntToScalar(20));
115 }
116
117protected:
118 virtual const char* onGetName() { return fName.c_str(); }
119 virtual void onDraw(SkCanvas* canvas) {
120 SkPaint paint;
121 this->setupPaint(&paint);
122
123 for (int i = 0; i < N; ++i) {
124 SkAAClip clip;
125 if (fDoPath) {
126 clip.setPath(fPath, &fRegion, fDoAA);
127 } else {
128 clip.setRect(fRect, fDoAA);
129 }
130 }
131 }
132private:
133 typedef SkBenchmark INHERITED;
134};
135
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000136////////////////////////////////////////////////////////////////////////////////
reed@google.coma069c8f2011-11-28 19:54:56 +0000137class AAClipRegionBench : public SkBenchmark {
138public:
139 AAClipRegionBench(void* param) : INHERITED(param) {
140 SkPath path;
141 // test conversion of a complex clip to a aaclip
142 path.addCircle(0, 0, SkIntToScalar(200));
143 path.addCircle(0, 0, SkIntToScalar(180));
144 // evenodd means we've constructed basically a stroked circle
145 path.setFillType(SkPath::kEvenOdd_FillType);
146
147 SkIRect bounds;
148 path.getBounds().roundOut(&bounds);
149 fRegion.setPath(path, SkRegion(bounds));
150 }
151
152protected:
153 virtual const char* onGetName() { return "aaclip_setregion"; }
154 virtual void onDraw(SkCanvas* canvas) {
155 for (int i = 0; i < N; ++i) {
156 SkAAClip clip;
157 clip.setRegion(fRegion);
158 }
159 }
160
161private:
162 enum {
163 N = SkBENCHLOOP(400),
164 };
165 SkRegion fRegion;
166 typedef SkBenchmark INHERITED;
167};
168
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000169////////////////////////////////////////////////////////////////////////////////
reed@google.com57c49572011-10-31 14:33:35 +0000170
171static SkBenchmark* Fact0(void* p) { return SkNEW_ARGS(AAClipBuilderBench, (p, false, false)); }
172static SkBenchmark* Fact1(void* p) { return SkNEW_ARGS(AAClipBuilderBench, (p, false, true)); }
173static SkBenchmark* Fact2(void* p) { return SkNEW_ARGS(AAClipBuilderBench, (p, true, false)); }
174static SkBenchmark* Fact3(void* p) { return SkNEW_ARGS(AAClipBuilderBench, (p, true, true)); }
175
176static BenchRegistry gReg0(Fact0);
177static BenchRegistry gReg1(Fact1);
178static BenchRegistry gReg2(Fact2);
179static BenchRegistry gReg3(Fact3);
reed@google.coma069c8f2011-11-28 19:54:56 +0000180
181static SkBenchmark* Fact01(void* p) { return SkNEW_ARGS(AAClipRegionBench, (p)); }
182static BenchRegistry gReg01(Fact01);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000183
184static SkBenchmark* Fact000(void* p) { return SkNEW_ARGS(AAClipBench, (p, false, false)); }
185static SkBenchmark* Fact001(void* p) { return SkNEW_ARGS(AAClipBench, (p, false, true)); }
186static SkBenchmark* Fact002(void* p) { return SkNEW_ARGS(AAClipBench, (p, true, false)); }
187static SkBenchmark* Fact003(void* p) { return SkNEW_ARGS(AAClipBench, (p, true, true)); }
188
189static BenchRegistry gReg000(Fact000);
190static BenchRegistry gReg001(Fact001);
191static BenchRegistry gReg002(Fact002);
192static BenchRegistry gReg003(Fact003);
193