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: |
reed | ff2f423 | 2015-05-29 07:17:16 -0700 | [diff] [blame^] | 16 | StrokeBench(const SkPath& path, const SkPaint& paint, const char pathType[]) |
| 17 | : fPath(path), fPaint(paint) |
| 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: |
reed | ff2f423 | 2015-05-29 07:17:16 -0700 | [diff] [blame^] | 24 | const char* onGetName() override { return fName.c_str(); } |
reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 25 | |
reed | ff2f423 | 2015-05-29 07:17:16 -0700 | [diff] [blame^] | 26 | void onDraw(const int loops, SkCanvas* canvas) override { |
| 27 | SkPaint paint(fPaint); |
| 28 | this->setupPaint(&paint); |
| 29 | |
| 30 | for (int outer = 0; outer < 10; ++outer) { |
| 31 | for (int i = 0; i < loops; ++i) { |
| 32 | SkPath result; |
| 33 | paint.getFillPath(fPath, &result); |
| 34 | } |
| 35 | } |
reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | private: |
reed | ff2f423 | 2015-05-29 07:17:16 -0700 | [diff] [blame^] | 39 | SkPath fPath; |
| 40 | SkPaint fPaint; |
| 41 | SkString fName; |
| 42 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 43 | typedef Benchmark INHERITED; |
reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
reed | ff2f423 | 2015-05-29 07:17:16 -0700 | [diff] [blame^] | 46 | /////////////////////////////////////////////////////////////////////////////// |
reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 47 | |
reed | ff2f423 | 2015-05-29 07:17:16 -0700 | [diff] [blame^] | 48 | static const int N = 100; |
| 49 | static const SkScalar X = 100; |
| 50 | static const SkScalar Y = 100; |
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 SkPoint rand_pt(SkRandom& rand) { |
| 53 | return SkPoint::Make(rand.nextSScalar1() * X, rand.nextSScalar1() * Y); |
| 54 | } |
| 55 | |
| 56 | static SkPath line_path_maker() { |
| 57 | SkPath path; |
| 58 | SkRandom rand; |
| 59 | path.moveTo(rand_pt(rand)); |
| 60 | for (int i = 0; i < N; ++i) { |
| 61 | path.lineTo(rand_pt(rand)); |
| 62 | } |
| 63 | return path; |
| 64 | } |
| 65 | static SkPath quad_path_maker() { |
| 66 | SkPath path; |
| 67 | SkRandom rand; |
| 68 | path.moveTo(rand_pt(rand)); |
| 69 | for (int i = 0; i < N; ++i) { |
| 70 | path.quadTo(rand_pt(rand), rand_pt(rand)); |
| 71 | } |
| 72 | return path; |
| 73 | } |
| 74 | static SkPath conic_path_maker() { |
| 75 | SkPath path; |
| 76 | SkRandom rand; |
| 77 | path.moveTo(rand_pt(rand)); |
| 78 | for (int i = 0; i < N; ++i) { |
| 79 | path.conicTo(rand_pt(rand), rand_pt(rand), rand.nextUScalar1()); |
| 80 | } |
| 81 | return path; |
| 82 | } |
| 83 | static SkPath cubic_path_maker() { |
| 84 | SkPath path; |
| 85 | SkRandom rand; |
| 86 | path.moveTo(rand_pt(rand)); |
| 87 | for (int i = 0; i < N; ++i) { |
| 88 | path.cubicTo(rand_pt(rand), rand_pt(rand), rand_pt(rand)); |
| 89 | } |
| 90 | return path; |
| 91 | } |
| 92 | |
| 93 | static SkPaint paint_maker() { |
| 94 | SkPaint paint; |
| 95 | paint.setStyle(SkPaint::kStroke_Style); |
| 96 | paint.setStrokeWidth(X / 10); |
| 97 | paint.setStrokeJoin(SkPaint::kMiter_Join); |
| 98 | paint.setStrokeCap(SkPaint::kSquare_Cap); |
| 99 | return paint; |
| 100 | } |
| 101 | |
| 102 | DEF_BENCH( return SkNEW_ARGS(StrokeBench, (line_path_maker(), paint_maker(), "line")); ) |
| 103 | DEF_BENCH( return SkNEW_ARGS(StrokeBench, (quad_path_maker(), paint_maker(), "quad")); ) |
| 104 | DEF_BENCH( return SkNEW_ARGS(StrokeBench, (conic_path_maker(), paint_maker(), "conic")); ) |
| 105 | DEF_BENCH( return SkNEW_ARGS(StrokeBench, (cubic_path_maker(), paint_maker(), "cubic")); ) |