senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 8 | #include "SkBicubicImageFilter.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkBitmapDevice.h" |
| 11 | #include "SkBitmapSource.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 12 | #include "SkBlurImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 13 | #include "SkCanvas.h" |
| 14 | #include "SkColorFilterImageFilter.h" |
| 15 | #include "SkColorMatrixFilter.h" |
| 16 | #include "SkDeviceImageFilterProxy.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 17 | #include "SkDisplacementMapEffect.h" |
| 18 | #include "SkDropShadowImageFilter.h" |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 19 | #include "SkFlattenableBuffers.h" |
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 | d5424a4 | 2014-04-02 19:20:05 +0000 | [diff] [blame] | 23 | #include "SkMatrixImageFilter.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 24 | #include "SkMergeImageFilter.h" |
| 25 | #include "SkMorphologyImageFilter.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 26 | #include "SkOffsetImageFilter.h" |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 27 | #include "SkPicture.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 28 | #include "SkPictureRecorder.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 29 | #include "SkRect.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 30 | #include "SkTileImageFilter.h" |
| 31 | #include "SkXfermodeImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 32 | #include "Test.h" |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 33 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 34 | #if SK_SUPPORT_GPU |
| 35 | #include "GrContextFactory.h" |
| 36 | #include "SkGpuDevice.h" |
| 37 | #endif |
| 38 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 39 | static const int kBitmapSize = 4; |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 40 | |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 41 | namespace { |
| 42 | |
| 43 | class MatrixTestImageFilter : public SkImageFilter { |
| 44 | public: |
| 45 | MatrixTestImageFilter(skiatest::Reporter* reporter, const SkMatrix& expectedMatrix) |
| 46 | : SkImageFilter(0), fReporter(reporter), fExpectedMatrix(expectedMatrix) { |
| 47 | } |
| 48 | |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 49 | virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context& ctx, |
senorblanco@chromium.org | 0937335 | 2014-02-05 23:04:28 +0000 | [diff] [blame] | 50 | SkBitmap* result, SkIPoint* offset) const SK_OVERRIDE { |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 51 | REPORTER_ASSERT(fReporter, ctx.ctm() == fExpectedMatrix); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 52 | return true; |
| 53 | } |
| 54 | |
| 55 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(MatrixTestImageFilter) |
| 56 | |
| 57 | protected: |
| 58 | explicit MatrixTestImageFilter(SkReadBuffer& buffer) : SkImageFilter(0) { |
| 59 | fReporter = static_cast<skiatest::Reporter*>(buffer.readFunctionPtr()); |
| 60 | buffer.readMatrix(&fExpectedMatrix); |
| 61 | } |
| 62 | |
| 63 | virtual void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE { |
| 64 | buffer.writeFunctionPtr(fReporter); |
| 65 | buffer.writeMatrix(fExpectedMatrix); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | skiatest::Reporter* fReporter; |
| 70 | SkMatrix fExpectedMatrix; |
| 71 | }; |
| 72 | |
| 73 | } |
| 74 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 75 | static void make_small_bitmap(SkBitmap& bitmap) { |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 76 | bitmap.allocN32Pixels(kBitmapSize, kBitmapSize); |
| 77 | SkCanvas canvas(bitmap); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 78 | canvas.clear(0x00000000); |
| 79 | SkPaint darkPaint; |
| 80 | darkPaint.setColor(0xFF804020); |
| 81 | SkPaint lightPaint; |
| 82 | lightPaint.setColor(0xFF244484); |
| 83 | const int i = kBitmapSize / 4; |
| 84 | for (int y = 0; y < kBitmapSize; y += i) { |
| 85 | for (int x = 0; x < kBitmapSize; x += i) { |
| 86 | canvas.save(); |
| 87 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 88 | canvas.drawRect(SkRect::MakeXYWH(0, 0, |
| 89 | SkIntToScalar(i), |
| 90 | SkIntToScalar(i)), darkPaint); |
| 91 | canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i), |
| 92 | 0, |
| 93 | SkIntToScalar(i), |
| 94 | SkIntToScalar(i)), lightPaint); |
| 95 | canvas.drawRect(SkRect::MakeXYWH(0, |
| 96 | SkIntToScalar(i), |
| 97 | SkIntToScalar(i), |
| 98 | SkIntToScalar(i)), lightPaint); |
| 99 | canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i), |
| 100 | SkIntToScalar(i), |
| 101 | SkIntToScalar(i), |
| 102 | SkIntToScalar(i)), darkPaint); |
| 103 | canvas.restore(); |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 106 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 107 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 108 | static SkImageFilter* make_scale(float amount, SkImageFilter* input = NULL) { |
| 109 | SkScalar s = amount; |
| 110 | SkScalar matrix[20] = { s, 0, 0, 0, 0, |
| 111 | 0, s, 0, 0, 0, |
| 112 | 0, 0, s, 0, 0, |
| 113 | 0, 0, 0, s, 0 }; |
commit-bot@chromium.org | 727a352 | 2014-02-21 18:46:30 +0000 | [diff] [blame] | 114 | SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix)); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 115 | return SkColorFilterImageFilter::Create(filter, input); |
| 116 | } |
| 117 | |
| 118 | static SkImageFilter* make_grayscale(SkImageFilter* input = NULL, const SkImageFilter::CropRect* cropRect = NULL) { |
| 119 | SkScalar matrix[20]; |
| 120 | memset(matrix, 0, 20 * sizeof(SkScalar)); |
| 121 | matrix[0] = matrix[5] = matrix[10] = 0.2126f; |
| 122 | matrix[1] = matrix[6] = matrix[11] = 0.7152f; |
| 123 | matrix[2] = matrix[7] = matrix[12] = 0.0722f; |
| 124 | matrix[18] = 1.0f; |
commit-bot@chromium.org | 727a352 | 2014-02-21 18:46:30 +0000 | [diff] [blame] | 125 | SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix)); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 126 | return SkColorFilterImageFilter::Create(filter, input, cropRect); |
| 127 | } |
| 128 | |
| 129 | DEF_TEST(ImageFilter, reporter) { |
| 130 | { |
| 131 | // Check that two non-clipping color matrices concatenate into a single filter. |
| 132 | SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f)); |
| 133 | SkAutoTUnref<SkImageFilter> quarterBrightness(make_scale(0.5f, halfBrightness)); |
| 134 | REPORTER_ASSERT(reporter, NULL == quarterBrightness->getInput(0)); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 135 | } |
| 136 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 137 | { |
| 138 | // Check that a clipping color matrix followed by a grayscale does not concatenate into a single filter. |
| 139 | SkAutoTUnref<SkImageFilter> doubleBrightness(make_scale(2.0f)); |
| 140 | SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f, doubleBrightness)); |
| 141 | REPORTER_ASSERT(reporter, NULL != halfBrightness->getInput(0)); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 142 | } |
| 143 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 144 | { |
| 145 | // Check that a color filter image filter without a crop rect can be |
| 146 | // expressed as a color filter. |
| 147 | SkAutoTUnref<SkImageFilter> gray(make_grayscale()); |
| 148 | REPORTER_ASSERT(reporter, true == gray->asColorFilter(NULL)); |
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 color filter image filter with a crop rect cannot |
| 153 | // be expressed as a color filter. |
| 154 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(0, 0, 100, 100)); |
| 155 | SkAutoTUnref<SkImageFilter> grayWithCrop(make_grayscale(NULL, &cropRect)); |
| 156 | REPORTER_ASSERT(reporter, false == grayWithCrop->asColorFilter(NULL)); |
| 157 | } |
| 158 | |
| 159 | { |
| 160 | // Tests pass by not asserting |
| 161 | SkBitmap bitmap, result; |
| 162 | make_small_bitmap(bitmap); |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 163 | result.allocN32Pixels(kBitmapSize, kBitmapSize); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 164 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 165 | { |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 166 | // This tests for : |
| 167 | // 1 ) location at (0,0,1) |
| 168 | SkPoint3 location(0, 0, SK_Scalar1); |
| 169 | // 2 ) location and target at same value |
| 170 | SkPoint3 target(location.fX, location.fY, location.fZ); |
| 171 | // 3 ) large negative specular exponent value |
| 172 | SkScalar specularExponent = -1000; |
| 173 | |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 174 | SkAutoTUnref<SkImageFilter> bmSrc(SkBitmapSource::Create(bitmap)); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 175 | SkPaint paint; |
| 176 | paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular( |
| 177 | location, target, specularExponent, 180, |
| 178 | 0xFFFFFFFF, SK_Scalar1, SK_Scalar1, SK_Scalar1, |
| 179 | bmSrc))->unref(); |
| 180 | SkCanvas canvas(result); |
| 181 | SkRect r = SkRect::MakeWH(SkIntToScalar(kBitmapSize), |
| 182 | SkIntToScalar(kBitmapSize)); |
| 183 | canvas.drawRect(r, paint); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 184 | } |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 185 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 186 | { |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 187 | // This tests for scale bringing width to 0 |
| 188 | SkSize scale = SkSize::Make(-0.001f, SK_Scalar1); |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 189 | SkAutoTUnref<SkImageFilter> bmSrc(SkBitmapSource::Create(bitmap)); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 190 | SkAutoTUnref<SkBicubicImageFilter> bicubic( |
| 191 | SkBicubicImageFilter::CreateMitchell(scale, bmSrc)); |
| 192 | SkBitmapDevice device(bitmap); |
| 193 | SkDeviceImageFilterProxy proxy(&device); |
| 194 | SkIPoint loc = SkIPoint::Make(0, 0); |
| 195 | // An empty input should early return and return false |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 196 | SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(2)); |
| 197 | SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeEmpty(), cache.get()); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 198 | REPORTER_ASSERT(reporter, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 199 | !bicubic->filterImage(&proxy, bitmap, ctx, &result, &loc)); |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 200 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 201 | } |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 202 | } |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 203 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 204 | static void test_crop_rects(SkBaseDevice* device, skiatest::Reporter* reporter) { |
| 205 | // Check that all filters offset to their absolute crop rect, |
| 206 | // unaffected by the input crop rect. |
| 207 | // Tests pass by not asserting. |
| 208 | SkBitmap bitmap; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 209 | bitmap.allocN32Pixels(100, 100); |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 210 | bitmap.eraseARGB(0, 0, 0, 0); |
| 211 | SkDeviceImageFilterProxy proxy(device); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 212 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 213 | SkImageFilter::CropRect inputCropRect(SkRect::MakeXYWH(8, 13, 80, 80)); |
| 214 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 30, 60, 60)); |
| 215 | SkAutoTUnref<SkImageFilter> input(make_grayscale(NULL, &inputCropRect)); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 216 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 217 | SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_ColorRED, SkXfermode::kSrcIn_Mode)); |
| 218 | SkPoint3 location(0, 0, SK_Scalar1); |
| 219 | SkPoint3 target(SK_Scalar1, SK_Scalar1, SK_Scalar1); |
| 220 | SkScalar kernel[9] = { |
| 221 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 222 | SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1), |
| 223 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 224 | }; |
| 225 | SkISize kernelSize = SkISize::Make(3, 3); |
| 226 | SkScalar gain = SK_Scalar1, bias = 0; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 227 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 228 | SkImageFilter* filters[] = { |
| 229 | SkColorFilterImageFilter::Create(cf.get(), input.get(), &cropRect), |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 230 | SkDisplacementMapEffect::Create(SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 231 | SkDisplacementMapEffect::kB_ChannelSelectorType, |
| 232 | 40.0f, input.get(), input.get(), &cropRect), |
| 233 | SkBlurImageFilter::Create(SK_Scalar1, SK_Scalar1, input.get(), &cropRect), |
| 234 | 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] | 235 | SkLightingImageFilter::CreatePointLitDiffuse(location, SK_ColorGREEN, 0, 0, input.get(), &cropRect), |
| 236 | 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] | 237 | SkMatrixConvolutionImageFilter::Create(kernelSize, kernel, gain, bias, SkIPoint::Make(1, 1), SkMatrixConvolutionImageFilter::kRepeat_TileMode, false, input.get(), &cropRect), |
| 238 | SkMergeImageFilter::Create(input.get(), input.get(), SkXfermode::kSrcOver_Mode, &cropRect), |
| 239 | SkOffsetImageFilter::Create(SK_Scalar1, SK_Scalar1, input.get(), &cropRect), |
| 240 | SkOffsetImageFilter::Create(SK_Scalar1, SK_Scalar1, input.get(), &cropRect), |
| 241 | SkDilateImageFilter::Create(3, 2, input.get(), &cropRect), |
| 242 | SkErodeImageFilter::Create(2, 3, input.get(), &cropRect), |
| 243 | SkTileImageFilter::Create(inputCropRect.rect(), cropRect.rect(), input.get()), |
| 244 | 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] | 245 | }; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 246 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 247 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 248 | SkImageFilter* filter = filters[i]; |
| 249 | SkBitmap result; |
| 250 | SkIPoint offset; |
| 251 | SkString str; |
senorblanco@chromium.org | f4e1a76 | 2014-02-04 00:28:46 +0000 | [diff] [blame] | 252 | str.printf("filter %d", static_cast<int>(i)); |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 253 | SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(2)); |
| 254 | SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeLargest(), cache.get()); |
| 255 | REPORTER_ASSERT_MESSAGE(reporter, filter->filterImage(&proxy, bitmap, ctx, |
| 256 | &result, &offset), str.c_str()); |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 257 | 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] | 258 | } |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 259 | |
| 260 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 261 | SkSafeUnref(filters[i]); |
| 262 | } |
| 263 | } |
| 264 | |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame^] | 265 | static SkBitmap make_gradient_circle(int width, int height) { |
| 266 | SkBitmap bitmap; |
| 267 | SkScalar x = SkIntToScalar(width / 2); |
| 268 | SkScalar y = SkIntToScalar(height / 2); |
| 269 | SkScalar radius = SkMinScalar(x, y) * 0.8f; |
| 270 | bitmap.allocN32Pixels(width, height); |
| 271 | SkCanvas canvas(bitmap); |
| 272 | canvas.clear(0x00000000); |
| 273 | SkColor colors[2]; |
| 274 | colors[0] = SK_ColorWHITE; |
| 275 | colors[1] = SK_ColorBLACK; |
| 276 | SkAutoTUnref<SkShader> shader( |
| 277 | SkGradientShader::CreateRadial(SkPoint::Make(x, y), radius, colors, NULL, 2, |
| 278 | SkShader::kClamp_TileMode) |
| 279 | ); |
| 280 | SkPaint paint; |
| 281 | paint.setShader(shader); |
| 282 | canvas.drawCircle(x, y, radius, paint); |
| 283 | return bitmap; |
| 284 | } |
| 285 | |
| 286 | DEF_TEST(ImageFilterDrawTiled, reporter) { |
| 287 | // Check that all filters when drawn tiled (with subsequent clip rects) exactly |
| 288 | // match the same filters drawn with a single full-canvas bitmap draw. |
| 289 | // Tests pass by not asserting. |
| 290 | |
| 291 | SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_ColorRED, SkXfermode::kSrcIn_Mode)); |
| 292 | SkPoint3 location(0, 0, SK_Scalar1); |
| 293 | SkPoint3 target(SK_Scalar1, SK_Scalar1, SK_Scalar1); |
| 294 | SkScalar kernel[9] = { |
| 295 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 296 | SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1), |
| 297 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 298 | }; |
| 299 | SkISize kernelSize = SkISize::Make(3, 3); |
| 300 | SkScalar gain = SK_Scalar1, bias = 0; |
| 301 | |
| 302 | SkAutoTUnref<SkImageFilter> gradient_source(SkBitmapSource::Create(make_gradient_circle(64, 64))); |
| 303 | |
| 304 | struct { |
| 305 | const char* fName; |
| 306 | SkImageFilter* fFilter; |
| 307 | } filters[] = { |
| 308 | { "color filter", SkColorFilterImageFilter::Create(cf.get()) }, |
| 309 | { "displacement map", SkDisplacementMapEffect::Create( |
| 310 | SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 311 | SkDisplacementMapEffect::kB_ChannelSelectorType, |
| 312 | 40.0f, gradient_source.get()) }, |
| 313 | { "blur", SkBlurImageFilter::Create(SK_Scalar1, SK_Scalar1) }, |
| 314 | { "drop shadow", SkDropShadowImageFilter::Create( |
| 315 | SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_ColorGREEN) }, |
| 316 | { "diffuse lighting", SkLightingImageFilter::CreatePointLitDiffuse( |
| 317 | location, SK_ColorGREEN, 0, 0) }, |
| 318 | { "specular lighting", |
| 319 | SkLightingImageFilter::CreatePointLitSpecular(location, SK_ColorGREEN, 0, 0, 0) }, |
| 320 | { "matrix convolution", |
| 321 | SkMatrixConvolutionImageFilter::Create( |
| 322 | kernelSize, kernel, gain, bias, SkIPoint::Make(1, 1), |
| 323 | SkMatrixConvolutionImageFilter::kRepeat_TileMode, false) }, |
| 324 | { "merge", SkMergeImageFilter::Create(NULL, NULL, SkXfermode::kSrcOver_Mode) }, |
| 325 | { "offset", SkOffsetImageFilter::Create(SK_Scalar1, SK_Scalar1) }, |
| 326 | { "dilate", SkDilateImageFilter::Create(3, 2) }, |
| 327 | { "erode", SkErodeImageFilter::Create(2, 3) }, |
| 328 | { "tile", SkTileImageFilter::Create(SkRect::MakeXYWH(0, 0, 50, 50), |
| 329 | SkRect::MakeXYWH(0, 0, 100, 100), NULL) }, |
| 330 | }; |
| 331 | |
| 332 | SkBitmap untiledResult, tiledResult; |
| 333 | int width = 64, height = 64; |
| 334 | untiledResult.allocN32Pixels(width, height); |
| 335 | tiledResult.allocN32Pixels(width, height); |
| 336 | SkCanvas tiledCanvas(tiledResult); |
| 337 | SkCanvas untiledCanvas(untiledResult); |
| 338 | tiledCanvas.clear(0); |
| 339 | untiledCanvas.clear(0); |
| 340 | int tileSize = 16; |
| 341 | |
| 342 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 343 | SkPaint paint; |
| 344 | paint.setImageFilter(filters[i].fFilter); |
| 345 | paint.setTextSize(SkIntToScalar(height)); |
| 346 | paint.setColor(SK_ColorWHITE); |
| 347 | SkString str; |
| 348 | const char* text = "ABC"; |
| 349 | SkScalar ypos = SkIntToScalar(height); |
| 350 | untiledCanvas.drawText(text, strlen(text), 0, ypos, paint); |
| 351 | for (int y = 0; y < height; y += tileSize) { |
| 352 | for (int x = 0; x < width; x += tileSize) { |
| 353 | tiledCanvas.save(); |
| 354 | tiledCanvas.clipRect(SkRect::Make(SkIRect::MakeXYWH(x, y, tileSize, tileSize))); |
| 355 | tiledCanvas.drawText(text, strlen(text), 0, ypos, paint); |
| 356 | tiledCanvas.restore(); |
| 357 | } |
| 358 | } |
| 359 | untiledCanvas.flush(); |
| 360 | tiledCanvas.flush(); |
| 361 | for (int y = 0; y < height; y++) { |
| 362 | int diffs = memcmp(untiledResult.getAddr32(0, y), tiledResult.getAddr32(0, y), untiledResult.rowBytes()); |
| 363 | REPORTER_ASSERT_MESSAGE(reporter, !diffs, filters[i].fName); |
| 364 | if (diffs) { |
| 365 | break; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 371 | SkSafeUnref(filters[i].fFilter); |
| 372 | } |
| 373 | } |
| 374 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 375 | DEF_TEST(ImageFilterCropRect, reporter) { |
| 376 | SkBitmap temp; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 377 | temp.allocN32Pixels(100, 100); |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 378 | SkBitmapDevice device(temp); |
| 379 | test_crop_rects(&device, reporter); |
| 380 | } |
| 381 | |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 382 | DEF_TEST(ImageFilterMatrixTest, reporter) { |
| 383 | SkBitmap temp; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 384 | temp.allocN32Pixels(100, 100); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 385 | SkBitmapDevice device(temp); |
| 386 | SkCanvas canvas(&device); |
| 387 | canvas.scale(SkIntToScalar(2), SkIntToScalar(2)); |
| 388 | |
| 389 | SkMatrix expectedMatrix = canvas.getTotalMatrix(); |
| 390 | |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 391 | SkRTreeFactory factory; |
| 392 | SkPictureRecorder recorder; |
| 393 | SkCanvas* recordingCanvas = recorder.beginRecording(100, 100, &factory, 0); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 394 | |
| 395 | SkPaint paint; |
| 396 | SkAutoTUnref<MatrixTestImageFilter> imageFilter( |
| 397 | new MatrixTestImageFilter(reporter, expectedMatrix)); |
| 398 | paint.setImageFilter(imageFilter.get()); |
commit-bot@chromium.org | 091a594 | 2014-04-18 14:19:31 +0000 | [diff] [blame] | 399 | recordingCanvas->saveLayer(NULL, &paint); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 400 | SkPaint solidPaint; |
| 401 | solidPaint.setColor(0xFFFFFFFF); |
| 402 | recordingCanvas->save(); |
| 403 | recordingCanvas->scale(SkIntToScalar(10), SkIntToScalar(10)); |
| 404 | recordingCanvas->drawRect(SkRect::Make(SkIRect::MakeWH(100, 100)), solidPaint); |
| 405 | recordingCanvas->restore(); // scale |
| 406 | recordingCanvas->restore(); // saveLayer |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 407 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 408 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 409 | canvas.drawPicture(*picture); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 410 | } |
| 411 | |
senorblanco@chromium.org | 28ae55d | 2014-03-24 21:32:28 +0000 | [diff] [blame] | 412 | static void test_huge_blur(SkBaseDevice* device, skiatest::Reporter* reporter) { |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 413 | SkCanvas canvas(device); |
| 414 | |
| 415 | SkBitmap bitmap; |
| 416 | bitmap.allocN32Pixels(100, 100); |
| 417 | bitmap.eraseARGB(0, 0, 0, 0); |
| 418 | |
| 419 | // Check that a blur with an insane radius does not crash or assert. |
| 420 | SkAutoTUnref<SkImageFilter> blur(SkBlurImageFilter::Create(SkIntToScalar(1<<30), SkIntToScalar(1<<30))); |
| 421 | |
| 422 | SkPaint paint; |
| 423 | paint.setImageFilter(blur); |
| 424 | canvas.drawSprite(bitmap, 0, 0, &paint); |
| 425 | } |
| 426 | |
| 427 | DEF_TEST(HugeBlurImageFilter, reporter) { |
| 428 | SkBitmap temp; |
| 429 | temp.allocN32Pixels(100, 100); |
| 430 | SkBitmapDevice device(temp); |
| 431 | test_huge_blur(&device, reporter); |
| 432 | } |
| 433 | |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 434 | static void test_xfermode_cropped_input(SkBaseDevice* device, skiatest::Reporter* reporter) { |
| 435 | SkCanvas canvas(device); |
| 436 | canvas.clear(0); |
| 437 | |
| 438 | SkBitmap bitmap; |
| 439 | bitmap.allocN32Pixels(1, 1); |
| 440 | bitmap.eraseARGB(255, 255, 255, 255); |
| 441 | |
| 442 | SkAutoTUnref<SkColorFilter> green( |
| 443 | SkColorFilter::CreateModeFilter(SK_ColorGREEN, SkXfermode::kSrcIn_Mode)); |
| 444 | SkAutoTUnref<SkColorFilterImageFilter> greenFilter( |
| 445 | SkColorFilterImageFilter::Create(green.get())); |
| 446 | SkImageFilter::CropRect cropRect(SkRect::MakeEmpty()); |
| 447 | SkAutoTUnref<SkColorFilterImageFilter> croppedOut( |
| 448 | SkColorFilterImageFilter::Create(green.get(), NULL, &cropRect)); |
| 449 | |
| 450 | // Check that an xfermode image filter whose input has been cropped out still draws the other |
| 451 | // input. Also check that drawing with both inputs cropped out doesn't cause a GPU warning. |
| 452 | SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrcOver_Mode); |
| 453 | SkAutoTUnref<SkImageFilter> xfermodeNoFg( |
| 454 | SkXfermodeImageFilter::Create(mode, greenFilter, croppedOut)); |
| 455 | SkAutoTUnref<SkImageFilter> xfermodeNoBg( |
| 456 | SkXfermodeImageFilter::Create(mode, croppedOut, greenFilter)); |
| 457 | SkAutoTUnref<SkImageFilter> xfermodeNoFgNoBg( |
| 458 | SkXfermodeImageFilter::Create(mode, croppedOut, croppedOut)); |
| 459 | |
| 460 | SkPaint paint; |
| 461 | paint.setImageFilter(xfermodeNoFg); |
| 462 | canvas.drawSprite(bitmap, 0, 0, &paint); |
| 463 | |
| 464 | uint32_t pixel; |
| 465 | SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 466 | canvas.readPixels(info, &pixel, 4, 0, 0); |
| 467 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 468 | |
| 469 | paint.setImageFilter(xfermodeNoBg); |
| 470 | canvas.drawSprite(bitmap, 0, 0, &paint); |
| 471 | canvas.readPixels(info, &pixel, 4, 0, 0); |
| 472 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 473 | |
| 474 | paint.setImageFilter(xfermodeNoFgNoBg); |
| 475 | canvas.drawSprite(bitmap, 0, 0, &paint); |
| 476 | canvas.readPixels(info, &pixel, 4, 0, 0); |
| 477 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 478 | } |
| 479 | |
senorblanco@chromium.org | d5424a4 | 2014-04-02 19:20:05 +0000 | [diff] [blame] | 480 | DEF_TEST(ImageFilterNestedSaveLayer, reporter) { |
| 481 | SkBitmap temp; |
| 482 | temp.allocN32Pixels(50, 50); |
| 483 | SkBitmapDevice device(temp); |
| 484 | SkCanvas canvas(&device); |
| 485 | canvas.clear(0x0); |
| 486 | |
| 487 | SkBitmap bitmap; |
| 488 | bitmap.allocN32Pixels(10, 10); |
| 489 | bitmap.eraseColor(SK_ColorGREEN); |
| 490 | |
| 491 | SkMatrix matrix; |
| 492 | matrix.setScale(SkIntToScalar(2), SkIntToScalar(2)); |
| 493 | matrix.postTranslate(SkIntToScalar(-20), SkIntToScalar(-20)); |
| 494 | SkAutoTUnref<SkImageFilter> matrixFilter( |
| 495 | SkMatrixImageFilter::Create(matrix, SkPaint::kLow_FilterLevel)); |
| 496 | |
| 497 | // Test that saveLayer() with a filter nested inside another saveLayer() applies the |
| 498 | // correct offset to the filter matrix. |
| 499 | SkRect bounds1 = SkRect::MakeXYWH(10, 10, 30, 30); |
| 500 | canvas.saveLayer(&bounds1, NULL); |
| 501 | SkPaint filterPaint; |
| 502 | filterPaint.setImageFilter(matrixFilter); |
| 503 | SkRect bounds2 = SkRect::MakeXYWH(20, 20, 10, 10); |
| 504 | canvas.saveLayer(&bounds2, &filterPaint); |
| 505 | SkPaint greenPaint; |
| 506 | greenPaint.setColor(SK_ColorGREEN); |
| 507 | canvas.drawRect(bounds2, greenPaint); |
| 508 | canvas.restore(); |
| 509 | canvas.restore(); |
| 510 | SkPaint strokePaint; |
| 511 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 512 | strokePaint.setColor(SK_ColorRED); |
| 513 | |
| 514 | SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 515 | uint32_t pixel; |
| 516 | canvas.readPixels(info, &pixel, 4, 25, 25); |
| 517 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 518 | |
| 519 | // Test that drawSprite() with a filter nested inside a saveLayer() applies the |
| 520 | // correct offset to the filter matrix. |
| 521 | canvas.clear(0x0); |
| 522 | canvas.readPixels(info, &pixel, 4, 25, 25); |
| 523 | canvas.saveLayer(&bounds1, NULL); |
| 524 | canvas.drawSprite(bitmap, 20, 20, &filterPaint); |
| 525 | canvas.restore(); |
| 526 | |
| 527 | canvas.readPixels(info, &pixel, 4, 25, 25); |
| 528 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 529 | } |
| 530 | |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 531 | DEF_TEST(XfermodeImageFilterCroppedInput, reporter) { |
| 532 | SkBitmap temp; |
| 533 | temp.allocN32Pixels(100, 100); |
| 534 | SkBitmapDevice device(temp); |
| 535 | test_xfermode_cropped_input(&device, reporter); |
| 536 | } |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 537 | |
senorblanco@chromium.org | 58d1466 | 2014-02-03 22:36:39 +0000 | [diff] [blame] | 538 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 539 | DEF_GPUTEST(ImageFilterCropRectGPU, reporter, factory) { |
| 540 | GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(0)); |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 541 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(context, |
| 542 | SkImageInfo::MakeN32Premul(100, 100), |
| 543 | 0)); |
| 544 | test_crop_rects(device, reporter); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 545 | } |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 546 | |
| 547 | DEF_GPUTEST(HugeBlurImageFilterGPU, reporter, factory) { |
| 548 | GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(0)); |
| 549 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(context, |
| 550 | SkImageInfo::MakeN32Premul(100, 100), |
| 551 | 0)); |
| 552 | test_huge_blur(device, reporter); |
| 553 | } |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 554 | |
| 555 | DEF_GPUTEST(XfermodeImageFilterCroppedInputGPU, reporter, factory) { |
| 556 | GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(0)); |
| 557 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(context, |
| 558 | SkImageInfo::MakeN32Premul(1, 1), |
| 559 | 0)); |
| 560 | test_xfermode_cropped_input(device, reporter); |
| 561 | } |
senorblanco@chromium.org | 58d1466 | 2014-02-03 22:36:39 +0000 | [diff] [blame] | 562 | #endif |