blob: 985917f1c0ad85b301caf47339886ed459996eb3 [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);
Mike Reed12e946b2017-04-17 10:53:29 -0400245 canvas->readPixels(readback, 0, 0);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000246
247 readback.lockPixels();
248 SkPMColor* pixels = (SkPMColor*) readback.getAddr32(0, 15);
249
250 for (int i = 0; i < resultCount; ++i) {
251 result[i] = SkColorGetR(pixels[i]);
252 }
253}
254
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000255// Draw a blurred version of the provided path.
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000256// Return the right half of the middle row in 'result'.
257static void cpu_blur_path(const SkPath& path, SkScalar gaussianSigma,
258 int* result, int resultCount) {
259
260 SkBitmap bitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000261 bitmap.allocN32Pixels(resultCount, 30);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000262 SkCanvas canvas(bitmap);
263
264 blur_path(&canvas, path, gaussianSigma);
265 readback(&canvas, result, resultCount);
266}
267
268#if SK_SUPPORT_GPU
humper4a24cd82014-06-17 13:39:29 -0700269#if 0
270// temporary disable; see below for explanation
kkinnunen15302832015-12-01 04:35:26 -0800271static bool gpu_blur_path(GrContext* context, const SkPath& path,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000272 SkScalar gaussianSigma,
273 int* result, int resultCount) {
bsalomonf2703d82014-10-28 14:33:06 -0700274 GrSurfaceDesc desc;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000275 desc.fConfig = kSkia8888_GrPixelConfig;
bsalomonf2703d82014-10-28 14:33:06 -0700276 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000277 desc.fWidth = resultCount;
278 desc.fHeight = 30;
279 desc.fSampleCnt = 0;
280
Hal Canary342b7ac2016-11-04 11:49:42 -0400281 sk_sp<GrTexture> texture(grContext->createTexture(desc, false, nullptr, 0));
282 sk_sp<SkGpuDevice> device(new SkGpuDevice(grContext, texture.get()));
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000283 SkCanvas canvas(device.get());
284
285 blur_path(&canvas, path, gaussianSigma);
286 readback(&canvas, result, resultCount);
robertphillips@google.comcb3b6152013-11-14 14:47:56 +0000287 return true;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000288}
289#endif
humper4a24cd82014-06-17 13:39:29 -0700290#endif
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000291
292#if WRITE_CSV
293static void write_as_csv(const char* label, SkScalar scale, int* data, int count) {
294 SkDebugf("%s_%.2f,", label, scale);
295 for (int i = 0; i < count-1; ++i) {
296 SkDebugf("%d,", data[i]);
297 }
298 SkDebugf("%d\n", data[count-1]);
299}
300#endif
301
302static bool match(int* first, int* second, int count, int tol) {
303 int delta;
304 for (int i = 0; i < count; ++i) {
305 delta = first[i] - second[i];
306 if (delta > tol || delta < -tol) {
307 return false;
308 }
309 }
310
311 return true;
312}
313
314// Test out the normal blur style with a wide range of sigmas
bsalomonf2f1c172016-04-05 12:59:06 -0700315DEF_TEST(BlurSigmaRange, reporter) {
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000316 static const int kSize = 100;
317
318 // The geometry is offset a smidge to trigger:
319 // https://code.google.com/p/chromium/issues/detail?id=282418
320 SkPath rectPath;
321 rectPath.addRect(0.3f, 0.3f, 100.3f, 100.3f);
322
323 SkPoint polyPts[] = {
324 { 0.3f, 0.3f },
325 { 100.3f, 0.3f },
326 { 100.3f, 100.3f },
327 { 0.3f, 100.3f },
328 { 2.3f, 50.3f } // a little divet to throw off the rect special case
329 };
330 SkPath polyPath;
331 polyPath.addPoly(polyPts, SK_ARRAY_COUNT(polyPts), true);
332
333 int rectSpecialCaseResult[kSize];
334 int generalCaseResult[kSize];
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000335 int groundTruthResult[kSize];
336 int bruteForce1DResult[kSize];
337
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000338 SkScalar sigma = 10.0f;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000339
340 for (int i = 0; i < 4; ++i, sigma /= 10) {
341
342 cpu_blur_path(rectPath, sigma, rectSpecialCaseResult, kSize);
343 cpu_blur_path(polyPath, sigma, generalCaseResult, kSize);
humper4a24cd82014-06-17 13:39:29 -0700344
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000345 ground_truth_2d(100, 100, sigma, groundTruthResult, kSize);
346 brute_force_1d(-50.0f, 50.0f, sigma, bruteForce1DResult, kSize);
347
348 REPORTER_ASSERT(reporter, match(rectSpecialCaseResult, bruteForce1DResult, kSize, 5));
349 REPORTER_ASSERT(reporter, match(generalCaseResult, bruteForce1DResult, kSize, 15));
350#if SK_SUPPORT_GPU
humper4a24cd82014-06-17 13:39:29 -0700351#if 0
352 int gpuResult[kSize];
kkinnunen15302832015-12-01 04:35:26 -0800353 bool haveGPUResult = gpu_blur_path(context, rectPath, sigma, gpuResult, kSize);
humper4a24cd82014-06-17 13:39:29 -0700354 // Disabling this test for now -- I don't think it's a legit comparison.
355 // Will continue to investigate this.
robertphillips@google.comcb3b6152013-11-14 14:47:56 +0000356 if (haveGPUResult) {
357 // 1 works everywhere but: Ubuntu13 & Nexus4
358 REPORTER_ASSERT(reporter, match(gpuResult, bruteForce1DResult, kSize, 10));
359 }
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000360#endif
humper4a24cd82014-06-17 13:39:29 -0700361#endif
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000362 REPORTER_ASSERT(reporter, match(groundTruthResult, bruteForce1DResult, kSize, 1));
363
364#if WRITE_CSV
365 write_as_csv("RectSpecialCase", sigma, rectSpecialCaseResult, kSize);
366 write_as_csv("GeneralCase", sigma, generalCaseResult, kSize);
367#if SK_SUPPORT_GPU
368 write_as_csv("GPU", sigma, gpuResult, kSize);
369#endif
370 write_as_csv("GroundTruth2D", sigma, groundTruthResult, kSize);
371 write_as_csv("BruteForce1D", sigma, bruteForce1DResult, kSize);
372#endif
373 }
374}
375
reed@google.comdaaafa62014-04-29 15:20:16 +0000376///////////////////////////////////////////////////////////////////////////////////////////
377
378static SkBlurQuality blurMaskFilterFlags_as_quality(uint32_t blurMaskFilterFlags) {
379 return (blurMaskFilterFlags & SkBlurMaskFilter::kHighQuality_BlurFlag) ?
380 kHigh_SkBlurQuality : kLow_SkBlurQuality;
381}
382
reed@google.comdaaafa62014-04-29 15:20:16 +0000383static void test_blurDrawLooper(skiatest::Reporter* reporter, SkScalar sigma,
384 SkBlurStyle style, uint32_t blurMaskFilterFlags) {
385 if (kNormal_SkBlurStyle != style) {
386 return; // blurdrawlooper only supports normal
387 }
388
389 const SkColor color = 0xFF335577;
390 const SkScalar dx = 10;
391 const SkScalar dy = -5;
Matt Sarettf160ad42017-03-23 16:23:38 -0400392 sk_sp<SkDrawLooper> lp(SkBlurDrawLooper::Make(color, sigma, dx, dy));
393 const bool expectSuccess = sigma > 0;
reed@google.comdaaafa62014-04-29 15:20:16 +0000394
reed7b380d02016-03-21 13:25:16 -0700395 if (nullptr == lp) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000396 REPORTER_ASSERT(reporter, sigma <= 0);
397 } else {
398 SkDrawLooper::BlurShadowRec rec;
399 bool success = lp->asABlurShadow(&rec);
400 REPORTER_ASSERT(reporter, success == expectSuccess);
401 if (success) {
402 REPORTER_ASSERT(reporter, rec.fSigma == sigma);
403 REPORTER_ASSERT(reporter, rec.fOffset.x() == dx);
404 REPORTER_ASSERT(reporter, rec.fOffset.y() == dy);
405 REPORTER_ASSERT(reporter, rec.fColor == color);
406 REPORTER_ASSERT(reporter, rec.fStyle == style);
Matt Sarettf160ad42017-03-23 16:23:38 -0400407 REPORTER_ASSERT(reporter, rec.fQuality == kLow_SkBlurQuality);
reed@google.comdaaafa62014-04-29 15:20:16 +0000408 }
409 }
410}
411
reed7b380d02016-03-21 13:25:16 -0700412static void test_looper(skiatest::Reporter* reporter, sk_sp<SkDrawLooper> lp, SkScalar sigma,
413 SkBlurStyle style, SkBlurQuality quality, bool expectSuccess) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000414 SkDrawLooper::BlurShadowRec rec;
415 bool success = lp->asABlurShadow(&rec);
416 REPORTER_ASSERT(reporter, success == expectSuccess);
417 if (success != expectSuccess) {
418 lp->asABlurShadow(&rec);
419 }
420 if (success) {
421 REPORTER_ASSERT(reporter, rec.fSigma == sigma);
422 REPORTER_ASSERT(reporter, rec.fStyle == style);
423 REPORTER_ASSERT(reporter, rec.fQuality == quality);
424 }
reed@google.comdaaafa62014-04-29 15:20:16 +0000425}
426
427static void make_noop_layer(SkLayerDrawLooper::Builder* builder) {
428 SkLayerDrawLooper::LayerInfo info;
429
430 info.fPaintBits = 0;
Mike Reedfaba3712016-11-03 14:45:31 -0400431 info.fColorMode = SkBlendMode::kDst;
reed@google.comdaaafa62014-04-29 15:20:16 +0000432 builder->addLayer(info);
433}
434
reedefdfd512016-04-04 10:02:58 -0700435static void make_blur_layer(SkLayerDrawLooper::Builder* builder, sk_sp<SkMaskFilter> mf) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000436 SkLayerDrawLooper::LayerInfo info;
437
438 info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit;
Mike Reedfaba3712016-11-03 14:45:31 -0400439 info.fColorMode = SkBlendMode::kSrc;
reed@google.comdaaafa62014-04-29 15:20:16 +0000440 SkPaint* paint = builder->addLayer(info);
reedefdfd512016-04-04 10:02:58 -0700441 paint->setMaskFilter(std::move(mf));
reed@google.comdaaafa62014-04-29 15:20:16 +0000442}
443
reedefdfd512016-04-04 10:02:58 -0700444static void test_layerDrawLooper(skiatest::Reporter* reporter, sk_sp<SkMaskFilter> mf,
445 SkScalar sigma, SkBlurStyle style, SkBlurQuality quality,
446 bool expectSuccess) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000447
448 SkLayerDrawLooper::LayerInfo info;
449 SkLayerDrawLooper::Builder builder;
450
451 // 1 layer is too few
452 make_noop_layer(&builder);
reed7b380d02016-03-21 13:25:16 -0700453 test_looper(reporter, builder.detach(), sigma, style, quality, false);
reed@google.comdaaafa62014-04-29 15:20:16 +0000454
455 // 2 layers is good, but need blur
456 make_noop_layer(&builder);
457 make_noop_layer(&builder);
reed7b380d02016-03-21 13:25:16 -0700458 test_looper(reporter, builder.detach(), sigma, style, quality, false);
reed@google.comdaaafa62014-04-29 15:20:16 +0000459
460 // 2 layers is just right
461 make_noop_layer(&builder);
462 make_blur_layer(&builder, mf);
reed7b380d02016-03-21 13:25:16 -0700463 test_looper(reporter, builder.detach(), sigma, style, quality, expectSuccess);
reed@google.comdaaafa62014-04-29 15:20:16 +0000464
465 // 3 layers is too many
466 make_noop_layer(&builder);
467 make_blur_layer(&builder, mf);
468 make_noop_layer(&builder);
reed7b380d02016-03-21 13:25:16 -0700469 test_looper(reporter, builder.detach(), sigma, style, quality, false);
reed@google.comdaaafa62014-04-29 15:20:16 +0000470}
471
kkinnunen15302832015-12-01 04:35:26 -0800472DEF_TEST(BlurAsABlur, reporter) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000473 const SkBlurStyle styles[] = {
474 kNormal_SkBlurStyle, kSolid_SkBlurStyle, kOuter_SkBlurStyle, kInner_SkBlurStyle
475 };
476 const SkScalar sigmas[] = {
477 // values <= 0 should not success for a blur
478 -1, 0, 0.5f, 2
479 };
480
481 // Test asABlur for SkBlurMaskFilter
482 //
483 for (size_t i = 0; i < SK_ARRAY_COUNT(styles); ++i) {
Robert Phillips98624d22016-12-19 11:37:37 -0500484 const SkBlurStyle style = styles[i];
reed@google.comdaaafa62014-04-29 15:20:16 +0000485 for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) {
486 const SkScalar sigma = sigmas[j];
487 for (int flags = 0; flags <= SkBlurMaskFilter::kAll_BlurFlag; ++flags) {
488 const SkBlurQuality quality = blurMaskFilterFlags_as_quality(flags);
489
reedefdfd512016-04-04 10:02:58 -0700490 sk_sp<SkMaskFilter> mf(SkBlurMaskFilter::Make(style, sigma, flags));
halcanary96fcdcc2015-08-27 07:41:13 -0700491 if (nullptr == mf.get()) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000492 REPORTER_ASSERT(reporter, sigma <= 0);
493 } else {
494 REPORTER_ASSERT(reporter, sigma > 0);
495 SkMaskFilter::BlurRec rec;
496 bool success = mf->asABlur(&rec);
497 if (flags & SkBlurMaskFilter::kIgnoreTransform_BlurFlag) {
498 REPORTER_ASSERT(reporter, !success);
499 } else {
500 REPORTER_ASSERT(reporter, success);
501 REPORTER_ASSERT(reporter, rec.fSigma == sigma);
502 REPORTER_ASSERT(reporter, rec.fStyle == style);
503 REPORTER_ASSERT(reporter, rec.fQuality == quality);
504 }
reedefdfd512016-04-04 10:02:58 -0700505 test_layerDrawLooper(reporter, std::move(mf), sigma, style, quality, success);
reed@google.comdaaafa62014-04-29 15:20:16 +0000506 }
507 test_blurDrawLooper(reporter, sigma, style, flags);
508 }
509 }
510 }
511
512 // Test asABlur for SkEmbossMaskFilter -- should never succeed
513 //
514 {
515 SkEmbossMaskFilter::Light light = {
516 { 1, 1, 1 }, 0, 127, 127
517 };
518 for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) {
519 const SkScalar sigma = sigmas[j];
reedefdfd512016-04-04 10:02:58 -0700520 auto mf(SkEmbossMaskFilter::Make(sigma, light));
521 if (mf) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000522 SkMaskFilter::BlurRec rec;
523 bool success = mf->asABlur(&rec);
524 REPORTER_ASSERT(reporter, !success);
525 }
526 }
527 }
528}
529
robertphillips4abb0c12016-01-04 11:20:25 -0800530#if SK_SUPPORT_GPU
531
halcanary9d524f22016-03-29 09:03:52 -0700532// This exercises the problem discovered in crbug.com/570232. The return value from
robertphillips4abb0c12016-01-04 11:20:25 -0800533// SkBlurMask::BoxBlur wasn't being checked in SkBlurMaskFilter.cpp::GrRRectBlurEffect::Create
egdanielab527a52016-06-28 08:07:26 -0700534DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SmallBoxBlurBug, reporter, ctxInfo) {
robertphillips4abb0c12016-01-04 11:20:25 -0800535
536 SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128);
bsalomon8b7451a2016-05-11 06:33:06 -0700537 auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
robertphillips4abb0c12016-01-04 11:20:25 -0800538 SkCanvas* canvas = surface->getCanvas();
539
540 SkRect r = SkRect::MakeXYWH(10, 10, 100, 100);
541 SkRRect rr = SkRRect::MakeRectXY(r, 10, 10);
542
543 SkPaint p;
reedefdfd512016-04-04 10:02:58 -0700544 p.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, 0.01f));
robertphillips4abb0c12016-01-04 11:20:25 -0800545
546 canvas->drawRRect(rr, p);
547}
548
549#endif
550
robertphillipsf5a83e82016-08-10 12:00:09 -0700551
552DEF_TEST(BlurredRRectNinePatchComputation, reporter) {
553 const SkRect r = SkRect::MakeXYWH(10, 10, 100, 100);
robertphillipsc4d2f902016-08-16 09:30:03 -0700554 static const SkScalar kBlurRad = 3.0f;
robertphillipsf5a83e82016-08-10 12:00:09 -0700555
556 bool ninePatchable;
557 SkRRect rrectToDraw;
558 SkISize size;
robertphillipsc4d2f902016-08-16 09:30:03 -0700559 SkScalar rectXs[SkBlurMaskFilter::kMaxDivisions], rectYs[SkBlurMaskFilter::kMaxDivisions];
560 SkScalar texXs[SkBlurMaskFilter::kMaxDivisions], texYs[SkBlurMaskFilter::kMaxDivisions];
561 int numX, numY;
562 uint32_t skipMask;
robertphillipsf5a83e82016-08-10 12:00:09 -0700563
564 // not nine-patchable
565 {
566 SkVector radii[4] = { { 100, 100 }, { 0, 0 }, { 100, 100 }, { 0, 0 } };
567
568 SkRRect rr;
569 rr.setRectRadii(r, radii);
570
robertphillipsc4d2f902016-08-16 09:30:03 -0700571 ninePatchable = SkBlurMaskFilter::ComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(),
572 kBlurRad, kBlurRad,
573 &rrectToDraw, &size,
574 rectXs, rectYs, texXs, texYs,
575 &numX, &numY, &skipMask);
robertphillipsf5a83e82016-08-10 12:00:09 -0700576 REPORTER_ASSERT(reporter, !ninePatchable);
577 }
578
579 // simple circular
580 {
robertphillipsc4d2f902016-08-16 09:30:03 -0700581 static const SkScalar kCornerRad = 10.0f;
robertphillipsf5a83e82016-08-10 12:00:09 -0700582 SkRRect rr;
robertphillipsc4d2f902016-08-16 09:30:03 -0700583 rr.setRectXY(r, kCornerRad, kCornerRad);
robertphillipsf5a83e82016-08-10 12:00:09 -0700584
robertphillipsc4d2f902016-08-16 09:30:03 -0700585 ninePatchable = SkBlurMaskFilter::ComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(),
586 kBlurRad, kBlurRad,
587 &rrectToDraw, &size,
588 rectXs, rectYs, texXs, texYs,
589 &numX, &numY, &skipMask);
590
591 static const SkScalar kAns = 12.0f * kBlurRad + 2.0f * kCornerRad + 1.0f;
robertphillipsf5a83e82016-08-10 12:00:09 -0700592 REPORTER_ASSERT(reporter, ninePatchable);
robertphillipsc4d2f902016-08-16 09:30:03 -0700593 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fWidth), kAns));
594 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fHeight), kAns));
595 REPORTER_ASSERT(reporter, 4 == numX && 4 == numY);
596 REPORTER_ASSERT(reporter, !skipMask);
robertphillipsf5a83e82016-08-10 12:00:09 -0700597 }
598
599 // simple elliptical
600 {
robertphillipsc4d2f902016-08-16 09:30:03 -0700601 static const SkScalar kXCornerRad = 2.0f;
602 static const SkScalar kYCornerRad = 10.0f;
robertphillipsf5a83e82016-08-10 12:00:09 -0700603 SkRRect rr;
robertphillipsc4d2f902016-08-16 09:30:03 -0700604 rr.setRectXY(r, kXCornerRad, kYCornerRad);
robertphillipsf5a83e82016-08-10 12:00:09 -0700605
robertphillipsc4d2f902016-08-16 09:30:03 -0700606 ninePatchable = SkBlurMaskFilter::ComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(),
607 kBlurRad, kBlurRad,
608 &rrectToDraw, &size,
609 rectXs, rectYs, texXs, texYs,
610 &numX, &numY, &skipMask);
611
612 static const SkScalar kXAns = 12.0f * kBlurRad + 2.0f * kXCornerRad + 1.0f;
613 static const SkScalar kYAns = 12.0f * kBlurRad + 2.0f * kYCornerRad + 1.0f;
614
robertphillipsf5a83e82016-08-10 12:00:09 -0700615 REPORTER_ASSERT(reporter, ninePatchable);
robertphillipsc4d2f902016-08-16 09:30:03 -0700616 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fWidth), kXAns));
617 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fHeight), kYAns));
618 REPORTER_ASSERT(reporter, 4 == numX && 4 == numY);
619 REPORTER_ASSERT(reporter, !skipMask);
620 }
621
622 // test-out occlusion
623 {
624 static const SkScalar kCornerRad = 10.0f;
625 SkRRect rr;
626 rr.setRectXY(r, kCornerRad, kCornerRad);
627
628 // The rectXs & rectYs should be { 1, 29, 91, 119 }. Add two more points around each.
629 SkScalar testLocs[] = {
630 -18.0f, -9.0f,
631 1.0f,
632 9.0f, 18.0f,
633 29.0f,
634 39.0f, 49.0f,
635 91.0f,
636 109.0f, 118.0f,
637 119.0f,
638 139.0f, 149.0f
639 };
640
641 for (int minY = 0; minY < (int)SK_ARRAY_COUNT(testLocs); ++minY) {
642 for (int maxY = minY+1; maxY < (int)SK_ARRAY_COUNT(testLocs); ++maxY) {
643 for (int minX = 0; minX < (int)SK_ARRAY_COUNT(testLocs); ++minX) {
644 for (int maxX = minX+1; maxX < (int)SK_ARRAY_COUNT(testLocs); ++maxX) {
645 SkRect occluder = SkRect::MakeLTRB(testLocs[minX], testLocs[minY],
646 testLocs[maxX], testLocs[maxY]);
647 if (occluder.isEmpty()) {
648 continue;
649 }
650
651 ninePatchable = SkBlurMaskFilter::ComputeBlurredRRectParams(
652 rr, rr, occluder,
653 kBlurRad, kBlurRad,
654 &rrectToDraw, &size,
655 rectXs, rectYs, texXs, texYs,
656 &numX, &numY, &skipMask);
657
658 static const SkScalar kAns = 12.0f * kBlurRad + 2.0f * kCornerRad + 1.0f;
659 REPORTER_ASSERT(reporter, ninePatchable);
660 REPORTER_ASSERT(reporter,
661 SkScalarNearlyEqual(SkIntToScalar(size.fWidth), kAns));
662 REPORTER_ASSERT(reporter,
663 SkScalarNearlyEqual(SkIntToScalar(size.fHeight), kAns));
664
665 int checkBit = 0x1;
666 for (int y = 0; y < numY-1; ++y) {
667 for (int x = 0; x < numX-1; ++x) {
668 SkRect cell = SkRect::MakeLTRB(rectXs[x], rectYs[y],
669 rectXs[x+1], rectYs[y+1]);
670 REPORTER_ASSERT(reporter,
671 SkToBool(skipMask & checkBit) ==
672 (cell.isEmpty() || occluder.contains(cell)));
673
674 REPORTER_ASSERT(reporter, texXs[x] >= 0 &&
675 texXs[x] <= size.fWidth);
676 REPORTER_ASSERT(reporter, texYs[y] >= 0 &&
677 texXs[y] <= size.fHeight);
678
679 checkBit <<= 1;
680 }
681 }
682 }
683 }
684 }
robertphillipsf5a83e82016-08-10 12:00:09 -0700685 }
robertphillipsc4d2f902016-08-16 09:30:03 -0700686
687
robertphillipsf5a83e82016-08-10 12:00:09 -0700688 }
689
690}
691
reed@google.comdaaafa62014-04-29 15:20:16 +0000692///////////////////////////////////////////////////////////////////////////////////////////