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