blob: 35c3e68404e98af9716014628afa0c32d11c4920 [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")
reed@google.com44699382013-10-31 17:28:30 +000047 {
reed@google.com7d683352012-12-03 21:19:52 +000048 fM1.set(0, 0, 0);
49 fM2.set(3, 3, 0);
50 }
51protected:
mtkleinf0599002015-07-13 06:18:39 -070052 void performTest() override {
reed@google.com7d683352012-12-03 21:19:52 +000053 for (int i = 0; i < 10; ++i) {
humper@google.com05af1af2013-01-07 16:47:43 +000054 (void) (fM0 == fM1);
55 (void) (fM1 == fM2);
56 (void) (fM2 == fM0);
reed@google.com7d683352012-12-03 21:19:52 +000057 }
58 }
59private:
60 SkMatrix44 fM0, fM1, fM2;
61 typedef Matrix44Bench INHERITED;
62};
63
shawnsingh@chromium.org63bf68d2013-08-28 05:07:26 +000064class SetIdentityMatrix44Bench : public Matrix44Bench {
65public:
reed@google.com44699382013-10-31 17:28:30 +000066 SetIdentityMatrix44Bench()
67 : INHERITED("setidentity")
reed@google.com44699382013-10-31 17:28:30 +000068 {
shawnsingh@chromium.org63bf68d2013-08-28 05:07:26 +000069 double rowMajor[16] =
70 { 1, 2, 3, 4,
71 5, 6, 7, 8,
72 9, 10, 11, 12,
73 13, 14, 15, 16};
74 mat.setRowMajord(rowMajor);
75 }
76protected:
mtkleinf0599002015-07-13 06:18:39 -070077 void performTest() override {
shawnsingh@chromium.org63bf68d2013-08-28 05:07:26 +000078 for (int i = 0; i < 10; ++i) {
79 mat.setIdentity();
80 }
81 }
82private:
83 SkMatrix44 mat;
84 typedef Matrix44Bench INHERITED;
85};
86
reed@google.com7d683352012-12-03 21:19:52 +000087class PreScaleMatrix44Bench : public Matrix44Bench {
88public:
reed@google.com44699382013-10-31 17:28:30 +000089 PreScaleMatrix44Bench()
90 : INHERITED("prescale")
reed@google.com44699382013-10-31 17:28:30 +000091 {
reed@google.com7d683352012-12-03 21:19:52 +000092 fX = fY = fZ = SkDoubleToMScalar(1.5);
93 }
94protected:
mtkleinf0599002015-07-13 06:18:39 -070095 void performTest() override {
reed@google.com7d683352012-12-03 21:19:52 +000096 fM0.reset();
97 for (int i = 0; i < 10; ++i) {
98 fM0.preScale(fX, fY, fZ);
99 }
100 }
101private:
102 SkMatrix44 fM0;
103 SkMScalar fX, fY, fZ;
104 typedef Matrix44Bench INHERITED;
105};
106
tomhudson@google.com9973a8a2012-12-13 09:55:42 +0000107class InvertMatrix44Bench : public Matrix44Bench {
108public:
reed@google.com44699382013-10-31 17:28:30 +0000109 InvertMatrix44Bench()
110 : INHERITED("invert")
reed@google.com44699382013-10-31 17:28:30 +0000111 {
mtklein1831f992015-06-09 11:47:01 -0700112 fM0.setDouble(0, 0, -1.1);
113 fM0.setDouble(0, 1, 2.1);
114 fM0.setDouble(0, 2, -3.1);
115 fM0.setDouble(0, 3, 4.1);
116 fM0.setDouble(1, 0, 5.1);
117 fM0.setDouble(1, 1, -6.1);
118 fM0.setDouble(1, 2, 7.1);
119 fM0.setDouble(1, 3, 8.1);
120 fM0.setDouble(2, 0, -9.1);
121 fM0.setDouble(2, 1, 10.1);
122 fM0.setDouble(2, 2, 11.1);
123 fM0.setDouble(2, 3, -12.1);
124 fM0.setDouble(3, 0, -13.1);
125 fM0.setDouble(3, 1, 14.1);
126 fM0.setDouble(3, 2, -15.1);
127 fM0.setDouble(3, 3, 16.1);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000128 }
129protected:
mtkleinf0599002015-07-13 06:18:39 -0700130 void performTest() override {
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000131 for (int i = 0; i < 10; ++i) {
132 fM0.invert(&fM1);
133 }
134 }
135private:
136 SkMatrix44 fM0, fM1;
137 typedef Matrix44Bench INHERITED;
138};
139
140class InvertAffineMatrix44Bench : public Matrix44Bench {
141public:
reed@google.com44699382013-10-31 17:28:30 +0000142 InvertAffineMatrix44Bench()
143 : INHERITED("invertaffine")
reed@google.com44699382013-10-31 17:28:30 +0000144 {
mtklein1831f992015-06-09 11:47:01 -0700145 fM0.setDouble(0, 0, -1.1);
146 fM0.setDouble(0, 1, 2.1);
147 fM0.setDouble(0, 2, -3.1);
148 fM0.setDouble(0, 3, 4.1);
149 fM0.setDouble(1, 0, 5.1);
150 fM0.setDouble(1, 1, -6.1);
151 fM0.setDouble(1, 2, 7.1);
152 fM0.setDouble(1, 3, 8.1);
153 fM0.setDouble(2, 0, -9.1);
154 fM0.setDouble(2, 1, 10.1);
155 fM0.setDouble(2, 2, 11.1);
156 fM0.setDouble(2, 3, -12.1);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000157 // bottom row (perspective component) remains (0, 0, 0, 1).
158 }
159protected:
mtkleinf0599002015-07-13 06:18:39 -0700160 void performTest() override {
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000161 for (int i = 0; i < 10; ++i) {
162 fM0.invert(&fM1);
163 }
164 }
165private:
166 SkMatrix44 fM0, fM1;
167 typedef Matrix44Bench INHERITED;
168};
169
170class InvertScaleTranslateMatrix44Bench : public Matrix44Bench {
171public:
reed@google.com44699382013-10-31 17:28:30 +0000172 InvertScaleTranslateMatrix44Bench()
173 : INHERITED("invertscaletranslate")
reed@google.com44699382013-10-31 17:28:30 +0000174 {
mtklein1831f992015-06-09 11:47:01 -0700175 fM0.setDouble(0, 0, -1.1);
176 fM0.setDouble(0, 3, 4.1);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000177
mtklein1831f992015-06-09 11:47:01 -0700178 fM0.setDouble(1, 1, -6.1);
179 fM0.setDouble(1, 3, 8.1);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000180
mtklein1831f992015-06-09 11:47:01 -0700181 fM0.setDouble(2, 2, 11.1);
182 fM0.setDouble(2, 3, -12.1);
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000183 }
184protected:
mtkleinf0599002015-07-13 06:18:39 -0700185 void performTest() override {
commit-bot@chromium.org95045752013-08-20 20:15:24 +0000186 for (int i = 0; i < 10; ++i) {
187 fM0.invert(&fM1);
188 }
189 }
190private:
191 SkMatrix44 fM0, fM1;
192 typedef Matrix44Bench INHERITED;
193};
194
195class InvertTranslateMatrix44Bench : public Matrix44Bench {
196public:
reed@google.com44699382013-10-31 17:28:30 +0000197 InvertTranslateMatrix44Bench()
198 : INHERITED("inverttranslate")
reed@google.com44699382013-10-31 17:28:30 +0000199 {
mtklein1831f992015-06-09 11:47:01 -0700200 fM0.setDouble(0, 3, 4.1);
201 fM0.setDouble(1, 3, 8.1);
202 fM0.setDouble(2, 3, -12.1);
tomhudson@google.com9973a8a2012-12-13 09:55:42 +0000203 }
204protected:
mtkleinf0599002015-07-13 06:18:39 -0700205 void performTest() override {
tomhudson@google.com9973a8a2012-12-13 09:55:42 +0000206 for (int i = 0; i < 10; ++i) {
207 fM0.invert(&fM1);
208 }
209 }
210private:
211 SkMatrix44 fM0, fM1;
212 typedef Matrix44Bench INHERITED;
213};
214
reed@google.com7d683352012-12-03 21:19:52 +0000215class PostScaleMatrix44Bench : public Matrix44Bench {
216public:
reed@google.com44699382013-10-31 17:28:30 +0000217 PostScaleMatrix44Bench()
218 : INHERITED("postscale")
reed@google.com44699382013-10-31 17:28:30 +0000219 {
reed@google.com7d683352012-12-03 21:19:52 +0000220 fX = fY = fZ = SkDoubleToMScalar(1.5);
221 }
222protected:
mtkleinf0599002015-07-13 06:18:39 -0700223 void performTest() override {
reed@google.com7d683352012-12-03 21:19:52 +0000224 fM0.reset();
225 for (int i = 0; i < 10; ++i) {
226 fM0.postScale(fX, fY, fZ);
227 }
228 }
229private:
230 SkMatrix44 fM0;
231 SkMScalar fX, fY, fZ;
232 typedef Matrix44Bench INHERITED;
233};
234
235class SetConcatMatrix44Bench : public Matrix44Bench {
236public:
mtkleinc6c6a912015-06-09 04:44:07 -0700237 // SkMatrix44::setConcat() has a fast path for matrices that are at most scale+translate.
238 SetConcatMatrix44Bench(bool fastPath)
239 : INHERITED(fastPath ? "setconcat_fast" : "setconcat_general")
reed@google.com44699382013-10-31 17:28:30 +0000240{
mtkleinc6c6a912015-06-09 04:44:07 -0700241 if (fastPath) {
242 const SkMScalar v = SkDoubleToMScalar(1.5);
243 fM1.setScale(v,v,v);
244 fM2.setTranslate(v,v,v);
245 } else {
246 SkRandom rand;
247 for (int x = 0; x < 4; x++) {
248 for (int y = 0; y < 4; y++) {
249 fM1.setFloat(x,y, rand.nextF());
250 fM2.setFloat(x,y, rand.nextF());
251 }}
252 }
reed@google.com7d683352012-12-03 21:19:52 +0000253 }
254protected:
mtkleinf0599002015-07-13 06:18:39 -0700255 void performTest() override {
reed@google.com7d683352012-12-03 21:19:52 +0000256 fM0.reset(); // just to normalize this test with prescale/postscale
mtklein25791882015-06-09 09:29:12 -0700257 for (int i = 0; i < 10000; ++i) {
reed@google.com7d683352012-12-03 21:19:52 +0000258 fM0.setConcat(fM1, fM2);
259 }
260 }
261private:
262 SkMatrix44 fM0, fM1, fM2;
reed@google.com7d683352012-12-03 21:19:52 +0000263 typedef Matrix44Bench INHERITED;
264};
265
266class GetTypeMatrix44Bench : public Matrix44Bench {
267public:
reed@google.com44699382013-10-31 17:28:30 +0000268 GetTypeMatrix44Bench()
269 : INHERITED("gettype")
reed@google.com44699382013-10-31 17:28:30 +0000270 {}
reed@google.com7d683352012-12-03 21:19:52 +0000271protected:
272 // Putting random generation of the matrix inside performTest()
273 // would help us avoid anomalous runs, but takes up 25% or
274 // more of the function time.
mtkleinf0599002015-07-13 06:18:39 -0700275 void performTest() override {
reed@google.com7d683352012-12-03 21:19:52 +0000276 for (int i = 0; i < 20; ++i) {
277 fMatrix.set(1, 2, 1); // to invalidate the type-cache
278 fMatrix.getType();
279 }
280 }
281private:
282 SkMatrix44 fMatrix;
283 typedef Matrix44Bench INHERITED;
284};
285
mtklein@google.com410e6e82013-09-13 19:52:27 +0000286DEF_BENCH( return new SetIdentityMatrix44Bench(); )
287DEF_BENCH( return new EqualsMatrix44Bench(); )
288DEF_BENCH( return new PreScaleMatrix44Bench(); )
289DEF_BENCH( return new PostScaleMatrix44Bench(); )
290DEF_BENCH( return new InvertMatrix44Bench(); )
291DEF_BENCH( return new InvertAffineMatrix44Bench(); )
292DEF_BENCH( return new InvertScaleTranslateMatrix44Bench(); )
293DEF_BENCH( return new InvertTranslateMatrix44Bench(); )
mtkleinc6c6a912015-06-09 04:44:07 -0700294DEF_BENCH( return new SetConcatMatrix44Bench(true); )
295DEF_BENCH( return new SetConcatMatrix44Bench(false); )
mtklein@google.com410e6e82013-09-13 19:52:27 +0000296DEF_BENCH( return new GetTypeMatrix44Bench(); )