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 "SkBitmap.h" |
| 9 | #include "SkBitmapDevice.h" |
| 10 | #include "SkBitmapSource.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 11 | #include "SkBlurImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 12 | #include "SkCanvas.h" |
| 13 | #include "SkColorFilterImageFilter.h" |
| 14 | #include "SkColorMatrixFilter.h" |
ajuma | 5788faa | 2015-02-13 09:05:47 -0800 | [diff] [blame] | 15 | #include "SkComposeImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 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 | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 19 | #include "SkFlattenableSerialization.h" |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 20 | #include "SkGradientShader.h" |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 21 | #include "SkLightingImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 22 | #include "SkMatrixConvolutionImageFilter.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 | 8f3937d | 2014-10-29 12:36:32 -0700 | [diff] [blame] | 26 | #include "SkPerlinNoiseShader.h" |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 27 | #include "SkPicture.h" |
senorblanco@chromium.org | 910702b | 2014-05-30 20:36:15 +0000 | [diff] [blame] | 28 | #include "SkPictureImageFilter.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 29 | #include "SkPictureRecorder.h" |
halcanary | 97d2c0a | 2014-08-19 06:27:53 -0700 | [diff] [blame] | 30 | #include "SkReadBuffer.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 31 | #include "SkRect.h" |
senorblanco | 8f3937d | 2014-10-29 12:36:32 -0700 | [diff] [blame] | 32 | #include "SkRectShaderImageFilter.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 33 | #include "SkTileImageFilter.h" |
| 34 | #include "SkXfermodeImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 35 | #include "Test.h" |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 36 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 37 | #if SK_SUPPORT_GPU |
| 38 | #include "GrContextFactory.h" |
| 39 | #include "SkGpuDevice.h" |
| 40 | #endif |
| 41 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 42 | static const int kBitmapSize = 4; |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 43 | |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 44 | namespace { |
| 45 | |
| 46 | class MatrixTestImageFilter : public SkImageFilter { |
| 47 | public: |
| 48 | MatrixTestImageFilter(skiatest::Reporter* reporter, const SkMatrix& expectedMatrix) |
senorblanco | 9ea3d57 | 2014-07-08 09:16:22 -0700 | [diff] [blame] | 49 | : SkImageFilter(0, NULL), fReporter(reporter), fExpectedMatrix(expectedMatrix) { |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 50 | } |
| 51 | |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 52 | virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context& ctx, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 53 | SkBitmap* result, SkIPoint* offset) const override { |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 54 | REPORTER_ASSERT(fReporter, ctx.ctm() == fExpectedMatrix); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 55 | return true; |
| 56 | } |
| 57 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 58 | SK_TO_STRING_OVERRIDE() |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 59 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(MatrixTestImageFilter) |
| 60 | |
| 61 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 62 | void flatten(SkWriteBuffer& buffer) const override { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 63 | this->INHERITED::flatten(buffer); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 64 | buffer.writeFunctionPtr(fReporter); |
| 65 | buffer.writeMatrix(fExpectedMatrix); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | skiatest::Reporter* fReporter; |
| 70 | SkMatrix fExpectedMatrix; |
mtklein | 3f3b3d0 | 2014-12-01 11:47:08 -0800 | [diff] [blame] | 71 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 72 | typedef SkImageFilter INHERITED; |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | } |
| 76 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 77 | SkFlattenable* MatrixTestImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 78 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 79 | skiatest::Reporter* reporter = (skiatest::Reporter*)buffer.readFunctionPtr(); |
| 80 | SkMatrix matrix; |
| 81 | buffer.readMatrix(&matrix); |
| 82 | return SkNEW_ARGS(MatrixTestImageFilter, (reporter, matrix)); |
| 83 | } |
| 84 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 85 | #ifndef SK_IGNORE_TO_STRING |
| 86 | void MatrixTestImageFilter::toString(SkString* str) const { |
| 87 | str->appendf("MatrixTestImageFilter: ("); |
| 88 | str->append(")"); |
| 89 | } |
| 90 | #endif |
| 91 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 92 | static void make_small_bitmap(SkBitmap& bitmap) { |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 93 | bitmap.allocN32Pixels(kBitmapSize, kBitmapSize); |
| 94 | SkCanvas canvas(bitmap); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 95 | canvas.clear(0x00000000); |
| 96 | SkPaint darkPaint; |
| 97 | darkPaint.setColor(0xFF804020); |
| 98 | SkPaint lightPaint; |
| 99 | lightPaint.setColor(0xFF244484); |
| 100 | const int i = kBitmapSize / 4; |
| 101 | for (int y = 0; y < kBitmapSize; y += i) { |
| 102 | for (int x = 0; x < kBitmapSize; x += i) { |
| 103 | canvas.save(); |
| 104 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 105 | canvas.drawRect(SkRect::MakeXYWH(0, 0, |
| 106 | SkIntToScalar(i), |
| 107 | SkIntToScalar(i)), darkPaint); |
| 108 | canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i), |
| 109 | 0, |
| 110 | SkIntToScalar(i), |
| 111 | SkIntToScalar(i)), lightPaint); |
| 112 | canvas.drawRect(SkRect::MakeXYWH(0, |
| 113 | SkIntToScalar(i), |
| 114 | SkIntToScalar(i), |
| 115 | SkIntToScalar(i)), lightPaint); |
| 116 | canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i), |
| 117 | SkIntToScalar(i), |
| 118 | SkIntToScalar(i), |
| 119 | SkIntToScalar(i)), darkPaint); |
| 120 | canvas.restore(); |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 123 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 124 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 125 | static SkImageFilter* make_scale(float amount, SkImageFilter* input = NULL) { |
| 126 | SkScalar s = amount; |
| 127 | SkScalar matrix[20] = { s, 0, 0, 0, 0, |
| 128 | 0, s, 0, 0, 0, |
| 129 | 0, 0, s, 0, 0, |
| 130 | 0, 0, 0, s, 0 }; |
commit-bot@chromium.org | 727a352 | 2014-02-21 18:46:30 +0000 | [diff] [blame] | 131 | SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix)); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 132 | return SkColorFilterImageFilter::Create(filter, input); |
| 133 | } |
| 134 | |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 135 | static SkImageFilter* make_grayscale(SkImageFilter* input, const SkImageFilter::CropRect* cropRect) { |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 136 | SkScalar matrix[20]; |
| 137 | memset(matrix, 0, 20 * sizeof(SkScalar)); |
| 138 | matrix[0] = matrix[5] = matrix[10] = 0.2126f; |
| 139 | matrix[1] = matrix[6] = matrix[11] = 0.7152f; |
| 140 | matrix[2] = matrix[7] = matrix[12] = 0.0722f; |
| 141 | matrix[18] = 1.0f; |
commit-bot@chromium.org | 727a352 | 2014-02-21 18:46:30 +0000 | [diff] [blame] | 142 | SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix)); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 143 | return SkColorFilterImageFilter::Create(filter, input, cropRect); |
| 144 | } |
| 145 | |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 146 | static SkImageFilter* make_blue(SkImageFilter* input, const SkImageFilter::CropRect* cropRect) { |
| 147 | SkAutoTUnref<SkColorFilter> filter(SkColorFilter::CreateModeFilter(SK_ColorBLUE, |
| 148 | SkXfermode::kSrcIn_Mode)); |
| 149 | return SkColorFilterImageFilter::Create(filter, input, cropRect); |
| 150 | } |
| 151 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 152 | DEF_TEST(ImageFilter, reporter) { |
| 153 | { |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 154 | // Check that two non-clipping color-matrice-filters concatenate into a single filter. |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 155 | SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f)); |
| 156 | SkAutoTUnref<SkImageFilter> quarterBrightness(make_scale(0.5f, halfBrightness)); |
| 157 | REPORTER_ASSERT(reporter, NULL == quarterBrightness->getInput(0)); |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 158 | SkColorFilter* cf; |
| 159 | REPORTER_ASSERT(reporter, quarterBrightness->asColorFilter(&cf)); |
| 160 | REPORTER_ASSERT(reporter, cf->asColorMatrix(NULL)); |
| 161 | cf->unref(); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 162 | } |
| 163 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 164 | { |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 165 | // Check that a clipping color-matrice-filter followed by a color-matrice-filters |
| 166 | // concatenates into a single filter, but not a matrixfilter (due to clamping). |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 167 | SkAutoTUnref<SkImageFilter> doubleBrightness(make_scale(2.0f)); |
| 168 | SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f, doubleBrightness)); |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 169 | REPORTER_ASSERT(reporter, NULL == halfBrightness->getInput(0)); |
| 170 | SkColorFilter* cf; |
| 171 | REPORTER_ASSERT(reporter, halfBrightness->asColorFilter(&cf)); |
| 172 | REPORTER_ASSERT(reporter, !cf->asColorMatrix(NULL)); |
| 173 | cf->unref(); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 174 | } |
| 175 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 176 | { |
| 177 | // Check that a color filter image filter without a crop rect can be |
| 178 | // expressed as a color filter. |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 179 | SkAutoTUnref<SkImageFilter> gray(make_grayscale(NULL, NULL)); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 180 | REPORTER_ASSERT(reporter, true == gray->asColorFilter(NULL)); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 181 | } |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 182 | |
| 183 | { |
| 184 | // Check that a colorfilterimage filter without a crop rect but with an input |
| 185 | // that is another colorfilterimage can be expressed as a colorfilter (composed). |
| 186 | SkAutoTUnref<SkImageFilter> mode(make_blue(NULL, NULL)); |
| 187 | SkAutoTUnref<SkImageFilter> gray(make_grayscale(mode, NULL)); |
| 188 | REPORTER_ASSERT(reporter, true == gray->asColorFilter(NULL)); |
| 189 | } |
| 190 | |
| 191 | { |
| 192 | // Test that if we exceed the limit of what ComposeColorFilter can combine, we still |
| 193 | // can build the DAG and won't assert if we call asColorFilter. |
| 194 | SkAutoTUnref<SkImageFilter> filter(make_blue(NULL, NULL)); |
| 195 | const int kWayTooManyForComposeColorFilter = 100; |
| 196 | for (int i = 0; i < kWayTooManyForComposeColorFilter; ++i) { |
| 197 | filter.reset(make_blue(filter, NULL)); |
| 198 | // the first few of these will succeed, but after we hit the internal limit, |
| 199 | // it will then return false. |
| 200 | (void)filter->asColorFilter(NULL); |
| 201 | } |
| 202 | } |
reed | 5c518a8 | 2015-03-05 14:47:29 -0800 | [diff] [blame] | 203 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 204 | { |
| 205 | // Check that a color filter image filter with a crop rect cannot |
| 206 | // be expressed as a color filter. |
| 207 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(0, 0, 100, 100)); |
| 208 | SkAutoTUnref<SkImageFilter> grayWithCrop(make_grayscale(NULL, &cropRect)); |
| 209 | REPORTER_ASSERT(reporter, false == grayWithCrop->asColorFilter(NULL)); |
| 210 | } |
| 211 | |
| 212 | { |
senorblanco | 3df0501 | 2014-07-03 11:13:09 -0700 | [diff] [blame] | 213 | // Check that two non-commutative matrices are concatenated in |
| 214 | // the correct order. |
| 215 | SkScalar blueToRedMatrix[20] = { 0 }; |
| 216 | blueToRedMatrix[2] = blueToRedMatrix[18] = SK_Scalar1; |
| 217 | SkScalar redToGreenMatrix[20] = { 0 }; |
| 218 | redToGreenMatrix[5] = redToGreenMatrix[18] = SK_Scalar1; |
| 219 | SkAutoTUnref<SkColorFilter> blueToRed(SkColorMatrixFilter::Create(blueToRedMatrix)); |
| 220 | SkAutoTUnref<SkImageFilter> filter1(SkColorFilterImageFilter::Create(blueToRed.get())); |
| 221 | SkAutoTUnref<SkColorFilter> redToGreen(SkColorMatrixFilter::Create(redToGreenMatrix)); |
| 222 | SkAutoTUnref<SkImageFilter> filter2(SkColorFilterImageFilter::Create(redToGreen.get(), filter1.get())); |
| 223 | |
| 224 | SkBitmap result; |
| 225 | result.allocN32Pixels(kBitmapSize, kBitmapSize); |
| 226 | |
| 227 | SkPaint paint; |
| 228 | paint.setColor(SK_ColorBLUE); |
| 229 | paint.setImageFilter(filter2.get()); |
| 230 | SkCanvas canvas(result); |
| 231 | canvas.clear(0x0); |
| 232 | SkRect rect = SkRect::Make(SkIRect::MakeWH(kBitmapSize, kBitmapSize)); |
| 233 | canvas.drawRect(rect, paint); |
| 234 | uint32_t pixel = *result.getAddr32(0, 0); |
| 235 | // The result here should be green, since we have effectively shifted blue to green. |
| 236 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 237 | } |
| 238 | |
| 239 | { |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 240 | // Tests pass by not asserting |
| 241 | SkBitmap bitmap, result; |
| 242 | make_small_bitmap(bitmap); |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 243 | result.allocN32Pixels(kBitmapSize, kBitmapSize); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 244 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 245 | { |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 246 | // This tests for : |
| 247 | // 1 ) location at (0,0,1) |
| 248 | SkPoint3 location(0, 0, SK_Scalar1); |
| 249 | // 2 ) location and target at same value |
| 250 | SkPoint3 target(location.fX, location.fY, location.fZ); |
| 251 | // 3 ) large negative specular exponent value |
| 252 | SkScalar specularExponent = -1000; |
| 253 | |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 254 | SkAutoTUnref<SkImageFilter> bmSrc(SkBitmapSource::Create(bitmap)); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 255 | SkPaint paint; |
| 256 | paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular( |
| 257 | location, target, specularExponent, 180, |
| 258 | 0xFFFFFFFF, SK_Scalar1, SK_Scalar1, SK_Scalar1, |
| 259 | bmSrc))->unref(); |
| 260 | SkCanvas canvas(result); |
| 261 | SkRect r = SkRect::MakeWH(SkIntToScalar(kBitmapSize), |
| 262 | SkIntToScalar(kBitmapSize)); |
| 263 | canvas.drawRect(r, paint); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 264 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 265 | } |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 266 | } |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 267 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 268 | static void test_crop_rects(SkBaseDevice* device, skiatest::Reporter* reporter) { |
| 269 | // Check that all filters offset to their absolute crop rect, |
| 270 | // unaffected by the input crop rect. |
| 271 | // Tests pass by not asserting. |
| 272 | SkBitmap bitmap; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 273 | bitmap.allocN32Pixels(100, 100); |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 274 | bitmap.eraseARGB(0, 0, 0, 0); |
fmalita | 2d97bc1 | 2014-11-20 10:44:58 -0800 | [diff] [blame] | 275 | SkDeviceImageFilterProxy proxy(device, SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType)); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 276 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 277 | SkImageFilter::CropRect inputCropRect(SkRect::MakeXYWH(8, 13, 80, 80)); |
| 278 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 30, 60, 60)); |
| 279 | SkAutoTUnref<SkImageFilter> input(make_grayscale(NULL, &inputCropRect)); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 280 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 281 | SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_ColorRED, SkXfermode::kSrcIn_Mode)); |
| 282 | SkPoint3 location(0, 0, SK_Scalar1); |
| 283 | SkPoint3 target(SK_Scalar1, SK_Scalar1, SK_Scalar1); |
| 284 | SkScalar kernel[9] = { |
| 285 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 286 | SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1), |
| 287 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 288 | }; |
| 289 | SkISize kernelSize = SkISize::Make(3, 3); |
| 290 | SkScalar gain = SK_Scalar1, bias = 0; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 291 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 292 | SkImageFilter* filters[] = { |
| 293 | SkColorFilterImageFilter::Create(cf.get(), input.get(), &cropRect), |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 294 | SkDisplacementMapEffect::Create(SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 295 | SkDisplacementMapEffect::kB_ChannelSelectorType, |
| 296 | 40.0f, input.get(), input.get(), &cropRect), |
| 297 | SkBlurImageFilter::Create(SK_Scalar1, SK_Scalar1, input.get(), &cropRect), |
sugoi | 234f036 | 2014-10-23 13:59:52 -0700 | [diff] [blame] | 298 | SkDropShadowImageFilter::Create(SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, |
| 299 | SK_ColorGREEN, SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 300 | input.get(), &cropRect), |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 301 | SkLightingImageFilter::CreatePointLitDiffuse(location, SK_ColorGREEN, 0, 0, input.get(), &cropRect), |
| 302 | 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] | 303 | SkMatrixConvolutionImageFilter::Create(kernelSize, kernel, gain, bias, SkIPoint::Make(1, 1), SkMatrixConvolutionImageFilter::kRepeat_TileMode, false, input.get(), &cropRect), |
| 304 | SkMergeImageFilter::Create(input.get(), input.get(), SkXfermode::kSrcOver_Mode, &cropRect), |
| 305 | SkOffsetImageFilter::Create(SK_Scalar1, SK_Scalar1, input.get(), &cropRect), |
| 306 | SkOffsetImageFilter::Create(SK_Scalar1, SK_Scalar1, input.get(), &cropRect), |
| 307 | SkDilateImageFilter::Create(3, 2, input.get(), &cropRect), |
| 308 | SkErodeImageFilter::Create(2, 3, input.get(), &cropRect), |
| 309 | SkTileImageFilter::Create(inputCropRect.rect(), cropRect.rect(), input.get()), |
| 310 | 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] | 311 | }; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 312 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 313 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 314 | SkImageFilter* filter = filters[i]; |
| 315 | SkBitmap result; |
| 316 | SkIPoint offset; |
| 317 | SkString str; |
senorblanco@chromium.org | f4e1a76 | 2014-02-04 00:28:46 +0000 | [diff] [blame] | 318 | str.printf("filter %d", static_cast<int>(i)); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 319 | SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeLargest(), NULL); |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 320 | REPORTER_ASSERT_MESSAGE(reporter, filter->filterImage(&proxy, bitmap, ctx, |
| 321 | &result, &offset), str.c_str()); |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 322 | 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] | 323 | } |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 324 | |
| 325 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 326 | SkSafeUnref(filters[i]); |
| 327 | } |
| 328 | } |
| 329 | |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 330 | static SkBitmap make_gradient_circle(int width, int height) { |
| 331 | SkBitmap bitmap; |
| 332 | SkScalar x = SkIntToScalar(width / 2); |
| 333 | SkScalar y = SkIntToScalar(height / 2); |
| 334 | SkScalar radius = SkMinScalar(x, y) * 0.8f; |
| 335 | bitmap.allocN32Pixels(width, height); |
| 336 | SkCanvas canvas(bitmap); |
| 337 | canvas.clear(0x00000000); |
| 338 | SkColor colors[2]; |
| 339 | colors[0] = SK_ColorWHITE; |
| 340 | colors[1] = SK_ColorBLACK; |
| 341 | SkAutoTUnref<SkShader> shader( |
| 342 | SkGradientShader::CreateRadial(SkPoint::Make(x, y), radius, colors, NULL, 2, |
| 343 | SkShader::kClamp_TileMode) |
| 344 | ); |
| 345 | SkPaint paint; |
| 346 | paint.setShader(shader); |
| 347 | canvas.drawCircle(x, y, radius, paint); |
| 348 | return bitmap; |
| 349 | } |
| 350 | |
senorblanco | 32673b9 | 2014-09-09 09:15:04 -0700 | [diff] [blame] | 351 | static void test_negative_blur_sigma(SkBaseDevice* device, skiatest::Reporter* reporter) { |
| 352 | // Check that SkBlurImageFilter will accept a negative sigma, either in |
| 353 | // the given arguments or after CTM application. |
| 354 | int width = 32, height = 32; |
fmalita | 2d97bc1 | 2014-11-20 10:44:58 -0800 | [diff] [blame] | 355 | SkDeviceImageFilterProxy proxy(device, SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType)); |
senorblanco | 32673b9 | 2014-09-09 09:15:04 -0700 | [diff] [blame] | 356 | SkScalar five = SkIntToScalar(5); |
| 357 | |
| 358 | SkAutoTUnref<SkBlurImageFilter> positiveFilter( |
| 359 | SkBlurImageFilter::Create(five, five) |
| 360 | ); |
| 361 | |
| 362 | SkAutoTUnref<SkBlurImageFilter> negativeFilter( |
| 363 | SkBlurImageFilter::Create(-five, five) |
| 364 | ); |
| 365 | |
| 366 | SkBitmap gradient = make_gradient_circle(width, height); |
| 367 | SkBitmap positiveResult1, negativeResult1; |
| 368 | SkBitmap positiveResult2, negativeResult2; |
| 369 | SkIPoint offset; |
| 370 | SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeLargest(), NULL); |
| 371 | positiveFilter->filterImage(&proxy, gradient, ctx, &positiveResult1, &offset); |
| 372 | negativeFilter->filterImage(&proxy, gradient, ctx, &negativeResult1, &offset); |
| 373 | SkMatrix negativeScale; |
| 374 | negativeScale.setScale(-SK_Scalar1, SK_Scalar1); |
| 375 | SkImageFilter::Context negativeCTX(negativeScale, SkIRect::MakeLargest(), NULL); |
| 376 | positiveFilter->filterImage(&proxy, gradient, negativeCTX, &negativeResult2, &offset); |
| 377 | negativeFilter->filterImage(&proxy, gradient, negativeCTX, &positiveResult2, &offset); |
| 378 | SkAutoLockPixels lockP1(positiveResult1); |
| 379 | SkAutoLockPixels lockP2(positiveResult2); |
| 380 | SkAutoLockPixels lockN1(negativeResult1); |
| 381 | SkAutoLockPixels lockN2(negativeResult2); |
| 382 | for (int y = 0; y < height; y++) { |
| 383 | int diffs = memcmp(positiveResult1.getAddr32(0, y), negativeResult1.getAddr32(0, y), positiveResult1.rowBytes()); |
| 384 | REPORTER_ASSERT(reporter, !diffs); |
| 385 | if (diffs) { |
| 386 | break; |
| 387 | } |
| 388 | diffs = memcmp(positiveResult1.getAddr32(0, y), negativeResult2.getAddr32(0, y), positiveResult1.rowBytes()); |
| 389 | REPORTER_ASSERT(reporter, !diffs); |
| 390 | if (diffs) { |
| 391 | break; |
| 392 | } |
| 393 | diffs = memcmp(positiveResult1.getAddr32(0, y), positiveResult2.getAddr32(0, y), positiveResult1.rowBytes()); |
| 394 | REPORTER_ASSERT(reporter, !diffs); |
| 395 | if (diffs) { |
| 396 | break; |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | DEF_TEST(TestNegativeBlurSigma, reporter) { |
| 402 | SkBitmap temp; |
| 403 | temp.allocN32Pixels(100, 100); |
| 404 | SkBitmapDevice device(temp); |
| 405 | test_negative_blur_sigma(&device, reporter); |
| 406 | } |
| 407 | |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 408 | DEF_TEST(ImageFilterDrawTiled, reporter) { |
| 409 | // Check that all filters when drawn tiled (with subsequent clip rects) exactly |
| 410 | // match the same filters drawn with a single full-canvas bitmap draw. |
| 411 | // Tests pass by not asserting. |
| 412 | |
| 413 | SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_ColorRED, SkXfermode::kSrcIn_Mode)); |
| 414 | SkPoint3 location(0, 0, SK_Scalar1); |
| 415 | SkPoint3 target(SK_Scalar1, SK_Scalar1, SK_Scalar1); |
| 416 | SkScalar kernel[9] = { |
| 417 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 418 | SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1), |
| 419 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 420 | }; |
| 421 | SkISize kernelSize = SkISize::Make(3, 3); |
| 422 | SkScalar gain = SK_Scalar1, bias = 0; |
senorblanco@chromium.org | 29ac34e | 2014-05-28 19:29:25 +0000 | [diff] [blame] | 423 | SkScalar five = SkIntToScalar(5); |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 424 | |
| 425 | SkAutoTUnref<SkImageFilter> gradient_source(SkBitmapSource::Create(make_gradient_circle(64, 64))); |
senorblanco@chromium.org | 29ac34e | 2014-05-28 19:29:25 +0000 | [diff] [blame] | 426 | SkAutoTUnref<SkImageFilter> blur(SkBlurImageFilter::Create(five, five)); |
senorblanco@chromium.org | ba31f1d | 2014-05-07 20:56:08 +0000 | [diff] [blame] | 427 | SkMatrix matrix; |
senorblanco@chromium.org | 29ac34e | 2014-05-28 19:29:25 +0000 | [diff] [blame] | 428 | |
senorblanco@chromium.org | ba31f1d | 2014-05-07 20:56:08 +0000 | [diff] [blame] | 429 | matrix.setTranslate(SK_Scalar1, SK_Scalar1); |
| 430 | matrix.postRotate(SkIntToScalar(45), SK_Scalar1, SK_Scalar1); |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 431 | |
senorblanco@chromium.org | 910702b | 2014-05-30 20:36:15 +0000 | [diff] [blame] | 432 | SkRTreeFactory factory; |
| 433 | SkPictureRecorder recorder; |
| 434 | SkCanvas* recordingCanvas = recorder.beginRecording(64, 64, &factory, 0); |
| 435 | |
| 436 | SkPaint greenPaint; |
| 437 | greenPaint.setColor(SK_ColorGREEN); |
| 438 | recordingCanvas->drawRect(SkRect::Make(SkIRect::MakeXYWH(10, 10, 30, 20)), greenPaint); |
| 439 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 440 | SkAutoTUnref<SkImageFilter> pictureFilter(SkPictureImageFilter::Create(picture.get())); |
senorblanco | 8f3937d | 2014-10-29 12:36:32 -0700 | [diff] [blame] | 441 | SkAutoTUnref<SkShader> shader(SkPerlinNoiseShader::CreateTurbulence(SK_Scalar1, SK_Scalar1, 1, 0)); |
| 442 | |
| 443 | SkAutoTUnref<SkImageFilter> rectShaderFilter(SkRectShaderImageFilter::Create(shader.get())); |
senorblanco@chromium.org | 910702b | 2014-05-30 20:36:15 +0000 | [diff] [blame] | 444 | |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 445 | struct { |
| 446 | const char* fName; |
| 447 | SkImageFilter* fFilter; |
| 448 | } filters[] = { |
| 449 | { "color filter", SkColorFilterImageFilter::Create(cf.get()) }, |
| 450 | { "displacement map", SkDisplacementMapEffect::Create( |
| 451 | SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 452 | SkDisplacementMapEffect::kB_ChannelSelectorType, |
senorblanco@chromium.org | d4db657 | 2014-05-07 20:00:04 +0000 | [diff] [blame] | 453 | 20.0f, gradient_source.get()) }, |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 454 | { "blur", SkBlurImageFilter::Create(SK_Scalar1, SK_Scalar1) }, |
| 455 | { "drop shadow", SkDropShadowImageFilter::Create( |
sugoi | 234f036 | 2014-10-23 13:59:52 -0700 | [diff] [blame] | 456 | SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_ColorGREEN, |
| 457 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode) }, |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 458 | { "diffuse lighting", SkLightingImageFilter::CreatePointLitDiffuse( |
| 459 | location, SK_ColorGREEN, 0, 0) }, |
| 460 | { "specular lighting", |
| 461 | SkLightingImageFilter::CreatePointLitSpecular(location, SK_ColorGREEN, 0, 0, 0) }, |
| 462 | { "matrix convolution", |
| 463 | SkMatrixConvolutionImageFilter::Create( |
| 464 | kernelSize, kernel, gain, bias, SkIPoint::Make(1, 1), |
| 465 | SkMatrixConvolutionImageFilter::kRepeat_TileMode, false) }, |
| 466 | { "merge", SkMergeImageFilter::Create(NULL, NULL, SkXfermode::kSrcOver_Mode) }, |
| 467 | { "offset", SkOffsetImageFilter::Create(SK_Scalar1, SK_Scalar1) }, |
| 468 | { "dilate", SkDilateImageFilter::Create(3, 2) }, |
| 469 | { "erode", SkErodeImageFilter::Create(2, 3) }, |
| 470 | { "tile", SkTileImageFilter::Create(SkRect::MakeXYWH(0, 0, 50, 50), |
| 471 | SkRect::MakeXYWH(0, 0, 100, 100), NULL) }, |
senorblanco | 8c874ee | 2015-03-20 06:38:17 -0700 | [diff] [blame] | 472 | { "matrix", SkImageFilter::CreateMatrixFilter(matrix, kLow_SkFilterQuality) }, |
senorblanco@chromium.org | 29ac34e | 2014-05-28 19:29:25 +0000 | [diff] [blame] | 473 | { "blur and offset", SkOffsetImageFilter::Create(five, five, blur.get()) }, |
senorblanco@chromium.org | 910702b | 2014-05-30 20:36:15 +0000 | [diff] [blame] | 474 | { "picture and blur", SkBlurImageFilter::Create(five, five, pictureFilter.get()) }, |
senorblanco | 8f3937d | 2014-10-29 12:36:32 -0700 | [diff] [blame] | 475 | { "rect shader and blur", SkBlurImageFilter::Create(five, five, rectShaderFilter.get()) }, |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 476 | }; |
| 477 | |
| 478 | SkBitmap untiledResult, tiledResult; |
| 479 | int width = 64, height = 64; |
| 480 | untiledResult.allocN32Pixels(width, height); |
| 481 | tiledResult.allocN32Pixels(width, height); |
| 482 | SkCanvas tiledCanvas(tiledResult); |
| 483 | SkCanvas untiledCanvas(untiledResult); |
senorblanco@chromium.org | d4db657 | 2014-05-07 20:00:04 +0000 | [diff] [blame] | 484 | int tileSize = 8; |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 485 | |
senorblanco@chromium.org | d4db657 | 2014-05-07 20:00:04 +0000 | [diff] [blame] | 486 | for (int scale = 1; scale <= 2; ++scale) { |
| 487 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 488 | tiledCanvas.clear(0); |
| 489 | untiledCanvas.clear(0); |
| 490 | SkPaint paint; |
| 491 | paint.setImageFilter(filters[i].fFilter); |
| 492 | paint.setTextSize(SkIntToScalar(height)); |
| 493 | paint.setColor(SK_ColorWHITE); |
| 494 | SkString str; |
| 495 | const char* text = "ABC"; |
| 496 | SkScalar ypos = SkIntToScalar(height); |
| 497 | untiledCanvas.save(); |
| 498 | untiledCanvas.scale(SkIntToScalar(scale), SkIntToScalar(scale)); |
| 499 | untiledCanvas.drawText(text, strlen(text), 0, ypos, paint); |
| 500 | untiledCanvas.restore(); |
| 501 | for (int y = 0; y < height; y += tileSize) { |
| 502 | for (int x = 0; x < width; x += tileSize) { |
| 503 | tiledCanvas.save(); |
| 504 | tiledCanvas.clipRect(SkRect::Make(SkIRect::MakeXYWH(x, y, tileSize, tileSize))); |
| 505 | tiledCanvas.scale(SkIntToScalar(scale), SkIntToScalar(scale)); |
| 506 | tiledCanvas.drawText(text, strlen(text), 0, ypos, paint); |
| 507 | tiledCanvas.restore(); |
| 508 | } |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 509 | } |
senorblanco@chromium.org | d4db657 | 2014-05-07 20:00:04 +0000 | [diff] [blame] | 510 | untiledCanvas.flush(); |
| 511 | tiledCanvas.flush(); |
| 512 | for (int y = 0; y < height; y++) { |
| 513 | int diffs = memcmp(untiledResult.getAddr32(0, y), tiledResult.getAddr32(0, y), untiledResult.rowBytes()); |
| 514 | REPORTER_ASSERT_MESSAGE(reporter, !diffs, filters[i].fName); |
| 515 | if (diffs) { |
| 516 | break; |
| 517 | } |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 523 | SkSafeUnref(filters[i].fFilter); |
| 524 | } |
| 525 | } |
| 526 | |
mtklein | 3f3b3d0 | 2014-12-01 11:47:08 -0800 | [diff] [blame] | 527 | static void draw_saveLayer_picture(int width, int height, int tileSize, |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 528 | SkBBHFactory* factory, SkBitmap* result) { |
mtklein | d910f54 | 2014-08-22 09:06:34 -0700 | [diff] [blame] | 529 | |
| 530 | SkMatrix matrix; |
| 531 | matrix.setTranslate(SkIntToScalar(50), 0); |
| 532 | |
| 533 | SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_ColorWHITE, SkXfermode::kSrc_Mode)); |
| 534 | SkAutoTUnref<SkImageFilter> cfif(SkColorFilterImageFilter::Create(cf.get())); |
senorblanco | 8c874ee | 2015-03-20 06:38:17 -0700 | [diff] [blame] | 535 | SkAutoTUnref<SkImageFilter> imageFilter(SkImageFilter::CreateMatrixFilter(matrix, kNone_SkFilterQuality, cfif.get())); |
mtklein | d910f54 | 2014-08-22 09:06:34 -0700 | [diff] [blame] | 536 | |
| 537 | SkPaint paint; |
| 538 | paint.setImageFilter(imageFilter.get()); |
| 539 | SkPictureRecorder recorder; |
| 540 | SkRect bounds = SkRect::Make(SkIRect::MakeXYWH(0, 0, 50, 50)); |
mtklein | 3f3b3d0 | 2014-12-01 11:47:08 -0800 | [diff] [blame] | 541 | SkCanvas* recordingCanvas = recorder.beginRecording(SkIntToScalar(width), |
| 542 | SkIntToScalar(height), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 543 | factory, 0); |
mtklein | d910f54 | 2014-08-22 09:06:34 -0700 | [diff] [blame] | 544 | recordingCanvas->translate(-55, 0); |
| 545 | recordingCanvas->saveLayer(&bounds, &paint); |
| 546 | recordingCanvas->restore(); |
| 547 | SkAutoTUnref<SkPicture> picture1(recorder.endRecording()); |
| 548 | |
| 549 | result->allocN32Pixels(width, height); |
| 550 | SkCanvas canvas(*result); |
| 551 | canvas.clear(0); |
| 552 | canvas.clipRect(SkRect::Make(SkIRect::MakeWH(tileSize, tileSize))); |
| 553 | canvas.drawPicture(picture1.get()); |
| 554 | } |
| 555 | |
| 556 | DEF_TEST(ImageFilterDrawMatrixBBH, reporter) { |
| 557 | // Check that matrix filter when drawn tiled with BBH exactly |
| 558 | // matches the same thing drawn without BBH. |
| 559 | // Tests pass by not asserting. |
| 560 | |
| 561 | const int width = 200, height = 200; |
| 562 | const int tileSize = 100; |
| 563 | SkBitmap result1, result2; |
| 564 | SkRTreeFactory factory; |
| 565 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 566 | draw_saveLayer_picture(width, height, tileSize, &factory, &result1); |
| 567 | draw_saveLayer_picture(width, height, tileSize, NULL, &result2); |
mtklein | d910f54 | 2014-08-22 09:06:34 -0700 | [diff] [blame] | 568 | |
| 569 | for (int y = 0; y < height; y++) { |
| 570 | int diffs = memcmp(result1.getAddr32(0, y), result2.getAddr32(0, y), result1.rowBytes()); |
| 571 | REPORTER_ASSERT(reporter, !diffs); |
| 572 | if (diffs) { |
| 573 | break; |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
senorblanco | 1150a6d | 2014-08-25 12:46:58 -0700 | [diff] [blame] | 578 | static SkImageFilter* makeBlur(SkImageFilter* input = NULL) { |
| 579 | return SkBlurImageFilter::Create(SK_Scalar1, SK_Scalar1, input); |
| 580 | } |
| 581 | |
| 582 | static SkImageFilter* makeDropShadow(SkImageFilter* input = NULL) { |
| 583 | return SkDropShadowImageFilter::Create( |
| 584 | SkIntToScalar(100), SkIntToScalar(100), |
| 585 | SkIntToScalar(10), SkIntToScalar(10), |
sugoi | 234f036 | 2014-10-23 13:59:52 -0700 | [diff] [blame] | 586 | SK_ColorBLUE, SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 587 | input, NULL); |
senorblanco | 1150a6d | 2014-08-25 12:46:58 -0700 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | DEF_TEST(ImageFilterBlurThenShadowBounds, reporter) { |
| 591 | SkAutoTUnref<SkImageFilter> filter1(makeBlur()); |
| 592 | SkAutoTUnref<SkImageFilter> filter2(makeDropShadow(filter1.get())); |
| 593 | |
| 594 | SkIRect bounds = SkIRect::MakeXYWH(0, 0, 100, 100); |
| 595 | SkIRect expectedBounds = SkIRect::MakeXYWH(-133, -133, 236, 236); |
| 596 | filter2->filterBounds(bounds, SkMatrix::I(), &bounds); |
| 597 | |
| 598 | REPORTER_ASSERT(reporter, bounds == expectedBounds); |
| 599 | } |
| 600 | |
| 601 | DEF_TEST(ImageFilterShadowThenBlurBounds, reporter) { |
| 602 | SkAutoTUnref<SkImageFilter> filter1(makeDropShadow()); |
| 603 | SkAutoTUnref<SkImageFilter> filter2(makeBlur(filter1.get())); |
| 604 | |
| 605 | SkIRect bounds = SkIRect::MakeXYWH(0, 0, 100, 100); |
| 606 | SkIRect expectedBounds = SkIRect::MakeXYWH(-133, -133, 236, 236); |
| 607 | filter2->filterBounds(bounds, SkMatrix::I(), &bounds); |
| 608 | |
| 609 | REPORTER_ASSERT(reporter, bounds == expectedBounds); |
| 610 | } |
| 611 | |
| 612 | DEF_TEST(ImageFilterDilateThenBlurBounds, reporter) { |
| 613 | SkAutoTUnref<SkImageFilter> filter1(SkDilateImageFilter::Create(2, 2)); |
| 614 | SkAutoTUnref<SkImageFilter> filter2(makeDropShadow(filter1.get())); |
| 615 | |
| 616 | SkIRect bounds = SkIRect::MakeXYWH(0, 0, 100, 100); |
| 617 | SkIRect expectedBounds = SkIRect::MakeXYWH(-132, -132, 234, 234); |
| 618 | filter2->filterBounds(bounds, SkMatrix::I(), &bounds); |
| 619 | |
| 620 | REPORTER_ASSERT(reporter, bounds == expectedBounds); |
| 621 | } |
| 622 | |
ajuma | 5788faa | 2015-02-13 09:05:47 -0800 | [diff] [blame] | 623 | DEF_TEST(ImageFilterComposedBlurFastBounds, reporter) { |
| 624 | SkAutoTUnref<SkImageFilter> filter1(makeBlur()); |
| 625 | SkAutoTUnref<SkImageFilter> filter2(makeBlur()); |
| 626 | SkAutoTUnref<SkImageFilter> composedFilter(SkComposeImageFilter::Create(filter1.get(), filter2.get())); |
| 627 | |
| 628 | SkRect boundsSrc = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100)); |
| 629 | SkRect expectedBounds = SkRect::MakeXYWH( |
| 630 | SkIntToScalar(-6), SkIntToScalar(-6), SkIntToScalar(112), SkIntToScalar(112)); |
| 631 | SkRect boundsDst = SkRect::MakeEmpty(); |
| 632 | composedFilter->computeFastBounds(boundsSrc, &boundsDst); |
| 633 | |
| 634 | REPORTER_ASSERT(reporter, boundsDst == expectedBounds); |
| 635 | } |
| 636 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 637 | static void draw_blurred_rect(SkCanvas* canvas) { |
senorblanco | 837f532 | 2014-07-14 10:19:54 -0700 | [diff] [blame] | 638 | SkAutoTUnref<SkImageFilter> filter(SkBlurImageFilter::Create(SkIntToScalar(8), 0)); |
| 639 | SkPaint filterPaint; |
| 640 | filterPaint.setColor(SK_ColorWHITE); |
| 641 | filterPaint.setImageFilter(filter); |
| 642 | canvas->saveLayer(NULL, &filterPaint); |
| 643 | SkPaint whitePaint; |
| 644 | whitePaint.setColor(SK_ColorWHITE); |
| 645 | canvas->drawRect(SkRect::Make(SkIRect::MakeWH(4, 4)), whitePaint); |
| 646 | canvas->restore(); |
| 647 | } |
| 648 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 649 | static void draw_picture_clipped(SkCanvas* canvas, const SkRect& clipRect, const SkPicture* picture) { |
senorblanco | 837f532 | 2014-07-14 10:19:54 -0700 | [diff] [blame] | 650 | canvas->save(); |
| 651 | canvas->clipRect(clipRect); |
| 652 | canvas->drawPicture(picture); |
| 653 | canvas->restore(); |
| 654 | } |
| 655 | |
| 656 | DEF_TEST(ImageFilterDrawTiledBlurRTree, reporter) { |
| 657 | // Check that the blur filter when recorded with RTree acceleration, |
| 658 | // and drawn tiled (with subsequent clip rects) exactly |
| 659 | // matches the same filter drawn with without RTree acceleration. |
| 660 | // This tests that the "bleed" from the blur into the otherwise-blank |
| 661 | // tiles is correctly rendered. |
| 662 | // Tests pass by not asserting. |
| 663 | |
| 664 | int width = 16, height = 8; |
| 665 | SkBitmap result1, result2; |
| 666 | result1.allocN32Pixels(width, height); |
| 667 | result2.allocN32Pixels(width, height); |
| 668 | SkCanvas canvas1(result1); |
| 669 | SkCanvas canvas2(result2); |
| 670 | int tileSize = 8; |
| 671 | |
| 672 | canvas1.clear(0); |
| 673 | canvas2.clear(0); |
| 674 | |
| 675 | SkRTreeFactory factory; |
| 676 | |
| 677 | SkPictureRecorder recorder1, recorder2; |
| 678 | // The only difference between these two pictures is that one has RTree aceleration. |
mtklein | 3f3b3d0 | 2014-12-01 11:47:08 -0800 | [diff] [blame] | 679 | SkCanvas* recordingCanvas1 = recorder1.beginRecording(SkIntToScalar(width), |
| 680 | SkIntToScalar(height), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 681 | NULL, 0); |
mtklein | 3f3b3d0 | 2014-12-01 11:47:08 -0800 | [diff] [blame] | 682 | SkCanvas* recordingCanvas2 = recorder2.beginRecording(SkIntToScalar(width), |
| 683 | SkIntToScalar(height), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 684 | &factory, 0); |
| 685 | draw_blurred_rect(recordingCanvas1); |
| 686 | draw_blurred_rect(recordingCanvas2); |
senorblanco | 837f532 | 2014-07-14 10:19:54 -0700 | [diff] [blame] | 687 | SkAutoTUnref<SkPicture> picture1(recorder1.endRecording()); |
| 688 | SkAutoTUnref<SkPicture> picture2(recorder2.endRecording()); |
| 689 | for (int y = 0; y < height; y += tileSize) { |
| 690 | for (int x = 0; x < width; x += tileSize) { |
| 691 | SkRect tileRect = SkRect::Make(SkIRect::MakeXYWH(x, y, tileSize, tileSize)); |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 692 | draw_picture_clipped(&canvas1, tileRect, picture1); |
| 693 | draw_picture_clipped(&canvas2, tileRect, picture2); |
senorblanco | 837f532 | 2014-07-14 10:19:54 -0700 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | for (int y = 0; y < height; y++) { |
| 697 | int diffs = memcmp(result1.getAddr32(0, y), result2.getAddr32(0, y), result1.rowBytes()); |
| 698 | REPORTER_ASSERT(reporter, !diffs); |
| 699 | if (diffs) { |
| 700 | break; |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | |
senorblanco@chromium.org | 9195743 | 2014-05-01 14:03:41 +0000 | [diff] [blame] | 705 | DEF_TEST(ImageFilterMatrixConvolution, reporter) { |
| 706 | // Check that a 1x3 filter does not cause a spurious assert. |
| 707 | SkScalar kernel[3] = { |
| 708 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 709 | }; |
| 710 | SkISize kernelSize = SkISize::Make(1, 3); |
| 711 | SkScalar gain = SK_Scalar1, bias = 0; |
| 712 | SkIPoint kernelOffset = SkIPoint::Make(0, 0); |
| 713 | |
| 714 | SkAutoTUnref<SkImageFilter> filter( |
| 715 | SkMatrixConvolutionImageFilter::Create( |
| 716 | kernelSize, kernel, gain, bias, kernelOffset, |
| 717 | SkMatrixConvolutionImageFilter::kRepeat_TileMode, false)); |
| 718 | |
| 719 | SkBitmap result; |
| 720 | int width = 16, height = 16; |
| 721 | result.allocN32Pixels(width, height); |
| 722 | SkCanvas canvas(result); |
| 723 | canvas.clear(0); |
| 724 | |
| 725 | SkPaint paint; |
| 726 | paint.setImageFilter(filter); |
| 727 | SkRect rect = SkRect::Make(SkIRect::MakeWH(width, height)); |
| 728 | canvas.drawRect(rect, paint); |
| 729 | } |
| 730 | |
senorblanco@chromium.org | 8c7372b | 2014-05-02 19:13:11 +0000 | [diff] [blame] | 731 | DEF_TEST(ImageFilterMatrixConvolutionBorder, reporter) { |
| 732 | // Check that a filter with borders outside the target bounds |
| 733 | // does not crash. |
| 734 | SkScalar kernel[3] = { |
| 735 | 0, 0, 0, |
| 736 | }; |
| 737 | SkISize kernelSize = SkISize::Make(3, 1); |
| 738 | SkScalar gain = SK_Scalar1, bias = 0; |
| 739 | SkIPoint kernelOffset = SkIPoint::Make(2, 0); |
| 740 | |
| 741 | SkAutoTUnref<SkImageFilter> filter( |
| 742 | SkMatrixConvolutionImageFilter::Create( |
| 743 | kernelSize, kernel, gain, bias, kernelOffset, |
| 744 | SkMatrixConvolutionImageFilter::kClamp_TileMode, true)); |
| 745 | |
| 746 | SkBitmap result; |
| 747 | |
| 748 | int width = 10, height = 10; |
| 749 | result.allocN32Pixels(width, height); |
| 750 | SkCanvas canvas(result); |
| 751 | canvas.clear(0); |
| 752 | |
| 753 | SkPaint filterPaint; |
| 754 | filterPaint.setImageFilter(filter); |
| 755 | SkRect bounds = SkRect::MakeWH(1, 10); |
| 756 | SkRect rect = SkRect::Make(SkIRect::MakeWH(width, height)); |
| 757 | SkPaint rectPaint; |
| 758 | canvas.saveLayer(&bounds, &filterPaint); |
| 759 | canvas.drawRect(rect, rectPaint); |
| 760 | canvas.restore(); |
| 761 | } |
| 762 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 763 | DEF_TEST(ImageFilterCropRect, reporter) { |
| 764 | SkBitmap temp; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 765 | temp.allocN32Pixels(100, 100); |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 766 | SkBitmapDevice device(temp); |
| 767 | test_crop_rects(&device, reporter); |
| 768 | } |
| 769 | |
tfarina | 9ea53f9 | 2014-06-24 06:50:39 -0700 | [diff] [blame] | 770 | DEF_TEST(ImageFilterMatrix, reporter) { |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 771 | SkBitmap temp; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 772 | temp.allocN32Pixels(100, 100); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 773 | SkBitmapDevice device(temp); |
| 774 | SkCanvas canvas(&device); |
| 775 | canvas.scale(SkIntToScalar(2), SkIntToScalar(2)); |
| 776 | |
| 777 | SkMatrix expectedMatrix = canvas.getTotalMatrix(); |
| 778 | |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 779 | SkRTreeFactory factory; |
| 780 | SkPictureRecorder recorder; |
| 781 | SkCanvas* recordingCanvas = recorder.beginRecording(100, 100, &factory, 0); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 782 | |
| 783 | SkPaint paint; |
| 784 | SkAutoTUnref<MatrixTestImageFilter> imageFilter( |
| 785 | new MatrixTestImageFilter(reporter, expectedMatrix)); |
| 786 | paint.setImageFilter(imageFilter.get()); |
commit-bot@chromium.org | 091a594 | 2014-04-18 14:19:31 +0000 | [diff] [blame] | 787 | recordingCanvas->saveLayer(NULL, &paint); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 788 | SkPaint solidPaint; |
| 789 | solidPaint.setColor(0xFFFFFFFF); |
| 790 | recordingCanvas->save(); |
| 791 | recordingCanvas->scale(SkIntToScalar(10), SkIntToScalar(10)); |
| 792 | recordingCanvas->drawRect(SkRect::Make(SkIRect::MakeWH(100, 100)), solidPaint); |
| 793 | recordingCanvas->restore(); // scale |
| 794 | recordingCanvas->restore(); // saveLayer |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 795 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 796 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 797 | canvas.drawPicture(picture); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 798 | } |
| 799 | |
senorblanco | 3d822c2 | 2014-07-30 14:49:31 -0700 | [diff] [blame] | 800 | DEF_TEST(ImageFilterCrossProcessPictureImageFilter, reporter) { |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 801 | SkRTreeFactory factory; |
| 802 | SkPictureRecorder recorder; |
| 803 | SkCanvas* recordingCanvas = recorder.beginRecording(1, 1, &factory, 0); |
| 804 | |
| 805 | // Create an SkPicture which simply draws a green 1x1 rectangle. |
| 806 | SkPaint greenPaint; |
| 807 | greenPaint.setColor(SK_ColorGREEN); |
| 808 | recordingCanvas->drawRect(SkRect::Make(SkIRect::MakeWH(1, 1)), greenPaint); |
| 809 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 810 | |
| 811 | // Wrap that SkPicture in an SkPictureImageFilter. |
| 812 | SkAutoTUnref<SkImageFilter> imageFilter( |
| 813 | SkPictureImageFilter::Create(picture.get())); |
| 814 | |
| 815 | // Check that SkPictureImageFilter successfully serializes its contained |
| 816 | // SkPicture when not in cross-process mode. |
| 817 | SkPaint paint; |
| 818 | paint.setImageFilter(imageFilter.get()); |
| 819 | SkPictureRecorder outerRecorder; |
| 820 | SkCanvas* outerCanvas = outerRecorder.beginRecording(1, 1, &factory, 0); |
| 821 | SkPaint redPaintWithFilter; |
| 822 | redPaintWithFilter.setColor(SK_ColorRED); |
| 823 | redPaintWithFilter.setImageFilter(imageFilter.get()); |
| 824 | outerCanvas->drawRect(SkRect::Make(SkIRect::MakeWH(1, 1)), redPaintWithFilter); |
| 825 | SkAutoTUnref<SkPicture> outerPicture(outerRecorder.endRecording()); |
| 826 | |
| 827 | SkBitmap bitmap; |
| 828 | bitmap.allocN32Pixels(1, 1); |
| 829 | SkBitmapDevice device(bitmap); |
| 830 | SkCanvas canvas(&device); |
| 831 | |
| 832 | // The result here should be green, since the filter replaces the primitive's red interior. |
| 833 | canvas.clear(0x0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 834 | canvas.drawPicture(outerPicture); |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 835 | uint32_t pixel = *bitmap.getAddr32(0, 0); |
| 836 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 837 | |
| 838 | // Check that, for now, SkPictureImageFilter does not serialize or |
| 839 | // deserialize its contained picture when the filter is serialized |
| 840 | // cross-process. Do this by "laundering" it through SkValidatingReadBuffer. |
| 841 | SkAutoTUnref<SkData> data(SkValidatingSerializeFlattenable(imageFilter.get())); |
| 842 | SkAutoTUnref<SkFlattenable> flattenable(SkValidatingDeserializeFlattenable( |
| 843 | data->data(), data->size(), SkImageFilter::GetFlattenableType())); |
| 844 | SkImageFilter* unflattenedFilter = static_cast<SkImageFilter*>(flattenable.get()); |
| 845 | |
| 846 | redPaintWithFilter.setImageFilter(unflattenedFilter); |
| 847 | SkPictureRecorder crossProcessRecorder; |
| 848 | SkCanvas* crossProcessCanvas = crossProcessRecorder.beginRecording(1, 1, &factory, 0); |
| 849 | crossProcessCanvas->drawRect(SkRect::Make(SkIRect::MakeWH(1, 1)), redPaintWithFilter); |
| 850 | SkAutoTUnref<SkPicture> crossProcessPicture(crossProcessRecorder.endRecording()); |
| 851 | |
| 852 | canvas.clear(0x0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 853 | canvas.drawPicture(crossProcessPicture); |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 854 | pixel = *bitmap.getAddr32(0, 0); |
robertphillips | c4bd39c | 2015-01-06 09:17:02 -0800 | [diff] [blame] | 855 | #ifdef SK_DISALLOW_CROSSPROCESS_PICTUREIMAGEFILTERS |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 856 | // The result here should not be green, since the filter draws nothing. |
| 857 | REPORTER_ASSERT(reporter, pixel != SK_ColorGREEN); |
robertphillips | c4bd39c | 2015-01-06 09:17:02 -0800 | [diff] [blame] | 858 | #else |
| 859 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 860 | #endif |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 861 | } |
| 862 | |
senorblanco | 3d822c2 | 2014-07-30 14:49:31 -0700 | [diff] [blame] | 863 | DEF_TEST(ImageFilterClippedPictureImageFilter, reporter) { |
| 864 | SkRTreeFactory factory; |
| 865 | SkPictureRecorder recorder; |
| 866 | SkCanvas* recordingCanvas = recorder.beginRecording(1, 1, &factory, 0); |
| 867 | |
| 868 | // Create an SkPicture which simply draws a green 1x1 rectangle. |
| 869 | SkPaint greenPaint; |
| 870 | greenPaint.setColor(SK_ColorGREEN); |
| 871 | recordingCanvas->drawRect(SkRect::Make(SkIRect::MakeWH(1, 1)), greenPaint); |
| 872 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 873 | |
| 874 | SkAutoTUnref<SkImageFilter> imageFilter( |
| 875 | SkPictureImageFilter::Create(picture.get())); |
| 876 | |
| 877 | SkBitmap result; |
| 878 | SkIPoint offset; |
| 879 | SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeXYWH(1, 1, 1, 1), NULL); |
| 880 | SkBitmap bitmap; |
| 881 | bitmap.allocN32Pixels(2, 2); |
| 882 | SkBitmapDevice device(bitmap); |
fmalita | 2d97bc1 | 2014-11-20 10:44:58 -0800 | [diff] [blame] | 883 | SkDeviceImageFilterProxy proxy(&device, SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType)); |
senorblanco | 3d822c2 | 2014-07-30 14:49:31 -0700 | [diff] [blame] | 884 | REPORTER_ASSERT(reporter, !imageFilter->filterImage(&proxy, bitmap, ctx, &result, &offset)); |
| 885 | } |
| 886 | |
tfarina | 9ea53f9 | 2014-06-24 06:50:39 -0700 | [diff] [blame] | 887 | DEF_TEST(ImageFilterEmptySaveLayer, reporter) { |
senorblanco@chromium.org | 68250c8 | 2014-05-06 22:52:55 +0000 | [diff] [blame] | 888 | // Even when there's an empty saveLayer()/restore(), ensure that an image |
| 889 | // filter or color filter which affects transparent black still draws. |
| 890 | |
| 891 | SkBitmap bitmap; |
| 892 | bitmap.allocN32Pixels(10, 10); |
| 893 | SkBitmapDevice device(bitmap); |
| 894 | SkCanvas canvas(&device); |
| 895 | |
| 896 | SkRTreeFactory factory; |
| 897 | SkPictureRecorder recorder; |
| 898 | |
| 899 | SkAutoTUnref<SkColorFilter> green( |
| 900 | SkColorFilter::CreateModeFilter(SK_ColorGREEN, SkXfermode::kSrc_Mode)); |
| 901 | SkAutoTUnref<SkColorFilterImageFilter> imageFilter( |
| 902 | SkColorFilterImageFilter::Create(green.get())); |
| 903 | SkPaint imageFilterPaint; |
| 904 | imageFilterPaint.setImageFilter(imageFilter.get()); |
| 905 | SkPaint colorFilterPaint; |
| 906 | colorFilterPaint.setColorFilter(green.get()); |
| 907 | |
| 908 | SkRect bounds = SkRect::MakeWH(10, 10); |
| 909 | |
| 910 | SkCanvas* recordingCanvas = recorder.beginRecording(10, 10, &factory, 0); |
| 911 | recordingCanvas->saveLayer(&bounds, &imageFilterPaint); |
| 912 | recordingCanvas->restore(); |
| 913 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 914 | |
| 915 | canvas.clear(0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 916 | canvas.drawPicture(picture); |
senorblanco@chromium.org | 68250c8 | 2014-05-06 22:52:55 +0000 | [diff] [blame] | 917 | uint32_t pixel = *bitmap.getAddr32(0, 0); |
| 918 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 919 | |
| 920 | recordingCanvas = recorder.beginRecording(10, 10, &factory, 0); |
| 921 | recordingCanvas->saveLayer(NULL, &imageFilterPaint); |
| 922 | recordingCanvas->restore(); |
| 923 | SkAutoTUnref<SkPicture> picture2(recorder.endRecording()); |
| 924 | |
| 925 | canvas.clear(0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 926 | canvas.drawPicture(picture2); |
senorblanco@chromium.org | 68250c8 | 2014-05-06 22:52:55 +0000 | [diff] [blame] | 927 | pixel = *bitmap.getAddr32(0, 0); |
| 928 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 929 | |
| 930 | recordingCanvas = recorder.beginRecording(10, 10, &factory, 0); |
| 931 | recordingCanvas->saveLayer(&bounds, &colorFilterPaint); |
| 932 | recordingCanvas->restore(); |
| 933 | SkAutoTUnref<SkPicture> picture3(recorder.endRecording()); |
| 934 | |
| 935 | canvas.clear(0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 936 | canvas.drawPicture(picture3); |
senorblanco@chromium.org | 68250c8 | 2014-05-06 22:52:55 +0000 | [diff] [blame] | 937 | pixel = *bitmap.getAddr32(0, 0); |
| 938 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 939 | } |
| 940 | |
senorblanco@chromium.org | 28ae55d | 2014-03-24 21:32:28 +0000 | [diff] [blame] | 941 | static void test_huge_blur(SkBaseDevice* device, skiatest::Reporter* reporter) { |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 942 | SkCanvas canvas(device); |
| 943 | |
| 944 | SkBitmap bitmap; |
| 945 | bitmap.allocN32Pixels(100, 100); |
| 946 | bitmap.eraseARGB(0, 0, 0, 0); |
| 947 | |
| 948 | // Check that a blur with an insane radius does not crash or assert. |
| 949 | SkAutoTUnref<SkImageFilter> blur(SkBlurImageFilter::Create(SkIntToScalar(1<<30), SkIntToScalar(1<<30))); |
| 950 | |
| 951 | SkPaint paint; |
| 952 | paint.setImageFilter(blur); |
| 953 | canvas.drawSprite(bitmap, 0, 0, &paint); |
| 954 | } |
| 955 | |
| 956 | DEF_TEST(HugeBlurImageFilter, reporter) { |
| 957 | SkBitmap temp; |
| 958 | temp.allocN32Pixels(100, 100); |
| 959 | SkBitmapDevice device(temp); |
| 960 | test_huge_blur(&device, reporter); |
| 961 | } |
| 962 | |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 963 | DEF_TEST(MatrixConvolutionSanityTest, reporter) { |
| 964 | SkScalar kernel[1] = { 0 }; |
| 965 | SkScalar gain = SK_Scalar1, bias = 0; |
| 966 | SkIPoint kernelOffset = SkIPoint::Make(1, 1); |
| 967 | |
| 968 | // Check that an enormous (non-allocatable) kernel gives a NULL filter. |
| 969 | SkAutoTUnref<SkImageFilter> conv(SkMatrixConvolutionImageFilter::Create( |
| 970 | SkISize::Make(1<<30, 1<<30), |
| 971 | kernel, |
| 972 | gain, |
| 973 | bias, |
| 974 | kernelOffset, |
| 975 | SkMatrixConvolutionImageFilter::kRepeat_TileMode, |
| 976 | false)); |
| 977 | |
| 978 | REPORTER_ASSERT(reporter, NULL == conv.get()); |
| 979 | |
| 980 | // Check that a NULL kernel gives a NULL filter. |
| 981 | conv.reset(SkMatrixConvolutionImageFilter::Create( |
| 982 | SkISize::Make(1, 1), |
| 983 | NULL, |
| 984 | gain, |
| 985 | bias, |
| 986 | kernelOffset, |
| 987 | SkMatrixConvolutionImageFilter::kRepeat_TileMode, |
| 988 | false)); |
| 989 | |
| 990 | REPORTER_ASSERT(reporter, NULL == conv.get()); |
| 991 | |
| 992 | // Check that a kernel width < 1 gives a NULL filter. |
| 993 | conv.reset(SkMatrixConvolutionImageFilter::Create( |
| 994 | SkISize::Make(0, 1), |
| 995 | kernel, |
| 996 | gain, |
| 997 | bias, |
| 998 | kernelOffset, |
| 999 | SkMatrixConvolutionImageFilter::kRepeat_TileMode, |
| 1000 | false)); |
| 1001 | |
| 1002 | REPORTER_ASSERT(reporter, NULL == conv.get()); |
| 1003 | |
| 1004 | // Check that kernel height < 1 gives a NULL filter. |
| 1005 | conv.reset(SkMatrixConvolutionImageFilter::Create( |
| 1006 | SkISize::Make(1, -1), |
| 1007 | kernel, |
| 1008 | gain, |
| 1009 | bias, |
| 1010 | kernelOffset, |
| 1011 | SkMatrixConvolutionImageFilter::kRepeat_TileMode, |
| 1012 | false)); |
| 1013 | |
| 1014 | REPORTER_ASSERT(reporter, NULL == conv.get()); |
| 1015 | } |
| 1016 | |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 1017 | static void test_xfermode_cropped_input(SkBaseDevice* device, skiatest::Reporter* reporter) { |
| 1018 | SkCanvas canvas(device); |
| 1019 | canvas.clear(0); |
| 1020 | |
| 1021 | SkBitmap bitmap; |
| 1022 | bitmap.allocN32Pixels(1, 1); |
| 1023 | bitmap.eraseARGB(255, 255, 255, 255); |
| 1024 | |
| 1025 | SkAutoTUnref<SkColorFilter> green( |
| 1026 | SkColorFilter::CreateModeFilter(SK_ColorGREEN, SkXfermode::kSrcIn_Mode)); |
| 1027 | SkAutoTUnref<SkColorFilterImageFilter> greenFilter( |
| 1028 | SkColorFilterImageFilter::Create(green.get())); |
| 1029 | SkImageFilter::CropRect cropRect(SkRect::MakeEmpty()); |
| 1030 | SkAutoTUnref<SkColorFilterImageFilter> croppedOut( |
| 1031 | SkColorFilterImageFilter::Create(green.get(), NULL, &cropRect)); |
| 1032 | |
| 1033 | // Check that an xfermode image filter whose input has been cropped out still draws the other |
| 1034 | // input. Also check that drawing with both inputs cropped out doesn't cause a GPU warning. |
| 1035 | SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrcOver_Mode); |
| 1036 | SkAutoTUnref<SkImageFilter> xfermodeNoFg( |
| 1037 | SkXfermodeImageFilter::Create(mode, greenFilter, croppedOut)); |
| 1038 | SkAutoTUnref<SkImageFilter> xfermodeNoBg( |
| 1039 | SkXfermodeImageFilter::Create(mode, croppedOut, greenFilter)); |
| 1040 | SkAutoTUnref<SkImageFilter> xfermodeNoFgNoBg( |
| 1041 | SkXfermodeImageFilter::Create(mode, croppedOut, croppedOut)); |
| 1042 | |
| 1043 | SkPaint paint; |
| 1044 | paint.setImageFilter(xfermodeNoFg); |
| 1045 | canvas.drawSprite(bitmap, 0, 0, &paint); |
| 1046 | |
| 1047 | uint32_t pixel; |
kkinnunen | a9d9a39 | 2015-03-06 07:16:00 -0800 | [diff] [blame] | 1048 | SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType); |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 1049 | canvas.readPixels(info, &pixel, 4, 0, 0); |
| 1050 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 1051 | |
| 1052 | paint.setImageFilter(xfermodeNoBg); |
| 1053 | canvas.drawSprite(bitmap, 0, 0, &paint); |
| 1054 | canvas.readPixels(info, &pixel, 4, 0, 0); |
| 1055 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 1056 | |
| 1057 | paint.setImageFilter(xfermodeNoFgNoBg); |
| 1058 | canvas.drawSprite(bitmap, 0, 0, &paint); |
| 1059 | canvas.readPixels(info, &pixel, 4, 0, 0); |
| 1060 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 1061 | } |
| 1062 | |
senorblanco@chromium.org | d5424a4 | 2014-04-02 19:20:05 +0000 | [diff] [blame] | 1063 | DEF_TEST(ImageFilterNestedSaveLayer, reporter) { |
| 1064 | SkBitmap temp; |
| 1065 | temp.allocN32Pixels(50, 50); |
| 1066 | SkBitmapDevice device(temp); |
| 1067 | SkCanvas canvas(&device); |
| 1068 | canvas.clear(0x0); |
| 1069 | |
| 1070 | SkBitmap bitmap; |
| 1071 | bitmap.allocN32Pixels(10, 10); |
| 1072 | bitmap.eraseColor(SK_ColorGREEN); |
| 1073 | |
| 1074 | SkMatrix matrix; |
| 1075 | matrix.setScale(SkIntToScalar(2), SkIntToScalar(2)); |
| 1076 | matrix.postTranslate(SkIntToScalar(-20), SkIntToScalar(-20)); |
| 1077 | SkAutoTUnref<SkImageFilter> matrixFilter( |
senorblanco | 8c874ee | 2015-03-20 06:38:17 -0700 | [diff] [blame] | 1078 | SkImageFilter::CreateMatrixFilter(matrix, kLow_SkFilterQuality)); |
senorblanco@chromium.org | d5424a4 | 2014-04-02 19:20:05 +0000 | [diff] [blame] | 1079 | |
| 1080 | // Test that saveLayer() with a filter nested inside another saveLayer() applies the |
| 1081 | // correct offset to the filter matrix. |
| 1082 | SkRect bounds1 = SkRect::MakeXYWH(10, 10, 30, 30); |
| 1083 | canvas.saveLayer(&bounds1, NULL); |
| 1084 | SkPaint filterPaint; |
| 1085 | filterPaint.setImageFilter(matrixFilter); |
| 1086 | SkRect bounds2 = SkRect::MakeXYWH(20, 20, 10, 10); |
| 1087 | canvas.saveLayer(&bounds2, &filterPaint); |
| 1088 | SkPaint greenPaint; |
| 1089 | greenPaint.setColor(SK_ColorGREEN); |
| 1090 | canvas.drawRect(bounds2, greenPaint); |
| 1091 | canvas.restore(); |
| 1092 | canvas.restore(); |
| 1093 | SkPaint strokePaint; |
| 1094 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 1095 | strokePaint.setColor(SK_ColorRED); |
| 1096 | |
kkinnunen | a9d9a39 | 2015-03-06 07:16:00 -0800 | [diff] [blame] | 1097 | SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType); |
senorblanco@chromium.org | d5424a4 | 2014-04-02 19:20:05 +0000 | [diff] [blame] | 1098 | uint32_t pixel; |
| 1099 | canvas.readPixels(info, &pixel, 4, 25, 25); |
| 1100 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 1101 | |
| 1102 | // Test that drawSprite() with a filter nested inside a saveLayer() applies the |
| 1103 | // correct offset to the filter matrix. |
| 1104 | canvas.clear(0x0); |
| 1105 | canvas.readPixels(info, &pixel, 4, 25, 25); |
| 1106 | canvas.saveLayer(&bounds1, NULL); |
| 1107 | canvas.drawSprite(bitmap, 20, 20, &filterPaint); |
| 1108 | canvas.restore(); |
| 1109 | |
| 1110 | canvas.readPixels(info, &pixel, 4, 25, 25); |
| 1111 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 1112 | } |
| 1113 | |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 1114 | DEF_TEST(XfermodeImageFilterCroppedInput, reporter) { |
| 1115 | SkBitmap temp; |
| 1116 | temp.allocN32Pixels(100, 100); |
| 1117 | SkBitmapDevice device(temp); |
| 1118 | test_xfermode_cropped_input(&device, reporter); |
| 1119 | } |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 1120 | |
ajuma | 5788faa | 2015-02-13 09:05:47 -0800 | [diff] [blame] | 1121 | DEF_TEST(ComposedImageFilterOffset, reporter) { |
| 1122 | SkBitmap bitmap; |
| 1123 | bitmap.allocN32Pixels(100, 100); |
| 1124 | bitmap.eraseARGB(0, 0, 0, 0); |
| 1125 | SkBitmapDevice device(bitmap); |
| 1126 | SkDeviceImageFilterProxy proxy(&device, SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType)); |
| 1127 | |
| 1128 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(1, 0, 20, 20)); |
| 1129 | SkAutoTUnref<SkImageFilter> offsetFilter(SkOffsetImageFilter::Create(0, 0, NULL, &cropRect)); |
ajuma | 8e8c940 | 2015-02-13 10:15:46 -0800 | [diff] [blame] | 1130 | SkAutoTUnref<SkImageFilter> blurFilter(makeBlur()); |
| 1131 | SkAutoTUnref<SkImageFilter> composedFilter(SkComposeImageFilter::Create(blurFilter, offsetFilter.get())); |
ajuma | 5788faa | 2015-02-13 09:05:47 -0800 | [diff] [blame] | 1132 | SkBitmap result; |
| 1133 | SkIPoint offset; |
| 1134 | SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeLargest(), NULL); |
| 1135 | REPORTER_ASSERT(reporter, composedFilter->filterImage(&proxy, bitmap, ctx, &result, &offset)); |
| 1136 | REPORTER_ASSERT(reporter, offset.fX == 1 && offset.fY == 0); |
| 1137 | } |
| 1138 | |
senorblanco@chromium.org | 58d1466 | 2014-02-03 22:36:39 +0000 | [diff] [blame] | 1139 | #if SK_SUPPORT_GPU |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 1140 | const SkSurfaceProps gProps = SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType); |
| 1141 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 1142 | DEF_GPUTEST(ImageFilterCropRectGPU, reporter, factory) { |
| 1143 | GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(0)); |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 1144 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(context, |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 1145 | SkSurface::kNo_Budgeted, |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 1146 | SkImageInfo::MakeN32Premul(100, 100), |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 1147 | 0, |
| 1148 | &gProps)); |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 1149 | test_crop_rects(device, reporter); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 1150 | } |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 1151 | |
| 1152 | DEF_GPUTEST(HugeBlurImageFilterGPU, reporter, factory) { |
| 1153 | GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(0)); |
| 1154 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(context, |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 1155 | SkSurface::kNo_Budgeted, |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 1156 | SkImageInfo::MakeN32Premul(100, 100), |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 1157 | 0, |
| 1158 | &gProps)); |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 1159 | test_huge_blur(device, reporter); |
| 1160 | } |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 1161 | |
| 1162 | DEF_GPUTEST(XfermodeImageFilterCroppedInputGPU, reporter, factory) { |
| 1163 | GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(0)); |
| 1164 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(context, |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 1165 | SkSurface::kNo_Budgeted, |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 1166 | SkImageInfo::MakeN32Premul(1, 1), |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 1167 | 0, |
| 1168 | &gProps)); |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 1169 | test_xfermode_cropped_input(device, reporter); |
| 1170 | } |
senorblanco | 32673b9 | 2014-09-09 09:15:04 -0700 | [diff] [blame] | 1171 | |
| 1172 | DEF_GPUTEST(TestNegativeBlurSigmaGPU, reporter, factory) { |
| 1173 | GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(0)); |
| 1174 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(context, |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 1175 | SkSurface::kNo_Budgeted, |
senorblanco | 32673b9 | 2014-09-09 09:15:04 -0700 | [diff] [blame] | 1176 | SkImageInfo::MakeN32Premul(1, 1), |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 1177 | 0, |
| 1178 | &gProps)); |
senorblanco | 32673b9 | 2014-09-09 09:15:04 -0700 | [diff] [blame] | 1179 | test_negative_blur_sigma(device, reporter); |
| 1180 | } |
senorblanco@chromium.org | 58d1466 | 2014-02-03 22:36:39 +0000 | [diff] [blame] | 1181 | #endif |