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