Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
PathIterBench.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"
12#include "include/core/SkPath.h"
15#include "src/base/SkRandom.h"
16#include "src/core/SkPathPriv.h"
17
18enum class PathIterType {
19 kIter,
20 kRaw,
21 kEdge,
22};
23const char* gPathIterNames[] = {
24 "iter", "raw", "edge"
25};
26
27static int rand_pts(SkRandom& rand, SkPoint pts[4]) {
28 int n = rand.nextU() & 3;
29 n += 1;
30
31 for (int i = 0; i < n; ++i) {
32 pts[i].fX = rand.nextSScalar1();
33 pts[i].fY = rand.nextSScalar1();
34 }
35 return n;
36}
37
38class PathIterBench : public Benchmark {
39 SkString fName;
40 SkPath fPath;
41 PathIterType fType;
42
43 int fVerbInc = 0;
44 SkScalar fXInc = 0, fYInc = 0;
45
46public:
48 fName.printf("pathiter_%s", gPathIterNames[static_cast<unsigned>(t)]);
49
50 SkRandom rand;
51 for (int i = 0; i < 1000; ++i) {
52 SkPoint pts[4];
53 int n = rand_pts(rand, pts);
54 switch (n) {
55 case 1:
56 fPath.moveTo(pts[0]);
57 break;
58 case 2:
59 fPath.lineTo(pts[1]);
60 break;
61 case 3:
62 fPath.quadTo(pts[1], pts[2]);
63 break;
64 case 4:
65 fPath.cubicTo(pts[1], pts[2], pts[3]);
66 break;
67 }
68 }
69 }
70
71 bool isSuitableFor(Backend backend) override {
73 }
74
75protected:
76 const char* onGetName() override {
77 return fName.c_str();
78 }
79
80 void onDraw(int loops, SkCanvas*) override {
81 // Need to do *something* with the results, so the compile doesn't elide
82 // away the code we want to time.
83 auto handle = [this](int verb, const SkPoint pts[]) {
84 fVerbInc += verb;
85 fXInc += pts[0].fX;
86 fYInc += pts[0].fY;
87 };
88
89 switch (fType) {
91 for (int i = 0; i < loops; ++i) {
92 SkPath::Iter iter(fPath, true);
93 SkPath::Verb verb;
94 SkPoint pts[4];
95 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
96 handle(verb, pts);
97 }
98 }
99 break;
101 for (int i = 0; i < loops; ++i) {
102 for (auto [verb, pts, w] : SkPathPriv::Iterate(fPath)) {
103 handle((SkPath::Verb)verb, pts);
104 }
105 }
106 break;
108 for (int i = 0; i < loops; ++i) {
109 SkPathEdgeIter iter(fPath);
110 while (auto r = iter.next()) {
111 handle((int)r.fEdge, r.fPts);
112 }
113 }
114 break;
115 }
116 }
117
118private:
119 using INHERITED = Benchmark;
120};
121
122///////////////////////////////////////////////////////////////////////////////
123
SkPath fPath
#define DEF_BENCH(code)
Definition Benchmark.h:20
const char * backend
const char * fName
PathIterType
static int rand_pts(SkRandom &rand, SkPoint pts[4])
const char * gPathIterNames[]
bool isSuitableFor(Backend backend) override
void onDraw(int loops, SkCanvas *) override
const char * onGetName() override
PathIterBench(PathIterType t)
Result next()
Definition SkPathPriv.h:485
Verb next(SkPoint pts[4])
Definition SkPath.cpp:1837
SkPath & moveTo(SkScalar x, SkScalar y)
Definition SkPath.cpp:678
SkPath & lineTo(SkScalar x, SkScalar y)
Definition SkPath.cpp:718
SkPath & quadTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2)
Definition SkPath.cpp:736
@ kDone_Verb
Definition SkPath.h:1464
SkPath & cubicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, SkScalar x3, SkScalar y3)
Definition SkPath.cpp:789
uint32_t nextU()
Definition SkRandom.h:42
SkScalar nextSScalar1()
Definition SkRandom.h:113
float SkScalar
Definition extension.cpp:12
SkScalar w
float fX
x-axis value
float fY
y-axis value