caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 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" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 11 | #include "SkPath.h" |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 12 | #include "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 | |
| 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 | |
| 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: |
| 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 67 | fName.printf("draw_stroke_bezier_%s_%s_%s_%g", proc(nullptr, 0), gCapName[c], gJoinName[j], w); |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 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: |
| 78 | virtual const char* onGetName() { |
| 79 | return fName.c_str(); |
| 80 | } |
| 81 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 82 | virtual void onDraw(int loops, SkCanvas* canvas) { |
caryclark | feff7d2 | 2014-10-09 05:36:03 -0700 | [diff] [blame] | 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 | typedef Benchmark INHERITED; |
| 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); ) |