blob: cc664012da6e3612baec272d123d33259bb8bc39 [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() {
35 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 80, 80);
36 fBitmap.allocPixels();
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000037 SkBitmapDevice device(fBitmap);
sugoi@google.com781cc762013-01-15 15:40:19 +000038 SkCanvas canvas(&device);
39 canvas.clear(0x00000000);
40 SkPaint paint;
41 paint.setAntiAlias(true);
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.org5e79c2b2013-12-12 21:48:32 +000048 void make_checkerboard(SkBitmap* bitmap, int w, int h) {
49 bitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h);
50 bitmap->allocPixels();
51 SkBitmapDevice device(*bitmap);
sugoi@google.com781cc762013-01-15 15:40:19 +000052 SkCanvas canvas(&device);
53 canvas.clear(0x00000000);
54 SkPaint darkPaint;
55 darkPaint.setColor(0xFF804020);
56 SkPaint lightPaint;
57 lightPaint.setColor(0xFF244484);
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +000058 for (int y = 0; y < h; y += 16) {
59 for (int x = 0; x < w; x += 16) {
sugoi@google.com781cc762013-01-15 15:40:19 +000060 canvas.save();
61 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
62 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
63 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
64 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
65 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
66 canvas.restore();
67 }
68 }
69 }
70
71 virtual SkISize onISize() {
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +000072 return make_isize(500, 500);
sugoi@google.com781cc762013-01-15 15:40:19 +000073 }
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +000074
sugoi@google.com781cc762013-01-15 15:40:19 +000075 void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
76 canvas->save();
senorblanco@chromium.orgbc386ce2013-10-15 19:30:58 +000077 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +000078 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
79 canvas->drawBitmap(fBitmap, 0, 0, &paint);
sugoi@google.com781cc762013-01-15 15:40:19 +000080 canvas->restore();
81 }
82
83 virtual void onDraw(SkCanvas* canvas) {
84 if (!fInitialized) {
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +000085 make_bitmaps();
sugoi@google.com781cc762013-01-15 15:40:19 +000086 fInitialized = true;
87 }
88 canvas->clear(0x00000000);
89 SkPaint paint;
90 SkAutoTUnref<SkImageFilter> displ(SkNEW_ARGS(SkBitmapSource, (fCheckerboard)));
91 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
92 (SkDisplacementMapEffect::kR_ChannelSelectorType,
93 SkDisplacementMapEffect::kG_ChannelSelectorType, 0.0f, displ)))->unref();
94 drawClippedBitmap(canvas, 0, 0, paint);
95 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
96 (SkDisplacementMapEffect::kB_ChannelSelectorType,
sugoi@google.com4ecd42e2013-03-19 15:07:30 +000097 SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ)))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +000098 drawClippedBitmap(canvas, 100, 0, paint);
99 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
100 (SkDisplacementMapEffect::kR_ChannelSelectorType,
sugoi@google.com4ecd42e2013-03-19 15:07:30 +0000101 SkDisplacementMapEffect::kB_ChannelSelectorType, 32.0f, displ)))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000102 drawClippedBitmap(canvas, 200, 0, paint);
103 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
104 (SkDisplacementMapEffect::kG_ChannelSelectorType,
sugoi@google.com4ecd42e2013-03-19 15:07:30 +0000105 SkDisplacementMapEffect::kA_ChannelSelectorType, 48.0f, displ)))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000106 drawClippedBitmap(canvas, 300, 0, paint);
107 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
108 (SkDisplacementMapEffect::kR_ChannelSelectorType,
sugoi@google.com4ecd42e2013-03-19 15:07:30 +0000109 SkDisplacementMapEffect::kA_ChannelSelectorType, 64.0f, displ)))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000110 drawClippedBitmap(canvas, 400, 0, paint);
111
112 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
113 (SkDisplacementMapEffect::kR_ChannelSelectorType,
sugoi@google.com4ecd42e2013-03-19 15:07:30 +0000114 SkDisplacementMapEffect::kG_ChannelSelectorType, 40.0f, displ)))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000115 drawClippedBitmap(canvas, 0, 100, paint);
116 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
117 (SkDisplacementMapEffect::kB_ChannelSelectorType,
sugoi@google.com4ecd42e2013-03-19 15:07:30 +0000118 SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ)))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000119 drawClippedBitmap(canvas, 100, 100, paint);
120 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
121 (SkDisplacementMapEffect::kR_ChannelSelectorType,
sugoi@google.com4ecd42e2013-03-19 15:07:30 +0000122 SkDisplacementMapEffect::kB_ChannelSelectorType, 40.0f, displ)))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000123 drawClippedBitmap(canvas, 200, 100, paint);
124 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
125 (SkDisplacementMapEffect::kG_ChannelSelectorType,
sugoi@google.com4ecd42e2013-03-19 15:07:30 +0000126 SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ)))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000127 drawClippedBitmap(canvas, 300, 100, paint);
128 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
129 (SkDisplacementMapEffect::kR_ChannelSelectorType,
sugoi@google.com4ecd42e2013-03-19 15:07:30 +0000130 SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ)))->unref();
sugoi@google.com781cc762013-01-15 15:40:19 +0000131 drawClippedBitmap(canvas, 400, 100, paint);
senorblanco@chromium.org01bdf3c2013-10-15 19:02:43 +0000132
133 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(30, 30, 40, 40));
134 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
135 (SkDisplacementMapEffect::kR_ChannelSelectorType,
136 SkDisplacementMapEffect::kG_ChannelSelectorType,
137 0.0f, displ, NULL, &cropRect)))->unref();
138 drawClippedBitmap(canvas, 0, 200, paint);
139 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
140 (SkDisplacementMapEffect::kB_ChannelSelectorType,
141 SkDisplacementMapEffect::kA_ChannelSelectorType,
142 16.0f, displ, NULL, &cropRect)))->unref();
143 drawClippedBitmap(canvas, 100, 200, paint);
144 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
145 (SkDisplacementMapEffect::kR_ChannelSelectorType,
146 SkDisplacementMapEffect::kB_ChannelSelectorType,
147 32.0f, displ, NULL, &cropRect)))->unref();
148 drawClippedBitmap(canvas, 200, 200, paint);
149 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
150 (SkDisplacementMapEffect::kG_ChannelSelectorType,
151 SkDisplacementMapEffect::kA_ChannelSelectorType,
152 48.0f, displ, NULL, &cropRect)))->unref();
153 drawClippedBitmap(canvas, 300, 200, paint);
154 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
155 (SkDisplacementMapEffect::kR_ChannelSelectorType,
156 SkDisplacementMapEffect::kA_ChannelSelectorType,
157 64.0f, displ, NULL, &cropRect)))->unref();
158 drawClippedBitmap(canvas, 400, 200, paint);
159
160 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
161 (SkDisplacementMapEffect::kR_ChannelSelectorType,
162 SkDisplacementMapEffect::kG_ChannelSelectorType,
163 40.0f, displ, NULL, &cropRect)))->unref();
164 drawClippedBitmap(canvas, 0, 300, paint);
165 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
166 (SkDisplacementMapEffect::kB_ChannelSelectorType,
167 SkDisplacementMapEffect::kA_ChannelSelectorType,
168 40.0f, displ, NULL, &cropRect)))->unref();
169 drawClippedBitmap(canvas, 100, 300, paint);
170 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
171 (SkDisplacementMapEffect::kR_ChannelSelectorType,
172 SkDisplacementMapEffect::kB_ChannelSelectorType,
173 40.0f, displ, NULL, &cropRect)))->unref();
174 drawClippedBitmap(canvas, 200, 300, paint);
175 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
176 (SkDisplacementMapEffect::kG_ChannelSelectorType,
177 SkDisplacementMapEffect::kA_ChannelSelectorType,
178 40.0f, displ, NULL, &cropRect)))->unref();
179 drawClippedBitmap(canvas, 300, 300, paint);
180 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
181 (SkDisplacementMapEffect::kR_ChannelSelectorType,
182 SkDisplacementMapEffect::kA_ChannelSelectorType,
183 40.0f, displ, NULL, &cropRect)))->unref();
184 drawClippedBitmap(canvas, 400, 300, paint);
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +0000185
commit-bot@chromium.org6d7296a2013-12-19 17:00:46 +0000186 // Tests for images of different sizes
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +0000187 displ.reset(SkNEW_ARGS(SkBitmapSource, (fSmall)));
188 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
189 (SkDisplacementMapEffect::kR_ChannelSelectorType,
190 SkDisplacementMapEffect::kG_ChannelSelectorType,
191 40.0f, displ)))->unref();
192 drawClippedBitmap(canvas, 0, 400, paint);
193 displ.reset(SkNEW_ARGS(SkBitmapSource, (fLarge)));
194 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
195 (SkDisplacementMapEffect::kB_ChannelSelectorType,
196 SkDisplacementMapEffect::kA_ChannelSelectorType,
197 40.0f, displ)))->unref();
198 drawClippedBitmap(canvas, 100, 400, paint);
199 displ.reset(SkNEW_ARGS(SkBitmapSource, (fLargeW)));
200 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
201 (SkDisplacementMapEffect::kR_ChannelSelectorType,
202 SkDisplacementMapEffect::kB_ChannelSelectorType,
203 40.0f, displ)))->unref();
204 drawClippedBitmap(canvas, 200, 400, paint);
205 displ.reset(SkNEW_ARGS(SkBitmapSource, (fLargeH)));
206 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
207 (SkDisplacementMapEffect::kG_ChannelSelectorType,
208 SkDisplacementMapEffect::kA_ChannelSelectorType,
209 40.0f, displ)))->unref();
210 drawClippedBitmap(canvas, 300, 400, paint);
commit-bot@chromium.org6d7296a2013-12-19 17:00:46 +0000211
212 // Test for no given displacement input. In this case, both displacement
213 // and color should use the same bitmap, given to SkCanvas::drawBitmap()
214 // as an input argument.
215 paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
216 (SkDisplacementMapEffect::kG_ChannelSelectorType,
217 SkDisplacementMapEffect::kA_ChannelSelectorType,
218 40.0f, NULL)))->unref();
219 drawClippedBitmap(canvas, 400, 400, paint);
sugoi@google.com781cc762013-01-15 15:40:19 +0000220 }
221
222private:
223 typedef GM INHERITED;
commit-bot@chromium.org5e79c2b2013-12-12 21:48:32 +0000224 SkBitmap fBitmap, fCheckerboard, fSmall, fLarge, fLargeW, fLargeH;
sugoi@google.com781cc762013-01-15 15:40:19 +0000225 bool fInitialized;
226};
227
228//////////////////////////////////////////////////////////////////////////////
229
230static GM* MyFactory(void*) { return new DisplacementMapGM; }
231static GMRegistry reg(MyFactory);
232
233}