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