1 /* 2 * Copyright 2012 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 "SkCanvas.h" 10 #include "SkMorphologyImageFilter.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 #define BIG SkIntToScalar(10) 19 20 enum MorphologyType { 21 kErode_MT, 22 kDilate_MT 23 }; 24 25 static const char* gStyleName[] = { 26 "erode", 27 "dilate" 28 }; 29 30 class MorphologyBench : public Benchmark { 31 SkScalar fRadius; 32 MorphologyType fStyle; 33 SkString fName; 34 35 public: MorphologyBench(SkScalar rad,MorphologyType style)36 MorphologyBench(SkScalar rad, MorphologyType style) 37 { 38 fRadius = rad; 39 fStyle = style; 40 const char* name = rad > 0 ? gStyleName[style] : "none"; 41 if (SkScalarFraction(rad) != 0) { 42 fName.printf("morph_%.2f_%s", SkScalarToFloat(rad), name); 43 } else { 44 fName.printf("morph_%d_%s", SkScalarRoundToInt(rad), name); 45 } 46 } 47 48 protected: onGetName()49 const char* onGetName() override { 50 return fName.c_str(); 51 } 52 onDraw(int loops,SkCanvas * canvas)53 void onDraw(int loops, SkCanvas* canvas) override { 54 SkPaint paint; 55 this->setupPaint(&paint); 56 57 paint.setAntiAlias(true); 58 59 SkRandom rand; 60 for (int i = 0; i < loops; i++) { 61 SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400, 62 rand.nextUScalar1() * 400); 63 r.offset(fRadius, fRadius); 64 65 if (fRadius > 0) { 66 sk_sp<SkImageFilter> mf; 67 switch (fStyle) { 68 case kDilate_MT: 69 mf = SkDilateImageFilter::Make(SkScalarFloorToInt(fRadius), 70 SkScalarFloorToInt(fRadius), 71 nullptr); 72 break; 73 case kErode_MT: 74 mf = SkErodeImageFilter::Make(SkScalarFloorToInt(fRadius), 75 SkScalarFloorToInt(fRadius), 76 nullptr); 77 break; 78 } 79 paint.setImageFilter(std::move(mf)); 80 } 81 canvas->drawOval(r, paint); 82 } 83 } 84 85 private: 86 typedef Benchmark INHERITED; 87 }; 88 89 DEF_BENCH( return new MorphologyBench(SMALL, kErode_MT); ) 90 DEF_BENCH( return new MorphologyBench(SMALL, kDilate_MT); ) 91 92 DEF_BENCH( return new MorphologyBench(BIG, kErode_MT); ) 93 DEF_BENCH( return new MorphologyBench(BIG, kDilate_MT); ) 94 95 DEF_BENCH( return new MorphologyBench(REAL, kErode_MT); ) 96 DEF_BENCH( return new MorphologyBench(REAL, kDilate_MT); ) 97 98 DEF_BENCH( return new MorphologyBench(0, kErode_MT); ) 99