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