blob: 7844035bccad211caacb29e20efe25ed510c9671 [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:
reedff2f4232015-05-29 07:17:16 -070016 StrokeBench(const SkPath& path, const SkPaint& paint, const char pathType[])
17 : fPath(path), fPaint(paint)
18 {
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:
reedff2f4232015-05-29 07:17:16 -070024 const char* onGetName() override { return fName.c_str(); }
reed@google.comc1f90112013-02-04 14:57:28 +000025
reedff2f4232015-05-29 07:17:16 -070026 void onDraw(const int loops, SkCanvas* canvas) override {
27 SkPaint paint(fPaint);
28 this->setupPaint(&paint);
29
30 for (int outer = 0; outer < 10; ++outer) {
31 for (int i = 0; i < loops; ++i) {
32 SkPath result;
33 paint.getFillPath(fPath, &result);
34 }
35 }
reed@google.comc1f90112013-02-04 14:57:28 +000036 }
37
38private:
reedff2f4232015-05-29 07:17:16 -070039 SkPath fPath;
40 SkPaint fPaint;
41 SkString fName;
42
tfarinaf168b862014-06-19 12:32:29 -070043 typedef Benchmark INHERITED;
reed@google.comc1f90112013-02-04 14:57:28 +000044};
45
reedff2f4232015-05-29 07:17:16 -070046///////////////////////////////////////////////////////////////////////////////
reed@google.comc1f90112013-02-04 14:57:28 +000047
reedff2f4232015-05-29 07:17:16 -070048static const int N = 100;
49static const SkScalar X = 100;
50static const SkScalar Y = 100;
reed@google.comc1f90112013-02-04 14:57:28 +000051
reedff2f4232015-05-29 07:17:16 -070052static SkPoint rand_pt(SkRandom& rand) {
53 return SkPoint::Make(rand.nextSScalar1() * X, rand.nextSScalar1() * Y);
54}
55
56static SkPath line_path_maker() {
57 SkPath path;
58 SkRandom rand;
59 path.moveTo(rand_pt(rand));
60 for (int i = 0; i < N; ++i) {
61 path.lineTo(rand_pt(rand));
62 }
63 return path;
64}
65static SkPath quad_path_maker() {
66 SkPath path;
67 SkRandom rand;
68 path.moveTo(rand_pt(rand));
69 for (int i = 0; i < N; ++i) {
70 path.quadTo(rand_pt(rand), rand_pt(rand));
71 }
72 return path;
73}
74static SkPath conic_path_maker() {
75 SkPath path;
76 SkRandom rand;
77 path.moveTo(rand_pt(rand));
78 for (int i = 0; i < N; ++i) {
79 path.conicTo(rand_pt(rand), rand_pt(rand), rand.nextUScalar1());
80 }
81 return path;
82}
83static SkPath cubic_path_maker() {
84 SkPath path;
85 SkRandom rand;
86 path.moveTo(rand_pt(rand));
87 for (int i = 0; i < N; ++i) {
88 path.cubicTo(rand_pt(rand), rand_pt(rand), rand_pt(rand));
89 }
90 return path;
91}
92
93static SkPaint paint_maker() {
94 SkPaint paint;
95 paint.setStyle(SkPaint::kStroke_Style);
96 paint.setStrokeWidth(X / 10);
97 paint.setStrokeJoin(SkPaint::kMiter_Join);
98 paint.setStrokeCap(SkPaint::kSquare_Cap);
99 return paint;
100}
101
102DEF_BENCH( return SkNEW_ARGS(StrokeBench, (line_path_maker(), paint_maker(), "line")); )
103DEF_BENCH( return SkNEW_ARGS(StrokeBench, (quad_path_maker(), paint_maker(), "quad")); )
104DEF_BENCH( return SkNEW_ARGS(StrokeBench, (conic_path_maker(), paint_maker(), "conic")); )
105DEF_BENCH( return SkNEW_ARGS(StrokeBench, (cubic_path_maker(), paint_maker(), "cubic")); )