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