reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 9 | #include "SkBitmap.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkColorPriv.h" |
| 12 | #include "SkPaint.h" |
bungeman | 7403d87 | 2015-08-04 14:56:53 -0700 | [diff] [blame^] | 13 | #include "SkPath.h" |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 14 | #include "SkRandom.h" |
| 15 | #include "SkShader.h" |
| 16 | #include "SkString.h" |
| 17 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 18 | static int rand_pts(SkRandom& rand, SkPoint pts[4]) { |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 19 | int n = rand.nextU() & 3; |
| 20 | n += 1; |
| 21 | |
| 22 | for (int i = 0; i < n; ++i) { |
| 23 | pts[i].fX = rand.nextSScalar1(); |
| 24 | pts[i].fY = rand.nextSScalar1(); |
| 25 | } |
| 26 | return n; |
| 27 | } |
| 28 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 29 | class PathIterBench : public Benchmark { |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 30 | SkString fName; |
| 31 | SkPath fPath; |
| 32 | bool fRaw; |
| 33 | |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 34 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 35 | PathIterBench(bool raw) { |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 36 | fName.printf("pathiter_%s", raw ? "raw" : "consume"); |
| 37 | fRaw = raw; |
| 38 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 39 | SkRandom rand; |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 40 | for (int i = 0; i < 1000; ++i) { |
| 41 | SkPoint pts[4]; |
| 42 | int n = rand_pts(rand, pts); |
| 43 | switch (n) { |
| 44 | case 1: |
| 45 | fPath.moveTo(pts[0]); |
| 46 | break; |
| 47 | case 2: |
| 48 | fPath.lineTo(pts[1]); |
| 49 | break; |
| 50 | case 3: |
| 51 | fPath.quadTo(pts[1], pts[2]); |
| 52 | break; |
| 53 | case 4: |
| 54 | fPath.cubicTo(pts[1], pts[2], pts[3]); |
| 55 | break; |
| 56 | } |
| 57 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 58 | } |
robertphillips@google.com | 433ce5e | 2012-09-17 10:49:30 +0000 | [diff] [blame] | 59 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 60 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 61 | return backend == kNonRendering_Backend; |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 65 | const char* onGetName() override { |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 66 | return fName.c_str(); |
| 67 | } |
| 68 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 69 | void onDraw(const int loops, SkCanvas*) override { |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 70 | if (fRaw) { |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 71 | for (int i = 0; i < loops; ++i) { |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 72 | SkPath::RawIter iter(fPath); |
| 73 | SkPath::Verb verb; |
| 74 | SkPoint pts[4]; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 75 | |
commit-bot@chromium.org | faa5ae4 | 2013-07-23 11:13:56 +0000 | [diff] [blame] | 76 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { } |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 77 | } |
| 78 | } else { |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 79 | for (int i = 0; i < loops; ++i) { |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 80 | SkPath::Iter iter(fPath, false); |
| 81 | SkPath::Verb verb; |
| 82 | SkPoint pts[4]; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 83 | |
commit-bot@chromium.org | faa5ae4 | 2013-07-23 11:13:56 +0000 | [diff] [blame] | 84 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { } |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 90 | typedef Benchmark INHERITED; |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | /////////////////////////////////////////////////////////////////////////////// |
| 94 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 95 | DEF_BENCH( return new PathIterBench(false); ) |
| 96 | DEF_BENCH( return new PathIterBench(true); ) |