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