Flutter Engine
The Flutter Engine
LayersSlide.cpp
Go to the documentation of this file.
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
13#include "include/core/SkPath.h"
19#include "src/base/SkTime.h"
20#include "src/base/SkUTF.h"
21#include "tools/DecodeUtils.h"
23#include "tools/viewer/Slide.h"
24
25static void make_paint(SkPaint* paint, const SkMatrix& localMatrix) {
26 SkColor colors[] = { 0, SK_ColorWHITE };
27 SkPoint pts[] = { { 0, 0 }, { 0, SK_Scalar1*20 } };
28 paint->setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
29 SkTileMode::kClamp, 0, &localMatrix));
30 paint->setBlendMode(SkBlendMode::kDstIn);
31}
32
33// test drawing with strips of fading gradient above and below
34static void test_fade(SkCanvas* canvas) {
35 SkAutoCanvasRestore ar(canvas, true);
36
37 SkRect r;
38
39 SkAutoCanvasRestore ar2(canvas, false);
40
41 // create the layers
42
43 r.setWH(100, 100);
44 canvas->clipRect(r);
45
46 r.fBottom = SkIntToScalar(20);
47 canvas->saveLayer(&r, nullptr);
48
49 r.fTop = SkIntToScalar(80);
50 r.fBottom = SkIntToScalar(100);
51 canvas->saveLayer(&r, nullptr);
52
53 // now draw the "content"
54
55 if ((true)) {
56 r.setWH(100, 100);
57
58 canvas->saveLayerAlpha(&r, 0x80);
59
60 SkPaint p;
61 p.setColor(SK_ColorRED);
62 p.setAntiAlias(true);
63 canvas->drawOval(r, p);
64
65 canvas->restore();
66 } else {
67 r.setWH(100, 100);
68
69 SkPaint p;
70 p.setColor(SK_ColorRED);
71 p.setAntiAlias(true);
72 canvas->drawOval(r, p);
73 }
74
75// return;
76
77 // now apply an effect
78 SkMatrix m;
79 m.setScale(SK_Scalar1, -SK_Scalar1);
80 m.postTranslate(0, SkIntToScalar(100));
81
84 r.setWH(100, 20);
85// SkDebugf("--------- draw top grad\n");
86 canvas->drawRect(r, paint);
87
88 r.fTop = SkIntToScalar(80);
89 r.fBottom = SkIntToScalar(100);
90// SkDebugf("--------- draw bot grad\n");
91 canvas->drawRect(r, paint);
92}
93
94class LayersSlide : public Slide {
95public:
96 LayersSlide() { fName = "Layers"; }
97
98 void draw(SkCanvas* canvas) override {
99 canvas->drawColor(SK_ColorGRAY);
100
101 if ((true)) {
102 SkRect r;
103 r.setWH(220, 120);
104 SkPaint p;
105 canvas->saveLayer(&r, &p);
106 canvas->drawColor(0xFFFF0000);
107 p.setAlpha(0); // or 0
108 p.setBlendMode(SkBlendMode::kSrc);
109 canvas->drawOval(r, p);
110 canvas->restore();
111 return;
112 }
113
114 if ((false)) {
115 SkRect r;
116 r.setWH(220, 120);
117 SkPaint p;
118 p.setAlpha(0x88);
119 p.setAntiAlias(true);
120
121 if (true) {
122 canvas->saveLayer(&r, &p);
123 p.setColor(0xFFFF0000);
124 canvas->drawOval(r, p);
125 canvas->restore();
126 }
127
128 p.setColor(0xFF0000FF);
130 canvas->drawOval(r, p);
131 }
132
133 if ((false)) {
134 SkPaint p;
135 p.setAlpha(0x88);
136 p.setAntiAlias(true);
137
138 canvas->translate(SkIntToScalar(300), 0);
139
140 SkRect r;
141 r.setWH(220, 60);
142
143 canvas->saveLayer(&r, &p);
144
145 r.setWH(220, 120);
146 p.setColor(SK_ColorBLUE);
147 canvas->drawOval(r, p);
148 canvas->restore();
149 return;
150 }
151
152 test_fade(canvas);
153 }
154};
155DEF_SLIDE( return new LayersSlide; )
156
157//////////////////////////////////////////////////////////////////////////////
158
159#include "include/effects/SkImageFilters.h"
160
161#include "tools/Resources.h"
162
164 SkPoint fCenter;
165 SkScalar fAngle;
166 sk_sp<SkImage> fImage;
167 sk_sp<SkImageFilter> fFilter;
168
169public:
171 fName = "Backdrop";
172 }
173
174protected:
175 void load(SkScalar, SkScalar) override {
176 fCenter.set(200, 150);
177 fAngle = 0;
178 fImage = ToolUtils::GetResourceAsImage("images/mandrill_512.png");
179 fFilter = SkImageFilters::Dilate(8, 8, nullptr);
180 }
181
182 void draw(SkCanvas* canvas) override {
183 canvas->drawImage(fImage.get(), 0, 0);
184
185 const SkScalar w = 250;
186 const SkScalar h = 150;
187 SkPath path;
188 path.addOval(SkRect::MakeXYWH(-w/2, -h/2, w, h));
189 SkMatrix m;
190 m.setRotate(fAngle);
191 m.postTranslate(fCenter.x(), fCenter.y());
192 path.transform(m);
193
194 canvas->clipPath(path, SkClipOp::kIntersect, true);
195 const SkRect bounds = path.getBounds();
196
198 paint.setAlpha(0xCC);
199 canvas->saveLayer(SkCanvas::SaveLayerRec(&bounds, &paint, fFilter.get(), 0));
200
201 canvas->restore();
202 }
203
204 bool animate(double nanos) override {
205 fAngle = SkDoubleToScalar(fmod(1e-9 * nanos * 360 / 5, 360));
206 return true;
207 }
208
209protected:
211 return new Click();
212 }
213
214 bool onClick(Click* click) override {
215 fCenter = click->fCurr;
216 return true;
217 }
218};
219
220DEF_SLIDE( return new BackdropSlide; )
const char * fName
static void test_fade(SkCanvas *canvas)
Definition: LayersSlide.cpp:34
static void make_paint(SkPaint *paint, const SkMatrix &localMatrix)
Definition: LayersSlide.cpp:25
@ kDstIn
r = d * sa
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorGRAY
Definition: SkColor.h:113
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
#define SK_Scalar1
Definition: SkScalar.h:18
#define SkDoubleToScalar(x)
Definition: SkScalar.h:64
#define SkIntToScalar(x)
Definition: SkScalar.h:57
#define DEF_SLIDE(code)
Definition: Slide.h:25
Click * onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override
bool onClick(Click *click) override
void load(SkScalar, SkScalar) override
bool animate(double nanos) override
void draw(SkCanvas *canvas) override
void draw(SkCanvas *canvas) override
Definition: LayersSlide.cpp:98
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition: SkCanvas.cpp:496
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void drawOval(const SkRect &oval, const SkPaint &paint)
Definition: SkCanvas.cpp:1698
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition: SkCanvas.h:1182
int saveLayerAlpha(const SkRect *bounds, U8CPU alpha)
Definition: SkCanvas.h:661
void clipPath(const SkPath &path, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1456
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< SkImageFilter > Dilate(SkScalar radiusX, SkScalar radiusY, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
Definition: SkPath.h:59
Definition: Slide.h:29
SkString fName
Definition: Slide.h:54
T * get() const
Definition: SkRefCnt.h:303
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
double y
double x
Optional< SkRect > bounds
Definition: SkRecords.h:189
PODArray< SkColor > colors
Definition: SkRecords.h:276
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition: DecodeUtils.h:25
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
ModifierKey
Definition: ModifierKey.h:9
SkScalar w
SkScalar h
void set(float x, float y)
Definition: SkPoint_impl.h:200
constexpr float y() const
Definition: SkPoint_impl.h:187
constexpr float x() const
Definition: SkPoint_impl.h:181
SkScalar fBottom
larger y-axis bounds
Definition: extension.cpp:17
void setWH(float width, float height)
Definition: SkRect.h:944
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
void offset(float dx, float dy)
Definition: SkRect.h:1016
SkScalar fTop
smaller y-axis bounds
Definition: extension.cpp:15