blob: 71b2e8ba66c292483428adc6e22f2e88978ca447 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reed8a21c9f2016-03-08 18:50:00 -08007
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkBlurMaskFilter.h"
12#include "SkCamera.h"
13#include "SkColorFilter.h"
14#include "SkColorPriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkGradientShader.h"
msarettd15750c2016-03-18 15:48:49 -070016#include "SkImage.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkInterpolator.h"
18#include "SkMaskFilter.h"
19#include "SkPath.h"
20#include "SkRegion.h"
21#include "SkShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkTime.h"
23#include "SkTypeface.h"
24#include "SkUtils.h"
25#include "SkKey.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000026#include "SkXfermode.h"
27#include "SkDrawFilter.h"
28
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000029static void make_paint(SkPaint* paint, const SkMatrix& localMatrix) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000030 SkColor colors[] = { 0, SK_ColorWHITE };
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000031 SkPoint pts[] = { { 0, 0 }, { 0, SK_Scalar1*20 } };
reed8a21c9f2016-03-08 18:50:00 -080032 paint->setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
33 SkShader::kClamp_TileMode, 0, &localMatrix));
reed374772b2016-10-05 17:33:02 -070034 paint->setBlendMode(SkBlendMode::kDstIn);
reed@android.com8a1c16f2008-12-17 15:59:43 +000035}
36
reed@android.com8a1c16f2008-12-17 15:59:43 +000037// test drawing with strips of fading gradient above and below
38static void test_fade(SkCanvas* canvas) {
39 SkAutoCanvasRestore ar(canvas, true);
40
41 SkRect r;
rmistry@google.comae933ce2012-08-23 18:19:56 +000042
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 SkPaint p;
44 p.setAlpha(0x88);
45
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +000046 SkAutoCanvasRestore ar2(canvas, false);
reed@android.com8a1c16f2008-12-17 15:59:43 +000047
48 // create the layers
49
50 r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100));
51 canvas->clipRect(r);
rmistry@google.comae933ce2012-08-23 18:19:56 +000052
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 r.fBottom = SkIntToScalar(20);
halcanary96fcdcc2015-08-27 07:41:13 -070054 canvas->saveLayer(&r, nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000055
56 r.fTop = SkIntToScalar(80);
57 r.fBottom = SkIntToScalar(100);
halcanary96fcdcc2015-08-27 07:41:13 -070058 canvas->saveLayer(&r, nullptr);
rmistry@google.comae933ce2012-08-23 18:19:56 +000059
60 // now draw the "content"
reed@android.com8a1c16f2008-12-17 15:59:43 +000061
62 if (true) {
63 r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100));
64
65 canvas->saveLayerAlpha(&r, 0x80);
66
67 SkPaint p;
68 p.setColor(SK_ColorRED);
69 p.setAntiAlias(true);
70 canvas->drawOval(r, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000071
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 canvas->restore();
73 } else {
74 r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100));
rmistry@google.comae933ce2012-08-23 18:19:56 +000075
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 SkPaint p;
77 p.setColor(SK_ColorRED);
78 p.setAntiAlias(true);
79 canvas->drawOval(r, p);
80 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000081
reed@android.com8a1c16f2008-12-17 15:59:43 +000082// return;
83
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 // now apply an effect
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000085 SkMatrix m;
86 m.setScale(SK_Scalar1, -SK_Scalar1);
87 m.postTranslate(0, SkIntToScalar(100));
reed@android.com8a1c16f2008-12-17 15:59:43 +000088
89 SkPaint paint;
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000090 make_paint(&paint, m);
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 r.set(0, 0, SkIntToScalar(100), SkIntToScalar(20));
92// SkDebugf("--------- draw top grad\n");
93 canvas->drawRect(r, paint);
94
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 r.fTop = SkIntToScalar(80);
96 r.fBottom = SkIntToScalar(100);
97// SkDebugf("--------- draw bot grad\n");
98 canvas->drawRect(r, paint);
99}
100
101class RedFilter : public SkDrawFilter {
102public:
mtklein36352bf2015-03-25 18:17:31 -0700103 bool filter(SkPaint* p, SkDrawFilter::Type) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 fColor = p->getColor();
105 if (fColor == SK_ColorRED) {
106 p->setColor(SK_ColorGREEN);
107 }
reed@google.com971aca72012-11-26 20:26:54 +0000108 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000110
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111private:
112 SkColor fColor;
113};
114
115class LayersView : public SkView {
116public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000117 LayersView() {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118
119protected:
120 // overrides from SkEventSink
mtkleinf0599002015-07-13 06:18:39 -0700121 bool onQuery(SkEvent* evt) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 if (SampleCode::TitleQ(*evt)) {
123 SampleCode::TitleR(evt, "Layers");
124 return true;
125 }
126 return this->INHERITED::onQuery(evt);
127 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000128
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 void drawBG(SkCanvas* canvas) {
reed@android.com149e2f62009-05-22 14:39:03 +0000130 canvas->drawColor(SK_ColorGRAY);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000132
mtkleinf0599002015-07-13 06:18:39 -0700133 void onDraw(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 this->drawBG(canvas);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000135
reed@android.com149e2f62009-05-22 14:39:03 +0000136 if (true) {
137 SkRect r;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000138 r.set(SkIntToScalar(0), SkIntToScalar(0),
139 SkIntToScalar(220), SkIntToScalar(120));
reed@android.com149e2f62009-05-22 14:39:03 +0000140 SkPaint p;
141 canvas->saveLayer(&r, &p);
142 canvas->drawColor(0xFFFF0000);
reed@android.comf2b98d62010-12-20 18:26:13 +0000143 p.setAlpha(0); // or 0
reed374772b2016-10-05 17:33:02 -0700144 p.setBlendMode(SkBlendMode::kSrc);
reed@android.com149e2f62009-05-22 14:39:03 +0000145 canvas->drawOval(r, p);
146 canvas->restore();
147 return;
148 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000149
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 if (false) {
151 SkRect r;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000152 r.set(SkIntToScalar(0), SkIntToScalar(0),
153 SkIntToScalar(220), SkIntToScalar(120));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 SkPaint p;
155 p.setAlpha(0x88);
156 p.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000157
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 if (true) {
159 canvas->saveLayer(&r, &p);
160 p.setColor(0xFFFF0000);
161 canvas->drawOval(r, p);
162 canvas->restore();
163 }
164
165 p.setColor(0xFF0000FF);
166 r.offset(SkIntToScalar(20), SkIntToScalar(50));
167 canvas->drawOval(r, p);
168 }
169
170 if (false) {
171 SkPaint p;
172 p.setAlpha(0x88);
173 p.setAntiAlias(true);
174
175 canvas->translate(SkIntToScalar(300), 0);
176
177 SkRect r;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000178 r.set(SkIntToScalar(0), SkIntToScalar(0),
179 SkIntToScalar(220), SkIntToScalar(60));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180
commit-bot@chromium.org091a5942014-04-18 14:19:31 +0000181 canvas->saveLayer(&r, &p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182
rmistry@google.comae933ce2012-08-23 18:19:56 +0000183 r.set(SkIntToScalar(0), SkIntToScalar(0),
184 SkIntToScalar(220), SkIntToScalar(120));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185 p.setColor(SK_ColorBLUE);
186 canvas->drawOval(r, p);
187 canvas->restore();
188 return;
189 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000190
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191 test_fade(canvas);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000192 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000193
mtkleinf0599002015-07-13 06:18:39 -0700194 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
halcanary96fcdcc2015-08-27 07:41:13 -0700195 this->inval(nullptr);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000196
reed@google.com4d5c26d2013-01-08 16:17:50 +0000197 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000199
mtkleinf0599002015-07-13 06:18:39 -0700200 bool onClick(Click* click) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201 return this->INHERITED::onClick(click);
202 }
203
sugoi@google.com93c7ee32013-03-12 14:36:57 +0000204 virtual bool handleKey(SkKey) {
halcanary96fcdcc2015-08-27 07:41:13 -0700205 this->inval(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000206 return true;
207 }
208
209private:
210 typedef SkView INHERITED;
211};
reedbfd5f172016-01-07 11:28:08 -0800212DEF_SAMPLE( return new LayersView; )
reed@android.com8a1c16f2008-12-17 15:59:43 +0000213
214//////////////////////////////////////////////////////////////////////////////
215
reedbfd5f172016-01-07 11:28:08 -0800216#include "SkBlurImageFilter.h"
217#include "SkMatrixConvolutionImageFilter.h"
218#include "SkMorphologyImageFilter.h"
219
220#include "Resources.h"
221#include "SkAnimTimer.h"
222
223class BackdropView : public SampleView {
224 SkPoint fCenter;
225 SkScalar fAngle;
reed9ce9d672016-03-17 10:51:11 -0700226 sk_sp<SkImage> fImage;
robertphillipsfc11b0a2016-04-05 09:09:36 -0700227 sk_sp<SkImageFilter> fFilter;
reedbfd5f172016-01-07 11:28:08 -0800228public:
229 BackdropView() {
230 fCenter.set(200, 150);
231 fAngle = 0;
reed9ce9d672016-03-17 10:51:11 -0700232 fImage = GetResourceAsImage("mandrill_512.png");
robertphillipsfc11b0a2016-04-05 09:09:36 -0700233 fFilter = SkDilateImageFilter::Make(8, 8, nullptr);
reedbfd5f172016-01-07 11:28:08 -0800234 }
235
236protected:
237 // overrides from SkEventSink
238 bool onQuery(SkEvent* evt) override {
239 if (SampleCode::TitleQ(*evt)) {
240 SampleCode::TitleR(evt, "Backdrop");
241 return true;
242 }
243 return this->INHERITED::onQuery(evt);
244 }
245
246 void onDrawContent(SkCanvas* canvas) override {
reed9ce9d672016-03-17 10:51:11 -0700247 canvas->drawImage(fImage.get(), 0, 0, nullptr);
reedbfd5f172016-01-07 11:28:08 -0800248
249 const SkScalar w = 250;
250 const SkScalar h = 150;
251 SkPath path;
252 path.addOval(SkRect::MakeXYWH(-w/2, -h/2, w, h));
253 SkMatrix m;
254 m.setRotate(fAngle);
255 m.postTranslate(fCenter.x(), fCenter.y());
256 path.transform(m);
257
reed73603f32016-09-20 08:42:38 -0700258 canvas->clipPath(path, SkCanvas::kIntersect_Op, true);
reedbfd5f172016-01-07 11:28:08 -0800259 const SkRect bounds = path.getBounds();
260
261 SkPaint paint;
262 paint.setAlpha(0xCC);
robertphillipsfc11b0a2016-04-05 09:09:36 -0700263 canvas->saveLayer({ &bounds, &paint, fFilter.get(), 0 });
reedbfd5f172016-01-07 11:28:08 -0800264
265 canvas->restore();
266 }
267
268 bool onAnimate(const SkAnimTimer& timer) override {
269 fAngle = SkDoubleToScalar(fmod(timer.secs() * 360 / 5, 360));
270 return true;
271 }
272
273 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
274 this->inval(nullptr);
275 return new Click(this);
276 }
277
278 bool onClick(Click* click) override {
279 this->inval(nullptr);
280 fCenter = click->fCurr;
281 return this->INHERITED::onClick(click);
282 }
283
284private:
285 typedef SampleView INHERITED;
286};
287DEF_SAMPLE( return new BackdropView; )