sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | */ |
reed | 8c0c7b0 | 2014-06-27 05:49:53 -0700 | [diff] [blame] | 7 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
Mike Reed | 89126e4 | 2019-01-03 12:59:14 -0500 | [diff] [blame] | 10 | #include "SkFont.h" |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 11 | #include "SkDisplacementMapEffect.h" |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 12 | #include "SkImageSource.h" |
| 13 | #include "SkSurface.h" |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 14 | |
sugoi@google.com | ca425d9 | 2013-04-23 14:37:38 +0000 | [diff] [blame] | 15 | #define FILTER_WIDTH_SMALL 32 |
| 16 | #define FILTER_HEIGHT_SMALL 32 |
| 17 | #define FILTER_WIDTH_LARGE 256 |
| 18 | #define FILTER_HEIGHT_LARGE 256 |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 19 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 20 | class DisplacementBaseBench : public Benchmark { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 21 | public: |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 22 | DisplacementBaseBench(bool small) : fInitialized(false), fIsSmall(small) { } |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 23 | |
| 24 | protected: |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 25 | void onDelayedSetup() override { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 26 | if (!fInitialized) { |
robertphillips@google.com | 26e30c5 | 2013-10-28 18:07:44 +0000 | [diff] [blame] | 27 | this->makeBitmap(); |
| 28 | this->makeCheckerboard(); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 29 | fInitialized = true; |
| 30 | } |
| 31 | } |
| 32 | |
robertphillips@google.com | 26e30c5 | 2013-10-28 18:07:44 +0000 | [diff] [blame] | 33 | void makeBitmap() { |
| 34 | const int w = this->isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE; |
Greg Kaiser | fb7a713 | 2019-02-08 17:00:36 -0800 | [diff] [blame^] | 35 | const int h = this->isSmall() ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE; |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 36 | fBitmap.allocN32Pixels(w, h); |
| 37 | SkCanvas canvas(fBitmap); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 38 | canvas.clear(0x00000000); |
| 39 | SkPaint paint; |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 40 | paint.setColor(0xFF884422); |
Mike Reed | 89126e4 | 2019-01-03 12:59:14 -0500 | [diff] [blame] | 41 | |
| 42 | SkFont font; |
| 43 | font.setSize(SkIntToScalar(96)); |
| 44 | canvas.drawSimpleText("g", 1, kUTF8_SkTextEncoding, SkIntToScalar(15), SkIntToScalar(55), font, paint); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 45 | } |
| 46 | |
robertphillips@google.com | 26e30c5 | 2013-10-28 18:07:44 +0000 | [diff] [blame] | 47 | void makeCheckerboard() { |
| 48 | const int w = this->isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE; |
Greg Kaiser | fb7a713 | 2019-02-08 17:00:36 -0800 | [diff] [blame^] | 49 | const int h = this->isSmall() ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE; |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 50 | auto surface(SkSurface::MakeRasterN32Premul(w, h)); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 51 | SkCanvas* canvas = surface->getCanvas(); |
| 52 | canvas->clear(0x00000000); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 53 | SkPaint darkPaint; |
| 54 | darkPaint.setColor(0xFF804020); |
| 55 | SkPaint lightPaint; |
| 56 | lightPaint.setColor(0xFF244484); |
| 57 | for (int y = 0; y < h; y += 16) { |
| 58 | for (int x = 0; x < w; x += 16) { |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 59 | canvas->save(); |
| 60 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 61 | canvas->drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); |
| 62 | canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); |
| 63 | canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); |
| 64 | canvas->drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); |
| 65 | canvas->restore(); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 68 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 69 | fCheckerboard = surface->makeImageSnapshot(); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) { |
| 73 | canvas->save(); |
| 74 | canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
skia.committer@gmail.com | 70402c3 | 2013-10-29 07:01:50 +0000 | [diff] [blame] | 75 | SkIntToScalar(fBitmap.width()), |
robertphillips@google.com | 26e30c5 | 2013-10-28 18:07:44 +0000 | [diff] [blame] | 76 | SkIntToScalar(fBitmap.height()))); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 77 | canvas->drawBitmap(fBitmap, SkIntToScalar(x), SkIntToScalar(y), &paint); |
| 78 | canvas->restore(); |
| 79 | } |
| 80 | |
| 81 | inline bool isSmall() const { return fIsSmall; } |
| 82 | |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 83 | SkBitmap fBitmap; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 84 | sk_sp<SkImage> fCheckerboard; |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 85 | |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 86 | private: |
| 87 | bool fInitialized; |
| 88 | bool fIsSmall; |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 89 | typedef Benchmark INHERITED; |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | class DisplacementZeroBench : public DisplacementBaseBench { |
| 93 | public: |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 94 | DisplacementZeroBench(bool small) : INHERITED(small) { } |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 95 | |
| 96 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 97 | const char* onGetName() override { |
robertphillips@google.com | 26e30c5 | 2013-10-28 18:07:44 +0000 | [diff] [blame] | 98 | return this->isSmall() ? "displacement_zero_small" : "displacement_zero_large"; |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 99 | } |
| 100 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 101 | void onDraw(int loops, SkCanvas* canvas) override { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 102 | SkPaint paint; |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 103 | sk_sp<SkImageFilter> displ(SkImageSource::Make(fCheckerboard)); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 104 | // No displacement effect |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 105 | paint.setImageFilter(SkDisplacementMapEffect::Make( |
| 106 | SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 107 | SkDisplacementMapEffect::kG_ChannelSelectorType, |
| 108 | 0.0f, std::move(displ), nullptr)); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 109 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 110 | for (int i = 0; i < loops; i++) { |
robertphillips@google.com | 26e30c5 | 2013-10-28 18:07:44 +0000 | [diff] [blame] | 111 | this->drawClippedBitmap(canvas, 0, 0, paint); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 112 | } |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | private: |
| 116 | typedef DisplacementBaseBench INHERITED; |
| 117 | }; |
| 118 | |
| 119 | class DisplacementAlphaBench : public DisplacementBaseBench { |
| 120 | public: |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 121 | DisplacementAlphaBench(bool small) : INHERITED(small) { } |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 122 | |
| 123 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 124 | const char* onGetName() override { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 125 | return isSmall() ? "displacement_alpha_small" : "displacement_alpha_large"; |
| 126 | } |
| 127 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 128 | void onDraw(int loops, SkCanvas* canvas) override { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 129 | SkPaint paint; |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 130 | sk_sp<SkImageFilter> displ(SkImageSource::Make(fCheckerboard)); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 131 | // Displacement, with 1 alpha component (which isn't pre-multiplied) |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 132 | paint.setImageFilter(SkDisplacementMapEffect::Make( |
| 133 | SkDisplacementMapEffect::kB_ChannelSelectorType, |
| 134 | SkDisplacementMapEffect::kA_ChannelSelectorType, |
| 135 | 16.0f, std::move(displ), nullptr)); |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 136 | for (int i = 0; i < loops; i++) { |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 137 | this->drawClippedBitmap(canvas, 100, 0, paint); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 138 | } |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | private: |
| 142 | typedef DisplacementBaseBench INHERITED; |
| 143 | }; |
| 144 | |
| 145 | class DisplacementFullBench : public DisplacementBaseBench { |
| 146 | public: |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 147 | DisplacementFullBench(bool small) : INHERITED(small) { } |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 148 | |
| 149 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 150 | const char* onGetName() override { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 151 | return isSmall() ? "displacement_full_small" : "displacement_full_large"; |
| 152 | } |
| 153 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 154 | void onDraw(int loops, SkCanvas* canvas) override { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 155 | SkPaint paint; |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 156 | sk_sp<SkImageFilter> displ(SkImageSource::Make(fCheckerboard)); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 157 | // Displacement, with 2 non-alpha components |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 158 | paint.setImageFilter(SkDisplacementMapEffect::Make( |
| 159 | SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 160 | SkDisplacementMapEffect::kB_ChannelSelectorType, |
| 161 | 32.0f, std::move(displ), nullptr)); |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 162 | for (int i = 0; i < loops; ++i) { |
robertphillips@google.com | 26e30c5 | 2013-10-28 18:07:44 +0000 | [diff] [blame] | 163 | this->drawClippedBitmap(canvas, 200, 0, paint); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 164 | } |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | private: |
| 168 | typedef DisplacementBaseBench INHERITED; |
| 169 | }; |
| 170 | |
| 171 | /////////////////////////////////////////////////////////////////////////////// |
| 172 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 173 | DEF_BENCH( return new DisplacementZeroBench(true); ) |
| 174 | DEF_BENCH( return new DisplacementAlphaBench(true); ) |
| 175 | DEF_BENCH( return new DisplacementFullBench(true); ) |
| 176 | DEF_BENCH( return new DisplacementZeroBench(false); ) |
| 177 | DEF_BENCH( return new DisplacementAlphaBench(false); ) |
| 178 | DEF_BENCH( return new DisplacementFullBench(false); ) |