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