| 1 |
/* |
| 2 |
* Copyright 2018 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 |
|
| 8 |
#include "bench/Benchmark.h" |
| 9 |
#include "include/core/SkPath.h" |
| 10 |
#include "include/core/SkPathBuilder.h" |
| 11 |
#include "include/core/SkShader.h" |
| 12 |
#include "include/core/SkString.h" |
| 13 |
#include "include/pathops/SkPathOps.h" |
| 14 |
#include "include/private/base/SkTArray.h" |
| 15 |
#include "src/base/SkRandom.h" |
| 16 |
|
| 17 |
class PathOpsBench : public Benchmark { |
| 18 |
SkString fName; |
| 19 |
SkPath fPath1, fPath2; |
| 20 |
SkPathOp fOp; |
| 21 |
|
| 22 |
public: |
| 23 |
PathOpsBench(const char suffix[], SkPathOp op) : fOp(op) { |
| 24 |
fName.printf("pathops_%s", suffix); |
| 25 |
|
| 26 |
fPath1 = SkPath::Oval({-10, -20, 10, 20}); |
| 27 |
fPath2 = SkPath::Oval({-20, -10, 20, 10}); |
| 28 |
} |
| 29 |
|
| 30 |
bool isSuitableFor(Backend backend) override { |
| 31 |
return backend == Backend::kNonRendering; |
| 32 |
} |
| 33 |
|
| 34 |
protected: |
| 35 |
const char* onGetName() override { |
| 36 |
return fName.c_str(); |
| 37 |
} |
| 38 |
|
| 39 |
void onDraw(int loops, SkCanvas* canvas) override { |
| 40 |
for (int i = 0; i < loops; i++) { |
| 41 |
for (int j = 0; j < 1000; ++j) { |
| 42 |
std::ignore = Op(fPath1, fPath2, fOp); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
private: |
| 48 |
using INHERITED = Benchmark; |
| 49 |
}; |
| 50 |
|
| 51 |
class PathOpsSimplifyBench : public Benchmark { |
| 52 |
SkString fName; |
| 53 |
SkPath fPath; |
| 54 |
|
| 55 |
public: |
| 56 |
PathOpsSimplifyBench(const char suffix[], const SkPath& path) : fPath(path) { |
| 57 |
fName.printf("pathops_simplify_%s", suffix); |
| 58 |
} |
| 59 |
|
| 60 |
bool isSuitableFor(Backend backend) override { |
| 61 |
return backend == Backend::kNonRendering; |
| 62 |
} |
| 63 |
|
| 64 |
protected: |
| 65 |
const char* onGetName() override { |
| 66 |
return fName.c_str(); |
| 67 |
} |
| 68 |
|
| 69 |
void onDraw(int loops, SkCanvas* canvas) override { |
| 70 |
for (int i = 0; i < loops; i++) { |
| 71 |
for (int j = 0; j < 100; ++j) { |
| 72 |
std::ignore = Simplify(fPath); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
private: |
| 78 |
using INHERITED = Benchmark; |
| 79 |
}; |
| 80 |
DEF_BENCH( return new PathOpsBench("sect", kIntersect_SkPathOp); ) |
| 81 |
DEF_BENCH( return new PathOpsBench("join", kUnion_SkPathOp); ) |
| 82 |
|
| 83 |
static SkPath makerects() { |
| 84 |
SkRandom rand; |
| 85 |
SkPathBuilder builder; |
| 86 |
SkScalar scale = 100; |
| 87 |
for (int i = 0; i < 20; ++i) { |
| 88 |
SkScalar x = rand.nextUScalar1() * scale; |
| 89 |
SkScalar y = rand.nextUScalar1() * scale; |
| 90 |
builder.addRect({x, y, x + scale, y + scale}); |
| 91 |
} |
| 92 |
return builder.detach(); |
| 93 |
} |
| 94 |
DEF_BENCH( return new PathOpsSimplifyBench("rects", makerects()); ) |
| 95 |
|
| 96 |
#include "include/core/SkPathBuilder.h" |
| 97 |
|
| 98 |
template <size_t N> struct ArrayPath { |
| 99 |
SkPoint fPts[N]; |
| 100 |
SkPathVerb fVbs[N]; |
| 101 |
int fPIndex = 0, fVIndex = 0; |
| 102 |
|
| 103 |
void moveTo(float x, float y) { |
| 104 |
fVbs[fVIndex++] = SkPathVerb::kMove; |
| 105 |
fPts[fPIndex++] = {x, y}; |
| 106 |
} |
| 107 |
void lineTo(float x, float y) { |
| 108 |
fVbs[fVIndex++] = SkPathVerb::kLine; |
| 109 |
fPts[fPIndex++] = {x, y}; |
| 110 |
} |
| 111 |
void quadTo(float x, float y, float x1, float y1) { |
| 112 |
fVbs[fVIndex++] = SkPathVerb::kQuad; |
| 113 |
fPts[fPIndex++] = {x, y}; |
| 114 |
fPts[fPIndex++] = {x1, y1}; |
| 115 |
} |
| 116 |
void cubicTo(float x, float y, float x1, float y1, float x2, float y2) { |
| 117 |
fVbs[fVIndex++] = SkPathVerb::kCubic; |
| 118 |
fPts[fPIndex++] = {x, y}; |
| 119 |
fPts[fPIndex++] = {x1, y1}; |
| 120 |
fPts[fPIndex++] = {x2, y2}; |
| 121 |
} |
| 122 |
void incReserve(int) {} |
| 123 |
}; |
| 124 |
|
| 125 |
template <typename T> void run_builder(T& b, bool useReserve, int N) { |
| 126 |
if (useReserve) { |
| 127 |
b.incReserve(N * 12); |
| 128 |
} |
| 129 |
|
| 130 |
float x = 0, y = 0; |
| 131 |
b.moveTo(x, y); |
| 132 |
for (int i = 1; i < N; ++i) { |
| 133 |
b.lineTo(x, y); |
| 134 |
b.quadTo(x, y, x, y); |
| 135 |
b.cubicTo(x, y, x, y, x, y); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
enum class MakeType { |
| 140 |
kSnapshot, |
| 141 |
kDetach, |
| 142 |
kArray, |
| 143 |
}; |
| 144 |
|
| 145 |
class PathBuilderBench : public Benchmark { |
| 146 |
SkString fName; |
| 147 |
MakeType fMakeType; |
| 148 |
bool fUseReserve; |
| 149 |
|
| 150 |
enum { N = 100 }; |
| 151 |
ArrayPath<N*12> fArrays; |
| 152 |
|
| 153 |
public: |
| 154 |
PathBuilderBench(MakeType mt, bool reserve) : fMakeType(mt), fUseReserve(reserve) { |
| 155 |
const char* typenames[] = { "path", "snapshot", "detach", "arrays" }; |
| 156 |
|
| 157 |
fName.printf("makepath_%s_%s", typenames[(int)mt], reserve ? "reserve" : "noreserve"); |
| 158 |
} |
| 159 |
|
| 160 |
bool isSuitableFor(Backend backend) override { |
| 161 |
return backend == Backend::kNonRendering; |
| 162 |
} |
| 163 |
|
| 164 |
protected: |
| 165 |
const char* onGetName() override { |
| 166 |
return fName.c_str(); |
| 167 |
} |
| 168 |
|
| 169 |
void onDelayedSetup() override { |
| 170 |
run_builder(fArrays, false, N); |
| 171 |
} |
| 172 |
|
| 173 |
SkPath build() { |
| 174 |
switch (fMakeType) { |
| 175 |
case MakeType::kSnapshot: |
| 176 |
case MakeType::kDetach: { |
| 177 |
SkPathBuilder b; |
| 178 |
run_builder(b, fUseReserve, N); |
| 179 |
return MakeType::kSnapshot == fMakeType ? b.snapshot() : b.detach(); |
| 180 |
} |
| 181 |
case MakeType::kArray: { |
| 182 |
// ArrayPath<N*12> arrays; |
| 183 |
// run_builder(arrays, false, N); |
| 184 |
return SkPath::Raw({fArrays.fPts, (size_t)fArrays.fPIndex}, |
| 185 |
{fArrays.fVbs, (size_t)fArrays.fVIndex}, |
| 186 |
{}, SkPathFillType::kWinding); |
| 187 |
} |
| 188 |
} |
| 189 |
return SkPath(); |
| 190 |
} |
| 191 |
|
| 192 |
void onDraw(int loops, SkCanvas* canvas) override { |
| 193 |
for (int i = 0; i < loops; i++) { |
| 194 |
for (int j = 0; j < 100; ++j) { |
| 195 |
SkPath result = this->build(); |
| 196 |
// force bounds calc as part of the test |
| 197 |
if (!result.getBounds().isFinite()) { |
| 198 |
SkDebugf("should never get here!\n"); |
| 199 |
return; |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
private: |
| 206 |
using INHERITED = Benchmark; |
| 207 |
}; |
| 208 |
DEF_BENCH( return new PathBuilderBench(MakeType::kSnapshot, false); ) |
| 209 |
DEF_BENCH( return new PathBuilderBench(MakeType::kDetach, false); ) |
| 210 |
DEF_BENCH( return new PathBuilderBench(MakeType::kSnapshot, true); ) |
| 211 |
DEF_BENCH( return new PathBuilderBench(MakeType::kDetach, true); ) |
| 212 |
|
| 213 |
DEF_BENCH( return new PathBuilderBench(MakeType::kArray, true); ) |