reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 1 | |
| 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 | */ |
| 8 | #include "SkBenchmark.h" |
| 9 | #include "SkRandom.h" |
| 10 | #include "SkRegion.h" |
| 11 | #include "SkString.h" |
| 12 | |
| 13 | static bool union_proc(SkRegion& a, SkRegion& b) { |
| 14 | SkRegion result; |
| 15 | return result.op(a, b, SkRegion::kUnion_Op); |
| 16 | } |
| 17 | |
| 18 | static bool sect_proc(SkRegion& a, SkRegion& b) { |
| 19 | SkRegion result; |
| 20 | return result.op(a, b, SkRegion::kIntersect_Op); |
| 21 | } |
| 22 | |
| 23 | static bool diff_proc(SkRegion& a, SkRegion& b) { |
| 24 | SkRegion result; |
| 25 | return result.op(a, b, SkRegion::kDifference_Op); |
| 26 | } |
| 27 | |
reed@google.com | 0d10280 | 2012-05-31 18:28:59 +0000 | [diff] [blame] | 28 | static bool diffrect_proc(SkRegion& a, SkRegion& b) { |
| 29 | SkRegion result; |
| 30 | return result.op(a, b.getBounds(), SkRegion::kDifference_Op); |
| 31 | } |
| 32 | |
| 33 | static bool diffrectbig_proc(SkRegion& a, SkRegion& b) { |
| 34 | SkRegion result; |
| 35 | return result.op(a, a.getBounds(), SkRegion::kDifference_Op); |
| 36 | } |
| 37 | |
reed@google.com | 01049d5 | 2012-05-02 16:45:36 +0000 | [diff] [blame] | 38 | static bool containsrect_proc(SkRegion& a, SkRegion& b) { |
reed@google.com | 50129db | 2012-04-30 12:07:55 +0000 | [diff] [blame] | 39 | SkIRect r = a.getBounds(); |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 40 | r.inset(r.width()/4, r.height()/4); |
reed@google.com | 50129db | 2012-04-30 12:07:55 +0000 | [diff] [blame] | 41 | (void)a.contains(r); |
| 42 | |
| 43 | r = b.getBounds(); |
| 44 | r.inset(r.width()/4, r.height()/4); |
reed@google.com | 71937d6 | 2012-04-30 13:54:36 +0000 | [diff] [blame] | 45 | return b.contains(r); |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 46 | } |
| 47 | |
reed@google.com | 4b4f86d | 2012-05-02 17:20:02 +0000 | [diff] [blame] | 48 | static bool sectsrgn_proc(SkRegion& a, SkRegion& b) { |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 49 | return a.intersects(b); |
| 50 | } |
| 51 | |
reed@google.com | 4b4f86d | 2012-05-02 17:20:02 +0000 | [diff] [blame] | 52 | static bool sectsrect_proc(SkRegion& a, SkRegion& b) { |
| 53 | SkIRect r = a.getBounds(); |
| 54 | r.inset(r.width()/4, r.height()/4); |
reed@google.com | 26dc5b6 | 2012-05-02 17:41:13 +0000 | [diff] [blame] | 55 | return a.intersects(r); |
reed@google.com | 4b4f86d | 2012-05-02 17:20:02 +0000 | [diff] [blame] | 56 | } |
| 57 | |
reed@google.com | 50129db | 2012-04-30 12:07:55 +0000 | [diff] [blame] | 58 | static bool containsxy_proc(SkRegion& a, SkRegion& b) { |
| 59 | const SkIRect& r = a.getBounds(); |
| 60 | const int dx = r.width() / 8; |
| 61 | const int dy = r.height() / 8; |
| 62 | for (int y = r.fTop; y < r.fBottom; y += dy) { |
| 63 | for (int x = r.fLeft; x < r.fRight; x += dx) { |
| 64 | (void)a.contains(x, y); |
| 65 | } |
| 66 | } |
| 67 | return true; |
| 68 | } |
| 69 | |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 70 | class RegionBench : public SkBenchmark { |
| 71 | public: |
| 72 | typedef bool (*Proc)(SkRegion& a, SkRegion& b); |
| 73 | |
| 74 | SkRegion fA, fB; |
| 75 | Proc fProc; |
| 76 | SkString fName; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 77 | |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 78 | enum { |
| 79 | W = 1024, |
| 80 | H = 768, |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 83 | SkIRect randrect(SkRandom& rand) { |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 84 | int x = rand.nextU() % W; |
| 85 | int y = rand.nextU() % H; |
| 86 | int w = rand.nextU() % W; |
| 87 | int h = rand.nextU() % H; |
| 88 | return SkIRect::MakeXYWH(x, y, w >> 1, h >> 1); |
| 89 | } |
| 90 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 91 | RegionBench(int count, Proc proc, const char name[]) { |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 92 | fProc = proc; |
reed@google.com | 515d998 | 2012-04-30 14:43:46 +0000 | [diff] [blame] | 93 | fName.printf("region_%s_%d", name, count); |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 94 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 95 | SkRandom rand; |
reed@google.com | 50129db | 2012-04-30 12:07:55 +0000 | [diff] [blame] | 96 | for (int i = 0; i < count; i++) { |
| 97 | fA.op(randrect(rand), SkRegion::kXOR_Op); |
| 98 | fB.op(randrect(rand), SkRegion::kXOR_Op); |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 99 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| 103 | return backend == kNonRendering_Backend; |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | protected: |
| 107 | virtual const char* onGetName() { return fName.c_str(); } |
| 108 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 109 | virtual void onDraw(const int loops, SkCanvas* canvas) { |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 110 | Proc proc = fProc; |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 111 | for (int i = 0; i < loops; ++i) { |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 112 | proc(fA, fB); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | private: |
| 117 | typedef SkBenchmark INHERITED; |
| 118 | }; |
| 119 | |
mike@reedtribe.org | ae8f952 | 2014-01-01 20:32:45 +0000 | [diff] [blame] | 120 | class RectSectBench : public SkBenchmark { |
| 121 | enum { |
| 122 | N = 1000 |
| 123 | }; |
| 124 | SkRect fArray0[N]; |
| 125 | SkRect fArray1[N]; |
| 126 | SkString fName; |
| 127 | bool fNewWay; |
| 128 | |
| 129 | public: |
| 130 | static void RandRect(SkRect* r, SkRandom& rand) { |
| 131 | r->set(rand.nextSScalar1(), rand.nextSScalar1(), |
| 132 | rand.nextSScalar1(), rand.nextSScalar1()); |
| 133 | r->sort(); |
| 134 | } |
skia.committer@gmail.com | f8affa2 | 2014-01-02 07:01:35 +0000 | [diff] [blame^] | 135 | |
mike@reedtribe.org | ae8f952 | 2014-01-01 20:32:45 +0000 | [diff] [blame] | 136 | RectSectBench(bool newWay) : fNewWay(newWay) { |
| 137 | fName.printf("rect_intersect_%s", newWay ? "new" : "old"); |
skia.committer@gmail.com | f8affa2 | 2014-01-02 07:01:35 +0000 | [diff] [blame^] | 138 | |
mike@reedtribe.org | ae8f952 | 2014-01-01 20:32:45 +0000 | [diff] [blame] | 139 | SkRandom rand; |
| 140 | for (int i = 0; i < N; i++) { |
| 141 | RandRect(&fArray0[i], rand); |
| 142 | RandRect(&fArray1[i], rand); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| 147 | return backend == kNonRendering_Backend; |
| 148 | } |
| 149 | |
| 150 | protected: |
| 151 | virtual const char* onGetName() { return fName.c_str(); } |
skia.committer@gmail.com | f8affa2 | 2014-01-02 07:01:35 +0000 | [diff] [blame^] | 152 | |
mike@reedtribe.org | ae8f952 | 2014-01-01 20:32:45 +0000 | [diff] [blame] | 153 | virtual void onDraw(const int loops, SkCanvas* canvas) { |
| 154 | for (int i = 0; i < loops; ++i) { |
| 155 | if (fNewWay) { |
| 156 | for (int j = 0; j < N; ++j) { |
| 157 | SkRect r = fArray0[j]; |
| 158 | r.intersect2(fArray1[j]); |
| 159 | } |
| 160 | } else { |
| 161 | for (int j = 0; j < N; ++j) { |
| 162 | SkRect r = fArray0[j]; |
| 163 | r.intersect(fArray1[j]); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
skia.committer@gmail.com | f8affa2 | 2014-01-02 07:01:35 +0000 | [diff] [blame^] | 168 | |
mike@reedtribe.org | ae8f952 | 2014-01-01 20:32:45 +0000 | [diff] [blame] | 169 | private: |
| 170 | typedef SkBenchmark INHERITED; |
| 171 | }; |
| 172 | |
| 173 | /////////////////////////////////////////////////////////////////////////////// |
| 174 | |
reed@google.com | 50129db | 2012-04-30 12:07:55 +0000 | [diff] [blame] | 175 | #define SMALL 16 |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 176 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 177 | DEF_BENCH( return SkNEW_ARGS(RegionBench, (SMALL, union_proc, "union")); ) |
| 178 | DEF_BENCH( return SkNEW_ARGS(RegionBench, (SMALL, sect_proc, "intersect")); ) |
| 179 | DEF_BENCH( return SkNEW_ARGS(RegionBench, (SMALL, diff_proc, "difference")); ) |
| 180 | DEF_BENCH( return SkNEW_ARGS(RegionBench, (SMALL, diffrect_proc, "differencerect")); ) |
| 181 | DEF_BENCH( return SkNEW_ARGS(RegionBench, (SMALL, diffrectbig_proc, "differencerectbig")); ) |
| 182 | DEF_BENCH( return SkNEW_ARGS(RegionBench, (SMALL, containsrect_proc, "containsrect")); ) |
| 183 | DEF_BENCH( return SkNEW_ARGS(RegionBench, (SMALL, sectsrgn_proc, "intersectsrgn")); ) |
| 184 | DEF_BENCH( return SkNEW_ARGS(RegionBench, (SMALL, sectsrect_proc, "intersectsrect")); ) |
| 185 | DEF_BENCH( return SkNEW_ARGS(RegionBench, (SMALL, containsxy_proc, "containsxy")); ) |
mike@reedtribe.org | ae8f952 | 2014-01-01 20:32:45 +0000 | [diff] [blame] | 186 | |
| 187 | DEF_BENCH( return SkNEW_ARGS(RectSectBench, (false)); ) |
| 188 | DEF_BENCH( return SkNEW_ARGS(RectSectBench, (true)); ) |