blob: eb964f6bf10c2a76ac21c078b3216b0f49919de1 [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"
halcanary435657f2015-09-15 12:53:07 -070016#include "SkStrokeRec.h"
reed@google.com4aa1a702012-05-04 16:37:45 +000017#include "SkTDArray.h"
18
reed@google.comef85e3c2012-05-10 15:40:57 +000019
reed@google.com4aa1a702012-05-04 16:37:45 +000020/*
21 * Cases to consider:
22 *
23 * 1. antialiasing on/off (esp. width <= 1)
24 * 2. strokewidth == 0, 1, 2
25 * 3. hline, vline, diagonal, rect, oval
26 * 4. dots [1,1] ([N,N] where N=strokeWidth?) or arbitrary (e.g. [2,1] or [1,2,3,2])
27 */
28static void path_hline(SkPath* path) {
29 path->moveTo(SkIntToScalar(10), SkIntToScalar(10));
30 path->lineTo(SkIntToScalar(600), SkIntToScalar(10));
31}
32
tfarinaf168b862014-06-19 12:32:29 -070033class DashBench : public Benchmark {
reed@google.comef85e3c2012-05-10 15:40:57 +000034protected:
mike@reedtribe.org99a6ef42012-05-05 13:46:10 +000035 SkString fName;
reed@google.com4aa1a702012-05-04 16:37:45 +000036 SkTDArray<SkScalar> fIntervals;
mike@reedtribe.org99a6ef42012-05-05 13:46:10 +000037 int fWidth;
reed@google.coma584aed2012-05-16 14:06:02 +000038 SkPoint fPts[2];
39 bool fDoClip;
reed@google.com4aa1a702012-05-04 16:37:45 +000040
reed@google.com4aa1a702012-05-04 16:37:45 +000041public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000042 DashBench(const SkScalar intervals[], int count, int width,
43 bool doClip = false) {
reed@google.com4aa1a702012-05-04 16:37:45 +000044 fIntervals.append(count, intervals);
mike@reedtribe.org99a6ef42012-05-05 13:46:10 +000045 for (int i = 0; i < count; ++i) {
46 fIntervals[i] *= width;
47 }
48 fWidth = width;
reed@google.com4ad22752012-05-15 19:50:58 +000049 fName.printf("dash_%d_%s", width, doClip ? "clipped" : "noclip");
50 fDoClip = doClip;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000051
reed@google.coma584aed2012-05-16 14:06:02 +000052 fPts[0].set(SkIntToScalar(10), SkIntToScalar(10));
53 fPts[1].set(SkIntToScalar(600), SkIntToScalar(10));
reed@google.com4aa1a702012-05-04 16:37:45 +000054 }
55
56 virtual void makePath(SkPath* path) {
57 path_hline(path);
58 }
59
60protected:
mtklein36352bf2015-03-25 18:17:31 -070061 const char* onGetName() override {
mike@reedtribe.org99a6ef42012-05-05 13:46:10 +000062 return fName.c_str();
reed@google.com4aa1a702012-05-04 16:37:45 +000063 }
64
mtkleina1ebeb22015-10-01 09:43:39 -070065 void onDraw(int loops, SkCanvas* canvas) override {
reed@google.com4aa1a702012-05-04 16:37:45 +000066 SkPaint paint;
67 this->setupPaint(&paint);
68 paint.setStyle(SkPaint::kStroke_Style);
mike@reedtribe.org99a6ef42012-05-05 13:46:10 +000069 paint.setStrokeWidth(SkIntToScalar(fWidth));
reed@google.com4aa1a702012-05-04 16:37:45 +000070 paint.setAntiAlias(false);
71
72 SkPath path;
73 this->makePath(&path);
74
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +000075 paint.setPathEffect(SkDashPathEffect::Create(fIntervals.begin(),
76 fIntervals.count(), 0))->unref();
reed@google.com4ad22752012-05-15 19:50:58 +000077
78 if (fDoClip) {
79 SkRect r = path.getBounds();
80 r.inset(-SkIntToScalar(20), -SkIntToScalar(20));
81 // now move it so we don't intersect
82 r.offset(0, r.height() * 3 / 2);
83 canvas->clipRect(r);
84 }
85
commit-bot@chromium.org33614712013-12-03 18:17:16 +000086 this->handlePath(canvas, path, paint, loops);
reed@google.comef85e3c2012-05-10 15:40:57 +000087 }
88
89 virtual void handlePath(SkCanvas* canvas, const SkPath& path,
90 const SkPaint& paint, int N) {
reed@google.com4aa1a702012-05-04 16:37:45 +000091 for (int i = 0; i < N; ++i) {
reed@google.coma584aed2012-05-16 14:06:02 +000092// canvas->drawPoints(SkCanvas::kLines_PointMode, 2, fPts, paint);
reed@google.com4aa1a702012-05-04 16:37:45 +000093 canvas->drawPath(path, paint);
94 }
95 }
96
97private:
tfarinaf168b862014-06-19 12:32:29 -070098 typedef Benchmark INHERITED;
reed@google.com4aa1a702012-05-04 16:37:45 +000099};
100
reed@google.comef85e3c2012-05-10 15:40:57 +0000101class RectDashBench : public DashBench {
102public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000103 RectDashBench(const SkScalar intervals[], int count, int width)
104 : INHERITED(intervals, count, width) {
reed@google.comef85e3c2012-05-10 15:40:57 +0000105 fName.append("_rect");
106 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000107
reed@google.comef85e3c2012-05-10 15:40:57 +0000108protected:
109 virtual void handlePath(SkCanvas* canvas, const SkPath& path,
mtklein36352bf2015-03-25 18:17:31 -0700110 const SkPaint& paint, int N) override {
reed@google.comef85e3c2012-05-10 15:40:57 +0000111 SkPoint pts[2];
112 if (!path.isLine(pts) || pts[0].fY != pts[1].fY) {
113 this->INHERITED::handlePath(canvas, path, paint, N);
114 } else {
115 SkRect rect;
116 rect.fLeft = pts[0].fX;
117 rect.fTop = pts[0].fY - paint.getStrokeWidth() / 2;
118 rect.fRight = rect.fLeft + SkIntToScalar(fWidth);
119 rect.fBottom = rect.fTop + paint.getStrokeWidth();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000120
reed@google.comef85e3c2012-05-10 15:40:57 +0000121 SkPaint p(paint);
122 p.setStyle(SkPaint::kFill_Style);
halcanary96fcdcc2015-08-27 07:41:13 -0700123 p.setPathEffect(nullptr);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000124
reed@google.comef85e3c2012-05-10 15:40:57 +0000125 int count = SkScalarRoundToInt((pts[1].fX - pts[0].fX) / (2*fWidth));
126 SkScalar dx = SkIntToScalar(2 * fWidth);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000127
reed@google.comef85e3c2012-05-10 15:40:57 +0000128 for (int i = 0; i < N*10; ++i) {
129 SkRect r = rect;
130 for (int j = 0; j < count; ++j) {
131 canvas->drawRect(r, p);
132 r.offset(dx, 0);
133 }
134 }
135 }
136 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000137
reed@google.comef85e3c2012-05-10 15:40:57 +0000138private:
139 typedef DashBench INHERITED;
140};
141
reed@google.comea6f6832012-05-18 18:32:54 +0000142static void make_unit_star(SkPath* path, int n) {
143 SkScalar rad = -SK_ScalarPI / 2;
144 const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000145
reed@google.comea6f6832012-05-18 18:32:54 +0000146 path->moveTo(0, -SK_Scalar1);
147 for (int i = 1; i < n; i++) {
148 rad += drad;
149 SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV);
150 path->lineTo(cosV, sinV);
151 }
152 path->close();
153}
154
155static void make_poly(SkPath* path) {
156 make_unit_star(path, 9);
robertphillips1d24b8d2015-03-26 19:57:08 -0700157 const SkMatrix matrix = SkMatrix::MakeScale(SkIntToScalar(100), SkIntToScalar(100));
reed@google.comea6f6832012-05-18 18:32:54 +0000158 path->transform(matrix);
159}
160
161static void make_quad(SkPath* path) {
162 SkScalar x0 = SkIntToScalar(10);
163 SkScalar y0 = SkIntToScalar(10);
164 path->moveTo(x0, y0);
165 path->quadTo(x0, y0 + 400 * SK_Scalar1,
166 x0 + 600 * SK_Scalar1, y0 + 400 * SK_Scalar1);
167}
168
169static void make_cubic(SkPath* path) {
170 SkScalar x0 = SkIntToScalar(10);
171 SkScalar y0 = SkIntToScalar(10);
172 path->moveTo(x0, y0);
173 path->cubicTo(x0, y0 + 400 * SK_Scalar1,
174 x0 + 600 * SK_Scalar1, y0 + 400 * SK_Scalar1,
175 x0 + 600 * SK_Scalar1, y0);
176}
177
tfarinaf168b862014-06-19 12:32:29 -0700178class MakeDashBench : public Benchmark {
reed@google.comea6f6832012-05-18 18:32:54 +0000179 SkString fName;
180 SkPath fPath;
181 SkAutoTUnref<SkPathEffect> fPE;
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000182
reed@google.comea6f6832012-05-18 18:32:54 +0000183public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000184 MakeDashBench(void (*proc)(SkPath*), const char name[]) {
reed@google.comea6f6832012-05-18 18:32:54 +0000185 fName.printf("makedash_%s", name);
186 proc(&fPath);
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000187
reed@google.comea6f6832012-05-18 18:32:54 +0000188 SkScalar vals[] = { SkIntToScalar(4), SkIntToScalar(4) };
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000189 fPE.reset(SkDashPathEffect::Create(vals, 2, 0));
reed@google.comea6f6832012-05-18 18:32:54 +0000190 }
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000191
reed@google.comea6f6832012-05-18 18:32:54 +0000192protected:
mtklein36352bf2015-03-25 18:17:31 -0700193 const char* onGetName() override {
reed@google.comea6f6832012-05-18 18:32:54 +0000194 return fName.c_str();
195 }
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000196
mtkleina1ebeb22015-10-01 09:43:39 -0700197 void onDraw(int loops, SkCanvas*) override {
reed@google.comea6f6832012-05-18 18:32:54 +0000198 SkPath dst;
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000199 for (int i = 0; i < loops; ++i) {
reed@google.comfd4be262012-05-25 01:04:12 +0000200 SkStrokeRec rec(SkStrokeRec::kHairline_InitStyle);
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000201
halcanary96fcdcc2015-08-27 07:41:13 -0700202 fPE->filterPath(&dst, fPath, &rec, nullptr);
reed@google.comea6f6832012-05-18 18:32:54 +0000203 dst.rewind();
204 }
205 }
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000206
reed@google.com31e9d642012-09-04 20:07:23 +0000207private:
tfarinaf168b862014-06-19 12:32:29 -0700208 typedef Benchmark INHERITED;
reed@google.com31e9d642012-09-04 20:07:23 +0000209};
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000210
reed@google.com5125d842012-09-04 20:19:35 +0000211/*
212 * We try to special case square dashes (intervals are equal to strokewidth).
213 */
tfarinaf168b862014-06-19 12:32:29 -0700214class DashLineBench : public Benchmark {
reed@google.com31e9d642012-09-04 20:07:23 +0000215 SkString fName;
reed@google.com5125d842012-09-04 20:19:35 +0000216 SkScalar fStrokeWidth;
217 bool fIsRound;
reed@google.com31e9d642012-09-04 20:07:23 +0000218 SkAutoTUnref<SkPathEffect> fPE;
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000219
reed@google.com31e9d642012-09-04 20:07:23 +0000220public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000221 DashLineBench(SkScalar width, bool isRound) {
reed@google.com5125d842012-09-04 20:19:35 +0000222 fName.printf("dashline_%g_%s", SkScalarToFloat(width), isRound ? "circle" : "square");
223 fStrokeWidth = width;
224 fIsRound = isRound;
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000225
reed@google.com5125d842012-09-04 20:19:35 +0000226 SkScalar vals[] = { SK_Scalar1, SK_Scalar1 };
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000227 fPE.reset(SkDashPathEffect::Create(vals, 2, 0));
reed@google.com31e9d642012-09-04 20:07:23 +0000228 }
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000229
reed@google.com31e9d642012-09-04 20:07:23 +0000230protected:
mtklein36352bf2015-03-25 18:17:31 -0700231 const char* onGetName() override {
reed@google.com31e9d642012-09-04 20:07:23 +0000232 return fName.c_str();
233 }
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000234
mtkleina1ebeb22015-10-01 09:43:39 -0700235 void onDraw(int loops, SkCanvas* canvas) override {
reed@google.com31e9d642012-09-04 20:07:23 +0000236 SkPaint paint;
237 this->setupPaint(&paint);
reed@google.com5125d842012-09-04 20:19:35 +0000238 paint.setStrokeWidth(fStrokeWidth);
239 paint.setStrokeCap(fIsRound ? SkPaint::kRound_Cap : SkPaint::kSquare_Cap);
reed@google.com31e9d642012-09-04 20:07:23 +0000240 paint.setPathEffect(fPE);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000241 for (int i = 0; i < loops; ++i) {
reed@google.com5125d842012-09-04 20:19:35 +0000242 canvas->drawLine(10 * SK_Scalar1, 10 * SK_Scalar1,
243 640 * SK_Scalar1, 10 * SK_Scalar1, paint);
reed@google.com31e9d642012-09-04 20:07:23 +0000244 }
245 }
skia.committer@gmail.com73bb3be2012-09-05 02:01:29 +0000246
reed@google.comea6f6832012-05-18 18:32:54 +0000247private:
tfarinaf168b862014-06-19 12:32:29 -0700248 typedef Benchmark INHERITED;
reed@google.comea6f6832012-05-18 18:32:54 +0000249};
250
tfarinaf168b862014-06-19 12:32:29 -0700251class DrawPointsDashingBench : public Benchmark {
robertphillips@google.com935ad022012-12-05 19:07:21 +0000252 SkString fName;
253 int fStrokeWidth;
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000254 bool fDoAA;
robertphillips@google.com935ad022012-12-05 19:07:21 +0000255
256 SkAutoTUnref<SkPathEffect> fPathEffect;
257
robertphillips@google.com935ad022012-12-05 19:07:21 +0000258public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000259 DrawPointsDashingBench(int dashLength, int strokeWidth, bool doAA)
260 {
robertphillips@google.com935ad022012-12-05 19:07:21 +0000261 fName.printf("drawpointsdash_%d_%d%s", dashLength, strokeWidth, doAA ? "_aa" : "_bw");
262 fStrokeWidth = strokeWidth;
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000263 fDoAA = doAA;
robertphillips@google.com935ad022012-12-05 19:07:21 +0000264
265 SkScalar vals[] = { SkIntToScalar(dashLength), SkIntToScalar(dashLength) };
commit-bot@chromium.org35c03fb2014-03-31 18:52:51 +0000266 fPathEffect.reset(SkDashPathEffect::Create(vals, 2, SK_Scalar1));
robertphillips@google.com935ad022012-12-05 19:07:21 +0000267 }
268
269protected:
mtklein36352bf2015-03-25 18:17:31 -0700270 const char* onGetName() override {
robertphillips@google.com935ad022012-12-05 19:07:21 +0000271 return fName.c_str();
272 }
273
mtkleina1ebeb22015-10-01 09:43:39 -0700274 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips@google.com935ad022012-12-05 19:07:21 +0000275 SkPaint p;
276 this->setupPaint(&p);
277 p.setColor(SK_ColorBLACK);
278 p.setStyle(SkPaint::kStroke_Style);
279 p.setStrokeWidth(SkIntToScalar(fStrokeWidth));
280 p.setPathEffect(fPathEffect);
robertphillips@google.comca47aae2012-12-12 15:58:25 +0000281 p.setAntiAlias(fDoAA);
robertphillips@google.com935ad022012-12-05 19:07:21 +0000282
283 SkPoint pts[2] = {
284 { SkIntToScalar(10), 0 },
285 { SkIntToScalar(640), 0 }
286 };
287
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000288 for (int i = 0; i < loops; ++i) {
robertphillips@google.com935ad022012-12-05 19:07:21 +0000289 pts[0].fY = pts[1].fY = SkIntToScalar(i % 480);
290 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p);
291 }
292 }
293
294private:
tfarinaf168b862014-06-19 12:32:29 -0700295 typedef Benchmark INHERITED;
robertphillips@google.com935ad022012-12-05 19:07:21 +0000296};
reed@google.com996f64f2013-01-24 17:17:28 +0000297
reed@google.come5ceea92013-01-24 17:33:21 +0000298// Want to test how we handle dashing when 99% of the dash is clipped out
tfarinaf168b862014-06-19 12:32:29 -0700299class GiantDashBench : public Benchmark {
reed@google.com996f64f2013-01-24 17:17:28 +0000300 SkString fName;
reed@google.come5ceea92013-01-24 17:33:21 +0000301 SkScalar fStrokeWidth;
reed@google.com996f64f2013-01-24 17:17:28 +0000302 SkPoint fPts[2];
303 SkAutoTUnref<SkPathEffect> fPathEffect;
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000304
reed@google.com996f64f2013-01-24 17:17:28 +0000305public:
306 enum LineType {
307 kHori_LineType,
308 kVert_LineType,
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +0000309 kDiag_LineType,
310 kLineTypeCount
reed@google.com996f64f2013-01-24 17:17:28 +0000311 };
312
313 static const char* LineTypeName(LineType lt) {
314 static const char* gNames[] = { "hori", "vert", "diag" };
bungeman99fe8222015-08-20 07:57:51 -0700315 static_assert(kLineTypeCount == SK_ARRAY_COUNT(gNames), "names_wrong_size");
reed@google.com996f64f2013-01-24 17:17:28 +0000316 return gNames[lt];
317 }
318
mtklein@google.com410e6e82013-09-13 19:52:27 +0000319 GiantDashBench(LineType lt, SkScalar width) {
reed@google.come5ceea92013-01-24 17:33:21 +0000320 fName.printf("giantdashline_%s_%g", LineTypeName(lt), width);
321 fStrokeWidth = width;
322
reed@google.com996f64f2013-01-24 17:17:28 +0000323 // deliberately pick intervals that won't be caught by asPoints(), so
324 // we can test the filterPath code-path.
commit-bot@chromium.orgae4976c2014-05-01 15:30:52 +0000325 const SkScalar intervals[] = { 20, 10, 10, 10 };
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000326 fPathEffect.reset(SkDashPathEffect::Create(intervals,
327 SK_ARRAY_COUNT(intervals), 0));
reed@google.com996f64f2013-01-24 17:17:28 +0000328
329 SkScalar cx = 640 / 2; // center X
330 SkScalar cy = 480 / 2; // center Y
331 SkMatrix matrix;
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000332
reed@google.com996f64f2013-01-24 17:17:28 +0000333 switch (lt) {
334 case kHori_LineType:
335 matrix.setIdentity();
336 break;
337 case kVert_LineType:
338 matrix.setRotate(90, cx, cy);
339 break;
340 case kDiag_LineType:
341 matrix.setRotate(45, cx, cy);
342 break;
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +0000343 case kLineTypeCount:
344 // Not a real enum value.
345 break;
reed@google.com996f64f2013-01-24 17:17:28 +0000346 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000347
reed@google.come5ceea92013-01-24 17:33:21 +0000348 const SkScalar overshoot = 100*1000;
reed@google.com996f64f2013-01-24 17:17:28 +0000349 const SkPoint pts[2] = {
350 { -overshoot, cy }, { 640 + overshoot, cy }
351 };
352 matrix.mapPoints(fPts, pts, 2);
353 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000354
reed@google.com996f64f2013-01-24 17:17:28 +0000355protected:
mtklein36352bf2015-03-25 18:17:31 -0700356 const char* onGetName() override {
reed@google.com996f64f2013-01-24 17:17:28 +0000357 return fName.c_str();
358 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000359
mtkleina1ebeb22015-10-01 09:43:39 -0700360 void onDraw(int loops, SkCanvas* canvas) override {
reed@google.com996f64f2013-01-24 17:17:28 +0000361 SkPaint p;
362 this->setupPaint(&p);
363 p.setStyle(SkPaint::kStroke_Style);
reed@google.come5ceea92013-01-24 17:33:21 +0000364 p.setStrokeWidth(fStrokeWidth);
reed@google.com996f64f2013-01-24 17:17:28 +0000365 p.setPathEffect(fPathEffect);
366
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000367 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000368 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, fPts, p);
369 }
reed@google.com996f64f2013-01-24 17:17:28 +0000370 }
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000371
reed@google.com996f64f2013-01-24 17:17:28 +0000372private:
tfarinaf168b862014-06-19 12:32:29 -0700373 typedef Benchmark INHERITED;
reed@google.com996f64f2013-01-24 17:17:28 +0000374};
375
egdaniel05bb6f12014-06-04 08:15:31 -0700376// Want to test how we draw a dashed grid (like what is used in spreadsheets) of many
377// small dashed lines switching back and forth between horizontal and vertical
tfarinaf168b862014-06-19 12:32:29 -0700378class DashGridBench : public Benchmark {
egdaniel05bb6f12014-06-04 08:15:31 -0700379 SkString fName;
380 int fStrokeWidth;
381 bool fDoAA;
382
383 SkAutoTUnref<SkPathEffect> fPathEffect;
384
385public:
386 DashGridBench(int dashLength, int strokeWidth, bool doAA) {
387 fName.printf("dashgrid_%d_%d%s", dashLength, strokeWidth, doAA ? "_aa" : "_bw");
388 fStrokeWidth = strokeWidth;
389 fDoAA = doAA;
390
391 SkScalar vals[] = { SkIntToScalar(dashLength), SkIntToScalar(dashLength) };
392 fPathEffect.reset(SkDashPathEffect::Create(vals, 2, SK_Scalar1));
393 }
394
395protected:
mtklein36352bf2015-03-25 18:17:31 -0700396 const char* onGetName() override {
egdaniel05bb6f12014-06-04 08:15:31 -0700397 return fName.c_str();
398 }
399
mtkleina1ebeb22015-10-01 09:43:39 -0700400 void onDraw(int loops, SkCanvas* canvas) override {
egdaniel05bb6f12014-06-04 08:15:31 -0700401 SkPaint p;
402 this->setupPaint(&p);
403 p.setColor(SK_ColorBLACK);
404 p.setStyle(SkPaint::kStroke_Style);
405 p.setStrokeWidth(SkIntToScalar(fStrokeWidth));
406 p.setPathEffect(fPathEffect);
407 p.setAntiAlias(fDoAA);
408
409 SkPoint pts[4] = {
410 { SkIntToScalar(0), 20.5f },
411 { SkIntToScalar(20), 20.5f },
412 { 20.5f, SkIntToScalar(0) },
413 { 20.5f, SkIntToScalar(20) }
414 };
415
416 for (int i = 0; i < loops; ++i) {
417 for (int j = 0; j < 10; ++j) {
418 for (int k = 0; k < 10; ++k) {
419 // Horizontal line
420 SkPoint horPts[2];
421 horPts[0].fX = pts[0].fX + k * 22.f;
422 horPts[0].fY = pts[0].fY + j * 22.f;
423 horPts[1].fX = pts[1].fX + k * 22.f;
424 horPts[1].fY = pts[1].fY + j * 22.f;
425 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, horPts, p);
426
427 // Vertical line
428 SkPoint vertPts[2];
429 vertPts[0].fX = pts[2].fX + k * 22.f;
430 vertPts[0].fY = pts[2].fY + j * 22.f;
431 vertPts[1].fX = pts[3].fX + k * 22.f;
432 vertPts[1].fY = pts[3].fY + j * 22.f;
433 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, vertPts, p);
434 }
435 }
436 }
437 }
438
439private:
tfarinaf168b862014-06-19 12:32:29 -0700440 typedef Benchmark INHERITED;
egdaniel05bb6f12014-06-04 08:15:31 -0700441};
reed@google.com996f64f2013-01-24 17:17:28 +0000442
reed@google.com4aa1a702012-05-04 16:37:45 +0000443///////////////////////////////////////////////////////////////////////////////
444
445static const SkScalar gDots[] = { SK_Scalar1, SK_Scalar1 };
446
447#define PARAM(array) array, SK_ARRAY_COUNT(array)
448
mtklein@google.com410e6e82013-09-13 19:52:27 +0000449DEF_BENCH( return new DashBench(PARAM(gDots), 0); )
450DEF_BENCH( return new DashBench(PARAM(gDots), 1); )
451DEF_BENCH( return new DashBench(PARAM(gDots), 1, true); )
452DEF_BENCH( return new DashBench(PARAM(gDots), 4); )
453DEF_BENCH( return new MakeDashBench(make_poly, "poly"); )
454DEF_BENCH( return new MakeDashBench(make_quad, "quad"); )
455DEF_BENCH( return new MakeDashBench(make_cubic, "cubic"); )
456DEF_BENCH( return new DashLineBench(0, false); )
457DEF_BENCH( return new DashLineBench(SK_Scalar1, false); )
458DEF_BENCH( return new DashLineBench(2 * SK_Scalar1, false); )
459DEF_BENCH( return new DashLineBench(0, true); )
460DEF_BENCH( return new DashLineBench(SK_Scalar1, true); )
461DEF_BENCH( return new DashLineBench(2 * SK_Scalar1, true); )
reed@google.com4aa1a702012-05-04 16:37:45 +0000462
mtklein@google.com410e6e82013-09-13 19:52:27 +0000463DEF_BENCH( return new DrawPointsDashingBench(1, 1, false); )
464DEF_BENCH( return new DrawPointsDashingBench(1, 1, true); )
465DEF_BENCH( return new DrawPointsDashingBench(3, 1, false); )
466DEF_BENCH( return new DrawPointsDashingBench(3, 1, true); )
467DEF_BENCH( return new DrawPointsDashingBench(5, 5, false); )
468DEF_BENCH( return new DrawPointsDashingBench(5, 5, true); )
reed@google.com996f64f2013-01-24 17:17:28 +0000469
djsollen@google.com69feaca2013-07-18 17:22:30 +0000470/* Disable the GiantDashBench for Android devices until we can better control
471 * the memory usage. (https://code.google.com/p/skia/issues/detail?id=1430)
472 */
473#ifndef SK_BUILD_FOR_ANDROID
mtklein@google.com410e6e82013-09-13 19:52:27 +0000474DEF_BENCH( return new GiantDashBench(GiantDashBench::kHori_LineType, 0); )
475DEF_BENCH( return new GiantDashBench(GiantDashBench::kVert_LineType, 0); )
476DEF_BENCH( return new GiantDashBench(GiantDashBench::kDiag_LineType, 0); )
reed@google.come5ceea92013-01-24 17:33:21 +0000477
478// pass 2 to explicitly avoid any 1-is-the-same-as-hairline special casing
479
480// hori_2 is just too slow to enable at the moment
mtklein@google.com410e6e82013-09-13 19:52:27 +0000481DEF_BENCH( return new GiantDashBench(GiantDashBench::kHori_LineType, 2); )
482DEF_BENCH( return new GiantDashBench(GiantDashBench::kVert_LineType, 2); )
483DEF_BENCH( return new GiantDashBench(GiantDashBench::kDiag_LineType, 2); )
egdaniel05bb6f12014-06-04 08:15:31 -0700484
485DEF_BENCH( return new DashGridBench(1, 1, true); )
486DEF_BENCH( return new DashGridBench(1, 1, false); )
487DEF_BENCH( return new DashGridBench(3, 1, true); )
488DEF_BENCH( return new DashGridBench(3, 1, false); )
djsollen@google.com69feaca2013-07-18 17:22:30 +0000489#endif