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 | */ |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 7 | #include "bench/Benchmark.h" |
| 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkPaint.h" |
| 10 | #include "include/core/SkString.h" |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 11 | #include "include/core/SkTileMode.h" |
| 12 | #include "include/effects/SkImageFilters.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/utils/SkRandom.h" |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 14 | |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 15 | #include "tools/ToolUtils.h" |
mtklein | 20efb41 | 2016-04-12 15:52:52 -0700 | [diff] [blame] | 16 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 17 | class MatrixConvolutionBench : public Benchmark { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 18 | public: |
Adlai Holler | 6f994a8 | 2020-05-01 15:41:55 -0400 | [diff] [blame] | 19 | MatrixConvolutionBench(bool bigKernel, SkTileMode tileMode, bool convolveAlpha) |
| 20 | : fName(SkStringPrintf("matrixconvolution_%s%s%s", |
| 21 | bigKernel ? "bigKernel_" : "", |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 22 | ToolUtils::tilemode_name(tileMode), |
mtklein | 20efb41 | 2016-04-12 15:52:52 -0700 | [diff] [blame] | 23 | convolveAlpha ? "" : "_noConvolveAlpha")) { |
Adlai Holler | 6f994a8 | 2020-05-01 15:41:55 -0400 | [diff] [blame] | 24 | if (bigKernel) { |
| 25 | SkISize kernelSize = SkISize::Make(9, 9); |
| 26 | SkScalar kernel[81]; |
| 27 | for (int i = 0; i < 81; i++) { |
| 28 | kernel[i] = SkIntToScalar(1); |
| 29 | } |
| 30 | kernel[40] = SkIntToScalar(-79); |
| 31 | SkScalar gain = 0.3f, bias = SkIntToScalar(100); |
| 32 | SkIPoint kernelOffset = SkIPoint::Make(4, 4); |
| 33 | fFilter = SkImageFilters::MatrixConvolution(kernelSize, kernel, gain, bias, |
| 34 | kernelOffset, tileMode, convolveAlpha, |
| 35 | nullptr); |
| 36 | } else { |
| 37 | SkISize kernelSize = SkISize::Make(3, 3); |
| 38 | SkScalar kernel[9] = { |
| 39 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 40 | SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1), |
| 41 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 42 | }; |
| 43 | SkScalar gain = 0.3f, bias = SkIntToScalar(100); |
| 44 | SkIPoint kernelOffset = SkIPoint::Make(1, 1); |
| 45 | fFilter = SkImageFilters::MatrixConvolution(kernelSize, kernel, gain, bias, |
| 46 | kernelOffset, tileMode, convolveAlpha, |
| 47 | nullptr); |
| 48 | } |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | protected: |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 52 | const char* onGetName() override { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 53 | return fName.c_str(); |
| 54 | } |
| 55 | |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 56 | void onDraw(int loops, SkCanvas* canvas) override { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 57 | SkPaint paint; |
| 58 | this->setupPaint(&paint); |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 59 | paint.setImageFilter(fFilter); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 60 | paint.setAntiAlias(true); |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 61 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 62 | SkRandom rand; |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 63 | for (int i = 0; i < loops; i++) { |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 64 | SkRect r = SkRect::MakeWH(rand.nextUScalar1() * 400, |
| 65 | rand.nextUScalar1() * 400); |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 66 | canvas->drawOval(r, paint); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | private: |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 71 | sk_sp<SkImageFilter> fFilter; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 72 | SkString fName; |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 73 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 74 | using INHERITED = Benchmark; |
senorblanco@chromium.org | 5faa2dc | 2012-09-18 20:32:34 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Adlai Holler | 6f994a8 | 2020-05-01 15:41:55 -0400 | [diff] [blame] | 77 | DEF_BENCH( return new MatrixConvolutionBench(false, SkTileMode::kClamp, true); ) |
| 78 | DEF_BENCH( return new MatrixConvolutionBench(false, SkTileMode::kRepeat, true); ) |
| 79 | DEF_BENCH( return new MatrixConvolutionBench(false, SkTileMode::kMirror, true); ) |
| 80 | DEF_BENCH( return new MatrixConvolutionBench(false, SkTileMode::kDecal, true); ) |
| 81 | DEF_BENCH( return new MatrixConvolutionBench(false, SkTileMode::kDecal, false); ) |
| 82 | |
| 83 | DEF_BENCH( return new MatrixConvolutionBench(true, SkTileMode::kClamp, true); ) |
| 84 | DEF_BENCH( return new MatrixConvolutionBench(true, SkTileMode::kRepeat, true); ) |
| 85 | DEF_BENCH( return new MatrixConvolutionBench(true, SkTileMode::kMirror, true); ) |
| 86 | DEF_BENCH( return new MatrixConvolutionBench(true, SkTileMode::kDecal, true); ) |
| 87 | DEF_BENCH( return new MatrixConvolutionBench(true, SkTileMode::kDecal, false); ) |