Flutter Engine
The Flutter Engine
Matrix44Bench.cpp
Go to the documentation of this file.
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 "bench/Benchmark.h"
11#include "src/base/SkRandom.h"
13
14class M4Bench : public Benchmark {
15 SkString fName;
16public:
17 M4Bench(const char name[]) {
18 fName.printf("m4_%s", name);
19
20 SkRandom rand;
21 float value[32];
22 for (auto& v : value) {
23 v = rand.nextF();
24 }
27 }
28
29 bool isSuitableFor(Backend backend) override {
31 }
32
33 virtual void performTest() = 0;
34
35protected:
37
38 virtual int mulLoopCount() const { return 1; }
39
40 const char* onGetName() override {
41 return fName.c_str();
42 }
43
44 void onDraw(int loops, SkCanvas*) override {
45 for (int i = 0; i < loops; i++) {
46 this->performTest();
47 }
48 }
49
50private:
51 using INHERITED = Benchmark;
52};
53
54class M4NEQ : public M4Bench {
55public:
56 M4NEQ() : INHERITED("neq") {}
57protected:
58 void performTest() override {
59 for (int i = 0; i < 10000; ++i) {
60 fEQ = (fM2 == fM1); // should always be false
61 }
62 }
63private:
64 bool fEQ;
65 using INHERITED = M4Bench;
66};
67
68class M4EQ : public M4Bench {
69public:
70 M4EQ() : INHERITED("eq") {}
71protected:
72 void performTest() override {
73 fM2 = fM1;
74 for (int i = 0; i < 10000; ++i) {
75 fEQ = (fM2 == fM1); // should always be true
76 }
77 }
78private:
79 bool fEQ;
80 using INHERITED = M4Bench;
81};
82
83class M4Concat : public M4Bench {
84public:
85 M4Concat() : INHERITED("op_concat") {}
86protected:
87 void performTest() override {
88 for (int i = 0; i < 10000; ++i) {
89 fM0 = SkM44(fM1, fM2);
90 }
91 }
92private:
93 using INHERITED = M4Bench;
94};
95
96class M4SetConcat : public M4Bench {
97public:
98 M4SetConcat() : INHERITED("set_concat") {}
99protected:
100 void performTest() override {
101 for (int i = 0; i < 10000; ++i) {
103 }
104 }
105private:
106 using INHERITED = M4Bench;
107};
108
109DEF_BENCH( return new M4EQ(); )
110DEF_BENCH( return new M4NEQ(); )
111DEF_BENCH( return new M4Concat(); )
112DEF_BENCH( return new M4SetConcat(); )
113
114class M4_map4 : public M4Bench {
115public:
116 M4_map4() : INHERITED("map4") {}
117protected:
118 void performTest() override {
119 SkV4 v = {1, 2, 3, 4};
120 for (int i = 0; i < 100000; ++i) {
121 fV = fM0 * v;
122 }
123 }
124private:
125 SkV4 fV;
126 using INHERITED = M4Bench;
127};
128DEF_BENCH( return new M4_map4(); )
129
130class M4_map2 : public M4Bench {
131public:
132 M4_map2() : INHERITED("map2") {}
133protected:
134 void performTest() override {
135 SkMatrix m;
136 m.setRotate(1);
137 for (int i = 0; i < 100000; ++i) {
138 fV = m.mapXY(5, 6);
139 }
140 }
141private:
142 SkPoint fV;
143 using INHERITED = M4Bench;
144};
145DEF_BENCH( return new M4_map2(); )
146
147
148enum class MapMatrixType {
151 kRotate,
154};
155class MapRectBench : public Benchmark {
156 SkString fName;
157
158public:
160 SkRandom rand;
161 const char* typeName;
162 switch(type) {
164 typeName = "t";
165 fM = SkM44::Translate(rand.nextF(), rand.nextF());
166 break;
168 typeName = "s+t";
169 fM = SkM44::Scale(rand.nextF(), rand.nextF());
170 fM.postTranslate(rand.nextF(), rand.nextF());
171 break;
173 typeName = "r";
174 fM = SkM44::Rotate({0.f, 0.f, 1.f}, SkDegreesToRadians(45.f));
175 break;
177 typeName = "p";
178 // Hand chosen to have all corners with w > 0 and w != 1
179 fM = SkM44::Perspective(0.01f, 10.f, SK_ScalarPI / 3.f);
180 fM.preTranslate(0.f, 5.f, -0.1f);
181 fM.preConcat(SkM44::Rotate({0.f, 1.f, 0.f}, 0.008f /* radians */));
182 break;
184 typeName = "pc";
185 // Hand chosen to have some corners with w > 0 and some with w < 0
186 fM = SkM44();
187 fM.setRow(3, {-.2f, -.6f, 0.f, 8.f});
188 break;
189 }
190 fS = SkRect::MakeXYWH(10.f * rand.nextF(), 10.f * rand.nextF(),
191 150.f * rand.nextF(), 150.f * rand.nextF());
192
193 fName.printf("mapRect_%s_%s", name, typeName);
194 }
195
197
198 virtual void performTest() = 0;
199
200protected:
203
204 virtual int mulLoopCount() const { return 1; }
205
206 const char* onGetName() override { return fName.c_str(); }
207
208 void onDraw(int loops, SkCanvas*) override {
209 for (int i = 0; i < loops; i++) {
210 this->performTest();
211 }
212 }
213
214private:
215 using INHERITED = Benchmark;
216};
217
219public:
221
222protected:
223 void performTest() override {
224 for (int i = 0; i < 100000; ++i) {
226 }
227 }
228
229private:
230 using INHERITED = MapRectBench;
231};
237
239public:
241 fM33 = fM.asM33();
242 }
243
244protected:
245 void performTest() override {
246 for (int i = 0; i < 100000; ++i) {
247 fD = fM33.mapRect(fS);
248 }
249 }
250private:
251 SkMatrix fM33;
252 using INHERITED = MapRectBench;
253};
254
#define DEF_BENCH(code)
Definition: Benchmark.h:20
const char * backend
const char * fName
MapMatrixType
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
#define SkDegreesToRadians(degrees)
Definition: SkScalar.h:77
#define SK_ScalarPI
Definition: SkScalar.h:21
GLenum type
void performTest() override
M33_mapRectBench(MapMatrixType type)
void onDraw(int loops, SkCanvas *) override
bool isSuitableFor(Backend backend) override
virtual void performTest()=0
M4Bench(const char name[])
const char * onGetName() override
virtual int mulLoopCount() const
void performTest() override
void performTest() override
void performTest() override
void performTest() override
void performTest() override
void performTest() override
M4_mapRectBench(MapMatrixType type)
void performTest() override
MapRectBench(MapMatrixType type, const char name[])
bool isSuitableFor(Backend backend) override
virtual int mulLoopCount() const
virtual void performTest()=0
const char * onGetName() override
void onDraw(int loops, SkCanvas *) override
Definition: SkM44.h:150
SkM44 & postTranslate(SkScalar x, SkScalar y, SkScalar z=0)
Definition: SkM44.cpp:100
static SkM44 ColMajor(const SkScalar c[16])
Definition: SkM44.h:218
void setRow(int i, const SkV4 &v)
Definition: SkM44.h:281
static SkM44 Rotate(SkV3 axis, SkScalar radians)
Definition: SkM44.h:239
SkM44 & preConcat(const SkM44 &m)
Definition: SkM44.h:351
static SkM44 Translate(SkScalar x, SkScalar y, SkScalar z=0)
Definition: SkM44.h:225
static SkM44 Perspective(float near, float far, float angle)
Definition: SkM44.cpp:343
static SkM44 Scale(SkScalar x, SkScalar y, SkScalar z=1)
Definition: SkM44.h:232
SkM44 & preTranslate(SkScalar x, SkScalar y, SkScalar z=0)
Definition: SkM44.cpp:89
SkM44 & setConcat(const SkM44 &a, const SkM44 &b)
Definition: SkM44.cpp:48
static SkRect MapRect(const SkM44 &m, const SkRect &r)
Definition: SkM44.cpp:216
float nextF()
Definition: SkRandom.h:55
uint8_t value
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
Definition: SkM44.h:98