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