• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2011 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 #include "Benchmark.h"
9 #include "SkBlurMask.h"
10 #include "SkBlurMaskFilter.h"
11 #include "SkCanvas.h"
12 #include "SkPaint.h"
13 #include "SkRandom.h"
14 #include "SkShader.h"
15 #include "SkString.h"
16 
17 #define MINI   0.01f
18 #define SMALL   SkIntToScalar(2)
19 #define REAL    1.5f
20 #define BIG     SkIntToScalar(10)
21 #define REALBIG 100.5f
22 
23 static const char* gStyleName[] = {
24     "normal",
25     "solid",
26     "outer",
27     "inner"
28 };
29 
30 class BlurBench : public Benchmark {
31     SkScalar    fRadius;
32     SkBlurStyle fStyle;
33     uint32_t    fFlags;
34     SkString    fName;
35 
36 public:
BlurBench(SkScalar rad,SkBlurStyle bs,uint32_t flags=0)37     BlurBench(SkScalar rad, SkBlurStyle bs, uint32_t flags = 0) {
38         fRadius = rad;
39         fStyle = bs;
40         fFlags = flags;
41         const char* name = rad > 0 ? gStyleName[bs] : "none";
42         const char* quality = flags & SkBlurMaskFilter::kHighQuality_BlurFlag ? "high_quality"
43                                                                               : "low_quality";
44         if (SkScalarFraction(rad) != 0) {
45             fName.printf("blur_%.2f_%s_%s", SkScalarToFloat(rad), name, quality);
46         } else {
47             fName.printf("blur_%d_%s_%s", SkScalarRoundToInt(rad), name, quality);
48         }
49     }
50 
51 protected:
onGetName()52     virtual const char* onGetName() {
53         return fName.c_str();
54     }
55 
onDraw(int loops,SkCanvas * canvas)56     virtual void onDraw(int loops, SkCanvas* canvas) {
57         SkPaint paint;
58         this->setupPaint(&paint);
59 
60         paint.setAntiAlias(true);
61 
62         SkRandom rand;
63         for (int i = 0; i < loops; i++) {
64             SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400,
65                                       rand.nextUScalar1() * 400);
66             r.offset(fRadius, fRadius);
67 
68             if (fRadius > 0) {
69                 SkMaskFilter* mf = SkBlurMaskFilter::Create(fStyle,
70                                             SkBlurMask::ConvertRadiusToSigma(fRadius),
71                                             fFlags);
72                 paint.setMaskFilter(mf)->unref();
73             }
74             canvas->drawOval(r, paint);
75         }
76     }
77 
78 private:
79     typedef Benchmark INHERITED;
80 };
81 
82 DEF_BENCH(return new BlurBench(MINI, kNormal_SkBlurStyle);)
83 DEF_BENCH(return new BlurBench(MINI, kSolid_SkBlurStyle);)
84 DEF_BENCH(return new BlurBench(MINI, kOuter_SkBlurStyle);)
85 DEF_BENCH(return new BlurBench(MINI, kInner_SkBlurStyle);)
86 
87 DEF_BENCH(return new BlurBench(SMALL, kNormal_SkBlurStyle);)
88 DEF_BENCH(return new BlurBench(SMALL, kSolid_SkBlurStyle);)
89 DEF_BENCH(return new BlurBench(SMALL, kOuter_SkBlurStyle);)
90 DEF_BENCH(return new BlurBench(SMALL, kInner_SkBlurStyle);)
91 
92 DEF_BENCH(return new BlurBench(BIG, kNormal_SkBlurStyle);)
93 DEF_BENCH(return new BlurBench(BIG, kSolid_SkBlurStyle);)
94 DEF_BENCH(return new BlurBench(BIG, kOuter_SkBlurStyle);)
95 DEF_BENCH(return new BlurBench(BIG, kInner_SkBlurStyle);)
96 
97 DEF_BENCH(return new BlurBench(REALBIG, kNormal_SkBlurStyle);)
98 DEF_BENCH(return new BlurBench(REALBIG, kSolid_SkBlurStyle);)
99 DEF_BENCH(return new BlurBench(REALBIG, kOuter_SkBlurStyle);)
100 DEF_BENCH(return new BlurBench(REALBIG, kInner_SkBlurStyle);)
101 
102 DEF_BENCH(return new BlurBench(REAL, kNormal_SkBlurStyle);)
103 DEF_BENCH(return new BlurBench(REAL, kSolid_SkBlurStyle);)
104 DEF_BENCH(return new BlurBench(REAL, kOuter_SkBlurStyle);)
105 DEF_BENCH(return new BlurBench(REAL, kInner_SkBlurStyle);)
106 
107 DEF_BENCH(return new BlurBench(MINI, kNormal_SkBlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);)
108 
109 DEF_BENCH(return new BlurBench(SMALL, kNormal_SkBlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);)
110 
111 DEF_BENCH(return new BlurBench(BIG, kNormal_SkBlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);)
112 
113 DEF_BENCH(return new BlurBench(REALBIG, kNormal_SkBlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);)
114 
115 DEF_BENCH(return new BlurBench(REAL, kNormal_SkBlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);)
116 
117 DEF_BENCH(return new BlurBench(0, kNormal_SkBlurStyle);)
118