blob: fc26016a3b1318cd181b9ebef7f5df1986ca58fd [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"
317#include "SkNinePatch.h"
318
319static SkShader* make_shader0(SkIPoint* size) {
320 SkBitmap bm;
321
322 SkImageDecoder::DecodeFile("/skimages/logo.gif", &bm);
323 size->set(bm.width(), bm.height());
324 return SkShader::CreateBitmapShader(bm, SkShader::kClamp_TileMode,
325 SkShader::kClamp_TileMode);
326}
327
328static SkShader* make_shader1(const SkIPoint& size) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000329 SkPoint pts[] = { { 0, 0 },
330 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
reed@android.com63e34c62009-10-06 21:19:18 +0000331 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
332 return SkGradientShader::CreateLinear(pts, colors, NULL,
333 SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode, NULL);
334}
335
reed@android.comf2b98d62010-12-20 18:26:13 +0000336class Rec {
337public:
reed@android.com63e34c62009-10-06 21:19:18 +0000338 SkCanvas::VertexMode fMode;
339 int fCount;
340 SkPoint* fVerts;
341 SkPoint* fTexs;
342
343 Rec() : fCount(0), fVerts(NULL), fTexs(NULL) {}
344 ~Rec() { delete[] fVerts; delete[] fTexs; }
345};
346
347void make_tris(Rec* rec) {
348 int n = 10;
349 SkRandom rand;
350
351 rec->fMode = SkCanvas::kTriangles_VertexMode;
352 rec->fCount = n * 3;
353 rec->fVerts = new SkPoint[rec->fCount];
354
355 for (int i = 0; i < n; i++) {
356 SkPoint* v = &rec->fVerts[i*3];
357 for (int j = 0; j < 3; j++) {
358 v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250);
359 }
360 }
361}
362
363void make_fan(Rec* rec, int texWidth, int texHeight) {
364 const SkScalar tx = SkIntToScalar(texWidth);
365 const SkScalar ty = SkIntToScalar(texHeight);
366 const int n = 24;
367
368 rec->fMode = SkCanvas::kTriangleFan_VertexMode;
369 rec->fCount = n + 2;
370 rec->fVerts = new SkPoint[rec->fCount];
371 rec->fTexs = new SkPoint[rec->fCount];
372
373 SkPoint* v = rec->fVerts;
374 SkPoint* t = rec->fTexs;
375
376 v[0].set(0, 0);
377 t[0].set(0, 0);
378 for (int i = 0; i < n; i++) {
379 SkScalar cos;
380 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
381 v[i+1].set(cos, sin);
382 t[i+1].set(i*tx/n, ty);
383 }
384 v[n+1] = v[1];
385 t[n+1].set(tx, ty);
386
387 SkMatrix m;
388 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
389 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
390 m.mapPoints(v, rec->fCount);
391}
392
393void make_strip(Rec* rec, int texWidth, int texHeight) {
394 const SkScalar tx = SkIntToScalar(texWidth);
395 const SkScalar ty = SkIntToScalar(texHeight);
396 const int n = 24;
397
398 rec->fMode = SkCanvas::kTriangleStrip_VertexMode;
399 rec->fCount = 2 * (n + 1);
400 rec->fVerts = new SkPoint[rec->fCount];
401 rec->fTexs = new SkPoint[rec->fCount];
402
403 SkPoint* v = rec->fVerts;
404 SkPoint* t = rec->fTexs;
405
406 for (int i = 0; i < n; i++) {
407 SkScalar cos;
408 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
409 v[i*2 + 0].set(cos/2, sin/2);
410 v[i*2 + 1].set(cos, sin);
411
412 t[i*2 + 0].set(tx * i / n, ty);
413 t[i*2 + 1].set(tx * i / n, 0);
414 }
415 v[2*n + 0] = v[0];
416 v[2*n + 1] = v[1];
417
418 t[2*n + 0].set(tx, ty);
419 t[2*n + 1].set(tx, 0);
420
421 SkMatrix m;
422 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
423 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
424 m.mapPoints(v, rec->fCount);
425}
426
427static void mesh_slide(SkCanvas* canvas) {
428 Rec fRecs[3];
429 SkIPoint size;
430
431 SkShader* fShader0 = make_shader0(&size);
432 SkShader* fShader1 = make_shader1(size);
reed@android.comf2b98d62010-12-20 18:26:13 +0000433
434 SkAutoUnref aur0(fShader0);
435 SkAutoUnref aur1(fShader1);
436
reed@android.com63e34c62009-10-06 21:19:18 +0000437 make_strip(&fRecs[0], size.fX, size.fY);
438 make_fan(&fRecs[1], size.fX, size.fY);
439 make_tris(&fRecs[2]);
440
reed@android.com63e34c62009-10-06 21:19:18 +0000441 SkPaint paint;
442 paint.setDither(true);
443 paint.setFilterBitmap(true);
444
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000445 for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) {
reed@android.com63e34c62009-10-06 21:19:18 +0000446 canvas->save();
447
448 paint.setShader(NULL);
449 canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount,
450 fRecs[i].fVerts, fRecs[i].fTexs,
451 NULL, NULL, NULL, 0, paint);
452
453 canvas->translate(SkIntToScalar(210), 0);
454
455 paint.setShader(fShader0);
456 canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount,
457 fRecs[i].fVerts, fRecs[i].fTexs,
458 NULL, NULL, NULL, 0, paint);
459
460 canvas->translate(SkIntToScalar(210), 0);
461
462 paint.setShader(fShader1);
463 canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount,
464 fRecs[i].fVerts, fRecs[i].fTexs,
465 NULL, NULL, NULL, 0, paint);
466 canvas->restore();
467
468 canvas->translate(0, SkIntToScalar(250));
469 }
470}
471
472///////////////////////////////////////////////////////////////////////////////
473
reed@android.com8af96022009-10-12 17:02:22 +0000474#include "SkGradientShader.h"
475#include "SkLayerRasterizer.h"
476#include "SkBlurMaskFilter.h"
477
478static void r0(SkLayerRasterizer* rast, SkPaint& p)
479{
480 p.setMaskFilter(SkBlurMaskFilter::Create(SkIntToScalar(3),
481 SkBlurMaskFilter::kNormal_BlurStyle))->unref();
482 rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
483
484 p.setMaskFilter(NULL);
485 p.setStyle(SkPaint::kStroke_Style);
486 p.setStrokeWidth(SK_Scalar1);
487 rast->addLayer(p);
488
489 p.setAlpha(0x11);
490 p.setStyle(SkPaint::kFill_Style);
491 p.setXfermodeMode(SkXfermode::kSrc_Mode);
492 rast->addLayer(p);
493}
494
495static void r1(SkLayerRasterizer* rast, SkPaint& p)
496{
497 rast->addLayer(p);
498
499 p.setAlpha(0x40);
500 p.setXfermodeMode(SkXfermode::kSrc_Mode);
501 p.setStyle(SkPaint::kStroke_Style);
502 p.setStrokeWidth(SK_Scalar1*2);
503 rast->addLayer(p);
504}
505
506static void r2(SkLayerRasterizer* rast, SkPaint& p)
507{
508 p.setStyle(SkPaint::kStrokeAndFill_Style);
509 p.setStrokeWidth(SK_Scalar1*4);
510 rast->addLayer(p);
511
512 p.setStyle(SkPaint::kStroke_Style);
513 p.setStrokeWidth(SK_Scalar1*3/2);
514 p.setXfermodeMode(SkXfermode::kClear_Mode);
515 rast->addLayer(p);
516}
517
518static void r3(SkLayerRasterizer* rast, SkPaint& p)
519{
520 p.setStyle(SkPaint::kStroke_Style);
521 p.setStrokeWidth(SK_Scalar1*3);
522 rast->addLayer(p);
523
524 p.setAlpha(0x20);
525 p.setStyle(SkPaint::kFill_Style);
526 p.setXfermodeMode(SkXfermode::kSrc_Mode);
527 rast->addLayer(p);
528}
529
530static void r4(SkLayerRasterizer* rast, SkPaint& p)
531{
532 p.setAlpha(0x60);
533 rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
534
535 p.setAlpha(0xFF);
536 p.setXfermodeMode(SkXfermode::kClear_Mode);
537 rast->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
538
539 p.setXfermode(NULL);
540 rast->addLayer(p);
541}
542
543#include "SkDiscretePathEffect.h"
544
545static void r5(SkLayerRasterizer* rast, SkPaint& p)
546{
547 rast->addLayer(p);
548
549 p.setPathEffect(new SkDiscretePathEffect(SK_Scalar1*4, SK_Scalar1*3))->unref();
550 p.setXfermodeMode(SkXfermode::kSrcOut_Mode);
551 rast->addLayer(p);
552}
553
554static void r6(SkLayerRasterizer* rast, SkPaint& p)
555{
556 rast->addLayer(p);
557
558 p.setAntiAlias(false);
559 SkLayerRasterizer* rast2 = new SkLayerRasterizer;
560 r5(rast2, p);
561 p.setRasterizer(rast2)->unref();
562 p.setXfermodeMode(SkXfermode::kClear_Mode);
563 rast->addLayer(p);
564}
565
566#include "Sk2DPathEffect.h"
567
reed@google.com18dc4772011-08-09 18:47:40 +0000568static SkPathEffect* MakeDotEffect(SkScalar radius, const SkMatrix& matrix) {
569 SkPath path;
570 path.addCircle(0, 0, radius);
571 return new SkPath2DPathEffect(matrix, path);
572}
reed@android.com8af96022009-10-12 17:02:22 +0000573
574static void r7(SkLayerRasterizer* rast, SkPaint& p)
575{
576 SkMatrix lattice;
577 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
578 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reed@google.com18dc4772011-08-09 18:47:40 +0000579 p.setPathEffect(MakeDotEffect(SK_Scalar1*4, lattice))->unref();
reed@android.com8af96022009-10-12 17:02:22 +0000580 rast->addLayer(p);
581}
582
583static void r8(SkLayerRasterizer* rast, SkPaint& p)
584{
585 rast->addLayer(p);
586
587 SkMatrix lattice;
588 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
589 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reed@google.com18dc4772011-08-09 18:47:40 +0000590 p.setPathEffect(MakeDotEffect(SK_Scalar1*2, lattice))->unref();
reed@android.com8af96022009-10-12 17:02:22 +0000591 p.setXfermodeMode(SkXfermode::kClear_Mode);
592 rast->addLayer(p);
593
594 p.setPathEffect(NULL);
595 p.setXfermode(NULL);
596 p.setStyle(SkPaint::kStroke_Style);
597 p.setStrokeWidth(SK_Scalar1);
598 rast->addLayer(p);
599}
600
601class Line2DPathEffect : public Sk2DPathEffect {
602public:
603 Line2DPathEffect(SkScalar width, const SkMatrix& matrix)
604 : Sk2DPathEffect(matrix), fWidth(width) {}
605
606 virtual bool filterPath(SkPath* dst, const SkPath& src, SkScalar* width)
607 {
608 if (this->INHERITED::filterPath(dst, src, width))
609 {
610 *width = fWidth;
611 return true;
612 }
613 return false;
614 }
615
616 virtual Factory getFactory() { return CreateProc; }
617 virtual void flatten(SkFlattenableWriteBuffer& buffer)
618 {
619 this->INHERITED::flatten(buffer);
620 buffer.writeScalar(fWidth);
621 }
622protected:
623 virtual void nextSpan(int u, int v, int ucount, SkPath* dst)
624 {
625 if (ucount > 1)
626 {
627 SkPoint src[2], dstP[2];
628
629 src[0].set(SkIntToScalar(u) + SK_ScalarHalf,
630 SkIntToScalar(v) + SK_ScalarHalf);
631 src[1].set(SkIntToScalar(u+ucount) + SK_ScalarHalf,
632 SkIntToScalar(v) + SK_ScalarHalf);
633 this->getMatrix().mapPoints(dstP, src, 2);
634
635 dst->moveTo(dstP[0]);
636 dst->lineTo(dstP[1]);
637 }
638 }
639
640 Line2DPathEffect(SkFlattenableReadBuffer& buffer) : Sk2DPathEffect(buffer)
641 {
642 fWidth = buffer.readScalar();
643 }
644
645private:
646 SkScalar fWidth;
647
648 static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer)
649 {
650 return new Line2DPathEffect(buffer);
651 }
652
653 typedef Sk2DPathEffect INHERITED;
654};
655
656static void r9(SkLayerRasterizer* rast, SkPaint& p)
657{
658 rast->addLayer(p);
659
660 SkMatrix lattice;
661 lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
662 lattice.postRotate(SkIntToScalar(30), 0, 0);
663 p.setPathEffect(new Line2DPathEffect(SK_Scalar1*2, lattice))->unref();
664 p.setXfermodeMode(SkXfermode::kClear_Mode);
665 rast->addLayer(p);
666
667 p.setPathEffect(NULL);
668 p.setXfermode(NULL);
669 p.setStyle(SkPaint::kStroke_Style);
670 p.setStrokeWidth(SK_Scalar1);
671 rast->addLayer(p);
672}
673
674typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&);
675
676static const raster_proc gRastProcs[] = {
677 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
678};
679
680static void apply_shader(SkPaint* paint, int index) {
681 raster_proc proc = gRastProcs[index];
682 SkPaint p;
683 SkLayerRasterizer* rast = new SkLayerRasterizer;
684
685 p.setAntiAlias(true);
686 proc(rast, p);
687 paint->setRasterizer(rast)->unref();
688 paint->setColor(SK_ColorBLUE);
689}
690
691#include "SkTypeface.h"
692
693static void texteffect_slide(SkCanvas* canvas) {
694 const char* str = "Google";
695 size_t len = strlen(str);
696 SkScalar x = 20;
697 SkScalar y = 80;
698 SkPaint paint;
699 paint.setTypeface(SkTypeface::CreateFromName("Georgia", SkTypeface::kItalic));
700 paint.setTextSize(75);
701 paint.setAntiAlias(true);
702 paint.setColor(SK_ColorBLUE);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000703 for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) {
reed@android.com8af96022009-10-12 17:02:22 +0000704 apply_shader(&paint, i);
705 canvas->drawText(str, len, x, y, paint);
706 y += 80;
707 if (i == 4) {
708 x += 320;
709 y = 80;
710 }
711 }
712}
713
714///////////////////////////////////////////////////////////////////////////////
715
reed@android.com63e34c62009-10-06 21:19:18 +0000716#include "SkImageEncoder.h"
717
718static const SlideProc gProc[] = {
719 patheffect_slide,
720 gradient_slide,
721 textonpath_slide,
reed@android.com8af96022009-10-12 17:02:22 +0000722 mesh_slide,
723 texteffect_slide
reed@android.com63e34c62009-10-06 21:19:18 +0000724};
725
reed@google.com81e3d7f2011-06-01 12:42:36 +0000726class SlideView : public SampleView {
reed@android.com63e34c62009-10-06 21:19:18 +0000727 int fIndex;
728public:
729 SlideView() {
730 fIndex = 0;
731
732 SkBitmap bm;
733 bm.setConfig(SkBitmap::kARGB_8888_Config, 1024, 768);
734 bm.allocPixels();
735 SkCanvas canvas(bm);
736 SkScalar s = SkIntToScalar(1024) / 640;
737 canvas.scale(s, s);
738 for (size_t i = 0; i < SK_ARRAY_COUNT(gProc); i++) {
739 canvas.save();
reed@android.com8af96022009-10-12 17:02:22 +0000740 canvas.drawColor(BG_COLOR);
reed@android.com63e34c62009-10-06 21:19:18 +0000741 gProc[i](&canvas);
742 canvas.restore();
743 SkString str;
744 str.printf("/skimages/slide_%d.png", i);
745 SkImageEncoder::EncodeFile(str.c_str(), bm, SkImageEncoder::kPNG_Type, 100);
746 }
reed@google.com81e3d7f2011-06-01 12:42:36 +0000747 this->setBGColor(BG_COLOR);
reed@android.com63e34c62009-10-06 21:19:18 +0000748 }
749
750protected:
751 // overrides from SkEventSink
752 virtual bool onQuery(SkEvent* evt) {
753 if (SampleCode::TitleQ(*evt)) {
754 SampleCode::TitleR(evt, "Slides");
755 return true;
756 }
757 return this->INHERITED::onQuery(evt);
758 }
759
reed@google.com81e3d7f2011-06-01 12:42:36 +0000760 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com63e34c62009-10-06 21:19:18 +0000761 gProc[fIndex](canvas);
762 }
763
764 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
765 fIndex = (fIndex + 1) % SK_ARRAY_COUNT(gProc);
766 this->inval(NULL);
767 return NULL;
768 }
769
770private:
reed@google.com81e3d7f2011-06-01 12:42:36 +0000771 typedef SampleView INHERITED;
reed@android.com63e34c62009-10-06 21:19:18 +0000772};
773
774//////////////////////////////////////////////////////////////////////////////
775
776static SkView* MyFactory() { return new SlideView; }
777static SkViewRegister reg(MyFactory);
778