• 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 "Sample.h"
9 #include "SkCanvas.h"
10 #include "SkBlurMaskFilter.h"
11 #include "SkCamera.h"
12 #include "SkColorFilter.h"
13 #include "SkColorPriv.h"
14 #include "SkGradientShader.h"
15 #include "SkImage.h"
16 #include "SkInterpolator.h"
17 #include "SkMaskFilter.h"
18 #include "SkPath.h"
19 #include "SkRegion.h"
20 #include "SkShader.h"
21 #include "SkTime.h"
22 #include "SkTypeface.h"
23 #include "SkUTF.h"
24 #include "SkClipOpPriv.h"
25 
make_paint(SkPaint * paint,const SkMatrix & localMatrix)26 static void make_paint(SkPaint* paint, const SkMatrix& localMatrix) {
27     SkColor colors[] = { 0, SK_ColorWHITE };
28     SkPoint pts[] = { { 0, 0 }, { 0, SK_Scalar1*20 } };
29     paint->setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
30                                                   SkShader::kClamp_TileMode, 0, &localMatrix));
31     paint->setBlendMode(SkBlendMode::kDstIn);
32 }
33 
34 // test drawing with strips of fading gradient above and below
test_fade(SkCanvas * canvas)35 static void test_fade(SkCanvas* canvas) {
36     SkAutoCanvasRestore ar(canvas, true);
37 
38     SkRect r;
39 
40     SkPaint p;
41     p.setAlpha(0x88);
42 
43     SkAutoCanvasRestore ar2(canvas, false);
44 
45     // create the layers
46 
47     r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100));
48     canvas->clipRect(r);
49 
50     r.fBottom = SkIntToScalar(20);
51     canvas->saveLayer(&r, nullptr);
52 
53     r.fTop = SkIntToScalar(80);
54     r.fBottom = SkIntToScalar(100);
55     canvas->saveLayer(&r, nullptr);
56 
57     // now draw the "content"
58 
59     if (true) {
60         r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100));
61 
62         canvas->saveLayerAlpha(&r, 0x80);
63 
64         SkPaint p;
65         p.setColor(SK_ColorRED);
66         p.setAntiAlias(true);
67         canvas->drawOval(r, p);
68 
69         canvas->restore();
70     } else {
71         r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100));
72 
73         SkPaint p;
74         p.setColor(SK_ColorRED);
75         p.setAntiAlias(true);
76         canvas->drawOval(r, p);
77     }
78 
79 //    return;
80 
81     // now apply an effect
82     SkMatrix m;
83     m.setScale(SK_Scalar1, -SK_Scalar1);
84     m.postTranslate(0, SkIntToScalar(100));
85 
86     SkPaint paint;
87     make_paint(&paint, m);
88     r.set(0, 0, SkIntToScalar(100), SkIntToScalar(20));
89 //    SkDebugf("--------- draw top grad\n");
90     canvas->drawRect(r, paint);
91 
92     r.fTop = SkIntToScalar(80);
93     r.fBottom = SkIntToScalar(100);
94 //    SkDebugf("--------- draw bot grad\n");
95     canvas->drawRect(r, paint);
96 }
97 
98 class LayersView : public Sample {
99 public:
LayersView()100     LayersView() {}
101 
102 protected:
onQuery(Sample::Event * evt)103     bool onQuery(Sample::Event* evt) override {
104         if (Sample::TitleQ(*evt)) {
105             Sample::TitleR(evt, "Layers");
106             return true;
107         }
108         return this->INHERITED::onQuery(evt);
109     }
110 
drawBG(SkCanvas * canvas)111     void drawBG(SkCanvas* canvas) {
112         canvas->drawColor(SK_ColorGRAY);
113     }
114 
onDrawContent(SkCanvas * canvas)115     void onDrawContent(SkCanvas* canvas) override {
116         this->drawBG(canvas);
117 
118         if (true) {
119             SkRect r;
120             r.set(SkIntToScalar(0), SkIntToScalar(0),
121                   SkIntToScalar(220), SkIntToScalar(120));
122             SkPaint p;
123             canvas->saveLayer(&r, &p);
124             canvas->drawColor(0xFFFF0000);
125             p.setAlpha(0);  // or 0
126             p.setBlendMode(SkBlendMode::kSrc);
127             canvas->drawOval(r, p);
128             canvas->restore();
129             return;
130         }
131 
132         if (false) {
133             SkRect r;
134             r.set(SkIntToScalar(0), SkIntToScalar(0),
135                   SkIntToScalar(220), SkIntToScalar(120));
136             SkPaint p;
137             p.setAlpha(0x88);
138             p.setAntiAlias(true);
139 
140             if (true) {
141                 canvas->saveLayer(&r, &p);
142                 p.setColor(0xFFFF0000);
143                 canvas->drawOval(r, p);
144                 canvas->restore();
145             }
146 
147             p.setColor(0xFF0000FF);
148             r.offset(SkIntToScalar(20), SkIntToScalar(50));
149             canvas->drawOval(r, p);
150         }
151 
152         if (false) {
153             SkPaint p;
154             p.setAlpha(0x88);
155             p.setAntiAlias(true);
156 
157             canvas->translate(SkIntToScalar(300), 0);
158 
159             SkRect r;
160             r.set(SkIntToScalar(0), SkIntToScalar(0),
161                   SkIntToScalar(220), SkIntToScalar(60));
162 
163             canvas->saveLayer(&r, &p);
164 
165             r.set(SkIntToScalar(0), SkIntToScalar(0),
166                   SkIntToScalar(220), SkIntToScalar(120));
167             p.setColor(SK_ColorBLUE);
168             canvas->drawOval(r, p);
169             canvas->restore();
170             return;
171         }
172 
173         test_fade(canvas);
174     }
175 
176 private:
177     typedef Sample INHERITED;
178 };
179 DEF_SAMPLE( return new LayersView; )
180 
181 //////////////////////////////////////////////////////////////////////////////
182 
183 #include "SkBlurImageFilter.h"
184 #include "SkMatrixConvolutionImageFilter.h"
185 #include "SkMorphologyImageFilter.h"
186 
187 #include "Resources.h"
188 #include "SkAnimTimer.h"
189 
190 class BackdropView : public Sample {
191     SkPoint fCenter;
192     SkScalar fAngle;
193     sk_sp<SkImage> fImage;
194     sk_sp<SkImageFilter> fFilter;
195 public:
BackdropView()196     BackdropView() {
197         fCenter.set(200, 150);
198         fAngle = 0;
199         fImage = GetResourceAsImage("images/mandrill_512.png");
200         fFilter = SkDilateImageFilter::Make(8, 8, nullptr);
201     }
202 
203 protected:
onQuery(Sample::Event * evt)204     bool onQuery(Sample::Event* evt) override {
205         if (Sample::TitleQ(*evt)) {
206             Sample::TitleR(evt, "Backdrop");
207             return true;
208         }
209         return this->INHERITED::onQuery(evt);
210     }
211 
onDrawContent(SkCanvas * canvas)212     void onDrawContent(SkCanvas* canvas) override {
213         canvas->drawImage(fImage.get(), 0, 0, nullptr);
214 
215         const SkScalar w = 250;
216         const SkScalar h = 150;
217         SkPath path;
218         path.addOval(SkRect::MakeXYWH(-w/2, -h/2, w, h));
219         SkMatrix m;
220         m.setRotate(fAngle);
221         m.postTranslate(fCenter.x(), fCenter.y());
222         path.transform(m);
223 
224         canvas->clipPath(path, kIntersect_SkClipOp, true);
225         const SkRect bounds = path.getBounds();
226 
227         SkPaint paint;
228         paint.setAlpha(0xCC);
229         canvas->saveLayer({ &bounds, &paint, fFilter.get(), nullptr, nullptr, 0 });
230 
231         canvas->restore();
232     }
233 
onAnimate(const SkAnimTimer & timer)234     bool onAnimate(const SkAnimTimer& timer) override {
235         fAngle = SkDoubleToScalar(fmod(timer.secs() * 360 / 5, 360));
236         return true;
237     }
238 
onFindClickHandler(SkScalar x,SkScalar y,unsigned modi)239     Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
240         return new Click(this);
241     }
242 
onClick(Click * click)243     bool onClick(Click* click) override {
244         fCenter = click->fCurr;
245         return this->INHERITED::onClick(click);
246     }
247 
248 private:
249     typedef Sample INHERITED;
250 };
251 DEF_SAMPLE( return new BackdropView; )
252