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) |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 17 | #define REAL SkFloatToScalar(1.5f) |
reed@google.com | 25df888 | 2011-07-14 19:03:58 +0000 | [diff] [blame] | 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: |
| 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.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 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 | } |
reed@google.com | 25df888 | 2011-07-14 19:03:58 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | protected: |
| 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.com | ca529d3 | 2011-10-28 15:34:49 +0000 | [diff] [blame] | 56 | for (int i = 0; i < SkBENCHLOOP(10); i++) { |
reed@google.com | 25df888 | 2011-07-14 19:03:58 +0000 | [diff] [blame] | 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 | |
| 73 | static SkBenchmark* Fact00(void* p) { return new BlurBench(p, SMALL, SkBlurMaskFilter::kNormal_BlurStyle); } |
| 74 | static SkBenchmark* Fact01(void* p) { return new BlurBench(p, SMALL, SkBlurMaskFilter::kSolid_BlurStyle); } |
| 75 | static SkBenchmark* Fact02(void* p) { return new BlurBench(p, SMALL, SkBlurMaskFilter::kOuter_BlurStyle); } |
| 76 | static SkBenchmark* Fact03(void* p) { return new BlurBench(p, SMALL, SkBlurMaskFilter::kInner_BlurStyle); } |
| 77 | |
| 78 | static SkBenchmark* Fact10(void* p) { return new BlurBench(p, BIG, SkBlurMaskFilter::kNormal_BlurStyle); } |
| 79 | static SkBenchmark* Fact11(void* p) { return new BlurBench(p, BIG, SkBlurMaskFilter::kSolid_BlurStyle); } |
| 80 | static SkBenchmark* Fact12(void* p) { return new BlurBench(p, BIG, SkBlurMaskFilter::kOuter_BlurStyle); } |
| 81 | static SkBenchmark* Fact13(void* p) { return new BlurBench(p, BIG, SkBlurMaskFilter::kInner_BlurStyle); } |
| 82 | |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 83 | static SkBenchmark* Fact20(void* p) { return new BlurBench(p, REAL, SkBlurMaskFilter::kNormal_BlurStyle); } |
| 84 | static SkBenchmark* Fact21(void* p) { return new BlurBench(p, REAL, SkBlurMaskFilter::kSolid_BlurStyle); } |
| 85 | static SkBenchmark* Fact22(void* p) { return new BlurBench(p, REAL, SkBlurMaskFilter::kOuter_BlurStyle); } |
| 86 | static SkBenchmark* Fact23(void* p) { return new BlurBench(p, REAL, SkBlurMaskFilter::kInner_BlurStyle); } |
| 87 | |
reed@google.com | 25df888 | 2011-07-14 19:03:58 +0000 | [diff] [blame] | 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 | |
tomhudson@google.com | 8caac64 | 2011-11-22 15:58:06 +0000 | [diff] [blame] | 100 | static BenchRegistry gReg20(Fact20); |
| 101 | static BenchRegistry gReg21(Fact21); |
| 102 | static BenchRegistry gReg22(Fact22); |
| 103 | static BenchRegistry gReg23(Fact23); |
| 104 | |
reed@google.com | 25df888 | 2011-07-14 19:03:58 +0000 | [diff] [blame] | 105 | static BenchRegistry gRegNone(FactNone); |
| 106 | |