blob: bf0d45614308b6ff366e6cce4dff1b93442adffb [file] [log] [blame]
reed@google.com4aa1a702012-05-04 16:37:45 +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 */
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
reed@google.com4aa1a702012-05-04 16:37:45 +00009#include "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkDashPathEffect.h"
12#include "SkPaint.h"
13#include "SkPath.h"
14#include "SkRandom.h"
15#include "SkString.h"
16#include "SkTDArray.h"
17
reed@google.comef85e3c2012-05-10 15:40:57 +000018
reed@google.com4aa1a702012-05-04 16:37:45 +000019/*
20 * Cases to consider:
21 *
22 * 1. antialiasing on/off (esp. width <= 1)
23 * 2. strokewidth == 0, 1, 2
24 * 3. hline, vline, diagonal, rect, oval
25 * 4. dots [1,1] ([N,N] where N=strokeWidth?) or arbitrary (e.g. [2,1] or [1,2,3,2])
26 */
27static void path_hline(SkPath* path) {
28 path->moveTo(SkIntToScalar(10), SkIntToScalar(10));
29 path->lineTo(SkIntToScalar(600), SkIntToScalar(10));
30}
31
tfarinaf168b862014-06-19 12:32:29 -070032class DashBench : public Benchmark {
reed@google.comef85e3c2012-05-10 15:40:57 +000033protected:
mike@reedtribe.org99a6ef42012-05-05 13:46:10 +000034 SkString fName;
reed@google.com4aa1a702012-05-04 16:37:45 +000035 SkTDArray<SkScalar> fIntervals;
mike@reedtribe.org99a6ef42012-05-05 13:46:10 +000036 int fWidth;
reed@google.coma584aed2012-05-16 14:06:02 +000037 SkPoint fPts[2];
38 bool fDoClip;
reed@google.com4aa1a702012-05-04 16:37:45 +000039
reed@google.com4aa1a702012-05-04 16:37:45 +000040public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000041 DashBench(const SkScalar intervals[], int count, int width,
42 bool doClip = false) {
reed@google.com4aa1a702012-05-04 16:37:45 +000043 fIntervals.append(count, intervals);
mike@reedtribe.org99a6ef42012-05-05 13:46:10 +000044 for (int i = 0; i < count; ++i) {
45 fIntervals[i] *= width;
46 }
47 fWidth = width;
reed@google.com4ad22752012-05-15 19:50:58 +000048 fName.printf("dash_%d_%s", width, doClip ? "clipped" : "noclip");
49 fDoClip = doClip;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000050
reed@google.coma584aed2012-05-16 14:06:02 +000051 fPts[0].set(SkIntToScalar(10), SkIntToScalar(10));
52 fPts[1].set(SkIntToScalar(600), SkIntToScalar(10));
reed@google.com4aa1a702012-05-04 16:37:45 +000053 }
54
55 virtual void makePath(SkPath* path) {
56 path_hline(path);
57 }
58
59protected:
mtklein36352bf2015-03-25 18:17:31 -070060 const char* onGetName() override {
mike@reedtribe.org99a6ef42012-05-05 13:46:10 +000061 return fName.c_str();
reed@google.com4aa1a702012-05-04 16:37:45 +000062 }
63
mtklein36352bf2015-03-25 18:17:31 -070064 void onDraw(const int loops, SkCanvas* canvas) override {
reed@google.com4aa1a702012-05-04 16:37:45 +000065 SkPaint paint;
66 this->setupPaint(&paint);
67 paint.setStyle(SkPaint::kStroke_Style);
mike@reedtribe.org99a6ef42012-05-05 13:46:10 +000068 paint.setStrokeWidth(SkIntToScalar(fWidth));
reed@google.com4aa1a702012-05-04 16:37:45 +000069 paint.setAntiAlias(false);
70
71 SkPath path;
72 this->makePath(&path);
73
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +000074 paint.setPathEffect(SkDashPathEffect::Create(fIntervals.begin(),
75 fIntervals.count(), 0))->unref();
reed@google.com4ad22752012-05-15 19:50:58 +000076
77 if (fDoClip) {
78 SkRect r = path.getBounds();
79 r.inset(-SkIntToScalar(20), -SkIntToScalar(20));
80 // now move it so we don't intersect
81 r.offset(0, r.height() * 3 / 2);
82 canvas->clipRect(r);
83 }
84
commit-bot@chromium.org33614712013-12-03 18:17:16 +000085 this->handlePath(canvas, path, paint, loops);
reed@google.comef85e3c2012-05-10 15:40:57 +000086 }
87
88 virtual void handlePath(SkCanvas* canvas, const SkPath& path,
89 const SkPaint& paint, int N) {
reed@google.com4aa1a702012-05-04 16:37:45 +000090 for (int i = 0; i < N; ++i) {
reed@google.coma584aed2012-05-16 14:06:02 +000091// canvas->drawPoints(SkCanvas::kLines_PointMode, 2, fPts, paint);
reed@google.com4aa1a702012-05-04 16:37:45 +000092 canvas->drawPath(path, paint);
93 }
94 }
95
96private:
tfarinaf168b862014-06-19 12:32:29 -070097 typedef Benchmark INHERITED;
reed@google.com4aa1a702012-05-04 16:37:45 +000098};
99
reed@google.comef85e3c2012-05-10 15:40:57 +0000100class RectDashBench : public DashBench {
101public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000102 RectDashBench(const SkScalar intervals[], int count, int width)
103 : INHERITED(intervals, count, width) {
reed@google.comef85e3c2012-05-10 15:40:57 +0000104 fName.append("_rect");
105 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000106
reed@google.comef85e3c2012-05-10 15:40:57 +0000107protected:
108 virtual void handlePath(SkCanvas* canvas, const SkPath& path,
mtklein36352bf2015-03-25 18:17:31 -0700109 const SkPaint& paint, int N) override {
reed@google.comef85e3c2012-05-10 15:40:57 +0000110 SkPoint pts[2];
111 if (!path.isLine(pts) || pts[0].fY != pts[1].fY) {
112 this->INHERITED::handlePath(canvas, path, paint, N);
113 } else {
114 SkRect rect;
115 rect.fLeft = pts[0].fX;
116 rect.fTop = pts[0].fY - paint.getStrokeWidth() / 2;
117 rect.fRight = rect.fLeft + SkIntToScalar(fWidth);
118 rect.fBottom = rect.fTop + paint.getStrokeWidth();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000119
reed@google.comef85e3c2012-05-10 15:40:57 +0000120 SkPaint p(paint);
121 p.setStyle(SkPaint::kFill_Style);
122 p.setPathEffect(NULL);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000123
reed@google.comef85e3c2012-05-10 15:40:57 +0000124 int count = SkScalarRoundToInt((pts[1].fX - pts[0].fX) / (2*fWidth));
125 SkScalar dx = SkIntToScalar(2 * fWidth);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000126
reed@google.comef85e3c2012-05-10 15:40:57 +0000127 for (int i = 0; i < N*10; ++i) {
128 SkRect r = rect;
129 for (int j = 0; j < count; ++j) {
130 canvas->drawRect(r, p);
131 r.offset(dx, 0);
132 }
133 }
134 }
135 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000136
reed@google.comef85e3c2012-05-10 15:40:57 +0000137private:
138 typedef DashBench INHERITED;
139};
140
reed@google.comea6f6832012-05-18 18:32:54 +0000141static void make_unit_star(SkPath* path, int n) {
142 SkScalar rad = -SK_ScalarPI / 2;
143 const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000144
reed@google.comea6f6832012-05-18 18:32:54 +0000145 path->moveTo(0, -SK_Scalar1);
146 for (int i = 1; i < n; i++) {
147 rad += drad;
148 SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV);
149 path->lineTo(cosV, sinV);
150 }
151 path->close();
152}
153
154static void make_poly(SkPath* path) {
155 make_unit_star(path, 9);
robertphillips1d24b8d2015-03-26 19:57:08 -0700156 const SkMatrix matrix = SkMatrix::MakeScale(SkIntToScalar(100), SkIntToScalar(100));
reed@google.comea6f6832012-05-18 18:32:54 +0000157 path->transform(matrix);
158}
159
160static void make_quad(SkPath* path) {
161 SkScalar x0 = SkIntToScalar(10);
162 SkScalar y0 = SkIntToScalar(10);
163 path->moveTo(x0, y0);
164 path->quadTo(x0, y0 + 400 * SK_Scalar1,
165 x0 + 600 * SK_Scalar1, y0 + 400 * SK_Scalar1);
166}
167
168static void make_cubic(SkPath* path) {
169 SkScalar x0 = SkIntToScalar(10);
170 SkScalar y0 = SkIntToScalar(10);
171 path->moveTo(x0, y0);
172 path->cubicTo(x0, y0 + 400 * SK_Scalar1,
173 x0 + 600 * SK_Scalar1, y0 + 400 * SK_Scalar1,
174 x0 + 600 * SK_Scalar1, y0);
175}
176
tfarinaf168b862014-06-19 12:32:29 -0700177class MakeDashBench : public Benchmark {
reed@google.comea6f6832012-05-18 18:32:54 +0000178 SkString fName;
179 SkPath fPath;
180 SkAutoTUnref<SkPathEffect> fPE;
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000181
reed@google.comea6f6832012-05-18 18:32:54 +0000182public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000183 MakeDashBench(void (*proc)(SkPath*), const char name[]) {
reed@google.comea6f6832012-05-18 18:32:54 +0000184 fName.printf("makedash_%s", name);
185 proc(&fPath);
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000186
reed@google.comea6f6832012-05-18 18:32:54 +0000187 SkScalar vals[] = { SkIntToScalar(4), SkIntToScalar(4) };
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000188 fPE.reset(SkDashPathEffect::Create(vals, 2, 0));
reed@google.comea6f6832012-05-18 18:32:54 +0000189 }
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000190
reed@google.comea6f6832012-05-18 18:32:54 +0000191protected:
mtklein36352bf2015-03-25 18:17:31 -0700192 const char* onGetName() override {
reed@google.comea6f6832012-05-18 18:32:54 +0000193 return fName.c_str();
194 }
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000195
mtklein36352bf2015-03-25 18:17:31 -0700196 void onDraw(const int loops, SkCanvas*) override {
reed@google.comea6f6832012-05-18 18:32:54 +0000197 SkPath dst;
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000198 for (int i = 0; i < loops; ++i) {
reed@google.comfd4be262012-05-25 01:04:12 +0000199 SkStrokeRec rec(SkStrokeRec::kHairline_InitStyle);
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000200
reed@google.com4bbdeac2013-01-24 21:03:11 +0000201 fPE->filterPath(&dst, fPath, &rec, NULL);
reed@google.comea6f6832012-05-18 18:32:54 +0000202 dst.rewind();
203 }
204 }
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000205
reed@google.com31e9d642012-09-04 20:07:23 +0000206private:
tfarinaf168b862014-06-19 12:32:29 -0700207 typedef Benchmark INHERITED;
reed@google.com31e9d642012-09-04 20:07:23 +0000208};
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000209
reed@google.com5125d842012-09-04 20:19:35 +0000210/*
211 * We try to special case square dashes (intervals are equal to strokewidth).
212 */
tfarinaf168b862014-06-19 12:32:29 -0700213class DashLineBench : public Benchmark {
reed@google.com31e9d642012-09-04 20:07:23 +0000214 SkString fName;
reed@google.com5125d842012-09-04 20:19:35 +0000215 SkScalar fStrokeWidth;
216 bool fIsRound;
reed@google.com31e9d642012-09-04 20:07:23 +0000217 SkAutoTUnref<SkPathEffect> fPE;
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000218
reed@google.com31e9d642012-09-04 20:07:23 +0000219public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000220 DashLineBench(SkScalar width, bool isRound) {
reed@google.com5125d842012-09-04 20:19:35 +0000221 fName.printf("dashline_%g_%s", SkScalarToFloat(width), isRound ? "circle" : "square");
222 fStrokeWidth = width;
223 fIsRound = isRound;
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000224
reed@google.com5125d842012-09-04 20:19:35 +0000225 SkScalar vals[] = { SK_Scalar1, SK_Scalar1 };
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000226 fPE.reset(SkDashPathEffect::Create(vals, 2, 0));
reed@google.com31e9d642012-09-04 20:07:23 +0000227 }
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000228
reed@google.com31e9d642012-09-04 20:07:23 +0000229protected:
mtklein36352bf2015-03-25 18:17:31 -0700230 const char* onGetName() override {
reed@google.com31e9d642012-09-04 20:07:23 +0000231 return fName.c_str();
232 }
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000233
mtklein36352bf2015-03-25 18:17:31 -0700234 void onDraw(const int loops, SkCanvas* canvas) override {
reed@google.com31e9d642012-09-04 20:07:23 +0000235 SkPaint paint;
236 this->setupPaint(&paint);
reed@google.com5125d842012-09-04 20:19:35 +0000237 paint.setStrokeWidth(fStrokeWidth);
238 paint.setStrokeCap(fIsRound ? SkPaint::kRound_Cap : SkPaint::kSquare_Cap);
reed@google.com31e9d642012-09-04 20:07:23 +0000239 paint.setPathEffect(fPE);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000240 for (int i = 0; i < loops; ++i) {
reed@google.com5125d842012-09-04 20:19:35 +0000241 canvas->drawLine(10 * SK_Scalar1, 10 * SK_Scalar1,
242 640 * SK_Scalar1, 10 * SK_Scalar1, paint);
reed@google.com31e9d642012-09-04 20:07:23 +0000243 }
244 }
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000245
reed@google.comea6f6832012-05-18 18:32:54 +0000246private:
tfarinaf168b862014-06-19 12:32:29 -0700247 typedef Benchmark INHERITED;
reed@google.comea6f6832012-05-18 18:32:54 +0000248};
249
tfarinaf168b862014-06-19 12:32:29 -0700250class DrawPointsDashingBench : public Benchmark {
robertphillips@google.com935ad022012-12-05 19:07:21 +0000251 SkString fName;
252 int fStrokeWidth;
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000253 bool fDoAA;
robertphillips@google.com935ad022012-12-05 19:07:21 +0000254
255 SkAutoTUnref<SkPathEffect> fPathEffect;
256
robertphillips@google.com935ad022012-12-05 19:07:21 +0000257public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000258 DrawPointsDashingBench(int dashLength, int strokeWidth, bool doAA)
259 {
robertphillips@google.com935ad022012-12-05 19:07:21 +0000260 fName.printf("drawpointsdash_%d_%d%s", dashLength, strokeWidth, doAA ? "_aa" : "_bw");
261 fStrokeWidth = strokeWidth;
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000262 fDoAA = doAA;
robertphillips@google.com935ad022012-12-05 19:07:21 +0000263
264 SkScalar vals[] = { SkIntToScalar(dashLength), SkIntToScalar(dashLength) };
commit-bot@chromium.org35c03fb2014-03-31 18:52:51 +0000265 fPathEffect.reset(SkDashPathEffect::Create(vals, 2, SK_Scalar1));
robertphillips@google.com935ad022012-12-05 19:07:21 +0000266 }
267
268protected:
mtklein36352bf2015-03-25 18:17:31 -0700269 const char* onGetName() override {
robertphillips@google.com935ad022012-12-05 19:07:21 +0000270 return fName.c_str();
271 }
272
mtklein36352bf2015-03-25 18:17:31 -0700273 void onDraw(const int loops, SkCanvas* canvas) override {
robertphillips@google.com935ad022012-12-05 19:07:21 +0000274 SkPaint p;
275 this->setupPaint(&p);
276 p.setColor(SK_ColorBLACK);
277 p.setStyle(SkPaint::kStroke_Style);
278 p.setStrokeWidth(SkIntToScalar(fStrokeWidth));
279 p.setPathEffect(fPathEffect);
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000280 p.setAntiAlias(fDoAA);
robertphillips@google.com935ad022012-12-05 19:07:21 +0000281
282 SkPoint pts[2] = {
283 { SkIntToScalar(10), 0 },
284 { SkIntToScalar(640), 0 }
285 };
286
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000287 for (int i = 0; i < loops; ++i) {
robertphillips@google.com935ad022012-12-05 19:07:21 +0000288 pts[0].fY = pts[1].fY = SkIntToScalar(i % 480);
289 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p);
290 }
291 }
292
293private:
tfarinaf168b862014-06-19 12:32:29 -0700294 typedef Benchmark INHERITED;
robertphillips@google.com935ad022012-12-05 19:07:21 +0000295};
reed@google.com996f64f2013-01-24 17:17:28 +0000296
reed@google.come5ceea92013-01-24 17:33:21 +0000297// Want to test how we handle dashing when 99% of the dash is clipped out
tfarinaf168b862014-06-19 12:32:29 -0700298class GiantDashBench : public Benchmark {
reed@google.com996f64f2013-01-24 17:17:28 +0000299 SkString fName;
reed@google.come5ceea92013-01-24 17:33:21 +0000300 SkScalar fStrokeWidth;
reed@google.com996f64f2013-01-24 17:17:28 +0000301 SkPoint fPts[2];
302 SkAutoTUnref<SkPathEffect> fPathEffect;
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000303
reed@google.com996f64f2013-01-24 17:17:28 +0000304public:
305 enum LineType {
306 kHori_LineType,
307 kVert_LineType,
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +0000308 kDiag_LineType,
309 kLineTypeCount
reed@google.com996f64f2013-01-24 17:17:28 +0000310 };
311
312 static const char* LineTypeName(LineType lt) {
313 static const char* gNames[] = { "hori", "vert", "diag" };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +0000314 SK_COMPILE_ASSERT(kLineTypeCount == SK_ARRAY_COUNT(gNames), names_wrong_size);
reed@google.com996f64f2013-01-24 17:17:28 +0000315 return gNames[lt];
316 }
317
mtklein@google.com410e6e82013-09-13 19:52:27 +0000318 GiantDashBench(LineType lt, SkScalar width) {
reed@google.come5ceea92013-01-24 17:33:21 +0000319 fName.printf("giantdashline_%s_%g", LineTypeName(lt), width);
320 fStrokeWidth = width;
321
reed@google.com996f64f2013-01-24 17:17:28 +0000322 // deliberately pick intervals that won't be caught by asPoints(), so
323 // we can test the filterPath code-path.
commit-bot@chromium.orgae4976c2014-05-01 15:30:52 +0000324 const SkScalar intervals[] = { 20, 10, 10, 10 };
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000325 fPathEffect.reset(SkDashPathEffect::Create(intervals,
326 SK_ARRAY_COUNT(intervals), 0));
reed@google.com996f64f2013-01-24 17:17:28 +0000327
328 SkScalar cx = 640 / 2; // center X
329 SkScalar cy = 480 / 2; // center Y
330 SkMatrix matrix;
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000331
reed@google.com996f64f2013-01-24 17:17:28 +0000332 switch (lt) {
333 case kHori_LineType:
334 matrix.setIdentity();
335 break;
336 case kVert_LineType:
337 matrix.setRotate(90, cx, cy);
338 break;
339 case kDiag_LineType:
340 matrix.setRotate(45, cx, cy);
341 break;
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +0000342 case kLineTypeCount:
343 // Not a real enum value.
344 break;
reed@google.com996f64f2013-01-24 17:17:28 +0000345 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000346
reed@google.come5ceea92013-01-24 17:33:21 +0000347 const SkScalar overshoot = 100*1000;
reed@google.com996f64f2013-01-24 17:17:28 +0000348 const SkPoint pts[2] = {
349 { -overshoot, cy }, { 640 + overshoot, cy }
350 };
351 matrix.mapPoints(fPts, pts, 2);
352 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000353
reed@google.com996f64f2013-01-24 17:17:28 +0000354protected:
mtklein36352bf2015-03-25 18:17:31 -0700355 const char* onGetName() override {
reed@google.com996f64f2013-01-24 17:17:28 +0000356 return fName.c_str();
357 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000358
mtklein36352bf2015-03-25 18:17:31 -0700359 void onDraw(const int loops, SkCanvas* canvas) override {
reed@google.com996f64f2013-01-24 17:17:28 +0000360 SkPaint p;
361 this->setupPaint(&p);
362 p.setStyle(SkPaint::kStroke_Style);
reed@google.come5ceea92013-01-24 17:33:21 +0000363 p.setStrokeWidth(fStrokeWidth);
reed@google.com996f64f2013-01-24 17:17:28 +0000364 p.setPathEffect(fPathEffect);
365
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000366 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000367 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, fPts, p);
368 }
reed@google.com996f64f2013-01-24 17:17:28 +0000369 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000370
reed@google.com996f64f2013-01-24 17:17:28 +0000371private:
tfarinaf168b862014-06-19 12:32:29 -0700372 typedef Benchmark INHERITED;
reed@google.com996f64f2013-01-24 17:17:28 +0000373};
374
egdaniel05bb6f12014-06-04 08:15:31 -0700375// Want to test how we draw a dashed grid (like what is used in spreadsheets) of many
376// small dashed lines switching back and forth between horizontal and vertical
tfarinaf168b862014-06-19 12:32:29 -0700377class DashGridBench : public Benchmark {
egdaniel05bb6f12014-06-04 08:15:31 -0700378 SkString fName;
379 int fStrokeWidth;
380 bool fDoAA;
381
382 SkAutoTUnref<SkPathEffect> fPathEffect;
383
384public:
385 DashGridBench(int dashLength, int strokeWidth, bool doAA) {
386 fName.printf("dashgrid_%d_%d%s", dashLength, strokeWidth, doAA ? "_aa" : "_bw");
387 fStrokeWidth = strokeWidth;
388 fDoAA = doAA;
389
390 SkScalar vals[] = { SkIntToScalar(dashLength), SkIntToScalar(dashLength) };
391 fPathEffect.reset(SkDashPathEffect::Create(vals, 2, SK_Scalar1));
392 }
393
394protected:
mtklein36352bf2015-03-25 18:17:31 -0700395 const char* onGetName() override {
egdaniel05bb6f12014-06-04 08:15:31 -0700396 return fName.c_str();
397 }
398
mtklein36352bf2015-03-25 18:17:31 -0700399 void onDraw(const int loops, SkCanvas* canvas) override {
egdaniel05bb6f12014-06-04 08:15:31 -0700400 SkPaint p;
401 this->setupPaint(&p);
402 p.setColor(SK_ColorBLACK);
403 p.setStyle(SkPaint::kStroke_Style);
404 p.setStrokeWidth(SkIntToScalar(fStrokeWidth));
405 p.setPathEffect(fPathEffect);
406 p.setAntiAlias(fDoAA);
407
408 SkPoint pts[4] = {
409 { SkIntToScalar(0), 20.5f },
410 { SkIntToScalar(20), 20.5f },
411 { 20.5f, SkIntToScalar(0) },
412 { 20.5f, SkIntToScalar(20) }
413 };
414
415 for (int i = 0; i < loops; ++i) {
416 for (int j = 0; j < 10; ++j) {
417 for (int k = 0; k < 10; ++k) {
418 // Horizontal line
419 SkPoint horPts[2];
420 horPts[0].fX = pts[0].fX + k * 22.f;
421 horPts[0].fY = pts[0].fY + j * 22.f;
422 horPts[1].fX = pts[1].fX + k * 22.f;
423 horPts[1].fY = pts[1].fY + j * 22.f;
424 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, horPts, p);
425
426 // Vertical line
427 SkPoint vertPts[2];
428 vertPts[0].fX = pts[2].fX + k * 22.f;
429 vertPts[0].fY = pts[2].fY + j * 22.f;
430 vertPts[1].fX = pts[3].fX + k * 22.f;
431 vertPts[1].fY = pts[3].fY + j * 22.f;
432 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, vertPts, p);
433 }
434 }
435 }
436 }
437
438private:
tfarinaf168b862014-06-19 12:32:29 -0700439 typedef Benchmark INHERITED;
egdaniel05bb6f12014-06-04 08:15:31 -0700440};
reed@google.com996f64f2013-01-24 17:17:28 +0000441
reed@google.com4aa1a702012-05-04 16:37:45 +0000442///////////////////////////////////////////////////////////////////////////////
443
444static const SkScalar gDots[] = { SK_Scalar1, SK_Scalar1 };
445
446#define PARAM(array) array, SK_ARRAY_COUNT(array)
447
mtklein@google.com410e6e82013-09-13 19:52:27 +0000448DEF_BENCH( return new DashBench(PARAM(gDots), 0); )
449DEF_BENCH( return new DashBench(PARAM(gDots), 1); )
450DEF_BENCH( return new DashBench(PARAM(gDots), 1, true); )
451DEF_BENCH( return new DashBench(PARAM(gDots), 4); )
452DEF_BENCH( return new MakeDashBench(make_poly, "poly"); )
453DEF_BENCH( return new MakeDashBench(make_quad, "quad"); )
454DEF_BENCH( return new MakeDashBench(make_cubic, "cubic"); )
455DEF_BENCH( return new DashLineBench(0, false); )
456DEF_BENCH( return new DashLineBench(SK_Scalar1, false); )
457DEF_BENCH( return new DashLineBench(2 * SK_Scalar1, false); )
458DEF_BENCH( return new DashLineBench(0, true); )
459DEF_BENCH( return new DashLineBench(SK_Scalar1, true); )
460DEF_BENCH( return new DashLineBench(2 * SK_Scalar1, true); )
reed@google.com4aa1a702012-05-04 16:37:45 +0000461
mtklein@google.com410e6e82013-09-13 19:52:27 +0000462DEF_BENCH( return new DrawPointsDashingBench(1, 1, false); )
463DEF_BENCH( return new DrawPointsDashingBench(1, 1, true); )
464DEF_BENCH( return new DrawPointsDashingBench(3, 1, false); )
465DEF_BENCH( return new DrawPointsDashingBench(3, 1, true); )
466DEF_BENCH( return new DrawPointsDashingBench(5, 5, false); )
467DEF_BENCH( return new DrawPointsDashingBench(5, 5, true); )
reed@google.com996f64f2013-01-24 17:17:28 +0000468
djsollen@google.com69feaca2013-07-18 17:22:30 +0000469/* Disable the GiantDashBench for Android devices until we can better control
470 * the memory usage. (https://code.google.com/p/skia/issues/detail?id=1430)
471 */
472#ifndef SK_BUILD_FOR_ANDROID
mtklein@google.com410e6e82013-09-13 19:52:27 +0000473DEF_BENCH( return new GiantDashBench(GiantDashBench::kHori_LineType, 0); )
474DEF_BENCH( return new GiantDashBench(GiantDashBench::kVert_LineType, 0); )
475DEF_BENCH( return new GiantDashBench(GiantDashBench::kDiag_LineType, 0); )
reed@google.come5ceea92013-01-24 17:33:21 +0000476
477// pass 2 to explicitly avoid any 1-is-the-same-as-hairline special casing
478
479// hori_2 is just too slow to enable at the moment
mtklein@google.com410e6e82013-09-13 19:52:27 +0000480DEF_BENCH( return new GiantDashBench(GiantDashBench::kHori_LineType, 2); )
481DEF_BENCH( return new GiantDashBench(GiantDashBench::kVert_LineType, 2); )
482DEF_BENCH( return new GiantDashBench(GiantDashBench::kDiag_LineType, 2); )
egdaniel05bb6f12014-06-04 08:15:31 -0700483
484DEF_BENCH( return new DashGridBench(1, 1, true); )
485DEF_BENCH( return new DashGridBench(1, 1, false); )
486DEF_BENCH( return new DashGridBench(3, 1, true); )
487DEF_BENCH( return new DashGridBench(3, 1, false); )
djsollen@google.com69feaca2013-07-18 17:22:30 +0000488#endif