reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | */ |
| 7 | |
| 8 | #include "SkBenchmark.h" |
| 9 | #include "SkMatrix44.h" |
| 10 | #include "SkRandom.h" |
| 11 | #include "SkString.h" |
| 12 | |
| 13 | class Matrix44Bench : public SkBenchmark { |
| 14 | SkString fName; |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 15 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 16 | Matrix44Bench(const char name[]) { |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 17 | fName.printf("matrix44_%s", name); |
| 18 | fIsRendering = false; |
| 19 | } |
| 20 | |
| 21 | virtual void performTest() = 0; |
| 22 | |
| 23 | protected: |
| 24 | virtual int mulLoopCount() const { return 1; } |
| 25 | |
| 26 | virtual const char* onGetName() { |
| 27 | return fName.c_str(); |
| 28 | } |
| 29 | |
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 30 | virtual void onDraw(SkCanvas*) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 31 | for (int i = 0; i < this->getLoops(); i++) { |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 32 | this->performTest(); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | private: |
| 37 | typedef SkBenchmark INHERITED; |
| 38 | }; |
| 39 | |
| 40 | class EqualsMatrix44Bench : public Matrix44Bench { |
| 41 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 42 | EqualsMatrix44Bench() : INHERITED("equals") { |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 43 | fM1.set(0, 0, 0); |
| 44 | fM2.set(3, 3, 0); |
| 45 | } |
| 46 | protected: |
| 47 | virtual void performTest() { |
| 48 | for (int i = 0; i < 10; ++i) { |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 49 | (void) (fM0 == fM1); |
| 50 | (void) (fM1 == fM2); |
| 51 | (void) (fM2 == fM0); |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | private: |
| 55 | SkMatrix44 fM0, fM1, fM2; |
| 56 | typedef Matrix44Bench INHERITED; |
| 57 | }; |
| 58 | |
shawnsingh@chromium.org | 63bf68d | 2013-08-28 05:07:26 +0000 | [diff] [blame] | 59 | class SetIdentityMatrix44Bench : public Matrix44Bench { |
| 60 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 61 | SetIdentityMatrix44Bench() : INHERITED("setidentity") { |
shawnsingh@chromium.org | 63bf68d | 2013-08-28 05:07:26 +0000 | [diff] [blame] | 62 | double rowMajor[16] = |
| 63 | { 1, 2, 3, 4, |
| 64 | 5, 6, 7, 8, |
| 65 | 9, 10, 11, 12, |
| 66 | 13, 14, 15, 16}; |
| 67 | mat.setRowMajord(rowMajor); |
| 68 | } |
| 69 | protected: |
| 70 | virtual void performTest() { |
| 71 | for (int i = 0; i < 10; ++i) { |
| 72 | mat.setIdentity(); |
| 73 | } |
| 74 | } |
| 75 | private: |
| 76 | SkMatrix44 mat; |
| 77 | typedef Matrix44Bench INHERITED; |
| 78 | }; |
| 79 | |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 80 | class PreScaleMatrix44Bench : public Matrix44Bench { |
| 81 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 82 | PreScaleMatrix44Bench() : INHERITED("prescale") { |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 83 | fX = fY = fZ = SkDoubleToMScalar(1.5); |
| 84 | } |
| 85 | protected: |
| 86 | virtual void performTest() { |
| 87 | fM0.reset(); |
| 88 | for (int i = 0; i < 10; ++i) { |
| 89 | fM0.preScale(fX, fY, fZ); |
| 90 | } |
| 91 | } |
| 92 | private: |
| 93 | SkMatrix44 fM0; |
| 94 | SkMScalar fX, fY, fZ; |
| 95 | typedef Matrix44Bench INHERITED; |
| 96 | }; |
| 97 | |
tomhudson@google.com | 9973a8a | 2012-12-13 09:55:42 +0000 | [diff] [blame] | 98 | class InvertMatrix44Bench : public Matrix44Bench { |
| 99 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 100 | InvertMatrix44Bench() : INHERITED("invert") { |
commit-bot@chromium.org | 9504575 | 2013-08-20 20:15:24 +0000 | [diff] [blame] | 101 | fM0.set(0, 0, -1.1); |
| 102 | fM0.set(0, 1, 2.1); |
| 103 | fM0.set(0, 2, -3.1); |
| 104 | fM0.set(0, 3, 4.1); |
| 105 | fM0.set(1, 0, 5.1); |
| 106 | fM0.set(1, 1, -6.1); |
| 107 | fM0.set(1, 2, 7.1); |
| 108 | fM0.set(1, 3, 8.1); |
| 109 | fM0.set(2, 0, -9.1); |
| 110 | fM0.set(2, 1, 10.1); |
| 111 | fM0.set(2, 2, 11.1); |
| 112 | fM0.set(2, 3, -12.1); |
| 113 | fM0.set(3, 0, -13.1); |
| 114 | fM0.set(3, 1, 14.1); |
| 115 | fM0.set(3, 2, -15.1); |
| 116 | fM0.set(3, 3, 16.1); |
| 117 | } |
| 118 | protected: |
| 119 | virtual void performTest() { |
| 120 | for (int i = 0; i < 10; ++i) { |
| 121 | fM0.invert(&fM1); |
| 122 | } |
| 123 | } |
| 124 | private: |
| 125 | SkMatrix44 fM0, fM1; |
| 126 | typedef Matrix44Bench INHERITED; |
| 127 | }; |
| 128 | |
| 129 | class InvertAffineMatrix44Bench : public Matrix44Bench { |
| 130 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 131 | InvertAffineMatrix44Bench() : INHERITED("invertaffine") { |
commit-bot@chromium.org | 9504575 | 2013-08-20 20:15:24 +0000 | [diff] [blame] | 132 | fM0.set(0, 0, -1.1); |
| 133 | fM0.set(0, 1, 2.1); |
| 134 | fM0.set(0, 2, -3.1); |
| 135 | fM0.set(0, 3, 4.1); |
| 136 | fM0.set(1, 0, 5.1); |
| 137 | fM0.set(1, 1, -6.1); |
| 138 | fM0.set(1, 2, 7.1); |
| 139 | fM0.set(1, 3, 8.1); |
| 140 | fM0.set(2, 0, -9.1); |
| 141 | fM0.set(2, 1, 10.1); |
| 142 | fM0.set(2, 2, 11.1); |
| 143 | fM0.set(2, 3, -12.1); |
| 144 | // bottom row (perspective component) remains (0, 0, 0, 1). |
| 145 | } |
| 146 | protected: |
| 147 | virtual void performTest() { |
| 148 | for (int i = 0; i < 10; ++i) { |
| 149 | fM0.invert(&fM1); |
| 150 | } |
| 151 | } |
| 152 | private: |
| 153 | SkMatrix44 fM0, fM1; |
| 154 | typedef Matrix44Bench INHERITED; |
| 155 | }; |
| 156 | |
| 157 | class InvertScaleTranslateMatrix44Bench : public Matrix44Bench { |
| 158 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 159 | InvertScaleTranslateMatrix44Bench() : INHERITED("invertscaletranslate") { |
commit-bot@chromium.org | 9504575 | 2013-08-20 20:15:24 +0000 | [diff] [blame] | 160 | fM0.set(0, 0, -1.1); |
| 161 | fM0.set(0, 3, 4.1); |
| 162 | |
| 163 | fM0.set(1, 1, -6.1); |
| 164 | fM0.set(1, 3, 8.1); |
| 165 | |
| 166 | fM0.set(2, 2, 11.1); |
| 167 | fM0.set(2, 3, -12.1); |
| 168 | } |
| 169 | protected: |
| 170 | virtual void performTest() { |
| 171 | for (int i = 0; i < 10; ++i) { |
| 172 | fM0.invert(&fM1); |
| 173 | } |
| 174 | } |
| 175 | private: |
| 176 | SkMatrix44 fM0, fM1; |
| 177 | typedef Matrix44Bench INHERITED; |
| 178 | }; |
| 179 | |
| 180 | class InvertTranslateMatrix44Bench : public Matrix44Bench { |
| 181 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 182 | InvertTranslateMatrix44Bench() : INHERITED("inverttranslate") { |
commit-bot@chromium.org | 9504575 | 2013-08-20 20:15:24 +0000 | [diff] [blame] | 183 | fM0.set(0, 3, 4.1); |
| 184 | fM0.set(1, 3, 8.1); |
| 185 | fM0.set(2, 3, -12.1); |
tomhudson@google.com | 9973a8a | 2012-12-13 09:55:42 +0000 | [diff] [blame] | 186 | } |
| 187 | protected: |
| 188 | virtual void performTest() { |
| 189 | for (int i = 0; i < 10; ++i) { |
| 190 | fM0.invert(&fM1); |
| 191 | } |
| 192 | } |
| 193 | private: |
| 194 | SkMatrix44 fM0, fM1; |
| 195 | typedef Matrix44Bench INHERITED; |
| 196 | }; |
| 197 | |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 198 | class PostScaleMatrix44Bench : public Matrix44Bench { |
| 199 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 200 | PostScaleMatrix44Bench() : INHERITED("postscale") { |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 201 | fX = fY = fZ = SkDoubleToMScalar(1.5); |
| 202 | } |
| 203 | protected: |
| 204 | virtual void performTest() { |
| 205 | fM0.reset(); |
| 206 | for (int i = 0; i < 10; ++i) { |
| 207 | fM0.postScale(fX, fY, fZ); |
| 208 | } |
| 209 | } |
| 210 | private: |
| 211 | SkMatrix44 fM0; |
| 212 | SkMScalar fX, fY, fZ; |
| 213 | typedef Matrix44Bench INHERITED; |
| 214 | }; |
| 215 | |
| 216 | class SetConcatMatrix44Bench : public Matrix44Bench { |
| 217 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 218 | SetConcatMatrix44Bench() : INHERITED("setconcat") { |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 219 | fX = fY = fZ = SkDoubleToMScalar(1.5); |
| 220 | fM1.setScale(fX, fY, fZ); |
| 221 | fM2.setTranslate(fX, fY, fZ); |
| 222 | } |
| 223 | protected: |
| 224 | virtual void performTest() { |
| 225 | fM0.reset(); // just to normalize this test with prescale/postscale |
| 226 | for (int i = 0; i < 10; ++i) { |
| 227 | fM0.setConcat(fM1, fM2); |
| 228 | } |
| 229 | } |
| 230 | private: |
| 231 | SkMatrix44 fM0, fM1, fM2; |
| 232 | SkMScalar fX, fY, fZ; |
| 233 | typedef Matrix44Bench INHERITED; |
| 234 | }; |
| 235 | |
| 236 | class GetTypeMatrix44Bench : public Matrix44Bench { |
| 237 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 238 | GetTypeMatrix44Bench() : INHERITED("gettype") {} |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 239 | protected: |
| 240 | // Putting random generation of the matrix inside performTest() |
| 241 | // would help us avoid anomalous runs, but takes up 25% or |
| 242 | // more of the function time. |
| 243 | virtual void performTest() { |
| 244 | for (int i = 0; i < 20; ++i) { |
| 245 | fMatrix.set(1, 2, 1); // to invalidate the type-cache |
| 246 | fMatrix.getType(); |
| 247 | } |
| 248 | } |
| 249 | private: |
| 250 | SkMatrix44 fMatrix; |
| 251 | typedef Matrix44Bench INHERITED; |
| 252 | }; |
| 253 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 254 | DEF_BENCH( return new SetIdentityMatrix44Bench(); ) |
| 255 | DEF_BENCH( return new EqualsMatrix44Bench(); ) |
| 256 | DEF_BENCH( return new PreScaleMatrix44Bench(); ) |
| 257 | DEF_BENCH( return new PostScaleMatrix44Bench(); ) |
| 258 | DEF_BENCH( return new InvertMatrix44Bench(); ) |
| 259 | DEF_BENCH( return new InvertAffineMatrix44Bench(); ) |
| 260 | DEF_BENCH( return new InvertScaleTranslateMatrix44Bench(); ) |
| 261 | DEF_BENCH( return new InvertTranslateMatrix44Bench(); ) |
| 262 | DEF_BENCH( return new SetConcatMatrix44Bench(); ) |
| 263 | DEF_BENCH( return new GetTypeMatrix44Bench(); ) |