blob: 89f3a0a5cf69cfe06ce09336f9d8e07d0e08ea4a [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"
bungemand3ebb482015-08-05 13:57:49 -070012#include "SkEmbossMaskFilter.h"
13#include "SkLayerDrawLooper.h"
tomhudson@google.com889bd8b2011-09-27 17:38:17 +000014#include "SkMath.h"
bungeman@google.com5af16f82011-09-02 15:06:44 +000015#include "SkPaint.h"
bungemand3ebb482015-08-05 13:57:49 -070016#include "SkPath.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000017#include "Test.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000018
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +000019#if SK_SUPPORT_GPU
20#include "GrContextFactory.h"
21#include "SkGpuDevice.h"
22#endif
23
24#define WRITE_CSV 0
reed@google.com2b75f422011-07-07 13:43:38 +000025
26///////////////////////////////////////////////////////////////////////////////
27
28#define ILLEGAL_MODE ((SkXfermode::Mode)-1)
29
bungeman@google.com5af16f82011-09-02 15:06:44 +000030static const int outset = 100;
31static const SkColor bgColor = SK_ColorWHITE;
32static const int strokeWidth = 4;
33
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000034static void create(SkBitmap* bm, const SkIRect& bound) {
35 bm->allocN32Pixels(bound.width(), bound.height());
bungeman@google.com5af16f82011-09-02 15:06:44 +000036}
37
38static void drawBG(SkCanvas* canvas) {
39 canvas->drawColor(bgColor);
40}
41
42
43struct BlurTest {
44 void (*addPath)(SkPath*);
45 int viewLen;
46 SkIRect views[9];
47};
48
49//Path Draw Procs
50//Beware that paths themselves my draw differently depending on the clip.
51static void draw50x50Rect(SkPath* path) {
52 path->addRect(0, 0, SkIntToScalar(50), SkIntToScalar(50));
53}
54
55//Tests
56static BlurTest tests[] = {
57 { draw50x50Rect, 3, {
58 //inner half of blur
59 { 0, 0, 50, 50 },
60 //blur, but no path.
61 { 50 + strokeWidth/2, 50 + strokeWidth/2, 100, 100 },
62 //just an edge
63 { 40, strokeWidth, 60, 50 - strokeWidth },
64 }},
65};
66
67/** Assumes that the ref draw was completely inside ref canvas --
68 implies that everything outside is "bgColor".
69 Checks that all overlap is the same and that all non-overlap on the
70 ref is "bgColor".
71 */
72static bool compare(const SkBitmap& ref, const SkIRect& iref,
73 const SkBitmap& test, const SkIRect& itest)
74{
75 const int xOff = itest.fLeft - iref.fLeft;
76 const int yOff = itest.fTop - iref.fTop;
77
78 SkAutoLockPixels alpRef(ref);
79 SkAutoLockPixels alpTest(test);
80
81 for (int y = 0; y < test.height(); ++y) {
82 for (int x = 0; x < test.width(); ++x) {
83 SkColor testColor = test.getColor(x, y);
84 int refX = x + xOff;
85 int refY = y + yOff;
86 SkColor refColor;
87 if (refX >= 0 && refX < ref.width() &&
88 refY >= 0 && refY < ref.height())
89 {
90 refColor = ref.getColor(refX, refY);
91 } else {
92 refColor = bgColor;
93 }
94 if (refColor != testColor) {
95 return false;
96 }
97 }
98 }
99 return true;
100}
101
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000102static void test_blur_drawing(skiatest::Reporter* reporter) {
reed@google.com2b75f422011-07-07 13:43:38 +0000103
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) {
115 SkMaskFilter* filter;
robertphillips@google.comb7061172013-09-06 14:16:12 +0000116 filter = SkBlurMaskFilter::Create(blurStyle, sigma, flags);
reed@google.com2b75f422011-07-07 13:43:38 +0000117
bungeman@google.com5af16f82011-09-02 15:06:44 +0000118 paint.setMaskFilter(filter);
119 filter->unref();
120
tomhudson@google.com83a44462011-10-27 15:27:51 +0000121 for (size_t test = 0; test < SK_ARRAY_COUNT(tests); ++test) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000122 SkPath path;
123 tests[test].addPath(&path);
bungeman@google.comd16872c2011-09-02 15:46:17 +0000124 SkPath strokedPath;
125 paint.getFillPath(path, &strokedPath);
126 SkRect refBound = strokedPath.getBounds();
bungeman@google.com5af16f82011-09-02 15:06:44 +0000127 SkIRect iref;
128 refBound.roundOut(&iref);
bungeman@google.comd16872c2011-09-02 15:46:17 +0000129 iref.inset(-outset, -outset);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000130 SkBitmap refBitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000131 create(&refBitmap, iref);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000132
133 SkCanvas refCanvas(refBitmap);
134 refCanvas.translate(SkIntToScalar(-iref.fLeft),
135 SkIntToScalar(-iref.fTop));
136 drawBG(&refCanvas);
137 refCanvas.drawPath(path, paint);
138
139 for (int view = 0; view < tests[test].viewLen; ++view) {
140 SkIRect itest = tests[test].views[view];
141 SkBitmap testBitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000142 create(&testBitmap, itest);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000143
144 SkCanvas testCanvas(testBitmap);
145 testCanvas.translate(SkIntToScalar(-itest.fLeft),
146 SkIntToScalar(-itest.fTop));
147 drawBG(&testCanvas);
148 testCanvas.drawPath(path, paint);
149
150 REPORTER_ASSERT(reporter,
151 compare(refBitmap, iref, testBitmap, itest));
152 }
153 }
154 }
reed@google.com2b75f422011-07-07 13:43:38 +0000155 }
156}
157
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000158///////////////////////////////////////////////////////////////////////////////
159
160// Use SkBlurMask::BlurGroundTruth to blur a 'width' x 'height' solid
161// white rect. Return the right half of the middle row in 'result'.
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000162static void ground_truth_2d(int width, int height,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000163 SkScalar sigma,
164 int* result, int resultCount) {
165 SkMask src, dst;
166
167 src.fBounds.set(0, 0, width, height);
168 src.fFormat = SkMask::kA8_Format;
169 src.fRowBytes = src.fBounds.width();
170 src.fImage = SkMask::AllocImage(src.computeTotalImageSize());
171
172 memset(src.fImage, 0xff, src.computeTotalImageSize());
173
halcanary96fcdcc2015-08-27 07:41:13 -0700174 dst.fImage = nullptr;
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000175 SkBlurMask::BlurGroundTruth(sigma, &dst, src, kNormal_SkBlurStyle);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000176
177 int midX = dst.fBounds.centerX();
178 int midY = dst.fBounds.centerY();
179 uint8_t* bytes = dst.getAddr8(midX, midY);
180 int i;
181 for (i = 0; i < dst.fBounds.width()-(midX-dst.fBounds.fLeft); ++i) {
182 if (i < resultCount) {
183 result[i] = bytes[i];
184 }
185 }
186 for ( ; i < resultCount; ++i) {
187 result[i] = 0;
188 }
robertphillips@google.com6d837aa2013-10-11 14:38:46 +0000189
190 SkMask::FreeImage(src.fImage);
191 SkMask::FreeImage(dst.fImage);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000192}
193
194// Implement a step function that is 255 between min and max; 0 elsewhere.
195static int step(int x, SkScalar min, SkScalar max) {
196 if (min < x && x < max) {
197 return 255;
198 }
199 return 0;
200}
201
202// Implement a Gaussian function with 0 mean and std.dev. of 'sigma'.
203static float gaussian(int x, SkScalar sigma) {
robertphillips@google.comb85564a2013-09-05 17:53:34 +0000204 float k = SK_Scalar1/(sigma * sqrtf(2.0f*SK_ScalarPI));
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000205 float exponent = -(x * x) / (2 * sigma * sigma);
robertphillips@google.comb85564a2013-09-05 17:53:34 +0000206 return k * expf(exponent);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000207}
208
209// Perform a brute force convolution of a step function with a Gaussian.
210// Return the right half in 'result'
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000211static void brute_force_1d(SkScalar stepMin, SkScalar stepMax,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000212 SkScalar gaussianSigma,
213 int* result, int resultCount) {
214
215 int gaussianRange = SkScalarCeilToInt(10 * gaussianSigma);
216
217 for (int i = 0; i < resultCount; ++i) {
218 SkScalar sum = 0.0f;
219 for (int j = -gaussianRange; j < gaussianRange; ++j) {
220 sum += gaussian(j, gaussianSigma) * step(i-j, stepMin, stepMax);
221 }
222
223 result[i] = SkClampMax(SkClampPos(int(sum + 0.5f)), 255);
224 }
225}
226
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000227static void blur_path(SkCanvas* canvas, const SkPath& path,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000228 SkScalar gaussianSigma) {
229
230 SkScalar midX = path.getBounds().centerX();
231 SkScalar midY = path.getBounds().centerY();
232
233 canvas->translate(-midX, -midY);
234
235 SkPaint blurPaint;
236 blurPaint.setColor(SK_ColorWHITE);
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000237 SkMaskFilter* filter = SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000238 gaussianSigma,
239 SkBlurMaskFilter::kHighQuality_BlurFlag);
240 blurPaint.setMaskFilter(filter)->unref();
241
242 canvas->drawColor(SK_ColorBLACK);
243 canvas->drawPath(path, blurPaint);
244}
245
246// Readback the blurred draw results from the canvas
247static void readback(SkCanvas* canvas, int* result, int resultCount) {
248 SkBitmap readback;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000249 readback.allocN32Pixels(resultCount, 30);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000250
251 SkIRect readBackRect = { 0, 0, resultCount, 30 };
252
253 canvas->readPixels(readBackRect, &readback);
254
255 readback.lockPixels();
256 SkPMColor* pixels = (SkPMColor*) readback.getAddr32(0, 15);
257
258 for (int i = 0; i < resultCount; ++i) {
259 result[i] = SkColorGetR(pixels[i]);
260 }
261}
262
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000263// Draw a blurred version of the provided path.
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000264// Return the right half of the middle row in 'result'.
265static void cpu_blur_path(const SkPath& path, SkScalar gaussianSigma,
266 int* result, int resultCount) {
267
268 SkBitmap bitmap;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +0000269 bitmap.allocN32Pixels(resultCount, 30);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000270 SkCanvas canvas(bitmap);
271
272 blur_path(&canvas, path, gaussianSigma);
273 readback(&canvas, result, resultCount);
274}
275
276#if SK_SUPPORT_GPU
humper4a24cd82014-06-17 13:39:29 -0700277#if 0
278// temporary disable; see below for explanation
robertphillips@google.comcb3b6152013-11-14 14:47:56 +0000279static bool gpu_blur_path(GrContextFactory* factory, const SkPath& path,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000280 SkScalar gaussianSigma,
281 int* result, int resultCount) {
282
283 GrContext* grContext = factory->get(GrContextFactory::kNative_GLContextType);
halcanary96fcdcc2015-08-27 07:41:13 -0700284 if (nullptr == grContext) {
robertphillips@google.comcb3b6152013-11-14 14:47:56 +0000285 return false;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000286 }
287
bsalomonf2703d82014-10-28 14:33:06 -0700288 GrSurfaceDesc desc;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000289 desc.fConfig = kSkia8888_GrPixelConfig;
bsalomonf2703d82014-10-28 14:33:06 -0700290 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000291 desc.fWidth = resultCount;
292 desc.fHeight = 30;
293 desc.fSampleCnt = 0;
294
halcanary96fcdcc2015-08-27 07:41:13 -0700295 SkAutoTUnref<GrTexture> texture(grContext->createTexture(desc, false, nullptr, 0));
halcanary385fe4d2015-08-26 13:07:48 -0700296 SkAutoTUnref<SkGpuDevice> device(new SkGpuDevice (grContext, texture.get()));
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000297 SkCanvas canvas(device.get());
298
299 blur_path(&canvas, path, gaussianSigma);
300 readback(&canvas, result, resultCount);
robertphillips@google.comcb3b6152013-11-14 14:47:56 +0000301 return true;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000302}
303#endif
humper4a24cd82014-06-17 13:39:29 -0700304#endif
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000305
306#if WRITE_CSV
307static void write_as_csv(const char* label, SkScalar scale, int* data, int count) {
308 SkDebugf("%s_%.2f,", label, scale);
309 for (int i = 0; i < count-1; ++i) {
310 SkDebugf("%d,", data[i]);
311 }
312 SkDebugf("%d\n", data[count-1]);
313}
314#endif
315
316static bool match(int* first, int* second, int count, int tol) {
317 int delta;
318 for (int i = 0; i < count; ++i) {
319 delta = first[i] - second[i];
320 if (delta > tol || delta < -tol) {
321 return false;
322 }
323 }
324
325 return true;
326}
327
328// Test out the normal blur style with a wide range of sigmas
329static void test_sigma_range(skiatest::Reporter* reporter, GrContextFactory* factory) {
330
331 static const int kSize = 100;
332
333 // The geometry is offset a smidge to trigger:
334 // https://code.google.com/p/chromium/issues/detail?id=282418
335 SkPath rectPath;
336 rectPath.addRect(0.3f, 0.3f, 100.3f, 100.3f);
337
338 SkPoint polyPts[] = {
339 { 0.3f, 0.3f },
340 { 100.3f, 0.3f },
341 { 100.3f, 100.3f },
342 { 0.3f, 100.3f },
343 { 2.3f, 50.3f } // a little divet to throw off the rect special case
344 };
345 SkPath polyPath;
346 polyPath.addPoly(polyPts, SK_ARRAY_COUNT(polyPts), true);
347
348 int rectSpecialCaseResult[kSize];
349 int generalCaseResult[kSize];
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000350 int groundTruthResult[kSize];
351 int bruteForce1DResult[kSize];
352
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000353 SkScalar sigma = 10.0f;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000354
355 for (int i = 0; i < 4; ++i, sigma /= 10) {
356
357 cpu_blur_path(rectPath, sigma, rectSpecialCaseResult, kSize);
358 cpu_blur_path(polyPath, sigma, generalCaseResult, kSize);
humper4a24cd82014-06-17 13:39:29 -0700359
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000360 ground_truth_2d(100, 100, sigma, groundTruthResult, kSize);
361 brute_force_1d(-50.0f, 50.0f, sigma, bruteForce1DResult, kSize);
362
363 REPORTER_ASSERT(reporter, match(rectSpecialCaseResult, bruteForce1DResult, kSize, 5));
364 REPORTER_ASSERT(reporter, match(generalCaseResult, bruteForce1DResult, kSize, 15));
365#if SK_SUPPORT_GPU
humper4a24cd82014-06-17 13:39:29 -0700366#if 0
367 int gpuResult[kSize];
368 bool haveGPUResult = gpu_blur_path(factory, rectPath, sigma, gpuResult, kSize);
369 // Disabling this test for now -- I don't think it's a legit comparison.
370 // Will continue to investigate this.
robertphillips@google.comcb3b6152013-11-14 14:47:56 +0000371 if (haveGPUResult) {
372 // 1 works everywhere but: Ubuntu13 & Nexus4
373 REPORTER_ASSERT(reporter, match(gpuResult, bruteForce1DResult, kSize, 10));
374 }
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000375#endif
humper4a24cd82014-06-17 13:39:29 -0700376#endif
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000377 REPORTER_ASSERT(reporter, match(groundTruthResult, bruteForce1DResult, kSize, 1));
378
379#if WRITE_CSV
380 write_as_csv("RectSpecialCase", sigma, rectSpecialCaseResult, kSize);
381 write_as_csv("GeneralCase", sigma, generalCaseResult, kSize);
382#if SK_SUPPORT_GPU
383 write_as_csv("GPU", sigma, gpuResult, kSize);
384#endif
385 write_as_csv("GroundTruth2D", sigma, groundTruthResult, kSize);
386 write_as_csv("BruteForce1D", sigma, bruteForce1DResult, kSize);
387#endif
388 }
389}
390
reed@google.comdaaafa62014-04-29 15:20:16 +0000391///////////////////////////////////////////////////////////////////////////////////////////
392
393static SkBlurQuality blurMaskFilterFlags_as_quality(uint32_t blurMaskFilterFlags) {
394 return (blurMaskFilterFlags & SkBlurMaskFilter::kHighQuality_BlurFlag) ?
395 kHigh_SkBlurQuality : kLow_SkBlurQuality;
396}
397
398static uint32_t blurMaskFilterFlags_to_blurDrawLooperFlags(uint32_t bmf) {
399 const struct {
400 uint32_t fBlurMaskFilterFlag;
401 uint32_t fBlurDrawLooperFlag;
402 } pairs[] = {
403 { SkBlurMaskFilter::kIgnoreTransform_BlurFlag, SkBlurDrawLooper::kIgnoreTransform_BlurFlag },
404 { SkBlurMaskFilter::kHighQuality_BlurFlag, SkBlurDrawLooper::kHighQuality_BlurFlag },
405 };
406
407 uint32_t bdl = 0;
408 for (size_t i = 0; i < SK_ARRAY_COUNT(pairs); ++i) {
409 if (bmf & pairs[i].fBlurMaskFilterFlag) {
410 bdl |= pairs[i].fBlurDrawLooperFlag;
411 }
412 }
413 return bdl;
414}
415
416static void test_blurDrawLooper(skiatest::Reporter* reporter, SkScalar sigma,
417 SkBlurStyle style, uint32_t blurMaskFilterFlags) {
418 if (kNormal_SkBlurStyle != style) {
419 return; // blurdrawlooper only supports normal
420 }
421
422 const SkColor color = 0xFF335577;
423 const SkScalar dx = 10;
424 const SkScalar dy = -5;
425 const SkBlurQuality quality = blurMaskFilterFlags_as_quality(blurMaskFilterFlags);
426 uint32_t flags = blurMaskFilterFlags_to_blurDrawLooperFlags(blurMaskFilterFlags);
427
428 SkAutoTUnref<SkDrawLooper> lp(SkBlurDrawLooper::Create(color, sigma, dx, dy, flags));
429
430 const bool expectSuccess = sigma > 0 &&
431 0 == (flags & SkBlurDrawLooper::kIgnoreTransform_BlurFlag);
432
halcanary96fcdcc2015-08-27 07:41:13 -0700433 if (nullptr == lp.get()) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000434 REPORTER_ASSERT(reporter, sigma <= 0);
435 } else {
436 SkDrawLooper::BlurShadowRec rec;
437 bool success = lp->asABlurShadow(&rec);
438 REPORTER_ASSERT(reporter, success == expectSuccess);
439 if (success) {
440 REPORTER_ASSERT(reporter, rec.fSigma == sigma);
441 REPORTER_ASSERT(reporter, rec.fOffset.x() == dx);
442 REPORTER_ASSERT(reporter, rec.fOffset.y() == dy);
443 REPORTER_ASSERT(reporter, rec.fColor == color);
444 REPORTER_ASSERT(reporter, rec.fStyle == style);
445 REPORTER_ASSERT(reporter, rec.fQuality == quality);
446 }
447 }
448}
449
450static void test_delete_looper(skiatest::Reporter* reporter, SkDrawLooper* lp, SkScalar sigma,
451 SkBlurStyle style, SkBlurQuality quality, bool expectSuccess) {
452 SkDrawLooper::BlurShadowRec rec;
453 bool success = lp->asABlurShadow(&rec);
454 REPORTER_ASSERT(reporter, success == expectSuccess);
455 if (success != expectSuccess) {
456 lp->asABlurShadow(&rec);
457 }
458 if (success) {
459 REPORTER_ASSERT(reporter, rec.fSigma == sigma);
460 REPORTER_ASSERT(reporter, rec.fStyle == style);
461 REPORTER_ASSERT(reporter, rec.fQuality == quality);
462 }
463 lp->unref();
464}
465
466static void make_noop_layer(SkLayerDrawLooper::Builder* builder) {
467 SkLayerDrawLooper::LayerInfo info;
468
469 info.fPaintBits = 0;
470 info.fColorMode = SkXfermode::kDst_Mode;
471 builder->addLayer(info);
472}
473
474static void make_blur_layer(SkLayerDrawLooper::Builder* builder, SkMaskFilter* mf) {
475 SkLayerDrawLooper::LayerInfo info;
476
477 info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit;
478 info.fColorMode = SkXfermode::kSrc_Mode;
479 SkPaint* paint = builder->addLayer(info);
480 paint->setMaskFilter(mf);
481}
482
483static void test_layerDrawLooper(skiatest::Reporter* reporter, SkMaskFilter* mf, SkScalar sigma,
484 SkBlurStyle style, SkBlurQuality quality, bool expectSuccess) {
485
486 SkLayerDrawLooper::LayerInfo info;
487 SkLayerDrawLooper::Builder builder;
488
489 // 1 layer is too few
490 make_noop_layer(&builder);
491 test_delete_looper(reporter, builder.detachLooper(), sigma, style, quality, false);
492
493 // 2 layers is good, but need blur
494 make_noop_layer(&builder);
495 make_noop_layer(&builder);
496 test_delete_looper(reporter, builder.detachLooper(), sigma, style, quality, false);
497
498 // 2 layers is just right
499 make_noop_layer(&builder);
500 make_blur_layer(&builder, mf);
501 test_delete_looper(reporter, builder.detachLooper(), sigma, style, quality, expectSuccess);
502
503 // 3 layers is too many
504 make_noop_layer(&builder);
505 make_blur_layer(&builder, mf);
506 make_noop_layer(&builder);
507 test_delete_looper(reporter, builder.detachLooper(), sigma, style, quality, false);
508}
509
510static void test_asABlur(skiatest::Reporter* reporter) {
511 const SkBlurStyle styles[] = {
512 kNormal_SkBlurStyle, kSolid_SkBlurStyle, kOuter_SkBlurStyle, kInner_SkBlurStyle
513 };
514 const SkScalar sigmas[] = {
515 // values <= 0 should not success for a blur
516 -1, 0, 0.5f, 2
517 };
518
519 // Test asABlur for SkBlurMaskFilter
520 //
521 for (size_t i = 0; i < SK_ARRAY_COUNT(styles); ++i) {
522 const SkBlurStyle style = (SkBlurStyle)styles[i];
523 for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) {
524 const SkScalar sigma = sigmas[j];
525 for (int flags = 0; flags <= SkBlurMaskFilter::kAll_BlurFlag; ++flags) {
526 const SkBlurQuality quality = blurMaskFilterFlags_as_quality(flags);
527
528 SkAutoTUnref<SkMaskFilter> mf(SkBlurMaskFilter::Create(style, sigma, flags));
halcanary96fcdcc2015-08-27 07:41:13 -0700529 if (nullptr == mf.get()) {
reed@google.comdaaafa62014-04-29 15:20:16 +0000530 REPORTER_ASSERT(reporter, sigma <= 0);
531 } else {
532 REPORTER_ASSERT(reporter, sigma > 0);
533 SkMaskFilter::BlurRec rec;
534 bool success = mf->asABlur(&rec);
535 if (flags & SkBlurMaskFilter::kIgnoreTransform_BlurFlag) {
536 REPORTER_ASSERT(reporter, !success);
537 } else {
538 REPORTER_ASSERT(reporter, success);
539 REPORTER_ASSERT(reporter, rec.fSigma == sigma);
540 REPORTER_ASSERT(reporter, rec.fStyle == style);
541 REPORTER_ASSERT(reporter, rec.fQuality == quality);
542 }
543 test_layerDrawLooper(reporter, mf, sigma, style, quality, success);
544 }
545 test_blurDrawLooper(reporter, sigma, style, flags);
546 }
547 }
548 }
549
550 // Test asABlur for SkEmbossMaskFilter -- should never succeed
551 //
552 {
553 SkEmbossMaskFilter::Light light = {
554 { 1, 1, 1 }, 0, 127, 127
555 };
556 for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) {
557 const SkScalar sigma = sigmas[j];
558 SkAutoTUnref<SkMaskFilter> mf(SkEmbossMaskFilter::Create(sigma, light));
559 if (mf.get()) {
560 SkMaskFilter::BlurRec rec;
561 bool success = mf->asABlur(&rec);
562 REPORTER_ASSERT(reporter, !success);
563 }
564 }
565 }
566}
567
568///////////////////////////////////////////////////////////////////////////////////////////
569
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000570DEF_GPUTEST(Blur, reporter, factory) {
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000571 test_blur_drawing(reporter);
572 test_sigma_range(reporter, factory);
reed@google.comdaaafa62014-04-29 15:20:16 +0000573 test_asABlur(reporter);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000574}