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