blob: 5de16cd175dc1b7b1795714bf8bdb54a8a309f13 [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
humper@google.com7c7292c2013-01-04 20:29:03 +00008#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.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);
humper@google.com7c7292c2013-01-04 20:29:03 +000021#define REALBIG SkFloatToScalar(30.5f)
22
23class BlurRectBench: public SkBenchmark {
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:
29 BlurRectBench(void *param, SkScalar rad) : INHERITED(param) {
30 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
sugoi@google.com77472f02013-03-05 18:50:01 +000054 virtual void onDraw(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
mtklein@google.comc2897432013-09-10 19:23:38 +000065 for (int i = 0; i < this->getLoops(); 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:
73 typedef SkBenchmark INHERITED;
74};
75
76
77class BlurRectDirectBench: public BlurRectBench {
78 public:
humper@google.coma99a92c2013-02-20 16:42:06 +000079 BlurRectDirectBench(void *param, SkScalar rad) : INHERITED(param, 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:
reed@google.comcb88d322013-01-07 21:54:25 +000091 virtual 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()),
robertphillips@google.comb7061172013-09-06 14:16:12 +000094 &mask, r, SkBlurMask::kNormal_Style);
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:
humper@google.coma99a92c2013-02-20 16:42:06 +0000104 BlurRectSeparableBench(void *param, SkScalar rad) : INHERITED(param, 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:
reed@google.comcb88d322013-01-07 21:54:25 +0000113 virtual 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:
131 BlurRectBoxFilterBench(void *param, SkScalar rad) : INHERITED(param, rad) {
132 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
sugoi@google.com77472f02013-03-05 18:50:01 +0000145 virtual 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()),
149 SkBlurMask::kNormal_Style,
150 SkBlurMask::kHigh_Quality);
bsalomon@google.com33cdbde2013-01-11 20:54:44 +0000151 SkMask::FreeImage(mask.fImage);
humper@google.com7c7292c2013-01-04 20:29:03 +0000152 }
humper@google.coma99a92c2013-02-20 16:42:06 +0000153private:
154 typedef BlurRectSeparableBench INHERITED;
humper@google.com7c7292c2013-01-04 20:29:03 +0000155};
156
humper@google.coma99a92c2013-02-20 16:42:06 +0000157class BlurRectGaussianBench: public BlurRectSeparableBench {
158public:
159 BlurRectGaussianBench(void *param, SkScalar rad) : INHERITED(param, rad) {
160 SkString name;
robertphillips@google.comb7061172013-09-06 14:16:12 +0000161
humper@google.coma99a92c2013-02-20 16:42:06 +0000162 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.comb3ec29d2013-09-07 07:01:16 +0000167
robertphillips@google.comb7061172013-09-06 14:16:12 +0000168 this->setName(name);
humper@google.coma99a92c2013-02-20 16:42:06 +0000169 }
170
171protected:
172
sugoi@google.com77472f02013-03-05 18:50:01 +0000173 virtual void makeBlurryRect(const SkRect&) SK_OVERRIDE {
humper@google.coma99a92c2013-02-20 16:42:06 +0000174 SkMask mask;
175 mask.fImage = NULL;
robertphillips@google.comb7061172013-09-06 14:16:12 +0000176 SkBlurMask::BlurGroundTruth(SkBlurMask::ConvertRadiusToSigma(this->radius()),
177 &mask, fSrcMask, SkBlurMask::kNormal_Style);
humper@google.coma99a92c2013-02-20 16:42:06 +0000178 SkMask::FreeImage(mask.fImage);
179 }
180private:
181 typedef BlurRectSeparableBench INHERITED;
182};
183
184DEF_BENCH(return new BlurRectBoxFilterBench(p, SMALL);)
185DEF_BENCH(return new BlurRectBoxFilterBench(p, BIG);)
186DEF_BENCH(return new BlurRectBoxFilterBench(p, REALBIG);)
187DEF_BENCH(return new BlurRectBoxFilterBench(p, REAL);)
188DEF_BENCH(return new BlurRectGaussianBench(p, SMALL);)
189DEF_BENCH(return new BlurRectGaussianBench(p, BIG);)
190DEF_BENCH(return new BlurRectGaussianBench(p, REALBIG);)
191DEF_BENCH(return new BlurRectGaussianBench(p, REAL);)
humper@google.com7c7292c2013-01-04 20:29:03 +0000192DEF_BENCH(return new BlurRectDirectBench(p, SMALL);)
193DEF_BENCH(return new BlurRectDirectBench(p, BIG);)
194DEF_BENCH(return new BlurRectDirectBench(p, REALBIG);)
195DEF_BENCH(return new BlurRectDirectBench(p, REAL);)
humper@google.coma99a92c2013-02-20 16:42:06 +0000196
robertphillips@google.comb7061172013-09-06 14:16:12 +0000197DEF_BENCH(return new BlurRectDirectBench(p, kMedium);)
198DEF_BENCH(return new BlurRectDirectBench(p, kMedBig);)
humper@google.coma99a92c2013-02-20 16:42:06 +0000199
robertphillips@google.comb7061172013-09-06 14:16:12 +0000200DEF_BENCH(return new BlurRectBoxFilterBench(p, kMedium);)
201DEF_BENCH(return new BlurRectBoxFilterBench(p, kMedBig);)
humper@google.coma99a92c2013-02-20 16:42:06 +0000202
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
209DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(1));)
210DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(2));)
211DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(3));)
212DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(4));)
213DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(5));)
214DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(6));)
215DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(7));)
216DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(8));)
217DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(9));)
218DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(10));)
219DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(11));)
220DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(12));)
221DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(13));)
222DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(14));)
223DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(15));)
224DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(16));)
225DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(17));)
226DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(18));)
227DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(19));)
228DEF_BENCH(return new BlurRectGaussianBench(p, SkIntToScalar(20));)
229#endif