blob: f968a4e9c5ea216893fcb2dc1cabf18d966f4d88 [file] [log] [blame]
senorblanco@chromium.org194d7752013-07-24 22:19:24 +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
commit-bot@chromium.org4b681bc2013-09-13 12:40:02 +00008#include "SkBicubicImageFilter.h"
9#include "SkBitmap.h"
10#include "SkBitmapDevice.h"
11#include "SkBitmapSource.h"
senorblanco@chromium.org6776b822014-01-03 21:48:22 +000012#include "SkBlurImageFilter.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000013#include "SkCanvas.h"
14#include "SkColorFilterImageFilter.h"
15#include "SkColorMatrixFilter.h"
16#include "SkDeviceImageFilterProxy.h"
senorblanco@chromium.org6776b822014-01-03 21:48:22 +000017#include "SkDisplacementMapEffect.h"
18#include "SkDropShadowImageFilter.h"
senorblanco@chromium.org5251e2b2014-02-05 22:36:31 +000019#include "SkFlattenableBuffers.h"
commit-bot@chromium.org4b681bc2013-09-13 12:40:02 +000020#include "SkLightingImageFilter.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000021#include "SkMatrixConvolutionImageFilter.h"
senorblanco@chromium.orgd5424a42014-04-02 19:20:05 +000022#include "SkMatrixImageFilter.h"
senorblanco@chromium.org6776b822014-01-03 21:48:22 +000023#include "SkMergeImageFilter.h"
24#include "SkMorphologyImageFilter.h"
senorblanco@chromium.org6776b822014-01-03 21:48:22 +000025#include "SkOffsetImageFilter.h"
senorblanco@chromium.org5251e2b2014-02-05 22:36:31 +000026#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000027#include "SkPictureRecorder.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000028#include "SkRect.h"
senorblanco@chromium.org6776b822014-01-03 21:48:22 +000029#include "SkTileImageFilter.h"
30#include "SkXfermodeImageFilter.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000031#include "Test.h"
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000032
senorblanco@chromium.orgaba651c2014-02-03 22:22:16 +000033#if SK_SUPPORT_GPU
34#include "GrContextFactory.h"
35#include "SkGpuDevice.h"
36#endif
37
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000038static const int kBitmapSize = 4;
commit-bot@chromium.org4b681bc2013-09-13 12:40:02 +000039
senorblanco@chromium.org5251e2b2014-02-05 22:36:31 +000040namespace {
41
42class MatrixTestImageFilter : public SkImageFilter {
43public:
44 MatrixTestImageFilter(skiatest::Reporter* reporter, const SkMatrix& expectedMatrix)
45 : SkImageFilter(0), fReporter(reporter), fExpectedMatrix(expectedMatrix) {
46 }
47
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +000048 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context& ctx,
senorblanco@chromium.org09373352014-02-05 23:04:28 +000049 SkBitmap* result, SkIPoint* offset) const SK_OVERRIDE {
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +000050 REPORTER_ASSERT(fReporter, ctx.ctm() == fExpectedMatrix);
senorblanco@chromium.org5251e2b2014-02-05 22:36:31 +000051 return true;
52 }
53
54 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(MatrixTestImageFilter)
55
56protected:
57 explicit MatrixTestImageFilter(SkReadBuffer& buffer) : SkImageFilter(0) {
58 fReporter = static_cast<skiatest::Reporter*>(buffer.readFunctionPtr());
59 buffer.readMatrix(&fExpectedMatrix);
60 }
61
62 virtual void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE {
63 buffer.writeFunctionPtr(fReporter);
64 buffer.writeMatrix(fExpectedMatrix);
65 }
66
67private:
68 skiatest::Reporter* fReporter;
69 SkMatrix fExpectedMatrix;
70};
71
72}
73
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000074static void make_small_bitmap(SkBitmap& bitmap) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000075 bitmap.allocN32Pixels(kBitmapSize, kBitmapSize);
76 SkCanvas canvas(bitmap);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +000077 canvas.clear(0x00000000);
78 SkPaint darkPaint;
79 darkPaint.setColor(0xFF804020);
80 SkPaint lightPaint;
81 lightPaint.setColor(0xFF244484);
82 const int i = kBitmapSize / 4;
83 for (int y = 0; y < kBitmapSize; y += i) {
84 for (int x = 0; x < kBitmapSize; x += i) {
85 canvas.save();
86 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
87 canvas.drawRect(SkRect::MakeXYWH(0, 0,
88 SkIntToScalar(i),
89 SkIntToScalar(i)), darkPaint);
90 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i),
91 0,
92 SkIntToScalar(i),
93 SkIntToScalar(i)), lightPaint);
94 canvas.drawRect(SkRect::MakeXYWH(0,
95 SkIntToScalar(i),
96 SkIntToScalar(i),
97 SkIntToScalar(i)), lightPaint);
98 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i),
99 SkIntToScalar(i),
100 SkIntToScalar(i),
101 SkIntToScalar(i)), darkPaint);
102 canvas.restore();
commit-bot@chromium.org4b681bc2013-09-13 12:40:02 +0000103 }
104 }
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000105}
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000106
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000107static SkImageFilter* make_scale(float amount, SkImageFilter* input = NULL) {
108 SkScalar s = amount;
109 SkScalar matrix[20] = { s, 0, 0, 0, 0,
110 0, s, 0, 0, 0,
111 0, 0, s, 0, 0,
112 0, 0, 0, s, 0 };
commit-bot@chromium.org727a3522014-02-21 18:46:30 +0000113 SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix));
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000114 return SkColorFilterImageFilter::Create(filter, input);
115}
116
117static SkImageFilter* make_grayscale(SkImageFilter* input = NULL, const SkImageFilter::CropRect* cropRect = NULL) {
118 SkScalar matrix[20];
119 memset(matrix, 0, 20 * sizeof(SkScalar));
120 matrix[0] = matrix[5] = matrix[10] = 0.2126f;
121 matrix[1] = matrix[6] = matrix[11] = 0.7152f;
122 matrix[2] = matrix[7] = matrix[12] = 0.0722f;
123 matrix[18] = 1.0f;
commit-bot@chromium.org727a3522014-02-21 18:46:30 +0000124 SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix));
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000125 return SkColorFilterImageFilter::Create(filter, input, cropRect);
126}
127
128DEF_TEST(ImageFilter, reporter) {
129 {
130 // Check that two non-clipping color matrices concatenate into a single filter.
131 SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f));
132 SkAutoTUnref<SkImageFilter> quarterBrightness(make_scale(0.5f, halfBrightness));
133 REPORTER_ASSERT(reporter, NULL == quarterBrightness->getInput(0));
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000134 }
135
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000136 {
137 // Check that a clipping color matrix followed by a grayscale does not concatenate into a single filter.
138 SkAutoTUnref<SkImageFilter> doubleBrightness(make_scale(2.0f));
139 SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f, doubleBrightness));
140 REPORTER_ASSERT(reporter, NULL != halfBrightness->getInput(0));
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000141 }
142
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000143 {
144 // Check that a color filter image filter without a crop rect can be
145 // expressed as a color filter.
146 SkAutoTUnref<SkImageFilter> gray(make_grayscale());
147 REPORTER_ASSERT(reporter, true == gray->asColorFilter(NULL));
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000148 }
149
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000150 {
151 // Check that a color filter image filter with a crop rect cannot
152 // be expressed as a color filter.
153 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(0, 0, 100, 100));
154 SkAutoTUnref<SkImageFilter> grayWithCrop(make_grayscale(NULL, &cropRect));
155 REPORTER_ASSERT(reporter, false == grayWithCrop->asColorFilter(NULL));
156 }
157
158 {
159 // Tests pass by not asserting
160 SkBitmap bitmap, result;
161 make_small_bitmap(bitmap);
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000162 result.allocN32Pixels(kBitmapSize, kBitmapSize);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000163
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000164 {
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000165 // This tests for :
166 // 1 ) location at (0,0,1)
167 SkPoint3 location(0, 0, SK_Scalar1);
168 // 2 ) location and target at same value
169 SkPoint3 target(location.fX, location.fY, location.fZ);
170 // 3 ) large negative specular exponent value
171 SkScalar specularExponent = -1000;
172
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000173 SkAutoTUnref<SkImageFilter> bmSrc(SkBitmapSource::Create(bitmap));
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000174 SkPaint paint;
175 paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular(
176 location, target, specularExponent, 180,
177 0xFFFFFFFF, SK_Scalar1, SK_Scalar1, SK_Scalar1,
178 bmSrc))->unref();
179 SkCanvas canvas(result);
180 SkRect r = SkRect::MakeWH(SkIntToScalar(kBitmapSize),
181 SkIntToScalar(kBitmapSize));
182 canvas.drawRect(r, paint);
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000183 }
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000184
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000185 {
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000186 // This tests for scale bringing width to 0
187 SkSize scale = SkSize::Make(-0.001f, SK_Scalar1);
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000188 SkAutoTUnref<SkImageFilter> bmSrc(SkBitmapSource::Create(bitmap));
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000189 SkAutoTUnref<SkBicubicImageFilter> bicubic(
190 SkBicubicImageFilter::CreateMitchell(scale, bmSrc));
191 SkBitmapDevice device(bitmap);
192 SkDeviceImageFilterProxy proxy(&device);
193 SkIPoint loc = SkIPoint::Make(0, 0);
194 // An empty input should early return and return false
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +0000195 SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(2));
196 SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeEmpty(), cache.get());
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000197 REPORTER_ASSERT(reporter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000198 !bicubic->filterImage(&proxy, bitmap, ctx, &result, &loc));
commit-bot@chromium.org4b681bc2013-09-13 12:40:02 +0000199 }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000200 }
senorblanco@chromium.orgaba651c2014-02-03 22:22:16 +0000201}
senorblanco@chromium.org6776b822014-01-03 21:48:22 +0000202
senorblanco@chromium.orgaba651c2014-02-03 22:22:16 +0000203static void test_crop_rects(SkBaseDevice* device, skiatest::Reporter* reporter) {
204 // Check that all filters offset to their absolute crop rect,
205 // unaffected by the input crop rect.
206 // Tests pass by not asserting.
207 SkBitmap bitmap;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000208 bitmap.allocN32Pixels(100, 100);
senorblanco@chromium.orgaba651c2014-02-03 22:22:16 +0000209 bitmap.eraseARGB(0, 0, 0, 0);
210 SkDeviceImageFilterProxy proxy(device);
senorblanco@chromium.org6776b822014-01-03 21:48:22 +0000211
senorblanco@chromium.orgaba651c2014-02-03 22:22:16 +0000212 SkImageFilter::CropRect inputCropRect(SkRect::MakeXYWH(8, 13, 80, 80));
213 SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 30, 60, 60));
214 SkAutoTUnref<SkImageFilter> input(make_grayscale(NULL, &inputCropRect));
senorblanco@chromium.org6776b822014-01-03 21:48:22 +0000215
senorblanco@chromium.orgaba651c2014-02-03 22:22:16 +0000216 SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_ColorRED, SkXfermode::kSrcIn_Mode));
217 SkPoint3 location(0, 0, SK_Scalar1);
218 SkPoint3 target(SK_Scalar1, SK_Scalar1, SK_Scalar1);
219 SkScalar kernel[9] = {
220 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
221 SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1),
222 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
223 };
224 SkISize kernelSize = SkISize::Make(3, 3);
225 SkScalar gain = SK_Scalar1, bias = 0;
senorblanco@chromium.org6776b822014-01-03 21:48:22 +0000226
senorblanco@chromium.orgaba651c2014-02-03 22:22:16 +0000227 SkImageFilter* filters[] = {
228 SkColorFilterImageFilter::Create(cf.get(), input.get(), &cropRect),
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000229 SkDisplacementMapEffect::Create(SkDisplacementMapEffect::kR_ChannelSelectorType,
230 SkDisplacementMapEffect::kB_ChannelSelectorType,
231 40.0f, input.get(), input.get(), &cropRect),
232 SkBlurImageFilter::Create(SK_Scalar1, SK_Scalar1, input.get(), &cropRect),
233 SkDropShadowImageFilter::Create(SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_ColorGREEN, input.get(), &cropRect),
senorblanco@chromium.orgaba651c2014-02-03 22:22:16 +0000234 SkLightingImageFilter::CreatePointLitDiffuse(location, SK_ColorGREEN, 0, 0, input.get(), &cropRect),
235 SkLightingImageFilter::CreatePointLitSpecular(location, SK_ColorGREEN, 0, 0, 0, input.get(), &cropRect),
commit-bot@chromium.orgcac5fd52014-03-10 10:51:58 +0000236 SkMatrixConvolutionImageFilter::Create(kernelSize, kernel, gain, bias, SkIPoint::Make(1, 1), SkMatrixConvolutionImageFilter::kRepeat_TileMode, false, input.get(), &cropRect),
237 SkMergeImageFilter::Create(input.get(), input.get(), SkXfermode::kSrcOver_Mode, &cropRect),
238 SkOffsetImageFilter::Create(SK_Scalar1, SK_Scalar1, input.get(), &cropRect),
239 SkOffsetImageFilter::Create(SK_Scalar1, SK_Scalar1, input.get(), &cropRect),
240 SkDilateImageFilter::Create(3, 2, input.get(), &cropRect),
241 SkErodeImageFilter::Create(2, 3, input.get(), &cropRect),
242 SkTileImageFilter::Create(inputCropRect.rect(), cropRect.rect(), input.get()),
243 SkXfermodeImageFilter::Create(SkXfermode::Create(SkXfermode::kSrcOver_Mode), input.get(), input.get(), &cropRect),
senorblanco@chromium.orgaba651c2014-02-03 22:22:16 +0000244 };
senorblanco@chromium.org6776b822014-01-03 21:48:22 +0000245
senorblanco@chromium.orgaba651c2014-02-03 22:22:16 +0000246 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
247 SkImageFilter* filter = filters[i];
248 SkBitmap result;
249 SkIPoint offset;
250 SkString str;
senorblanco@chromium.orgf4e1a762014-02-04 00:28:46 +0000251 str.printf("filter %d", static_cast<int>(i));
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +0000252 SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(2));
253 SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeLargest(), cache.get());
254 REPORTER_ASSERT_MESSAGE(reporter, filter->filterImage(&proxy, bitmap, ctx,
255 &result, &offset), str.c_str());
senorblanco@chromium.orgaba651c2014-02-03 22:22:16 +0000256 REPORTER_ASSERT_MESSAGE(reporter, offset.fX == 20 && offset.fY == 30, str.c_str());
senorblanco@chromium.org6776b822014-01-03 21:48:22 +0000257 }
senorblanco@chromium.orgaba651c2014-02-03 22:22:16 +0000258
259 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
260 SkSafeUnref(filters[i]);
261 }
262}
263
264DEF_TEST(ImageFilterCropRect, reporter) {
265 SkBitmap temp;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000266 temp.allocN32Pixels(100, 100);
senorblanco@chromium.orgaba651c2014-02-03 22:22:16 +0000267 SkBitmapDevice device(temp);
268 test_crop_rects(&device, reporter);
269}
270
senorblanco@chromium.org5251e2b2014-02-05 22:36:31 +0000271DEF_TEST(ImageFilterMatrixTest, reporter) {
272 SkBitmap temp;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000273 temp.allocN32Pixels(100, 100);
senorblanco@chromium.org5251e2b2014-02-05 22:36:31 +0000274 SkBitmapDevice device(temp);
275 SkCanvas canvas(&device);
276 canvas.scale(SkIntToScalar(2), SkIntToScalar(2));
277
278 SkMatrix expectedMatrix = canvas.getTotalMatrix();
279
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000280 SkRTreeFactory factory;
281 SkPictureRecorder recorder;
282 SkCanvas* recordingCanvas = recorder.beginRecording(100, 100, &factory, 0);
senorblanco@chromium.org5251e2b2014-02-05 22:36:31 +0000283
284 SkPaint paint;
285 SkAutoTUnref<MatrixTestImageFilter> imageFilter(
286 new MatrixTestImageFilter(reporter, expectedMatrix));
287 paint.setImageFilter(imageFilter.get());
commit-bot@chromium.org091a5942014-04-18 14:19:31 +0000288 recordingCanvas->saveLayer(NULL, &paint);
senorblanco@chromium.org5251e2b2014-02-05 22:36:31 +0000289 SkPaint solidPaint;
290 solidPaint.setColor(0xFFFFFFFF);
291 recordingCanvas->save();
292 recordingCanvas->scale(SkIntToScalar(10), SkIntToScalar(10));
293 recordingCanvas->drawRect(SkRect::Make(SkIRect::MakeWH(100, 100)), solidPaint);
294 recordingCanvas->restore(); // scale
295 recordingCanvas->restore(); // saveLayer
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000296 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
senorblanco@chromium.org5251e2b2014-02-05 22:36:31 +0000297
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000298 canvas.drawPicture(*picture);
senorblanco@chromium.org5251e2b2014-02-05 22:36:31 +0000299}
300
senorblanco@chromium.org28ae55d2014-03-24 21:32:28 +0000301static void test_huge_blur(SkBaseDevice* device, skiatest::Reporter* reporter) {
senorblanco@chromium.org09843fd2014-03-24 20:50:59 +0000302 SkCanvas canvas(device);
303
304 SkBitmap bitmap;
305 bitmap.allocN32Pixels(100, 100);
306 bitmap.eraseARGB(0, 0, 0, 0);
307
308 // Check that a blur with an insane radius does not crash or assert.
309 SkAutoTUnref<SkImageFilter> blur(SkBlurImageFilter::Create(SkIntToScalar(1<<30), SkIntToScalar(1<<30)));
310
311 SkPaint paint;
312 paint.setImageFilter(blur);
313 canvas.drawSprite(bitmap, 0, 0, &paint);
314}
315
316DEF_TEST(HugeBlurImageFilter, reporter) {
317 SkBitmap temp;
318 temp.allocN32Pixels(100, 100);
319 SkBitmapDevice device(temp);
320 test_huge_blur(&device, reporter);
321}
322
senorblanco@chromium.orgee845ae2014-04-01 19:15:23 +0000323static void test_xfermode_cropped_input(SkBaseDevice* device, skiatest::Reporter* reporter) {
324 SkCanvas canvas(device);
325 canvas.clear(0);
326
327 SkBitmap bitmap;
328 bitmap.allocN32Pixels(1, 1);
329 bitmap.eraseARGB(255, 255, 255, 255);
330
331 SkAutoTUnref<SkColorFilter> green(
332 SkColorFilter::CreateModeFilter(SK_ColorGREEN, SkXfermode::kSrcIn_Mode));
333 SkAutoTUnref<SkColorFilterImageFilter> greenFilter(
334 SkColorFilterImageFilter::Create(green.get()));
335 SkImageFilter::CropRect cropRect(SkRect::MakeEmpty());
336 SkAutoTUnref<SkColorFilterImageFilter> croppedOut(
337 SkColorFilterImageFilter::Create(green.get(), NULL, &cropRect));
338
339 // Check that an xfermode image filter whose input has been cropped out still draws the other
340 // input. Also check that drawing with both inputs cropped out doesn't cause a GPU warning.
341 SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrcOver_Mode);
342 SkAutoTUnref<SkImageFilter> xfermodeNoFg(
343 SkXfermodeImageFilter::Create(mode, greenFilter, croppedOut));
344 SkAutoTUnref<SkImageFilter> xfermodeNoBg(
345 SkXfermodeImageFilter::Create(mode, croppedOut, greenFilter));
346 SkAutoTUnref<SkImageFilter> xfermodeNoFgNoBg(
347 SkXfermodeImageFilter::Create(mode, croppedOut, croppedOut));
348
349 SkPaint paint;
350 paint.setImageFilter(xfermodeNoFg);
351 canvas.drawSprite(bitmap, 0, 0, &paint);
352
353 uint32_t pixel;
354 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
355 canvas.readPixels(info, &pixel, 4, 0, 0);
356 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN);
357
358 paint.setImageFilter(xfermodeNoBg);
359 canvas.drawSprite(bitmap, 0, 0, &paint);
360 canvas.readPixels(info, &pixel, 4, 0, 0);
361 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN);
362
363 paint.setImageFilter(xfermodeNoFgNoBg);
364 canvas.drawSprite(bitmap, 0, 0, &paint);
365 canvas.readPixels(info, &pixel, 4, 0, 0);
366 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN);
367}
368
senorblanco@chromium.orgd5424a42014-04-02 19:20:05 +0000369DEF_TEST(ImageFilterNestedSaveLayer, reporter) {
370 SkBitmap temp;
371 temp.allocN32Pixels(50, 50);
372 SkBitmapDevice device(temp);
373 SkCanvas canvas(&device);
374 canvas.clear(0x0);
375
376 SkBitmap bitmap;
377 bitmap.allocN32Pixels(10, 10);
378 bitmap.eraseColor(SK_ColorGREEN);
379
380 SkMatrix matrix;
381 matrix.setScale(SkIntToScalar(2), SkIntToScalar(2));
382 matrix.postTranslate(SkIntToScalar(-20), SkIntToScalar(-20));
383 SkAutoTUnref<SkImageFilter> matrixFilter(
384 SkMatrixImageFilter::Create(matrix, SkPaint::kLow_FilterLevel));
385
386 // Test that saveLayer() with a filter nested inside another saveLayer() applies the
387 // correct offset to the filter matrix.
388 SkRect bounds1 = SkRect::MakeXYWH(10, 10, 30, 30);
389 canvas.saveLayer(&bounds1, NULL);
390 SkPaint filterPaint;
391 filterPaint.setImageFilter(matrixFilter);
392 SkRect bounds2 = SkRect::MakeXYWH(20, 20, 10, 10);
393 canvas.saveLayer(&bounds2, &filterPaint);
394 SkPaint greenPaint;
395 greenPaint.setColor(SK_ColorGREEN);
396 canvas.drawRect(bounds2, greenPaint);
397 canvas.restore();
398 canvas.restore();
399 SkPaint strokePaint;
400 strokePaint.setStyle(SkPaint::kStroke_Style);
401 strokePaint.setColor(SK_ColorRED);
402
403 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
404 uint32_t pixel;
405 canvas.readPixels(info, &pixel, 4, 25, 25);
406 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN);
407
408 // Test that drawSprite() with a filter nested inside a saveLayer() applies the
409 // correct offset to the filter matrix.
410 canvas.clear(0x0);
411 canvas.readPixels(info, &pixel, 4, 25, 25);
412 canvas.saveLayer(&bounds1, NULL);
413 canvas.drawSprite(bitmap, 20, 20, &filterPaint);
414 canvas.restore();
415
416 canvas.readPixels(info, &pixel, 4, 25, 25);
417 REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN);
418}
419
senorblanco@chromium.orgee845ae2014-04-01 19:15:23 +0000420DEF_TEST(XfermodeImageFilterCroppedInput, reporter) {
421 SkBitmap temp;
422 temp.allocN32Pixels(100, 100);
423 SkBitmapDevice device(temp);
424 test_xfermode_cropped_input(&device, reporter);
425}
senorblanco@chromium.org09843fd2014-03-24 20:50:59 +0000426
senorblanco@chromium.org58d14662014-02-03 22:36:39 +0000427#if SK_SUPPORT_GPU
senorblanco@chromium.orgaba651c2014-02-03 22:22:16 +0000428DEF_GPUTEST(ImageFilterCropRectGPU, reporter, factory) {
429 GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(0));
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000430 SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(context,
431 SkImageInfo::MakeN32Premul(100, 100),
432 0));
433 test_crop_rects(device, reporter);
tfarina@chromium.org9f9d5822013-12-18 22:15:12 +0000434}
senorblanco@chromium.org09843fd2014-03-24 20:50:59 +0000435
436DEF_GPUTEST(HugeBlurImageFilterGPU, reporter, factory) {
437 GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(0));
438 SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(context,
439 SkImageInfo::MakeN32Premul(100, 100),
440 0));
441 test_huge_blur(device, reporter);
442}
senorblanco@chromium.orgee845ae2014-04-01 19:15:23 +0000443
444DEF_GPUTEST(XfermodeImageFilterCroppedInputGPU, reporter, factory) {
445 GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(0));
446 SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(context,
447 SkImageInfo::MakeN32Premul(1, 1),
448 0));
449 test_xfermode_cropped_input(device, reporter);
450}
senorblanco@chromium.org58d14662014-02-03 22:36:39 +0000451#endif