| reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | #include "SkBenchmark.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkDashPathEffect.h" |
| 12 | #include "SkPaint.h" |
| 13 | #include "SkPath.h" |
| 14 | #include "SkRandom.h" |
| 15 | #include "SkString.h" |
| 16 | #include "SkTDArray.h" |
| 17 | |
| reed@google.com | ef85e3c | 2012-05-10 15:40:57 +0000 | [diff] [blame^] | 18 | |
| reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 19 | /* |
| 20 | * Cases to consider: |
| 21 | * |
| 22 | * 1. antialiasing on/off (esp. width <= 1) |
| 23 | * 2. strokewidth == 0, 1, 2 |
| 24 | * 3. hline, vline, diagonal, rect, oval |
| 25 | * 4. dots [1,1] ([N,N] where N=strokeWidth?) or arbitrary (e.g. [2,1] or [1,2,3,2]) |
| 26 | */ |
| 27 | static void path_hline(SkPath* path) { |
| 28 | path->moveTo(SkIntToScalar(10), SkIntToScalar(10)); |
| 29 | path->lineTo(SkIntToScalar(600), SkIntToScalar(10)); |
| 30 | } |
| 31 | |
| 32 | class DashBench : public SkBenchmark { |
| reed@google.com | ef85e3c | 2012-05-10 15:40:57 +0000 | [diff] [blame^] | 33 | protected: |
| mike@reedtribe.org | 99a6ef4 | 2012-05-05 13:46:10 +0000 | [diff] [blame] | 34 | SkString fName; |
| reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 35 | SkTDArray<SkScalar> fIntervals; |
| mike@reedtribe.org | 99a6ef4 | 2012-05-05 13:46:10 +0000 | [diff] [blame] | 36 | int fWidth; |
| reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 37 | |
| 38 | enum { |
| 39 | N = SkBENCHLOOP(100) |
| 40 | }; |
| 41 | public: |
| mike@reedtribe.org | 99a6ef4 | 2012-05-05 13:46:10 +0000 | [diff] [blame] | 42 | DashBench(void* param, const SkScalar intervals[], int count, int width) : INHERITED(param) { |
| reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 43 | fIntervals.append(count, intervals); |
| mike@reedtribe.org | 99a6ef4 | 2012-05-05 13:46:10 +0000 | [diff] [blame] | 44 | for (int i = 0; i < count; ++i) { |
| 45 | fIntervals[i] *= width; |
| 46 | } |
| 47 | fWidth = width; |
| 48 | fName.printf("dash_%d", width); |
| reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | virtual void makePath(SkPath* path) { |
| 52 | path_hline(path); |
| 53 | } |
| 54 | |
| 55 | protected: |
| 56 | virtual const char* onGetName() SK_OVERRIDE { |
| mike@reedtribe.org | 99a6ef4 | 2012-05-05 13:46:10 +0000 | [diff] [blame] | 57 | return fName.c_str(); |
| reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 61 | SkPaint paint; |
| 62 | this->setupPaint(&paint); |
| 63 | paint.setStyle(SkPaint::kStroke_Style); |
| mike@reedtribe.org | 99a6ef4 | 2012-05-05 13:46:10 +0000 | [diff] [blame] | 64 | paint.setStrokeWidth(SkIntToScalar(fWidth)); |
| reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 65 | paint.setAntiAlias(false); |
| 66 | |
| 67 | SkPath path; |
| 68 | this->makePath(&path); |
| 69 | |
| 70 | paint.setPathEffect(new SkDashPathEffect(fIntervals.begin(), |
| 71 | fIntervals.count(), 0))->unref(); |
| reed@google.com | ef85e3c | 2012-05-10 15:40:57 +0000 | [diff] [blame^] | 72 | this->handlePath(canvas, path, paint, N); |
| 73 | } |
| 74 | |
| 75 | virtual void handlePath(SkCanvas* canvas, const SkPath& path, |
| 76 | const SkPaint& paint, int N) { |
| reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 77 | for (int i = 0; i < N; ++i) { |
| 78 | canvas->drawPath(path, paint); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | private: |
| 83 | typedef SkBenchmark INHERITED; |
| 84 | }; |
| 85 | |
| reed@google.com | ef85e3c | 2012-05-10 15:40:57 +0000 | [diff] [blame^] | 86 | class RectDashBench : public DashBench { |
| 87 | public: |
| 88 | RectDashBench(void* param, const SkScalar intervals[], int count, int width) |
| 89 | : INHERITED(param, intervals, count, width) { |
| 90 | fName.append("_rect"); |
| 91 | } |
| 92 | |
| 93 | protected: |
| 94 | virtual void handlePath(SkCanvas* canvas, const SkPath& path, |
| 95 | const SkPaint& paint, int N) SK_OVERRIDE { |
| 96 | SkPoint pts[2]; |
| 97 | if (!path.isLine(pts) || pts[0].fY != pts[1].fY) { |
| 98 | this->INHERITED::handlePath(canvas, path, paint, N); |
| 99 | } else { |
| 100 | SkRect rect; |
| 101 | rect.fLeft = pts[0].fX; |
| 102 | rect.fTop = pts[0].fY - paint.getStrokeWidth() / 2; |
| 103 | rect.fRight = rect.fLeft + SkIntToScalar(fWidth); |
| 104 | rect.fBottom = rect.fTop + paint.getStrokeWidth(); |
| 105 | |
| 106 | SkPaint p(paint); |
| 107 | p.setStyle(SkPaint::kFill_Style); |
| 108 | p.setPathEffect(NULL); |
| 109 | |
| 110 | int count = SkScalarRoundToInt((pts[1].fX - pts[0].fX) / (2*fWidth)); |
| 111 | SkScalar dx = SkIntToScalar(2 * fWidth); |
| 112 | |
| 113 | for (int i = 0; i < N*10; ++i) { |
| 114 | SkRect r = rect; |
| 115 | for (int j = 0; j < count; ++j) { |
| 116 | canvas->drawRect(r, p); |
| 117 | r.offset(dx, 0); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | private: |
| 124 | typedef DashBench INHERITED; |
| 125 | }; |
| 126 | |
| reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 127 | /////////////////////////////////////////////////////////////////////////////// |
| 128 | |
| 129 | static const SkScalar gDots[] = { SK_Scalar1, SK_Scalar1 }; |
| 130 | |
| 131 | #define PARAM(array) array, SK_ARRAY_COUNT(array) |
| 132 | |
| mike@reedtribe.org | 99a6ef4 | 2012-05-05 13:46:10 +0000 | [diff] [blame] | 133 | static SkBenchmark* gF0(void* p) { return new DashBench(p, PARAM(gDots), 0); } |
| 134 | static SkBenchmark* gF1(void* p) { return new DashBench(p, PARAM(gDots), 1); } |
| 135 | static SkBenchmark* gF2(void* p) { return new DashBench(p, PARAM(gDots), 4); } |
| reed@google.com | ef85e3c | 2012-05-10 15:40:57 +0000 | [diff] [blame^] | 136 | static SkBenchmark* gF3(void* p) { return new RectDashBench(p, PARAM(gDots), 4); } |
| reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 137 | |
| 138 | static BenchRegistry gR0(gF0); |
| mike@reedtribe.org | 99a6ef4 | 2012-05-05 13:46:10 +0000 | [diff] [blame] | 139 | static BenchRegistry gR1(gF1); |
| 140 | static BenchRegistry gR2(gF2); |
| reed@google.com | ef85e3c | 2012-05-10 15:40:57 +0000 | [diff] [blame^] | 141 | static BenchRegistry gR3(gF3); |