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