blob: 8a7c50e6068ea22b96a19ed6de2410a320be15bb [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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
reed@google.com7d683352012-12-03 21:19:52 +00009#include "SkMatrix44.h"
10#include "SkRandom.h"
11#include "SkString.h"
12
tfarinaf168b862014-06-19 12:32:29 -070013class Matrix44Bench : public Benchmark {
reed@google.com7d683352012-12-03 21:19:52 +000014 SkString fName;
reed@google.com7d683352012-12-03 21:19:52 +000015public:
reed@google.com44699382013-10-31 17:28:30 +000016 Matrix44Bench(const char name[]) {
reed@google.com7d683352012-12-03 21:19:52 +000017 fName.printf("matrix44_%s", name);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000018 }
19
20 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
21 return backend == kNonRendering_Backend;
reed@google.com7d683352012-12-03 21:19:52 +000022 }
23
24 virtual void performTest() = 0;
25
26protected:
27 virtual int mulLoopCount() const { return 1; }
28
29 virtual const char* onGetName() {
30 return fName.c_str();
31 }
32
commit-bot@chromium.org33614712013-12-03 18:17:16 +000033 virtual void onDraw(const int loops, SkCanvas*) {
34 for (int i = 0; i < loops; i++) {
reed@google.com7d683352012-12-03 21:19:52 +000035 this->performTest();
36 }
37 }
38
39private:
tfarinaf168b862014-06-19 12:32:29 -070040 typedef Benchmark INHERITED;
reed@google.com7d683352012-12-03 21:19:52 +000041};
42
43class EqualsMatrix44Bench : public Matrix44Bench {
44public:
reed@google.com44699382013-10-31 17:28:30 +000045 EqualsMatrix44Bench()
46 : INHERITED("equals")
47 , fM0(SkMatrix44::kIdentity_Constructor)
48 , fM1(SkMatrix44::kIdentity_Constructor)
49 , fM2(SkMatrix44::kIdentity_Constructor)
50 {
reed@google.com7d683352012-12-03 21:19:52 +000051 fM1.set(0, 0, 0);
52 fM2.set(3, 3, 0);
53 }
54protected:
55 virtual void performTest() {
56 for (int i = 0; i < 10; ++i) {
humper@google.com05af1af2013-01-07 16:47:43 +000057 (void) (fM0 == fM1);
58 (void) (fM1 == fM2);
59 (void) (fM2 == fM0);
reed@google.com7d683352012-12-03 21:19:52 +000060 }
61 }
62private:
63 SkMatrix44 fM0, fM1, fM2;
64 typedef Matrix44Bench INHERITED;
65};
66
shawnsingh@chromium.org63bf68d2013-08-28 05:07:26 +000067class SetIdentityMatrix44Bench : public Matrix44Bench {
68public:
reed@google.com44699382013-10-31 17:28:30 +000069 SetIdentityMatrix44Bench()
70 : INHERITED("setidentity")
71 , mat(SkMatrix44::kIdentity_Constructor)
72 {
shawnsingh@chromium.org63bf68d2013-08-28 05:07:26 +000073 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 }
80protected:
81 virtual void performTest() {
82 for (int i = 0; i < 10; ++i) {
83 mat.setIdentity();
84 }
85 }
86private:
87 SkMatrix44 mat;
88 typedef Matrix44Bench INHERITED;
89};
90
reed@google.com7d683352012-12-03 21:19:52 +000091class PreScaleMatrix44Bench : public Matrix44Bench {
92public:
reed@google.com44699382013-10-31 17:28:30 +000093 PreScaleMatrix44Bench()
94 : INHERITED("prescale")
95 , fM0(SkMatrix44::kUninitialized_Constructor)
96 {
reed@google.com7d683352012-12-03 21:19:52 +000097 fX = fY = fZ = SkDoubleToMScalar(1.5);
98 }
99protected:
100 virtual void performTest() {
101 fM0.reset();
102 for (int i = 0; i < 10; ++i) {
103 fM0.preScale(fX, fY, fZ);
104 }
105 }
106private:
107 SkMatrix44 fM0;
108 SkMScalar fX, fY, fZ;
109 typedef Matrix44Bench INHERITED;
110};
111
tomhudson@google.com9973a8a2012-12-13 09:55:42 +0000112class InvertMatrix44Bench : public Matrix44Bench {
113public:
reed@google.com44699382013-10-31 17:28:30 +0000114 InvertMatrix44Bench()
115 : INHERITED("invert")
116 , fM0(SkMatrix44::kUninitialized_Constructor)
117 , fM1(SkMatrix44::kUninitialized_Constructor)
118 {
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000119 fM0.set(0, 0, -1.1);
120 fM0.set(0, 1, 2.1);
121 fM0.set(0, 2, -3.1);
122 fM0.set(0, 3, 4.1);
123 fM0.set(1, 0, 5.1);
124 fM0.set(1, 1, -6.1);
125 fM0.set(1, 2, 7.1);
126 fM0.set(1, 3, 8.1);
127 fM0.set(2, 0, -9.1);
128 fM0.set(2, 1, 10.1);
129 fM0.set(2, 2, 11.1);
130 fM0.set(2, 3, -12.1);
131 fM0.set(3, 0, -13.1);
132 fM0.set(3, 1, 14.1);
133 fM0.set(3, 2, -15.1);
134 fM0.set(3, 3, 16.1);
135 }
136protected:
137 virtual void performTest() {
138 for (int i = 0; i < 10; ++i) {
139 fM0.invert(&fM1);
140 }
141 }
142private:
143 SkMatrix44 fM0, fM1;
144 typedef Matrix44Bench INHERITED;
145};
146
147class InvertAffineMatrix44Bench : public Matrix44Bench {
148public:
reed@google.com44699382013-10-31 17:28:30 +0000149 InvertAffineMatrix44Bench()
150 : INHERITED("invertaffine")
151 , fM0(SkMatrix44::kIdentity_Constructor)
152 , fM1(SkMatrix44::kUninitialized_Constructor)
153 {
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000154 fM0.set(0, 0, -1.1);
155 fM0.set(0, 1, 2.1);
156 fM0.set(0, 2, -3.1);
157 fM0.set(0, 3, 4.1);
158 fM0.set(1, 0, 5.1);
159 fM0.set(1, 1, -6.1);
160 fM0.set(1, 2, 7.1);
161 fM0.set(1, 3, 8.1);
162 fM0.set(2, 0, -9.1);
163 fM0.set(2, 1, 10.1);
164 fM0.set(2, 2, 11.1);
165 fM0.set(2, 3, -12.1);
166 // bottom row (perspective component) remains (0, 0, 0, 1).
167 }
168protected:
169 virtual void performTest() {
170 for (int i = 0; i < 10; ++i) {
171 fM0.invert(&fM1);
172 }
173 }
174private:
175 SkMatrix44 fM0, fM1;
176 typedef Matrix44Bench INHERITED;
177};
178
179class InvertScaleTranslateMatrix44Bench : public Matrix44Bench {
180public:
reed@google.com44699382013-10-31 17:28:30 +0000181 InvertScaleTranslateMatrix44Bench()
182 : INHERITED("invertscaletranslate")
183 , fM0(SkMatrix44::kIdentity_Constructor)
184 , fM1(SkMatrix44::kUninitialized_Constructor)
185 {
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000186 fM0.set(0, 0, -1.1);
187 fM0.set(0, 3, 4.1);
188
189 fM0.set(1, 1, -6.1);
190 fM0.set(1, 3, 8.1);
191
192 fM0.set(2, 2, 11.1);
193 fM0.set(2, 3, -12.1);
194 }
195protected:
196 virtual void performTest() {
197 for (int i = 0; i < 10; ++i) {
198 fM0.invert(&fM1);
199 }
200 }
201private:
202 SkMatrix44 fM0, fM1;
203 typedef Matrix44Bench INHERITED;
204};
205
206class InvertTranslateMatrix44Bench : public Matrix44Bench {
207public:
reed@google.com44699382013-10-31 17:28:30 +0000208 InvertTranslateMatrix44Bench()
209 : INHERITED("inverttranslate")
210 , fM0(SkMatrix44::kIdentity_Constructor)
211 , fM1(SkMatrix44::kUninitialized_Constructor)
212 {
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000213 fM0.set(0, 3, 4.1);
214 fM0.set(1, 3, 8.1);
215 fM0.set(2, 3, -12.1);
tomhudson@google.com9973a8a2012-12-13 09:55:42 +0000216 }
217protected:
218 virtual void performTest() {
219 for (int i = 0; i < 10; ++i) {
220 fM0.invert(&fM1);
221 }
222 }
223private:
224 SkMatrix44 fM0, fM1;
225 typedef Matrix44Bench INHERITED;
226};
227
reed@google.com7d683352012-12-03 21:19:52 +0000228class PostScaleMatrix44Bench : public Matrix44Bench {
229public:
reed@google.com44699382013-10-31 17:28:30 +0000230 PostScaleMatrix44Bench()
231 : INHERITED("postscale")
232 , fM0(SkMatrix44::kUninitialized_Constructor)
233 {
reed@google.com7d683352012-12-03 21:19:52 +0000234 fX = fY = fZ = SkDoubleToMScalar(1.5);
235 }
236protected:
237 virtual void performTest() {
238 fM0.reset();
239 for (int i = 0; i < 10; ++i) {
240 fM0.postScale(fX, fY, fZ);
241 }
242 }
243private:
244 SkMatrix44 fM0;
245 SkMScalar fX, fY, fZ;
246 typedef Matrix44Bench INHERITED;
247};
248
249class SetConcatMatrix44Bench : public Matrix44Bench {
250public:
reed@google.com44699382013-10-31 17:28:30 +0000251 SetConcatMatrix44Bench()
252 : INHERITED("setconcat")
253 , fM0(SkMatrix44::kUninitialized_Constructor)
254 , fM1(SkMatrix44::kUninitialized_Constructor)
255 , fM2(SkMatrix44::kUninitialized_Constructor)
256{
reed@google.com7d683352012-12-03 21:19:52 +0000257 fX = fY = fZ = SkDoubleToMScalar(1.5);
258 fM1.setScale(fX, fY, fZ);
259 fM2.setTranslate(fX, fY, fZ);
260 }
261protected:
262 virtual void performTest() {
263 fM0.reset(); // just to normalize this test with prescale/postscale
264 for (int i = 0; i < 10; ++i) {
265 fM0.setConcat(fM1, fM2);
266 }
267 }
268private:
269 SkMatrix44 fM0, fM1, fM2;
270 SkMScalar fX, fY, fZ;
271 typedef Matrix44Bench INHERITED;
272};
273
274class GetTypeMatrix44Bench : public Matrix44Bench {
275public:
reed@google.com44699382013-10-31 17:28:30 +0000276 GetTypeMatrix44Bench()
277 : INHERITED("gettype")
278 , fMatrix(SkMatrix44::kIdentity_Constructor)
279 {}
reed@google.com7d683352012-12-03 21:19:52 +0000280protected:
281 // Putting random generation of the matrix inside performTest()
282 // would help us avoid anomalous runs, but takes up 25% or
283 // more of the function time.
284 virtual void performTest() {
285 for (int i = 0; i < 20; ++i) {
286 fMatrix.set(1, 2, 1); // to invalidate the type-cache
287 fMatrix.getType();
288 }
289 }
290private:
291 SkMatrix44 fMatrix;
292 typedef Matrix44Bench INHERITED;
293};
294
mtklein@google.com410e6e82013-09-13 19:52:27 +0000295DEF_BENCH( return new SetIdentityMatrix44Bench(); )
296DEF_BENCH( return new EqualsMatrix44Bench(); )
297DEF_BENCH( return new PreScaleMatrix44Bench(); )
298DEF_BENCH( return new PostScaleMatrix44Bench(); )
299DEF_BENCH( return new InvertMatrix44Bench(); )
300DEF_BENCH( return new InvertAffineMatrix44Bench(); )
301DEF_BENCH( return new InvertScaleTranslateMatrix44Bench(); )
302DEF_BENCH( return new InvertTranslateMatrix44Bench(); )
303DEF_BENCH( return new SetConcatMatrix44Bench(); )
304DEF_BENCH( return new GetTypeMatrix44Bench(); )