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 "SkBlurMask.h" 10 #include "SkCanvas.h" 11 #include "SkPaint.h" 12 #include "SkRandom.h" 13 #include "SkShader.h" 14 #include "SkString.h" 15 #include "SkBlurMaskFilter.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 SkBenchmark { 30 SkScalar fRadius; 31 SkBlurMaskFilter::BlurStyle fStyle; 32 uint32_t fFlags; 33 SkString fName; 34 35 public: 36 BlurBench(SkScalar rad, SkBlurMaskFilter::BlurStyle 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", SkScalarRound(rad), name, quality); 47 } 48 } 49 50 protected: 51 virtual const char* onGetName() { 52 return fName.c_str(); 53 } 54 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 SkBenchmark INHERITED; 79 }; 80 81 DEF_BENCH(return new BlurBench(SMALL, SkBlurMaskFilter::kNormal_BlurStyle);) 82 DEF_BENCH(return new BlurBench(SMALL, SkBlurMaskFilter::kSolid_BlurStyle);) 83 DEF_BENCH(return new BlurBench(SMALL, SkBlurMaskFilter::kOuter_BlurStyle);) 84 DEF_BENCH(return new BlurBench(SMALL, SkBlurMaskFilter::kInner_BlurStyle);) 85 86 DEF_BENCH(return new BlurBench(BIG, SkBlurMaskFilter::kNormal_BlurStyle);) 87 DEF_BENCH(return new BlurBench(BIG, SkBlurMaskFilter::kSolid_BlurStyle);) 88 DEF_BENCH(return new BlurBench(BIG, SkBlurMaskFilter::kOuter_BlurStyle);) 89 DEF_BENCH(return new BlurBench(BIG, SkBlurMaskFilter::kInner_BlurStyle);) 90 91 DEF_BENCH(return new BlurBench(REALBIG, SkBlurMaskFilter::kNormal_BlurStyle);) 92 DEF_BENCH(return new BlurBench(REALBIG, SkBlurMaskFilter::kSolid_BlurStyle);) 93 DEF_BENCH(return new BlurBench(REALBIG, SkBlurMaskFilter::kOuter_BlurStyle);) 94 DEF_BENCH(return new BlurBench(REALBIG, SkBlurMaskFilter::kInner_BlurStyle);) 95 96 DEF_BENCH(return new BlurBench(REAL, SkBlurMaskFilter::kNormal_BlurStyle);) 97 DEF_BENCH(return new BlurBench(REAL, SkBlurMaskFilter::kSolid_BlurStyle);) 98 DEF_BENCH(return new BlurBench(REAL, SkBlurMaskFilter::kOuter_BlurStyle);) 99 DEF_BENCH(return new BlurBench(REAL, SkBlurMaskFilter::kInner_BlurStyle);) 100 101 DEF_BENCH(return new BlurBench(SMALL, SkBlurMaskFilter::kNormal_BlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);) 102 103 DEF_BENCH(return new BlurBench(BIG, SkBlurMaskFilter::kNormal_BlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);) 104 105 DEF_BENCH(return new BlurBench(REALBIG, SkBlurMaskFilter::kNormal_BlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);) 106 107 DEF_BENCH(return new BlurBench(REAL, SkBlurMaskFilter::kNormal_BlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);) 108 109 DEF_BENCH(return new BlurBench(0, SkBlurMaskFilter::kNormal_BlurStyle);) 110