blob: 91323b153c8ed7c418ded50f1b86b6dfaa28115e [file] [log] [blame]
reed@google.com4aa1a702012-05-04 16:37:45 +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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "bench/Benchmark.h"
8#include "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkPaint.h"
11#include "include/core/SkPath.h"
12#include "include/core/SkString.h"
13#include "include/core/SkStrokeRec.h"
14#include "include/effects/SkDashPathEffect.h"
15#include "include/private/SkTDArray.h"
16#include "include/utils/SkRandom.h"
reed@google.com4aa1a702012-05-04 16:37:45 +000017
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
mtkleina1ebeb22015-10-01 09:43:39 -070064 void onDraw(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
reeda4393342016-03-18 11:22:57 -070074 paint.setPathEffect(SkDashPathEffect::Make(fIntervals.begin(), fIntervals.count(), 0));
reed@google.com4ad22752012-05-15 19:50:58 +000075
76 if (fDoClip) {
77 SkRect r = path.getBounds();
78 r.inset(-SkIntToScalar(20), -SkIntToScalar(20));
79 // now move it so we don't intersect
80 r.offset(0, r.height() * 3 / 2);
81 canvas->clipRect(r);
82 }
83
commit-bot@chromium.org33614712013-12-03 18:17:16 +000084 this->handlePath(canvas, path, paint, loops);
reed@google.comef85e3c2012-05-10 15:40:57 +000085 }
86
87 virtual void handlePath(SkCanvas* canvas, const SkPath& path,
88 const SkPaint& paint, int N) {
reed@google.com4aa1a702012-05-04 16:37:45 +000089 for (int i = 0; i < N; ++i) {
reed@google.coma584aed2012-05-16 14:06:02 +000090// canvas->drawPoints(SkCanvas::kLines_PointMode, 2, fPts, paint);
reed@google.com4aa1a702012-05-04 16:37:45 +000091 canvas->drawPath(path, paint);
92 }
93 }
94
95private:
John Stiles7571f9e2020-09-02 22:42:33 -040096 using INHERITED = Benchmark;
reed@google.com4aa1a702012-05-04 16:37:45 +000097};
98
reed@google.comef85e3c2012-05-10 15:40:57 +000099class RectDashBench : public DashBench {
100public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000101 RectDashBench(const SkScalar intervals[], int count, int width)
102 : INHERITED(intervals, count, width) {
reed@google.comef85e3c2012-05-10 15:40:57 +0000103 fName.append("_rect");
104 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000105
reed@google.comef85e3c2012-05-10 15:40:57 +0000106protected:
John Stiles1cf2c8d2020-08-13 22:58:04 -0400107 void handlePath(SkCanvas* canvas, const SkPath& path, const SkPaint& paint, int N) override {
reed@google.comef85e3c2012-05-10 15:40:57 +0000108 SkPoint pts[2];
109 if (!path.isLine(pts) || pts[0].fY != pts[1].fY) {
110 this->INHERITED::handlePath(canvas, path, paint, N);
111 } else {
112 SkRect rect;
113 rect.fLeft = pts[0].fX;
114 rect.fTop = pts[0].fY - paint.getStrokeWidth() / 2;
115 rect.fRight = rect.fLeft + SkIntToScalar(fWidth);
116 rect.fBottom = rect.fTop + paint.getStrokeWidth();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000117
reed@google.comef85e3c2012-05-10 15:40:57 +0000118 SkPaint p(paint);
119 p.setStyle(SkPaint::kFill_Style);
halcanary96fcdcc2015-08-27 07:41:13 -0700120 p.setPathEffect(nullptr);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000121
reed@google.comef85e3c2012-05-10 15:40:57 +0000122 int count = SkScalarRoundToInt((pts[1].fX - pts[0].fX) / (2*fWidth));
123 SkScalar dx = SkIntToScalar(2 * fWidth);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000124
reed@google.comef85e3c2012-05-10 15:40:57 +0000125 for (int i = 0; i < N*10; ++i) {
126 SkRect r = rect;
127 for (int j = 0; j < count; ++j) {
128 canvas->drawRect(r, p);
129 r.offset(dx, 0);
130 }
131 }
132 }
133 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000134
reed@google.comef85e3c2012-05-10 15:40:57 +0000135private:
John Stiles7571f9e2020-09-02 22:42:33 -0400136 using INHERITED = DashBench;
reed@google.comef85e3c2012-05-10 15:40:57 +0000137};
138
reed@google.comea6f6832012-05-18 18:32:54 +0000139static void make_unit_star(SkPath* path, int n) {
140 SkScalar rad = -SK_ScalarPI / 2;
141 const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000142
reed@google.comea6f6832012-05-18 18:32:54 +0000143 path->moveTo(0, -SK_Scalar1);
144 for (int i = 1; i < n; i++) {
145 rad += drad;
Brian Osman4428f2c2019-04-02 10:59:28 -0400146 path->lineTo(SkScalarCos(rad), SkScalarSin(rad));
reed@google.comea6f6832012-05-18 18:32:54 +0000147 }
148 path->close();
149}
150
151static void make_poly(SkPath* path) {
152 make_unit_star(path, 9);
Mike Reed1f607332020-05-21 12:11:27 -0400153 const SkMatrix matrix = SkMatrix::Scale(100, 100);
reed@google.comea6f6832012-05-18 18:32:54 +0000154 path->transform(matrix);
155}
156
157static void make_quad(SkPath* path) {
158 SkScalar x0 = SkIntToScalar(10);
159 SkScalar y0 = SkIntToScalar(10);
160 path->moveTo(x0, y0);
161 path->quadTo(x0, y0 + 400 * SK_Scalar1,
162 x0 + 600 * SK_Scalar1, y0 + 400 * SK_Scalar1);
163}
164
165static void make_cubic(SkPath* path) {
166 SkScalar x0 = SkIntToScalar(10);
167 SkScalar y0 = SkIntToScalar(10);
168 path->moveTo(x0, y0);
169 path->cubicTo(x0, y0 + 400 * SK_Scalar1,
170 x0 + 600 * SK_Scalar1, y0 + 400 * SK_Scalar1,
171 x0 + 600 * SK_Scalar1, y0);
172}
173
tfarinaf168b862014-06-19 12:32:29 -0700174class MakeDashBench : public Benchmark {
reed@google.comea6f6832012-05-18 18:32:54 +0000175 SkString fName;
176 SkPath fPath;
reeda4393342016-03-18 11:22:57 -0700177 sk_sp<SkPathEffect> fPE;
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000178
reed@google.comea6f6832012-05-18 18:32:54 +0000179public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000180 MakeDashBench(void (*proc)(SkPath*), const char name[]) {
reed@google.comea6f6832012-05-18 18:32:54 +0000181 fName.printf("makedash_%s", name);
182 proc(&fPath);
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000183
reed@google.comea6f6832012-05-18 18:32:54 +0000184 SkScalar vals[] = { SkIntToScalar(4), SkIntToScalar(4) };
reeda4393342016-03-18 11:22:57 -0700185 fPE = SkDashPathEffect::Make(vals, 2, 0);
reed@google.comea6f6832012-05-18 18:32:54 +0000186 }
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000187
reed@google.comea6f6832012-05-18 18:32:54 +0000188protected:
mtklein36352bf2015-03-25 18:17:31 -0700189 const char* onGetName() override {
reed@google.comea6f6832012-05-18 18:32:54 +0000190 return fName.c_str();
191 }
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000192
mtkleina1ebeb22015-10-01 09:43:39 -0700193 void onDraw(int loops, SkCanvas*) override {
reed@google.comea6f6832012-05-18 18:32:54 +0000194 SkPath dst;
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000195 for (int i = 0; i < loops; ++i) {
reed@google.comfd4be262012-05-25 01:04:12 +0000196 SkStrokeRec rec(SkStrokeRec::kHairline_InitStyle);
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000197
halcanary96fcdcc2015-08-27 07:41:13 -0700198 fPE->filterPath(&dst, fPath, &rec, nullptr);
reed@google.comea6f6832012-05-18 18:32:54 +0000199 dst.rewind();
200 }
201 }
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000202
reed@google.com31e9d642012-09-04 20:07:23 +0000203private:
John Stiles7571f9e2020-09-02 22:42:33 -0400204 using INHERITED = Benchmark;
reed@google.com31e9d642012-09-04 20:07:23 +0000205};
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000206
reed@google.com5125d842012-09-04 20:19:35 +0000207/*
208 * We try to special case square dashes (intervals are equal to strokewidth).
209 */
tfarinaf168b862014-06-19 12:32:29 -0700210class DashLineBench : public Benchmark {
reed@google.com31e9d642012-09-04 20:07:23 +0000211 SkString fName;
reed@google.com5125d842012-09-04 20:19:35 +0000212 SkScalar fStrokeWidth;
213 bool fIsRound;
reeda4393342016-03-18 11:22:57 -0700214 sk_sp<SkPathEffect> fPE;
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000215
reed@google.com31e9d642012-09-04 20:07:23 +0000216public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000217 DashLineBench(SkScalar width, bool isRound) {
reed@google.com5125d842012-09-04 20:19:35 +0000218 fName.printf("dashline_%g_%s", SkScalarToFloat(width), isRound ? "circle" : "square");
219 fStrokeWidth = width;
220 fIsRound = isRound;
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000221
reed@google.com5125d842012-09-04 20:19:35 +0000222 SkScalar vals[] = { SK_Scalar1, SK_Scalar1 };
reeda4393342016-03-18 11:22:57 -0700223 fPE = SkDashPathEffect::Make(vals, 2, 0);
reed@google.com31e9d642012-09-04 20:07:23 +0000224 }
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000225
reed@google.com31e9d642012-09-04 20:07:23 +0000226protected:
mtklein36352bf2015-03-25 18:17:31 -0700227 const char* onGetName() override {
reed@google.com31e9d642012-09-04 20:07:23 +0000228 return fName.c_str();
229 }
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000230
mtkleina1ebeb22015-10-01 09:43:39 -0700231 void onDraw(int loops, SkCanvas* canvas) override {
reed@google.com31e9d642012-09-04 20:07:23 +0000232 SkPaint paint;
233 this->setupPaint(&paint);
reed@google.com5125d842012-09-04 20:19:35 +0000234 paint.setStrokeWidth(fStrokeWidth);
235 paint.setStrokeCap(fIsRound ? SkPaint::kRound_Cap : SkPaint::kSquare_Cap);
reed@google.com31e9d642012-09-04 20:07:23 +0000236 paint.setPathEffect(fPE);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000237 for (int i = 0; i < loops; ++i) {
reed@google.com5125d842012-09-04 20:19:35 +0000238 canvas->drawLine(10 * SK_Scalar1, 10 * SK_Scalar1,
239 640 * SK_Scalar1, 10 * SK_Scalar1, paint);
reed@google.com31e9d642012-09-04 20:07:23 +0000240 }
241 }
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000242
reed@google.comea6f6832012-05-18 18:32:54 +0000243private:
John Stiles7571f9e2020-09-02 22:42:33 -0400244 using INHERITED = Benchmark;
reed@google.comea6f6832012-05-18 18:32:54 +0000245};
246
tfarinaf168b862014-06-19 12:32:29 -0700247class DrawPointsDashingBench : public Benchmark {
robertphillips@google.com935ad022012-12-05 19:07:21 +0000248 SkString fName;
249 int fStrokeWidth;
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000250 bool fDoAA;
robertphillips@google.com935ad022012-12-05 19:07:21 +0000251
reeda4393342016-03-18 11:22:57 -0700252 sk_sp<SkPathEffect> fPathEffect;
robertphillips@google.com935ad022012-12-05 19:07:21 +0000253
robertphillips@google.com935ad022012-12-05 19:07:21 +0000254public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000255 DrawPointsDashingBench(int dashLength, int strokeWidth, bool doAA)
256 {
robertphillips@google.com935ad022012-12-05 19:07:21 +0000257 fName.printf("drawpointsdash_%d_%d%s", dashLength, strokeWidth, doAA ? "_aa" : "_bw");
258 fStrokeWidth = strokeWidth;
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000259 fDoAA = doAA;
robertphillips@google.com935ad022012-12-05 19:07:21 +0000260
261 SkScalar vals[] = { SkIntToScalar(dashLength), SkIntToScalar(dashLength) };
reeda4393342016-03-18 11:22:57 -0700262 fPathEffect = SkDashPathEffect::Make(vals, 2, SK_Scalar1);
robertphillips@google.com935ad022012-12-05 19:07:21 +0000263 }
264
265protected:
mtklein36352bf2015-03-25 18:17:31 -0700266 const char* onGetName() override {
robertphillips@google.com935ad022012-12-05 19:07:21 +0000267 return fName.c_str();
268 }
269
mtkleina1ebeb22015-10-01 09:43:39 -0700270 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips@google.com935ad022012-12-05 19:07:21 +0000271 SkPaint p;
272 this->setupPaint(&p);
273 p.setColor(SK_ColorBLACK);
274 p.setStyle(SkPaint::kStroke_Style);
275 p.setStrokeWidth(SkIntToScalar(fStrokeWidth));
276 p.setPathEffect(fPathEffect);
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000277 p.setAntiAlias(fDoAA);
robertphillips@google.com935ad022012-12-05 19:07:21 +0000278
279 SkPoint pts[2] = {
280 { SkIntToScalar(10), 0 },
281 { SkIntToScalar(640), 0 }
282 };
283
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000284 for (int i = 0; i < loops; ++i) {
robertphillips@google.com935ad022012-12-05 19:07:21 +0000285 pts[0].fY = pts[1].fY = SkIntToScalar(i % 480);
286 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p);
287 }
288 }
289
290private:
John Stiles7571f9e2020-09-02 22:42:33 -0400291 using INHERITED = Benchmark;
robertphillips@google.com935ad022012-12-05 19:07:21 +0000292};
reed@google.com996f64f2013-01-24 17:17:28 +0000293
reed@google.come5ceea92013-01-24 17:33:21 +0000294// Want to test how we handle dashing when 99% of the dash is clipped out
tfarinaf168b862014-06-19 12:32:29 -0700295class GiantDashBench : public Benchmark {
reed@google.com996f64f2013-01-24 17:17:28 +0000296 SkString fName;
reed@google.come5ceea92013-01-24 17:33:21 +0000297 SkScalar fStrokeWidth;
reed@google.com996f64f2013-01-24 17:17:28 +0000298 SkPoint fPts[2];
reeda4393342016-03-18 11:22:57 -0700299 sk_sp<SkPathEffect> fPathEffect;
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000300
reed@google.com996f64f2013-01-24 17:17:28 +0000301public:
302 enum LineType {
303 kHori_LineType,
304 kVert_LineType,
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +0000305 kDiag_LineType,
306 kLineTypeCount
reed@google.com996f64f2013-01-24 17:17:28 +0000307 };
308
309 static const char* LineTypeName(LineType lt) {
310 static const char* gNames[] = { "hori", "vert", "diag" };
bungeman99fe8222015-08-20 07:57:51 -0700311 static_assert(kLineTypeCount == SK_ARRAY_COUNT(gNames), "names_wrong_size");
reed@google.com996f64f2013-01-24 17:17:28 +0000312 return gNames[lt];
313 }
314
mtklein@google.com410e6e82013-09-13 19:52:27 +0000315 GiantDashBench(LineType lt, SkScalar width) {
reed@google.come5ceea92013-01-24 17:33:21 +0000316 fName.printf("giantdashline_%s_%g", LineTypeName(lt), width);
317 fStrokeWidth = width;
318
reed@google.com996f64f2013-01-24 17:17:28 +0000319 // deliberately pick intervals that won't be caught by asPoints(), so
320 // we can test the filterPath code-path.
commit-bot@chromium.orgae4976c2014-05-01 15:30:52 +0000321 const SkScalar intervals[] = { 20, 10, 10, 10 };
reeda4393342016-03-18 11:22:57 -0700322 fPathEffect = SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0);
reed@google.com996f64f2013-01-24 17:17:28 +0000323
324 SkScalar cx = 640 / 2; // center X
325 SkScalar cy = 480 / 2; // center Y
326 SkMatrix matrix;
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000327
reed@google.com996f64f2013-01-24 17:17:28 +0000328 switch (lt) {
329 case kHori_LineType:
330 matrix.setIdentity();
331 break;
332 case kVert_LineType:
333 matrix.setRotate(90, cx, cy);
334 break;
335 case kDiag_LineType:
336 matrix.setRotate(45, cx, cy);
337 break;
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +0000338 case kLineTypeCount:
339 // Not a real enum value.
340 break;
reed@google.com996f64f2013-01-24 17:17:28 +0000341 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000342
reed@google.come5ceea92013-01-24 17:33:21 +0000343 const SkScalar overshoot = 100*1000;
reed@google.com996f64f2013-01-24 17:17:28 +0000344 const SkPoint pts[2] = {
345 { -overshoot, cy }, { 640 + overshoot, cy }
346 };
347 matrix.mapPoints(fPts, pts, 2);
348 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000349
reed@google.com996f64f2013-01-24 17:17:28 +0000350protected:
mtklein36352bf2015-03-25 18:17:31 -0700351 const char* onGetName() override {
reed@google.com996f64f2013-01-24 17:17:28 +0000352 return fName.c_str();
353 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000354
mtkleina1ebeb22015-10-01 09:43:39 -0700355 void onDraw(int loops, SkCanvas* canvas) override {
reed@google.com996f64f2013-01-24 17:17:28 +0000356 SkPaint p;
357 this->setupPaint(&p);
358 p.setStyle(SkPaint::kStroke_Style);
reed@google.come5ceea92013-01-24 17:33:21 +0000359 p.setStrokeWidth(fStrokeWidth);
reed@google.com996f64f2013-01-24 17:17:28 +0000360 p.setPathEffect(fPathEffect);
361
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000362 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000363 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, fPts, p);
364 }
reed@google.com996f64f2013-01-24 17:17:28 +0000365 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000366
reed@google.com996f64f2013-01-24 17:17:28 +0000367private:
John Stiles7571f9e2020-09-02 22:42:33 -0400368 using INHERITED = Benchmark;
reed@google.com996f64f2013-01-24 17:17:28 +0000369};
370
egdaniel05bb6f12014-06-04 08:15:31 -0700371// Want to test how we draw a dashed grid (like what is used in spreadsheets) of many
372// small dashed lines switching back and forth between horizontal and vertical
tfarinaf168b862014-06-19 12:32:29 -0700373class DashGridBench : public Benchmark {
egdaniel05bb6f12014-06-04 08:15:31 -0700374 SkString fName;
375 int fStrokeWidth;
376 bool fDoAA;
377
reeda4393342016-03-18 11:22:57 -0700378 sk_sp<SkPathEffect> fPathEffect;
egdaniel05bb6f12014-06-04 08:15:31 -0700379
380public:
381 DashGridBench(int dashLength, int strokeWidth, bool doAA) {
382 fName.printf("dashgrid_%d_%d%s", dashLength, strokeWidth, doAA ? "_aa" : "_bw");
383 fStrokeWidth = strokeWidth;
384 fDoAA = doAA;
385
386 SkScalar vals[] = { SkIntToScalar(dashLength), SkIntToScalar(dashLength) };
reeda4393342016-03-18 11:22:57 -0700387 fPathEffect = SkDashPathEffect::Make(vals, 2, SK_Scalar1);
egdaniel05bb6f12014-06-04 08:15:31 -0700388 }
389
390protected:
mtklein36352bf2015-03-25 18:17:31 -0700391 const char* onGetName() override {
egdaniel05bb6f12014-06-04 08:15:31 -0700392 return fName.c_str();
393 }
394
mtkleina1ebeb22015-10-01 09:43:39 -0700395 void onDraw(int loops, SkCanvas* canvas) override {
egdaniel05bb6f12014-06-04 08:15:31 -0700396 SkPaint p;
397 this->setupPaint(&p);
398 p.setColor(SK_ColorBLACK);
399 p.setStyle(SkPaint::kStroke_Style);
400 p.setStrokeWidth(SkIntToScalar(fStrokeWidth));
401 p.setPathEffect(fPathEffect);
402 p.setAntiAlias(fDoAA);
403
404 SkPoint pts[4] = {
405 { SkIntToScalar(0), 20.5f },
406 { SkIntToScalar(20), 20.5f },
407 { 20.5f, SkIntToScalar(0) },
408 { 20.5f, SkIntToScalar(20) }
409 };
410
411 for (int i = 0; i < loops; ++i) {
412 for (int j = 0; j < 10; ++j) {
413 for (int k = 0; k < 10; ++k) {
414 // Horizontal line
415 SkPoint horPts[2];
416 horPts[0].fX = pts[0].fX + k * 22.f;
417 horPts[0].fY = pts[0].fY + j * 22.f;
418 horPts[1].fX = pts[1].fX + k * 22.f;
419 horPts[1].fY = pts[1].fY + j * 22.f;
420 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, horPts, p);
421
422 // Vertical line
423 SkPoint vertPts[2];
424 vertPts[0].fX = pts[2].fX + k * 22.f;
425 vertPts[0].fY = pts[2].fY + j * 22.f;
426 vertPts[1].fX = pts[3].fX + k * 22.f;
427 vertPts[1].fY = pts[3].fY + j * 22.f;
428 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, vertPts, p);
429 }
430 }
431 }
432 }
433
434private:
John Stiles7571f9e2020-09-02 22:42:33 -0400435 using INHERITED = Benchmark;
egdaniel05bb6f12014-06-04 08:15:31 -0700436};
reed@google.com996f64f2013-01-24 17:17:28 +0000437
reed@google.com4aa1a702012-05-04 16:37:45 +0000438///////////////////////////////////////////////////////////////////////////////
439
440static const SkScalar gDots[] = { SK_Scalar1, SK_Scalar1 };
441
442#define PARAM(array) array, SK_ARRAY_COUNT(array)
443
mtklein@google.com410e6e82013-09-13 19:52:27 +0000444DEF_BENCH( return new DashBench(PARAM(gDots), 0); )
445DEF_BENCH( return new DashBench(PARAM(gDots), 1); )
446DEF_BENCH( return new DashBench(PARAM(gDots), 1, true); )
447DEF_BENCH( return new DashBench(PARAM(gDots), 4); )
448DEF_BENCH( return new MakeDashBench(make_poly, "poly"); )
449DEF_BENCH( return new MakeDashBench(make_quad, "quad"); )
450DEF_BENCH( return new MakeDashBench(make_cubic, "cubic"); )
451DEF_BENCH( return new DashLineBench(0, false); )
452DEF_BENCH( return new DashLineBench(SK_Scalar1, false); )
453DEF_BENCH( return new DashLineBench(2 * SK_Scalar1, false); )
454DEF_BENCH( return new DashLineBench(0, true); )
455DEF_BENCH( return new DashLineBench(SK_Scalar1, true); )
456DEF_BENCH( return new DashLineBench(2 * SK_Scalar1, true); )
reed@google.com4aa1a702012-05-04 16:37:45 +0000457
mtklein@google.com410e6e82013-09-13 19:52:27 +0000458DEF_BENCH( return new DrawPointsDashingBench(1, 1, false); )
459DEF_BENCH( return new DrawPointsDashingBench(1, 1, true); )
460DEF_BENCH( return new DrawPointsDashingBench(3, 1, false); )
461DEF_BENCH( return new DrawPointsDashingBench(3, 1, true); )
462DEF_BENCH( return new DrawPointsDashingBench(5, 5, false); )
463DEF_BENCH( return new DrawPointsDashingBench(5, 5, true); )
reed@google.com996f64f2013-01-24 17:17:28 +0000464
djsollen@google.com69feaca2013-07-18 17:22:30 +0000465/* Disable the GiantDashBench for Android devices until we can better control
466 * the memory usage. (https://code.google.com/p/skia/issues/detail?id=1430)
467 */
468#ifndef SK_BUILD_FOR_ANDROID
mtklein@google.com410e6e82013-09-13 19:52:27 +0000469DEF_BENCH( return new GiantDashBench(GiantDashBench::kHori_LineType, 0); )
470DEF_BENCH( return new GiantDashBench(GiantDashBench::kVert_LineType, 0); )
471DEF_BENCH( return new GiantDashBench(GiantDashBench::kDiag_LineType, 0); )
reed@google.come5ceea92013-01-24 17:33:21 +0000472
473// pass 2 to explicitly avoid any 1-is-the-same-as-hairline special casing
474
475// hori_2 is just too slow to enable at the moment
mtklein@google.com410e6e82013-09-13 19:52:27 +0000476DEF_BENCH( return new GiantDashBench(GiantDashBench::kHori_LineType, 2); )
477DEF_BENCH( return new GiantDashBench(GiantDashBench::kVert_LineType, 2); )
478DEF_BENCH( return new GiantDashBench(GiantDashBench::kDiag_LineType, 2); )
egdaniel05bb6f12014-06-04 08:15:31 -0700479
480DEF_BENCH( return new DashGridBench(1, 1, true); )
481DEF_BENCH( return new DashGridBench(1, 1, false); )
482DEF_BENCH( return new DashGridBench(3, 1, true); )
483DEF_BENCH( return new DashGridBench(3, 1, false); )
djsollen@google.com69feaca2013-07-18 17:22:30 +0000484#endif