reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 1 | /* |
reed | ff2f423 | 2015-05-29 07:17:16 -0700 | [diff] [blame] | 2 | * Copyright 2015 Google Inc. |
reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 9 | #include "SkPaint.h" |
reed | ff2f423 | 2015-05-29 07:17:16 -0700 | [diff] [blame] | 10 | #include "SkPath.h" |
| 11 | #include "SkRandom.h" |
reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 12 | #include "SkString.h" |
| 13 | |
reed | ff2f423 | 2015-05-29 07:17:16 -0700 | [diff] [blame] | 14 | class StrokeBench : public Benchmark { |
reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 15 | public: |
caryclark | c58e532 | 2015-06-01 06:30:06 -0700 | [diff] [blame] | 16 | StrokeBench(const SkPath& path, const SkPaint& paint, const char pathType[], SkScalar res) |
| 17 | : fPath(path), fPaint(paint), fRes(res) |
reed | ff2f423 | 2015-05-29 07:17:16 -0700 | [diff] [blame] | 18 | { |
| 19 | fName.printf("build_stroke_%s_%g_%d_%d", |
| 20 | pathType, paint.getStrokeWidth(), paint.getStrokeJoin(), paint.getStrokeCap()); |
reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | protected: |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 24 | bool isSuitableFor(Backend backend) override { |
caryclark | c58e532 | 2015-06-01 06:30:06 -0700 | [diff] [blame] | 25 | return backend == kNonRendering_Backend; |
| 26 | } |
| 27 | |
reed | ff2f423 | 2015-05-29 07:17:16 -0700 | [diff] [blame] | 28 | const char* onGetName() override { return fName.c_str(); } |
reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 29 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 30 | void onDraw(int loops, SkCanvas* canvas) override { |
reed | ff2f423 | 2015-05-29 07:17:16 -0700 | [diff] [blame] | 31 | SkPaint paint(fPaint); |
| 32 | this->setupPaint(&paint); |
| 33 | |
| 34 | for (int outer = 0; outer < 10; ++outer) { |
| 35 | for (int i = 0; i < loops; ++i) { |
| 36 | SkPath result; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 37 | paint.getFillPath(fPath, &result, nullptr, fRes); |
reed | ff2f423 | 2015-05-29 07:17:16 -0700 | [diff] [blame] | 38 | } |
| 39 | } |
reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | private: |
reed | ff2f423 | 2015-05-29 07:17:16 -0700 | [diff] [blame] | 43 | SkPath fPath; |
| 44 | SkPaint fPaint; |
| 45 | SkString fName; |
caryclark | c58e532 | 2015-06-01 06:30:06 -0700 | [diff] [blame] | 46 | SkScalar fRes; |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 47 | typedef Benchmark INHERITED; |
reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
reed | ff2f423 | 2015-05-29 07:17:16 -0700 | [diff] [blame] | 50 | /////////////////////////////////////////////////////////////////////////////// |
reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 51 | |
reed | ff2f423 | 2015-05-29 07:17:16 -0700 | [diff] [blame] | 52 | static const int N = 100; |
| 53 | static const SkScalar X = 100; |
| 54 | static const SkScalar Y = 100; |
reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 55 | |
reed | ff2f423 | 2015-05-29 07:17:16 -0700 | [diff] [blame] | 56 | static SkPoint rand_pt(SkRandom& rand) { |
| 57 | return SkPoint::Make(rand.nextSScalar1() * X, rand.nextSScalar1() * Y); |
| 58 | } |
| 59 | |
| 60 | static SkPath line_path_maker() { |
| 61 | SkPath path; |
| 62 | SkRandom rand; |
| 63 | path.moveTo(rand_pt(rand)); |
| 64 | for (int i = 0; i < N; ++i) { |
| 65 | path.lineTo(rand_pt(rand)); |
| 66 | } |
| 67 | return path; |
| 68 | } |
| 69 | static SkPath quad_path_maker() { |
| 70 | SkPath path; |
| 71 | SkRandom rand; |
| 72 | path.moveTo(rand_pt(rand)); |
| 73 | for (int i = 0; i < N; ++i) { |
| 74 | path.quadTo(rand_pt(rand), rand_pt(rand)); |
| 75 | } |
| 76 | return path; |
| 77 | } |
| 78 | static SkPath conic_path_maker() { |
| 79 | SkPath path; |
| 80 | SkRandom rand; |
| 81 | path.moveTo(rand_pt(rand)); |
| 82 | for (int i = 0; i < N; ++i) { |
| 83 | path.conicTo(rand_pt(rand), rand_pt(rand), rand.nextUScalar1()); |
| 84 | } |
| 85 | return path; |
| 86 | } |
| 87 | static SkPath cubic_path_maker() { |
| 88 | SkPath path; |
| 89 | SkRandom rand; |
| 90 | path.moveTo(rand_pt(rand)); |
| 91 | for (int i = 0; i < N; ++i) { |
| 92 | path.cubicTo(rand_pt(rand), rand_pt(rand), rand_pt(rand)); |
| 93 | } |
| 94 | return path; |
| 95 | } |
| 96 | |
| 97 | static SkPaint paint_maker() { |
| 98 | SkPaint paint; |
| 99 | paint.setStyle(SkPaint::kStroke_Style); |
| 100 | paint.setStrokeWidth(X / 10); |
| 101 | paint.setStrokeJoin(SkPaint::kMiter_Join); |
| 102 | paint.setStrokeCap(SkPaint::kSquare_Cap); |
| 103 | return paint; |
| 104 | } |
| 105 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 106 | DEF_BENCH(return new StrokeBench(line_path_maker(), paint_maker(), "line_1", 1);) |
| 107 | DEF_BENCH(return new StrokeBench(quad_path_maker(), paint_maker(), "quad_1", 1);) |
| 108 | DEF_BENCH(return new StrokeBench(conic_path_maker(), paint_maker(), "conic_1", 1);) |
| 109 | DEF_BENCH(return new StrokeBench(cubic_path_maker(), paint_maker(), "cubic_1", 1);) |
caryclark | c58e532 | 2015-06-01 06:30:06 -0700 | [diff] [blame] | 110 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 111 | DEF_BENCH(return new StrokeBench(line_path_maker(), paint_maker(), "line_4", 4);) |
| 112 | DEF_BENCH(return new StrokeBench(quad_path_maker(), paint_maker(), "quad_4", 4);) |
| 113 | DEF_BENCH(return new StrokeBench(conic_path_maker(), paint_maker(), "conic_4", 4);) |
| 114 | DEF_BENCH(return new StrokeBench(cubic_path_maker(), paint_maker(), "cubic_4", 4);) |
caryclark | c58e532 | 2015-06-01 06:30:06 -0700 | [diff] [blame] | 115 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 116 | DEF_BENCH(return new StrokeBench(line_path_maker(), paint_maker(), "line_.25", .25f);) |
| 117 | DEF_BENCH(return new StrokeBench(quad_path_maker(), paint_maker(), "quad_.25", .25f);) |
| 118 | DEF_BENCH(return new StrokeBench(conic_path_maker(), paint_maker(), "conic_.25", .25f);) |
| 119 | DEF_BENCH(return new StrokeBench(cubic_path_maker(), paint_maker(), "cubic_.25", .25f);) |