| 1 |
/* |
| 2 |
* Copyright 2015 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/SkPaint.h" |
| 10 |
#include "include/core/SkPath.h" |
| 11 |
#include "include/core/SkPathBuilder.h" |
| 12 |
#include "include/core/SkPathUtils.h" |
| 13 |
#include "include/core/SkString.h" |
| 14 |
#include "src/base/SkRandom.h" |
| 15 |
|
| 16 |
class StrokeBench : public Benchmark { |
| 17 |
public: |
| 18 |
StrokeBench(const SkPath& path, const SkPaint& paint, const char pathType[], SkScalar res) |
| 19 |
: fPath(path), fPaint(paint), fRes(res) |
| 20 |
{ |
| 21 |
fName.printf("build_stroke_%s_%g_%d_%d", |
| 22 |
pathType, paint.getStrokeWidth(), paint.getStrokeJoin(), paint.getStrokeCap()); |
| 23 |
} |
| 24 |
|
| 25 |
protected: |
| 26 |
bool isSuitableFor(Backend backend) override { |
| 27 |
return backend == Backend::kNonRendering; |
| 28 |
} |
| 29 |
|
| 30 |
const char* onGetName() override { return fName.c_str(); } |
| 31 |
|
| 32 |
void onDraw(int loops, SkCanvas* canvas) override { |
| 33 |
SkPaint paint(fPaint); |
| 34 |
this->setupPaint(&paint); |
| 35 |
|
| 36 |
const SkMatrix mx = SkMatrix::Scale(fRes, fRes); |
| 37 |
for (int outer = 0; outer < 10; ++outer) { |
| 38 |
for (int i = 0; i < loops; ++i) { |
| 39 |
SkPathBuilder result; |
| 40 |
skpathutils::FillPathWithPaint(fPath, paint, &result, nullptr, mx); |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
private: |
| 46 |
SkPath fPath; |
| 47 |
SkPaint fPaint; |
| 48 |
SkString fName; |
| 49 |
SkScalar fRes; |
| 50 |
using INHERITED = Benchmark; |
| 51 |
}; |
| 52 |
|
| 53 |
/////////////////////////////////////////////////////////////////////////////// |
| 54 |
|
| 55 |
static const int N = 100; |
| 56 |
static const SkScalar X = 100; |
| 57 |
static const SkScalar Y = 100; |
| 58 |
|
| 59 |
static SkPoint rand_pt(SkRandom& rand) { |
| 60 |
return SkPoint::Make(rand.nextSScalar1() * X, rand.nextSScalar1() * Y); |
| 61 |
} |
| 62 |
|
| 63 |
static SkPath line_path_maker() { |
| 64 |
SkPathBuilder builder; |
| 65 |
SkRandom rand; |
| 66 |
builder.moveTo(rand_pt(rand)); |
| 67 |
for (int i = 0; i < N; ++i) { |
| 68 |
builder.lineTo(rand_pt(rand)); |
| 69 |
} |
| 70 |
return builder.detach(); |
| 71 |
} |
| 72 |
static SkPath quad_path_maker() { |
| 73 |
SkPathBuilder builder; |
| 74 |
SkRandom rand; |
| 75 |
builder.moveTo(rand_pt(rand)); |
| 76 |
for (int i = 0; i < N; ++i) { |
| 77 |
builder.quadTo(rand_pt(rand), rand_pt(rand)); |
| 78 |
} |
| 79 |
return builder.detach(); |
| 80 |
} |
| 81 |
static SkPath conic_path_maker() { |
| 82 |
SkPathBuilder builder; |
| 83 |
SkRandom rand; |
| 84 |
builder.moveTo(rand_pt(rand)); |
| 85 |
for (int i = 0; i < N; ++i) { |
| 86 |
builder.conicTo(rand_pt(rand), rand_pt(rand), rand.nextUScalar1()); |
| 87 |
} |
| 88 |
return builder.detach(); |
| 89 |
} |
| 90 |
static SkPath cubic_path_maker() { |
| 91 |
SkPathBuilder builder; |
| 92 |
SkRandom rand; |
| 93 |
builder.moveTo(rand_pt(rand)); |
| 94 |
for (int i = 0; i < N; ++i) { |
| 95 |
builder.cubicTo(rand_pt(rand), rand_pt(rand), rand_pt(rand)); |
| 96 |
} |
| 97 |
return builder.detach(); |
| 98 |
} |
| 99 |
|
| 100 |
static SkPaint paint_maker() { |
| 101 |
SkPaint paint; |
| 102 |
paint.setStyle(SkPaint::kStroke_Style); |
| 103 |
paint.setStrokeWidth(X / 10); |
| 104 |
paint.setStrokeJoin(SkPaint::kMiter_Join); |
| 105 |
paint.setStrokeCap(SkPaint::kSquare_Cap); |
| 106 |
return paint; |
| 107 |
} |
| 108 |
|
| 109 |
DEF_BENCH(return new StrokeBench(line_path_maker(), paint_maker(), "line_1", 1);) |
| 110 |
DEF_BENCH(return new StrokeBench(quad_path_maker(), paint_maker(), "quad_1", 1);) |
| 111 |
DEF_BENCH(return new StrokeBench(conic_path_maker(), paint_maker(), "conic_1", 1);) |
| 112 |
DEF_BENCH(return new StrokeBench(cubic_path_maker(), paint_maker(), "cubic_1", 1);) |
| 113 |
|
| 114 |
DEF_BENCH(return new StrokeBench(line_path_maker(), paint_maker(), "line_4", 4);) |
| 115 |
DEF_BENCH(return new StrokeBench(quad_path_maker(), paint_maker(), "quad_4", 4);) |
| 116 |
DEF_BENCH(return new StrokeBench(conic_path_maker(), paint_maker(), "conic_4", 4);) |
| 117 |
DEF_BENCH(return new StrokeBench(cubic_path_maker(), paint_maker(), "cubic_4", 4);) |
| 118 |
|
| 119 |
DEF_BENCH(return new StrokeBench(line_path_maker(), paint_maker(), "line_.25", .25f);) |
| 120 |
DEF_BENCH(return new StrokeBench(quad_path_maker(), paint_maker(), "quad_.25", .25f);) |
| 121 |
DEF_BENCH(return new StrokeBench(conic_path_maker(), paint_maker(), "conic_.25", .25f);) |
| 122 |
DEF_BENCH(return new StrokeBench(cubic_path_maker(), paint_maker(), "cubic_.25", .25f);) |