blob: de78fe1f9a0970de8fdfba93450260448491f09c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@google.com25df8882011-07-14 19:03:58 +00008#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)
tomhudson@google.com8caac642011-11-22 15:58:06 +000017#define REAL SkFloatToScalar(1.5f)
reed@google.com25df8882011-07-14 19:03:58 +000018#define BIG SkIntToScalar(10)
19
20static const char* gStyleName[] = {
21 "normal",
22 "solid",
23 "outer",
24 "inner"
25};
26
27class BlurBench : public SkBenchmark {
28 SkScalar fRadius;
29 SkBlurMaskFilter::BlurStyle fStyle;
30 SkString fName;
31
32public:
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";
tomhudson@google.com8caac642011-11-22 15:58:06 +000037 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 }
reed@google.com25df8882011-07-14 19:03:58 +000042 }
43
44protected:
45 virtual const char* onGetName() {
46 return fName.c_str();
47 }
48
49 virtual void onDraw(SkCanvas* canvas) {
50 SkPaint paint;
51 this->setupPaint(&paint);
52
53 paint.setAntiAlias(true);
54
55 SkRandom rand;
tomhudson@google.comca529d32011-10-28 15:34:49 +000056 for (int i = 0; i < SkBENCHLOOP(10); i++) {
reed@google.com25df8882011-07-14 19:03:58 +000057 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
69private:
70 typedef SkBenchmark INHERITED;
71};
72
73static SkBenchmark* Fact00(void* p) { return new BlurBench(p, SMALL, SkBlurMaskFilter::kNormal_BlurStyle); }
74static SkBenchmark* Fact01(void* p) { return new BlurBench(p, SMALL, SkBlurMaskFilter::kSolid_BlurStyle); }
75static SkBenchmark* Fact02(void* p) { return new BlurBench(p, SMALL, SkBlurMaskFilter::kOuter_BlurStyle); }
76static SkBenchmark* Fact03(void* p) { return new BlurBench(p, SMALL, SkBlurMaskFilter::kInner_BlurStyle); }
77
78static SkBenchmark* Fact10(void* p) { return new BlurBench(p, BIG, SkBlurMaskFilter::kNormal_BlurStyle); }
79static SkBenchmark* Fact11(void* p) { return new BlurBench(p, BIG, SkBlurMaskFilter::kSolid_BlurStyle); }
80static SkBenchmark* Fact12(void* p) { return new BlurBench(p, BIG, SkBlurMaskFilter::kOuter_BlurStyle); }
81static SkBenchmark* Fact13(void* p) { return new BlurBench(p, BIG, SkBlurMaskFilter::kInner_BlurStyle); }
82
tomhudson@google.com8caac642011-11-22 15:58:06 +000083static SkBenchmark* Fact20(void* p) { return new BlurBench(p, REAL, SkBlurMaskFilter::kNormal_BlurStyle); }
84static SkBenchmark* Fact21(void* p) { return new BlurBench(p, REAL, SkBlurMaskFilter::kSolid_BlurStyle); }
85static SkBenchmark* Fact22(void* p) { return new BlurBench(p, REAL, SkBlurMaskFilter::kOuter_BlurStyle); }
86static SkBenchmark* Fact23(void* p) { return new BlurBench(p, REAL, SkBlurMaskFilter::kInner_BlurStyle); }
87
reed@google.com25df8882011-07-14 19:03:58 +000088static SkBenchmark* FactNone(void* p) { return new BlurBench(p, 0, SkBlurMaskFilter::kNormal_BlurStyle); }
89
90static BenchRegistry gReg00(Fact00);
91static BenchRegistry gReg01(Fact01);
92static BenchRegistry gReg02(Fact02);
93static BenchRegistry gReg03(Fact03);
94
95static BenchRegistry gReg10(Fact10);
96static BenchRegistry gReg11(Fact11);
97static BenchRegistry gReg12(Fact12);
98static BenchRegistry gReg13(Fact13);
99
tomhudson@google.com8caac642011-11-22 15:58:06 +0000100static BenchRegistry gReg20(Fact20);
101static BenchRegistry gReg21(Fact21);
102static BenchRegistry gReg22(Fact22);
103static BenchRegistry gReg23(Fact23);
104
reed@google.com25df8882011-07-14 19:03:58 +0000105static BenchRegistry gRegNone(FactNone);
106