senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 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 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 7 | #include "Benchmark.h" |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 8 | #include "SkCanvas.h" |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 9 | #include "SkMatrixConvolutionImageFilter.h" |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 10 | #include "SkPaint.h" |
| 11 | #include "SkRandom.h" |
| 12 | #include "SkString.h" |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 13 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 14 | class MatrixConvolutionBench : public Benchmark { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 15 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 16 | MatrixConvolutionBench(SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAlpha) |
| 17 | : fName("matrixconvolution") { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 18 | SkISize kernelSize = SkISize::Make(3, 3); |
| 19 | SkScalar kernel[9] = { |
| 20 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 21 | SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1), |
| 22 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 23 | }; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 24 | SkScalar gain = 0.3f, bias = SkIntToScalar(100); |
commit-bot@chromium.org | 84cd099 | 2014-03-12 16:36:08 +0000 | [diff] [blame] | 25 | SkIPoint kernelOffset = SkIPoint::Make(1, 1); |
| 26 | fFilter = SkMatrixConvolutionImageFilter::Create(kernelSize, kernel, gain, bias, kernelOffset, tileMode, convolveAlpha); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | ~MatrixConvolutionBench() { |
| 30 | fFilter->unref(); |
| 31 | } |
| 32 | |
| 33 | protected: |
| 34 | virtual const char* onGetName() { |
| 35 | return fName.c_str(); |
| 36 | } |
| 37 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 38 | virtual void onDraw(const int loops, SkCanvas* canvas) { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 39 | SkPaint paint; |
| 40 | this->setupPaint(&paint); |
| 41 | paint.setAntiAlias(true); |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 42 | SkRandom rand; |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 43 | for (int i = 0; i < loops; i++) { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 44 | SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400, |
| 45 | rand.nextUScalar1() * 400); |
| 46 | paint.setImageFilter(fFilter); |
| 47 | canvas->drawOval(r, paint); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 52 | typedef Benchmark INHERITED; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 53 | SkMatrixConvolutionImageFilter* fFilter; |
| 54 | SkString fName; |
| 55 | }; |
| 56 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 57 | DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kClamp_TileMode, true); ) |
| 58 | DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kRepeat_TileMode, true); ) |
| 59 | DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kClampToBlack_TileMode, true); ) |
| 60 | DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kClampToBlack_TileMode, false); ) |