• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bench/Benchmark.h"
8 #include "include/core/SkBitmap.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColorPriv.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPath.h"
13 #include "include/core/SkShader.h"
14 #include "include/core/SkString.h"
15 #include "include/utils/SkRandom.h"
16 #include "src/core/SkPathPriv.h"
17 
18 enum class PathIterType {
19     kIter,
20     kRaw,
21     kEdge,
22 };
23 const char* gPathIterNames[] = {
24     "iter", "raw", "edge"
25 };
26 
rand_pts(SkRandom & rand,SkPoint pts[4])27 static int rand_pts(SkRandom& rand, SkPoint pts[4]) {
28     int n = rand.nextU() & 3;
29     n += 1;
30 
31     for (int i = 0; i < n; ++i) {
32         pts[i].fX = rand.nextSScalar1();
33         pts[i].fY = rand.nextSScalar1();
34     }
35     return n;
36 }
37 
38 class PathIterBench : public Benchmark {
39     SkString        fName;
40     SkPath          fPath;
41     PathIterType    fType;
42 
43     int fVerbInc = 0;
44     SkScalar fXInc = 0, fYInc = 0;
45 
46 public:
PathIterBench(PathIterType t)47     PathIterBench(PathIterType t) : fType(t) {
48         fName.printf("pathiter_%s", gPathIterNames[static_cast<unsigned>(t)]);
49 
50         SkRandom rand;
51         for (int i = 0; i < 1000; ++i) {
52             SkPoint pts[4];
53             int n = rand_pts(rand, pts);
54             switch (n) {
55                 case 1:
56                     fPath.moveTo(pts[0]);
57                     break;
58                 case 2:
59                     fPath.lineTo(pts[1]);
60                     break;
61                 case 3:
62                     fPath.quadTo(pts[1], pts[2]);
63                     break;
64                 case 4:
65                     fPath.cubicTo(pts[1], pts[2], pts[3]);
66                     break;
67             }
68         }
69     }
70 
isSuitableFor(Backend backend)71     bool isSuitableFor(Backend backend) override {
72         return backend == kNonRendering_Backend;
73     }
74 
75 protected:
onGetName()76     const char* onGetName() override {
77         return fName.c_str();
78     }
79 
onDraw(int loops,SkCanvas *)80     void onDraw(int loops, SkCanvas*) override {
81         // Need to do *something* with the results, so the compile doesn't elide
82         // away the code we want to time.
83         auto handle = [this](int verb, const SkPoint pts[]) {
84             fVerbInc += verb;
85             fXInc += pts[0].fX;
86             fYInc += pts[0].fY;
87         };
88 
89         switch (fType) {
90             case PathIterType::kIter:
91                 for (int i = 0; i < loops; ++i) {
92                     SkPath::Iter iter(fPath, true);
93                     SkPath::Verb verb;
94                     SkPoint      pts[4];
95                     while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
96                         handle(verb, pts);
97                     }
98                 }
99                 break;
100             case PathIterType::kRaw:
101                 for (int i = 0; i < loops; ++i) {
102                     for (auto [verb, pts, w] : SkPathPriv::Iterate(fPath)) {
103                         handle((SkPath::Verb)verb, pts);
104                     }
105                 }
106                 break;
107             case PathIterType::kEdge:
108                 for (int i = 0; i < loops; ++i) {
109                     SkPathEdgeIter iter(fPath);
110                     while (auto r = iter.next()) {
111                         handle((int)r.fEdge, r.fPts);
112                     }
113                 }
114                 break;
115         }
116     }
117 
118 private:
119     using INHERITED = Benchmark;
120 };
121 
122 ///////////////////////////////////////////////////////////////////////////////
123 
124 DEF_BENCH( return new PathIterBench(PathIterType::kIter); )
125 DEF_BENCH( return new PathIterBench(PathIterType::kRaw); )
126 DEF_BENCH( return new PathIterBench(PathIterType::kEdge); )
127