1 /* 2 * Copyright 2011 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 #include "Benchmark.h" 8 #include "SkBlurMask.h" 9 #include "SkBlurMaskFilter.h" 10 #include "SkCanvas.h" 11 #include "SkPaint.h" 12 #include "SkRandom.h" 13 #include "SkShader.h" 14 #include "SkString.h" 15 16 #define MINI 0.01f 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 Benchmark { 30 SkScalar fRadius; 31 SkBlurStyle fStyle; 32 uint32_t fFlags; 33 SkString fName; 34 35 public: BlurBench(SkScalar rad,SkBlurStyle bs,uint32_t flags=0)36 BlurBench(SkScalar rad, SkBlurStyle 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", SkScalarRoundToInt(rad), name, quality); 47 } 48 } 49 50 protected: onGetName()51 virtual const char* onGetName() { 52 return fName.c_str(); 53 } 54 onDraw(int loops,SkCanvas * canvas)55 virtual void onDraw(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 paint.setMaskFilter(SkBlurMaskFilter::Make(fStyle, 69 SkBlurMask::ConvertRadiusToSigma(fRadius), 70 fFlags)); 71 } 72 canvas->drawOval(r, paint); 73 } 74 } 75 76 private: 77 typedef Benchmark INHERITED; 78 }; 79 80 DEF_BENCH(return new BlurBench(MINI, kNormal_SkBlurStyle);) 81 DEF_BENCH(return new BlurBench(MINI, kSolid_SkBlurStyle);) 82 DEF_BENCH(return new BlurBench(MINI, kOuter_SkBlurStyle);) 83 DEF_BENCH(return new BlurBench(MINI, kInner_SkBlurStyle);) 84 85 DEF_BENCH(return new BlurBench(SMALL, kNormal_SkBlurStyle);) 86 DEF_BENCH(return new BlurBench(SMALL, kSolid_SkBlurStyle);) 87 DEF_BENCH(return new BlurBench(SMALL, kOuter_SkBlurStyle);) 88 DEF_BENCH(return new BlurBench(SMALL, kInner_SkBlurStyle);) 89 90 DEF_BENCH(return new BlurBench(BIG, kNormal_SkBlurStyle);) 91 DEF_BENCH(return new BlurBench(BIG, kSolid_SkBlurStyle);) 92 DEF_BENCH(return new BlurBench(BIG, kOuter_SkBlurStyle);) 93 DEF_BENCH(return new BlurBench(BIG, kInner_SkBlurStyle);) 94 95 DEF_BENCH(return new BlurBench(REALBIG, kNormal_SkBlurStyle);) 96 DEF_BENCH(return new BlurBench(REALBIG, kSolid_SkBlurStyle);) 97 DEF_BENCH(return new BlurBench(REALBIG, kOuter_SkBlurStyle);) 98 DEF_BENCH(return new BlurBench(REALBIG, kInner_SkBlurStyle);) 99 100 DEF_BENCH(return new BlurBench(REAL, kNormal_SkBlurStyle);) 101 DEF_BENCH(return new BlurBench(REAL, kSolid_SkBlurStyle);) 102 DEF_BENCH(return new BlurBench(REAL, kOuter_SkBlurStyle);) 103 DEF_BENCH(return new BlurBench(REAL, kInner_SkBlurStyle);) 104 105 DEF_BENCH(return new BlurBench(MINI, kNormal_SkBlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);) 106 107 DEF_BENCH(return new BlurBench(SMALL, kNormal_SkBlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);) 108 109 DEF_BENCH(return new BlurBench(BIG, kNormal_SkBlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);) 110 111 DEF_BENCH(return new BlurBench(REALBIG, kNormal_SkBlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);) 112 113 DEF_BENCH(return new BlurBench(REAL, kNormal_SkBlurStyle, SkBlurMaskFilter::kHighQuality_BlurFlag);) 114 115 DEF_BENCH(return new BlurBench(0, kNormal_SkBlurStyle);) 116