96a9e304019d837fc293346dd81b78df76bb301c
[platform/upstream/libSkiaSharp.git] / gm / blurs.cpp
1 /*
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  */
7
8 #include "gm.h"
9 #include "SkBlurMask.h"
10 #include "SkBlurMaskFilter.h"
11
12 class BlursGM : public skiagm::GM {
13 public:
14     BlursGM() {
15         this->setBGColor(0xFFDDDDDD);
16     }
17
18 protected:
19
20     SkString onShortName() SK_OVERRIDE {
21         return SkString("blurs");
22     }
23
24     SkISize onISize() SK_OVERRIDE {
25         return SkISize::Make(700, 500);
26     }
27
28     void onDraw(SkCanvas* canvas) SK_OVERRIDE {
29         SkBlurStyle NONE = SkBlurStyle(-999);
30         static const struct {
31             SkBlurStyle fStyle;
32             int         fCx, fCy;
33         } gRecs[] = {
34             { NONE,                 0,  0 },
35             { kInner_SkBlurStyle,  -1,  0 },
36             { kNormal_SkBlurStyle,  0,  1 },
37             { kSolid_SkBlurStyle,   0, -1 },
38             { kOuter_SkBlurStyle,   1,  0 },
39         };
40
41         SkPaint paint;
42         paint.setAntiAlias(true);
43         sk_tool_utils::set_portable_typeface(&paint);
44         paint.setTextSize(SkIntToScalar(25));
45         canvas->translate(SkIntToScalar(-40), SkIntToScalar(0));
46
47         SkBlurMaskFilter::BlurFlags flags = SkBlurMaskFilter::kNone_BlurFlag;
48         for (int j = 0; j < 2; j++) {
49             canvas->save();
50             paint.setColor(SK_ColorBLUE);
51             for (size_t i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
52                 if (gRecs[i].fStyle != NONE) {
53                     SkMaskFilter* mf = SkBlurMaskFilter::Create(gRecs[i].fStyle,
54                                            SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20)),
55                                            flags);
56                     paint.setMaskFilter(mf)->unref();
57                 } else {
58                     paint.setMaskFilter(NULL);
59                 }
60                 canvas->drawCircle(SkIntToScalar(200 + gRecs[i].fCx*100),
61                                    SkIntToScalar(200 + gRecs[i].fCy*100),
62                                    SkIntToScalar(50),
63                                    paint);
64             }
65             // draw text
66             {
67                 SkMaskFilter* mf = SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
68                                            SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4)),
69                                            flags);
70                 paint.setMaskFilter(mf)->unref();
71                 SkScalar x = SkIntToScalar(70);
72                 SkScalar y = SkIntToScalar(400);
73                 paint.setColor(SK_ColorBLACK);
74                 canvas->drawText("Hamburgefons Style", 18, x, y, paint);
75                 canvas->drawText("Hamburgefons Style", 18,
76                                  x, y + SkIntToScalar(50), paint);
77                 paint.setMaskFilter(NULL);
78                 paint.setColor(SK_ColorWHITE);
79                 x -= SkIntToScalar(2);
80                 y -= SkIntToScalar(2);
81                 canvas->drawText("Hamburgefons Style", 18, x, y, paint);
82             }
83             canvas->restore();
84             flags = SkBlurMaskFilter::kHighQuality_BlurFlag;
85             canvas->translate(SkIntToScalar(350), SkIntToScalar(0));
86         }
87     }
88
89 private:
90     typedef skiagm::GM INHERITED;
91 };
92 DEF_GM( return new BlursGM; )
93
94 //////////////////////////////////////////////////////////////////////////////////////////////
95
96 // exercise a special-case of blurs, which is two nested rects. These are drawn specially,
97 // and possibly cached.
98 //
99 // in particular, we want to notice that the 2nd rect draws slightly differently, since it
100 // is translated a fractional amount.
101 //
102 class Blur2RectsGM : public skiagm::GM {
103 public:
104     SkString onShortName() SK_OVERRIDE {
105         return SkString("blur2rects");
106     }
107
108     SkISize onISize() SK_OVERRIDE {
109         return SkISize::Make(700, 500);
110     }
111
112     void onDraw(SkCanvas* canvas) SK_OVERRIDE {
113         SkPaint paint;
114
115         paint.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
116                                                      2.3f))->unref();
117
118         SkRect outer = SkRect::MakeXYWH(10.125f, 10.125f, 100.125f, 100);
119         SkRect inner = SkRect::MakeXYWH(20.25f, 20.125f, 80, 80);
120         SkPath path;
121         path.addRect(outer, SkPath::kCW_Direction);
122         path.addRect(inner, SkPath::kCCW_Direction);
123
124         canvas->drawPath(path, paint);
125         // important to translate by a factional amount to exercise a different "phase"
126         // of the same path w.r.t. the pixel grid
127         SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 14 + 0.25f;
128         canvas->translate(dx, 0);
129         canvas->drawPath(path, paint);
130     }
131 };
132 DEF_GM( return new Blur2RectsGM; )
133
134 class Blur2RectsNonNinePatchGM : public skiagm::GM {
135 public:
136     SkString onShortName() SK_OVERRIDE {
137         return SkString("blur2rectsnonninepatch");
138     }
139
140     SkISize onISize() SK_OVERRIDE {
141         return SkISize::Make(700, 500);
142     }
143
144     void onDraw(SkCanvas* canvas) SK_OVERRIDE {
145         SkPaint paint;
146         paint.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
147                                                      4.3f))->unref();
148
149         SkRect outer = SkRect::MakeXYWH(10, 110, 100, 100);
150         SkRect inner = SkRect::MakeXYWH(50, 150, 10, 10);
151         SkPath path;
152         path.addRect(outer, SkPath::kCW_Direction);
153         path.addRect(inner, SkPath::kCW_Direction);
154         canvas->drawPath(path, paint);
155
156         SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 40 + 0.25f;
157         canvas->translate(dx, 0);
158         canvas->drawPath(path, paint);
159
160         // Translate to outside of clip bounds.
161         canvas->translate(-dx, 0);
162         canvas->translate(-30, -150);
163         canvas->drawPath(path, paint);
164     }
165 };
166 DEF_GM( return new Blur2RectsNonNinePatchGM; )