| 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/SkPoint.h" |
| 10 |
#include "include/core/SkRect.h" |
| 11 |
#include "include/private/base/SkTDArray.h" |
| 12 |
#include "include/private/base/SkTemplates.h" |
| 13 |
#include "src/utils/SkPolyUtils.h" |
| 14 |
|
| 15 |
using namespace skia_private; |
| 16 |
|
| 17 |
#if !defined(SK_ENABLE_OPTIMIZE_SIZE) |
| 18 |
|
| 19 |
class PolyUtilsBench : public Benchmark { |
| 20 |
public: |
| 21 |
// Evaluate SkTriangulateSimplePolygon's performance (via derived classes) on: |
| 22 |
// a non-self-intersecting star, a circle of tiny line segments and a self-intersecting star |
| 23 |
enum class Type { kConvexCheck, kSimpleCheck, kInsetConvex, kOffsetSimple, kTessellateSimple }; |
| 24 |
|
| 25 |
PolyUtilsBench(Type type) : fType(type) {} |
| 26 |
|
| 27 |
virtual void appendName(SkString*) = 0; |
| 28 |
virtual void makePoly(SkTDArray<SkPoint>* poly) = 0; |
| 29 |
virtual int complexity() { return 0; } |
| 30 |
|
| 31 |
protected: |
| 32 |
const char* onGetName() override { |
| 33 |
fName = "poly_utils_"; |
| 34 |
this->appendName(&fName); |
| 35 |
switch (fType) { |
| 36 |
case Type::kConvexCheck: |
| 37 |
fName.append("_c"); |
| 38 |
break; |
| 39 |
case Type::kSimpleCheck: |
| 40 |
fName.append("_s"); |
| 41 |
break; |
| 42 |
case Type::kInsetConvex: |
| 43 |
fName.append("_i"); |
| 44 |
break; |
| 45 |
case Type::kOffsetSimple: |
| 46 |
fName.append("_o"); |
| 47 |
break; |
| 48 |
case Type::kTessellateSimple: |
| 49 |
fName.append("_t"); |
| 50 |
break; |
| 51 |
} |
| 52 |
return fName.c_str(); |
| 53 |
} |
| 54 |
|
| 55 |
void onDraw(int loops, SkCanvas* canvas) override { |
| 56 |
SkTDArray<SkPoint> poly; |
| 57 |
this->makePoly(&poly); |
| 58 |
switch (fType) { |
| 59 |
case Type::kConvexCheck: |
| 60 |
for (int i = 0; i < loops; i++) { |
| 61 |
(void)SkIsConvexPolygon(poly.begin(), poly.size()); |
| 62 |
} |
| 63 |
break; |
| 64 |
case Type::kSimpleCheck: |
| 65 |
for (int i = 0; i < loops; i++) { |
| 66 |
(void)SkIsSimplePolygon(poly.begin(), poly.size()); |
| 67 |
} |
| 68 |
break; |
| 69 |
case Type::kInsetConvex: |
| 70 |
if (SkIsConvexPolygon(poly.begin(), poly.size())) { |
| 71 |
SkTDArray<SkPoint> result; |
| 72 |
for (int i = 0; i < loops; i++) { |
| 73 |
(void)SkInsetConvexPolygon(poly.begin(), poly.size(), 10, &result); |
| 74 |
(void)SkInsetConvexPolygon(poly.begin(), poly.size(), 40, &result); |
| 75 |
} |
| 76 |
} |
| 77 |
break; |
| 78 |
case Type::kOffsetSimple: |
| 79 |
if (SkIsSimplePolygon(poly.begin(), poly.size())) { |
| 80 |
SkTDArray<SkPoint> result; |
| 81 |
const auto bounds = SkRect::BoundsOrEmpty(poly); |
| 82 |
for (int i = 0; i < loops; i++) { |
| 83 |
(void)SkOffsetSimplePolygon(poly.begin(), poly.size(), bounds, 10, |
| 84 |
&result); |
| 85 |
(void)SkOffsetSimplePolygon(poly.begin(), poly.size(), bounds, -10, |
| 86 |
&result); |
| 87 |
} |
| 88 |
} |
| 89 |
break; |
| 90 |
case Type::kTessellateSimple: |
| 91 |
if (SkIsSimplePolygon(poly.begin(), poly.size())) { |
| 92 |
AutoSTMalloc<64, uint16_t> indexMap(poly.size()); |
| 93 |
for (int i = 0; i < poly.size(); ++i) { |
| 94 |
indexMap[i] = i; |
| 95 |
} |
| 96 |
SkTDArray<uint16_t> triangleIndices; |
| 97 |
for (int i = 0; i < loops; i++) { |
| 98 |
SkTriangulateSimplePolygon(poly.begin(), indexMap, poly.size(), |
| 99 |
&triangleIndices); |
| 100 |
} |
| 101 |
} |
| 102 |
break; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
private: |
| 107 |
SkString fName; |
| 108 |
Type fType; |
| 109 |
|
| 110 |
using INHERITED = Benchmark; |
| 111 |
}; |
| 112 |
|
| 113 |
class StarPolyUtilsBench : public PolyUtilsBench { |
| 114 |
public: |
| 115 |
StarPolyUtilsBench(PolyUtilsBench::Type type) : INHERITED(type) {} |
| 116 |
|
| 117 |
void appendName(SkString* name) override { |
| 118 |
name->append("star"); |
| 119 |
} |
| 120 |
void makePoly(SkTDArray<SkPoint>* poly) override { |
| 121 |
// create non-intersecting star |
| 122 |
const SkScalar c = SkIntToScalar(45); |
| 123 |
const SkScalar r1 = SkIntToScalar(20); |
| 124 |
const SkScalar r2 = SkIntToScalar(3); |
| 125 |
const int n = 500; |
| 126 |
SkScalar rad = 0; |
| 127 |
const SkScalar drad = SK_ScalarPI / n; |
| 128 |
for (int i = 0; i < n; i++) { |
| 129 |
*poly->append() = SkPoint::Make(c + SkScalarCos(rad) * r1, c + SkScalarSin(rad) * r1); |
| 130 |
rad += drad; |
| 131 |
*poly->append() = SkPoint::Make(c + SkScalarCos(rad) * r2, c + SkScalarSin(rad) * r2); |
| 132 |
rad += drad; |
| 133 |
} |
| 134 |
} |
| 135 |
private: |
| 136 |
using INHERITED = PolyUtilsBench; |
| 137 |
}; |
| 138 |
|
| 139 |
class CirclePolyUtilsBench : public PolyUtilsBench { |
| 140 |
public: |
| 141 |
CirclePolyUtilsBench(PolyUtilsBench::Type type) : INHERITED(type) {} |
| 142 |
|
| 143 |
void appendName(SkString* name) override { |
| 144 |
name->append("circle"); |
| 145 |
} |
| 146 |
void makePoly(SkTDArray<SkPoint>* poly) override { |
| 147 |
// create circle with many vertices |
| 148 |
const SkScalar c = SkIntToScalar(45); |
| 149 |
const SkScalar r = SkIntToScalar(20); |
| 150 |
const int n = 1000; |
| 151 |
SkScalar rad = 0; |
| 152 |
const SkScalar drad = 2 * SK_ScalarPI / n; |
| 153 |
for (int i = 0; i < n; i++) { |
| 154 |
*poly->append() = SkPoint::Make(c + SkScalarCos(rad) * r, c + SkScalarSin(rad) * r); |
| 155 |
rad += drad; |
| 156 |
} |
| 157 |
} |
| 158 |
private: |
| 159 |
using INHERITED = PolyUtilsBench; |
| 160 |
}; |
| 161 |
|
| 162 |
class IntersectingPolyUtilsBench : public PolyUtilsBench { |
| 163 |
public: |
| 164 |
IntersectingPolyUtilsBench(PolyUtilsBench::Type type) : INHERITED(type) {} |
| 165 |
|
| 166 |
void appendName(SkString* name) override { |
| 167 |
name->append("intersecting"); |
| 168 |
} |
| 169 |
void makePoly(SkTDArray<SkPoint>* poly) override { |
| 170 |
// create self-intersecting star |
| 171 |
const SkScalar c = SkIntToScalar(45); |
| 172 |
const SkScalar r = SkIntToScalar(20); |
| 173 |
const int n = 1000; |
| 174 |
|
| 175 |
SkScalar rad = -SK_ScalarPI / 2; |
| 176 |
const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n; |
| 177 |
*poly->append() = SkPoint::Make(c, c - r); |
| 178 |
for (int i = 1; i < n; i++) { |
| 179 |
rad += drad; |
| 180 |
*poly->append() = SkPoint::Make(c + SkScalarCos(rad) * r, c + SkScalarSin(rad) * r); |
| 181 |
} |
| 182 |
} |
| 183 |
private: |
| 184 |
using INHERITED = PolyUtilsBench; |
| 185 |
}; |
| 186 |
|
| 187 |
// familiar videogame character |
| 188 |
class NotchPolyUtilsBench : public PolyUtilsBench { |
| 189 |
public: |
| 190 |
NotchPolyUtilsBench(PolyUtilsBench::Type type) : INHERITED(type) {} |
| 191 |
|
| 192 |
void appendName(SkString* name) override { |
| 193 |
name->append("notch"); |
| 194 |
} |
| 195 |
void makePoly(SkTDArray<SkPoint>* poly) override { |
| 196 |
// create 3/4 circle with many vertices |
| 197 |
const SkScalar c = SkIntToScalar(45); |
| 198 |
const SkScalar r = SkIntToScalar(20); |
| 199 |
const int n = 1000; |
| 200 |
SkScalar rad = 0; |
| 201 |
const SkScalar drad = 3 * SK_ScalarPI / (2*n); |
| 202 |
for (int i = 0; i < n; i++) { |
| 203 |
*poly->append() = SkPoint::Make(c + SkScalarCos(rad) * r, c + SkScalarSin(rad) * r); |
| 204 |
rad += drad; |
| 205 |
} |
| 206 |
// and the mouth |
| 207 |
*poly->append() = SkPoint::Make(45, 45); |
| 208 |
} |
| 209 |
private: |
| 210 |
using INHERITED = PolyUtilsBench; |
| 211 |
}; |
| 212 |
|
| 213 |
class IceCreamPolyUtilsBench : public PolyUtilsBench { |
| 214 |
public: |
| 215 |
IceCreamPolyUtilsBench(PolyUtilsBench::Type type) : INHERITED(type) {} |
| 216 |
|
| 217 |
void appendName(SkString* name) override { |
| 218 |
name->append("icecream"); |
| 219 |
} |
| 220 |
void makePoly(SkTDArray<SkPoint>* poly) override { |
| 221 |
// create 3/4 circle with many vertices |
| 222 |
const SkScalar c = SkIntToScalar(45); |
| 223 |
const SkScalar r = SkIntToScalar(20); |
| 224 |
const int n = 1000; |
| 225 |
SkScalar rad = 0; |
| 226 |
const SkScalar drad = 3 * SK_ScalarPI / (2*n); |
| 227 |
for (int i = 0; i < n; i++) { |
| 228 |
*poly->append() = SkPoint::Make(c + SkScalarCos(rad) * r, c + SkScalarSin(rad) * r); |
| 229 |
rad += drad; |
| 230 |
} |
| 231 |
// and the tip of the cone |
| 232 |
*poly->append() = SkPoint::Make(90, 0); |
| 233 |
} |
| 234 |
private: |
| 235 |
using INHERITED = PolyUtilsBench; |
| 236 |
}; |
| 237 |
|
| 238 |
DEF_BENCH(return new StarPolyUtilsBench(PolyUtilsBench::Type::kConvexCheck);) |
| 239 |
DEF_BENCH(return new StarPolyUtilsBench(PolyUtilsBench::Type::kSimpleCheck);) |
| 240 |
DEF_BENCH(return new StarPolyUtilsBench(PolyUtilsBench::Type::kInsetConvex);) |
| 241 |
DEF_BENCH(return new StarPolyUtilsBench(PolyUtilsBench::Type::kOffsetSimple);) |
| 242 |
DEF_BENCH(return new StarPolyUtilsBench(PolyUtilsBench::Type::kTessellateSimple);) |
| 243 |
DEF_BENCH(return new CirclePolyUtilsBench(PolyUtilsBench::Type::kConvexCheck);) |
| 244 |
DEF_BENCH(return new CirclePolyUtilsBench(PolyUtilsBench::Type::kSimpleCheck);) |
| 245 |
DEF_BENCH(return new CirclePolyUtilsBench(PolyUtilsBench::Type::kInsetConvex);) |
| 246 |
DEF_BENCH(return new CirclePolyUtilsBench(PolyUtilsBench::Type::kOffsetSimple);) |
| 247 |
DEF_BENCH(return new CirclePolyUtilsBench(PolyUtilsBench::Type::kTessellateSimple);) |
| 248 |
DEF_BENCH(return new IntersectingPolyUtilsBench(PolyUtilsBench::Type::kConvexCheck);) |
| 249 |
DEF_BENCH(return new IntersectingPolyUtilsBench(PolyUtilsBench::Type::kSimpleCheck);) |
| 250 |
DEF_BENCH(return new IntersectingPolyUtilsBench(PolyUtilsBench::Type::kInsetConvex);) |
| 251 |
DEF_BENCH(return new IntersectingPolyUtilsBench(PolyUtilsBench::Type::kOffsetSimple);) |
| 252 |
DEF_BENCH(return new IntersectingPolyUtilsBench(PolyUtilsBench::Type::kTessellateSimple);) |
| 253 |
DEF_BENCH(return new NotchPolyUtilsBench(PolyUtilsBench::Type::kConvexCheck);) |
| 254 |
DEF_BENCH(return new NotchPolyUtilsBench(PolyUtilsBench::Type::kSimpleCheck);) |
| 255 |
DEF_BENCH(return new NotchPolyUtilsBench(PolyUtilsBench::Type::kInsetConvex);) |
| 256 |
DEF_BENCH(return new NotchPolyUtilsBench(PolyUtilsBench::Type::kOffsetSimple);) |
| 257 |
DEF_BENCH(return new NotchPolyUtilsBench(PolyUtilsBench::Type::kTessellateSimple);) |
| 258 |
DEF_BENCH(return new IceCreamPolyUtilsBench(PolyUtilsBench::Type::kConvexCheck);) |
| 259 |
DEF_BENCH(return new IceCreamPolyUtilsBench(PolyUtilsBench::Type::kSimpleCheck);) |
| 260 |
DEF_BENCH(return new IceCreamPolyUtilsBench(PolyUtilsBench::Type::kInsetConvex);) |
| 261 |
DEF_BENCH(return new IceCreamPolyUtilsBench(PolyUtilsBench::Type::kOffsetSimple);) |
| 262 |
DEF_BENCH(return new IceCreamPolyUtilsBench(PolyUtilsBench::Type::kTessellateSimple);) |
| 263 |
|
| 264 |
#endif // !defined(SK_ENABLE_OPTIMIZE_SIZE) |