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