2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkBlurMask.h"
16 #define SMALL SkIntToScalar(2)
18 static const SkScalar kMedium = SkIntToScalar(5);
19 #define BIG SkIntToScalar(10)
20 static const SkScalar kMedBig = SkIntToScalar(20);
23 class BlurRectBench: public Benchmark {
29 BlurRectBench(SkScalar rad) {
32 if (fRadius > SkIntToScalar(25)) {
34 } else if (fRadius > SkIntToScalar(5)) {
42 virtual const char* onGetName() {
46 SkScalar radius() const {
50 void setName(const SkString& name) {
54 virtual void onDraw(const int loops, SkCanvas*) {
56 this->setupPaint(&paint);
58 paint.setAntiAlias(true);
60 SkScalar pad = fRadius*3/2 + SK_Scalar1;
61 SkRect r = SkRect::MakeWH(2 * pad + SK_Scalar1, 2 * pad + SK_Scalar1);
65 for (int i = 0; i < loops; i++) {
70 virtual void makeBlurryRect(const SkRect&) = 0;
71 virtual void preBenchSetup(const SkRect&) {}
73 typedef Benchmark INHERITED;
77 class BlurRectDirectBench: public BlurRectBench {
79 BlurRectDirectBench(SkScalar rad) : INHERITED(rad) {
82 if (SkScalarFraction(rad) != 0) {
83 name.printf("blurrect_direct_%.2f", SkScalarToFloat(rad));
85 name.printf("blurrect_direct_%d", SkScalarRoundToInt(rad));
91 void makeBlurryRect(const SkRect& r) SK_OVERRIDE {
93 SkBlurMask::BlurRect(SkBlurMask::ConvertRadiusToSigma(this->radius()),
94 &mask, r, kNormal_SkBlurStyle);
95 SkMask::FreeImage(mask.fImage);
98 typedef BlurRectBench INHERITED;
101 class BlurRectSeparableBench: public BlurRectBench {
104 BlurRectSeparableBench(SkScalar rad) : INHERITED(rad) {
105 fSrcMask.fImage = NULL;
108 ~BlurRectSeparableBench() {
109 SkMask::FreeImage(fSrcMask.fImage);
113 void preBenchSetup(const SkRect& r) SK_OVERRIDE {
114 SkMask::FreeImage(fSrcMask.fImage);
116 r.roundOut(&fSrcMask.fBounds);
117 fSrcMask.fFormat = SkMask::kA8_Format;
118 fSrcMask.fRowBytes = fSrcMask.fBounds.width();
119 fSrcMask.fImage = SkMask::AllocImage(fSrcMask.computeTotalImageSize());
121 memset(fSrcMask.fImage, 0xff, fSrcMask.computeTotalImageSize());
126 typedef BlurRectBench INHERITED;
129 class BlurRectBoxFilterBench: public BlurRectSeparableBench {
131 BlurRectBoxFilterBench(SkScalar rad) : INHERITED(rad) {
134 if (SkScalarFraction(rad) != 0) {
135 name.printf("blurrect_boxfilter_%.2f", SkScalarToFloat(rad));
137 name.printf("blurrect_boxfilter_%d", SkScalarRoundToInt(rad));
145 void makeBlurryRect(const SkRect&) SK_OVERRIDE {
148 SkBlurMask::BoxBlur(&mask, fSrcMask, SkBlurMask::ConvertRadiusToSigma(this->radius()),
149 kNormal_SkBlurStyle, kHigh_SkBlurQuality);
150 SkMask::FreeImage(mask.fImage);
153 typedef BlurRectSeparableBench INHERITED;
156 class BlurRectGaussianBench: public BlurRectSeparableBench {
158 BlurRectGaussianBench(SkScalar rad) : INHERITED(rad) {
161 if (SkScalarFraction(rad) != 0) {
162 name.printf("blurrect_gaussian_%.2f", SkScalarToFloat(rad));
164 name.printf("blurrect_gaussian_%d", SkScalarRoundToInt(rad));
172 void makeBlurryRect(const SkRect&) SK_OVERRIDE {
175 SkBlurMask::BlurGroundTruth(SkBlurMask::ConvertRadiusToSigma(this->radius()),
176 &mask, fSrcMask, kNormal_SkBlurStyle);
177 SkMask::FreeImage(mask.fImage);
180 typedef BlurRectSeparableBench INHERITED;
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);)
196 DEF_BENCH(return new BlurRectDirectBench(kMedium);)
197 DEF_BENCH(return new BlurRectDirectBench(kMedBig);)
199 DEF_BENCH(return new BlurRectBoxFilterBench(kMedium);)
200 DEF_BENCH(return new BlurRectBoxFilterBench(kMedBig);)
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.
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));)