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