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