• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SampleCode.h"
9 #include "SkBitmap.h"
10 #include "SkBlurMask.h"
11 #include "SkBlurMaskFilter.h"
12 #include "SkCanvas.h"
13 #include "SkColorPriv.h"
14 #include "SkGradientShader.h"
15 #include "SkUtils.h"
16 #include "SkView.h"
17 
18 class BlurView : public SampleView {
19     SkBitmap    fBM;
20 public:
BlurView()21     BlurView() {}
22 
23 protected:
24     // overrides from SkEventSink
onQuery(SkEvent * evt)25     virtual bool onQuery(SkEvent* evt) {
26         if (SampleCode::TitleQ(*evt)) {
27             SampleCode::TitleR(evt, "Blur");
28             return true;
29         }
30         return this->INHERITED::onQuery(evt);
31     }
32 
drawBG(SkCanvas * canvas)33     void drawBG(SkCanvas* canvas) {
34         canvas->drawColor(0xFFDDDDDD);
35     }
36 
onDrawContent(SkCanvas * canvas)37     virtual void onDrawContent(SkCanvas* canvas) {
38         drawBG(canvas);
39 
40         SkBlurStyle NONE = SkBlurStyle(-999);
41         static const struct {
42             SkBlurStyle fStyle;
43             int         fCx, fCy;
44         } gRecs[] = {
45             { NONE,                                 0,  0 },
46             { kInner_SkBlurStyle,  -1,  0 },
47             { kNormal_SkBlurStyle,  0,  1 },
48             { kSolid_SkBlurStyle,   0, -1 },
49             { kOuter_SkBlurStyle,   1,  0 },
50         };
51 
52         SkPaint paint;
53         paint.setAntiAlias(true);
54         paint.setTextSize(25);
55         canvas->translate(-40, 0);
56 
57         SkBlurMaskFilter::BlurFlags flags = SkBlurMaskFilter::kNone_BlurFlag;
58         for (int j = 0; j < 2; j++) {
59             canvas->save();
60             paint.setColor(SK_ColorBLUE);
61             for (size_t i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
62                 if (gRecs[i].fStyle != NONE) {
63                     paint.setMaskFilter(SkBlurMaskFilter::Make(gRecs[i].fStyle,
64                                       SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20)),
65                                       flags));
66                 } else {
67                     paint.setMaskFilter(nullptr);
68                 }
69                 canvas->drawCircle(200 + gRecs[i].fCx*100.f,
70                                    200 + gRecs[i].fCy*100.f, 50, paint);
71             }
72             // draw text
73             {
74                 paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
75                                                            SkBlurMask::ConvertRadiusToSigma(4),
76                                                            flags));
77                 SkScalar x = SkIntToScalar(70);
78                 SkScalar y = SkIntToScalar(400);
79                 paint.setColor(SK_ColorBLACK);
80                 canvas->drawString("Hamburgefons Style", x, y, paint);
81                 canvas->drawString("Hamburgefons Style", x, y + SkIntToScalar(50), paint);
82                 paint.setMaskFilter(nullptr);
83                 paint.setColor(SK_ColorWHITE);
84                 x -= SkIntToScalar(2);
85                 y -= SkIntToScalar(2);
86                 canvas->drawString("Hamburgefons Style", x, y, paint);
87             }
88             canvas->restore();
89             flags = SkBlurMaskFilter::kHighQuality_BlurFlag;
90             canvas->translate(350, 0);
91         }
92     }
93 
94 private:
95     typedef SkView INHERITED;
96 };
97 
98 //////////////////////////////////////////////////////////////////////////////
99 
MyFactory()100 static SkView* MyFactory() { return new BlurView; }
101 static SkViewRegister reg(MyFactory);
102