• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
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  */
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 
make_bitmap()17 static SkBitmap make_bitmap() {
18     SkPMColor c[256];
19     for (int i = 0; i < 256; i++) {
20         c[i] = SkPackARGB32(255 - i, 0, 0, 0);
21     }
22 
23     SkBitmap bm;
24     SkColorTable* ctable = new SkColorTable(c, 256);
25 
26     bm.allocPixels(SkImageInfo::Make(256, 256, kIndex_8_SkColorType,
27                                      kPremul_SkAlphaType),
28                    nullptr, ctable);
29     ctable->unref();
30 
31     bm.lockPixels();
32     const float cx = bm.width() * 0.5f;
33     const float cy = bm.height() * 0.5f;
34     for (int y = 0; y < bm.height(); y++) {
35         float dy = y - cy;
36         dy *= dy;
37         uint8_t* p = bm.getAddr8(0, y);
38         for (int x = 0; x < 256; x++) {
39             float dx = x - cx;
40             dx *= dx;
41             float d = (dx + dy) / (cx/2);
42             int id = (int)d;
43             if (id > 255) {
44                 id = 255;
45             }
46             p[x] = id;
47         }
48     }
49     bm.unlockPixels();
50     return bm;
51 }
52 
53 class BlurView : public SampleView {
54     SkBitmap    fBM;
55 public:
BlurView()56     BlurView() {
57         if (false) { // avoid bit rot, suppress warning
58             fBM = make_bitmap();
59         }
60     }
61 
62 protected:
63     // overrides from SkEventSink
onQuery(SkEvent * evt)64     virtual bool onQuery(SkEvent* evt) {
65         if (SampleCode::TitleQ(*evt)) {
66             SampleCode::TitleR(evt, "Blur");
67             return true;
68         }
69         return this->INHERITED::onQuery(evt);
70     }
71 
drawBG(SkCanvas * canvas)72     void drawBG(SkCanvas* canvas) {
73         canvas->drawColor(0xFFDDDDDD);
74     }
75 
onDrawContent(SkCanvas * canvas)76     virtual void onDrawContent(SkCanvas* canvas) {
77         drawBG(canvas);
78 
79         SkBlurStyle NONE = SkBlurStyle(-999);
80         static const struct {
81             SkBlurStyle fStyle;
82             int         fCx, fCy;
83         } gRecs[] = {
84             { NONE,                                 0,  0 },
85             { kInner_SkBlurStyle,  -1,  0 },
86             { kNormal_SkBlurStyle,  0,  1 },
87             { kSolid_SkBlurStyle,   0, -1 },
88             { kOuter_SkBlurStyle,   1,  0 },
89         };
90 
91         SkPaint paint;
92         paint.setAntiAlias(true);
93         paint.setTextSize(25);
94         canvas->translate(-40, 0);
95 
96         SkBlurMaskFilter::BlurFlags flags = SkBlurMaskFilter::kNone_BlurFlag;
97         for (int j = 0; j < 2; j++) {
98             canvas->save();
99             paint.setColor(SK_ColorBLUE);
100             for (size_t i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
101                 if (gRecs[i].fStyle != NONE) {
102                     SkMaskFilter* mf = SkBlurMaskFilter::Create(gRecs[i].fStyle,
103                                       SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20)),
104                                       flags);
105                     paint.setMaskFilter(mf)->unref();
106                 } else {
107                     paint.setMaskFilter(nullptr);
108                 }
109                 canvas->drawCircle(200 + gRecs[i].fCx*100.f,
110                                    200 + gRecs[i].fCy*100.f, 50, paint);
111             }
112             // draw text
113             {
114                 SkMaskFilter* mf = SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
115                                       SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4)),
116                                       flags);
117                 paint.setMaskFilter(mf)->unref();
118                 SkScalar x = SkIntToScalar(70);
119                 SkScalar y = SkIntToScalar(400);
120                 paint.setColor(SK_ColorBLACK);
121                 canvas->drawText("Hamburgefons Style", 18, x, y, paint);
122                 canvas->drawText("Hamburgefons Style", 18, x, y + SkIntToScalar(50), paint);
123                 paint.setMaskFilter(nullptr);
124                 paint.setColor(SK_ColorWHITE);
125                 x -= SkIntToScalar(2);
126                 y -= SkIntToScalar(2);
127                 canvas->drawText("Hamburgefons Style", 18, x, y, paint);
128             }
129             canvas->restore();
130             flags = SkBlurMaskFilter::kHighQuality_BlurFlag;
131             canvas->translate(350, 0);
132         }
133     }
134 
135 private:
136     typedef SkView INHERITED;
137 };
138 
139 //////////////////////////////////////////////////////////////////////////////
140 
MyFactory()141 static SkView* MyFactory() { return new BlurView; }
142 static SkViewRegister reg(MyFactory);
143