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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 SkScalar x, SkScalar y) { |
74 canvas->save(); | 74 canvas->save(); |
75 canvas->translate(x, y); | 75 canvas->clipRect(SkRect::MakeXYWH(x, y, |
76 canvas->clipRect(SkRect::MakeXYWH(0, 0, | |
77 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()))); | 76 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()))); |
78 canvas->drawBitmap(bitmap, 0, 0, &paint); | 77 canvas->drawBitmap(bitmap, x, y, &paint); |
79 canvas->restore(); | |
80 } | |
81 | |
82 void drawClippedPaint(SkCanvas* canvas, const SkRect& rect, const SkPaint& p
aint, | |
83 SkScalar x, SkScalar y) { | |
84 canvas->save(); | |
85 canvas->translate(x, y); | |
86 canvas->clipRect(rect); | |
87 canvas->drawPaint(paint); | |
88 canvas->restore(); | 78 canvas->restore(); |
89 } | 79 } |
90 | 80 |
91 virtual void onDraw(SkCanvas* canvas) { | 81 virtual void onDraw(SkCanvas* canvas) { |
92 if (!fInitialized) { | 82 if (!fInitialized) { |
93 make_bitmap(); | 83 make_bitmap(); |
94 make_checkerboard(); | 84 make_checkerboard(); |
95 fInitialized = true; | 85 fInitialized = true; |
96 } | 86 } |
97 canvas->clear(0x00000000); | 87 canvas->clear(0x00000000); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 } | 149 } |
160 // Test NULL mode | 150 // Test NULL mode |
161 filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, (NULL, background))); | 151 filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, (NULL, background))); |
162 paint.setImageFilter(filter); | 152 paint.setImageFilter(filter); |
163 drawClippedBitmap(canvas, fBitmap, paint, SkIntToScalar(x), SkIntToScala
r(y)); | 153 drawClippedBitmap(canvas, fBitmap, paint, SkIntToScalar(x), SkIntToScala
r(y)); |
164 x += fBitmap.width() + MARGIN; | 154 x += fBitmap.width() + MARGIN; |
165 if (x + fBitmap.width() > WIDTH) { | 155 if (x + fBitmap.width() > WIDTH) { |
166 x = 0; | 156 x = 0; |
167 y += fBitmap.height() + MARGIN; | 157 y += fBitmap.height() + MARGIN; |
168 } | 158 } |
169 SkRect clipRect = SkRect::MakeWH(SkIntToScalar(fBitmap.width() + 4), | |
170 SkIntToScalar(fBitmap.height() + 4)); | |
171 // Test offsets on SrcMode (uses fixed-function blend) | 159 // Test offsets on SrcMode (uses fixed-function blend) |
172 SkAutoTUnref<SkImageFilter> foreground(SkNEW_ARGS(SkBitmapSource, (fBitm
ap))); | 160 SkAutoTUnref<SkImageFilter> foreground(SkNEW_ARGS(SkBitmapSource, (fBitm
ap))); |
173 SkAutoTUnref<SkImageFilter> offsetForeground(SkNEW_ARGS(SkOffsetImageFil
ter, | 161 SkAutoTUnref<SkImageFilter> offsetForeground(SkNEW_ARGS(SkOffsetImageFil
ter, |
174 (SkIntToScalar(4), SkIntToScalar(-4), foreground))); | 162 (SkIntToScalar(4), SkIntToScalar(-4), foreground))); |
175 SkAutoTUnref<SkImageFilter> offsetBackground(SkNEW_ARGS(SkOffsetImageFil
ter, | 163 SkAutoTUnref<SkImageFilter> offsetBackground(SkNEW_ARGS(SkOffsetImageFil
ter, |
176 (SkIntToScalar(4), SkIntToScalar(4), background))); | 164 (SkIntToScalar(4), SkIntToScalar(4), background))); |
177 mode.reset(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); | 165 mode.reset(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); |
178 filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, | 166 filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, |
179 (mode, offsetBackground, offsetForeground))); | 167 (mode, offsetBackground, offsetForeground))); |
180 paint.setImageFilter(filter); | 168 paint.setImageFilter(filter); |
181 drawClippedPaint(canvas, clipRect, paint, | 169 canvas->save(); |
182 SkIntToScalar(x), SkIntToScalar(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(); |
183 x += fBitmap.width() + MARGIN; | 176 x += fBitmap.width() + MARGIN; |
184 if (x + fBitmap.width() > WIDTH) { | 177 if (x + fBitmap.width() > WIDTH) { |
185 x = 0; | 178 x = 0; |
186 y += fBitmap.height() + MARGIN; | 179 y += fBitmap.height() + MARGIN; |
187 } | 180 } |
188 // Test offsets on Darken (uses shader blend) | 181 // Test offsets on Darken (uses shader blend) |
189 mode.reset(SkXfermode::Create(SkXfermode::kDarken_Mode)); | 182 mode.reset(SkXfermode::Create(SkXfermode::kDarken_Mode)); |
190 filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, (mode, offsetBackground,
offsetForeground))); | 183 filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, (mode, offsetBackground,
offsetForeground))); |
191 paint.setImageFilter(filter); | 184 paint.setImageFilter(filter); |
192 drawClippedPaint(canvas, clipRect, paint, | 185 canvas->save(); |
193 SkIntToScalar(x), SkIntToScalar(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(); |
194 x += fBitmap.width() + MARGIN; | 192 x += fBitmap.width() + MARGIN; |
195 if (x + fBitmap.width() > WIDTH) { | 193 if (x + fBitmap.width() > WIDTH) { |
196 x = 0; | 194 x = 0; |
197 y += fBitmap.height() + MARGIN; | 195 y += fBitmap.height() + MARGIN; |
198 } | 196 } |
199 // Test cropping | 197 // Test cropping |
200 static const size_t nbSamples = 3; | 198 static const size_t nbSamples = 3; |
201 SkXfermode::Mode sampledModes[nbSamples] = {SkXfermode::kOverlay_Mode, | 199 SkXfermode::Mode sampledModes[nbSamples] = {SkXfermode::kOverlay_Mode, |
202 SkXfermode::kSrcOver_Mode, | 200 SkXfermode::kSrcOver_Mode, |
203 SkXfermode::kPlus_Mode}; | 201 SkXfermode::kPlus_Mode}; |
204 int offsets[nbSamples][4] = {{ 10, 10, -16, -16}, | 202 int offsets[nbSamples][4] = {{ 10, 10, -16, -16}, |
205 { 10, 10, 10, 10}, | 203 { 10, 10, 10, 10}, |
206 {-10, -10, -6, -6}}; | 204 {-10, -10, -6, -6}}; |
207 for (size_t i = 0; i < nbSamples; ++i) { | 205 for (size_t i = 0; i < nbSamples; ++i) { |
208 SkIRect cropRect = SkIRect::MakeXYWH(offsets[i][0], | 206 SkIRect cropRect = SkIRect::MakeXYWH(x + offsets[i][0], |
209 offsets[i][1], | 207 y + offsets[i][1], |
210 fBitmap.width() + offsets[i][2
], | 208 fBitmap.width() + offsets[i][2
], |
211 fBitmap.height() + offsets[i][3
]); | 209 fBitmap.height() + offsets[i][3
]); |
212 SkImageFilter::CropRect rect(SkRect::Make(cropRect)); | 210 SkImageFilter::CropRect rect(SkRect::Make(cropRect)); |
213 mode.reset(SkXfermode::Create(sampledModes[i])); | 211 mode.reset(SkXfermode::Create(sampledModes[i])); |
214 filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, | 212 filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, |
215 (mode, offsetBackground, offsetForeground, &
rect))); | 213 (mode, offsetBackground, offsetForeground, &
rect))); |
216 paint.setImageFilter(filter); | 214 paint.setImageFilter(filter); |
217 drawClippedPaint(canvas, clipRect, paint, | 215 canvas->save(); |
218 SkIntToScalar(x), SkIntToScalar(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(); |
219 x += fBitmap.width() + MARGIN; | 222 x += fBitmap.width() + MARGIN; |
220 if (x + fBitmap.width() > WIDTH) { | 223 if (x + fBitmap.width() > WIDTH) { |
221 x = 0; | 224 x = 0; |
222 y += fBitmap.height() + MARGIN; | 225 y += fBitmap.height() + MARGIN; |
223 } | 226 } |
224 } | 227 } |
225 } | 228 } |
226 private: | 229 private: |
227 typedef GM INHERITED; | 230 typedef GM INHERITED; |
228 SkBitmap fBitmap, fCheckerboard; | 231 SkBitmap fBitmap, fCheckerboard; |
229 bool fInitialized; | 232 bool fInitialized; |
230 }; | 233 }; |
231 | 234 |
232 ////////////////////////////////////////////////////////////////////////////// | 235 ////////////////////////////////////////////////////////////////////////////// |
233 | 236 |
234 static GM* MyFactory(void*) { return new XfermodeImageFilterGM; } | 237 static GM* MyFactory(void*) { return new XfermodeImageFilterGM; } |
235 static GMRegistry reg(MyFactory); | 238 static GMRegistry reg(MyFactory); |
236 | 239 |
237 } | 240 } |
OLD | NEW |