blob: 7436fd795ad3fa2b533fc12ad8cf669102b4adff [file] [log] [blame]
Mike Reed534e7762018-11-05 07:46:38 -05001/*
2 * Copyright 2018 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/Benchmark.h"
9#include "include/core/SkPath.h"
10#include "include/core/SkShader.h"
11#include "include/core/SkString.h"
12#include "include/pathops/SkPathOps.h"
13#include "include/private/SkTArray.h"
14#include "include/utils/SkRandom.h"
Mike Reed534e7762018-11-05 07:46:38 -050015
16class PathOpsBench : public Benchmark {
17 SkString fName;
18 SkPath fPath1, fPath2;
19 SkPathOp fOp;
20
21public:
22 PathOpsBench(const char suffix[], SkPathOp op) : fOp(op) {
23 fName.printf("pathops_%s", suffix);
24
25 fPath1.addOval({-10, -20, 10, 20});
26 fPath2.addOval({-20, -10, 20, 10});
27 }
28
29 bool isSuitableFor(Backend backend) override {
30 return backend == kNonRendering_Backend;
31 }
32
33protected:
34 const char* onGetName() override {
35 return fName.c_str();
36 }
37
38 void onDraw(int loops, SkCanvas* canvas) override {
39 for (int i = 0; i < loops; i++) {
40 for (int j = 0; j < 1000; ++j) {
41 SkPath result;
42 Op(fPath1, fPath2, fOp, &result);
43 }
44 }
45 }
46
47private:
John Stiles7571f9e2020-09-02 22:42:33 -040048 using INHERITED = Benchmark;
Mike Reed534e7762018-11-05 07:46:38 -050049};
50
51class PathOpsSimplifyBench : public Benchmark {
52 SkString fName;
53 SkPath fPath;
54
55public:
56 PathOpsSimplifyBench(const char suffix[], const SkPath& path) : fPath(path) {
57 fName.printf("pathops_simplify_%s", suffix);
58 }
59
60 bool isSuitableFor(Backend backend) override {
61 return backend == kNonRendering_Backend;
62 }
63
64protected:
65 const char* onGetName() override {
66 return fName.c_str();
67 }
68
69 void onDraw(int loops, SkCanvas* canvas) override {
70 for (int i = 0; i < loops; i++) {
71 for (int j = 0; j < 100; ++j) {
72 SkPath result;
73 Simplify(fPath, &result);
74 }
75 }
76 }
77
78private:
John Stiles7571f9e2020-09-02 22:42:33 -040079 using INHERITED = Benchmark;
Mike Reed534e7762018-11-05 07:46:38 -050080};
Mike Reed534e7762018-11-05 07:46:38 -050081DEF_BENCH( return new PathOpsBench("sect", kIntersect_SkPathOp); )
82DEF_BENCH( return new PathOpsBench("join", kUnion_SkPathOp); )
83
84static SkPath makerects() {
85 SkRandom rand;
86 SkPath path;
87 SkScalar scale = 100;
88 for (int i = 0; i < 20; ++i) {
89 SkScalar x = rand.nextUScalar1() * scale;
90 SkScalar y = rand.nextUScalar1() * scale;
91 path.addRect({x, y, x + scale, y + scale});
92 }
93 return path;
94}
Mike Reed534e7762018-11-05 07:46:38 -050095DEF_BENCH( return new PathOpsSimplifyBench("rects", makerects()); )
Mike Reed22f246f2020-06-23 21:06:28 -040096
97#include "include/core/SkPathBuilder.h"
98
99template <size_t N> struct ArrayPath {
100 SkPoint fPts[N];
101 uint8_t fVbs[N];
102 int fPIndex = 0, fVIndex = 0;
103
104 void moveTo(float x, float y) {
105 fVbs[fVIndex++] = (uint8_t)SkPathVerb::kMove;
106 fPts[fPIndex++] = {x, y};
107 }
108 void lineTo(float x, float y) {
109 fVbs[fVIndex++] = (uint8_t)SkPathVerb::kLine;
110 fPts[fPIndex++] = {x, y};
111 }
112 void quadTo(float x, float y, float x1, float y1) {
113 fVbs[fVIndex++] = (uint8_t)SkPathVerb::kQuad;
114 fPts[fPIndex++] = {x, y};
115 fPts[fPIndex++] = {x1, y1};
116 }
117 void cubicTo(float x, float y, float x1, float y1, float x2, float y2) {
118 fVbs[fVIndex++] = (uint8_t)SkPathVerb::kCubic;
119 fPts[fPIndex++] = {x, y};
120 fPts[fPIndex++] = {x1, y1};
121 fPts[fPIndex++] = {x2, y2};
122 }
123 void incReserve(int) {}
124};
125
126template <typename T> void run_builder(T& b, bool useReserve, int N) {
127 if (useReserve) {
128 b.incReserve(N * 12);
129 }
130
131 float x = 0, y = 0;
132 b.moveTo(x, y);
133 for (int i = 1; i < N; ++i) {
134 b.lineTo(x, y);
135 b.quadTo(x, y, x, y);
136 b.cubicTo(x, y, x, y, x, y);
137 }
138}
139
140enum class MakeType {
141 kPath,
142 kSnapshot,
143 kDetach,
144 kArray,
145};
146
147class PathBuilderBench : public Benchmark {
148 SkString fName;
149 MakeType fMakeType;
150 bool fUseReserve;
151
152 enum { N = 100 };
153 ArrayPath<N*12> fArrays;
154
155public:
156 PathBuilderBench(MakeType mt, bool reserve) : fMakeType(mt), fUseReserve(reserve) {
157 const char* typenames[] = { "path", "snapshot", "detach", "arrays" };
158
159 fName.printf("makepath_%s_%s", typenames[(int)mt], reserve ? "reserve" : "noreserve");
160 }
161
162 bool isSuitableFor(Backend backend) override {
163 return backend == kNonRendering_Backend;
164 }
165
166protected:
167 const char* onGetName() override {
168 return fName.c_str();
169 }
170
171 void onDelayedSetup() override {
172 run_builder(fArrays, false, N);
173 }
174
175 SkPath build() {
176 switch (fMakeType) {
177 case MakeType::kSnapshot:
178 case MakeType::kDetach: {
179 SkPathBuilder b;
180 run_builder(b, fUseReserve, N);
181 return MakeType::kSnapshot == fMakeType ? b.snapshot() : b.detach();
182 }
183 case MakeType::kPath: {
184 SkPath p;
185 run_builder(p, fUseReserve, N);
186 return p;
187 }
188 case MakeType::kArray: {
189 // ArrayPath<N*12> arrays;
190 // run_builder(arrays, false, N);
Mike Reed92c33f32020-07-02 09:14:20 -0400191 return SkPath::Make(fArrays.fPts, fArrays.fPIndex,
192 fArrays.fVbs, fArrays.fVIndex,
193 nullptr, 0, SkPathFillType::kWinding);
Mike Reed22f246f2020-06-23 21:06:28 -0400194 }
195 }
196 return SkPath();
197 }
198
199 void onDraw(int loops, SkCanvas* canvas) override {
200 for (int i = 0; i < loops; i++) {
201 for (int j = 0; j < 100; ++j) {
202 SkPath result = this->build();
203 // force bounds calc as part of the test
204 if (!result.getBounds().isFinite()) {
205 SkDebugf("should never get here!\n");
206 return;
207 }
208 }
209 }
210 }
211
212private:
John Stiles7571f9e2020-09-02 22:42:33 -0400213 using INHERITED = Benchmark;
Mike Reed22f246f2020-06-23 21:06:28 -0400214};
215DEF_BENCH( return new PathBuilderBench(MakeType::kPath, false); )
216DEF_BENCH( return new PathBuilderBench(MakeType::kSnapshot, false); )
217DEF_BENCH( return new PathBuilderBench(MakeType::kDetach, false); )
218DEF_BENCH( return new PathBuilderBench(MakeType::kPath, true); )
219DEF_BENCH( return new PathBuilderBench(MakeType::kSnapshot, true); )
220DEF_BENCH( return new PathBuilderBench(MakeType::kDetach, true); )
221
222DEF_BENCH( return new PathBuilderBench(MakeType::kArray, true); )