| 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/SkPathBuilder.h" |
| 13 |
#include "include/core/SkPathEffect.h" |
| 14 |
#include "include/core/SkString.h" |
| 15 |
#include "include/core/SkStrokeRec.h" |
| 16 |
#include "include/effects/SkDashPathEffect.h" |
| 17 |
#include "include/private/base/SkTDArray.h" |
| 18 |
#include "src/base/SkRandom.h" |
| 19 |
|
| 20 |
/* |
| 21 |
* Cases to consider: |
| 22 |
* |
| 23 |
* 1. antialiasing on/off (esp. width <= 1) |
| 24 |
* 2. strokewidth == 0, 1, 2 |
| 25 |
* 3. hline, vline, diagonal, rect, oval |
| 26 |
* 4. dots [1,1] ([N,N] where N=strokeWidth?) or arbitrary (e.g. [2,1] or [1,2,3,2]) |
| 27 |
*/ |
| 28 |
static SkPath path_hline() { |
| 29 |
return SkPath::Line({10, 10}, {600, 10}); |
| 30 |
} |
| 31 |
|
| 32 |
class DashBench : public Benchmark { |
| 33 |
protected: |
| 34 |
SkString fName; |
| 35 |
SkTDArray<SkScalar> fIntervals; |
| 36 |
int fWidth; |
| 37 |
SkPoint fPts[2]; |
| 38 |
bool fDoClip; |
| 39 |
|
| 40 |
public: |
| 41 |
DashBench(const SkScalar intervals[], int count, int width, |
| 42 |
bool doClip = false) { |
| 43 |
fIntervals.append(count, intervals); |
| 44 |
for (int i = 0; i < count; ++i) { |
| 45 |
fIntervals[i] *= width; |
| 46 |
} |
| 47 |
fWidth = width; |
| 48 |
fName.printf("dash_%d_%s", width, doClip ? "clipped" : "noclip"); |
| 49 |
fDoClip = doClip; |
| 50 |
|
| 51 |
fPts[0].set(SkIntToScalar(10), SkIntToScalar(10)); |
| 52 |
fPts[1].set(SkIntToScalar(600), SkIntToScalar(10)); |
| 53 |
} |
| 54 |
|
| 55 |
virtual SkPath makePath() { |
| 56 |
return path_hline(); |
| 57 |
} |
| 58 |
|
| 59 |
protected: |
| 60 |
const char* onGetName() override { |
| 61 |
return fName.c_str(); |
| 62 |
} |
| 63 |
|
| 64 |
void onDraw(int loops, SkCanvas* canvas) override { |
| 65 |
SkPaint paint; |
| 66 |
this->setupPaint(&paint); |
| 67 |
paint.setStyle(SkPaint::kStroke_Style); |
| 68 |
paint.setStrokeWidth(SkIntToScalar(fWidth)); |
| 69 |
paint.setAntiAlias(false); |
| 70 |
|
| 71 |
SkPath path = this->makePath(); |
| 72 |
|
| 73 |
paint.setPathEffect(SkDashPathEffect::Make(fIntervals, 0)); |
| 74 |
|
| 75 |
if (fDoClip) { |
| 76 |
SkRect r = path.getBounds(); |
| 77 |
r.inset(-SkIntToScalar(20), -SkIntToScalar(20)); |
| 78 |
// now move it so we don't intersect |
| 79 |
r.offset(0, r.height() * 3 / 2); |
| 80 |
canvas->clipRect(r); |
| 81 |
} |
| 82 |
|
| 83 |
this->handlePath(canvas, path, paint, loops); |
| 84 |
} |
| 85 |
|
| 86 |
virtual void handlePath(SkCanvas* canvas, const SkPath& path, |
| 87 |
const SkPaint& paint, int N) { |
| 88 |
for (int i = 0; i < N; ++i) { |
| 89 |
// canvas->drawPoints(SkCanvas::kLines_PointMode, 2, fPts, paint); |
| 90 |
canvas->drawPath(path, paint); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
private: |
| 95 |
using INHERITED = Benchmark; |
| 96 |
}; |
| 97 |
|
| 98 |
class RectDashBench : public DashBench { |
| 99 |
public: |
| 100 |
RectDashBench(const SkScalar intervals[], int count, int width) |
| 101 |
: INHERITED(intervals, count, width) { |
| 102 |
fName.append("_rect"); |
| 103 |
} |
| 104 |
|
| 105 |
protected: |
| 106 |
void handlePath(SkCanvas* canvas, const SkPath& path, const SkPaint& paint, int N) override { |
| 107 |
SkPoint pts[2]; |
| 108 |
if (!path.isLine(pts) || pts[0].fY != pts[1].fY) { |
| 109 |
this->INHERITED::handlePath(canvas, path, paint, N); |
| 110 |
} else { |
| 111 |
SkRect rect; |
| 112 |
rect.fLeft = pts[0].fX; |
| 113 |
rect.fTop = pts[0].fY - paint.getStrokeWidth() / 2; |
| 114 |
rect.fRight = rect.fLeft + SkIntToScalar(fWidth); |
| 115 |
rect.fBottom = rect.fTop + paint.getStrokeWidth(); |
| 116 |
|
| 117 |
SkPaint p(paint); |
| 118 |
p.setStyle(SkPaint::kFill_Style); |
| 119 |
p.setPathEffect(nullptr); |
| 120 |
|
| 121 |
int count = SkScalarRoundToInt((pts[1].fX - pts[0].fX) / (2*fWidth)); |
| 122 |
SkScalar dx = SkIntToScalar(2 * fWidth); |
| 123 |
|
| 124 |
for (int i = 0; i < N*10; ++i) { |
| 125 |
SkRect r = rect; |
| 126 |
for (int j = 0; j < count; ++j) { |
| 127 |
canvas->drawRect(r, p); |
| 128 |
r.offset(dx, 0); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
private: |
| 135 |
using INHERITED = DashBench; |
| 136 |
}; |
| 137 |
|
| 138 |
static SkPath make_unit_star(int n) { |
| 139 |
SkScalar rad = -SK_ScalarPI / 2; |
| 140 |
const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n; |
| 141 |
|
| 142 |
SkPathBuilder builder; |
| 143 |
builder.moveTo(0, -SK_Scalar1); |
| 144 |
for (int i = 1; i < n; i++) { |
| 145 |
rad += drad; |
| 146 |
builder.lineTo(SkScalarCos(rad), SkScalarSin(rad)); |
| 147 |
} |
| 148 |
builder.close(); |
| 149 |
return builder.detach(); |
| 150 |
} |
| 151 |
|
| 152 |
static SkPath make_poly() { |
| 153 |
return make_unit_star(9).makeTransform(SkMatrix::Scale(100, 100)); |
| 154 |
} |
| 155 |
|
| 156 |
static SkPath make_quad() { |
| 157 |
SkScalar x0 = SkIntToScalar(10); |
| 158 |
SkScalar y0 = SkIntToScalar(10); |
| 159 |
SkPathBuilder builder; |
| 160 |
builder.moveTo(x0, y0); |
| 161 |
builder.quadTo(x0, y0 + 400 * SK_Scalar1, |
| 162 |
x0 + 600 * SK_Scalar1, y0 + 400 * SK_Scalar1); |
| 163 |
return builder.detach(); |
| 164 |
} |
| 165 |
|
| 166 |
static SkPath make_cubic() { |
| 167 |
SkScalar x0 = SkIntToScalar(10); |
| 168 |
SkScalar y0 = SkIntToScalar(10); |
| 169 |
SkPathBuilder builder; |
| 170 |
builder.moveTo(x0, y0); |
| 171 |
builder.cubicTo(x0, y0 + 400 * SK_Scalar1, |
| 172 |
x0 + 600 * SK_Scalar1, y0 + 400 * SK_Scalar1, |
| 173 |
x0 + 600 * SK_Scalar1, y0); |
| 174 |
return builder.detach(); |
| 175 |
} |
| 176 |
|
| 177 |
class MakeDashBench : public Benchmark { |
| 178 |
SkString fName; |
| 179 |
SkPath fPath; |
| 180 |
sk_sp<SkPathEffect> fPE; |
| 181 |
|
| 182 |
public: |
| 183 |
MakeDashBench(SkPath(*proc)(), const char name[]) { |
| 184 |
fName.printf("makedash_%s", name); |
| 185 |
fPath = proc(); |
| 186 |
|
| 187 |
SkScalar vals[] = { SkIntToScalar(4), SkIntToScalar(4) }; |
| 188 |
fPE = SkDashPathEffect::Make(vals, 0); |
| 189 |
} |
| 190 |
|
| 191 |
protected: |
| 192 |
const char* onGetName() override { |
| 193 |
return fName.c_str(); |
| 194 |
} |
| 195 |
|
| 196 |
void onDraw(int loops, SkCanvas*) override { |
| 197 |
SkPathBuilder dst; |
| 198 |
for (int i = 0; i < loops; ++i) { |
| 199 |
SkStrokeRec rec(SkStrokeRec::kHairline_InitStyle); |
| 200 |
|
| 201 |
fPE->filterPath(&dst, fPath, &rec); |
| 202 |
dst.reset(); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
private: |
| 207 |
using INHERITED = Benchmark; |
| 208 |
}; |
| 209 |
|
| 210 |
/* |
| 211 |
* We try to special case square dashes (intervals are equal to strokewidth). |
| 212 |
*/ |
| 213 |
class DashLineBench : public Benchmark { |
| 214 |
SkString fName; |
| 215 |
SkScalar fStrokeWidth; |
| 216 |
bool fIsRound; |
| 217 |
sk_sp<SkPathEffect> fPE; |
| 218 |
|
| 219 |
public: |
| 220 |
DashLineBench(SkScalar width, bool isRound) { |
| 221 |
fName.printf("dashline_%g_%s", width, isRound ? "circle" : "square"); |
| 222 |
fStrokeWidth = width; |
| 223 |
fIsRound = isRound; |
| 224 |
|
| 225 |
SkScalar vals[] = { SK_Scalar1, SK_Scalar1 }; |
| 226 |
fPE = SkDashPathEffect::Make(vals, 0); |
| 227 |
} |
| 228 |
|
| 229 |
protected: |
| 230 |
const char* onGetName() override { |
| 231 |
return fName.c_str(); |
| 232 |
} |
| 233 |
|
| 234 |
void onDraw(int loops, SkCanvas* canvas) override { |
| 235 |
SkPaint paint; |
| 236 |
this->setupPaint(&paint); |
| 237 |
paint.setStrokeWidth(fStrokeWidth); |
| 238 |
paint.setStrokeCap(fIsRound ? SkPaint::kRound_Cap : SkPaint::kSquare_Cap); |
| 239 |
paint.setPathEffect(fPE); |
| 240 |
for (int i = 0; i < loops; ++i) { |
| 241 |
canvas->drawLine(10 * SK_Scalar1, 10 * SK_Scalar1, |
| 242 |
640 * SK_Scalar1, 10 * SK_Scalar1, paint); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
private: |
| 247 |
using INHERITED = Benchmark; |
| 248 |
}; |
| 249 |
|
| 250 |
class DrawPointsDashingBench : public Benchmark { |
| 251 |
SkString fName; |
| 252 |
int fStrokeWidth; |
| 253 |
bool fDoAA; |
| 254 |
|
| 255 |
sk_sp<SkPathEffect> fPathEffect; |
| 256 |
|
| 257 |
public: |
| 258 |
DrawPointsDashingBench(int dashLength, int strokeWidth, bool doAA) |
| 259 |
{ |
| 260 |
fName.printf("drawpointsdash_%d_%d%s", dashLength, strokeWidth, doAA ? "_aa" : "_bw"); |
| 261 |
fStrokeWidth = strokeWidth; |
| 262 |
fDoAA = doAA; |
| 263 |
|
| 264 |
SkScalar vals[] = { SkIntToScalar(dashLength), SkIntToScalar(dashLength) }; |
| 265 |
fPathEffect = SkDashPathEffect::Make(vals, SK_Scalar1); |
| 266 |
} |
| 267 |
|
| 268 |
protected: |
| 269 |
const char* onGetName() override { |
| 270 |
return fName.c_str(); |
| 271 |
} |
| 272 |
|
| 273 |
void onDraw(int loops, SkCanvas* canvas) override { |
| 274 |
SkPaint p; |
| 275 |
this->setupPaint(&p); |
| 276 |
p.setColor(SK_ColorBLACK); |
| 277 |
p.setStyle(SkPaint::kStroke_Style); |
| 278 |
p.setStrokeWidth(SkIntToScalar(fStrokeWidth)); |
| 279 |
p.setPathEffect(fPathEffect); |
| 280 |
p.setAntiAlias(fDoAA); |
| 281 |
|
| 282 |
SkPoint pts[2] = { |
| 283 |
{ SkIntToScalar(10), 0 }, |
| 284 |
{ SkIntToScalar(640), 0 } |
| 285 |
}; |
| 286 |
|
| 287 |
for (int i = 0; i < loops; ++i) { |
| 288 |
pts[0].fY = pts[1].fY = SkIntToScalar(i % 480); |
| 289 |
canvas->drawPoints(SkCanvas::kLines_PointMode, pts, p); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
private: |
| 294 |
using INHERITED = Benchmark; |
| 295 |
}; |
| 296 |
|
| 297 |
// Want to test how we handle dashing when 99% of the dash is clipped out |
| 298 |
class GiantDashBench : public Benchmark { |
| 299 |
SkString fName; |
| 300 |
SkScalar fStrokeWidth; |
| 301 |
SkPoint fPts[2]; |
| 302 |
sk_sp<SkPathEffect> fPathEffect; |
| 303 |
|
| 304 |
public: |
| 305 |
enum LineType { |
| 306 |
kHori_LineType, |
| 307 |
kVert_LineType, |
| 308 |
kDiag_LineType, |
| 309 |
kLineTypeCount |
| 310 |
}; |
| 311 |
|
| 312 |
static const char* LineTypeName(LineType lt) { |
| 313 |
static const char* gNames[] = { "hori", "vert", "diag" }; |
| 314 |
static_assert(kLineTypeCount == std::size(gNames), "names_wrong_size"); |
| 315 |
return gNames[lt]; |
| 316 |
} |
| 317 |
|
| 318 |
GiantDashBench(LineType lt, SkScalar width) { |
| 319 |
fName.printf("giantdashline_%s_%g", LineTypeName(lt), width); |
| 320 |
fStrokeWidth = width; |
| 321 |
|
| 322 |
// deliberately pick intervals that won't be caught by asPoints(), so |
| 323 |
// we can test the filterPath code-path. |
| 324 |
const SkScalar intervals[] = { 20, 10, 10, 10 }; |
| 325 |
fPathEffect = SkDashPathEffect::Make(intervals, 0); |
| 326 |
|
| 327 |
SkScalar cx = 640 / 2; // center X |
| 328 |
SkScalar cy = 480 / 2; // center Y |
| 329 |
SkMatrix matrix; |
| 330 |
|
| 331 |
switch (lt) { |
| 332 |
case kHori_LineType: |
| 333 |
matrix.setIdentity(); |
| 334 |
break; |
| 335 |
case kVert_LineType: |
| 336 |
matrix.setRotate(90, cx, cy); |
| 337 |
break; |
| 338 |
case kDiag_LineType: |
| 339 |
matrix.setRotate(45, cx, cy); |
| 340 |
break; |
| 341 |
case kLineTypeCount: |
| 342 |
// Not a real enum value. |
| 343 |
break; |
| 344 |
} |
| 345 |
|
| 346 |
const SkScalar overshoot = 100*1000; |
| 347 |
const SkPoint pts[2] = { |
| 348 |
{ -overshoot, cy }, { 640 + overshoot, cy } |
| 349 |
}; |
| 350 |
matrix.mapPoints(fPts, pts); |
| 351 |
} |
| 352 |
|
| 353 |
protected: |
| 354 |
const char* onGetName() override { |
| 355 |
return fName.c_str(); |
| 356 |
} |
| 357 |
|
| 358 |
void onDraw(int loops, SkCanvas* canvas) override { |
| 359 |
SkPaint p; |
| 360 |
this->setupPaint(&p); |
| 361 |
p.setStyle(SkPaint::kStroke_Style); |
| 362 |
p.setStrokeWidth(fStrokeWidth); |
| 363 |
p.setPathEffect(fPathEffect); |
| 364 |
|
| 365 |
for (int i = 0; i < loops; i++) { |
| 366 |
canvas->drawPoints(SkCanvas::kLines_PointMode, fPts, p); |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
private: |
| 371 |
using INHERITED = Benchmark; |
| 372 |
}; |
| 373 |
|
| 374 |
// Want to test how we draw a dashed grid (like what is used in spreadsheets) of many |
| 375 |
// small dashed lines switching back and forth between horizontal and vertical |
| 376 |
class DashGridBench : public Benchmark { |
| 377 |
SkString fName; |
| 378 |
int fStrokeWidth; |
| 379 |
bool fDoAA; |
| 380 |
|
| 381 |
sk_sp<SkPathEffect> fPathEffect; |
| 382 |
|
| 383 |
public: |
| 384 |
DashGridBench(int dashLength, int strokeWidth, bool doAA) { |
| 385 |
fName.printf("dashgrid_%d_%d%s", dashLength, strokeWidth, doAA ? "_aa" : "_bw"); |
| 386 |
fStrokeWidth = strokeWidth; |
| 387 |
fDoAA = doAA; |
| 388 |
|
| 389 |
SkScalar vals[] = { SkIntToScalar(dashLength), SkIntToScalar(dashLength) }; |
| 390 |
fPathEffect = SkDashPathEffect::Make(vals, SK_Scalar1); |
| 391 |
} |
| 392 |
|
| 393 |
protected: |
| 394 |
const char* onGetName() override { |
| 395 |
return fName.c_str(); |
| 396 |
} |
| 397 |
|
| 398 |
void onDraw(int loops, SkCanvas* canvas) override { |
| 399 |
SkPaint p; |
| 400 |
this->setupPaint(&p); |
| 401 |
p.setColor(SK_ColorBLACK); |
| 402 |
p.setStyle(SkPaint::kStroke_Style); |
| 403 |
p.setStrokeWidth(SkIntToScalar(fStrokeWidth)); |
| 404 |
p.setPathEffect(fPathEffect); |
| 405 |
p.setAntiAlias(fDoAA); |
| 406 |
|
| 407 |
SkPoint pts[4] = { |
| 408 |
{ SkIntToScalar(0), 20.5f }, |
| 409 |
{ SkIntToScalar(20), 20.5f }, |
| 410 |
{ 20.5f, SkIntToScalar(0) }, |
| 411 |
{ 20.5f, SkIntToScalar(20) } |
| 412 |
}; |
| 413 |
|
| 414 |
for (int i = 0; i < loops; ++i) { |
| 415 |
for (int j = 0; j < 10; ++j) { |
| 416 |
for (int k = 0; k < 10; ++k) { |
| 417 |
// Horizontal line |
| 418 |
SkPoint horPts[2]; |
| 419 |
horPts[0].fX = pts[0].fX + k * 22.f; |
| 420 |
horPts[0].fY = pts[0].fY + j * 22.f; |
| 421 |
horPts[1].fX = pts[1].fX + k * 22.f; |
| 422 |
horPts[1].fY = pts[1].fY + j * 22.f; |
| 423 |
canvas->drawPoints(SkCanvas::kLines_PointMode, horPts, p); |
| 424 |
|
| 425 |
// Vertical line |
| 426 |
SkPoint vertPts[2]; |
| 427 |
vertPts[0].fX = pts[2].fX + k * 22.f; |
| 428 |
vertPts[0].fY = pts[2].fY + j * 22.f; |
| 429 |
vertPts[1].fX = pts[3].fX + k * 22.f; |
| 430 |
vertPts[1].fY = pts[3].fY + j * 22.f; |
| 431 |
canvas->drawPoints(SkCanvas::kLines_PointMode, vertPts, p); |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
private: |
| 438 |
using INHERITED = Benchmark; |
| 439 |
}; |
| 440 |
|
| 441 |
/////////////////////////////////////////////////////////////////////////////// |
| 442 |
|
| 443 |
static const SkScalar gDots[] = { SK_Scalar1, SK_Scalar1 }; |
| 444 |
|
| 445 |
#define PARAM(array) array, std::size(array) |
| 446 |
|
| 447 |
DEF_BENCH( return new DashBench(PARAM(gDots), 0); ) |
| 448 |
DEF_BENCH( return new DashBench(PARAM(gDots), 1); ) |
| 449 |
DEF_BENCH( return new DashBench(PARAM(gDots), 1, true); ) |
| 450 |
DEF_BENCH( return new DashBench(PARAM(gDots), 4); ) |
| 451 |
DEF_BENCH( return new MakeDashBench(make_poly, "poly"); ) |
| 452 |
DEF_BENCH( return new MakeDashBench(make_quad, "quad"); ) |
| 453 |
DEF_BENCH( return new MakeDashBench(make_cubic, "cubic"); ) |
| 454 |
DEF_BENCH( return new DashLineBench(0, false); ) |
| 455 |
DEF_BENCH( return new DashLineBench(SK_Scalar1, false); ) |
| 456 |
DEF_BENCH( return new DashLineBench(2 * SK_Scalar1, false); ) |
| 457 |
DEF_BENCH( return new DashLineBench(0, true); ) |
| 458 |
DEF_BENCH( return new DashLineBench(SK_Scalar1, true); ) |
| 459 |
DEF_BENCH( return new DashLineBench(2 * SK_Scalar1, true); ) |
| 460 |
|
| 461 |
DEF_BENCH( return new DrawPointsDashingBench(1, 1, false); ) |
| 462 |
DEF_BENCH( return new DrawPointsDashingBench(1, 1, true); ) |
| 463 |
DEF_BENCH( return new DrawPointsDashingBench(3, 1, false); ) |
| 464 |
DEF_BENCH( return new DrawPointsDashingBench(3, 1, true); ) |
| 465 |
DEF_BENCH( return new DrawPointsDashingBench(5, 5, false); ) |
| 466 |
DEF_BENCH( return new DrawPointsDashingBench(5, 5, true); ) |
| 467 |
|
| 468 |
/* Disable the GiantDashBench for Android devices until we can better control |
| 469 |
* the memory usage. (https://code.google.com/p/skia/issues/detail?id=1430) |
| 470 |
*/ |
| 471 |
#ifndef SK_BUILD_FOR_ANDROID |
| 472 |
DEF_BENCH( return new GiantDashBench(GiantDashBench::kHori_LineType, 0); ) |
| 473 |
DEF_BENCH( return new GiantDashBench(GiantDashBench::kVert_LineType, 0); ) |
| 474 |
DEF_BENCH( return new GiantDashBench(GiantDashBench::kDiag_LineType, 0); ) |
| 475 |
|
| 476 |
// pass 2 to explicitly avoid any 1-is-the-same-as-hairline special casing |
| 477 |
|
| 478 |
// hori_2 is just too slow to enable at the moment |
| 479 |
DEF_BENCH( return new GiantDashBench(GiantDashBench::kHori_LineType, 2); ) |
| 480 |
DEF_BENCH( return new GiantDashBench(GiantDashBench::kVert_LineType, 2); ) |
| 481 |
DEF_BENCH( return new GiantDashBench(GiantDashBench::kDiag_LineType, 2); ) |
| 482 |
|
| 483 |
DEF_BENCH( return new DashGridBench(1, 1, true); ) |
| 484 |
DEF_BENCH( return new DashGridBench(1, 1, false); ) |
| 485 |
DEF_BENCH( return new DashGridBench(3, 1, true); ) |
| 486 |
DEF_BENCH( return new DashGridBench(3, 1, false); ) |
| 487 |
#endif |