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" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 9 | #include "SkBlurImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkColorFilterImageFilter.h" |
| 12 | #include "SkColorMatrixFilter.h" |
Florin Malita | 08252ec | 2017-07-06 12:48:15 -0400 | [diff] [blame] | 13 | #include "SkColorSpaceXformer.h" |
ajuma | 5788faa | 2015-02-13 09:05:47 -0800 | [diff] [blame] | 14 | #include "SkComposeImageFilter.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 15 | #include "SkDisplacementMapEffect.h" |
| 16 | #include "SkDropShadowImageFilter.h" |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 17 | #include "SkFlattenableSerialization.h" |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 18 | #include "SkGradientShader.h" |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 19 | #include "SkImage.h" |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 20 | #include "SkImageSource.h" |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 21 | #include "SkLightingImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 22 | #include "SkMatrixConvolutionImageFilter.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 23 | #include "SkMergeImageFilter.h" |
| 24 | #include "SkMorphologyImageFilter.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 25 | #include "SkOffsetImageFilter.h" |
ajuma | 77b6ba3 | 2016-01-08 14:58:35 -0800 | [diff] [blame] | 26 | #include "SkPaintImageFilter.h" |
senorblanco | 8f3937d | 2014-10-29 12:36:32 -0700 | [diff] [blame] | 27 | #include "SkPerlinNoiseShader.h" |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 28 | #include "SkPicture.h" |
senorblanco@chromium.org | 910702b | 2014-05-30 20:36:15 +0000 | [diff] [blame] | 29 | #include "SkPictureImageFilter.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 30 | #include "SkPictureRecorder.h" |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 31 | #include "SkPoint3.h" |
halcanary | 97d2c0a | 2014-08-19 06:27:53 -0700 | [diff] [blame] | 32 | #include "SkReadBuffer.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 33 | #include "SkRect.h" |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 34 | #include "SkSpecialImage.h" |
| 35 | #include "SkSpecialSurface.h" |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 36 | #include "SkSurface.h" |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 37 | #include "SkTableColorFilter.h" |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 38 | #include "SkTileImageFilter.h" |
| 39 | #include "SkXfermodeImageFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 40 | #include "Test.h" |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 41 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 42 | #if SK_SUPPORT_GPU |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 43 | #include "GrContext.h" |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 44 | #endif |
| 45 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 46 | static const int kBitmapSize = 4; |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 47 | |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 48 | namespace { |
| 49 | |
| 50 | class MatrixTestImageFilter : public SkImageFilter { |
| 51 | public: |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 52 | static sk_sp<SkImageFilter> Make(skiatest::Reporter* reporter, |
| 53 | const SkMatrix& expectedMatrix) { |
| 54 | return sk_sp<SkImageFilter>(new MatrixTestImageFilter(reporter, expectedMatrix)); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 55 | } |
| 56 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 57 | SK_TO_STRING_OVERRIDE() |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 58 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(MatrixTestImageFilter) |
| 59 | |
| 60 | protected: |
robertphillips | 4ba94e2 | 2016-04-04 12:07:47 -0700 | [diff] [blame] | 61 | sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context& ctx, |
| 62 | SkIPoint* offset) const override { |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 63 | REPORTER_ASSERT(fReporter, ctx.ctm() == fExpectedMatrix); |
robertphillips | 4ba94e2 | 2016-04-04 12:07:47 -0700 | [diff] [blame] | 64 | offset->fX = offset->fY = 0; |
| 65 | return sk_ref_sp<SkSpecialImage>(source); |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 66 | } |
Matt Sarett | 62745a8 | 2017-04-17 11:57:29 -0400 | [diff] [blame] | 67 | sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override { |
| 68 | return sk_ref_sp(const_cast<MatrixTestImageFilter*>(this)); |
| 69 | } |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 70 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 71 | void flatten(SkWriteBuffer& buffer) const override { |
brianosman | 18f13f3 | 2016-05-02 07:51:08 -0700 | [diff] [blame] | 72 | SkDEBUGFAIL("Should never get here"); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | private: |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 76 | MatrixTestImageFilter(skiatest::Reporter* reporter, const SkMatrix& expectedMatrix) |
| 77 | : INHERITED(nullptr, 0, nullptr) |
| 78 | , fReporter(reporter) |
| 79 | , fExpectedMatrix(expectedMatrix) { |
| 80 | } |
| 81 | |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 82 | skiatest::Reporter* fReporter; |
| 83 | SkMatrix fExpectedMatrix; |
mtklein | 3f3b3d0 | 2014-12-01 11:47:08 -0800 | [diff] [blame] | 84 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 85 | typedef SkImageFilter INHERITED; |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
senorblanco | 6a93fa1 | 2016-04-05 04:43:45 -0700 | [diff] [blame] | 88 | class FailImageFilter : public SkImageFilter { |
| 89 | public: |
robertphillips | 6b13473 | 2016-04-15 09:58:37 -0700 | [diff] [blame] | 90 | FailImageFilter() : SkImageFilter(nullptr, 0, nullptr) { } |
senorblanco | 6a93fa1 | 2016-04-05 04:43:45 -0700 | [diff] [blame] | 91 | |
| 92 | sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, |
| 93 | const Context& ctx, |
| 94 | SkIPoint* offset) const override { |
| 95 | return nullptr; |
| 96 | } |
Matt Sarett | 62745a8 | 2017-04-17 11:57:29 -0400 | [diff] [blame] | 97 | sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override { |
| 98 | return nullptr; |
| 99 | } |
senorblanco | 6a93fa1 | 2016-04-05 04:43:45 -0700 | [diff] [blame] | 100 | |
| 101 | SK_TO_STRING_OVERRIDE() |
| 102 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(FailImageFilter) |
| 103 | |
| 104 | private: |
| 105 | typedef SkImageFilter INHERITED; |
| 106 | }; |
| 107 | |
| 108 | sk_sp<SkFlattenable> FailImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 109 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0); |
| 110 | return sk_sp<SkFlattenable>(new FailImageFilter()); |
| 111 | } |
| 112 | |
| 113 | #ifndef SK_IGNORE_TO_STRING |
| 114 | void FailImageFilter::toString(SkString* str) const { |
| 115 | str->appendf("FailImageFilter: ("); |
| 116 | str->append(")"); |
| 117 | } |
| 118 | #endif |
| 119 | |
senorblanco | 297f7ce | 2016-03-23 13:44:26 -0700 | [diff] [blame] | 120 | void draw_gradient_circle(SkCanvas* canvas, int width, int height) { |
| 121 | SkScalar x = SkIntToScalar(width / 2); |
| 122 | SkScalar y = SkIntToScalar(height / 2); |
| 123 | SkScalar radius = SkMinScalar(x, y) * 0.8f; |
| 124 | canvas->clear(0x00000000); |
| 125 | SkColor colors[2]; |
| 126 | colors[0] = SK_ColorWHITE; |
| 127 | colors[1] = SK_ColorBLACK; |
| 128 | sk_sp<SkShader> shader( |
| 129 | SkGradientShader::MakeRadial(SkPoint::Make(x, y), radius, colors, nullptr, 2, |
| 130 | SkShader::kClamp_TileMode) |
| 131 | ); |
| 132 | SkPaint paint; |
| 133 | paint.setShader(shader); |
| 134 | canvas->drawCircle(x, y, radius, paint); |
| 135 | } |
| 136 | |
| 137 | SkBitmap make_gradient_circle(int width, int height) { |
| 138 | SkBitmap bitmap; |
| 139 | bitmap.allocN32Pixels(width, height); |
| 140 | SkCanvas canvas(bitmap); |
| 141 | draw_gradient_circle(&canvas, width, height); |
| 142 | return bitmap; |
| 143 | } |
| 144 | |
| 145 | class FilterList { |
| 146 | public: |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 147 | FilterList(sk_sp<SkImageFilter> input, const SkImageFilter::CropRect* cropRect = nullptr) { |
senorblanco | 297f7ce | 2016-03-23 13:44:26 -0700 | [diff] [blame] | 148 | SkPoint3 location = SkPoint3::Make(0, 0, SK_Scalar1); |
senorblanco | 297f7ce | 2016-03-23 13:44:26 -0700 | [diff] [blame] | 149 | const SkScalar five = SkIntToScalar(5); |
| 150 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 151 | { |
| 152 | sk_sp<SkColorFilter> cf(SkColorFilter::MakeModeFilter(SK_ColorRED, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 153 | SkBlendMode::kSrcIn)); |
senorblanco | 297f7ce | 2016-03-23 13:44:26 -0700 | [diff] [blame] | 154 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 155 | this->addFilter("color filter", |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 156 | SkColorFilterImageFilter::Make(std::move(cf), input, cropRect)); |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 157 | } |
senorblanco | 297f7ce | 2016-03-23 13:44:26 -0700 | [diff] [blame] | 158 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 159 | { |
| 160 | sk_sp<SkImage> gradientImage(SkImage::MakeFromBitmap(make_gradient_circle(64, 64))); |
| 161 | sk_sp<SkImageFilter> gradientSource(SkImageSource::Make(std::move(gradientImage))); |
senorblanco | 297f7ce | 2016-03-23 13:44:26 -0700 | [diff] [blame] | 162 | |
liyuqian | bfebe22 | 2016-11-14 11:17:16 -0800 | [diff] [blame] | 163 | this->addFilter("displacement map", |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 164 | SkDisplacementMapEffect::Make(SkDisplacementMapEffect::kR_ChannelSelectorType, |
| 165 | SkDisplacementMapEffect::kB_ChannelSelectorType, |
| 166 | 20.0f, |
| 167 | std::move(gradientSource), input, cropRect)); |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 168 | } |
senorblanco | 297f7ce | 2016-03-23 13:44:26 -0700 | [diff] [blame] | 169 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 170 | this->addFilter("blur", SkBlurImageFilter::Make(SK_Scalar1, |
| 171 | SK_Scalar1, |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 172 | input, |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 173 | cropRect)); |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 174 | this->addFilter("drop shadow", SkDropShadowImageFilter::Make( |
senorblanco | 297f7ce | 2016-03-23 13:44:26 -0700 | [diff] [blame] | 175 | SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_ColorGREEN, |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 176 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 177 | input, cropRect)); |
| 178 | this->addFilter("diffuse lighting", |
| 179 | SkLightingImageFilter::MakePointLitDiffuse(location, SK_ColorGREEN, 0, 0, |
| 180 | input, cropRect)); |
senorblanco | 297f7ce | 2016-03-23 13:44:26 -0700 | [diff] [blame] | 181 | this->addFilter("specular lighting", |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 182 | SkLightingImageFilter::MakePointLitSpecular(location, SK_ColorGREEN, 0, 0, 0, |
| 183 | input, cropRect)); |
| 184 | { |
| 185 | SkScalar kernel[9] = { |
| 186 | SkIntToScalar(1), SkIntToScalar(1), SkIntToScalar(1), |
| 187 | SkIntToScalar(1), SkIntToScalar(-7), SkIntToScalar(1), |
| 188 | SkIntToScalar(1), SkIntToScalar(1), SkIntToScalar(1), |
| 189 | }; |
| 190 | const SkISize kernelSize = SkISize::Make(3, 3); |
| 191 | const SkScalar gain = SK_Scalar1, bias = 0; |
| 192 | |
| 193 | this->addFilter("matrix convolution", |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 194 | SkMatrixConvolutionImageFilter::Make( |
senorblanco | 297f7ce | 2016-03-23 13:44:26 -0700 | [diff] [blame] | 195 | kernelSize, kernel, gain, bias, SkIPoint::Make(1, 1), |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 196 | SkMatrixConvolutionImageFilter::kRepeat_TileMode, false, |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 197 | input, cropRect)); |
| 198 | } |
| 199 | |
Mike Reed | 0bdaf05 | 2017-06-18 23:35:57 -0400 | [diff] [blame] | 200 | this->addFilter("merge", SkMergeImageFilter::Make(input, input, cropRect)); |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 201 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 202 | { |
| 203 | SkPaint greenColorShaderPaint; |
| 204 | greenColorShaderPaint.setShader(SkShader::MakeColorShader(SK_ColorGREEN)); |
| 205 | |
| 206 | SkImageFilter::CropRect leftSideCropRect(SkRect::MakeXYWH(0, 0, 32, 64)); |
| 207 | sk_sp<SkImageFilter> paintFilterLeft(SkPaintImageFilter::Make(greenColorShaderPaint, |
| 208 | &leftSideCropRect)); |
| 209 | SkImageFilter::CropRect rightSideCropRect(SkRect::MakeXYWH(32, 0, 32, 64)); |
| 210 | sk_sp<SkImageFilter> paintFilterRight(SkPaintImageFilter::Make(greenColorShaderPaint, |
| 211 | &rightSideCropRect)); |
| 212 | |
| 213 | |
| 214 | this->addFilter("merge with disjoint inputs", SkMergeImageFilter::Make( |
Mike Reed | 0bdaf05 | 2017-06-18 23:35:57 -0400 | [diff] [blame] | 215 | std::move(paintFilterLeft), std::move(paintFilterRight), cropRect)); |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 216 | } |
| 217 | |
senorblanco | 297f7ce | 2016-03-23 13:44:26 -0700 | [diff] [blame] | 218 | this->addFilter("offset", |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 219 | SkOffsetImageFilter::Make(SK_Scalar1, SK_Scalar1, input, |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 220 | cropRect)); |
| 221 | this->addFilter("dilate", SkDilateImageFilter::Make(3, 2, input, cropRect)); |
| 222 | this->addFilter("erode", SkErodeImageFilter::Make(2, 3, input, cropRect)); |
robertphillips | 534c270 | 2016-04-15 07:57:40 -0700 | [diff] [blame] | 223 | this->addFilter("tile", SkTileImageFilter::Make( |
| 224 | SkRect::MakeXYWH(0, 0, 50, 50), |
| 225 | cropRect ? cropRect->rect() : SkRect::MakeXYWH(0, 0, 100, 100), |
| 226 | input)); |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 227 | |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 228 | if (!cropRect) { |
| 229 | SkMatrix matrix; |
| 230 | |
| 231 | matrix.setTranslate(SK_Scalar1, SK_Scalar1); |
| 232 | matrix.postRotate(SkIntToScalar(45), SK_Scalar1, SK_Scalar1); |
| 233 | |
| 234 | this->addFilter("matrix", |
| 235 | SkImageFilter::MakeMatrixFilter(matrix, kLow_SkFilterQuality, input)); |
| 236 | } |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 237 | { |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 238 | sk_sp<SkImageFilter> blur(SkBlurImageFilter::Make(five, five, input)); |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 239 | |
| 240 | this->addFilter("blur and offset", SkOffsetImageFilter::Make(five, five, |
| 241 | std::move(blur), |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 242 | cropRect)); |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 243 | } |
| 244 | { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 245 | SkPictureRecorder recorder; |
Mike Klein | 26eb16f | 2017-04-10 09:50:25 -0400 | [diff] [blame] | 246 | SkCanvas* recordingCanvas = recorder.beginRecording(64, 64); |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 247 | |
| 248 | SkPaint greenPaint; |
| 249 | greenPaint.setColor(SK_ColorGREEN); |
| 250 | recordingCanvas->drawRect(SkRect::Make(SkIRect::MakeXYWH(10, 10, 30, 20)), greenPaint); |
| 251 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
| 252 | sk_sp<SkImageFilter> pictureFilter(SkPictureImageFilter::Make(std::move(picture))); |
| 253 | |
| 254 | this->addFilter("picture and blur", SkBlurImageFilter::Make(five, five, |
| 255 | std::move(pictureFilter), |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 256 | cropRect)); |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 257 | } |
| 258 | { |
| 259 | SkPaint paint; |
| 260 | paint.setShader(SkPerlinNoiseShader::MakeTurbulence(SK_Scalar1, SK_Scalar1, 1, 0)); |
| 261 | sk_sp<SkImageFilter> paintFilter(SkPaintImageFilter::Make(paint)); |
| 262 | |
| 263 | this->addFilter("paint and blur", SkBlurImageFilter::Make(five, five, |
| 264 | std::move(paintFilter), |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 265 | cropRect)); |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 266 | } |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 267 | this->addFilter("xfermode", SkXfermodeImageFilter::Make(SkBlendMode::kSrc, input, input, |
| 268 | cropRect)); |
senorblanco | 297f7ce | 2016-03-23 13:44:26 -0700 | [diff] [blame] | 269 | } |
| 270 | int count() const { return fFilters.count(); } |
| 271 | SkImageFilter* getFilter(int index) const { return fFilters[index].fFilter.get(); } |
| 272 | const char* getName(int index) const { return fFilters[index].fName; } |
| 273 | private: |
| 274 | struct Filter { |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 275 | Filter() : fName(nullptr) {} |
| 276 | Filter(const char* name, sk_sp<SkImageFilter> filter) |
| 277 | : fName(name) |
| 278 | , fFilter(std::move(filter)) { |
| 279 | } |
senorblanco | 297f7ce | 2016-03-23 13:44:26 -0700 | [diff] [blame] | 280 | const char* fName; |
| 281 | sk_sp<SkImageFilter> fFilter; |
| 282 | }; |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 283 | void addFilter(const char* name, sk_sp<SkImageFilter> filter) { |
| 284 | fFilters.push_back(Filter(name, std::move(filter))); |
senorblanco | 297f7ce | 2016-03-23 13:44:26 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | SkTArray<Filter> fFilters; |
| 288 | }; |
| 289 | |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 290 | } |
| 291 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 292 | sk_sp<SkFlattenable> MatrixTestImageFilter::CreateProc(SkReadBuffer& buffer) { |
brianosman | 18f13f3 | 2016-05-02 07:51:08 -0700 | [diff] [blame] | 293 | SkDEBUGFAIL("Should never get here"); |
| 294 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 295 | } |
| 296 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 297 | #ifndef SK_IGNORE_TO_STRING |
| 298 | void MatrixTestImageFilter::toString(SkString* str) const { |
| 299 | str->appendf("MatrixTestImageFilter: ("); |
| 300 | str->append(")"); |
| 301 | } |
| 302 | #endif |
| 303 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 304 | static sk_sp<SkImage> make_small_image() { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 305 | auto surface(SkSurface::MakeRasterN32Premul(kBitmapSize, kBitmapSize)); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 306 | SkCanvas* canvas = surface->getCanvas(); |
| 307 | canvas->clear(0x00000000); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 308 | SkPaint darkPaint; |
| 309 | darkPaint.setColor(0xFF804020); |
| 310 | SkPaint lightPaint; |
| 311 | lightPaint.setColor(0xFF244484); |
| 312 | const int i = kBitmapSize / 4; |
| 313 | for (int y = 0; y < kBitmapSize; y += i) { |
| 314 | for (int x = 0; x < kBitmapSize; x += i) { |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 315 | canvas->save(); |
| 316 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 317 | canvas->drawRect(SkRect::MakeXYWH(0, 0, |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 318 | SkIntToScalar(i), |
| 319 | SkIntToScalar(i)), darkPaint); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 320 | canvas->drawRect(SkRect::MakeXYWH(SkIntToScalar(i), |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 321 | 0, |
| 322 | SkIntToScalar(i), |
| 323 | SkIntToScalar(i)), lightPaint); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 324 | canvas->drawRect(SkRect::MakeXYWH(0, |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 325 | SkIntToScalar(i), |
| 326 | SkIntToScalar(i), |
| 327 | SkIntToScalar(i)), lightPaint); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 328 | canvas->drawRect(SkRect::MakeXYWH(SkIntToScalar(i), |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 329 | SkIntToScalar(i), |
| 330 | SkIntToScalar(i), |
| 331 | SkIntToScalar(i)), darkPaint); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 332 | canvas->restore(); |
commit-bot@chromium.org | 4b681bc | 2013-09-13 12:40:02 +0000 | [diff] [blame] | 333 | } |
| 334 | } |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 335 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 336 | return surface->makeImageSnapshot(); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 337 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 338 | |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 339 | static sk_sp<SkImageFilter> make_scale(float amount, sk_sp<SkImageFilter> input) { |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 340 | SkScalar s = amount; |
| 341 | SkScalar matrix[20] = { s, 0, 0, 0, 0, |
| 342 | 0, s, 0, 0, 0, |
| 343 | 0, 0, s, 0, 0, |
| 344 | 0, 0, 0, s, 0 }; |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 345 | sk_sp<SkColorFilter> filter(SkColorFilter::MakeMatrixFilterRowMajor255(matrix)); |
| 346 | return SkColorFilterImageFilter::Make(std::move(filter), std::move(input)); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 347 | } |
| 348 | |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 349 | static sk_sp<SkImageFilter> make_grayscale(sk_sp<SkImageFilter> input, |
| 350 | const SkImageFilter::CropRect* cropRect) { |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 351 | SkScalar matrix[20]; |
| 352 | memset(matrix, 0, 20 * sizeof(SkScalar)); |
| 353 | matrix[0] = matrix[5] = matrix[10] = 0.2126f; |
| 354 | matrix[1] = matrix[6] = matrix[11] = 0.7152f; |
| 355 | matrix[2] = matrix[7] = matrix[12] = 0.0722f; |
| 356 | matrix[18] = 1.0f; |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 357 | sk_sp<SkColorFilter> filter(SkColorFilter::MakeMatrixFilterRowMajor255(matrix)); |
| 358 | return SkColorFilterImageFilter::Make(std::move(filter), std::move(input), cropRect); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 359 | } |
| 360 | |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 361 | static sk_sp<SkImageFilter> make_blue(sk_sp<SkImageFilter> input, |
| 362 | const SkImageFilter::CropRect* cropRect) { |
| 363 | sk_sp<SkColorFilter> filter(SkColorFilter::MakeModeFilter(SK_ColorBLUE, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 364 | SkBlendMode::kSrcIn)); |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 365 | return SkColorFilterImageFilter::Make(std::move(filter), std::move(input), cropRect); |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 366 | } |
| 367 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 368 | static sk_sp<SkSpecialSurface> create_empty_special_surface(GrContext* context, int widthHeight) { |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 369 | #if SK_SUPPORT_GPU |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 370 | if (context) { |
robertphillips | 4df1656 | 2016-04-28 15:09:34 -0700 | [diff] [blame] | 371 | return SkSpecialSurface::MakeRenderTarget(context, |
| 372 | widthHeight, widthHeight, |
Brian Osman | 777b563 | 2016-10-14 09:16:21 -0400 | [diff] [blame] | 373 | kRGBA_8888_GrPixelConfig, nullptr); |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 374 | } else |
| 375 | #endif |
| 376 | { |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 377 | const SkImageInfo info = SkImageInfo::MakeN32(widthHeight, widthHeight, |
| 378 | kOpaque_SkAlphaType); |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 379 | return SkSpecialSurface::MakeRaster(info); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 380 | } |
senorblanco | bf680c3 | 2016-03-16 16:15:53 -0700 | [diff] [blame] | 381 | } |
| 382 | |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 383 | static sk_sp<SkSurface> create_surface(GrContext* context, int width, int height) { |
| 384 | const SkImageInfo info = SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType); |
| 385 | #if SK_SUPPORT_GPU |
| 386 | if (context) { |
| 387 | return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info); |
| 388 | } else |
| 389 | #endif |
| 390 | { |
| 391 | return SkSurface::MakeRaster(info); |
| 392 | } |
| 393 | } |
| 394 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 395 | static sk_sp<SkSpecialImage> create_empty_special_image(GrContext* context, int widthHeight) { |
| 396 | sk_sp<SkSpecialSurface> surf(create_empty_special_surface(context, widthHeight)); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 397 | |
| 398 | SkASSERT(surf); |
| 399 | |
| 400 | SkCanvas* canvas = surf->getCanvas(); |
| 401 | SkASSERT(canvas); |
| 402 | |
| 403 | canvas->clear(0x0); |
| 404 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 405 | return surf->makeImageSnapshot(); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 409 | DEF_TEST(ImageFilter, reporter) { |
| 410 | { |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 411 | // Check that two non-clipping color-matrice-filters concatenate into a single filter. |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 412 | sk_sp<SkImageFilter> halfBrightness(make_scale(0.5f, nullptr)); |
| 413 | sk_sp<SkImageFilter> quarterBrightness(make_scale(0.5f, std::move(halfBrightness))); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 414 | REPORTER_ASSERT(reporter, nullptr == quarterBrightness->getInput(0)); |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 415 | SkColorFilter* cf; |
| 416 | REPORTER_ASSERT(reporter, quarterBrightness->asColorFilter(&cf)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 417 | REPORTER_ASSERT(reporter, cf->asColorMatrix(nullptr)); |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 418 | cf->unref(); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 419 | } |
| 420 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 421 | { |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 422 | // Check that a clipping color-matrice-filter followed by a color-matrice-filters |
| 423 | // concatenates into a single filter, but not a matrixfilter (due to clamping). |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 424 | sk_sp<SkImageFilter> doubleBrightness(make_scale(2.0f, nullptr)); |
| 425 | sk_sp<SkImageFilter> halfBrightness(make_scale(0.5f, std::move(doubleBrightness))); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 426 | REPORTER_ASSERT(reporter, nullptr == halfBrightness->getInput(0)); |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 427 | SkColorFilter* cf; |
| 428 | REPORTER_ASSERT(reporter, halfBrightness->asColorFilter(&cf)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 429 | REPORTER_ASSERT(reporter, !cf->asColorMatrix(nullptr)); |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 430 | cf->unref(); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 431 | } |
| 432 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 433 | { |
| 434 | // Check that a color filter image filter without a crop rect can be |
| 435 | // expressed as a color filter. |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 436 | sk_sp<SkImageFilter> gray(make_grayscale(nullptr, nullptr)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 437 | REPORTER_ASSERT(reporter, true == gray->asColorFilter(nullptr)); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 438 | } |
mtklein | 2afbe23 | 2016-02-07 12:23:10 -0800 | [diff] [blame] | 439 | |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 440 | { |
| 441 | // Check that a colorfilterimage filter without a crop rect but with an input |
| 442 | // that is another colorfilterimage can be expressed as a colorfilter (composed). |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 443 | sk_sp<SkImageFilter> mode(make_blue(nullptr, nullptr)); |
| 444 | sk_sp<SkImageFilter> gray(make_grayscale(std::move(mode), nullptr)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 445 | REPORTER_ASSERT(reporter, true == gray->asColorFilter(nullptr)); |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | { |
| 449 | // Test that if we exceed the limit of what ComposeColorFilter can combine, we still |
| 450 | // can build the DAG and won't assert if we call asColorFilter. |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 451 | sk_sp<SkImageFilter> filter(make_blue(nullptr, nullptr)); |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 452 | const int kWayTooManyForComposeColorFilter = 100; |
| 453 | for (int i = 0; i < kWayTooManyForComposeColorFilter; ++i) { |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 454 | filter = make_blue(filter, nullptr); |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 455 | // the first few of these will succeed, but after we hit the internal limit, |
| 456 | // it will then return false. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 457 | (void)filter->asColorFilter(nullptr); |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 458 | } |
| 459 | } |
reed | 5c518a8 | 2015-03-05 14:47:29 -0800 | [diff] [blame] | 460 | |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 461 | { |
| 462 | // Check that a color filter image filter with a crop rect cannot |
| 463 | // be expressed as a color filter. |
| 464 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(0, 0, 100, 100)); |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 465 | sk_sp<SkImageFilter> grayWithCrop(make_grayscale(nullptr, &cropRect)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 466 | REPORTER_ASSERT(reporter, false == grayWithCrop->asColorFilter(nullptr)); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | { |
senorblanco | 3df0501 | 2014-07-03 11:13:09 -0700 | [diff] [blame] | 470 | // Check that two non-commutative matrices are concatenated in |
| 471 | // the correct order. |
| 472 | SkScalar blueToRedMatrix[20] = { 0 }; |
| 473 | blueToRedMatrix[2] = blueToRedMatrix[18] = SK_Scalar1; |
| 474 | SkScalar redToGreenMatrix[20] = { 0 }; |
| 475 | redToGreenMatrix[5] = redToGreenMatrix[18] = SK_Scalar1; |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 476 | sk_sp<SkColorFilter> blueToRed(SkColorFilter::MakeMatrixFilterRowMajor255(blueToRedMatrix)); |
| 477 | sk_sp<SkImageFilter> filter1(SkColorFilterImageFilter::Make(std::move(blueToRed), |
| 478 | nullptr)); |
| 479 | sk_sp<SkColorFilter> redToGreen(SkColorFilter::MakeMatrixFilterRowMajor255(redToGreenMatrix)); |
| 480 | sk_sp<SkImageFilter> filter2(SkColorFilterImageFilter::Make(std::move(redToGreen), |
| 481 | std::move(filter1))); |
senorblanco | 3df0501 | 2014-07-03 11:13:09 -0700 | [diff] [blame] | 482 | |
| 483 | SkBitmap result; |
| 484 | result.allocN32Pixels(kBitmapSize, kBitmapSize); |
| 485 | |
| 486 | SkPaint paint; |
| 487 | paint.setColor(SK_ColorBLUE); |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 488 | paint.setImageFilter(std::move(filter2)); |
senorblanco | 3df0501 | 2014-07-03 11:13:09 -0700 | [diff] [blame] | 489 | SkCanvas canvas(result); |
| 490 | canvas.clear(0x0); |
| 491 | SkRect rect = SkRect::Make(SkIRect::MakeWH(kBitmapSize, kBitmapSize)); |
| 492 | canvas.drawRect(rect, paint); |
| 493 | uint32_t pixel = *result.getAddr32(0, 0); |
| 494 | // The result here should be green, since we have effectively shifted blue to green. |
| 495 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 496 | } |
| 497 | |
| 498 | { |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 499 | // Tests pass by not asserting |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 500 | sk_sp<SkImage> image(make_small_image()); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 501 | SkBitmap result; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 502 | result.allocN32Pixels(kBitmapSize, kBitmapSize); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 503 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 504 | { |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 505 | // This tests for : |
| 506 | // 1 ) location at (0,0,1) |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 507 | SkPoint3 location = SkPoint3::Make(0, 0, SK_Scalar1); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 508 | // 2 ) location and target at same value |
robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame] | 509 | SkPoint3 target = SkPoint3::Make(location.fX, location.fY, location.fZ); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 510 | // 3 ) large negative specular exponent value |
| 511 | SkScalar specularExponent = -1000; |
| 512 | |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 513 | sk_sp<SkImageFilter> bmSrc(SkImageSource::Make(std::move(image))); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 514 | SkPaint paint; |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 515 | paint.setImageFilter(SkLightingImageFilter::MakeSpotLitSpecular( |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 516 | location, target, specularExponent, 180, |
| 517 | 0xFFFFFFFF, SK_Scalar1, SK_Scalar1, SK_Scalar1, |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 518 | std::move(bmSrc))); |
tfarina@chromium.org | 9f9d582 | 2013-12-18 22:15:12 +0000 | [diff] [blame] | 519 | SkCanvas canvas(result); |
| 520 | SkRect r = SkRect::MakeWH(SkIntToScalar(kBitmapSize), |
| 521 | SkIntToScalar(kBitmapSize)); |
| 522 | canvas.drawRect(r, paint); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 523 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 524 | } |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 525 | } |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 526 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 527 | static void test_crop_rects(skiatest::Reporter* reporter, |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 528 | GrContext* context) { |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 529 | // Check that all filters offset to their absolute crop rect, |
| 530 | // unaffected by the input crop rect. |
| 531 | // Tests pass by not asserting. |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 532 | sk_sp<SkSpecialImage> srcImg(create_empty_special_image(context, 100)); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 533 | SkASSERT(srcImg); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 534 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 535 | SkImageFilter::CropRect inputCropRect(SkRect::MakeXYWH(8, 13, 80, 80)); |
| 536 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 30, 60, 60)); |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 537 | sk_sp<SkImageFilter> input(make_grayscale(nullptr, &inputCropRect)); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 538 | |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 539 | FilterList filters(input, &cropRect); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 540 | |
senorblanco | 297f7ce | 2016-03-23 13:44:26 -0700 | [diff] [blame] | 541 | for (int i = 0; i < filters.count(); ++i) { |
| 542 | SkImageFilter* filter = filters.getFilter(i); |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 543 | SkIPoint offset; |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 544 | SkImageFilter::OutputProperties noColorSpace(nullptr); |
| 545 | SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeWH(100, 100), nullptr, noColorSpace); |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 546 | sk_sp<SkSpecialImage> resultImg(filter->filterImage(srcImg.get(), ctx, &offset)); |
senorblanco | 297f7ce | 2016-03-23 13:44:26 -0700 | [diff] [blame] | 547 | REPORTER_ASSERT_MESSAGE(reporter, resultImg, filters.getName(i)); |
| 548 | REPORTER_ASSERT_MESSAGE(reporter, offset.fX == 20 && offset.fY == 30, filters.getName(i)); |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 552 | static void test_negative_blur_sigma(skiatest::Reporter* reporter, |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 553 | GrContext* context) { |
senorblanco | 32673b9 | 2014-09-09 09:15:04 -0700 | [diff] [blame] | 554 | // Check that SkBlurImageFilter will accept a negative sigma, either in |
| 555 | // the given arguments or after CTM application. |
reed | 5ea95df | 2015-10-06 14:05:32 -0700 | [diff] [blame] | 556 | const int width = 32, height = 32; |
| 557 | const SkScalar five = SkIntToScalar(5); |
senorblanco | 32673b9 | 2014-09-09 09:15:04 -0700 | [diff] [blame] | 558 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 559 | sk_sp<SkImageFilter> positiveFilter(SkBlurImageFilter::Make(five, five, nullptr)); |
| 560 | sk_sp<SkImageFilter> negativeFilter(SkBlurImageFilter::Make(-five, five, nullptr)); |
senorblanco | 32673b9 | 2014-09-09 09:15:04 -0700 | [diff] [blame] | 561 | |
| 562 | SkBitmap gradient = make_gradient_circle(width, height); |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 563 | sk_sp<SkSpecialImage> imgSrc(SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(width, height), |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 564 | gradient)); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 565 | |
senorblanco | 32673b9 | 2014-09-09 09:15:04 -0700 | [diff] [blame] | 566 | SkIPoint offset; |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 567 | SkImageFilter::OutputProperties noColorSpace(nullptr); |
| 568 | SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeWH(32, 32), nullptr, noColorSpace); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 569 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 570 | sk_sp<SkSpecialImage> positiveResult1(positiveFilter->filterImage(imgSrc.get(), ctx, &offset)); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 571 | REPORTER_ASSERT(reporter, positiveResult1); |
| 572 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 573 | sk_sp<SkSpecialImage> negativeResult1(negativeFilter->filterImage(imgSrc.get(), ctx, &offset)); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 574 | REPORTER_ASSERT(reporter, negativeResult1); |
| 575 | |
senorblanco | 32673b9 | 2014-09-09 09:15:04 -0700 | [diff] [blame] | 576 | SkMatrix negativeScale; |
| 577 | negativeScale.setScale(-SK_Scalar1, SK_Scalar1); |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 578 | SkImageFilter::Context negativeCTX(negativeScale, SkIRect::MakeWH(32, 32), nullptr, |
| 579 | noColorSpace); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 580 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 581 | sk_sp<SkSpecialImage> negativeResult2(positiveFilter->filterImage(imgSrc.get(), |
| 582 | negativeCTX, |
| 583 | &offset)); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 584 | REPORTER_ASSERT(reporter, negativeResult2); |
| 585 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 586 | sk_sp<SkSpecialImage> positiveResult2(negativeFilter->filterImage(imgSrc.get(), |
| 587 | negativeCTX, |
| 588 | &offset)); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 589 | REPORTER_ASSERT(reporter, positiveResult2); |
| 590 | |
| 591 | |
| 592 | SkBitmap positiveResultBM1, positiveResultBM2; |
| 593 | SkBitmap negativeResultBM1, negativeResultBM2; |
| 594 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 595 | REPORTER_ASSERT(reporter, positiveResult1->getROPixels(&positiveResultBM1)); |
| 596 | REPORTER_ASSERT(reporter, positiveResult2->getROPixels(&positiveResultBM2)); |
| 597 | REPORTER_ASSERT(reporter, negativeResult1->getROPixels(&negativeResultBM1)); |
| 598 | REPORTER_ASSERT(reporter, negativeResult2->getROPixels(&negativeResultBM2)); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 599 | |
senorblanco | 32673b9 | 2014-09-09 09:15:04 -0700 | [diff] [blame] | 600 | for (int y = 0; y < height; y++) { |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 601 | int diffs = memcmp(positiveResultBM1.getAddr32(0, y), |
| 602 | negativeResultBM1.getAddr32(0, y), |
| 603 | positiveResultBM1.rowBytes()); |
senorblanco | 32673b9 | 2014-09-09 09:15:04 -0700 | [diff] [blame] | 604 | REPORTER_ASSERT(reporter, !diffs); |
| 605 | if (diffs) { |
| 606 | break; |
| 607 | } |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 608 | diffs = memcmp(positiveResultBM1.getAddr32(0, y), |
| 609 | negativeResultBM2.getAddr32(0, y), |
| 610 | positiveResultBM1.rowBytes()); |
senorblanco | 32673b9 | 2014-09-09 09:15:04 -0700 | [diff] [blame] | 611 | REPORTER_ASSERT(reporter, !diffs); |
| 612 | if (diffs) { |
| 613 | break; |
| 614 | } |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 615 | diffs = memcmp(positiveResultBM1.getAddr32(0, y), |
| 616 | positiveResultBM2.getAddr32(0, y), |
| 617 | positiveResultBM1.rowBytes()); |
senorblanco | 32673b9 | 2014-09-09 09:15:04 -0700 | [diff] [blame] | 618 | REPORTER_ASSERT(reporter, !diffs); |
| 619 | if (diffs) { |
| 620 | break; |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
senorblanco | 21a465d | 2016-04-11 11:58:39 -0700 | [diff] [blame] | 625 | DEF_TEST(ImageFilterNegativeBlurSigma, reporter) { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 626 | test_negative_blur_sigma(reporter, nullptr); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | #if SK_SUPPORT_GPU |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 630 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageFilterNegativeBlurSigma_Gpu, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 631 | test_negative_blur_sigma(reporter, ctxInfo.grContext()); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 632 | } |
| 633 | #endif |
| 634 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 635 | static void test_zero_blur_sigma(skiatest::Reporter* reporter, GrContext* context) { |
senorblanco | bf680c3 | 2016-03-16 16:15:53 -0700 | [diff] [blame] | 636 | // Check that SkBlurImageFilter with a zero sigma and a non-zero srcOffset works correctly. |
| 637 | SkImageFilter::CropRect cropRect(SkRect::Make(SkIRect::MakeXYWH(5, 0, 5, 10))); |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame] | 638 | sk_sp<SkImageFilter> input(SkOffsetImageFilter::Make(0, 0, nullptr, &cropRect)); |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 639 | sk_sp<SkImageFilter> filter(SkBlurImageFilter::Make(0, 0, std::move(input), &cropRect)); |
senorblanco | bf680c3 | 2016-03-16 16:15:53 -0700 | [diff] [blame] | 640 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 641 | sk_sp<SkSpecialSurface> surf(create_empty_special_surface(context, 10)); |
senorblanco | bf680c3 | 2016-03-16 16:15:53 -0700 | [diff] [blame] | 642 | surf->getCanvas()->clear(SK_ColorGREEN); |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 643 | sk_sp<SkSpecialImage> image(surf->makeImageSnapshot()); |
senorblanco | bf680c3 | 2016-03-16 16:15:53 -0700 | [diff] [blame] | 644 | |
| 645 | SkIPoint offset; |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 646 | SkImageFilter::OutputProperties noColorSpace(nullptr); |
| 647 | SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeWH(32, 32), nullptr, noColorSpace); |
senorblanco | bf680c3 | 2016-03-16 16:15:53 -0700 | [diff] [blame] | 648 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 649 | sk_sp<SkSpecialImage> result(filter->filterImage(image.get(), ctx, &offset)); |
senorblanco | bf680c3 | 2016-03-16 16:15:53 -0700 | [diff] [blame] | 650 | REPORTER_ASSERT(reporter, offset.fX == 5 && offset.fY == 0); |
| 651 | REPORTER_ASSERT(reporter, result); |
| 652 | REPORTER_ASSERT(reporter, result->width() == 5 && result->height() == 10); |
| 653 | |
| 654 | SkBitmap resultBM; |
| 655 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 656 | REPORTER_ASSERT(reporter, result->getROPixels(&resultBM)); |
senorblanco | bf680c3 | 2016-03-16 16:15:53 -0700 | [diff] [blame] | 657 | |
senorblanco | bf680c3 | 2016-03-16 16:15:53 -0700 | [diff] [blame] | 658 | for (int y = 0; y < resultBM.height(); y++) { |
| 659 | for (int x = 0; x < resultBM.width(); x++) { |
| 660 | bool diff = *resultBM.getAddr32(x, y) != SK_ColorGREEN; |
| 661 | REPORTER_ASSERT(reporter, !diff); |
| 662 | if (diff) { |
| 663 | break; |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | |
senorblanco | 21a465d | 2016-04-11 11:58:39 -0700 | [diff] [blame] | 669 | DEF_TEST(ImageFilterZeroBlurSigma, reporter) { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 670 | test_zero_blur_sigma(reporter, nullptr); |
senorblanco | bf680c3 | 2016-03-16 16:15:53 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | #if SK_SUPPORT_GPU |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 674 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageFilterZeroBlurSigma_Gpu, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 675 | test_zero_blur_sigma(reporter, ctxInfo.grContext()); |
senorblanco | bf680c3 | 2016-03-16 16:15:53 -0700 | [diff] [blame] | 676 | } |
| 677 | #endif |
| 678 | |
senorblanco | 6a93fa1 | 2016-04-05 04:43:45 -0700 | [diff] [blame] | 679 | |
| 680 | // Tests that, even when an upstream filter has returned null (due to failure or clipping), a |
| 681 | // downstream filter that affects transparent black still does so even with a nullptr input. |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 682 | static void test_fail_affects_transparent_black(skiatest::Reporter* reporter, GrContext* context) { |
senorblanco | 6a93fa1 | 2016-04-05 04:43:45 -0700 | [diff] [blame] | 683 | sk_sp<FailImageFilter> failFilter(new FailImageFilter()); |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 684 | sk_sp<SkSpecialImage> source(create_empty_special_image(context, 5)); |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 685 | SkImageFilter::OutputProperties noColorSpace(nullptr); |
| 686 | SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeXYWH(0, 0, 1, 1), nullptr, noColorSpace); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 687 | sk_sp<SkColorFilter> green(SkColorFilter::MakeModeFilter(SK_ColorGREEN, SkBlendMode::kSrc)); |
senorblanco | 6a93fa1 | 2016-04-05 04:43:45 -0700 | [diff] [blame] | 688 | SkASSERT(green->affectsTransparentBlack()); |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 689 | sk_sp<SkImageFilter> greenFilter(SkColorFilterImageFilter::Make(std::move(green), |
| 690 | std::move(failFilter))); |
senorblanco | 6a93fa1 | 2016-04-05 04:43:45 -0700 | [diff] [blame] | 691 | SkIPoint offset; |
| 692 | sk_sp<SkSpecialImage> result(greenFilter->filterImage(source.get(), ctx, &offset)); |
| 693 | REPORTER_ASSERT(reporter, nullptr != result.get()); |
| 694 | if (result.get()) { |
| 695 | SkBitmap resultBM; |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 696 | REPORTER_ASSERT(reporter, result->getROPixels(&resultBM)); |
senorblanco | 6a93fa1 | 2016-04-05 04:43:45 -0700 | [diff] [blame] | 697 | REPORTER_ASSERT(reporter, *resultBM.getAddr32(0, 0) == SK_ColorGREEN); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | DEF_TEST(ImageFilterFailAffectsTransparentBlack, reporter) { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 702 | test_fail_affects_transparent_black(reporter, nullptr); |
senorblanco | 6a93fa1 | 2016-04-05 04:43:45 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | #if SK_SUPPORT_GPU |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 706 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageFilterFailAffectsTransparentBlack_Gpu, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 707 | test_fail_affects_transparent_black(reporter, ctxInfo.grContext()); |
senorblanco | 6a93fa1 | 2016-04-05 04:43:45 -0700 | [diff] [blame] | 708 | } |
| 709 | #endif |
| 710 | |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 711 | DEF_TEST(ImageFilterDrawTiled, reporter) { |
| 712 | // Check that all filters when drawn tiled (with subsequent clip rects) exactly |
| 713 | // match the same filters drawn with a single full-canvas bitmap draw. |
| 714 | // Tests pass by not asserting. |
| 715 | |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 716 | FilterList filters(nullptr); |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 717 | |
| 718 | SkBitmap untiledResult, tiledResult; |
reed | 5ea95df | 2015-10-06 14:05:32 -0700 | [diff] [blame] | 719 | const int width = 64, height = 64; |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 720 | untiledResult.allocN32Pixels(width, height); |
| 721 | tiledResult.allocN32Pixels(width, height); |
| 722 | SkCanvas tiledCanvas(tiledResult); |
| 723 | SkCanvas untiledCanvas(untiledResult); |
senorblanco@chromium.org | d4db657 | 2014-05-07 20:00:04 +0000 | [diff] [blame] | 724 | int tileSize = 8; |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 725 | |
senorblanco@chromium.org | d4db657 | 2014-05-07 20:00:04 +0000 | [diff] [blame] | 726 | for (int scale = 1; scale <= 2; ++scale) { |
senorblanco | 297f7ce | 2016-03-23 13:44:26 -0700 | [diff] [blame] | 727 | for (int i = 0; i < filters.count(); ++i) { |
senorblanco@chromium.org | d4db657 | 2014-05-07 20:00:04 +0000 | [diff] [blame] | 728 | tiledCanvas.clear(0); |
| 729 | untiledCanvas.clear(0); |
| 730 | SkPaint paint; |
Mike Reed | 5e25717 | 2016-11-01 11:22:05 -0400 | [diff] [blame] | 731 | paint.setImageFilter(sk_ref_sp(filters.getFilter(i))); |
senorblanco@chromium.org | d4db657 | 2014-05-07 20:00:04 +0000 | [diff] [blame] | 732 | paint.setTextSize(SkIntToScalar(height)); |
| 733 | paint.setColor(SK_ColorWHITE); |
| 734 | SkString str; |
| 735 | const char* text = "ABC"; |
| 736 | SkScalar ypos = SkIntToScalar(height); |
| 737 | untiledCanvas.save(); |
| 738 | untiledCanvas.scale(SkIntToScalar(scale), SkIntToScalar(scale)); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 739 | untiledCanvas.drawString(text, 0, ypos, paint); |
senorblanco@chromium.org | d4db657 | 2014-05-07 20:00:04 +0000 | [diff] [blame] | 740 | untiledCanvas.restore(); |
| 741 | for (int y = 0; y < height; y += tileSize) { |
| 742 | for (int x = 0; x < width; x += tileSize) { |
| 743 | tiledCanvas.save(); |
| 744 | tiledCanvas.clipRect(SkRect::Make(SkIRect::MakeXYWH(x, y, tileSize, tileSize))); |
| 745 | tiledCanvas.scale(SkIntToScalar(scale), SkIntToScalar(scale)); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 746 | tiledCanvas.drawString(text, 0, ypos, paint); |
senorblanco@chromium.org | d4db657 | 2014-05-07 20:00:04 +0000 | [diff] [blame] | 747 | tiledCanvas.restore(); |
| 748 | } |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 749 | } |
senorblanco@chromium.org | d4db657 | 2014-05-07 20:00:04 +0000 | [diff] [blame] | 750 | untiledCanvas.flush(); |
| 751 | tiledCanvas.flush(); |
| 752 | for (int y = 0; y < height; y++) { |
| 753 | int diffs = memcmp(untiledResult.getAddr32(0, y), tiledResult.getAddr32(0, y), untiledResult.rowBytes()); |
senorblanco | 297f7ce | 2016-03-23 13:44:26 -0700 | [diff] [blame] | 754 | REPORTER_ASSERT_MESSAGE(reporter, !diffs, filters.getName(i)); |
senorblanco@chromium.org | d4db657 | 2014-05-07 20:00:04 +0000 | [diff] [blame] | 755 | if (diffs) { |
| 756 | break; |
| 757 | } |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | } |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 761 | } |
| 762 | |
mtklein | 3f3b3d0 | 2014-12-01 11:47:08 -0800 | [diff] [blame] | 763 | static void draw_saveLayer_picture(int width, int height, int tileSize, |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 764 | SkBBHFactory* factory, SkBitmap* result) { |
mtklein | d910f54 | 2014-08-22 09:06:34 -0700 | [diff] [blame] | 765 | |
| 766 | SkMatrix matrix; |
| 767 | matrix.setTranslate(SkIntToScalar(50), 0); |
| 768 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 769 | sk_sp<SkColorFilter> cf(SkColorFilter::MakeModeFilter(SK_ColorWHITE, SkBlendMode::kSrc)); |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 770 | sk_sp<SkImageFilter> cfif(SkColorFilterImageFilter::Make(std::move(cf), nullptr)); |
robertphillips | ae8c933 | 2016-04-05 15:09:00 -0700 | [diff] [blame] | 771 | sk_sp<SkImageFilter> imageFilter(SkImageFilter::MakeMatrixFilter(matrix, |
| 772 | kNone_SkFilterQuality, |
| 773 | std::move(cfif))); |
mtklein | d910f54 | 2014-08-22 09:06:34 -0700 | [diff] [blame] | 774 | |
| 775 | SkPaint paint; |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 776 | paint.setImageFilter(std::move(imageFilter)); |
mtklein | d910f54 | 2014-08-22 09:06:34 -0700 | [diff] [blame] | 777 | SkPictureRecorder recorder; |
| 778 | SkRect bounds = SkRect::Make(SkIRect::MakeXYWH(0, 0, 50, 50)); |
mtklein | 3f3b3d0 | 2014-12-01 11:47:08 -0800 | [diff] [blame] | 779 | SkCanvas* recordingCanvas = recorder.beginRecording(SkIntToScalar(width), |
| 780 | SkIntToScalar(height), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 781 | factory, 0); |
mtklein | d910f54 | 2014-08-22 09:06:34 -0700 | [diff] [blame] | 782 | recordingCanvas->translate(-55, 0); |
| 783 | recordingCanvas->saveLayer(&bounds, &paint); |
| 784 | recordingCanvas->restore(); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 785 | sk_sp<SkPicture> picture1(recorder.finishRecordingAsPicture()); |
mtklein | d910f54 | 2014-08-22 09:06:34 -0700 | [diff] [blame] | 786 | |
| 787 | result->allocN32Pixels(width, height); |
| 788 | SkCanvas canvas(*result); |
| 789 | canvas.clear(0); |
| 790 | canvas.clipRect(SkRect::Make(SkIRect::MakeWH(tileSize, tileSize))); |
| 791 | canvas.drawPicture(picture1.get()); |
| 792 | } |
| 793 | |
| 794 | DEF_TEST(ImageFilterDrawMatrixBBH, reporter) { |
| 795 | // Check that matrix filter when drawn tiled with BBH exactly |
| 796 | // matches the same thing drawn without BBH. |
| 797 | // Tests pass by not asserting. |
| 798 | |
| 799 | const int width = 200, height = 200; |
| 800 | const int tileSize = 100; |
| 801 | SkBitmap result1, result2; |
| 802 | SkRTreeFactory factory; |
| 803 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 804 | draw_saveLayer_picture(width, height, tileSize, &factory, &result1); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 805 | draw_saveLayer_picture(width, height, tileSize, nullptr, &result2); |
mtklein | d910f54 | 2014-08-22 09:06:34 -0700 | [diff] [blame] | 806 | |
| 807 | for (int y = 0; y < height; y++) { |
| 808 | int diffs = memcmp(result1.getAddr32(0, y), result2.getAddr32(0, y), result1.rowBytes()); |
| 809 | REPORTER_ASSERT(reporter, !diffs); |
| 810 | if (diffs) { |
| 811 | break; |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 816 | static sk_sp<SkImageFilter> make_blur(sk_sp<SkImageFilter> input) { |
| 817 | return SkBlurImageFilter::Make(SK_Scalar1, SK_Scalar1, std::move(input)); |
senorblanco | 1150a6d | 2014-08-25 12:46:58 -0700 | [diff] [blame] | 818 | } |
| 819 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 820 | static sk_sp<SkImageFilter> make_drop_shadow(sk_sp<SkImageFilter> input) { |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 821 | return SkDropShadowImageFilter::Make( |
senorblanco | 1150a6d | 2014-08-25 12:46:58 -0700 | [diff] [blame] | 822 | SkIntToScalar(100), SkIntToScalar(100), |
| 823 | SkIntToScalar(10), SkIntToScalar(10), |
sugoi | 234f036 | 2014-10-23 13:59:52 -0700 | [diff] [blame] | 824 | SK_ColorBLUE, SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 825 | std::move(input)); |
senorblanco | 1150a6d | 2014-08-25 12:46:58 -0700 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | DEF_TEST(ImageFilterBlurThenShadowBounds, reporter) { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 829 | sk_sp<SkImageFilter> filter1(make_blur(nullptr)); |
| 830 | sk_sp<SkImageFilter> filter2(make_drop_shadow(std::move(filter1))); |
senorblanco | 1150a6d | 2014-08-25 12:46:58 -0700 | [diff] [blame] | 831 | |
| 832 | SkIRect bounds = SkIRect::MakeXYWH(0, 0, 100, 100); |
| 833 | SkIRect expectedBounds = SkIRect::MakeXYWH(-133, -133, 236, 236); |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 834 | bounds = filter2->filterBounds(bounds, SkMatrix::I()); |
senorblanco | 1150a6d | 2014-08-25 12:46:58 -0700 | [diff] [blame] | 835 | |
| 836 | REPORTER_ASSERT(reporter, bounds == expectedBounds); |
| 837 | } |
| 838 | |
| 839 | DEF_TEST(ImageFilterShadowThenBlurBounds, reporter) { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 840 | sk_sp<SkImageFilter> filter1(make_drop_shadow(nullptr)); |
| 841 | sk_sp<SkImageFilter> filter2(make_blur(std::move(filter1))); |
senorblanco | 1150a6d | 2014-08-25 12:46:58 -0700 | [diff] [blame] | 842 | |
| 843 | SkIRect bounds = SkIRect::MakeXYWH(0, 0, 100, 100); |
| 844 | SkIRect expectedBounds = SkIRect::MakeXYWH(-133, -133, 236, 236); |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 845 | bounds = filter2->filterBounds(bounds, SkMatrix::I()); |
senorblanco | 1150a6d | 2014-08-25 12:46:58 -0700 | [diff] [blame] | 846 | |
| 847 | REPORTER_ASSERT(reporter, bounds == expectedBounds); |
| 848 | } |
| 849 | |
| 850 | DEF_TEST(ImageFilterDilateThenBlurBounds, reporter) { |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 851 | sk_sp<SkImageFilter> filter1(SkDilateImageFilter::Make(2, 2, nullptr)); |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 852 | sk_sp<SkImageFilter> filter2(make_drop_shadow(std::move(filter1))); |
senorblanco | 1150a6d | 2014-08-25 12:46:58 -0700 | [diff] [blame] | 853 | |
| 854 | SkIRect bounds = SkIRect::MakeXYWH(0, 0, 100, 100); |
| 855 | SkIRect expectedBounds = SkIRect::MakeXYWH(-132, -132, 234, 234); |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 856 | bounds = filter2->filterBounds(bounds, SkMatrix::I()); |
senorblanco | 1150a6d | 2014-08-25 12:46:58 -0700 | [diff] [blame] | 857 | |
| 858 | REPORTER_ASSERT(reporter, bounds == expectedBounds); |
| 859 | } |
| 860 | |
jbroman | 203a993 | 2016-07-11 14:07:59 -0700 | [diff] [blame] | 861 | DEF_TEST(ImageFilterScaledBlurRadius, reporter) { |
| 862 | // Each blur should spread 3*sigma, so 3 for the blur and 30 for the shadow |
| 863 | // (before the CTM). Bounds should be computed correctly in the presence of |
| 864 | // a (possibly negative) scale. |
| 865 | sk_sp<SkImageFilter> blur(make_blur(nullptr)); |
| 866 | sk_sp<SkImageFilter> dropShadow(make_drop_shadow(nullptr)); |
| 867 | { |
| 868 | // Uniform scale by 2. |
| 869 | SkMatrix scaleMatrix; |
| 870 | scaleMatrix.setScale(2, 2); |
| 871 | SkIRect bounds = SkIRect::MakeLTRB(0, 0, 200, 200); |
| 872 | |
| 873 | SkIRect expectedBlurBounds = SkIRect::MakeLTRB(-6, -6, 206, 206); |
| 874 | SkIRect blurBounds = blur->filterBounds( |
| 875 | bounds, scaleMatrix, SkImageFilter::kForward_MapDirection); |
| 876 | REPORTER_ASSERT(reporter, blurBounds == expectedBlurBounds); |
| 877 | SkIRect reverseBlurBounds = blur->filterBounds( |
| 878 | bounds, scaleMatrix, SkImageFilter::kReverse_MapDirection); |
| 879 | REPORTER_ASSERT(reporter, reverseBlurBounds == expectedBlurBounds); |
| 880 | |
| 881 | SkIRect expectedShadowBounds = SkIRect::MakeLTRB(0, 0, 460, 460); |
| 882 | SkIRect shadowBounds = dropShadow->filterBounds( |
| 883 | bounds, scaleMatrix, SkImageFilter::kForward_MapDirection); |
| 884 | REPORTER_ASSERT(reporter, shadowBounds == expectedShadowBounds); |
| 885 | SkIRect expectedReverseShadowBounds = |
| 886 | SkIRect::MakeLTRB(-260, -260, 200, 200); |
| 887 | SkIRect reverseShadowBounds = dropShadow->filterBounds( |
| 888 | bounds, scaleMatrix, SkImageFilter::kReverse_MapDirection); |
| 889 | REPORTER_ASSERT(reporter, |
| 890 | reverseShadowBounds == expectedReverseShadowBounds); |
| 891 | } |
| 892 | { |
| 893 | // Vertical flip. |
| 894 | SkMatrix scaleMatrix; |
| 895 | scaleMatrix.setScale(1, -1); |
| 896 | SkIRect bounds = SkIRect::MakeLTRB(0, -100, 100, 0); |
| 897 | |
| 898 | SkIRect expectedBlurBounds = SkIRect::MakeLTRB(-3, -103, 103, 3); |
| 899 | SkIRect blurBounds = blur->filterBounds( |
| 900 | bounds, scaleMatrix, SkImageFilter::kForward_MapDirection); |
| 901 | REPORTER_ASSERT(reporter, blurBounds == expectedBlurBounds); |
| 902 | SkIRect reverseBlurBounds = blur->filterBounds( |
| 903 | bounds, scaleMatrix, SkImageFilter::kReverse_MapDirection); |
| 904 | REPORTER_ASSERT(reporter, reverseBlurBounds == expectedBlurBounds); |
| 905 | |
| 906 | SkIRect expectedShadowBounds = SkIRect::MakeLTRB(0, -230, 230, 0); |
| 907 | SkIRect shadowBounds = dropShadow->filterBounds( |
| 908 | bounds, scaleMatrix, SkImageFilter::kForward_MapDirection); |
| 909 | REPORTER_ASSERT(reporter, shadowBounds == expectedShadowBounds); |
| 910 | SkIRect expectedReverseShadowBounds = |
| 911 | SkIRect::MakeLTRB(-130, -100, 100, 130); |
| 912 | SkIRect reverseShadowBounds = dropShadow->filterBounds( |
| 913 | bounds, scaleMatrix, SkImageFilter::kReverse_MapDirection); |
| 914 | REPORTER_ASSERT(reporter, |
| 915 | reverseShadowBounds == expectedReverseShadowBounds); |
| 916 | } |
| 917 | } |
| 918 | |
ajuma | 5788faa | 2015-02-13 09:05:47 -0800 | [diff] [blame] | 919 | DEF_TEST(ImageFilterComposedBlurFastBounds, reporter) { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 920 | sk_sp<SkImageFilter> filter1(make_blur(nullptr)); |
| 921 | sk_sp<SkImageFilter> filter2(make_blur(nullptr)); |
robertphillips | 491fb17 | 2016-03-30 12:32:58 -0700 | [diff] [blame] | 922 | sk_sp<SkImageFilter> composedFilter(SkComposeImageFilter::Make(std::move(filter1), |
| 923 | std::move(filter2))); |
ajuma | 5788faa | 2015-02-13 09:05:47 -0800 | [diff] [blame] | 924 | |
| 925 | SkRect boundsSrc = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100)); |
| 926 | SkRect expectedBounds = SkRect::MakeXYWH( |
| 927 | SkIntToScalar(-6), SkIntToScalar(-6), SkIntToScalar(112), SkIntToScalar(112)); |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 928 | SkRect boundsDst = composedFilter->computeFastBounds(boundsSrc); |
ajuma | 5788faa | 2015-02-13 09:05:47 -0800 | [diff] [blame] | 929 | |
| 930 | REPORTER_ASSERT(reporter, boundsDst == expectedBounds); |
| 931 | } |
| 932 | |
jbroman | 0e3129d | 2016-03-17 12:24:23 -0700 | [diff] [blame] | 933 | DEF_TEST(ImageFilterUnionBounds, reporter) { |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame] | 934 | sk_sp<SkImageFilter> offset(SkOffsetImageFilter::Make(50, 0, nullptr)); |
jbroman | 0e3129d | 2016-03-17 12:24:23 -0700 | [diff] [blame] | 935 | // Regardless of which order they appear in, the image filter bounds should |
| 936 | // be combined correctly. |
| 937 | { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 938 | sk_sp<SkImageFilter> composite(SkXfermodeImageFilter::Make(SkBlendMode::kSrcOver, offset)); |
jbroman | 0e3129d | 2016-03-17 12:24:23 -0700 | [diff] [blame] | 939 | SkRect bounds = SkRect::MakeWH(100, 100); |
| 940 | // Intentionally aliasing here, as that's what the real callers do. |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 941 | bounds = composite->computeFastBounds(bounds); |
jbroman | 0e3129d | 2016-03-17 12:24:23 -0700 | [diff] [blame] | 942 | REPORTER_ASSERT(reporter, bounds == SkRect::MakeWH(150, 100)); |
| 943 | } |
| 944 | { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 945 | sk_sp<SkImageFilter> composite(SkXfermodeImageFilter::Make(SkBlendMode::kSrcOver, nullptr, |
robertphillips | 8c0326d | 2016-04-05 12:48:34 -0700 | [diff] [blame] | 946 | offset, nullptr)); |
jbroman | 0e3129d | 2016-03-17 12:24:23 -0700 | [diff] [blame] | 947 | SkRect bounds = SkRect::MakeWH(100, 100); |
| 948 | // Intentionally aliasing here, as that's what the real callers do. |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 949 | bounds = composite->computeFastBounds(bounds); |
jbroman | 0e3129d | 2016-03-17 12:24:23 -0700 | [diff] [blame] | 950 | REPORTER_ASSERT(reporter, bounds == SkRect::MakeWH(150, 100)); |
| 951 | } |
| 952 | } |
| 953 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 954 | static void test_imagefilter_merge_result_size(skiatest::Reporter* reporter, GrContext* context) { |
senorblanco | 4a24398 | 2015-11-25 07:06:55 -0800 | [diff] [blame] | 955 | SkBitmap greenBM; |
| 956 | greenBM.allocN32Pixels(20, 20); |
| 957 | greenBM.eraseColor(SK_ColorGREEN); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 958 | sk_sp<SkImage> greenImage(SkImage::MakeFromBitmap(greenBM)); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 959 | sk_sp<SkImageFilter> source(SkImageSource::Make(std::move(greenImage))); |
Mike Reed | 0bdaf05 | 2017-06-18 23:35:57 -0400 | [diff] [blame] | 960 | sk_sp<SkImageFilter> merge(SkMergeImageFilter::Make(source, source)); |
senorblanco | 4a24398 | 2015-11-25 07:06:55 -0800 | [diff] [blame] | 961 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 962 | sk_sp<SkSpecialImage> srcImg(create_empty_special_image(context, 1)); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 963 | |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 964 | SkImageFilter::OutputProperties noColorSpace(nullptr); |
| 965 | SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeXYWH(0, 0, 100, 100), nullptr, |
| 966 | noColorSpace); |
senorblanco | 4a24398 | 2015-11-25 07:06:55 -0800 | [diff] [blame] | 967 | SkIPoint offset; |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 968 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 969 | sk_sp<SkSpecialImage> resultImg(merge->filterImage(srcImg.get(), ctx, &offset)); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 970 | REPORTER_ASSERT(reporter, resultImg); |
| 971 | |
| 972 | REPORTER_ASSERT(reporter, resultImg->width() == 20 && resultImg->height() == 20); |
senorblanco | 4a24398 | 2015-11-25 07:06:55 -0800 | [diff] [blame] | 973 | } |
| 974 | |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 975 | DEF_TEST(ImageFilterMergeResultSize, reporter) { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 976 | test_imagefilter_merge_result_size(reporter, nullptr); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | #if SK_SUPPORT_GPU |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 980 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageFilterMergeResultSize_Gpu, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 981 | test_imagefilter_merge_result_size(reporter, ctxInfo.grContext()); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 982 | } |
| 983 | #endif |
| 984 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 985 | static void draw_blurred_rect(SkCanvas* canvas) { |
senorblanco | 837f532 | 2014-07-14 10:19:54 -0700 | [diff] [blame] | 986 | SkPaint filterPaint; |
| 987 | filterPaint.setColor(SK_ColorWHITE); |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 988 | filterPaint.setImageFilter(SkBlurImageFilter::Make(SkIntToScalar(8), 0, nullptr)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 989 | canvas->saveLayer(nullptr, &filterPaint); |
senorblanco | 837f532 | 2014-07-14 10:19:54 -0700 | [diff] [blame] | 990 | SkPaint whitePaint; |
| 991 | whitePaint.setColor(SK_ColorWHITE); |
| 992 | canvas->drawRect(SkRect::Make(SkIRect::MakeWH(4, 4)), whitePaint); |
| 993 | canvas->restore(); |
| 994 | } |
| 995 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 996 | static void draw_picture_clipped(SkCanvas* canvas, const SkRect& clipRect, const SkPicture* picture) { |
senorblanco | 837f532 | 2014-07-14 10:19:54 -0700 | [diff] [blame] | 997 | canvas->save(); |
| 998 | canvas->clipRect(clipRect); |
| 999 | canvas->drawPicture(picture); |
| 1000 | canvas->restore(); |
| 1001 | } |
| 1002 | |
| 1003 | DEF_TEST(ImageFilterDrawTiledBlurRTree, reporter) { |
| 1004 | // Check that the blur filter when recorded with RTree acceleration, |
| 1005 | // and drawn tiled (with subsequent clip rects) exactly |
| 1006 | // matches the same filter drawn with without RTree acceleration. |
| 1007 | // This tests that the "bleed" from the blur into the otherwise-blank |
| 1008 | // tiles is correctly rendered. |
| 1009 | // Tests pass by not asserting. |
| 1010 | |
| 1011 | int width = 16, height = 8; |
| 1012 | SkBitmap result1, result2; |
| 1013 | result1.allocN32Pixels(width, height); |
| 1014 | result2.allocN32Pixels(width, height); |
| 1015 | SkCanvas canvas1(result1); |
| 1016 | SkCanvas canvas2(result2); |
| 1017 | int tileSize = 8; |
| 1018 | |
| 1019 | canvas1.clear(0); |
| 1020 | canvas2.clear(0); |
| 1021 | |
| 1022 | SkRTreeFactory factory; |
| 1023 | |
| 1024 | SkPictureRecorder recorder1, recorder2; |
| 1025 | // The only difference between these two pictures is that one has RTree aceleration. |
mtklein | 3f3b3d0 | 2014-12-01 11:47:08 -0800 | [diff] [blame] | 1026 | SkCanvas* recordingCanvas1 = recorder1.beginRecording(SkIntToScalar(width), |
| 1027 | SkIntToScalar(height), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1028 | nullptr, 0); |
mtklein | 3f3b3d0 | 2014-12-01 11:47:08 -0800 | [diff] [blame] | 1029 | SkCanvas* recordingCanvas2 = recorder2.beginRecording(SkIntToScalar(width), |
| 1030 | SkIntToScalar(height), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 1031 | &factory, 0); |
| 1032 | draw_blurred_rect(recordingCanvas1); |
| 1033 | draw_blurred_rect(recordingCanvas2); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1034 | sk_sp<SkPicture> picture1(recorder1.finishRecordingAsPicture()); |
| 1035 | sk_sp<SkPicture> picture2(recorder2.finishRecordingAsPicture()); |
senorblanco | 837f532 | 2014-07-14 10:19:54 -0700 | [diff] [blame] | 1036 | for (int y = 0; y < height; y += tileSize) { |
| 1037 | for (int x = 0; x < width; x += tileSize) { |
| 1038 | SkRect tileRect = SkRect::Make(SkIRect::MakeXYWH(x, y, tileSize, tileSize)); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1039 | draw_picture_clipped(&canvas1, tileRect, picture1.get()); |
| 1040 | draw_picture_clipped(&canvas2, tileRect, picture2.get()); |
senorblanco | 837f532 | 2014-07-14 10:19:54 -0700 | [diff] [blame] | 1041 | } |
| 1042 | } |
| 1043 | for (int y = 0; y < height; y++) { |
| 1044 | int diffs = memcmp(result1.getAddr32(0, y), result2.getAddr32(0, y), result1.rowBytes()); |
| 1045 | REPORTER_ASSERT(reporter, !diffs); |
| 1046 | if (diffs) { |
| 1047 | break; |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | |
senorblanco@chromium.org | 9195743 | 2014-05-01 14:03:41 +0000 | [diff] [blame] | 1052 | DEF_TEST(ImageFilterMatrixConvolution, reporter) { |
| 1053 | // Check that a 1x3 filter does not cause a spurious assert. |
| 1054 | SkScalar kernel[3] = { |
| 1055 | SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), |
| 1056 | }; |
| 1057 | SkISize kernelSize = SkISize::Make(1, 3); |
| 1058 | SkScalar gain = SK_Scalar1, bias = 0; |
| 1059 | SkIPoint kernelOffset = SkIPoint::Make(0, 0); |
| 1060 | |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 1061 | sk_sp<SkImageFilter> filter(SkMatrixConvolutionImageFilter::Make( |
| 1062 | kernelSize, kernel, |
| 1063 | gain, bias, kernelOffset, |
| 1064 | SkMatrixConvolutionImageFilter::kRepeat_TileMode, |
| 1065 | false, nullptr)); |
senorblanco@chromium.org | 9195743 | 2014-05-01 14:03:41 +0000 | [diff] [blame] | 1066 | |
| 1067 | SkBitmap result; |
| 1068 | int width = 16, height = 16; |
| 1069 | result.allocN32Pixels(width, height); |
| 1070 | SkCanvas canvas(result); |
| 1071 | canvas.clear(0); |
| 1072 | |
| 1073 | SkPaint paint; |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 1074 | paint.setImageFilter(std::move(filter)); |
senorblanco@chromium.org | 9195743 | 2014-05-01 14:03:41 +0000 | [diff] [blame] | 1075 | SkRect rect = SkRect::Make(SkIRect::MakeWH(width, height)); |
| 1076 | canvas.drawRect(rect, paint); |
| 1077 | } |
| 1078 | |
senorblanco@chromium.org | 8c7372b | 2014-05-02 19:13:11 +0000 | [diff] [blame] | 1079 | DEF_TEST(ImageFilterMatrixConvolutionBorder, reporter) { |
| 1080 | // Check that a filter with borders outside the target bounds |
| 1081 | // does not crash. |
| 1082 | SkScalar kernel[3] = { |
| 1083 | 0, 0, 0, |
| 1084 | }; |
| 1085 | SkISize kernelSize = SkISize::Make(3, 1); |
| 1086 | SkScalar gain = SK_Scalar1, bias = 0; |
| 1087 | SkIPoint kernelOffset = SkIPoint::Make(2, 0); |
| 1088 | |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 1089 | sk_sp<SkImageFilter> filter(SkMatrixConvolutionImageFilter::Make( |
| 1090 | kernelSize, kernel, gain, bias, kernelOffset, |
| 1091 | SkMatrixConvolutionImageFilter::kClamp_TileMode, |
| 1092 | true, nullptr)); |
senorblanco@chromium.org | 8c7372b | 2014-05-02 19:13:11 +0000 | [diff] [blame] | 1093 | |
| 1094 | SkBitmap result; |
| 1095 | |
| 1096 | int width = 10, height = 10; |
| 1097 | result.allocN32Pixels(width, height); |
| 1098 | SkCanvas canvas(result); |
| 1099 | canvas.clear(0); |
| 1100 | |
| 1101 | SkPaint filterPaint; |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 1102 | filterPaint.setImageFilter(std::move(filter)); |
senorblanco@chromium.org | 8c7372b | 2014-05-02 19:13:11 +0000 | [diff] [blame] | 1103 | SkRect bounds = SkRect::MakeWH(1, 10); |
| 1104 | SkRect rect = SkRect::Make(SkIRect::MakeWH(width, height)); |
| 1105 | SkPaint rectPaint; |
| 1106 | canvas.saveLayer(&bounds, &filterPaint); |
| 1107 | canvas.drawRect(rect, rectPaint); |
| 1108 | canvas.restore(); |
| 1109 | } |
| 1110 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 1111 | static void test_big_kernel(skiatest::Reporter* reporter, GrContext* context) { |
robertphillips | dada4dd | 2016-04-13 04:54:36 -0700 | [diff] [blame] | 1112 | // Check that a kernel that is too big for the GPU still works |
| 1113 | SkScalar identityKernel[49] = { |
| 1114 | 0, 0, 0, 0, 0, 0, 0, |
| 1115 | 0, 0, 0, 0, 0, 0, 0, |
| 1116 | 0, 0, 0, 0, 0, 0, 0, |
| 1117 | 0, 0, 0, 1, 0, 0, 0, |
| 1118 | 0, 0, 0, 0, 0, 0, 0, |
| 1119 | 0, 0, 0, 0, 0, 0, 0, |
| 1120 | 0, 0, 0, 0, 0, 0, 0 |
| 1121 | }; |
| 1122 | SkISize kernelSize = SkISize::Make(7, 7); |
| 1123 | SkScalar gain = SK_Scalar1, bias = 0; |
| 1124 | SkIPoint kernelOffset = SkIPoint::Make(0, 0); |
| 1125 | |
| 1126 | sk_sp<SkImageFilter> filter(SkMatrixConvolutionImageFilter::Make( |
| 1127 | kernelSize, identityKernel, gain, bias, kernelOffset, |
| 1128 | SkMatrixConvolutionImageFilter::kClamp_TileMode, |
| 1129 | true, nullptr)); |
| 1130 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 1131 | sk_sp<SkSpecialImage> srcImg(create_empty_special_image(context, 100)); |
robertphillips | dada4dd | 2016-04-13 04:54:36 -0700 | [diff] [blame] | 1132 | SkASSERT(srcImg); |
| 1133 | |
| 1134 | SkIPoint offset; |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 1135 | SkImageFilter::OutputProperties noColorSpace(nullptr); |
| 1136 | SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeWH(100, 100), nullptr, noColorSpace); |
robertphillips | dada4dd | 2016-04-13 04:54:36 -0700 | [diff] [blame] | 1137 | sk_sp<SkSpecialImage> resultImg(filter->filterImage(srcImg.get(), ctx, &offset)); |
| 1138 | REPORTER_ASSERT(reporter, resultImg); |
| 1139 | REPORTER_ASSERT(reporter, SkToBool(context) == resultImg->isTextureBacked()); |
| 1140 | REPORTER_ASSERT(reporter, resultImg->width() == 100 && resultImg->height() == 100); |
| 1141 | REPORTER_ASSERT(reporter, offset.fX == 0 && offset.fY == 0); |
| 1142 | } |
| 1143 | |
| 1144 | DEF_TEST(ImageFilterMatrixConvolutionBigKernel, reporter) { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 1145 | test_big_kernel(reporter, nullptr); |
robertphillips | dada4dd | 2016-04-13 04:54:36 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | #if SK_SUPPORT_GPU |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 1149 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageFilterMatrixConvolutionBigKernel_Gpu, |
| 1150 | reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 1151 | test_big_kernel(reporter, ctxInfo.grContext()); |
robertphillips | dada4dd | 2016-04-13 04:54:36 -0700 | [diff] [blame] | 1152 | } |
| 1153 | #endif |
| 1154 | |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 1155 | DEF_TEST(ImageFilterCropRect, reporter) { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 1156 | test_crop_rects(reporter, nullptr); |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 1159 | #if SK_SUPPORT_GPU |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 1160 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageFilterCropRect_Gpu, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 1161 | test_crop_rects(reporter, ctxInfo.grContext()); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 1162 | } |
| 1163 | #endif |
| 1164 | |
tfarina | 9ea53f9 | 2014-06-24 06:50:39 -0700 | [diff] [blame] | 1165 | DEF_TEST(ImageFilterMatrix, reporter) { |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 1166 | SkBitmap temp; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 1167 | temp.allocN32Pixels(100, 100); |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 1168 | SkCanvas canvas(temp); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 1169 | canvas.scale(SkIntToScalar(2), SkIntToScalar(2)); |
| 1170 | |
| 1171 | SkMatrix expectedMatrix = canvas.getTotalMatrix(); |
| 1172 | |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 1173 | SkRTreeFactory factory; |
| 1174 | SkPictureRecorder recorder; |
| 1175 | SkCanvas* recordingCanvas = recorder.beginRecording(100, 100, &factory, 0); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 1176 | |
| 1177 | SkPaint paint; |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 1178 | paint.setImageFilter(MatrixTestImageFilter::Make(reporter, expectedMatrix)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1179 | recordingCanvas->saveLayer(nullptr, &paint); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 1180 | SkPaint solidPaint; |
| 1181 | solidPaint.setColor(0xFFFFFFFF); |
| 1182 | recordingCanvas->save(); |
| 1183 | recordingCanvas->scale(SkIntToScalar(10), SkIntToScalar(10)); |
| 1184 | recordingCanvas->drawRect(SkRect::Make(SkIRect::MakeWH(100, 100)), solidPaint); |
| 1185 | recordingCanvas->restore(); // scale |
| 1186 | recordingCanvas->restore(); // saveLayer |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 1187 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1188 | canvas.drawPicture(recorder.finishRecordingAsPicture()); |
senorblanco@chromium.org | 5251e2b | 2014-02-05 22:36:31 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
senorblanco | 3d822c2 | 2014-07-30 14:49:31 -0700 | [diff] [blame] | 1191 | DEF_TEST(ImageFilterCrossProcessPictureImageFilter, reporter) { |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 1192 | SkRTreeFactory factory; |
| 1193 | SkPictureRecorder recorder; |
| 1194 | SkCanvas* recordingCanvas = recorder.beginRecording(1, 1, &factory, 0); |
| 1195 | |
| 1196 | // Create an SkPicture which simply draws a green 1x1 rectangle. |
| 1197 | SkPaint greenPaint; |
| 1198 | greenPaint.setColor(SK_ColorGREEN); |
| 1199 | recordingCanvas->drawRect(SkRect::Make(SkIRect::MakeWH(1, 1)), greenPaint); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1200 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 1201 | |
| 1202 | // Wrap that SkPicture in an SkPictureImageFilter. |
robertphillips | 5ff17b1 | 2016-03-28 13:13:42 -0700 | [diff] [blame] | 1203 | sk_sp<SkImageFilter> imageFilter(SkPictureImageFilter::Make(picture)); |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 1204 | |
| 1205 | // Check that SkPictureImageFilter successfully serializes its contained |
| 1206 | // SkPicture when not in cross-process mode. |
| 1207 | SkPaint paint; |
robertphillips | 5ff17b1 | 2016-03-28 13:13:42 -0700 | [diff] [blame] | 1208 | paint.setImageFilter(imageFilter); |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 1209 | SkPictureRecorder outerRecorder; |
| 1210 | SkCanvas* outerCanvas = outerRecorder.beginRecording(1, 1, &factory, 0); |
| 1211 | SkPaint redPaintWithFilter; |
| 1212 | redPaintWithFilter.setColor(SK_ColorRED); |
robertphillips | 5ff17b1 | 2016-03-28 13:13:42 -0700 | [diff] [blame] | 1213 | redPaintWithFilter.setImageFilter(imageFilter); |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 1214 | outerCanvas->drawRect(SkRect::Make(SkIRect::MakeWH(1, 1)), redPaintWithFilter); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1215 | sk_sp<SkPicture> outerPicture(outerRecorder.finishRecordingAsPicture()); |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 1216 | |
| 1217 | SkBitmap bitmap; |
| 1218 | bitmap.allocN32Pixels(1, 1); |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 1219 | SkCanvas canvas(bitmap); |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 1220 | |
| 1221 | // The result here should be green, since the filter replaces the primitive's red interior. |
| 1222 | canvas.clear(0x0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1223 | canvas.drawPicture(outerPicture); |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 1224 | uint32_t pixel = *bitmap.getAddr32(0, 0); |
| 1225 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 1226 | |
| 1227 | // Check that, for now, SkPictureImageFilter does not serialize or |
| 1228 | // deserialize its contained picture when the filter is serialized |
| 1229 | // cross-process. Do this by "laundering" it through SkValidatingReadBuffer. |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 1230 | sk_sp<SkData> data(SkValidatingSerializeFlattenable(imageFilter.get())); |
Mike Reed | 5e25717 | 2016-11-01 11:22:05 -0400 | [diff] [blame] | 1231 | sk_sp<SkImageFilter> unflattenedFilter = SkValidatingDeserializeImageFilter(data->data(), |
| 1232 | data->size()); |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 1233 | |
| 1234 | redPaintWithFilter.setImageFilter(unflattenedFilter); |
| 1235 | SkPictureRecorder crossProcessRecorder; |
| 1236 | SkCanvas* crossProcessCanvas = crossProcessRecorder.beginRecording(1, 1, &factory, 0); |
| 1237 | crossProcessCanvas->drawRect(SkRect::Make(SkIRect::MakeWH(1, 1)), redPaintWithFilter); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1238 | sk_sp<SkPicture> crossProcessPicture(crossProcessRecorder.finishRecordingAsPicture()); |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 1239 | |
| 1240 | canvas.clear(0x0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1241 | canvas.drawPicture(crossProcessPicture); |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 1242 | pixel = *bitmap.getAddr32(0, 0); |
hendrikw | 446ee67 | 2015-06-16 09:28:37 -0700 | [diff] [blame] | 1243 | // If the security precautions are enabled, the result here should not be green, since the |
| 1244 | // filter draws nothing. |
mtklein | 2afbe23 | 2016-02-07 12:23:10 -0800 | [diff] [blame] | 1245 | REPORTER_ASSERT(reporter, SkPicture::PictureIOSecurityPrecautionsEnabled() |
hendrikw | 446ee67 | 2015-06-16 09:28:37 -0700 | [diff] [blame] | 1246 | ? pixel != SK_ColorGREEN : pixel == SK_ColorGREEN); |
senorblanco@chromium.org | 97f5fc6 | 2014-05-30 20:50:56 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 1249 | static void test_clipped_picture_imagefilter(skiatest::Reporter* reporter, GrContext* context) { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1250 | sk_sp<SkPicture> picture; |
senorblanco | 3d822c2 | 2014-07-30 14:49:31 -0700 | [diff] [blame] | 1251 | |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 1252 | { |
| 1253 | SkRTreeFactory factory; |
| 1254 | SkPictureRecorder recorder; |
| 1255 | SkCanvas* recordingCanvas = recorder.beginRecording(1, 1, &factory, 0); |
| 1256 | |
| 1257 | // Create an SkPicture which simply draws a green 1x1 rectangle. |
| 1258 | SkPaint greenPaint; |
| 1259 | greenPaint.setColor(SK_ColorGREEN); |
| 1260 | recordingCanvas->drawRect(SkRect::Make(SkIRect::MakeWH(1, 1)), greenPaint); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1261 | picture = recorder.finishRecordingAsPicture(); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 1262 | } |
| 1263 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 1264 | sk_sp<SkSpecialImage> srcImg(create_empty_special_image(context, 2)); |
senorblanco | 3d822c2 | 2014-07-30 14:49:31 -0700 | [diff] [blame] | 1265 | |
robertphillips | 5ff17b1 | 2016-03-28 13:13:42 -0700 | [diff] [blame] | 1266 | sk_sp<SkImageFilter> imageFilter(SkPictureImageFilter::Make(picture)); |
senorblanco | 3d822c2 | 2014-07-30 14:49:31 -0700 | [diff] [blame] | 1267 | |
senorblanco | 3d822c2 | 2014-07-30 14:49:31 -0700 | [diff] [blame] | 1268 | SkIPoint offset; |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 1269 | SkImageFilter::OutputProperties noColorSpace(nullptr); |
| 1270 | SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeXYWH(1, 1, 1, 1), nullptr, noColorSpace); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 1271 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 1272 | sk_sp<SkSpecialImage> resultImage(imageFilter->filterImage(srcImg.get(), ctx, &offset)); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 1273 | REPORTER_ASSERT(reporter, !resultImage); |
senorblanco | 3d822c2 | 2014-07-30 14:49:31 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 1276 | DEF_TEST(ImageFilterClippedPictureImageFilter, reporter) { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 1277 | test_clipped_picture_imagefilter(reporter, nullptr); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | #if SK_SUPPORT_GPU |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 1281 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageFilterClippedPictureImageFilter_Gpu, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 1282 | test_clipped_picture_imagefilter(reporter, ctxInfo.grContext()); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 1283 | } |
| 1284 | #endif |
| 1285 | |
tfarina | 9ea53f9 | 2014-06-24 06:50:39 -0700 | [diff] [blame] | 1286 | DEF_TEST(ImageFilterEmptySaveLayer, reporter) { |
senorblanco@chromium.org | 68250c8 | 2014-05-06 22:52:55 +0000 | [diff] [blame] | 1287 | // Even when there's an empty saveLayer()/restore(), ensure that an image |
| 1288 | // filter or color filter which affects transparent black still draws. |
| 1289 | |
| 1290 | SkBitmap bitmap; |
| 1291 | bitmap.allocN32Pixels(10, 10); |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 1292 | SkCanvas canvas(bitmap); |
senorblanco@chromium.org | 68250c8 | 2014-05-06 22:52:55 +0000 | [diff] [blame] | 1293 | |
| 1294 | SkRTreeFactory factory; |
| 1295 | SkPictureRecorder recorder; |
| 1296 | |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 1297 | sk_sp<SkColorFilter> green(SkColorFilter::MakeModeFilter(SK_ColorGREEN, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 1298 | SkBlendMode::kSrc)); |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 1299 | sk_sp<SkImageFilter> imageFilter(SkColorFilterImageFilter::Make(green, nullptr)); |
senorblanco@chromium.org | 68250c8 | 2014-05-06 22:52:55 +0000 | [diff] [blame] | 1300 | SkPaint imageFilterPaint; |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 1301 | imageFilterPaint.setImageFilter(std::move(imageFilter)); |
senorblanco@chromium.org | 68250c8 | 2014-05-06 22:52:55 +0000 | [diff] [blame] | 1302 | SkPaint colorFilterPaint; |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 1303 | colorFilterPaint.setColorFilter(green); |
senorblanco@chromium.org | 68250c8 | 2014-05-06 22:52:55 +0000 | [diff] [blame] | 1304 | |
| 1305 | SkRect bounds = SkRect::MakeWH(10, 10); |
| 1306 | |
| 1307 | SkCanvas* recordingCanvas = recorder.beginRecording(10, 10, &factory, 0); |
| 1308 | recordingCanvas->saveLayer(&bounds, &imageFilterPaint); |
| 1309 | recordingCanvas->restore(); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1310 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
senorblanco@chromium.org | 68250c8 | 2014-05-06 22:52:55 +0000 | [diff] [blame] | 1311 | |
| 1312 | canvas.clear(0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1313 | canvas.drawPicture(picture); |
senorblanco@chromium.org | 68250c8 | 2014-05-06 22:52:55 +0000 | [diff] [blame] | 1314 | uint32_t pixel = *bitmap.getAddr32(0, 0); |
| 1315 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 1316 | |
| 1317 | recordingCanvas = recorder.beginRecording(10, 10, &factory, 0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1318 | recordingCanvas->saveLayer(nullptr, &imageFilterPaint); |
senorblanco@chromium.org | 68250c8 | 2014-05-06 22:52:55 +0000 | [diff] [blame] | 1319 | recordingCanvas->restore(); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1320 | sk_sp<SkPicture> picture2(recorder.finishRecordingAsPicture()); |
senorblanco@chromium.org | 68250c8 | 2014-05-06 22:52:55 +0000 | [diff] [blame] | 1321 | |
| 1322 | canvas.clear(0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1323 | canvas.drawPicture(picture2); |
senorblanco@chromium.org | 68250c8 | 2014-05-06 22:52:55 +0000 | [diff] [blame] | 1324 | pixel = *bitmap.getAddr32(0, 0); |
| 1325 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 1326 | |
| 1327 | recordingCanvas = recorder.beginRecording(10, 10, &factory, 0); |
| 1328 | recordingCanvas->saveLayer(&bounds, &colorFilterPaint); |
| 1329 | recordingCanvas->restore(); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 1330 | sk_sp<SkPicture> picture3(recorder.finishRecordingAsPicture()); |
senorblanco@chromium.org | 68250c8 | 2014-05-06 22:52:55 +0000 | [diff] [blame] | 1331 | |
| 1332 | canvas.clear(0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1333 | canvas.drawPicture(picture3); |
senorblanco@chromium.org | 68250c8 | 2014-05-06 22:52:55 +0000 | [diff] [blame] | 1334 | pixel = *bitmap.getAddr32(0, 0); |
| 1335 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 1336 | } |
| 1337 | |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 1338 | static void test_huge_blur(SkCanvas* canvas, skiatest::Reporter* reporter) { |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 1339 | SkBitmap bitmap; |
| 1340 | bitmap.allocN32Pixels(100, 100); |
| 1341 | bitmap.eraseARGB(0, 0, 0, 0); |
| 1342 | |
| 1343 | // Check that a blur with an insane radius does not crash or assert. |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 1344 | SkPaint paint; |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 1345 | paint.setImageFilter(SkBlurImageFilter::Make(SkIntToScalar(1<<30), |
| 1346 | SkIntToScalar(1<<30), |
| 1347 | nullptr)); |
reed | da420b9 | 2015-12-16 08:38:15 -0800 | [diff] [blame] | 1348 | canvas->drawBitmap(bitmap, 0, 0, &paint); |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | DEF_TEST(HugeBlurImageFilter, reporter) { |
| 1352 | SkBitmap temp; |
| 1353 | temp.allocN32Pixels(100, 100); |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 1354 | SkCanvas canvas(temp); |
| 1355 | test_huge_blur(&canvas, reporter); |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
senorblanco | 21a465d | 2016-04-11 11:58:39 -0700 | [diff] [blame] | 1358 | DEF_TEST(ImageFilterMatrixConvolutionSanityTest, reporter) { |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 1359 | SkScalar kernel[1] = { 0 }; |
| 1360 | SkScalar gain = SK_Scalar1, bias = 0; |
| 1361 | SkIPoint kernelOffset = SkIPoint::Make(1, 1); |
| 1362 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1363 | // Check that an enormous (non-allocatable) kernel gives a nullptr filter. |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 1364 | sk_sp<SkImageFilter> conv(SkMatrixConvolutionImageFilter::Make( |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 1365 | SkISize::Make(1<<30, 1<<30), |
| 1366 | kernel, |
| 1367 | gain, |
| 1368 | bias, |
| 1369 | kernelOffset, |
| 1370 | SkMatrixConvolutionImageFilter::kRepeat_TileMode, |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 1371 | false, |
| 1372 | nullptr)); |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 1373 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1374 | REPORTER_ASSERT(reporter, nullptr == conv.get()); |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 1375 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1376 | // Check that a nullptr kernel gives a nullptr filter. |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 1377 | conv = SkMatrixConvolutionImageFilter::Make( |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 1378 | SkISize::Make(1, 1), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1379 | nullptr, |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 1380 | gain, |
| 1381 | bias, |
| 1382 | kernelOffset, |
| 1383 | SkMatrixConvolutionImageFilter::kRepeat_TileMode, |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 1384 | false, |
| 1385 | nullptr); |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 1386 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1387 | REPORTER_ASSERT(reporter, nullptr == conv.get()); |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 1388 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1389 | // Check that a kernel width < 1 gives a nullptr filter. |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 1390 | conv = SkMatrixConvolutionImageFilter::Make( |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 1391 | SkISize::Make(0, 1), |
| 1392 | kernel, |
| 1393 | gain, |
| 1394 | bias, |
| 1395 | kernelOffset, |
| 1396 | SkMatrixConvolutionImageFilter::kRepeat_TileMode, |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 1397 | false, |
| 1398 | nullptr); |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 1399 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1400 | REPORTER_ASSERT(reporter, nullptr == conv.get()); |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 1401 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1402 | // Check that kernel height < 1 gives a nullptr filter. |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 1403 | conv = SkMatrixConvolutionImageFilter::Make( |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 1404 | SkISize::Make(1, -1), |
| 1405 | kernel, |
| 1406 | gain, |
| 1407 | bias, |
| 1408 | kernelOffset, |
| 1409 | SkMatrixConvolutionImageFilter::kRepeat_TileMode, |
robertphillips | ef6a47b | 2016-04-08 08:01:20 -0700 | [diff] [blame] | 1410 | false, |
| 1411 | nullptr); |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 1412 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1413 | REPORTER_ASSERT(reporter, nullptr == conv.get()); |
senorblanco | 3a49520 | 2014-09-29 07:57:20 -0700 | [diff] [blame] | 1414 | } |
| 1415 | |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 1416 | static void test_xfermode_cropped_input(SkSurface* surf, skiatest::Reporter* reporter) { |
| 1417 | auto canvas = surf->getCanvas(); |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 1418 | canvas->clear(0); |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 1419 | |
| 1420 | SkBitmap bitmap; |
| 1421 | bitmap.allocN32Pixels(1, 1); |
| 1422 | bitmap.eraseARGB(255, 255, 255, 255); |
| 1423 | |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 1424 | sk_sp<SkColorFilter> green(SkColorFilter::MakeModeFilter(SK_ColorGREEN, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 1425 | SkBlendMode::kSrcIn)); |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 1426 | sk_sp<SkImageFilter> greenFilter(SkColorFilterImageFilter::Make(green, nullptr)); |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 1427 | SkImageFilter::CropRect cropRect(SkRect::MakeEmpty()); |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 1428 | sk_sp<SkImageFilter> croppedOut(SkColorFilterImageFilter::Make(green, nullptr, &cropRect)); |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 1429 | |
| 1430 | // Check that an xfermode image filter whose input has been cropped out still draws the other |
| 1431 | // input. Also check that drawing with both inputs cropped out doesn't cause a GPU warning. |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1432 | SkBlendMode mode = SkBlendMode::kSrcOver; |
robertphillips | 8c0326d | 2016-04-05 12:48:34 -0700 | [diff] [blame] | 1433 | sk_sp<SkImageFilter> xfermodeNoFg(SkXfermodeImageFilter::Make(mode, greenFilter, |
| 1434 | croppedOut, nullptr)); |
| 1435 | sk_sp<SkImageFilter> xfermodeNoBg(SkXfermodeImageFilter::Make(mode, croppedOut, |
| 1436 | greenFilter, nullptr)); |
| 1437 | sk_sp<SkImageFilter> xfermodeNoFgNoBg(SkXfermodeImageFilter::Make(mode, croppedOut, |
| 1438 | croppedOut, nullptr)); |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 1439 | |
| 1440 | SkPaint paint; |
robertphillips | 8c0326d | 2016-04-05 12:48:34 -0700 | [diff] [blame] | 1441 | paint.setImageFilter(std::move(xfermodeNoFg)); |
reed | da420b9 | 2015-12-16 08:38:15 -0800 | [diff] [blame] | 1442 | canvas->drawBitmap(bitmap, 0, 0, &paint); // drawSprite |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 1443 | |
| 1444 | uint32_t pixel; |
kkinnunen | a9d9a39 | 2015-03-06 07:16:00 -0800 | [diff] [blame] | 1445 | SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType); |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 1446 | surf->readPixels(info, &pixel, 4, 0, 0); |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 1447 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 1448 | |
robertphillips | 8c0326d | 2016-04-05 12:48:34 -0700 | [diff] [blame] | 1449 | paint.setImageFilter(std::move(xfermodeNoBg)); |
reed | da420b9 | 2015-12-16 08:38:15 -0800 | [diff] [blame] | 1450 | canvas->drawBitmap(bitmap, 0, 0, &paint); // drawSprite |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 1451 | surf->readPixels(info, &pixel, 4, 0, 0); |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 1452 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 1453 | |
robertphillips | 8c0326d | 2016-04-05 12:48:34 -0700 | [diff] [blame] | 1454 | paint.setImageFilter(std::move(xfermodeNoFgNoBg)); |
reed | da420b9 | 2015-12-16 08:38:15 -0800 | [diff] [blame] | 1455 | canvas->drawBitmap(bitmap, 0, 0, &paint); // drawSprite |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 1456 | surf->readPixels(info, &pixel, 4, 0, 0); |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 1457 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 1458 | } |
| 1459 | |
senorblanco@chromium.org | d5424a4 | 2014-04-02 19:20:05 +0000 | [diff] [blame] | 1460 | DEF_TEST(ImageFilterNestedSaveLayer, reporter) { |
| 1461 | SkBitmap temp; |
| 1462 | temp.allocN32Pixels(50, 50); |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 1463 | SkCanvas canvas(temp); |
senorblanco@chromium.org | d5424a4 | 2014-04-02 19:20:05 +0000 | [diff] [blame] | 1464 | canvas.clear(0x0); |
| 1465 | |
| 1466 | SkBitmap bitmap; |
| 1467 | bitmap.allocN32Pixels(10, 10); |
| 1468 | bitmap.eraseColor(SK_ColorGREEN); |
| 1469 | |
| 1470 | SkMatrix matrix; |
| 1471 | matrix.setScale(SkIntToScalar(2), SkIntToScalar(2)); |
| 1472 | matrix.postTranslate(SkIntToScalar(-20), SkIntToScalar(-20)); |
robertphillips | ae8c933 | 2016-04-05 15:09:00 -0700 | [diff] [blame] | 1473 | sk_sp<SkImageFilter> matrixFilter( |
| 1474 | SkImageFilter::MakeMatrixFilter(matrix, kLow_SkFilterQuality, nullptr)); |
senorblanco@chromium.org | d5424a4 | 2014-04-02 19:20:05 +0000 | [diff] [blame] | 1475 | |
| 1476 | // Test that saveLayer() with a filter nested inside another saveLayer() applies the |
| 1477 | // correct offset to the filter matrix. |
| 1478 | SkRect bounds1 = SkRect::MakeXYWH(10, 10, 30, 30); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1479 | canvas.saveLayer(&bounds1, nullptr); |
senorblanco@chromium.org | d5424a4 | 2014-04-02 19:20:05 +0000 | [diff] [blame] | 1480 | SkPaint filterPaint; |
robertphillips | ae8c933 | 2016-04-05 15:09:00 -0700 | [diff] [blame] | 1481 | filterPaint.setImageFilter(std::move(matrixFilter)); |
senorblanco@chromium.org | d5424a4 | 2014-04-02 19:20:05 +0000 | [diff] [blame] | 1482 | SkRect bounds2 = SkRect::MakeXYWH(20, 20, 10, 10); |
| 1483 | canvas.saveLayer(&bounds2, &filterPaint); |
| 1484 | SkPaint greenPaint; |
| 1485 | greenPaint.setColor(SK_ColorGREEN); |
| 1486 | canvas.drawRect(bounds2, greenPaint); |
| 1487 | canvas.restore(); |
| 1488 | canvas.restore(); |
| 1489 | SkPaint strokePaint; |
| 1490 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 1491 | strokePaint.setColor(SK_ColorRED); |
| 1492 | |
kkinnunen | a9d9a39 | 2015-03-06 07:16:00 -0800 | [diff] [blame] | 1493 | SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType); |
senorblanco@chromium.org | d5424a4 | 2014-04-02 19:20:05 +0000 | [diff] [blame] | 1494 | uint32_t pixel; |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 1495 | temp.readPixels(info, &pixel, 4, 25, 25); |
senorblanco@chromium.org | d5424a4 | 2014-04-02 19:20:05 +0000 | [diff] [blame] | 1496 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 1497 | |
| 1498 | // Test that drawSprite() with a filter nested inside a saveLayer() applies the |
| 1499 | // correct offset to the filter matrix. |
| 1500 | canvas.clear(0x0); |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 1501 | temp.readPixels(info, &pixel, 4, 25, 25); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1502 | canvas.saveLayer(&bounds1, nullptr); |
reed | da420b9 | 2015-12-16 08:38:15 -0800 | [diff] [blame] | 1503 | canvas.drawBitmap(bitmap, 20, 20, &filterPaint); // drawSprite |
senorblanco@chromium.org | d5424a4 | 2014-04-02 19:20:05 +0000 | [diff] [blame] | 1504 | canvas.restore(); |
| 1505 | |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 1506 | temp.readPixels(info, &pixel, 4, 25, 25); |
senorblanco@chromium.org | d5424a4 | 2014-04-02 19:20:05 +0000 | [diff] [blame] | 1507 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 1508 | } |
| 1509 | |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 1510 | DEF_TEST(XfermodeImageFilterCroppedInput, reporter) { |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 1511 | test_xfermode_cropped_input(SkSurface::MakeRasterN32Premul(100, 100).get(), reporter); |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 1512 | } |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 1513 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 1514 | static void test_composed_imagefilter_offset(skiatest::Reporter* reporter, GrContext* context) { |
| 1515 | sk_sp<SkSpecialImage> srcImg(create_empty_special_image(context, 100)); |
ajuma | 5788faa | 2015-02-13 09:05:47 -0800 | [diff] [blame] | 1516 | |
| 1517 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(1, 0, 20, 20)); |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame] | 1518 | sk_sp<SkImageFilter> offsetFilter(SkOffsetImageFilter::Make(0, 0, nullptr, &cropRect)); |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 1519 | sk_sp<SkImageFilter> blurFilter(SkBlurImageFilter::Make(SK_Scalar1, SK_Scalar1, |
| 1520 | nullptr, &cropRect)); |
robertphillips | 491fb17 | 2016-03-30 12:32:58 -0700 | [diff] [blame] | 1521 | sk_sp<SkImageFilter> composedFilter(SkComposeImageFilter::Make(std::move(blurFilter), |
| 1522 | std::move(offsetFilter))); |
ajuma | 5788faa | 2015-02-13 09:05:47 -0800 | [diff] [blame] | 1523 | SkIPoint offset; |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 1524 | SkImageFilter::OutputProperties noColorSpace(nullptr); |
| 1525 | SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeWH(100, 100), nullptr, noColorSpace); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 1526 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 1527 | sk_sp<SkSpecialImage> resultImg(composedFilter->filterImage(srcImg.get(), ctx, &offset)); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 1528 | REPORTER_ASSERT(reporter, resultImg); |
ajuma | 5788faa | 2015-02-13 09:05:47 -0800 | [diff] [blame] | 1529 | REPORTER_ASSERT(reporter, offset.fX == 1 && offset.fY == 0); |
| 1530 | } |
| 1531 | |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 1532 | DEF_TEST(ComposedImageFilterOffset, reporter) { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 1533 | test_composed_imagefilter_offset(reporter, nullptr); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | #if SK_SUPPORT_GPU |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 1537 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ComposedImageFilterOffset_Gpu, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 1538 | test_composed_imagefilter_offset(reporter, ctxInfo.grContext()); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 1539 | } |
| 1540 | #endif |
| 1541 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 1542 | static void test_composed_imagefilter_bounds(skiatest::Reporter* reporter, GrContext* context) { |
jbroman | 17a6520 | 2016-03-21 08:38:58 -0700 | [diff] [blame] | 1543 | // The bounds passed to the inner filter must be filtered by the outer |
| 1544 | // filter, so that the inner filter produces the pixels that the outer |
| 1545 | // filter requires as input. This matters if the outer filter moves pixels. |
| 1546 | // Here, accounting for the outer offset is necessary so that the green |
| 1547 | // pixels of the picture are not clipped. |
| 1548 | |
| 1549 | SkPictureRecorder recorder; |
| 1550 | SkCanvas* recordingCanvas = recorder.beginRecording(SkRect::MakeWH(200, 100)); |
| 1551 | recordingCanvas->clipRect(SkRect::MakeXYWH(100, 0, 100, 100)); |
| 1552 | recordingCanvas->clear(SK_ColorGREEN); |
robertphillips | 491fb17 | 2016-03-30 12:32:58 -0700 | [diff] [blame] | 1553 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
robertphillips | 5ff17b1 | 2016-03-28 13:13:42 -0700 | [diff] [blame] | 1554 | sk_sp<SkImageFilter> pictureFilter(SkPictureImageFilter::Make(picture)); |
jbroman | 17a6520 | 2016-03-21 08:38:58 -0700 | [diff] [blame] | 1555 | SkImageFilter::CropRect cropRect(SkRect::MakeWH(100, 100)); |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame] | 1556 | sk_sp<SkImageFilter> offsetFilter(SkOffsetImageFilter::Make(-100, 0, nullptr, &cropRect)); |
robertphillips | 491fb17 | 2016-03-30 12:32:58 -0700 | [diff] [blame] | 1557 | sk_sp<SkImageFilter> composedFilter(SkComposeImageFilter::Make(std::move(offsetFilter), |
| 1558 | std::move(pictureFilter))); |
jbroman | 17a6520 | 2016-03-21 08:38:58 -0700 | [diff] [blame] | 1559 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 1560 | sk_sp<SkSpecialImage> sourceImage(create_empty_special_image(context, 100)); |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 1561 | SkImageFilter::OutputProperties noColorSpace(nullptr); |
| 1562 | SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeWH(100, 100), nullptr, noColorSpace); |
jbroman | 17a6520 | 2016-03-21 08:38:58 -0700 | [diff] [blame] | 1563 | SkIPoint offset; |
| 1564 | sk_sp<SkSpecialImage> result(composedFilter->filterImage(sourceImage.get(), ctx, &offset)); |
| 1565 | REPORTER_ASSERT(reporter, offset.isZero()); |
| 1566 | REPORTER_ASSERT(reporter, result); |
| 1567 | REPORTER_ASSERT(reporter, result->subset().size() == SkISize::Make(100, 100)); |
| 1568 | |
| 1569 | SkBitmap resultBM; |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 1570 | REPORTER_ASSERT(reporter, result->getROPixels(&resultBM)); |
jbroman | 17a6520 | 2016-03-21 08:38:58 -0700 | [diff] [blame] | 1571 | REPORTER_ASSERT(reporter, resultBM.getColor(50, 50) == SK_ColorGREEN); |
| 1572 | } |
| 1573 | |
| 1574 | DEF_TEST(ComposedImageFilterBounds, reporter) { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 1575 | test_composed_imagefilter_bounds(reporter, nullptr); |
jbroman | 17a6520 | 2016-03-21 08:38:58 -0700 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | #if SK_SUPPORT_GPU |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 1579 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ComposedImageFilterBounds_Gpu, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 1580 | test_composed_imagefilter_bounds(reporter, ctxInfo.grContext()); |
jbroman | 17a6520 | 2016-03-21 08:38:58 -0700 | [diff] [blame] | 1581 | } |
| 1582 | #endif |
| 1583 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 1584 | static void test_partial_crop_rect(skiatest::Reporter* reporter, GrContext* context) { |
| 1585 | sk_sp<SkSpecialImage> srcImg(create_empty_special_image(context, 100)); |
senorblanco | 24d2a7b | 2015-07-13 10:27:05 -0700 | [diff] [blame] | 1586 | |
| 1587 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(100, 0, 20, 30), |
senorblanco | ed7cf27 | 2015-07-16 15:19:11 -0700 | [diff] [blame] | 1588 | SkImageFilter::CropRect::kHasWidth_CropEdge | SkImageFilter::CropRect::kHasHeight_CropEdge); |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 1589 | sk_sp<SkImageFilter> filter(make_grayscale(nullptr, &cropRect)); |
senorblanco | 24d2a7b | 2015-07-13 10:27:05 -0700 | [diff] [blame] | 1590 | SkIPoint offset; |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 1591 | SkImageFilter::OutputProperties noColorSpace(nullptr); |
| 1592 | SkImageFilter::Context ctx(SkMatrix::I(), SkIRect::MakeWH(100, 100), nullptr, noColorSpace); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 1593 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 1594 | sk_sp<SkSpecialImage> resultImg(filter->filterImage(srcImg.get(), ctx, &offset)); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 1595 | REPORTER_ASSERT(reporter, resultImg); |
| 1596 | |
senorblanco | 24d2a7b | 2015-07-13 10:27:05 -0700 | [diff] [blame] | 1597 | REPORTER_ASSERT(reporter, offset.fX == 0); |
| 1598 | REPORTER_ASSERT(reporter, offset.fY == 0); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 1599 | REPORTER_ASSERT(reporter, resultImg->width() == 20); |
| 1600 | REPORTER_ASSERT(reporter, resultImg->height() == 30); |
senorblanco | 24d2a7b | 2015-07-13 10:27:05 -0700 | [diff] [blame] | 1601 | } |
| 1602 | |
senorblanco | 21a465d | 2016-04-11 11:58:39 -0700 | [diff] [blame] | 1603 | DEF_TEST(ImageFilterPartialCropRect, reporter) { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 1604 | test_partial_crop_rect(reporter, nullptr); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | #if SK_SUPPORT_GPU |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 1608 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageFilterPartialCropRect_Gpu, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 1609 | test_partial_crop_rect(reporter, ctxInfo.grContext()); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 1610 | } |
| 1611 | #endif |
| 1612 | |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 1613 | DEF_TEST(ImageFilterCanComputeFastBounds, reporter) { |
| 1614 | |
robertphillips | 12fa47d | 2016-04-08 16:28:09 -0700 | [diff] [blame] | 1615 | { |
| 1616 | SkPoint3 location = SkPoint3::Make(0, 0, SK_Scalar1); |
| 1617 | sk_sp<SkImageFilter> lighting(SkLightingImageFilter::MakePointLitDiffuse(location, |
| 1618 | SK_ColorGREEN, |
| 1619 | 0, 0, nullptr)); |
| 1620 | REPORTER_ASSERT(reporter, !lighting->canComputeFastBounds()); |
| 1621 | } |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 1622 | |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 1623 | { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 1624 | sk_sp<SkImageFilter> gray(make_grayscale(nullptr, nullptr)); |
| 1625 | REPORTER_ASSERT(reporter, gray->canComputeFastBounds()); |
| 1626 | { |
| 1627 | SkColorFilter* grayCF; |
| 1628 | REPORTER_ASSERT(reporter, gray->asAColorFilter(&grayCF)); |
| 1629 | REPORTER_ASSERT(reporter, !grayCF->affectsTransparentBlack()); |
| 1630 | grayCF->unref(); |
| 1631 | } |
| 1632 | REPORTER_ASSERT(reporter, gray->canComputeFastBounds()); |
| 1633 | |
| 1634 | sk_sp<SkImageFilter> grayBlur(SkBlurImageFilter::Make(SK_Scalar1, SK_Scalar1, |
| 1635 | std::move(gray))); |
| 1636 | REPORTER_ASSERT(reporter, grayBlur->canComputeFastBounds()); |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 1637 | } |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 1638 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 1639 | { |
| 1640 | SkScalar greenMatrix[20] = { 0, 0, 0, 0, 0, |
| 1641 | 0, 0, 0, 0, 1, |
| 1642 | 0, 0, 0, 0, 0, |
| 1643 | 0, 0, 0, 0, 1 }; |
| 1644 | sk_sp<SkColorFilter> greenCF(SkColorFilter::MakeMatrixFilterRowMajor255(greenMatrix)); |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 1645 | sk_sp<SkImageFilter> green(SkColorFilterImageFilter::Make(greenCF, nullptr)); |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 1646 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 1647 | REPORTER_ASSERT(reporter, greenCF->affectsTransparentBlack()); |
| 1648 | REPORTER_ASSERT(reporter, !green->canComputeFastBounds()); |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 1649 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 1650 | sk_sp<SkImageFilter> greenBlur(SkBlurImageFilter::Make(SK_Scalar1, SK_Scalar1, |
| 1651 | std::move(green))); |
| 1652 | REPORTER_ASSERT(reporter, !greenBlur->canComputeFastBounds()); |
| 1653 | } |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 1654 | |
| 1655 | uint8_t allOne[256], identity[256]; |
| 1656 | for (int i = 0; i < 256; ++i) { |
| 1657 | identity[i] = i; |
| 1658 | allOne[i] = 255; |
| 1659 | } |
| 1660 | |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 1661 | sk_sp<SkColorFilter> identityCF(SkTableColorFilter::MakeARGB(identity, identity, |
| 1662 | identity, allOne)); |
| 1663 | sk_sp<SkImageFilter> identityFilter(SkColorFilterImageFilter::Make(identityCF, nullptr)); |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 1664 | REPORTER_ASSERT(reporter, !identityCF->affectsTransparentBlack()); |
| 1665 | REPORTER_ASSERT(reporter, identityFilter->canComputeFastBounds()); |
| 1666 | |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 1667 | sk_sp<SkColorFilter> forceOpaqueCF(SkTableColorFilter::MakeARGB(allOne, identity, |
| 1668 | identity, identity)); |
| 1669 | sk_sp<SkImageFilter> forceOpaque(SkColorFilterImageFilter::Make(forceOpaqueCF, nullptr)); |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 1670 | REPORTER_ASSERT(reporter, forceOpaqueCF->affectsTransparentBlack()); |
| 1671 | REPORTER_ASSERT(reporter, !forceOpaque->canComputeFastBounds()); |
| 1672 | } |
| 1673 | |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 1674 | // Verify that SkImageSource survives serialization |
| 1675 | DEF_TEST(ImageFilterImageSourceSerialization, reporter) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1676 | auto surface(SkSurface::MakeRasterN32Premul(10, 10)); |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 1677 | surface->getCanvas()->clear(SK_ColorGREEN); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 1678 | sk_sp<SkImage> image(surface->makeImageSnapshot()); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 1679 | sk_sp<SkImageFilter> filter(SkImageSource::Make(std::move(image))); |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 1680 | |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 1681 | sk_sp<SkData> data(SkValidatingSerializeFlattenable(filter.get())); |
Mike Reed | 5e25717 | 2016-11-01 11:22:05 -0400 | [diff] [blame] | 1682 | sk_sp<SkImageFilter> unflattenedFilter = SkValidatingDeserializeImageFilter(data->data(), |
| 1683 | data->size()); |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 1684 | REPORTER_ASSERT(reporter, unflattenedFilter); |
| 1685 | |
| 1686 | SkBitmap bm; |
| 1687 | bm.allocN32Pixels(10, 10); |
fmalita | 23cb88c | 2015-09-15 06:56:23 -0700 | [diff] [blame] | 1688 | bm.eraseColor(SK_ColorBLUE); |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 1689 | SkPaint paint; |
| 1690 | paint.setColor(SK_ColorRED); |
| 1691 | paint.setImageFilter(unflattenedFilter); |
| 1692 | |
| 1693 | SkCanvas canvas(bm); |
| 1694 | canvas.drawRect(SkRect::MakeWH(10, 10), paint); |
| 1695 | REPORTER_ASSERT(reporter, *bm.getAddr32(0, 0) == SkPreMultiplyColor(SK_ColorGREEN)); |
| 1696 | } |
| 1697 | |
bsalomon | 45eefcf | 2016-01-05 08:39:28 -0800 | [diff] [blame] | 1698 | static void test_large_blur_input(skiatest::Reporter* reporter, SkCanvas* canvas) { |
| 1699 | SkBitmap largeBmp; |
| 1700 | int largeW = 5000; |
| 1701 | int largeH = 5000; |
| 1702 | #if SK_SUPPORT_GPU |
| 1703 | // If we're GPU-backed make the bitmap too large to be converted into a texture. |
| 1704 | if (GrContext* ctx = canvas->getGrContext()) { |
| 1705 | largeW = ctx->caps()->maxTextureSize() + 1; |
| 1706 | } |
| 1707 | #endif |
| 1708 | |
| 1709 | largeBmp.allocN32Pixels(largeW, largeH); |
mtklein | 2afbe23 | 2016-02-07 12:23:10 -0800 | [diff] [blame] | 1710 | largeBmp.eraseColor(0); |
bsalomon | 45eefcf | 2016-01-05 08:39:28 -0800 | [diff] [blame] | 1711 | if (!largeBmp.getPixels()) { |
| 1712 | ERRORF(reporter, "Failed to allocate large bmp."); |
| 1713 | return; |
| 1714 | } |
| 1715 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 1716 | sk_sp<SkImage> largeImage(SkImage::MakeFromBitmap(largeBmp)); |
bsalomon | 45eefcf | 2016-01-05 08:39:28 -0800 | [diff] [blame] | 1717 | if (!largeImage) { |
| 1718 | ERRORF(reporter, "Failed to create large image."); |
| 1719 | return; |
| 1720 | } |
| 1721 | |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 1722 | sk_sp<SkImageFilter> largeSource(SkImageSource::Make(std::move(largeImage))); |
bsalomon | 45eefcf | 2016-01-05 08:39:28 -0800 | [diff] [blame] | 1723 | if (!largeSource) { |
| 1724 | ERRORF(reporter, "Failed to create large SkImageSource."); |
| 1725 | return; |
| 1726 | } |
| 1727 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 1728 | sk_sp<SkImageFilter> blur(SkBlurImageFilter::Make(10.f, 10.f, std::move(largeSource))); |
bsalomon | 45eefcf | 2016-01-05 08:39:28 -0800 | [diff] [blame] | 1729 | if (!blur) { |
| 1730 | ERRORF(reporter, "Failed to create SkBlurImageFilter."); |
| 1731 | return; |
| 1732 | } |
| 1733 | |
| 1734 | SkPaint paint; |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 1735 | paint.setImageFilter(std::move(blur)); |
bsalomon | 45eefcf | 2016-01-05 08:39:28 -0800 | [diff] [blame] | 1736 | |
| 1737 | // This should not crash (http://crbug.com/570479). |
| 1738 | canvas->drawRect(SkRect::MakeIWH(largeW, largeH), paint); |
| 1739 | } |
| 1740 | |
senorblanco | 21a465d | 2016-04-11 11:58:39 -0700 | [diff] [blame] | 1741 | DEF_TEST(ImageFilterBlurLargeImage, reporter) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1742 | auto surface(SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(100, 100))); |
bsalomon | 45eefcf | 2016-01-05 08:39:28 -0800 | [diff] [blame] | 1743 | test_large_blur_input(reporter, surface->getCanvas()); |
| 1744 | } |
| 1745 | |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 1746 | static void test_make_with_filter(skiatest::Reporter* reporter, GrContext* context) { |
Robert Phillips | a5fdc97 | 2017-02-18 16:58:09 -0500 | [diff] [blame] | 1747 | sk_sp<SkSurface> surface(create_surface(context, 192, 128)); |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 1748 | surface->getCanvas()->clear(SK_ColorRED); |
| 1749 | SkPaint bluePaint; |
| 1750 | bluePaint.setColor(SK_ColorBLUE); |
| 1751 | SkIRect subset = SkIRect::MakeXYWH(25, 20, 50, 50); |
| 1752 | surface->getCanvas()->drawRect(SkRect::Make(subset), bluePaint); |
| 1753 | sk_sp<SkImage> sourceImage = surface->makeImageSnapshot(); |
| 1754 | |
| 1755 | sk_sp<SkImageFilter> filter = make_grayscale(nullptr, nullptr); |
| 1756 | SkIRect clipBounds = SkIRect::MakeXYWH(30, 35, 100, 100); |
| 1757 | SkIRect outSubset; |
| 1758 | SkIPoint offset; |
| 1759 | sk_sp<SkImage> result; |
| 1760 | |
| 1761 | result = sourceImage->makeWithFilter(nullptr, subset, clipBounds, &outSubset, &offset); |
| 1762 | REPORTER_ASSERT(reporter, !result); |
| 1763 | |
| 1764 | result = sourceImage->makeWithFilter(filter.get(), subset, clipBounds, nullptr, &offset); |
| 1765 | REPORTER_ASSERT(reporter, !result); |
| 1766 | |
| 1767 | result = sourceImage->makeWithFilter(filter.get(), subset, clipBounds, &outSubset, nullptr); |
| 1768 | REPORTER_ASSERT(reporter, !result); |
| 1769 | |
| 1770 | SkIRect bigSubset = SkIRect::MakeXYWH(-10000, -10000, 20000, 20000); |
| 1771 | result = sourceImage->makeWithFilter(filter.get(), bigSubset, clipBounds, &outSubset, &offset); |
| 1772 | REPORTER_ASSERT(reporter, !result); |
| 1773 | |
| 1774 | SkIRect empty = SkIRect::MakeEmpty(); |
| 1775 | result = sourceImage->makeWithFilter(filter.get(), empty, clipBounds, &outSubset, &offset); |
| 1776 | REPORTER_ASSERT(reporter, !result); |
| 1777 | |
| 1778 | result = sourceImage->makeWithFilter(filter.get(), subset, empty, &outSubset, &offset); |
| 1779 | REPORTER_ASSERT(reporter, !result); |
| 1780 | |
| 1781 | SkIRect leftField = SkIRect::MakeXYWH(-1000, 0, 100, 100); |
| 1782 | result = sourceImage->makeWithFilter(filter.get(), subset, leftField, &outSubset, &offset); |
| 1783 | REPORTER_ASSERT(reporter, !result); |
| 1784 | |
| 1785 | result = sourceImage->makeWithFilter(filter.get(), subset, clipBounds, &outSubset, &offset); |
| 1786 | |
| 1787 | REPORTER_ASSERT(reporter, result); |
| 1788 | REPORTER_ASSERT(reporter, result->bounds().contains(outSubset)); |
| 1789 | SkIRect destRect = SkIRect::MakeXYWH(offset.x(), offset.y(), |
| 1790 | outSubset.width(), outSubset.height()); |
| 1791 | REPORTER_ASSERT(reporter, clipBounds.contains(destRect)); |
Robert Phillips | a5fdc97 | 2017-02-18 16:58:09 -0500 | [diff] [blame] | 1792 | |
| 1793 | // In GPU-mode, this case creates a special image with a backing size that differs from |
| 1794 | // the content size |
| 1795 | { |
| 1796 | clipBounds.setXYWH(0, 0, 170, 100); |
| 1797 | subset.setXYWH(0, 0, 160, 90); |
| 1798 | |
| 1799 | filter = SkXfermodeImageFilter::Make(SkBlendMode::kSrc, nullptr); |
| 1800 | result = sourceImage->makeWithFilter(filter.get(), subset, clipBounds, &outSubset, &offset); |
| 1801 | REPORTER_ASSERT(reporter, result); |
| 1802 | } |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 1803 | } |
| 1804 | |
| 1805 | DEF_TEST(ImageFilterMakeWithFilter, reporter) { |
| 1806 | test_make_with_filter(reporter, nullptr); |
| 1807 | } |
| 1808 | |
| 1809 | #if SK_SUPPORT_GPU |
| 1810 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageFilterMakeWithFilter_Gpu, reporter, ctxInfo) { |
| 1811 | test_make_with_filter(reporter, ctxInfo.grContext()); |
| 1812 | } |
| 1813 | #endif |
| 1814 | |
senorblanco@chromium.org | 58d1466 | 2014-02-03 22:36:39 +0000 | [diff] [blame] | 1815 | #if SK_SUPPORT_GPU |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 1816 | |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 1817 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageFilterHugeBlur_Gpu, reporter, ctxInfo) { |
robertphillips | efbffed | 2015-06-22 12:06:08 -0700 | [diff] [blame] | 1818 | |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 1819 | sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(ctxInfo.grContext(), |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 1820 | SkBudgeted::kNo, |
| 1821 | SkImageInfo::MakeN32Premul(100, 100))); |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 1822 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 1823 | |
| 1824 | SkCanvas* canvas = surf->getCanvas(); |
| 1825 | |
| 1826 | test_huge_blur(canvas, reporter); |
senorblanco@chromium.org | 09843fd | 2014-03-24 20:50:59 +0000 | [diff] [blame] | 1827 | } |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 1828 | |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 1829 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(XfermodeImageFilterCroppedInput_Gpu, reporter, ctxInfo) { |
Brian Salomon | 8996e18 | 2017-07-05 17:01:48 -0400 | [diff] [blame] | 1830 | sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget( |
| 1831 | ctxInfo.grContext(), |
| 1832 | SkBudgeted::kNo, |
| 1833 | SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType))); |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 1834 | |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 1835 | test_xfermode_cropped_input(surf.get(), reporter); |
senorblanco@chromium.org | ee845ae | 2014-04-01 19:15:23 +0000 | [diff] [blame] | 1836 | } |
senorblanco | 32673b9 | 2014-09-09 09:15:04 -0700 | [diff] [blame] | 1837 | |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 1838 | DEF_GPUTEST_FOR_ALL_CONTEXTS(ImageFilterBlurLargeImage_Gpu, reporter, ctxInfo) { |
Brian Salomon | 8996e18 | 2017-07-05 17:01:48 -0400 | [diff] [blame] | 1839 | auto surface(SkSurface::MakeRenderTarget( |
| 1840 | ctxInfo.grContext(), SkBudgeted::kYes, |
| 1841 | SkImageInfo::Make(100, 100, kRGBA_8888_SkColorType, kPremul_SkAlphaType))); |
bsalomon | 45eefcf | 2016-01-05 08:39:28 -0800 | [diff] [blame] | 1842 | test_large_blur_input(reporter, surface->getCanvas()); |
| 1843 | } |
senorblanco@chromium.org | 58d1466 | 2014-02-03 22:36:39 +0000 | [diff] [blame] | 1844 | #endif |
reed | bb34a8a | 2016-04-23 15:19:07 -0700 | [diff] [blame] | 1845 | |
| 1846 | /* |
| 1847 | * Test that colorfilterimagefilter does not require its CTM to be decomposed when it has more |
| 1848 | * than just scale/translate, but that other filters do. |
| 1849 | */ |
reed | 96a04f3 | 2016-04-25 09:25:15 -0700 | [diff] [blame] | 1850 | DEF_TEST(ImageFilterComplexCTM, reporter) { |
reed | bb34a8a | 2016-04-23 15:19:07 -0700 | [diff] [blame] | 1851 | // just need a colorfilter to exercise the corresponding imagefilter |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 1852 | sk_sp<SkColorFilter> cf = SkColorFilter::MakeModeFilter(SK_ColorRED, SkBlendMode::kSrcATop); |
reed | 96a04f3 | 2016-04-25 09:25:15 -0700 | [diff] [blame] | 1853 | sk_sp<SkImageFilter> cfif = SkColorFilterImageFilter::Make(cf, nullptr); // can handle |
| 1854 | sk_sp<SkImageFilter> blif = SkBlurImageFilter::Make(3, 3, nullptr); // cannot handle |
reed | bb34a8a | 2016-04-23 15:19:07 -0700 | [diff] [blame] | 1855 | |
| 1856 | struct { |
| 1857 | sk_sp<SkImageFilter> fFilter; |
| 1858 | bool fExpectCanHandle; |
| 1859 | } recs[] = { |
| 1860 | { cfif, true }, |
| 1861 | { SkColorFilterImageFilter::Make(cf, cfif), true }, |
Mike Reed | 0bdaf05 | 2017-06-18 23:35:57 -0400 | [diff] [blame] | 1862 | { SkMergeImageFilter::Make(cfif, cfif), true }, |
reed | 96a04f3 | 2016-04-25 09:25:15 -0700 | [diff] [blame] | 1863 | { SkComposeImageFilter::Make(cfif, cfif), true }, |
| 1864 | |
reed | bb34a8a | 2016-04-23 15:19:07 -0700 | [diff] [blame] | 1865 | { blif, false }, |
reed | 96a04f3 | 2016-04-25 09:25:15 -0700 | [diff] [blame] | 1866 | { SkBlurImageFilter::Make(3, 3, cfif), false }, |
reed | bb34a8a | 2016-04-23 15:19:07 -0700 | [diff] [blame] | 1867 | { SkColorFilterImageFilter::Make(cf, blif), false }, |
Mike Reed | 0bdaf05 | 2017-06-18 23:35:57 -0400 | [diff] [blame] | 1868 | { SkMergeImageFilter::Make(cfif, blif), false }, |
reed | 96a04f3 | 2016-04-25 09:25:15 -0700 | [diff] [blame] | 1869 | { SkComposeImageFilter::Make(blif, cfif), false }, |
reed | bb34a8a | 2016-04-23 15:19:07 -0700 | [diff] [blame] | 1870 | }; |
liyuqian | bfebe22 | 2016-11-14 11:17:16 -0800 | [diff] [blame] | 1871 | |
reed | bb34a8a | 2016-04-23 15:19:07 -0700 | [diff] [blame] | 1872 | for (const auto& rec : recs) { |
reed | 96a04f3 | 2016-04-25 09:25:15 -0700 | [diff] [blame] | 1873 | const bool canHandle = rec.fFilter->canHandleComplexCTM(); |
reed | bb34a8a | 2016-04-23 15:19:07 -0700 | [diff] [blame] | 1874 | REPORTER_ASSERT(reporter, canHandle == rec.fExpectCanHandle); |
| 1875 | } |
| 1876 | } |
Florin Malita | 08252ec | 2017-07-06 12:48:15 -0400 | [diff] [blame] | 1877 | |
| 1878 | // Test that transforming the filter DAG doesn't clone shared nodes multiple times. |
| 1879 | DEF_TEST(ImageFilterColorSpaceDAG, reporter) { |
| 1880 | |
| 1881 | // Helper for counting makeColorSpace() clones. |
| 1882 | class TestFilter final : public SkImageFilter { |
| 1883 | public: |
| 1884 | TestFilter() : INHERITED(nullptr, 0, nullptr) {} |
| 1885 | |
| 1886 | #ifndef SK_IGNORE_TO_STRING |
| 1887 | void toString(SkString*) const override {} |
| 1888 | #endif |
| 1889 | Factory getFactory() const override { return nullptr; } |
| 1890 | |
| 1891 | size_t cloneCount() const { return fCloneCount; } |
| 1892 | |
| 1893 | protected: |
| 1894 | sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* src, const Context&, |
| 1895 | SkIPoint* offset) const override { |
| 1896 | return nullptr; |
| 1897 | } |
| 1898 | sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override { |
| 1899 | fCloneCount++; |
| 1900 | return sk_ref_sp(const_cast<TestFilter*>(this)); |
| 1901 | } |
| 1902 | |
| 1903 | private: |
| 1904 | typedef SkImageFilter INHERITED; |
| 1905 | |
| 1906 | mutable size_t fCloneCount = 0; |
| 1907 | }; |
| 1908 | |
| 1909 | auto filter = sk_make_sp<TestFilter>(); |
| 1910 | REPORTER_ASSERT(reporter, filter->cloneCount() == 0u); |
| 1911 | |
| 1912 | // Build a DAG referencing the filter twice. |
| 1913 | auto complexFilter = SkMergeImageFilter::Make(filter, SkOffsetImageFilter::Make(1, 1, filter)); |
| 1914 | REPORTER_ASSERT(reporter, filter->cloneCount() == 0u); |
| 1915 | |
| 1916 | auto xformer = SkColorSpaceXformer::Make(SkColorSpace::MakeSRGB()); |
| 1917 | auto xformedFilter = xformer->apply(complexFilter.get()); |
| 1918 | |
Florin Malita | 39e0855 | 2017-07-06 14:16:18 -0400 | [diff] [blame] | 1919 | REPORTER_ASSERT(reporter, filter->cloneCount() == 1u); |
Florin Malita | 08252ec | 2017-07-06 12:48:15 -0400 | [diff] [blame] | 1920 | } |