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 #include "bench/Benchmark.h" 8 #include "include/core/SkCanvas.h" 9 #include "include/core/SkPaint.h" 10 #include "include/core/SkString.h" 11 #include "include/core/SkTileMode.h" 12 #include "include/effects/SkImageFilters.h" 13 #include "include/utils/SkRandom.h" 14 15 #include "tools/ToolUtils.h" 16 17 class MatrixConvolutionBench : public Benchmark { 18 public: MatrixConvolutionBench(SkTileMode tileMode,bool convolveAlpha)19 MatrixConvolutionBench(SkTileMode tileMode, bool convolveAlpha) 20 : fName(SkStringPrintf("matrixconvolution_%s%s", 21 ToolUtils::tilemode_name(tileMode), 22 convolveAlpha ? "" : "_noConvolveAlpha")) { 23 SkISize kernelSize = SkISize::Make(3, 3); 24 SkScalar kernel[9] = { 25 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), 26 SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1), 27 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), 28 }; 29 SkScalar gain = 0.3f, bias = SkIntToScalar(100); 30 SkIPoint kernelOffset = SkIPoint::Make(1, 1); 31 fFilter = SkImageFilters::MatrixConvolution(kernelSize, kernel, gain, bias, 32 kernelOffset, tileMode, convolveAlpha, nullptr); 33 } 34 35 protected: onGetName()36 const char* onGetName() override { 37 return fName.c_str(); 38 } 39 onDraw(int loops,SkCanvas * canvas)40 void onDraw(int loops, SkCanvas* canvas) override { 41 SkPaint paint; 42 this->setupPaint(&paint); 43 paint.setImageFilter(fFilter); 44 paint.setAntiAlias(true); 45 46 SkRandom rand; 47 for (int i = 0; i < loops; i++) { 48 SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400, 49 rand.nextUScalar1() * 400); 50 canvas->drawOval(r, paint); 51 } 52 } 53 54 private: 55 sk_sp<SkImageFilter> fFilter; 56 SkString fName; 57 58 typedef Benchmark INHERITED; 59 }; 60 61 DEF_BENCH( return new MatrixConvolutionBench(SkTileMode::kClamp, true); ) 62 DEF_BENCH( return new MatrixConvolutionBench(SkTileMode::kRepeat, true); ) 63 DEF_BENCH( return new MatrixConvolutionBench(SkTileMode::kMirror, true); ) 64 DEF_BENCH( return new MatrixConvolutionBench(SkTileMode::kDecal, true); ) 65 DEF_BENCH( return new MatrixConvolutionBench(SkTileMode::kDecal, false); ) 66