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