• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
12 namespace skiagm {
13 
14 class DisplacementMapGM : public GM {
15 public:
DisplacementMapGM()16     DisplacementMapGM() : fInitialized(false) {
17         this->setBGColor(0xFF000000);
18     }
19 
20 protected:
onShortName()21     virtual SkString onShortName() {
22         return SkString("displacement");
23     }
24 
make_bitmaps()25     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 
make_bitmap()34     void make_bitmap() {
35         fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 80, 80);
36         fBitmap.allocPixels();
37         SkBitmapDevice device(fBitmap);
38         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 
make_checkerboard(SkBitmap * bitmap,int w,int h)48     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);
52         SkCanvas canvas(&device);
53         canvas.clear(0x00000000);
54         SkPaint darkPaint;
55         darkPaint.setColor(0xFF804020);
56         SkPaint lightPaint;
57         lightPaint.setColor(0xFF244484);
58         for (int y = 0; y < h; y += 16) {
59           for (int x = 0; x < w; x += 16) {
60             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 
onISize()71     virtual SkISize onISize() {
72         return make_isize(500, 500);
73     }
74 
drawClippedBitmap(SkCanvas * canvas,int x,int y,const SkPaint & paint)75     void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
76         canvas->save();
77         canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
78         canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
79         canvas->drawBitmap(fBitmap, 0, 0, &paint);
80         canvas->restore();
81     }
82 
onDraw(SkCanvas * canvas)83     virtual void onDraw(SkCanvas* canvas) {
84         if (!fInitialized) {
85             make_bitmaps();
86             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,
97              SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ)))->unref();
98         drawClippedBitmap(canvas, 100, 0, paint);
99         paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
100             (SkDisplacementMapEffect::kR_ChannelSelectorType,
101              SkDisplacementMapEffect::kB_ChannelSelectorType, 32.0f, displ)))->unref();
102         drawClippedBitmap(canvas, 200, 0, paint);
103         paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
104             (SkDisplacementMapEffect::kG_ChannelSelectorType,
105              SkDisplacementMapEffect::kA_ChannelSelectorType, 48.0f, displ)))->unref();
106         drawClippedBitmap(canvas, 300, 0, paint);
107         paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
108             (SkDisplacementMapEffect::kR_ChannelSelectorType,
109              SkDisplacementMapEffect::kA_ChannelSelectorType, 64.0f, displ)))->unref();
110         drawClippedBitmap(canvas, 400, 0, paint);
111 
112         paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
113             (SkDisplacementMapEffect::kR_ChannelSelectorType,
114              SkDisplacementMapEffect::kG_ChannelSelectorType, 40.0f, displ)))->unref();
115         drawClippedBitmap(canvas, 0, 100, paint);
116         paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
117             (SkDisplacementMapEffect::kB_ChannelSelectorType,
118              SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ)))->unref();
119         drawClippedBitmap(canvas, 100, 100, paint);
120         paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
121             (SkDisplacementMapEffect::kR_ChannelSelectorType,
122              SkDisplacementMapEffect::kB_ChannelSelectorType, 40.0f, displ)))->unref();
123         drawClippedBitmap(canvas, 200, 100, paint);
124         paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
125             (SkDisplacementMapEffect::kG_ChannelSelectorType,
126              SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ)))->unref();
127         drawClippedBitmap(canvas, 300, 100, paint);
128         paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
129             (SkDisplacementMapEffect::kR_ChannelSelectorType,
130              SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ)))->unref();
131         drawClippedBitmap(canvas, 400, 100, paint);
132 
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);
185 
186         displ.reset(SkNEW_ARGS(SkBitmapSource, (fSmall)));
187         paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
188             (SkDisplacementMapEffect::kR_ChannelSelectorType,
189              SkDisplacementMapEffect::kG_ChannelSelectorType,
190              40.0f, displ)))->unref();
191         drawClippedBitmap(canvas, 0, 400, paint);
192         displ.reset(SkNEW_ARGS(SkBitmapSource, (fLarge)));
193         paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
194             (SkDisplacementMapEffect::kB_ChannelSelectorType,
195              SkDisplacementMapEffect::kA_ChannelSelectorType,
196              40.0f, displ)))->unref();
197         drawClippedBitmap(canvas, 100, 400, paint);
198         displ.reset(SkNEW_ARGS(SkBitmapSource, (fLargeW)));
199         paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
200             (SkDisplacementMapEffect::kR_ChannelSelectorType,
201              SkDisplacementMapEffect::kB_ChannelSelectorType,
202              40.0f, displ)))->unref();
203         drawClippedBitmap(canvas, 200, 400, paint);
204         displ.reset(SkNEW_ARGS(SkBitmapSource, (fLargeH)));
205         paint.setImageFilter(SkNEW_ARGS(SkDisplacementMapEffect,
206             (SkDisplacementMapEffect::kG_ChannelSelectorType,
207              SkDisplacementMapEffect::kA_ChannelSelectorType,
208              40.0f, displ)))->unref();
209         drawClippedBitmap(canvas, 300, 400, paint);
210     }
211 
212 private:
213     typedef GM INHERITED;
214     SkBitmap fBitmap, fCheckerboard, fSmall, fLarge, fLargeW, fLargeH;
215     bool fInitialized;
216 };
217 
218 //////////////////////////////////////////////////////////////////////////////
219 
MyFactory(void *)220 static GM* MyFactory(void*) { return new DisplacementMapGM; }
221 static GMRegistry reg(MyFactory);
222 
223 }
224