• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "include/core/SkCanvas.h"
8 #include "include/core/SkPaint.h"
9 #include "include/core/SkVertices.h"
10 #include "include/effects/SkBlurMaskFilter.h"
11 #include "include/effects/SkGradientShader.h"
12 #include "samplecode/Sample.h"
13 #include "src/core/SkBlurMask.h"
14 #include "src/core/SkReadBuffer.h"
15 #include "src/core/SkWriteBuffer.h"
16 
17 #include "tools/ToolUtils.h"
18 
19 #define BG_COLOR    0xFFDDDDDD
20 
21 typedef void (*SlideProc)(SkCanvas*);
22 
23 ///////////////////////////////////////////////////////////////////////////////
24 
25 #include "include/effects/Sk1DPathEffect.h"
26 #include "include/effects/Sk2DPathEffect.h"
27 #include "include/effects/SkCornerPathEffect.h"
28 #include "include/effects/SkDashPathEffect.h"
29 #include "include/effects/SkDiscretePathEffect.h"
30 
compose_pe(SkPaint * paint)31 static void compose_pe(SkPaint* paint) {
32     SkPathEffect* pe = paint->getPathEffect();
33     sk_sp<SkPathEffect> corner = SkCornerPathEffect::Make(25);
34     sk_sp<SkPathEffect> compose;
35     if (pe) {
36         compose = SkPathEffect::MakeCompose(sk_ref_sp(pe), corner);
37     } else {
38         compose = corner;
39     }
40     paint->setPathEffect(compose);
41 }
42 
hair_pe(SkPaint * paint)43 static void hair_pe(SkPaint* paint) {
44     paint->setStrokeWidth(0);
45 }
46 
hair2_pe(SkPaint * paint)47 static void hair2_pe(SkPaint* paint) {
48     paint->setStrokeWidth(0);
49     compose_pe(paint);
50 }
51 
stroke_pe(SkPaint * paint)52 static void stroke_pe(SkPaint* paint) {
53     paint->setStrokeWidth(12);
54     compose_pe(paint);
55 }
56 
dash_pe(SkPaint * paint)57 static void dash_pe(SkPaint* paint) {
58     SkScalar inter[] = { 20, 10, 10, 10 };
59     paint->setStrokeWidth(12);
60     paint->setPathEffect(SkDashPathEffect::Make(inter, SK_ARRAY_COUNT(inter), 0));
61     compose_pe(paint);
62 }
63 
64 static const int gXY[] = {
65 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
66 };
67 
scale(SkPath * path,SkScalar scale)68 static void scale(SkPath* path, SkScalar scale) {
69     SkMatrix m;
70     m.setScale(scale, scale);
71     path->transform(m);
72 }
73 
one_d_pe(SkPaint * paint)74 static void one_d_pe(SkPaint* paint) {
75     SkPath  path;
76     path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
77     for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
78         path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
79     path.close();
80     path.offset(SkIntToScalar(-6), 0);
81     scale(&path, 1.5f);
82 
83     paint->setPathEffect(SkPath1DPathEffect::Make(path, SkIntToScalar(21), 0,
84                                                   SkPath1DPathEffect::kRotate_Style));
85     compose_pe(paint);
86 }
87 
88 typedef void (*PE_Proc)(SkPaint*);
89 static const PE_Proc gPE[] = { hair_pe, hair2_pe, stroke_pe, dash_pe, one_d_pe };
90 
fill_pe(SkPaint * paint)91 static void fill_pe(SkPaint* paint) {
92     paint->setStyle(SkPaint::kFill_Style);
93     paint->setPathEffect(nullptr);
94 }
95 
discrete_pe(SkPaint * paint)96 static void discrete_pe(SkPaint* paint) {
97     paint->setPathEffect(SkDiscretePathEffect::Make(10, 4));
98 }
99 
MakeTileEffect()100 static sk_sp<SkPathEffect> MakeTileEffect() {
101     SkMatrix m;
102     m.setScale(SkIntToScalar(12), SkIntToScalar(12));
103 
104     SkPath path;
105     path.addCircle(0, 0, SkIntToScalar(5));
106 
107     return SkPath2DPathEffect::Make(m, path);
108 }
109 
tile_pe(SkPaint * paint)110 static void tile_pe(SkPaint* paint) {
111     paint->setPathEffect(MakeTileEffect());
112 }
113 
114 static const PE_Proc gPE2[] = { fill_pe, discrete_pe, tile_pe };
115 
patheffect_slide(SkCanvas * canvas)116 static void patheffect_slide(SkCanvas* canvas) {
117     SkPaint paint;
118     paint.setAntiAlias(true);
119     paint.setStyle(SkPaint::kStroke_Style);
120 
121     SkPath path;
122     path.moveTo(20, 20);
123     path.lineTo(70, 120);
124     path.lineTo(120, 30);
125     path.lineTo(170, 80);
126     path.lineTo(240, 50);
127 
128     size_t i;
129     canvas->save();
130     for (i = 0; i < SK_ARRAY_COUNT(gPE); i++) {
131         gPE[i](&paint);
132         canvas->drawPath(path, paint);
133         canvas->translate(0, 75);
134     }
135     canvas->restore();
136 
137     path.reset();
138     SkRect r = { 0, 0, 250, 120 };
139     path.addOval(r, SkPath::kCW_Direction);
140     r.inset(50, 50);
141     path.addRect(r, SkPath::kCCW_Direction);
142 
143     canvas->translate(320, 20);
144     for (i = 0; i < SK_ARRAY_COUNT(gPE2); i++) {
145         gPE2[i](&paint);
146         canvas->drawPath(path, paint);
147         canvas->translate(0, 160);
148     }
149 }
150 
151 ///////////////////////////////////////////////////////////////////////////////
152 
153 #include "include/effects/SkGradientShader.h"
154 
155 struct GradData {
156     int             fCount;
157     const SkColor*  fColors;
158     const SkScalar* fPos;
159 };
160 
161 static const SkColor gColors[] = {
162 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
163 };
164 static const SkScalar gPos0[] = { 0, SK_Scalar1 };
165 static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
166 static const SkScalar gPos2[] = {
167 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1
168 };
169 
170 static const GradData gGradData[] = {
171 { 2, gColors, nullptr },
172 { 2, gColors, gPos0 },
173 { 2, gColors, gPos1 },
174 { 5, gColors, nullptr },
175 { 5, gColors, gPos2 }
176 };
177 
MakeLinear(const SkPoint pts[2],const GradData & data,SkTileMode tm)178 static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkTileMode tm) {
179     return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm);
180 }
181 
MakeRadial(const SkPoint pts[2],const GradData & data,SkTileMode tm)182 static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkTileMode tm) {
183     SkPoint center;
184     center.set(SkScalarAve(pts[0].fX, pts[1].fX),
185                SkScalarAve(pts[0].fY, pts[1].fY));
186     return SkGradientShader::MakeRadial(center, center.fX, data.fColors,
187                                           data.fPos, data.fCount, tm);
188 }
189 
MakeSweep(const SkPoint pts[2],const GradData & data,SkTileMode tm)190 static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkTileMode tm) {
191     SkPoint center;
192     center.set(SkScalarAve(pts[0].fX, pts[1].fX),
193                SkScalarAve(pts[0].fY, pts[1].fY));
194     return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount);
195 }
196 
Make2Conical(const SkPoint pts[2],const GradData & data,SkTileMode tm)197 static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, SkTileMode tm) {
198     SkPoint center0, center1;
199     center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
200                 SkScalarAve(pts[0].fY, pts[1].fY));
201     center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
202                 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
203     return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7,
204                                                   center0, (pts[1].fX - pts[0].fX) / 2,
205                                                   data.fColors, data.fPos, data.fCount, tm);
206 }
207 
208 typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData&, SkTileMode);
209 static const GradMaker gGradMakers[] = {
210     MakeLinear, MakeRadial, MakeSweep, Make2Conical
211 };
212 
gradient_slide(SkCanvas * canvas)213 static void gradient_slide(SkCanvas* canvas) {
214     SkPoint pts[2] = {
215         { 0, 0 },
216         { SkIntToScalar(100), SkIntToScalar(100) }
217     };
218     SkTileMode tm = SkTileMode::kClamp;
219     SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
220     SkPaint paint;
221     paint.setAntiAlias(true);
222     paint.setDither(true);
223 
224     canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
225     for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
226         canvas->save();
227         for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
228             paint.setShader(gGradMakers[j](pts, gGradData[i], tm));
229             canvas->drawRect(r, paint);
230             canvas->translate(0, SkIntToScalar(120));
231         }
232         canvas->restore();
233         canvas->translate(SkIntToScalar(120), 0);
234     }
235 }
236 
237 ///////////////////////////////////////////////////////////////////////////////
238 
239 #include "include/core/SkStream.h"
240 #include "include/utils/SkRandom.h"
241 #include "samplecode/DecodeFile.h"
242 #include "src/core/SkOSFile.h"
243 
make_shader0(SkIPoint * size)244 static sk_sp<SkShader> make_shader0(SkIPoint* size) {
245     SkBitmap    bm;
246 
247     decode_file("/skimages/logo.gif", &bm);
248     size->set(bm.width(), bm.height());
249     return bm.makeShader();
250 }
251 
make_shader1(const SkIPoint & size)252 static sk_sp<SkShader> make_shader1(const SkIPoint& size) {
253     SkPoint pts[] = { { 0, 0 },
254                       { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
255     SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
256     return SkGradientShader::MakeLinear(pts, colors, nullptr,
257                                           SK_ARRAY_COUNT(colors), SkTileMode::kMirror);
258 }
259 
260 class Rec {
261 public:
262     SkVertices::VertexMode  fMode;
263     int                     fCount;
264     SkPoint*                fVerts;
265     SkPoint*                fTexs;
266 
Rec()267     Rec() : fCount(0), fVerts(nullptr), fTexs(nullptr) {}
~Rec()268     ~Rec() { delete[] fVerts; delete[] fTexs; }
269 };
270 
make_tris(Rec * rec)271 static void make_tris(Rec* rec) {
272     int n = 10;
273     SkRandom    rand;
274 
275     rec->fMode = SkVertices::kTriangles_VertexMode;
276     rec->fCount = n * 3;
277     rec->fVerts = new SkPoint[rec->fCount];
278 
279     for (int i = 0; i < n; i++) {
280         SkPoint* v = &rec->fVerts[i*3];
281         for (int j = 0; j < 3; j++) {
282             v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250);
283         }
284     }
285 }
286 
make_fan(Rec * rec,int texWidth,int texHeight)287 static void make_fan(Rec* rec, int texWidth, int texHeight) {
288     const SkScalar tx = SkIntToScalar(texWidth);
289     const SkScalar ty = SkIntToScalar(texHeight);
290     const int n = 24;
291 
292     rec->fMode = SkVertices::kTriangleFan_VertexMode;
293     rec->fCount = n + 2;
294     rec->fVerts = new SkPoint[rec->fCount];
295     rec->fTexs  = new SkPoint[rec->fCount];
296 
297     SkPoint* v = rec->fVerts;
298     SkPoint* t = rec->fTexs;
299 
300     v[0].set(0, 0);
301     t[0].set(0, 0);
302     for (int i = 0; i < n; i++) {
303         SkScalar r   = SK_ScalarPI * 2 * i / n,
304                  sin = SkScalarSin(r),
305                  cos = SkScalarCos(r);
306         v[i+1].set(cos, sin);
307         t[i+1].set(i*tx/n, ty);
308     }
309     v[n+1] = v[1];
310     t[n+1].set(tx, ty);
311 
312     SkMatrix m;
313     m.setScale(SkIntToScalar(100), SkIntToScalar(100));
314     m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
315     m.mapPoints(v, rec->fCount);
316 }
317 
make_strip(Rec * rec,int texWidth,int texHeight)318 static void make_strip(Rec* rec, int texWidth, int texHeight) {
319     const SkScalar tx = SkIntToScalar(texWidth);
320     const SkScalar ty = SkIntToScalar(texHeight);
321     const int n = 24;
322 
323     rec->fMode = SkVertices::kTriangleStrip_VertexMode;
324     rec->fCount = 2 * (n + 1);
325     rec->fVerts = new SkPoint[rec->fCount];
326     rec->fTexs  = new SkPoint[rec->fCount];
327 
328     SkPoint* v = rec->fVerts;
329     SkPoint* t = rec->fTexs;
330 
331     for (int i = 0; i < n; i++) {
332         SkScalar r   = SK_ScalarPI * 2 * i / n,
333                  sin = SkScalarSin(r),
334                  cos = SkScalarCos(r);
335         v[i*2 + 0].set(cos/2, sin/2);
336         v[i*2 + 1].set(cos, sin);
337 
338         t[i*2 + 0].set(tx * i / n, ty);
339         t[i*2 + 1].set(tx * i / n, 0);
340     }
341     v[2*n + 0] = v[0];
342     v[2*n + 1] = v[1];
343 
344     t[2*n + 0].set(tx, ty);
345     t[2*n + 1].set(tx, 0);
346 
347     SkMatrix m;
348     m.setScale(SkIntToScalar(100), SkIntToScalar(100));
349     m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
350     m.mapPoints(v, rec->fCount);
351 }
352 
mesh_slide(SkCanvas * canvas)353 static void mesh_slide(SkCanvas* canvas) {
354     Rec fRecs[3];
355     SkIPoint    size;
356 
357     auto fShader0 = make_shader0(&size);
358     auto fShader1 = make_shader1(size);
359 
360     make_strip(&fRecs[0], size.fX, size.fY);
361     make_fan(&fRecs[1], size.fX, size.fY);
362     make_tris(&fRecs[2]);
363 
364     SkPaint paint;
365     paint.setDither(true);
366     paint.setFilterQuality(kLow_SkFilterQuality);
367 
368     for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) {
369         auto verts = SkVertices::MakeCopy(fRecs[i].fMode, fRecs[i].fCount,
370                                           fRecs[i].fVerts, fRecs[i].fTexs, nullptr);
371         canvas->save();
372 
373         paint.setShader(nullptr);
374         canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
375 
376         canvas->translate(SkIntToScalar(210), 0);
377 
378         paint.setShader(fShader0);
379         canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
380 
381         canvas->translate(SkIntToScalar(210), 0);
382 
383         paint.setShader(fShader1);
384         canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
385         canvas->restore();
386 
387         canvas->translate(0, SkIntToScalar(250));
388     }
389 }
390 
391 ///////////////////////////////////////////////////////////////////////////////
392 
393 #include "include/core/SkTypeface.h"
394 
395 ///////////////////////////////////////////////////////////////////////////////
396 
397 #include "include/core/SkImageEncoder.h"
398 
399 static const SlideProc gProc[] = {
400     patheffect_slide,
401     gradient_slide,
402     mesh_slide,
403 };
404 
405 class SlideView : public Sample {
406     int fIndex;
407     bool fOnce;
408 public:
SlideView()409     SlideView() {
410         fOnce = false;
411     }
412 
init()413     void init() {
414         if (fOnce) {
415             return;
416         }
417         fOnce = true;
418 
419         fIndex = 0;
420 
421         SkBitmap bm;
422         bm.allocN32Pixels(1024, 768);
423         SkCanvas canvas(bm);
424         SkScalar s = SkIntToScalar(1024) / 640;
425         canvas.scale(s, s);
426         for (size_t i = 0; i < SK_ARRAY_COUNT(gProc); i++) {
427             canvas.save();
428             canvas.drawColor(BG_COLOR);
429             gProc[i](&canvas);
430             canvas.restore();
431             SkString str;
432             str.printf("/skimages/slide_" SK_SIZE_T_SPECIFIER ".png", i);
433             ToolUtils::EncodeImageToFile(str.c_str(), bm, SkEncodedImageFormat::kPNG, 100);
434         }
435         this->setBGColor(BG_COLOR);
436     }
437 
438 protected:
name()439     SkString name() override { return SkString("Slides"); }
440 
onDrawContent(SkCanvas * canvas)441     void onDrawContent(SkCanvas* canvas) override {
442         this->init();
443         gProc[fIndex](canvas);
444     }
445 
onFindClickHandler(SkScalar x,SkScalar y,ModifierKey)446     Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, ModifierKey) override {
447         this->init();
448         fIndex = (fIndex + 1) % SK_ARRAY_COUNT(gProc);
449         return nullptr;
450     }
451 
452 private:
453     typedef Sample INHERITED;
454 };
455 
456 //////////////////////////////////////////////////////////////////////////////
457 
458 DEF_SAMPLE( return new SlideView(); )
459