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 "include/core/SkCanvas.h"
9 #include "include/core/SkColorFilter.h"
10 #include "include/core/SkColorPriv.h"
11 #include "include/core/SkImage.h"
12 #include "include/core/SkMaskFilter.h"
13 #include "include/core/SkPath.h"
14 #include "include/core/SkRegion.h"
15 #include "include/core/SkShader.h"
16 #include "include/core/SkTime.h"
17 #include "include/core/SkTypeface.h"
18 #include "include/effects/SkBlurMaskFilter.h"
19 #include "include/effects/SkGradientShader.h"
20 #include "include/utils/SkCamera.h"
21 #include "include/utils/SkInterpolator.h"
22 #include "samplecode/Sample.h"
23 #include "src/core/SkClipOpPriv.h"
24 #include "src/utils/SkUTF.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 SkTileMode::kClamp, 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:
name()103 SkString name() override { return SkString("Layers"); }
104
drawBG(SkCanvas * canvas)105 void drawBG(SkCanvas* canvas) {
106 canvas->drawColor(SK_ColorGRAY);
107 }
108
onDrawContent(SkCanvas * canvas)109 void onDrawContent(SkCanvas* canvas) override {
110 this->drawBG(canvas);
111
112 if (true) {
113 SkRect r;
114 r.set(SkIntToScalar(0), SkIntToScalar(0),
115 SkIntToScalar(220), SkIntToScalar(120));
116 SkPaint p;
117 canvas->saveLayer(&r, &p);
118 canvas->drawColor(0xFFFF0000);
119 p.setAlpha(0); // or 0
120 p.setBlendMode(SkBlendMode::kSrc);
121 canvas->drawOval(r, p);
122 canvas->restore();
123 return;
124 }
125
126 if (false) {
127 SkRect r;
128 r.set(SkIntToScalar(0), SkIntToScalar(0),
129 SkIntToScalar(220), SkIntToScalar(120));
130 SkPaint p;
131 p.setAlpha(0x88);
132 p.setAntiAlias(true);
133
134 if (true) {
135 canvas->saveLayer(&r, &p);
136 p.setColor(0xFFFF0000);
137 canvas->drawOval(r, p);
138 canvas->restore();
139 }
140
141 p.setColor(0xFF0000FF);
142 r.offset(SkIntToScalar(20), SkIntToScalar(50));
143 canvas->drawOval(r, p);
144 }
145
146 if (false) {
147 SkPaint p;
148 p.setAlpha(0x88);
149 p.setAntiAlias(true);
150
151 canvas->translate(SkIntToScalar(300), 0);
152
153 SkRect r;
154 r.set(SkIntToScalar(0), SkIntToScalar(0),
155 SkIntToScalar(220), SkIntToScalar(60));
156
157 canvas->saveLayer(&r, &p);
158
159 r.set(SkIntToScalar(0), SkIntToScalar(0),
160 SkIntToScalar(220), SkIntToScalar(120));
161 p.setColor(SK_ColorBLUE);
162 canvas->drawOval(r, p);
163 canvas->restore();
164 return;
165 }
166
167 test_fade(canvas);
168 }
169
170 private:
171 typedef Sample INHERITED;
172 };
173 DEF_SAMPLE( return new LayersView; )
174
175 //////////////////////////////////////////////////////////////////////////////
176
177 #include "include/effects/SkImageFilters.h"
178
179 #include "tools/Resources.h"
180
181 class BackdropView : public Sample {
182 SkPoint fCenter;
183 SkScalar fAngle;
184 sk_sp<SkImage> fImage;
185 sk_sp<SkImageFilter> fFilter;
186 public:
BackdropView()187 BackdropView() {
188 fCenter.set(200, 150);
189 fAngle = 0;
190 fImage = GetResourceAsImage("images/mandrill_512.png");
191 fFilter = SkImageFilters::Dilate(8, 8, nullptr);
192 }
193
194 protected:
name()195 SkString name() override { return SkString("Backdrop"); }
196
onDrawContent(SkCanvas * canvas)197 void onDrawContent(SkCanvas* canvas) override {
198 canvas->drawImage(fImage.get(), 0, 0, nullptr);
199
200 const SkScalar w = 250;
201 const SkScalar h = 150;
202 SkPath path;
203 path.addOval(SkRect::MakeXYWH(-w/2, -h/2, w, h));
204 SkMatrix m;
205 m.setRotate(fAngle);
206 m.postTranslate(fCenter.x(), fCenter.y());
207 path.transform(m);
208
209 canvas->clipPath(path, kIntersect_SkClipOp, true);
210 const SkRect bounds = path.getBounds();
211
212 SkPaint paint;
213 paint.setAlpha(0xCC);
214 canvas->saveLayer({ &bounds, &paint, fFilter.get(), nullptr, nullptr, 0 });
215
216 canvas->restore();
217 }
218
onAnimate(double nanos)219 bool onAnimate(double nanos) override {
220 fAngle = SkDoubleToScalar(fmod(1e-9 * nanos * 360 / 5, 360));
221 return true;
222 }
223
onFindClickHandler(SkScalar x,SkScalar y,ModifierKey modi)224 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, ModifierKey modi) override {
225 return new Click();
226 }
227
onClick(Click * click)228 bool onClick(Click* click) override {
229 fCenter = click->fCurr;
230 return true;
231 }
232
233 private:
234 typedef Sample INHERITED;
235 };
236 DEF_SAMPLE( return new BackdropView; )
237