blob: 63199d009b31323251804b312a1ae930b47d0e65 [file] [log] [blame]
reed@google.comc1f90112013-02-04 14:57:28 +00001/*
reedff2f4232015-05-29 07:17:16 -07002 * Copyright 2015 Google Inc.
reed@google.comc1f90112013-02-04 14:57:28 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
reed@google.comc1f90112013-02-04 14:57:28 +00009#include "SkPaint.h"
reedff2f4232015-05-29 07:17:16 -070010#include "SkPath.h"
11#include "SkRandom.h"
reed@google.comc1f90112013-02-04 14:57:28 +000012#include "SkString.h"
13
reedff2f4232015-05-29 07:17:16 -070014class StrokeBench : public Benchmark {
reed@google.comc1f90112013-02-04 14:57:28 +000015public:
caryclarkc58e5322015-06-01 06:30:06 -070016 StrokeBench(const SkPath& path, const SkPaint& paint, const char pathType[], SkScalar res)
17 : fPath(path), fPaint(paint), fRes(res)
reedff2f4232015-05-29 07:17:16 -070018 {
19 fName.printf("build_stroke_%s_%g_%d_%d",
20 pathType, paint.getStrokeWidth(), paint.getStrokeJoin(), paint.getStrokeCap());
reed@google.comc1f90112013-02-04 14:57:28 +000021 }
22
23protected:
mtkleinf0599002015-07-13 06:18:39 -070024 bool isSuitableFor(Backend backend) override {
caryclarkc58e5322015-06-01 06:30:06 -070025 return backend == kNonRendering_Backend;
26 }
27
reedff2f4232015-05-29 07:17:16 -070028 const char* onGetName() override { return fName.c_str(); }
reed@google.comc1f90112013-02-04 14:57:28 +000029
mtkleina1ebeb22015-10-01 09:43:39 -070030 void onDraw(int loops, SkCanvas* canvas) override {
reedff2f4232015-05-29 07:17:16 -070031 SkPaint paint(fPaint);
32 this->setupPaint(&paint);
33
34 for (int outer = 0; outer < 10; ++outer) {
35 for (int i = 0; i < loops; ++i) {
36 SkPath result;
halcanary96fcdcc2015-08-27 07:41:13 -070037 paint.getFillPath(fPath, &result, nullptr, fRes);
reedff2f4232015-05-29 07:17:16 -070038 }
39 }
reed@google.comc1f90112013-02-04 14:57:28 +000040 }
41
42private:
reedff2f4232015-05-29 07:17:16 -070043 SkPath fPath;
44 SkPaint fPaint;
45 SkString fName;
caryclarkc58e5322015-06-01 06:30:06 -070046 SkScalar fRes;
tfarinaf168b862014-06-19 12:32:29 -070047 typedef Benchmark INHERITED;
reed@google.comc1f90112013-02-04 14:57:28 +000048};
49
reedff2f4232015-05-29 07:17:16 -070050///////////////////////////////////////////////////////////////////////////////
reed@google.comc1f90112013-02-04 14:57:28 +000051
reedff2f4232015-05-29 07:17:16 -070052static const int N = 100;
53static const SkScalar X = 100;
54static const SkScalar Y = 100;
reed@google.comc1f90112013-02-04 14:57:28 +000055
reedff2f4232015-05-29 07:17:16 -070056static SkPoint rand_pt(SkRandom& rand) {
57 return SkPoint::Make(rand.nextSScalar1() * X, rand.nextSScalar1() * Y);
58}
59
60static SkPath line_path_maker() {
61 SkPath path;
62 SkRandom rand;
63 path.moveTo(rand_pt(rand));
64 for (int i = 0; i < N; ++i) {
65 path.lineTo(rand_pt(rand));
66 }
67 return path;
68}
69static SkPath quad_path_maker() {
70 SkPath path;
71 SkRandom rand;
72 path.moveTo(rand_pt(rand));
73 for (int i = 0; i < N; ++i) {
74 path.quadTo(rand_pt(rand), rand_pt(rand));
75 }
76 return path;
77}
78static SkPath conic_path_maker() {
79 SkPath path;
80 SkRandom rand;
81 path.moveTo(rand_pt(rand));
82 for (int i = 0; i < N; ++i) {
83 path.conicTo(rand_pt(rand), rand_pt(rand), rand.nextUScalar1());
84 }
85 return path;
86}
87static SkPath cubic_path_maker() {
88 SkPath path;
89 SkRandom rand;
90 path.moveTo(rand_pt(rand));
91 for (int i = 0; i < N; ++i) {
92 path.cubicTo(rand_pt(rand), rand_pt(rand), rand_pt(rand));
93 }
94 return path;
95}
96
97static SkPaint paint_maker() {
98 SkPaint paint;
99 paint.setStyle(SkPaint::kStroke_Style);
100 paint.setStrokeWidth(X / 10);
101 paint.setStrokeJoin(SkPaint::kMiter_Join);
102 paint.setStrokeCap(SkPaint::kSquare_Cap);
103 return paint;
104}
105
halcanary385fe4d2015-08-26 13:07:48 -0700106DEF_BENCH(return new StrokeBench(line_path_maker(), paint_maker(), "line_1", 1);)
107DEF_BENCH(return new StrokeBench(quad_path_maker(), paint_maker(), "quad_1", 1);)
108DEF_BENCH(return new StrokeBench(conic_path_maker(), paint_maker(), "conic_1", 1);)
109DEF_BENCH(return new StrokeBench(cubic_path_maker(), paint_maker(), "cubic_1", 1);)
caryclarkc58e5322015-06-01 06:30:06 -0700110
halcanary385fe4d2015-08-26 13:07:48 -0700111DEF_BENCH(return new StrokeBench(line_path_maker(), paint_maker(), "line_4", 4);)
112DEF_BENCH(return new StrokeBench(quad_path_maker(), paint_maker(), "quad_4", 4);)
113DEF_BENCH(return new StrokeBench(conic_path_maker(), paint_maker(), "conic_4", 4);)
114DEF_BENCH(return new StrokeBench(cubic_path_maker(), paint_maker(), "cubic_4", 4);)
caryclarkc58e5322015-06-01 06:30:06 -0700115
halcanary385fe4d2015-08-26 13:07:48 -0700116DEF_BENCH(return new StrokeBench(line_path_maker(), paint_maker(), "line_.25", .25f);)
117DEF_BENCH(return new StrokeBench(quad_path_maker(), paint_maker(), "quad_.25", .25f);)
118DEF_BENCH(return new StrokeBench(conic_path_maker(), paint_maker(), "conic_.25", .25f);)
119DEF_BENCH(return new StrokeBench(cubic_path_maker(), paint_maker(), "cubic_.25", .25f);)