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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
| 9 | #include "SkBlurMask.h" |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkPaint.h" |
| 12 | #include "SkRandom.h" |
| 13 | #include "SkShader.h" |
| 14 | #include "SkString.h" |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 15 | |
| 16 | #define SMALL SkIntToScalar(2) |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 17 | #define REAL 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); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 21 | #define REALBIG 30.5f |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 22 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 23 | class BlurRectBench: public Benchmark { |
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: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 29 | BlurRectBench(SkScalar rad) { |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 54 | virtual void onDraw(const int loops, 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 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 65 | for (int i = 0; i < loops; 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: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 73 | typedef Benchmark INHERITED; |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | |
| 77 | class BlurRectDirectBench: public BlurRectBench { |
| 78 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 79 | BlurRectDirectBench(SkScalar rad) : INHERITED(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: |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 91 | 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()), |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 94 | &mask, r, kNormal_SkBlurStyle); |
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: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 104 | BlurRectSeparableBench(SkScalar rad) : INHERITED(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: |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 113 | 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: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 131 | BlurRectBoxFilterBench(SkScalar rad) : INHERITED(rad) { |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 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 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 145 | 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()), |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 149 | kNormal_SkBlurStyle, kHigh_SkBlurQuality); |
bsalomon@google.com | 33cdbde | 2013-01-11 20:54:44 +0000 | [diff] [blame] | 150 | SkMask::FreeImage(mask.fImage); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 151 | } |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 152 | private: |
| 153 | typedef BlurRectSeparableBench INHERITED; |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 156 | class BlurRectGaussianBench: public BlurRectSeparableBench { |
| 157 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 158 | BlurRectGaussianBench(SkScalar rad) : INHERITED(rad) { |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 159 | SkString name; |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 160 | |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 161 | if (SkScalarFraction(rad) != 0) { |
| 162 | name.printf("blurrect_gaussian_%.2f", SkScalarToFloat(rad)); |
| 163 | } else { |
| 164 | name.printf("blurrect_gaussian_%d", SkScalarRoundToInt(rad)); |
| 165 | } |
skia.committer@gmail.com | b3ec29d | 2013-09-07 07:01:16 +0000 | [diff] [blame] | 166 | |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 167 | this->setName(name); |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | protected: |
| 171 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 172 | void makeBlurryRect(const SkRect&) SK_OVERRIDE { |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 173 | SkMask mask; |
| 174 | mask.fImage = NULL; |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 175 | SkBlurMask::BlurGroundTruth(SkBlurMask::ConvertRadiusToSigma(this->radius()), |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 176 | &mask, fSrcMask, kNormal_SkBlurStyle); |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 177 | SkMask::FreeImage(mask.fImage); |
| 178 | } |
| 179 | private: |
| 180 | typedef BlurRectSeparableBench INHERITED; |
| 181 | }; |
| 182 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 183 | DEF_BENCH(return new BlurRectBoxFilterBench(SMALL);) |
| 184 | DEF_BENCH(return new BlurRectBoxFilterBench(BIG);) |
| 185 | DEF_BENCH(return new BlurRectBoxFilterBench(REALBIG);) |
| 186 | DEF_BENCH(return new BlurRectBoxFilterBench(REAL);) |
| 187 | DEF_BENCH(return new BlurRectGaussianBench(SMALL);) |
| 188 | DEF_BENCH(return new BlurRectGaussianBench(BIG);) |
| 189 | DEF_BENCH(return new BlurRectGaussianBench(REALBIG);) |
| 190 | DEF_BENCH(return new BlurRectGaussianBench(REAL);) |
| 191 | DEF_BENCH(return new BlurRectDirectBench(SMALL);) |
| 192 | DEF_BENCH(return new BlurRectDirectBench(BIG);) |
| 193 | DEF_BENCH(return new BlurRectDirectBench(REALBIG);) |
| 194 | DEF_BENCH(return new BlurRectDirectBench(REAL);) |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 195 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 196 | DEF_BENCH(return new BlurRectDirectBench(kMedium);) |
| 197 | DEF_BENCH(return new BlurRectDirectBench(kMedBig);) |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 198 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 199 | DEF_BENCH(return new BlurRectBoxFilterBench(kMedium);) |
| 200 | DEF_BENCH(return new BlurRectBoxFilterBench(kMedBig);) |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 201 | |
| 202 | #if 0 |
| 203 | // disable Gaussian benchmarks; the algorithm works well enough |
| 204 | // and serves as a baseline for ground truth, but it's too slow |
| 205 | // to use in production for non-trivial radii, so no real point |
| 206 | // in having the bots benchmark it all the time. |
| 207 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 208 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(1));) |
| 209 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(2));) |
| 210 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(3));) |
| 211 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(4));) |
| 212 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(5));) |
| 213 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(6));) |
| 214 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(7));) |
| 215 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(8));) |
| 216 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(9));) |
| 217 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(10));) |
| 218 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(11));) |
| 219 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(12));) |
| 220 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(13));) |
| 221 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(14));) |
| 222 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(15));) |
| 223 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(16));) |
| 224 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(17));) |
| 225 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(18));) |
| 226 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(19));) |
| 227 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(20));) |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 228 | #endif |