blob: 6c8be692ce08304d2f68bb5c318f03183a54d1ab [file] [log] [blame]
caryclarkfeff7d22014-10-09 05:36:03 -07001/*
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"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkPath.h"
caryclarkfeff7d22014-10-09 05:36:03 -070012#include "SkString.h"
13
14struct BezierRec {
15 SkCanvas* fCanvas;
16 SkPaint fPaint;
17 SkPath fQuad;
18 SkPath fCubic;
19};
20
21typedef const char* (*DrawProc)(const BezierRec*, int);
22
23static 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
35static 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
47class BezierBench : public Benchmark {
48 SkString fName;
49 SkPaint::Cap fCap;
50 SkPaint::Join fJoin;
51 BezierRec fRec;
52 DrawProc fProc;
53 SkScalar fWidth;
54public:
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);
halcanary96fcdcc2015-08-27 07:41:13 -070067 fName.printf("draw_stroke_bezier_%s_%s_%s_%g", proc(nullptr, 0), gCapName[c], gJoinName[j], w);
caryclarkfeff7d22014-10-09 05:36:03 -070068
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
77protected:
78 virtual const char* onGetName() {
79 return fName.c_str();
80 }
81
mtkleina1ebeb22015-10-01 09:43:39 -070082 virtual void onDraw(int loops, SkCanvas* canvas) {
caryclarkfeff7d22014-10-09 05:36:03 -070083 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
92private:
93 typedef Benchmark INHERITED;
94};
95
96DEF_BENCH( return new BezierBench(SkPaint::kButt_Cap, SkPaint::kRound_Join, 2, draw_quad); )
97DEF_BENCH( return new BezierBench(SkPaint::kSquare_Cap, SkPaint::kBevel_Join, 10, draw_quad); )
98DEF_BENCH( return new BezierBench(SkPaint::kRound_Cap, SkPaint::kMiter_Join, 50, draw_quad); )
99
100DEF_BENCH( return new BezierBench(SkPaint::kButt_Cap, SkPaint::kRound_Join, 2, draw_cubic); )
101DEF_BENCH( return new BezierBench(SkPaint::kSquare_Cap, SkPaint::kBevel_Join, 10, draw_cubic); )
102DEF_BENCH( return new BezierBench(SkPaint::kRound_Cap, SkPaint::kMiter_Join, 50, draw_cubic); )