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