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 | |
| 18 | /* |
| 19 | * Cases to consider: |
| 20 | * |
| 21 | * 1. antialiasing on/off (esp. width <= 1) |
| 22 | * 2. strokewidth == 0, 1, 2 |
| 23 | * 3. hline, vline, diagonal, rect, oval |
| 24 | * 4. dots [1,1] ([N,N] where N=strokeWidth?) or arbitrary (e.g. [2,1] or [1,2,3,2]) |
| 25 | */ |
| 26 | static void path_hline(SkPath* path) { |
| 27 | path->moveTo(SkIntToScalar(10), SkIntToScalar(10)); |
| 28 | path->lineTo(SkIntToScalar(600), SkIntToScalar(10)); |
| 29 | } |
| 30 | |
| 31 | class DashBench : public SkBenchmark { |
mike@reedtribe.org | 99a6ef4 | 2012-05-05 13:46:10 +0000 | [diff] [blame^] | 32 | SkString fName; |
reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 33 | SkTDArray<SkScalar> fIntervals; |
mike@reedtribe.org | 99a6ef4 | 2012-05-05 13:46:10 +0000 | [diff] [blame^] | 34 | int fWidth; |
reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 35 | |
| 36 | enum { |
| 37 | N = SkBENCHLOOP(100) |
| 38 | }; |
| 39 | public: |
mike@reedtribe.org | 99a6ef4 | 2012-05-05 13:46:10 +0000 | [diff] [blame^] | 40 | 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] | 41 | fIntervals.append(count, intervals); |
mike@reedtribe.org | 99a6ef4 | 2012-05-05 13:46:10 +0000 | [diff] [blame^] | 42 | for (int i = 0; i < count; ++i) { |
| 43 | fIntervals[i] *= width; |
| 44 | } |
| 45 | fWidth = width; |
| 46 | fName.printf("dash_%d", width); |
reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | virtual void makePath(SkPath* path) { |
| 50 | path_hline(path); |
| 51 | } |
| 52 | |
| 53 | protected: |
| 54 | virtual const char* onGetName() SK_OVERRIDE { |
mike@reedtribe.org | 99a6ef4 | 2012-05-05 13:46:10 +0000 | [diff] [blame^] | 55 | return fName.c_str(); |
reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 59 | SkPaint paint; |
| 60 | this->setupPaint(&paint); |
| 61 | paint.setStyle(SkPaint::kStroke_Style); |
mike@reedtribe.org | 99a6ef4 | 2012-05-05 13:46:10 +0000 | [diff] [blame^] | 62 | paint.setStrokeWidth(SkIntToScalar(fWidth)); |
reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 63 | paint.setAntiAlias(false); |
| 64 | |
| 65 | SkPath path; |
| 66 | this->makePath(&path); |
| 67 | |
| 68 | paint.setPathEffect(new SkDashPathEffect(fIntervals.begin(), |
| 69 | fIntervals.count(), 0))->unref(); |
| 70 | for (int i = 0; i < N; ++i) { |
| 71 | canvas->drawPath(path, paint); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | typedef SkBenchmark INHERITED; |
| 77 | }; |
| 78 | |
| 79 | /////////////////////////////////////////////////////////////////////////////// |
| 80 | |
| 81 | static const SkScalar gDots[] = { SK_Scalar1, SK_Scalar1 }; |
| 82 | |
| 83 | #define PARAM(array) array, SK_ARRAY_COUNT(array) |
| 84 | |
mike@reedtribe.org | 99a6ef4 | 2012-05-05 13:46:10 +0000 | [diff] [blame^] | 85 | static SkBenchmark* gF0(void* p) { return new DashBench(p, PARAM(gDots), 0); } |
| 86 | static SkBenchmark* gF1(void* p) { return new DashBench(p, PARAM(gDots), 1); } |
| 87 | static SkBenchmark* gF2(void* p) { return new DashBench(p, PARAM(gDots), 4); } |
reed@google.com | 4aa1a70 | 2012-05-04 16:37:45 +0000 | [diff] [blame] | 88 | |
| 89 | static BenchRegistry gR0(gF0); |
mike@reedtribe.org | 99a6ef4 | 2012-05-05 13:46:10 +0000 | [diff] [blame^] | 90 | static BenchRegistry gR1(gF1); |
| 91 | static BenchRegistry gR2(gF2); |