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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "bench/Benchmark.h" |
| 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkImage.h" |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 11 | #include "include/effects/SkImageFilters.h" |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 12 | #include "include/gpu/GrDirectContext.h" |
| 13 | #include "include/gpu/GrRecordingContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "tools/Resources.h" |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 15 | |
| 16 | // Exercise a blur filter connected to 5 inputs of the same merge filter. |
| 17 | // This bench shows an improvement in performance once cacheing of re-used |
| 18 | // nodes is implemented, since the DAG is no longer flattened to a tree. |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 19 | class ImageFilterDAGBench : public Benchmark { |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 20 | public: |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 21 | ImageFilterDAGBench() {} |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 22 | |
| 23 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 24 | const char* onGetName() override { |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 25 | return "image_filter_dag"; |
| 26 | } |
| 27 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 28 | void onDraw(int loops, SkCanvas* canvas) override { |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 29 | const SkRect rect = SkRect::Make(SkIRect::MakeWH(400, 400)); |
| 30 | |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 31 | // Set up the filters once, we're not interested in measuring allocation time here |
| 32 | sk_sp<SkImageFilter> blur(SkImageFilters::Blur(20.0f, 20.0f, nullptr)); |
| 33 | sk_sp<SkImageFilter> inputs[kNumInputs]; |
| 34 | for (int i = 0; i < kNumInputs; ++i) { |
| 35 | inputs[i] = blur; |
| 36 | } |
| 37 | SkPaint paint; |
| 38 | paint.setImageFilter(SkImageFilters::Merge(inputs, kNumInputs)); |
| 39 | |
| 40 | // Only measure the filter computations done in drawRect() |
| 41 | // TODO (michaelludwig) - This benchmark, and the ones defined below, allocate their filters |
| 42 | // outside of the loop. This means that repeatedly drawing with the same filter will hit |
| 43 | // the global image filter cache inside the loop. Raster backend uses this cache so will see |
| 44 | // artificially improved performance. Ganesh will not because it uses a cache per filter |
| 45 | // call, so only within-DAG cache hits are measured (as desired). skbug:9297 wants to move |
| 46 | // raster backend to the same pattern, which will make the benchmark executions fair again. |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 47 | for (int j = 0; j < loops; j++) { |
mtklein | bb6a028 | 2014-07-01 08:43:42 -0700 | [diff] [blame] | 48 | canvas->drawRect(rect, paint); |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 49 | } |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | private: |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 53 | static const int kNumInputs = 5; |
| 54 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 55 | using INHERITED = Benchmark; |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 58 | class ImageMakeWithFilterDAGBench : public Benchmark { |
| 59 | public: |
| 60 | ImageMakeWithFilterDAGBench() {} |
| 61 | |
| 62 | protected: |
| 63 | const char* onGetName() override { |
| 64 | return "image_make_with_filter_dag"; |
| 65 | } |
| 66 | |
| 67 | void onDelayedSetup() override { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 68 | fImage = GetResourceAsImage("images/mandrill_512.png"); |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void onDraw(int loops, SkCanvas* canvas) override { |
| 72 | SkIRect subset = SkIRect::MakeSize(fImage->dimensions()); |
| 73 | SkIPoint offset = SkIPoint::Make(0, 0); |
| 74 | SkIRect discardSubset; |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 75 | |
Adlai Holler | 247835b | 2020-07-21 16:50:06 -0400 | [diff] [blame] | 76 | auto dContext = GrAsDirectContext(canvas->recordingContext()); |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 77 | // makeWithFilter will only use the GPU backend if the image is already a texture |
Adlai Holler | 247835b | 2020-07-21 16:50:06 -0400 | [diff] [blame] | 78 | sk_sp<SkImage> image = fImage->makeTextureImage(dContext); |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 79 | if (!image) { |
| 80 | image = fImage; |
| 81 | } |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 82 | |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 83 | // Set up the filters once so the allocation cost isn't included per-loop |
| 84 | sk_sp<SkImageFilter> blur(SkImageFilters::Blur(20.0f, 20.0f, nullptr)); |
| 85 | sk_sp<SkImageFilter> inputs[kNumInputs]; |
| 86 | for (int i = 0; i < kNumInputs; ++i) { |
| 87 | inputs[i] = blur; |
| 88 | } |
| 89 | sk_sp<SkImageFilter> mergeFilter = SkImageFilters::Merge(inputs, kNumInputs); |
| 90 | |
| 91 | // But measure makeWithFilter() per loop since that's the focus of this benchmark |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 92 | for (int j = 0; j < loops; j++) { |
Adlai Holler | 247835b | 2020-07-21 16:50:06 -0400 | [diff] [blame] | 93 | image = image->makeWithFilter(dContext, mergeFilter.get(), subset, subset, |
| 94 | &discardSubset, &offset); |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 95 | SkASSERT(image && image->dimensions() == fImage->dimensions()); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | static const int kNumInputs = 5; |
| 101 | sk_sp<SkImage> fImage; |
| 102 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 103 | using INHERITED = Benchmark; |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 106 | // Exercise a blur filter connected to both inputs of an SkDisplacementMapEffect. |
| 107 | |
| 108 | class ImageFilterDisplacedBlur : public Benchmark { |
| 109 | public: |
| 110 | ImageFilterDisplacedBlur() {} |
| 111 | |
| 112 | protected: |
| 113 | const char* onGetName() override { |
| 114 | return "image_filter_displaced_blur"; |
| 115 | } |
| 116 | |
| 117 | void onDraw(int loops, SkCanvas* canvas) override { |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 118 | // Setup filter once |
| 119 | sk_sp<SkImageFilter> blur(SkImageFilters::Blur(4.0f, 4.0f, nullptr)); |
| 120 | SkScalar scale = 2; |
| 121 | |
| 122 | SkPaint paint; |
| 123 | paint.setImageFilter(SkImageFilters::DisplacementMap(SkColorChannel::kR, SkColorChannel::kR, |
| 124 | scale, blur, blur)); |
| 125 | |
| 126 | SkRect rect = SkRect::Make(SkIRect::MakeWH(400, 400)); |
| 127 | |
| 128 | // As before, measure just the filter computation time inside the loops |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 129 | for (int j = 0; j < loops; j++) { |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 130 | canvas->drawRect(rect, paint); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 135 | using INHERITED = Benchmark; |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
Xianzhu Wang | 0bff418 | 2017-09-11 09:12:56 -0700 | [diff] [blame] | 138 | // Exercise an Xfermode kSrcIn filter compositing two inputs which have a small intersection. |
| 139 | class ImageFilterXfermodeIn : public Benchmark { |
| 140 | public: |
| 141 | ImageFilterXfermodeIn() {} |
| 142 | |
| 143 | protected: |
| 144 | const char* onGetName() override { return "image_filter_xfermode_in"; } |
| 145 | |
| 146 | void onDraw(int loops, SkCanvas* canvas) override { |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 147 | // Allocate filters once to avoid measuring instantiation time |
| 148 | auto blur = SkImageFilters::Blur(20.0f, 20.0f, nullptr); |
| 149 | auto offset1 = SkImageFilters::Offset(100.0f, 100.0f, blur); |
| 150 | auto offset2 = SkImageFilters::Offset(-100.0f, -100.0f, blur); |
| 151 | auto xfermode = |
Michael Ludwig | 01b93ea | 2020-10-09 10:45:07 -0400 | [diff] [blame^] | 152 | SkImageFilters::Blend(SkBlendMode::kSrcIn, offset1, offset2, nullptr); |
Xianzhu Wang | 0bff418 | 2017-09-11 09:12:56 -0700 | [diff] [blame] | 153 | |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 154 | SkPaint paint; |
| 155 | paint.setImageFilter(xfermode); |
| 156 | |
| 157 | // Measure only the filter time |
| 158 | for (int j = 0; j < loops; j++) { |
Xianzhu Wang | 0bff418 | 2017-09-11 09:12:56 -0700 | [diff] [blame] | 159 | canvas->drawRect(SkRect::MakeWH(200.0f, 200.0f), paint); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 164 | using INHERITED = Benchmark; |
Xianzhu Wang | 0bff418 | 2017-09-11 09:12:56 -0700 | [diff] [blame] | 165 | }; |
| 166 | |
senorblanco@chromium.org | 0d64356 | 2014-04-10 16:16:13 +0000 | [diff] [blame] | 167 | DEF_BENCH(return new ImageFilterDAGBench;) |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 168 | DEF_BENCH(return new ImageMakeWithFilterDAGBench;) |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 169 | DEF_BENCH(return new ImageFilterDisplacedBlur;) |
Xianzhu Wang | 0bff418 | 2017-09-11 09:12:56 -0700 | [diff] [blame] | 170 | DEF_BENCH(return new ImageFilterXfermodeIn;) |