blob: 2462342032081d524c56c83c1b006aba4292daf0 [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
Ben Wagnere6b04a12018-03-09 14:41:36 -05008#include "SkBitmap.h"
9#include "SkBlendMode.h"
10#include "SkBlurDrawLooper.h"
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +000011#include "SkBlurMask.h"
Mike Reed28d47732018-03-05 16:56:52 -050012#include "SkBlurPriv.h"
Ben Wagnere6b04a12018-03-09 14:41:36 -050013#include "SkBlurTypes.h"
reedfb8c1fc2015-08-04 18:44:56 -070014#include "SkCanvas.h"
Ben Wagnere6b04a12018-03-09 14:41:36 -050015#include "SkColor.h"
Mike Reedf221b492018-02-15 11:38:20 -050016#include "SkColorPriv.h"
Ben Wagnere6b04a12018-03-09 14:41:36 -050017#include "SkDrawLooper.h"
bungemand3ebb482015-08-05 13:57:49 -070018#include "SkEmbossMaskFilter.h"
Ben Wagnere6b04a12018-03-09 14:41:36 -050019#include "SkFloatBits.h"
20#include "SkImageInfo.h"
bungemand3ebb482015-08-05 13:57:49 -070021#include "SkLayerDrawLooper.h"
Ben Wagnere6b04a12018-03-09 14:41:36 -050022#include "SkMask.h"
23#include "SkMaskFilter.h"
Mike Reed80747ef2018-01-23 15:29:32 -050024#include "SkMaskFilterBase.h"
tomhudson@google.com889bd8b2011-09-27 17:38:17 +000025#include "SkMath.h"
bungeman@google.com5af16f82011-09-02 15:06:44 +000026#include "SkPaint.h"
bungemand3ebb482015-08-05 13:57:49 -070027#include "SkPath.h"
Florin Malita1ba5bfe2017-11-28 17:54:23 -050028#include "SkPerlinNoiseShader.h"
Ben Wagnere6b04a12018-03-09 14:41:36 -050029#include "SkPixmap.h"
30#include "SkPoint.h"
31#include "SkRRect.h"
Mike Reed1f275852018-04-11 14:30:17 -040032#include "SkRectPriv.h"
Ben Wagnere6b04a12018-03-09 14:41:36 -050033#include "SkRefCnt.h"
34#include "SkScalar.h"
35#include "SkShader.h"
36#include "SkSize.h"
Florin Malita1ba5bfe2017-11-28 17:54:23 -050037#include "SkSurface.h"
Ben Wagnere6b04a12018-03-09 14:41:36 -050038#include "SkTypes.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000039#include "Test.h"
Ben Wagnere6b04a12018-03-09 14:41:36 -050040#include "sk_pixel_iter.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000041
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +000042#include "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>
46#include <utility>
47
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +000048#define WRITE_CSV 0
reed@google.com2b75f422011-07-07 13:43:38 +000049
50///////////////////////////////////////////////////////////////////////////////
51
52#define ILLEGAL_MODE ((SkXfermode::Mode)-1)
53
bungeman@google.com5af16f82011-09-02 15:06:44 +000054static const int outset = 100;
55static const SkColor bgColor = SK_ColorWHITE;
56static const int strokeWidth = 4;
57
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000058static void create(SkBitmap* bm, const SkIRect& bound) {
59 bm->allocN32Pixels(bound.width(), bound.height());
bungeman@google.com5af16f82011-09-02 15:06:44 +000060}
61
62static void drawBG(SkCanvas* canvas) {
63 canvas->drawColor(bgColor);
64}
65
66
67struct BlurTest {
68 void (*addPath)(SkPath*);
69 int viewLen;
70 SkIRect views[9];
71};
72
73//Path Draw Procs
74//Beware that paths themselves my draw differently depending on the clip.
75static void draw50x50Rect(SkPath* path) {
76 path->addRect(0, 0, SkIntToScalar(50), SkIntToScalar(50));
77}
78
79//Tests
80static BlurTest tests[] = {
81 { draw50x50Rect, 3, {
82 //inner half of blur
83 { 0, 0, 50, 50 },
84 //blur, but no path.
85 { 50 + strokeWidth/2, 50 + strokeWidth/2, 100, 100 },
86 //just an edge
87 { 40, strokeWidth, 60, 50 - strokeWidth },
88 }},
89};
90
91/** Assumes that the ref draw was completely inside ref canvas --
92 implies that everything outside is "bgColor".
93 Checks that all overlap is the same and that all non-overlap on the
94 ref is "bgColor".
95 */
96static bool compare(const SkBitmap& ref, const SkIRect& iref,
97 const SkBitmap& test, const SkIRect& itest)
98{
99 const int xOff = itest.fLeft - iref.fLeft;
100 const int yOff = itest.fTop - iref.fTop;
101
bungeman@google.com5af16f82011-09-02 15:06:44 +0000102 for (int y = 0; y < test.height(); ++y) {
103 for (int x = 0; x < test.width(); ++x) {
104 SkColor testColor = test.getColor(x, y);
105 int refX = x + xOff;
106 int refY = y + yOff;
107 SkColor refColor;
108 if (refX >= 0 && refX < ref.width() &&
109 refY >= 0 && refY < ref.height())
110 {
111 refColor = ref.getColor(refX, refY);
112 } else {
113 refColor = bgColor;
114 }
115 if (refColor != testColor) {
116 return false;
117 }
118 }
119 }
120 return true;
121}
122
kkinnunen15302832015-12-01 04:35:26 -0800123DEF_TEST(BlurDrawing, reporter) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000124 SkPaint paint;
125 paint.setColor(SK_ColorGRAY);
126 paint.setStyle(SkPaint::kStroke_Style);
127 paint.setStrokeWidth(SkIntToScalar(strokeWidth));
reed@google.com2b75f422011-07-07 13:43:38 +0000128
robertphillips@google.comb7061172013-09-06 14:16:12 +0000129 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5));
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000130 for (int style = 0; style <= kLastEnum_SkBlurStyle; ++style) {
131 SkBlurStyle blurStyle = static_cast<SkBlurStyle>(style);
reed@google.com2b75f422011-07-07 13:43:38 +0000132
Mike Reed18e75562018-03-12 14:03:47 -0400133 for (bool respectCTM : { false, true }) {
134 paint.setMaskFilter(SkMaskFilter::MakeBlur(blurStyle, sigma, respectCTM));
bungeman@google.com5af16f82011-09-02 15:06:44 +0000135
tomhudson@google.com83a44462011-10-27 15:27:51 +0000136 for (size_t test = 0; test < SK_ARRAY_COUNT(tests); ++test) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000137 SkPath path;
138 tests[test].addPath(&path);
bungeman@google.comd16872c2011-09-02 15:46:17 +0000139 SkPath strokedPath;
140 paint.getFillPath(path, &strokedPath);
141 SkRect refBound = strokedPath.getBounds();
bungeman@google.com5af16f82011-09-02 15:06:44 +0000142 SkIRect iref;
143 refBound.roundOut(&iref);
bungeman@google.comd16872c2011-09-02 15:46:17 +0000144 iref.inset(-outset, -outset);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000145 SkBitmap refBitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000146 create(&refBitmap, iref);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000147
148 SkCanvas refCanvas(refBitmap);
149 refCanvas.translate(SkIntToScalar(-iref.fLeft),
150 SkIntToScalar(-iref.fTop));
151 drawBG(&refCanvas);
152 refCanvas.drawPath(path, paint);
153
154 for (int view = 0; view < tests[test].viewLen; ++view) {
155 SkIRect itest = tests[test].views[view];
156 SkBitmap testBitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000157 create(&testBitmap, itest);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000158
159 SkCanvas testCanvas(testBitmap);
160 testCanvas.translate(SkIntToScalar(-itest.fLeft),
161 SkIntToScalar(-itest.fTop));
162 drawBG(&testCanvas);
163 testCanvas.drawPath(path, paint);
164
165 REPORTER_ASSERT(reporter,
166 compare(refBitmap, iref, testBitmap, itest));
167 }
168 }
169 }
reed@google.com2b75f422011-07-07 13:43:38 +0000170 }
171}
172
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000173///////////////////////////////////////////////////////////////////////////////
174
175// Use SkBlurMask::BlurGroundTruth to blur a 'width' x 'height' solid
176// white rect. Return the right half of the middle row in 'result'.
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000177static void ground_truth_2d(int width, int height,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000178 SkScalar sigma,
179 int* result, int resultCount) {
180 SkMask src, dst;
181
182 src.fBounds.set(0, 0, width, height);
183 src.fFormat = SkMask::kA8_Format;
184 src.fRowBytes = src.fBounds.width();
185 src.fImage = SkMask::AllocImage(src.computeTotalImageSize());
186
187 memset(src.fImage, 0xff, src.computeTotalImageSize());
188
robertphillipse80eb922015-12-17 11:33:12 -0800189 if (!SkBlurMask::BlurGroundTruth(sigma, &dst, src, kNormal_SkBlurStyle)) {
190 return;
191 }
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000192
Mike Reed1f275852018-04-11 14:30:17 -0400193 int midX = dst.fBounds.x() + dst.fBounds.width()/2;
194 int midY = dst.fBounds.y() + dst.fBounds.height()/2;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000195 uint8_t* bytes = dst.getAddr8(midX, midY);
196 int i;
197 for (i = 0; i < dst.fBounds.width()-(midX-dst.fBounds.fLeft); ++i) {
198 if (i < resultCount) {
199 result[i] = bytes[i];
200 }
201 }
202 for ( ; i < resultCount; ++i) {
203 result[i] = 0;
204 }
robertphillips@google.com6d837aa2013-10-11 14:38:46 +0000205
206 SkMask::FreeImage(src.fImage);
207 SkMask::FreeImage(dst.fImage);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000208}
209
210// Implement a step function that is 255 between min and max; 0 elsewhere.
211static int step(int x, SkScalar min, SkScalar max) {
212 if (min < x && x < max) {
213 return 255;
214 }
215 return 0;
216}
217
218// Implement a Gaussian function with 0 mean and std.dev. of 'sigma'.
219static float gaussian(int x, SkScalar sigma) {
robertphillips@google.comb85564a2013-09-05 17:53:34 +0000220 float k = SK_Scalar1/(sigma * sqrtf(2.0f*SK_ScalarPI));
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000221 float exponent = -(x * x) / (2 * sigma * sigma);
robertphillips@google.comb85564a2013-09-05 17:53:34 +0000222 return k * expf(exponent);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000223}
224
225// Perform a brute force convolution of a step function with a Gaussian.
226// Return the right half in 'result'
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000227static void brute_force_1d(SkScalar stepMin, SkScalar stepMax,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000228 SkScalar gaussianSigma,
229 int* result, int resultCount) {
230
231 int gaussianRange = SkScalarCeilToInt(10 * gaussianSigma);
232
233 for (int i = 0; i < resultCount; ++i) {
234 SkScalar sum = 0.0f;
235 for (int j = -gaussianRange; j < gaussianRange; ++j) {
236 sum += gaussian(j, gaussianSigma) * step(i-j, stepMin, stepMax);
237 }
238
239 result[i] = SkClampMax(SkClampPos(int(sum + 0.5f)), 255);
240 }
241}
242
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000243static void blur_path(SkCanvas* canvas, const SkPath& path,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000244 SkScalar gaussianSigma) {
245
246 SkScalar midX = path.getBounds().centerX();
247 SkScalar midY = path.getBounds().centerY();
248
249 canvas->translate(-midX, -midY);
250
251 SkPaint blurPaint;
252 blurPaint.setColor(SK_ColorWHITE);
Mike Reed18e75562018-03-12 14:03:47 -0400253 blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, gaussianSigma));
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000254
255 canvas->drawColor(SK_ColorBLACK);
256 canvas->drawPath(path, blurPaint);
257}
258
259// Readback the blurred draw results from the canvas
Mike Reedf1942192017-07-21 14:24:29 -0400260static void readback(const SkBitmap& src, int* result, int resultCount) {
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000261 SkBitmap readback;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000262 readback.allocN32Pixels(resultCount, 30);
Mike Reedf1942192017-07-21 14:24:29 -0400263 SkPixmap pm;
264 readback.peekPixels(&pm);
265 src.readPixels(pm, 0, 0);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000266
Mike Reedf1942192017-07-21 14:24:29 -0400267 const SkPMColor* pixels = pm.addr32(0, 15);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000268
269 for (int i = 0; i < resultCount; ++i) {
270 result[i] = SkColorGetR(pixels[i]);
271 }
272}
273
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000274// Draw a blurred version of the provided path.
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000275// Return the right half of the middle row in 'result'.
276static void cpu_blur_path(const SkPath& path, SkScalar gaussianSigma,
277 int* result, int resultCount) {
278
279 SkBitmap bitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000280 bitmap.allocN32Pixels(resultCount, 30);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000281 SkCanvas canvas(bitmap);
282
283 blur_path(&canvas, path, gaussianSigma);
Mike Reedf1942192017-07-21 14:24:29 -0400284 readback(bitmap, result, resultCount);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000285}
286
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000287#if WRITE_CSV
288static void write_as_csv(const char* label, SkScalar scale, int* data, int count) {
289 SkDebugf("%s_%.2f,", label, scale);
290 for (int i = 0; i < count-1; ++i) {
291 SkDebugf("%d,", data[i]);
292 }
293 SkDebugf("%d\n", data[count-1]);
294}
295#endif
296
297static bool match(int* first, int* second, int count, int tol) {
298 int delta;
299 for (int i = 0; i < count; ++i) {
300 delta = first[i] - second[i];
301 if (delta > tol || delta < -tol) {
302 return false;
303 }
304 }
305
306 return true;
307}
308
309// Test out the normal blur style with a wide range of sigmas
bsalomonf2f1c172016-04-05 12:59:06 -0700310DEF_TEST(BlurSigmaRange, reporter) {
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000311 static const int kSize = 100;
312
313 // The geometry is offset a smidge to trigger:
314 // https://code.google.com/p/chromium/issues/detail?id=282418
315 SkPath rectPath;
316 rectPath.addRect(0.3f, 0.3f, 100.3f, 100.3f);
317
318 SkPoint polyPts[] = {
319 { 0.3f, 0.3f },
320 { 100.3f, 0.3f },
321 { 100.3f, 100.3f },
322 { 0.3f, 100.3f },
323 { 2.3f, 50.3f } // a little divet to throw off the rect special case
324 };
325 SkPath polyPath;
326 polyPath.addPoly(polyPts, SK_ARRAY_COUNT(polyPts), true);
327
328 int rectSpecialCaseResult[kSize];
329 int generalCaseResult[kSize];
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000330 int groundTruthResult[kSize];
331 int bruteForce1DResult[kSize];
332
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000333 SkScalar sigma = 10.0f;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000334
335 for (int i = 0; i < 4; ++i, sigma /= 10) {
336
337 cpu_blur_path(rectPath, sigma, rectSpecialCaseResult, kSize);
338 cpu_blur_path(polyPath, sigma, generalCaseResult, kSize);
humper4a24cd82014-06-17 13:39:29 -0700339
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000340 ground_truth_2d(100, 100, sigma, groundTruthResult, kSize);
341 brute_force_1d(-50.0f, 50.0f, sigma, bruteForce1DResult, kSize);
342
343 REPORTER_ASSERT(reporter, match(rectSpecialCaseResult, bruteForce1DResult, kSize, 5));
344 REPORTER_ASSERT(reporter, match(generalCaseResult, bruteForce1DResult, kSize, 15));
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000345 REPORTER_ASSERT(reporter, match(groundTruthResult, bruteForce1DResult, kSize, 1));
346
347#if WRITE_CSV
348 write_as_csv("RectSpecialCase", sigma, rectSpecialCaseResult, kSize);
349 write_as_csv("GeneralCase", sigma, generalCaseResult, kSize);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000350 write_as_csv("GPU", sigma, gpuResult, kSize);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000351 write_as_csv("GroundTruth2D", sigma, groundTruthResult, kSize);
352 write_as_csv("BruteForce1D", sigma, bruteForce1DResult, kSize);
353#endif
354 }
355}
356
reed@google.comdaaafa62014-04-29 15:20:16 +0000357///////////////////////////////////////////////////////////////////////////////////////////
358
Mike Reed18e75562018-03-12 14:03:47 -0400359static void test_blurDrawLooper(skiatest::Reporter* reporter, SkScalar sigma, SkBlurStyle style) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000360 if (kNormal_SkBlurStyle != style) {
361 return; // blurdrawlooper only supports normal
362 }
363
364 const SkColor color = 0xFF335577;
365 const SkScalar dx = 10;
366 const SkScalar dy = -5;
Matt Sarettf160ad42017-03-23 16:23:38 -0400367 sk_sp<SkDrawLooper> lp(SkBlurDrawLooper::Make(color, sigma, dx, dy));
368 const bool expectSuccess = sigma > 0;
reed@google.comdaaafa62014-04-29 15:20:16 +0000369
reed7b380d02016-03-21 13:25:16 -0700370 if (nullptr == lp) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000371 REPORTER_ASSERT(reporter, sigma <= 0);
372 } else {
373 SkDrawLooper::BlurShadowRec rec;
374 bool success = lp->asABlurShadow(&rec);
375 REPORTER_ASSERT(reporter, success == expectSuccess);
376 if (success) {
377 REPORTER_ASSERT(reporter, rec.fSigma == sigma);
378 REPORTER_ASSERT(reporter, rec.fOffset.x() == dx);
379 REPORTER_ASSERT(reporter, rec.fOffset.y() == dy);
380 REPORTER_ASSERT(reporter, rec.fColor == color);
381 REPORTER_ASSERT(reporter, rec.fStyle == style);
reed@google.comdaaafa62014-04-29 15:20:16 +0000382 }
383 }
384}
385
reed7b380d02016-03-21 13:25:16 -0700386static void test_looper(skiatest::Reporter* reporter, sk_sp<SkDrawLooper> lp, SkScalar sigma,
Mike Reed18e75562018-03-12 14:03:47 -0400387 SkBlurStyle style, bool expectSuccess) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000388 SkDrawLooper::BlurShadowRec rec;
389 bool success = lp->asABlurShadow(&rec);
390 REPORTER_ASSERT(reporter, success == expectSuccess);
391 if (success != expectSuccess) {
392 lp->asABlurShadow(&rec);
393 }
394 if (success) {
395 REPORTER_ASSERT(reporter, rec.fSigma == sigma);
396 REPORTER_ASSERT(reporter, rec.fStyle == style);
reed@google.comdaaafa62014-04-29 15:20:16 +0000397 }
reed@google.comdaaafa62014-04-29 15:20:16 +0000398}
399
400static void make_noop_layer(SkLayerDrawLooper::Builder* builder) {
401 SkLayerDrawLooper::LayerInfo info;
402
403 info.fPaintBits = 0;
Mike Reedfaba3712016-11-03 14:45:31 -0400404 info.fColorMode = SkBlendMode::kDst;
reed@google.comdaaafa62014-04-29 15:20:16 +0000405 builder->addLayer(info);
406}
407
reedefdfd512016-04-04 10:02:58 -0700408static void make_blur_layer(SkLayerDrawLooper::Builder* builder, sk_sp<SkMaskFilter> mf) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000409 SkLayerDrawLooper::LayerInfo info;
410
411 info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit;
Mike Reedfaba3712016-11-03 14:45:31 -0400412 info.fColorMode = SkBlendMode::kSrc;
reed@google.comdaaafa62014-04-29 15:20:16 +0000413 SkPaint* paint = builder->addLayer(info);
reedefdfd512016-04-04 10:02:58 -0700414 paint->setMaskFilter(std::move(mf));
reed@google.comdaaafa62014-04-29 15:20:16 +0000415}
416
reedefdfd512016-04-04 10:02:58 -0700417static void test_layerDrawLooper(skiatest::Reporter* reporter, sk_sp<SkMaskFilter> mf,
Mike Reed18e75562018-03-12 14:03:47 -0400418 SkScalar sigma, SkBlurStyle style, bool expectSuccess) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000419
420 SkLayerDrawLooper::LayerInfo info;
421 SkLayerDrawLooper::Builder builder;
422
423 // 1 layer is too few
424 make_noop_layer(&builder);
Mike Reed18e75562018-03-12 14:03:47 -0400425 test_looper(reporter, builder.detach(), sigma, style, false);
reed@google.comdaaafa62014-04-29 15:20:16 +0000426
427 // 2 layers is good, but need blur
428 make_noop_layer(&builder);
429 make_noop_layer(&builder);
Mike Reed18e75562018-03-12 14:03:47 -0400430 test_looper(reporter, builder.detach(), sigma, style, false);
reed@google.comdaaafa62014-04-29 15:20:16 +0000431
432 // 2 layers is just right
433 make_noop_layer(&builder);
434 make_blur_layer(&builder, mf);
Mike Reed18e75562018-03-12 14:03:47 -0400435 test_looper(reporter, builder.detach(), sigma, style, expectSuccess);
reed@google.comdaaafa62014-04-29 15:20:16 +0000436
437 // 3 layers is too many
438 make_noop_layer(&builder);
439 make_blur_layer(&builder, mf);
440 make_noop_layer(&builder);
Mike Reed18e75562018-03-12 14:03:47 -0400441 test_looper(reporter, builder.detach(), sigma, style, false);
reed@google.comdaaafa62014-04-29 15:20:16 +0000442}
443
kkinnunen15302832015-12-01 04:35:26 -0800444DEF_TEST(BlurAsABlur, reporter) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000445 const SkBlurStyle styles[] = {
446 kNormal_SkBlurStyle, kSolid_SkBlurStyle, kOuter_SkBlurStyle, kInner_SkBlurStyle
447 };
448 const SkScalar sigmas[] = {
449 // values <= 0 should not success for a blur
450 -1, 0, 0.5f, 2
451 };
452
453 // Test asABlur for SkBlurMaskFilter
454 //
455 for (size_t i = 0; i < SK_ARRAY_COUNT(styles); ++i) {
Robert Phillips98624d22016-12-19 11:37:37 -0500456 const SkBlurStyle style = styles[i];
reed@google.comdaaafa62014-04-29 15:20:16 +0000457 for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) {
458 const SkScalar sigma = sigmas[j];
Mike Reed18e75562018-03-12 14:03:47 -0400459 for (bool respectCTM : { false, true }) {
460 sk_sp<SkMaskFilter> mf(SkMaskFilter::MakeBlur(style, sigma, respectCTM));
halcanary96fcdcc2015-08-27 07:41:13 -0700461 if (nullptr == mf.get()) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000462 REPORTER_ASSERT(reporter, sigma <= 0);
463 } else {
464 REPORTER_ASSERT(reporter, sigma > 0);
Mike Reed80747ef2018-01-23 15:29:32 -0500465 SkMaskFilterBase::BlurRec rec;
466 bool success = as_MFB(mf)->asABlur(&rec);
Mike Reed18e75562018-03-12 14:03:47 -0400467 if (respectCTM) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000468 REPORTER_ASSERT(reporter, success);
469 REPORTER_ASSERT(reporter, rec.fSigma == sigma);
470 REPORTER_ASSERT(reporter, rec.fStyle == style);
Mike Reed18e75562018-03-12 14:03:47 -0400471 } else {
472 REPORTER_ASSERT(reporter, !success);
reed@google.comdaaafa62014-04-29 15:20:16 +0000473 }
Mike Reed18e75562018-03-12 14:03:47 -0400474 test_layerDrawLooper(reporter, std::move(mf), sigma, style, success);
reed@google.comdaaafa62014-04-29 15:20:16 +0000475 }
Mike Reed18e75562018-03-12 14:03:47 -0400476 test_blurDrawLooper(reporter, sigma, style);
reed@google.comdaaafa62014-04-29 15:20:16 +0000477 }
478 }
479 }
480
481 // Test asABlur for SkEmbossMaskFilter -- should never succeed
482 //
483 {
484 SkEmbossMaskFilter::Light light = {
485 { 1, 1, 1 }, 0, 127, 127
486 };
487 for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) {
488 const SkScalar sigma = sigmas[j];
reedefdfd512016-04-04 10:02:58 -0700489 auto mf(SkEmbossMaskFilter::Make(sigma, light));
490 if (mf) {
Mike Reed80747ef2018-01-23 15:29:32 -0500491 SkMaskFilterBase::BlurRec rec;
492 bool success = as_MFB(mf)->asABlur(&rec);
reed@google.comdaaafa62014-04-29 15:20:16 +0000493 REPORTER_ASSERT(reporter, !success);
494 }
495 }
496 }
497}
498
halcanary9d524f22016-03-29 09:03:52 -0700499// This exercises the problem discovered in crbug.com/570232. The return value from
robertphillips4abb0c12016-01-04 11:20:25 -0800500// SkBlurMask::BoxBlur wasn't being checked in SkBlurMaskFilter.cpp::GrRRectBlurEffect::Create
egdanielab527a52016-06-28 08:07:26 -0700501DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SmallBoxBlurBug, reporter, ctxInfo) {
robertphillips4abb0c12016-01-04 11:20:25 -0800502
503 SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128);
bsalomon8b7451a2016-05-11 06:33:06 -0700504 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
robertphillips4abb0c12016-01-04 11:20:25 -0800505 SkCanvas* canvas = surface->getCanvas();
506
507 SkRect r = SkRect::MakeXYWH(10, 10, 100, 100);
508 SkRRect rr = SkRRect::MakeRectXY(r, 10, 10);
509
510 SkPaint p;
Mike Reed18e75562018-03-12 14:03:47 -0400511 p.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 0.01f));
robertphillips4abb0c12016-01-04 11:20:25 -0800512
513 canvas->drawRRect(rr, p);
514}
515
robertphillipsf5a83e82016-08-10 12:00:09 -0700516DEF_TEST(BlurredRRectNinePatchComputation, reporter) {
517 const SkRect r = SkRect::MakeXYWH(10, 10, 100, 100);
robertphillipsc4d2f902016-08-16 09:30:03 -0700518 static const SkScalar kBlurRad = 3.0f;
robertphillipsf5a83e82016-08-10 12:00:09 -0700519
520 bool ninePatchable;
521 SkRRect rrectToDraw;
522 SkISize size;
Mike Reed28d47732018-03-05 16:56:52 -0500523 SkScalar rectXs[kSkBlurRRectMaxDivisions],
524 rectYs[kSkBlurRRectMaxDivisions];
525 SkScalar texXs[kSkBlurRRectMaxDivisions],
526 texYs[kSkBlurRRectMaxDivisions];
robertphillipsc4d2f902016-08-16 09:30:03 -0700527 int numX, numY;
528 uint32_t skipMask;
robertphillipsf5a83e82016-08-10 12:00:09 -0700529
530 // not nine-patchable
531 {
532 SkVector radii[4] = { { 100, 100 }, { 0, 0 }, { 100, 100 }, { 0, 0 } };
533
534 SkRRect rr;
535 rr.setRectRadii(r, radii);
536
Mike Reed28d47732018-03-05 16:56:52 -0500537 ninePatchable = SkComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(),
538 kBlurRad, kBlurRad,
539 &rrectToDraw, &size,
540 rectXs, rectYs, texXs, texYs,
541 &numX, &numY, &skipMask);
robertphillipsf5a83e82016-08-10 12:00:09 -0700542 REPORTER_ASSERT(reporter, !ninePatchable);
543 }
544
545 // simple circular
546 {
robertphillipsc4d2f902016-08-16 09:30:03 -0700547 static const SkScalar kCornerRad = 10.0f;
robertphillipsf5a83e82016-08-10 12:00:09 -0700548 SkRRect rr;
robertphillipsc4d2f902016-08-16 09:30:03 -0700549 rr.setRectXY(r, kCornerRad, kCornerRad);
robertphillipsf5a83e82016-08-10 12:00:09 -0700550
Mike Reed28d47732018-03-05 16:56:52 -0500551 ninePatchable = SkComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(),
552 kBlurRad, kBlurRad,
553 &rrectToDraw, &size,
554 rectXs, rectYs, texXs, texYs,
555 &numX, &numY, &skipMask);
robertphillipsc4d2f902016-08-16 09:30:03 -0700556
557 static const SkScalar kAns = 12.0f * kBlurRad + 2.0f * kCornerRad + 1.0f;
robertphillipsf5a83e82016-08-10 12:00:09 -0700558 REPORTER_ASSERT(reporter, ninePatchable);
robertphillipsc4d2f902016-08-16 09:30:03 -0700559 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fWidth), kAns));
560 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fHeight), kAns));
561 REPORTER_ASSERT(reporter, 4 == numX && 4 == numY);
562 REPORTER_ASSERT(reporter, !skipMask);
robertphillipsf5a83e82016-08-10 12:00:09 -0700563 }
564
565 // simple elliptical
566 {
robertphillipsc4d2f902016-08-16 09:30:03 -0700567 static const SkScalar kXCornerRad = 2.0f;
568 static const SkScalar kYCornerRad = 10.0f;
robertphillipsf5a83e82016-08-10 12:00:09 -0700569 SkRRect rr;
robertphillipsc4d2f902016-08-16 09:30:03 -0700570 rr.setRectXY(r, kXCornerRad, kYCornerRad);
robertphillipsf5a83e82016-08-10 12:00:09 -0700571
Mike Reed28d47732018-03-05 16:56:52 -0500572 ninePatchable = SkComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(),
573 kBlurRad, kBlurRad,
574 &rrectToDraw, &size,
575 rectXs, rectYs, texXs, texYs,
576 &numX, &numY, &skipMask);
robertphillipsc4d2f902016-08-16 09:30:03 -0700577
578 static const SkScalar kXAns = 12.0f * kBlurRad + 2.0f * kXCornerRad + 1.0f;
579 static const SkScalar kYAns = 12.0f * kBlurRad + 2.0f * kYCornerRad + 1.0f;
580
robertphillipsf5a83e82016-08-10 12:00:09 -0700581 REPORTER_ASSERT(reporter, ninePatchable);
robertphillipsc4d2f902016-08-16 09:30:03 -0700582 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fWidth), kXAns));
583 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fHeight), kYAns));
584 REPORTER_ASSERT(reporter, 4 == numX && 4 == numY);
585 REPORTER_ASSERT(reporter, !skipMask);
586 }
587
588 // test-out occlusion
589 {
590 static const SkScalar kCornerRad = 10.0f;
591 SkRRect rr;
592 rr.setRectXY(r, kCornerRad, kCornerRad);
593
594 // The rectXs & rectYs should be { 1, 29, 91, 119 }. Add two more points around each.
595 SkScalar testLocs[] = {
596 -18.0f, -9.0f,
597 1.0f,
Ben Wagner63fd7602017-10-09 15:45:33 -0400598 9.0f, 18.0f,
599 29.0f,
robertphillipsc4d2f902016-08-16 09:30:03 -0700600 39.0f, 49.0f,
601 91.0f,
602 109.0f, 118.0f,
603 119.0f,
604 139.0f, 149.0f
605 };
606
607 for (int minY = 0; minY < (int)SK_ARRAY_COUNT(testLocs); ++minY) {
608 for (int maxY = minY+1; maxY < (int)SK_ARRAY_COUNT(testLocs); ++maxY) {
609 for (int minX = 0; minX < (int)SK_ARRAY_COUNT(testLocs); ++minX) {
610 for (int maxX = minX+1; maxX < (int)SK_ARRAY_COUNT(testLocs); ++maxX) {
611 SkRect occluder = SkRect::MakeLTRB(testLocs[minX], testLocs[minY],
612 testLocs[maxX], testLocs[maxY]);
613 if (occluder.isEmpty()) {
614 continue;
615 }
616
Mike Reed28d47732018-03-05 16:56:52 -0500617 ninePatchable = SkComputeBlurredRRectParams(rr, rr, occluder,
robertphillipsc4d2f902016-08-16 09:30:03 -0700618 kBlurRad, kBlurRad,
619 &rrectToDraw, &size,
620 rectXs, rectYs, texXs, texYs,
Ben Wagner63fd7602017-10-09 15:45:33 -0400621 &numX, &numY, &skipMask);
robertphillipsc4d2f902016-08-16 09:30:03 -0700622
623 static const SkScalar kAns = 12.0f * kBlurRad + 2.0f * kCornerRad + 1.0f;
624 REPORTER_ASSERT(reporter, ninePatchable);
625 REPORTER_ASSERT(reporter,
626 SkScalarNearlyEqual(SkIntToScalar(size.fWidth), kAns));
627 REPORTER_ASSERT(reporter,
628 SkScalarNearlyEqual(SkIntToScalar(size.fHeight), kAns));
629
630 int checkBit = 0x1;
631 for (int y = 0; y < numY-1; ++y) {
632 for (int x = 0; x < numX-1; ++x) {
633 SkRect cell = SkRect::MakeLTRB(rectXs[x], rectYs[y],
634 rectXs[x+1], rectYs[y+1]);
635 REPORTER_ASSERT(reporter,
636 SkToBool(skipMask & checkBit) ==
637 (cell.isEmpty() || occluder.contains(cell)));
638
639 REPORTER_ASSERT(reporter, texXs[x] >= 0 &&
640 texXs[x] <= size.fWidth);
641 REPORTER_ASSERT(reporter, texYs[y] >= 0 &&
642 texXs[y] <= size.fHeight);
643
644 checkBit <<= 1;
645 }
646 }
647 }
648 }
649 }
robertphillipsf5a83e82016-08-10 12:00:09 -0700650 }
robertphillipsc4d2f902016-08-16 09:30:03 -0700651
652
robertphillipsf5a83e82016-08-10 12:00:09 -0700653 }
654
655}
656
Florin Malita1ba5bfe2017-11-28 17:54:23 -0500657// https://crbugs.com/787712
658DEF_TEST(EmbossPerlinCrash, reporter) {
659 SkPaint p;
660
661 static constexpr SkEmbossMaskFilter::Light light = {
662 { 1, 1, 1 }, 0, 127, 127
663 };
664 p.setMaskFilter(SkEmbossMaskFilter::Make(1, light));
665 p.setShader(SkPerlinNoiseShader::MakeFractalNoise(1.0f, 1.0f, 2, 0.0f));
666
667 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(100, 100);
668 surface->getCanvas()->drawPaint(p);
669}
670
reed@google.comdaaafa62014-04-29 15:20:16 +0000671///////////////////////////////////////////////////////////////////////////////////////////
Mike Reedf221b492018-02-15 11:38:20 -0500672
673DEF_TEST(BlurZeroSigma, reporter) {
674 auto surf = SkSurface::MakeRasterN32Premul(20, 20);
675 SkPaint paint;
676 paint.setAntiAlias(true);
677
678 const SkIRect ir = { 5, 5, 15, 15 };
679 const SkRect r = SkRect::Make(ir);
680
681 const SkScalar sigmas[] = { 0, SkBits2Float(1) };
682 // if sigma is zero (or nearly so), we need to draw correctly (unblurred) and not crash
683 // or assert.
684 for (auto sigma : sigmas) {
Mike Reed18e75562018-03-12 14:03:47 -0400685 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma));
Mike Reedf221b492018-02-15 11:38:20 -0500686 surf->getCanvas()->drawRect(r, paint);
687
688 sk_tool_utils::PixelIter iter(surf.get());
689 SkIPoint loc;
690 while (const SkPMColor* p = (const SkPMColor*)iter.next(&loc)) {
691 if (ir.contains(loc.fX, loc.fY)) {
692 // inside the rect we draw (opaque black)
693 REPORTER_ASSERT(reporter, *p == SkPackARGB32(0xFF, 0, 0, 0));
694 } else {
695 // outside the rect we didn't draw at all, no blurred edges
696 REPORTER_ASSERT(reporter, *p == 0);
697 }
698 }
699 }
700}
701
Robert Phillipsc90b4db2018-04-23 15:12:00 +0000702
703///////////////////////////////////////////////////////////////////////////////////////////
Robert Phillipsc90b4db2018-04-23 15:12:00 +0000704
705DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BlurMaskBiggerThanDest, reporter, ctxInfo) {
706 GrContext* context = ctxInfo.grContext();
707
708 SkImageInfo ii = SkImageInfo::Make(32, 32, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
709
710 sk_sp<SkSurface> dst(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii));
711 if (!dst) {
712 ERRORF(reporter, "Could not create surface for test.");
713 return;
714 }
715
716 SkPaint p;
717 p.setColor(SK_ColorRED);
718 p.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 3));
719
720 SkCanvas* canvas = dst->getCanvas();
721
722 canvas->clear(SK_ColorBLACK);
723 canvas->drawCircle(SkPoint::Make(16, 16), 8, p);
724
725 SkBitmap readback;
726 SkAssertResult(readback.tryAllocPixels(ii));
727
728 canvas->readPixels(readback, 0, 0);
729 REPORTER_ASSERT(reporter, SkColorGetR(readback.getColor(15, 15)) > 128);
730 REPORTER_ASSERT(reporter, SkColorGetG(readback.getColor(15, 15)) == 0);
731 REPORTER_ASSERT(reporter, SkColorGetB(readback.getColor(15, 15)) == 0);
732 REPORTER_ASSERT(reporter, readback.getColor(31, 31) == SK_ColorBLACK);
733}