OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "gm.h" | 8 #include "gm.h" |
9 #include "SkArithmeticMode.h" | 9 #include "SkArithmeticMode.h" |
10 #include "SkOffsetImageFilter.h" | 10 #include "SkOffsetImageFilter.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
63 canvas.restore(); | 63 canvas.restore(); |
64 } | 64 } |
65 } | 65 } |
66 } | 66 } |
67 | 67 |
68 virtual SkISize onISize() { | 68 virtual SkISize onISize() { |
69 return make_isize(WIDTH, HEIGHT); | 69 return make_isize(WIDTH, HEIGHT); |
70 } | 70 } |
71 | 71 |
72 void drawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPai nt& paint, | 72 void drawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPai nt& paint, |
73 SkScalar x, SkScalar y) { | 73 int x, int y) { |
74 canvas->save(); | 74 canvas->save(); |
75 canvas->clipRect(SkRect::MakeXYWH(x, y, | 75 canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
76 canvas->clipRect(SkRect::MakeWH( | |
76 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()))); | 77 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()))); |
77 canvas->drawBitmap(bitmap, x, y, &paint); | 78 canvas->drawBitmap(bitmap, 0, 0, &paint); |
78 canvas->restore(); | 79 canvas->restore(); |
79 } | 80 } |
80 | 81 |
82 void drawClippedPaint(SkCanvas* canvas, const SkRect& rect, const SkPaint& p aint, | |
83 int x, int y) { | |
84 canvas->save(); | |
85 canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); | |
86 canvas->clipRect(rect); | |
87 canvas->drawPaint(paint); | |
88 canvas->restore(); | |
89 } | |
90 | |
81 virtual void onDraw(SkCanvas* canvas) { | 91 virtual void onDraw(SkCanvas* canvas) { |
82 if (!fInitialized) { | 92 if (!fInitialized) { |
robertphillips
2013/12/09 16:23:01
onOnceBeforeDraw?
Stephen White
2013/12/09 17:04:25
Done.
| |
83 make_bitmap(); | 93 make_bitmap(); |
84 make_checkerboard(); | 94 make_checkerboard(); |
85 fInitialized = true; | 95 fInitialized = true; |
86 } | 96 } |
87 canvas->clear(0x00000000); | 97 canvas->clear(0x00000000); |
88 SkPaint paint; | 98 SkPaint paint; |
89 | 99 |
90 const struct { | 100 const struct { |
91 SkXfermode::Mode fMode; | 101 SkXfermode::Mode fMode; |
92 const char* fLabel; | 102 const char* fLabel; |
(...skipping 29 matching lines...) Expand all Loading... | |
122 { SkXfermode::kColor_Mode, "Color" }, | 132 { SkXfermode::kColor_Mode, "Color" }, |
123 { SkXfermode::kLuminosity_Mode, "Luminosity" }, | 133 { SkXfermode::kLuminosity_Mode, "Luminosity" }, |
124 }; | 134 }; |
125 | 135 |
126 int x = 0, y = 0; | 136 int x = 0, y = 0; |
127 SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fChec kerboard))); | 137 SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fChec kerboard))); |
128 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) { | 138 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) { |
129 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(gModes[i].fMode)); | 139 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(gModes[i].fMode)); |
130 SkAutoTUnref<SkImageFilter> filter(SkNEW_ARGS( | 140 SkAutoTUnref<SkImageFilter> filter(SkNEW_ARGS( |
131 SkXfermodeImageFilter, (mode, background))); | 141 SkXfermodeImageFilter, (mode, background))); |
132 paint.setImageFilter(filter); | 142 paint.setImageFilter(filter); |
robertphillips
2013/12/09 16:23:01
this->
Stephen White
2013/12/09 17:04:25
Made static instead.
| |
133 drawClippedBitmap(canvas, fBitmap, paint, SkIntToScalar(x), SkIntToS calar(y)); | 143 drawClippedBitmap(canvas, fBitmap, paint, x, y); |
134 x += fBitmap.width() + MARGIN; | 144 x += fBitmap.width() + MARGIN; |
135 if (x + fBitmap.width() > WIDTH) { | 145 if (x + fBitmap.width() > WIDTH) { |
136 x = 0; | 146 x = 0; |
137 y += fBitmap.height() + MARGIN; | 147 y += fBitmap.height() + MARGIN; |
138 } | 148 } |
139 } | 149 } |
140 // Test arithmetic mode as image filter | 150 // Test arithmetic mode as image filter |
141 SkAutoTUnref<SkXfermode> mode(SkArithmeticMode::Create(0, SK_Scalar1, SK _Scalar1, 0)); | 151 SkAutoTUnref<SkXfermode> mode(SkArithmeticMode::Create(0, SK_Scalar1, SK _Scalar1, 0)); |
142 SkAutoTUnref<SkImageFilter> filter(SkNEW_ARGS(SkXfermodeImageFilter, (mo de, background))); | 152 SkAutoTUnref<SkImageFilter> filter(SkNEW_ARGS(SkXfermodeImageFilter, (mo de, background))); |
143 paint.setImageFilter(filter); | 153 paint.setImageFilter(filter); |
robertphillips
2013/12/09 16:23:01
this->
| |
144 drawClippedBitmap(canvas, fBitmap, paint, SkIntToScalar(x), SkIntToScala r(y)); | 154 drawClippedBitmap(canvas, fBitmap, paint, x, y); |
145 x += fBitmap.width() + MARGIN; | 155 x += fBitmap.width() + MARGIN; |
146 if (x + fBitmap.width() > WIDTH) { | 156 if (x + fBitmap.width() > WIDTH) { |
147 x = 0; | 157 x = 0; |
148 y += fBitmap.height() + MARGIN; | 158 y += fBitmap.height() + MARGIN; |
149 } | 159 } |
150 // Test NULL mode | 160 // Test NULL mode |
151 filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, (NULL, background))); | 161 filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, (NULL, background))); |
152 paint.setImageFilter(filter); | 162 paint.setImageFilter(filter); |
robertphillips
2013/12/09 16:23:01
this->
| |
153 drawClippedBitmap(canvas, fBitmap, paint, SkIntToScalar(x), SkIntToScala r(y)); | 163 drawClippedBitmap(canvas, fBitmap, paint, x, y); |
154 x += fBitmap.width() + MARGIN; | 164 x += fBitmap.width() + MARGIN; |
155 if (x + fBitmap.width() > WIDTH) { | 165 if (x + fBitmap.width() > WIDTH) { |
156 x = 0; | 166 x = 0; |
157 y += fBitmap.height() + MARGIN; | 167 y += fBitmap.height() + MARGIN; |
158 } | 168 } |
169 SkRect clipRect = SkRect::MakeWH(SkIntToScalar(fBitmap.width() + 4), | |
170 SkIntToScalar(fBitmap.height() + 4)); | |
159 // Test offsets on SrcMode (uses fixed-function blend) | 171 // Test offsets on SrcMode (uses fixed-function blend) |
160 SkAutoTUnref<SkImageFilter> foreground(SkNEW_ARGS(SkBitmapSource, (fBitm ap))); | 172 SkAutoTUnref<SkImageFilter> foreground(SkNEW_ARGS(SkBitmapSource, (fBitm ap))); |
161 SkAutoTUnref<SkImageFilter> offsetForeground(SkNEW_ARGS(SkOffsetImageFil ter, | 173 SkAutoTUnref<SkImageFilter> offsetForeground(SkNEW_ARGS(SkOffsetImageFil ter, |
162 (SkIntToScalar(4), SkIntToScalar(-4), foreground))); | 174 (SkIntToScalar(4), SkIntToScalar(-4), foreground))); |
163 SkAutoTUnref<SkImageFilter> offsetBackground(SkNEW_ARGS(SkOffsetImageFil ter, | 175 SkAutoTUnref<SkImageFilter> offsetBackground(SkNEW_ARGS(SkOffsetImageFil ter, |
164 (SkIntToScalar(4), SkIntToScalar(4), background))); | 176 (SkIntToScalar(4), SkIntToScalar(4), background))); |
165 mode.reset(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); | 177 mode.reset(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); |
166 filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, | 178 filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, |
167 (mode, offsetBackground, offsetForeground))); | 179 (mode, offsetBackground, offsetForeground))); |
168 paint.setImageFilter(filter); | 180 paint.setImageFilter(filter); |
robertphillips
2013/12/09 16:23:01
this-> on all drawClippedPaint calls
Stephen White
2013/12/09 17:04:25
See above.
| |
169 canvas->save(); | 181 drawClippedPaint(canvas, clipRect, paint, x, y); |
170 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), | |
171 SkIntToScalar(y), | |
172 SkIntToScalar(fBitmap.width() + 4), | |
173 SkIntToScalar(fBitmap.height() + 4))); | |
174 canvas->drawPaint(paint); | |
175 canvas->restore(); | |
176 x += fBitmap.width() + MARGIN; | 182 x += fBitmap.width() + MARGIN; |
177 if (x + fBitmap.width() > WIDTH) { | 183 if (x + fBitmap.width() > WIDTH) { |
178 x = 0; | 184 x = 0; |
179 y += fBitmap.height() + MARGIN; | 185 y += fBitmap.height() + MARGIN; |
180 } | 186 } |
181 // Test offsets on Darken (uses shader blend) | 187 // Test offsets on Darken (uses shader blend) |
182 mode.reset(SkXfermode::Create(SkXfermode::kDarken_Mode)); | 188 mode.reset(SkXfermode::Create(SkXfermode::kDarken_Mode)); |
183 filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, (mode, offsetBackground, offsetForeground))); | 189 filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, (mode, offsetBackground, offsetForeground))); |
184 paint.setImageFilter(filter); | 190 paint.setImageFilter(filter); |
185 canvas->save(); | 191 drawClippedPaint(canvas, clipRect, paint, x, y); |
186 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), | |
187 SkIntToScalar(y), | |
188 SkIntToScalar(fBitmap.width() + 4), | |
189 SkIntToScalar(fBitmap.height() + 4))); | |
190 canvas->drawPaint(paint); | |
191 canvas->restore(); | |
192 x += fBitmap.width() + MARGIN; | 192 x += fBitmap.width() + MARGIN; |
193 if (x + fBitmap.width() > WIDTH) { | 193 if (x + fBitmap.width() > WIDTH) { |
194 x = 0; | 194 x = 0; |
195 y += fBitmap.height() + MARGIN; | 195 y += fBitmap.height() + MARGIN; |
196 } | 196 } |
197 // Test cropping | 197 // Test cropping |
198 static const size_t nbSamples = 3; | 198 static const size_t nbSamples = 3; |
199 SkXfermode::Mode sampledModes[nbSamples] = {SkXfermode::kOverlay_Mode, | 199 SkXfermode::Mode sampledModes[nbSamples] = {SkXfermode::kOverlay_Mode, |
200 SkXfermode::kSrcOver_Mode, | 200 SkXfermode::kSrcOver_Mode, |
201 SkXfermode::kPlus_Mode}; | 201 SkXfermode::kPlus_Mode}; |
202 int offsets[nbSamples][4] = {{ 10, 10, -16, -16}, | 202 int offsets[nbSamples][4] = {{ 10, 10, -16, -16}, |
203 { 10, 10, 10, 10}, | 203 { 10, 10, 10, 10}, |
204 {-10, -10, -6, -6}}; | 204 {-10, -10, -6, -6}}; |
205 for (size_t i = 0; i < nbSamples; ++i) { | 205 for (size_t i = 0; i < nbSamples; ++i) { |
206 SkIRect cropRect = SkIRect::MakeXYWH(x + offsets[i][0], | 206 SkIRect cropRect = SkIRect::MakeXYWH(offsets[i][0], |
207 y + offsets[i][1], | 207 offsets[i][1], |
208 fBitmap.width() + offsets[i][2 ], | 208 fBitmap.width() + offsets[i][2 ], |
209 fBitmap.height() + offsets[i][3 ]); | 209 fBitmap.height() + offsets[i][3 ]); |
210 SkImageFilter::CropRect rect(SkRect::Make(cropRect)); | 210 SkImageFilter::CropRect rect(SkRect::Make(cropRect)); |
211 mode.reset(SkXfermode::Create(sampledModes[i])); | 211 mode.reset(SkXfermode::Create(sampledModes[i])); |
212 filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, | 212 filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, |
213 (mode, offsetBackground, offsetForeground, & rect))); | 213 (mode, offsetBackground, offsetForeground, & rect))); |
214 paint.setImageFilter(filter); | 214 paint.setImageFilter(filter); |
215 canvas->save(); | 215 drawClippedPaint(canvas, clipRect, paint, x, y); |
216 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), | |
217 SkIntToScalar(y), | |
218 SkIntToScalar(fBitmap.width() + 4) , | |
219 SkIntToScalar(fBitmap.height() + 4 ))); | |
220 canvas->drawPaint(paint); | |
221 canvas->restore(); | |
222 x += fBitmap.width() + MARGIN; | 216 x += fBitmap.width() + MARGIN; |
223 if (x + fBitmap.width() > WIDTH) { | 217 if (x + fBitmap.width() > WIDTH) { |
224 x = 0; | 218 x = 0; |
225 y += fBitmap.height() + MARGIN; | 219 y += fBitmap.height() + MARGIN; |
226 } | 220 } |
227 } | 221 } |
228 } | 222 } |
229 private: | 223 private: |
robertphillips
2013/12/09 16:23:01
last?
| |
230 typedef GM INHERITED; | 224 typedef GM INHERITED; |
231 SkBitmap fBitmap, fCheckerboard; | 225 SkBitmap fBitmap, fCheckerboard; |
232 bool fInitialized; | 226 bool fInitialized; |
233 }; | 227 }; |
234 | 228 |
235 ////////////////////////////////////////////////////////////////////////////// | 229 ////////////////////////////////////////////////////////////////////////////// |
236 | 230 |
robertphillips
2013/12/09 16:23:01
DEF_GM?
Stephen White
2013/12/09 17:04:25
Done.
| |
237 static GM* MyFactory(void*) { return new XfermodeImageFilterGM; } | 231 static GM* MyFactory(void*) { return new XfermodeImageFilterGM; } |
238 static GMRegistry reg(MyFactory); | 232 static GMRegistry reg(MyFactory); |
239 | 233 |
240 } | 234 } |
OLD | NEW |