blob: 43f5d4236af8b52971f818e7a0b1b42297850743 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
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"
15#include "SkDevice.h"
16#include "SkGradientShader.h"
17#include "SkImageDecoder.h"
18#include "SkInterpolator.h"
19#include "SkMaskFilter.h"
20#include "SkPath.h"
21#include "SkRegion.h"
22#include "SkShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#include "SkTime.h"
24#include "SkTypeface.h"
25#include "SkUtils.h"
26#include "SkKey.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000027#include "SkXfermode.h"
28#include "SkDrawFilter.h"
29
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000030static void make_paint(SkPaint* paint, const SkMatrix& localMatrix) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 SkColor colors[] = { 0, SK_ColorWHITE };
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000032 SkPoint pts[] = { { 0, 0 }, { 0, SK_Scalar1*20 } };
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000033 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000034 0, &localMatrix);
rmistry@google.comae933ce2012-08-23 18:19:56 +000035
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 paint->setShader(s)->unref();
reed@android.com0baf1932009-06-24 12:41:42 +000037 paint->setXfermodeMode(SkXfermode::kDstIn_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000038}
39
40static void dump_layers(const char label[], SkCanvas* canvas) {
41 SkDebugf("Dump Layers(%s)\n", label);
42
43 SkCanvas::LayerIter iter(canvas, true);
44 int index = 0;
45 while (!iter.done()) {
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000046 SkImageInfo info = iter.device()->imageInfo();
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 const SkIRect& clip = iter.clip().getBounds();
48 SkDebugf("Layer[%d] bitmap [%d %d] X=%d Y=%d clip=[%d %d %d %d] alpha=%d\n", index++,
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000049 info.width(), info.height(), iter.x(), iter.y(),
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 clip.fLeft, clip.fTop, clip.fRight, clip.fBottom,
51 iter.paint().getAlpha());
52 iter.next();
53 }
54}
55
56// test drawing with strips of fading gradient above and below
57static void test_fade(SkCanvas* canvas) {
58 SkAutoCanvasRestore ar(canvas, true);
59
60 SkRect r;
rmistry@google.comae933ce2012-08-23 18:19:56 +000061
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 SkPaint p;
63 p.setAlpha(0x88);
64
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +000065 SkAutoCanvasRestore ar2(canvas, false);
reed@android.com8a1c16f2008-12-17 15:59:43 +000066
67 // create the layers
68
69 r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100));
70 canvas->clipRect(r);
rmistry@google.comae933ce2012-08-23 18:19:56 +000071
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 r.fBottom = SkIntToScalar(20);
commit-bot@chromium.org091a5942014-04-18 14:19:31 +000073 canvas->saveLayer(&r, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +000074
75 r.fTop = SkIntToScalar(80);
76 r.fBottom = SkIntToScalar(100);
commit-bot@chromium.org091a5942014-04-18 14:19:31 +000077 canvas->saveLayer(&r, NULL);
rmistry@google.comae933ce2012-08-23 18:19:56 +000078
79 // now draw the "content"
reed@android.com8a1c16f2008-12-17 15:59:43 +000080
81 if (true) {
82 r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100));
83
84 canvas->saveLayerAlpha(&r, 0x80);
85
86 SkPaint p;
87 p.setColor(SK_ColorRED);
88 p.setAntiAlias(true);
89 canvas->drawOval(r, p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000090
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 dump_layers("inside layer alpha", canvas);
rmistry@google.comae933ce2012-08-23 18:19:56 +000092
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 canvas->restore();
94 } else {
95 r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100));
rmistry@google.comae933ce2012-08-23 18:19:56 +000096
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 SkPaint p;
98 p.setColor(SK_ColorRED);
99 p.setAntiAlias(true);
100 canvas->drawOval(r, p);
101 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000102
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103// return;
104
105 dump_layers("outside layer alpha", canvas);
106
107 // now apply an effect
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000108 SkMatrix m;
109 m.setScale(SK_Scalar1, -SK_Scalar1);
110 m.postTranslate(0, SkIntToScalar(100));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111
112 SkPaint paint;
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000113 make_paint(&paint, m);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 r.set(0, 0, SkIntToScalar(100), SkIntToScalar(20));
115// SkDebugf("--------- draw top grad\n");
116 canvas->drawRect(r, paint);
117
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 r.fTop = SkIntToScalar(80);
119 r.fBottom = SkIntToScalar(100);
120// SkDebugf("--------- draw bot grad\n");
121 canvas->drawRect(r, paint);
122}
123
124class RedFilter : public SkDrawFilter {
125public:
reed@google.com971aca72012-11-26 20:26:54 +0000126 virtual bool filter(SkPaint* p, SkDrawFilter::Type) SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 fColor = p->getColor();
128 if (fColor == SK_ColorRED) {
129 p->setColor(SK_ColorGREEN);
130 }
reed@google.com971aca72012-11-26 20:26:54 +0000131 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000133
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134private:
135 SkColor fColor;
136};
137
138class LayersView : public SkView {
139public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000140 LayersView() {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141
142protected:
143 // overrides from SkEventSink
144 virtual bool onQuery(SkEvent* evt) {
145 if (SampleCode::TitleQ(*evt)) {
146 SampleCode::TitleR(evt, "Layers");
147 return true;
148 }
149 return this->INHERITED::onQuery(evt);
150 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000151
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 void drawBG(SkCanvas* canvas) {
reed@android.com149e2f62009-05-22 14:39:03 +0000153 canvas->drawColor(SK_ColorGRAY);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000155
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 virtual void onDraw(SkCanvas* canvas) {
157 this->drawBG(canvas);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000158
reed@android.com149e2f62009-05-22 14:39:03 +0000159 if (true) {
160 SkRect r;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000161 r.set(SkIntToScalar(0), SkIntToScalar(0),
162 SkIntToScalar(220), SkIntToScalar(120));
reed@android.com149e2f62009-05-22 14:39:03 +0000163 SkPaint p;
164 canvas->saveLayer(&r, &p);
165 canvas->drawColor(0xFFFF0000);
reed@android.comf2b98d62010-12-20 18:26:13 +0000166 p.setAlpha(0); // or 0
reed@android.com0baf1932009-06-24 12:41:42 +0000167 p.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@android.com149e2f62009-05-22 14:39:03 +0000168 canvas->drawOval(r, p);
169 canvas->restore();
170 return;
171 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000172
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 if (false) {
174 SkRect r;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000175 r.set(SkIntToScalar(0), SkIntToScalar(0),
176 SkIntToScalar(220), SkIntToScalar(120));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177 SkPaint p;
178 p.setAlpha(0x88);
179 p.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000180
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181 if (true) {
182 canvas->saveLayer(&r, &p);
183 p.setColor(0xFFFF0000);
184 canvas->drawOval(r, p);
185 canvas->restore();
186 }
187
188 p.setColor(0xFF0000FF);
189 r.offset(SkIntToScalar(20), SkIntToScalar(50));
190 canvas->drawOval(r, p);
191 }
192
193 if (false) {
194 SkPaint p;
195 p.setAlpha(0x88);
196 p.setAntiAlias(true);
197
198 canvas->translate(SkIntToScalar(300), 0);
199
200 SkRect r;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000201 r.set(SkIntToScalar(0), SkIntToScalar(0),
202 SkIntToScalar(220), SkIntToScalar(60));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203
commit-bot@chromium.org091a5942014-04-18 14:19:31 +0000204 canvas->saveLayer(&r, &p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205
rmistry@google.comae933ce2012-08-23 18:19:56 +0000206 r.set(SkIntToScalar(0), SkIntToScalar(0),
207 SkIntToScalar(220), SkIntToScalar(120));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208 p.setColor(SK_ColorBLUE);
209 canvas->drawOval(r, p);
210 canvas->restore();
211 return;
212 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000213
reed@android.com8a1c16f2008-12-17 15:59:43 +0000214 test_fade(canvas);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000216
reed@google.com4d5c26d2013-01-08 16:17:50 +0000217 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y,
218 unsigned modi) SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000219 this->inval(NULL);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000220
reed@google.com4d5c26d2013-01-08 16:17:50 +0000221 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000222 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000223
reed@android.com8a1c16f2008-12-17 15:59:43 +0000224 virtual bool onClick(Click* click) {
225 return this->INHERITED::onClick(click);
226 }
227
sugoi@google.com93c7ee32013-03-12 14:36:57 +0000228 virtual bool handleKey(SkKey) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229 this->inval(NULL);
230 return true;
231 }
232
233private:
234 typedef SkView INHERITED;
235};
236
237//////////////////////////////////////////////////////////////////////////////
238
239static SkView* MyFactory() { return new LayersView; }
240static SkViewRegister reg(MyFactory);