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 "Benchmark.h"
9 #include "SkPath.h"
10 #include "SkPathOps.h"
11 #include "SkRandom.h"
12 #include "SkShader.h"
13 #include "SkString.h"
14 #include "SkTArray.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 typedef Benchmark INHERITED;
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 typedef Benchmark INHERITED;
80 };
81
82
83 DEF_BENCH( return new PathOpsBench("sect", kIntersect_SkPathOp); )
84 DEF_BENCH( return new PathOpsBench("join", kUnion_SkPathOp); )
85
makerects()86 static SkPath makerects() {
87 SkRandom rand;
88 SkPath path;
89 SkScalar scale = 100;
90 for (int i = 0; i < 20; ++i) {
91 SkScalar x = rand.nextUScalar1() * scale;
92 SkScalar y = rand.nextUScalar1() * scale;
93 path.addRect({x, y, x + scale, y + scale});
94 }
95 return path;
96 }
97
98 DEF_BENCH( return new PathOpsSimplifyBench("rects", makerects()); )
99