• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 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 #include "bench/Benchmark.h"
8 #include "include/core/SkCanvas.h"
9 #include "include/core/SkMaskFilter.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 MINI    0.01f
17 #define SMALL   SkIntToScalar(2)
18 #define REAL    0.5f
19 #define BIG     SkIntToScalar(10)
20 #define REALBIG 100.5f
21 // The value that produces a sigma of just over 2.
22 #define CUTOVER 2.6f
23 
24 static const char* gStyleName[] = {
25     "normal",
26     "solid",
27     "outer",
28     "inner"
29 };
30 
31 class BlurBench : public Benchmark {
32     SkScalar    fRadius;
33     SkBlurStyle fStyle;
34     SkString    fName;
35 
36 public:
BlurBench(SkScalar rad,SkBlurStyle bs)37     BlurBench(SkScalar rad, SkBlurStyle bs) {
38         fRadius = rad;
39         fStyle = bs;
40         const char* name = rad > 0 ? gStyleName[bs] : "none";
41         const char* quality = "high_quality";
42         if (SkScalarFraction(rad) != 0) {
43             fName.printf("blur_%.2f_%s_%s", SkScalarToFloat(rad), name, quality);
44         } else {
45             fName.printf("blur_%d_%s_%s", SkScalarRoundToInt(rad), name, quality);
46         }
47     }
48 
49 protected:
onGetName()50     const char* onGetName() override {
51         return fName.c_str();
52     }
53 
onDraw(int loops,SkCanvas * canvas)54     void onDraw(int loops, SkCanvas* canvas) override {
55         SkPaint paint;
56         this->setupPaint(&paint);
57 
58         paint.setAntiAlias(true);
59 
60         SkRandom rand;
61         for (int i = 0; i < loops; i++) {
62             SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400,
63                                       rand.nextUScalar1() * 400);
64             r.offset(fRadius, fRadius);
65 
66             if (fRadius > 0) {
67                 paint.setMaskFilter(SkMaskFilter::MakeBlur(fStyle,
68                                                       SkBlurMask::ConvertRadiusToSigma(fRadius)));
69             }
70             canvas->drawOval(r, paint);
71         }
72     }
73 
74 private:
75     typedef Benchmark INHERITED;
76 };
77 
78 DEF_BENCH(return new BlurBench(MINI, kNormal_SkBlurStyle);)
79 DEF_BENCH(return new BlurBench(MINI, kSolid_SkBlurStyle);)
80 DEF_BENCH(return new BlurBench(MINI, kOuter_SkBlurStyle);)
81 DEF_BENCH(return new BlurBench(MINI, kInner_SkBlurStyle);)
82 
83 DEF_BENCH(return new BlurBench(SMALL, kNormal_SkBlurStyle);)
84 DEF_BENCH(return new BlurBench(SMALL, kSolid_SkBlurStyle);)
85 DEF_BENCH(return new BlurBench(SMALL, kOuter_SkBlurStyle);)
86 DEF_BENCH(return new BlurBench(SMALL, kInner_SkBlurStyle);)
87 
88 DEF_BENCH(return new BlurBench(BIG, kNormal_SkBlurStyle);)
89 DEF_BENCH(return new BlurBench(BIG, kSolid_SkBlurStyle);)
90 DEF_BENCH(return new BlurBench(BIG, kOuter_SkBlurStyle);)
91 DEF_BENCH(return new BlurBench(BIG, kInner_SkBlurStyle);)
92 
93 DEF_BENCH(return new BlurBench(REALBIG, kNormal_SkBlurStyle);)
94 DEF_BENCH(return new BlurBench(REALBIG, kSolid_SkBlurStyle);)
95 DEF_BENCH(return new BlurBench(REALBIG, kOuter_SkBlurStyle);)
96 DEF_BENCH(return new BlurBench(REALBIG, kInner_SkBlurStyle);)
97 
98 DEF_BENCH(return new BlurBench(REAL, kNormal_SkBlurStyle);)
99 DEF_BENCH(return new BlurBench(REAL, kSolid_SkBlurStyle);)
100 DEF_BENCH(return new BlurBench(REAL, kOuter_SkBlurStyle);)
101 DEF_BENCH(return new BlurBench(REAL, kInner_SkBlurStyle);)
102 
103 DEF_BENCH(return new BlurBench(0, kNormal_SkBlurStyle);)
104