blob: b376d5a96149b794c81cfe43438f0ba04e023201 [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
mtklein36352bf2015-03-25 18:17:31 -070020 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000021 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
mtkleinf0599002015-07-13 06:18:39 -070029 const char* onGetName() override {
reed@google.com7d683352012-12-03 21:19:52 +000030 return fName.c_str();
31 }
32
mtkleina1ebeb22015-10-01 09:43:39 -070033 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000034 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:
mtkleinf0599002015-07-13 06:18:39 -070055 void performTest() override {
reed@google.com7d683352012-12-03 21:19:52 +000056 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:
mtkleinf0599002015-07-13 06:18:39 -070081 void performTest() override {
shawnsingh@chromium.org63bf68d2013-08-28 05:07:26 +000082 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:
mtkleinf0599002015-07-13 06:18:39 -0700100 void performTest() override {
reed@google.com7d683352012-12-03 21:19:52 +0000101 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 {
mtklein1831f992015-06-09 11:47:01 -0700119 fM0.setDouble(0, 0, -1.1);
120 fM0.setDouble(0, 1, 2.1);
121 fM0.setDouble(0, 2, -3.1);
122 fM0.setDouble(0, 3, 4.1);
123 fM0.setDouble(1, 0, 5.1);
124 fM0.setDouble(1, 1, -6.1);
125 fM0.setDouble(1, 2, 7.1);
126 fM0.setDouble(1, 3, 8.1);
127 fM0.setDouble(2, 0, -9.1);
128 fM0.setDouble(2, 1, 10.1);
129 fM0.setDouble(2, 2, 11.1);
130 fM0.setDouble(2, 3, -12.1);
131 fM0.setDouble(3, 0, -13.1);
132 fM0.setDouble(3, 1, 14.1);
133 fM0.setDouble(3, 2, -15.1);
134 fM0.setDouble(3, 3, 16.1);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000135 }
136protected:
mtkleinf0599002015-07-13 06:18:39 -0700137 void performTest() override {
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000138 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 {
mtklein1831f992015-06-09 11:47:01 -0700154 fM0.setDouble(0, 0, -1.1);
155 fM0.setDouble(0, 1, 2.1);
156 fM0.setDouble(0, 2, -3.1);
157 fM0.setDouble(0, 3, 4.1);
158 fM0.setDouble(1, 0, 5.1);
159 fM0.setDouble(1, 1, -6.1);
160 fM0.setDouble(1, 2, 7.1);
161 fM0.setDouble(1, 3, 8.1);
162 fM0.setDouble(2, 0, -9.1);
163 fM0.setDouble(2, 1, 10.1);
164 fM0.setDouble(2, 2, 11.1);
165 fM0.setDouble(2, 3, -12.1);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000166 // bottom row (perspective component) remains (0, 0, 0, 1).
167 }
168protected:
mtkleinf0599002015-07-13 06:18:39 -0700169 void performTest() override {
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000170 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 {
mtklein1831f992015-06-09 11:47:01 -0700186 fM0.setDouble(0, 0, -1.1);
187 fM0.setDouble(0, 3, 4.1);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000188
mtklein1831f992015-06-09 11:47:01 -0700189 fM0.setDouble(1, 1, -6.1);
190 fM0.setDouble(1, 3, 8.1);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000191
mtklein1831f992015-06-09 11:47:01 -0700192 fM0.setDouble(2, 2, 11.1);
193 fM0.setDouble(2, 3, -12.1);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000194 }
195protected:
mtkleinf0599002015-07-13 06:18:39 -0700196 void performTest() override {
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000197 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 {
mtklein1831f992015-06-09 11:47:01 -0700213 fM0.setDouble(0, 3, 4.1);
214 fM0.setDouble(1, 3, 8.1);
215 fM0.setDouble(2, 3, -12.1);
tomhudson@google.com9973a8a2012-12-13 09:55:42 +0000216 }
217protected:
mtkleinf0599002015-07-13 06:18:39 -0700218 void performTest() override {
tomhudson@google.com9973a8a2012-12-13 09:55:42 +0000219 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:
mtkleinf0599002015-07-13 06:18:39 -0700237 void performTest() override {
reed@google.com7d683352012-12-03 21:19:52 +0000238 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:
mtkleinc6c6a912015-06-09 04:44:07 -0700251 // SkMatrix44::setConcat() has a fast path for matrices that are at most scale+translate.
252 SetConcatMatrix44Bench(bool fastPath)
253 : INHERITED(fastPath ? "setconcat_fast" : "setconcat_general")
reed@google.com44699382013-10-31 17:28:30 +0000254 , fM0(SkMatrix44::kUninitialized_Constructor)
255 , fM1(SkMatrix44::kUninitialized_Constructor)
256 , fM2(SkMatrix44::kUninitialized_Constructor)
257{
mtkleinc6c6a912015-06-09 04:44:07 -0700258 if (fastPath) {
259 const SkMScalar v = SkDoubleToMScalar(1.5);
260 fM1.setScale(v,v,v);
261 fM2.setTranslate(v,v,v);
262 } else {
263 SkRandom rand;
264 for (int x = 0; x < 4; x++) {
265 for (int y = 0; y < 4; y++) {
266 fM1.setFloat(x,y, rand.nextF());
267 fM2.setFloat(x,y, rand.nextF());
268 }}
269 }
reed@google.com7d683352012-12-03 21:19:52 +0000270 }
271protected:
mtkleinf0599002015-07-13 06:18:39 -0700272 void performTest() override {
reed@google.com7d683352012-12-03 21:19:52 +0000273 fM0.reset(); // just to normalize this test with prescale/postscale
mtklein25791882015-06-09 09:29:12 -0700274 for (int i = 0; i < 10000; ++i) {
reed@google.com7d683352012-12-03 21:19:52 +0000275 fM0.setConcat(fM1, fM2);
276 }
277 }
278private:
279 SkMatrix44 fM0, fM1, fM2;
reed@google.com7d683352012-12-03 21:19:52 +0000280 typedef Matrix44Bench INHERITED;
281};
282
283class GetTypeMatrix44Bench : public Matrix44Bench {
284public:
reed@google.com44699382013-10-31 17:28:30 +0000285 GetTypeMatrix44Bench()
286 : INHERITED("gettype")
287 , fMatrix(SkMatrix44::kIdentity_Constructor)
288 {}
reed@google.com7d683352012-12-03 21:19:52 +0000289protected:
290 // Putting random generation of the matrix inside performTest()
291 // would help us avoid anomalous runs, but takes up 25% or
292 // more of the function time.
mtkleinf0599002015-07-13 06:18:39 -0700293 void performTest() override {
reed@google.com7d683352012-12-03 21:19:52 +0000294 for (int i = 0; i < 20; ++i) {
295 fMatrix.set(1, 2, 1); // to invalidate the type-cache
296 fMatrix.getType();
297 }
298 }
299private:
300 SkMatrix44 fMatrix;
301 typedef Matrix44Bench INHERITED;
302};
303
mtklein@google.com410e6e82013-09-13 19:52:27 +0000304DEF_BENCH( return new SetIdentityMatrix44Bench(); )
305DEF_BENCH( return new EqualsMatrix44Bench(); )
306DEF_BENCH( return new PreScaleMatrix44Bench(); )
307DEF_BENCH( return new PostScaleMatrix44Bench(); )
308DEF_BENCH( return new InvertMatrix44Bench(); )
309DEF_BENCH( return new InvertAffineMatrix44Bench(); )
310DEF_BENCH( return new InvertScaleTranslateMatrix44Bench(); )
311DEF_BENCH( return new InvertTranslateMatrix44Bench(); )
mtkleinc6c6a912015-06-09 04:44:07 -0700312DEF_BENCH( return new SetConcatMatrix44Bench(true); )
313DEF_BENCH( return new SetConcatMatrix44Bench(false); )
mtklein@google.com410e6e82013-09-13 19:52:27 +0000314DEF_BENCH( return new GetTypeMatrix44Bench(); )