blob: 3c04bd47f77e7fbf6611172e9709b96b1a12b9d1 [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"
robertphillips@google.com6bb99e32012-05-09 15:48:31 +000014#include "SkRandom.h"
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
robertphillips@google.com6d62df42012-05-07 18:07:36 +000026public:
rmistry@google.comfbfcd562012-08-23 18:09:54 +000027 AAClipBench(void* param, bool doPath, bool doAA)
robertphillips@google.com6bb99e32012-05-09 15:48:31 +000028 : INHERITED(param)
29 , fDoPath(doPath)
30 , fDoAA(doAA) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +000031
32 fName.printf("aaclip_%s_%s",
33 doPath ? "path" : "rect",
34 doAA ? "AA" : "BW");
35
rmistry@google.comfbfcd562012-08-23 18:09:54 +000036 fClipRect.set(SkFloatToScalar(10.5f), SkFloatToScalar(10.5f),
caryclark@google.com19069a22012-06-06 12:11:45 +000037 SkFloatToScalar(50.5f), SkFloatToScalar(50.5f));
robertphillips@google.com6d62df42012-05-07 18:07:36 +000038 fClipPath.addRoundRect(fClipRect, SkIntToScalar(10), SkIntToScalar(10));
39 fDrawRect.set(SkIntToScalar(0), SkIntToScalar(0),
40 SkIntToScalar(100), SkIntToScalar(100));
41
42 SkASSERT(fClipPath.isConvex());
43 }
44
45protected:
46 virtual const char* onGetName() { return fName.c_str(); }
47 virtual void onDraw(SkCanvas* canvas) {
48
49 SkPaint paint;
50 this->setupPaint(&paint);
51
mtklein@google.comc2897432013-09-10 19:23:38 +000052 for (int i = 0; i < this->getLoops(); ++i) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +000053 // jostle the clip regions each time to prevent caching
54 fClipRect.offset((i % 2) == 0 ? SkIntToScalar(10) : SkIntToScalar(-10), 0);
55 fClipPath.reset();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000056 fClipPath.addRoundRect(fClipRect,
robertphillips@google.com6d62df42012-05-07 18:07:36 +000057 SkIntToScalar(5), SkIntToScalar(5));
58 SkASSERT(fClipPath.isConvex());
59
60 canvas->save();
61#if 1
62 if (fDoPath) {
63 canvas->clipPath(fClipPath, SkRegion::kReplace_Op, fDoAA);
64 } else {
65 canvas->clipRect(fClipRect, SkRegion::kReplace_Op, fDoAA);
66 }
67
68 canvas->drawRect(fDrawRect, paint);
69#else
70 // this path tests out directly draw the clip primitive
71 // use it to comparing just drawing the clip vs. drawing using
72 // the clip
73 if (fDoPath) {
74 canvas->drawPath(fClipPath, paint);
75 } else {
76 canvas->drawRect(fClipRect, paint);
77 }
78#endif
79 canvas->restore();
80 }
81 }
82private:
83 typedef SkBenchmark INHERITED;
84};
85
86////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6bb99e32012-05-09 15:48:31 +000087// This bench tests out nested clip stacks. It is intended to simulate
88// how WebKit nests clips.
89class NestedAAClipBench : public SkBenchmark {
90 SkString fName;
91 bool fDoAA;
92 SkRect fDrawRect;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000093 SkRandom fRandom;
robertphillips@google.com6bb99e32012-05-09 15:48:31 +000094
robertphillips@google.comb1af07a2012-05-09 16:27:10 +000095 static const int kNestingDepth = 3;
robertphillips@google.com6bb99e32012-05-09 15:48:31 +000096 static const int kImageSize = 400;
97
98 SkPoint fSizes[kNestingDepth+1];
99
100public:
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000101 NestedAAClipBench(void* param, bool doAA)
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000102 : INHERITED(param)
103 , fDoAA(doAA) {
104
105 fName.printf("nested_aaclip_%s", doAA ? "AA" : "BW");
106
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000107 fDrawRect = SkRect::MakeLTRB(0, 0,
108 SkIntToScalar(kImageSize),
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000109 SkIntToScalar(kImageSize));
110
111 fSizes[0].set(SkIntToScalar(kImageSize), SkIntToScalar(kImageSize));
112
113 for (int i = 1; i < kNestingDepth+1; ++i) {
114 fSizes[i].set(fSizes[i-1].fX/2, fSizes[i-1].fY/2);
115 }
116 }
117
118protected:
119 virtual const char* onGetName() { return fName.c_str(); }
120
121
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000122 void recurse(SkCanvas* canvas,
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000123 int depth,
124 const SkPoint& offset) {
125
126 canvas->save();
127
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000128 SkRect temp = SkRect::MakeLTRB(0, 0,
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000129 fSizes[depth].fX, fSizes[depth].fY);
130 temp.offset(offset);
131
132 SkPath path;
133 path.addRoundRect(temp, SkIntToScalar(3), SkIntToScalar(3));
134 SkASSERT(path.isConvex());
135
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000136 canvas->clipPath(path,
137 0 == depth ? SkRegion::kReplace_Op :
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000138 SkRegion::kIntersect_Op,
139 fDoAA);
140
141 if (kNestingDepth == depth) {
142 // we only draw the draw rect at the lowest nesting level
143 SkPaint paint;
144 paint.setColor(0xff000000 | fRandom.nextU());
145 canvas->drawRect(fDrawRect, paint);
146 } else {
147 SkPoint childOffset = offset;
148 this->recurse(canvas, depth+1, childOffset);
149
150 childOffset += fSizes[depth+1];
151 this->recurse(canvas, depth+1, childOffset);
152
153 childOffset.fX = offset.fX + fSizes[depth+1].fX;
154 childOffset.fY = offset.fY;
155 this->recurse(canvas, depth+1, childOffset);
156
157 childOffset.fX = offset.fX;
158 childOffset.fY = offset.fY + fSizes[depth+1].fY;
159 this->recurse(canvas, depth+1, childOffset);
160 }
161
162 canvas->restore();
163 }
164
165 virtual void onDraw(SkCanvas* canvas) {
166
mtklein@google.comc2897432013-09-10 19:23:38 +0000167 for (int i = 0; i < this->getLoops(); ++i) {
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000168 SkPoint offset = SkPoint::Make(0, 0);
169 this->recurse(canvas, 0, offset);
170 }
171 }
172
173private:
174 typedef SkBenchmark INHERITED;
175};
176
177////////////////////////////////////////////////////////////////////////////////
reed@google.com57c49572011-10-31 14:33:35 +0000178class AAClipBuilderBench : public SkBenchmark {
179 SkString fName;
180 SkPath fPath;
181 SkRect fRect;
182 SkRegion fRegion;
183 bool fDoPath;
184 bool fDoAA;
185
reed@google.com57c49572011-10-31 14:33:35 +0000186public:
187 AAClipBuilderBench(void* param, bool doPath, bool doAA) : INHERITED(param) {
188 fDoPath = doPath;
189 fDoAA = doAA;
190
191 fName.printf("aaclip_build_%s_%s", doPath ? "path" : "rect",
192 doAA ? "AA" : "BW");
193
194 fRegion.setRect(0, 0, 640, 480);
195 fRect.set(fRegion.getBounds());
196 fRect.inset(SK_Scalar1/4, SK_Scalar1/4);
197 fPath.addRoundRect(fRect, SkIntToScalar(20), SkIntToScalar(20));
198 }
199
200protected:
201 virtual const char* onGetName() { return fName.c_str(); }
sugoi@google.com77472f02013-03-05 18:50:01 +0000202 virtual void onDraw(SkCanvas*) {
reed@google.com57c49572011-10-31 14:33:35 +0000203 SkPaint paint;
204 this->setupPaint(&paint);
205
mtklein@google.comc2897432013-09-10 19:23:38 +0000206 for (int i = 0; i < this->getLoops(); ++i) {
reed@google.com57c49572011-10-31 14:33:35 +0000207 SkAAClip clip;
208 if (fDoPath) {
209 clip.setPath(fPath, &fRegion, fDoAA);
210 } else {
211 clip.setRect(fRect, fDoAA);
212 }
213 }
214 }
215private:
216 typedef SkBenchmark INHERITED;
217};
218
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000219////////////////////////////////////////////////////////////////////////////////
reed@google.coma069c8f2011-11-28 19:54:56 +0000220class AAClipRegionBench : public SkBenchmark {
221public:
222 AAClipRegionBench(void* param) : INHERITED(param) {
223 SkPath path;
224 // test conversion of a complex clip to a aaclip
225 path.addCircle(0, 0, SkIntToScalar(200));
226 path.addCircle(0, 0, SkIntToScalar(180));
227 // evenodd means we've constructed basically a stroked circle
228 path.setFillType(SkPath::kEvenOdd_FillType);
229
230 SkIRect bounds;
231 path.getBounds().roundOut(&bounds);
232 fRegion.setPath(path, SkRegion(bounds));
233 }
234
235protected:
236 virtual const char* onGetName() { return "aaclip_setregion"; }
sugoi@google.com77472f02013-03-05 18:50:01 +0000237 virtual void onDraw(SkCanvas*) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000238 for (int i = 0; i < this->getLoops(); ++i) {
reed@google.coma069c8f2011-11-28 19:54:56 +0000239 SkAAClip clip;
240 clip.setRegion(fRegion);
241 }
242 }
243
244private:
reed@google.coma069c8f2011-11-28 19:54:56 +0000245 SkRegion fRegion;
246 typedef SkBenchmark INHERITED;
247};
248
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000249////////////////////////////////////////////////////////////////////////////////
reed@google.com57c49572011-10-31 14:33:35 +0000250
251static SkBenchmark* Fact0(void* p) { return SkNEW_ARGS(AAClipBuilderBench, (p, false, false)); }
252static SkBenchmark* Fact1(void* p) { return SkNEW_ARGS(AAClipBuilderBench, (p, false, true)); }
253static SkBenchmark* Fact2(void* p) { return SkNEW_ARGS(AAClipBuilderBench, (p, true, false)); }
254static SkBenchmark* Fact3(void* p) { return SkNEW_ARGS(AAClipBuilderBench, (p, true, true)); }
255
256static BenchRegistry gReg0(Fact0);
257static BenchRegistry gReg1(Fact1);
258static BenchRegistry gReg2(Fact2);
259static BenchRegistry gReg3(Fact3);
reed@google.coma069c8f2011-11-28 19:54:56 +0000260
261static SkBenchmark* Fact01(void* p) { return SkNEW_ARGS(AAClipRegionBench, (p)); }
262static BenchRegistry gReg01(Fact01);
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000263
264static SkBenchmark* Fact000(void* p) { return SkNEW_ARGS(AAClipBench, (p, false, false)); }
265static SkBenchmark* Fact001(void* p) { return SkNEW_ARGS(AAClipBench, (p, false, true)); }
266static SkBenchmark* Fact002(void* p) { return SkNEW_ARGS(AAClipBench, (p, true, false)); }
267static SkBenchmark* Fact003(void* p) { return SkNEW_ARGS(AAClipBench, (p, true, true)); }
268
269static BenchRegistry gReg000(Fact000);
270static BenchRegistry gReg001(Fact001);
271static BenchRegistry gReg002(Fact002);
272static BenchRegistry gReg003(Fact003);
273
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000274static SkBenchmark* Fact004(void* p) { return SkNEW_ARGS(NestedAAClipBench, (p, false)); }
275static SkBenchmark* Fact005(void* p) { return SkNEW_ARGS(NestedAAClipBench, (p, true)); }
276
277static BenchRegistry gReg004(Fact004);
278static BenchRegistry gReg005(Fact005);