• 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 "SkBitmap.h"
9 #include "SkBlendMode.h"
10 #include "SkBlurDrawLooper.h"
11 #include "SkBlurMask.h"
12 #include "SkBlurPriv.h"
13 #include "SkBlurTypes.h"
14 #include "SkCanvas.h"
15 #include "SkColor.h"
16 #include "SkColorPriv.h"
17 #include "SkDrawLooper.h"
18 #include "SkEmbossMaskFilter.h"
19 #include "SkFloatBits.h"
20 #include "SkImageInfo.h"
21 #include "SkLayerDrawLooper.h"
22 #include "SkMask.h"
23 #include "SkMaskFilter.h"
24 #include "SkMaskFilterBase.h"
25 #include "SkMath.h"
26 #include "SkMathPriv.h"
27 #include "SkPaint.h"
28 #include "SkPath.h"
29 #include "SkPerlinNoiseShader.h"
30 #include "SkPixmap.h"
31 #include "SkPoint.h"
32 #include "SkRRect.h"
33 #include "SkRectPriv.h"
34 #include "SkRefCnt.h"
35 #include "SkScalar.h"
36 #include "SkShader.h"
37 #include "SkSize.h"
38 #include "SkSurface.h"
39 #include "SkTypes.h"
40 #include "Test.h"
41 #include "sk_pixel_iter.h"
42 
43 #include "GrContextFactory.h"
44 
45 #include <math.h>
46 #include <string.h>
47 #include <utility>
48 
49 #define WRITE_CSV 0
50 
51 ///////////////////////////////////////////////////////////////////////////////
52 
53 #define ILLEGAL_MODE    ((SkXfermode::Mode)-1)
54 
55 static const int outset = 100;
56 static const SkColor bgColor = SK_ColorWHITE;
57 static const int strokeWidth = 4;
58 
create(SkBitmap * bm,const SkIRect & bound)59 static void create(SkBitmap* bm, const SkIRect& bound) {
60     bm->allocN32Pixels(bound.width(), bound.height());
61 }
62 
drawBG(SkCanvas * canvas)63 static void drawBG(SkCanvas* canvas) {
64     canvas->drawColor(bgColor);
65 }
66 
67 
68 struct 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.
draw50x50Rect(SkPath * path)76 static void draw50x50Rect(SkPath* path) {
77     path->addRect(0, 0, SkIntToScalar(50), SkIntToScalar(50));
78 }
79 
80 //Tests
81 static 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  */
compare(const SkBitmap & ref,const SkIRect & iref,const SkBitmap & test,const SkIRect & itest)97 static 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 
103     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 
DEF_TEST(BlurDrawing,reporter)124 DEF_TEST(BlurDrawing, reporter) {
125     SkPaint paint;
126     paint.setColor(SK_ColorGRAY);
127     paint.setStyle(SkPaint::kStroke_Style);
128     paint.setStrokeWidth(SkIntToScalar(strokeWidth));
129 
130     SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5));
131     for (int style = 0; style <= kLastEnum_SkBlurStyle; ++style) {
132         SkBlurStyle blurStyle = static_cast<SkBlurStyle>(style);
133 
134         for (bool respectCTM : { false, true }) {
135             paint.setMaskFilter(SkMaskFilter::MakeBlur(blurStyle, sigma, respectCTM));
136 
137             for (size_t test = 0; test < SK_ARRAY_COUNT(tests); ++test) {
138                 SkPath path;
139                 tests[test].addPath(&path);
140                 SkPath strokedPath;
141                 paint.getFillPath(path, &strokedPath);
142                 SkRect refBound = strokedPath.getBounds();
143                 SkIRect iref;
144                 refBound.roundOut(&iref);
145                 iref.inset(-outset, -outset);
146                 SkBitmap refBitmap;
147                 create(&refBitmap, iref);
148 
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;
158                     create(&testBitmap, itest);
159 
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         }
171     }
172 }
173 
174 ///////////////////////////////////////////////////////////////////////////////
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'.
ground_truth_2d(int width,int height,SkScalar sigma,int * result,int resultCount)178 static void ground_truth_2d(int width, int height,
179                             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 
190     if (!SkBlurMask::BlurGroundTruth(sigma, &dst, src, kNormal_SkBlurStyle)) {
191         return;
192     }
193 
194     int midX = dst.fBounds.x() + dst.fBounds.width()/2;
195     int midY = dst.fBounds.y() + dst.fBounds.height()/2;
196     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     }
206 
207     SkMask::FreeImage(src.fImage);
208     SkMask::FreeImage(dst.fImage);
209 }
210 
211 // Implement a step function that is 255 between min and max; 0 elsewhere.
step(int x,SkScalar min,SkScalar max)212 static 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'.
gaussian(int x,SkScalar sigma)220 static float gaussian(int x, SkScalar sigma) {
221     float k = SK_Scalar1/(sigma * sqrtf(2.0f*SK_ScalarPI));
222     float exponent = -(x * x) / (2 * sigma * sigma);
223     return k * expf(exponent);
224 }
225 
226 // Perform a brute force convolution of a step function with a Gaussian.
227 // Return the right half in 'result'
brute_force_1d(SkScalar stepMin,SkScalar stepMax,SkScalar gaussianSigma,int * result,int resultCount)228 static void brute_force_1d(SkScalar stepMin, SkScalar stepMax,
229                            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 
blur_path(SkCanvas * canvas,const SkPath & path,SkScalar gaussianSigma)244 static void blur_path(SkCanvas* canvas, const SkPath& path,
245                       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);
254     blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, gaussianSigma));
255 
256     canvas->drawColor(SK_ColorBLACK);
257     canvas->drawPath(path, blurPaint);
258 }
259 
260 // Readback the blurred draw results from the canvas
readback(const SkBitmap & src,int * result,int resultCount)261 static void readback(const SkBitmap& src, int* result, int resultCount) {
262     SkBitmap readback;
263     readback.allocN32Pixels(resultCount, 30);
264     SkPixmap pm;
265     readback.peekPixels(&pm);
266     src.readPixels(pm, 0, 0);
267 
268     const SkPMColor* pixels = pm.addr32(0, 15);
269 
270     for (int i = 0; i < resultCount; ++i) {
271         result[i] = SkColorGetR(pixels[i]);
272     }
273 }
274 
275 // Draw a blurred version of the provided path.
276 // Return the right half of the middle row in 'result'.
cpu_blur_path(const SkPath & path,SkScalar gaussianSigma,int * result,int resultCount)277 static void cpu_blur_path(const SkPath& path, SkScalar gaussianSigma,
278                           int* result, int resultCount) {
279 
280     SkBitmap bitmap;
281     bitmap.allocN32Pixels(resultCount, 30);
282     SkCanvas canvas(bitmap);
283 
284     blur_path(&canvas, path, gaussianSigma);
285     readback(bitmap, result, resultCount);
286 }
287 
288 #if WRITE_CSV
write_as_csv(const char * label,SkScalar scale,int * data,int count)289 static 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 
match(int * first,int * second,int count,int tol)298 static 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
DEF_TEST(BlurSigmaRange,reporter)311 DEF_TEST(BlurSigmaRange, reporter) {
312     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];
331     int groundTruthResult[kSize];
332     int bruteForce1DResult[kSize];
333 
334     SkScalar sigma = 10.0f;
335 
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);
340 
341         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));
346         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);
351         write_as_csv("GPU", sigma, gpuResult, kSize);
352         write_as_csv("GroundTruth2D", sigma, groundTruthResult, kSize);
353         write_as_csv("BruteForce1D", sigma, bruteForce1DResult, kSize);
354 #endif
355     }
356 }
357 
358 ///////////////////////////////////////////////////////////////////////////////////////////
359 
test_blurDrawLooper(skiatest::Reporter * reporter,SkScalar sigma,SkBlurStyle style)360 static void test_blurDrawLooper(skiatest::Reporter* reporter, SkScalar sigma, SkBlurStyle style) {
361     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;
368     sk_sp<SkDrawLooper> lp(SkBlurDrawLooper::Make(color, sigma, dx, dy));
369     const bool expectSuccess = sigma > 0;
370 
371     if (nullptr == lp) {
372         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);
383         }
384     }
385 }
386 
test_looper(skiatest::Reporter * reporter,sk_sp<SkDrawLooper> lp,SkScalar sigma,SkBlurStyle style,bool expectSuccess)387 static void test_looper(skiatest::Reporter* reporter, sk_sp<SkDrawLooper> lp, SkScalar sigma,
388                         SkBlurStyle style, bool expectSuccess) {
389     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);
398     }
399 }
400 
make_noop_layer(SkLayerDrawLooper::Builder * builder)401 static void make_noop_layer(SkLayerDrawLooper::Builder* builder) {
402     SkLayerDrawLooper::LayerInfo info;
403 
404     info.fPaintBits = 0;
405     info.fColorMode = SkBlendMode::kDst;
406     builder->addLayer(info);
407 }
408 
make_blur_layer(SkLayerDrawLooper::Builder * builder,sk_sp<SkMaskFilter> mf)409 static void make_blur_layer(SkLayerDrawLooper::Builder* builder, sk_sp<SkMaskFilter> mf) {
410     SkLayerDrawLooper::LayerInfo info;
411 
412     info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit;
413     info.fColorMode = SkBlendMode::kSrc;
414     SkPaint* paint = builder->addLayer(info);
415     paint->setMaskFilter(std::move(mf));
416 }
417 
test_layerDrawLooper(skiatest::Reporter * reporter,sk_sp<SkMaskFilter> mf,SkScalar sigma,SkBlurStyle style,bool expectSuccess)418 static void test_layerDrawLooper(skiatest::Reporter* reporter, sk_sp<SkMaskFilter> mf,
419                                  SkScalar sigma, SkBlurStyle style, bool expectSuccess) {
420 
421     SkLayerDrawLooper::LayerInfo info;
422     SkLayerDrawLooper::Builder builder;
423 
424     // 1 layer is too few
425     make_noop_layer(&builder);
426     test_looper(reporter, builder.detach(), sigma, style, false);
427 
428     // 2 layers is good, but need blur
429     make_noop_layer(&builder);
430     make_noop_layer(&builder);
431     test_looper(reporter, builder.detach(), sigma, style, false);
432 
433     // 2 layers is just right
434     make_noop_layer(&builder);
435     make_blur_layer(&builder, mf);
436     test_looper(reporter, builder.detach(), sigma, style, expectSuccess);
437 
438     // 3 layers is too many
439     make_noop_layer(&builder);
440     make_blur_layer(&builder, mf);
441     make_noop_layer(&builder);
442     test_looper(reporter, builder.detach(), sigma, style, false);
443 }
444 
DEF_TEST(BlurAsABlur,reporter)445 DEF_TEST(BlurAsABlur, reporter) {
446     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) {
457         const SkBlurStyle style = styles[i];
458         for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) {
459             const SkScalar sigma = sigmas[j];
460             for (bool respectCTM : { false, true }) {
461                 sk_sp<SkMaskFilter> mf(SkMaskFilter::MakeBlur(style, sigma, respectCTM));
462                 if (nullptr == mf.get()) {
463                     REPORTER_ASSERT(reporter, sigma <= 0);
464                 } else {
465                     REPORTER_ASSERT(reporter, sigma > 0);
466                     SkMaskFilterBase::BlurRec rec;
467                     bool success = as_MFB(mf)->asABlur(&rec);
468                     if (respectCTM) {
469                         REPORTER_ASSERT(reporter, success);
470                         REPORTER_ASSERT(reporter, rec.fSigma == sigma);
471                         REPORTER_ASSERT(reporter, rec.fStyle == style);
472                     } else {
473                         REPORTER_ASSERT(reporter, !success);
474                     }
475                     test_layerDrawLooper(reporter, std::move(mf), sigma, style, success);
476                 }
477                 test_blurDrawLooper(reporter, sigma, style);
478             }
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];
490             auto mf(SkEmbossMaskFilter::Make(sigma, light));
491             if (mf) {
492                 SkMaskFilterBase::BlurRec rec;
493                 bool success = as_MFB(mf)->asABlur(&rec);
494                 REPORTER_ASSERT(reporter, !success);
495             }
496         }
497     }
498 }
499 
500 // This exercises the problem discovered in crbug.com/570232. The return value from
501 // SkBlurMask::BoxBlur wasn't being checked in SkBlurMaskFilter.cpp::GrRRectBlurEffect::Create
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SmallBoxBlurBug,reporter,ctxInfo)502 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SmallBoxBlurBug, reporter, ctxInfo) {
503 
504     SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128);
505     auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
506     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;
512     p.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 0.01f));
513 
514     canvas->drawRRect(rr, p);
515 }
516 
DEF_TEST(BlurredRRectNinePatchComputation,reporter)517 DEF_TEST(BlurredRRectNinePatchComputation, reporter) {
518     const SkRect r = SkRect::MakeXYWH(10, 10, 100, 100);
519     static const SkScalar kBlurRad = 3.0f;
520 
521     bool ninePatchable;
522     SkRRect rrectToDraw;
523     SkISize size;
524     SkScalar rectXs[kSkBlurRRectMaxDivisions],
525              rectYs[kSkBlurRRectMaxDivisions];
526     SkScalar texXs[kSkBlurRRectMaxDivisions],
527              texYs[kSkBlurRRectMaxDivisions];
528     int numX, numY;
529     uint32_t skipMask;
530 
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 
538         ninePatchable = SkComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(),
539                                                     kBlurRad, kBlurRad,
540                                                     &rrectToDraw, &size,
541                                                     rectXs, rectYs, texXs, texYs,
542                                                     &numX, &numY, &skipMask);
543         REPORTER_ASSERT(reporter, !ninePatchable);
544     }
545 
546     // simple circular
547     {
548         static const SkScalar kCornerRad = 10.0f;
549         SkRRect rr;
550         rr.setRectXY(r, kCornerRad, kCornerRad);
551 
552         ninePatchable = SkComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(),
553                                                     kBlurRad, kBlurRad,
554                                                     &rrectToDraw, &size,
555                                                     rectXs, rectYs, texXs, texYs,
556                                                     &numX, &numY, &skipMask);
557 
558         static const SkScalar kAns = 12.0f * kBlurRad + 2.0f * kCornerRad + 1.0f;
559         REPORTER_ASSERT(reporter, ninePatchable);
560         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);
564     }
565 
566     // simple elliptical
567     {
568         static const SkScalar kXCornerRad = 2.0f;
569         static const SkScalar kYCornerRad = 10.0f;
570         SkRRect rr;
571         rr.setRectXY(r, kXCornerRad, kYCornerRad);
572 
573         ninePatchable = SkComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(),
574                                                     kBlurRad, kBlurRad,
575                                                     &rrectToDraw, &size,
576                                                     rectXs, rectYs, texXs, texYs,
577                                                     &numX, &numY, &skipMask);
578 
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 
582         REPORTER_ASSERT(reporter, ninePatchable);
583         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,
599                9.0f, 18.0f,
600               29.0f,
601               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 
618                         ninePatchable = SkComputeBlurredRRectParams(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 
DEF_TEST(BlurZeroSigma,reporter)674 DEF_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) {
686         paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma));
687         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 
703 
704 ///////////////////////////////////////////////////////////////////////////////////////////
705 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BlurMaskBiggerThanDest,reporter,ctxInfo)706 DEF_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 }
735 
DEF_TEST(zero_blur,reporter)736 DEF_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 
745