blob: cfcc10362b1229b9a428f8b79bd18c7b139e94a1 [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 */
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +00008#include "SkBlurMask.h"
reed@google.com2b75f422011-07-07 13:43:38 +00009#include "SkBlurMaskFilter.h"
bungeman@google.com5af16f82011-09-02 15:06:44 +000010#include "SkCanvas.h"
tomhudson@google.com889bd8b2011-09-27 17:38:17 +000011#include "SkMath.h"
bungeman@google.com5af16f82011-09-02 15:06:44 +000012#include "SkPaint.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000013#include "Test.h"
14#include "TestClassDef.h"
15
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +000016#if SK_SUPPORT_GPU
17#include "GrContextFactory.h"
18#include "SkGpuDevice.h"
19#endif
20
21#define WRITE_CSV 0
reed@google.com2b75f422011-07-07 13:43:38 +000022
23///////////////////////////////////////////////////////////////////////////////
24
25#define ILLEGAL_MODE ((SkXfermode::Mode)-1)
26
bungeman@google.com5af16f82011-09-02 15:06:44 +000027static const int outset = 100;
28static const SkColor bgColor = SK_ColorWHITE;
29static const int strokeWidth = 4;
30
31static void create(SkBitmap* bm, SkIRect bound, SkBitmap::Config config) {
32 bm->setConfig(config, bound.width(), bound.height());
33 bm->allocPixels();
34}
35
36static void drawBG(SkCanvas* canvas) {
37 canvas->drawColor(bgColor);
38}
39
40
41struct BlurTest {
42 void (*addPath)(SkPath*);
43 int viewLen;
44 SkIRect views[9];
45};
46
47//Path Draw Procs
48//Beware that paths themselves my draw differently depending on the clip.
49static void draw50x50Rect(SkPath* path) {
50 path->addRect(0, 0, SkIntToScalar(50), SkIntToScalar(50));
51}
52
53//Tests
54static BlurTest tests[] = {
55 { draw50x50Rect, 3, {
56 //inner half of blur
57 { 0, 0, 50, 50 },
58 //blur, but no path.
59 { 50 + strokeWidth/2, 50 + strokeWidth/2, 100, 100 },
60 //just an edge
61 { 40, strokeWidth, 60, 50 - strokeWidth },
62 }},
63};
64
65/** Assumes that the ref draw was completely inside ref canvas --
66 implies that everything outside is "bgColor".
67 Checks that all overlap is the same and that all non-overlap on the
68 ref is "bgColor".
69 */
70static bool compare(const SkBitmap& ref, const SkIRect& iref,
71 const SkBitmap& test, const SkIRect& itest)
72{
73 const int xOff = itest.fLeft - iref.fLeft;
74 const int yOff = itest.fTop - iref.fTop;
75
76 SkAutoLockPixels alpRef(ref);
77 SkAutoLockPixels alpTest(test);
78
79 for (int y = 0; y < test.height(); ++y) {
80 for (int x = 0; x < test.width(); ++x) {
81 SkColor testColor = test.getColor(x, y);
82 int refX = x + xOff;
83 int refY = y + yOff;
84 SkColor refColor;
85 if (refX >= 0 && refX < ref.width() &&
86 refY >= 0 && refY < ref.height())
87 {
88 refColor = ref.getColor(refX, refY);
89 } else {
90 refColor = bgColor;
91 }
92 if (refColor != testColor) {
93 return false;
94 }
95 }
96 }
97 return true;
98}
99
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000100static void test_blur_drawing(skiatest::Reporter* reporter) {
reed@google.com2b75f422011-07-07 13:43:38 +0000101
bungeman@google.com5af16f82011-09-02 15:06:44 +0000102 SkPaint paint;
103 paint.setColor(SK_ColorGRAY);
104 paint.setStyle(SkPaint::kStroke_Style);
105 paint.setStrokeWidth(SkIntToScalar(strokeWidth));
reed@google.com2b75f422011-07-07 13:43:38 +0000106
robertphillips@google.comb7061172013-09-06 14:16:12 +0000107 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5));
bungeman@google.com5af16f82011-09-02 15:06:44 +0000108 for (int style = 0; style < SkBlurMaskFilter::kBlurStyleCount; ++style) {
109 SkBlurMaskFilter::BlurStyle blurStyle =
110 static_cast<SkBlurMaskFilter::BlurStyle>(style);
reed@google.com2b75f422011-07-07 13:43:38 +0000111
bungeman@google.com5af16f82011-09-02 15:06:44 +0000112 const uint32_t flagPermutations = SkBlurMaskFilter::kAll_BlurFlag;
113 for (uint32_t flags = 0; flags < flagPermutations; ++flags) {
114 SkMaskFilter* filter;
robertphillips@google.comb7061172013-09-06 14:16:12 +0000115 filter = SkBlurMaskFilter::Create(blurStyle, sigma, flags);
reed@google.com2b75f422011-07-07 13:43:38 +0000116
bungeman@google.com5af16f82011-09-02 15:06:44 +0000117 paint.setMaskFilter(filter);
118 filter->unref();
119
tomhudson@google.com83a44462011-10-27 15:27:51 +0000120 for (size_t test = 0; test < SK_ARRAY_COUNT(tests); ++test) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000121 SkPath path;
122 tests[test].addPath(&path);
bungeman@google.comd16872c2011-09-02 15:46:17 +0000123 SkPath strokedPath;
124 paint.getFillPath(path, &strokedPath);
125 SkRect refBound = strokedPath.getBounds();
bungeman@google.com5af16f82011-09-02 15:06:44 +0000126 SkIRect iref;
127 refBound.roundOut(&iref);
bungeman@google.comd16872c2011-09-02 15:46:17 +0000128 iref.inset(-outset, -outset);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000129 SkBitmap refBitmap;
130 create(&refBitmap, iref, SkBitmap::kARGB_8888_Config);
131
132 SkCanvas refCanvas(refBitmap);
133 refCanvas.translate(SkIntToScalar(-iref.fLeft),
134 SkIntToScalar(-iref.fTop));
135 drawBG(&refCanvas);
136 refCanvas.drawPath(path, paint);
137
138 for (int view = 0; view < tests[test].viewLen; ++view) {
139 SkIRect itest = tests[test].views[view];
140 SkBitmap testBitmap;
141 create(&testBitmap, itest, SkBitmap::kARGB_8888_Config);
142
143 SkCanvas testCanvas(testBitmap);
144 testCanvas.translate(SkIntToScalar(-itest.fLeft),
145 SkIntToScalar(-itest.fTop));
146 drawBG(&testCanvas);
147 testCanvas.drawPath(path, paint);
148
149 REPORTER_ASSERT(reporter,
150 compare(refBitmap, iref, testBitmap, itest));
151 }
152 }
153 }
reed@google.com2b75f422011-07-07 13:43:38 +0000154 }
155}
156
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000157///////////////////////////////////////////////////////////////////////////////
158
159// Use SkBlurMask::BlurGroundTruth to blur a 'width' x 'height' solid
160// white rect. Return the right half of the middle row in 'result'.
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000161static void ground_truth_2d(int width, int height,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000162 SkScalar sigma,
163 int* result, int resultCount) {
164 SkMask src, dst;
165
166 src.fBounds.set(0, 0, width, height);
167 src.fFormat = SkMask::kA8_Format;
168 src.fRowBytes = src.fBounds.width();
169 src.fImage = SkMask::AllocImage(src.computeTotalImageSize());
170
171 memset(src.fImage, 0xff, src.computeTotalImageSize());
172
173 dst.fImage = NULL;
174 SkBlurMask::BlurGroundTruth(sigma, &dst, src, SkBlurMask::kNormal_Style);
175
176 int midX = dst.fBounds.centerX();
177 int midY = dst.fBounds.centerY();
178 uint8_t* bytes = dst.getAddr8(midX, midY);
179 int i;
180 for (i = 0; i < dst.fBounds.width()-(midX-dst.fBounds.fLeft); ++i) {
181 if (i < resultCount) {
182 result[i] = bytes[i];
183 }
184 }
185 for ( ; i < resultCount; ++i) {
186 result[i] = 0;
187 }
robertphillips@google.com6d837aa2013-10-11 14:38:46 +0000188
189 SkMask::FreeImage(src.fImage);
190 SkMask::FreeImage(dst.fImage);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000191}
192
193// Implement a step function that is 255 between min and max; 0 elsewhere.
194static int step(int x, SkScalar min, SkScalar max) {
195 if (min < x && x < max) {
196 return 255;
197 }
198 return 0;
199}
200
201// Implement a Gaussian function with 0 mean and std.dev. of 'sigma'.
202static float gaussian(int x, SkScalar sigma) {
robertphillips@google.comb85564a2013-09-05 17:53:34 +0000203 float k = SK_Scalar1/(sigma * sqrtf(2.0f*SK_ScalarPI));
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000204 float exponent = -(x * x) / (2 * sigma * sigma);
robertphillips@google.comb85564a2013-09-05 17:53:34 +0000205 return k * expf(exponent);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000206}
207
208// Perform a brute force convolution of a step function with a Gaussian.
209// Return the right half in 'result'
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000210static void brute_force_1d(SkScalar stepMin, SkScalar stepMax,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000211 SkScalar gaussianSigma,
212 int* result, int resultCount) {
213
214 int gaussianRange = SkScalarCeilToInt(10 * gaussianSigma);
215
216 for (int i = 0; i < resultCount; ++i) {
217 SkScalar sum = 0.0f;
218 for (int j = -gaussianRange; j < gaussianRange; ++j) {
219 sum += gaussian(j, gaussianSigma) * step(i-j, stepMin, stepMax);
220 }
221
222 result[i] = SkClampMax(SkClampPos(int(sum + 0.5f)), 255);
223 }
224}
225
skia.committer@gmail.com6fc1b492013-09-06 07:01:45 +0000226static void blur_path(SkCanvas* canvas, const SkPath& path,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000227 SkScalar gaussianSigma) {
228
229 SkScalar midX = path.getBounds().centerX();
230 SkScalar midY = path.getBounds().centerY();
231
232 canvas->translate(-midX, -midY);
233
234 SkPaint blurPaint;
235 blurPaint.setColor(SK_ColorWHITE);
236 SkMaskFilter* filter = SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle,
237 gaussianSigma,
238 SkBlurMaskFilter::kHighQuality_BlurFlag);
239 blurPaint.setMaskFilter(filter)->unref();
240
241 canvas->drawColor(SK_ColorBLACK);
242 canvas->drawPath(path, blurPaint);
243}
244
245// Readback the blurred draw results from the canvas
246static void readback(SkCanvas* canvas, int* result, int resultCount) {
247 SkBitmap readback;
248 readback.setConfig(SkBitmap::kARGB_8888_Config, resultCount, 30);
249 readback.allocPixels();
250
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;
269 bitmap.setConfig(SkBitmap::kARGB_8888_Config, resultCount, 30);
270 bitmap.allocPixels();
271 SkCanvas canvas(bitmap);
272
273 blur_path(&canvas, path, gaussianSigma);
274 readback(&canvas, result, resultCount);
275}
276
277#if SK_SUPPORT_GPU
robertphillips@google.comcb3b6152013-11-14 14:47:56 +0000278static bool gpu_blur_path(GrContextFactory* factory, const SkPath& path,
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000279 SkScalar gaussianSigma,
280 int* result, int resultCount) {
281
282 GrContext* grContext = factory->get(GrContextFactory::kNative_GLContextType);
283 if (NULL == grContext) {
robertphillips@google.comcb3b6152013-11-14 14:47:56 +0000284 return false;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000285 }
286
287 GrTextureDesc desc;
288 desc.fConfig = kSkia8888_GrPixelConfig;
289 desc.fFlags = kRenderTarget_GrTextureFlagBit;
290 desc.fWidth = resultCount;
291 desc.fHeight = 30;
292 desc.fSampleCnt = 0;
293
294 SkAutoTUnref<GrTexture> texture(grContext->createUncachedTexture(desc, NULL, 0));
295 SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (grContext, texture.get())));
296 SkCanvas canvas(device.get());
297
298 blur_path(&canvas, path, gaussianSigma);
299 readback(&canvas, result, resultCount);
robertphillips@google.comcb3b6152013-11-14 14:47:56 +0000300 return true;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000301}
302#endif
303
304#if WRITE_CSV
305static void write_as_csv(const char* label, SkScalar scale, int* data, int count) {
306 SkDebugf("%s_%.2f,", label, scale);
307 for (int i = 0; i < count-1; ++i) {
308 SkDebugf("%d,", data[i]);
309 }
310 SkDebugf("%d\n", data[count-1]);
311}
312#endif
313
314static bool match(int* first, int* second, int count, int tol) {
315 int delta;
316 for (int i = 0; i < count; ++i) {
317 delta = first[i] - second[i];
318 if (delta > tol || delta < -tol) {
319 return false;
320 }
321 }
322
323 return true;
324}
325
326// Test out the normal blur style with a wide range of sigmas
327static void test_sigma_range(skiatest::Reporter* reporter, GrContextFactory* factory) {
328
329 static const int kSize = 100;
330
331 // The geometry is offset a smidge to trigger:
332 // https://code.google.com/p/chromium/issues/detail?id=282418
333 SkPath rectPath;
334 rectPath.addRect(0.3f, 0.3f, 100.3f, 100.3f);
335
336 SkPoint polyPts[] = {
337 { 0.3f, 0.3f },
338 { 100.3f, 0.3f },
339 { 100.3f, 100.3f },
340 { 0.3f, 100.3f },
341 { 2.3f, 50.3f } // a little divet to throw off the rect special case
342 };
343 SkPath polyPath;
344 polyPath.addPoly(polyPts, SK_ARRAY_COUNT(polyPts), true);
345
346 int rectSpecialCaseResult[kSize];
347 int generalCaseResult[kSize];
348#if SK_SUPPORT_GPU
349 int gpuResult[kSize];
350#endif
351 int groundTruthResult[kSize];
352 int bruteForce1DResult[kSize];
353
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000354 SkScalar sigma = 10.0f;
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000355
356 for (int i = 0; i < 4; ++i, sigma /= 10) {
357
358 cpu_blur_path(rectPath, sigma, rectSpecialCaseResult, kSize);
359 cpu_blur_path(polyPath, sigma, generalCaseResult, kSize);
360#if SK_SUPPORT_GPU
robertphillips@google.comcb3b6152013-11-14 14:47:56 +0000361 bool haveGPUResult = gpu_blur_path(factory, rectPath, sigma, gpuResult, kSize);
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000362#endif
363 ground_truth_2d(100, 100, sigma, groundTruthResult, kSize);
364 brute_force_1d(-50.0f, 50.0f, sigma, bruteForce1DResult, kSize);
365
366 REPORTER_ASSERT(reporter, match(rectSpecialCaseResult, bruteForce1DResult, kSize, 5));
367 REPORTER_ASSERT(reporter, match(generalCaseResult, bruteForce1DResult, kSize, 15));
368#if SK_SUPPORT_GPU
robertphillips@google.comcb3b6152013-11-14 14:47:56 +0000369 if (haveGPUResult) {
370 // 1 works everywhere but: Ubuntu13 & Nexus4
371 REPORTER_ASSERT(reporter, match(gpuResult, bruteForce1DResult, kSize, 10));
372 }
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000373#endif
374 REPORTER_ASSERT(reporter, match(groundTruthResult, bruteForce1DResult, kSize, 1));
375
376#if WRITE_CSV
377 write_as_csv("RectSpecialCase", sigma, rectSpecialCaseResult, kSize);
378 write_as_csv("GeneralCase", sigma, generalCaseResult, kSize);
379#if SK_SUPPORT_GPU
380 write_as_csv("GPU", sigma, gpuResult, kSize);
381#endif
382 write_as_csv("GroundTruth2D", sigma, groundTruthResult, kSize);
383 write_as_csv("BruteForce1D", sigma, bruteForce1DResult, kSize);
384#endif
385 }
386}
387
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000388DEF_GPUTEST(Blur, reporter, factory) {
robertphillips@google.com3dfa4cc2013-09-05 16:39:03 +0000389 test_blur_drawing(reporter);
390 test_sigma_range(reporter, factory);
391}