| reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 1 | #include "SkBenchmark.h" |
| 2 | #include "SkMatrix.h" |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 3 | #include "SkRandom.h" |
| reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 4 | #include "SkString.h" |
| 5 | |
| 6 | class MatrixBench : public SkBenchmark { |
| 7 | SkString fName; |
| 8 | enum { N = 100000 }; |
| 9 | public: |
| 10 | MatrixBench(void* param, const char name[]) : INHERITED(param) { |
| 11 | fName.printf("matrix_%s", name); |
| 12 | } |
| 13 | |
| 14 | virtual void performTest() = 0; |
| 15 | |
| 16 | protected: |
| 17 | virtual const char* onGetName() { |
| 18 | return fName.c_str(); |
| 19 | } |
| 20 | |
| 21 | virtual void onDraw(SkCanvas* canvas) { |
| 22 | for (int i = 0; i < N; i++) { |
| 23 | this->performTest(); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | private: |
| 28 | typedef SkBenchmark INHERITED; |
| 29 | }; |
| 30 | |
| 31 | // we want to stop the compiler from eliminating code that it thinks is a no-op |
| 32 | // so we have a non-static global we increment, hoping that will convince the |
| 33 | // compiler to execute everything |
| 34 | int gMatrixBench_NonStaticGlobal; |
| 35 | |
| 36 | #define always_do(pred) \ |
| 37 | do { \ |
| 38 | if (pred) { \ |
| 39 | ++gMatrixBench_NonStaticGlobal; \ |
| 40 | } \ |
| 41 | } while (0) |
| 42 | |
| 43 | class EqualsMatrixBench : public MatrixBench { |
| 44 | public: |
| 45 | EqualsMatrixBench(void* param) : INHERITED(param, "equals") {} |
| 46 | protected: |
| 47 | virtual void performTest() { |
| 48 | SkMatrix m0, m1, m2; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 49 | |
| reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 50 | m0.reset(); |
| 51 | m1.reset(); |
| 52 | m2.reset(); |
| 53 | always_do(m0 == m1); |
| 54 | always_do(m1 == m2); |
| 55 | always_do(m2 == m0); |
| 56 | always_do(m0.getType()); |
| 57 | always_do(m1.getType()); |
| 58 | always_do(m2.getType()); |
| 59 | } |
| 60 | private: |
| 61 | typedef MatrixBench INHERITED; |
| 62 | }; |
| 63 | |
| 64 | class ScaleMatrixBench : public MatrixBench { |
| 65 | public: |
| 66 | ScaleMatrixBench(void* param) : INHERITED(param, "scale") { |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 67 | |
| reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 68 | fM0.reset(); |
| 69 | fM1.setScale(fSX, fSY); |
| 70 | fM2.setTranslate(fSX, fSY); |
| 71 | fSX = fSY = SkFloatToScalar(1.5f); |
| 72 | } |
| 73 | protected: |
| 74 | virtual void performTest() { |
| 75 | SkMatrix m; |
| 76 | m = fM0; m.preScale(fSX, fSY); |
| 77 | m = fM1; m.preScale(fSX, fSY); |
| 78 | m = fM2; m.preScale(fSX, fSY); |
| 79 | } |
| 80 | private: |
| 81 | SkMatrix fM0, fM1, fM2; |
| 82 | SkScalar fSX, fSY; |
| 83 | typedef MatrixBench INHERITED; |
| 84 | }; |
| 85 | |
| reed@google.com | e0dcde7 | 2011-06-06 13:20:29 +0000 | [diff] [blame^] | 86 | // having unknown values in our arrays can throw off the timing a lot, perhaps |
| 87 | // handling NaN values is a lot slower. Anyway, this guy is just meant to put |
| 88 | // reasonable values in our arrays. |
| 89 | template <typename T> void init9(T array[9]) { |
| 90 | SkRandom rand; |
| 91 | for (int i = 0; i < 9; i++) { |
| 92 | array[i] = rand.nextSScalar1(); |
| 93 | } |
| 94 | } |
| 95 | |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 96 | // Test the performance of setConcat() non-perspective case: |
| 97 | // using floating point precision only. |
| 98 | class FloatConcatMatrixBench : public MatrixBench { |
| 99 | public: |
| reed@google.com | e0dcde7 | 2011-06-06 13:20:29 +0000 | [diff] [blame^] | 100 | FloatConcatMatrixBench(void* p) : INHERITED(p, "concat_floatfloat") { |
| 101 | init9(mya); |
| 102 | init9(myb); |
| 103 | init9(myr); |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 104 | } |
| 105 | protected: |
| 106 | static inline void muladdmul(float a, float b, float c, float d, |
| 107 | float* result) { |
| 108 | *result = a * b + c * d; |
| 109 | } |
| 110 | virtual void performTest() { |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 111 | const float* a = mya; |
| 112 | const float* b = myb; |
| 113 | float* r = myr; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 114 | muladdmul(a[0], b[0], a[1], b[3], &r[0]); |
| 115 | muladdmul(a[0], b[1], a[1], b[4], &r[1]); |
| 116 | muladdmul(a[0], b[2], a[1], b[5], &r[2]); |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 117 | r[2] += a[2]; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 118 | muladdmul(a[3], b[0], a[4], b[3], &r[3]); |
| 119 | muladdmul(a[3], b[1], a[4], b[4], &r[4]); |
| 120 | muladdmul(a[3], b[2], a[4], b[5], &r[5]); |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 121 | r[5] += a[5]; |
| 122 | r[6] = r[7] = 0.0f; |
| 123 | r[8] = 1.0f; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 124 | } |
| 125 | private: |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 126 | float mya [9]; |
| 127 | float myb [9]; |
| 128 | float myr [9]; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 129 | typedef MatrixBench INHERITED; |
| 130 | }; |
| 131 | |
| 132 | static inline float SkDoubleToFloat(double x) { |
| 133 | return static_cast<float>(x); |
| 134 | } |
| 135 | |
| 136 | // Test the performance of setConcat() non-perspective case: |
| 137 | // using floating point precision but casting up to float for |
| 138 | // intermediate results during computations. |
| 139 | class FloatDoubleConcatMatrixBench : public MatrixBench { |
| 140 | public: |
| reed@google.com | e0dcde7 | 2011-06-06 13:20:29 +0000 | [diff] [blame^] | 141 | FloatDoubleConcatMatrixBench(void* p) : INHERITED(p, "concat_floatdouble") { |
| 142 | init9(mya); |
| 143 | init9(myb); |
| 144 | init9(myr); |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 145 | } |
| 146 | protected: |
| 147 | static inline void muladdmul(float a, float b, float c, float d, |
| 148 | float* result) { |
| 149 | *result = SkDoubleToFloat((double)a * b + (double)c * d); |
| 150 | } |
| 151 | virtual void performTest() { |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 152 | const float* a = mya; |
| 153 | const float* b = myb; |
| 154 | float* r = myr; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 155 | muladdmul(a[0], b[0], a[1], b[3], &r[0]); |
| 156 | muladdmul(a[0], b[1], a[1], b[4], &r[1]); |
| 157 | muladdmul(a[0], b[2], a[1], b[5], &r[2]); |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 158 | r[2] += a[2]; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 159 | muladdmul(a[3], b[0], a[4], b[3], &r[3]); |
| 160 | muladdmul(a[3], b[1], a[4], b[4], &r[4]); |
| 161 | muladdmul(a[3], b[2], a[4], b[5], &r[5]); |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 162 | r[5] += a[5]; |
| 163 | r[6] = r[7] = 0.0f; |
| 164 | r[8] = 1.0f; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 165 | } |
| 166 | private: |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 167 | float mya [9]; |
| 168 | float myb [9]; |
| 169 | float myr [9]; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 170 | typedef MatrixBench INHERITED; |
| 171 | }; |
| 172 | |
| 173 | // Test the performance of setConcat() non-perspective case: |
| 174 | // using double precision only. |
| 175 | class DoubleConcatMatrixBench : public MatrixBench { |
| 176 | public: |
| reed@google.com | e0dcde7 | 2011-06-06 13:20:29 +0000 | [diff] [blame^] | 177 | DoubleConcatMatrixBench(void* p) : INHERITED(p, "concat_double") { |
| 178 | init9(mya); |
| 179 | init9(myb); |
| 180 | init9(myr); |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 181 | } |
| 182 | protected: |
| 183 | static inline void muladdmul(double a, double b, double c, double d, |
| 184 | double* result) { |
| 185 | *result = a * b + c * d; |
| 186 | } |
| 187 | virtual void performTest() { |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 188 | const double* a = mya; |
| 189 | const double* b = myb; |
| 190 | double* r = myr; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 191 | muladdmul(a[0], b[0], a[1], b[3], &r[0]); |
| 192 | muladdmul(a[0], b[1], a[1], b[4], &r[1]); |
| 193 | muladdmul(a[0], b[2], a[1], b[5], &r[2]); |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 194 | r[2] += a[2]; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 195 | muladdmul(a[3], b[0], a[4], b[3], &r[3]); |
| 196 | muladdmul(a[3], b[1], a[4], b[4], &r[4]); |
| 197 | muladdmul(a[3], b[2], a[4], b[5], &r[5]); |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 198 | r[5] += a[5]; |
| 199 | r[6] = r[7] = 0.0; |
| 200 | r[8] = 1.0; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 201 | } |
| 202 | private: |
| tomhudson@google.com | a20416b | 2011-06-03 20:32:58 +0000 | [diff] [blame] | 203 | double mya [9]; |
| 204 | double myb [9]; |
| 205 | double myr [9]; |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 206 | typedef MatrixBench INHERITED; |
| 207 | }; |
| 208 | |
| 209 | |
| reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 210 | static SkBenchmark* M0(void* p) { return new EqualsMatrixBench(p); } |
| 211 | static SkBenchmark* M1(void* p) { return new ScaleMatrixBench(p); } |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 212 | static SkBenchmark* M2(void* p) { return new FloatConcatMatrixBench(p); } |
| 213 | static SkBenchmark* M3(void* p) { return new FloatDoubleConcatMatrixBench(p); } |
| 214 | static SkBenchmark* M4(void* p) { return new DoubleConcatMatrixBench(p); } |
| reed@google.com | 3fb5187 | 2011-06-01 15:11:22 +0000 | [diff] [blame] | 215 | |
| 216 | static BenchRegistry gReg0(M0); |
| 217 | static BenchRegistry gReg1(M1); |
| tomhudson@google.com | 7b4e107 | 2011-06-03 19:16:56 +0000 | [diff] [blame] | 218 | static BenchRegistry gReg2(M2); |
| 219 | static BenchRegistry gReg3(M3); |
| 220 | static BenchRegistry gReg4(M4); |