reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 7 | #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.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 17 | |
reed@google.com | ef85e3c | 2012-05-10 15:40:57 +0000 | [diff] [blame] | 18 | |
reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 19 | /* |
| 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 | */ |
| 27 | static void path_hline(SkPath* path) { |
| 28 | path->moveTo(SkIntToScalar(10), SkIntToScalar(10)); |
| 29 | path->lineTo(SkIntToScalar(600), SkIntToScalar(10)); |
| 30 | } |
| 31 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 32 | class DashBench : public Benchmark { |
reed@google.com | ef85e3c | 2012-05-10 15:40:57 +0000 | [diff] [blame] | 33 | protected: |
mike@reedtribe.org | 99a6ef4 | 2012-05-05 13:46:10 +0000 | [diff] [blame] | 34 | SkString fName; |
reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 35 | SkTDArray<SkScalar> fIntervals; |
mike@reedtribe.org | 99a6ef4 | 2012-05-05 13:46:10 +0000 | [diff] [blame] | 36 | int fWidth; |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 37 | SkPoint fPts[2]; |
| 38 | bool fDoClip; |
reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 39 | |
reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 40 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 41 | DashBench(const SkScalar intervals[], int count, int width, |
| 42 | bool doClip = false) { |
reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 43 | fIntervals.append(count, intervals); |
mike@reedtribe.org | 99a6ef4 | 2012-05-05 13:46:10 +0000 | [diff] [blame] | 44 | for (int i = 0; i < count; ++i) { |
| 45 | fIntervals[i] *= width; |
| 46 | } |
| 47 | fWidth = width; |
reed@google.com | 4ad2275 | 2012-05-15 19:50:58 +0000 | [diff] [blame] | 48 | fName.printf("dash_%d_%s", width, doClip ? "clipped" : "noclip"); |
| 49 | fDoClip = doClip; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 50 | |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 51 | fPts[0].set(SkIntToScalar(10), SkIntToScalar(10)); |
| 52 | fPts[1].set(SkIntToScalar(600), SkIntToScalar(10)); |
reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | virtual void makePath(SkPath* path) { |
| 56 | path_hline(path); |
| 57 | } |
| 58 | |
| 59 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 60 | const char* onGetName() override { |
mike@reedtribe.org | 99a6ef4 | 2012-05-05 13:46:10 +0000 | [diff] [blame] | 61 | return fName.c_str(); |
reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 62 | } |
| 63 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 64 | void onDraw(int loops, SkCanvas* canvas) override { |
reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 65 | SkPaint paint; |
| 66 | this->setupPaint(&paint); |
| 67 | paint.setStyle(SkPaint::kStroke_Style); |
mike@reedtribe.org | 99a6ef4 | 2012-05-05 13:46:10 +0000 | [diff] [blame] | 68 | paint.setStrokeWidth(SkIntToScalar(fWidth)); |
reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 69 | paint.setAntiAlias(false); |
| 70 | |
| 71 | SkPath path; |
| 72 | this->makePath(&path); |
| 73 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 74 | paint.setPathEffect(SkDashPathEffect::Make(fIntervals.begin(), fIntervals.count(), 0)); |
reed@google.com | 4ad2275 | 2012-05-15 19:50:58 +0000 | [diff] [blame] | 75 | |
| 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.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 84 | this->handlePath(canvas, path, paint, loops); |
reed@google.com | ef85e3c | 2012-05-10 15:40:57 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | virtual void handlePath(SkCanvas* canvas, const SkPath& path, |
| 88 | const SkPaint& paint, int N) { |
reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 89 | for (int i = 0; i < N; ++i) { |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 90 | // canvas->drawPoints(SkCanvas::kLines_PointMode, 2, fPts, paint); |
reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 91 | canvas->drawPath(path, paint); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 96 | typedef Benchmark INHERITED; |
reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
reed@google.com | ef85e3c | 2012-05-10 15:40:57 +0000 | [diff] [blame] | 99 | class RectDashBench : public DashBench { |
| 100 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 101 | RectDashBench(const SkScalar intervals[], int count, int width) |
| 102 | : INHERITED(intervals, count, width) { |
reed@google.com | ef85e3c | 2012-05-10 15:40:57 +0000 | [diff] [blame] | 103 | fName.append("_rect"); |
| 104 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 105 | |
reed@google.com | ef85e3c | 2012-05-10 15:40:57 +0000 | [diff] [blame] | 106 | protected: |
| 107 | virtual void handlePath(SkCanvas* canvas, const SkPath& path, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 108 | const SkPaint& paint, int N) override { |
reed@google.com | ef85e3c | 2012-05-10 15:40:57 +0000 | [diff] [blame] | 109 | SkPoint pts[2]; |
| 110 | if (!path.isLine(pts) || pts[0].fY != pts[1].fY) { |
| 111 | this->INHERITED::handlePath(canvas, path, paint, N); |
| 112 | } else { |
| 113 | SkRect rect; |
| 114 | rect.fLeft = pts[0].fX; |
| 115 | rect.fTop = pts[0].fY - paint.getStrokeWidth() / 2; |
| 116 | rect.fRight = rect.fLeft + SkIntToScalar(fWidth); |
| 117 | rect.fBottom = rect.fTop + paint.getStrokeWidth(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 118 | |
reed@google.com | ef85e3c | 2012-05-10 15:40:57 +0000 | [diff] [blame] | 119 | SkPaint p(paint); |
| 120 | p.setStyle(SkPaint::kFill_Style); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 121 | p.setPathEffect(nullptr); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 122 | |
reed@google.com | ef85e3c | 2012-05-10 15:40:57 +0000 | [diff] [blame] | 123 | int count = SkScalarRoundToInt((pts[1].fX - pts[0].fX) / (2*fWidth)); |
| 124 | SkScalar dx = SkIntToScalar(2 * fWidth); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 125 | |
reed@google.com | ef85e3c | 2012-05-10 15:40:57 +0000 | [diff] [blame] | 126 | for (int i = 0; i < N*10; ++i) { |
| 127 | SkRect r = rect; |
| 128 | for (int j = 0; j < count; ++j) { |
| 129 | canvas->drawRect(r, p); |
| 130 | r.offset(dx, 0); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 135 | |
reed@google.com | ef85e3c | 2012-05-10 15:40:57 +0000 | [diff] [blame] | 136 | private: |
| 137 | typedef DashBench INHERITED; |
| 138 | }; |
| 139 | |
reed@google.com | ea6f683 | 2012-05-18 18:32:54 +0000 | [diff] [blame] | 140 | static void make_unit_star(SkPath* path, int n) { |
| 141 | SkScalar rad = -SK_ScalarPI / 2; |
| 142 | const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 143 | |
reed@google.com | ea6f683 | 2012-05-18 18:32:54 +0000 | [diff] [blame] | 144 | path->moveTo(0, -SK_Scalar1); |
| 145 | for (int i = 1; i < n; i++) { |
| 146 | rad += drad; |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 147 | path->lineTo(SkScalarCos(rad), SkScalarSin(rad)); |
reed@google.com | ea6f683 | 2012-05-18 18:32:54 +0000 | [diff] [blame] | 148 | } |
| 149 | path->close(); |
| 150 | } |
| 151 | |
| 152 | static void make_poly(SkPath* path) { |
| 153 | make_unit_star(path, 9); |
robertphillips | 1d24b8d | 2015-03-26 19:57:08 -0700 | [diff] [blame] | 154 | const SkMatrix matrix = SkMatrix::MakeScale(SkIntToScalar(100), SkIntToScalar(100)); |
reed@google.com | ea6f683 | 2012-05-18 18:32:54 +0000 | [diff] [blame] | 155 | path->transform(matrix); |
| 156 | } |
| 157 | |
| 158 | static void make_quad(SkPath* path) { |
| 159 | SkScalar x0 = SkIntToScalar(10); |
| 160 | SkScalar y0 = SkIntToScalar(10); |
| 161 | path->moveTo(x0, y0); |
| 162 | path->quadTo(x0, y0 + 400 * SK_Scalar1, |
| 163 | x0 + 600 * SK_Scalar1, y0 + 400 * SK_Scalar1); |
| 164 | } |
| 165 | |
| 166 | static void make_cubic(SkPath* path) { |
| 167 | SkScalar x0 = SkIntToScalar(10); |
| 168 | SkScalar y0 = SkIntToScalar(10); |
| 169 | path->moveTo(x0, y0); |
| 170 | path->cubicTo(x0, y0 + 400 * SK_Scalar1, |
| 171 | x0 + 600 * SK_Scalar1, y0 + 400 * SK_Scalar1, |
| 172 | x0 + 600 * SK_Scalar1, y0); |
| 173 | } |
| 174 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 175 | class MakeDashBench : public Benchmark { |
reed@google.com | ea6f683 | 2012-05-18 18:32:54 +0000 | [diff] [blame] | 176 | SkString fName; |
| 177 | SkPath fPath; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 178 | sk_sp<SkPathEffect> fPE; |
skia.committer@gmail.com | 73bb3be | 2012-09-05 02:01:29 +0000 | [diff] [blame] | 179 | |
reed@google.com | ea6f683 | 2012-05-18 18:32:54 +0000 | [diff] [blame] | 180 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 181 | MakeDashBench(void (*proc)(SkPath*), const char name[]) { |
reed@google.com | ea6f683 | 2012-05-18 18:32:54 +0000 | [diff] [blame] | 182 | fName.printf("makedash_%s", name); |
| 183 | proc(&fPath); |
skia.committer@gmail.com | 73bb3be | 2012-09-05 02:01:29 +0000 | [diff] [blame] | 184 | |
reed@google.com | ea6f683 | 2012-05-18 18:32:54 +0000 | [diff] [blame] | 185 | SkScalar vals[] = { SkIntToScalar(4), SkIntToScalar(4) }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 186 | fPE = SkDashPathEffect::Make(vals, 2, 0); |
reed@google.com | ea6f683 | 2012-05-18 18:32:54 +0000 | [diff] [blame] | 187 | } |
skia.committer@gmail.com | 73bb3be | 2012-09-05 02:01:29 +0000 | [diff] [blame] | 188 | |
reed@google.com | ea6f683 | 2012-05-18 18:32:54 +0000 | [diff] [blame] | 189 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 190 | const char* onGetName() override { |
reed@google.com | ea6f683 | 2012-05-18 18:32:54 +0000 | [diff] [blame] | 191 | return fName.c_str(); |
| 192 | } |
skia.committer@gmail.com | 73bb3be | 2012-09-05 02:01:29 +0000 | [diff] [blame] | 193 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 194 | void onDraw(int loops, SkCanvas*) override { |
reed@google.com | ea6f683 | 2012-05-18 18:32:54 +0000 | [diff] [blame] | 195 | SkPath dst; |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 196 | for (int i = 0; i < loops; ++i) { |
reed@google.com | fd4be26 | 2012-05-25 01:04:12 +0000 | [diff] [blame] | 197 | SkStrokeRec rec(SkStrokeRec::kHairline_InitStyle); |
skia.committer@gmail.com | 73bb3be | 2012-09-05 02:01:29 +0000 | [diff] [blame] | 198 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 199 | fPE->filterPath(&dst, fPath, &rec, nullptr); |
reed@google.com | ea6f683 | 2012-05-18 18:32:54 +0000 | [diff] [blame] | 200 | dst.rewind(); |
| 201 | } |
| 202 | } |
skia.committer@gmail.com | 73bb3be | 2012-09-05 02:01:29 +0000 | [diff] [blame] | 203 | |
reed@google.com | 31e9d64 | 2012-09-04 20:07:23 +0000 | [diff] [blame] | 204 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 205 | typedef Benchmark INHERITED; |
reed@google.com | 31e9d64 | 2012-09-04 20:07:23 +0000 | [diff] [blame] | 206 | }; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 207 | |
reed@google.com | 5125d84 | 2012-09-04 20:19:35 +0000 | [diff] [blame] | 208 | /* |
| 209 | * We try to special case square dashes (intervals are equal to strokewidth). |
| 210 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 211 | class DashLineBench : public Benchmark { |
reed@google.com | 31e9d64 | 2012-09-04 20:07:23 +0000 | [diff] [blame] | 212 | SkString fName; |
reed@google.com | 5125d84 | 2012-09-04 20:19:35 +0000 | [diff] [blame] | 213 | SkScalar fStrokeWidth; |
| 214 | bool fIsRound; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 215 | sk_sp<SkPathEffect> fPE; |
skia.committer@gmail.com | 73bb3be | 2012-09-05 02:01:29 +0000 | [diff] [blame] | 216 | |
reed@google.com | 31e9d64 | 2012-09-04 20:07:23 +0000 | [diff] [blame] | 217 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 218 | DashLineBench(SkScalar width, bool isRound) { |
reed@google.com | 5125d84 | 2012-09-04 20:19:35 +0000 | [diff] [blame] | 219 | fName.printf("dashline_%g_%s", SkScalarToFloat(width), isRound ? "circle" : "square"); |
| 220 | fStrokeWidth = width; |
| 221 | fIsRound = isRound; |
skia.committer@gmail.com | 73bb3be | 2012-09-05 02:01:29 +0000 | [diff] [blame] | 222 | |
reed@google.com | 5125d84 | 2012-09-04 20:19:35 +0000 | [diff] [blame] | 223 | SkScalar vals[] = { SK_Scalar1, SK_Scalar1 }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 224 | fPE = SkDashPathEffect::Make(vals, 2, 0); |
reed@google.com | 31e9d64 | 2012-09-04 20:07:23 +0000 | [diff] [blame] | 225 | } |
skia.committer@gmail.com | 73bb3be | 2012-09-05 02:01:29 +0000 | [diff] [blame] | 226 | |
reed@google.com | 31e9d64 | 2012-09-04 20:07:23 +0000 | [diff] [blame] | 227 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 228 | const char* onGetName() override { |
reed@google.com | 31e9d64 | 2012-09-04 20:07:23 +0000 | [diff] [blame] | 229 | return fName.c_str(); |
| 230 | } |
skia.committer@gmail.com | 73bb3be | 2012-09-05 02:01:29 +0000 | [diff] [blame] | 231 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 232 | void onDraw(int loops, SkCanvas* canvas) override { |
reed@google.com | 31e9d64 | 2012-09-04 20:07:23 +0000 | [diff] [blame] | 233 | SkPaint paint; |
| 234 | this->setupPaint(&paint); |
reed@google.com | 5125d84 | 2012-09-04 20:19:35 +0000 | [diff] [blame] | 235 | paint.setStrokeWidth(fStrokeWidth); |
| 236 | paint.setStrokeCap(fIsRound ? SkPaint::kRound_Cap : SkPaint::kSquare_Cap); |
reed@google.com | 31e9d64 | 2012-09-04 20:07:23 +0000 | [diff] [blame] | 237 | paint.setPathEffect(fPE); |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 238 | for (int i = 0; i < loops; ++i) { |
reed@google.com | 5125d84 | 2012-09-04 20:19:35 +0000 | [diff] [blame] | 239 | canvas->drawLine(10 * SK_Scalar1, 10 * SK_Scalar1, |
| 240 | 640 * SK_Scalar1, 10 * SK_Scalar1, paint); |
reed@google.com | 31e9d64 | 2012-09-04 20:07:23 +0000 | [diff] [blame] | 241 | } |
| 242 | } |
skia.committer@gmail.com | 73bb3be | 2012-09-05 02:01:29 +0000 | [diff] [blame] | 243 | |
reed@google.com | ea6f683 | 2012-05-18 18:32:54 +0000 | [diff] [blame] | 244 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 245 | typedef Benchmark INHERITED; |
reed@google.com | ea6f683 | 2012-05-18 18:32:54 +0000 | [diff] [blame] | 246 | }; |
| 247 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 248 | class DrawPointsDashingBench : public Benchmark { |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 249 | SkString fName; |
| 250 | int fStrokeWidth; |
robertphillips@google.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 251 | bool fDoAA; |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 252 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 253 | sk_sp<SkPathEffect> fPathEffect; |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 254 | |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 255 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 256 | DrawPointsDashingBench(int dashLength, int strokeWidth, bool doAA) |
| 257 | { |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 258 | fName.printf("drawpointsdash_%d_%d%s", dashLength, strokeWidth, doAA ? "_aa" : "_bw"); |
| 259 | fStrokeWidth = strokeWidth; |
robertphillips@google.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 260 | fDoAA = doAA; |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 261 | |
| 262 | SkScalar vals[] = { SkIntToScalar(dashLength), SkIntToScalar(dashLength) }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 263 | fPathEffect = SkDashPathEffect::Make(vals, 2, SK_Scalar1); |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 267 | const char* onGetName() override { |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 268 | return fName.c_str(); |
| 269 | } |
| 270 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 271 | void onDraw(int loops, SkCanvas* canvas) override { |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 272 | SkPaint p; |
| 273 | this->setupPaint(&p); |
| 274 | p.setColor(SK_ColorBLACK); |
| 275 | p.setStyle(SkPaint::kStroke_Style); |
| 276 | p.setStrokeWidth(SkIntToScalar(fStrokeWidth)); |
| 277 | p.setPathEffect(fPathEffect); |
robertphillips@google.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 278 | p.setAntiAlias(fDoAA); |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 279 | |
| 280 | SkPoint pts[2] = { |
| 281 | { SkIntToScalar(10), 0 }, |
| 282 | { SkIntToScalar(640), 0 } |
| 283 | }; |
| 284 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 285 | for (int i = 0; i < loops; ++i) { |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 286 | pts[0].fY = pts[1].fY = SkIntToScalar(i % 480); |
| 287 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 292 | typedef Benchmark INHERITED; |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 293 | }; |
reed@google.com | 996f64f | 2013-01-24 17:17:28 +0000 | [diff] [blame] | 294 | |
reed@google.com | e5ceea9 | 2013-01-24 17:33:21 +0000 | [diff] [blame] | 295 | // Want to test how we handle dashing when 99% of the dash is clipped out |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 296 | class GiantDashBench : public Benchmark { |
reed@google.com | 996f64f | 2013-01-24 17:17:28 +0000 | [diff] [blame] | 297 | SkString fName; |
reed@google.com | e5ceea9 | 2013-01-24 17:33:21 +0000 | [diff] [blame] | 298 | SkScalar fStrokeWidth; |
reed@google.com | 996f64f | 2013-01-24 17:17:28 +0000 | [diff] [blame] | 299 | SkPoint fPts[2]; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 300 | sk_sp<SkPathEffect> fPathEffect; |
skia.committer@gmail.com | 4024f32 | 2013-01-25 07:06:46 +0000 | [diff] [blame] | 301 | |
reed@google.com | 996f64f | 2013-01-24 17:17:28 +0000 | [diff] [blame] | 302 | public: |
| 303 | enum LineType { |
| 304 | kHori_LineType, |
| 305 | kVert_LineType, |
commit-bot@chromium.org | 5d7ca95 | 2013-04-22 20:26:44 +0000 | [diff] [blame] | 306 | kDiag_LineType, |
| 307 | kLineTypeCount |
reed@google.com | 996f64f | 2013-01-24 17:17:28 +0000 | [diff] [blame] | 308 | }; |
| 309 | |
| 310 | static const char* LineTypeName(LineType lt) { |
| 311 | static const char* gNames[] = { "hori", "vert", "diag" }; |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 312 | static_assert(kLineTypeCount == SK_ARRAY_COUNT(gNames), "names_wrong_size"); |
reed@google.com | 996f64f | 2013-01-24 17:17:28 +0000 | [diff] [blame] | 313 | return gNames[lt]; |
| 314 | } |
| 315 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 316 | GiantDashBench(LineType lt, SkScalar width) { |
reed@google.com | e5ceea9 | 2013-01-24 17:33:21 +0000 | [diff] [blame] | 317 | fName.printf("giantdashline_%s_%g", LineTypeName(lt), width); |
| 318 | fStrokeWidth = width; |
| 319 | |
reed@google.com | 996f64f | 2013-01-24 17:17:28 +0000 | [diff] [blame] | 320 | // deliberately pick intervals that won't be caught by asPoints(), so |
| 321 | // we can test the filterPath code-path. |
commit-bot@chromium.org | ae4976c | 2014-05-01 15:30:52 +0000 | [diff] [blame] | 322 | const SkScalar intervals[] = { 20, 10, 10, 10 }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 323 | fPathEffect = SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0); |
reed@google.com | 996f64f | 2013-01-24 17:17:28 +0000 | [diff] [blame] | 324 | |
| 325 | SkScalar cx = 640 / 2; // center X |
| 326 | SkScalar cy = 480 / 2; // center Y |
| 327 | SkMatrix matrix; |
skia.committer@gmail.com | 4024f32 | 2013-01-25 07:06:46 +0000 | [diff] [blame] | 328 | |
reed@google.com | 996f64f | 2013-01-24 17:17:28 +0000 | [diff] [blame] | 329 | switch (lt) { |
| 330 | case kHori_LineType: |
| 331 | matrix.setIdentity(); |
| 332 | break; |
| 333 | case kVert_LineType: |
| 334 | matrix.setRotate(90, cx, cy); |
| 335 | break; |
| 336 | case kDiag_LineType: |
| 337 | matrix.setRotate(45, cx, cy); |
| 338 | break; |
commit-bot@chromium.org | 5d7ca95 | 2013-04-22 20:26:44 +0000 | [diff] [blame] | 339 | case kLineTypeCount: |
| 340 | // Not a real enum value. |
| 341 | break; |
reed@google.com | 996f64f | 2013-01-24 17:17:28 +0000 | [diff] [blame] | 342 | } |
skia.committer@gmail.com | 4024f32 | 2013-01-25 07:06:46 +0000 | [diff] [blame] | 343 | |
reed@google.com | e5ceea9 | 2013-01-24 17:33:21 +0000 | [diff] [blame] | 344 | const SkScalar overshoot = 100*1000; |
reed@google.com | 996f64f | 2013-01-24 17:17:28 +0000 | [diff] [blame] | 345 | const SkPoint pts[2] = { |
| 346 | { -overshoot, cy }, { 640 + overshoot, cy } |
| 347 | }; |
| 348 | matrix.mapPoints(fPts, pts, 2); |
| 349 | } |
skia.committer@gmail.com | 4024f32 | 2013-01-25 07:06:46 +0000 | [diff] [blame] | 350 | |
reed@google.com | 996f64f | 2013-01-24 17:17:28 +0000 | [diff] [blame] | 351 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 352 | const char* onGetName() override { |
reed@google.com | 996f64f | 2013-01-24 17:17:28 +0000 | [diff] [blame] | 353 | return fName.c_str(); |
| 354 | } |
skia.committer@gmail.com | 4024f32 | 2013-01-25 07:06:46 +0000 | [diff] [blame] | 355 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 356 | void onDraw(int loops, SkCanvas* canvas) override { |
reed@google.com | 996f64f | 2013-01-24 17:17:28 +0000 | [diff] [blame] | 357 | SkPaint p; |
| 358 | this->setupPaint(&p); |
| 359 | p.setStyle(SkPaint::kStroke_Style); |
reed@google.com | e5ceea9 | 2013-01-24 17:33:21 +0000 | [diff] [blame] | 360 | p.setStrokeWidth(fStrokeWidth); |
reed@google.com | 996f64f | 2013-01-24 17:17:28 +0000 | [diff] [blame] | 361 | p.setPathEffect(fPathEffect); |
| 362 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 363 | for (int i = 0; i < loops; i++) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 364 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, fPts, p); |
| 365 | } |
reed@google.com | 996f64f | 2013-01-24 17:17:28 +0000 | [diff] [blame] | 366 | } |
skia.committer@gmail.com | 4024f32 | 2013-01-25 07:06:46 +0000 | [diff] [blame] | 367 | |
reed@google.com | 996f64f | 2013-01-24 17:17:28 +0000 | [diff] [blame] | 368 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 369 | typedef Benchmark INHERITED; |
reed@google.com | 996f64f | 2013-01-24 17:17:28 +0000 | [diff] [blame] | 370 | }; |
| 371 | |
egdaniel | 05bb6f1 | 2014-06-04 08:15:31 -0700 | [diff] [blame] | 372 | // Want to test how we draw a dashed grid (like what is used in spreadsheets) of many |
| 373 | // small dashed lines switching back and forth between horizontal and vertical |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 374 | class DashGridBench : public Benchmark { |
egdaniel | 05bb6f1 | 2014-06-04 08:15:31 -0700 | [diff] [blame] | 375 | SkString fName; |
| 376 | int fStrokeWidth; |
| 377 | bool fDoAA; |
| 378 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 379 | sk_sp<SkPathEffect> fPathEffect; |
egdaniel | 05bb6f1 | 2014-06-04 08:15:31 -0700 | [diff] [blame] | 380 | |
| 381 | public: |
| 382 | DashGridBench(int dashLength, int strokeWidth, bool doAA) { |
| 383 | fName.printf("dashgrid_%d_%d%s", dashLength, strokeWidth, doAA ? "_aa" : "_bw"); |
| 384 | fStrokeWidth = strokeWidth; |
| 385 | fDoAA = doAA; |
| 386 | |
| 387 | SkScalar vals[] = { SkIntToScalar(dashLength), SkIntToScalar(dashLength) }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 388 | fPathEffect = SkDashPathEffect::Make(vals, 2, SK_Scalar1); |
egdaniel | 05bb6f1 | 2014-06-04 08:15:31 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 392 | const char* onGetName() override { |
egdaniel | 05bb6f1 | 2014-06-04 08:15:31 -0700 | [diff] [blame] | 393 | return fName.c_str(); |
| 394 | } |
| 395 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 396 | void onDraw(int loops, SkCanvas* canvas) override { |
egdaniel | 05bb6f1 | 2014-06-04 08:15:31 -0700 | [diff] [blame] | 397 | SkPaint p; |
| 398 | this->setupPaint(&p); |
| 399 | p.setColor(SK_ColorBLACK); |
| 400 | p.setStyle(SkPaint::kStroke_Style); |
| 401 | p.setStrokeWidth(SkIntToScalar(fStrokeWidth)); |
| 402 | p.setPathEffect(fPathEffect); |
| 403 | p.setAntiAlias(fDoAA); |
| 404 | |
| 405 | SkPoint pts[4] = { |
| 406 | { SkIntToScalar(0), 20.5f }, |
| 407 | { SkIntToScalar(20), 20.5f }, |
| 408 | { 20.5f, SkIntToScalar(0) }, |
| 409 | { 20.5f, SkIntToScalar(20) } |
| 410 | }; |
| 411 | |
| 412 | for (int i = 0; i < loops; ++i) { |
| 413 | for (int j = 0; j < 10; ++j) { |
| 414 | for (int k = 0; k < 10; ++k) { |
| 415 | // Horizontal line |
| 416 | SkPoint horPts[2]; |
| 417 | horPts[0].fX = pts[0].fX + k * 22.f; |
| 418 | horPts[0].fY = pts[0].fY + j * 22.f; |
| 419 | horPts[1].fX = pts[1].fX + k * 22.f; |
| 420 | horPts[1].fY = pts[1].fY + j * 22.f; |
| 421 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, horPts, p); |
| 422 | |
| 423 | // Vertical line |
| 424 | SkPoint vertPts[2]; |
| 425 | vertPts[0].fX = pts[2].fX + k * 22.f; |
| 426 | vertPts[0].fY = pts[2].fY + j * 22.f; |
| 427 | vertPts[1].fX = pts[3].fX + k * 22.f; |
| 428 | vertPts[1].fY = pts[3].fY + j * 22.f; |
| 429 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, vertPts, p); |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 436 | typedef Benchmark INHERITED; |
egdaniel | 05bb6f1 | 2014-06-04 08:15:31 -0700 | [diff] [blame] | 437 | }; |
reed@google.com | 996f64f | 2013-01-24 17:17:28 +0000 | [diff] [blame] | 438 | |
reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 439 | /////////////////////////////////////////////////////////////////////////////// |
| 440 | |
| 441 | static const SkScalar gDots[] = { SK_Scalar1, SK_Scalar1 }; |
| 442 | |
| 443 | #define PARAM(array) array, SK_ARRAY_COUNT(array) |
| 444 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 445 | DEF_BENCH( return new DashBench(PARAM(gDots), 0); ) |
| 446 | DEF_BENCH( return new DashBench(PARAM(gDots), 1); ) |
| 447 | DEF_BENCH( return new DashBench(PARAM(gDots), 1, true); ) |
| 448 | DEF_BENCH( return new DashBench(PARAM(gDots), 4); ) |
| 449 | DEF_BENCH( return new MakeDashBench(make_poly, "poly"); ) |
| 450 | DEF_BENCH( return new MakeDashBench(make_quad, "quad"); ) |
| 451 | DEF_BENCH( return new MakeDashBench(make_cubic, "cubic"); ) |
| 452 | DEF_BENCH( return new DashLineBench(0, false); ) |
| 453 | DEF_BENCH( return new DashLineBench(SK_Scalar1, false); ) |
| 454 | DEF_BENCH( return new DashLineBench(2 * SK_Scalar1, false); ) |
| 455 | DEF_BENCH( return new DashLineBench(0, true); ) |
| 456 | DEF_BENCH( return new DashLineBench(SK_Scalar1, true); ) |
| 457 | DEF_BENCH( return new DashLineBench(2 * SK_Scalar1, true); ) |
reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 458 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 459 | DEF_BENCH( return new DrawPointsDashingBench(1, 1, false); ) |
| 460 | DEF_BENCH( return new DrawPointsDashingBench(1, 1, true); ) |
| 461 | DEF_BENCH( return new DrawPointsDashingBench(3, 1, false); ) |
| 462 | DEF_BENCH( return new DrawPointsDashingBench(3, 1, true); ) |
| 463 | DEF_BENCH( return new DrawPointsDashingBench(5, 5, false); ) |
| 464 | DEF_BENCH( return new DrawPointsDashingBench(5, 5, true); ) |
reed@google.com | 996f64f | 2013-01-24 17:17:28 +0000 | [diff] [blame] | 465 | |
djsollen@google.com | 69feaca | 2013-07-18 17:22:30 +0000 | [diff] [blame] | 466 | /* Disable the GiantDashBench for Android devices until we can better control |
| 467 | * the memory usage. (https://code.google.com/p/skia/issues/detail?id=1430) |
| 468 | */ |
| 469 | #ifndef SK_BUILD_FOR_ANDROID |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 470 | DEF_BENCH( return new GiantDashBench(GiantDashBench::kHori_LineType, 0); ) |
| 471 | DEF_BENCH( return new GiantDashBench(GiantDashBench::kVert_LineType, 0); ) |
| 472 | DEF_BENCH( return new GiantDashBench(GiantDashBench::kDiag_LineType, 0); ) |
reed@google.com | e5ceea9 | 2013-01-24 17:33:21 +0000 | [diff] [blame] | 473 | |
| 474 | // pass 2 to explicitly avoid any 1-is-the-same-as-hairline special casing |
| 475 | |
| 476 | // hori_2 is just too slow to enable at the moment |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 477 | DEF_BENCH( return new GiantDashBench(GiantDashBench::kHori_LineType, 2); ) |
| 478 | DEF_BENCH( return new GiantDashBench(GiantDashBench::kVert_LineType, 2); ) |
| 479 | DEF_BENCH( return new GiantDashBench(GiantDashBench::kDiag_LineType, 2); ) |
egdaniel | 05bb6f1 | 2014-06-04 08:15:31 -0700 | [diff] [blame] | 480 | |
| 481 | DEF_BENCH( return new DashGridBench(1, 1, true); ) |
| 482 | DEF_BENCH( return new DashGridBench(1, 1, false); ) |
| 483 | DEF_BENCH( return new DashGridBench(3, 1, true); ) |
| 484 | DEF_BENCH( return new DashGridBench(3, 1, false); ) |
djsollen@google.com | 69feaca | 2013-07-18 17:22:30 +0000 | [diff] [blame] | 485 | #endif |