blob: a4b36d894508ef0e69baba494e86d130a05fd131 [file] [log] [blame]
reed@google.com7d683352012-12-03 21:19:52 +00001/*
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
13class Matrix44Bench : public SkBenchmark {
14 SkString fName;
reed@google.com7d683352012-12-03 21:19:52 +000015public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000016 Matrix44Bench(const char name[]) {
reed@google.com7d683352012-12-03 21:19:52 +000017 fName.printf("matrix44_%s", name);
18 fIsRendering = false;
19 }
20
21 virtual void performTest() = 0;
22
23protected:
24 virtual int mulLoopCount() const { return 1; }
25
26 virtual const char* onGetName() {
27 return fName.c_str();
28 }
29
sugoi@google.com77472f02013-03-05 18:50:01 +000030 virtual void onDraw(SkCanvas*) {
mtklein@google.comc2897432013-09-10 19:23:38 +000031 for (int i = 0; i < this->getLoops(); i++) {
reed@google.com7d683352012-12-03 21:19:52 +000032 this->performTest();
33 }
34 }
35
36private:
37 typedef SkBenchmark INHERITED;
38};
39
40class EqualsMatrix44Bench : public Matrix44Bench {
41public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000042 EqualsMatrix44Bench() : INHERITED("equals") {
reed@google.com7d683352012-12-03 21:19:52 +000043 fM1.set(0, 0, 0);
44 fM2.set(3, 3, 0);
45 }
46protected:
47 virtual void performTest() {
48 for (int i = 0; i < 10; ++i) {
humper@google.com05af1af2013-01-07 16:47:43 +000049 (void) (fM0 == fM1);
50 (void) (fM1 == fM2);
51 (void) (fM2 == fM0);
reed@google.com7d683352012-12-03 21:19:52 +000052 }
53 }
54private:
55 SkMatrix44 fM0, fM1, fM2;
56 typedef Matrix44Bench INHERITED;
57};
58
shawnsingh@chromium.org63bf68d2013-08-28 05:07:26 +000059class SetIdentityMatrix44Bench : public Matrix44Bench {
60public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000061 SetIdentityMatrix44Bench() : INHERITED("setidentity") {
shawnsingh@chromium.org63bf68d2013-08-28 05:07:26 +000062 double rowMajor[16] =
63 { 1, 2, 3, 4,
64 5, 6, 7, 8,
65 9, 10, 11, 12,
66 13, 14, 15, 16};
67 mat.setRowMajord(rowMajor);
68 }
69protected:
70 virtual void performTest() {
71 for (int i = 0; i < 10; ++i) {
72 mat.setIdentity();
73 }
74 }
75private:
76 SkMatrix44 mat;
77 typedef Matrix44Bench INHERITED;
78};
79
reed@google.com7d683352012-12-03 21:19:52 +000080class PreScaleMatrix44Bench : public Matrix44Bench {
81public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000082 PreScaleMatrix44Bench() : INHERITED("prescale") {
reed@google.com7d683352012-12-03 21:19:52 +000083 fX = fY = fZ = SkDoubleToMScalar(1.5);
84 }
85protected:
86 virtual void performTest() {
87 fM0.reset();
88 for (int i = 0; i < 10; ++i) {
89 fM0.preScale(fX, fY, fZ);
90 }
91 }
92private:
93 SkMatrix44 fM0;
94 SkMScalar fX, fY, fZ;
95 typedef Matrix44Bench INHERITED;
96};
97
tomhudson@google.com9973a8a2012-12-13 09:55:42 +000098class InvertMatrix44Bench : public Matrix44Bench {
99public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000100 InvertMatrix44Bench() : INHERITED("invert") {
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000101 fM0.set(0, 0, -1.1);
102 fM0.set(0, 1, 2.1);
103 fM0.set(0, 2, -3.1);
104 fM0.set(0, 3, 4.1);
105 fM0.set(1, 0, 5.1);
106 fM0.set(1, 1, -6.1);
107 fM0.set(1, 2, 7.1);
108 fM0.set(1, 3, 8.1);
109 fM0.set(2, 0, -9.1);
110 fM0.set(2, 1, 10.1);
111 fM0.set(2, 2, 11.1);
112 fM0.set(2, 3, -12.1);
113 fM0.set(3, 0, -13.1);
114 fM0.set(3, 1, 14.1);
115 fM0.set(3, 2, -15.1);
116 fM0.set(3, 3, 16.1);
117 }
118protected:
119 virtual void performTest() {
120 for (int i = 0; i < 10; ++i) {
121 fM0.invert(&fM1);
122 }
123 }
124private:
125 SkMatrix44 fM0, fM1;
126 typedef Matrix44Bench INHERITED;
127};
128
129class InvertAffineMatrix44Bench : public Matrix44Bench {
130public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000131 InvertAffineMatrix44Bench() : INHERITED("invertaffine") {
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000132 fM0.set(0, 0, -1.1);
133 fM0.set(0, 1, 2.1);
134 fM0.set(0, 2, -3.1);
135 fM0.set(0, 3, 4.1);
136 fM0.set(1, 0, 5.1);
137 fM0.set(1, 1, -6.1);
138 fM0.set(1, 2, 7.1);
139 fM0.set(1, 3, 8.1);
140 fM0.set(2, 0, -9.1);
141 fM0.set(2, 1, 10.1);
142 fM0.set(2, 2, 11.1);
143 fM0.set(2, 3, -12.1);
144 // bottom row (perspective component) remains (0, 0, 0, 1).
145 }
146protected:
147 virtual void performTest() {
148 for (int i = 0; i < 10; ++i) {
149 fM0.invert(&fM1);
150 }
151 }
152private:
153 SkMatrix44 fM0, fM1;
154 typedef Matrix44Bench INHERITED;
155};
156
157class InvertScaleTranslateMatrix44Bench : public Matrix44Bench {
158public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000159 InvertScaleTranslateMatrix44Bench() : INHERITED("invertscaletranslate") {
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000160 fM0.set(0, 0, -1.1);
161 fM0.set(0, 3, 4.1);
162
163 fM0.set(1, 1, -6.1);
164 fM0.set(1, 3, 8.1);
165
166 fM0.set(2, 2, 11.1);
167 fM0.set(2, 3, -12.1);
168 }
169protected:
170 virtual void performTest() {
171 for (int i = 0; i < 10; ++i) {
172 fM0.invert(&fM1);
173 }
174 }
175private:
176 SkMatrix44 fM0, fM1;
177 typedef Matrix44Bench INHERITED;
178};
179
180class InvertTranslateMatrix44Bench : public Matrix44Bench {
181public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000182 InvertTranslateMatrix44Bench() : INHERITED("inverttranslate") {
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000183 fM0.set(0, 3, 4.1);
184 fM0.set(1, 3, 8.1);
185 fM0.set(2, 3, -12.1);
tomhudson@google.com9973a8a2012-12-13 09:55:42 +0000186 }
187protected:
188 virtual void performTest() {
189 for (int i = 0; i < 10; ++i) {
190 fM0.invert(&fM1);
191 }
192 }
193private:
194 SkMatrix44 fM0, fM1;
195 typedef Matrix44Bench INHERITED;
196};
197
reed@google.com7d683352012-12-03 21:19:52 +0000198class PostScaleMatrix44Bench : public Matrix44Bench {
199public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000200 PostScaleMatrix44Bench() : INHERITED("postscale") {
reed@google.com7d683352012-12-03 21:19:52 +0000201 fX = fY = fZ = SkDoubleToMScalar(1.5);
202 }
203protected:
204 virtual void performTest() {
205 fM0.reset();
206 for (int i = 0; i < 10; ++i) {
207 fM0.postScale(fX, fY, fZ);
208 }
209 }
210private:
211 SkMatrix44 fM0;
212 SkMScalar fX, fY, fZ;
213 typedef Matrix44Bench INHERITED;
214};
215
216class SetConcatMatrix44Bench : public Matrix44Bench {
217public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000218 SetConcatMatrix44Bench() : INHERITED("setconcat") {
reed@google.com7d683352012-12-03 21:19:52 +0000219 fX = fY = fZ = SkDoubleToMScalar(1.5);
220 fM1.setScale(fX, fY, fZ);
221 fM2.setTranslate(fX, fY, fZ);
222 }
223protected:
224 virtual void performTest() {
225 fM0.reset(); // just to normalize this test with prescale/postscale
226 for (int i = 0; i < 10; ++i) {
227 fM0.setConcat(fM1, fM2);
228 }
229 }
230private:
231 SkMatrix44 fM0, fM1, fM2;
232 SkMScalar fX, fY, fZ;
233 typedef Matrix44Bench INHERITED;
234};
235
236class GetTypeMatrix44Bench : public Matrix44Bench {
237public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000238 GetTypeMatrix44Bench() : INHERITED("gettype") {}
reed@google.com7d683352012-12-03 21:19:52 +0000239protected:
240 // Putting random generation of the matrix inside performTest()
241 // would help us avoid anomalous runs, but takes up 25% or
242 // more of the function time.
243 virtual void performTest() {
244 for (int i = 0; i < 20; ++i) {
245 fMatrix.set(1, 2, 1); // to invalidate the type-cache
246 fMatrix.getType();
247 }
248 }
249private:
250 SkMatrix44 fMatrix;
251 typedef Matrix44Bench INHERITED;
252};
253
mtklein@google.com410e6e82013-09-13 19:52:27 +0000254DEF_BENCH( return new SetIdentityMatrix44Bench(); )
255DEF_BENCH( return new EqualsMatrix44Bench(); )
256DEF_BENCH( return new PreScaleMatrix44Bench(); )
257DEF_BENCH( return new PostScaleMatrix44Bench(); )
258DEF_BENCH( return new InvertMatrix44Bench(); )
259DEF_BENCH( return new InvertAffineMatrix44Bench(); )
260DEF_BENCH( return new InvertScaleTranslateMatrix44Bench(); )
261DEF_BENCH( return new InvertTranslateMatrix44Bench(); )
262DEF_BENCH( return new SetConcatMatrix44Bench(); )
263DEF_BENCH( return new GetTypeMatrix44Bench(); )