senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 8 | #include "SkBicubicImageFilter.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkBitmapDevice.h" |
| 11 | #include "SkBitmapSource.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 12 | #include "SkBlurImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 13 | #include "SkCanvas.h" |
| 14 | #include "SkColorFilterImageFilter.h" |
| 15 | #include "SkColorMatrixFilter.h" |
| 16 | #include "SkDeviceImageFilterProxy.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 17 | #include "SkDisplacementMapEffect.h" |
| 18 | #include "SkDropShadowImageFilter.h" |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 19 | #include "SkFlattenableBuffers.h" |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 20 | #include "SkLightingImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 21 | #include "SkMatrixConvolutionImageFilter.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 22 | #include "SkMergeImageFilter.h" |
| 23 | #include "SkMorphologyImageFilter.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 24 | #include "SkOffsetImageFilter.h" |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 25 | #include "SkPicture.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 26 | #include "SkRect.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 27 | #include "SkTileImageFilter.h" |
| 28 | #include "SkXfermodeImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 29 | #include "Test.h" |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 30 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 31 | #if SK_SUPPORT_GPU |
| 32 | #include "GrContextFactory.h" |
| 33 | #include "SkGpuDevice.h" |
| 34 | #endif |
| 35 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 36 | static const int kBitmapSize = 4; |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 37 | |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 38 | namespace { |
| 39 | |
| 40 | class MatrixTestImageFilter : public SkImageFilter { |
| 41 | public: |
| 42 | MatrixTestImageFilter(skiatest::Reporter* reporter, const SkMatrix& expectedMatrix) |
| 43 | : SkImageFilter(0), fReporter(reporter), fExpectedMatrix(expectedMatrix) { |
| 44 | } |
| 45 | |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 46 | virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context& ctx, |
senorblanco@chromium.org | 0937335 | 2014-02-05 23:04:28 +0000 | [diff] [blame] | 47 | SkBitmap* result, SkIPoint* offset) const SK_OVERRIDE { |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 48 | REPORTER_ASSERT(fReporter, ctx.ctm() == fExpectedMatrix); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 49 | return true; |
| 50 | } |
| 51 | |
| 52 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(MatrixTestImageFilter) |
| 53 | |
| 54 | protected: |
| 55 | explicit MatrixTestImageFilter(SkReadBuffer& buffer) : SkImageFilter(0) { |
| 56 | fReporter = static_cast<skiatest::Reporter*>(buffer.readFunctionPtr()); |
| 57 | buffer.readMatrix(&fExpectedMatrix); |
| 58 | } |
| 59 | |
| 60 | virtual void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE { |
| 61 | buffer.writeFunctionPtr(fReporter); |
| 62 | buffer.writeMatrix(fExpectedMatrix); |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | skiatest::Reporter* fReporter; |
| 67 | SkMatrix fExpectedMatrix; |
| 68 | }; |
| 69 | |
| 70 | } |
| 71 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 72 | static void make_small_bitmap(SkBitmap& bitmap) { |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 73 | bitmap.allocN32Pixels(kBitmapSize, kBitmapSize); |
| 74 | SkCanvas canvas(bitmap); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 75 | canvas.clear(0x00000000); |
| 76 | SkPaint darkPaint; |
| 77 | darkPaint.setColor(0xFF804020); |
| 78 | SkPaint lightPaint; |
| 79 | lightPaint.setColor(0xFF244484); |
| 80 | const int i = kBitmapSize / 4; |
| 81 | for (int y = 0; y < kBitmapSize; y += i) { |
| 82 | for (int x = 0; x < kBitmapSize; x += i) { |
| 83 | canvas.save(); |
| 84 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 85 | canvas.drawRect(SkRect::MakeXYWH(0, 0, |
| 86 | SkIntToScalar(i), |
| 87 | SkIntToScalar(i)), darkPaint); |
| 88 | canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i), |
| 89 | 0, |
| 90 | SkIntToScalar(i), |
| 91 | SkIntToScalar(i)), lightPaint); |
| 92 | canvas.drawRect(SkRect::MakeXYWH(0, |
| 93 | SkIntToScalar(i), |
| 94 | SkIntToScalar(i), |
| 95 | SkIntToScalar(i)), lightPaint); |
| 96 | canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(i), |
| 97 | SkIntToScalar(i), |
| 98 | SkIntToScalar(i), |
| 99 | SkIntToScalar(i)), darkPaint); |
| 100 | canvas.restore(); |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 103 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 104 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 105 | static SkImageFilter* make_scale(float amount, SkImageFilter* input = NULL) { |
| 106 | SkScalar s = amount; |
| 107 | SkScalar matrix[20] = { s, 0, 0, 0, 0, |
| 108 | 0, s, 0, 0, 0, |
| 109 | 0, 0, s, 0, 0, |
| 110 | 0, 0, 0, s, 0 }; |
commit-bot@chromium.org | 727a352 | 2014-02-21 18:46:30 +0000 | [diff] [blame] | 111 | SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix)); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 112 | return SkColorFilterImageFilter::Create(filter, input); |
| 113 | } |
| 114 | |
| 115 | static SkImageFilter* make_grayscale(SkImageFilter* input = NULL, const SkImageFilter::CropRect* cropRect = NULL) { |
| 116 | SkScalar matrix[20]; |
| 117 | memset(matrix, 0, 20 * sizeof(SkScalar)); |
| 118 | matrix[0] = matrix[5] = matrix[10] = 0.2126f; |
| 119 | matrix[1] = matrix[6] = matrix[11] = 0.7152f; |
| 120 | matrix[2] = matrix[7] = matrix[12] = 0.0722f; |
| 121 | matrix[18] = 1.0f; |
commit-bot@chromium.org | 727a352 | 2014-02-21 18:46:30 +0000 | [diff] [blame] | 122 | SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix)); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 123 | return SkColorFilterImageFilter::Create(filter, input, cropRect); |
| 124 | } |
| 125 | |
| 126 | DEF_TEST(ImageFilter, reporter) { |
| 127 | { |
| 128 | // Check that two non-clipping color matrices concatenate into a single filter. |
| 129 | SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f)); |
| 130 | SkAutoTUnref<SkImageFilter> quarterBrightness(make_scale(0.5f, halfBrightness)); |
| 131 | REPORTER_ASSERT(reporter, NULL == quarterBrightness->getInput(0)); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 132 | } |
| 133 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 134 | { |
| 135 | // Check that a clipping color matrix followed by a grayscale does not concatenate into a single filter. |
| 136 | SkAutoTUnref<SkImageFilter> doubleBrightness(make_scale(2.0f)); |
| 137 | SkAutoTUnref<SkImageFilter> halfBrightness(make_scale(0.5f, doubleBrightness)); |
| 138 | REPORTER_ASSERT(reporter, NULL != halfBrightness->getInput(0)); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 139 | } |
| 140 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 141 | { |
| 142 | // Check that a color filter image filter without a crop rect can be |
| 143 | // expressed as a color filter. |
| 144 | SkAutoTUnref<SkImageFilter> gray(make_grayscale()); |
| 145 | REPORTER_ASSERT(reporter, true == gray->asColorFilter(NULL)); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 146 | } |
| 147 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 148 | { |
| 149 | // Check that a color filter image filter with a crop rect cannot |
| 150 | // be expressed as a color filter. |
| 151 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(0, 0, 100, 100)); |
| 152 | SkAutoTUnref<SkImageFilter> grayWithCrop(make_grayscale(NULL, &cropRect)); |
| 153 | REPORTER_ASSERT(reporter, false == grayWithCrop->asColorFilter(NULL)); |
| 154 | } |
| 155 | |
| 156 | { |
| 157 | // Tests pass by not asserting |
| 158 | SkBitmap bitmap, result; |
| 159 | make_small_bitmap(bitmap); |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 160 | result.allocN32Pixels(kBitmapSize, kBitmapSize); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 161 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 162 | { |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 163 | // This tests for : |
| 164 | // 1 ) location at (0,0,1) |
| 165 | SkPoint3 location(0, 0, SK_Scalar1); |
| 166 | // 2 ) location and target at same value |
| 167 | SkPoint3 target(location.fX, location.fY, location.fZ); |
| 168 | // 3 ) large negative specular exponent value |
| 169 | SkScalar specularExponent = -1000; |
| 170 | |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 171 | SkAutoTUnref<SkImageFilter> bmSrc(SkBitmapSource::Create(bitmap)); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 172 | SkPaint paint; |
| 173 | paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular( |
| 174 | location, target, specularExponent, 180, |
| 175 | 0xFFFFFFFF, SK_Scalar1, SK_Scalar1, SK_Scalar1, |
| 176 | bmSrc))->unref(); |
| 177 | SkCanvas canvas(result); |
| 178 | SkRect r = SkRect::MakeWH(SkIntToScalar(kBitmapSize), |
| 179 | SkIntToScalar(kBitmapSize)); |
| 180 | canvas.drawRect(r, paint); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 181 | } |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 182 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 183 | { |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 184 | // This tests for scale bringing width to 0 |
| 185 | SkSize scale = SkSize::Make(-0.001f, SK_Scalar1); |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 186 | SkAutoTUnref<SkImageFilter> bmSrc(SkBitmapSource::Create(bitmap)); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 187 | SkAutoTUnref<SkBicubicImageFilter> bicubic( |
| 188 | SkBicubicImageFilter::CreateMitchell(scale, bmSrc)); |
| 189 | SkBitmapDevice device(bitmap); |
| 190 | SkDeviceImageFilterProxy proxy(&device); |
| 191 | SkIPoint loc = SkIPoint::Make(0, 0); |
| 192 | // An empty input should early return and return false |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 193 | SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeEmpty()); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 194 | REPORTER_ASSERT(reporter, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 195 | !bicubic->filterImage(&proxy, bitmap, ctx, &result, &loc)); |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 196 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 197 | } |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 198 | } |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 199 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 200 | static void test_crop_rects(SkBaseDevice* device, skiatest::Reporter* reporter) { |
| 201 | // Check that all filters offset to their absolute crop rect, |
| 202 | // unaffected by the input crop rect. |
| 203 | // Tests pass by not asserting. |
| 204 | SkBitmap bitmap; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 205 | bitmap.allocN32Pixels(100, 100); |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 206 | bitmap.eraseARGB(0, 0, 0, 0); |
| 207 | SkDeviceImageFilterProxy proxy(device); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 208 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 209 | SkImageFilter::CropRect inputCropRect(SkRect::MakeXYWH(8, 13, 80, 80)); |
| 210 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 30, 60, 60)); |
| 211 | SkAutoTUnref<SkImageFilter> input(make_grayscale(NULL, &inputCropRect)); |
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 | SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_ColorRED, SkXfermode::kSrcIn_Mode)); |
| 214 | SkPoint3 location(0, 0, SK_Scalar1); |
| 215 | SkPoint3 target(SK_Scalar1, SK_Scalar1, SK_Scalar1); |
| 216 | SkScalar kernel[9] = { |
| 217 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 218 | SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1), |
| 219 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 220 | }; |
| 221 | SkISize kernelSize = SkISize::Make(3, 3); |
| 222 | SkScalar gain = SK_Scalar1, bias = 0; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 223 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 224 | SkImageFilter* filters[] = { |
| 225 | SkColorFilterImageFilter::Create(cf.get(), input.get(), &cropRect), |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 226 | SkDisplacementMapEffect::Create(SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 227 | SkDisplacementMapEffect::kB_ChannelSelectorType, |
| 228 | 40.0f, input.get(), input.get(), &cropRect), |
| 229 | SkBlurImageFilter::Create(SK_Scalar1, SK_Scalar1, input.get(), &cropRect), |
| 230 | 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] | 231 | SkLightingImageFilter::CreatePointLitDiffuse(location, SK_ColorGREEN, 0, 0, input.get(), &cropRect), |
| 232 | 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] | 233 | SkMatrixConvolutionImageFilter::Create(kernelSize, kernel, gain, bias, SkIPoint::Make(1, 1), SkMatrixConvolutionImageFilter::kRepeat_TileMode, false, input.get(), &cropRect), |
| 234 | SkMergeImageFilter::Create(input.get(), input.get(), SkXfermode::kSrcOver_Mode, &cropRect), |
| 235 | SkOffsetImageFilter::Create(SK_Scalar1, SK_Scalar1, input.get(), &cropRect), |
| 236 | SkOffsetImageFilter::Create(SK_Scalar1, SK_Scalar1, input.get(), &cropRect), |
| 237 | SkDilateImageFilter::Create(3, 2, input.get(), &cropRect), |
| 238 | SkErodeImageFilter::Create(2, 3, input.get(), &cropRect), |
| 239 | SkTileImageFilter::Create(inputCropRect.rect(), cropRect.rect(), input.get()), |
| 240 | 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] | 241 | }; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 242 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 243 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 244 | SkImageFilter* filter = filters[i]; |
| 245 | SkBitmap result; |
| 246 | SkIPoint offset; |
| 247 | SkString str; |
senorblanco@chromium.org | f4e1a76 | 2014-02-04 00:28:46 +0000 | [diff] [blame] | 248 | str.printf("filter %d", static_cast<int>(i)); |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 249 | SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeLargest()); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 250 | REPORTER_ASSERT_MESSAGE(reporter, filter->filterImage(&proxy, bitmap, ctx, &result, &offset), str.c_str()); |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 251 | 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] | 252 | } |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 253 | |
| 254 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 255 | SkSafeUnref(filters[i]); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | DEF_TEST(ImageFilterCropRect, reporter) { |
| 260 | SkBitmap temp; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 261 | temp.allocN32Pixels(100, 100); |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 262 | SkBitmapDevice device(temp); |
| 263 | test_crop_rects(&device, reporter); |
| 264 | } |
| 265 | |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 266 | DEF_TEST(ImageFilterMatrixTest, reporter) { |
| 267 | SkBitmap temp; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 268 | temp.allocN32Pixels(100, 100); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 269 | SkBitmapDevice device(temp); |
| 270 | SkCanvas canvas(&device); |
| 271 | canvas.scale(SkIntToScalar(2), SkIntToScalar(2)); |
| 272 | |
| 273 | SkMatrix expectedMatrix = canvas.getTotalMatrix(); |
| 274 | |
| 275 | SkPicture picture; |
| 276 | SkCanvas* recordingCanvas = picture.beginRecording(100, 100, |
| 277 | SkPicture::kOptimizeForClippedPlayback_RecordingFlag); |
| 278 | |
| 279 | SkPaint paint; |
| 280 | SkAutoTUnref<MatrixTestImageFilter> imageFilter( |
| 281 | new MatrixTestImageFilter(reporter, expectedMatrix)); |
| 282 | paint.setImageFilter(imageFilter.get()); |
| 283 | SkCanvas::SaveFlags saveFlags = static_cast<SkCanvas::SaveFlags>( |
| 284 | SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kFullColorLayer_SaveFlag); |
| 285 | recordingCanvas->saveLayer(NULL, &paint, saveFlags); |
| 286 | SkPaint solidPaint; |
| 287 | solidPaint.setColor(0xFFFFFFFF); |
| 288 | recordingCanvas->save(); |
| 289 | recordingCanvas->scale(SkIntToScalar(10), SkIntToScalar(10)); |
| 290 | recordingCanvas->drawRect(SkRect::Make(SkIRect::MakeWH(100, 100)), solidPaint); |
| 291 | recordingCanvas->restore(); // scale |
| 292 | recordingCanvas->restore(); // saveLayer |
| 293 | picture.endRecording(); |
| 294 | |
| 295 | canvas.drawPicture(picture); |
| 296 | } |
| 297 | |
senorblanco@chromium.org | 28ae55d | 2014-03-24 21:32:28 +0000 | [diff] [blame] | 298 | static void test_huge_blur(SkBaseDevice* device, skiatest::Reporter* reporter) { |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 299 | SkCanvas canvas(device); |
| 300 | |
| 301 | SkBitmap bitmap; |
| 302 | bitmap.allocN32Pixels(100, 100); |
| 303 | bitmap.eraseARGB(0, 0, 0, 0); |
| 304 | |
| 305 | // Check that a blur with an insane radius does not crash or assert. |
| 306 | SkAutoTUnref<SkImageFilter> blur(SkBlurImageFilter::Create(SkIntToScalar(1<<30), SkIntToScalar(1<<30))); |
| 307 | |
| 308 | SkPaint paint; |
| 309 | paint.setImageFilter(blur); |
| 310 | canvas.drawSprite(bitmap, 0, 0, &paint); |
| 311 | } |
| 312 | |
| 313 | DEF_TEST(HugeBlurImageFilter, reporter) { |
| 314 | SkBitmap temp; |
| 315 | temp.allocN32Pixels(100, 100); |
| 316 | SkBitmapDevice device(temp); |
| 317 | test_huge_blur(&device, reporter); |
| 318 | } |
| 319 | |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame^] | 320 | static void test_xfermode_cropped_input(SkBaseDevice* device, skiatest::Reporter* reporter) { |
| 321 | SkCanvas canvas(device); |
| 322 | canvas.clear(0); |
| 323 | |
| 324 | SkBitmap bitmap; |
| 325 | bitmap.allocN32Pixels(1, 1); |
| 326 | bitmap.eraseARGB(255, 255, 255, 255); |
| 327 | |
| 328 | SkAutoTUnref<SkColorFilter> green( |
| 329 | SkColorFilter::CreateModeFilter(SK_ColorGREEN, SkXfermode::kSrcIn_Mode)); |
| 330 | SkAutoTUnref<SkColorFilterImageFilter> greenFilter( |
| 331 | SkColorFilterImageFilter::Create(green.get())); |
| 332 | SkImageFilter::CropRect cropRect(SkRect::MakeEmpty()); |
| 333 | SkAutoTUnref<SkColorFilterImageFilter> croppedOut( |
| 334 | SkColorFilterImageFilter::Create(green.get(), NULL, &cropRect)); |
| 335 | |
| 336 | // Check that an xfermode image filter whose input has been cropped out still draws the other |
| 337 | // input. Also check that drawing with both inputs cropped out doesn't cause a GPU warning. |
| 338 | SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrcOver_Mode); |
| 339 | SkAutoTUnref<SkImageFilter> xfermodeNoFg( |
| 340 | SkXfermodeImageFilter::Create(mode, greenFilter, croppedOut)); |
| 341 | SkAutoTUnref<SkImageFilter> xfermodeNoBg( |
| 342 | SkXfermodeImageFilter::Create(mode, croppedOut, greenFilter)); |
| 343 | SkAutoTUnref<SkImageFilter> xfermodeNoFgNoBg( |
| 344 | SkXfermodeImageFilter::Create(mode, croppedOut, croppedOut)); |
| 345 | |
| 346 | SkPaint paint; |
| 347 | paint.setImageFilter(xfermodeNoFg); |
| 348 | canvas.drawSprite(bitmap, 0, 0, &paint); |
| 349 | |
| 350 | uint32_t pixel; |
| 351 | SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 352 | canvas.readPixels(info, &pixel, 4, 0, 0); |
| 353 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 354 | |
| 355 | paint.setImageFilter(xfermodeNoBg); |
| 356 | canvas.drawSprite(bitmap, 0, 0, &paint); |
| 357 | canvas.readPixels(info, &pixel, 4, 0, 0); |
| 358 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 359 | |
| 360 | paint.setImageFilter(xfermodeNoFgNoBg); |
| 361 | canvas.drawSprite(bitmap, 0, 0, &paint); |
| 362 | canvas.readPixels(info, &pixel, 4, 0, 0); |
| 363 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 364 | } |
| 365 | |
| 366 | DEF_TEST(XfermodeImageFilterCroppedInput, reporter) { |
| 367 | SkBitmap temp; |
| 368 | temp.allocN32Pixels(100, 100); |
| 369 | SkBitmapDevice device(temp); |
| 370 | test_xfermode_cropped_input(&device, reporter); |
| 371 | } |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 372 | |
senorblanco@chromium.org | 58d1466 | 2014-02-03 22:36:39 +0000 | [diff] [blame] | 373 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 374 | DEF_GPUTEST(ImageFilterCropRectGPU, reporter, factory) { |
| 375 | GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(0)); |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 376 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(context, |
| 377 | SkImageInfo::MakeN32Premul(100, 100), |
| 378 | 0)); |
| 379 | test_crop_rects(device, reporter); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 380 | } |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 381 | |
| 382 | DEF_GPUTEST(HugeBlurImageFilterGPU, reporter, factory) { |
| 383 | GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(0)); |
| 384 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(context, |
| 385 | SkImageInfo::MakeN32Premul(100, 100), |
| 386 | 0)); |
| 387 | test_huge_blur(device, reporter); |
| 388 | } |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame^] | 389 | |
| 390 | DEF_GPUTEST(XfermodeImageFilterCroppedInputGPU, reporter, factory) { |
| 391 | GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(0)); |
| 392 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(context, |
| 393 | SkImageInfo::MakeN32Premul(1, 1), |
| 394 | 0)); |
| 395 | test_xfermode_cropped_input(device, reporter); |
| 396 | } |
senorblanco@chromium.org | 58d1466 | 2014-02-03 22:36:39 +0000 | [diff] [blame] | 397 | #endif |