blob: c5f1837c91133bede3fcf4f01ba98cca24253173 [file] [log] [blame]
sugoi@google.com781cc762013-01-15 15:40:19 +00001/*
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
halcanary878fa022015-01-26 11:24:32 -08008#include "SkDisplacementMapEffect.h"
fmalita5598b632015-09-15 11:26:13 -07009#include "SkImage.h"
10#include "SkImageSource.h"
halcanary878fa022015-01-26 11:24:32 -080011#include "gm.h"
robertphillips3e5c2b12015-03-23 05:46:51 -070012#include "sk_tool_utils.h"
sugoi@google.com781cc762013-01-15 15:40:19 +000013
14namespace skiagm {
15
16class DisplacementMapGM : public GM {
17public:
robertphillips943a4622015-09-03 13:32:33 -070018 DisplacementMapGM() {
sugoi@google.com781cc762013-01-15 15:40:19 +000019 this->setBGColor(0xFF000000);
20 }
21
22protected:
robertphillips943a4622015-09-03 13:32:33 -070023 SkString onShortName() override {
sugoi@google.com781cc762013-01-15 15:40:19 +000024 return SkString("displacement");
25 }
26
robertphillips943a4622015-09-03 13:32:33 -070027 void onOnceBeforeDraw() override {
28 fBitmap = sk_tool_utils::create_string_bitmap(80, 80, 0xFF884422, 15, 55, 96, "g");
29
30 SkColor c1 = sk_tool_utils::color_to_565(0xFF244484);
31 SkColor c2 = sk_tool_utils::color_to_565(0xFF804020);
32
fmalita5598b632015-09-15 11:26:13 -070033 fCheckerboard.reset(SkImage::NewFromBitmap(
34 sk_tool_utils::create_checkerboard_bitmap(80, 80, c1, c2, 8)));
35 fSmall.reset(SkImage::NewFromBitmap(
36 sk_tool_utils::create_checkerboard_bitmap(64, 64, c1, c2, 8)));
37 fLarge.reset(SkImage::NewFromBitmap(
38 sk_tool_utils::create_checkerboard_bitmap(96, 96, c1, c2, 8)));
39 fLargeW.reset(
40 SkImage::NewFromBitmap(sk_tool_utils::create_checkerboard_bitmap(96, 64, c1, c2, 8)));
41 fLargeH.reset(SkImage::NewFromBitmap(
42 sk_tool_utils::create_checkerboard_bitmap(64, 96, c1, c2, 8)));
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +000043 }
44
robertphillips943a4622015-09-03 13:32:33 -070045 SkISize onISize() override {
senorblanco00502372016-01-21 09:55:47 -080046 return SkISize::Make(600, 500);
sugoi@google.com781cc762013-01-15 15:40:19 +000047 }
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +000048
robertphillips943a4622015-09-03 13:32:33 -070049 void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) const {
sugoi@google.com781cc762013-01-15 15:40:19 +000050 canvas->save();
senorblanco@chromium.orgbc386ce2013-10-15 19:30:58 +000051 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000052 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
53 canvas->drawBitmap(fBitmap, 0, 0, &paint);
sugoi@google.com781cc762013-01-15 15:40:19 +000054 canvas->restore();
55 }
56
robertphillips943a4622015-09-03 13:32:33 -070057 void onDraw(SkCanvas* canvas) override {
senorblanco16b254a2015-04-09 11:13:24 -070058 canvas->clear(SK_ColorBLACK);
sugoi@google.com781cc762013-01-15 15:40:19 +000059 SkPaint paint;
fmalita5598b632015-09-15 11:26:13 -070060 SkAutoTUnref<SkImageFilter> displ(SkImageSource::Create(fCheckerboard));
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000061 paint.setImageFilter(SkDisplacementMapEffect::Create(
62 SkDisplacementMapEffect::kR_ChannelSelectorType,
63 SkDisplacementMapEffect::kG_ChannelSelectorType, 0.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +000064 drawClippedBitmap(canvas, 0, 0, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000065 paint.setImageFilter(SkDisplacementMapEffect::Create(
66 SkDisplacementMapEffect::kB_ChannelSelectorType,
67 SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +000068 drawClippedBitmap(canvas, 100, 0, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000069 paint.setImageFilter(SkDisplacementMapEffect::Create(
70 SkDisplacementMapEffect::kR_ChannelSelectorType,
71 SkDisplacementMapEffect::kB_ChannelSelectorType, 32.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +000072 drawClippedBitmap(canvas, 200, 0, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000073 paint.setImageFilter(SkDisplacementMapEffect::Create(
74 SkDisplacementMapEffect::kG_ChannelSelectorType,
75 SkDisplacementMapEffect::kA_ChannelSelectorType, 48.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +000076 drawClippedBitmap(canvas, 300, 0, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000077 paint.setImageFilter(SkDisplacementMapEffect::Create(
78 SkDisplacementMapEffect::kR_ChannelSelectorType,
79 SkDisplacementMapEffect::kA_ChannelSelectorType, 64.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +000080 drawClippedBitmap(canvas, 400, 0, paint);
81
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000082 paint.setImageFilter(SkDisplacementMapEffect::Create(
83 SkDisplacementMapEffect::kR_ChannelSelectorType,
84 SkDisplacementMapEffect::kG_ChannelSelectorType, 40.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +000085 drawClippedBitmap(canvas, 0, 100, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000086 paint.setImageFilter(SkDisplacementMapEffect::Create(
87 SkDisplacementMapEffect::kB_ChannelSelectorType,
88 SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +000089 drawClippedBitmap(canvas, 100, 100, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000090 paint.setImageFilter(SkDisplacementMapEffect::Create(
91 SkDisplacementMapEffect::kR_ChannelSelectorType,
92 SkDisplacementMapEffect::kB_ChannelSelectorType, 40.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +000093 drawClippedBitmap(canvas, 200, 100, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000094 paint.setImageFilter(SkDisplacementMapEffect::Create(
95 SkDisplacementMapEffect::kG_ChannelSelectorType,
96 SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +000097 drawClippedBitmap(canvas, 300, 100, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000098 paint.setImageFilter(SkDisplacementMapEffect::Create(
99 SkDisplacementMapEffect::kR_ChannelSelectorType,
100 SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000101 drawClippedBitmap(canvas, 400, 100, paint);
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000102
103 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(30, 30, 40, 40));
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000104 paint.setImageFilter(SkDisplacementMapEffect::Create(
105 SkDisplacementMapEffect::kR_ChannelSelectorType,
106 SkDisplacementMapEffect::kG_ChannelSelectorType,
halcanary96fcdcc2015-08-27 07:41:13 -0700107 0.0f, displ, nullptr, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000108 drawClippedBitmap(canvas, 0, 200, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000109 paint.setImageFilter(SkDisplacementMapEffect::Create(
110 SkDisplacementMapEffect::kB_ChannelSelectorType,
111 SkDisplacementMapEffect::kA_ChannelSelectorType,
halcanary96fcdcc2015-08-27 07:41:13 -0700112 16.0f, displ, nullptr, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000113 drawClippedBitmap(canvas, 100, 200, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000114 paint.setImageFilter(SkDisplacementMapEffect::Create(
115 SkDisplacementMapEffect::kR_ChannelSelectorType,
116 SkDisplacementMapEffect::kB_ChannelSelectorType,
halcanary96fcdcc2015-08-27 07:41:13 -0700117 32.0f, displ, nullptr, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000118 drawClippedBitmap(canvas, 200, 200, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000119 paint.setImageFilter(SkDisplacementMapEffect::Create(
120 SkDisplacementMapEffect::kG_ChannelSelectorType,
121 SkDisplacementMapEffect::kA_ChannelSelectorType,
halcanary96fcdcc2015-08-27 07:41:13 -0700122 48.0f, displ, nullptr, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000123 drawClippedBitmap(canvas, 300, 200, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000124 paint.setImageFilter(SkDisplacementMapEffect::Create(
125 SkDisplacementMapEffect::kR_ChannelSelectorType,
126 SkDisplacementMapEffect::kA_ChannelSelectorType,
halcanary96fcdcc2015-08-27 07:41:13 -0700127 64.0f, displ, nullptr, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000128 drawClippedBitmap(canvas, 400, 200, paint);
129
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000130 paint.setImageFilter(SkDisplacementMapEffect::Create(
131 SkDisplacementMapEffect::kR_ChannelSelectorType,
132 SkDisplacementMapEffect::kG_ChannelSelectorType,
halcanary96fcdcc2015-08-27 07:41:13 -0700133 40.0f, displ, nullptr, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000134 drawClippedBitmap(canvas, 0, 300, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000135 paint.setImageFilter(SkDisplacementMapEffect::Create(
136 SkDisplacementMapEffect::kB_ChannelSelectorType,
137 SkDisplacementMapEffect::kA_ChannelSelectorType,
halcanary96fcdcc2015-08-27 07:41:13 -0700138 40.0f, displ, nullptr, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000139 drawClippedBitmap(canvas, 100, 300, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000140 paint.setImageFilter(SkDisplacementMapEffect::Create(
141 SkDisplacementMapEffect::kR_ChannelSelectorType,
142 SkDisplacementMapEffect::kB_ChannelSelectorType,
halcanary96fcdcc2015-08-27 07:41:13 -0700143 40.0f, displ, nullptr, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000144 drawClippedBitmap(canvas, 200, 300, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000145 paint.setImageFilter(SkDisplacementMapEffect::Create(
146 SkDisplacementMapEffect::kG_ChannelSelectorType,
147 SkDisplacementMapEffect::kA_ChannelSelectorType,
halcanary96fcdcc2015-08-27 07:41:13 -0700148 40.0f, displ, nullptr, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000149 drawClippedBitmap(canvas, 300, 300, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000150 paint.setImageFilter(SkDisplacementMapEffect::Create(
151 SkDisplacementMapEffect::kR_ChannelSelectorType,
152 SkDisplacementMapEffect::kA_ChannelSelectorType,
halcanary96fcdcc2015-08-27 07:41:13 -0700153 40.0f, displ, nullptr, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000154 drawClippedBitmap(canvas, 400, 300, paint);
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +0000155
senorblanco00502372016-01-21 09:55:47 -0800156 // Test for negative scale.
157 paint.setImageFilter(SkDisplacementMapEffect::Create(
158 SkDisplacementMapEffect::kG_ChannelSelectorType,
159 SkDisplacementMapEffect::kA_ChannelSelectorType,
160 -40.0f, displ))->unref();
161 this->drawClippedBitmap(canvas, 500, 0, paint);
162
commit-bot@chromium.org6d7296a2013-12-19 17:00:46 +0000163 // Tests for images of different sizes
fmalita5598b632015-09-15 11:26:13 -0700164 displ.reset(SkImageSource::Create(fSmall));
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000165 paint.setImageFilter(SkDisplacementMapEffect::Create(
166 SkDisplacementMapEffect::kR_ChannelSelectorType,
167 SkDisplacementMapEffect::kG_ChannelSelectorType,
168 40.0f, displ))->unref();
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +0000169 drawClippedBitmap(canvas, 0, 400, paint);
fmalita5598b632015-09-15 11:26:13 -0700170 displ.reset(SkImageSource::Create(fLarge));
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000171 paint.setImageFilter(SkDisplacementMapEffect::Create(
172 SkDisplacementMapEffect::kB_ChannelSelectorType,
173 SkDisplacementMapEffect::kA_ChannelSelectorType,
174 40.0f, displ))->unref();
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +0000175 drawClippedBitmap(canvas, 100, 400, paint);
fmalita5598b632015-09-15 11:26:13 -0700176 displ.reset(SkImageSource::Create(fLargeW));
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000177 paint.setImageFilter(SkDisplacementMapEffect::Create(
178 SkDisplacementMapEffect::kR_ChannelSelectorType,
179 SkDisplacementMapEffect::kB_ChannelSelectorType,
180 40.0f, displ))->unref();
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +0000181 drawClippedBitmap(canvas, 200, 400, paint);
fmalita5598b632015-09-15 11:26:13 -0700182 displ.reset(SkImageSource::Create(fLargeH));
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000183 paint.setImageFilter(SkDisplacementMapEffect::Create(
184 SkDisplacementMapEffect::kG_ChannelSelectorType,
185 SkDisplacementMapEffect::kA_ChannelSelectorType,
186 40.0f, displ))->unref();
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +0000187 drawClippedBitmap(canvas, 300, 400, paint);
commit-bot@chromium.org6d7296a2013-12-19 17:00:46 +0000188
189 // Test for no given displacement input. In this case, both displacement
190 // and color should use the same bitmap, given to SkCanvas::drawBitmap()
191 // as an input argument.
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000192 paint.setImageFilter(SkDisplacementMapEffect::Create(
193 SkDisplacementMapEffect::kG_ChannelSelectorType,
194 SkDisplacementMapEffect::kA_ChannelSelectorType,
halcanary96fcdcc2015-08-27 07:41:13 -0700195 40.0f, nullptr))->unref();
commit-bot@chromium.org6d7296a2013-12-19 17:00:46 +0000196 drawClippedBitmap(canvas, 400, 400, paint);
sugoi@google.com781cc762013-01-15 15:40:19 +0000197 }
198
199private:
fmalita5598b632015-09-15 11:26:13 -0700200 SkBitmap fBitmap;
201 SkAutoTUnref<SkImage> fCheckerboard, fSmall, fLarge, fLargeW, fLargeH;
robertphillips943a4622015-09-03 13:32:33 -0700202
203 typedef GM INHERITED;
sugoi@google.com781cc762013-01-15 15:40:19 +0000204};
205
206//////////////////////////////////////////////////////////////////////////////
207
robertphillips943a4622015-09-03 13:32:33 -0700208DEF_GM(return new DisplacementMapGM;)
sugoi@google.com781cc762013-01-15 15:40:19 +0000209}