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 | */ |
| 7 | #include "SkBenchmark.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 8 | #include "SkBitmapDevice.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 | |
| 18 | class DisplacementBaseBench : public SkBenchmark { |
| 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: |
| 25 | virtual void onPreDraw() SK_OVERRIDE { |
| 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; |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 36 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 37 | fBitmap.allocPixels(); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 38 | SkBitmapDevice device(fBitmap); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 39 | SkCanvas canvas(&device); |
| 40 | canvas.clear(0x00000000); |
| 41 | SkPaint paint; |
| 42 | paint.setAntiAlias(true); |
| 43 | paint.setColor(0xFF884422); |
| 44 | paint.setTextSize(SkIntToScalar(96)); |
| 45 | const char* str = "g"; |
| 46 | canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(55), paint); |
| 47 | } |
| 48 | |
robertphillips@google.com | 26e30c5 | 2013-10-28 18:07:44 +0000 | [diff] [blame] | 49 | void makeCheckerboard() { |
| 50 | const int w = this->isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE; |
| 51 | const int h = this->isSmall() ? FILTER_HEIGHT_LARGE : FILTER_HEIGHT_LARGE; |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 52 | fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 53 | fCheckerboard.allocPixels(); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 54 | SkBitmapDevice device(fCheckerboard); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 55 | SkCanvas canvas(&device); |
| 56 | canvas.clear(0x00000000); |
| 57 | SkPaint darkPaint; |
| 58 | darkPaint.setColor(0xFF804020); |
| 59 | SkPaint lightPaint; |
| 60 | lightPaint.setColor(0xFF244484); |
| 61 | for (int y = 0; y < h; y += 16) { |
| 62 | for (int x = 0; x < w; x += 16) { |
| 63 | canvas.save(); |
| 64 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 65 | canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); |
| 66 | canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); |
| 67 | canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); |
| 68 | canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); |
| 69 | canvas.restore(); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) { |
| 75 | canvas->save(); |
| 76 | canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
skia.committer@gmail.com | 70402c3 | 2013-10-29 07:01:50 +0000 | [diff] [blame] | 77 | SkIntToScalar(fBitmap.width()), |
robertphillips@google.com | 26e30c5 | 2013-10-28 18:07:44 +0000 | [diff] [blame] | 78 | SkIntToScalar(fBitmap.height()))); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 79 | canvas->drawBitmap(fBitmap, SkIntToScalar(x), SkIntToScalar(y), &paint); |
| 80 | canvas->restore(); |
| 81 | } |
| 82 | |
| 83 | inline bool isSmall() const { return fIsSmall; } |
| 84 | |
| 85 | SkBitmap fBitmap, fCheckerboard; |
| 86 | private: |
| 87 | bool fInitialized; |
| 88 | bool fIsSmall; |
| 89 | typedef SkBenchmark INHERITED; |
| 90 | }; |
| 91 | |
| 92 | class DisplacementZeroBench : public DisplacementBaseBench { |
| 93 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 94 | DisplacementZeroBench(bool small) : INHERITED(small) { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | protected: |
| 98 | virtual const char* onGetName() SK_OVERRIDE { |
robertphillips@google.com | 26e30c5 | 2013-10-28 18:07:44 +0000 | [diff] [blame] | 99 | return this->isSmall() ? "displacement_zero_small" : "displacement_zero_large"; |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 100 | } |
| 101 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 102 | virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 103 | SkPaint paint; |
| 104 | SkAutoTUnref<SkImageFilter> displ(SkNEW_ARGS(SkBitmapSource, (fCheckerboard))); |
| 105 | // No displacement effect |
| 106 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 107 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 108 | SkDisplacementMapEffect::kG_ChannelSelectorType, 0.0f, displ)))->unref(); |
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: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 121 | DisplacementAlphaBench(bool small) : INHERITED(small) { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | protected: |
| 125 | virtual const char* onGetName() SK_OVERRIDE { |
| 126 | return isSmall() ? "displacement_alpha_small" : "displacement_alpha_large"; |
| 127 | } |
| 128 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 129 | virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 130 | SkPaint paint; |
| 131 | SkAutoTUnref<SkImageFilter> displ(SkNEW_ARGS(SkBitmapSource, (fCheckerboard))); |
| 132 | // Displacement, with 1 alpha component (which isn't pre-multiplied) |
| 133 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 134 | (SkDisplacementMapEffect::kB_ChannelSelectorType, |
| 135 | SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ)))->unref(); |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 136 | for (int i = 0; i < loops; i++) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 137 | drawClippedBitmap(canvas, 100, 0, paint); |
| 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: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 147 | DisplacementFullBench(bool small) : INHERITED(small) { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | protected: |
| 151 | virtual const char* onGetName() SK_OVERRIDE { |
| 152 | return isSmall() ? "displacement_full_small" : "displacement_full_large"; |
| 153 | } |
| 154 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 155 | virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 156 | SkPaint paint; |
| 157 | SkAutoTUnref<SkImageFilter> displ(SkNEW_ARGS(SkBitmapSource, (fCheckerboard))); |
| 158 | // Displacement, with 2 non-alpha components |
| 159 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 160 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 161 | SkDisplacementMapEffect::kB_ChannelSelectorType, 32.0f, displ)))->unref(); |
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); ) |