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