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 */
7 #include "Benchmark.h"
8 #include "SkBitmap.h"
9 #include "SkCanvas.h"
10 #include "SkColorPriv.h"
11 #include "SkPaint.h"
12 #include "SkPath.h"
13 #include "SkRandom.h"
14 #include "SkShader.h"
15 #include "SkString.h"
16
rand_pts(SkRandom & rand,SkPoint pts[4])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 Benchmark {
29 SkString fName;
30 SkPath fPath;
31 bool fRaw;
32
33 public:
PathIterBench(bool raw)34 PathIterBench(bool raw) {
35 fName.printf("pathiter_%s", raw ? "raw" : "consume");
36 fRaw = raw;
37
38 SkRandom rand;
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 }
57 }
58
isSuitableFor(Backend backend)59 bool isSuitableFor(Backend backend) override {
60 return backend == kNonRendering_Backend;
61 }
62
63 protected:
onGetName()64 const char* onGetName() override {
65 return fName.c_str();
66 }
67
onDraw(int loops,SkCanvas *)68 void onDraw(int loops, SkCanvas*) override {
69 if (fRaw) {
70 for (int i = 0; i < loops; ++i) {
71 SkPath::RawIter iter(fPath);
72 SkPath::Verb verb;
73 SkPoint pts[4];
74
75 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { }
76 }
77 } else {
78 for (int i = 0; i < loops; ++i) {
79 SkPath::Iter iter(fPath, false);
80 SkPath::Verb verb;
81 SkPoint pts[4];
82
83 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { }
84 }
85 }
86 }
87
88 private:
89 typedef Benchmark INHERITED;
90 };
91
92 ///////////////////////////////////////////////////////////////////////////////
93
94 DEF_BENCH( return new PathIterBench(false); )
95 DEF_BENCH( return new PathIterBench(true); )
96