1 /*
2 * Copyright 2014 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/SkCanvas.h"
10 #include "include/core/SkPaint.h"
11 #include "include/core/SkPath.h"
12 #include "include/core/SkString.h"
13
14 struct BezierRec {
15 SkCanvas* fCanvas;
16 SkPaint fPaint;
17 SkPath fQuad;
18 SkPath fCubic;
19 };
20
21 typedef const char* (*DrawProc)(const BezierRec*, int);
22
draw_quad(const BezierRec * rec,int count)23 static const char* draw_quad(const BezierRec* rec, int count) {
24 if (rec) {
25 SkCanvas* canvas = rec->fCanvas;
26 const SkPaint& paint = rec->fPaint;
27 const SkPath& path = rec->fQuad;
28 for (int i = 0; i < count; ++i) {
29 canvas->drawPath(path, paint);
30 }
31 }
32 return "quad";
33 }
34
draw_cubic(const BezierRec * rec,int count)35 static const char* draw_cubic(const BezierRec* rec, int count) {
36 if (rec) {
37 SkCanvas* canvas = rec->fCanvas;
38 const SkPaint& paint = rec->fPaint;
39 const SkPath& path = rec->fCubic;
40 for (int i = 0; i < count; ++i) {
41 canvas->drawPath(path, paint);
42 }
43 }
44 return "cubic";
45 }
46
47 class BezierBench : public Benchmark {
48 SkString fName;
49 SkPaint::Cap fCap;
50 SkPaint::Join fJoin;
51 BezierRec fRec;
52 DrawProc fProc;
53 SkScalar fWidth;
54 public:
BezierBench(SkPaint::Cap c,SkPaint::Join j,SkScalar w,DrawProc proc)55 BezierBench(SkPaint::Cap c, SkPaint::Join j, SkScalar w, DrawProc proc) {
56 static const char* gCapName[] = {
57 "butt", "round", "square"
58 };
59 static const char* gJoinName[] = {
60 "miter", "round", "bevel"
61 };
62
63 fCap = c;
64 fJoin = j;
65 fProc = proc;
66 fWidth = SkIntToScalar(w);
67 fName.printf("draw_stroke_bezier_%s_%s_%s_%g", proc(nullptr, 0), gCapName[c], gJoinName[j], w);
68
69 fRec.fQuad.moveTo(20, 20);
70 fRec.fQuad.quadTo(60, 20, 60, 60);
71 fRec.fQuad.quadTo(20, 60, 20, 100);
72 fRec.fCubic.moveTo(20, 20);
73 fRec.fCubic.cubicTo(40, 20, 60, 40, 60, 60);
74 fRec.fCubic.cubicTo(40, 60, 20, 80, 20, 100);
75 }
76
77 protected:
onGetName()78 const char* onGetName() override {
79 return fName.c_str();
80 }
81
onDraw(int loops,SkCanvas * canvas)82 void onDraw(int loops, SkCanvas* canvas) override {
83 fRec.fCanvas = canvas;
84 this->setupPaint(&fRec.fPaint);
85 fRec.fPaint.setStyle(SkPaint::kStroke_Style);
86 fRec.fPaint.setStrokeCap(fCap);
87 fRec.fPaint.setStrokeJoin(fJoin);
88 fRec.fPaint.setStrokeWidth(fWidth);
89 fProc(&fRec, loops);
90 }
91
92 private:
93 using INHERITED = Benchmark;
94 };
95
96 DEF_BENCH( return new BezierBench(SkPaint::kButt_Cap, SkPaint::kRound_Join, 2, draw_quad); )
97 DEF_BENCH( return new BezierBench(SkPaint::kSquare_Cap, SkPaint::kBevel_Join, 10, draw_quad); )
98 DEF_BENCH( return new BezierBench(SkPaint::kRound_Cap, SkPaint::kMiter_Join, 50, draw_quad); )
99
100 DEF_BENCH( return new BezierBench(SkPaint::kButt_Cap, SkPaint::kRound_Join, 2, draw_cubic); )
101 DEF_BENCH( return new BezierBench(SkPaint::kSquare_Cap, SkPaint::kBevel_Join, 10, draw_cubic); )
102 DEF_BENCH( return new BezierBench(SkPaint::kRound_Cap, SkPaint::kMiter_Join, 50, draw_cubic); )
103