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" |
| 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 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 59 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| 60 | return backend == kNonRendering_Backend; |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | protected: |
| 64 | virtual const char* onGetName() SK_OVERRIDE { |
| 65 | return fName.c_str(); |
| 66 | } |
| 67 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 68 | virtual void onDraw(const int loops, SkCanvas*) SK_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); ) |