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 | */ |
| 8 | #include "SkBenchmark.h" |
| 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 | |
| 17 | static int rand_pts(SkRandom& rand, SkPoint pts[4]) { |
| 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 | |
| 28 | class PathIterBench : public SkBenchmark { |
| 29 | SkString fName; |
| 30 | SkPath fPath; |
| 31 | bool fRaw; |
| 32 | |
| 33 | enum { N = SkBENCHLOOP(500) }; |
| 34 | |
| 35 | public: |
| 36 | PathIterBench(void* param, bool raw) : INHERITED(param) { |
| 37 | fName.printf("pathiter_%s", raw ? "raw" : "consume"); |
| 38 | fRaw = raw; |
| 39 | |
| 40 | SkRandom rand; |
| 41 | for (int i = 0; i < 1000; ++i) { |
| 42 | SkPoint pts[4]; |
| 43 | int n = rand_pts(rand, pts); |
| 44 | switch (n) { |
| 45 | case 1: |
| 46 | fPath.moveTo(pts[0]); |
| 47 | break; |
| 48 | case 2: |
| 49 | fPath.lineTo(pts[1]); |
| 50 | break; |
| 51 | case 3: |
| 52 | fPath.quadTo(pts[1], pts[2]); |
| 53 | break; |
| 54 | case 4: |
| 55 | fPath.cubicTo(pts[1], pts[2], pts[3]); |
| 56 | break; |
| 57 | } |
| 58 | } |
robertphillips@google.com | 433ce5e | 2012-09-17 10:49:30 +0000 | [diff] [blame] | 59 | |
| 60 | fIsRendering = false; |
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 | |
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 68 | virtual void onDraw(SkCanvas*) SK_OVERRIDE { |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 69 | if (fRaw) { |
| 70 | for (int i = 0; i < N; ++i) { |
| 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 | |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 75 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb); |
| 76 | } |
| 77 | } else { |
| 78 | for (int i = 0; i < N; ++i) { |
| 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 | |
reed@google.com | c56a83c | 2012-05-07 13:46:32 +0000 | [diff] [blame] | 83 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | private: |
| 89 | typedef SkBenchmark INHERITED; |
| 90 | }; |
| 91 | |
| 92 | /////////////////////////////////////////////////////////////////////////////// |
| 93 | |
| 94 | static SkBenchmark* F0(void* p) { return new PathIterBench(p, false); } |
| 95 | static SkBenchmark* F1(void* p) { return new PathIterBench(p, true); } |
| 96 | |
| 97 | static BenchRegistry gR0(F0); |
| 98 | static BenchRegistry gR1(F1); |