blob: d9fca98fa65e19edaf932efc3ca19a2fe6119ae3 [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
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +00008#include "SkBlurMask.h"
reed@google.com2b75f422011-07-07 13:43:38 +00009#include "SkBlurMaskFilter.h"
reed@google.comdaaafa62014-04-29 15:20:16 +000010#include "SkBlurDrawLooper.h"
reedfb8c1fc2015-08-04 18:44:56 -070011#include "SkCanvas.h"
mtklein7a1f45f2016-08-04 06:19:33 -070012#include "SkColorFilter.h"
bungemand3ebb482015-08-05 13:57:49 -070013#include "SkEmbossMaskFilter.h"
14#include "SkLayerDrawLooper.h"
tomhudson@google.com889bd8b2011-09-27 17:38:17 +000015#include "SkMath.h"
bungeman@google.com5af16f82011-09-02 15:06:44 +000016#include "SkPaint.h"
bungemand3ebb482015-08-05 13:57:49 -070017#include "SkPath.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000018#include "Test.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000019
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +000020#if SK_SUPPORT_GPU
21#include "GrContextFactory.h"
22#include "SkGpuDevice.h"
23#endif
24
25#define WRITE_CSV 0
reed@google.com2b75f422011-07-07 13:43:38 +000026
27///////////////////////////////////////////////////////////////////////////////
28
29#define ILLEGAL_MODE ((SkXfermode::Mode)-1)
30
bungeman@google.com5af16f82011-09-02 15:06:44 +000031static const int outset = 100;
32static const SkColor bgColor = SK_ColorWHITE;
33static const int strokeWidth = 4;
34
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000035static void create(SkBitmap* bm, const SkIRect& bound) {
36 bm->allocN32Pixels(bound.width(), bound.height());
bungeman@google.com5af16f82011-09-02 15:06:44 +000037}
38
39static void drawBG(SkCanvas* canvas) {
40 canvas->drawColor(bgColor);
41}
42
43
44struct BlurTest {
45 void (*addPath)(SkPath*);
46 int viewLen;
47 SkIRect views[9];
48};
49
50//Path Draw Procs
51//Beware that paths themselves my draw differently depending on the clip.
52static void draw50x50Rect(SkPath* path) {
53 path->addRect(0, 0, SkIntToScalar(50), SkIntToScalar(50));
54}
55
56//Tests
57static BlurTest tests[] = {
58 { draw50x50Rect, 3, {
59 //inner half of blur
60 { 0, 0, 50, 50 },
61 //blur, but no path.
62 { 50 + strokeWidth/2, 50 + strokeWidth/2, 100, 100 },
63 //just an edge
64 { 40, strokeWidth, 60, 50 - strokeWidth },
65 }},
66};
67
68/** Assumes that the ref draw was completely inside ref canvas --
69 implies that everything outside is "bgColor".
70 Checks that all overlap is the same and that all non-overlap on the
71 ref is "bgColor".
72 */
73static bool compare(const SkBitmap& ref, const SkIRect& iref,
74 const SkBitmap& test, const SkIRect& itest)
75{
76 const int xOff = itest.fLeft - iref.fLeft;
77 const int yOff = itest.fTop - iref.fTop;
78
79 SkAutoLockPixels alpRef(ref);
80 SkAutoLockPixels alpTest(test);
81
82 for (int y = 0; y < test.height(); ++y) {
83 for (int x = 0; x < test.width(); ++x) {
84 SkColor testColor = test.getColor(x, y);
85 int refX = x + xOff;
86 int refY = y + yOff;
87 SkColor refColor;
88 if (refX >= 0 && refX < ref.width() &&
89 refY >= 0 && refY < ref.height())
90 {
91 refColor = ref.getColor(refX, refY);
92 } else {
93 refColor = bgColor;
94 }
95 if (refColor != testColor) {
96 return false;
97 }
98 }
99 }
100 return true;
101}
102
kkinnunen15302832015-12-01 04:35:26 -0800103DEF_TEST(BlurDrawing, reporter) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000104 SkPaint paint;
105 paint.setColor(SK_ColorGRAY);
106 paint.setStyle(SkPaint::kStroke_Style);
107 paint.setStrokeWidth(SkIntToScalar(strokeWidth));
reed@google.com2b75f422011-07-07 13:43:38 +0000108
robertphillips@google.comb7061172013-09-06 14:16:12 +0000109 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5));
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000110 for (int style = 0; style <= kLastEnum_SkBlurStyle; ++style) {
111 SkBlurStyle blurStyle = static_cast<SkBlurStyle>(style);
reed@google.com2b75f422011-07-07 13:43:38 +0000112
bungeman@google.com5af16f82011-09-02 15:06:44 +0000113 const uint32_t flagPermutations = SkBlurMaskFilter::kAll_BlurFlag;
114 for (uint32_t flags = 0; flags < flagPermutations; ++flags) {
reedefdfd512016-04-04 10:02:58 -0700115 paint.setMaskFilter(SkBlurMaskFilter::Make(blurStyle, sigma, flags));
bungeman@google.com5af16f82011-09-02 15:06:44 +0000116
tomhudson@google.com83a44462011-10-27 15:27:51 +0000117 for (size_t test = 0; test < SK_ARRAY_COUNT(tests); ++test) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000118 SkPath path;
119 tests[test].addPath(&path);
bungeman@google.comd16872c2011-09-02 15:46:17 +0000120 SkPath strokedPath;
121 paint.getFillPath(path, &strokedPath);
122 SkRect refBound = strokedPath.getBounds();
bungeman@google.com5af16f82011-09-02 15:06:44 +0000123 SkIRect iref;
124 refBound.roundOut(&iref);
bungeman@google.comd16872c2011-09-02 15:46:17 +0000125 iref.inset(-outset, -outset);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000126 SkBitmap refBitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000127 create(&refBitmap, iref);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000128
129 SkCanvas refCanvas(refBitmap);
130 refCanvas.translate(SkIntToScalar(-iref.fLeft),
131 SkIntToScalar(-iref.fTop));
132 drawBG(&refCanvas);
133 refCanvas.drawPath(path, paint);
134
135 for (int view = 0; view < tests[test].viewLen; ++view) {
136 SkIRect itest = tests[test].views[view];
137 SkBitmap testBitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000138 create(&testBitmap, itest);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000139
140 SkCanvas testCanvas(testBitmap);
141 testCanvas.translate(SkIntToScalar(-itest.fLeft),
142 SkIntToScalar(-itest.fTop));
143 drawBG(&testCanvas);
144 testCanvas.drawPath(path, paint);
145
146 REPORTER_ASSERT(reporter,
147 compare(refBitmap, iref, testBitmap, itest));
148 }
149 }
150 }
reed@google.com2b75f422011-07-07 13:43:38 +0000151 }
152}
153
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000154///////////////////////////////////////////////////////////////////////////////
155
156// Use SkBlurMask::BlurGroundTruth to blur a 'width' x 'height' solid
157// white rect. Return the right half of the middle row in 'result'.
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000158static void ground_truth_2d(int width, int height,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000159 SkScalar sigma,
160 int* result, int resultCount) {
161 SkMask src, dst;
162
163 src.fBounds.set(0, 0, width, height);
164 src.fFormat = SkMask::kA8_Format;
165 src.fRowBytes = src.fBounds.width();
166 src.fImage = SkMask::AllocImage(src.computeTotalImageSize());
167
168 memset(src.fImage, 0xff, src.computeTotalImageSize());
169
robertphillipse80eb922015-12-17 11:33:12 -0800170 if (!SkBlurMask::BlurGroundTruth(sigma, &dst, src, kNormal_SkBlurStyle)) {
171 return;
172 }
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000173
174 int midX = dst.fBounds.centerX();
175 int midY = dst.fBounds.centerY();
176 uint8_t* bytes = dst.getAddr8(midX, midY);
177 int i;
178 for (i = 0; i < dst.fBounds.width()-(midX-dst.fBounds.fLeft); ++i) {
179 if (i < resultCount) {
180 result[i] = bytes[i];
181 }
182 }
183 for ( ; i < resultCount; ++i) {
184 result[i] = 0;
185 }
robertphillips@google.com6d837aa2013-10-11 14:38:46 +0000186
187 SkMask::FreeImage(src.fImage);
188 SkMask::FreeImage(dst.fImage);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000189}
190
191// Implement a step function that is 255 between min and max; 0 elsewhere.
192static int step(int x, SkScalar min, SkScalar max) {
193 if (min < x && x < max) {
194 return 255;
195 }
196 return 0;
197}
198
199// Implement a Gaussian function with 0 mean and std.dev. of 'sigma'.
200static float gaussian(int x, SkScalar sigma) {
robertphillips@google.comb85564a2013-09-05 17:53:34 +0000201 float k = SK_Scalar1/(sigma * sqrtf(2.0f*SK_ScalarPI));
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000202 float exponent = -(x * x) / (2 * sigma * sigma);
robertphillips@google.comb85564a2013-09-05 17:53:34 +0000203 return k * expf(exponent);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000204}
205
206// Perform a brute force convolution of a step function with a Gaussian.
207// Return the right half in 'result'
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000208static void brute_force_1d(SkScalar stepMin, SkScalar stepMax,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000209 SkScalar gaussianSigma,
210 int* result, int resultCount) {
211
212 int gaussianRange = SkScalarCeilToInt(10 * gaussianSigma);
213
214 for (int i = 0; i < resultCount; ++i) {
215 SkScalar sum = 0.0f;
216 for (int j = -gaussianRange; j < gaussianRange; ++j) {
217 sum += gaussian(j, gaussianSigma) * step(i-j, stepMin, stepMax);
218 }
219
220 result[i] = SkClampMax(SkClampPos(int(sum + 0.5f)), 255);
221 }
222}
223
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000224static void blur_path(SkCanvas* canvas, const SkPath& path,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000225 SkScalar gaussianSigma) {
226
227 SkScalar midX = path.getBounds().centerX();
228 SkScalar midY = path.getBounds().centerY();
229
230 canvas->translate(-midX, -midY);
231
232 SkPaint blurPaint;
233 blurPaint.setColor(SK_ColorWHITE);
reedefdfd512016-04-04 10:02:58 -0700234 blurPaint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, gaussianSigma,
235 SkBlurMaskFilter::kHighQuality_BlurFlag));
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000236
237 canvas->drawColor(SK_ColorBLACK);
238 canvas->drawPath(path, blurPaint);
239}
240
241// Readback the blurred draw results from the canvas
242static void readback(SkCanvas* canvas, int* result, int resultCount) {
243 SkBitmap readback;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000244 readback.allocN32Pixels(resultCount, 30);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000245
246 SkIRect readBackRect = { 0, 0, resultCount, 30 };
247
248 canvas->readPixels(readBackRect, &readback);
249
250 readback.lockPixels();
251 SkPMColor* pixels = (SkPMColor*) readback.getAddr32(0, 15);
252
253 for (int i = 0; i < resultCount; ++i) {
254 result[i] = SkColorGetR(pixels[i]);
255 }
256}
257
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000258// Draw a blurred version of the provided path.
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000259// Return the right half of the middle row in 'result'.
260static void cpu_blur_path(const SkPath& path, SkScalar gaussianSigma,
261 int* result, int resultCount) {
262
263 SkBitmap bitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000264 bitmap.allocN32Pixels(resultCount, 30);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000265 SkCanvas canvas(bitmap);
266
267 blur_path(&canvas, path, gaussianSigma);
268 readback(&canvas, result, resultCount);
269}
270
271#if SK_SUPPORT_GPU
humper4a24cd82014-06-17 13:39:29 -0700272#if 0
273// temporary disable; see below for explanation
kkinnunen15302832015-12-01 04:35:26 -0800274static bool gpu_blur_path(GrContext* context, const SkPath& path,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000275 SkScalar gaussianSigma,
276 int* result, int resultCount) {
bsalomonf2703d82014-10-28 14:33:06 -0700277 GrSurfaceDesc desc;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000278 desc.fConfig = kSkia8888_GrPixelConfig;
bsalomonf2703d82014-10-28 14:33:06 -0700279 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000280 desc.fWidth = resultCount;
281 desc.fHeight = 30;
282 desc.fSampleCnt = 0;
283
halcanary96fcdcc2015-08-27 07:41:13 -0700284 SkAutoTUnref<GrTexture> texture(grContext->createTexture(desc, false, nullptr, 0));
halcanary385fe4d2015-08-26 13:07:48 -0700285 SkAutoTUnref<SkGpuDevice> device(new SkGpuDevice (grContext, texture.get()));
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000286 SkCanvas canvas(device.get());
287
288 blur_path(&canvas, path, gaussianSigma);
289 readback(&canvas, result, resultCount);
robertphillips@google.comcb3b6152013-11-14 14:47:56 +0000290 return true;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000291}
292#endif
humper4a24cd82014-06-17 13:39:29 -0700293#endif
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000294
295#if WRITE_CSV
296static void write_as_csv(const char* label, SkScalar scale, int* data, int count) {
297 SkDebugf("%s_%.2f,", label, scale);
298 for (int i = 0; i < count-1; ++i) {
299 SkDebugf("%d,", data[i]);
300 }
301 SkDebugf("%d\n", data[count-1]);
302}
303#endif
304
305static bool match(int* first, int* second, int count, int tol) {
306 int delta;
307 for (int i = 0; i < count; ++i) {
308 delta = first[i] - second[i];
309 if (delta > tol || delta < -tol) {
310 return false;
311 }
312 }
313
314 return true;
315}
316
317// Test out the normal blur style with a wide range of sigmas
bsalomonf2f1c172016-04-05 12:59:06 -0700318DEF_TEST(BlurSigmaRange, reporter) {
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000319 static const int kSize = 100;
320
321 // The geometry is offset a smidge to trigger:
322 // https://code.google.com/p/chromium/issues/detail?id=282418
323 SkPath rectPath;
324 rectPath.addRect(0.3f, 0.3f, 100.3f, 100.3f);
325
326 SkPoint polyPts[] = {
327 { 0.3f, 0.3f },
328 { 100.3f, 0.3f },
329 { 100.3f, 100.3f },
330 { 0.3f, 100.3f },
331 { 2.3f, 50.3f } // a little divet to throw off the rect special case
332 };
333 SkPath polyPath;
334 polyPath.addPoly(polyPts, SK_ARRAY_COUNT(polyPts), true);
335
336 int rectSpecialCaseResult[kSize];
337 int generalCaseResult[kSize];
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000338 int groundTruthResult[kSize];
339 int bruteForce1DResult[kSize];
340
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000341 SkScalar sigma = 10.0f;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000342
343 for (int i = 0; i < 4; ++i, sigma /= 10) {
344
345 cpu_blur_path(rectPath, sigma, rectSpecialCaseResult, kSize);
346 cpu_blur_path(polyPath, sigma, generalCaseResult, kSize);
humper4a24cd82014-06-17 13:39:29 -0700347
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000348 ground_truth_2d(100, 100, sigma, groundTruthResult, kSize);
349 brute_force_1d(-50.0f, 50.0f, sigma, bruteForce1DResult, kSize);
350
351 REPORTER_ASSERT(reporter, match(rectSpecialCaseResult, bruteForce1DResult, kSize, 5));
352 REPORTER_ASSERT(reporter, match(generalCaseResult, bruteForce1DResult, kSize, 15));
353#if SK_SUPPORT_GPU
humper4a24cd82014-06-17 13:39:29 -0700354#if 0
355 int gpuResult[kSize];
kkinnunen15302832015-12-01 04:35:26 -0800356 bool haveGPUResult = gpu_blur_path(context, rectPath, sigma, gpuResult, kSize);
humper4a24cd82014-06-17 13:39:29 -0700357 // Disabling this test for now -- I don't think it's a legit comparison.
358 // Will continue to investigate this.
robertphillips@google.comcb3b6152013-11-14 14:47:56 +0000359 if (haveGPUResult) {
360 // 1 works everywhere but: Ubuntu13 & Nexus4
361 REPORTER_ASSERT(reporter, match(gpuResult, bruteForce1DResult, kSize, 10));
362 }
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000363#endif
humper4a24cd82014-06-17 13:39:29 -0700364#endif
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000365 REPORTER_ASSERT(reporter, match(groundTruthResult, bruteForce1DResult, kSize, 1));
366
367#if WRITE_CSV
368 write_as_csv("RectSpecialCase", sigma, rectSpecialCaseResult, kSize);
369 write_as_csv("GeneralCase", sigma, generalCaseResult, kSize);
370#if SK_SUPPORT_GPU
371 write_as_csv("GPU", sigma, gpuResult, kSize);
372#endif
373 write_as_csv("GroundTruth2D", sigma, groundTruthResult, kSize);
374 write_as_csv("BruteForce1D", sigma, bruteForce1DResult, kSize);
375#endif
376 }
377}
378
reed@google.comdaaafa62014-04-29 15:20:16 +0000379///////////////////////////////////////////////////////////////////////////////////////////
380
381static SkBlurQuality blurMaskFilterFlags_as_quality(uint32_t blurMaskFilterFlags) {
382 return (blurMaskFilterFlags & SkBlurMaskFilter::kHighQuality_BlurFlag) ?
383 kHigh_SkBlurQuality : kLow_SkBlurQuality;
384}
385
386static uint32_t blurMaskFilterFlags_to_blurDrawLooperFlags(uint32_t bmf) {
387 const struct {
388 uint32_t fBlurMaskFilterFlag;
389 uint32_t fBlurDrawLooperFlag;
390 } pairs[] = {
391 { SkBlurMaskFilter::kIgnoreTransform_BlurFlag, SkBlurDrawLooper::kIgnoreTransform_BlurFlag },
392 { SkBlurMaskFilter::kHighQuality_BlurFlag, SkBlurDrawLooper::kHighQuality_BlurFlag },
393 };
394
395 uint32_t bdl = 0;
396 for (size_t i = 0; i < SK_ARRAY_COUNT(pairs); ++i) {
397 if (bmf & pairs[i].fBlurMaskFilterFlag) {
398 bdl |= pairs[i].fBlurDrawLooperFlag;
399 }
400 }
401 return bdl;
402}
403
404static void test_blurDrawLooper(skiatest::Reporter* reporter, SkScalar sigma,
405 SkBlurStyle style, uint32_t blurMaskFilterFlags) {
406 if (kNormal_SkBlurStyle != style) {
407 return; // blurdrawlooper only supports normal
408 }
409
410 const SkColor color = 0xFF335577;
411 const SkScalar dx = 10;
412 const SkScalar dy = -5;
413 const SkBlurQuality quality = blurMaskFilterFlags_as_quality(blurMaskFilterFlags);
414 uint32_t flags = blurMaskFilterFlags_to_blurDrawLooperFlags(blurMaskFilterFlags);
415
reed7b380d02016-03-21 13:25:16 -0700416 sk_sp<SkDrawLooper> lp(SkBlurDrawLooper::Make(color, sigma, dx, dy, flags));
reed@google.comdaaafa62014-04-29 15:20:16 +0000417
418 const bool expectSuccess = sigma > 0 &&
419 0 == (flags & SkBlurDrawLooper::kIgnoreTransform_BlurFlag);
420
reed7b380d02016-03-21 13:25:16 -0700421 if (nullptr == lp) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000422 REPORTER_ASSERT(reporter, sigma <= 0);
423 } else {
424 SkDrawLooper::BlurShadowRec rec;
425 bool success = lp->asABlurShadow(&rec);
426 REPORTER_ASSERT(reporter, success == expectSuccess);
427 if (success) {
428 REPORTER_ASSERT(reporter, rec.fSigma == sigma);
429 REPORTER_ASSERT(reporter, rec.fOffset.x() == dx);
430 REPORTER_ASSERT(reporter, rec.fOffset.y() == dy);
431 REPORTER_ASSERT(reporter, rec.fColor == color);
432 REPORTER_ASSERT(reporter, rec.fStyle == style);
433 REPORTER_ASSERT(reporter, rec.fQuality == quality);
434 }
435 }
436}
437
reed7b380d02016-03-21 13:25:16 -0700438static void test_looper(skiatest::Reporter* reporter, sk_sp<SkDrawLooper> lp, SkScalar sigma,
439 SkBlurStyle style, SkBlurQuality quality, bool expectSuccess) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000440 SkDrawLooper::BlurShadowRec rec;
441 bool success = lp->asABlurShadow(&rec);
442 REPORTER_ASSERT(reporter, success == expectSuccess);
443 if (success != expectSuccess) {
444 lp->asABlurShadow(&rec);
445 }
446 if (success) {
447 REPORTER_ASSERT(reporter, rec.fSigma == sigma);
448 REPORTER_ASSERT(reporter, rec.fStyle == style);
449 REPORTER_ASSERT(reporter, rec.fQuality == quality);
450 }
reed@google.comdaaafa62014-04-29 15:20:16 +0000451}
452
453static void make_noop_layer(SkLayerDrawLooper::Builder* builder) {
454 SkLayerDrawLooper::LayerInfo info;
455
456 info.fPaintBits = 0;
457 info.fColorMode = SkXfermode::kDst_Mode;
458 builder->addLayer(info);
459}
460
reedefdfd512016-04-04 10:02:58 -0700461static void make_blur_layer(SkLayerDrawLooper::Builder* builder, sk_sp<SkMaskFilter> mf) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000462 SkLayerDrawLooper::LayerInfo info;
463
464 info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit;
465 info.fColorMode = SkXfermode::kSrc_Mode;
466 SkPaint* paint = builder->addLayer(info);
reedefdfd512016-04-04 10:02:58 -0700467 paint->setMaskFilter(std::move(mf));
reed@google.comdaaafa62014-04-29 15:20:16 +0000468}
469
reedefdfd512016-04-04 10:02:58 -0700470static void test_layerDrawLooper(skiatest::Reporter* reporter, sk_sp<SkMaskFilter> mf,
471 SkScalar sigma, SkBlurStyle style, SkBlurQuality quality,
472 bool expectSuccess) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000473
474 SkLayerDrawLooper::LayerInfo info;
475 SkLayerDrawLooper::Builder builder;
476
477 // 1 layer is too few
478 make_noop_layer(&builder);
reed7b380d02016-03-21 13:25:16 -0700479 test_looper(reporter, builder.detach(), sigma, style, quality, false);
reed@google.comdaaafa62014-04-29 15:20:16 +0000480
481 // 2 layers is good, but need blur
482 make_noop_layer(&builder);
483 make_noop_layer(&builder);
reed7b380d02016-03-21 13:25:16 -0700484 test_looper(reporter, builder.detach(), sigma, style, quality, false);
reed@google.comdaaafa62014-04-29 15:20:16 +0000485
486 // 2 layers is just right
487 make_noop_layer(&builder);
488 make_blur_layer(&builder, mf);
reed7b380d02016-03-21 13:25:16 -0700489 test_looper(reporter, builder.detach(), sigma, style, quality, expectSuccess);
reed@google.comdaaafa62014-04-29 15:20:16 +0000490
491 // 3 layers is too many
492 make_noop_layer(&builder);
493 make_blur_layer(&builder, mf);
494 make_noop_layer(&builder);
reed7b380d02016-03-21 13:25:16 -0700495 test_looper(reporter, builder.detach(), sigma, style, quality, false);
reed@google.comdaaafa62014-04-29 15:20:16 +0000496}
497
kkinnunen15302832015-12-01 04:35:26 -0800498DEF_TEST(BlurAsABlur, reporter) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000499 const SkBlurStyle styles[] = {
500 kNormal_SkBlurStyle, kSolid_SkBlurStyle, kOuter_SkBlurStyle, kInner_SkBlurStyle
501 };
502 const SkScalar sigmas[] = {
503 // values <= 0 should not success for a blur
504 -1, 0, 0.5f, 2
505 };
506
507 // Test asABlur for SkBlurMaskFilter
508 //
509 for (size_t i = 0; i < SK_ARRAY_COUNT(styles); ++i) {
510 const SkBlurStyle style = (SkBlurStyle)styles[i];
511 for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) {
512 const SkScalar sigma = sigmas[j];
513 for (int flags = 0; flags <= SkBlurMaskFilter::kAll_BlurFlag; ++flags) {
514 const SkBlurQuality quality = blurMaskFilterFlags_as_quality(flags);
515
reedefdfd512016-04-04 10:02:58 -0700516 sk_sp<SkMaskFilter> mf(SkBlurMaskFilter::Make(style, sigma, flags));
halcanary96fcdcc2015-08-27 07:41:13 -0700517 if (nullptr == mf.get()) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000518 REPORTER_ASSERT(reporter, sigma <= 0);
519 } else {
520 REPORTER_ASSERT(reporter, sigma > 0);
521 SkMaskFilter::BlurRec rec;
522 bool success = mf->asABlur(&rec);
523 if (flags & SkBlurMaskFilter::kIgnoreTransform_BlurFlag) {
524 REPORTER_ASSERT(reporter, !success);
525 } else {
526 REPORTER_ASSERT(reporter, success);
527 REPORTER_ASSERT(reporter, rec.fSigma == sigma);
528 REPORTER_ASSERT(reporter, rec.fStyle == style);
529 REPORTER_ASSERT(reporter, rec.fQuality == quality);
530 }
reedefdfd512016-04-04 10:02:58 -0700531 test_layerDrawLooper(reporter, std::move(mf), sigma, style, quality, success);
reed@google.comdaaafa62014-04-29 15:20:16 +0000532 }
533 test_blurDrawLooper(reporter, sigma, style, flags);
534 }
535 }
536 }
537
538 // Test asABlur for SkEmbossMaskFilter -- should never succeed
539 //
540 {
541 SkEmbossMaskFilter::Light light = {
542 { 1, 1, 1 }, 0, 127, 127
543 };
544 for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) {
545 const SkScalar sigma = sigmas[j];
reedefdfd512016-04-04 10:02:58 -0700546 auto mf(SkEmbossMaskFilter::Make(sigma, light));
547 if (mf) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000548 SkMaskFilter::BlurRec rec;
549 bool success = mf->asABlur(&rec);
550 REPORTER_ASSERT(reporter, !success);
551 }
552 }
553 }
554}
555
robertphillips4abb0c12016-01-04 11:20:25 -0800556#if SK_SUPPORT_GPU
557
halcanary9d524f22016-03-29 09:03:52 -0700558// This exercises the problem discovered in crbug.com/570232. The return value from
robertphillips4abb0c12016-01-04 11:20:25 -0800559// SkBlurMask::BoxBlur wasn't being checked in SkBlurMaskFilter.cpp::GrRRectBlurEffect::Create
egdanielab527a52016-06-28 08:07:26 -0700560DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SmallBoxBlurBug, reporter, ctxInfo) {
robertphillips4abb0c12016-01-04 11:20:25 -0800561
562 SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128);
bsalomon8b7451a2016-05-11 06:33:06 -0700563 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
robertphillips4abb0c12016-01-04 11:20:25 -0800564 SkCanvas* canvas = surface->getCanvas();
565
566 SkRect r = SkRect::MakeXYWH(10, 10, 100, 100);
567 SkRRect rr = SkRRect::MakeRectXY(r, 10, 10);
568
569 SkPaint p;
reedefdfd512016-04-04 10:02:58 -0700570 p.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, 0.01f));
robertphillips4abb0c12016-01-04 11:20:25 -0800571
572 canvas->drawRRect(rr, p);
573}
574
575#endif
576
robertphillipsf5a83e82016-08-10 12:00:09 -0700577
578DEF_TEST(BlurredRRectNinePatchComputation, reporter) {
579 const SkRect r = SkRect::MakeXYWH(10, 10, 100, 100);
robertphillipsc4d2f902016-08-16 09:30:03 -0700580 static const SkScalar kBlurRad = 3.0f;
robertphillipsf5a83e82016-08-10 12:00:09 -0700581
582 bool ninePatchable;
583 SkRRect rrectToDraw;
584 SkISize size;
robertphillipsc4d2f902016-08-16 09:30:03 -0700585 SkScalar rectXs[SkBlurMaskFilter::kMaxDivisions], rectYs[SkBlurMaskFilter::kMaxDivisions];
586 SkScalar texXs[SkBlurMaskFilter::kMaxDivisions], texYs[SkBlurMaskFilter::kMaxDivisions];
587 int numX, numY;
588 uint32_t skipMask;
robertphillipsf5a83e82016-08-10 12:00:09 -0700589
590 // not nine-patchable
591 {
592 SkVector radii[4] = { { 100, 100 }, { 0, 0 }, { 100, 100 }, { 0, 0 } };
593
594 SkRRect rr;
595 rr.setRectRadii(r, radii);
596
robertphillipsc4d2f902016-08-16 09:30:03 -0700597 ninePatchable = SkBlurMaskFilter::ComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(),
598 kBlurRad, kBlurRad,
599 &rrectToDraw, &size,
600 rectXs, rectYs, texXs, texYs,
601 &numX, &numY, &skipMask);
robertphillipsf5a83e82016-08-10 12:00:09 -0700602 REPORTER_ASSERT(reporter, !ninePatchable);
603 }
604
605 // simple circular
606 {
robertphillipsc4d2f902016-08-16 09:30:03 -0700607 static const SkScalar kCornerRad = 10.0f;
robertphillipsf5a83e82016-08-10 12:00:09 -0700608 SkRRect rr;
robertphillipsc4d2f902016-08-16 09:30:03 -0700609 rr.setRectXY(r, kCornerRad, kCornerRad);
robertphillipsf5a83e82016-08-10 12:00:09 -0700610
robertphillipsc4d2f902016-08-16 09:30:03 -0700611 ninePatchable = SkBlurMaskFilter::ComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(),
612 kBlurRad, kBlurRad,
613 &rrectToDraw, &size,
614 rectXs, rectYs, texXs, texYs,
615 &numX, &numY, &skipMask);
616
617 static const SkScalar kAns = 12.0f * kBlurRad + 2.0f * kCornerRad + 1.0f;
robertphillipsf5a83e82016-08-10 12:00:09 -0700618 REPORTER_ASSERT(reporter, ninePatchable);
robertphillipsc4d2f902016-08-16 09:30:03 -0700619 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fWidth), kAns));
620 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fHeight), kAns));
621 REPORTER_ASSERT(reporter, 4 == numX && 4 == numY);
622 REPORTER_ASSERT(reporter, !skipMask);
robertphillipsf5a83e82016-08-10 12:00:09 -0700623 }
624
625 // simple elliptical
626 {
robertphillipsc4d2f902016-08-16 09:30:03 -0700627 static const SkScalar kXCornerRad = 2.0f;
628 static const SkScalar kYCornerRad = 10.0f;
robertphillipsf5a83e82016-08-10 12:00:09 -0700629 SkRRect rr;
robertphillipsc4d2f902016-08-16 09:30:03 -0700630 rr.setRectXY(r, kXCornerRad, kYCornerRad);
robertphillipsf5a83e82016-08-10 12:00:09 -0700631
robertphillipsc4d2f902016-08-16 09:30:03 -0700632 ninePatchable = SkBlurMaskFilter::ComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(),
633 kBlurRad, kBlurRad,
634 &rrectToDraw, &size,
635 rectXs, rectYs, texXs, texYs,
636 &numX, &numY, &skipMask);
637
638 static const SkScalar kXAns = 12.0f * kBlurRad + 2.0f * kXCornerRad + 1.0f;
639 static const SkScalar kYAns = 12.0f * kBlurRad + 2.0f * kYCornerRad + 1.0f;
640
robertphillipsf5a83e82016-08-10 12:00:09 -0700641 REPORTER_ASSERT(reporter, ninePatchable);
robertphillipsc4d2f902016-08-16 09:30:03 -0700642 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fWidth), kXAns));
643 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fHeight), kYAns));
644 REPORTER_ASSERT(reporter, 4 == numX && 4 == numY);
645 REPORTER_ASSERT(reporter, !skipMask);
646 }
647
648 // test-out occlusion
649 {
650 static const SkScalar kCornerRad = 10.0f;
651 SkRRect rr;
652 rr.setRectXY(r, kCornerRad, kCornerRad);
653
654 // The rectXs & rectYs should be { 1, 29, 91, 119 }. Add two more points around each.
655 SkScalar testLocs[] = {
656 -18.0f, -9.0f,
657 1.0f,
658 9.0f, 18.0f,
659 29.0f,
660 39.0f, 49.0f,
661 91.0f,
662 109.0f, 118.0f,
663 119.0f,
664 139.0f, 149.0f
665 };
666
667 for (int minY = 0; minY < (int)SK_ARRAY_COUNT(testLocs); ++minY) {
668 for (int maxY = minY+1; maxY < (int)SK_ARRAY_COUNT(testLocs); ++maxY) {
669 for (int minX = 0; minX < (int)SK_ARRAY_COUNT(testLocs); ++minX) {
670 for (int maxX = minX+1; maxX < (int)SK_ARRAY_COUNT(testLocs); ++maxX) {
671 SkRect occluder = SkRect::MakeLTRB(testLocs[minX], testLocs[minY],
672 testLocs[maxX], testLocs[maxY]);
673 if (occluder.isEmpty()) {
674 continue;
675 }
676
677 ninePatchable = SkBlurMaskFilter::ComputeBlurredRRectParams(
678 rr, rr, occluder,
679 kBlurRad, kBlurRad,
680 &rrectToDraw, &size,
681 rectXs, rectYs, texXs, texYs,
682 &numX, &numY, &skipMask);
683
684 static const SkScalar kAns = 12.0f * kBlurRad + 2.0f * kCornerRad + 1.0f;
685 REPORTER_ASSERT(reporter, ninePatchable);
686 REPORTER_ASSERT(reporter,
687 SkScalarNearlyEqual(SkIntToScalar(size.fWidth), kAns));
688 REPORTER_ASSERT(reporter,
689 SkScalarNearlyEqual(SkIntToScalar(size.fHeight), kAns));
690
691 int checkBit = 0x1;
692 for (int y = 0; y < numY-1; ++y) {
693 for (int x = 0; x < numX-1; ++x) {
694 SkRect cell = SkRect::MakeLTRB(rectXs[x], rectYs[y],
695 rectXs[x+1], rectYs[y+1]);
696 REPORTER_ASSERT(reporter,
697 SkToBool(skipMask & checkBit) ==
698 (cell.isEmpty() || occluder.contains(cell)));
699
700 REPORTER_ASSERT(reporter, texXs[x] >= 0 &&
701 texXs[x] <= size.fWidth);
702 REPORTER_ASSERT(reporter, texYs[y] >= 0 &&
703 texXs[y] <= size.fHeight);
704
705 checkBit <<= 1;
706 }
707 }
708 }
709 }
710 }
robertphillipsf5a83e82016-08-10 12:00:09 -0700711 }
robertphillipsc4d2f902016-08-16 09:30:03 -0700712
713
robertphillipsf5a83e82016-08-10 12:00:09 -0700714 }
715
716}
717
reed@google.comdaaafa62014-04-29 15:20:16 +0000718///////////////////////////////////////////////////////////////////////////////////////////