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