blob: 9fea0188df3da7ecc770396391ecad177311429c [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
reed9ce9d672016-03-17 10:51:11 -070033 fCheckerboard = SkImage::MakeFromBitmap(
34 sk_tool_utils::create_checkerboard_bitmap(80, 80, c1, c2, 8));
35 fSmall = SkImage::MakeFromBitmap(
36 sk_tool_utils::create_checkerboard_bitmap(64, 64, c1, c2, 8));
37 fLarge = SkImage::MakeFromBitmap(
38 sk_tool_utils::create_checkerboard_bitmap(96, 96, c1, c2, 8));
39 fLargeW =
40 SkImage::MakeFromBitmap(sk_tool_utils::create_checkerboard_bitmap(96, 64, c1, c2, 8));
41 fLargeH = SkImage::MakeFromBitmap(
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;
robertphillips549c8992016-04-01 09:28:51 -070060 sk_sp<SkImageFilter> displ(SkImageSource::Make(fCheckerboard));
robertphillipsbfe11fc2016-04-15 07:17:36 -070061 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000062 SkDisplacementMapEffect::kR_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -070063 SkDisplacementMapEffect::kG_ChannelSelectorType, 0.0f, displ, nullptr));
64 this->drawClippedBitmap(canvas, 0, 0, paint);
65 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000066 SkDisplacementMapEffect::kB_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -070067 SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ, nullptr));
68 this->drawClippedBitmap(canvas, 100, 0, paint);
69 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000070 SkDisplacementMapEffect::kR_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -070071 SkDisplacementMapEffect::kB_ChannelSelectorType, 32.0f, displ, nullptr));
72 this->drawClippedBitmap(canvas, 200, 0, paint);
73 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000074 SkDisplacementMapEffect::kG_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -070075 SkDisplacementMapEffect::kA_ChannelSelectorType, 48.0f, displ, nullptr));
76 this->drawClippedBitmap(canvas, 300, 0, paint);
77 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000078 SkDisplacementMapEffect::kR_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -070079 SkDisplacementMapEffect::kA_ChannelSelectorType, 64.0f, displ, nullptr));
80 this->drawClippedBitmap(canvas, 400, 0, paint);
sugoi@google.com781cc762013-01-15 15:40:19 +000081
robertphillipsbfe11fc2016-04-15 07:17:36 -070082 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000083 SkDisplacementMapEffect::kR_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -070084 SkDisplacementMapEffect::kG_ChannelSelectorType, 40.0f, displ, nullptr));
85 this->drawClippedBitmap(canvas, 0, 100, paint);
86 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000087 SkDisplacementMapEffect::kB_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -070088 SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ, nullptr));
89 this->drawClippedBitmap(canvas, 100, 100, paint);
90 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000091 SkDisplacementMapEffect::kR_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -070092 SkDisplacementMapEffect::kB_ChannelSelectorType, 40.0f, displ, nullptr));
93 this->drawClippedBitmap(canvas, 200, 100, paint);
94 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000095 SkDisplacementMapEffect::kG_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -070096 SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ, nullptr));
97 this->drawClippedBitmap(canvas, 300, 100, paint);
98 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000099 SkDisplacementMapEffect::kR_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -0700100 SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ, nullptr));
101 this->drawClippedBitmap(canvas, 400, 100, paint);
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000102
103 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(30, 30, 40, 40));
robertphillipsbfe11fc2016-04-15 07:17:36 -0700104 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000105 SkDisplacementMapEffect::kR_ChannelSelectorType,
106 SkDisplacementMapEffect::kG_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -0700107 0.0f, displ, nullptr, &cropRect));
108 this->drawClippedBitmap(canvas, 0, 200, paint);
109 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000110 SkDisplacementMapEffect::kB_ChannelSelectorType,
111 SkDisplacementMapEffect::kA_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -0700112 16.0f, displ, nullptr, &cropRect));
113 this->drawClippedBitmap(canvas, 100, 200, paint);
114 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000115 SkDisplacementMapEffect::kR_ChannelSelectorType,
116 SkDisplacementMapEffect::kB_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -0700117 32.0f, displ, nullptr, &cropRect));
118 this->drawClippedBitmap(canvas, 200, 200, paint);
119 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000120 SkDisplacementMapEffect::kG_ChannelSelectorType,
121 SkDisplacementMapEffect::kA_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -0700122 48.0f, displ, nullptr, &cropRect));
123 this->drawClippedBitmap(canvas, 300, 200, paint);
124 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000125 SkDisplacementMapEffect::kR_ChannelSelectorType,
126 SkDisplacementMapEffect::kA_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -0700127 64.0f, displ, nullptr, &cropRect));
128 this->drawClippedBitmap(canvas, 400, 200, paint);
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000129
robertphillipsbfe11fc2016-04-15 07:17:36 -0700130 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000131 SkDisplacementMapEffect::kR_ChannelSelectorType,
132 SkDisplacementMapEffect::kG_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -0700133 40.0f, displ, nullptr, &cropRect));
134 this->drawClippedBitmap(canvas, 0, 300, paint);
135 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000136 SkDisplacementMapEffect::kB_ChannelSelectorType,
137 SkDisplacementMapEffect::kA_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -0700138 40.0f, displ, nullptr, &cropRect));
139 this->drawClippedBitmap(canvas, 100, 300, paint);
140 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000141 SkDisplacementMapEffect::kR_ChannelSelectorType,
142 SkDisplacementMapEffect::kB_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -0700143 40.0f, displ, nullptr, &cropRect));
144 this->drawClippedBitmap(canvas, 200, 300, paint);
145 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000146 SkDisplacementMapEffect::kG_ChannelSelectorType,
147 SkDisplacementMapEffect::kA_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -0700148 40.0f, displ, nullptr, &cropRect));
149 this->drawClippedBitmap(canvas, 300, 300, paint);
150 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000151 SkDisplacementMapEffect::kR_ChannelSelectorType,
152 SkDisplacementMapEffect::kA_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -0700153 40.0f, displ, nullptr, &cropRect));
154 this->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.
robertphillipsbfe11fc2016-04-15 07:17:36 -0700157 paint.setImageFilter(SkDisplacementMapEffect::Make(
senorblanco00502372016-01-21 09:55:47 -0800158 SkDisplacementMapEffect::kG_ChannelSelectorType,
159 SkDisplacementMapEffect::kA_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -0700160 -40.0f, displ, nullptr));
senorblanco00502372016-01-21 09:55:47 -0800161 this->drawClippedBitmap(canvas, 500, 0, paint);
162
commit-bot@chromium.org6d7296a2013-12-19 17:00:46 +0000163 // Tests for images of different sizes
robertphillips549c8992016-04-01 09:28:51 -0700164 displ = SkImageSource::Make(fSmall);
robertphillipsbfe11fc2016-04-15 07:17:36 -0700165 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000166 SkDisplacementMapEffect::kR_ChannelSelectorType,
167 SkDisplacementMapEffect::kG_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -0700168 40.0f, std::move(displ), nullptr));
169 this->drawClippedBitmap(canvas, 0, 400, paint);
robertphillips549c8992016-04-01 09:28:51 -0700170 displ = SkImageSource::Make(fLarge);
robertphillipsbfe11fc2016-04-15 07:17:36 -0700171 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000172 SkDisplacementMapEffect::kB_ChannelSelectorType,
173 SkDisplacementMapEffect::kA_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -0700174 40.0f, std::move(displ), nullptr));
175 this->drawClippedBitmap(canvas, 100, 400, paint);
robertphillips549c8992016-04-01 09:28:51 -0700176 displ = SkImageSource::Make(fLargeW);
robertphillipsbfe11fc2016-04-15 07:17:36 -0700177 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000178 SkDisplacementMapEffect::kR_ChannelSelectorType,
179 SkDisplacementMapEffect::kB_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -0700180 40.0f, std::move(displ), nullptr));
181 this->drawClippedBitmap(canvas, 200, 400, paint);
robertphillips549c8992016-04-01 09:28:51 -0700182 displ = SkImageSource::Make(fLargeH);
robertphillipsbfe11fc2016-04-15 07:17:36 -0700183 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000184 SkDisplacementMapEffect::kG_ChannelSelectorType,
185 SkDisplacementMapEffect::kA_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -0700186 40.0f, std::move(displ), nullptr));
187 this->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.
robertphillipsbfe11fc2016-04-15 07:17:36 -0700192 paint.setImageFilter(SkDisplacementMapEffect::Make(
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000193 SkDisplacementMapEffect::kG_ChannelSelectorType,
194 SkDisplacementMapEffect::kA_ChannelSelectorType,
robertphillipsbfe11fc2016-04-15 07:17:36 -0700195 40.0f, nullptr, nullptr));
196 this->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;
reed9ce9d672016-03-17 10:51:11 -0700201 sk_sp<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}