sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +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 | |
| 8 | #include "gm.h" |
| 9 | #include "SkDisplacementMapEffect.h" |
| 10 | #include "SkBitmapSource.h" |
| 11 | |
| 12 | namespace skiagm { |
| 13 | |
| 14 | class DisplacementMapGM : public GM { |
| 15 | public: |
| 16 | DisplacementMapGM() : fInitialized(false) { |
| 17 | this->setBGColor(0xFF000000); |
| 18 | } |
| 19 | |
| 20 | protected: |
| 21 | virtual SkString onShortName() { |
| 22 | return SkString("displacement"); |
| 23 | } |
| 24 | |
commit-bot@chromium.org | 5e79c2b | 2013-12-12 21:48:32 +0000 | [diff] [blame] | 25 | void make_bitmaps() { |
| 26 | make_bitmap(); |
| 27 | make_checkerboard(&fCheckerboard, 80, 80); |
| 28 | make_checkerboard(&fSmall, 64, 64); |
| 29 | make_checkerboard(&fLarge, 96, 96); |
| 30 | make_checkerboard(&fLargeW, 96, 64); |
| 31 | make_checkerboard(&fLargeH, 64, 96); |
| 32 | } |
| 33 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 34 | void make_bitmap() { |
| 35 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 80, 80); |
| 36 | fBitmap.allocPixels(); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 37 | SkBitmapDevice device(fBitmap); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 38 | SkCanvas canvas(&device); |
| 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 | |
commit-bot@chromium.org | 5e79c2b | 2013-12-12 21:48:32 +0000 | [diff] [blame] | 48 | void make_checkerboard(SkBitmap* bitmap, int w, int h) { |
| 49 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 50 | bitmap->allocPixels(); |
| 51 | SkBitmapDevice device(*bitmap); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 52 | SkCanvas canvas(&device); |
| 53 | canvas.clear(0x00000000); |
| 54 | SkPaint darkPaint; |
| 55 | darkPaint.setColor(0xFF804020); |
| 56 | SkPaint lightPaint; |
| 57 | lightPaint.setColor(0xFF244484); |
commit-bot@chromium.org | 5e79c2b | 2013-12-12 21:48:32 +0000 | [diff] [blame] | 58 | for (int y = 0; y < h; y += 16) { |
| 59 | for (int x = 0; x < w; x += 16) { |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [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(); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | virtual SkISize onISize() { |
commit-bot@chromium.org | 5e79c2b | 2013-12-12 21:48:32 +0000 | [diff] [blame] | 72 | return make_isize(500, 500); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 73 | } |
skia.committer@gmail.com | ff21c2e | 2013-01-16 07:05:56 +0000 | [diff] [blame] | 74 | |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 75 | void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) { |
| 76 | canvas->save(); |
senorblanco@chromium.org | bc386ce | 2013-10-15 19:30:58 +0000 | [diff] [blame] | 77 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 78 | canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height()))); |
| 79 | canvas->drawBitmap(fBitmap, 0, 0, &paint); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 80 | canvas->restore(); |
| 81 | } |
| 82 | |
| 83 | virtual void onDraw(SkCanvas* canvas) { |
| 84 | if (!fInitialized) { |
commit-bot@chromium.org | 5e79c2b | 2013-12-12 21:48:32 +0000 | [diff] [blame] | 85 | make_bitmaps(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 86 | fInitialized = true; |
| 87 | } |
| 88 | canvas->clear(0x00000000); |
| 89 | SkPaint paint; |
| 90 | SkAutoTUnref<SkImageFilter> displ(SkNEW_ARGS(SkBitmapSource, (fCheckerboard))); |
| 91 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 92 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 93 | SkDisplacementMapEffect::kG_ChannelSelectorType, 0.0f, displ)))->unref(); |
| 94 | drawClippedBitmap(canvas, 0, 0, paint); |
| 95 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 96 | (SkDisplacementMapEffect::kB_ChannelSelectorType, |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 97 | SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ)))->unref(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 98 | drawClippedBitmap(canvas, 100, 0, paint); |
| 99 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 100 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 101 | SkDisplacementMapEffect::kB_ChannelSelectorType, 32.0f, displ)))->unref(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 102 | drawClippedBitmap(canvas, 200, 0, paint); |
| 103 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 104 | (SkDisplacementMapEffect::kG_ChannelSelectorType, |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 105 | SkDisplacementMapEffect::kA_ChannelSelectorType, 48.0f, displ)))->unref(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 106 | drawClippedBitmap(canvas, 300, 0, paint); |
| 107 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 108 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 109 | SkDisplacementMapEffect::kA_ChannelSelectorType, 64.0f, displ)))->unref(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 110 | drawClippedBitmap(canvas, 400, 0, paint); |
| 111 | |
| 112 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 113 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 114 | SkDisplacementMapEffect::kG_ChannelSelectorType, 40.0f, displ)))->unref(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 115 | drawClippedBitmap(canvas, 0, 100, paint); |
| 116 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 117 | (SkDisplacementMapEffect::kB_ChannelSelectorType, |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 118 | SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ)))->unref(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 119 | drawClippedBitmap(canvas, 100, 100, paint); |
| 120 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 121 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 122 | SkDisplacementMapEffect::kB_ChannelSelectorType, 40.0f, displ)))->unref(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 123 | drawClippedBitmap(canvas, 200, 100, paint); |
| 124 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 125 | (SkDisplacementMapEffect::kG_ChannelSelectorType, |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 126 | SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ)))->unref(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 127 | drawClippedBitmap(canvas, 300, 100, paint); |
| 128 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 129 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
sugoi@google.com | 4ecd42e | 2013-03-19 15:07:30 +0000 | [diff] [blame] | 130 | SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ)))->unref(); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 131 | drawClippedBitmap(canvas, 400, 100, paint); |
senorblanco@chromium.org | 01bdf3c | 2013-10-15 19:02:43 +0000 | [diff] [blame] | 132 | |
| 133 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(30, 30, 40, 40)); |
| 134 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 135 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 136 | SkDisplacementMapEffect::kG_ChannelSelectorType, |
| 137 | 0.0f, displ, NULL, &cropRect)))->unref(); |
| 138 | drawClippedBitmap(canvas, 0, 200, paint); |
| 139 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 140 | (SkDisplacementMapEffect::kB_ChannelSelectorType, |
| 141 | SkDisplacementMapEffect::kA_ChannelSelectorType, |
| 142 | 16.0f, displ, NULL, &cropRect)))->unref(); |
| 143 | drawClippedBitmap(canvas, 100, 200, paint); |
| 144 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 145 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 146 | SkDisplacementMapEffect::kB_ChannelSelectorType, |
| 147 | 32.0f, displ, NULL, &cropRect)))->unref(); |
| 148 | drawClippedBitmap(canvas, 200, 200, paint); |
| 149 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 150 | (SkDisplacementMapEffect::kG_ChannelSelectorType, |
| 151 | SkDisplacementMapEffect::kA_ChannelSelectorType, |
| 152 | 48.0f, displ, NULL, &cropRect)))->unref(); |
| 153 | drawClippedBitmap(canvas, 300, 200, paint); |
| 154 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 155 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 156 | SkDisplacementMapEffect::kA_ChannelSelectorType, |
| 157 | 64.0f, displ, NULL, &cropRect)))->unref(); |
| 158 | drawClippedBitmap(canvas, 400, 200, paint); |
| 159 | |
| 160 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 161 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 162 | SkDisplacementMapEffect::kG_ChannelSelectorType, |
| 163 | 40.0f, displ, NULL, &cropRect)))->unref(); |
| 164 | drawClippedBitmap(canvas, 0, 300, paint); |
| 165 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 166 | (SkDisplacementMapEffect::kB_ChannelSelectorType, |
| 167 | SkDisplacementMapEffect::kA_ChannelSelectorType, |
| 168 | 40.0f, displ, NULL, &cropRect)))->unref(); |
| 169 | drawClippedBitmap(canvas, 100, 300, paint); |
| 170 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 171 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 172 | SkDisplacementMapEffect::kB_ChannelSelectorType, |
| 173 | 40.0f, displ, NULL, &cropRect)))->unref(); |
| 174 | drawClippedBitmap(canvas, 200, 300, paint); |
| 175 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 176 | (SkDisplacementMapEffect::kG_ChannelSelectorType, |
| 177 | SkDisplacementMapEffect::kA_ChannelSelectorType, |
| 178 | 40.0f, displ, NULL, &cropRect)))->unref(); |
| 179 | drawClippedBitmap(canvas, 300, 300, paint); |
| 180 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 181 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 182 | SkDisplacementMapEffect::kA_ChannelSelectorType, |
| 183 | 40.0f, displ, NULL, &cropRect)))->unref(); |
| 184 | drawClippedBitmap(canvas, 400, 300, paint); |
commit-bot@chromium.org | 5e79c2b | 2013-12-12 21:48:32 +0000 | [diff] [blame] | 185 | |
commit-bot@chromium.org | 6d7296a | 2013-12-19 17:00:46 +0000 | [diff] [blame] | 186 | // Tests for images of different sizes |
commit-bot@chromium.org | 5e79c2b | 2013-12-12 21:48:32 +0000 | [diff] [blame] | 187 | displ.reset(SkNEW_ARGS(SkBitmapSource, (fSmall))); |
| 188 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 189 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 190 | SkDisplacementMapEffect::kG_ChannelSelectorType, |
| 191 | 40.0f, displ)))->unref(); |
| 192 | drawClippedBitmap(canvas, 0, 400, paint); |
| 193 | displ.reset(SkNEW_ARGS(SkBitmapSource, (fLarge))); |
| 194 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 195 | (SkDisplacementMapEffect::kB_ChannelSelectorType, |
| 196 | SkDisplacementMapEffect::kA_ChannelSelectorType, |
| 197 | 40.0f, displ)))->unref(); |
| 198 | drawClippedBitmap(canvas, 100, 400, paint); |
| 199 | displ.reset(SkNEW_ARGS(SkBitmapSource, (fLargeW))); |
| 200 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 201 | (SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 202 | SkDisplacementMapEffect::kB_ChannelSelectorType, |
| 203 | 40.0f, displ)))->unref(); |
| 204 | drawClippedBitmap(canvas, 200, 400, paint); |
| 205 | displ.reset(SkNEW_ARGS(SkBitmapSource, (fLargeH))); |
| 206 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 207 | (SkDisplacementMapEffect::kG_ChannelSelectorType, |
| 208 | SkDisplacementMapEffect::kA_ChannelSelectorType, |
| 209 | 40.0f, displ)))->unref(); |
| 210 | drawClippedBitmap(canvas, 300, 400, paint); |
commit-bot@chromium.org | 6d7296a | 2013-12-19 17:00:46 +0000 | [diff] [blame] | 211 | |
| 212 | // Test for no given displacement input. In this case, both displacement |
| 213 | // and color should use the same bitmap, given to SkCanvas::drawBitmap() |
| 214 | // as an input argument. |
| 215 | paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect, |
| 216 | (SkDisplacementMapEffect::kG_ChannelSelectorType, |
| 217 | SkDisplacementMapEffect::kA_ChannelSelectorType, |
| 218 | 40.0f, NULL)))->unref(); |
| 219 | drawClippedBitmap(canvas, 400, 400, paint); |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | private: |
| 223 | typedef GM INHERITED; |
commit-bot@chromium.org | 5e79c2b | 2013-12-12 21:48:32 +0000 | [diff] [blame] | 224 | SkBitmap fBitmap, fCheckerboard, fSmall, fLarge, fLargeW, fLargeH; |
sugoi@google.com | 781cc76 | 2013-01-15 15:40:19 +0000 | [diff] [blame] | 225 | bool fInitialized; |
| 226 | }; |
| 227 | |
| 228 | ////////////////////////////////////////////////////////////////////////////// |
| 229 | |
| 230 | static GM* MyFactory(void*) { return new DisplacementMapGM; } |
| 231 | static GMRegistry reg(MyFactory); |
| 232 | |
| 233 | } |