blob: 8eb1050102c5a4dad741d924324ea367cfe303d5 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@google.com2b75f422011-07-07 13:43:38 +00008#include "Test.h"
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +00009#include "SkBlurMask.h"
reed@google.com2b75f422011-07-07 13:43:38 +000010#include "SkBlurMaskFilter.h"
bungeman@google.com5af16f82011-09-02 15:06:44 +000011#include "SkCanvas.h"
tomhudson@google.com889bd8b2011-09-27 17:38:17 +000012#include "SkMath.h"
bungeman@google.com5af16f82011-09-02 15:06:44 +000013#include "SkPaint.h"
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +000014#if SK_SUPPORT_GPU
15#include "GrContextFactory.h"
16#include "SkGpuDevice.h"
17#endif
18
19#define WRITE_CSV 0
reed@google.com2b75f422011-07-07 13:43:38 +000020
21///////////////////////////////////////////////////////////////////////////////
22
23#define ILLEGAL_MODE ((SkXfermode::Mode)-1)
24
bungeman@google.com5af16f82011-09-02 15:06:44 +000025static const int outset = 100;
26static const SkColor bgColor = SK_ColorWHITE;
27static const int strokeWidth = 4;
28
29static void create(SkBitmap* bm, SkIRect bound, SkBitmap::Config config) {
30 bm->setConfig(config, bound.width(), bound.height());
31 bm->allocPixels();
32}
33
34static void drawBG(SkCanvas* canvas) {
35 canvas->drawColor(bgColor);
36}
37
38
39struct BlurTest {
40 void (*addPath)(SkPath*);
41 int viewLen;
42 SkIRect views[9];
43};
44
45//Path Draw Procs
46//Beware that paths themselves my draw differently depending on the clip.
47static void draw50x50Rect(SkPath* path) {
48 path->addRect(0, 0, SkIntToScalar(50), SkIntToScalar(50));
49}
50
51//Tests
52static BlurTest tests[] = {
53 { draw50x50Rect, 3, {
54 //inner half of blur
55 { 0, 0, 50, 50 },
56 //blur, but no path.
57 { 50 + strokeWidth/2, 50 + strokeWidth/2, 100, 100 },
58 //just an edge
59 { 40, strokeWidth, 60, 50 - strokeWidth },
60 }},
61};
62
63/** Assumes that the ref draw was completely inside ref canvas --
64 implies that everything outside is "bgColor".
65 Checks that all overlap is the same and that all non-overlap on the
66 ref is "bgColor".
67 */
68static bool compare(const SkBitmap& ref, const SkIRect& iref,
69 const SkBitmap& test, const SkIRect& itest)
70{
71 const int xOff = itest.fLeft - iref.fLeft;
72 const int yOff = itest.fTop - iref.fTop;
73
74 SkAutoLockPixels alpRef(ref);
75 SkAutoLockPixels alpTest(test);
76
77 for (int y = 0; y < test.height(); ++y) {
78 for (int x = 0; x < test.width(); ++x) {
79 SkColor testColor = test.getColor(x, y);
80 int refX = x + xOff;
81 int refY = y + yOff;
82 SkColor refColor;
83 if (refX >= 0 && refX < ref.width() &&
84 refY >= 0 && refY < ref.height())
85 {
86 refColor = ref.getColor(refX, refY);
87 } else {
88 refColor = bgColor;
89 }
90 if (refColor != testColor) {
91 return false;
92 }
93 }
94 }
95 return true;
96}
97
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +000098static void test_blur_drawing(skiatest::Reporter* reporter) {
reed@google.com2b75f422011-07-07 13:43:38 +000099
bungeman@google.com5af16f82011-09-02 15:06:44 +0000100 SkPaint paint;
101 paint.setColor(SK_ColorGRAY);
102 paint.setStyle(SkPaint::kStroke_Style);
103 paint.setStrokeWidth(SkIntToScalar(strokeWidth));
reed@google.com2b75f422011-07-07 13:43:38 +0000104
robertphillips@google.comb7061172013-09-06 14:16:12 +0000105 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5));
bungeman@google.com5af16f82011-09-02 15:06:44 +0000106 for (int style = 0; style < SkBlurMaskFilter::kBlurStyleCount; ++style) {
107 SkBlurMaskFilter::BlurStyle blurStyle =
108 static_cast<SkBlurMaskFilter::BlurStyle>(style);
reed@google.com2b75f422011-07-07 13:43:38 +0000109
bungeman@google.com5af16f82011-09-02 15:06:44 +0000110 const uint32_t flagPermutations = SkBlurMaskFilter::kAll_BlurFlag;
111 for (uint32_t flags = 0; flags < flagPermutations; ++flags) {
112 SkMaskFilter* filter;
robertphillips@google.comb7061172013-09-06 14:16:12 +0000113 filter = SkBlurMaskFilter::Create(blurStyle, sigma, flags);
reed@google.com2b75f422011-07-07 13:43:38 +0000114
bungeman@google.com5af16f82011-09-02 15:06:44 +0000115 paint.setMaskFilter(filter);
116 filter->unref();
117
tomhudson@google.com83a44462011-10-27 15:27:51 +0000118 for (size_t test = 0; test < SK_ARRAY_COUNT(tests); ++test) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000119 SkPath path;
120 tests[test].addPath(&path);
bungeman@google.comd16872c2011-09-02 15:46:17 +0000121 SkPath strokedPath;
122 paint.getFillPath(path, &strokedPath);
123 SkRect refBound = strokedPath.getBounds();
bungeman@google.com5af16f82011-09-02 15:06:44 +0000124 SkIRect iref;
125 refBound.roundOut(&iref);
bungeman@google.comd16872c2011-09-02 15:46:17 +0000126 iref.inset(-outset, -outset);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000127 SkBitmap refBitmap;
128 create(&refBitmap, iref, SkBitmap::kARGB_8888_Config);
129
130 SkCanvas refCanvas(refBitmap);
131 refCanvas.translate(SkIntToScalar(-iref.fLeft),
132 SkIntToScalar(-iref.fTop));
133 drawBG(&refCanvas);
134 refCanvas.drawPath(path, paint);
135
136 for (int view = 0; view < tests[test].viewLen; ++view) {
137 SkIRect itest = tests[test].views[view];
138 SkBitmap testBitmap;
139 create(&testBitmap, itest, SkBitmap::kARGB_8888_Config);
140
141 SkCanvas testCanvas(testBitmap);
142 testCanvas.translate(SkIntToScalar(-itest.fLeft),
143 SkIntToScalar(-itest.fTop));
144 drawBG(&testCanvas);
145 testCanvas.drawPath(path, paint);
146
147 REPORTER_ASSERT(reporter,
148 compare(refBitmap, iref, testBitmap, itest));
149 }
150 }
151 }
reed@google.com2b75f422011-07-07 13:43:38 +0000152 }
153}
154
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000155///////////////////////////////////////////////////////////////////////////////
156
157// Use SkBlurMask::BlurGroundTruth to blur a 'width' x 'height' solid
158// white rect. Return the right half of the middle row in 'result'.
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000159static void ground_truth_2d(int width, int height,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000160 SkScalar sigma,
161 int* result, int resultCount) {
162 SkMask src, dst;
163
164 src.fBounds.set(0, 0, width, height);
165 src.fFormat = SkMask::kA8_Format;
166 src.fRowBytes = src.fBounds.width();
167 src.fImage = SkMask::AllocImage(src.computeTotalImageSize());
168
169 memset(src.fImage, 0xff, src.computeTotalImageSize());
170
171 dst.fImage = NULL;
172 SkBlurMask::BlurGroundTruth(sigma, &dst, src, SkBlurMask::kNormal_Style);
173
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 }
186}
187
188// Implement a step function that is 255 between min and max; 0 elsewhere.
189static int step(int x, SkScalar min, SkScalar max) {
190 if (min < x && x < max) {
191 return 255;
192 }
193 return 0;
194}
195
196// Implement a Gaussian function with 0 mean and std.dev. of 'sigma'.
197static float gaussian(int x, SkScalar sigma) {
robertphillips@google.comb85564a2013-09-05 17:53:34 +0000198 float k = SK_Scalar1/(sigma * sqrtf(2.0f*SK_ScalarPI));
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000199 float exponent = -(x * x) / (2 * sigma * sigma);
robertphillips@google.comb85564a2013-09-05 17:53:34 +0000200 return k * expf(exponent);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000201}
202
203// Perform a brute force convolution of a step function with a Gaussian.
204// Return the right half in 'result'
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000205static void brute_force_1d(SkScalar stepMin, SkScalar stepMax,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000206 SkScalar gaussianSigma,
207 int* result, int resultCount) {
208
209 int gaussianRange = SkScalarCeilToInt(10 * gaussianSigma);
210
211 for (int i = 0; i < resultCount; ++i) {
212 SkScalar sum = 0.0f;
213 for (int j = -gaussianRange; j < gaussianRange; ++j) {
214 sum += gaussian(j, gaussianSigma) * step(i-j, stepMin, stepMax);
215 }
216
217 result[i] = SkClampMax(SkClampPos(int(sum + 0.5f)), 255);
218 }
219}
220
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000221static void blur_path(SkCanvas* canvas, const SkPath& path,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000222 SkScalar gaussianSigma) {
223
224 SkScalar midX = path.getBounds().centerX();
225 SkScalar midY = path.getBounds().centerY();
226
227 canvas->translate(-midX, -midY);
228
229 SkPaint blurPaint;
230 blurPaint.setColor(SK_ColorWHITE);
231 SkMaskFilter* filter = SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle,
232 gaussianSigma,
233 SkBlurMaskFilter::kHighQuality_BlurFlag);
234 blurPaint.setMaskFilter(filter)->unref();
235
236 canvas->drawColor(SK_ColorBLACK);
237 canvas->drawPath(path, blurPaint);
238}
239
240// Readback the blurred draw results from the canvas
241static void readback(SkCanvas* canvas, int* result, int resultCount) {
242 SkBitmap readback;
243 readback.setConfig(SkBitmap::kARGB_8888_Config, resultCount, 30);
244 readback.allocPixels();
245
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;
264 bitmap.setConfig(SkBitmap::kARGB_8888_Config, resultCount, 30);
265 bitmap.allocPixels();
266 SkCanvas canvas(bitmap);
267
268 blur_path(&canvas, path, gaussianSigma);
269 readback(&canvas, result, resultCount);
270}
271
272#if SK_SUPPORT_GPU
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000273static void gpu_blur_path(GrContextFactory* factory, const SkPath& path,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000274 SkScalar gaussianSigma,
275 int* result, int resultCount) {
276
277 GrContext* grContext = factory->get(GrContextFactory::kNative_GLContextType);
278 if (NULL == grContext) {
279 return;
280 }
281
282 GrTextureDesc desc;
283 desc.fConfig = kSkia8888_GrPixelConfig;
284 desc.fFlags = kRenderTarget_GrTextureFlagBit;
285 desc.fWidth = resultCount;
286 desc.fHeight = 30;
287 desc.fSampleCnt = 0;
288
289 SkAutoTUnref<GrTexture> texture(grContext->createUncachedTexture(desc, NULL, 0));
290 SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (grContext, texture.get())));
291 SkCanvas canvas(device.get());
292
293 blur_path(&canvas, path, gaussianSigma);
294 readback(&canvas, result, resultCount);
295}
296#endif
297
298#if WRITE_CSV
299static void write_as_csv(const char* label, SkScalar scale, int* data, int count) {
300 SkDebugf("%s_%.2f,", label, scale);
301 for (int i = 0; i < count-1; ++i) {
302 SkDebugf("%d,", data[i]);
303 }
304 SkDebugf("%d\n", data[count-1]);
305}
306#endif
307
308static bool match(int* first, int* second, int count, int tol) {
309 int delta;
310 for (int i = 0; i < count; ++i) {
311 delta = first[i] - second[i];
312 if (delta > tol || delta < -tol) {
313 return false;
314 }
315 }
316
317 return true;
318}
319
320// Test out the normal blur style with a wide range of sigmas
321static void test_sigma_range(skiatest::Reporter* reporter, GrContextFactory* factory) {
322
323 static const int kSize = 100;
324
325 // The geometry is offset a smidge to trigger:
326 // https://code.google.com/p/chromium/issues/detail?id=282418
327 SkPath rectPath;
328 rectPath.addRect(0.3f, 0.3f, 100.3f, 100.3f);
329
330 SkPoint polyPts[] = {
331 { 0.3f, 0.3f },
332 { 100.3f, 0.3f },
333 { 100.3f, 100.3f },
334 { 0.3f, 100.3f },
335 { 2.3f, 50.3f } // a little divet to throw off the rect special case
336 };
337 SkPath polyPath;
338 polyPath.addPoly(polyPts, SK_ARRAY_COUNT(polyPts), true);
339
340 int rectSpecialCaseResult[kSize];
341 int generalCaseResult[kSize];
342#if SK_SUPPORT_GPU
343 int gpuResult[kSize];
344#endif
345 int groundTruthResult[kSize];
346 int bruteForce1DResult[kSize];
347
348 SkScalar sigma = SkFloatToScalar(10.0f);
349
350 for (int i = 0; i < 4; ++i, sigma /= 10) {
351
352 cpu_blur_path(rectPath, sigma, rectSpecialCaseResult, kSize);
353 cpu_blur_path(polyPath, sigma, generalCaseResult, kSize);
354#if SK_SUPPORT_GPU
355 gpu_blur_path(factory, rectPath, sigma, gpuResult, kSize);
356#endif
357 ground_truth_2d(100, 100, sigma, groundTruthResult, kSize);
358 brute_force_1d(-50.0f, 50.0f, sigma, bruteForce1DResult, kSize);
359
360 REPORTER_ASSERT(reporter, match(rectSpecialCaseResult, bruteForce1DResult, kSize, 5));
361 REPORTER_ASSERT(reporter, match(generalCaseResult, bruteForce1DResult, kSize, 15));
362#if SK_SUPPORT_GPU
robertphillips@google.comf4f3faa2013-09-05 18:55:42 +0000363 // 1 works everywhere but: Ubuntu13 & Nexus4
364 REPORTER_ASSERT(reporter, match(gpuResult, bruteForce1DResult, kSize, 10));
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000365#endif
366 REPORTER_ASSERT(reporter, match(groundTruthResult, bruteForce1DResult, kSize, 1));
367
368#if WRITE_CSV
369 write_as_csv("RectSpecialCase", sigma, rectSpecialCaseResult, kSize);
370 write_as_csv("GeneralCase", sigma, generalCaseResult, kSize);
371#if SK_SUPPORT_GPU
372 write_as_csv("GPU", sigma, gpuResult, kSize);
373#endif
374 write_as_csv("GroundTruth2D", sigma, groundTruthResult, kSize);
375 write_as_csv("BruteForce1D", sigma, bruteForce1DResult, kSize);
376#endif
377 }
378}
379
380///////////////////////////////////////////////////////////////////////////////
381
382static void test_blur(skiatest::Reporter* reporter, GrContextFactory* factory) {
383 test_blur_drawing(reporter);
384 test_sigma_range(reporter, factory);
385}
386
reed@google.com2b75f422011-07-07 13:43:38 +0000387#include "TestClassDef.h"
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000388DEFINE_GPUTESTCLASS("BlurMaskFilter", BlurTestClass, test_blur)