OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Benchmark.h" | 8 #include "Benchmark.h" |
9 #include "SkCanvas.h" | |
10 #include "SkPaint.h" | 9 #include "SkPaint.h" |
11 #include "SkRRect.h" | 10 #include "SkPath.h" |
| 11 #include "SkRandom.h" |
12 #include "SkString.h" | 12 #include "SkString.h" |
13 | 13 |
14 struct RRectRec { | 14 class StrokeBench : public Benchmark { |
15 SkCanvas* fCanvas; | |
16 SkRRect fRRect; | |
17 SkPaint fPaint; | |
18 }; | |
19 | |
20 typedef const char* (*DrawProc)(const RRectRec*, int); | |
21 | |
22 static const char* draw_rect(const RRectRec* rec, int count) { | |
23 if (rec) { | |
24 const SkRect& r = rec->fRRect.getBounds(); | |
25 for (int i = 0; i < count; ++i) { | |
26 rec->fCanvas->drawRect(r, rec->fPaint); | |
27 } | |
28 } | |
29 return "rect"; | |
30 } | |
31 | |
32 static const char* draw_rrect(const RRectRec* rec, int count) { | |
33 if (rec) { | |
34 for (int i = 0; i < count; ++i) { | |
35 rec->fCanvas->drawRRect(rec->fRRect, rec->fPaint); | |
36 } | |
37 } | |
38 return "rrect"; | |
39 } | |
40 | |
41 static const char* draw_oval(const RRectRec* rec, int count) { | |
42 if (rec) { | |
43 const SkRect& r = rec->fRRect.getBounds(); | |
44 for (int i = 0; i < count; ++i) { | |
45 rec->fCanvas->drawOval(r, rec->fPaint); | |
46 } | |
47 } | |
48 return "oval"; | |
49 } | |
50 | |
51 // Handles rect, rrect, and oval | |
52 // | |
53 // Test drawing a small stroked version to see the effect of special-casing | |
54 // our stroke code for these convex single-contour shapes. | |
55 // | |
56 class StrokeRRectBench : public Benchmark { | |
57 SkString fName; | |
58 SkPaint::Join fJoin; | |
59 RRectRec fRec; | |
60 DrawProc fProc; | |
61 public: | 15 public: |
62 StrokeRRectBench(SkPaint::Join j, DrawProc proc) { | 16 StrokeBench(const SkPath& path, const SkPaint& paint, const char pathType[]) |
63 static const char* gJoinName[] = { | 17 : fPath(path), fPaint(paint) |
64 "miter", "round", "bevel" | 18 { |
65 }; | 19 fName.printf("build_stroke_%s_%g_%d_%d", |
66 | 20 pathType, paint.getStrokeWidth(), paint.getStrokeJoin(), pa
int.getStrokeCap()); |
67 fJoin = j; | |
68 fProc = proc; | |
69 fName.printf("draw_stroke_%s_%s", proc(NULL, 0), gJoinName[j]); | |
70 | |
71 SkRect r = { 20, 20, 40, 40 }; | |
72 SkScalar rad = 4; | |
73 fRec.fRRect.setRectXY(r, rad, rad); | |
74 } | 21 } |
75 | 22 |
76 protected: | 23 protected: |
77 virtual const char* onGetName() { | 24 const char* onGetName() override { return fName.c_str(); } |
78 return fName.c_str(); | |
79 } | |
80 | 25 |
81 virtual void onDraw(const int loops, SkCanvas* canvas) { | 26 void onDraw(const int loops, SkCanvas* canvas) override { |
82 fRec.fCanvas = canvas; | 27 SkPaint paint(fPaint); |
83 this->setupPaint(&fRec.fPaint); | 28 this->setupPaint(&paint); |
84 fRec.fPaint.setStyle(SkPaint::kStroke_Style); | 29 |
85 fRec.fPaint.setStrokeJoin(fJoin); | 30 for (int outer = 0; outer < 10; ++outer) { |
86 fRec.fPaint.setStrokeWidth(5); | 31 for (int i = 0; i < loops; ++i) { |
87 fProc(&fRec, loops); | 32 SkPath result; |
| 33 paint.getFillPath(fPath, &result); |
| 34 } |
| 35 } |
88 } | 36 } |
89 | 37 |
90 private: | 38 private: |
| 39 SkPath fPath; |
| 40 SkPaint fPaint; |
| 41 SkString fName; |
| 42 |
91 typedef Benchmark INHERITED; | 43 typedef Benchmark INHERITED; |
92 }; | 44 }; |
93 | 45 |
94 DEF_BENCH( return new StrokeRRectBench(SkPaint::kRound_Join, draw_rect); ) | 46 /////////////////////////////////////////////////////////////////////////////// |
95 DEF_BENCH( return new StrokeRRectBench(SkPaint::kBevel_Join, draw_rect); ) | |
96 DEF_BENCH( return new StrokeRRectBench(SkPaint::kMiter_Join, draw_rect); ) | |
97 | 47 |
98 DEF_BENCH( return new StrokeRRectBench(SkPaint::kRound_Join, draw_rrect); ) | 48 static const int N = 100; |
99 DEF_BENCH( return new StrokeRRectBench(SkPaint::kBevel_Join, draw_rrect); ) | 49 static const SkScalar X = 100; |
100 DEF_BENCH( return new StrokeRRectBench(SkPaint::kMiter_Join, draw_rrect); ) | 50 static const SkScalar Y = 100; |
101 | 51 |
102 DEF_BENCH( return new StrokeRRectBench(SkPaint::kRound_Join, draw_oval); ) | 52 static SkPoint rand_pt(SkRandom& rand) { |
103 DEF_BENCH( return new StrokeRRectBench(SkPaint::kBevel_Join, draw_oval); ) | 53 return SkPoint::Make(rand.nextSScalar1() * X, rand.nextSScalar1() * Y); |
104 DEF_BENCH( return new StrokeRRectBench(SkPaint::kMiter_Join, draw_oval); ) | 54 } |
| 55 |
| 56 static 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 } |
| 65 static 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 } |
| 74 static 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 } |
| 83 static 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 |
| 93 static 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 |
| 102 DEF_BENCH( return SkNEW_ARGS(StrokeBench, (line_path_maker(), paint_maker(), "li
ne")); ) |
| 103 DEF_BENCH( return SkNEW_ARGS(StrokeBench, (quad_path_maker(), paint_maker(), "qu
ad")); ) |
| 104 DEF_BENCH( return SkNEW_ARGS(StrokeBench, (conic_path_maker(), paint_maker(), "c
onic")); ) |
| 105 DEF_BENCH( return SkNEW_ARGS(StrokeBench, (cubic_path_maker(), paint_maker(), "c
ubic")); ) |
OLD | NEW |