3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
8 #include "SampleCode.h"
9 #include "SkBlurMask.h"
10 #include "SkBlurMaskFilter.h"
13 #include "SkReadBuffer.h"
14 #include "SkWriteBuffer.h"
15 #include "SkGradientShader.h"
16 #include "SkLayerRasterizer.h"
20 #define BG_COLOR 0xFFDDDDDD
22 typedef void (*SlideProc)(SkCanvas*);
24 ///////////////////////////////////////////////////////////////////////////////
26 #include "Sk1DPathEffect.h"
27 #include "Sk2DPathEffect.h"
28 #include "SkCornerPathEffect.h"
29 #include "SkDashPathEffect.h"
30 #include "SkDiscretePathEffect.h"
32 static void compose_pe(SkPaint* paint) {
33 SkPathEffect* pe = paint->getPathEffect();
34 SkPathEffect* corner = SkCornerPathEffect::Create(25);
35 SkPathEffect* compose;
37 compose = SkComposePathEffect::Create(pe, corner);
42 paint->setPathEffect(compose)->unref();
45 static void hair_pe(SkPaint* paint) {
46 paint->setStrokeWidth(0);
49 static void hair2_pe(SkPaint* paint) {
50 paint->setStrokeWidth(0);
54 static void stroke_pe(SkPaint* paint) {
55 paint->setStrokeWidth(12);
59 static void dash_pe(SkPaint* paint) {
60 SkScalar inter[] = { 20, 10, 10, 10 };
61 paint->setStrokeWidth(12);
62 paint->setPathEffect(SkDashPathEffect::Create(inter, SK_ARRAY_COUNT(inter),
67 static const int gXY[] = {
68 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
71 static void scale(SkPath* path, SkScalar scale) {
73 m.setScale(scale, scale);
77 static void one_d_pe(SkPaint* paint) {
79 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
80 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
81 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
83 path.offset(SkIntToScalar(-6), 0);
86 paint->setPathEffect(SkPath1DPathEffect::Create(path, SkIntToScalar(21), 0,
87 SkPath1DPathEffect::kRotate_Style))->unref();
91 typedef void (*PE_Proc)(SkPaint*);
92 static const PE_Proc gPE[] = { hair_pe, hair2_pe, stroke_pe, dash_pe, one_d_pe };
94 static void fill_pe(SkPaint* paint) {
95 paint->setStyle(SkPaint::kFill_Style);
96 paint->setPathEffect(NULL);
99 static void discrete_pe(SkPaint* paint) {
100 paint->setPathEffect(SkDiscretePathEffect::Create(10, 4))->unref();
103 static SkPathEffect* MakeTileEffect() {
105 m.setScale(SkIntToScalar(12), SkIntToScalar(12));
108 path.addCircle(0, 0, SkIntToScalar(5));
110 return SkPath2DPathEffect::Create(m, path);
113 static void tile_pe(SkPaint* paint) {
114 paint->setPathEffect(MakeTileEffect())->unref();
117 static const PE_Proc gPE2[] = { fill_pe, discrete_pe, tile_pe };
119 static void patheffect_slide(SkCanvas* canvas) {
121 paint.setAntiAlias(true);
122 paint.setStyle(SkPaint::kStroke_Style);
126 path.lineTo(70, 120);
127 path.lineTo(120, 30);
128 path.lineTo(170, 80);
129 path.lineTo(240, 50);
133 for (i = 0; i < SK_ARRAY_COUNT(gPE); i++) {
135 canvas->drawPath(path, paint);
136 canvas->translate(0, 75);
141 SkRect r = { 0, 0, 250, 120 };
142 path.addOval(r, SkPath::kCW_Direction);
144 path.addRect(r, SkPath::kCCW_Direction);
146 canvas->translate(320, 20);
147 for (i = 0; i < SK_ARRAY_COUNT(gPE2); i++) {
149 canvas->drawPath(path, paint);
150 canvas->translate(0, 160);
154 ///////////////////////////////////////////////////////////////////////////////
156 #include "SkGradientShader.h"
160 const SkColor* fColors;
161 const SkScalar* fPos;
164 static const SkColor gColors[] = {
165 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
167 static const SkScalar gPos0[] = { 0, SK_Scalar1 };
168 static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
169 static const SkScalar gPos2[] = {
170 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1
173 static const GradData gGradData[] = {
174 { 2, gColors, NULL },
175 { 2, gColors, gPos0 },
176 { 2, gColors, gPos1 },
177 { 5, gColors, NULL },
178 { 5, gColors, gPos2 }
181 static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
182 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, data.fCount, tm);
185 static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
187 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
188 SkScalarAve(pts[0].fY, pts[1].fY));
189 return SkGradientShader::CreateRadial(center, center.fX, data.fColors,
190 data.fPos, data.fCount, tm);
193 static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
195 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
196 SkScalarAve(pts[0].fY, pts[1].fY));
197 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount);
200 static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
201 SkPoint center0, center1;
202 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
203 SkScalarAve(pts[0].fY, pts[1].fY));
204 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
205 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
206 return SkGradientShader::CreateTwoPointRadial(
207 center1, (pts[1].fX - pts[0].fX) / 7,
208 center0, (pts[1].fX - pts[0].fX) / 2,
209 data.fColors, data.fPos, data.fCount, tm);
212 typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm);
213 static const GradMaker gGradMakers[] = {
214 MakeLinear, MakeRadial, MakeSweep, Make2Radial
217 static void gradient_slide(SkCanvas* canvas) {
220 { SkIntToScalar(100), SkIntToScalar(100) }
222 SkShader::TileMode tm = SkShader::kClamp_TileMode;
223 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
225 paint.setAntiAlias(true);
226 paint.setDither(true);
228 canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
229 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
231 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
232 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm);
233 paint.setShader(shader);
234 canvas->drawRect(r, paint);
236 canvas->translate(0, SkIntToScalar(120));
239 canvas->translate(SkIntToScalar(120), 0);
243 ///////////////////////////////////////////////////////////////////////////////
245 #include "SkPathMeasure.h"
247 static SkScalar getpathlen(const SkPath& path) {
248 SkPathMeasure meas(path, false);
249 return meas.getLength();
252 static void textonpath_slide(SkCanvas* canvas) {
253 const char* text = "Displacement";
254 size_t len =strlen(text);
256 path.moveTo(100, 300);
257 path.quadTo(300, 100, 500, 300);
258 path.offset(0, -100);
261 paint.setAntiAlias(true);
262 paint.setTextSize(40);
264 paint.setStyle(SkPaint::kStroke_Style);
265 canvas->drawPath(path, paint);
266 paint.setStyle(SkPaint::kFill_Style);
269 paint.setColor(0xFF008800);
270 canvas->drawTextOnPathHV(text, len, path,
271 x, paint.getTextSize()*2/3, paint);
272 paint.setColor(SK_ColorRED);
273 canvas->drawTextOnPathHV(text, len, path,
275 paint.setColor(SK_ColorBLUE);
276 canvas->drawTextOnPathHV(text, len, path,
277 x + 120, -paint.getTextSize()*2/3, paint);
280 paint.setTextAlign(SkPaint::kRight_Align);
284 SkScalar pathLen = getpathlen(path);
287 paint.setColor(SK_ColorBLACK);
288 paint.setStyle(SkPaint::kStroke_Style);
289 canvas->drawPath(path, paint);
290 paint.setStyle(SkPaint::kFill_Style);
292 paint.setTextSize(50);
293 canvas->drawTextOnPath(text, len, path, NULL, paint);
295 paint.setColor(SK_ColorRED);
296 matrix.setScale(-SK_Scalar1, SK_Scalar1);
297 matrix.postTranslate(pathLen, 0);
298 canvas->drawTextOnPath(text, len, path, &matrix, paint);
300 paint.setColor(SK_ColorBLUE);
301 matrix.setScale(SK_Scalar1, -SK_Scalar1);
302 canvas->drawTextOnPath(text, len, path, &matrix, paint);
304 paint.setColor(0xFF008800);
305 matrix.setScale(-SK_Scalar1, -SK_Scalar1);
306 matrix.postTranslate(pathLen, 0);
307 canvas->drawTextOnPath(text, len, path, &matrix, paint);
310 ///////////////////////////////////////////////////////////////////////////////
312 #include "SkImageDecoder.h"
313 #include "SkOSFile.h"
314 #include "SkRandom.h"
315 #include "SkStream.h"
317 static SkShader* make_shader0(SkIPoint* size) {
320 SkImageDecoder::DecodeFile("/skimages/logo.gif", &bm);
321 size->set(bm.width(), bm.height());
322 return SkShader::CreateBitmapShader(bm, SkShader::kClamp_TileMode,
323 SkShader::kClamp_TileMode);
326 static SkShader* make_shader1(const SkIPoint& size) {
327 SkPoint pts[] = { { 0, 0 },
328 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
329 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
330 return SkGradientShader::CreateLinear(pts, colors, NULL,
331 SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode);
336 SkCanvas::VertexMode fMode;
341 Rec() : fCount(0), fVerts(NULL), fTexs(NULL) {}
342 ~Rec() { delete[] fVerts; delete[] fTexs; }
345 static void make_tris(Rec* rec) {
349 rec->fMode = SkCanvas::kTriangles_VertexMode;
351 rec->fVerts = new SkPoint[rec->fCount];
353 for (int i = 0; i < n; i++) {
354 SkPoint* v = &rec->fVerts[i*3];
355 for (int j = 0; j < 3; j++) {
356 v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250);
361 static void make_fan(Rec* rec, int texWidth, int texHeight) {
362 const SkScalar tx = SkIntToScalar(texWidth);
363 const SkScalar ty = SkIntToScalar(texHeight);
366 rec->fMode = SkCanvas::kTriangleFan_VertexMode;
368 rec->fVerts = new SkPoint[rec->fCount];
369 rec->fTexs = new SkPoint[rec->fCount];
371 SkPoint* v = rec->fVerts;
372 SkPoint* t = rec->fTexs;
376 for (int i = 0; i < n; i++) {
378 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
379 v[i+1].set(cos, sin);
380 t[i+1].set(i*tx/n, ty);
386 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
387 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
388 m.mapPoints(v, rec->fCount);
391 static void make_strip(Rec* rec, int texWidth, int texHeight) {
392 const SkScalar tx = SkIntToScalar(texWidth);
393 const SkScalar ty = SkIntToScalar(texHeight);
396 rec->fMode = SkCanvas::kTriangleStrip_VertexMode;
397 rec->fCount = 2 * (n + 1);
398 rec->fVerts = new SkPoint[rec->fCount];
399 rec->fTexs = new SkPoint[rec->fCount];
401 SkPoint* v = rec->fVerts;
402 SkPoint* t = rec->fTexs;
404 for (int i = 0; i < n; i++) {
406 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
407 v[i*2 + 0].set(cos/2, sin/2);
408 v[i*2 + 1].set(cos, sin);
410 t[i*2 + 0].set(tx * i / n, ty);
411 t[i*2 + 1].set(tx * i / n, 0);
416 t[2*n + 0].set(tx, ty);
417 t[2*n + 1].set(tx, 0);
420 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
421 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
422 m.mapPoints(v, rec->fCount);
425 static void mesh_slide(SkCanvas* canvas) {
429 SkShader* fShader0 = make_shader0(&size);
430 SkShader* fShader1 = make_shader1(size);
432 SkAutoUnref aur0(fShader0);
433 SkAutoUnref aur1(fShader1);
435 make_strip(&fRecs[0], size.fX, size.fY);
436 make_fan(&fRecs[1], size.fX, size.fY);
437 make_tris(&fRecs[2]);
440 paint.setDither(true);
441 paint.setFilterQuality(kLow_SkFilterQuality);
443 for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) {
446 paint.setShader(NULL);
447 canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount,
448 fRecs[i].fVerts, fRecs[i].fTexs,
449 NULL, NULL, NULL, 0, paint);
451 canvas->translate(SkIntToScalar(210), 0);
453 paint.setShader(fShader0);
454 canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount,
455 fRecs[i].fVerts, fRecs[i].fTexs,
456 NULL, NULL, NULL, 0, paint);
458 canvas->translate(SkIntToScalar(210), 0);
460 paint.setShader(fShader1);
461 canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount,
462 fRecs[i].fVerts, fRecs[i].fTexs,
463 NULL, NULL, NULL, 0, paint);
466 canvas->translate(0, SkIntToScalar(250));
470 ///////////////////////////////////////////////////////////////////////////////
472 static void r0(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
474 p.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
475 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3))))->unref();
476 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
478 p.setMaskFilter(NULL);
479 p.setStyle(SkPaint::kStroke_Style);
480 p.setStrokeWidth(SK_Scalar1);
481 rastBuilder->addLayer(p);
484 p.setStyle(SkPaint::kFill_Style);
485 p.setXfermodeMode(SkXfermode::kSrc_Mode);
486 rastBuilder->addLayer(p);
489 static void r1(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
491 rastBuilder->addLayer(p);
494 p.setXfermodeMode(SkXfermode::kSrc_Mode);
495 p.setStyle(SkPaint::kStroke_Style);
496 p.setStrokeWidth(SK_Scalar1*2);
497 rastBuilder->addLayer(p);
500 static void r2(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
502 p.setStyle(SkPaint::kStrokeAndFill_Style);
503 p.setStrokeWidth(SK_Scalar1*4);
504 rastBuilder->addLayer(p);
506 p.setStyle(SkPaint::kStroke_Style);
507 p.setStrokeWidth(SK_Scalar1*3/2);
508 p.setXfermodeMode(SkXfermode::kClear_Mode);
509 rastBuilder->addLayer(p);
512 static void r3(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
514 p.setStyle(SkPaint::kStroke_Style);
515 p.setStrokeWidth(SK_Scalar1*3);
516 rastBuilder->addLayer(p);
519 p.setStyle(SkPaint::kFill_Style);
520 p.setXfermodeMode(SkXfermode::kSrc_Mode);
521 rastBuilder->addLayer(p);
524 static void r4(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
527 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
530 p.setXfermodeMode(SkXfermode::kClear_Mode);
531 rastBuilder->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
534 rastBuilder->addLayer(p);
537 #include "SkDiscretePathEffect.h"
539 static void r5(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
541 rastBuilder->addLayer(p);
543 p.setPathEffect(SkDiscretePathEffect::Create(SK_Scalar1*4, SK_Scalar1*3))->unref();
544 p.setXfermodeMode(SkXfermode::kSrcOut_Mode);
545 rastBuilder->addLayer(p);
548 static void r6(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
550 rastBuilder->addLayer(p);
552 p.setAntiAlias(false);
553 SkLayerRasterizer::Builder rastBuilder2;
554 r5(&rastBuilder2, p);
555 p.setRasterizer(rastBuilder2.detachRasterizer())->unref();
556 p.setXfermodeMode(SkXfermode::kClear_Mode);
557 rastBuilder->addLayer(p);
560 #include "Sk2DPathEffect.h"
562 static SkPathEffect* MakeDotEffect(SkScalar radius, const SkMatrix& matrix) {
564 path.addCircle(0, 0, radius);
565 return SkPath2DPathEffect::Create(matrix, path);
568 static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
571 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
572 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
573 p.setPathEffect(MakeDotEffect(SK_Scalar1*4, lattice))->unref();
574 rastBuilder->addLayer(p);
577 static void r8(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
579 rastBuilder->addLayer(p);
582 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
583 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
584 p.setPathEffect(MakeDotEffect(SK_Scalar1*2, lattice))->unref();
585 p.setXfermodeMode(SkXfermode::kClear_Mode);
586 rastBuilder->addLayer(p);
588 p.setPathEffect(NULL);
590 p.setStyle(SkPaint::kStroke_Style);
591 p.setStrokeWidth(SK_Scalar1);
592 rastBuilder->addLayer(p);
595 static void r9(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
597 rastBuilder->addLayer(p);
600 lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
601 lattice.postRotate(SkIntToScalar(30), 0, 0);
602 p.setPathEffect(SkLine2DPathEffect::Create(SK_Scalar1*2, lattice))->unref();
603 p.setXfermodeMode(SkXfermode::kClear_Mode);
604 rastBuilder->addLayer(p);
606 p.setPathEffect(NULL);
608 p.setStyle(SkPaint::kStroke_Style);
609 p.setStrokeWidth(SK_Scalar1);
610 rastBuilder->addLayer(p);
613 typedef void (*raster_proc)(SkLayerRasterizer::Builder*, SkPaint&);
615 static const raster_proc gRastProcs[] = {
616 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
619 static void apply_shader(SkPaint* paint, int index) {
620 raster_proc proc = gRastProcs[index];
622 SkLayerRasterizer::Builder rastBuilder;
624 p.setAntiAlias(true);
625 proc(&rastBuilder, p);
626 paint->setRasterizer(rastBuilder.detachRasterizer())->unref();
627 paint->setColor(SK_ColorBLUE);
630 #include "SkTypeface.h"
632 static void texteffect_slide(SkCanvas* canvas) {
633 const char* str = "Google";
634 size_t len = strlen(str);
638 paint.setTypeface(SkTypeface::CreateFromName("Georgia", SkTypeface::kItalic));
639 paint.setTextSize(75);
640 paint.setAntiAlias(true);
641 paint.setColor(SK_ColorBLUE);
642 for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) {
643 apply_shader(&paint, (int)i);
644 canvas->drawText(str, len, x, y, paint);
653 ///////////////////////////////////////////////////////////////////////////////
655 #include "SkImageEncoder.h"
657 static const SlideProc gProc[] = {
665 class SlideView : public SampleView {
682 bm.allocN32Pixels(1024, 768);
684 SkScalar s = SkIntToScalar(1024) / 640;
686 for (size_t i = 0; i < SK_ARRAY_COUNT(gProc); i++) {
688 canvas.drawColor(BG_COLOR);
692 str.printf("/skimages/slide_" SK_SIZE_T_SPECIFIER ".png", i);
693 SkImageEncoder::EncodeFile(str.c_str(), bm, SkImageEncoder::kPNG_Type, 100);
695 this->setBGColor(BG_COLOR);
699 // overrides from SkEventSink
700 bool onQuery(SkEvent* evt) override {
701 if (SampleCode::TitleQ(*evt)) {
702 SampleCode::TitleR(evt, "Slides");
705 return this->INHERITED::onQuery(evt);
708 void onDrawContent(SkCanvas* canvas) override {
710 gProc[fIndex](canvas);
713 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override {
715 fIndex = (fIndex + 1) % SK_ARRAY_COUNT(gProc);
721 typedef SampleView INHERITED;
724 //////////////////////////////////////////////////////////////////////////////
726 static SkView* MyFactory() { return new SlideView; }
727 static SkViewRegister reg(MyFactory);