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 "SkBenchmark.h"
9#include "SkMatrix44.h"
10#include "SkRandom.h"
11#include "SkString.h"
12
13class Matrix44Bench : public SkBenchmark {
14    SkString    fName;
15    enum { N = 10000 };
16public:
17    Matrix44Bench(void* param, const char name[]) : INHERITED(param) {
18        fName.printf("matrix44_%s", name);
19        fIsRendering = false;
20    }
21
22    virtual void performTest() = 0;
23
24protected:
25    virtual int mulLoopCount() const { return 1; }
26
27    virtual const char* onGetName() {
28        return fName.c_str();
29    }
30
31    virtual void onDraw(SkCanvas*) {
32        int n = SkBENCHLOOP(N * this->mulLoopCount());
33        for (int i = 0; i < n; i++) {
34            this->performTest();
35        }
36    }
37
38private:
39    typedef SkBenchmark INHERITED;
40};
41
42class EqualsMatrix44Bench : public Matrix44Bench {
43public:
44    EqualsMatrix44Bench(void* param) : INHERITED(param, "equals") {
45        fM1.set(0, 0, 0);
46        fM2.set(3, 3, 0);
47    }
48protected:
49    virtual void performTest() {
50        for (int i = 0; i < 10; ++i) {
51            (void) (fM0 == fM1);
52            (void) (fM1 == fM2);
53            (void) (fM2 == fM0);
54        }
55    }
56private:
57    SkMatrix44 fM0, fM1, fM2;
58    typedef Matrix44Bench INHERITED;
59};
60
61class PreScaleMatrix44Bench : public Matrix44Bench {
62public:
63    PreScaleMatrix44Bench(void* param) : INHERITED(param, "prescale") {
64        fX = fY = fZ = SkDoubleToMScalar(1.5);
65    }
66protected:
67    virtual void performTest() {
68        fM0.reset();
69        for (int i = 0; i < 10; ++i) {
70            fM0.preScale(fX, fY, fZ);
71        }
72    }
73private:
74    SkMatrix44 fM0;
75    SkMScalar  fX, fY, fZ;
76    typedef Matrix44Bench INHERITED;
77};
78
79class InvertMatrix44Bench : public Matrix44Bench {
80public:
81    InvertMatrix44Bench(void* param) : INHERITED(param, "invert") {
82    fM0.set(0, 0, -1.1);
83    fM0.set(0, 1, 2.1);
84    fM0.set(0, 2, -3.1);
85    fM0.set(0, 3, 4.1);
86    fM0.set(1, 0, 5.1);
87    fM0.set(1, 1, -6.1);
88    fM0.set(1, 2, 7.1);
89    fM0.set(1, 3, 8.1);
90    fM0.set(2, 0, -9.1);
91    fM0.set(2, 1, 10.1);
92    fM0.set(2, 2, 11.1);
93    fM0.set(2, 3, -12.1);
94    fM0.set(3, 0, -13.1);
95    fM0.set(3, 1, 14.1);
96    fM0.set(3, 2, -15.1);
97    fM0.set(3, 3, 16.1);
98    }
99protected:
100    virtual void performTest() {
101        for (int i = 0; i < 10; ++i) {
102            fM0.invert(&fM1);
103        }
104    }
105private:
106    SkMatrix44 fM0, fM1;
107    typedef Matrix44Bench INHERITED;
108};
109
110class PostScaleMatrix44Bench : public Matrix44Bench {
111public:
112    PostScaleMatrix44Bench(void* param) : INHERITED(param, "postscale") {
113        fX = fY = fZ = SkDoubleToMScalar(1.5);
114    }
115protected:
116    virtual void performTest() {
117        fM0.reset();
118        for (int i = 0; i < 10; ++i) {
119            fM0.postScale(fX, fY, fZ);
120        }
121    }
122private:
123    SkMatrix44 fM0;
124    SkMScalar  fX, fY, fZ;
125    typedef Matrix44Bench INHERITED;
126};
127
128class SetConcatMatrix44Bench : public Matrix44Bench {
129public:
130    SetConcatMatrix44Bench(void* param) : INHERITED(param, "setconcat") {
131        fX = fY = fZ = SkDoubleToMScalar(1.5);
132        fM1.setScale(fX, fY, fZ);
133        fM2.setTranslate(fX, fY, fZ);
134    }
135protected:
136    virtual void performTest() {
137        fM0.reset();    // just to normalize this test with prescale/postscale
138        for (int i = 0; i < 10; ++i) {
139            fM0.setConcat(fM1, fM2);
140        }
141    }
142private:
143    SkMatrix44 fM0, fM1, fM2;
144    SkMScalar  fX, fY, fZ;
145    typedef Matrix44Bench INHERITED;
146};
147
148class GetTypeMatrix44Bench : public Matrix44Bench {
149public:
150    GetTypeMatrix44Bench(void* param) : INHERITED(param, "gettype") {}
151protected:
152    // Putting random generation of the matrix inside performTest()
153    // would help us avoid anomalous runs, but takes up 25% or
154    // more of the function time.
155    virtual void performTest() {
156        for (int i = 0; i < 20; ++i) {
157            fMatrix.set(1, 2, 1);   // to invalidate the type-cache
158            fMatrix.getType();
159        }
160    }
161private:
162    SkMatrix44 fMatrix;
163    typedef Matrix44Bench INHERITED;
164};
165
166DEF_BENCH( return new EqualsMatrix44Bench(p); )
167DEF_BENCH( return new PreScaleMatrix44Bench(p); )
168DEF_BENCH( return new PostScaleMatrix44Bench(p); )
169DEF_BENCH( return new InvertMatrix44Bench(p); )
170DEF_BENCH( return new SetConcatMatrix44Bench(p); )
171DEF_BENCH( return new GetTypeMatrix44Bench(p); )
172