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