blob: 61fff63c6b835231aad477ea089203ace98bac03 [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"
9#include "SkBlurMaskFilter.h"
bungeman@google.com5af16f82011-09-02 15:06:44 +000010#include "SkCanvas.h"
11#include "SkPaint.h"
reed@google.com2b75f422011-07-07 13:43:38 +000012#include "SkRandom.h"
13
14///////////////////////////////////////////////////////////////////////////////
15
16#define ILLEGAL_MODE ((SkXfermode::Mode)-1)
17
bungeman@google.com5af16f82011-09-02 15:06:44 +000018static const int outset = 100;
19static const SkColor bgColor = SK_ColorWHITE;
20static const int strokeWidth = 4;
21
22static void create(SkBitmap* bm, SkIRect bound, SkBitmap::Config config) {
23 bm->setConfig(config, bound.width(), bound.height());
24 bm->allocPixels();
25}
26
27static void drawBG(SkCanvas* canvas) {
28 canvas->drawColor(bgColor);
29}
30
31
32struct BlurTest {
33 void (*addPath)(SkPath*);
34 int viewLen;
35 SkIRect views[9];
36};
37
38//Path Draw Procs
39//Beware that paths themselves my draw differently depending on the clip.
40static void draw50x50Rect(SkPath* path) {
41 path->addRect(0, 0, SkIntToScalar(50), SkIntToScalar(50));
42}
43
44//Tests
45static BlurTest tests[] = {
46 { draw50x50Rect, 3, {
47 //inner half of blur
48 { 0, 0, 50, 50 },
49 //blur, but no path.
50 { 50 + strokeWidth/2, 50 + strokeWidth/2, 100, 100 },
51 //just an edge
52 { 40, strokeWidth, 60, 50 - strokeWidth },
53 }},
54};
55
56/** Assumes that the ref draw was completely inside ref canvas --
57 implies that everything outside is "bgColor".
58 Checks that all overlap is the same and that all non-overlap on the
59 ref is "bgColor".
60 */
61static bool compare(const SkBitmap& ref, const SkIRect& iref,
62 const SkBitmap& test, const SkIRect& itest)
63{
64 const int xOff = itest.fLeft - iref.fLeft;
65 const int yOff = itest.fTop - iref.fTop;
66
67 SkAutoLockPixels alpRef(ref);
68 SkAutoLockPixels alpTest(test);
69
70 for (int y = 0; y < test.height(); ++y) {
71 for (int x = 0; x < test.width(); ++x) {
72 SkColor testColor = test.getColor(x, y);
73 int refX = x + xOff;
74 int refY = y + yOff;
75 SkColor refColor;
76 if (refX >= 0 && refX < ref.width() &&
77 refY >= 0 && refY < ref.height())
78 {
79 refColor = ref.getColor(refX, refY);
80 } else {
81 refColor = bgColor;
82 }
83 if (refColor != testColor) {
84 return false;
85 }
86 }
87 }
88 return true;
89}
90
reed@google.com2b75f422011-07-07 13:43:38 +000091static void test_blur(skiatest::Reporter* reporter) {
reed@google.com2b75f422011-07-07 13:43:38 +000092
bungeman@google.com5af16f82011-09-02 15:06:44 +000093 SkPaint paint;
94 paint.setColor(SK_ColorGRAY);
95 paint.setStyle(SkPaint::kStroke_Style);
96 paint.setStrokeWidth(SkIntToScalar(strokeWidth));
reed@google.com2b75f422011-07-07 13:43:38 +000097
bungeman@google.com5af16f82011-09-02 15:06:44 +000098 SkScalar radius = SkIntToScalar(5);
99 for (int style = 0; style < SkBlurMaskFilter::kBlurStyleCount; ++style) {
100 SkBlurMaskFilter::BlurStyle blurStyle =
101 static_cast<SkBlurMaskFilter::BlurStyle>(style);
reed@google.com2b75f422011-07-07 13:43:38 +0000102
bungeman@google.com5af16f82011-09-02 15:06:44 +0000103 const uint32_t flagPermutations = SkBlurMaskFilter::kAll_BlurFlag;
104 for (uint32_t flags = 0; flags < flagPermutations; ++flags) {
105 SkMaskFilter* filter;
106 filter = SkBlurMaskFilter::Create(radius, blurStyle, flags);
reed@google.com2b75f422011-07-07 13:43:38 +0000107
bungeman@google.com5af16f82011-09-02 15:06:44 +0000108 SkMaskFilter::BlurInfo info;
109 sk_bzero(&info, sizeof(info));
110 SkMaskFilter::BlurType type = filter->asABlur(&info);
reed@google.com2b75f422011-07-07 13:43:38 +0000111
bungeman@google.com5af16f82011-09-02 15:06:44 +0000112 REPORTER_ASSERT(reporter, type ==
113 static_cast<SkMaskFilter::BlurType>(style + 1));
114 REPORTER_ASSERT(reporter, info.fRadius == radius);
115 REPORTER_ASSERT(reporter, info.fIgnoreTransform ==
116 SkToBool(flags & SkBlurMaskFilter::kIgnoreTransform_BlurFlag));
117 REPORTER_ASSERT(reporter, info.fHighQuality ==
118 SkToBool(flags & SkBlurMaskFilter::kHighQuality_BlurFlag));
119
120 paint.setMaskFilter(filter);
121 filter->unref();
122
123 for (int test = 0; test < SK_ARRAY_COUNT(tests); ++test) {
124 SkPath path;
125 tests[test].addPath(&path);
126 SkRect refBound = path.getBounds();
127 refBound.outset(outset, outset);
128 SkIRect iref;
129 refBound.roundOut(&iref);
130 SkBitmap refBitmap;
131 create(&refBitmap, iref, SkBitmap::kARGB_8888_Config);
132
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;
142 create(&testBitmap, itest, SkBitmap::kARGB_8888_Config);
143
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
158#include "TestClassDef.h"
159DEFINE_TESTCLASS("BlurMaskFilter", BlurTestClass, test_blur)