epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +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 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 7 | #include "Benchmark.h" |
reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 8 | #include "SkMatrix.h" |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 9 | #include "SkMatrixUtils.h" |
tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 10 | #include "SkRandom.h" |
reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 11 | #include "SkString.h" |
| 12 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 13 | class MatrixBench : public Benchmark { |
reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 14 | SkString fName; |
reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 15 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 16 | MatrixBench(const char name[]) { |
reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 17 | fName.printf("matrix_%s", name); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 18 | } |
| 19 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 20 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 21 | return backend == kNonRendering_Backend; |
reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | virtual void performTest() = 0; |
| 25 | |
| 26 | protected: |
reed@google.com | cbefd7d | 2011-06-06 13:31:30 +0000 | [diff] [blame] | 27 | virtual int mulLoopCount() const { return 1; } |
| 28 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 29 | const char* onGetName() override { |
reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 30 | return fName.c_str(); |
| 31 | } |
| 32 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 33 | void onDraw(int loops, SkCanvas*) override { |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 34 | for (int i = 0; i < loops; i++) { |
reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 35 | this->performTest(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 40 | typedef Benchmark INHERITED; |
reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 43 | |
| 44 | class EqualsMatrixBench : public MatrixBench { |
| 45 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 46 | EqualsMatrixBench() : INHERITED("equals") {} |
reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 47 | protected: |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 48 | void performTest() override { |
reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 49 | SkMatrix m0, m1, m2; |
tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 50 | |
reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 51 | m0.reset(); |
| 52 | m1.reset(); |
| 53 | m2.reset(); |
mtklein | cf44feb | 2014-06-02 18:22:12 -0700 | [diff] [blame] | 54 | |
| 55 | // xor into a volatile prevents these comparisons from being optimized away. |
| 56 | volatile bool junk = false; |
| 57 | junk ^= (m0 == m1); |
| 58 | junk ^= (m1 == m2); |
| 59 | junk ^= (m2 == m0); |
reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 60 | } |
| 61 | private: |
| 62 | typedef MatrixBench INHERITED; |
| 63 | }; |
| 64 | |
| 65 | class ScaleMatrixBench : public MatrixBench { |
| 66 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 67 | ScaleMatrixBench() : INHERITED("scale") { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 68 | fSX = fSY = 1.5f; |
reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 69 | fM0.reset(); |
| 70 | fM1.setScale(fSX, fSY); |
| 71 | fM2.setTranslate(fSX, fSY); |
reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 72 | } |
| 73 | protected: |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 74 | void performTest() override { |
reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 75 | SkMatrix m; |
| 76 | m = fM0; m.preScale(fSX, fSY); |
| 77 | m = fM1; m.preScale(fSX, fSY); |
| 78 | m = fM2; m.preScale(fSX, fSY); |
| 79 | } |
| 80 | private: |
| 81 | SkMatrix fM0, fM1, fM2; |
| 82 | SkScalar fSX, fSY; |
| 83 | typedef MatrixBench INHERITED; |
| 84 | }; |
| 85 | |
reed@google.com | e0dcde7 | 2011-06-06 13:20:29 +0000 | [diff] [blame] | 86 | // having unknown values in our arrays can throw off the timing a lot, perhaps |
| 87 | // handling NaN values is a lot slower. Anyway, this guy is just meant to put |
| 88 | // reasonable values in our arrays. |
| 89 | template <typename T> void init9(T array[9]) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 90 | SkRandom rand; |
reed@google.com | e0dcde7 | 2011-06-06 13:20:29 +0000 | [diff] [blame] | 91 | for (int i = 0; i < 9; i++) { |
| 92 | array[i] = rand.nextSScalar1(); |
| 93 | } |
| 94 | } |
| 95 | |
tomhudson@google.com | 317d540 | 2011-06-24 18:30:49 +0000 | [diff] [blame] | 96 | class GetTypeMatrixBench : public MatrixBench { |
| 97 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 98 | GetTypeMatrixBench() |
| 99 | : INHERITED("gettype") { |
bsalomon@google.com | 820e80a | 2011-10-24 21:09:40 +0000 | [diff] [blame] | 100 | fArray[0] = (float) fRnd.nextS(); |
| 101 | fArray[1] = (float) fRnd.nextS(); |
| 102 | fArray[2] = (float) fRnd.nextS(); |
| 103 | fArray[3] = (float) fRnd.nextS(); |
| 104 | fArray[4] = (float) fRnd.nextS(); |
| 105 | fArray[5] = (float) fRnd.nextS(); |
| 106 | fArray[6] = (float) fRnd.nextS(); |
| 107 | fArray[7] = (float) fRnd.nextS(); |
| 108 | fArray[8] = (float) fRnd.nextS(); |
tomhudson@google.com | 317d540 | 2011-06-24 18:30:49 +0000 | [diff] [blame] | 109 | } |
| 110 | protected: |
| 111 | // Putting random generation of the matrix inside performTest() |
| 112 | // would help us avoid anomalous runs, but takes up 25% or |
| 113 | // more of the function time. |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 114 | void performTest() override { |
tomhudson@google.com | 317d540 | 2011-06-24 18:30:49 +0000 | [diff] [blame] | 115 | fMatrix.setAll(fArray[0], fArray[1], fArray[2], |
| 116 | fArray[3], fArray[4], fArray[5], |
| 117 | fArray[6], fArray[7], fArray[8]); |
mtklein | cf44feb | 2014-06-02 18:22:12 -0700 | [diff] [blame] | 118 | // xoring into a volatile prevents the compiler from optimizing these away |
| 119 | volatile int junk = 0; |
| 120 | junk ^= (fMatrix.getType()); |
tomhudson@google.com | 317d540 | 2011-06-24 18:30:49 +0000 | [diff] [blame] | 121 | fMatrix.dirtyMatrixTypeCache(); |
mtklein | cf44feb | 2014-06-02 18:22:12 -0700 | [diff] [blame] | 122 | junk ^= (fMatrix.getType()); |
tomhudson@google.com | 317d540 | 2011-06-24 18:30:49 +0000 | [diff] [blame] | 123 | fMatrix.dirtyMatrixTypeCache(); |
mtklein | cf44feb | 2014-06-02 18:22:12 -0700 | [diff] [blame] | 124 | junk ^= (fMatrix.getType()); |
tomhudson@google.com | 317d540 | 2011-06-24 18:30:49 +0000 | [diff] [blame] | 125 | fMatrix.dirtyMatrixTypeCache(); |
mtklein | cf44feb | 2014-06-02 18:22:12 -0700 | [diff] [blame] | 126 | junk ^= (fMatrix.getType()); |
tomhudson@google.com | 317d540 | 2011-06-24 18:30:49 +0000 | [diff] [blame] | 127 | fMatrix.dirtyMatrixTypeCache(); |
mtklein | cf44feb | 2014-06-02 18:22:12 -0700 | [diff] [blame] | 128 | junk ^= (fMatrix.getType()); |
tomhudson@google.com | 317d540 | 2011-06-24 18:30:49 +0000 | [diff] [blame] | 129 | fMatrix.dirtyMatrixTypeCache(); |
mtklein | cf44feb | 2014-06-02 18:22:12 -0700 | [diff] [blame] | 130 | junk ^= (fMatrix.getType()); |
tomhudson@google.com | 317d540 | 2011-06-24 18:30:49 +0000 | [diff] [blame] | 131 | fMatrix.dirtyMatrixTypeCache(); |
mtklein | cf44feb | 2014-06-02 18:22:12 -0700 | [diff] [blame] | 132 | junk ^= (fMatrix.getType()); |
tomhudson@google.com | 317d540 | 2011-06-24 18:30:49 +0000 | [diff] [blame] | 133 | fMatrix.dirtyMatrixTypeCache(); |
mtklein | cf44feb | 2014-06-02 18:22:12 -0700 | [diff] [blame] | 134 | junk ^= (fMatrix.getType()); |
tomhudson@google.com | 317d540 | 2011-06-24 18:30:49 +0000 | [diff] [blame] | 135 | } |
| 136 | private: |
| 137 | SkMatrix fMatrix; |
| 138 | float fArray[9]; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 139 | SkRandom fRnd; |
tomhudson@google.com | 317d540 | 2011-06-24 18:30:49 +0000 | [diff] [blame] | 140 | typedef MatrixBench INHERITED; |
| 141 | }; |
| 142 | |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 143 | class DecomposeMatrixBench : public MatrixBench { |
| 144 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 145 | DecomposeMatrixBench() : INHERITED("decompose") {} |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 146 | |
| 147 | protected: |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 148 | void onDelayedSetup() override { |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 149 | for (int i = 0; i < 10; ++i) { |
| 150 | SkScalar rot0 = (fRandom.nextBool()) ? fRandom.nextRangeF(-180, 180) : 0.0f; |
| 151 | SkScalar sx = fRandom.nextRangeF(-3000.f, 3000.f); |
| 152 | SkScalar sy = (fRandom.nextBool()) ? fRandom.nextRangeF(-3000.f, 3000.f) : sx; |
| 153 | SkScalar rot1 = fRandom.nextRangeF(-180, 180); |
| 154 | fMatrix[i].setRotate(rot0); |
| 155 | fMatrix[i].postScale(sx, sy); |
| 156 | fMatrix[i].postRotate(rot1); |
| 157 | } |
| 158 | } |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 159 | void performTest() override { |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 160 | SkPoint rotation1, scale, rotation2; |
| 161 | for (int i = 0; i < 10; ++i) { |
| 162 | (void) SkDecomposeUpper2x2(fMatrix[i], &rotation1, &scale, &rotation2); |
| 163 | } |
| 164 | } |
| 165 | private: |
| 166 | SkMatrix fMatrix[10]; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 167 | SkRandom fRandom; |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 168 | typedef MatrixBench INHERITED; |
| 169 | }; |
| 170 | |
junov@chromium.org | 6fc5699 | 2012-07-12 14:01:32 +0000 | [diff] [blame] | 171 | class InvertMapRectMatrixBench : public MatrixBench { |
| 172 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 173 | InvertMapRectMatrixBench(const char* name, int flags) |
| 174 | : INHERITED(name) |
junov@chromium.org | 6fc5699 | 2012-07-12 14:01:32 +0000 | [diff] [blame] | 175 | , fFlags(flags) { |
| 176 | fMatrix.reset(); |
| 177 | fIteration = 0; |
| 178 | if (flags & kScale_Flag) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 179 | fMatrix.postScale(1.5f, 2.5f); |
junov@chromium.org | 6fc5699 | 2012-07-12 14:01:32 +0000 | [diff] [blame] | 180 | } |
| 181 | if (flags & kTranslate_Flag) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 182 | fMatrix.postTranslate(1.5f, 2.5f); |
junov@chromium.org | 6fc5699 | 2012-07-12 14:01:32 +0000 | [diff] [blame] | 183 | } |
| 184 | if (flags & kRotate_Flag) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 185 | fMatrix.postRotate(45.0f); |
junov@chromium.org | 6fc5699 | 2012-07-12 14:01:32 +0000 | [diff] [blame] | 186 | } |
| 187 | if (flags & kPerspective_Flag) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 188 | fMatrix.setPerspX(1.5f); |
| 189 | fMatrix.setPerspY(2.5f); |
junov@chromium.org | 6fc5699 | 2012-07-12 14:01:32 +0000 | [diff] [blame] | 190 | } |
| 191 | if (0 == (flags & kUncachedTypeMask_Flag)) { |
| 192 | fMatrix.getType(); |
| 193 | } |
| 194 | } |
| 195 | enum Flag { |
| 196 | kScale_Flag = 0x01, |
| 197 | kTranslate_Flag = 0x02, |
| 198 | kRotate_Flag = 0x04, |
| 199 | kPerspective_Flag = 0x08, |
| 200 | kUncachedTypeMask_Flag = 0x10, |
| 201 | }; |
| 202 | protected: |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 203 | void performTest() override { |
junov@chromium.org | 6fc5699 | 2012-07-12 14:01:32 +0000 | [diff] [blame] | 204 | if (fFlags & kUncachedTypeMask_Flag) { |
| 205 | // This will invalidate the typemask without |
| 206 | // changing the matrix. |
| 207 | fMatrix.setPerspX(fMatrix.getPerspX()); |
| 208 | } |
| 209 | SkMatrix inv; |
robertphillips@google.com | 31f8f73 | 2012-09-17 13:36:47 +0000 | [diff] [blame] | 210 | bool invertible = fMatrix.invert(&inv); |
junov@chromium.org | 6fc5699 | 2012-07-12 14:01:32 +0000 | [diff] [blame] | 211 | SkASSERT(invertible); |
| 212 | SkRect transformedRect; |
robertphillips@google.com | 31f8f73 | 2012-09-17 13:36:47 +0000 | [diff] [blame] | 213 | // an arbitrary, small, non-zero rect to transform |
| 214 | SkRect srcRect = SkRect::MakeWH(SkIntToScalar(10), SkIntToScalar(10)); |
junov@chromium.org | 6fc5699 | 2012-07-12 14:01:32 +0000 | [diff] [blame] | 215 | if (invertible) { |
robertphillips@google.com | 31f8f73 | 2012-09-17 13:36:47 +0000 | [diff] [blame] | 216 | inv.mapRect(&transformedRect, srcRect); |
junov@chromium.org | 6fc5699 | 2012-07-12 14:01:32 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | private: |
| 220 | SkMatrix fMatrix; |
junov@chromium.org | 6fc5699 | 2012-07-12 14:01:32 +0000 | [diff] [blame] | 221 | int fFlags; |
| 222 | unsigned fIteration; |
| 223 | typedef MatrixBench INHERITED; |
| 224 | }; |
tomhudson@google.com | 288ff33 | 2011-06-07 14:31:38 +0000 | [diff] [blame] | 225 | |
reed@google.com | 82bf8bb | 2013-01-04 14:04:08 +0000 | [diff] [blame] | 226 | /////////////////////////////////////////////////////////////////////////////// |
tomhudson@google.com | 288ff33 | 2011-06-07 14:31:38 +0000 | [diff] [blame] | 227 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 228 | DEF_BENCH( return new EqualsMatrixBench(); ) |
| 229 | DEF_BENCH( return new ScaleMatrixBench(); ) |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 230 | DEF_BENCH( return new GetTypeMatrixBench(); ) |
| 231 | DEF_BENCH( return new DecomposeMatrixBench(); ) |
commit-bot@chromium.org | 5b2e264 | 2013-09-03 19:08:14 +0000 | [diff] [blame] | 232 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 233 | DEF_BENCH( return new InvertMapRectMatrixBench("invert_maprect_identity", 0); ) |
tomhudson@google.com | 288ff33 | 2011-06-07 14:31:38 +0000 | [diff] [blame] | 234 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 235 | DEF_BENCH(return new InvertMapRectMatrixBench( |
reed@google.com | 90533be | 2013-01-04 15:46:42 +0000 | [diff] [blame] | 236 | "invert_maprect_rectstaysrect", |
| 237 | InvertMapRectMatrixBench::kScale_Flag | |
| 238 | InvertMapRectMatrixBench::kTranslate_Flag); ) |
| 239 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 240 | DEF_BENCH(return new InvertMapRectMatrixBench( |
reed@google.com | 90533be | 2013-01-04 15:46:42 +0000 | [diff] [blame] | 241 | "invert_maprect_translate", |
| 242 | InvertMapRectMatrixBench::kTranslate_Flag); ) |
tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 243 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 244 | DEF_BENCH(return new InvertMapRectMatrixBench( |
reed@google.com | 82bf8bb | 2013-01-04 14:04:08 +0000 | [diff] [blame] | 245 | "invert_maprect_nonpersp", |
| 246 | InvertMapRectMatrixBench::kScale_Flag | |
| 247 | InvertMapRectMatrixBench::kRotate_Flag | |
| 248 | InvertMapRectMatrixBench::kTranslate_Flag); ) |
reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 249 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 250 | DEF_BENCH( return new InvertMapRectMatrixBench( |
reed@google.com | 82bf8bb | 2013-01-04 14:04:08 +0000 | [diff] [blame] | 251 | "invert_maprect_persp", |
| 252 | InvertMapRectMatrixBench::kPerspective_Flag); ) |
tomhudson@google.com | 288ff33 | 2011-06-07 14:31:38 +0000 | [diff] [blame] | 253 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 254 | DEF_BENCH( return new InvertMapRectMatrixBench( |
reed@google.com | 82bf8bb | 2013-01-04 14:04:08 +0000 | [diff] [blame] | 255 | "invert_maprect_typemask_rectstaysrect", |
| 256 | InvertMapRectMatrixBench::kUncachedTypeMask_Flag | |
| 257 | InvertMapRectMatrixBench::kScale_Flag | |
| 258 | InvertMapRectMatrixBench::kTranslate_Flag); ) |
| 259 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 260 | DEF_BENCH( return new InvertMapRectMatrixBench( |
reed@google.com | 82bf8bb | 2013-01-04 14:04:08 +0000 | [diff] [blame] | 261 | "invert_maprect_typemask_nonpersp", |
| 262 | InvertMapRectMatrixBench::kUncachedTypeMask_Flag | |
| 263 | InvertMapRectMatrixBench::kScale_Flag | |
| 264 | InvertMapRectMatrixBench::kRotate_Flag | |
| 265 | InvertMapRectMatrixBench::kTranslate_Flag); ) |
| 266 | |
reed | 8010632 | 2015-03-23 08:15:14 -0700 | [diff] [blame] | 267 | /////////////////////////////////////////////////////////////////////////////// |
| 268 | |
reed | d8b34c2 | 2015-03-27 14:00:41 -0700 | [diff] [blame] | 269 | static SkMatrix make_trans() { return SkMatrix::MakeTrans(2, 3); } |
reed | 8010632 | 2015-03-23 08:15:14 -0700 | [diff] [blame] | 270 | static SkMatrix make_scale() { SkMatrix m(make_trans()); m.postScale(1.5f, 0.5f); return m; } |
| 271 | static SkMatrix make_afine() { SkMatrix m(make_trans()); m.postRotate(15); return m; } |
| 272 | |
| 273 | class MapPointsMatrixBench : public MatrixBench { |
| 274 | protected: |
| 275 | SkMatrix fM; |
| 276 | enum { |
| 277 | N = 32 |
| 278 | }; |
| 279 | SkPoint fSrc[N], fDst[N]; |
reed | 8010632 | 2015-03-23 08:15:14 -0700 | [diff] [blame] | 280 | public: |
reed | 7da1901 | 2015-03-29 11:58:48 -0700 | [diff] [blame] | 281 | MapPointsMatrixBench(const char name[], const SkMatrix& m) |
| 282 | : MatrixBench(name), fM(m) |
reed | 8010632 | 2015-03-23 08:15:14 -0700 | [diff] [blame] | 283 | { |
| 284 | SkRandom rand; |
| 285 | for (int i = 0; i < N; ++i) { |
| 286 | fSrc[i].set(rand.nextSScalar1(), rand.nextSScalar1()); |
| 287 | } |
| 288 | } |
| 289 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 290 | void performTest() override { |
reed | 7da1901 | 2015-03-29 11:58:48 -0700 | [diff] [blame] | 291 | for (int i = 0; i < 1000000; ++i) { |
| 292 | fM.mapPoints(fDst, fSrc, N); |
reed | 8010632 | 2015-03-23 08:15:14 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | }; |
reed | 7da1901 | 2015-03-29 11:58:48 -0700 | [diff] [blame] | 296 | DEF_BENCH( return new MapPointsMatrixBench("mappoints_identity", SkMatrix::I()); ) |
| 297 | DEF_BENCH( return new MapPointsMatrixBench("mappoints_trans", make_trans()); ) |
| 298 | DEF_BENCH( return new MapPointsMatrixBench("mappoints_scale", make_scale()); ) |
| 299 | DEF_BENCH( return new MapPointsMatrixBench("mappoints_affine", make_afine()); ) |
reed | 47df89e | 2016-06-30 06:38:54 -0700 | [diff] [blame] | 300 | |
| 301 | /////////////////////////////////////////////////////////////////////////////// |
| 302 | |
| 303 | class MapRectMatrixBench : public MatrixBench { |
| 304 | SkMatrix fM; |
| 305 | SkRect fR; |
| 306 | bool fScaleTrans; |
| 307 | |
| 308 | enum { MEGA_LOOP = 1000 * 1000 }; |
| 309 | public: |
| 310 | MapRectMatrixBench(const char name[], bool scale_trans) |
| 311 | : MatrixBench(name), fScaleTrans(scale_trans) |
| 312 | { |
| 313 | fM.setScale(2, 3); |
| 314 | fM.postTranslate(1, 2); |
| 315 | |
| 316 | fR.set(10, 10, 100, 200); |
| 317 | } |
| 318 | |
| 319 | void performTest() override { |
| 320 | SkRect dst; |
| 321 | if (fScaleTrans) { |
| 322 | for (int i = 0; i < MEGA_LOOP; ++i) { |
halcanary | c5769b2 | 2016-08-10 07:13:21 -0700 | [diff] [blame] | 323 | fM.mapRectScaleTranslate(&dst, fR); |
reed | 47df89e | 2016-06-30 06:38:54 -0700 | [diff] [blame] | 324 | } |
| 325 | } else { |
| 326 | for (int i = 0; i < MEGA_LOOP; ++i) { |
| 327 | fM.mapRect(&dst, fR); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | }; |
| 332 | DEF_BENCH( return new MapRectMatrixBench("maprect", false); ) |
| 333 | DEF_BENCH( return new MapRectMatrixBench("maprectscaletrans", true); ) |