blob: 36723f6fa61899724a46928cb31cd9c14f59d865 [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"
Hal Canary2d404902018-09-06 11:20:23 -040026#include "SkMathPriv.h"
bungeman@google.com5af16f82011-09-02 15:06:44 +000027#include "SkPaint.h"
bungemand3ebb482015-08-05 13:57:49 -070028#include "SkPath.h"
Florin Malita1ba5bfe2017-11-28 17:54:23 -050029#include "SkPerlinNoiseShader.h"
Ben Wagnere6b04a12018-03-09 14:41:36 -050030#include "SkPixmap.h"
31#include "SkPoint.h"
32#include "SkRRect.h"
Mike Reed1f275852018-04-11 14:30:17 -040033#include "SkRectPriv.h"
Ben Wagnere6b04a12018-03-09 14:41:36 -050034#include "SkRefCnt.h"
35#include "SkScalar.h"
36#include "SkShader.h"
37#include "SkSize.h"
Florin Malita1ba5bfe2017-11-28 17:54:23 -050038#include "SkSurface.h"
Ben Wagnere6b04a12018-03-09 14:41:36 -050039#include "SkTypes.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000040#include "Test.h"
Ben Wagnere6b04a12018-03-09 14:41:36 -050041#include "sk_pixel_iter.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000042
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +000043#include "GrContextFactory.h"
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +000044
Ben Wagnere6b04a12018-03-09 14:41:36 -050045#include <math.h>
46#include <string.h>
47#include <utility>
48
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +000049#define WRITE_CSV 0
reed@google.com2b75f422011-07-07 13:43:38 +000050
51///////////////////////////////////////////////////////////////////////////////
52
53#define ILLEGAL_MODE ((SkXfermode::Mode)-1)
54
bungeman@google.com5af16f82011-09-02 15:06:44 +000055static const int outset = 100;
56static const SkColor bgColor = SK_ColorWHITE;
57static const int strokeWidth = 4;
58
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000059static void create(SkBitmap* bm, const SkIRect& bound) {
60 bm->allocN32Pixels(bound.width(), bound.height());
bungeman@google.com5af16f82011-09-02 15:06:44 +000061}
62
63static void drawBG(SkCanvas* canvas) {
64 canvas->drawColor(bgColor);
65}
66
67
68struct BlurTest {
69 void (*addPath)(SkPath*);
70 int viewLen;
71 SkIRect views[9];
72};
73
74//Path Draw Procs
75//Beware that paths themselves my draw differently depending on the clip.
76static void draw50x50Rect(SkPath* path) {
77 path->addRect(0, 0, SkIntToScalar(50), SkIntToScalar(50));
78}
79
80//Tests
81static BlurTest tests[] = {
82 { draw50x50Rect, 3, {
83 //inner half of blur
84 { 0, 0, 50, 50 },
85 //blur, but no path.
86 { 50 + strokeWidth/2, 50 + strokeWidth/2, 100, 100 },
87 //just an edge
88 { 40, strokeWidth, 60, 50 - strokeWidth },
89 }},
90};
91
92/** Assumes that the ref draw was completely inside ref canvas --
93 implies that everything outside is "bgColor".
94 Checks that all overlap is the same and that all non-overlap on the
95 ref is "bgColor".
96 */
97static bool compare(const SkBitmap& ref, const SkIRect& iref,
98 const SkBitmap& test, const SkIRect& itest)
99{
100 const int xOff = itest.fLeft - iref.fLeft;
101 const int yOff = itest.fTop - iref.fTop;
102
bungeman@google.com5af16f82011-09-02 15:06:44 +0000103 for (int y = 0; y < test.height(); ++y) {
104 for (int x = 0; x < test.width(); ++x) {
105 SkColor testColor = test.getColor(x, y);
106 int refX = x + xOff;
107 int refY = y + yOff;
108 SkColor refColor;
109 if (refX >= 0 && refX < ref.width() &&
110 refY >= 0 && refY < ref.height())
111 {
112 refColor = ref.getColor(refX, refY);
113 } else {
114 refColor = bgColor;
115 }
116 if (refColor != testColor) {
117 return false;
118 }
119 }
120 }
121 return true;
122}
123
kkinnunen15302832015-12-01 04:35:26 -0800124DEF_TEST(BlurDrawing, reporter) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000125 SkPaint paint;
126 paint.setColor(SK_ColorGRAY);
127 paint.setStyle(SkPaint::kStroke_Style);
128 paint.setStrokeWidth(SkIntToScalar(strokeWidth));
reed@google.com2b75f422011-07-07 13:43:38 +0000129
robertphillips@google.comb7061172013-09-06 14:16:12 +0000130 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5));
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000131 for (int style = 0; style <= kLastEnum_SkBlurStyle; ++style) {
132 SkBlurStyle blurStyle = static_cast<SkBlurStyle>(style);
reed@google.com2b75f422011-07-07 13:43:38 +0000133
Mike Reed18e75562018-03-12 14:03:47 -0400134 for (bool respectCTM : { false, true }) {
135 paint.setMaskFilter(SkMaskFilter::MakeBlur(blurStyle, sigma, respectCTM));
bungeman@google.com5af16f82011-09-02 15:06:44 +0000136
tomhudson@google.com83a44462011-10-27 15:27:51 +0000137 for (size_t test = 0; test < SK_ARRAY_COUNT(tests); ++test) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000138 SkPath path;
139 tests[test].addPath(&path);
bungeman@google.comd16872c2011-09-02 15:46:17 +0000140 SkPath strokedPath;
141 paint.getFillPath(path, &strokedPath);
142 SkRect refBound = strokedPath.getBounds();
bungeman@google.com5af16f82011-09-02 15:06:44 +0000143 SkIRect iref;
144 refBound.roundOut(&iref);
bungeman@google.comd16872c2011-09-02 15:46:17 +0000145 iref.inset(-outset, -outset);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000146 SkBitmap refBitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000147 create(&refBitmap, iref);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000148
149 SkCanvas refCanvas(refBitmap);
150 refCanvas.translate(SkIntToScalar(-iref.fLeft),
151 SkIntToScalar(-iref.fTop));
152 drawBG(&refCanvas);
153 refCanvas.drawPath(path, paint);
154
155 for (int view = 0; view < tests[test].viewLen; ++view) {
156 SkIRect itest = tests[test].views[view];
157 SkBitmap testBitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000158 create(&testBitmap, itest);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000159
160 SkCanvas testCanvas(testBitmap);
161 testCanvas.translate(SkIntToScalar(-itest.fLeft),
162 SkIntToScalar(-itest.fTop));
163 drawBG(&testCanvas);
164 testCanvas.drawPath(path, paint);
165
166 REPORTER_ASSERT(reporter,
167 compare(refBitmap, iref, testBitmap, itest));
168 }
169 }
170 }
reed@google.com2b75f422011-07-07 13:43:38 +0000171 }
172}
173
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000174///////////////////////////////////////////////////////////////////////////////
175
176// Use SkBlurMask::BlurGroundTruth to blur a 'width' x 'height' solid
177// white rect. Return the right half of the middle row in 'result'.
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000178static void ground_truth_2d(int width, int height,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000179 SkScalar sigma,
180 int* result, int resultCount) {
181 SkMask src, dst;
182
183 src.fBounds.set(0, 0, width, height);
184 src.fFormat = SkMask::kA8_Format;
185 src.fRowBytes = src.fBounds.width();
186 src.fImage = SkMask::AllocImage(src.computeTotalImageSize());
187
188 memset(src.fImage, 0xff, src.computeTotalImageSize());
189
robertphillipse80eb922015-12-17 11:33:12 -0800190 if (!SkBlurMask::BlurGroundTruth(sigma, &dst, src, kNormal_SkBlurStyle)) {
191 return;
192 }
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000193
Mike Reed1f275852018-04-11 14:30:17 -0400194 int midX = dst.fBounds.x() + dst.fBounds.width()/2;
195 int midY = dst.fBounds.y() + dst.fBounds.height()/2;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000196 uint8_t* bytes = dst.getAddr8(midX, midY);
197 int i;
198 for (i = 0; i < dst.fBounds.width()-(midX-dst.fBounds.fLeft); ++i) {
199 if (i < resultCount) {
200 result[i] = bytes[i];
201 }
202 }
203 for ( ; i < resultCount; ++i) {
204 result[i] = 0;
205 }
robertphillips@google.com6d837aa2013-10-11 14:38:46 +0000206
207 SkMask::FreeImage(src.fImage);
208 SkMask::FreeImage(dst.fImage);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000209}
210
211// Implement a step function that is 255 between min and max; 0 elsewhere.
212static int step(int x, SkScalar min, SkScalar max) {
213 if (min < x && x < max) {
214 return 255;
215 }
216 return 0;
217}
218
219// Implement a Gaussian function with 0 mean and std.dev. of 'sigma'.
220static float gaussian(int x, SkScalar sigma) {
robertphillips@google.comb85564a2013-09-05 17:53:34 +0000221 float k = SK_Scalar1/(sigma * sqrtf(2.0f*SK_ScalarPI));
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000222 float exponent = -(x * x) / (2 * sigma * sigma);
robertphillips@google.comb85564a2013-09-05 17:53:34 +0000223 return k * expf(exponent);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000224}
225
226// Perform a brute force convolution of a step function with a Gaussian.
227// Return the right half in 'result'
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000228static void brute_force_1d(SkScalar stepMin, SkScalar stepMax,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000229 SkScalar gaussianSigma,
230 int* result, int resultCount) {
231
232 int gaussianRange = SkScalarCeilToInt(10 * gaussianSigma);
233
234 for (int i = 0; i < resultCount; ++i) {
235 SkScalar sum = 0.0f;
236 for (int j = -gaussianRange; j < gaussianRange; ++j) {
237 sum += gaussian(j, gaussianSigma) * step(i-j, stepMin, stepMax);
238 }
239
240 result[i] = SkClampMax(SkClampPos(int(sum + 0.5f)), 255);
241 }
242}
243
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000244static void blur_path(SkCanvas* canvas, const SkPath& path,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000245 SkScalar gaussianSigma) {
246
247 SkScalar midX = path.getBounds().centerX();
248 SkScalar midY = path.getBounds().centerY();
249
250 canvas->translate(-midX, -midY);
251
252 SkPaint blurPaint;
253 blurPaint.setColor(SK_ColorWHITE);
Mike Reed18e75562018-03-12 14:03:47 -0400254 blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, gaussianSigma));
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000255
256 canvas->drawColor(SK_ColorBLACK);
257 canvas->drawPath(path, blurPaint);
258}
259
260// Readback the blurred draw results from the canvas
Mike Reedf1942192017-07-21 14:24:29 -0400261static void readback(const SkBitmap& src, int* result, int resultCount) {
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000262 SkBitmap readback;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000263 readback.allocN32Pixels(resultCount, 30);
Mike Reedf1942192017-07-21 14:24:29 -0400264 SkPixmap pm;
265 readback.peekPixels(&pm);
266 src.readPixels(pm, 0, 0);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000267
Mike Reedf1942192017-07-21 14:24:29 -0400268 const SkPMColor* pixels = pm.addr32(0, 15);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000269
270 for (int i = 0; i < resultCount; ++i) {
271 result[i] = SkColorGetR(pixels[i]);
272 }
273}
274
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000275// Draw a blurred version of the provided path.
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000276// Return the right half of the middle row in 'result'.
277static void cpu_blur_path(const SkPath& path, SkScalar gaussianSigma,
278 int* result, int resultCount) {
279
280 SkBitmap bitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000281 bitmap.allocN32Pixels(resultCount, 30);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000282 SkCanvas canvas(bitmap);
283
284 blur_path(&canvas, path, gaussianSigma);
Mike Reedf1942192017-07-21 14:24:29 -0400285 readback(bitmap, result, resultCount);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000286}
287
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000288#if WRITE_CSV
289static void write_as_csv(const char* label, SkScalar scale, int* data, int count) {
290 SkDebugf("%s_%.2f,", label, scale);
291 for (int i = 0; i < count-1; ++i) {
292 SkDebugf("%d,", data[i]);
293 }
294 SkDebugf("%d\n", data[count-1]);
295}
296#endif
297
298static bool match(int* first, int* second, int count, int tol) {
299 int delta;
300 for (int i = 0; i < count; ++i) {
301 delta = first[i] - second[i];
302 if (delta > tol || delta < -tol) {
303 return false;
304 }
305 }
306
307 return true;
308}
309
310// Test out the normal blur style with a wide range of sigmas
bsalomonf2f1c172016-04-05 12:59:06 -0700311DEF_TEST(BlurSigmaRange, reporter) {
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000312 static const int kSize = 100;
313
314 // The geometry is offset a smidge to trigger:
315 // https://code.google.com/p/chromium/issues/detail?id=282418
316 SkPath rectPath;
317 rectPath.addRect(0.3f, 0.3f, 100.3f, 100.3f);
318
319 SkPoint polyPts[] = {
320 { 0.3f, 0.3f },
321 { 100.3f, 0.3f },
322 { 100.3f, 100.3f },
323 { 0.3f, 100.3f },
324 { 2.3f, 50.3f } // a little divet to throw off the rect special case
325 };
326 SkPath polyPath;
327 polyPath.addPoly(polyPts, SK_ARRAY_COUNT(polyPts), true);
328
329 int rectSpecialCaseResult[kSize];
330 int generalCaseResult[kSize];
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000331 int groundTruthResult[kSize];
332 int bruteForce1DResult[kSize];
333
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000334 SkScalar sigma = 10.0f;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000335
336 for (int i = 0; i < 4; ++i, sigma /= 10) {
337
338 cpu_blur_path(rectPath, sigma, rectSpecialCaseResult, kSize);
339 cpu_blur_path(polyPath, sigma, generalCaseResult, kSize);
humper4a24cd82014-06-17 13:39:29 -0700340
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000341 ground_truth_2d(100, 100, sigma, groundTruthResult, kSize);
342 brute_force_1d(-50.0f, 50.0f, sigma, bruteForce1DResult, kSize);
343
344 REPORTER_ASSERT(reporter, match(rectSpecialCaseResult, bruteForce1DResult, kSize, 5));
345 REPORTER_ASSERT(reporter, match(generalCaseResult, bruteForce1DResult, kSize, 15));
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000346 REPORTER_ASSERT(reporter, match(groundTruthResult, bruteForce1DResult, kSize, 1));
347
348#if WRITE_CSV
349 write_as_csv("RectSpecialCase", sigma, rectSpecialCaseResult, kSize);
350 write_as_csv("GeneralCase", sigma, generalCaseResult, kSize);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000351 write_as_csv("GPU", sigma, gpuResult, kSize);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000352 write_as_csv("GroundTruth2D", sigma, groundTruthResult, kSize);
353 write_as_csv("BruteForce1D", sigma, bruteForce1DResult, kSize);
354#endif
355 }
356}
357
reed@google.comdaaafa62014-04-29 15:20:16 +0000358///////////////////////////////////////////////////////////////////////////////////////////
359
Mike Reed18e75562018-03-12 14:03:47 -0400360static void test_blurDrawLooper(skiatest::Reporter* reporter, SkScalar sigma, SkBlurStyle style) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000361 if (kNormal_SkBlurStyle != style) {
362 return; // blurdrawlooper only supports normal
363 }
364
365 const SkColor color = 0xFF335577;
366 const SkScalar dx = 10;
367 const SkScalar dy = -5;
Matt Sarettf160ad42017-03-23 16:23:38 -0400368 sk_sp<SkDrawLooper> lp(SkBlurDrawLooper::Make(color, sigma, dx, dy));
369 const bool expectSuccess = sigma > 0;
reed@google.comdaaafa62014-04-29 15:20:16 +0000370
reed7b380d02016-03-21 13:25:16 -0700371 if (nullptr == lp) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000372 REPORTER_ASSERT(reporter, sigma <= 0);
373 } else {
374 SkDrawLooper::BlurShadowRec rec;
375 bool success = lp->asABlurShadow(&rec);
376 REPORTER_ASSERT(reporter, success == expectSuccess);
377 if (success) {
378 REPORTER_ASSERT(reporter, rec.fSigma == sigma);
379 REPORTER_ASSERT(reporter, rec.fOffset.x() == dx);
380 REPORTER_ASSERT(reporter, rec.fOffset.y() == dy);
381 REPORTER_ASSERT(reporter, rec.fColor == color);
382 REPORTER_ASSERT(reporter, rec.fStyle == style);
reed@google.comdaaafa62014-04-29 15:20:16 +0000383 }
384 }
385}
386
reed7b380d02016-03-21 13:25:16 -0700387static void test_looper(skiatest::Reporter* reporter, sk_sp<SkDrawLooper> lp, SkScalar sigma,
Mike Reed18e75562018-03-12 14:03:47 -0400388 SkBlurStyle style, bool expectSuccess) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000389 SkDrawLooper::BlurShadowRec rec;
390 bool success = lp->asABlurShadow(&rec);
391 REPORTER_ASSERT(reporter, success == expectSuccess);
392 if (success != expectSuccess) {
393 lp->asABlurShadow(&rec);
394 }
395 if (success) {
396 REPORTER_ASSERT(reporter, rec.fSigma == sigma);
397 REPORTER_ASSERT(reporter, rec.fStyle == style);
reed@google.comdaaafa62014-04-29 15:20:16 +0000398 }
reed@google.comdaaafa62014-04-29 15:20:16 +0000399}
400
401static void make_noop_layer(SkLayerDrawLooper::Builder* builder) {
402 SkLayerDrawLooper::LayerInfo info;
403
404 info.fPaintBits = 0;
Mike Reedfaba3712016-11-03 14:45:31 -0400405 info.fColorMode = SkBlendMode::kDst;
reed@google.comdaaafa62014-04-29 15:20:16 +0000406 builder->addLayer(info);
407}
408
reedefdfd512016-04-04 10:02:58 -0700409static void make_blur_layer(SkLayerDrawLooper::Builder* builder, sk_sp<SkMaskFilter> mf) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000410 SkLayerDrawLooper::LayerInfo info;
411
412 info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit;
Mike Reedfaba3712016-11-03 14:45:31 -0400413 info.fColorMode = SkBlendMode::kSrc;
reed@google.comdaaafa62014-04-29 15:20:16 +0000414 SkPaint* paint = builder->addLayer(info);
reedefdfd512016-04-04 10:02:58 -0700415 paint->setMaskFilter(std::move(mf));
reed@google.comdaaafa62014-04-29 15:20:16 +0000416}
417
reedefdfd512016-04-04 10:02:58 -0700418static void test_layerDrawLooper(skiatest::Reporter* reporter, sk_sp<SkMaskFilter> mf,
Mike Reed18e75562018-03-12 14:03:47 -0400419 SkScalar sigma, SkBlurStyle style, bool expectSuccess) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000420
421 SkLayerDrawLooper::LayerInfo info;
422 SkLayerDrawLooper::Builder builder;
423
424 // 1 layer is too few
425 make_noop_layer(&builder);
Mike Reed18e75562018-03-12 14:03:47 -0400426 test_looper(reporter, builder.detach(), sigma, style, false);
reed@google.comdaaafa62014-04-29 15:20:16 +0000427
428 // 2 layers is good, but need blur
429 make_noop_layer(&builder);
430 make_noop_layer(&builder);
Mike Reed18e75562018-03-12 14:03:47 -0400431 test_looper(reporter, builder.detach(), sigma, style, false);
reed@google.comdaaafa62014-04-29 15:20:16 +0000432
433 // 2 layers is just right
434 make_noop_layer(&builder);
435 make_blur_layer(&builder, mf);
Mike Reed18e75562018-03-12 14:03:47 -0400436 test_looper(reporter, builder.detach(), sigma, style, expectSuccess);
reed@google.comdaaafa62014-04-29 15:20:16 +0000437
438 // 3 layers is too many
439 make_noop_layer(&builder);
440 make_blur_layer(&builder, mf);
441 make_noop_layer(&builder);
Mike Reed18e75562018-03-12 14:03:47 -0400442 test_looper(reporter, builder.detach(), sigma, style, false);
reed@google.comdaaafa62014-04-29 15:20:16 +0000443}
444
kkinnunen15302832015-12-01 04:35:26 -0800445DEF_TEST(BlurAsABlur, reporter) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000446 const SkBlurStyle styles[] = {
447 kNormal_SkBlurStyle, kSolid_SkBlurStyle, kOuter_SkBlurStyle, kInner_SkBlurStyle
448 };
449 const SkScalar sigmas[] = {
450 // values <= 0 should not success for a blur
451 -1, 0, 0.5f, 2
452 };
453
454 // Test asABlur for SkBlurMaskFilter
455 //
456 for (size_t i = 0; i < SK_ARRAY_COUNT(styles); ++i) {
Robert Phillips98624d22016-12-19 11:37:37 -0500457 const SkBlurStyle style = styles[i];
reed@google.comdaaafa62014-04-29 15:20:16 +0000458 for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) {
459 const SkScalar sigma = sigmas[j];
Mike Reed18e75562018-03-12 14:03:47 -0400460 for (bool respectCTM : { false, true }) {
461 sk_sp<SkMaskFilter> mf(SkMaskFilter::MakeBlur(style, sigma, respectCTM));
halcanary96fcdcc2015-08-27 07:41:13 -0700462 if (nullptr == mf.get()) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000463 REPORTER_ASSERT(reporter, sigma <= 0);
464 } else {
465 REPORTER_ASSERT(reporter, sigma > 0);
Mike Reed80747ef2018-01-23 15:29:32 -0500466 SkMaskFilterBase::BlurRec rec;
467 bool success = as_MFB(mf)->asABlur(&rec);
Mike Reed18e75562018-03-12 14:03:47 -0400468 if (respectCTM) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000469 REPORTER_ASSERT(reporter, success);
470 REPORTER_ASSERT(reporter, rec.fSigma == sigma);
471 REPORTER_ASSERT(reporter, rec.fStyle == style);
Mike Reed18e75562018-03-12 14:03:47 -0400472 } else {
473 REPORTER_ASSERT(reporter, !success);
reed@google.comdaaafa62014-04-29 15:20:16 +0000474 }
Mike Reed18e75562018-03-12 14:03:47 -0400475 test_layerDrawLooper(reporter, std::move(mf), sigma, style, success);
reed@google.comdaaafa62014-04-29 15:20:16 +0000476 }
Mike Reed18e75562018-03-12 14:03:47 -0400477 test_blurDrawLooper(reporter, sigma, style);
reed@google.comdaaafa62014-04-29 15:20:16 +0000478 }
479 }
480 }
481
482 // Test asABlur for SkEmbossMaskFilter -- should never succeed
483 //
484 {
485 SkEmbossMaskFilter::Light light = {
486 { 1, 1, 1 }, 0, 127, 127
487 };
488 for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) {
489 const SkScalar sigma = sigmas[j];
reedefdfd512016-04-04 10:02:58 -0700490 auto mf(SkEmbossMaskFilter::Make(sigma, light));
491 if (mf) {
Mike Reed80747ef2018-01-23 15:29:32 -0500492 SkMaskFilterBase::BlurRec rec;
493 bool success = as_MFB(mf)->asABlur(&rec);
reed@google.comdaaafa62014-04-29 15:20:16 +0000494 REPORTER_ASSERT(reporter, !success);
495 }
496 }
497 }
498}
499
halcanary9d524f22016-03-29 09:03:52 -0700500// This exercises the problem discovered in crbug.com/570232. The return value from
robertphillips4abb0c12016-01-04 11:20:25 -0800501// SkBlurMask::BoxBlur wasn't being checked in SkBlurMaskFilter.cpp::GrRRectBlurEffect::Create
egdanielab527a52016-06-28 08:07:26 -0700502DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SmallBoxBlurBug, reporter, ctxInfo) {
robertphillips4abb0c12016-01-04 11:20:25 -0800503
504 SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128);
bsalomon8b7451a2016-05-11 06:33:06 -0700505 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
robertphillips4abb0c12016-01-04 11:20:25 -0800506 SkCanvas* canvas = surface->getCanvas();
507
508 SkRect r = SkRect::MakeXYWH(10, 10, 100, 100);
509 SkRRect rr = SkRRect::MakeRectXY(r, 10, 10);
510
511 SkPaint p;
Mike Reed18e75562018-03-12 14:03:47 -0400512 p.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 0.01f));
robertphillips4abb0c12016-01-04 11:20:25 -0800513
514 canvas->drawRRect(rr, p);
515}
516
robertphillipsf5a83e82016-08-10 12:00:09 -0700517DEF_TEST(BlurredRRectNinePatchComputation, reporter) {
518 const SkRect r = SkRect::MakeXYWH(10, 10, 100, 100);
robertphillipsc4d2f902016-08-16 09:30:03 -0700519 static const SkScalar kBlurRad = 3.0f;
robertphillipsf5a83e82016-08-10 12:00:09 -0700520
521 bool ninePatchable;
522 SkRRect rrectToDraw;
523 SkISize size;
Mike Reed28d47732018-03-05 16:56:52 -0500524 SkScalar rectXs[kSkBlurRRectMaxDivisions],
525 rectYs[kSkBlurRRectMaxDivisions];
526 SkScalar texXs[kSkBlurRRectMaxDivisions],
527 texYs[kSkBlurRRectMaxDivisions];
robertphillipsc4d2f902016-08-16 09:30:03 -0700528 int numX, numY;
529 uint32_t skipMask;
robertphillipsf5a83e82016-08-10 12:00:09 -0700530
531 // not nine-patchable
532 {
533 SkVector radii[4] = { { 100, 100 }, { 0, 0 }, { 100, 100 }, { 0, 0 } };
534
535 SkRRect rr;
536 rr.setRectRadii(r, radii);
537
Mike Reed28d47732018-03-05 16:56:52 -0500538 ninePatchable = SkComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(),
539 kBlurRad, kBlurRad,
540 &rrectToDraw, &size,
541 rectXs, rectYs, texXs, texYs,
542 &numX, &numY, &skipMask);
robertphillipsf5a83e82016-08-10 12:00:09 -0700543 REPORTER_ASSERT(reporter, !ninePatchable);
544 }
545
546 // simple circular
547 {
robertphillipsc4d2f902016-08-16 09:30:03 -0700548 static const SkScalar kCornerRad = 10.0f;
robertphillipsf5a83e82016-08-10 12:00:09 -0700549 SkRRect rr;
robertphillipsc4d2f902016-08-16 09:30:03 -0700550 rr.setRectXY(r, kCornerRad, kCornerRad);
robertphillipsf5a83e82016-08-10 12:00:09 -0700551
Mike Reed28d47732018-03-05 16:56:52 -0500552 ninePatchable = SkComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(),
553 kBlurRad, kBlurRad,
554 &rrectToDraw, &size,
555 rectXs, rectYs, texXs, texYs,
556 &numX, &numY, &skipMask);
robertphillipsc4d2f902016-08-16 09:30:03 -0700557
558 static const SkScalar kAns = 12.0f * kBlurRad + 2.0f * kCornerRad + 1.0f;
robertphillipsf5a83e82016-08-10 12:00:09 -0700559 REPORTER_ASSERT(reporter, ninePatchable);
robertphillipsc4d2f902016-08-16 09:30:03 -0700560 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fWidth), kAns));
561 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fHeight), kAns));
562 REPORTER_ASSERT(reporter, 4 == numX && 4 == numY);
563 REPORTER_ASSERT(reporter, !skipMask);
robertphillipsf5a83e82016-08-10 12:00:09 -0700564 }
565
566 // simple elliptical
567 {
robertphillipsc4d2f902016-08-16 09:30:03 -0700568 static const SkScalar kXCornerRad = 2.0f;
569 static const SkScalar kYCornerRad = 10.0f;
robertphillipsf5a83e82016-08-10 12:00:09 -0700570 SkRRect rr;
robertphillipsc4d2f902016-08-16 09:30:03 -0700571 rr.setRectXY(r, kXCornerRad, kYCornerRad);
robertphillipsf5a83e82016-08-10 12:00:09 -0700572
Mike Reed28d47732018-03-05 16:56:52 -0500573 ninePatchable = SkComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(),
574 kBlurRad, kBlurRad,
575 &rrectToDraw, &size,
576 rectXs, rectYs, texXs, texYs,
577 &numX, &numY, &skipMask);
robertphillipsc4d2f902016-08-16 09:30:03 -0700578
579 static const SkScalar kXAns = 12.0f * kBlurRad + 2.0f * kXCornerRad + 1.0f;
580 static const SkScalar kYAns = 12.0f * kBlurRad + 2.0f * kYCornerRad + 1.0f;
581
robertphillipsf5a83e82016-08-10 12:00:09 -0700582 REPORTER_ASSERT(reporter, ninePatchable);
robertphillipsc4d2f902016-08-16 09:30:03 -0700583 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fWidth), kXAns));
584 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fHeight), kYAns));
585 REPORTER_ASSERT(reporter, 4 == numX && 4 == numY);
586 REPORTER_ASSERT(reporter, !skipMask);
587 }
588
589 // test-out occlusion
590 {
591 static const SkScalar kCornerRad = 10.0f;
592 SkRRect rr;
593 rr.setRectXY(r, kCornerRad, kCornerRad);
594
595 // The rectXs & rectYs should be { 1, 29, 91, 119 }. Add two more points around each.
596 SkScalar testLocs[] = {
597 -18.0f, -9.0f,
598 1.0f,
Ben Wagner63fd7602017-10-09 15:45:33 -0400599 9.0f, 18.0f,
600 29.0f,
robertphillipsc4d2f902016-08-16 09:30:03 -0700601 39.0f, 49.0f,
602 91.0f,
603 109.0f, 118.0f,
604 119.0f,
605 139.0f, 149.0f
606 };
607
608 for (int minY = 0; minY < (int)SK_ARRAY_COUNT(testLocs); ++minY) {
609 for (int maxY = minY+1; maxY < (int)SK_ARRAY_COUNT(testLocs); ++maxY) {
610 for (int minX = 0; minX < (int)SK_ARRAY_COUNT(testLocs); ++minX) {
611 for (int maxX = minX+1; maxX < (int)SK_ARRAY_COUNT(testLocs); ++maxX) {
612 SkRect occluder = SkRect::MakeLTRB(testLocs[minX], testLocs[minY],
613 testLocs[maxX], testLocs[maxY]);
614 if (occluder.isEmpty()) {
615 continue;
616 }
617
Mike Reed28d47732018-03-05 16:56:52 -0500618 ninePatchable = SkComputeBlurredRRectParams(rr, rr, occluder,
robertphillipsc4d2f902016-08-16 09:30:03 -0700619 kBlurRad, kBlurRad,
620 &rrectToDraw, &size,
621 rectXs, rectYs, texXs, texYs,
Ben Wagner63fd7602017-10-09 15:45:33 -0400622 &numX, &numY, &skipMask);
robertphillipsc4d2f902016-08-16 09:30:03 -0700623
624 static const SkScalar kAns = 12.0f * kBlurRad + 2.0f * kCornerRad + 1.0f;
625 REPORTER_ASSERT(reporter, ninePatchable);
626 REPORTER_ASSERT(reporter,
627 SkScalarNearlyEqual(SkIntToScalar(size.fWidth), kAns));
628 REPORTER_ASSERT(reporter,
629 SkScalarNearlyEqual(SkIntToScalar(size.fHeight), kAns));
630
631 int checkBit = 0x1;
632 for (int y = 0; y < numY-1; ++y) {
633 for (int x = 0; x < numX-1; ++x) {
634 SkRect cell = SkRect::MakeLTRB(rectXs[x], rectYs[y],
635 rectXs[x+1], rectYs[y+1]);
636 REPORTER_ASSERT(reporter,
637 SkToBool(skipMask & checkBit) ==
638 (cell.isEmpty() || occluder.contains(cell)));
639
640 REPORTER_ASSERT(reporter, texXs[x] >= 0 &&
641 texXs[x] <= size.fWidth);
642 REPORTER_ASSERT(reporter, texYs[y] >= 0 &&
643 texXs[y] <= size.fHeight);
644
645 checkBit <<= 1;
646 }
647 }
648 }
649 }
650 }
robertphillipsf5a83e82016-08-10 12:00:09 -0700651 }
robertphillipsc4d2f902016-08-16 09:30:03 -0700652
653
robertphillipsf5a83e82016-08-10 12:00:09 -0700654 }
655
656}
657
Florin Malita1ba5bfe2017-11-28 17:54:23 -0500658// https://crbugs.com/787712
659DEF_TEST(EmbossPerlinCrash, reporter) {
660 SkPaint p;
661
662 static constexpr SkEmbossMaskFilter::Light light = {
663 { 1, 1, 1 }, 0, 127, 127
664 };
665 p.setMaskFilter(SkEmbossMaskFilter::Make(1, light));
666 p.setShader(SkPerlinNoiseShader::MakeFractalNoise(1.0f, 1.0f, 2, 0.0f));
667
668 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(100, 100);
669 surface->getCanvas()->drawPaint(p);
670}
671
reed@google.comdaaafa62014-04-29 15:20:16 +0000672///////////////////////////////////////////////////////////////////////////////////////////
Mike Reedf221b492018-02-15 11:38:20 -0500673
674DEF_TEST(BlurZeroSigma, reporter) {
675 auto surf = SkSurface::MakeRasterN32Premul(20, 20);
676 SkPaint paint;
677 paint.setAntiAlias(true);
678
679 const SkIRect ir = { 5, 5, 15, 15 };
680 const SkRect r = SkRect::Make(ir);
681
682 const SkScalar sigmas[] = { 0, SkBits2Float(1) };
683 // if sigma is zero (or nearly so), we need to draw correctly (unblurred) and not crash
684 // or assert.
685 for (auto sigma : sigmas) {
Mike Reed18e75562018-03-12 14:03:47 -0400686 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma));
Mike Reedf221b492018-02-15 11:38:20 -0500687 surf->getCanvas()->drawRect(r, paint);
688
689 sk_tool_utils::PixelIter iter(surf.get());
690 SkIPoint loc;
691 while (const SkPMColor* p = (const SkPMColor*)iter.next(&loc)) {
692 if (ir.contains(loc.fX, loc.fY)) {
693 // inside the rect we draw (opaque black)
694 REPORTER_ASSERT(reporter, *p == SkPackARGB32(0xFF, 0, 0, 0));
695 } else {
696 // outside the rect we didn't draw at all, no blurred edges
697 REPORTER_ASSERT(reporter, *p == 0);
698 }
699 }
700 }
701}
702
Robert Phillipsc90b4db2018-04-23 15:12:00 +0000703
704///////////////////////////////////////////////////////////////////////////////////////////
Robert Phillipsc90b4db2018-04-23 15:12:00 +0000705
706DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BlurMaskBiggerThanDest, reporter, ctxInfo) {
707 GrContext* context = ctxInfo.grContext();
708
709 SkImageInfo ii = SkImageInfo::Make(32, 32, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
710
711 sk_sp<SkSurface> dst(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii));
712 if (!dst) {
713 ERRORF(reporter, "Could not create surface for test.");
714 return;
715 }
716
717 SkPaint p;
718 p.setColor(SK_ColorRED);
719 p.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 3));
720
721 SkCanvas* canvas = dst->getCanvas();
722
723 canvas->clear(SK_ColorBLACK);
724 canvas->drawCircle(SkPoint::Make(16, 16), 8, p);
725
726 SkBitmap readback;
727 SkAssertResult(readback.tryAllocPixels(ii));
728
729 canvas->readPixels(readback, 0, 0);
730 REPORTER_ASSERT(reporter, SkColorGetR(readback.getColor(15, 15)) > 128);
731 REPORTER_ASSERT(reporter, SkColorGetG(readback.getColor(15, 15)) == 0);
732 REPORTER_ASSERT(reporter, SkColorGetB(readback.getColor(15, 15)) == 0);
733 REPORTER_ASSERT(reporter, readback.getColor(31, 31) == SK_ColorBLACK);
734}
Mike Reed840debe2018-09-15 12:15:27 -0400735
736DEF_TEST(zero_blur, reporter) {
737 SkBitmap alpha, bitmap;
738
739 SkPaint paint;
740 paint.setMaskFilter(SkMaskFilter::MakeBlur(kOuter_SkBlurStyle, 3));
741 SkIPoint offset;
742 bitmap.extractAlpha(&alpha, &paint, nullptr, &offset);
743}
744