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