senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 9 | #include "SkBlurImageFilter.h" |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 10 | #include "SkDisplacementMapEffect.h" |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 12 | #include "SkMergeImageFilter.h" |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 13 | |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 14 | |
| 15 | // Exercise a blur filter connected to 5 inputs of the same merge filter. |
| 16 | // This bench shows an improvement in performance once cacheing of re-used |
| 17 | // nodes is implemented, since the DAG is no longer flattened to a tree. |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 18 | class ImageFilterDAGBench : public Benchmark { |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 19 | public: |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 20 | ImageFilterDAGBench() {} |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 21 | |
| 22 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 23 | const char* onGetName() override { |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 24 | return "image_filter_dag"; |
| 25 | } |
| 26 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 27 | void onDraw(int loops, SkCanvas* canvas) override { |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 28 | const SkRect rect = SkRect::Make(SkIRect::MakeWH(400, 400)); |
| 29 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 30 | for (int j = 0; j < loops; j++) { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 31 | sk_sp<SkImageFilter> blur(SkBlurImageFilter::Make(20.0f, 20.0f, nullptr)); |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 32 | sk_sp<SkImageFilter> inputs[kNumInputs]; |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 33 | for (int i = 0; i < kNumInputs; ++i) { |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 34 | inputs[i] = blur; |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 35 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 36 | SkPaint paint; |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 37 | paint.setImageFilter(SkMergeImageFilter::Make(inputs, kNumInputs)); |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 38 | canvas->drawRect(rect, paint); |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 39 | } |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | private: |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 43 | static const int kNumInputs = 5; |
| 44 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 45 | typedef Benchmark INHERITED; |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 48 | // Exercise a blur filter connected to both inputs of an SkDisplacementMapEffect. |
| 49 | |
| 50 | class ImageFilterDisplacedBlur : public Benchmark { |
| 51 | public: |
| 52 | ImageFilterDisplacedBlur() {} |
| 53 | |
| 54 | protected: |
| 55 | const char* onGetName() override { |
| 56 | return "image_filter_displaced_blur"; |
| 57 | } |
| 58 | |
| 59 | void onDraw(int loops, SkCanvas* canvas) override { |
| 60 | for (int j = 0; j < loops; j++) { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 61 | sk_sp<SkImageFilter> blur(SkBlurImageFilter::Make(4.0f, 4.0f, nullptr)); |
| 62 | auto xSelector = SkDisplacementMapEffect::kR_ChannelSelectorType; |
| 63 | auto ySelector = SkDisplacementMapEffect::kB_ChannelSelectorType; |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 64 | SkScalar scale = 2; |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame^] | 65 | |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 66 | SkPaint paint; |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame^] | 67 | paint.setImageFilter(SkDisplacementMapEffect::Make(xSelector, ySelector, scale, |
| 68 | blur, blur)); |
| 69 | |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 70 | SkRect rect = SkRect::Make(SkIRect::MakeWH(400, 400)); |
| 71 | canvas->drawRect(rect, paint); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | typedef Benchmark INHERITED; |
| 77 | }; |
| 78 | |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 79 | DEF_BENCH(return new ImageFilterDAGBench;) |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 80 | DEF_BENCH(return new ImageFilterDisplacedBlur;) |