blob: 1c85e311be3d0e106880533a49cdb1d91a7e28fe [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
reed@google.comdaaafa62014-04-29 15:20:16 +00007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkBlendMode.h"
10#include "include/core/SkBlurTypes.h"
11#include "include/core/SkCanvas.h"
12#include "include/core/SkColor.h"
13#include "include/core/SkColorPriv.h"
14#include "include/core/SkDrawLooper.h"
15#include "include/core/SkImageInfo.h"
16#include "include/core/SkMaskFilter.h"
17#include "include/core/SkMath.h"
18#include "include/core/SkPaint.h"
19#include "include/core/SkPath.h"
20#include "include/core/SkPixmap.h"
21#include "include/core/SkPoint.h"
22#include "include/core/SkRRect.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040023#include "include/core/SkRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/core/SkRefCnt.h"
25#include "include/core/SkScalar.h"
26#include "include/core/SkShader.h"
27#include "include/core/SkSize.h"
28#include "include/core/SkSurface.h"
29#include "include/core/SkTypes.h"
30#include "include/effects/SkBlurDrawLooper.h"
31#include "include/effects/SkLayerDrawLooper.h"
32#include "include/effects/SkPerlinNoiseShader.h"
33#include "include/private/SkFloatBits.h"
34#include "src/core/SkBlurMask.h"
35#include "src/core/SkBlurPriv.h"
36#include "src/core/SkMask.h"
37#include "src/core/SkMaskFilterBase.h"
38#include "src/core/SkMathPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/effects/SkEmbossMaskFilter.h"
40#include "tests/Test.h"
41#include "tools/ToolUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "tools/gpu/GrContextFactory.h"
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +000043
Ben Wagnere6b04a12018-03-09 14:41:36 -050044#include <math.h>
45#include <string.h>
Ben Wagner9707a7e2019-05-06 17:17:19 -040046#include <initializer_list>
Ben Wagnere6b04a12018-03-09 14:41:36 -050047#include <utility>
48
Ben Wagner9707a7e2019-05-06 17:17:19 -040049class GrContext;
50
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +000051#define WRITE_CSV 0
reed@google.com2b75f422011-07-07 13:43:38 +000052
53///////////////////////////////////////////////////////////////////////////////
54
55#define ILLEGAL_MODE ((SkXfermode::Mode)-1)
56
bungeman@google.com5af16f82011-09-02 15:06:44 +000057static const int outset = 100;
58static const SkColor bgColor = SK_ColorWHITE;
59static const int strokeWidth = 4;
60
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000061static void create(SkBitmap* bm, const SkIRect& bound) {
62 bm->allocN32Pixels(bound.width(), bound.height());
bungeman@google.com5af16f82011-09-02 15:06:44 +000063}
64
65static void drawBG(SkCanvas* canvas) {
66 canvas->drawColor(bgColor);
67}
68
69
70struct BlurTest {
71 void (*addPath)(SkPath*);
72 int viewLen;
73 SkIRect views[9];
74};
75
76//Path Draw Procs
77//Beware that paths themselves my draw differently depending on the clip.
78static void draw50x50Rect(SkPath* path) {
79 path->addRect(0, 0, SkIntToScalar(50), SkIntToScalar(50));
80}
81
82//Tests
83static BlurTest tests[] = {
84 { draw50x50Rect, 3, {
85 //inner half of blur
86 { 0, 0, 50, 50 },
87 //blur, but no path.
88 { 50 + strokeWidth/2, 50 + strokeWidth/2, 100, 100 },
89 //just an edge
90 { 40, strokeWidth, 60, 50 - strokeWidth },
91 }},
92};
93
94/** Assumes that the ref draw was completely inside ref canvas --
95 implies that everything outside is "bgColor".
96 Checks that all overlap is the same and that all non-overlap on the
97 ref is "bgColor".
98 */
99static bool compare(const SkBitmap& ref, const SkIRect& iref,
100 const SkBitmap& test, const SkIRect& itest)
101{
102 const int xOff = itest.fLeft - iref.fLeft;
103 const int yOff = itest.fTop - iref.fTop;
104
bungeman@google.com5af16f82011-09-02 15:06:44 +0000105 for (int y = 0; y < test.height(); ++y) {
106 for (int x = 0; x < test.width(); ++x) {
107 SkColor testColor = test.getColor(x, y);
108 int refX = x + xOff;
109 int refY = y + yOff;
110 SkColor refColor;
111 if (refX >= 0 && refX < ref.width() &&
112 refY >= 0 && refY < ref.height())
113 {
114 refColor = ref.getColor(refX, refY);
115 } else {
116 refColor = bgColor;
117 }
118 if (refColor != testColor) {
119 return false;
120 }
121 }
122 }
123 return true;
124}
125
kkinnunen15302832015-12-01 04:35:26 -0800126DEF_TEST(BlurDrawing, reporter) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000127 SkPaint paint;
128 paint.setColor(SK_ColorGRAY);
129 paint.setStyle(SkPaint::kStroke_Style);
130 paint.setStrokeWidth(SkIntToScalar(strokeWidth));
reed@google.com2b75f422011-07-07 13:43:38 +0000131
robertphillips@google.comb7061172013-09-06 14:16:12 +0000132 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5));
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000133 for (int style = 0; style <= kLastEnum_SkBlurStyle; ++style) {
134 SkBlurStyle blurStyle = static_cast<SkBlurStyle>(style);
reed@google.com2b75f422011-07-07 13:43:38 +0000135
Mike Reed18e75562018-03-12 14:03:47 -0400136 for (bool respectCTM : { false, true }) {
137 paint.setMaskFilter(SkMaskFilter::MakeBlur(blurStyle, sigma, respectCTM));
bungeman@google.com5af16f82011-09-02 15:06:44 +0000138
tomhudson@google.com83a44462011-10-27 15:27:51 +0000139 for (size_t test = 0; test < SK_ARRAY_COUNT(tests); ++test) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000140 SkPath path;
141 tests[test].addPath(&path);
bungeman@google.comd16872c2011-09-02 15:46:17 +0000142 SkPath strokedPath;
143 paint.getFillPath(path, &strokedPath);
144 SkRect refBound = strokedPath.getBounds();
bungeman@google.com5af16f82011-09-02 15:06:44 +0000145 SkIRect iref;
146 refBound.roundOut(&iref);
bungeman@google.comd16872c2011-09-02 15:46:17 +0000147 iref.inset(-outset, -outset);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000148 SkBitmap refBitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000149 create(&refBitmap, iref);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000150
151 SkCanvas refCanvas(refBitmap);
152 refCanvas.translate(SkIntToScalar(-iref.fLeft),
153 SkIntToScalar(-iref.fTop));
154 drawBG(&refCanvas);
155 refCanvas.drawPath(path, paint);
156
157 for (int view = 0; view < tests[test].viewLen; ++view) {
158 SkIRect itest = tests[test].views[view];
159 SkBitmap testBitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000160 create(&testBitmap, itest);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000161
162 SkCanvas testCanvas(testBitmap);
163 testCanvas.translate(SkIntToScalar(-itest.fLeft),
164 SkIntToScalar(-itest.fTop));
165 drawBG(&testCanvas);
166 testCanvas.drawPath(path, paint);
167
168 REPORTER_ASSERT(reporter,
169 compare(refBitmap, iref, testBitmap, itest));
170 }
171 }
172 }
reed@google.com2b75f422011-07-07 13:43:38 +0000173 }
174}
175
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000176///////////////////////////////////////////////////////////////////////////////
177
178// Use SkBlurMask::BlurGroundTruth to blur a 'width' x 'height' solid
179// white rect. Return the right half of the middle row in 'result'.
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000180static void ground_truth_2d(int width, int height,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000181 SkScalar sigma,
182 int* result, int resultCount) {
183 SkMask src, dst;
184
Mike Reed92b33352019-08-24 19:39:13 -0400185 src.fBounds.setWH(width, height);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000186 src.fFormat = SkMask::kA8_Format;
187 src.fRowBytes = src.fBounds.width();
188 src.fImage = SkMask::AllocImage(src.computeTotalImageSize());
189
190 memset(src.fImage, 0xff, src.computeTotalImageSize());
191
robertphillipse80eb922015-12-17 11:33:12 -0800192 if (!SkBlurMask::BlurGroundTruth(sigma, &dst, src, kNormal_SkBlurStyle)) {
193 return;
194 }
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000195
Mike Reed1f275852018-04-11 14:30:17 -0400196 int midX = dst.fBounds.x() + dst.fBounds.width()/2;
197 int midY = dst.fBounds.y() + dst.fBounds.height()/2;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000198 uint8_t* bytes = dst.getAddr8(midX, midY);
199 int i;
200 for (i = 0; i < dst.fBounds.width()-(midX-dst.fBounds.fLeft); ++i) {
201 if (i < resultCount) {
202 result[i] = bytes[i];
203 }
204 }
205 for ( ; i < resultCount; ++i) {
206 result[i] = 0;
207 }
robertphillips@google.com6d837aa2013-10-11 14:38:46 +0000208
209 SkMask::FreeImage(src.fImage);
210 SkMask::FreeImage(dst.fImage);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000211}
212
213// Implement a step function that is 255 between min and max; 0 elsewhere.
214static int step(int x, SkScalar min, SkScalar max) {
215 if (min < x && x < max) {
216 return 255;
217 }
218 return 0;
219}
220
221// Implement a Gaussian function with 0 mean and std.dev. of 'sigma'.
222static float gaussian(int x, SkScalar sigma) {
robertphillips@google.comb85564a2013-09-05 17:53:34 +0000223 float k = SK_Scalar1/(sigma * sqrtf(2.0f*SK_ScalarPI));
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000224 float exponent = -(x * x) / (2 * sigma * sigma);
robertphillips@google.comb85564a2013-09-05 17:53:34 +0000225 return k * expf(exponent);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000226}
227
228// Perform a brute force convolution of a step function with a Gaussian.
229// Return the right half in 'result'
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000230static void brute_force_1d(SkScalar stepMin, SkScalar stepMax,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000231 SkScalar gaussianSigma,
232 int* result, int resultCount) {
233
234 int gaussianRange = SkScalarCeilToInt(10 * gaussianSigma);
235
236 for (int i = 0; i < resultCount; ++i) {
237 SkScalar sum = 0.0f;
238 for (int j = -gaussianRange; j < gaussianRange; ++j) {
239 sum += gaussian(j, gaussianSigma) * step(i-j, stepMin, stepMax);
240 }
241
242 result[i] = SkClampMax(SkClampPos(int(sum + 0.5f)), 255);
243 }
244}
245
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000246static void blur_path(SkCanvas* canvas, const SkPath& path,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000247 SkScalar gaussianSigma) {
248
249 SkScalar midX = path.getBounds().centerX();
250 SkScalar midY = path.getBounds().centerY();
251
252 canvas->translate(-midX, -midY);
253
254 SkPaint blurPaint;
255 blurPaint.setColor(SK_ColorWHITE);
Mike Reed18e75562018-03-12 14:03:47 -0400256 blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, gaussianSigma));
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000257
258 canvas->drawColor(SK_ColorBLACK);
259 canvas->drawPath(path, blurPaint);
260}
261
262// Readback the blurred draw results from the canvas
Mike Reedf1942192017-07-21 14:24:29 -0400263static void readback(const SkBitmap& src, int* result, int resultCount) {
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000264 SkBitmap readback;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000265 readback.allocN32Pixels(resultCount, 30);
Mike Reedf1942192017-07-21 14:24:29 -0400266 SkPixmap pm;
267 readback.peekPixels(&pm);
268 src.readPixels(pm, 0, 0);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000269
Mike Reedf1942192017-07-21 14:24:29 -0400270 const SkPMColor* pixels = pm.addr32(0, 15);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000271
272 for (int i = 0; i < resultCount; ++i) {
273 result[i] = SkColorGetR(pixels[i]);
274 }
275}
276
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000277// Draw a blurred version of the provided path.
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000278// Return the right half of the middle row in 'result'.
279static void cpu_blur_path(const SkPath& path, SkScalar gaussianSigma,
280 int* result, int resultCount) {
281
282 SkBitmap bitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000283 bitmap.allocN32Pixels(resultCount, 30);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000284 SkCanvas canvas(bitmap);
285
286 blur_path(&canvas, path, gaussianSigma);
Mike Reedf1942192017-07-21 14:24:29 -0400287 readback(bitmap, result, resultCount);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000288}
289
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000290#if WRITE_CSV
291static void write_as_csv(const char* label, SkScalar scale, int* data, int count) {
292 SkDebugf("%s_%.2f,", label, scale);
293 for (int i = 0; i < count-1; ++i) {
294 SkDebugf("%d,", data[i]);
295 }
296 SkDebugf("%d\n", data[count-1]);
297}
298#endif
299
300static bool match(int* first, int* second, int count, int tol) {
301 int delta;
302 for (int i = 0; i < count; ++i) {
303 delta = first[i] - second[i];
304 if (delta > tol || delta < -tol) {
305 return false;
306 }
307 }
308
309 return true;
310}
311
312// Test out the normal blur style with a wide range of sigmas
bsalomonf2f1c172016-04-05 12:59:06 -0700313DEF_TEST(BlurSigmaRange, reporter) {
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000314 static const int kSize = 100;
315
316 // The geometry is offset a smidge to trigger:
317 // https://code.google.com/p/chromium/issues/detail?id=282418
318 SkPath rectPath;
319 rectPath.addRect(0.3f, 0.3f, 100.3f, 100.3f);
320
321 SkPoint polyPts[] = {
322 { 0.3f, 0.3f },
323 { 100.3f, 0.3f },
324 { 100.3f, 100.3f },
325 { 0.3f, 100.3f },
326 { 2.3f, 50.3f } // a little divet to throw off the rect special case
327 };
328 SkPath polyPath;
329 polyPath.addPoly(polyPts, SK_ARRAY_COUNT(polyPts), true);
330
331 int rectSpecialCaseResult[kSize];
332 int generalCaseResult[kSize];
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000333 int groundTruthResult[kSize];
334 int bruteForce1DResult[kSize];
335
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000336 SkScalar sigma = 10.0f;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000337
338 for (int i = 0; i < 4; ++i, sigma /= 10) {
339
340 cpu_blur_path(rectPath, sigma, rectSpecialCaseResult, kSize);
341 cpu_blur_path(polyPath, sigma, generalCaseResult, kSize);
humper4a24cd82014-06-17 13:39:29 -0700342
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000343 ground_truth_2d(100, 100, sigma, groundTruthResult, kSize);
344 brute_force_1d(-50.0f, 50.0f, sigma, bruteForce1DResult, kSize);
345
346 REPORTER_ASSERT(reporter, match(rectSpecialCaseResult, bruteForce1DResult, kSize, 5));
347 REPORTER_ASSERT(reporter, match(generalCaseResult, bruteForce1DResult, kSize, 15));
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000348 REPORTER_ASSERT(reporter, match(groundTruthResult, bruteForce1DResult, kSize, 1));
349
350#if WRITE_CSV
351 write_as_csv("RectSpecialCase", sigma, rectSpecialCaseResult, kSize);
352 write_as_csv("GeneralCase", sigma, generalCaseResult, kSize);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000353 write_as_csv("GPU", sigma, gpuResult, kSize);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000354 write_as_csv("GroundTruth2D", sigma, groundTruthResult, kSize);
355 write_as_csv("BruteForce1D", sigma, bruteForce1DResult, kSize);
356#endif
357 }
358}
359
reed@google.comdaaafa62014-04-29 15:20:16 +0000360///////////////////////////////////////////////////////////////////////////////////////////
361
Mike Reed18e75562018-03-12 14:03:47 -0400362static void test_blurDrawLooper(skiatest::Reporter* reporter, SkScalar sigma, SkBlurStyle style) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000363 if (kNormal_SkBlurStyle != style) {
364 return; // blurdrawlooper only supports normal
365 }
366
367 const SkColor color = 0xFF335577;
368 const SkScalar dx = 10;
369 const SkScalar dy = -5;
Matt Sarettf160ad42017-03-23 16:23:38 -0400370 sk_sp<SkDrawLooper> lp(SkBlurDrawLooper::Make(color, sigma, dx, dy));
371 const bool expectSuccess = sigma > 0;
reed@google.comdaaafa62014-04-29 15:20:16 +0000372
reed7b380d02016-03-21 13:25:16 -0700373 if (nullptr == lp) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000374 REPORTER_ASSERT(reporter, sigma <= 0);
375 } else {
376 SkDrawLooper::BlurShadowRec rec;
377 bool success = lp->asABlurShadow(&rec);
378 REPORTER_ASSERT(reporter, success == expectSuccess);
379 if (success) {
380 REPORTER_ASSERT(reporter, rec.fSigma == sigma);
381 REPORTER_ASSERT(reporter, rec.fOffset.x() == dx);
382 REPORTER_ASSERT(reporter, rec.fOffset.y() == dy);
383 REPORTER_ASSERT(reporter, rec.fColor == color);
384 REPORTER_ASSERT(reporter, rec.fStyle == style);
reed@google.comdaaafa62014-04-29 15:20:16 +0000385 }
386 }
387}
388
reed7b380d02016-03-21 13:25:16 -0700389static void test_looper(skiatest::Reporter* reporter, sk_sp<SkDrawLooper> lp, SkScalar sigma,
Mike Reed18e75562018-03-12 14:03:47 -0400390 SkBlurStyle style, bool expectSuccess) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000391 SkDrawLooper::BlurShadowRec rec;
392 bool success = lp->asABlurShadow(&rec);
393 REPORTER_ASSERT(reporter, success == expectSuccess);
394 if (success != expectSuccess) {
395 lp->asABlurShadow(&rec);
396 }
397 if (success) {
398 REPORTER_ASSERT(reporter, rec.fSigma == sigma);
399 REPORTER_ASSERT(reporter, rec.fStyle == style);
reed@google.comdaaafa62014-04-29 15:20:16 +0000400 }
reed@google.comdaaafa62014-04-29 15:20:16 +0000401}
402
403static void make_noop_layer(SkLayerDrawLooper::Builder* builder) {
404 SkLayerDrawLooper::LayerInfo info;
405
406 info.fPaintBits = 0;
Mike Reedfaba3712016-11-03 14:45:31 -0400407 info.fColorMode = SkBlendMode::kDst;
reed@google.comdaaafa62014-04-29 15:20:16 +0000408 builder->addLayer(info);
409}
410
reedefdfd512016-04-04 10:02:58 -0700411static void make_blur_layer(SkLayerDrawLooper::Builder* builder, sk_sp<SkMaskFilter> mf) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000412 SkLayerDrawLooper::LayerInfo info;
413
414 info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit;
Mike Reedfaba3712016-11-03 14:45:31 -0400415 info.fColorMode = SkBlendMode::kSrc;
reed@google.comdaaafa62014-04-29 15:20:16 +0000416 SkPaint* paint = builder->addLayer(info);
reedefdfd512016-04-04 10:02:58 -0700417 paint->setMaskFilter(std::move(mf));
reed@google.comdaaafa62014-04-29 15:20:16 +0000418}
419
reedefdfd512016-04-04 10:02:58 -0700420static void test_layerDrawLooper(skiatest::Reporter* reporter, sk_sp<SkMaskFilter> mf,
Mike Reed18e75562018-03-12 14:03:47 -0400421 SkScalar sigma, SkBlurStyle style, bool expectSuccess) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000422
423 SkLayerDrawLooper::LayerInfo info;
424 SkLayerDrawLooper::Builder builder;
425
426 // 1 layer is too few
427 make_noop_layer(&builder);
Mike Reed18e75562018-03-12 14:03:47 -0400428 test_looper(reporter, builder.detach(), sigma, style, false);
reed@google.comdaaafa62014-04-29 15:20:16 +0000429
430 // 2 layers is good, but need blur
431 make_noop_layer(&builder);
432 make_noop_layer(&builder);
Mike Reed18e75562018-03-12 14:03:47 -0400433 test_looper(reporter, builder.detach(), sigma, style, false);
reed@google.comdaaafa62014-04-29 15:20:16 +0000434
435 // 2 layers is just right
436 make_noop_layer(&builder);
437 make_blur_layer(&builder, mf);
Mike Reed18e75562018-03-12 14:03:47 -0400438 test_looper(reporter, builder.detach(), sigma, style, expectSuccess);
reed@google.comdaaafa62014-04-29 15:20:16 +0000439
440 // 3 layers is too many
441 make_noop_layer(&builder);
442 make_blur_layer(&builder, mf);
443 make_noop_layer(&builder);
Mike Reed18e75562018-03-12 14:03:47 -0400444 test_looper(reporter, builder.detach(), sigma, style, false);
reed@google.comdaaafa62014-04-29 15:20:16 +0000445}
446
kkinnunen15302832015-12-01 04:35:26 -0800447DEF_TEST(BlurAsABlur, reporter) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000448 const SkBlurStyle styles[] = {
449 kNormal_SkBlurStyle, kSolid_SkBlurStyle, kOuter_SkBlurStyle, kInner_SkBlurStyle
450 };
451 const SkScalar sigmas[] = {
452 // values <= 0 should not success for a blur
453 -1, 0, 0.5f, 2
454 };
455
456 // Test asABlur for SkBlurMaskFilter
457 //
458 for (size_t i = 0; i < SK_ARRAY_COUNT(styles); ++i) {
Robert Phillips98624d22016-12-19 11:37:37 -0500459 const SkBlurStyle style = styles[i];
reed@google.comdaaafa62014-04-29 15:20:16 +0000460 for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) {
461 const SkScalar sigma = sigmas[j];
Mike Reed18e75562018-03-12 14:03:47 -0400462 for (bool respectCTM : { false, true }) {
463 sk_sp<SkMaskFilter> mf(SkMaskFilter::MakeBlur(style, sigma, respectCTM));
halcanary96fcdcc2015-08-27 07:41:13 -0700464 if (nullptr == mf.get()) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000465 REPORTER_ASSERT(reporter, sigma <= 0);
466 } else {
467 REPORTER_ASSERT(reporter, sigma > 0);
Mike Reed80747ef2018-01-23 15:29:32 -0500468 SkMaskFilterBase::BlurRec rec;
469 bool success = as_MFB(mf)->asABlur(&rec);
Mike Reed18e75562018-03-12 14:03:47 -0400470 if (respectCTM) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000471 REPORTER_ASSERT(reporter, success);
472 REPORTER_ASSERT(reporter, rec.fSigma == sigma);
473 REPORTER_ASSERT(reporter, rec.fStyle == style);
Mike Reed18e75562018-03-12 14:03:47 -0400474 } else {
475 REPORTER_ASSERT(reporter, !success);
reed@google.comdaaafa62014-04-29 15:20:16 +0000476 }
Mike Reed18e75562018-03-12 14:03:47 -0400477 test_layerDrawLooper(reporter, std::move(mf), sigma, style, success);
reed@google.comdaaafa62014-04-29 15:20:16 +0000478 }
Mike Reed18e75562018-03-12 14:03:47 -0400479 test_blurDrawLooper(reporter, sigma, style);
reed@google.comdaaafa62014-04-29 15:20:16 +0000480 }
481 }
482 }
483
484 // Test asABlur for SkEmbossMaskFilter -- should never succeed
485 //
486 {
487 SkEmbossMaskFilter::Light light = {
488 { 1, 1, 1 }, 0, 127, 127
489 };
490 for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) {
491 const SkScalar sigma = sigmas[j];
reedefdfd512016-04-04 10:02:58 -0700492 auto mf(SkEmbossMaskFilter::Make(sigma, light));
493 if (mf) {
Mike Reed80747ef2018-01-23 15:29:32 -0500494 SkMaskFilterBase::BlurRec rec;
495 bool success = as_MFB(mf)->asABlur(&rec);
reed@google.comdaaafa62014-04-29 15:20:16 +0000496 REPORTER_ASSERT(reporter, !success);
497 }
498 }
499 }
500}
501
halcanary9d524f22016-03-29 09:03:52 -0700502// This exercises the problem discovered in crbug.com/570232. The return value from
robertphillips4abb0c12016-01-04 11:20:25 -0800503// SkBlurMask::BoxBlur wasn't being checked in SkBlurMaskFilter.cpp::GrRRectBlurEffect::Create
egdanielab527a52016-06-28 08:07:26 -0700504DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SmallBoxBlurBug, reporter, ctxInfo) {
robertphillips4abb0c12016-01-04 11:20:25 -0800505
506 SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128);
bsalomon8b7451a2016-05-11 06:33:06 -0700507 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
robertphillips4abb0c12016-01-04 11:20:25 -0800508 SkCanvas* canvas = surface->getCanvas();
509
510 SkRect r = SkRect::MakeXYWH(10, 10, 100, 100);
511 SkRRect rr = SkRRect::MakeRectXY(r, 10, 10);
512
513 SkPaint p;
Mike Reed18e75562018-03-12 14:03:47 -0400514 p.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 0.01f));
robertphillips4abb0c12016-01-04 11:20:25 -0800515
516 canvas->drawRRect(rr, p);
517}
518
robertphillipsf5a83e82016-08-10 12:00:09 -0700519DEF_TEST(BlurredRRectNinePatchComputation, reporter) {
520 const SkRect r = SkRect::MakeXYWH(10, 10, 100, 100);
robertphillipsc4d2f902016-08-16 09:30:03 -0700521 static const SkScalar kBlurRad = 3.0f;
robertphillipsf5a83e82016-08-10 12:00:09 -0700522
523 bool ninePatchable;
524 SkRRect rrectToDraw;
525 SkISize size;
Mike Reed28d47732018-03-05 16:56:52 -0500526 SkScalar rectXs[kSkBlurRRectMaxDivisions],
527 rectYs[kSkBlurRRectMaxDivisions];
528 SkScalar texXs[kSkBlurRRectMaxDivisions],
529 texYs[kSkBlurRRectMaxDivisions];
robertphillipsc4d2f902016-08-16 09:30:03 -0700530 int numX, numY;
531 uint32_t skipMask;
robertphillipsf5a83e82016-08-10 12:00:09 -0700532
533 // not nine-patchable
534 {
535 SkVector radii[4] = { { 100, 100 }, { 0, 0 }, { 100, 100 }, { 0, 0 } };
536
537 SkRRect rr;
538 rr.setRectRadii(r, radii);
539
Mike Reed28d47732018-03-05 16:56:52 -0500540 ninePatchable = SkComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(),
541 kBlurRad, kBlurRad,
542 &rrectToDraw, &size,
543 rectXs, rectYs, texXs, texYs,
544 &numX, &numY, &skipMask);
robertphillipsf5a83e82016-08-10 12:00:09 -0700545 REPORTER_ASSERT(reporter, !ninePatchable);
546 }
547
548 // simple circular
549 {
robertphillipsc4d2f902016-08-16 09:30:03 -0700550 static const SkScalar kCornerRad = 10.0f;
robertphillipsf5a83e82016-08-10 12:00:09 -0700551 SkRRect rr;
robertphillipsc4d2f902016-08-16 09:30:03 -0700552 rr.setRectXY(r, kCornerRad, kCornerRad);
robertphillipsf5a83e82016-08-10 12:00:09 -0700553
Mike Reed28d47732018-03-05 16:56:52 -0500554 ninePatchable = SkComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(),
555 kBlurRad, kBlurRad,
556 &rrectToDraw, &size,
557 rectXs, rectYs, texXs, texYs,
558 &numX, &numY, &skipMask);
robertphillipsc4d2f902016-08-16 09:30:03 -0700559
560 static const SkScalar kAns = 12.0f * kBlurRad + 2.0f * kCornerRad + 1.0f;
robertphillipsf5a83e82016-08-10 12:00:09 -0700561 REPORTER_ASSERT(reporter, ninePatchable);
robertphillipsc4d2f902016-08-16 09:30:03 -0700562 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fWidth), kAns));
563 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fHeight), kAns));
564 REPORTER_ASSERT(reporter, 4 == numX && 4 == numY);
565 REPORTER_ASSERT(reporter, !skipMask);
robertphillipsf5a83e82016-08-10 12:00:09 -0700566 }
567
568 // simple elliptical
569 {
robertphillipsc4d2f902016-08-16 09:30:03 -0700570 static const SkScalar kXCornerRad = 2.0f;
571 static const SkScalar kYCornerRad = 10.0f;
robertphillipsf5a83e82016-08-10 12:00:09 -0700572 SkRRect rr;
robertphillipsc4d2f902016-08-16 09:30:03 -0700573 rr.setRectXY(r, kXCornerRad, kYCornerRad);
robertphillipsf5a83e82016-08-10 12:00:09 -0700574
Mike Reed28d47732018-03-05 16:56:52 -0500575 ninePatchable = SkComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(),
576 kBlurRad, kBlurRad,
577 &rrectToDraw, &size,
578 rectXs, rectYs, texXs, texYs,
579 &numX, &numY, &skipMask);
robertphillipsc4d2f902016-08-16 09:30:03 -0700580
581 static const SkScalar kXAns = 12.0f * kBlurRad + 2.0f * kXCornerRad + 1.0f;
582 static const SkScalar kYAns = 12.0f * kBlurRad + 2.0f * kYCornerRad + 1.0f;
583
robertphillipsf5a83e82016-08-10 12:00:09 -0700584 REPORTER_ASSERT(reporter, ninePatchable);
robertphillipsc4d2f902016-08-16 09:30:03 -0700585 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fWidth), kXAns));
586 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fHeight), kYAns));
587 REPORTER_ASSERT(reporter, 4 == numX && 4 == numY);
588 REPORTER_ASSERT(reporter, !skipMask);
589 }
590
591 // test-out occlusion
592 {
593 static const SkScalar kCornerRad = 10.0f;
594 SkRRect rr;
595 rr.setRectXY(r, kCornerRad, kCornerRad);
596
597 // The rectXs & rectYs should be { 1, 29, 91, 119 }. Add two more points around each.
598 SkScalar testLocs[] = {
599 -18.0f, -9.0f,
600 1.0f,
Ben Wagner63fd7602017-10-09 15:45:33 -0400601 9.0f, 18.0f,
602 29.0f,
robertphillipsc4d2f902016-08-16 09:30:03 -0700603 39.0f, 49.0f,
604 91.0f,
605 109.0f, 118.0f,
606 119.0f,
607 139.0f, 149.0f
608 };
609
610 for (int minY = 0; minY < (int)SK_ARRAY_COUNT(testLocs); ++minY) {
611 for (int maxY = minY+1; maxY < (int)SK_ARRAY_COUNT(testLocs); ++maxY) {
612 for (int minX = 0; minX < (int)SK_ARRAY_COUNT(testLocs); ++minX) {
613 for (int maxX = minX+1; maxX < (int)SK_ARRAY_COUNT(testLocs); ++maxX) {
614 SkRect occluder = SkRect::MakeLTRB(testLocs[minX], testLocs[minY],
615 testLocs[maxX], testLocs[maxY]);
616 if (occluder.isEmpty()) {
617 continue;
618 }
619
Mike Reed28d47732018-03-05 16:56:52 -0500620 ninePatchable = SkComputeBlurredRRectParams(rr, rr, occluder,
robertphillipsc4d2f902016-08-16 09:30:03 -0700621 kBlurRad, kBlurRad,
622 &rrectToDraw, &size,
623 rectXs, rectYs, texXs, texYs,
Ben Wagner63fd7602017-10-09 15:45:33 -0400624 &numX, &numY, &skipMask);
robertphillipsc4d2f902016-08-16 09:30:03 -0700625
626 static const SkScalar kAns = 12.0f * kBlurRad + 2.0f * kCornerRad + 1.0f;
627 REPORTER_ASSERT(reporter, ninePatchable);
628 REPORTER_ASSERT(reporter,
629 SkScalarNearlyEqual(SkIntToScalar(size.fWidth), kAns));
630 REPORTER_ASSERT(reporter,
631 SkScalarNearlyEqual(SkIntToScalar(size.fHeight), kAns));
632
633 int checkBit = 0x1;
634 for (int y = 0; y < numY-1; ++y) {
635 for (int x = 0; x < numX-1; ++x) {
636 SkRect cell = SkRect::MakeLTRB(rectXs[x], rectYs[y],
637 rectXs[x+1], rectYs[y+1]);
638 REPORTER_ASSERT(reporter,
639 SkToBool(skipMask & checkBit) ==
640 (cell.isEmpty() || occluder.contains(cell)));
641
642 REPORTER_ASSERT(reporter, texXs[x] >= 0 &&
643 texXs[x] <= size.fWidth);
644 REPORTER_ASSERT(reporter, texYs[y] >= 0 &&
645 texXs[y] <= size.fHeight);
646
647 checkBit <<= 1;
648 }
649 }
650 }
651 }
652 }
robertphillipsf5a83e82016-08-10 12:00:09 -0700653 }
robertphillipsc4d2f902016-08-16 09:30:03 -0700654
655
robertphillipsf5a83e82016-08-10 12:00:09 -0700656 }
657
658}
659
Florin Malita1ba5bfe2017-11-28 17:54:23 -0500660// https://crbugs.com/787712
661DEF_TEST(EmbossPerlinCrash, reporter) {
662 SkPaint p;
663
664 static constexpr SkEmbossMaskFilter::Light light = {
665 { 1, 1, 1 }, 0, 127, 127
666 };
667 p.setMaskFilter(SkEmbossMaskFilter::Make(1, light));
668 p.setShader(SkPerlinNoiseShader::MakeFractalNoise(1.0f, 1.0f, 2, 0.0f));
669
670 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(100, 100);
671 surface->getCanvas()->drawPaint(p);
672}
673
reed@google.comdaaafa62014-04-29 15:20:16 +0000674///////////////////////////////////////////////////////////////////////////////////////////
Mike Reedf221b492018-02-15 11:38:20 -0500675
676DEF_TEST(BlurZeroSigma, reporter) {
677 auto surf = SkSurface::MakeRasterN32Premul(20, 20);
678 SkPaint paint;
679 paint.setAntiAlias(true);
680
681 const SkIRect ir = { 5, 5, 15, 15 };
682 const SkRect r = SkRect::Make(ir);
683
684 const SkScalar sigmas[] = { 0, SkBits2Float(1) };
685 // if sigma is zero (or nearly so), we need to draw correctly (unblurred) and not crash
686 // or assert.
687 for (auto sigma : sigmas) {
Mike Reed18e75562018-03-12 14:03:47 -0400688 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma));
Mike Reedf221b492018-02-15 11:38:20 -0500689 surf->getCanvas()->drawRect(r, paint);
690
Mike Kleinea3f0142019-03-20 11:12:10 -0500691 ToolUtils::PixelIter iter(surf.get());
Mike Reedf221b492018-02-15 11:38:20 -0500692 SkIPoint loc;
693 while (const SkPMColor* p = (const SkPMColor*)iter.next(&loc)) {
694 if (ir.contains(loc.fX, loc.fY)) {
695 // inside the rect we draw (opaque black)
696 REPORTER_ASSERT(reporter, *p == SkPackARGB32(0xFF, 0, 0, 0));
697 } else {
698 // outside the rect we didn't draw at all, no blurred edges
699 REPORTER_ASSERT(reporter, *p == 0);
700 }
701 }
702 }
703}
704
Robert Phillipsc90b4db2018-04-23 15:12:00 +0000705
706///////////////////////////////////////////////////////////////////////////////////////////
Robert Phillipsc90b4db2018-04-23 15:12:00 +0000707
708DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BlurMaskBiggerThanDest, reporter, ctxInfo) {
709 GrContext* context = ctxInfo.grContext();
710
711 SkImageInfo ii = SkImageInfo::Make(32, 32, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
712
713 sk_sp<SkSurface> dst(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii));
714 if (!dst) {
715 ERRORF(reporter, "Could not create surface for test.");
716 return;
717 }
718
719 SkPaint p;
720 p.setColor(SK_ColorRED);
721 p.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 3));
722
723 SkCanvas* canvas = dst->getCanvas();
724
725 canvas->clear(SK_ColorBLACK);
726 canvas->drawCircle(SkPoint::Make(16, 16), 8, p);
727
728 SkBitmap readback;
729 SkAssertResult(readback.tryAllocPixels(ii));
730
731 canvas->readPixels(readback, 0, 0);
732 REPORTER_ASSERT(reporter, SkColorGetR(readback.getColor(15, 15)) > 128);
733 REPORTER_ASSERT(reporter, SkColorGetG(readback.getColor(15, 15)) == 0);
734 REPORTER_ASSERT(reporter, SkColorGetB(readback.getColor(15, 15)) == 0);
735 REPORTER_ASSERT(reporter, readback.getColor(31, 31) == SK_ColorBLACK);
736}
Mike Reed840debe2018-09-15 12:15:27 -0400737
738DEF_TEST(zero_blur, reporter) {
739 SkBitmap alpha, bitmap;
740
741 SkPaint paint;
742 paint.setMaskFilter(SkMaskFilter::MakeBlur(kOuter_SkBlurStyle, 3));
743 SkIPoint offset;
744 bitmap.extractAlpha(&alpha, &paint, nullptr, &offset);
745}
746