blob: 9b72afb72678e221ec291e35beed6cd56ea139a5 [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.com63e34c62009-10-06 21:19:18 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkDevice.h"
12#include "SkPaint.h"
13
reed@android.com8af96022009-10-12 17:02:22 +000014#define BG_COLOR 0xFFDDDDDD
15
reed@android.com63e34c62009-10-06 21:19:18 +000016typedef void (*SlideProc)(SkCanvas*);
17
18///////////////////////////////////////////////////////////////////////////////
19
20#include "Sk1DPathEffect.h"
21#include "Sk2DPathEffect.h"
22#include "SkCornerPathEffect.h"
23#include "SkDashPathEffect.h"
24#include "SkDiscretePathEffect.h"
25
26static void compose_pe(SkPaint* paint) {
27 SkPathEffect* pe = paint->getPathEffect();
28 SkPathEffect* corner = new SkCornerPathEffect(25);
29 SkPathEffect* compose;
30 if (pe) {
31 compose = new SkComposePathEffect(pe, corner);
32 corner->unref();
33 } else {
34 compose = corner;
35 }
36 paint->setPathEffect(compose)->unref();
37}
38
39static void hair_pe(SkPaint* paint) {
40 paint->setStrokeWidth(0);
41}
42
43static void hair2_pe(SkPaint* paint) {
44 paint->setStrokeWidth(0);
45 compose_pe(paint);
46}
47
48static void stroke_pe(SkPaint* paint) {
49 paint->setStrokeWidth(12);
50 compose_pe(paint);
51}
52
53static void dash_pe(SkPaint* paint) {
54 SkScalar inter[] = { 20, 10, 10, 10 };
55 paint->setStrokeWidth(12);
56 paint->setPathEffect(new SkDashPathEffect(inter, SK_ARRAY_COUNT(inter),
57 0))->unref();
58 compose_pe(paint);
59}
60
61static const int gXY[] = {
624, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
63};
64
65static void scale(SkPath* path, SkScalar scale) {
66 SkMatrix m;
67 m.setScale(scale, scale);
68 path->transform(m);
69}
70
71static void one_d_pe(SkPaint* paint) {
72 SkPath path;
73 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
74 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
75 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
76 path.close();
77 path.offset(SkIntToScalar(-6), 0);
78 scale(&path, 1.5);
79
80 paint->setPathEffect(new SkPath1DPathEffect(path, SkIntToScalar(21), 0,
81 SkPath1DPathEffect::kRotate_Style))->unref();
82 compose_pe(paint);
83}
84
85typedef void (*PE_Proc)(SkPaint*);
86static const PE_Proc gPE[] = { hair_pe, hair2_pe, stroke_pe, dash_pe, one_d_pe };
87
88static void fill_pe(SkPaint* paint) {
89 paint->setStyle(SkPaint::kFill_Style);
90 paint->setPathEffect(NULL);
91}
92
93static void discrete_pe(SkPaint* paint) {
94 paint->setPathEffect(new SkDiscretePathEffect(10, 4))->unref();
95}
96
reed@google.com43e9f202011-08-09 19:01:50 +000097static SkPathEffect* MakeTileEffect() {
98 SkMatrix m;
99 m.setScale(SkIntToScalar(12), SkIntToScalar(12));
reed@android.com63e34c62009-10-06 21:19:18 +0000100
reed@google.com43e9f202011-08-09 19:01:50 +0000101 SkPath path;
102 path.addCircle(0, 0, SkIntToScalar(5));
103
104 return new SkPath2DPathEffect(m, path);
105}
reed@android.com63e34c62009-10-06 21:19:18 +0000106
107static void tile_pe(SkPaint* paint) {
reed@google.com43e9f202011-08-09 19:01:50 +0000108 paint->setPathEffect(MakeTileEffect())->unref();
reed@android.com63e34c62009-10-06 21:19:18 +0000109}
110
111static const PE_Proc gPE2[] = { fill_pe, discrete_pe, tile_pe };
112
113static void patheffect_slide(SkCanvas* canvas) {
114 SkPaint paint;
115 paint.setAntiAlias(true);
116 paint.setStyle(SkPaint::kStroke_Style);
117
118 SkPath path;
119 path.moveTo(20, 20);
120 path.lineTo(70, 120);
121 path.lineTo(120, 30);
122 path.lineTo(170, 80);
123 path.lineTo(240, 50);
124
125 size_t i;
126 canvas->save();
127 for (i = 0; i < SK_ARRAY_COUNT(gPE); i++) {
128 gPE[i](&paint);
129 canvas->drawPath(path, paint);
130 canvas->translate(0, 75);
131 }
132 canvas->restore();
133
134 path.reset();
135 SkRect r = { 0, 0, 250, 120 };
136 path.addOval(r, SkPath::kCW_Direction);
137 r.inset(50, 50);
138 path.addRect(r, SkPath::kCCW_Direction);
139
140 canvas->translate(320, 20);
141 for (i = 0; i < SK_ARRAY_COUNT(gPE2); i++) {
142 gPE2[i](&paint);
143 canvas->drawPath(path, paint);
144 canvas->translate(0, 160);
145 }
146}
147
148///////////////////////////////////////////////////////////////////////////////
149
150#include "SkGradientShader.h"
151
152struct GradData {
153 int fCount;
154 const SkColor* fColors;
155 const SkScalar* fPos;
156};
157
158static const SkColor gColors[] = {
159SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
160};
161static const SkScalar gPos0[] = { 0, SK_Scalar1 };
162static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
163static const SkScalar gPos2[] = {
1640, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1
165};
166
167static const GradData gGradData[] = {
168{ 2, gColors, NULL },
169{ 2, gColors, gPos0 },
170{ 2, gColors, gPos1 },
171{ 5, gColors, NULL },
172{ 5, gColors, gPos2 }
173};
174
175static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data,
176 SkShader::TileMode tm, SkUnitMapper* mapper) {
177 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos,
178 data.fCount, tm, mapper);
179}
180
181static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data,
182 SkShader::TileMode tm, SkUnitMapper* mapper) {
183 SkPoint center;
184 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
185 SkScalarAve(pts[0].fY, pts[1].fY));
186 return SkGradientShader::CreateRadial(center, center.fX, data.fColors,
187 data.fPos, data.fCount, tm, mapper);
188}
189
190static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data,
191 SkShader::TileMode tm, SkUnitMapper* mapper) {
192 SkPoint center;
193 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
194 SkScalarAve(pts[0].fY, pts[1].fY));
195 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors,
196 data.fPos, data.fCount, mapper);
197}
198
199static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data,
200 SkShader::TileMode tm, SkUnitMapper* mapper) {
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, mapper);
210}
211
212typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data,
213 SkShader::TileMode tm, SkUnitMapper* mapper);
214static const GradMaker gGradMakers[] = {
215 MakeLinear, MakeRadial, MakeSweep, Make2Radial
216};
217
218static void gradient_slide(SkCanvas* canvas) {
219 SkPoint pts[2] = {
220 { 0, 0 },
221 { SkIntToScalar(100), SkIntToScalar(100) }
222 };
223 SkShader::TileMode tm = SkShader::kClamp_TileMode;
224 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
225 SkPaint paint;
226 paint.setAntiAlias(true);
227 paint.setDither(true);
228
229 canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
230 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
231 canvas->save();
232 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
233 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL);
234 paint.setShader(shader);
235 canvas->drawRect(r, paint);
236 shader->unref();
237 canvas->translate(0, SkIntToScalar(120));
238 }
239 canvas->restore();
240 canvas->translate(SkIntToScalar(120), 0);
241 }
242}
243
244///////////////////////////////////////////////////////////////////////////////
245
246#include "SkPathMeasure.h"
247
248static SkScalar getpathlen(const SkPath& path) {
249 SkPathMeasure meas(path, false);
250 return meas.getLength();
251}
252
253static void textonpath_slide(SkCanvas* canvas) {
254 const char* text = "Displacement";
255 size_t len =strlen(text);
256 SkPath path;
257 path.moveTo(100, 300);
258 path.quadTo(300, 100, 500, 300);
259 path.offset(0, -100);
260
261 SkPaint paint;
262 paint.setAntiAlias(true);
263 paint.setTextSize(40);
264
265 paint.setStyle(SkPaint::kStroke_Style);
266 canvas->drawPath(path, paint);
267 paint.setStyle(SkPaint::kFill_Style);
268
269 SkScalar x = 50;
270 paint.setColor(0xFF008800);
271 canvas->drawTextOnPathHV(text, len, path,
272 x, paint.getTextSize()*2/3, paint);
273 paint.setColor(SK_ColorRED);
274 canvas->drawTextOnPathHV(text, len, path,
275 x + 60, 0, paint);
276 paint.setColor(SK_ColorBLUE);
277 canvas->drawTextOnPathHV(text, len, path,
278 x + 120, -paint.getTextSize()*2/3, paint);
279
280 path.offset(0, 200);
281 paint.setTextAlign(SkPaint::kRight_Align);
282
283 text = "Matrices";
284 len = strlen(text);
285 SkScalar pathLen = getpathlen(path);
286 SkMatrix matrix;
287
288 paint.setColor(SK_ColorBLACK);
289 paint.setStyle(SkPaint::kStroke_Style);
290 canvas->drawPath(path, paint);
291 paint.setStyle(SkPaint::kFill_Style);
292
293 paint.setTextSize(50);
294 canvas->drawTextOnPath(text, len, path, NULL, paint);
295
296 paint.setColor(SK_ColorRED);
297 matrix.setScale(-SK_Scalar1, SK_Scalar1);
298 matrix.postTranslate(pathLen, 0);
299 canvas->drawTextOnPath(text, len, path, &matrix, paint);
300
301 paint.setColor(SK_ColorBLUE);
302 matrix.setScale(SK_Scalar1, -SK_Scalar1);
303 canvas->drawTextOnPath(text, len, path, &matrix, paint);
304
305 paint.setColor(0xFF008800);
306 matrix.setScale(-SK_Scalar1, -SK_Scalar1);
307 matrix.postTranslate(pathLen, 0);
308 canvas->drawTextOnPath(text, len, path, &matrix, paint);
309}
310
311///////////////////////////////////////////////////////////////////////////////
312
313#include "SkImageDecoder.h"
314#include "SkOSFile.h"
315#include "SkRandom.h"
316#include "SkStream.h"
reed@android.com63e34c62009-10-06 21:19:18 +0000317
318static SkShader* make_shader0(SkIPoint* size) {
319 SkBitmap bm;
320
321 SkImageDecoder::DecodeFile("/skimages/logo.gif", &bm);
322 size->set(bm.width(), bm.height());
323 return SkShader::CreateBitmapShader(bm, SkShader::kClamp_TileMode,
324 SkShader::kClamp_TileMode);
325}
326
327static SkShader* make_shader1(const SkIPoint& size) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000328 SkPoint pts[] = { { 0, 0 },
329 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
reed@android.com63e34c62009-10-06 21:19:18 +0000330 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
331 return SkGradientShader::CreateLinear(pts, colors, NULL,
332 SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode, NULL);
333}
334
reed@android.comf2b98d62010-12-20 18:26:13 +0000335class Rec {
336public:
reed@android.com63e34c62009-10-06 21:19:18 +0000337 SkCanvas::VertexMode fMode;
338 int fCount;
339 SkPoint* fVerts;
340 SkPoint* fTexs;
341
342 Rec() : fCount(0), fVerts(NULL), fTexs(NULL) {}
343 ~Rec() { delete[] fVerts; delete[] fTexs; }
344};
345
346void make_tris(Rec* rec) {
347 int n = 10;
348 SkRandom rand;
349
350 rec->fMode = SkCanvas::kTriangles_VertexMode;
351 rec->fCount = n * 3;
352 rec->fVerts = new SkPoint[rec->fCount];
353
354 for (int i = 0; i < n; i++) {
355 SkPoint* v = &rec->fVerts[i*3];
356 for (int j = 0; j < 3; j++) {
357 v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250);
358 }
359 }
360}
361
362void make_fan(Rec* rec, int texWidth, int texHeight) {
363 const SkScalar tx = SkIntToScalar(texWidth);
364 const SkScalar ty = SkIntToScalar(texHeight);
365 const int n = 24;
366
367 rec->fMode = SkCanvas::kTriangleFan_VertexMode;
368 rec->fCount = n + 2;
369 rec->fVerts = new SkPoint[rec->fCount];
370 rec->fTexs = new SkPoint[rec->fCount];
371
372 SkPoint* v = rec->fVerts;
373 SkPoint* t = rec->fTexs;
374
375 v[0].set(0, 0);
376 t[0].set(0, 0);
377 for (int i = 0; i < n; i++) {
378 SkScalar cos;
379 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
380 v[i+1].set(cos, sin);
381 t[i+1].set(i*tx/n, ty);
382 }
383 v[n+1] = v[1];
384 t[n+1].set(tx, ty);
385
386 SkMatrix m;
387 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
388 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
389 m.mapPoints(v, rec->fCount);
390}
391
392void make_strip(Rec* rec, int texWidth, int texHeight) {
393 const SkScalar tx = SkIntToScalar(texWidth);
394 const SkScalar ty = SkIntToScalar(texHeight);
395 const int n = 24;
396
397 rec->fMode = SkCanvas::kTriangleStrip_VertexMode;
398 rec->fCount = 2 * (n + 1);
399 rec->fVerts = new SkPoint[rec->fCount];
400 rec->fTexs = new SkPoint[rec->fCount];
401
402 SkPoint* v = rec->fVerts;
403 SkPoint* t = rec->fTexs;
404
405 for (int i = 0; i < n; i++) {
406 SkScalar cos;
407 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
408 v[i*2 + 0].set(cos/2, sin/2);
409 v[i*2 + 1].set(cos, sin);
410
411 t[i*2 + 0].set(tx * i / n, ty);
412 t[i*2 + 1].set(tx * i / n, 0);
413 }
414 v[2*n + 0] = v[0];
415 v[2*n + 1] = v[1];
416
417 t[2*n + 0].set(tx, ty);
418 t[2*n + 1].set(tx, 0);
419
420 SkMatrix m;
421 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
422 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
423 m.mapPoints(v, rec->fCount);
424}
425
426static void mesh_slide(SkCanvas* canvas) {
427 Rec fRecs[3];
428 SkIPoint size;
429
430 SkShader* fShader0 = make_shader0(&size);
431 SkShader* fShader1 = make_shader1(size);
reed@android.comf2b98d62010-12-20 18:26:13 +0000432
433 SkAutoUnref aur0(fShader0);
434 SkAutoUnref aur1(fShader1);
435
reed@android.com63e34c62009-10-06 21:19:18 +0000436 make_strip(&fRecs[0], size.fX, size.fY);
437 make_fan(&fRecs[1], size.fX, size.fY);
438 make_tris(&fRecs[2]);
439
reed@android.com63e34c62009-10-06 21:19:18 +0000440 SkPaint paint;
441 paint.setDither(true);
442 paint.setFilterBitmap(true);
443
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000444 for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) {
reed@android.com63e34c62009-10-06 21:19:18 +0000445 canvas->save();
446
447 paint.setShader(NULL);
448 canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount,
449 fRecs[i].fVerts, fRecs[i].fTexs,
450 NULL, NULL, NULL, 0, paint);
451
452 canvas->translate(SkIntToScalar(210), 0);
453
454 paint.setShader(fShader0);
455 canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount,
456 fRecs[i].fVerts, fRecs[i].fTexs,
457 NULL, NULL, NULL, 0, paint);
458
459 canvas->translate(SkIntToScalar(210), 0);
460
461 paint.setShader(fShader1);
462 canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount,
463 fRecs[i].fVerts, fRecs[i].fTexs,
464 NULL, NULL, NULL, 0, paint);
465 canvas->restore();
466
467 canvas->translate(0, SkIntToScalar(250));
468 }
469}
470
471///////////////////////////////////////////////////////////////////////////////
472
reed@android.com8af96022009-10-12 17:02:22 +0000473#include "SkGradientShader.h"
474#include "SkLayerRasterizer.h"
475#include "SkBlurMaskFilter.h"
476
477static void r0(SkLayerRasterizer* rast, SkPaint& p)
478{
479 p.setMaskFilter(SkBlurMaskFilter::Create(SkIntToScalar(3),
480 SkBlurMaskFilter::kNormal_BlurStyle))->unref();
481 rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
482
483 p.setMaskFilter(NULL);
484 p.setStyle(SkPaint::kStroke_Style);
485 p.setStrokeWidth(SK_Scalar1);
486 rast->addLayer(p);
487
488 p.setAlpha(0x11);
489 p.setStyle(SkPaint::kFill_Style);
490 p.setXfermodeMode(SkXfermode::kSrc_Mode);
491 rast->addLayer(p);
492}
493
494static void r1(SkLayerRasterizer* rast, SkPaint& p)
495{
496 rast->addLayer(p);
497
498 p.setAlpha(0x40);
499 p.setXfermodeMode(SkXfermode::kSrc_Mode);
500 p.setStyle(SkPaint::kStroke_Style);
501 p.setStrokeWidth(SK_Scalar1*2);
502 rast->addLayer(p);
503}
504
505static void r2(SkLayerRasterizer* rast, SkPaint& p)
506{
507 p.setStyle(SkPaint::kStrokeAndFill_Style);
508 p.setStrokeWidth(SK_Scalar1*4);
509 rast->addLayer(p);
510
511 p.setStyle(SkPaint::kStroke_Style);
512 p.setStrokeWidth(SK_Scalar1*3/2);
513 p.setXfermodeMode(SkXfermode::kClear_Mode);
514 rast->addLayer(p);
515}
516
517static void r3(SkLayerRasterizer* rast, SkPaint& p)
518{
519 p.setStyle(SkPaint::kStroke_Style);
520 p.setStrokeWidth(SK_Scalar1*3);
521 rast->addLayer(p);
522
523 p.setAlpha(0x20);
524 p.setStyle(SkPaint::kFill_Style);
525 p.setXfermodeMode(SkXfermode::kSrc_Mode);
526 rast->addLayer(p);
527}
528
529static void r4(SkLayerRasterizer* rast, SkPaint& p)
530{
531 p.setAlpha(0x60);
532 rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
533
534 p.setAlpha(0xFF);
535 p.setXfermodeMode(SkXfermode::kClear_Mode);
536 rast->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
537
538 p.setXfermode(NULL);
539 rast->addLayer(p);
540}
541
542#include "SkDiscretePathEffect.h"
543
544static void r5(SkLayerRasterizer* rast, SkPaint& p)
545{
546 rast->addLayer(p);
547
548 p.setPathEffect(new SkDiscretePathEffect(SK_Scalar1*4, SK_Scalar1*3))->unref();
549 p.setXfermodeMode(SkXfermode::kSrcOut_Mode);
550 rast->addLayer(p);
551}
552
553static void r6(SkLayerRasterizer* rast, SkPaint& p)
554{
555 rast->addLayer(p);
556
557 p.setAntiAlias(false);
558 SkLayerRasterizer* rast2 = new SkLayerRasterizer;
559 r5(rast2, p);
560 p.setRasterizer(rast2)->unref();
561 p.setXfermodeMode(SkXfermode::kClear_Mode);
562 rast->addLayer(p);
563}
564
565#include "Sk2DPathEffect.h"
566
reed@google.com18dc4772011-08-09 18:47:40 +0000567static SkPathEffect* MakeDotEffect(SkScalar radius, const SkMatrix& matrix) {
568 SkPath path;
569 path.addCircle(0, 0, radius);
570 return new SkPath2DPathEffect(matrix, path);
571}
reed@android.com8af96022009-10-12 17:02:22 +0000572
573static void r7(SkLayerRasterizer* rast, SkPaint& p)
574{
575 SkMatrix lattice;
576 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
577 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reed@google.com18dc4772011-08-09 18:47:40 +0000578 p.setPathEffect(MakeDotEffect(SK_Scalar1*4, lattice))->unref();
reed@android.com8af96022009-10-12 17:02:22 +0000579 rast->addLayer(p);
580}
581
582static void r8(SkLayerRasterizer* rast, SkPaint& p)
583{
584 rast->addLayer(p);
585
586 SkMatrix lattice;
587 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
588 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reed@google.com18dc4772011-08-09 18:47:40 +0000589 p.setPathEffect(MakeDotEffect(SK_Scalar1*2, lattice))->unref();
reed@android.com8af96022009-10-12 17:02:22 +0000590 p.setXfermodeMode(SkXfermode::kClear_Mode);
591 rast->addLayer(p);
592
593 p.setPathEffect(NULL);
594 p.setXfermode(NULL);
595 p.setStyle(SkPaint::kStroke_Style);
596 p.setStrokeWidth(SK_Scalar1);
597 rast->addLayer(p);
598}
599
600class Line2DPathEffect : public Sk2DPathEffect {
601public:
602 Line2DPathEffect(SkScalar width, const SkMatrix& matrix)
603 : Sk2DPathEffect(matrix), fWidth(width) {}
604
605 virtual bool filterPath(SkPath* dst, const SkPath& src, SkScalar* width)
606 {
607 if (this->INHERITED::filterPath(dst, src, width))
608 {
609 *width = fWidth;
610 return true;
611 }
612 return false;
613 }
614
615 virtual Factory getFactory() { return CreateProc; }
616 virtual void flatten(SkFlattenableWriteBuffer& buffer)
617 {
618 this->INHERITED::flatten(buffer);
619 buffer.writeScalar(fWidth);
620 }
621protected:
622 virtual void nextSpan(int u, int v, int ucount, SkPath* dst)
623 {
624 if (ucount > 1)
625 {
626 SkPoint src[2], dstP[2];
627
628 src[0].set(SkIntToScalar(u) + SK_ScalarHalf,
629 SkIntToScalar(v) + SK_ScalarHalf);
630 src[1].set(SkIntToScalar(u+ucount) + SK_ScalarHalf,
631 SkIntToScalar(v) + SK_ScalarHalf);
632 this->getMatrix().mapPoints(dstP, src, 2);
633
634 dst->moveTo(dstP[0]);
635 dst->lineTo(dstP[1]);
636 }
637 }
638
639 Line2DPathEffect(SkFlattenableReadBuffer& buffer) : Sk2DPathEffect(buffer)
640 {
641 fWidth = buffer.readScalar();
642 }
643
644private:
645 SkScalar fWidth;
646
647 static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer)
648 {
649 return new Line2DPathEffect(buffer);
650 }
651
652 typedef Sk2DPathEffect INHERITED;
653};
654
655static void r9(SkLayerRasterizer* rast, SkPaint& p)
656{
657 rast->addLayer(p);
658
659 SkMatrix lattice;
660 lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
661 lattice.postRotate(SkIntToScalar(30), 0, 0);
662 p.setPathEffect(new Line2DPathEffect(SK_Scalar1*2, lattice))->unref();
663 p.setXfermodeMode(SkXfermode::kClear_Mode);
664 rast->addLayer(p);
665
666 p.setPathEffect(NULL);
667 p.setXfermode(NULL);
668 p.setStyle(SkPaint::kStroke_Style);
669 p.setStrokeWidth(SK_Scalar1);
670 rast->addLayer(p);
671}
672
673typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&);
674
675static const raster_proc gRastProcs[] = {
676 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
677};
678
679static void apply_shader(SkPaint* paint, int index) {
680 raster_proc proc = gRastProcs[index];
681 SkPaint p;
682 SkLayerRasterizer* rast = new SkLayerRasterizer;
683
684 p.setAntiAlias(true);
685 proc(rast, p);
686 paint->setRasterizer(rast)->unref();
687 paint->setColor(SK_ColorBLUE);
688}
689
690#include "SkTypeface.h"
691
692static void texteffect_slide(SkCanvas* canvas) {
693 const char* str = "Google";
694 size_t len = strlen(str);
695 SkScalar x = 20;
696 SkScalar y = 80;
697 SkPaint paint;
698 paint.setTypeface(SkTypeface::CreateFromName("Georgia", SkTypeface::kItalic));
699 paint.setTextSize(75);
700 paint.setAntiAlias(true);
701 paint.setColor(SK_ColorBLUE);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000702 for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) {
reed@android.com8af96022009-10-12 17:02:22 +0000703 apply_shader(&paint, i);
704 canvas->drawText(str, len, x, y, paint);
705 y += 80;
706 if (i == 4) {
707 x += 320;
708 y = 80;
709 }
710 }
711}
712
713///////////////////////////////////////////////////////////////////////////////
714
reed@android.com63e34c62009-10-06 21:19:18 +0000715#include "SkImageEncoder.h"
716
717static const SlideProc gProc[] = {
718 patheffect_slide,
719 gradient_slide,
720 textonpath_slide,
reed@android.com8af96022009-10-12 17:02:22 +0000721 mesh_slide,
722 texteffect_slide
reed@android.com63e34c62009-10-06 21:19:18 +0000723};
724
reed@google.com81e3d7f2011-06-01 12:42:36 +0000725class SlideView : public SampleView {
reed@android.com63e34c62009-10-06 21:19:18 +0000726 int fIndex;
reed@google.com7b185902012-03-12 21:13:48 +0000727 bool fOnce;
reed@android.com63e34c62009-10-06 21:19:18 +0000728public:
729 SlideView() {
reed@google.com7b185902012-03-12 21:13:48 +0000730 fOnce = false;
731 }
732
733 void init() {
734 if (fOnce) {
735 return;
736 }
737 fOnce = true;
738
reed@android.com63e34c62009-10-06 21:19:18 +0000739 fIndex = 0;
740
741 SkBitmap bm;
742 bm.setConfig(SkBitmap::kARGB_8888_Config, 1024, 768);
743 bm.allocPixels();
744 SkCanvas canvas(bm);
745 SkScalar s = SkIntToScalar(1024) / 640;
746 canvas.scale(s, s);
747 for (size_t i = 0; i < SK_ARRAY_COUNT(gProc); i++) {
748 canvas.save();
reed@android.com8af96022009-10-12 17:02:22 +0000749 canvas.drawColor(BG_COLOR);
reed@android.com63e34c62009-10-06 21:19:18 +0000750 gProc[i](&canvas);
751 canvas.restore();
752 SkString str;
753 str.printf("/skimages/slide_%d.png", i);
754 SkImageEncoder::EncodeFile(str.c_str(), bm, SkImageEncoder::kPNG_Type, 100);
755 }
reed@google.com81e3d7f2011-06-01 12:42:36 +0000756 this->setBGColor(BG_COLOR);
reed@android.com63e34c62009-10-06 21:19:18 +0000757 }
758
759protected:
760 // overrides from SkEventSink
761 virtual bool onQuery(SkEvent* evt) {
762 if (SampleCode::TitleQ(*evt)) {
763 SampleCode::TitleR(evt, "Slides");
764 return true;
765 }
766 return this->INHERITED::onQuery(evt);
767 }
768
reed@google.com81e3d7f2011-06-01 12:42:36 +0000769 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com7b185902012-03-12 21:13:48 +0000770 this->init();
reed@android.com63e34c62009-10-06 21:19:18 +0000771 gProc[fIndex](canvas);
772 }
773
774 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
reed@google.com7b185902012-03-12 21:13:48 +0000775 this->init();
reed@android.com63e34c62009-10-06 21:19:18 +0000776 fIndex = (fIndex + 1) % SK_ARRAY_COUNT(gProc);
777 this->inval(NULL);
778 return NULL;
779 }
780
781private:
reed@google.com81e3d7f2011-06-01 12:42:36 +0000782 typedef SampleView INHERITED;
reed@android.com63e34c62009-10-06 21:19:18 +0000783};
784
785//////////////////////////////////////////////////////////////////////////////
786
787static SkView* MyFactory() { return new SlideView; }
788static SkViewRegister reg(MyFactory);
789