• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
7 
8 #include "SkBlurMask.h"
9 #include "SkBlurMaskFilter.h"
10 #include "SkBlurDrawLooper.h"
11 #include "SkCanvas.h"
12 #include "SkColorFilter.h"
13 #include "SkColorPriv.h"
14 #include "SkEmbossMaskFilter.h"
15 #include "SkLayerDrawLooper.h"
16 #include "SkMaskFilterBase.h"
17 #include "SkMath.h"
18 #include "SkPaint.h"
19 #include "SkPath.h"
20 #include "SkPerlinNoiseShader.h"
21 #include "SkSurface.h"
22 #include "Test.h"
23 
24 #if SK_SUPPORT_GPU
25 #include "GrContextFactory.h"
26 #include "SkGpuDevice.h"
27 #endif
28 
29 #define WRITE_CSV 0
30 
31 ///////////////////////////////////////////////////////////////////////////////
32 
33 #define ILLEGAL_MODE    ((SkXfermode::Mode)-1)
34 
35 static const int outset = 100;
36 static const SkColor bgColor = SK_ColorWHITE;
37 static const int strokeWidth = 4;
38 
create(SkBitmap * bm,const SkIRect & bound)39 static void create(SkBitmap* bm, const SkIRect& bound) {
40     bm->allocN32Pixels(bound.width(), bound.height());
41 }
42 
drawBG(SkCanvas * canvas)43 static void drawBG(SkCanvas* canvas) {
44     canvas->drawColor(bgColor);
45 }
46 
47 
48 struct BlurTest {
49     void (*addPath)(SkPath*);
50     int viewLen;
51     SkIRect views[9];
52 };
53 
54 //Path Draw Procs
55 //Beware that paths themselves my draw differently depending on the clip.
draw50x50Rect(SkPath * path)56 static void draw50x50Rect(SkPath* path) {
57     path->addRect(0, 0, SkIntToScalar(50), SkIntToScalar(50));
58 }
59 
60 //Tests
61 static BlurTest tests[] = {
62     { draw50x50Rect, 3, {
63         //inner half of blur
64         { 0, 0, 50, 50 },
65         //blur, but no path.
66         { 50 + strokeWidth/2, 50 + strokeWidth/2, 100, 100 },
67         //just an edge
68         { 40, strokeWidth, 60, 50 - strokeWidth },
69     }},
70 };
71 
72 /** Assumes that the ref draw was completely inside ref canvas --
73     implies that everything outside is "bgColor".
74     Checks that all overlap is the same and that all non-overlap on the
75     ref is "bgColor".
76  */
compare(const SkBitmap & ref,const SkIRect & iref,const SkBitmap & test,const SkIRect & itest)77 static bool compare(const SkBitmap& ref, const SkIRect& iref,
78                     const SkBitmap& test, const SkIRect& itest)
79 {
80     const int xOff = itest.fLeft - iref.fLeft;
81     const int yOff = itest.fTop - iref.fTop;
82 
83     for (int y = 0; y < test.height(); ++y) {
84         for (int x = 0; x < test.width(); ++x) {
85             SkColor testColor = test.getColor(x, y);
86             int refX = x + xOff;
87             int refY = y + yOff;
88             SkColor refColor;
89             if (refX >= 0 && refX < ref.width() &&
90                 refY >= 0 && refY < ref.height())
91             {
92                 refColor = ref.getColor(refX, refY);
93             } else {
94                 refColor = bgColor;
95             }
96             if (refColor != testColor) {
97                 return false;
98             }
99         }
100     }
101     return true;
102 }
103 
DEF_TEST(BlurDrawing,reporter)104 DEF_TEST(BlurDrawing, reporter) {
105     SkPaint paint;
106     paint.setColor(SK_ColorGRAY);
107     paint.setStyle(SkPaint::kStroke_Style);
108     paint.setStrokeWidth(SkIntToScalar(strokeWidth));
109 
110     SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5));
111     for (int style = 0; style <= kLastEnum_SkBlurStyle; ++style) {
112         SkBlurStyle blurStyle = static_cast<SkBlurStyle>(style);
113 
114         const uint32_t flagPermutations = SkBlurMaskFilter::kAll_BlurFlag;
115         for (uint32_t flags = 0; flags < flagPermutations; ++flags) {
116             paint.setMaskFilter(SkBlurMaskFilter::Make(blurStyle, sigma, flags));
117 
118             for (size_t test = 0; test < SK_ARRAY_COUNT(tests); ++test) {
119                 SkPath path;
120                 tests[test].addPath(&path);
121                 SkPath strokedPath;
122                 paint.getFillPath(path, &strokedPath);
123                 SkRect refBound = strokedPath.getBounds();
124                 SkIRect iref;
125                 refBound.roundOut(&iref);
126                 iref.inset(-outset, -outset);
127                 SkBitmap refBitmap;
128                 create(&refBitmap, iref);
129 
130                 SkCanvas refCanvas(refBitmap);
131                 refCanvas.translate(SkIntToScalar(-iref.fLeft),
132                                     SkIntToScalar(-iref.fTop));
133                 drawBG(&refCanvas);
134                 refCanvas.drawPath(path, paint);
135 
136                 for (int view = 0; view < tests[test].viewLen; ++view) {
137                     SkIRect itest = tests[test].views[view];
138                     SkBitmap testBitmap;
139                     create(&testBitmap, itest);
140 
141                     SkCanvas testCanvas(testBitmap);
142                     testCanvas.translate(SkIntToScalar(-itest.fLeft),
143                                          SkIntToScalar(-itest.fTop));
144                     drawBG(&testCanvas);
145                     testCanvas.drawPath(path, paint);
146 
147                     REPORTER_ASSERT(reporter,
148                         compare(refBitmap, iref, testBitmap, itest));
149                 }
150             }
151         }
152     }
153 }
154 
155 ///////////////////////////////////////////////////////////////////////////////
156 
157 // Use SkBlurMask::BlurGroundTruth to blur a 'width' x 'height' solid
158 // white rect. Return the right half of the middle row in 'result'.
ground_truth_2d(int width,int height,SkScalar sigma,int * result,int resultCount)159 static void ground_truth_2d(int width, int height,
160                             SkScalar sigma,
161                             int* result, int resultCount) {
162     SkMask src, dst;
163 
164     src.fBounds.set(0, 0, width, height);
165     src.fFormat = SkMask::kA8_Format;
166     src.fRowBytes = src.fBounds.width();
167     src.fImage = SkMask::AllocImage(src.computeTotalImageSize());
168 
169     memset(src.fImage, 0xff, src.computeTotalImageSize());
170 
171     if (!SkBlurMask::BlurGroundTruth(sigma, &dst, src, kNormal_SkBlurStyle)) {
172         return;
173     }
174 
175     int midX = dst.fBounds.centerX();
176     int midY = dst.fBounds.centerY();
177     uint8_t* bytes = dst.getAddr8(midX, midY);
178     int i;
179     for (i = 0; i < dst.fBounds.width()-(midX-dst.fBounds.fLeft); ++i) {
180         if (i < resultCount) {
181             result[i] = bytes[i];
182         }
183     }
184     for ( ; i < resultCount; ++i) {
185         result[i] = 0;
186     }
187 
188     SkMask::FreeImage(src.fImage);
189     SkMask::FreeImage(dst.fImage);
190 }
191 
192 // Implement a step function that is 255 between min and max; 0 elsewhere.
step(int x,SkScalar min,SkScalar max)193 static int step(int x, SkScalar min, SkScalar max) {
194     if (min < x && x < max) {
195         return 255;
196     }
197     return 0;
198 }
199 
200 // Implement a Gaussian function with 0 mean and std.dev. of 'sigma'.
gaussian(int x,SkScalar sigma)201 static float gaussian(int x, SkScalar sigma) {
202     float k = SK_Scalar1/(sigma * sqrtf(2.0f*SK_ScalarPI));
203     float exponent = -(x * x) / (2 * sigma * sigma);
204     return k * expf(exponent);
205 }
206 
207 // Perform a brute force convolution of a step function with a Gaussian.
208 // Return the right half in 'result'
brute_force_1d(SkScalar stepMin,SkScalar stepMax,SkScalar gaussianSigma,int * result,int resultCount)209 static void brute_force_1d(SkScalar stepMin, SkScalar stepMax,
210                            SkScalar gaussianSigma,
211                            int* result, int resultCount) {
212 
213     int gaussianRange = SkScalarCeilToInt(10 * gaussianSigma);
214 
215     for (int i = 0; i < resultCount; ++i) {
216         SkScalar sum = 0.0f;
217         for (int j = -gaussianRange; j < gaussianRange; ++j) {
218             sum += gaussian(j, gaussianSigma) * step(i-j, stepMin, stepMax);
219         }
220 
221         result[i] = SkClampMax(SkClampPos(int(sum + 0.5f)), 255);
222     }
223 }
224 
blur_path(SkCanvas * canvas,const SkPath & path,SkScalar gaussianSigma)225 static void blur_path(SkCanvas* canvas, const SkPath& path,
226                       SkScalar gaussianSigma) {
227 
228     SkScalar midX = path.getBounds().centerX();
229     SkScalar midY = path.getBounds().centerY();
230 
231     canvas->translate(-midX, -midY);
232 
233     SkPaint blurPaint;
234     blurPaint.setColor(SK_ColorWHITE);
235     blurPaint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, gaussianSigma,
236                                                    SkBlurMaskFilter::kHighQuality_BlurFlag));
237 
238     canvas->drawColor(SK_ColorBLACK);
239     canvas->drawPath(path, blurPaint);
240 }
241 
242 // Readback the blurred draw results from the canvas
readback(const SkBitmap & src,int * result,int resultCount)243 static void readback(const SkBitmap& src, int* result, int resultCount) {
244     SkBitmap readback;
245     readback.allocN32Pixels(resultCount, 30);
246     SkPixmap pm;
247     readback.peekPixels(&pm);
248     src.readPixels(pm, 0, 0);
249 
250     const SkPMColor* pixels = pm.addr32(0, 15);
251 
252     for (int i = 0; i < resultCount; ++i) {
253         result[i] = SkColorGetR(pixels[i]);
254     }
255 }
256 
257 // Draw a blurred version of the provided path.
258 // Return the right half of the middle row in 'result'.
cpu_blur_path(const SkPath & path,SkScalar gaussianSigma,int * result,int resultCount)259 static void cpu_blur_path(const SkPath& path, SkScalar gaussianSigma,
260                           int* result, int resultCount) {
261 
262     SkBitmap bitmap;
263     bitmap.allocN32Pixels(resultCount, 30);
264     SkCanvas canvas(bitmap);
265 
266     blur_path(&canvas, path, gaussianSigma);
267     readback(bitmap, result, resultCount);
268 }
269 
270 #if WRITE_CSV
write_as_csv(const char * label,SkScalar scale,int * data,int count)271 static void write_as_csv(const char* label, SkScalar scale, int* data, int count) {
272     SkDebugf("%s_%.2f,", label, scale);
273     for (int i = 0; i < count-1; ++i) {
274         SkDebugf("%d,", data[i]);
275     }
276     SkDebugf("%d\n", data[count-1]);
277 }
278 #endif
279 
match(int * first,int * second,int count,int tol)280 static bool match(int* first, int* second, int count, int tol) {
281     int delta;
282     for (int i = 0; i < count; ++i) {
283         delta = first[i] - second[i];
284         if (delta > tol || delta < -tol) {
285             return false;
286         }
287     }
288 
289     return true;
290 }
291 
292 // Test out the normal blur style with a wide range of sigmas
DEF_TEST(BlurSigmaRange,reporter)293 DEF_TEST(BlurSigmaRange, reporter) {
294     static const int kSize = 100;
295 
296     // The geometry is offset a smidge to trigger:
297     // https://code.google.com/p/chromium/issues/detail?id=282418
298     SkPath rectPath;
299     rectPath.addRect(0.3f, 0.3f, 100.3f, 100.3f);
300 
301     SkPoint polyPts[] = {
302         { 0.3f, 0.3f },
303         { 100.3f, 0.3f },
304         { 100.3f, 100.3f },
305         { 0.3f, 100.3f },
306         { 2.3f, 50.3f }     // a little divet to throw off the rect special case
307     };
308     SkPath polyPath;
309     polyPath.addPoly(polyPts, SK_ARRAY_COUNT(polyPts), true);
310 
311     int rectSpecialCaseResult[kSize];
312     int generalCaseResult[kSize];
313     int groundTruthResult[kSize];
314     int bruteForce1DResult[kSize];
315 
316     SkScalar sigma = 10.0f;
317 
318     for (int i = 0; i < 4; ++i, sigma /= 10) {
319 
320         cpu_blur_path(rectPath, sigma, rectSpecialCaseResult, kSize);
321         cpu_blur_path(polyPath, sigma, generalCaseResult, kSize);
322 
323         ground_truth_2d(100, 100, sigma, groundTruthResult, kSize);
324         brute_force_1d(-50.0f, 50.0f, sigma, bruteForce1DResult, kSize);
325 
326         REPORTER_ASSERT(reporter, match(rectSpecialCaseResult, bruteForce1DResult, kSize, 5));
327         REPORTER_ASSERT(reporter, match(generalCaseResult, bruteForce1DResult, kSize, 15));
328         REPORTER_ASSERT(reporter, match(groundTruthResult, bruteForce1DResult, kSize, 1));
329 
330 #if WRITE_CSV
331         write_as_csv("RectSpecialCase", sigma, rectSpecialCaseResult, kSize);
332         write_as_csv("GeneralCase", sigma, generalCaseResult, kSize);
333 #if SK_SUPPORT_GPU
334         write_as_csv("GPU", sigma, gpuResult, kSize);
335 #endif
336         write_as_csv("GroundTruth2D", sigma, groundTruthResult, kSize);
337         write_as_csv("BruteForce1D", sigma, bruteForce1DResult, kSize);
338 #endif
339     }
340 }
341 
342 ///////////////////////////////////////////////////////////////////////////////////////////
343 
blurMaskFilterFlags_as_quality(uint32_t blurMaskFilterFlags)344 static SkBlurQuality blurMaskFilterFlags_as_quality(uint32_t blurMaskFilterFlags) {
345     return (blurMaskFilterFlags & SkBlurMaskFilter::kHighQuality_BlurFlag) ?
346             kHigh_SkBlurQuality : kLow_SkBlurQuality;
347 }
348 
test_blurDrawLooper(skiatest::Reporter * reporter,SkScalar sigma,SkBlurStyle style,uint32_t blurMaskFilterFlags)349 static void test_blurDrawLooper(skiatest::Reporter* reporter, SkScalar sigma,
350                                 SkBlurStyle style, uint32_t blurMaskFilterFlags) {
351     if (kNormal_SkBlurStyle != style) {
352         return; // blurdrawlooper only supports normal
353     }
354 
355     const SkColor color = 0xFF335577;
356     const SkScalar dx = 10;
357     const SkScalar dy = -5;
358     sk_sp<SkDrawLooper> lp(SkBlurDrawLooper::Make(color, sigma, dx, dy));
359     const bool expectSuccess = sigma > 0;
360 
361     if (nullptr == lp) {
362         REPORTER_ASSERT(reporter, sigma <= 0);
363     } else {
364         SkDrawLooper::BlurShadowRec rec;
365         bool success = lp->asABlurShadow(&rec);
366         REPORTER_ASSERT(reporter, success == expectSuccess);
367         if (success) {
368             REPORTER_ASSERT(reporter, rec.fSigma == sigma);
369             REPORTER_ASSERT(reporter, rec.fOffset.x() == dx);
370             REPORTER_ASSERT(reporter, rec.fOffset.y() == dy);
371             REPORTER_ASSERT(reporter, rec.fColor == color);
372             REPORTER_ASSERT(reporter, rec.fStyle == style);
373             REPORTER_ASSERT(reporter, rec.fQuality == kLow_SkBlurQuality);
374         }
375     }
376 }
377 
test_looper(skiatest::Reporter * reporter,sk_sp<SkDrawLooper> lp,SkScalar sigma,SkBlurStyle style,SkBlurQuality quality,bool expectSuccess)378 static void test_looper(skiatest::Reporter* reporter, sk_sp<SkDrawLooper> lp, SkScalar sigma,
379                         SkBlurStyle style, SkBlurQuality quality, bool expectSuccess) {
380     SkDrawLooper::BlurShadowRec rec;
381     bool success = lp->asABlurShadow(&rec);
382     REPORTER_ASSERT(reporter, success == expectSuccess);
383     if (success != expectSuccess) {
384         lp->asABlurShadow(&rec);
385     }
386     if (success) {
387         REPORTER_ASSERT(reporter, rec.fSigma == sigma);
388         REPORTER_ASSERT(reporter, rec.fStyle == style);
389         REPORTER_ASSERT(reporter, rec.fQuality == quality);
390     }
391 }
392 
make_noop_layer(SkLayerDrawLooper::Builder * builder)393 static void make_noop_layer(SkLayerDrawLooper::Builder* builder) {
394     SkLayerDrawLooper::LayerInfo info;
395 
396     info.fPaintBits = 0;
397     info.fColorMode = SkBlendMode::kDst;
398     builder->addLayer(info);
399 }
400 
make_blur_layer(SkLayerDrawLooper::Builder * builder,sk_sp<SkMaskFilter> mf)401 static void make_blur_layer(SkLayerDrawLooper::Builder* builder, sk_sp<SkMaskFilter> mf) {
402     SkLayerDrawLooper::LayerInfo info;
403 
404     info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit;
405     info.fColorMode = SkBlendMode::kSrc;
406     SkPaint* paint = builder->addLayer(info);
407     paint->setMaskFilter(std::move(mf));
408 }
409 
test_layerDrawLooper(skiatest::Reporter * reporter,sk_sp<SkMaskFilter> mf,SkScalar sigma,SkBlurStyle style,SkBlurQuality quality,bool expectSuccess)410 static void test_layerDrawLooper(skiatest::Reporter* reporter, sk_sp<SkMaskFilter> mf,
411                                  SkScalar sigma, SkBlurStyle style, SkBlurQuality quality,
412                                  bool expectSuccess) {
413 
414     SkLayerDrawLooper::LayerInfo info;
415     SkLayerDrawLooper::Builder builder;
416 
417     // 1 layer is too few
418     make_noop_layer(&builder);
419     test_looper(reporter, builder.detach(), sigma, style, quality, false);
420 
421     // 2 layers is good, but need blur
422     make_noop_layer(&builder);
423     make_noop_layer(&builder);
424     test_looper(reporter, builder.detach(), sigma, style, quality, false);
425 
426     // 2 layers is just right
427     make_noop_layer(&builder);
428     make_blur_layer(&builder, mf);
429     test_looper(reporter, builder.detach(), sigma, style, quality, expectSuccess);
430 
431     // 3 layers is too many
432     make_noop_layer(&builder);
433     make_blur_layer(&builder, mf);
434     make_noop_layer(&builder);
435     test_looper(reporter, builder.detach(), sigma, style, quality, false);
436 }
437 
DEF_TEST(BlurAsABlur,reporter)438 DEF_TEST(BlurAsABlur, reporter) {
439     const SkBlurStyle styles[] = {
440         kNormal_SkBlurStyle, kSolid_SkBlurStyle, kOuter_SkBlurStyle, kInner_SkBlurStyle
441     };
442     const SkScalar sigmas[] = {
443         // values <= 0 should not success for a blur
444         -1, 0, 0.5f, 2
445     };
446 
447     // Test asABlur for SkBlurMaskFilter
448     //
449     for (size_t i = 0; i < SK_ARRAY_COUNT(styles); ++i) {
450         const SkBlurStyle style = styles[i];
451         for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) {
452             const SkScalar sigma = sigmas[j];
453             for (int flags = 0; flags <= SkBlurMaskFilter::kAll_BlurFlag; ++flags) {
454                 const SkBlurQuality quality = blurMaskFilterFlags_as_quality(flags);
455 
456                 sk_sp<SkMaskFilter> mf(SkBlurMaskFilter::Make(style, sigma, flags));
457                 if (nullptr == mf.get()) {
458                     REPORTER_ASSERT(reporter, sigma <= 0);
459                 } else {
460                     REPORTER_ASSERT(reporter, sigma > 0);
461                     SkMaskFilterBase::BlurRec rec;
462                     bool success = as_MFB(mf)->asABlur(&rec);
463                     if (flags & SkBlurMaskFilter::kIgnoreTransform_BlurFlag) {
464                         REPORTER_ASSERT(reporter, !success);
465                     } else {
466                         REPORTER_ASSERT(reporter, success);
467                         REPORTER_ASSERT(reporter, rec.fSigma == sigma);
468                         REPORTER_ASSERT(reporter, rec.fStyle == style);
469                         REPORTER_ASSERT(reporter, rec.fQuality == quality);
470                     }
471                     test_layerDrawLooper(reporter, std::move(mf), sigma, style, quality, success);
472                 }
473                 test_blurDrawLooper(reporter, sigma, style, flags);
474             }
475         }
476     }
477 
478     // Test asABlur for SkEmbossMaskFilter -- should never succeed
479     //
480     {
481         SkEmbossMaskFilter::Light light = {
482             { 1, 1, 1 }, 0, 127, 127
483         };
484         for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) {
485             const SkScalar sigma = sigmas[j];
486             auto mf(SkEmbossMaskFilter::Make(sigma, light));
487             if (mf) {
488                 SkMaskFilterBase::BlurRec rec;
489                 bool success = as_MFB(mf)->asABlur(&rec);
490                 REPORTER_ASSERT(reporter, !success);
491             }
492         }
493     }
494 }
495 
496 #if SK_SUPPORT_GPU
497 
498 // This exercises the problem discovered in crbug.com/570232. The return value from
499 // SkBlurMask::BoxBlur wasn't being checked in SkBlurMaskFilter.cpp::GrRRectBlurEffect::Create
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SmallBoxBlurBug,reporter,ctxInfo)500 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SmallBoxBlurBug, reporter, ctxInfo) {
501 
502     SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128);
503     auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
504     SkCanvas* canvas = surface->getCanvas();
505 
506     SkRect r = SkRect::MakeXYWH(10, 10, 100, 100);
507     SkRRect rr = SkRRect::MakeRectXY(r, 10, 10);
508 
509     SkPaint p;
510     p.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, 0.01f));
511 
512     canvas->drawRRect(rr, p);
513 }
514 
515 #endif
516 
517 
DEF_TEST(BlurredRRectNinePatchComputation,reporter)518 DEF_TEST(BlurredRRectNinePatchComputation, reporter) {
519     const SkRect r = SkRect::MakeXYWH(10, 10, 100, 100);
520     static const SkScalar kBlurRad = 3.0f;
521 
522     bool ninePatchable;
523     SkRRect rrectToDraw;
524     SkISize size;
525     SkScalar rectXs[SkBlurMaskFilter::kMaxDivisions], rectYs[SkBlurMaskFilter::kMaxDivisions];
526     SkScalar texXs[SkBlurMaskFilter::kMaxDivisions], texYs[SkBlurMaskFilter::kMaxDivisions];
527     int numX, numY;
528     uint32_t skipMask;
529 
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 
537         ninePatchable = SkBlurMaskFilter::ComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(),
538                                                                     kBlurRad, kBlurRad,
539                                                                     &rrectToDraw, &size,
540                                                                     rectXs, rectYs, texXs, texYs,
541                                                                     &numX, &numY, &skipMask);
542         REPORTER_ASSERT(reporter, !ninePatchable);
543     }
544 
545     // simple circular
546     {
547         static const SkScalar kCornerRad = 10.0f;
548         SkRRect rr;
549         rr.setRectXY(r, kCornerRad, kCornerRad);
550 
551         ninePatchable = SkBlurMaskFilter::ComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(),
552                                                                     kBlurRad, kBlurRad,
553                                                                     &rrectToDraw, &size,
554                                                                     rectXs, rectYs, texXs, texYs,
555                                                                     &numX, &numY, &skipMask);
556 
557         static const SkScalar kAns = 12.0f * kBlurRad + 2.0f * kCornerRad + 1.0f;
558         REPORTER_ASSERT(reporter, ninePatchable);
559         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);
563     }
564 
565     // simple elliptical
566     {
567         static const SkScalar kXCornerRad = 2.0f;
568         static const SkScalar kYCornerRad = 10.0f;
569         SkRRect rr;
570         rr.setRectXY(r, kXCornerRad, kYCornerRad);
571 
572         ninePatchable = SkBlurMaskFilter::ComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(),
573                                                                     kBlurRad, kBlurRad,
574                                                                     &rrectToDraw, &size,
575                                                                     rectXs, rectYs, texXs, texYs,
576                                                                     &numX, &numY, &skipMask);
577 
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 
581         REPORTER_ASSERT(reporter, ninePatchable);
582         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,
598                9.0f, 18.0f,
599               29.0f,
600               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 
617                         ninePatchable = SkBlurMaskFilter::ComputeBlurredRRectParams(
618                                                                     rr, rr, occluder,
619                                                                     kBlurRad, kBlurRad,
620                                                                     &rrectToDraw, &size,
621                                                                     rectXs, rectYs, texXs, texYs,
622                                                                     &numX, &numY, &skipMask);
623 
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             }
651         }
652 
653 
654     }
655 
656 }
657 
658 // https://crbugs.com/787712
DEF_TEST(EmbossPerlinCrash,reporter)659 DEF_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 
672 ///////////////////////////////////////////////////////////////////////////////////////////
673 #include "SkSurface.h"
674 #include "sk_pixel_iter.h"
675 
DEF_TEST(BlurZeroSigma,reporter)676 DEF_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) {
688         paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, sigma));
689         surf->getCanvas()->drawRect(r, paint);
690 
691         sk_tool_utils::PixelIter iter(surf.get());
692         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 
705