reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 "SkBenchmark.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkPaint.h" |
| 11 | #include "SkRRect.h" |
| 12 | #include "SkString.h" |
| 13 | |
| 14 | struct RRectRec { |
| 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 SkBenchmark { |
| 57 | SkString fName; |
| 58 | SkPaint::Join fJoin; |
| 59 | RRectRec fRec; |
| 60 | DrawProc fProc; |
reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 61 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 62 | StrokeRRectBench(SkPaint::Join j, DrawProc proc) { |
reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 63 | static const char* gJoinName[] = { |
| 64 | "miter", "round", "bevel" |
| 65 | }; |
| 66 | |
| 67 | fJoin = j; |
| 68 | fProc = proc; |
| 69 | fName.printf("draw_stroke_%s_%s", proc(NULL, 0), gJoinName[j]); |
skia.committer@gmail.com | 0c38ed3 | 2013-02-05 07:02:01 +0000 | [diff] [blame] | 70 | |
reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 71 | SkRect r = { 20, 20, 40, 40 }; |
| 72 | SkScalar rad = 4; |
| 73 | fRec.fRRect.setRectXY(r, rad, rad); |
| 74 | } |
| 75 | |
| 76 | protected: |
| 77 | virtual const char* onGetName() { |
| 78 | return fName.c_str(); |
| 79 | } |
| 80 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 81 | virtual void onDraw(const int loops, SkCanvas* canvas) { |
reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 82 | fRec.fCanvas = canvas; |
| 83 | this->setupPaint(&fRec.fPaint); |
| 84 | fRec.fPaint.setStyle(SkPaint::kStroke_Style); |
| 85 | fRec.fPaint.setStrokeJoin(fJoin); |
| 86 | fRec.fPaint.setStrokeWidth(5); |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 87 | fProc(&fRec, loops); |
reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | private: |
| 91 | typedef SkBenchmark INHERITED; |
| 92 | }; |
| 93 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 94 | DEF_BENCH( return new StrokeRRectBench(SkPaint::kRound_Join, draw_rect); ) |
| 95 | DEF_BENCH( return new StrokeRRectBench(SkPaint::kBevel_Join, draw_rect); ) |
| 96 | DEF_BENCH( return new StrokeRRectBench(SkPaint::kMiter_Join, draw_rect); ) |
reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 97 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 98 | DEF_BENCH( return new StrokeRRectBench(SkPaint::kRound_Join, draw_rrect); ) |
| 99 | DEF_BENCH( return new StrokeRRectBench(SkPaint::kBevel_Join, draw_rrect); ) |
| 100 | DEF_BENCH( return new StrokeRRectBench(SkPaint::kMiter_Join, draw_rrect); ) |
reed@google.com | c1f9011 | 2013-02-04 14:57:28 +0000 | [diff] [blame] | 101 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 102 | DEF_BENCH( return new StrokeRRectBench(SkPaint::kRound_Join, draw_oval); ) |
| 103 | DEF_BENCH( return new StrokeRRectBench(SkPaint::kBevel_Join, draw_oval); ) |
| 104 | DEF_BENCH( return new StrokeRRectBench(SkPaint::kMiter_Join, draw_oval); ) |