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 | |
mtklein | 20efb41 | 2016-04-12 15:52:52 -0700 | [diff] [blame] | 14 | static const char* name(SkMatrixConvolutionImageFilter::TileMode mode) { |
| 15 | switch (mode) { |
| 16 | case SkMatrixConvolutionImageFilter::kClamp_TileMode: return "clamp"; |
| 17 | case SkMatrixConvolutionImageFilter::kRepeat_TileMode: return "repeat"; |
| 18 | case SkMatrixConvolutionImageFilter::kClampToBlack_TileMode: return "clampToBlack"; |
| 19 | } |
| 20 | return "oops"; |
| 21 | } |
| 22 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 23 | class MatrixConvolutionBench : public Benchmark { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 24 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 25 | MatrixConvolutionBench(SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAlpha) |
mtklein | 20efb41 | 2016-04-12 15:52:52 -0700 | [diff] [blame] | 26 | : fName(SkStringPrintf("matrixconvolution_%s%s", |
| 27 | name(tileMode), |
| 28 | convolveAlpha ? "" : "_noConvolveAlpha")) { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 29 | SkISize kernelSize = SkISize::Make(3, 3); |
| 30 | SkScalar kernel[9] = { |
| 31 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 32 | SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1), |
| 33 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 34 | }; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 35 | SkScalar gain = 0.3f, bias = SkIntToScalar(100); |
commit-bot@chromium.org | 84cd099 | 2014-03-12 16:36:08 +0000 | [diff] [blame] | 36 | SkIPoint kernelOffset = SkIPoint::Make(1, 1); |
mtklein | 20efb41 | 2016-04-12 15:52:52 -0700 | [diff] [blame] | 37 | fFilter = SkMatrixConvolutionImageFilter::Make(kernelSize, kernel, gain, bias, |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 38 | kernelOffset, tileMode, convolveAlpha, |
| 39 | nullptr); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | protected: |
| 43 | virtual const char* onGetName() { |
| 44 | return fName.c_str(); |
| 45 | } |
| 46 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 47 | virtual void onDraw(int loops, SkCanvas* canvas) { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 48 | SkPaint paint; |
| 49 | this->setupPaint(&paint); |
| 50 | paint.setAntiAlias(true); |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 51 | SkRandom rand; |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 52 | for (int i = 0; i < loops; i++) { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 53 | SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400, |
| 54 | rand.nextUScalar1() * 400); |
| 55 | paint.setImageFilter(fFilter); |
| 56 | canvas->drawOval(r, paint); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | private: |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 61 | sk_sp<SkImageFilter> fFilter; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 62 | SkString fName; |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 63 | |
| 64 | typedef Benchmark INHERITED; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 67 | DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kClamp_TileMode, true); ) |
| 68 | DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kRepeat_TileMode, true); ) |
| 69 | DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kClampToBlack_TileMode, true); ) |
| 70 | DEF_BENCH( return new MatrixConvolutionBench(SkMatrixConvolutionImageFilter::kClampToBlack_TileMode, false); ) |