blob: af7c772532e21e7f596ab1d176ee1a6134d40651 [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
8#include "gm.h"
9#include "SkDisplacementMapEffect.h"
10#include "SkBitmapSource.h"
11
12namespace skiagm {
13
14class DisplacementMapGM : public GM {
15public:
16 DisplacementMapGM() : fInitialized(false) {
17 this->setBGColor(0xFF000000);
18 }
19
20protected:
21 virtual SkString onShortName() {
22 return SkString("displacement");
23 }
24
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +000025 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.com781cc762013-01-15 15:40:19 +000034 void make_bitmap() {
reed@google.comeb9a46c2014-01-25 16:46:20 +000035 fBitmap.allocN32Pixels(80, 80);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000036 SkCanvas canvas(fBitmap);
sugoi@google.com781cc762013-01-15 15:40:19 +000037 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.org5e79c2b2013-12-12 21:48:32 +000046 void make_checkerboard(SkBitmap* bitmap, int w, int h) {
reed@google.comeb9a46c2014-01-25 16:46:20 +000047 bitmap->allocN32Pixels(w, h);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000048 SkCanvas canvas(*bitmap);
sugoi@google.com781cc762013-01-15 15:40:19 +000049 canvas.clear(0x00000000);
50 SkPaint darkPaint;
51 darkPaint.setColor(0xFF804020);
52 SkPaint lightPaint;
53 lightPaint.setColor(0xFF244484);
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +000054 for (int y = 0; y < h; y += 16) {
55 for (int x = 0; x < w; x += 16) {
sugoi@google.com781cc762013-01-15 15:40:19 +000056 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.org5e79c2b2013-12-12 21:48:32 +000068 return make_isize(500, 500);
sugoi@google.com781cc762013-01-15 15:40:19 +000069 }
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +000070
sugoi@google.com781cc762013-01-15 15:40:19 +000071 void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
72 canvas->save();
senorblanco@chromium.orgbc386ce2013-10-15 19:30:58 +000073 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000074 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
75 canvas->drawBitmap(fBitmap, 0, 0, &paint);
sugoi@google.com781cc762013-01-15 15:40:19 +000076 canvas->restore();
77 }
78
79 virtual void onDraw(SkCanvas* canvas) {
80 if (!fInitialized) {
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +000081 make_bitmaps();
sugoi@google.com781cc762013-01-15 15:40:19 +000082 fInitialized = true;
83 }
84 canvas->clear(0x00000000);
85 SkPaint paint;
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000086 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.com781cc762013-01-15 15:40:19 +000090 drawClippedBitmap(canvas, 0, 0, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000091 paint.setImageFilter(SkDisplacementMapEffect::Create(
92 SkDisplacementMapEffect::kB_ChannelSelectorType,
93 SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +000094 drawClippedBitmap(canvas, 100, 0, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000095 paint.setImageFilter(SkDisplacementMapEffect::Create(
96 SkDisplacementMapEffect::kR_ChannelSelectorType,
97 SkDisplacementMapEffect::kB_ChannelSelectorType, 32.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +000098 drawClippedBitmap(canvas, 200, 0, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +000099 paint.setImageFilter(SkDisplacementMapEffect::Create(
100 SkDisplacementMapEffect::kG_ChannelSelectorType,
101 SkDisplacementMapEffect::kA_ChannelSelectorType, 48.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000102 drawClippedBitmap(canvas, 300, 0, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000103 paint.setImageFilter(SkDisplacementMapEffect::Create(
104 SkDisplacementMapEffect::kR_ChannelSelectorType,
105 SkDisplacementMapEffect::kA_ChannelSelectorType, 64.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000106 drawClippedBitmap(canvas, 400, 0, paint);
107
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000108 paint.setImageFilter(SkDisplacementMapEffect::Create(
109 SkDisplacementMapEffect::kR_ChannelSelectorType,
110 SkDisplacementMapEffect::kG_ChannelSelectorType, 40.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000111 drawClippedBitmap(canvas, 0, 100, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000112 paint.setImageFilter(SkDisplacementMapEffect::Create(
113 SkDisplacementMapEffect::kB_ChannelSelectorType,
114 SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000115 drawClippedBitmap(canvas, 100, 100, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000116 paint.setImageFilter(SkDisplacementMapEffect::Create(
117 SkDisplacementMapEffect::kR_ChannelSelectorType,
118 SkDisplacementMapEffect::kB_ChannelSelectorType, 40.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000119 drawClippedBitmap(canvas, 200, 100, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000120 paint.setImageFilter(SkDisplacementMapEffect::Create(
121 SkDisplacementMapEffect::kG_ChannelSelectorType,
122 SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000123 drawClippedBitmap(canvas, 300, 100, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000124 paint.setImageFilter(SkDisplacementMapEffect::Create(
125 SkDisplacementMapEffect::kR_ChannelSelectorType,
126 SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000127 drawClippedBitmap(canvas, 400, 100, paint);
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000128
129 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(30, 30, 40, 40));
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000130 paint.setImageFilter(SkDisplacementMapEffect::Create(
131 SkDisplacementMapEffect::kR_ChannelSelectorType,
132 SkDisplacementMapEffect::kG_ChannelSelectorType,
133 0.0f, displ, NULL, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000134 drawClippedBitmap(canvas, 0, 200, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000135 paint.setImageFilter(SkDisplacementMapEffect::Create(
136 SkDisplacementMapEffect::kB_ChannelSelectorType,
137 SkDisplacementMapEffect::kA_ChannelSelectorType,
138 16.0f, displ, NULL, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000139 drawClippedBitmap(canvas, 100, 200, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000140 paint.setImageFilter(SkDisplacementMapEffect::Create(
141 SkDisplacementMapEffect::kR_ChannelSelectorType,
142 SkDisplacementMapEffect::kB_ChannelSelectorType,
143 32.0f, displ, NULL, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000144 drawClippedBitmap(canvas, 200, 200, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000145 paint.setImageFilter(SkDisplacementMapEffect::Create(
146 SkDisplacementMapEffect::kG_ChannelSelectorType,
147 SkDisplacementMapEffect::kA_ChannelSelectorType,
148 48.0f, displ, NULL, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000149 drawClippedBitmap(canvas, 300, 200, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000150 paint.setImageFilter(SkDisplacementMapEffect::Create(
151 SkDisplacementMapEffect::kR_ChannelSelectorType,
152 SkDisplacementMapEffect::kA_ChannelSelectorType,
153 64.0f, displ, NULL, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000154 drawClippedBitmap(canvas, 400, 200, paint);
155
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000156 paint.setImageFilter(SkDisplacementMapEffect::Create(
157 SkDisplacementMapEffect::kR_ChannelSelectorType,
158 SkDisplacementMapEffect::kG_ChannelSelectorType,
159 40.0f, displ, NULL, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000160 drawClippedBitmap(canvas, 0, 300, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000161 paint.setImageFilter(SkDisplacementMapEffect::Create(
162 SkDisplacementMapEffect::kB_ChannelSelectorType,
163 SkDisplacementMapEffect::kA_ChannelSelectorType,
164 40.0f, displ, NULL, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000165 drawClippedBitmap(canvas, 100, 300, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000166 paint.setImageFilter(SkDisplacementMapEffect::Create(
167 SkDisplacementMapEffect::kR_ChannelSelectorType,
168 SkDisplacementMapEffect::kB_ChannelSelectorType,
169 40.0f, displ, NULL, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000170 drawClippedBitmap(canvas, 200, 300, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000171 paint.setImageFilter(SkDisplacementMapEffect::Create(
172 SkDisplacementMapEffect::kG_ChannelSelectorType,
173 SkDisplacementMapEffect::kA_ChannelSelectorType,
174 40.0f, displ, NULL, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000175 drawClippedBitmap(canvas, 300, 300, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000176 paint.setImageFilter(SkDisplacementMapEffect::Create(
177 SkDisplacementMapEffect::kR_ChannelSelectorType,
178 SkDisplacementMapEffect::kA_ChannelSelectorType,
179 40.0f, displ, NULL, &cropRect))->unref();
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000180 drawClippedBitmap(canvas, 400, 300, paint);
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +0000181
commit-bot@chromium.org6d7296a2013-12-19 17:00:46 +0000182 // Tests for images of different sizes
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000183 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.org5e79c2b2013-12-12 21:48:32 +0000188 drawClippedBitmap(canvas, 0, 400, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000189 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.org5e79c2b2013-12-12 21:48:32 +0000194 drawClippedBitmap(canvas, 100, 400, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000195 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.org5e79c2b2013-12-12 21:48:32 +0000200 drawClippedBitmap(canvas, 200, 400, paint);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000201 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.org5e79c2b2013-12-12 21:48:32 +0000206 drawClippedBitmap(canvas, 300, 400, paint);
commit-bot@chromium.org6d7296a2013-12-19 17:00:46 +0000207
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.orgcac5fd52014-03-10 10:51:58 +0000211 paint.setImageFilter(SkDisplacementMapEffect::Create(
212 SkDisplacementMapEffect::kG_ChannelSelectorType,
213 SkDisplacementMapEffect::kA_ChannelSelectorType,
214 40.0f, NULL))->unref();
commit-bot@chromium.org6d7296a2013-12-19 17:00:46 +0000215 drawClippedBitmap(canvas, 400, 400, paint);
sugoi@google.com781cc762013-01-15 15:40:19 +0000216 }
217
218private:
219 typedef GM INHERITED;
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +0000220 SkBitmap fBitmap, fCheckerboard, fSmall, fLarge, fLargeW, fLargeH;
sugoi@google.com781cc762013-01-15 15:40:19 +0000221 bool fInitialized;
222};
223
224//////////////////////////////////////////////////////////////////////////////
225
226static GM* MyFactory(void*) { return new DisplacementMapGM; }
227static GMRegistry reg(MyFactory);
228
229}