blob: 4d2e9e1f60cc3d0d4831810bd36090df8b2f2066 [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
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
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:
48 typedef Benchmark INHERITED;
49};
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:
79 typedef Benchmark INHERITED;
80};
81
82
83DEF_BENCH( return new PathOpsBench("sect", kIntersect_SkPathOp); )
84DEF_BENCH( return new PathOpsBench("join", kUnion_SkPathOp); )
85
86static 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
98DEF_BENCH( return new PathOpsSimplifyBench("rects", makerects()); )