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 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 54 | virtual void onDraw(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++) { |
robertphillips | e80eb92 | 2015-12-17 11:33:12 -0800 | [diff] [blame] | 66 | this->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 | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 91 | void makeBlurryRect(const SkRect& r) override { |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 92 | SkMask mask; |
robertphillips | e80eb92 | 2015-12-17 11:33:12 -0800 | [diff] [blame] | 93 | if (!SkBlurMask::BlurRect(SkBlurMask::ConvertRadiusToSigma(this->radius()), |
| 94 | &mask, r, kNormal_SkBlurStyle)) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 95 | return; |
robertphillips | e80eb92 | 2015-12-17 11:33:12 -0800 | [diff] [blame] | 96 | } |
bsalomon@google.com | 33cdbde | 2013-01-11 20:54:44 +0000 | [diff] [blame] | 97 | SkMask::FreeImage(mask.fImage); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 98 | } |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 99 | private: |
| 100 | typedef BlurRectBench INHERITED; |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | class BlurRectSeparableBench: public BlurRectBench { |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 104 | |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 105 | public: |
robertphillips | e80eb92 | 2015-12-17 11:33:12 -0800 | [diff] [blame] | 106 | BlurRectSeparableBench(SkScalar rad) : INHERITED(rad) { } |
bsalomon@google.com | 33cdbde | 2013-01-11 20:54:44 +0000 | [diff] [blame] | 107 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame^] | 108 | ~BlurRectSeparableBench() override { |
bsalomon@google.com | 33cdbde | 2013-01-11 20:54:44 +0000 | [diff] [blame] | 109 | SkMask::FreeImage(fSrcMask.fImage); |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 113 | void preBenchSetup(const SkRect& r) 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 | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 145 | void makeBlurryRect(const SkRect&) override { |
humper@google.com | 7c7292c | 2013-01-04 20:29:03 +0000 | [diff] [blame] | 146 | SkMask mask; |
robertphillips | e80eb92 | 2015-12-17 11:33:12 -0800 | [diff] [blame] | 147 | if (!SkBlurMask::BoxBlur(&mask, fSrcMask, SkBlurMask::ConvertRadiusToSigma(this->radius()), |
| 148 | kNormal_SkBlurStyle, kHigh_SkBlurQuality)) { |
| 149 | return; |
| 150 | } |
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: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 159 | BlurRectGaussianBench(SkScalar rad) : INHERITED(rad) { |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 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 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 173 | void makeBlurryRect(const SkRect&) override { |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 174 | SkMask mask; |
robertphillips | e80eb92 | 2015-12-17 11:33:12 -0800 | [diff] [blame] | 175 | if (!SkBlurMask::BlurGroundTruth(SkBlurMask::ConvertRadiusToSigma(this->radius()), |
| 176 | &mask, fSrcMask, kNormal_SkBlurStyle)) { |
| 177 | return; |
| 178 | } |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 179 | SkMask::FreeImage(mask.fImage); |
| 180 | } |
| 181 | private: |
| 182 | typedef BlurRectSeparableBench INHERITED; |
| 183 | }; |
| 184 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 185 | DEF_BENCH(return new BlurRectBoxFilterBench(SMALL);) |
| 186 | DEF_BENCH(return new BlurRectBoxFilterBench(BIG);) |
| 187 | DEF_BENCH(return new BlurRectBoxFilterBench(REALBIG);) |
| 188 | DEF_BENCH(return new BlurRectBoxFilterBench(REAL);) |
| 189 | DEF_BENCH(return new BlurRectGaussianBench(SMALL);) |
| 190 | DEF_BENCH(return new BlurRectGaussianBench(BIG);) |
| 191 | DEF_BENCH(return new BlurRectGaussianBench(REALBIG);) |
| 192 | DEF_BENCH(return new BlurRectGaussianBench(REAL);) |
| 193 | DEF_BENCH(return new BlurRectDirectBench(SMALL);) |
| 194 | DEF_BENCH(return new BlurRectDirectBench(BIG);) |
| 195 | DEF_BENCH(return new BlurRectDirectBench(REALBIG);) |
| 196 | DEF_BENCH(return new BlurRectDirectBench(REAL);) |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 197 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 198 | DEF_BENCH(return new BlurRectDirectBench(kMedium);) |
| 199 | DEF_BENCH(return new BlurRectDirectBench(kMedBig);) |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 200 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 201 | DEF_BENCH(return new BlurRectBoxFilterBench(kMedium);) |
| 202 | DEF_BENCH(return new BlurRectBoxFilterBench(kMedBig);) |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 203 | |
| 204 | #if 0 |
| 205 | // disable Gaussian benchmarks; the algorithm works well enough |
| 206 | // and serves as a baseline for ground truth, but it's too slow |
| 207 | // to use in production for non-trivial radii, so no real point |
| 208 | // in having the bots benchmark it all the time. |
| 209 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 210 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(1));) |
| 211 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(2));) |
| 212 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(3));) |
| 213 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(4));) |
| 214 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(5));) |
| 215 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(6));) |
| 216 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(7));) |
| 217 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(8));) |
| 218 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(9));) |
| 219 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(10));) |
| 220 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(11));) |
| 221 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(12));) |
| 222 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(13));) |
| 223 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(14));) |
| 224 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(15));) |
| 225 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(16));) |
| 226 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(17));) |
| 227 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(18));) |
| 228 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(19));) |
| 229 | DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(20));) |
humper@google.com | a99a92c | 2013-02-20 16:42:06 +0000 | [diff] [blame] | 230 | #endif |