humper@google.com | 7c7292c | 2013-01-04 20:29:03 +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 | */ |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame^] | 7 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 8 | #include "SkBenchmark.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkPaint.h" |
| 11 | #include "SkRandom.h" |
| 12 | #include "SkShader.h" |
| 13 | #include "SkString.h" |
| 14 | #include "SkBlurMask.h" |
| 15 | |
| 16 | #define SMALL SkIntToScalar(2) |
| 17 | #define REAL SkFloatToScalar(1.5f) |
| 18 | #define BIG SkIntToScalar(10) |
| 19 | #define REALBIG SkFloatToScalar(30.5f) |
| 20 | |
| 21 | class BlurRectBench: public SkBenchmark { |
| 22 | SkScalar fRadius; |
| 23 | SkString fName; |
| 24 | |
| 25 | public: |
| 26 | BlurRectBench(void *param, SkScalar rad) : INHERITED(param) { |
| 27 | fRadius = rad; |
| 28 | } |
| 29 | |
| 30 | protected: |
| 31 | virtual const char* onGetName() { |
| 32 | return fName.c_str(); |
| 33 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 34 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 35 | SkScalar radius() const { |
| 36 | return fRadius; |
| 37 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 38 | |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame^] | 39 | void setName(const SkString& name) { |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 40 | fName = name; |
| 41 | } |
| 42 | |
| 43 | virtual void onDraw(SkCanvas* canvas) { |
| 44 | SkPaint paint; |
| 45 | this->setupPaint(&paint); |
| 46 | |
| 47 | paint.setAntiAlias(true); |
| 48 | |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame^] | 49 | SkScalar pad = fRadius*3/2 + SK_Scalar1; |
| 50 | SkRect r = SkRect::MakeWH(2 * pad + SK_Scalar1, 2 * pad + SK_Scalar1); |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 51 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 52 | int loop_count; |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 53 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 54 | if (fRadius > SkIntToScalar(50)) { |
| 55 | loop_count = 10; |
| 56 | } else if (fRadius > SkIntToScalar(5)) { |
| 57 | loop_count = 1000; |
| 58 | } else { |
| 59 | loop_count = 10000; |
| 60 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 61 | |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame^] | 62 | preBenchSetup(r); |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 63 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 64 | for (int i = 0; i < SkBENCHLOOP(loop_count); i++) { |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame^] | 65 | makeBlurryRect(r); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 68 | |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame^] | 69 | virtual void makeBlurryRect(const SkRect&) = 0; |
| 70 | virtual void preBenchSetup(const SkRect&) {} |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 71 | private: |
| 72 | typedef SkBenchmark INHERITED; |
| 73 | }; |
| 74 | |
| 75 | |
| 76 | class BlurRectDirectBench: public BlurRectBench { |
| 77 | public: |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame^] | 78 | BlurRectDirectBench(void *param, SkScalar rad) : BlurRectBench(param, rad) { |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 79 | SkString name; |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 80 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 81 | if (SkScalarFraction(rad) != 0) { |
| 82 | name.printf("blurrect_direct_%.2f", SkScalarToFloat(rad)); |
| 83 | } else { |
| 84 | name.printf("blurrect_direct_%d", SkScalarRound(rad)); |
| 85 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 86 | |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame^] | 87 | setName(name); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 88 | } |
| 89 | protected: |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame^] | 90 | virtual void makeBlurryRect(const SkRect& r) SK_OVERRIDE { |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 91 | SkMask mask; |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame^] | 92 | SkBlurMask::BlurRect(&mask, r, radius(), SkBlurMask::kNormal_Style, |
| 93 | SkBlurMask::kHigh_Quality); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 94 | } |
| 95 | }; |
| 96 | |
| 97 | class BlurRectSeparableBench: public BlurRectBench { |
| 98 | SkMask fSrcMask; |
| 99 | public: |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame^] | 100 | BlurRectSeparableBench(void *param, SkScalar rad) : BlurRectBench(param, rad) { |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 101 | SkString name; |
| 102 | if (SkScalarFraction(rad) != 0) { |
| 103 | name.printf("blurrect_separable_%.2f", SkScalarToFloat(rad)); |
| 104 | } else { |
| 105 | name.printf("blurrect_separable_%d", SkScalarRound(rad)); |
| 106 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 107 | |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame^] | 108 | setName(name); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | protected: |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame^] | 112 | virtual void preBenchSetup(const SkRect& r) SK_OVERRIDE { |
| 113 | r.roundOut(&fSrcMask.fBounds); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 114 | fSrcMask.fFormat = SkMask::kA8_Format; |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame^] | 115 | fSrcMask.fRowBytes = fSrcMask.fBounds.width(); |
| 116 | fSrcMask.fImage = SkMask::AllocImage(fSrcMask.computeTotalImageSize()); |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 117 | |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame^] | 118 | memset(fSrcMask.fImage, 0xff, fSrcMask.computeTotalImageSize()); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 119 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 120 | |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame^] | 121 | virtual void makeBlurryRect(const SkRect& r) SK_OVERRIDE { |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 122 | SkMask mask; |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame^] | 123 | SkBlurMask::BlurSeparable(&mask, fSrcMask, radius(), |
| 124 | SkBlurMask::kNormal_Style, |
| 125 | SkBlurMask::kHigh_Quality); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 126 | } |
| 127 | }; |
| 128 | |
| 129 | DEF_BENCH(return new BlurRectSeparableBench(p, SMALL);) |
| 130 | DEF_BENCH(return new BlurRectSeparableBench(p, BIG);) |
| 131 | DEF_BENCH(return new BlurRectSeparableBench(p, REALBIG);) |
| 132 | DEF_BENCH(return new BlurRectSeparableBench(p, REAL);) |
| 133 | DEF_BENCH(return new BlurRectDirectBench(p, SMALL);) |
| 134 | DEF_BENCH(return new BlurRectDirectBench(p, BIG);) |
| 135 | DEF_BENCH(return new BlurRectDirectBench(p, REALBIG);) |
| 136 | DEF_BENCH(return new BlurRectDirectBench(p, REAL);) |