blob: bcf759a4bf5fec6a230bc13a1ee1cdce5f24dd26 [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
halcanaryb0cce2c2015-01-26 12:49:00 -08008#include "sk_tool_utils.h"
sugoi@google.com781cc762013-01-15 15:40:19 +00009#include "SkBitmapSource.h"
halcanary878fa022015-01-26 11:24:32 -080010#include "SkDisplacementMapEffect.h"
11#include "gm.h"
sugoi@google.com781cc762013-01-15 15:40:19 +000012
13namespace skiagm {
14
15class DisplacementMapGM : public GM {
16public:
17 DisplacementMapGM() : fInitialized(false) {
18 this->setBGColor(0xFF000000);
19 }
20
21protected:
22 virtual SkString onShortName() {
23 return SkString("displacement");
24 }
25
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +000026 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.com781cc762013-01-15 15:40:19 +000035 void make_bitmap() {
reed@google.comeb9a46c2014-01-25 16:46:20 +000036 fBitmap.allocN32Pixels(80, 80);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000037 SkCanvas canvas(fBitmap);
sugoi@google.com781cc762013-01-15 15:40:19 +000038 canvas.clear(0x00000000);
39 SkPaint paint;
40 paint.setAntiAlias(true);
caryclark5fb6bd42014-06-23 11:25:00 -070041 sk_tool_utils::set_portable_typeface(&paint);
sugoi@google.com781cc762013-01-15 15:40:19 +000042 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.org5e79c2b2013-12-12 21:48:32 +000048 void make_checkerboard(SkBitmap* bitmap, int w, int h) {
reed@google.comeb9a46c2014-01-25 16:46:20 +000049 bitmap->allocN32Pixels(w, h);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000050 SkCanvas canvas(*bitmap);
halcanaryb0cce2c2015-01-26 12:49:00 -080051 sk_tool_utils::draw_checkerboard(&canvas, 0xFF244484, 0xFF804020, 8);
sugoi@google.com781cc762013-01-15 15:40:19 +000052 }
53
54 virtual SkISize onISize() {
tfarinaf5393182014-06-09 23:59:03 -070055 return SkISize::Make(500, 500);
sugoi@google.com781cc762013-01-15 15:40:19 +000056 }
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +000057
sugoi@google.com781cc762013-01-15 15:40:19 +000058 void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
59 canvas->save();
senorblanco@chromium.orgbc386ce2013-10-15 19:30:58 +000060 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000061 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
62 canvas->drawBitmap(fBitmap, 0, 0, &paint);
sugoi@google.com781cc762013-01-15 15:40:19 +000063 canvas->restore();
64 }
65
66 virtual void onDraw(SkCanvas* canvas) {
67 if (!fInitialized) {
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +000068 make_bitmaps();
sugoi@google.com781cc762013-01-15 15:40:19 +000069 fInitialized = true;
70 }
71 canvas->clear(0x00000000);
72 SkPaint paint;
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000073 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.com781cc762013-01-15 15:40:19 +000077 drawClippedBitmap(canvas, 0, 0, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000078 paint.setImageFilter(SkDisplacementMapEffect::Create(
79 SkDisplacementMapEffect::kB_ChannelSelectorType,
80 SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +000081 drawClippedBitmap(canvas, 100, 0, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000082 paint.setImageFilter(SkDisplacementMapEffect::Create(
83 SkDisplacementMapEffect::kR_ChannelSelectorType,
84 SkDisplacementMapEffect::kB_ChannelSelectorType, 32.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +000085 drawClippedBitmap(canvas, 200, 0, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000086 paint.setImageFilter(SkDisplacementMapEffect::Create(
87 SkDisplacementMapEffect::kG_ChannelSelectorType,
88 SkDisplacementMapEffect::kA_ChannelSelectorType, 48.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +000089 drawClippedBitmap(canvas, 300, 0, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000090 paint.setImageFilter(SkDisplacementMapEffect::Create(
91 SkDisplacementMapEffect::kR_ChannelSelectorType,
92 SkDisplacementMapEffect::kA_ChannelSelectorType, 64.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +000093 drawClippedBitmap(canvas, 400, 0, paint);
94
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000095 paint.setImageFilter(SkDisplacementMapEffect::Create(
96 SkDisplacementMapEffect::kR_ChannelSelectorType,
97 SkDisplacementMapEffect::kG_ChannelSelectorType, 40.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +000098 drawClippedBitmap(canvas, 0, 100, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000099 paint.setImageFilter(SkDisplacementMapEffect::Create(
100 SkDisplacementMapEffect::kB_ChannelSelectorType,
101 SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000102 drawClippedBitmap(canvas, 100, 100, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000103 paint.setImageFilter(SkDisplacementMapEffect::Create(
104 SkDisplacementMapEffect::kR_ChannelSelectorType,
105 SkDisplacementMapEffect::kB_ChannelSelectorType, 40.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000106 drawClippedBitmap(canvas, 200, 100, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000107 paint.setImageFilter(SkDisplacementMapEffect::Create(
108 SkDisplacementMapEffect::kG_ChannelSelectorType,
109 SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000110 drawClippedBitmap(canvas, 300, 100, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000111 paint.setImageFilter(SkDisplacementMapEffect::Create(
112 SkDisplacementMapEffect::kR_ChannelSelectorType,
113 SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000114 drawClippedBitmap(canvas, 400, 100, paint);
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000115
116 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(30, 30, 40, 40));
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000117 paint.setImageFilter(SkDisplacementMapEffect::Create(
118 SkDisplacementMapEffect::kR_ChannelSelectorType,
119 SkDisplacementMapEffect::kG_ChannelSelectorType,
120 0.0f, displ, NULL, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000121 drawClippedBitmap(canvas, 0, 200, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000122 paint.setImageFilter(SkDisplacementMapEffect::Create(
123 SkDisplacementMapEffect::kB_ChannelSelectorType,
124 SkDisplacementMapEffect::kA_ChannelSelectorType,
125 16.0f, displ, NULL, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000126 drawClippedBitmap(canvas, 100, 200, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000127 paint.setImageFilter(SkDisplacementMapEffect::Create(
128 SkDisplacementMapEffect::kR_ChannelSelectorType,
129 SkDisplacementMapEffect::kB_ChannelSelectorType,
130 32.0f, displ, NULL, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000131 drawClippedBitmap(canvas, 200, 200, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000132 paint.setImageFilter(SkDisplacementMapEffect::Create(
133 SkDisplacementMapEffect::kG_ChannelSelectorType,
134 SkDisplacementMapEffect::kA_ChannelSelectorType,
135 48.0f, displ, NULL, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000136 drawClippedBitmap(canvas, 300, 200, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000137 paint.setImageFilter(SkDisplacementMapEffect::Create(
138 SkDisplacementMapEffect::kR_ChannelSelectorType,
139 SkDisplacementMapEffect::kA_ChannelSelectorType,
140 64.0f, displ, NULL, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000141 drawClippedBitmap(canvas, 400, 200, paint);
142
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000143 paint.setImageFilter(SkDisplacementMapEffect::Create(
144 SkDisplacementMapEffect::kR_ChannelSelectorType,
145 SkDisplacementMapEffect::kG_ChannelSelectorType,
146 40.0f, displ, NULL, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000147 drawClippedBitmap(canvas, 0, 300, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000148 paint.setImageFilter(SkDisplacementMapEffect::Create(
149 SkDisplacementMapEffect::kB_ChannelSelectorType,
150 SkDisplacementMapEffect::kA_ChannelSelectorType,
151 40.0f, displ, NULL, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000152 drawClippedBitmap(canvas, 100, 300, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000153 paint.setImageFilter(SkDisplacementMapEffect::Create(
154 SkDisplacementMapEffect::kR_ChannelSelectorType,
155 SkDisplacementMapEffect::kB_ChannelSelectorType,
156 40.0f, displ, NULL, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000157 drawClippedBitmap(canvas, 200, 300, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000158 paint.setImageFilter(SkDisplacementMapEffect::Create(
159 SkDisplacementMapEffect::kG_ChannelSelectorType,
160 SkDisplacementMapEffect::kA_ChannelSelectorType,
161 40.0f, displ, NULL, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000162 drawClippedBitmap(canvas, 300, 300, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000163 paint.setImageFilter(SkDisplacementMapEffect::Create(
164 SkDisplacementMapEffect::kR_ChannelSelectorType,
165 SkDisplacementMapEffect::kA_ChannelSelectorType,
166 40.0f, displ, NULL, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000167 drawClippedBitmap(canvas, 400, 300, paint);
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +0000168
commit-bot@chromium.org6d7296a2013-12-19 17:00:46 +0000169 // Tests for images of different sizes
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000170 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.org5e79c2b2013-12-12 21:48:32 +0000175 drawClippedBitmap(canvas, 0, 400, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000176 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.org5e79c2b2013-12-12 21:48:32 +0000181 drawClippedBitmap(canvas, 100, 400, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000182 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.org5e79c2b2013-12-12 21:48:32 +0000187 drawClippedBitmap(canvas, 200, 400, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000188 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.org5e79c2b2013-12-12 21:48:32 +0000193 drawClippedBitmap(canvas, 300, 400, paint);
commit-bot@chromium.org6d7296a2013-12-19 17:00:46 +0000194
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.orgcac5fd52014-03-10 10:51:58 +0000198 paint.setImageFilter(SkDisplacementMapEffect::Create(
199 SkDisplacementMapEffect::kG_ChannelSelectorType,
200 SkDisplacementMapEffect::kA_ChannelSelectorType,
201 40.0f, NULL))->unref();
commit-bot@chromium.org6d7296a2013-12-19 17:00:46 +0000202 drawClippedBitmap(canvas, 400, 400, paint);
sugoi@google.com781cc762013-01-15 15:40:19 +0000203 }
204
205private:
206 typedef GM INHERITED;
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +0000207 SkBitmap fBitmap, fCheckerboard, fSmall, fLarge, fLargeW, fLargeH;
sugoi@google.com781cc762013-01-15 15:40:19 +0000208 bool fInitialized;
209};
210
211//////////////////////////////////////////////////////////////////////////////
212
213static GM* MyFactory(void*) { return new DisplacementMapGM; }
214static GMRegistry reg(MyFactory);
215
216}