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" |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 9 | #include "Resources.h" |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 10 | #include "SkBlurImageFilter.h" |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 11 | #include "SkDisplacementMapEffect.h" |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 12 | #include "SkCanvas.h" |
Florin Malita | ab244f0 | 2017-05-03 19:16:58 +0000 | [diff] [blame^] | 13 | #include "SkImage.h" |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 14 | #include "SkMergeImageFilter.h" |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 15 | |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 16 | |
| 17 | // Exercise a blur filter connected to 5 inputs of the same merge filter. |
| 18 | // This bench shows an improvement in performance once cacheing of re-used |
| 19 | // nodes is implemented, since the DAG is no longer flattened to a tree. |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 20 | class ImageFilterDAGBench : public Benchmark { |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 21 | public: |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 22 | ImageFilterDAGBench() {} |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 23 | |
| 24 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 25 | const char* onGetName() override { |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 26 | return "image_filter_dag"; |
| 27 | } |
| 28 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 29 | void onDraw(int loops, SkCanvas* canvas) override { |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 30 | const SkRect rect = SkRect::Make(SkIRect::MakeWH(400, 400)); |
| 31 | |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 32 | for (int j = 0; j < loops; j++) { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 33 | sk_sp<SkImageFilter> blur(SkBlurImageFilter::Make(20.0f, 20.0f, nullptr)); |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 34 | sk_sp<SkImageFilter> inputs[kNumInputs]; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 35 | SkBlendMode modes[kNumInputs]; |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 36 | for (int i = 0; i < kNumInputs; ++i) { |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 37 | inputs[i] = blur; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 38 | modes[i] = SkBlendMode::kSrcOver; |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 39 | } |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 40 | SkPaint paint; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 41 | paint.setImageFilter(SkMergeImageFilter::MakeN(inputs, kNumInputs, modes)); |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 42 | canvas->drawRect(rect, paint); |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 43 | } |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | private: |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 47 | static const int kNumInputs = 5; |
| 48 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 49 | typedef Benchmark INHERITED; |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 52 | class ImageMakeWithFilterDAGBench : public Benchmark { |
| 53 | public: |
| 54 | ImageMakeWithFilterDAGBench() {} |
| 55 | |
| 56 | protected: |
| 57 | const char* onGetName() override { |
| 58 | return "image_make_with_filter_dag"; |
| 59 | } |
| 60 | |
| 61 | void onDelayedSetup() override { |
| 62 | fImage = GetResourceAsImage("mandrill_512.png"); |
| 63 | } |
| 64 | |
| 65 | void onDraw(int loops, SkCanvas* canvas) override { |
| 66 | SkIRect subset = SkIRect::MakeSize(fImage->dimensions()); |
| 67 | SkIPoint offset = SkIPoint::Make(0, 0); |
| 68 | SkIRect discardSubset; |
| 69 | sk_sp<SkImage> image = fImage; |
| 70 | |
| 71 | for (int j = 0; j < loops; j++) { |
| 72 | sk_sp<SkImageFilter> blur(SkBlurImageFilter::Make(20.0f, 20.0f, nullptr)); |
| 73 | sk_sp<SkImageFilter> inputs[kNumInputs]; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 74 | SkBlendMode modes[kNumInputs]; |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 75 | for (int i = 0; i < kNumInputs; ++i) { |
| 76 | inputs[i] = blur; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 77 | modes[i] = SkBlendMode::kSrcOver; |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 78 | } |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 79 | sk_sp<SkImageFilter> mergeFilter = SkMergeImageFilter::MakeN(inputs, kNumInputs, modes); |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 80 | image = image->makeWithFilter(mergeFilter.get(), subset, subset, &discardSubset, |
| 81 | &offset); |
| 82 | SkASSERT(image && image->dimensions() == fImage->dimensions()); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | static const int kNumInputs = 5; |
| 88 | sk_sp<SkImage> fImage; |
| 89 | |
| 90 | typedef Benchmark INHERITED; |
| 91 | }; |
| 92 | |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 93 | // Exercise a blur filter connected to both inputs of an SkDisplacementMapEffect. |
| 94 | |
| 95 | class ImageFilterDisplacedBlur : public Benchmark { |
| 96 | public: |
| 97 | ImageFilterDisplacedBlur() {} |
| 98 | |
| 99 | protected: |
| 100 | const char* onGetName() override { |
| 101 | return "image_filter_displaced_blur"; |
| 102 | } |
| 103 | |
| 104 | void onDraw(int loops, SkCanvas* canvas) override { |
| 105 | for (int j = 0; j < loops; j++) { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 106 | sk_sp<SkImageFilter> blur(SkBlurImageFilter::Make(4.0f, 4.0f, nullptr)); |
| 107 | auto xSelector = SkDisplacementMapEffect::kR_ChannelSelectorType; |
| 108 | auto ySelector = SkDisplacementMapEffect::kB_ChannelSelectorType; |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 109 | SkScalar scale = 2; |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 110 | |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 111 | SkPaint paint; |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 112 | paint.setImageFilter(SkDisplacementMapEffect::Make(xSelector, ySelector, scale, |
| 113 | blur, blur)); |
| 114 | |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 115 | SkRect rect = SkRect::Make(SkIRect::MakeWH(400, 400)); |
| 116 | canvas->drawRect(rect, paint); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | private: |
| 121 | typedef Benchmark INHERITED; |
| 122 | }; |
| 123 | |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 124 | DEF_BENCH(return new ImageFilterDAGBench;) |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 125 | DEF_BENCH(return new ImageMakeWithFilterDAGBench;) |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 126 | DEF_BENCH(return new ImageFilterDisplacedBlur;) |