blob: 88863e604cb9419ed9544dfb0880f0cb09b4f4a2 [file] [log] [blame]
reed@google.comdb87c962012-11-02 21:11:12 +00001/*
2 * Copyright 2012 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 */
7
8#include "gm.h"
9#include "SkBlurMaskFilter.h"
humper@google.com7c7292c2013-01-04 20:29:03 +000010#include "SkBlurMask.h"
reed@google.comdb87c962012-11-02 21:11:12 +000011#include "SkCanvas.h"
12#include "SkPath.h"
13
14#define STROKE_WIDTH SkIntToScalar(10)
15
16typedef void (*Proc)(SkCanvas*, const SkRect&, const SkPaint&);
17
18static void fill_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
19 canvas->drawRect(r, p);
20}
21
reed@google.comdb87c962012-11-02 21:11:12 +000022static void draw_donut(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
23 SkRect rect;
24 SkPath path;
skia.committer@gmail.com34587162012-11-20 02:01:23 +000025
reed@google.comdb87c962012-11-02 21:11:12 +000026 rect = r;
27 rect.outset(STROKE_WIDTH/2, STROKE_WIDTH/2);
28 path.addRect(rect);
29 rect = r;
30 rect.inset(STROKE_WIDTH/2, STROKE_WIDTH/2);
skia.committer@gmail.com34587162012-11-20 02:01:23 +000031
reed@google.comdb87c962012-11-02 21:11:12 +000032 path.addRect(rect);
33 path.setFillType(SkPath::kEvenOdd_FillType);
skia.committer@gmail.com34587162012-11-20 02:01:23 +000034
reed@google.com808b70f2012-11-19 16:14:02 +000035 canvas->drawPath(path, p);
36}
reed@google.comdb87c962012-11-02 21:11:12 +000037
reed@google.com808b70f2012-11-19 16:14:02 +000038static void draw_donut_skewed(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
39 SkRect rect;
40 SkPath path;
skia.committer@gmail.com34587162012-11-20 02:01:23 +000041
reed@google.com808b70f2012-11-19 16:14:02 +000042 rect = r;
43 rect.outset(STROKE_WIDTH/2, STROKE_WIDTH/2);
44 path.addRect(rect);
45 rect = r;
46 rect.inset(STROKE_WIDTH/2, STROKE_WIDTH/2);
skia.committer@gmail.com34587162012-11-20 02:01:23 +000047
reed@google.com808b70f2012-11-19 16:14:02 +000048 rect.offset(7, -7);
skia.committer@gmail.com34587162012-11-20 02:01:23 +000049
reed@google.com808b70f2012-11-19 16:14:02 +000050 path.addRect(rect);
51 path.setFillType(SkPath::kEvenOdd_FillType);
skia.committer@gmail.com34587162012-11-20 02:01:23 +000052
reed@google.comdb87c962012-11-02 21:11:12 +000053 canvas->drawPath(path, p);
54}
55
reed@google.com53007a22012-11-26 14:39:50 +000056#include "SkGradientShader.h"
57
58typedef void (*PaintProc)(SkPaint*, SkScalar width);
59
reed@google.com57850b92012-12-17 21:20:53 +000060static const char* gBlurStyle2Name[] = {
61 "normal",
62 "solid",
63 "outer",
64 "inner"
65};
66
reed@google.comdb87c962012-11-02 21:11:12 +000067class BlurRectGM : public skiagm::GM {
68 SkAutoTUnref<SkMaskFilter> fMaskFilter;
reed@google.com53007a22012-11-26 14:39:50 +000069 SkString fName;
70 PaintProc fPProc;
71 SkAlpha fAlpha;
reed@google.comdb87c962012-11-02 21:11:12 +000072public:
reed@google.com57850b92012-12-17 21:20:53 +000073 BlurRectGM(const char name[], PaintProc pproc, U8CPU alpha,
74 SkBlurMaskFilter::BlurStyle bs) :
75 fMaskFilter(SkBlurMaskFilter::Create(STROKE_WIDTH/2, bs,
reed@google.comdb87c962012-11-02 21:11:12 +000076 SkBlurMaskFilter::kHighQuality_BlurFlag))
reed@google.com53007a22012-11-26 14:39:50 +000077 , fName(name)
78 , fPProc(pproc)
79 , fAlpha(SkToU8(alpha))
reed@google.com57850b92012-12-17 21:20:53 +000080 {
81 fName.appendf("_%s", gBlurStyle2Name[bs]);
82 }
reed@google.comdb87c962012-11-02 21:11:12 +000083
84protected:
85 virtual SkString onShortName() {
reed@google.com53007a22012-11-26 14:39:50 +000086 return fName;
reed@google.comdb87c962012-11-02 21:11:12 +000087 }
88
89 virtual SkISize onISize() {
90 return SkISize::Make(640, 480);
91 }
92
93 virtual void onDraw(SkCanvas* canvas) {
94 canvas->translate(STROKE_WIDTH*3/2, STROKE_WIDTH*3/2);
95
reed@google.com53007a22012-11-26 14:39:50 +000096 SkRect r = { 0, 0, 250, 120 };
skia.committer@gmail.com8ccf5902012-11-27 02:01:19 +000097
reed@google.comdb87c962012-11-02 21:11:12 +000098 SkPaint paint;
99 paint.setMaskFilter(fMaskFilter);
reed@google.com53007a22012-11-26 14:39:50 +0000100 if (fPProc) {
101 fPProc(&paint, r.width());
102 }
103 paint.setAlpha(fAlpha);
reed@google.comdb87c962012-11-02 21:11:12 +0000104
105 static const Proc procs[] = {
reed@google.com808b70f2012-11-19 16:14:02 +0000106 fill_rect, draw_donut, draw_donut_skewed
reed@google.comdb87c962012-11-02 21:11:12 +0000107 };
108
reed@google.comdb87c962012-11-02 21:11:12 +0000109 this->drawProcs(canvas, r, paint, false, procs, SK_ARRAY_COUNT(procs));
110 canvas->translate(r.width() * 4/3, 0);
111 this->drawProcs(canvas, r, paint, true, procs, SK_ARRAY_COUNT(procs));
112 }
113
114 virtual uint32_t onGetFlags() const { return kSkipPipe_Flag; }
115
116private:
117 void drawProcs(SkCanvas* canvas, const SkRect& r, const SkPaint& paint,
118 bool doClip, const Proc procs[], size_t procsCount) {
119 SkAutoCanvasRestore acr(canvas, true);
120 for (size_t i = 0; i < procsCount; ++i) {
121 if (doClip) {
122 SkRect clipRect(r);
123 clipRect.inset(STROKE_WIDTH/2, STROKE_WIDTH/2);
124 canvas->save();
125 canvas->clipRect(r);
126 }
127 procs[i](canvas, r, paint);
128 if (doClip) {
129 canvas->restore();
130 }
131 canvas->translate(0, r.height() * 4/3);
132 }
133 }
134
135 typedef GM INHERITED;
136};
137
humper@google.com7c7292c2013-01-04 20:29:03 +0000138class BlurRectCompareGM : public skiagm::GM {
139 SkString fName;
140 unsigned int fRectWidth, fRectHeight;
reed@google.com140d7282013-01-07 20:25:04 +0000141 SkScalar fRadius;
humper@google.com7c7292c2013-01-04 20:29:03 +0000142public:
143 BlurRectCompareGM(const char name[], unsigned int rectWidth, unsigned int rectHeight, float radius) :
144 fName(name)
145 , fRectWidth( rectWidth )
146 , fRectHeight( rectHeight )
147 , fRadius( radius )
148 {}
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000149
humper@google.com7c7292c2013-01-04 20:29:03 +0000150 int width() const { return fRectWidth; }
151 int height() const { return fRectHeight; }
reed@google.com140d7282013-01-07 20:25:04 +0000152 SkScalar radius() const { return fRadius; }
humper@google.com7c7292c2013-01-04 20:29:03 +0000153
154protected:
155 virtual SkString onShortName() {
156 return fName;
157 }
158
159 virtual SkISize onISize() {
160 return SkISize::Make(640, 480);
161 }
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000162
reed@google.com140d7282013-01-07 20:25:04 +0000163 virtual void makeMask( SkMask *m, const SkRect& ) = 0;
humper@google.com7c7292c2013-01-04 20:29:03 +0000164
165 virtual void onDraw(SkCanvas* canvas) {
166 SkRect r;
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000167 r.setWH(SkIntToScalar(fRectWidth), SkIntToScalar(fRectHeight));
humper@google.com7c7292c2013-01-04 20:29:03 +0000168
169 SkMask mask;
170
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000171 makeMask(&mask, r);
humper@google.com7c7292c2013-01-04 20:29:03 +0000172
173 SkBitmap bm;
174 bm.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(), mask.fBounds.height());
175 bm.setPixels(mask.fImage);
176 canvas->drawBitmap(bm, 50, 50, NULL);
177 }
178
179 virtual uint32_t onGetFlags() const { return kSkipPipe_Flag; }
180
181private:
182 typedef GM INHERITED;
183};
184
185class BlurRectFastGM: public BlurRectCompareGM {
186public:
reed@google.com140d7282013-01-07 20:25:04 +0000187 BlurRectFastGM(const char name[], unsigned int rect_width,
188 unsigned int rect_height, float blur_radius) :
189 BlurRectCompareGM( name, rect_width, rect_height, blur_radius ) {}
humper@google.com7c7292c2013-01-04 20:29:03 +0000190protected:
reed@google.com140d7282013-01-07 20:25:04 +0000191 virtual void makeMask( SkMask *m, const SkRect& r) SK_OVERRIDE {
192 SkBlurMask::BlurRect( m, r, radius(), SkBlurMask::kNormal_Style,
193 SkBlurMask::kHigh_Quality );
194 }
humper@google.com7c7292c2013-01-04 20:29:03 +0000195};
196
197class BlurRectSlowGM: public BlurRectCompareGM {
198public:
199 BlurRectSlowGM(const char name[], unsigned int rect_width, unsigned int rect_height, float blur_radius) :
200 BlurRectCompareGM( name, rect_width, rect_height, blur_radius ) {}
201protected:
reed@google.com140d7282013-01-07 20:25:04 +0000202 virtual void makeMask( SkMask *m, const SkRect& r) SK_OVERRIDE {
203 SkMask src;
204 r.roundOut(&src.fBounds);
205 src.fBounds.offset(-src.fBounds.fLeft, -src.fBounds.fTop); // move to origin
206 src.fFormat = SkMask::kA8_Format;
207 src.fRowBytes = src.fBounds.width();
208 src.fImage = SkMask::AllocImage( src.computeTotalImageSize() );
humper@google.com7c7292c2013-01-04 20:29:03 +0000209
reed@google.com140d7282013-01-07 20:25:04 +0000210 memset( src.fImage, 0xff, src.computeTotalImageSize() );
humper@google.com7c7292c2013-01-04 20:29:03 +0000211
reed@google.com140d7282013-01-07 20:25:04 +0000212 SkBlurMask::BlurSeparable( m, src, radius()/2, SkBlurMask::kNormal_Style, SkBlurMask::kHigh_Quality );
213 }
humper@google.com7c7292c2013-01-04 20:29:03 +0000214};
215
216
reed@google.comdb87c962012-11-02 21:11:12 +0000217//////////////////////////////////////////////////////////////////////////////
218
reed@google.com57850b92012-12-17 21:20:53 +0000219DEF_GM(return new BlurRectGM("blurrect", NULL, 0xFF, SkBlurMaskFilter::kNormal_BlurStyle);)
220DEF_GM(return new BlurRectGM("blurrect", NULL, 0xFF, SkBlurMaskFilter::kSolid_BlurStyle);)
221DEF_GM(return new BlurRectGM("blurrect", NULL, 0xFF, SkBlurMaskFilter::kOuter_BlurStyle);)
222DEF_GM(return new BlurRectGM("blurrect", NULL, 0xFF, SkBlurMaskFilter::kInner_BlurStyle);)
223
reed@google.com697c1e12013-01-04 23:02:50 +0000224#if 0 // disable while we debug an rtree failure
humper@google.com7c7292c2013-01-04 20:29:03 +0000225DEF_GM(return new BlurRectFastGM("blurrect_fast_100_100_10", 100, 100, 10);)
226DEF_GM(return new BlurRectFastGM("blurrect_fast_100_100_2", 100, 100, 2);)
227DEF_GM(return new BlurRectFastGM("blurrect_fast_10_10_100", 10, 10, 100);)
228DEF_GM(return new BlurRectFastGM("blurrect_fast_10_100_10", 10, 100, 10);)
reed@google.com697c1e12013-01-04 23:02:50 +0000229#endif
reed@google.comdb87c962012-11-02 21:11:12 +0000230
humper@google.com7c7292c2013-01-04 20:29:03 +0000231DEF_GM(return new BlurRectSlowGM("blurrect_slow_100_100_10", 100, 100, 10);)
232DEF_GM(return new BlurRectSlowGM("blurrect_slow_100_100_2", 100, 100, 2);)
233DEF_GM(return new BlurRectSlowGM("blurrect_slow_10_10_100", 10, 10, 100);)
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000234DEF_GM(return new BlurRectSlowGM("blurrect_slow_10_100_10", 10, 100, 10);)