• 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.allocN32Pixels(80, 80);
36         SkCanvas canvas(fBitmap);
37         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 
make_checkerboard(SkBitmap * bitmap,int w,int h)46     void make_checkerboard(SkBitmap* bitmap, int w, int h) {
47         bitmap->allocN32Pixels(w, h);
48         SkCanvas canvas(*bitmap);
49         canvas.clear(0x00000000);
50         SkPaint darkPaint;
51         darkPaint.setColor(0xFF804020);
52         SkPaint lightPaint;
53         lightPaint.setColor(0xFF244484);
54         for (int y = 0; y < h; y += 16) {
55           for (int x = 0; x < w; x += 16) {
56             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 
onISize()67     virtual SkISize onISize() {
68         return SkISize::Make(500, 500);
69     }
70 
drawClippedBitmap(SkCanvas * canvas,int x,int y,const SkPaint & paint)71     void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
72         canvas->save();
73         canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
74         canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
75         canvas->drawBitmap(fBitmap, 0, 0, &paint);
76         canvas->restore();
77     }
78 
onDraw(SkCanvas * canvas)79     virtual void onDraw(SkCanvas* canvas) {
80         if (!fInitialized) {
81             make_bitmaps();
82             fInitialized = true;
83         }
84         canvas->clear(0x00000000);
85         SkPaint paint;
86         SkAutoTUnref<SkImageFilter> displ(SkBitmapSource::Create(fCheckerboard));
87         paint.setImageFilter(SkDisplacementMapEffect::Create(
88             SkDisplacementMapEffect::kR_ChannelSelectorType,
89             SkDisplacementMapEffect::kG_ChannelSelectorType, 0.0f, displ))->unref();
90         drawClippedBitmap(canvas, 0, 0, paint);
91         paint.setImageFilter(SkDisplacementMapEffect::Create(
92             SkDisplacementMapEffect::kB_ChannelSelectorType,
93             SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ))->unref();
94         drawClippedBitmap(canvas, 100, 0, paint);
95         paint.setImageFilter(SkDisplacementMapEffect::Create(
96             SkDisplacementMapEffect::kR_ChannelSelectorType,
97             SkDisplacementMapEffect::kB_ChannelSelectorType, 32.0f, displ))->unref();
98         drawClippedBitmap(canvas, 200, 0, paint);
99         paint.setImageFilter(SkDisplacementMapEffect::Create(
100             SkDisplacementMapEffect::kG_ChannelSelectorType,
101             SkDisplacementMapEffect::kA_ChannelSelectorType, 48.0f, displ))->unref();
102         drawClippedBitmap(canvas, 300, 0, paint);
103         paint.setImageFilter(SkDisplacementMapEffect::Create(
104             SkDisplacementMapEffect::kR_ChannelSelectorType,
105             SkDisplacementMapEffect::kA_ChannelSelectorType, 64.0f, displ))->unref();
106         drawClippedBitmap(canvas, 400, 0, paint);
107 
108         paint.setImageFilter(SkDisplacementMapEffect::Create(
109             SkDisplacementMapEffect::kR_ChannelSelectorType,
110             SkDisplacementMapEffect::kG_ChannelSelectorType, 40.0f, displ))->unref();
111         drawClippedBitmap(canvas, 0, 100, paint);
112         paint.setImageFilter(SkDisplacementMapEffect::Create(
113             SkDisplacementMapEffect::kB_ChannelSelectorType,
114             SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ))->unref();
115         drawClippedBitmap(canvas, 100, 100, paint);
116         paint.setImageFilter(SkDisplacementMapEffect::Create(
117             SkDisplacementMapEffect::kR_ChannelSelectorType,
118             SkDisplacementMapEffect::kB_ChannelSelectorType, 40.0f, displ))->unref();
119         drawClippedBitmap(canvas, 200, 100, paint);
120         paint.setImageFilter(SkDisplacementMapEffect::Create(
121             SkDisplacementMapEffect::kG_ChannelSelectorType,
122             SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ))->unref();
123         drawClippedBitmap(canvas, 300, 100, paint);
124         paint.setImageFilter(SkDisplacementMapEffect::Create(
125             SkDisplacementMapEffect::kR_ChannelSelectorType,
126             SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ))->unref();
127         drawClippedBitmap(canvas, 400, 100, paint);
128 
129         SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(30, 30, 40, 40));
130         paint.setImageFilter(SkDisplacementMapEffect::Create(
131             SkDisplacementMapEffect::kR_ChannelSelectorType,
132             SkDisplacementMapEffect::kG_ChannelSelectorType,
133             0.0f, displ, NULL, &cropRect))->unref();
134         drawClippedBitmap(canvas, 0, 200, paint);
135         paint.setImageFilter(SkDisplacementMapEffect::Create(
136             SkDisplacementMapEffect::kB_ChannelSelectorType,
137             SkDisplacementMapEffect::kA_ChannelSelectorType,
138             16.0f, displ, NULL, &cropRect))->unref();
139         drawClippedBitmap(canvas, 100, 200, paint);
140         paint.setImageFilter(SkDisplacementMapEffect::Create(
141             SkDisplacementMapEffect::kR_ChannelSelectorType,
142             SkDisplacementMapEffect::kB_ChannelSelectorType,
143             32.0f, displ, NULL, &cropRect))->unref();
144         drawClippedBitmap(canvas, 200, 200, paint);
145         paint.setImageFilter(SkDisplacementMapEffect::Create(
146             SkDisplacementMapEffect::kG_ChannelSelectorType,
147             SkDisplacementMapEffect::kA_ChannelSelectorType,
148             48.0f, displ, NULL, &cropRect))->unref();
149         drawClippedBitmap(canvas, 300, 200, paint);
150         paint.setImageFilter(SkDisplacementMapEffect::Create(
151             SkDisplacementMapEffect::kR_ChannelSelectorType,
152             SkDisplacementMapEffect::kA_ChannelSelectorType,
153             64.0f, displ, NULL, &cropRect))->unref();
154         drawClippedBitmap(canvas, 400, 200, paint);
155 
156         paint.setImageFilter(SkDisplacementMapEffect::Create(
157             SkDisplacementMapEffect::kR_ChannelSelectorType,
158             SkDisplacementMapEffect::kG_ChannelSelectorType,
159             40.0f, displ, NULL, &cropRect))->unref();
160         drawClippedBitmap(canvas, 0, 300, paint);
161         paint.setImageFilter(SkDisplacementMapEffect::Create(
162             SkDisplacementMapEffect::kB_ChannelSelectorType,
163             SkDisplacementMapEffect::kA_ChannelSelectorType,
164             40.0f, displ, NULL, &cropRect))->unref();
165         drawClippedBitmap(canvas, 100, 300, paint);
166         paint.setImageFilter(SkDisplacementMapEffect::Create(
167             SkDisplacementMapEffect::kR_ChannelSelectorType,
168             SkDisplacementMapEffect::kB_ChannelSelectorType,
169             40.0f, displ, NULL, &cropRect))->unref();
170         drawClippedBitmap(canvas, 200, 300, paint);
171         paint.setImageFilter(SkDisplacementMapEffect::Create(
172             SkDisplacementMapEffect::kG_ChannelSelectorType,
173             SkDisplacementMapEffect::kA_ChannelSelectorType,
174             40.0f, displ, NULL, &cropRect))->unref();
175         drawClippedBitmap(canvas, 300, 300, paint);
176         paint.setImageFilter(SkDisplacementMapEffect::Create(
177             SkDisplacementMapEffect::kR_ChannelSelectorType,
178             SkDisplacementMapEffect::kA_ChannelSelectorType,
179             40.0f, displ, NULL, &cropRect))->unref();
180         drawClippedBitmap(canvas, 400, 300, paint);
181 
182         // Tests for images of different sizes
183         displ.reset(SkBitmapSource::Create(fSmall));
184         paint.setImageFilter(SkDisplacementMapEffect::Create(
185             SkDisplacementMapEffect::kR_ChannelSelectorType,
186             SkDisplacementMapEffect::kG_ChannelSelectorType,
187             40.0f, displ))->unref();
188         drawClippedBitmap(canvas, 0, 400, paint);
189         displ.reset(SkBitmapSource::Create(fLarge));
190         paint.setImageFilter(SkDisplacementMapEffect::Create(
191             SkDisplacementMapEffect::kB_ChannelSelectorType,
192             SkDisplacementMapEffect::kA_ChannelSelectorType,
193             40.0f, displ))->unref();
194         drawClippedBitmap(canvas, 100, 400, paint);
195         displ.reset(SkBitmapSource::Create(fLargeW));
196         paint.setImageFilter(SkDisplacementMapEffect::Create(
197             SkDisplacementMapEffect::kR_ChannelSelectorType,
198             SkDisplacementMapEffect::kB_ChannelSelectorType,
199             40.0f, displ))->unref();
200         drawClippedBitmap(canvas, 200, 400, paint);
201         displ.reset(SkBitmapSource::Create(fLargeH));
202         paint.setImageFilter(SkDisplacementMapEffect::Create(
203             SkDisplacementMapEffect::kG_ChannelSelectorType,
204             SkDisplacementMapEffect::kA_ChannelSelectorType,
205             40.0f, displ))->unref();
206         drawClippedBitmap(canvas, 300, 400, paint);
207 
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.
211         paint.setImageFilter(SkDisplacementMapEffect::Create(
212             SkDisplacementMapEffect::kG_ChannelSelectorType,
213             SkDisplacementMapEffect::kA_ChannelSelectorType,
214             40.0f, NULL))->unref();
215         drawClippedBitmap(canvas, 400, 400, paint);
216     }
217 
218 private:
219     typedef GM INHERITED;
220     SkBitmap fBitmap, fCheckerboard, fSmall, fLarge, fLargeW, fLargeH;
221     bool fInitialized;
222 };
223 
224 //////////////////////////////////////////////////////////////////////////////
225 
MyFactory(void *)226 static GM* MyFactory(void*) { return new DisplacementMapGM; }
227 static GMRegistry reg(MyFactory);
228 
229 }
230