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) |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 18 | static const SkScalar kMedium = SkIntToScalar(5); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 19 | #define BIG SkIntToScalar(10) |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 20 | static const SkScalar kMedBig = SkIntToScalar(20); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 21 | #define REALBIG SkFloatToScalar(30.5f) |
| 22 | |
| 23 | class BlurRectBench: public SkBenchmark { |
djsollen@google.com | efbe8e9 | 2013-02-07 18:58:35 +0000 | [diff] [blame] | 24 | int fLoopCount; |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 25 | SkScalar fRadius; |
| 26 | SkString fName; |
| 27 | |
| 28 | public: |
| 29 | BlurRectBench(void *param, SkScalar rad) : INHERITED(param) { |
| 30 | fRadius = rad; |
djsollen@google.com | efbe8e9 | 2013-02-07 18:58:35 +0000 | [diff] [blame] | 31 | |
| 32 | if (fRadius > SkIntToScalar(25)) { |
| 33 | fLoopCount = 100; |
| 34 | } else if (fRadius > SkIntToScalar(5)) { |
| 35 | fLoopCount = 1000; |
| 36 | } else { |
| 37 | fLoopCount = 10000; |
| 38 | } |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | protected: |
| 42 | virtual const char* onGetName() { |
| 43 | return fName.c_str(); |
| 44 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 45 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 46 | SkScalar radius() const { |
| 47 | return fRadius; |
| 48 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 49 | |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame] | 50 | void setName(const SkString& name) { |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 51 | fName = name; |
| 52 | } |
| 53 | |
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 54 | virtual void onDraw(SkCanvas*) { |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 55 | SkPaint paint; |
| 56 | this->setupPaint(&paint); |
| 57 | |
| 58 | paint.setAntiAlias(true); |
| 59 | |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame] | 60 | SkScalar pad = fRadius*3/2 + SK_Scalar1; |
| 61 | 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] | 62 | |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame] | 63 | preBenchSetup(r); |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 64 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 65 | for (int i = 0; i < this->getLoops(); i++) { |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame] | 66 | makeBlurryRect(r); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 69 | |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame] | 70 | virtual void makeBlurryRect(const SkRect&) = 0; |
| 71 | virtual void preBenchSetup(const SkRect&) {} |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 72 | private: |
| 73 | typedef SkBenchmark INHERITED; |
| 74 | }; |
| 75 | |
| 76 | |
| 77 | class BlurRectDirectBench: public BlurRectBench { |
| 78 | public: |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 79 | BlurRectDirectBench(void *param, SkScalar rad) : INHERITED(param, rad) { |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 80 | SkString name; |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 81 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 82 | if (SkScalarFraction(rad) != 0) { |
| 83 | name.printf("blurrect_direct_%.2f", SkScalarToFloat(rad)); |
| 84 | } else { |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 85 | name.printf("blurrect_direct_%d", SkScalarRoundToInt(rad)); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 86 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 87 | |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 88 | this->setName(name); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 89 | } |
| 90 | protected: |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame] | 91 | virtual void makeBlurryRect(const SkRect& r) SK_OVERRIDE { |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 92 | SkMask mask; |
skia.committer@gmail.com | b3ec29d | 2013-09-07 07:01:16 +0000 | [diff] [blame] | 93 | SkBlurMask::BlurRect(SkBlurMask::ConvertRadiusToSigma(this->radius()), |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 94 | &mask, r, SkBlurMask::kNormal_Style); |
bsalomon@google.com | 33cdbde | 2013-01-11 20:54:44 +0000 | [diff] [blame] | 95 | SkMask::FreeImage(mask.fImage); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 96 | } |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 97 | private: |
| 98 | typedef BlurRectBench INHERITED; |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | class BlurRectSeparableBench: public BlurRectBench { |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 102 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 103 | public: |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 104 | BlurRectSeparableBench(void *param, SkScalar rad) : INHERITED(param, rad) { |
bsalomon@google.com | 33cdbde | 2013-01-11 20:54:44 +0000 | [diff] [blame] | 105 | fSrcMask.fImage = NULL; |
| 106 | } |
| 107 | |
| 108 | ~BlurRectSeparableBench() { |
| 109 | SkMask::FreeImage(fSrcMask.fImage); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | protected: |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame] | 113 | virtual void preBenchSetup(const SkRect& r) SK_OVERRIDE { |
bsalomon@google.com | 33cdbde | 2013-01-11 20:54:44 +0000 | [diff] [blame] | 114 | SkMask::FreeImage(fSrcMask.fImage); |
| 115 | |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame] | 116 | r.roundOut(&fSrcMask.fBounds); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 117 | fSrcMask.fFormat = SkMask::kA8_Format; |
reed@google.com | cb88d32 | 2013-01-07 21:54:25 +0000 | [diff] [blame] | 118 | fSrcMask.fRowBytes = fSrcMask.fBounds.width(); |
| 119 | fSrcMask.fImage = SkMask::AllocImage(fSrcMask.computeTotalImageSize()); |
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 | memset(fSrcMask.fImage, 0xff, fSrcMask.computeTotalImageSize()); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 122 | } |
skia.committer@gmail.com | d454ec1 | 2013-02-21 07:15:03 +0000 | [diff] [blame] | 123 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 124 | SkMask fSrcMask; |
| 125 | private: |
| 126 | typedef BlurRectBench INHERITED; |
| 127 | }; |
| 128 | |
| 129 | class BlurRectBoxFilterBench: public BlurRectSeparableBench { |
| 130 | public: |
| 131 | BlurRectBoxFilterBench(void *param, SkScalar rad) : INHERITED(param, rad) { |
| 132 | SkString name; |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 133 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 134 | if (SkScalarFraction(rad) != 0) { |
| 135 | name.printf("blurrect_boxfilter_%.2f", SkScalarToFloat(rad)); |
| 136 | } else { |
| 137 | name.printf("blurrect_boxfilter_%d", SkScalarRoundToInt(rad)); |
| 138 | } |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 139 | |
| 140 | this->setName(name); |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | protected: |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 144 | |
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 145 | virtual void makeBlurryRect(const SkRect&) SK_OVERRIDE { |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 146 | SkMask mask; |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 147 | mask.fImage = NULL; |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 148 | SkBlurMask::BoxBlur(&mask, fSrcMask, SkBlurMask::ConvertRadiusToSigma(this->radius()), |
| 149 | SkBlurMask::kNormal_Style, |
| 150 | SkBlurMask::kHigh_Quality); |
bsalomon@google.com | 33cdbde | 2013-01-11 20:54:44 +0000 | [diff] [blame] | 151 | SkMask::FreeImage(mask.fImage); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 152 | } |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 153 | private: |
| 154 | typedef BlurRectSeparableBench INHERITED; |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 157 | class BlurRectGaussianBench: public BlurRectSeparableBench { |
| 158 | public: |
| 159 | BlurRectGaussianBench(void *param, SkScalar rad) : INHERITED(param, rad) { |
| 160 | SkString name; |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 161 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 162 | if (SkScalarFraction(rad) != 0) { |
| 163 | name.printf("blurrect_gaussian_%.2f", SkScalarToFloat(rad)); |
| 164 | } else { |
| 165 | name.printf("blurrect_gaussian_%d", SkScalarRoundToInt(rad)); |
| 166 | } |
skia.committer@gmail.com | b3ec29d | 2013-09-07 07:01:16 +0000 | [diff] [blame] | 167 | |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 168 | this->setName(name); |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | protected: |
| 172 | |
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 173 | virtual void makeBlurryRect(const SkRect&) SK_OVERRIDE { |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 174 | SkMask mask; |
| 175 | mask.fImage = NULL; |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 176 | SkBlurMask::BlurGroundTruth(SkBlurMask::ConvertRadiusToSigma(this->radius()), |
| 177 | &mask, fSrcMask, SkBlurMask::kNormal_Style); |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 178 | SkMask::FreeImage(mask.fImage); |
| 179 | } |
| 180 | private: |
| 181 | typedef BlurRectSeparableBench INHERITED; |
| 182 | }; |
| 183 | |
| 184 | DEF_BENCH(return new BlurRectBoxFilterBench(p, SMALL);) |
| 185 | DEF_BENCH(return new BlurRectBoxFilterBench(p, BIG);) |
| 186 | DEF_BENCH(return new BlurRectBoxFilterBench(p, REALBIG);) |
| 187 | DEF_BENCH(return new BlurRectBoxFilterBench(p, REAL);) |
| 188 | DEF_BENCH(return new BlurRectGaussianBench(p, SMALL);) |
| 189 | DEF_BENCH(return new BlurRectGaussianBench(p, BIG);) |
| 190 | DEF_BENCH(return new BlurRectGaussianBench(p, REALBIG);) |
| 191 | DEF_BENCH(return new BlurRectGaussianBench(p, REAL);) |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 192 | DEF_BENCH(return new BlurRectDirectBench(p, SMALL);) |
| 193 | DEF_BENCH(return new BlurRectDirectBench(p, BIG);) |
| 194 | DEF_BENCH(return new BlurRectDirectBench(p, REALBIG);) |
| 195 | DEF_BENCH(return new BlurRectDirectBench(p, REAL);) |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 196 | |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 197 | DEF_BENCH(return new BlurRectDirectBench(p, kMedium);) |
| 198 | DEF_BENCH(return new BlurRectDirectBench(p, kMedBig);) |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 199 | |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 200 | DEF_BENCH(return new BlurRectBoxFilterBench(p, kMedium);) |
| 201 | DEF_BENCH(return new BlurRectBoxFilterBench(p, kMedBig);) |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 202 | |
| 203 | #if 0 |
| 204 | // disable Gaussian benchmarks; the algorithm works well enough |
| 205 | // and serves as a baseline for ground truth, but it's too slow |
| 206 | // to use in production for non-trivial radii, so no real point |
| 207 | // in having the bots benchmark it all the time. |
| 208 | |
| 209 | DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(1));) |
| 210 | DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(2));) |
| 211 | DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(3));) |
| 212 | DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(4));) |
| 213 | DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(5));) |
| 214 | DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(6));) |
| 215 | DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(7));) |
| 216 | DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(8));) |
| 217 | DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(9));) |
| 218 | DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(10));) |
| 219 | DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(11));) |
| 220 | DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(12));) |
| 221 | DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(13));) |
| 222 | DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(14));) |
| 223 | DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(15));) |
| 224 | DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(16));) |
| 225 | DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(17));) |
| 226 | DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(18));) |
| 227 | DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(19));) |
| 228 | DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(20));) |
| 229 | #endif |