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