senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 8 | #include "SkBicubicImageFilter.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkBitmapDevice.h" |
| 11 | #include "SkBitmapSource.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 12 | #include "SkBlurImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 13 | #include "SkCanvas.h" |
| 14 | #include "SkColorFilterImageFilter.h" |
| 15 | #include "SkColorMatrixFilter.h" |
| 16 | #include "SkDeviceImageFilterProxy.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 17 | #include "SkDisplacementMapEffect.h" |
| 18 | #include "SkDropShadowImageFilter.h" |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 19 | #include "SkFlattenableBuffers.h" |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 20 | #include "SkLightingImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 21 | #include "SkMatrixConvolutionImageFilter.h" |
senorblanco@chromium.org | d5424a4 | 2014-04-02 19:20:05 +0000 | [diff] [blame] | 22 | #include "SkMatrixImageFilter.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 23 | #include "SkMergeImageFilter.h" |
| 24 | #include "SkMorphologyImageFilter.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 25 | #include "SkOffsetImageFilter.h" |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 26 | #include "SkPicture.h" |
commit-bot@chromium.org | d393b17 | 2014-04-16 16:02:10 +0000 | [diff] [blame] | 27 | #include "SkRTreePicture.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 28 | #include "SkRect.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 29 | #include "SkTileImageFilter.h" |
| 30 | #include "SkXfermodeImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 31 | #include "Test.h" |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 32 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 33 | #if SK_SUPPORT_GPU |
| 34 | #include "GrContextFactory.h" |
| 35 | #include "SkGpuDevice.h" |
| 36 | #endif |
| 37 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 38 | static const int kBitmapSize = 4; |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 39 | |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 40 | namespace { |
| 41 | |
| 42 | class MatrixTestImageFilter : public SkImageFilter { |
| 43 | public: |
| 44 | MatrixTestImageFilter(skiatest::Reporter* reporter, const SkMatrix& expectedMatrix) |
| 45 | : SkImageFilter(0), fReporter(reporter), fExpectedMatrix(expectedMatrix) { |
| 46 | } |
| 47 | |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 48 | virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context& ctx, |
senorblanco@chromium.org | 0937335 | 2014-02-05 23:04:28 +0000 | [diff] [blame] | 49 | SkBitmap* result, SkIPoint* offset) const SK_OVERRIDE { |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 50 | REPORTER_ASSERT(fReporter, ctx.ctm() == fExpectedMatrix); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 51 | return true; |
| 52 | } |
| 53 | |
| 54 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(MatrixTestImageFilter) |
| 55 | |
| 56 | protected: |
| 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 | |
| 67 | private: |
| 68 | skiatest::Reporter* fReporter; |
| 69 | SkMatrix fExpectedMatrix; |
| 70 | }; |
| 71 | |
| 72 | } |
| 73 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 74 | static void make_small_bitmap(SkBitmap& bitmap) { |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 75 | bitmap.allocN32Pixels(kBitmapSize, kBitmapSize); |
| 76 | SkCanvas canvas(bitmap); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 77 | 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.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 105 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 106 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 107 | static 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.org | 727a352 | 2014-02-21 18:46:30 +0000 | [diff] [blame] | 113 | SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix)); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 114 | return SkColorFilterImageFilter::Create(filter, input); |
| 115 | } |
| 116 | |
| 117 | static 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.org | 727a352 | 2014-02-21 18:46:30 +0000 | [diff] [blame] | 124 | SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix)); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 125 | return SkColorFilterImageFilter::Create(filter, input, cropRect); |
| 126 | } |
| 127 | |
| 128 | DEF_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.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 134 | } |
| 135 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 136 | { |
| 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.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 141 | } |
| 142 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 143 | { |
| 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.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 148 | } |
| 149 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 150 | { |
| 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.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 162 | result.allocN32Pixels(kBitmapSize, kBitmapSize); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 163 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 164 | { |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 165 | // 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.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 173 | SkAutoTUnref<SkImageFilter> bmSrc(SkBitmapSource::Create(bitmap)); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 174 | 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.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 183 | } |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 184 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 185 | { |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 186 | // This tests for scale bringing width to 0 |
| 187 | SkSize scale = SkSize::Make(-0.001f, SK_Scalar1); |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 188 | SkAutoTUnref<SkImageFilter> bmSrc(SkBitmapSource::Create(bitmap)); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 189 | 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.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 195 | SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(2)); |
| 196 | SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeEmpty(), cache.get()); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 197 | REPORTER_ASSERT(reporter, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 198 | !bicubic->filterImage(&proxy, bitmap, ctx, &result, &loc)); |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 199 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 200 | } |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 201 | } |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 202 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 203 | static 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.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 208 | bitmap.allocN32Pixels(100, 100); |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 209 | bitmap.eraseARGB(0, 0, 0, 0); |
| 210 | SkDeviceImageFilterProxy proxy(device); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 211 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 212 | 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.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 215 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 216 | 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.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 226 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 227 | SkImageFilter* filters[] = { |
| 228 | SkColorFilterImageFilter::Create(cf.get(), input.get(), &cropRect), |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 229 | 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.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 234 | 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.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 236 | 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.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 244 | }; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 245 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 246 | 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.org | f4e1a76 | 2014-02-04 00:28:46 +0000 | [diff] [blame] | 251 | str.printf("filter %d", static_cast<int>(i)); |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 252 | 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.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 256 | REPORTER_ASSERT_MESSAGE(reporter, offset.fX == 20 && offset.fY == 30, str.c_str()); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 257 | } |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 258 | |
| 259 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 260 | SkSafeUnref(filters[i]); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | DEF_TEST(ImageFilterCropRect, reporter) { |
| 265 | SkBitmap temp; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 266 | temp.allocN32Pixels(100, 100); |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 267 | SkBitmapDevice device(temp); |
| 268 | test_crop_rects(&device, reporter); |
| 269 | } |
| 270 | |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 271 | DEF_TEST(ImageFilterMatrixTest, reporter) { |
| 272 | SkBitmap temp; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 273 | temp.allocN32Pixels(100, 100); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 274 | SkBitmapDevice device(temp); |
| 275 | SkCanvas canvas(&device); |
| 276 | canvas.scale(SkIntToScalar(2), SkIntToScalar(2)); |
| 277 | |
| 278 | SkMatrix expectedMatrix = canvas.getTotalMatrix(); |
| 279 | |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 280 | SkRTreeFactory factory; |
| 281 | SkPictureRecorder recorder; |
| 282 | SkCanvas* recordingCanvas = recorder.beginRecording(100, 100, &factory, 0); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 283 | |
| 284 | SkPaint paint; |
| 285 | SkAutoTUnref<MatrixTestImageFilter> imageFilter( |
| 286 | new MatrixTestImageFilter(reporter, expectedMatrix)); |
| 287 | paint.setImageFilter(imageFilter.get()); |
| 288 | SkCanvas::SaveFlags saveFlags = static_cast<SkCanvas::SaveFlags>( |
| 289 | SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kFullColorLayer_SaveFlag); |
| 290 | recordingCanvas->saveLayer(NULL, &paint, saveFlags); |
| 291 | SkPaint solidPaint; |
| 292 | solidPaint.setColor(0xFFFFFFFF); |
| 293 | recordingCanvas->save(); |
| 294 | recordingCanvas->scale(SkIntToScalar(10), SkIntToScalar(10)); |
| 295 | recordingCanvas->drawRect(SkRect::Make(SkIRect::MakeWH(100, 100)), solidPaint); |
| 296 | recordingCanvas->restore(); // scale |
| 297 | recordingCanvas->restore(); // saveLayer |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 298 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 299 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 300 | canvas.drawPicture(*picture); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 301 | } |
| 302 | |
senorblanco@chromium.org | 28ae55d | 2014-03-24 21:32:28 +0000 | [diff] [blame] | 303 | static void test_huge_blur(SkBaseDevice* device, skiatest::Reporter* reporter) { |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 304 | SkCanvas canvas(device); |
| 305 | |
| 306 | SkBitmap bitmap; |
| 307 | bitmap.allocN32Pixels(100, 100); |
| 308 | bitmap.eraseARGB(0, 0, 0, 0); |
| 309 | |
| 310 | // Check that a blur with an insane radius does not crash or assert. |
| 311 | SkAutoTUnref<SkImageFilter> blur(SkBlurImageFilter::Create(SkIntToScalar(1<<30), SkIntToScalar(1<<30))); |
| 312 | |
| 313 | SkPaint paint; |
| 314 | paint.setImageFilter(blur); |
| 315 | canvas.drawSprite(bitmap, 0, 0, &paint); |
| 316 | } |
| 317 | |
| 318 | DEF_TEST(HugeBlurImageFilter, reporter) { |
| 319 | SkBitmap temp; |
| 320 | temp.allocN32Pixels(100, 100); |
| 321 | SkBitmapDevice device(temp); |
| 322 | test_huge_blur(&device, reporter); |
| 323 | } |
| 324 | |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 325 | static void test_xfermode_cropped_input(SkBaseDevice* device, skiatest::Reporter* reporter) { |
| 326 | SkCanvas canvas(device); |
| 327 | canvas.clear(0); |
| 328 | |
| 329 | SkBitmap bitmap; |
| 330 | bitmap.allocN32Pixels(1, 1); |
| 331 | bitmap.eraseARGB(255, 255, 255, 255); |
| 332 | |
| 333 | SkAutoTUnref<SkColorFilter> green( |
| 334 | SkColorFilter::CreateModeFilter(SK_ColorGREEN, SkXfermode::kSrcIn_Mode)); |
| 335 | SkAutoTUnref<SkColorFilterImageFilter> greenFilter( |
| 336 | SkColorFilterImageFilter::Create(green.get())); |
| 337 | SkImageFilter::CropRect cropRect(SkRect::MakeEmpty()); |
| 338 | SkAutoTUnref<SkColorFilterImageFilter> croppedOut( |
| 339 | SkColorFilterImageFilter::Create(green.get(), NULL, &cropRect)); |
| 340 | |
| 341 | // Check that an xfermode image filter whose input has been cropped out still draws the other |
| 342 | // input. Also check that drawing with both inputs cropped out doesn't cause a GPU warning. |
| 343 | SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrcOver_Mode); |
| 344 | SkAutoTUnref<SkImageFilter> xfermodeNoFg( |
| 345 | SkXfermodeImageFilter::Create(mode, greenFilter, croppedOut)); |
| 346 | SkAutoTUnref<SkImageFilter> xfermodeNoBg( |
| 347 | SkXfermodeImageFilter::Create(mode, croppedOut, greenFilter)); |
| 348 | SkAutoTUnref<SkImageFilter> xfermodeNoFgNoBg( |
| 349 | SkXfermodeImageFilter::Create(mode, croppedOut, croppedOut)); |
| 350 | |
| 351 | SkPaint paint; |
| 352 | paint.setImageFilter(xfermodeNoFg); |
| 353 | canvas.drawSprite(bitmap, 0, 0, &paint); |
| 354 | |
| 355 | uint32_t pixel; |
| 356 | SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 357 | canvas.readPixels(info, &pixel, 4, 0, 0); |
| 358 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 359 | |
| 360 | paint.setImageFilter(xfermodeNoBg); |
| 361 | canvas.drawSprite(bitmap, 0, 0, &paint); |
| 362 | canvas.readPixels(info, &pixel, 4, 0, 0); |
| 363 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 364 | |
| 365 | paint.setImageFilter(xfermodeNoFgNoBg); |
| 366 | canvas.drawSprite(bitmap, 0, 0, &paint); |
| 367 | canvas.readPixels(info, &pixel, 4, 0, 0); |
| 368 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 369 | } |
| 370 | |
senorblanco@chromium.org | d5424a4 | 2014-04-02 19:20:05 +0000 | [diff] [blame] | 371 | DEF_TEST(ImageFilterNestedSaveLayer, reporter) { |
| 372 | SkBitmap temp; |
| 373 | temp.allocN32Pixels(50, 50); |
| 374 | SkBitmapDevice device(temp); |
| 375 | SkCanvas canvas(&device); |
| 376 | canvas.clear(0x0); |
| 377 | |
| 378 | SkBitmap bitmap; |
| 379 | bitmap.allocN32Pixels(10, 10); |
| 380 | bitmap.eraseColor(SK_ColorGREEN); |
| 381 | |
| 382 | SkMatrix matrix; |
| 383 | matrix.setScale(SkIntToScalar(2), SkIntToScalar(2)); |
| 384 | matrix.postTranslate(SkIntToScalar(-20), SkIntToScalar(-20)); |
| 385 | SkAutoTUnref<SkImageFilter> matrixFilter( |
| 386 | SkMatrixImageFilter::Create(matrix, SkPaint::kLow_FilterLevel)); |
| 387 | |
| 388 | // Test that saveLayer() with a filter nested inside another saveLayer() applies the |
| 389 | // correct offset to the filter matrix. |
| 390 | SkRect bounds1 = SkRect::MakeXYWH(10, 10, 30, 30); |
| 391 | canvas.saveLayer(&bounds1, NULL); |
| 392 | SkPaint filterPaint; |
| 393 | filterPaint.setImageFilter(matrixFilter); |
| 394 | SkRect bounds2 = SkRect::MakeXYWH(20, 20, 10, 10); |
| 395 | canvas.saveLayer(&bounds2, &filterPaint); |
| 396 | SkPaint greenPaint; |
| 397 | greenPaint.setColor(SK_ColorGREEN); |
| 398 | canvas.drawRect(bounds2, greenPaint); |
| 399 | canvas.restore(); |
| 400 | canvas.restore(); |
| 401 | SkPaint strokePaint; |
| 402 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 403 | strokePaint.setColor(SK_ColorRED); |
| 404 | |
| 405 | SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 406 | uint32_t pixel; |
| 407 | canvas.readPixels(info, &pixel, 4, 25, 25); |
| 408 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 409 | |
| 410 | // Test that drawSprite() with a filter nested inside a saveLayer() applies the |
| 411 | // correct offset to the filter matrix. |
| 412 | canvas.clear(0x0); |
| 413 | canvas.readPixels(info, &pixel, 4, 25, 25); |
| 414 | canvas.saveLayer(&bounds1, NULL); |
| 415 | canvas.drawSprite(bitmap, 20, 20, &filterPaint); |
| 416 | canvas.restore(); |
| 417 | |
| 418 | canvas.readPixels(info, &pixel, 4, 25, 25); |
| 419 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 420 | } |
| 421 | |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 422 | DEF_TEST(XfermodeImageFilterCroppedInput, reporter) { |
| 423 | SkBitmap temp; |
| 424 | temp.allocN32Pixels(100, 100); |
| 425 | SkBitmapDevice device(temp); |
| 426 | test_xfermode_cropped_input(&device, reporter); |
| 427 | } |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 428 | |
senorblanco@chromium.org | 58d1466 | 2014-02-03 22:36:39 +0000 | [diff] [blame] | 429 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 430 | DEF_GPUTEST(ImageFilterCropRectGPU, reporter, factory) { |
| 431 | GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(0)); |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 432 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(context, |
| 433 | SkImageInfo::MakeN32Premul(100, 100), |
| 434 | 0)); |
| 435 | test_crop_rects(device, reporter); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 436 | } |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 437 | |
| 438 | DEF_GPUTEST(HugeBlurImageFilterGPU, reporter, factory) { |
| 439 | GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(0)); |
| 440 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(context, |
| 441 | SkImageInfo::MakeN32Premul(100, 100), |
| 442 | 0)); |
| 443 | test_huge_blur(device, reporter); |
| 444 | } |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 445 | |
| 446 | DEF_GPUTEST(XfermodeImageFilterCroppedInputGPU, reporter, factory) { |
| 447 | GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(0)); |
| 448 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(context, |
| 449 | SkImageInfo::MakeN32Premul(1, 1), |
| 450 | 0)); |
| 451 | test_xfermode_cropped_input(device, reporter); |
| 452 | } |
senorblanco@chromium.org | 58d1466 | 2014-02-03 22:36:39 +0000 | [diff] [blame] | 453 | #endif |