humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2013 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 "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 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 39 | void setName( SkString name ) { |
| 40 | fName = name; |
| 41 | } |
| 42 | |
| 43 | virtual void onDraw(SkCanvas* canvas) { |
| 44 | SkPaint paint; |
| 45 | this->setupPaint(&paint); |
| 46 | |
| 47 | paint.setAntiAlias(true); |
| 48 | |
| 49 | int pad = fRadius * 1.5 + 1; |
| 50 | SkRect r = SkRect::MakeWH(2 * pad + 1, 2 * pad + 1); |
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 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +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++) { |
| 65 | makeBlurryRect( r ); |
| 66 | } |
| 67 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 68 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 69 | virtual void makeBlurryRect( SkRect &r ) = 0; |
| 70 | virtual void preBenchSetup( SkRect &r ) {} |
| 71 | private: |
| 72 | typedef SkBenchmark INHERITED; |
| 73 | }; |
| 74 | |
| 75 | |
| 76 | class BlurRectDirectBench: public BlurRectBench { |
| 77 | public: |
| 78 | BlurRectDirectBench( void *param, SkScalar rad ) : BlurRectBench( param, rad ) { |
| 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 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 87 | setName( name ); |
| 88 | } |
| 89 | protected: |
| 90 | virtual void makeBlurryRect( SkRect &r ) { |
| 91 | SkMask mask; |
| 92 | SkBlurMask::BlurRect( &mask, r, radius(), SkBlurMask::kNormal_Style, SkBlurMask::kHigh_Quality ); |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | class BlurRectSeparableBench: public BlurRectBench { |
| 97 | SkMask fSrcMask; |
| 98 | public: |
| 99 | BlurRectSeparableBench(void *param, SkScalar rad) : BlurRectBench( param, rad ) { |
| 100 | SkString name; |
| 101 | if (SkScalarFraction(rad) != 0) { |
| 102 | name.printf("blurrect_separable_%.2f", SkScalarToFloat(rad)); |
| 103 | } else { |
| 104 | name.printf("blurrect_separable_%d", SkScalarRound(rad)); |
| 105 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 106 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 107 | setName( name ); |
| 108 | } |
| 109 | |
| 110 | protected: |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 111 | virtual void preBenchSetup( SkRect &r ) { |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 112 | fSrcMask.fFormat = SkMask::kA8_Format; |
| 113 | fSrcMask.fRowBytes = r.width(); |
| 114 | fSrcMask.fBounds = SkIRect::MakeWH(r.width(), r.height()); |
| 115 | fSrcMask.fImage = SkMask::AllocImage( fSrcMask.computeTotalImageSize() ); |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 116 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 117 | memset( fSrcMask.fImage, 0xff, fSrcMask.computeTotalImageSize() ); |
| 118 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 119 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 120 | virtual void makeBlurryRect( SkRect &r ) { |
| 121 | SkMask mask; |
| 122 | SkBlurMask::BlurSeparable( &mask, fSrcMask, radius(), SkBlurMask::kNormal_Style, SkBlurMask::kHigh_Quality ); |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | DEF_BENCH(return new BlurRectSeparableBench(p, SMALL);) |
| 127 | DEF_BENCH(return new BlurRectSeparableBench(p, BIG);) |
| 128 | DEF_BENCH(return new BlurRectSeparableBench(p, REALBIG);) |
| 129 | DEF_BENCH(return new BlurRectSeparableBench(p, REAL);) |
| 130 | DEF_BENCH(return new BlurRectDirectBench(p, SMALL);) |
| 131 | DEF_BENCH(return new BlurRectDirectBench(p, BIG);) |
| 132 | DEF_BENCH(return new BlurRectDirectBench(p, REALBIG);) |
| 133 | DEF_BENCH(return new BlurRectDirectBench(p, REAL);) |