epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
reed@google.com | 25df888 | 2011-07-14 19:03:58 +0000 | [diff] [blame] | 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 BIG SkIntToScalar(10) |
| 18 | |
| 19 | static const char* gStyleName[] = { |
| 20 | "normal", |
| 21 | "solid", |
| 22 | "outer", |
| 23 | "inner" |
| 24 | }; |
| 25 | |
| 26 | class BlurBench : public SkBenchmark { |
| 27 | SkScalar fRadius; |
| 28 | SkBlurMaskFilter::BlurStyle fStyle; |
| 29 | SkString fName; |
| 30 | |
| 31 | public: |
| 32 | BlurBench(void* param, SkScalar rad, SkBlurMaskFilter::BlurStyle bs) : INHERITED(param) { |
| 33 | fRadius = rad; |
| 34 | fStyle = bs; |
| 35 | const char* name = rad > 0 ? gStyleName[bs] : "none"; |
reed@google.com | 8c4a2db | 2011-07-14 19:22:19 +0000 | [diff] [blame] | 36 | fName.printf("blur_%d_%s", SkScalarRound(rad), name); |
reed@google.com | 25df888 | 2011-07-14 19:03:58 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | protected: |
| 40 | virtual const char* onGetName() { |
| 41 | return fName.c_str(); |
| 42 | } |
| 43 | |
| 44 | virtual void onDraw(SkCanvas* canvas) { |
| 45 | SkPaint paint; |
| 46 | this->setupPaint(&paint); |
| 47 | |
| 48 | paint.setAntiAlias(true); |
| 49 | |
| 50 | SkRandom rand; |
tomhudson@google.com | ca529d3 | 2011-10-28 15:34:49 +0000 | [diff] [blame] | 51 | for (int i = 0; i < SkBENCHLOOP(10); i++) { |
reed@google.com | 25df888 | 2011-07-14 19:03:58 +0000 | [diff] [blame] | 52 | SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400, |
| 53 | rand.nextUScalar1() * 400); |
| 54 | r.offset(fRadius, fRadius); |
| 55 | |
| 56 | if (fRadius > 0) { |
| 57 | SkMaskFilter* mf = SkBlurMaskFilter::Create(fRadius, fStyle, 0); |
| 58 | paint.setMaskFilter(mf)->unref(); |
| 59 | } |
| 60 | canvas->drawOval(r, paint); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | typedef SkBenchmark INHERITED; |
| 66 | }; |
| 67 | |
| 68 | static SkBenchmark* Fact00(void* p) { return new BlurBench(p, SMALL, SkBlurMaskFilter::kNormal_BlurStyle); } |
| 69 | static SkBenchmark* Fact01(void* p) { return new BlurBench(p, SMALL, SkBlurMaskFilter::kSolid_BlurStyle); } |
| 70 | static SkBenchmark* Fact02(void* p) { return new BlurBench(p, SMALL, SkBlurMaskFilter::kOuter_BlurStyle); } |
| 71 | static SkBenchmark* Fact03(void* p) { return new BlurBench(p, SMALL, SkBlurMaskFilter::kInner_BlurStyle); } |
| 72 | |
| 73 | static SkBenchmark* Fact10(void* p) { return new BlurBench(p, BIG, SkBlurMaskFilter::kNormal_BlurStyle); } |
| 74 | static SkBenchmark* Fact11(void* p) { return new BlurBench(p, BIG, SkBlurMaskFilter::kSolid_BlurStyle); } |
| 75 | static SkBenchmark* Fact12(void* p) { return new BlurBench(p, BIG, SkBlurMaskFilter::kOuter_BlurStyle); } |
| 76 | static SkBenchmark* Fact13(void* p) { return new BlurBench(p, BIG, SkBlurMaskFilter::kInner_BlurStyle); } |
| 77 | |
| 78 | static SkBenchmark* FactNone(void* p) { return new BlurBench(p, 0, SkBlurMaskFilter::kNormal_BlurStyle); } |
| 79 | |
| 80 | static BenchRegistry gReg00(Fact00); |
| 81 | static BenchRegistry gReg01(Fact01); |
| 82 | static BenchRegistry gReg02(Fact02); |
| 83 | static BenchRegistry gReg03(Fact03); |
| 84 | |
| 85 | static BenchRegistry gReg10(Fact10); |
| 86 | static BenchRegistry gReg11(Fact11); |
| 87 | static BenchRegistry gReg12(Fact12); |
| 88 | static BenchRegistry gReg13(Fact13); |
| 89 | |
| 90 | static BenchRegistry gRegNone(FactNone); |
| 91 | |