blob: 66aff2207387be27685e4196c64158c2ff35bb0b [file] [log] [blame]
humper@google.com7c7292c2013-01-04 20:29:03 +00001/*
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.comcb88d322013-01-07 21:54:25 +00007
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
9#include "SkBlurMask.h"
humper@google.com7c7292c2013-01-04 20:29:03 +000010#include "SkCanvas.h"
11#include "SkPaint.h"
12#include "SkRandom.h"
13#include "SkShader.h"
14#include "SkString.h"
humper@google.com7c7292c2013-01-04 20:29:03 +000015
16#define SMALL SkIntToScalar(2)
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000017#define REAL 1.5f
robertphillips@google.comb7061172013-09-06 14:16:12 +000018static const SkScalar kMedium = SkIntToScalar(5);
humper@google.com7c7292c2013-01-04 20:29:03 +000019#define BIG SkIntToScalar(10)
robertphillips@google.comb7061172013-09-06 14:16:12 +000020static const SkScalar kMedBig = SkIntToScalar(20);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000021#define REALBIG 30.5f
humper@google.com7c7292c2013-01-04 20:29:03 +000022
tfarinaf168b862014-06-19 12:32:29 -070023class BlurRectBench: public Benchmark {
djsollen@google.comefbe8e92013-02-07 18:58:35 +000024 int fLoopCount;
humper@google.com7c7292c2013-01-04 20:29:03 +000025 SkScalar fRadius;
26 SkString fName;
27
28public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000029 BlurRectBench(SkScalar rad) {
humper@google.com7c7292c2013-01-04 20:29:03 +000030 fRadius = rad;
djsollen@google.comefbe8e92013-02-07 18:58:35 +000031
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.com7c7292c2013-01-04 20:29:03 +000039 }
40
41protected:
42 virtual const char* onGetName() {
43 return fName.c_str();
44 }
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000045
humper@google.com7c7292c2013-01-04 20:29:03 +000046 SkScalar radius() const {
47 return fRadius;
48 }
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000049
reed@google.comcb88d322013-01-07 21:54:25 +000050 void setName(const SkString& name) {
humper@google.com7c7292c2013-01-04 20:29:03 +000051 fName = name;
52 }
53
commit-bot@chromium.org33614712013-12-03 18:17:16 +000054 virtual void onDraw(const int loops, SkCanvas*) {
humper@google.com7c7292c2013-01-04 20:29:03 +000055 SkPaint paint;
56 this->setupPaint(&paint);
57
58 paint.setAntiAlias(true);
59
reed@google.comcb88d322013-01-07 21:54:25 +000060 SkScalar pad = fRadius*3/2 + SK_Scalar1;
61 SkRect r = SkRect::MakeWH(2 * pad + SK_Scalar1, 2 * pad + SK_Scalar1);
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000062
reed@google.comcb88d322013-01-07 21:54:25 +000063 preBenchSetup(r);
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000064
commit-bot@chromium.org33614712013-12-03 18:17:16 +000065 for (int i = 0; i < loops; i++) {
reed@google.comcb88d322013-01-07 21:54:25 +000066 makeBlurryRect(r);
humper@google.com7c7292c2013-01-04 20:29:03 +000067 }
68 }
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000069
reed@google.comcb88d322013-01-07 21:54:25 +000070 virtual void makeBlurryRect(const SkRect&) = 0;
71 virtual void preBenchSetup(const SkRect&) {}
humper@google.com7c7292c2013-01-04 20:29:03 +000072private:
tfarinaf168b862014-06-19 12:32:29 -070073 typedef Benchmark INHERITED;
humper@google.com7c7292c2013-01-04 20:29:03 +000074};
75
76
77class BlurRectDirectBench: public BlurRectBench {
78 public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000079 BlurRectDirectBench(SkScalar rad) : INHERITED(rad) {
humper@google.com7c7292c2013-01-04 20:29:03 +000080 SkString name;
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000081
humper@google.com7c7292c2013-01-04 20:29:03 +000082 if (SkScalarFraction(rad) != 0) {
83 name.printf("blurrect_direct_%.2f", SkScalarToFloat(rad));
84 } else {
humper@google.coma99a92c2013-02-20 16:42:06 +000085 name.printf("blurrect_direct_%d", SkScalarRoundToInt(rad));
humper@google.com7c7292c2013-01-04 20:29:03 +000086 }
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000087
robertphillips@google.comb7061172013-09-06 14:16:12 +000088 this->setName(name);
humper@google.com7c7292c2013-01-04 20:29:03 +000089 }
90protected:
mtklein72c9faa2015-01-09 10:06:39 -080091 void makeBlurryRect(const SkRect& r) SK_OVERRIDE {
humper@google.com7c7292c2013-01-04 20:29:03 +000092 SkMask mask;
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +000093 SkBlurMask::BlurRect(SkBlurMask::ConvertRadiusToSigma(this->radius()),
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000094 &mask, r, kNormal_SkBlurStyle);
bsalomon@google.com33cdbde2013-01-11 20:54:44 +000095 SkMask::FreeImage(mask.fImage);
humper@google.com7c7292c2013-01-04 20:29:03 +000096 }
humper@google.coma99a92c2013-02-20 16:42:06 +000097private:
98 typedef BlurRectBench INHERITED;
humper@google.com7c7292c2013-01-04 20:29:03 +000099};
100
101class BlurRectSeparableBench: public BlurRectBench {
humper@google.coma99a92c2013-02-20 16:42:06 +0000102
humper@google.com7c7292c2013-01-04 20:29:03 +0000103public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000104 BlurRectSeparableBench(SkScalar rad) : INHERITED(rad) {
bsalomon@google.com33cdbde2013-01-11 20:54:44 +0000105 fSrcMask.fImage = NULL;
106 }
107
108 ~BlurRectSeparableBench() {
109 SkMask::FreeImage(fSrcMask.fImage);
humper@google.com7c7292c2013-01-04 20:29:03 +0000110 }
111
112protected:
mtklein72c9faa2015-01-09 10:06:39 -0800113 void preBenchSetup(const SkRect& r) SK_OVERRIDE {
bsalomon@google.com33cdbde2013-01-11 20:54:44 +0000114 SkMask::FreeImage(fSrcMask.fImage);
115
reed@google.comcb88d322013-01-07 21:54:25 +0000116 r.roundOut(&fSrcMask.fBounds);
humper@google.com7c7292c2013-01-04 20:29:03 +0000117 fSrcMask.fFormat = SkMask::kA8_Format;
reed@google.comcb88d322013-01-07 21:54:25 +0000118 fSrcMask.fRowBytes = fSrcMask.fBounds.width();
119 fSrcMask.fImage = SkMask::AllocImage(fSrcMask.computeTotalImageSize());
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000120
reed@google.comcb88d322013-01-07 21:54:25 +0000121 memset(fSrcMask.fImage, 0xff, fSrcMask.computeTotalImageSize());
humper@google.com7c7292c2013-01-04 20:29:03 +0000122 }
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000123
humper@google.coma99a92c2013-02-20 16:42:06 +0000124 SkMask fSrcMask;
125private:
126 typedef BlurRectBench INHERITED;
127};
128
129class BlurRectBoxFilterBench: public BlurRectSeparableBench {
130public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000131 BlurRectBoxFilterBench(SkScalar rad) : INHERITED(rad) {
humper@google.coma99a92c2013-02-20 16:42:06 +0000132 SkString name;
robertphillips@google.comb7061172013-09-06 14:16:12 +0000133
humper@google.coma99a92c2013-02-20 16:42:06 +0000134 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.comb7061172013-09-06 14:16:12 +0000139
140 this->setName(name);
humper@google.coma99a92c2013-02-20 16:42:06 +0000141 }
142
143protected:
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000144
mtklein72c9faa2015-01-09 10:06:39 -0800145 void makeBlurryRect(const SkRect&) SK_OVERRIDE {
humper@google.com7c7292c2013-01-04 20:29:03 +0000146 SkMask mask;
humper@google.coma99a92c2013-02-20 16:42:06 +0000147 mask.fImage = NULL;
robertphillips@google.comb7061172013-09-06 14:16:12 +0000148 SkBlurMask::BoxBlur(&mask, fSrcMask, SkBlurMask::ConvertRadiusToSigma(this->radius()),
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000149 kNormal_SkBlurStyle, kHigh_SkBlurQuality);
bsalomon@google.com33cdbde2013-01-11 20:54:44 +0000150 SkMask::FreeImage(mask.fImage);
humper@google.com7c7292c2013-01-04 20:29:03 +0000151 }
humper@google.coma99a92c2013-02-20 16:42:06 +0000152private:
153 typedef BlurRectSeparableBench INHERITED;
humper@google.com7c7292c2013-01-04 20:29:03 +0000154};
155
humper@google.coma99a92c2013-02-20 16:42:06 +0000156class BlurRectGaussianBench: public BlurRectSeparableBench {
157public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000158 BlurRectGaussianBench(SkScalar rad) : INHERITED(rad) {
humper@google.coma99a92c2013-02-20 16:42:06 +0000159 SkString name;
robertphillips@google.comb7061172013-09-06 14:16:12 +0000160
humper@google.coma99a92c2013-02-20 16:42:06 +0000161 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.comb3ec29d2013-09-07 07:01:16 +0000166
robertphillips@google.comb7061172013-09-06 14:16:12 +0000167 this->setName(name);
humper@google.coma99a92c2013-02-20 16:42:06 +0000168 }
169
170protected:
171
mtklein72c9faa2015-01-09 10:06:39 -0800172 void makeBlurryRect(const SkRect&) SK_OVERRIDE {
humper@google.coma99a92c2013-02-20 16:42:06 +0000173 SkMask mask;
174 mask.fImage = NULL;
robertphillips@google.comb7061172013-09-06 14:16:12 +0000175 SkBlurMask::BlurGroundTruth(SkBlurMask::ConvertRadiusToSigma(this->radius()),
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000176 &mask, fSrcMask, kNormal_SkBlurStyle);
humper@google.coma99a92c2013-02-20 16:42:06 +0000177 SkMask::FreeImage(mask.fImage);
178 }
179private:
180 typedef BlurRectSeparableBench INHERITED;
181};
182
mtklein@google.com410e6e82013-09-13 19:52:27 +0000183DEF_BENCH(return new BlurRectBoxFilterBench(SMALL);)
184DEF_BENCH(return new BlurRectBoxFilterBench(BIG);)
185DEF_BENCH(return new BlurRectBoxFilterBench(REALBIG);)
186DEF_BENCH(return new BlurRectBoxFilterBench(REAL);)
187DEF_BENCH(return new BlurRectGaussianBench(SMALL);)
188DEF_BENCH(return new BlurRectGaussianBench(BIG);)
189DEF_BENCH(return new BlurRectGaussianBench(REALBIG);)
190DEF_BENCH(return new BlurRectGaussianBench(REAL);)
191DEF_BENCH(return new BlurRectDirectBench(SMALL);)
192DEF_BENCH(return new BlurRectDirectBench(BIG);)
193DEF_BENCH(return new BlurRectDirectBench(REALBIG);)
194DEF_BENCH(return new BlurRectDirectBench(REAL);)
humper@google.coma99a92c2013-02-20 16:42:06 +0000195
mtklein@google.com410e6e82013-09-13 19:52:27 +0000196DEF_BENCH(return new BlurRectDirectBench(kMedium);)
197DEF_BENCH(return new BlurRectDirectBench(kMedBig);)
humper@google.coma99a92c2013-02-20 16:42:06 +0000198
mtklein@google.com410e6e82013-09-13 19:52:27 +0000199DEF_BENCH(return new BlurRectBoxFilterBench(kMedium);)
200DEF_BENCH(return new BlurRectBoxFilterBench(kMedBig);)
humper@google.coma99a92c2013-02-20 16:42:06 +0000201
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.com410e6e82013-09-13 19:52:27 +0000208DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(1));)
209DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(2));)
210DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(3));)
211DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(4));)
212DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(5));)
213DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(6));)
214DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(7));)
215DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(8));)
216DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(9));)
217DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(10));)
218DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(11));)
219DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(12));)
220DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(13));)
221DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(14));)
222DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(15));)
223DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(16));)
224DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(17));)
225DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(18));)
226DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(19));)
227DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(20));)
humper@google.coma99a92c2013-02-20 16:42:06 +0000228#endif