Mike Reed | 534e776 | 2018-11-05 07:46:38 -0500 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
Mike Reed | 534e776 | 2018-11-05 07:46:38 -0500 | [diff] [blame] | 15 | |
| 16 | class PathOpsBench : public Benchmark { |
| 17 | SkString fName; |
| 18 | SkPath fPath1, fPath2; |
| 19 | SkPathOp fOp; |
| 20 | |
| 21 | public: |
| 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 | |
| 33 | protected: |
| 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 | |
| 47 | private: |
| 48 | typedef Benchmark INHERITED; |
| 49 | }; |
| 50 | |
| 51 | class PathOpsSimplifyBench : public Benchmark { |
| 52 | SkString fName; |
| 53 | SkPath fPath; |
| 54 | |
| 55 | public: |
| 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 | |
| 64 | protected: |
| 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 | |
| 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 | |
| 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()); ) |