• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "Benchmark.h"
9 #include "SkMatrix44.h"
10 #include "SkRandom.h"
11 #include "SkString.h"
12 
13 class Matrix44Bench : public Benchmark {
14     SkString    fName;
15 public:
Matrix44Bench(const char name[])16     Matrix44Bench(const char name[]) {
17         fName.printf("matrix44_%s", name);
18     }
19 
isSuitableFor(Backend backend)20     bool isSuitableFor(Backend backend) override {
21         return backend == kNonRendering_Backend;
22     }
23 
24     virtual void performTest() = 0;
25 
26 protected:
mulLoopCount() const27     virtual int mulLoopCount() const { return 1; }
28 
onGetName()29     const char* onGetName() override {
30         return fName.c_str();
31     }
32 
onDraw(int loops,SkCanvas *)33     void onDraw(int loops, SkCanvas*) override {
34         for (int i = 0; i < loops; i++) {
35             this->performTest();
36         }
37     }
38 
39 private:
40     typedef Benchmark INHERITED;
41 };
42 
43 class EqualsMatrix44Bench : public Matrix44Bench {
44 public:
EqualsMatrix44Bench()45     EqualsMatrix44Bench()
46         : INHERITED("equals")
47     {
48         fM1.set(0, 0, 0);
49         fM2.set(3, 3, 0);
50     }
51 protected:
performTest()52     void performTest() override {
53         for (int i = 0; i < 10; ++i) {
54             (void) (fM0 == fM1);
55             (void) (fM1 == fM2);
56             (void) (fM2 == fM0);
57         }
58     }
59 private:
60     SkMatrix44 fM0, fM1, fM2;
61     typedef Matrix44Bench INHERITED;
62 };
63 
64 class SetIdentityMatrix44Bench : public Matrix44Bench {
65 public:
SetIdentityMatrix44Bench()66     SetIdentityMatrix44Bench()
67         : INHERITED("setidentity")
68     {
69         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     }
76 protected:
performTest()77     void performTest() override {
78         for (int i = 0; i < 10; ++i) {
79             mat.setIdentity();
80         }
81     }
82 private:
83     SkMatrix44 mat;
84     typedef Matrix44Bench INHERITED;
85 };
86 
87 class PreScaleMatrix44Bench : public Matrix44Bench {
88 public:
PreScaleMatrix44Bench()89     PreScaleMatrix44Bench()
90         : INHERITED("prescale")
91     {
92         fX = fY = fZ = SkDoubleToMScalar(1.5);
93     }
94 protected:
performTest()95     void performTest() override {
96         fM0.reset();
97         for (int i = 0; i < 10; ++i) {
98             fM0.preScale(fX, fY, fZ);
99         }
100     }
101 private:
102     SkMatrix44 fM0;
103     SkMScalar  fX, fY, fZ;
104     typedef Matrix44Bench INHERITED;
105 };
106 
107 class InvertMatrix44Bench : public Matrix44Bench {
108 public:
InvertMatrix44Bench()109     InvertMatrix44Bench()
110         : INHERITED("invert")
111     {
112         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);
128     }
129 protected:
performTest()130     void performTest() override {
131         for (int i = 0; i < 10; ++i) {
132             fM0.invert(&fM1);
133         }
134     }
135 private:
136     SkMatrix44 fM0, fM1;
137     typedef Matrix44Bench INHERITED;
138 };
139 
140 class InvertAffineMatrix44Bench : public Matrix44Bench {
141 public:
InvertAffineMatrix44Bench()142     InvertAffineMatrix44Bench()
143         : INHERITED("invertaffine")
144     {
145         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);
157         // bottom row (perspective component) remains (0, 0, 0, 1).
158     }
159 protected:
performTest()160     void performTest() override {
161         for (int i = 0; i < 10; ++i) {
162             fM0.invert(&fM1);
163         }
164     }
165 private:
166     SkMatrix44 fM0, fM1;
167     typedef Matrix44Bench INHERITED;
168 };
169 
170 class InvertScaleTranslateMatrix44Bench : public Matrix44Bench {
171 public:
InvertScaleTranslateMatrix44Bench()172     InvertScaleTranslateMatrix44Bench()
173         : INHERITED("invertscaletranslate")
174     {
175         fM0.setDouble(0, 0, -1.1);
176         fM0.setDouble(0, 3, 4.1);
177 
178         fM0.setDouble(1, 1, -6.1);
179         fM0.setDouble(1, 3, 8.1);
180 
181         fM0.setDouble(2, 2, 11.1);
182         fM0.setDouble(2, 3, -12.1);
183     }
184 protected:
performTest()185     void performTest() override {
186         for (int i = 0; i < 10; ++i) {
187             fM0.invert(&fM1);
188         }
189     }
190 private:
191     SkMatrix44 fM0, fM1;
192     typedef Matrix44Bench INHERITED;
193 };
194 
195 class InvertTranslateMatrix44Bench : public Matrix44Bench {
196 public:
InvertTranslateMatrix44Bench()197     InvertTranslateMatrix44Bench()
198         : INHERITED("inverttranslate")
199     {
200         fM0.setDouble(0, 3, 4.1);
201         fM0.setDouble(1, 3, 8.1);
202         fM0.setDouble(2, 3, -12.1);
203     }
204 protected:
performTest()205     void performTest() override {
206         for (int i = 0; i < 10; ++i) {
207             fM0.invert(&fM1);
208         }
209     }
210 private:
211     SkMatrix44 fM0, fM1;
212     typedef Matrix44Bench INHERITED;
213 };
214 
215 class PostScaleMatrix44Bench : public Matrix44Bench {
216 public:
PostScaleMatrix44Bench()217     PostScaleMatrix44Bench()
218         : INHERITED("postscale")
219     {
220         fX = fY = fZ = SkDoubleToMScalar(1.5);
221     }
222 protected:
performTest()223     void performTest() override {
224         fM0.reset();
225         for (int i = 0; i < 10; ++i) {
226             fM0.postScale(fX, fY, fZ);
227         }
228     }
229 private:
230     SkMatrix44 fM0;
231     SkMScalar  fX, fY, fZ;
232     typedef Matrix44Bench INHERITED;
233 };
234 
235 class SetConcatMatrix44Bench : public Matrix44Bench {
236 public:
237     // SkMatrix44::setConcat() has a fast path for matrices that are at most scale+translate.
SetConcatMatrix44Bench(bool fastPath)238     SetConcatMatrix44Bench(bool fastPath)
239         : INHERITED(fastPath ? "setconcat_fast" : "setconcat_general")
240 {
241         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         }
253     }
254 protected:
performTest()255     void performTest() override {
256         fM0.reset();    // just to normalize this test with prescale/postscale
257         for (int i = 0; i < 10000; ++i) {
258             fM0.setConcat(fM1, fM2);
259         }
260     }
261 private:
262     SkMatrix44 fM0, fM1, fM2;
263     typedef Matrix44Bench INHERITED;
264 };
265 
266 class GetTypeMatrix44Bench : public Matrix44Bench {
267 public:
GetTypeMatrix44Bench()268     GetTypeMatrix44Bench()
269         : INHERITED("gettype")
270     {}
271 protected:
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.
performTest()275     void performTest() override {
276         for (int i = 0; i < 20; ++i) {
277             fMatrix.set(1, 2, 1);   // to invalidate the type-cache
278             fMatrix.getType();
279         }
280     }
281 private:
282     SkMatrix44 fMatrix;
283     typedef Matrix44Bench INHERITED;
284 };
285 
286 DEF_BENCH( return new SetIdentityMatrix44Bench(); )
287 DEF_BENCH( return new EqualsMatrix44Bench(); )
288 DEF_BENCH( return new PreScaleMatrix44Bench(); )
289 DEF_BENCH( return new PostScaleMatrix44Bench(); )
290 DEF_BENCH( return new InvertMatrix44Bench(); )
291 DEF_BENCH( return new InvertAffineMatrix44Bench(); )
292 DEF_BENCH( return new InvertScaleTranslateMatrix44Bench(); )
293 DEF_BENCH( return new InvertTranslateMatrix44Bench(); )
294 DEF_BENCH( return new SetConcatMatrix44Bench(true); )
295 DEF_BENCH( return new SetConcatMatrix44Bench(false); )
296 DEF_BENCH( return new GetTypeMatrix44Bench(); )
297