• 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 "SkBenchmark.h"
9 #include "SkCanvas.h"
10 #include "SkPaint.h"
11 #include "SkRandom.h"
12 #include "SkShader.h"
13 #include "SkString.h"
14 #include "SkBlurMaskFilter.h"
15 
16 #define SMALL   SkIntToScalar(2)
17 #define REAL    SkFloatToScalar(1.5f)
18 #define BIG     SkIntToScalar(10)
19 
20 static const char* gStyleName[] = {
21     "normal",
22     "solid",
23     "outer",
24     "inner"
25 };
26 
27 class BlurBench : public SkBenchmark {
28     SkScalar    fRadius;
29     SkBlurMaskFilter::BlurStyle fStyle;
30     SkString    fName;
31 
32 public:
BlurBench(void * param,SkScalar rad,SkBlurMaskFilter::BlurStyle bs)33     BlurBench(void* param, SkScalar rad, SkBlurMaskFilter::BlurStyle bs) : INHERITED(param) {
34         fRadius = rad;
35         fStyle = bs;
36         const char* name = rad > 0 ? gStyleName[bs] : "none";
37         if (SkScalarFraction(rad) != 0) {
38             fName.printf("blur_%.2f_%s", SkScalarToFloat(rad), name);
39         } else {
40             fName.printf("blur_%d_%s", SkScalarRound(rad), name);
41         }
42     }
43 
44 protected:
onGetName()45     virtual const char* onGetName() {
46         return fName.c_str();
47     }
48 
onDraw(SkCanvas * canvas)49     virtual void onDraw(SkCanvas* canvas) {
50         SkPaint paint;
51         this->setupPaint(&paint);
52 
53         paint.setAntiAlias(true);
54 
55         SkRandom rand;
56         for (int i = 0; i < SkBENCHLOOP(10); i++) {
57             SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400,
58                                       rand.nextUScalar1() * 400);
59             r.offset(fRadius, fRadius);
60 
61             if (fRadius > 0) {
62                 SkMaskFilter* mf = SkBlurMaskFilter::Create(fRadius, fStyle, 0);
63                 paint.setMaskFilter(mf)->unref();
64             }
65             canvas->drawOval(r, paint);
66         }
67     }
68 
69 private:
70     typedef SkBenchmark INHERITED;
71 };
72 
Fact00(void * p)73 static SkBenchmark* Fact00(void* p) { return new BlurBench(p, SMALL, SkBlurMaskFilter::kNormal_BlurStyle); }
Fact01(void * p)74 static SkBenchmark* Fact01(void* p) { return new BlurBench(p, SMALL, SkBlurMaskFilter::kSolid_BlurStyle); }
Fact02(void * p)75 static SkBenchmark* Fact02(void* p) { return new BlurBench(p, SMALL, SkBlurMaskFilter::kOuter_BlurStyle); }
Fact03(void * p)76 static SkBenchmark* Fact03(void* p) { return new BlurBench(p, SMALL, SkBlurMaskFilter::kInner_BlurStyle); }
77 
Fact10(void * p)78 static SkBenchmark* Fact10(void* p) { return new BlurBench(p, BIG, SkBlurMaskFilter::kNormal_BlurStyle); }
Fact11(void * p)79 static SkBenchmark* Fact11(void* p) { return new BlurBench(p, BIG, SkBlurMaskFilter::kSolid_BlurStyle); }
Fact12(void * p)80 static SkBenchmark* Fact12(void* p) { return new BlurBench(p, BIG, SkBlurMaskFilter::kOuter_BlurStyle); }
Fact13(void * p)81 static SkBenchmark* Fact13(void* p) { return new BlurBench(p, BIG, SkBlurMaskFilter::kInner_BlurStyle); }
82 
Fact20(void * p)83 static SkBenchmark* Fact20(void* p) { return new BlurBench(p, REAL, SkBlurMaskFilter::kNormal_BlurStyle); }
Fact21(void * p)84 static SkBenchmark* Fact21(void* p) { return new BlurBench(p, REAL, SkBlurMaskFilter::kSolid_BlurStyle); }
Fact22(void * p)85 static SkBenchmark* Fact22(void* p) { return new BlurBench(p, REAL, SkBlurMaskFilter::kOuter_BlurStyle); }
Fact23(void * p)86 static SkBenchmark* Fact23(void* p) { return new BlurBench(p, REAL, SkBlurMaskFilter::kInner_BlurStyle); }
87 
FactNone(void * p)88 static SkBenchmark* FactNone(void* p) { return new BlurBench(p, 0, SkBlurMaskFilter::kNormal_BlurStyle); }
89 
90 static BenchRegistry gReg00(Fact00);
91 static BenchRegistry gReg01(Fact01);
92 static BenchRegistry gReg02(Fact02);
93 static BenchRegistry gReg03(Fact03);
94 
95 static BenchRegistry gReg10(Fact10);
96 static BenchRegistry gReg11(Fact11);
97 static BenchRegistry gReg12(Fact12);
98 static BenchRegistry gReg13(Fact13);
99 
100 static BenchRegistry gReg20(Fact20);
101 static BenchRegistry gReg21(Fact21);
102 static BenchRegistry gReg22(Fact22);
103 static BenchRegistry gReg23(Fact23);
104 
105 static BenchRegistry gRegNone(FactNone);
106 
107