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