reed@google.com | c56a83c | 2012-05-07 13:46:32 +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/SkColorPriv.h" |
| 11 | #include "include/core/SkPaint.h" |
| 12 | #include "include/core/SkPath.h" |
| 13 | #include "include/core/SkShader.h" |
| 14 | #include "include/core/SkString.h" |
| 15 | #include "include/utils/SkRandom.h" |
Mike Reed | 5f152f0 | 2019-08-19 14:40:16 -0400 | [diff] [blame] | 16 | #include "src/core/SkPathPriv.h" |
| 17 | |
| 18 | enum class PathIterType { |
| 19 | kIter, |
| 20 | kRaw, |
| 21 | kEdge, |
| 22 | }; |
| 23 | const char* gPathIterNames[] = { |
| 24 | "iter", "raw", "edge" |
| 25 | }; |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 26 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 27 | static int rand_pts(SkRandom& rand, SkPoint pts[4]) { |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 28 | int n = rand.nextU() & 3; |
| 29 | n += 1; |
| 30 | |
| 31 | for (int i = 0; i < n; ++i) { |
| 32 | pts[i].fX = rand.nextSScalar1(); |
| 33 | pts[i].fY = rand.nextSScalar1(); |
| 34 | } |
| 35 | return n; |
| 36 | } |
| 37 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 38 | class PathIterBench : public Benchmark { |
Mike Reed | 5f152f0 | 2019-08-19 14:40:16 -0400 | [diff] [blame] | 39 | SkString fName; |
| 40 | SkPath fPath; |
| 41 | PathIterType fType; |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 42 | |
Mike Reed | 8f3bd77 | 2019-08-20 12:52:13 -0400 | [diff] [blame] | 43 | int fVerbInc = 0; |
| 44 | SkScalar fXInc = 0, fYInc = 0; |
| 45 | |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 46 | public: |
Mike Reed | 5f152f0 | 2019-08-19 14:40:16 -0400 | [diff] [blame] | 47 | PathIterBench(PathIterType t) : fType(t) { |
| 48 | fName.printf("pathiter_%s", gPathIterNames[static_cast<unsigned>(t)]); |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 49 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 50 | SkRandom rand; |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 51 | for (int i = 0; i < 1000; ++i) { |
| 52 | SkPoint pts[4]; |
| 53 | int n = rand_pts(rand, pts); |
| 54 | switch (n) { |
| 55 | case 1: |
| 56 | fPath.moveTo(pts[0]); |
| 57 | break; |
| 58 | case 2: |
| 59 | fPath.lineTo(pts[1]); |
| 60 | break; |
| 61 | case 3: |
| 62 | fPath.quadTo(pts[1], pts[2]); |
| 63 | break; |
| 64 | case 4: |
| 65 | fPath.cubicTo(pts[1], pts[2], pts[3]); |
| 66 | break; |
| 67 | } |
| 68 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 69 | } |
robertphillips@google.com | 433ce5e | 2012-09-17 10:49:30 +0000 | [diff] [blame] | 70 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 71 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 72 | return backend == kNonRendering_Backend; |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 76 | const char* onGetName() override { |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 77 | return fName.c_str(); |
| 78 | } |
| 79 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 80 | void onDraw(int loops, SkCanvas*) override { |
Mike Reed | 8f3bd77 | 2019-08-20 12:52:13 -0400 | [diff] [blame] | 81 | // Need to do *something* with the results, so the compile doesn't elide |
| 82 | // away the code we want to time. |
| 83 | auto handle = [this](int verb, const SkPoint pts[]) { |
| 84 | fVerbInc += verb; |
| 85 | fXInc += pts[0].fX; |
| 86 | fYInc += pts[0].fY; |
| 87 | }; |
| 88 | |
Mike Reed | 5f152f0 | 2019-08-19 14:40:16 -0400 | [diff] [blame] | 89 | SkPath::Verb verb; |
| 90 | SkPoint pts[4]; |
| 91 | switch (fType) { |
| 92 | case PathIterType::kIter: |
| 93 | for (int i = 0; i < loops; ++i) { |
| 94 | SkPath::Iter iter(fPath, true); |
Mike Reed | 8f3bd77 | 2019-08-20 12:52:13 -0400 | [diff] [blame] | 95 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 96 | handle(verb, pts); |
| 97 | } |
Mike Reed | 5f152f0 | 2019-08-19 14:40:16 -0400 | [diff] [blame] | 98 | } |
| 99 | break; |
| 100 | case PathIterType::kRaw: |
| 101 | for (int i = 0; i < loops; ++i) { |
Chris Dalton | 8650d33 | 2020-05-05 16:21:19 -0600 | [diff] [blame] | 102 | for (auto [verb, pts, w] : SkPathPriv::Iterate(fPath)) { |
| 103 | handle((SkPath::Verb)verb, pts); |
Mike Reed | 8f3bd77 | 2019-08-20 12:52:13 -0400 | [diff] [blame] | 104 | } |
Mike Reed | 5f152f0 | 2019-08-19 14:40:16 -0400 | [diff] [blame] | 105 | } |
| 106 | break; |
| 107 | case PathIterType::kEdge: |
| 108 | for (int i = 0; i < loops; ++i) { |
| 109 | SkPathEdgeIter iter(fPath); |
Mike Reed | 8f3bd77 | 2019-08-20 12:52:13 -0400 | [diff] [blame] | 110 | while (auto r = iter.next()) { |
| 111 | handle((int)r.fEdge, r.fPts); |
| 112 | } |
Mike Reed | 5f152f0 | 2019-08-19 14:40:16 -0400 | [diff] [blame] | 113 | } |
| 114 | break; |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 119 | using INHERITED = Benchmark; |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | /////////////////////////////////////////////////////////////////////////////// |
| 123 | |
Mike Reed | 5f152f0 | 2019-08-19 14:40:16 -0400 | [diff] [blame] | 124 | DEF_BENCH( return new PathIterBench(PathIterType::kIter); ) |
| 125 | DEF_BENCH( return new PathIterBench(PathIterType::kRaw); ) |
| 126 | DEF_BENCH( return new PathIterBench(PathIterType::kEdge); ) |