Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SlidesSlide.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 */
12#include "src/core/SkBlurMask.h"
15#include "tools/EncodeUtils.h"
17
18#define BG_COLOR 0xFFDDDDDD
19
20typedef void (*SlideProc)(SkCanvas*);
21
22///////////////////////////////////////////////////////////////////////////////
23
29
30static void compose_pe(SkPaint* paint) {
31 SkPathEffect* pe = paint->getPathEffect();
33 sk_sp<SkPathEffect> compose;
34 if (pe) {
35 compose = SkPathEffect::MakeCompose(sk_ref_sp(pe), corner);
36 } else {
37 compose = corner;
38 }
39 paint->setPathEffect(compose);
40}
41
42static void hair_pe(SkPaint* paint) {
43 paint->setStrokeWidth(0);
44}
45
46static void hair2_pe(SkPaint* paint) {
47 paint->setStrokeWidth(0);
49}
50
51static void stroke_pe(SkPaint* paint) {
52 paint->setStrokeWidth(12);
54}
55
56static void dash_pe(SkPaint* paint) {
57 SkScalar inter[] = { 20, 10, 10, 10 };
58 paint->setStrokeWidth(12);
59 paint->setPathEffect(SkDashPathEffect::Make(inter, std::size(inter), 0));
61}
62
63static const int gXY[] = {
644, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
65};
66
67static void scale(SkPath* path, SkScalar scale) {
68 SkMatrix m;
69 m.setScale(scale, scale);
70 path->transform(m);
71}
72
73static void one_d_pe(SkPaint* paint) {
74 SkPath path;
75 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
76 for (unsigned i = 2; i < std::size(gXY); i += 2)
77 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
78 path.close();
79 path.offset(SkIntToScalar(-6), 0);
80 scale(&path, 1.5f);
81
82 paint->setPathEffect(SkPath1DPathEffect::Make(path, SkIntToScalar(21), 0,
85}
86
87typedef void (*PE_Proc)(SkPaint*);
89
90static void fill_pe(SkPaint* paint) {
91 paint->setStyle(SkPaint::kFill_Style);
92 paint->setPathEffect(nullptr);
93}
94
95static void discrete_pe(SkPaint* paint) {
96 paint->setPathEffect(SkDiscretePathEffect::Make(10, 4));
97}
98
100 SkMatrix m;
101 m.setScale(SkIntToScalar(12), SkIntToScalar(12));
102
103 SkPath path;
104 path.addCircle(0, 0, SkIntToScalar(5));
105
106 return SkPath2DPathEffect::Make(m, path);
107}
108
109static void tile_pe(SkPaint* paint) {
110 paint->setPathEffect(MakeTileEffect());
111}
112
113static const PE_Proc gPE2[] = { fill_pe, discrete_pe, tile_pe };
114
115static void patheffect_slide(SkCanvas* canvas) {
117 paint.setAntiAlias(true);
119
120 SkPath path;
121 path.moveTo(20, 20);
122 path.lineTo(70, 120);
123 path.lineTo(120, 30);
124 path.lineTo(170, 80);
125 path.lineTo(240, 50);
126
127 size_t i;
128 canvas->save();
129 for (i = 0; i < std::size(gPE); i++) {
130 gPE[i](&paint);
131 canvas->drawPath(path, paint);
132 canvas->translate(0, 75);
133 }
134 canvas->restore();
135
136 path.reset();
137 SkRect r = { 0, 0, 250, 120 };
138 path.addOval(r, SkPathDirection::kCW);
139 r.inset(50, 50);
140 path.addRect(r, SkPathDirection::kCCW);
141
142 canvas->translate(320, 20);
143 for (i = 0; i < std::size(gPE2); i++) {
144 gPE2[i](&paint);
145 canvas->drawPath(path, paint);
146 canvas->translate(0, 160);
147 }
148}
149
150///////////////////////////////////////////////////////////////////////////////
151
153
154struct GradData {
155 int fCount;
156 const SkColor* fColors;
157 const SkScalar* fPos;
158};
159
163static const SkScalar gPos0[] = { 0, SK_Scalar1 };
164static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
165static const SkScalar gPos2[] = {
167};
168
169static const GradData gGradData[] = {
170{ 2, gColors, nullptr },
171{ 2, gColors, gPos0 },
172{ 2, gColors, gPos1 },
173{ 5, gColors, nullptr },
174{ 5, gColors, gPos2 }
175};
176
177static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkTileMode tm) {
178 return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm);
179}
180
181static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkTileMode tm) {
183 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
184 SkScalarAve(pts[0].fY, pts[1].fY));
185 return SkGradientShader::MakeRadial(center, center.fX, data.fColors,
186 data.fPos, data.fCount, tm);
187}
188
189static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkTileMode tm) {
191 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
192 SkScalarAve(pts[0].fY, pts[1].fY));
193 return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount);
194}
195
196static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, SkTileMode tm) {
197 SkPoint center0, center1;
198 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
199 SkScalarAve(pts[0].fY, pts[1].fY));
200 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
201 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
202 return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7,
203 center0, (pts[1].fX - pts[0].fX) / 2,
204 data.fColors, data.fPos, data.fCount, tm);
205}
206
207typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData&, SkTileMode);
211
212static void gradient_slide(SkCanvas* canvas) {
213 SkPoint pts[2] = {
214 { 0, 0 },
215 { SkIntToScalar(100), SkIntToScalar(100) }
216 };
218 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
220 paint.setAntiAlias(true);
221 paint.setDither(true);
222
223 canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
224 for (size_t i = 0; i < std::size(gGradData); i++) {
225 canvas->save();
226 for (size_t j = 0; j < std::size(gGradMakers); j++) {
227 paint.setShader(gGradMakers[j](pts, gGradData[i], tm));
228 canvas->drawRect(r, paint);
229 canvas->translate(0, SkIntToScalar(120));
230 }
231 canvas->restore();
232 canvas->translate(SkIntToScalar(120), 0);
233 }
234}
235
236///////////////////////////////////////////////////////////////////////////////
237
239#include "src/base/SkRandom.h"
240#include "src/core/SkOSFile.h"
241#include "tools/DecodeUtils.h"
242
244 SkBitmap bm;
245 ToolUtils::GetResourceAsBitmap("images/baby_tux.png", &bm);
246 size->set(bm.width(), bm.height());
248}
249
251 SkPoint pts[] = { { 0, 0 },
252 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
254 return SkGradientShader::MakeLinear(pts, colors, nullptr,
255 std::size(colors), SkTileMode::kMirror);
256}
257
258class Rec {
259public:
264
265 Rec() : fCount(0), fVerts(nullptr), fTexs(nullptr) {}
266 ~Rec() { delete[] fVerts; delete[] fTexs; }
267};
268
269static void make_tris(Rec* rec) {
270 int n = 10;
271 SkRandom rand;
272
274 rec->fCount = n * 3;
275 rec->fVerts = new SkPoint[rec->fCount];
276
277 for (int i = 0; i < n; i++) {
278 SkPoint* v = &rec->fVerts[i*3];
279 for (int j = 0; j < 3; j++) {
280 v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250);
281 }
282 }
283}
284
285static void make_fan(Rec* rec, int texWidth, int texHeight) {
286 const SkScalar tx = SkIntToScalar(texWidth);
287 const SkScalar ty = SkIntToScalar(texHeight);
288 const int n = 24;
289
291 rec->fCount = n + 2;
292 rec->fVerts = new SkPoint[rec->fCount];
293 rec->fTexs = new SkPoint[rec->fCount];
294
295 SkPoint* v = rec->fVerts;
296 SkPoint* t = rec->fTexs;
297
298 v[0].set(0, 0);
299 t[0].set(0, 0);
300 for (int i = 0; i < n; i++) {
301 SkScalar r = SK_ScalarPI * 2 * i / n,
302 sin = SkScalarSin(r),
303 cos = SkScalarCos(r);
304 v[i+1].set(cos, sin);
305 t[i+1].set(i*tx/n, ty);
306 }
307 v[n+1] = v[1];
308 t[n+1].set(tx, ty);
309
310 SkMatrix m;
311 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
312 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
313 m.mapPoints(v, rec->fCount);
314}
315
316static void make_strip(Rec* rec, int texWidth, int texHeight) {
317 const SkScalar tx = SkIntToScalar(texWidth);
318 const SkScalar ty = SkIntToScalar(texHeight);
319 const int n = 24;
320
322 rec->fCount = 2 * (n + 1);
323 rec->fVerts = new SkPoint[rec->fCount];
324 rec->fTexs = new SkPoint[rec->fCount];
325
326 SkPoint* v = rec->fVerts;
327 SkPoint* t = rec->fTexs;
328
329 for (int i = 0; i < n; i++) {
330 SkScalar r = SK_ScalarPI * 2 * i / n,
331 sin = SkScalarSin(r),
332 cos = SkScalarCos(r);
333 v[i*2 + 0].set(cos/2, sin/2);
334 v[i*2 + 1].set(cos, sin);
335
336 t[i*2 + 0].set(tx * i / n, ty);
337 t[i*2 + 1].set(tx * i / n, 0);
338 }
339 v[2*n + 0] = v[0];
340 v[2*n + 1] = v[1];
341
342 t[2*n + 0].set(tx, ty);
343 t[2*n + 1].set(tx, 0);
344
345 SkMatrix m;
346 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
347 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
348 m.mapPoints(v, rec->fCount);
349}
350
351static void mesh_slide(SkCanvas* canvas) {
352 Rec fRecs[3];
353 SkIPoint size;
354
355 auto fShader0 = make_shader0(&size);
356 auto fShader1 = make_shader1(size);
357
358 make_strip(&fRecs[0], size.fX, size.fY);
359 make_fan(&fRecs[1], size.fX, size.fY);
360 make_tris(&fRecs[2]);
361
363 paint.setDither(true);
364
365 for (size_t i = 0; i < std::size(fRecs); i++) {
366 auto verts = SkVertices::MakeCopy(fRecs[i].fMode, fRecs[i].fCount,
367 fRecs[i].fVerts, fRecs[i].fTexs, nullptr);
368 canvas->save();
369
370 paint.setShader(nullptr);
372
373 canvas->translate(SkIntToScalar(210), 0);
374
375 paint.setShader(fShader0);
377
378 canvas->translate(SkIntToScalar(210), 0);
379
380 paint.setShader(fShader1);
382 canvas->restore();
383
384 canvas->translate(0, SkIntToScalar(250));
385 }
386}
387
388///////////////////////////////////////////////////////////////////////////////
389
391
392///////////////////////////////////////////////////////////////////////////////
393
394
395static const SlideProc gProc[] = {
399};
400
402 int fIndex;
403
404public:
405 SlidesSlide() { fName = "Slides"; }
406
407 void load(SkScalar w, SkScalar h) override {
408 fIndex = 0;
409
410 SkBitmap bm;
411 bm.allocN32Pixels(1024, 768);
412 SkCanvas canvas(bm);
413 SkScalar s = SkIntToScalar(1024) / 640;
414 canvas.scale(s, s);
415 for (size_t i = 0; i < std::size(gProc); i++) {
416 canvas.save();
417 canvas.drawColor(BG_COLOR);
418 gProc[i](&canvas);
419 canvas.restore();
420 SkString str;
421 str.printf("/skimages/slide_%zu.png", i);
423 }
424 }
425
426 void draw(SkCanvas* canvas) override {
427 gProc[fIndex](canvas);
428 }
429
430protected:
432 fIndex = (fIndex + 1) % std::size(gProc);
433 return nullptr;
434 }
435
436 bool onClick(ClickHandlerSlide::Click *) override { return false; }
437};
438
439//////////////////////////////////////////////////////////////////////////////
440
441DEF_SLIDE( return new SlidesSlide(); )
@ kModulate
r = s*d
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
sk_sp< T > sk_ref_sp(T *obj)
Definition SkRefCnt.h:381
#define SkScalarSin(radians)
Definition SkScalar.h:45
#define SK_Scalar1
Definition SkScalar.h:18
#define SkScalarAve(a, b)
Definition SkScalar.h:74
#define SkIntToScalar(x)
Definition SkScalar.h:57
#define SkScalarCos(radians)
Definition SkScalar.h:46
static SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t)
Definition SkScalar.h:131
#define SK_ScalarPI
Definition SkScalar.h:21
SkTileMode
Definition SkTileMode.h:13
#define DEF_SLIDE(code)
Definition Slide.h:25
static void fill_pe(SkPaint *paint)
sk_sp< SkShader >(* GradMaker)(const SkPoint pts[2], const GradData &, SkTileMode)
static const GradData gGradData[]
static void one_d_pe(SkPaint *paint)
void(* PE_Proc)(SkPaint *)
static const SlideProc gProc[]
static sk_sp< SkShader > Make2Conical(const SkPoint pts[2], const GradData &data, SkTileMode tm)
static void hair_pe(SkPaint *paint)
static const GradMaker gGradMakers[]
static void tile_pe(SkPaint *paint)
static void mesh_slide(SkCanvas *canvas)
static sk_sp< SkShader > MakeSweep(const SkPoint pts[2], const GradData &data, SkTileMode tm)
static const SkScalar gPos2[]
static void stroke_pe(SkPaint *paint)
static void discrete_pe(SkPaint *paint)
void(* SlideProc)(SkCanvas *)
static void compose_pe(SkPaint *paint)
static void dash_pe(SkPaint *paint)
static sk_sp< SkShader > make_shader0(SkIPoint *size)
static sk_sp< SkPathEffect > MakeTileEffect()
static void make_strip(Rec *rec, int texWidth, int texHeight)
static const int gXY[]
static const PE_Proc gPE2[]
static void gradient_slide(SkCanvas *canvas)
static const SkScalar gPos1[]
static void make_fan(Rec *rec, int texWidth, int texHeight)
static const SkScalar gPos0[]
#define BG_COLOR
static sk_sp< SkShader > make_shader1(const SkIPoint &size)
static void make_tris(Rec *rec)
static const SkColor gColors[]
static void patheffect_slide(SkCanvas *canvas)
static const PE_Proc gPE[]
static void hair2_pe(SkPaint *paint)
static SkScalar center(float pos0, float pos1)
static sk_sp< SkShader > MakeRadial()
int width() const
Definition SkBitmap.h:149
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition SkBitmap.cpp:669
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition SkBitmap.cpp:232
int height() const
Definition SkBitmap.h:158
void drawRect(const SkRect &rect, const SkPaint &paint)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition SkCanvas.h:1182
int save()
Definition SkCanvas.cpp:451
void drawPath(const SkPath &path, const SkPaint &paint)
void scale(SkScalar sx, SkScalar sy)
void drawVertices(const SkVertices *vertices, SkBlendMode mode, const SkPaint &paint)
static sk_sp< SkPathEffect > Make(SkScalar radius)
static sk_sp< SkPathEffect > Make(const SkScalar intervals[], int count, SkScalar phase)
static sk_sp< SkPathEffect > Make(SkScalar segLength, SkScalar dev, uint32_t seedAssist=0)
static sk_sp< SkShader > MakeTwoPointConical(const SkPoint &start, SkScalar startRadius, const SkPoint &end, SkScalar endRadius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< SkShader > MakeSweep(SkScalar cx, SkScalar cy, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, SkScalar startAngle, SkScalar endAngle, uint32_t flags, const SkMatrix *localMatrix)
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
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)
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193
static sk_sp< SkPathEffect > Make(const SkPath &path, SkScalar advance, SkScalar phase, Style)
static sk_sp< SkPathEffect > Make(const SkMatrix &matrix, const SkPath &path)
static sk_sp< SkPathEffect > MakeCompose(sk_sp< SkPathEffect > outer, sk_sp< SkPathEffect > inner)
SkScalar nextUScalar1()
Definition SkRandom.h:101
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition SkString.cpp:534
const char * c_str() const
Definition SkString.h:133
static sk_sp< SkVertices > MakeCopy(VertexMode mode, int vertexCount, const SkPoint positions[], const SkPoint texs[], const SkColor colors[], int indexCount, const uint16_t indices[])
@ kTriangleStrip_VertexMode
Definition SkVertices.h:32
@ kTriangleFan_VertexMode
Definition SkVertices.h:33
@ kTriangles_VertexMode
Definition SkVertices.h:31
SkString fName
Definition Slide.h:54
void load(SkScalar w, SkScalar h) override
void draw(SkCanvas *canvas) override
bool onClick(ClickHandlerSlide::Click *) override
Click * onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override
const Paint & paint
static sk_sp< SkShader > MakeLinear()
float SkScalar
Definition extension.cpp:12
struct MyStruct s
double y
double x
bool EncodeImageToPngFile(const char *path, const SkBitmap &src)
bool GetResourceAsBitmap(const char *resource, SkBitmap *dst)
Definition DecodeUtils.h:21
ModifierKey
Definition ModifierKey.h:9
SkScalar w
SkScalar h
const Scalar scale
const SkColor * fColors
const SkScalar * fPos
int fCount
SkVertices::VertexMode fMode
SkPoint * fTexs
SkPoint * fVerts
void set(float x, float y)
void inset(float dx, float dy)
Definition SkRect.h:1060
SkBlendMode fMode
Definition xfermodes.cpp:52