1 /* 2 * Copyright 2013 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 8 #include "Benchmark.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 16 #define SMALL SkIntToScalar(2) 17 #define REAL 1.5f 18 static const SkScalar kMedium = SkIntToScalar(5); 19 #define BIG SkIntToScalar(10) 20 static const SkScalar kMedBig = SkIntToScalar(20); 21 #define REALBIG 30.5f 22 23 class BlurRectBench: public Benchmark { 24 int fLoopCount; 25 SkScalar fRadius; 26 SkString fName; 27 28 public: BlurRectBench(SkScalar rad)29 BlurRectBench(SkScalar rad) { 30 fRadius = rad; 31 32 if (fRadius > SkIntToScalar(25)) { 33 fLoopCount = 100; 34 } else if (fRadius > SkIntToScalar(5)) { 35 fLoopCount = 1000; 36 } else { 37 fLoopCount = 10000; 38 } 39 } 40 41 protected: onGetName()42 virtual const char* onGetName() { 43 return fName.c_str(); 44 } 45 radius() const46 SkScalar radius() const { 47 return fRadius; 48 } 49 setName(const SkString & name)50 void setName(const SkString& name) { 51 fName = name; 52 } 53 onDraw(const int loops,SkCanvas *)54 virtual void onDraw(const int loops, SkCanvas*) { 55 SkPaint paint; 56 this->setupPaint(&paint); 57 58 paint.setAntiAlias(true); 59 60 SkScalar pad = fRadius*3/2 + SK_Scalar1; 61 SkRect r = SkRect::MakeWH(2 * pad + SK_Scalar1, 2 * pad + SK_Scalar1); 62 63 preBenchSetup(r); 64 65 for (int i = 0; i < loops; i++) { 66 makeBlurryRect(r); 67 } 68 } 69 70 virtual void makeBlurryRect(const SkRect&) = 0; preBenchSetup(const SkRect &)71 virtual void preBenchSetup(const SkRect&) {} 72 private: 73 typedef Benchmark INHERITED; 74 }; 75 76 77 class BlurRectDirectBench: public BlurRectBench { 78 public: BlurRectDirectBench(SkScalar rad)79 BlurRectDirectBench(SkScalar rad) : INHERITED(rad) { 80 SkString name; 81 82 if (SkScalarFraction(rad) != 0) { 83 name.printf("blurrect_direct_%.2f", SkScalarToFloat(rad)); 84 } else { 85 name.printf("blurrect_direct_%d", SkScalarRoundToInt(rad)); 86 } 87 88 this->setName(name); 89 } 90 protected: makeBlurryRect(const SkRect & r)91 void makeBlurryRect(const SkRect& r) override { 92 SkMask mask; 93 SkBlurMask::BlurRect(SkBlurMask::ConvertRadiusToSigma(this->radius()), 94 &mask, r, kNormal_SkBlurStyle); 95 SkMask::FreeImage(mask.fImage); 96 } 97 private: 98 typedef BlurRectBench INHERITED; 99 }; 100 101 class BlurRectSeparableBench: public BlurRectBench { 102 103 public: BlurRectSeparableBench(SkScalar rad)104 BlurRectSeparableBench(SkScalar rad) : INHERITED(rad) { 105 fSrcMask.fImage = NULL; 106 } 107 ~BlurRectSeparableBench()108 ~BlurRectSeparableBench() { 109 SkMask::FreeImage(fSrcMask.fImage); 110 } 111 112 protected: preBenchSetup(const SkRect & r)113 void preBenchSetup(const SkRect& r) override { 114 SkMask::FreeImage(fSrcMask.fImage); 115 116 r.roundOut(&fSrcMask.fBounds); 117 fSrcMask.fFormat = SkMask::kA8_Format; 118 fSrcMask.fRowBytes = fSrcMask.fBounds.width(); 119 fSrcMask.fImage = SkMask::AllocImage(fSrcMask.computeTotalImageSize()); 120 121 memset(fSrcMask.fImage, 0xff, fSrcMask.computeTotalImageSize()); 122 } 123 124 SkMask fSrcMask; 125 private: 126 typedef BlurRectBench INHERITED; 127 }; 128 129 class BlurRectBoxFilterBench: public BlurRectSeparableBench { 130 public: BlurRectBoxFilterBench(SkScalar rad)131 BlurRectBoxFilterBench(SkScalar rad) : INHERITED(rad) { 132 SkString name; 133 134 if (SkScalarFraction(rad) != 0) { 135 name.printf("blurrect_boxfilter_%.2f", SkScalarToFloat(rad)); 136 } else { 137 name.printf("blurrect_boxfilter_%d", SkScalarRoundToInt(rad)); 138 } 139 140 this->setName(name); 141 } 142 143 protected: 144 makeBlurryRect(const SkRect &)145 void makeBlurryRect(const SkRect&) override { 146 SkMask mask; 147 mask.fImage = NULL; 148 SkBlurMask::BoxBlur(&mask, fSrcMask, SkBlurMask::ConvertRadiusToSigma(this->radius()), 149 kNormal_SkBlurStyle, kHigh_SkBlurQuality); 150 SkMask::FreeImage(mask.fImage); 151 } 152 private: 153 typedef BlurRectSeparableBench INHERITED; 154 }; 155 156 class BlurRectGaussianBench: public BlurRectSeparableBench { 157 public: BlurRectGaussianBench(SkScalar rad)158 BlurRectGaussianBench(SkScalar rad) : INHERITED(rad) { 159 SkString name; 160 161 if (SkScalarFraction(rad) != 0) { 162 name.printf("blurrect_gaussian_%.2f", SkScalarToFloat(rad)); 163 } else { 164 name.printf("blurrect_gaussian_%d", SkScalarRoundToInt(rad)); 165 } 166 167 this->setName(name); 168 } 169 170 protected: 171 makeBlurryRect(const SkRect &)172 void makeBlurryRect(const SkRect&) override { 173 SkMask mask; 174 mask.fImage = NULL; 175 SkBlurMask::BlurGroundTruth(SkBlurMask::ConvertRadiusToSigma(this->radius()), 176 &mask, fSrcMask, kNormal_SkBlurStyle); 177 SkMask::FreeImage(mask.fImage); 178 } 179 private: 180 typedef BlurRectSeparableBench INHERITED; 181 }; 182 183 DEF_BENCH(return new BlurRectBoxFilterBench(SMALL);) 184 DEF_BENCH(return new BlurRectBoxFilterBench(BIG);) 185 DEF_BENCH(return new BlurRectBoxFilterBench(REALBIG);) 186 DEF_BENCH(return new BlurRectBoxFilterBench(REAL);) 187 DEF_BENCH(return new BlurRectGaussianBench(SMALL);) 188 DEF_BENCH(return new BlurRectGaussianBench(BIG);) 189 DEF_BENCH(return new BlurRectGaussianBench(REALBIG);) 190 DEF_BENCH(return new BlurRectGaussianBench(REAL);) 191 DEF_BENCH(return new BlurRectDirectBench(SMALL);) 192 DEF_BENCH(return new BlurRectDirectBench(BIG);) 193 DEF_BENCH(return new BlurRectDirectBench(REALBIG);) 194 DEF_BENCH(return new BlurRectDirectBench(REAL);) 195 196 DEF_BENCH(return new BlurRectDirectBench(kMedium);) 197 DEF_BENCH(return new BlurRectDirectBench(kMedBig);) 198 199 DEF_BENCH(return new BlurRectBoxFilterBench(kMedium);) 200 DEF_BENCH(return new BlurRectBoxFilterBench(kMedBig);) 201 202 #if 0 203 // disable Gaussian benchmarks; the algorithm works well enough 204 // and serves as a baseline for ground truth, but it's too slow 205 // to use in production for non-trivial radii, so no real point 206 // in having the bots benchmark it all the time. 207 208 DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(1));) 209 DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(2));) 210 DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(3));) 211 DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(4));) 212 DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(5));) 213 DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(6));) 214 DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(7));) 215 DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(8));) 216 DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(9));) 217 DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(10));) 218 DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(11));) 219 DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(12));) 220 DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(13));) 221 DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(14));) 222 DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(15));) 223 DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(16));) 224 DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(17));) 225 DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(18));) 226 DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(19));) 227 DEF_BENCH(return new BlurRectGaussianBench(SkIntToScalar(20));) 228 #endif 229