• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
7 
8 #include "bench/Benchmark.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkPaint.h"
11 #include "include/core/SkShader.h"
12 #include "include/core/SkString.h"
13 #include "include/utils/SkRandom.h"
14 #include "src/core/SkBlurMask.h"
15 
16 #define SMALL   SkIntToScalar(2)
17 #define REAL    1.5f
18 static const SkScalar kMedium = SkIntToScalar(5);
19 #define BIG     SkIntToScalar(10)
20 static const SkScalar kMedBig = SkIntToScalar(20);
21 #define REALBIG 30.5f
22 
23 class BlurRectBench: public Benchmark {
24     int         fLoopCount;
25     SkScalar    fRadius;
26     SkString    fName;
27 
28 public:
BlurRectBench(SkScalar rad)29     BlurRectBench(SkScalar rad) {
30         fRadius = rad;
31 
32         if (fRadius > SkIntToScalar(25)) {
33             fLoopCount = 100;
34         } else if (fRadius > SkIntToScalar(5)) {
35             fLoopCount = 1000;
36         } else {
37             fLoopCount = 10000;
38         }
39     }
40 
41 protected:
onGetName()42     virtual const char* onGetName() {
43         return fName.c_str();
44     }
45 
radius() const46     SkScalar radius() const {
47         return fRadius;
48     }
49 
setName(const SkString & name)50     void setName(const SkString& name) {
51         fName = name;
52     }
53 
onDraw(int loops,SkCanvas *)54     virtual void onDraw(int loops, SkCanvas*) {
55         SkPaint paint;
56         this->setupPaint(&paint);
57 
58         paint.setAntiAlias(true);
59 
60         SkScalar pad = fRadius*3/2 + SK_Scalar1;
61         SkRect r = SkRect::MakeWH(2 * pad + SK_Scalar1, 2 * pad + SK_Scalar1);
62 
63         preBenchSetup(r);
64 
65         for (int i = 0; i < loops; i++) {
66             this->makeBlurryRect(r);
67         }
68     }
69 
70     virtual void makeBlurryRect(const SkRect&) = 0;
preBenchSetup(const SkRect &)71     virtual void preBenchSetup(const SkRect&) {}
72 private:
73     typedef Benchmark INHERITED;
74 };
75 
76 
77 class BlurRectDirectBench: public BlurRectBench {
78  public:
BlurRectDirectBench(SkScalar rad)79     BlurRectDirectBench(SkScalar rad) : INHERITED(rad) {
80         SkString name;
81 
82         if (SkScalarFraction(rad) != 0) {
83             name.printf("blurrect_direct_%.2f", SkScalarToFloat(rad));
84         } else {
85             name.printf("blurrect_direct_%d", SkScalarRoundToInt(rad));
86         }
87 
88         this->setName(name);
89     }
90 protected:
makeBlurryRect(const SkRect & r)91     void makeBlurryRect(const SkRect& r) override {
92         SkMask mask;
93         if (!SkBlurMask::BlurRect(SkBlurMask::ConvertRadiusToSigma(this->radius()),
94                                   &mask, r, kNormal_SkBlurStyle)) {
95             return;
96         }
97         SkMask::FreeImage(mask.fImage);
98     }
99 private:
100     typedef BlurRectBench INHERITED;
101 };
102 
103 class BlurRectSeparableBench: public BlurRectBench {
104 
105 public:
BlurRectSeparableBench(SkScalar rad)106     BlurRectSeparableBench(SkScalar rad) : INHERITED(rad) { }
107 
~BlurRectSeparableBench()108     ~BlurRectSeparableBench() override {
109         SkMask::FreeImage(fSrcMask.fImage);
110     }
111 
112 protected:
preBenchSetup(const SkRect & r)113     void preBenchSetup(const SkRect& r) override {
114         SkMask::FreeImage(fSrcMask.fImage);
115 
116         r.roundOut(&fSrcMask.fBounds);
117         fSrcMask.fFormat = SkMask::kA8_Format;
118         fSrcMask.fRowBytes = fSrcMask.fBounds.width();
119         fSrcMask.fImage = SkMask::AllocImage(fSrcMask.computeTotalImageSize());
120 
121         memset(fSrcMask.fImage, 0xff, fSrcMask.computeTotalImageSize());
122     }
123 
124     SkMask fSrcMask;
125 private:
126     typedef BlurRectBench INHERITED;
127 };
128 
129 class BlurRectBoxFilterBench: public BlurRectSeparableBench {
130 public:
BlurRectBoxFilterBench(SkScalar rad)131     BlurRectBoxFilterBench(SkScalar rad) : INHERITED(rad) {
132         SkString name;
133 
134         if (SkScalarFraction(rad) != 0) {
135             name.printf("blurrect_boxfilter_%.2f", SkScalarToFloat(rad));
136         } else {
137             name.printf("blurrect_boxfilter_%d", SkScalarRoundToInt(rad));
138         }
139 
140         this->setName(name);
141     }
142 
143 protected:
144 
makeBlurryRect(const SkRect &)145     void makeBlurryRect(const SkRect&) override {
146         SkMask mask;
147         if (!SkBlurMask::BoxBlur(&mask, fSrcMask, SkBlurMask::ConvertRadiusToSigma(this->radius()),
148                                  kNormal_SkBlurStyle)) {
149             return;
150         }
151         SkMask::FreeImage(mask.fImage);
152     }
153 private:
154     typedef BlurRectSeparableBench INHERITED;
155 };
156 
157 class BlurRectGaussianBench: public BlurRectSeparableBench {
158 public:
BlurRectGaussianBench(SkScalar rad)159     BlurRectGaussianBench(SkScalar rad) : INHERITED(rad) {
160         SkString name;
161 
162         if (SkScalarFraction(rad) != 0) {
163             name.printf("blurrect_gaussian_%.2f", SkScalarToFloat(rad));
164         } else {
165             name.printf("blurrect_gaussian_%d", SkScalarRoundToInt(rad));
166         }
167 
168         this->setName(name);
169     }
170 
171 protected:
172 
makeBlurryRect(const SkRect &)173     void makeBlurryRect(const SkRect&) override {
174         SkMask mask;
175         if (!SkBlurMask::BlurGroundTruth(SkBlurMask::ConvertRadiusToSigma(this->radius()),
176                                          &mask, fSrcMask, kNormal_SkBlurStyle)) {
177             return;
178         }
179         SkMask::FreeImage(mask.fImage);
180     }
181 private:
182     typedef BlurRectSeparableBench INHERITED;
183 };
184 
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);)
197 
198 DEF_BENCH(return new BlurRectDirectBench(kMedium);)
199 DEF_BENCH(return new BlurRectDirectBench(kMedBig);)
200 
201 DEF_BENCH(return new BlurRectBoxFilterBench(kMedium);)
202 DEF_BENCH(return new BlurRectBoxFilterBench(kMedBig);)
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 
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));)
230 #endif
231