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