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" |
| 15 | #include "SkDeviceImageFilterProxy.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 16 | #include "SkDisplacementMapEffect.h" |
| 17 | #include "SkDropShadowImageFilter.h" |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 18 | #include "SkFlattenableSerialization.h" |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 19 | #include "SkGradientShader.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" |
senorblanco@chromium.org | 910702b | 2014-05-30 20:36:15 +0000 | [diff] [blame] | 27 | #include "SkPictureImageFilter.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 28 | #include "SkPictureRecorder.h" |
halcanary | 97d2c0a | 2014-08-19 06:27:53 -0700 | [diff] [blame] | 29 | #include "SkReadBuffer.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 30 | #include "SkRect.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 31 | #include "SkTileImageFilter.h" |
| 32 | #include "SkXfermodeImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 33 | #include "Test.h" |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 34 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 35 | #if SK_SUPPORT_GPU |
| 36 | #include "GrContextFactory.h" |
| 37 | #include "SkGpuDevice.h" |
| 38 | #endif |
| 39 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 40 | static const int kBitmapSize = 4; |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 41 | |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 42 | namespace { |
| 43 | |
| 44 | class MatrixTestImageFilter : public SkImageFilter { |
| 45 | public: |
| 46 | MatrixTestImageFilter(skiatest::Reporter* reporter, const SkMatrix& expectedMatrix) |
senorblanco | 9ea3d57 | 2014-07-08 09:16:22 -0700 | [diff] [blame] | 47 | : SkImageFilter(0, NULL), fReporter(reporter), fExpectedMatrix(expectedMatrix) { |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 48 | } |
| 49 | |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 50 | virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context& ctx, |
senorblanco@chromium.org | 0937335 | 2014-02-05 23:04:28 +0000 | [diff] [blame] | 51 | SkBitmap* result, SkIPoint* offset) const SK_OVERRIDE { |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 52 | REPORTER_ASSERT(fReporter, ctx.ctm() == fExpectedMatrix); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 53 | return true; |
| 54 | } |
| 55 | |
| 56 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(MatrixTestImageFilter) |
| 57 | |
| 58 | protected: |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame^] | 59 | #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
senorblanco | 9ea3d57 | 2014-07-08 09:16:22 -0700 | [diff] [blame] | 60 | explicit MatrixTestImageFilter(SkReadBuffer& buffer) : SkImageFilter(0, NULL) { |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 61 | fReporter = static_cast<skiatest::Reporter*>(buffer.readFunctionPtr()); |
| 62 | buffer.readMatrix(&fExpectedMatrix); |
| 63 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame^] | 64 | #endif |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 65 | |
| 66 | virtual void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame^] | 67 | this->INHERITED::flatten(buffer); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 68 | buffer.writeFunctionPtr(fReporter); |
| 69 | buffer.writeMatrix(fExpectedMatrix); |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | skiatest::Reporter* fReporter; |
| 74 | SkMatrix fExpectedMatrix; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame^] | 75 | |
| 76 | typedef SkImageFilter INHERITED; |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | } |
| 80 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame^] | 81 | SkFlattenable* MatrixTestImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 82 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 83 | skiatest::Reporter* reporter = (skiatest::Reporter*)buffer.readFunctionPtr(); |
| 84 | SkMatrix matrix; |
| 85 | buffer.readMatrix(&matrix); |
| 86 | return SkNEW_ARGS(MatrixTestImageFilter, (reporter, matrix)); |
| 87 | } |
| 88 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 89 | static void make_small_bitmap(SkBitmap& bitmap) { |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 90 | bitmap.allocN32Pixels(kBitmapSize, kBitmapSize); |
| 91 | SkCanvas canvas(bitmap); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 92 | canvas.clear(0x00000000); |
| 93 | SkPaint darkPaint; |
| 94 | darkPaint.setColor(0xFF804020); |
| 95 | SkPaint lightPaint; |
| 96 | lightPaint.setColor(0xFF244484); |
| 97 | const int i = kBitmapSize / 4; |
| 98 | for (int y = 0; y < kBitmapSize; y += i) { |
| 99 | for (int x = 0; x < kBitmapSize; x += i) { |
| 100 | canvas.save(); |
| 101 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 102 | canvas.drawRect(SkRect::MakeXYWH(0, 0, |
| 103 | SkIntToScalar(i), |
| 104 | SkIntToScalar(i)), darkPaint); |
| 105 | canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i), |
| 106 | 0, |
| 107 | SkIntToScalar(i), |
| 108 | SkIntToScalar(i)), lightPaint); |
| 109 | canvas.drawRect(SkRect::MakeXYWH(0, |
| 110 | SkIntToScalar(i), |
| 111 | SkIntToScalar(i), |
| 112 | SkIntToScalar(i)), lightPaint); |
| 113 | canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i), |
| 114 | SkIntToScalar(i), |
| 115 | SkIntToScalar(i), |
| 116 | SkIntToScalar(i)), darkPaint); |
| 117 | canvas.restore(); |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 120 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 121 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 122 | static SkImageFilter* make_scale(float amount, SkImageFilter* input = NULL) { |
| 123 | SkScalar s = amount; |
| 124 | SkScalar matrix[20] = { s, 0, 0, 0, 0, |
| 125 | 0, s, 0, 0, 0, |
| 126 | 0, 0, s, 0, 0, |
| 127 | 0, 0, 0, s, 0 }; |
commit-bot@chromium.org | 727a352 | 2014-02-21 18:46:30 +0000 | [diff] [blame] | 128 | SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix)); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 129 | return SkColorFilterImageFilter::Create(filter, input); |
| 130 | } |
| 131 | |
| 132 | static SkImageFilter* make_grayscale(SkImageFilter* input = NULL, const SkImageFilter::CropRect* cropRect = NULL) { |
| 133 | SkScalar matrix[20]; |
| 134 | memset(matrix, 0, 20 * sizeof(SkScalar)); |
| 135 | matrix[0] = matrix[5] = matrix[10] = 0.2126f; |
| 136 | matrix[1] = matrix[6] = matrix[11] = 0.7152f; |
| 137 | matrix[2] = matrix[7] = matrix[12] = 0.0722f; |
| 138 | matrix[18] = 1.0f; |
commit-bot@chromium.org | 727a352 | 2014-02-21 18:46:30 +0000 | [diff] [blame] | 139 | SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix)); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 140 | return SkColorFilterImageFilter::Create(filter, input, cropRect); |
| 141 | } |
| 142 | |
| 143 | DEF_TEST(ImageFilter, reporter) { |
| 144 | { |
| 145 | // Check that two non-clipping color matrices concatenate into a single filter. |
| 146 | SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f)); |
| 147 | SkAutoTUnref<SkImageFilter> quarterBrightness(make_scale(0.5f, halfBrightness)); |
| 148 | REPORTER_ASSERT(reporter, NULL == quarterBrightness->getInput(0)); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 149 | } |
| 150 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 151 | { |
| 152 | // Check that a clipping color matrix followed by a grayscale does not concatenate into a single filter. |
| 153 | SkAutoTUnref<SkImageFilter> doubleBrightness(make_scale(2.0f)); |
| 154 | SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f, doubleBrightness)); |
| 155 | REPORTER_ASSERT(reporter, NULL != halfBrightness->getInput(0)); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 156 | } |
| 157 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 158 | { |
| 159 | // Check that a color filter image filter without a crop rect can be |
| 160 | // expressed as a color filter. |
| 161 | SkAutoTUnref<SkImageFilter> gray(make_grayscale()); |
| 162 | REPORTER_ASSERT(reporter, true == gray->asColorFilter(NULL)); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 163 | } |
| 164 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 165 | { |
| 166 | // Check that a color filter image filter with a crop rect cannot |
| 167 | // be expressed as a color filter. |
| 168 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(0, 0, 100, 100)); |
| 169 | SkAutoTUnref<SkImageFilter> grayWithCrop(make_grayscale(NULL, &cropRect)); |
| 170 | REPORTER_ASSERT(reporter, false == grayWithCrop->asColorFilter(NULL)); |
| 171 | } |
| 172 | |
| 173 | { |
senorblanco | 3df0501 | 2014-07-03 11:13:09 -0700 | [diff] [blame] | 174 | // Check that two non-commutative matrices are concatenated in |
| 175 | // the correct order. |
| 176 | SkScalar blueToRedMatrix[20] = { 0 }; |
| 177 | blueToRedMatrix[2] = blueToRedMatrix[18] = SK_Scalar1; |
| 178 | SkScalar redToGreenMatrix[20] = { 0 }; |
| 179 | redToGreenMatrix[5] = redToGreenMatrix[18] = SK_Scalar1; |
| 180 | SkAutoTUnref<SkColorFilter> blueToRed(SkColorMatrixFilter::Create(blueToRedMatrix)); |
| 181 | SkAutoTUnref<SkImageFilter> filter1(SkColorFilterImageFilter::Create(blueToRed.get())); |
| 182 | SkAutoTUnref<SkColorFilter> redToGreen(SkColorMatrixFilter::Create(redToGreenMatrix)); |
| 183 | SkAutoTUnref<SkImageFilter> filter2(SkColorFilterImageFilter::Create(redToGreen.get(), filter1.get())); |
| 184 | |
| 185 | SkBitmap result; |
| 186 | result.allocN32Pixels(kBitmapSize, kBitmapSize); |
| 187 | |
| 188 | SkPaint paint; |
| 189 | paint.setColor(SK_ColorBLUE); |
| 190 | paint.setImageFilter(filter2.get()); |
| 191 | SkCanvas canvas(result); |
| 192 | canvas.clear(0x0); |
| 193 | SkRect rect = SkRect::Make(SkIRect::MakeWH(kBitmapSize, kBitmapSize)); |
| 194 | canvas.drawRect(rect, paint); |
| 195 | uint32_t pixel = *result.getAddr32(0, 0); |
| 196 | // The result here should be green, since we have effectively shifted blue to green. |
| 197 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 198 | } |
| 199 | |
| 200 | { |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 201 | // Tests pass by not asserting |
| 202 | SkBitmap bitmap, result; |
| 203 | make_small_bitmap(bitmap); |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 204 | result.allocN32Pixels(kBitmapSize, kBitmapSize); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 205 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 206 | { |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 207 | // This tests for : |
| 208 | // 1 ) location at (0,0,1) |
| 209 | SkPoint3 location(0, 0, SK_Scalar1); |
| 210 | // 2 ) location and target at same value |
| 211 | SkPoint3 target(location.fX, location.fY, location.fZ); |
| 212 | // 3 ) large negative specular exponent value |
| 213 | SkScalar specularExponent = -1000; |
| 214 | |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 215 | SkAutoTUnref<SkImageFilter> bmSrc(SkBitmapSource::Create(bitmap)); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 216 | SkPaint paint; |
| 217 | paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular( |
| 218 | location, target, specularExponent, 180, |
| 219 | 0xFFFFFFFF, SK_Scalar1, SK_Scalar1, SK_Scalar1, |
| 220 | bmSrc))->unref(); |
| 221 | SkCanvas canvas(result); |
| 222 | SkRect r = SkRect::MakeWH(SkIntToScalar(kBitmapSize), |
| 223 | SkIntToScalar(kBitmapSize)); |
| 224 | canvas.drawRect(r, paint); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 225 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 226 | } |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 227 | } |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 228 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 229 | static void test_crop_rects(SkBaseDevice* device, skiatest::Reporter* reporter) { |
| 230 | // Check that all filters offset to their absolute crop rect, |
| 231 | // unaffected by the input crop rect. |
| 232 | // Tests pass by not asserting. |
| 233 | SkBitmap bitmap; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 234 | bitmap.allocN32Pixels(100, 100); |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 235 | bitmap.eraseARGB(0, 0, 0, 0); |
| 236 | SkDeviceImageFilterProxy proxy(device); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 237 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 238 | SkImageFilter::CropRect inputCropRect(SkRect::MakeXYWH(8, 13, 80, 80)); |
| 239 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 30, 60, 60)); |
| 240 | SkAutoTUnref<SkImageFilter> input(make_grayscale(NULL, &inputCropRect)); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 241 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 242 | SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_ColorRED, SkXfermode::kSrcIn_Mode)); |
| 243 | SkPoint3 location(0, 0, SK_Scalar1); |
| 244 | SkPoint3 target(SK_Scalar1, SK_Scalar1, SK_Scalar1); |
| 245 | SkScalar kernel[9] = { |
| 246 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 247 | SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1), |
| 248 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 249 | }; |
| 250 | SkISize kernelSize = SkISize::Make(3, 3); |
| 251 | SkScalar gain = SK_Scalar1, bias = 0; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 252 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 253 | SkImageFilter* filters[] = { |
| 254 | SkColorFilterImageFilter::Create(cf.get(), input.get(), &cropRect), |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 255 | SkDisplacementMapEffect::Create(SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 256 | SkDisplacementMapEffect::kB_ChannelSelectorType, |
| 257 | 40.0f, input.get(), input.get(), &cropRect), |
| 258 | SkBlurImageFilter::Create(SK_Scalar1, SK_Scalar1, input.get(), &cropRect), |
| 259 | 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] | 260 | SkLightingImageFilter::CreatePointLitDiffuse(location, SK_ColorGREEN, 0, 0, input.get(), &cropRect), |
| 261 | 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] | 262 | SkMatrixConvolutionImageFilter::Create(kernelSize, kernel, gain, bias, SkIPoint::Make(1, 1), SkMatrixConvolutionImageFilter::kRepeat_TileMode, false, input.get(), &cropRect), |
| 263 | SkMergeImageFilter::Create(input.get(), input.get(), SkXfermode::kSrcOver_Mode, &cropRect), |
| 264 | SkOffsetImageFilter::Create(SK_Scalar1, SK_Scalar1, input.get(), &cropRect), |
| 265 | SkOffsetImageFilter::Create(SK_Scalar1, SK_Scalar1, input.get(), &cropRect), |
| 266 | SkDilateImageFilter::Create(3, 2, input.get(), &cropRect), |
| 267 | SkErodeImageFilter::Create(2, 3, input.get(), &cropRect), |
| 268 | SkTileImageFilter::Create(inputCropRect.rect(), cropRect.rect(), input.get()), |
| 269 | 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] | 270 | }; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 271 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 272 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 273 | SkImageFilter* filter = filters[i]; |
| 274 | SkBitmap result; |
| 275 | SkIPoint offset; |
| 276 | SkString str; |
senorblanco@chromium.org | f4e1a76 | 2014-02-04 00:28:46 +0000 | [diff] [blame] | 277 | str.printf("filter %d", static_cast<int>(i)); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 278 | SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeLargest(), NULL); |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 279 | REPORTER_ASSERT_MESSAGE(reporter, filter->filterImage(&proxy, bitmap, ctx, |
| 280 | &result, &offset), str.c_str()); |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 281 | 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] | 282 | } |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 283 | |
| 284 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 285 | SkSafeUnref(filters[i]); |
| 286 | } |
| 287 | } |
| 288 | |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 289 | static SkBitmap make_gradient_circle(int width, int height) { |
| 290 | SkBitmap bitmap; |
| 291 | SkScalar x = SkIntToScalar(width / 2); |
| 292 | SkScalar y = SkIntToScalar(height / 2); |
| 293 | SkScalar radius = SkMinScalar(x, y) * 0.8f; |
| 294 | bitmap.allocN32Pixels(width, height); |
| 295 | SkCanvas canvas(bitmap); |
| 296 | canvas.clear(0x00000000); |
| 297 | SkColor colors[2]; |
| 298 | colors[0] = SK_ColorWHITE; |
| 299 | colors[1] = SK_ColorBLACK; |
| 300 | SkAutoTUnref<SkShader> shader( |
| 301 | SkGradientShader::CreateRadial(SkPoint::Make(x, y), radius, colors, NULL, 2, |
| 302 | SkShader::kClamp_TileMode) |
| 303 | ); |
| 304 | SkPaint paint; |
| 305 | paint.setShader(shader); |
| 306 | canvas.drawCircle(x, y, radius, paint); |
| 307 | return bitmap; |
| 308 | } |
| 309 | |
| 310 | DEF_TEST(ImageFilterDrawTiled, reporter) { |
| 311 | // Check that all filters when drawn tiled (with subsequent clip rects) exactly |
| 312 | // match the same filters drawn with a single full-canvas bitmap draw. |
| 313 | // Tests pass by not asserting. |
| 314 | |
| 315 | SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_ColorRED, SkXfermode::kSrcIn_Mode)); |
| 316 | SkPoint3 location(0, 0, SK_Scalar1); |
| 317 | SkPoint3 target(SK_Scalar1, SK_Scalar1, SK_Scalar1); |
| 318 | SkScalar kernel[9] = { |
| 319 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 320 | SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1), |
| 321 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 322 | }; |
| 323 | SkISize kernelSize = SkISize::Make(3, 3); |
| 324 | SkScalar gain = SK_Scalar1, bias = 0; |
senorblanco@chromium.org | 29ac34e | 2014-05-28 19:29:25 +0000 | [diff] [blame] | 325 | SkScalar five = SkIntToScalar(5); |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 326 | |
| 327 | SkAutoTUnref<SkImageFilter> gradient_source(SkBitmapSource::Create(make_gradient_circle(64, 64))); |
senorblanco@chromium.org | 29ac34e | 2014-05-28 19:29:25 +0000 | [diff] [blame] | 328 | SkAutoTUnref<SkImageFilter> blur(SkBlurImageFilter::Create(five, five)); |
senorblanco@chromium.org | ba31f1d | 2014-05-07 20:56:08 +0000 | [diff] [blame] | 329 | SkMatrix matrix; |
senorblanco@chromium.org | 29ac34e | 2014-05-28 19:29:25 +0000 | [diff] [blame] | 330 | |
senorblanco@chromium.org | ba31f1d | 2014-05-07 20:56:08 +0000 | [diff] [blame] | 331 | matrix.setTranslate(SK_Scalar1, SK_Scalar1); |
| 332 | matrix.postRotate(SkIntToScalar(45), SK_Scalar1, SK_Scalar1); |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 333 | |
senorblanco@chromium.org | 910702b | 2014-05-30 20:36:15 +0000 | [diff] [blame] | 334 | SkRTreeFactory factory; |
| 335 | SkPictureRecorder recorder; |
| 336 | SkCanvas* recordingCanvas = recorder.beginRecording(64, 64, &factory, 0); |
| 337 | |
| 338 | SkPaint greenPaint; |
| 339 | greenPaint.setColor(SK_ColorGREEN); |
| 340 | recordingCanvas->drawRect(SkRect::Make(SkIRect::MakeXYWH(10, 10, 30, 20)), greenPaint); |
| 341 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 342 | SkAutoTUnref<SkImageFilter> pictureFilter(SkPictureImageFilter::Create(picture.get())); |
| 343 | |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 344 | struct { |
| 345 | const char* fName; |
| 346 | SkImageFilter* fFilter; |
| 347 | } filters[] = { |
| 348 | { "color filter", SkColorFilterImageFilter::Create(cf.get()) }, |
| 349 | { "displacement map", SkDisplacementMapEffect::Create( |
| 350 | SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 351 | SkDisplacementMapEffect::kB_ChannelSelectorType, |
senorblanco@chromium.org | d4db657 | 2014-05-07 20:00:04 +0000 | [diff] [blame] | 352 | 20.0f, gradient_source.get()) }, |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 353 | { "blur", SkBlurImageFilter::Create(SK_Scalar1, SK_Scalar1) }, |
| 354 | { "drop shadow", SkDropShadowImageFilter::Create( |
| 355 | SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_ColorGREEN) }, |
| 356 | { "diffuse lighting", SkLightingImageFilter::CreatePointLitDiffuse( |
| 357 | location, SK_ColorGREEN, 0, 0) }, |
| 358 | { "specular lighting", |
| 359 | SkLightingImageFilter::CreatePointLitSpecular(location, SK_ColorGREEN, 0, 0, 0) }, |
| 360 | { "matrix convolution", |
| 361 | SkMatrixConvolutionImageFilter::Create( |
| 362 | kernelSize, kernel, gain, bias, SkIPoint::Make(1, 1), |
| 363 | SkMatrixConvolutionImageFilter::kRepeat_TileMode, false) }, |
| 364 | { "merge", SkMergeImageFilter::Create(NULL, NULL, SkXfermode::kSrcOver_Mode) }, |
| 365 | { "offset", SkOffsetImageFilter::Create(SK_Scalar1, SK_Scalar1) }, |
| 366 | { "dilate", SkDilateImageFilter::Create(3, 2) }, |
| 367 | { "erode", SkErodeImageFilter::Create(2, 3) }, |
| 368 | { "tile", SkTileImageFilter::Create(SkRect::MakeXYWH(0, 0, 50, 50), |
| 369 | SkRect::MakeXYWH(0, 0, 100, 100), NULL) }, |
senorblanco@chromium.org | ba31f1d | 2014-05-07 20:56:08 +0000 | [diff] [blame] | 370 | { "matrix", SkMatrixImageFilter::Create(matrix, SkPaint::kLow_FilterLevel) }, |
senorblanco@chromium.org | 29ac34e | 2014-05-28 19:29:25 +0000 | [diff] [blame] | 371 | { "blur and offset", SkOffsetImageFilter::Create(five, five, blur.get()) }, |
senorblanco@chromium.org | 910702b | 2014-05-30 20:36:15 +0000 | [diff] [blame] | 372 | { "picture and blur", SkBlurImageFilter::Create(five, five, pictureFilter.get()) }, |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 373 | }; |
| 374 | |
| 375 | SkBitmap untiledResult, tiledResult; |
| 376 | int width = 64, height = 64; |
| 377 | untiledResult.allocN32Pixels(width, height); |
| 378 | tiledResult.allocN32Pixels(width, height); |
| 379 | SkCanvas tiledCanvas(tiledResult); |
| 380 | SkCanvas untiledCanvas(untiledResult); |
senorblanco@chromium.org | d4db657 | 2014-05-07 20:00:04 +0000 | [diff] [blame] | 381 | int tileSize = 8; |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 382 | |
senorblanco@chromium.org | d4db657 | 2014-05-07 20:00:04 +0000 | [diff] [blame] | 383 | for (int scale = 1; scale <= 2; ++scale) { |
| 384 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 385 | tiledCanvas.clear(0); |
| 386 | untiledCanvas.clear(0); |
| 387 | SkPaint paint; |
| 388 | paint.setImageFilter(filters[i].fFilter); |
| 389 | paint.setTextSize(SkIntToScalar(height)); |
| 390 | paint.setColor(SK_ColorWHITE); |
| 391 | SkString str; |
| 392 | const char* text = "ABC"; |
| 393 | SkScalar ypos = SkIntToScalar(height); |
| 394 | untiledCanvas.save(); |
| 395 | untiledCanvas.scale(SkIntToScalar(scale), SkIntToScalar(scale)); |
| 396 | untiledCanvas.drawText(text, strlen(text), 0, ypos, paint); |
| 397 | untiledCanvas.restore(); |
| 398 | for (int y = 0; y < height; y += tileSize) { |
| 399 | for (int x = 0; x < width; x += tileSize) { |
| 400 | tiledCanvas.save(); |
| 401 | tiledCanvas.clipRect(SkRect::Make(SkIRect::MakeXYWH(x, y, tileSize, tileSize))); |
| 402 | tiledCanvas.scale(SkIntToScalar(scale), SkIntToScalar(scale)); |
| 403 | tiledCanvas.drawText(text, strlen(text), 0, ypos, paint); |
| 404 | tiledCanvas.restore(); |
| 405 | } |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 406 | } |
senorblanco@chromium.org | d4db657 | 2014-05-07 20:00:04 +0000 | [diff] [blame] | 407 | untiledCanvas.flush(); |
| 408 | tiledCanvas.flush(); |
| 409 | for (int y = 0; y < height; y++) { |
| 410 | int diffs = memcmp(untiledResult.getAddr32(0, y), tiledResult.getAddr32(0, y), untiledResult.rowBytes()); |
| 411 | REPORTER_ASSERT_MESSAGE(reporter, !diffs, filters[i].fName); |
| 412 | if (diffs) { |
| 413 | break; |
| 414 | } |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 420 | SkSafeUnref(filters[i].fFilter); |
| 421 | } |
| 422 | } |
| 423 | |
senorblanco | 837f532 | 2014-07-14 10:19:54 -0700 | [diff] [blame] | 424 | static void drawBlurredRect(SkCanvas* canvas) { |
| 425 | SkAutoTUnref<SkImageFilter> filter(SkBlurImageFilter::Create(SkIntToScalar(8), 0)); |
| 426 | SkPaint filterPaint; |
| 427 | filterPaint.setColor(SK_ColorWHITE); |
| 428 | filterPaint.setImageFilter(filter); |
| 429 | canvas->saveLayer(NULL, &filterPaint); |
| 430 | SkPaint whitePaint; |
| 431 | whitePaint.setColor(SK_ColorWHITE); |
| 432 | canvas->drawRect(SkRect::Make(SkIRect::MakeWH(4, 4)), whitePaint); |
| 433 | canvas->restore(); |
| 434 | } |
| 435 | |
| 436 | static void drawPictureClipped(SkCanvas* canvas, const SkRect& clipRect, const SkPicture* picture) { |
| 437 | canvas->save(); |
| 438 | canvas->clipRect(clipRect); |
| 439 | canvas->drawPicture(picture); |
| 440 | canvas->restore(); |
| 441 | } |
| 442 | |
| 443 | DEF_TEST(ImageFilterDrawTiledBlurRTree, reporter) { |
| 444 | // Check that the blur filter when recorded with RTree acceleration, |
| 445 | // and drawn tiled (with subsequent clip rects) exactly |
| 446 | // matches the same filter drawn with without RTree acceleration. |
| 447 | // This tests that the "bleed" from the blur into the otherwise-blank |
| 448 | // tiles is correctly rendered. |
| 449 | // Tests pass by not asserting. |
| 450 | |
| 451 | int width = 16, height = 8; |
| 452 | SkBitmap result1, result2; |
| 453 | result1.allocN32Pixels(width, height); |
| 454 | result2.allocN32Pixels(width, height); |
| 455 | SkCanvas canvas1(result1); |
| 456 | SkCanvas canvas2(result2); |
| 457 | int tileSize = 8; |
| 458 | |
| 459 | canvas1.clear(0); |
| 460 | canvas2.clear(0); |
| 461 | |
| 462 | SkRTreeFactory factory; |
| 463 | |
| 464 | SkPictureRecorder recorder1, recorder2; |
| 465 | // The only difference between these two pictures is that one has RTree aceleration. |
| 466 | SkCanvas* recordingCanvas1 = recorder1.beginRecording(width, height, NULL, 0); |
| 467 | SkCanvas* recordingCanvas2 = recorder2.beginRecording(width, height, &factory, 0); |
| 468 | drawBlurredRect(recordingCanvas1); |
| 469 | drawBlurredRect(recordingCanvas2); |
| 470 | SkAutoTUnref<SkPicture> picture1(recorder1.endRecording()); |
| 471 | SkAutoTUnref<SkPicture> picture2(recorder2.endRecording()); |
| 472 | for (int y = 0; y < height; y += tileSize) { |
| 473 | for (int x = 0; x < width; x += tileSize) { |
| 474 | SkRect tileRect = SkRect::Make(SkIRect::MakeXYWH(x, y, tileSize, tileSize)); |
| 475 | drawPictureClipped(&canvas1, tileRect, picture1); |
| 476 | drawPictureClipped(&canvas2, tileRect, picture2); |
| 477 | } |
| 478 | } |
| 479 | for (int y = 0; y < height; y++) { |
| 480 | int diffs = memcmp(result1.getAddr32(0, y), result2.getAddr32(0, y), result1.rowBytes()); |
| 481 | REPORTER_ASSERT(reporter, !diffs); |
| 482 | if (diffs) { |
| 483 | break; |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
senorblanco@chromium.org | 9195743 | 2014-05-01 14:03:41 +0000 | [diff] [blame] | 488 | DEF_TEST(ImageFilterMatrixConvolution, reporter) { |
| 489 | // Check that a 1x3 filter does not cause a spurious assert. |
| 490 | SkScalar kernel[3] = { |
| 491 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 492 | }; |
| 493 | SkISize kernelSize = SkISize::Make(1, 3); |
| 494 | SkScalar gain = SK_Scalar1, bias = 0; |
| 495 | SkIPoint kernelOffset = SkIPoint::Make(0, 0); |
| 496 | |
| 497 | SkAutoTUnref<SkImageFilter> filter( |
| 498 | SkMatrixConvolutionImageFilter::Create( |
| 499 | kernelSize, kernel, gain, bias, kernelOffset, |
| 500 | SkMatrixConvolutionImageFilter::kRepeat_TileMode, false)); |
| 501 | |
| 502 | SkBitmap result; |
| 503 | int width = 16, height = 16; |
| 504 | result.allocN32Pixels(width, height); |
| 505 | SkCanvas canvas(result); |
| 506 | canvas.clear(0); |
| 507 | |
| 508 | SkPaint paint; |
| 509 | paint.setImageFilter(filter); |
| 510 | SkRect rect = SkRect::Make(SkIRect::MakeWH(width, height)); |
| 511 | canvas.drawRect(rect, paint); |
| 512 | } |
| 513 | |
senorblanco@chromium.org | 8c7372b | 2014-05-02 19:13:11 +0000 | [diff] [blame] | 514 | DEF_TEST(ImageFilterMatrixConvolutionBorder, reporter) { |
| 515 | // Check that a filter with borders outside the target bounds |
| 516 | // does not crash. |
| 517 | SkScalar kernel[3] = { |
| 518 | 0, 0, 0, |
| 519 | }; |
| 520 | SkISize kernelSize = SkISize::Make(3, 1); |
| 521 | SkScalar gain = SK_Scalar1, bias = 0; |
| 522 | SkIPoint kernelOffset = SkIPoint::Make(2, 0); |
| 523 | |
| 524 | SkAutoTUnref<SkImageFilter> filter( |
| 525 | SkMatrixConvolutionImageFilter::Create( |
| 526 | kernelSize, kernel, gain, bias, kernelOffset, |
| 527 | SkMatrixConvolutionImageFilter::kClamp_TileMode, true)); |
| 528 | |
| 529 | SkBitmap result; |
| 530 | |
| 531 | int width = 10, height = 10; |
| 532 | result.allocN32Pixels(width, height); |
| 533 | SkCanvas canvas(result); |
| 534 | canvas.clear(0); |
| 535 | |
| 536 | SkPaint filterPaint; |
| 537 | filterPaint.setImageFilter(filter); |
| 538 | SkRect bounds = SkRect::MakeWH(1, 10); |
| 539 | SkRect rect = SkRect::Make(SkIRect::MakeWH(width, height)); |
| 540 | SkPaint rectPaint; |
| 541 | canvas.saveLayer(&bounds, &filterPaint); |
| 542 | canvas.drawRect(rect, rectPaint); |
| 543 | canvas.restore(); |
| 544 | } |
| 545 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 546 | DEF_TEST(ImageFilterCropRect, reporter) { |
| 547 | SkBitmap temp; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 548 | temp.allocN32Pixels(100, 100); |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 549 | SkBitmapDevice device(temp); |
| 550 | test_crop_rects(&device, reporter); |
| 551 | } |
| 552 | |
tfarina | 9ea53f9 | 2014-06-24 06:50:39 -0700 | [diff] [blame] | 553 | DEF_TEST(ImageFilterMatrix, reporter) { |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 554 | SkBitmap temp; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 555 | temp.allocN32Pixels(100, 100); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 556 | SkBitmapDevice device(temp); |
| 557 | SkCanvas canvas(&device); |
| 558 | canvas.scale(SkIntToScalar(2), SkIntToScalar(2)); |
| 559 | |
| 560 | SkMatrix expectedMatrix = canvas.getTotalMatrix(); |
| 561 | |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 562 | SkRTreeFactory factory; |
| 563 | SkPictureRecorder recorder; |
| 564 | SkCanvas* recordingCanvas = recorder.beginRecording(100, 100, &factory, 0); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 565 | |
| 566 | SkPaint paint; |
| 567 | SkAutoTUnref<MatrixTestImageFilter> imageFilter( |
| 568 | new MatrixTestImageFilter(reporter, expectedMatrix)); |
| 569 | paint.setImageFilter(imageFilter.get()); |
commit-bot@chromium.org | 091a594 | 2014-04-18 14:19:31 +0000 | [diff] [blame] | 570 | recordingCanvas->saveLayer(NULL, &paint); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 571 | SkPaint solidPaint; |
| 572 | solidPaint.setColor(0xFFFFFFFF); |
| 573 | recordingCanvas->save(); |
| 574 | recordingCanvas->scale(SkIntToScalar(10), SkIntToScalar(10)); |
| 575 | recordingCanvas->drawRect(SkRect::Make(SkIRect::MakeWH(100, 100)), solidPaint); |
| 576 | recordingCanvas->restore(); // scale |
| 577 | recordingCanvas->restore(); // saveLayer |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 578 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 579 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 580 | canvas.drawPicture(picture); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 581 | } |
| 582 | |
senorblanco | 3d822c2 | 2014-07-30 14:49:31 -0700 | [diff] [blame] | 583 | DEF_TEST(ImageFilterCrossProcessPictureImageFilter, reporter) { |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 584 | SkRTreeFactory factory; |
| 585 | SkPictureRecorder recorder; |
| 586 | SkCanvas* recordingCanvas = recorder.beginRecording(1, 1, &factory, 0); |
| 587 | |
| 588 | // Create an SkPicture which simply draws a green 1x1 rectangle. |
| 589 | SkPaint greenPaint; |
| 590 | greenPaint.setColor(SK_ColorGREEN); |
| 591 | recordingCanvas->drawRect(SkRect::Make(SkIRect::MakeWH(1, 1)), greenPaint); |
| 592 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 593 | |
| 594 | // Wrap that SkPicture in an SkPictureImageFilter. |
| 595 | SkAutoTUnref<SkImageFilter> imageFilter( |
| 596 | SkPictureImageFilter::Create(picture.get())); |
| 597 | |
| 598 | // Check that SkPictureImageFilter successfully serializes its contained |
| 599 | // SkPicture when not in cross-process mode. |
| 600 | SkPaint paint; |
| 601 | paint.setImageFilter(imageFilter.get()); |
| 602 | SkPictureRecorder outerRecorder; |
| 603 | SkCanvas* outerCanvas = outerRecorder.beginRecording(1, 1, &factory, 0); |
| 604 | SkPaint redPaintWithFilter; |
| 605 | redPaintWithFilter.setColor(SK_ColorRED); |
| 606 | redPaintWithFilter.setImageFilter(imageFilter.get()); |
| 607 | outerCanvas->drawRect(SkRect::Make(SkIRect::MakeWH(1, 1)), redPaintWithFilter); |
| 608 | SkAutoTUnref<SkPicture> outerPicture(outerRecorder.endRecording()); |
| 609 | |
| 610 | SkBitmap bitmap; |
| 611 | bitmap.allocN32Pixels(1, 1); |
| 612 | SkBitmapDevice device(bitmap); |
| 613 | SkCanvas canvas(&device); |
| 614 | |
| 615 | // The result here should be green, since the filter replaces the primitive's red interior. |
| 616 | canvas.clear(0x0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 617 | canvas.drawPicture(outerPicture); |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 618 | uint32_t pixel = *bitmap.getAddr32(0, 0); |
| 619 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 620 | |
| 621 | // Check that, for now, SkPictureImageFilter does not serialize or |
| 622 | // deserialize its contained picture when the filter is serialized |
| 623 | // cross-process. Do this by "laundering" it through SkValidatingReadBuffer. |
| 624 | SkAutoTUnref<SkData> data(SkValidatingSerializeFlattenable(imageFilter.get())); |
| 625 | SkAutoTUnref<SkFlattenable> flattenable(SkValidatingDeserializeFlattenable( |
| 626 | data->data(), data->size(), SkImageFilter::GetFlattenableType())); |
| 627 | SkImageFilter* unflattenedFilter = static_cast<SkImageFilter*>(flattenable.get()); |
| 628 | |
| 629 | redPaintWithFilter.setImageFilter(unflattenedFilter); |
| 630 | SkPictureRecorder crossProcessRecorder; |
| 631 | SkCanvas* crossProcessCanvas = crossProcessRecorder.beginRecording(1, 1, &factory, 0); |
| 632 | crossProcessCanvas->drawRect(SkRect::Make(SkIRect::MakeWH(1, 1)), redPaintWithFilter); |
| 633 | SkAutoTUnref<SkPicture> crossProcessPicture(crossProcessRecorder.endRecording()); |
| 634 | |
| 635 | canvas.clear(0x0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 636 | canvas.drawPicture(crossProcessPicture); |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 637 | pixel = *bitmap.getAddr32(0, 0); |
| 638 | // The result here should not be green, since the filter draws nothing. |
| 639 | REPORTER_ASSERT(reporter, pixel != SK_ColorGREEN); |
| 640 | } |
| 641 | |
senorblanco | 3d822c2 | 2014-07-30 14:49:31 -0700 | [diff] [blame] | 642 | DEF_TEST(ImageFilterClippedPictureImageFilter, reporter) { |
| 643 | SkRTreeFactory factory; |
| 644 | SkPictureRecorder recorder; |
| 645 | SkCanvas* recordingCanvas = recorder.beginRecording(1, 1, &factory, 0); |
| 646 | |
| 647 | // Create an SkPicture which simply draws a green 1x1 rectangle. |
| 648 | SkPaint greenPaint; |
| 649 | greenPaint.setColor(SK_ColorGREEN); |
| 650 | recordingCanvas->drawRect(SkRect::Make(SkIRect::MakeWH(1, 1)), greenPaint); |
| 651 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 652 | |
| 653 | SkAutoTUnref<SkImageFilter> imageFilter( |
| 654 | SkPictureImageFilter::Create(picture.get())); |
| 655 | |
| 656 | SkBitmap result; |
| 657 | SkIPoint offset; |
| 658 | SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeXYWH(1, 1, 1, 1), NULL); |
| 659 | SkBitmap bitmap; |
| 660 | bitmap.allocN32Pixels(2, 2); |
| 661 | SkBitmapDevice device(bitmap); |
| 662 | SkDeviceImageFilterProxy proxy(&device); |
| 663 | REPORTER_ASSERT(reporter, !imageFilter->filterImage(&proxy, bitmap, ctx, &result, &offset)); |
| 664 | } |
| 665 | |
tfarina | 9ea53f9 | 2014-06-24 06:50:39 -0700 | [diff] [blame] | 666 | DEF_TEST(ImageFilterEmptySaveLayer, reporter) { |
senorblanco@chromium.org | 68250c8 | 2014-05-06 22:52:55 +0000 | [diff] [blame] | 667 | // Even when there's an empty saveLayer()/restore(), ensure that an image |
| 668 | // filter or color filter which affects transparent black still draws. |
| 669 | |
| 670 | SkBitmap bitmap; |
| 671 | bitmap.allocN32Pixels(10, 10); |
| 672 | SkBitmapDevice device(bitmap); |
| 673 | SkCanvas canvas(&device); |
| 674 | |
| 675 | SkRTreeFactory factory; |
| 676 | SkPictureRecorder recorder; |
| 677 | |
| 678 | SkAutoTUnref<SkColorFilter> green( |
| 679 | SkColorFilter::CreateModeFilter(SK_ColorGREEN, SkXfermode::kSrc_Mode)); |
| 680 | SkAutoTUnref<SkColorFilterImageFilter> imageFilter( |
| 681 | SkColorFilterImageFilter::Create(green.get())); |
| 682 | SkPaint imageFilterPaint; |
| 683 | imageFilterPaint.setImageFilter(imageFilter.get()); |
| 684 | SkPaint colorFilterPaint; |
| 685 | colorFilterPaint.setColorFilter(green.get()); |
| 686 | |
| 687 | SkRect bounds = SkRect::MakeWH(10, 10); |
| 688 | |
| 689 | SkCanvas* recordingCanvas = recorder.beginRecording(10, 10, &factory, 0); |
| 690 | recordingCanvas->saveLayer(&bounds, &imageFilterPaint); |
| 691 | recordingCanvas->restore(); |
| 692 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 693 | |
| 694 | canvas.clear(0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 695 | canvas.drawPicture(picture); |
senorblanco@chromium.org | 68250c8 | 2014-05-06 22:52:55 +0000 | [diff] [blame] | 696 | uint32_t pixel = *bitmap.getAddr32(0, 0); |
| 697 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 698 | |
| 699 | recordingCanvas = recorder.beginRecording(10, 10, &factory, 0); |
| 700 | recordingCanvas->saveLayer(NULL, &imageFilterPaint); |
| 701 | recordingCanvas->restore(); |
| 702 | SkAutoTUnref<SkPicture> picture2(recorder.endRecording()); |
| 703 | |
| 704 | canvas.clear(0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 705 | canvas.drawPicture(picture2); |
senorblanco@chromium.org | 68250c8 | 2014-05-06 22:52:55 +0000 | [diff] [blame] | 706 | pixel = *bitmap.getAddr32(0, 0); |
| 707 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 708 | |
| 709 | recordingCanvas = recorder.beginRecording(10, 10, &factory, 0); |
| 710 | recordingCanvas->saveLayer(&bounds, &colorFilterPaint); |
| 711 | recordingCanvas->restore(); |
| 712 | SkAutoTUnref<SkPicture> picture3(recorder.endRecording()); |
| 713 | |
| 714 | canvas.clear(0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 715 | canvas.drawPicture(picture3); |
senorblanco@chromium.org | 68250c8 | 2014-05-06 22:52:55 +0000 | [diff] [blame] | 716 | pixel = *bitmap.getAddr32(0, 0); |
| 717 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 718 | } |
| 719 | |
senorblanco@chromium.org | 28ae55d | 2014-03-24 21:32:28 +0000 | [diff] [blame] | 720 | static void test_huge_blur(SkBaseDevice* device, skiatest::Reporter* reporter) { |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 721 | SkCanvas canvas(device); |
| 722 | |
| 723 | SkBitmap bitmap; |
| 724 | bitmap.allocN32Pixels(100, 100); |
| 725 | bitmap.eraseARGB(0, 0, 0, 0); |
| 726 | |
| 727 | // Check that a blur with an insane radius does not crash or assert. |
| 728 | SkAutoTUnref<SkImageFilter> blur(SkBlurImageFilter::Create(SkIntToScalar(1<<30), SkIntToScalar(1<<30))); |
| 729 | |
| 730 | SkPaint paint; |
| 731 | paint.setImageFilter(blur); |
| 732 | canvas.drawSprite(bitmap, 0, 0, &paint); |
| 733 | } |
| 734 | |
| 735 | DEF_TEST(HugeBlurImageFilter, reporter) { |
| 736 | SkBitmap temp; |
| 737 | temp.allocN32Pixels(100, 100); |
| 738 | SkBitmapDevice device(temp); |
| 739 | test_huge_blur(&device, reporter); |
| 740 | } |
| 741 | |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 742 | static void test_xfermode_cropped_input(SkBaseDevice* device, skiatest::Reporter* reporter) { |
| 743 | SkCanvas canvas(device); |
| 744 | canvas.clear(0); |
| 745 | |
| 746 | SkBitmap bitmap; |
| 747 | bitmap.allocN32Pixels(1, 1); |
| 748 | bitmap.eraseARGB(255, 255, 255, 255); |
| 749 | |
| 750 | SkAutoTUnref<SkColorFilter> green( |
| 751 | SkColorFilter::CreateModeFilter(SK_ColorGREEN, SkXfermode::kSrcIn_Mode)); |
| 752 | SkAutoTUnref<SkColorFilterImageFilter> greenFilter( |
| 753 | SkColorFilterImageFilter::Create(green.get())); |
| 754 | SkImageFilter::CropRect cropRect(SkRect::MakeEmpty()); |
| 755 | SkAutoTUnref<SkColorFilterImageFilter> croppedOut( |
| 756 | SkColorFilterImageFilter::Create(green.get(), NULL, &cropRect)); |
| 757 | |
| 758 | // Check that an xfermode image filter whose input has been cropped out still draws the other |
| 759 | // input. Also check that drawing with both inputs cropped out doesn't cause a GPU warning. |
| 760 | SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrcOver_Mode); |
| 761 | SkAutoTUnref<SkImageFilter> xfermodeNoFg( |
| 762 | SkXfermodeImageFilter::Create(mode, greenFilter, croppedOut)); |
| 763 | SkAutoTUnref<SkImageFilter> xfermodeNoBg( |
| 764 | SkXfermodeImageFilter::Create(mode, croppedOut, greenFilter)); |
| 765 | SkAutoTUnref<SkImageFilter> xfermodeNoFgNoBg( |
| 766 | SkXfermodeImageFilter::Create(mode, croppedOut, croppedOut)); |
| 767 | |
| 768 | SkPaint paint; |
| 769 | paint.setImageFilter(xfermodeNoFg); |
| 770 | canvas.drawSprite(bitmap, 0, 0, &paint); |
| 771 | |
| 772 | uint32_t pixel; |
| 773 | SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 774 | canvas.readPixels(info, &pixel, 4, 0, 0); |
| 775 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 776 | |
| 777 | paint.setImageFilter(xfermodeNoBg); |
| 778 | canvas.drawSprite(bitmap, 0, 0, &paint); |
| 779 | canvas.readPixels(info, &pixel, 4, 0, 0); |
| 780 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 781 | |
| 782 | paint.setImageFilter(xfermodeNoFgNoBg); |
| 783 | canvas.drawSprite(bitmap, 0, 0, &paint); |
| 784 | canvas.readPixels(info, &pixel, 4, 0, 0); |
| 785 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 786 | } |
| 787 | |
senorblanco@chromium.org | d5424a4 | 2014-04-02 19:20:05 +0000 | [diff] [blame] | 788 | DEF_TEST(ImageFilterNestedSaveLayer, reporter) { |
| 789 | SkBitmap temp; |
| 790 | temp.allocN32Pixels(50, 50); |
| 791 | SkBitmapDevice device(temp); |
| 792 | SkCanvas canvas(&device); |
| 793 | canvas.clear(0x0); |
| 794 | |
| 795 | SkBitmap bitmap; |
| 796 | bitmap.allocN32Pixels(10, 10); |
| 797 | bitmap.eraseColor(SK_ColorGREEN); |
| 798 | |
| 799 | SkMatrix matrix; |
| 800 | matrix.setScale(SkIntToScalar(2), SkIntToScalar(2)); |
| 801 | matrix.postTranslate(SkIntToScalar(-20), SkIntToScalar(-20)); |
| 802 | SkAutoTUnref<SkImageFilter> matrixFilter( |
| 803 | SkMatrixImageFilter::Create(matrix, SkPaint::kLow_FilterLevel)); |
| 804 | |
| 805 | // Test that saveLayer() with a filter nested inside another saveLayer() applies the |
| 806 | // correct offset to the filter matrix. |
| 807 | SkRect bounds1 = SkRect::MakeXYWH(10, 10, 30, 30); |
| 808 | canvas.saveLayer(&bounds1, NULL); |
| 809 | SkPaint filterPaint; |
| 810 | filterPaint.setImageFilter(matrixFilter); |
| 811 | SkRect bounds2 = SkRect::MakeXYWH(20, 20, 10, 10); |
| 812 | canvas.saveLayer(&bounds2, &filterPaint); |
| 813 | SkPaint greenPaint; |
| 814 | greenPaint.setColor(SK_ColorGREEN); |
| 815 | canvas.drawRect(bounds2, greenPaint); |
| 816 | canvas.restore(); |
| 817 | canvas.restore(); |
| 818 | SkPaint strokePaint; |
| 819 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 820 | strokePaint.setColor(SK_ColorRED); |
| 821 | |
| 822 | SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 823 | uint32_t pixel; |
| 824 | canvas.readPixels(info, &pixel, 4, 25, 25); |
| 825 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 826 | |
| 827 | // Test that drawSprite() with a filter nested inside a saveLayer() applies the |
| 828 | // correct offset to the filter matrix. |
| 829 | canvas.clear(0x0); |
| 830 | canvas.readPixels(info, &pixel, 4, 25, 25); |
| 831 | canvas.saveLayer(&bounds1, NULL); |
| 832 | canvas.drawSprite(bitmap, 20, 20, &filterPaint); |
| 833 | canvas.restore(); |
| 834 | |
| 835 | canvas.readPixels(info, &pixel, 4, 25, 25); |
| 836 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 837 | } |
| 838 | |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 839 | DEF_TEST(XfermodeImageFilterCroppedInput, reporter) { |
| 840 | SkBitmap temp; |
| 841 | temp.allocN32Pixels(100, 100); |
| 842 | SkBitmapDevice device(temp); |
| 843 | test_xfermode_cropped_input(&device, reporter); |
| 844 | } |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 845 | |
senorblanco@chromium.org | 58d1466 | 2014-02-03 22:36:39 +0000 | [diff] [blame] | 846 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 847 | DEF_GPUTEST(ImageFilterCropRectGPU, reporter, factory) { |
| 848 | GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(0)); |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 849 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(context, |
| 850 | SkImageInfo::MakeN32Premul(100, 100), |
| 851 | 0)); |
| 852 | test_crop_rects(device, reporter); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 853 | } |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 854 | |
| 855 | DEF_GPUTEST(HugeBlurImageFilterGPU, reporter, factory) { |
| 856 | GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(0)); |
| 857 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(context, |
| 858 | SkImageInfo::MakeN32Premul(100, 100), |
| 859 | 0)); |
| 860 | test_huge_blur(device, reporter); |
| 861 | } |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 862 | |
| 863 | DEF_GPUTEST(XfermodeImageFilterCroppedInputGPU, reporter, factory) { |
| 864 | GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(0)); |
| 865 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(context, |
| 866 | SkImageInfo::MakeN32Premul(1, 1), |
| 867 | 0)); |
| 868 | test_xfermode_cropped_input(device, reporter); |
| 869 | } |
senorblanco@chromium.org | 58d1466 | 2014-02-03 22:36:39 +0000 | [diff] [blame] | 870 | #endif |