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 "SkBenchmark.h" 8 #include "SkCanvas.h" 9 #include "SkPaint.h" 10 #include "SkRandom.h" 11 #include "SkString.h" 12 #include "SkMatrixConvolutionImageFilter.h" 13 14 class MatrixConvolutionBench : public SkBenchmark { 15 SkMatrixConvolutionImageFilter::TileMode fTileMode; 16 17 public: 18 MatrixConvolutionBench(void* param, SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAlpha) 19 : INHERITED(param), fName("matrixconvolution") { 20 SkISize kernelSize = SkISize::Make(3, 3); 21 SkScalar kernel[9] = { 22 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), 23 SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1), 24 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), 25 }; 26 SkScalar gain = SkFloatToScalar(0.3f), bias = SkIntToScalar(100); 27 SkIPoint target = SkIPoint::Make(1, 1); 28 fFilter = new SkMatrixConvolutionImageFilter(kernelSize, kernel, gain, bias, target, tileMode, convolveAlpha); 29 } 30 31 ~MatrixConvolutionBench() { 32 fFilter->unref(); 33 } 34 35 protected: 36 virtual const char* onGetName() { 37 return fName.c_str(); 38 } 39 40 virtual void onDraw(SkCanvas* canvas) { 41 SkPaint paint; 42 this->setupPaint(&paint); 43 paint.setAntiAlias(true); 44 SkRandom rand; 45 for (int i = 0; i < SkBENCHLOOP(3); i++) { 46 SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400, 47 rand.nextUScalar1() * 400); 48 paint.setImageFilter(fFilter); 49 canvas->drawOval(r, paint); 50 } 51 } 52 53 private: 54 typedef SkBenchmark INHERITED; 55 SkMatrixConvolutionImageFilter* fFilter; 56 SkString fName; 57 }; 58 59 static SkBenchmark* Fact00(void* p) { return new MatrixConvolutionBench(p, SkMatrixConvolutionImageFilter::kClamp_TileMode, true); } 60 static SkBenchmark* Fact01(void* p) { return new MatrixConvolutionBench(p, SkMatrixConvolutionImageFilter::kRepeat_TileMode, true); } 61 static SkBenchmark* Fact02(void* p) { return new MatrixConvolutionBench(p, SkMatrixConvolutionImageFilter::kClampToBlack_TileMode, true); } 62 static SkBenchmark* Fact03(void* p) { return new MatrixConvolutionBench(p, SkMatrixConvolutionImageFilter::kClampToBlack_TileMode, false); } 63 64 static BenchRegistry gReg00(Fact00); 65 static BenchRegistry gReg01(Fact01); 66 static BenchRegistry gReg02(Fact02); 67 static BenchRegistry gReg03(Fact03); 68