• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
8 #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"
15 
16 class PathOpsBench : public Benchmark {
17     SkString    fName;
18     SkPath      fPath1, fPath2;
19     SkPathOp    fOp;
20 
21 public:
PathOpsBench(const char suffix[],SkPathOp op)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 
isSuitableFor(Backend backend)29     bool isSuitableFor(Backend backend) override {
30         return backend == kNonRendering_Backend;
31     }
32 
33 protected:
onGetName()34     const char* onGetName() override {
35         return fName.c_str();
36     }
37 
onDraw(int loops,SkCanvas * canvas)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 
47 private:
48     using INHERITED = Benchmark;
49 };
50 
51 class PathOpsSimplifyBench : public Benchmark {
52     SkString    fName;
53     SkPath      fPath;
54 
55 public:
PathOpsSimplifyBench(const char suffix[],const SkPath & path)56     PathOpsSimplifyBench(const char suffix[], const SkPath& path) : fPath(path) {
57         fName.printf("pathops_simplify_%s", suffix);
58     }
59 
isSuitableFor(Backend backend)60     bool isSuitableFor(Backend backend) override {
61         return backend == kNonRendering_Backend;
62     }
63 
64 protected:
onGetName()65     const char* onGetName() override {
66         return fName.c_str();
67     }
68 
onDraw(int loops,SkCanvas * canvas)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 
78 private:
79     using INHERITED = Benchmark;
80 };
81 DEF_BENCH( return new PathOpsBench("sect", kIntersect_SkPathOp); )
82 DEF_BENCH( return new PathOpsBench("join", kUnion_SkPathOp); )
83 
makerects()84 static 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 }
95 DEF_BENCH( return new PathOpsSimplifyBench("rects", makerects()); )
96 
97 #include "include/core/SkPathBuilder.h"
98 
99 template <size_t N> struct ArrayPath {
100     SkPoint fPts[N];
101     uint8_t fVbs[N];
102     int fPIndex = 0, fVIndex = 0;
103 
moveToArrayPath104     void moveTo(float x, float y) {
105         fVbs[fVIndex++] = (uint8_t)SkPathVerb::kMove;
106         fPts[fPIndex++] = {x, y};
107     }
lineToArrayPath108     void lineTo(float x, float y) {
109         fVbs[fVIndex++] = (uint8_t)SkPathVerb::kLine;
110         fPts[fPIndex++] = {x, y};
111     }
quadToArrayPath112     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     }
cubicToArrayPath117     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     }
incReserveArrayPath123     void incReserve(int) {}
124 };
125 
run_builder(T & b,bool useReserve,int N)126 template <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 
140 enum class MakeType {
141     kPath,
142     kSnapshot,
143     kDetach,
144     kArray,
145 };
146 
147 class PathBuilderBench : public Benchmark {
148     SkString    fName;
149     MakeType    fMakeType;
150     bool        fUseReserve;
151 
152     enum { N = 100 };
153     ArrayPath<N*12> fArrays;
154 
155 public:
PathBuilderBench(MakeType mt,bool reserve)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 
isSuitableFor(Backend backend)162     bool isSuitableFor(Backend backend) override {
163         return backend == kNonRendering_Backend;
164     }
165 
166 protected:
onGetName()167     const char* onGetName() override {
168         return fName.c_str();
169     }
170 
onDelayedSetup()171     void onDelayedSetup() override {
172         run_builder(fArrays, false, N);
173     }
174 
build()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);
191                 return SkPath::Make(fArrays.fPts, fArrays.fPIndex,
192                                     fArrays.fVbs, fArrays.fVIndex,
193                                     nullptr, 0, SkPathFillType::kWinding);
194             }
195         }
196         return SkPath();
197     }
198 
onDraw(int loops,SkCanvas * canvas)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 
212 private:
213     using INHERITED = Benchmark;
214 };
215 DEF_BENCH( return new PathBuilderBench(MakeType::kPath, false); )
216 DEF_BENCH( return new PathBuilderBench(MakeType::kSnapshot, false); )
217 DEF_BENCH( return new PathBuilderBench(MakeType::kDetach, false); )
218 DEF_BENCH( return new PathBuilderBench(MakeType::kPath, true); )
219 DEF_BENCH( return new PathBuilderBench(MakeType::kSnapshot, true); )
220 DEF_BENCH( return new PathBuilderBench(MakeType::kDetach, true); )
221 
222 DEF_BENCH( return new PathBuilderBench(MakeType::kArray, true); )
223