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