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; |
| 15 | enum { N = 10000 }; |
| 16 | public: |
| 17 | Matrix44Bench(void* param, const char name[]) : INHERITED(param) { |
| 18 | fName.printf("matrix44_%s", name); |
| 19 | fIsRendering = false; |
| 20 | } |
| 21 | |
| 22 | virtual void performTest() = 0; |
| 23 | |
| 24 | protected: |
| 25 | virtual int mulLoopCount() const { return 1; } |
| 26 | |
| 27 | virtual const char* onGetName() { |
| 28 | return fName.c_str(); |
| 29 | } |
| 30 | |
| 31 | virtual void onDraw(SkCanvas* canvas) { |
| 32 | int n = SkBENCHLOOP(N * this->mulLoopCount()); |
| 33 | for (int i = 0; i < n; i++) { |
| 34 | this->performTest(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | private: |
| 39 | typedef SkBenchmark INHERITED; |
| 40 | }; |
| 41 | |
| 42 | class EqualsMatrix44Bench : public Matrix44Bench { |
| 43 | public: |
| 44 | EqualsMatrix44Bench(void* param) : INHERITED(param, "equals") { |
| 45 | fM1.set(0, 0, 0); |
| 46 | fM2.set(3, 3, 0); |
| 47 | } |
| 48 | protected: |
| 49 | virtual void performTest() { |
| 50 | for (int i = 0; i < 10; ++i) { |
| 51 | fM0 == fM1; |
| 52 | fM1 == fM2; |
| 53 | fM2 == fM0; |
| 54 | } |
| 55 | } |
| 56 | private: |
| 57 | SkMatrix44 fM0, fM1, fM2; |
| 58 | typedef Matrix44Bench INHERITED; |
| 59 | }; |
| 60 | |
| 61 | class PreScaleMatrix44Bench : public Matrix44Bench { |
| 62 | public: |
| 63 | PreScaleMatrix44Bench(void* param) : INHERITED(param, "prescale") { |
| 64 | fX = fY = fZ = SkDoubleToMScalar(1.5); |
| 65 | } |
| 66 | protected: |
| 67 | virtual void performTest() { |
| 68 | fM0.reset(); |
| 69 | for (int i = 0; i < 10; ++i) { |
| 70 | fM0.preScale(fX, fY, fZ); |
| 71 | } |
| 72 | } |
| 73 | private: |
| 74 | SkMatrix44 fM0; |
| 75 | SkMScalar fX, fY, fZ; |
| 76 | typedef Matrix44Bench INHERITED; |
| 77 | }; |
| 78 | |
tomhudson@google.com | 9973a8a | 2012-12-13 09:55:42 +0000 | [diff] [blame^] | 79 | class InvertMatrix44Bench : public Matrix44Bench { |
| 80 | public: |
| 81 | InvertMatrix44Bench(void* param) : INHERITED(param, "invert") { |
| 82 | fM0.set(0, 0, -1.1); |
| 83 | fM0.set(0, 1, 2.1); |
| 84 | fM0.set(0, 2, -3.1); |
| 85 | fM0.set(0, 3, 4.1); |
| 86 | fM0.set(1, 0, 5.1); |
| 87 | fM0.set(1, 1, -6.1); |
| 88 | fM0.set(1, 2, 7.1); |
| 89 | fM0.set(1, 3, 8.1); |
| 90 | fM0.set(2, 0, -9.1); |
| 91 | fM0.set(2, 1, 10.1); |
| 92 | fM0.set(2, 2, 11.1); |
| 93 | fM0.set(2, 3, -12.1); |
| 94 | fM0.set(3, 0, -13.1); |
| 95 | fM0.set(3, 1, 14.1); |
| 96 | fM0.set(3, 2, -15.1); |
| 97 | fM0.set(3, 3, 16.1); |
| 98 | } |
| 99 | protected: |
| 100 | virtual void performTest() { |
| 101 | for (int i = 0; i < 10; ++i) { |
| 102 | fM0.invert(&fM1); |
| 103 | } |
| 104 | } |
| 105 | private: |
| 106 | SkMatrix44 fM0, fM1; |
| 107 | typedef Matrix44Bench INHERITED; |
| 108 | }; |
| 109 | |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 110 | class PostScaleMatrix44Bench : public Matrix44Bench { |
| 111 | public: |
| 112 | PostScaleMatrix44Bench(void* param) : INHERITED(param, "postscale") { |
| 113 | fX = fY = fZ = SkDoubleToMScalar(1.5); |
| 114 | } |
| 115 | protected: |
| 116 | virtual void performTest() { |
| 117 | fM0.reset(); |
| 118 | for (int i = 0; i < 10; ++i) { |
| 119 | fM0.postScale(fX, fY, fZ); |
| 120 | } |
| 121 | } |
| 122 | private: |
| 123 | SkMatrix44 fM0; |
| 124 | SkMScalar fX, fY, fZ; |
| 125 | typedef Matrix44Bench INHERITED; |
| 126 | }; |
| 127 | |
| 128 | class SetConcatMatrix44Bench : public Matrix44Bench { |
| 129 | public: |
| 130 | SetConcatMatrix44Bench(void* param) : INHERITED(param, "setconcat") { |
| 131 | fX = fY = fZ = SkDoubleToMScalar(1.5); |
| 132 | fM1.setScale(fX, fY, fZ); |
| 133 | fM2.setTranslate(fX, fY, fZ); |
| 134 | } |
| 135 | protected: |
| 136 | virtual void performTest() { |
| 137 | fM0.reset(); // just to normalize this test with prescale/postscale |
| 138 | for (int i = 0; i < 10; ++i) { |
| 139 | fM0.setConcat(fM1, fM2); |
| 140 | } |
| 141 | } |
| 142 | private: |
| 143 | SkMatrix44 fM0, fM1, fM2; |
| 144 | SkMScalar fX, fY, fZ; |
| 145 | typedef Matrix44Bench INHERITED; |
| 146 | }; |
| 147 | |
| 148 | class GetTypeMatrix44Bench : public Matrix44Bench { |
| 149 | public: |
| 150 | GetTypeMatrix44Bench(void* param) : INHERITED(param, "gettype") {} |
| 151 | protected: |
| 152 | // Putting random generation of the matrix inside performTest() |
| 153 | // would help us avoid anomalous runs, but takes up 25% or |
| 154 | // more of the function time. |
| 155 | virtual void performTest() { |
| 156 | for (int i = 0; i < 20; ++i) { |
| 157 | fMatrix.set(1, 2, 1); // to invalidate the type-cache |
| 158 | fMatrix.getType(); |
| 159 | } |
| 160 | } |
| 161 | private: |
| 162 | SkMatrix44 fMatrix; |
| 163 | typedef Matrix44Bench INHERITED; |
| 164 | }; |
| 165 | |
| 166 | DEF_BENCH( return new EqualsMatrix44Bench(p); ) |
| 167 | DEF_BENCH( return new PreScaleMatrix44Bench(p); ) |
| 168 | DEF_BENCH( return new PostScaleMatrix44Bench(p); ) |
tomhudson@google.com | 9973a8a | 2012-12-13 09:55:42 +0000 | [diff] [blame^] | 169 | DEF_BENCH( return new InvertMatrix44Bench(p); ) |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 170 | DEF_BENCH( return new SetConcatMatrix44Bench(p); ) |
| 171 | DEF_BENCH( return new GetTypeMatrix44Bench(p); ) |
| 172 | |