Flutter Engine
The Flutter Engine
MatrixBench.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2011 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#include "bench/Benchmark.h"
10#include "src/base/SkRandom.h"
12
13class MatrixBench : public Benchmark {
14 SkString fName;
15public:
16 MatrixBench(const char name[]) {
17 fName.printf("matrix_%s", name);
18 }
19
20 bool isSuitableFor(Backend backend) override {
22 }
23
24 virtual void performTest() = 0;
25
26protected:
27 virtual int mulLoopCount() const { return 1; }
28
29 const char* onGetName() override {
30 return fName.c_str();
31 }
32
33 void onDraw(int loops, SkCanvas*) override {
34 for (int i = 0; i < loops; i++) {
35 this->performTest();
36 }
37 }
38
39private:
40 using INHERITED = Benchmark;
41};
42
44public:
46 fSX = fSY = 1.5f;
47 fM0.reset();
48 fM1.setScale(fSX, fSY);
49 fM2.setTranslate(fSX, fSY);
50 }
51protected:
52 void performTest() override {
53 SkMatrix m;
54 m = fM0; m.preScale(fSX, fSY);
55 m = fM1; m.preScale(fSX, fSY);
56 m = fM2; m.preScale(fSX, fSY);
57 }
58private:
59 SkMatrix fM0, fM1, fM2;
60 SkScalar fSX, fSY;
61 using INHERITED = MatrixBench;
62};
63
64// having unknown values in our arrays can throw off the timing a lot, perhaps
65// handling NaN values is a lot slower. Anyway, this is just meant to put
66// reasonable values in our arrays.
67template <typename T> void init9(T array[9]) {
68 SkRandom rand;
69 for (int i = 0; i < 9; i++) {
70 array[i] = rand.nextSScalar1();
71 }
72}
73
75public:
76 DecomposeMatrixBench() : INHERITED("decompose") {}
77
78protected:
79 void onDelayedSetup() override {
80 for (int i = 0; i < 10; ++i) {
81 SkScalar rot0 = (fRandom.nextBool()) ? fRandom.nextRangeF(-180, 180) : 0.0f;
82 SkScalar sx = fRandom.nextRangeF(-3000.f, 3000.f);
83 SkScalar sy = (fRandom.nextBool()) ? fRandom.nextRangeF(-3000.f, 3000.f) : sx;
84 SkScalar rot1 = fRandom.nextRangeF(-180, 180);
85 fMatrix[i].setRotate(rot0);
86 fMatrix[i].postScale(sx, sy);
87 fMatrix[i].postRotate(rot1);
88 }
89 }
90 void performTest() override {
91 SkPoint rotation1, scale, rotation2;
92 for (int i = 0; i < 10; ++i) {
93 (void) SkDecomposeUpper2x2(fMatrix[i], &rotation1, &scale, &rotation2);
94 }
95 }
96private:
97 SkMatrix fMatrix[10];
98 SkRandom fRandom;
99 using INHERITED = MatrixBench;
100};
101
103public:
105 : INHERITED(name)
106 , fFlags(flags) {
107 fMatrix.reset();
108 fIteration = 0;
109 if (flags & kScale_Flag) {
110 fMatrix.postScale(1.5f, 2.5f);
111 }
112 if (flags & kTranslate_Flag) {
113 fMatrix.postTranslate(1.5f, 2.5f);
114 }
115 if (flags & kRotate_Flag) {
116 fMatrix.postRotate(45.0f);
117 }
118 if (flags & kPerspective_Flag) {
119 fMatrix.setPerspX(1.5f);
120 fMatrix.setPerspY(2.5f);
121 }
122 if (0 == (flags & kUncachedTypeMask_Flag)) {
123 fMatrix.getType();
124 }
125 }
126 enum Flag {
132 };
133protected:
134 void performTest() override {
135 if (fFlags & kUncachedTypeMask_Flag) {
136 // This will invalidate the typemask without
137 // changing the matrix.
138 fMatrix.setPerspX(fMatrix.getPerspX());
139 }
141 bool invertible = fMatrix.invert(&inv);
142 SkASSERT(invertible);
143 SkRect transformedRect;
144 // an arbitrary, small, non-zero rect to transform
146 if (invertible) {
147 inv.mapRect(&transformedRect, srcRect);
148 }
149 }
150private:
151 SkMatrix fMatrix;
152 int fFlags;
153 unsigned fIteration;
154 using INHERITED = MatrixBench;
155};
156
157///////////////////////////////////////////////////////////////////////////////
158
159DEF_BENCH( return new ScaleMatrixBench(); )
160DEF_BENCH( return new DecomposeMatrixBench(); )
161
162DEF_BENCH( return new InvertMapRectMatrixBench("invert_maprect_identity", 0); )
163
165 "invert_maprect_rectstaysrect",
168
170 "invert_maprect_translate",
172
174 "invert_maprect_nonpersp",
178
180 "invert_maprect_persp",
182
184 "invert_maprect_typemask_rectstaysrect",
188
190 "invert_maprect_typemask_nonpersp",
195
196///////////////////////////////////////////////////////////////////////////////
197
198static SkMatrix make_trans() { return SkMatrix::Translate(2, 3); }
199static SkMatrix make_scale() { SkMatrix m(make_trans()); m.postScale(1.5f, 0.5f); return m; }
200static SkMatrix make_afine() { SkMatrix m(make_trans()); m.postRotate(15); return m; }
201
203protected:
205 enum {
206 N = 32
207 };
209public:
210 MapPointsMatrixBench(const char name[], const SkMatrix& m)
211 : MatrixBench(name), fM(m)
212 {
213 SkRandom rand;
214 for (int i = 0; i < N; ++i) {
215 fSrc[i].set(rand.nextSScalar1(), rand.nextSScalar1());
216 }
217 }
218
219 void performTest() override {
220 for (int i = 0; i < 1000000; ++i) {
222 }
223 }
224};
225DEF_BENCH( return new MapPointsMatrixBench("mappoints_identity", SkMatrix::I()); )
226DEF_BENCH( return new MapPointsMatrixBench("mappoints_trans", make_trans()); )
227DEF_BENCH( return new MapPointsMatrixBench("mappoints_scale", make_scale()); )
228DEF_BENCH( return new MapPointsMatrixBench("mappoints_affine", make_afine()); )
229
230///////////////////////////////////////////////////////////////////////////////
231
233 SkMatrix fM;
234 SkRect fR;
235 bool fScaleTrans;
236
237 enum { MEGA_LOOP = 1000 * 1000 };
238public:
239 MapRectMatrixBench(const char name[], bool scale_trans)
240 : MatrixBench(name), fScaleTrans(scale_trans)
241 {
242 fM.setScale(2, 3);
243 fM.postTranslate(1, 2);
244
245 fR.setLTRB(10, 10, 100, 200);
246 }
247
248 void performTest() override {
249 SkRect dst;
250 if (fScaleTrans) {
251 for (int i = 0; i < MEGA_LOOP; ++i) {
252 fM.mapRectScaleTranslate(&dst, fR);
253 }
254 } else {
255 for (int i = 0; i < MEGA_LOOP; ++i) {
256 fM.mapRect(&dst, fR);
257 }
258 }
259 }
260};
261DEF_BENCH( return new MapRectMatrixBench("maprect", false); )
262DEF_BENCH( return new MapRectMatrixBench("maprectscaletrans", true); )
static SkM44 inv(const SkM44 &m)
Definition: 3d.cpp:26
const char * backend
const char * fName
DEF_BENCH(return new InvertMapRectMatrixBench("invert_maprect_rectstaysrect", InvertMapRectMatrixBench::kScale_Flag|InvertMapRectMatrixBench::kTranslate_Flag);) DEF_BENCH(return new InvertMapRectMatrixBench("invert_maprect_translate"
static SkMatrix make_afine()
static SkMatrix make_scale()
void init9(T array[9])
Definition: MatrixBench.cpp:67
static SkMatrix make_trans()
#define SkASSERT(cond)
Definition: SkAssert.h:116
bool SkDecomposeUpper2x2(const SkMatrix &matrix, SkPoint *rotation1, SkPoint *scale, SkPoint *rotation2)
Definition: SkMatrix.cpp:1689
#define SkIntToScalar(x)
Definition: SkScalar.h:57
void performTest() override
Definition: MatrixBench.cpp:90
void onDelayedSetup() override
Definition: MatrixBench.cpp:79
void performTest() override
InvertMapRectMatrixBench(const char *name, int flags)
MapPointsMatrixBench(const char name[], const SkMatrix &m)
void performTest() override
void performTest() override
MapRectMatrixBench(const char name[], bool scale_trans)
bool isSuitableFor(Backend backend) override
Definition: MatrixBench.cpp:20
const char * onGetName() override
Definition: MatrixBench.cpp:29
virtual void performTest()=0
MatrixBench(const char name[])
Definition: MatrixBench.cpp:16
void onDraw(int loops, SkCanvas *) override
Definition: MatrixBench.cpp:33
virtual int mulLoopCount() const
Definition: MatrixBench.cpp:27
void performTest() override
Definition: MatrixBench.cpp:52
SkMatrix & postTranslate(SkScalar dx, SkScalar dy)
Definition: SkMatrix.cpp:281
SkMatrix & postRotate(SkScalar degrees, SkScalar px, SkScalar py)
Definition: SkMatrix.cpp:474
SkMatrix & postScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py)
Definition: SkMatrix.cpp:360
static SkMatrix Translate(SkScalar dx, SkScalar dy)
Definition: SkMatrix.h:91
SkMatrix & setPerspX(SkScalar v)
Definition: SkMatrix.h:537
void mapPoints(SkPoint dst[], const SkPoint src[], int count) const
Definition: SkMatrix.cpp:770
SkMatrix & setTranslate(SkScalar dx, SkScalar dy)
Definition: SkMatrix.cpp:254
SkMatrix & setScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py)
Definition: SkMatrix.cpp:296
bool invert(SkMatrix *inverse) const
Definition: SkMatrix.h:1206
SkMatrix & setRotate(SkScalar degrees, SkScalar px, SkScalar py)
Definition: SkMatrix.cpp:452
SkMatrix & setPerspY(SkScalar v)
Definition: SkMatrix.h:544
static const SkMatrix & I()
Definition: SkMatrix.cpp:1544
void mapRectScaleTranslate(SkRect *dst, const SkRect &src) const
Definition: SkMatrix.cpp:1128
SkScalar getPerspX() const
Definition: SkMatrix.h:458
SkMatrix & reset()
Definition: SkMatrix.cpp:49
bool mapRect(SkRect *dst, const SkRect &src, SkApplyPerspectiveClip pc=SkApplyPerspectiveClip::kYes) const
Definition: SkMatrix.cpp:1141
TypeMask getType() const
Definition: SkMatrix.h:207
bool nextBool()
Definition: SkRandom.h:117
float nextRangeF(float min, float max)
Definition: SkRandom.h:64
SkScalar nextSScalar1()
Definition: SkRandom.h:113
float SkScalar
Definition: extension.cpp:12
FlutterSemanticsFlag flags
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
dst
Definition: cp.py:12
#define T
Definition: precompiler.cc:65
const Scalar scale
void set(float x, float y)
Definition: SkPoint_impl.h:200
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609