blob: fa948f820a63d51a12277b1674bc711ed1c34992 [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"
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000012#include "SkFlattenableBuffers.h"
reed@android.com63e34c62009-10-06 21:19:18 +000013#include "SkPaint.h"
14
reed@android.com8af96022009-10-12 17:02:22 +000015#define BG_COLOR 0xFFDDDDDD
16
reed@android.com63e34c62009-10-06 21:19:18 +000017typedef void (*SlideProc)(SkCanvas*);
18
19///////////////////////////////////////////////////////////////////////////////
20
21#include "Sk1DPathEffect.h"
22#include "Sk2DPathEffect.h"
23#include "SkCornerPathEffect.h"
24#include "SkDashPathEffect.h"
25#include "SkDiscretePathEffect.h"
26
27static void compose_pe(SkPaint* paint) {
28 SkPathEffect* pe = paint->getPathEffect();
29 SkPathEffect* corner = new SkCornerPathEffect(25);
30 SkPathEffect* compose;
31 if (pe) {
32 compose = new SkComposePathEffect(pe, corner);
33 corner->unref();
34 } else {
35 compose = corner;
36 }
37 paint->setPathEffect(compose)->unref();
38}
39
40static void hair_pe(SkPaint* paint) {
41 paint->setStrokeWidth(0);
42}
43
44static void hair2_pe(SkPaint* paint) {
45 paint->setStrokeWidth(0);
46 compose_pe(paint);
47}
48
49static void stroke_pe(SkPaint* paint) {
50 paint->setStrokeWidth(12);
51 compose_pe(paint);
52}
53
54static void dash_pe(SkPaint* paint) {
55 SkScalar inter[] = { 20, 10, 10, 10 };
56 paint->setStrokeWidth(12);
57 paint->setPathEffect(new SkDashPathEffect(inter, SK_ARRAY_COUNT(inter),
58 0))->unref();
59 compose_pe(paint);
60}
61
62static const int gXY[] = {
634, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
64};
65
66static void scale(SkPath* path, SkScalar scale) {
67 SkMatrix m;
68 m.setScale(scale, scale);
69 path->transform(m);
70}
71
72static void one_d_pe(SkPaint* paint) {
73 SkPath path;
74 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
75 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
76 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
77 path.close();
78 path.offset(SkIntToScalar(-6), 0);
robertphillips@google.comc6ce7502012-05-08 13:15:37 +000079 scale(&path, SkFloatToScalar(1.5f));
rmistry@google.comae933ce2012-08-23 18:19:56 +000080
reed@android.com63e34c62009-10-06 21:19:18 +000081 paint->setPathEffect(new SkPath1DPathEffect(path, SkIntToScalar(21), 0,
82 SkPath1DPathEffect::kRotate_Style))->unref();
83 compose_pe(paint);
84}
85
86typedef void (*PE_Proc)(SkPaint*);
87static const PE_Proc gPE[] = { hair_pe, hair2_pe, stroke_pe, dash_pe, one_d_pe };
88
89static void fill_pe(SkPaint* paint) {
90 paint->setStyle(SkPaint::kFill_Style);
91 paint->setPathEffect(NULL);
92}
93
94static void discrete_pe(SkPaint* paint) {
95 paint->setPathEffect(new SkDiscretePathEffect(10, 4))->unref();
96}
97
reed@google.com43e9f202011-08-09 19:01:50 +000098static SkPathEffect* MakeTileEffect() {
99 SkMatrix m;
100 m.setScale(SkIntToScalar(12), SkIntToScalar(12));
reed@android.com63e34c62009-10-06 21:19:18 +0000101
reed@google.com43e9f202011-08-09 19:01:50 +0000102 SkPath path;
103 path.addCircle(0, 0, SkIntToScalar(5));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000104
reed@google.com43e9f202011-08-09 19:01:50 +0000105 return new SkPath2DPathEffect(m, path);
106}
reed@android.com63e34c62009-10-06 21:19:18 +0000107
108static void tile_pe(SkPaint* paint) {
reed@google.com43e9f202011-08-09 19:01:50 +0000109 paint->setPathEffect(MakeTileEffect())->unref();
reed@android.com63e34c62009-10-06 21:19:18 +0000110}
111
112static const PE_Proc gPE2[] = { fill_pe, discrete_pe, tile_pe };
113
114static void patheffect_slide(SkCanvas* canvas) {
115 SkPaint paint;
116 paint.setAntiAlias(true);
117 paint.setStyle(SkPaint::kStroke_Style);
118
119 SkPath path;
120 path.moveTo(20, 20);
121 path.lineTo(70, 120);
122 path.lineTo(120, 30);
123 path.lineTo(170, 80);
124 path.lineTo(240, 50);
125
126 size_t i;
127 canvas->save();
128 for (i = 0; i < SK_ARRAY_COUNT(gPE); i++) {
129 gPE[i](&paint);
130 canvas->drawPath(path, paint);
131 canvas->translate(0, 75);
132 }
133 canvas->restore();
134
135 path.reset();
136 SkRect r = { 0, 0, 250, 120 };
137 path.addOval(r, SkPath::kCW_Direction);
138 r.inset(50, 50);
139 path.addRect(r, SkPath::kCCW_Direction);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000140
reed@android.com63e34c62009-10-06 21:19:18 +0000141 canvas->translate(320, 20);
142 for (i = 0; i < SK_ARRAY_COUNT(gPE2); i++) {
143 gPE2[i](&paint);
144 canvas->drawPath(path, paint);
145 canvas->translate(0, 160);
146 }
147}
148
149///////////////////////////////////////////////////////////////////////////////
150
151#include "SkGradientShader.h"
152
153struct GradData {
154 int fCount;
155 const SkColor* fColors;
156 const SkScalar* fPos;
157};
158
159static const SkColor gColors[] = {
160SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
161};
162static const SkScalar gPos0[] = { 0, SK_Scalar1 };
163static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
164static const SkScalar gPos2[] = {
1650, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1
166};
167
168static const GradData gGradData[] = {
169{ 2, gColors, NULL },
170{ 2, gColors, gPos0 },
171{ 2, gColors, gPos1 },
172{ 5, gColors, NULL },
173{ 5, gColors, gPos2 }
174};
175
176static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data,
177 SkShader::TileMode tm, SkUnitMapper* mapper) {
178 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos,
179 data.fCount, tm, mapper);
180}
181
182static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data,
183 SkShader::TileMode tm, SkUnitMapper* mapper) {
184 SkPoint center;
185 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
186 SkScalarAve(pts[0].fY, pts[1].fY));
187 return SkGradientShader::CreateRadial(center, center.fX, data.fColors,
188 data.fPos, data.fCount, tm, mapper);
189}
190
191static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data,
192 SkShader::TileMode tm, SkUnitMapper* mapper) {
193 SkPoint center;
194 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
195 SkScalarAve(pts[0].fY, pts[1].fY));
196 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors,
197 data.fPos, data.fCount, mapper);
198}
199
200static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data,
201 SkShader::TileMode tm, SkUnitMapper* mapper) {
202 SkPoint center0, center1;
203 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
204 SkScalarAve(pts[0].fY, pts[1].fY));
205 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
206 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
207 return SkGradientShader::CreateTwoPointRadial(
208 center1, (pts[1].fX - pts[0].fX) / 7,
209 center0, (pts[1].fX - pts[0].fX) / 2,
210 data.fColors, data.fPos, data.fCount, tm, mapper);
211}
212
213typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data,
214 SkShader::TileMode tm, SkUnitMapper* mapper);
215static const GradMaker gGradMakers[] = {
216 MakeLinear, MakeRadial, MakeSweep, Make2Radial
217};
218
219static void gradient_slide(SkCanvas* canvas) {
220 SkPoint pts[2] = {
221 { 0, 0 },
222 { SkIntToScalar(100), SkIntToScalar(100) }
223 };
224 SkShader::TileMode tm = SkShader::kClamp_TileMode;
225 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
226 SkPaint paint;
227 paint.setAntiAlias(true);
228 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000229
reed@android.com63e34c62009-10-06 21:19:18 +0000230 canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
231 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
232 canvas->save();
233 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
234 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL);
235 paint.setShader(shader);
236 canvas->drawRect(r, paint);
237 shader->unref();
238 canvas->translate(0, SkIntToScalar(120));
239 }
240 canvas->restore();
241 canvas->translate(SkIntToScalar(120), 0);
242 }
243}
244
245///////////////////////////////////////////////////////////////////////////////
246
247#include "SkPathMeasure.h"
248
249static SkScalar getpathlen(const SkPath& path) {
250 SkPathMeasure meas(path, false);
251 return meas.getLength();
252}
253
254static void textonpath_slide(SkCanvas* canvas) {
255 const char* text = "Displacement";
256 size_t len =strlen(text);
257 SkPath path;
258 path.moveTo(100, 300);
259 path.quadTo(300, 100, 500, 300);
260 path.offset(0, -100);
261
262 SkPaint paint;
263 paint.setAntiAlias(true);
264 paint.setTextSize(40);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000265
reed@android.com63e34c62009-10-06 21:19:18 +0000266 paint.setStyle(SkPaint::kStroke_Style);
267 canvas->drawPath(path, paint);
268 paint.setStyle(SkPaint::kFill_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000269
reed@android.com63e34c62009-10-06 21:19:18 +0000270 SkScalar x = 50;
271 paint.setColor(0xFF008800);
272 canvas->drawTextOnPathHV(text, len, path,
273 x, paint.getTextSize()*2/3, paint);
274 paint.setColor(SK_ColorRED);
275 canvas->drawTextOnPathHV(text, len, path,
rmistry@google.comae933ce2012-08-23 18:19:56 +0000276 x + 60, 0, paint);
reed@android.com63e34c62009-10-06 21:19:18 +0000277 paint.setColor(SK_ColorBLUE);
278 canvas->drawTextOnPathHV(text, len, path,
279 x + 120, -paint.getTextSize()*2/3, paint);
280
281 path.offset(0, 200);
282 paint.setTextAlign(SkPaint::kRight_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000283
reed@android.com63e34c62009-10-06 21:19:18 +0000284 text = "Matrices";
285 len = strlen(text);
286 SkScalar pathLen = getpathlen(path);
287 SkMatrix matrix;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000288
reed@android.com63e34c62009-10-06 21:19:18 +0000289 paint.setColor(SK_ColorBLACK);
290 paint.setStyle(SkPaint::kStroke_Style);
291 canvas->drawPath(path, paint);
292 paint.setStyle(SkPaint::kFill_Style);
293
294 paint.setTextSize(50);
295 canvas->drawTextOnPath(text, len, path, NULL, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000296
reed@android.com63e34c62009-10-06 21:19:18 +0000297 paint.setColor(SK_ColorRED);
298 matrix.setScale(-SK_Scalar1, SK_Scalar1);
299 matrix.postTranslate(pathLen, 0);
300 canvas->drawTextOnPath(text, len, path, &matrix, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000301
reed@android.com63e34c62009-10-06 21:19:18 +0000302 paint.setColor(SK_ColorBLUE);
303 matrix.setScale(SK_Scalar1, -SK_Scalar1);
304 canvas->drawTextOnPath(text, len, path, &matrix, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000305
reed@android.com63e34c62009-10-06 21:19:18 +0000306 paint.setColor(0xFF008800);
307 matrix.setScale(-SK_Scalar1, -SK_Scalar1);
308 matrix.postTranslate(pathLen, 0);
309 canvas->drawTextOnPath(text, len, path, &matrix, paint);
310}
311
312///////////////////////////////////////////////////////////////////////////////
313
314#include "SkImageDecoder.h"
315#include "SkOSFile.h"
316#include "SkRandom.h"
317#include "SkStream.h"
reed@android.com63e34c62009-10-06 21:19:18 +0000318
319static SkShader* make_shader0(SkIPoint* size) {
320 SkBitmap bm;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000321
reed@android.com63e34c62009-10-06 21:19:18 +0000322 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;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000342
reed@android.com63e34c62009-10-06 21:19:18 +0000343 Rec() : fCount(0), fVerts(NULL), fTexs(NULL) {}
344 ~Rec() { delete[] fVerts; delete[] fTexs; }
345};
346
caryclark@google.com02939ce2012-06-06 12:09:51 +0000347static void make_tris(Rec* rec) {
reed@android.com63e34c62009-10-06 21:19:18 +0000348 int n = 10;
349 SkRandom rand;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000350
reed@android.com63e34c62009-10-06 21:19:18 +0000351 rec->fMode = SkCanvas::kTriangles_VertexMode;
352 rec->fCount = n * 3;
353 rec->fVerts = new SkPoint[rec->fCount];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000354
reed@android.com63e34c62009-10-06 21:19:18 +0000355 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
caryclark@google.com02939ce2012-06-06 12:09:51 +0000363static void make_fan(Rec* rec, int texWidth, int texHeight) {
reed@android.com63e34c62009-10-06 21:19:18 +0000364 const SkScalar tx = SkIntToScalar(texWidth);
365 const SkScalar ty = SkIntToScalar(texHeight);
366 const int n = 24;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000367
reed@android.com63e34c62009-10-06 21:19:18 +0000368 rec->fMode = SkCanvas::kTriangleFan_VertexMode;
369 rec->fCount = n + 2;
370 rec->fVerts = new SkPoint[rec->fCount];
371 rec->fTexs = new SkPoint[rec->fCount];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000372
reed@android.com63e34c62009-10-06 21:19:18 +0000373 SkPoint* v = rec->fVerts;
374 SkPoint* t = rec->fTexs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000375
reed@android.com63e34c62009-10-06 21:19:18 +0000376 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000386
reed@android.com63e34c62009-10-06 21:19:18 +0000387 SkMatrix m;
388 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
389 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
390 m.mapPoints(v, rec->fCount);
391}
392
caryclark@google.com02939ce2012-06-06 12:09:51 +0000393static void make_strip(Rec* rec, int texWidth, int texHeight) {
reed@android.com63e34c62009-10-06 21:19:18 +0000394 const SkScalar tx = SkIntToScalar(texWidth);
395 const SkScalar ty = SkIntToScalar(texHeight);
396 const int n = 24;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000397
reed@android.com63e34c62009-10-06 21:19:18 +0000398 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];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000402
reed@android.com63e34c62009-10-06 21:19:18 +0000403 SkPoint* v = rec->fVerts;
404 SkPoint* t = rec->fTexs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000405
reed@android.com63e34c62009-10-06 21:19:18 +0000406 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000411
reed@android.com63e34c62009-10-06 21:19:18 +0000412 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];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000417
reed@android.com63e34c62009-10-06 21:19:18 +0000418 t[2*n + 0].set(tx, ty);
419 t[2*n + 1].set(tx, 0);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000420
reed@android.com63e34c62009-10-06 21:19:18 +0000421 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;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000430
reed@android.com63e34c62009-10-06 21:19:18 +0000431 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000444
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();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000447
reed@android.com63e34c62009-10-06 21:19:18 +0000448 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000452
reed@android.com63e34c62009-10-06 21:19:18 +0000453 canvas->translate(SkIntToScalar(210), 0);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000454
reed@android.com63e34c62009-10-06 21:19:18 +0000455 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000459
reed@android.com63e34c62009-10-06 21:19:18 +0000460 canvas->translate(SkIntToScalar(210), 0);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000461
reed@android.com63e34c62009-10-06 21:19:18 +0000462 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();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000467
reed@android.com63e34c62009-10-06 21:19:18 +0000468 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));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000483
reed@android.com8af96022009-10-12 17:02:22 +0000484 p.setMaskFilter(NULL);
485 p.setStyle(SkPaint::kStroke_Style);
486 p.setStrokeWidth(SK_Scalar1);
487 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000488
reed@android.com8af96022009-10-12 17:02:22 +0000489 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000498
reed@android.com8af96022009-10-12 17:02:22 +0000499 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000511
reed@android.com8af96022009-10-12 17:02:22 +0000512 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000523
reed@android.com8af96022009-10-12 17:02:22 +0000524 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));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000534
reed@android.com8af96022009-10-12 17:02:22 +0000535 p.setAlpha(0xFF);
536 p.setXfermodeMode(SkXfermode::kClear_Mode);
537 rast->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000538
reed@android.com8af96022009-10-12 17:02:22 +0000539 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000548
reed@android.com8af96022009-10-12 17:02:22 +0000549 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000557
reed@android.com8af96022009-10-12 17:02:22 +0000558 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000586
reed@android.com8af96022009-10-12 17:02:22 +0000587 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000593
reed@android.com8af96022009-10-12 17:02:22 +0000594 p.setPathEffect(NULL);
595 p.setXfermode(NULL);
596 p.setStyle(SkPaint::kStroke_Style);
597 p.setStrokeWidth(SK_Scalar1);
598 rast->addLayer(p);
599}
600
reed@android.com8af96022009-10-12 17:02:22 +0000601static void r9(SkLayerRasterizer* rast, SkPaint& p)
602{
603 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000604
reed@android.com8af96022009-10-12 17:02:22 +0000605 SkMatrix lattice;
606 lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
607 lattice.postRotate(SkIntToScalar(30), 0, 0);
scroggo@google.comd8a6cc82012-09-12 18:53:49 +0000608 p.setPathEffect(new SkLine2DPathEffect(SK_Scalar1*2, lattice))->unref();
reed@android.com8af96022009-10-12 17:02:22 +0000609 p.setXfermodeMode(SkXfermode::kClear_Mode);
610 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000611
reed@android.com8af96022009-10-12 17:02:22 +0000612 p.setPathEffect(NULL);
613 p.setXfermode(NULL);
614 p.setStyle(SkPaint::kStroke_Style);
615 p.setStrokeWidth(SK_Scalar1);
616 rast->addLayer(p);
617}
618
619typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&);
620
621static const raster_proc gRastProcs[] = {
622 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
623};
624
rmistry@google.comae933ce2012-08-23 18:19:56 +0000625static void apply_shader(SkPaint* paint, int index) {
reed@android.com8af96022009-10-12 17:02:22 +0000626 raster_proc proc = gRastProcs[index];
627 SkPaint p;
628 SkLayerRasterizer* rast = new SkLayerRasterizer;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000629
reed@android.com8af96022009-10-12 17:02:22 +0000630 p.setAntiAlias(true);
631 proc(rast, p);
632 paint->setRasterizer(rast)->unref();
633 paint->setColor(SK_ColorBLUE);
634}
635
636#include "SkTypeface.h"
637
638static void texteffect_slide(SkCanvas* canvas) {
639 const char* str = "Google";
640 size_t len = strlen(str);
641 SkScalar x = 20;
642 SkScalar y = 80;
643 SkPaint paint;
644 paint.setTypeface(SkTypeface::CreateFromName("Georgia", SkTypeface::kItalic));
645 paint.setTextSize(75);
646 paint.setAntiAlias(true);
647 paint.setColor(SK_ColorBLUE);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000648 for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) {
reed@android.com8af96022009-10-12 17:02:22 +0000649 apply_shader(&paint, i);
650 canvas->drawText(str, len, x, y, paint);
651 y += 80;
652 if (i == 4) {
653 x += 320;
654 y = 80;
655 }
656 }
657}
658
659///////////////////////////////////////////////////////////////////////////////
660
reed@android.com63e34c62009-10-06 21:19:18 +0000661#include "SkImageEncoder.h"
662
663static const SlideProc gProc[] = {
664 patheffect_slide,
665 gradient_slide,
666 textonpath_slide,
reed@android.com8af96022009-10-12 17:02:22 +0000667 mesh_slide,
668 texteffect_slide
reed@android.com63e34c62009-10-06 21:19:18 +0000669};
670
reed@google.com81e3d7f2011-06-01 12:42:36 +0000671class SlideView : public SampleView {
reed@android.com63e34c62009-10-06 21:19:18 +0000672 int fIndex;
reed@google.com7b185902012-03-12 21:13:48 +0000673 bool fOnce;
reed@android.com63e34c62009-10-06 21:19:18 +0000674public:
675 SlideView() {
reed@google.com7b185902012-03-12 21:13:48 +0000676 fOnce = false;
677 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000678
reed@google.com7b185902012-03-12 21:13:48 +0000679 void init() {
680 if (fOnce) {
681 return;
682 }
683 fOnce = true;
684
reed@android.com63e34c62009-10-06 21:19:18 +0000685 fIndex = 0;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000686
reed@android.com63e34c62009-10-06 21:19:18 +0000687 SkBitmap bm;
688 bm.setConfig(SkBitmap::kARGB_8888_Config, 1024, 768);
689 bm.allocPixels();
690 SkCanvas canvas(bm);
691 SkScalar s = SkIntToScalar(1024) / 640;
692 canvas.scale(s, s);
693 for (size_t i = 0; i < SK_ARRAY_COUNT(gProc); i++) {
694 canvas.save();
reed@android.com8af96022009-10-12 17:02:22 +0000695 canvas.drawColor(BG_COLOR);
reed@android.com63e34c62009-10-06 21:19:18 +0000696 gProc[i](&canvas);
697 canvas.restore();
698 SkString str;
senorblanco@chromium.org3a67a662012-07-09 18:22:08 +0000699 str.printf("/skimages/slide_" SK_SIZE_T_SPECIFIER ".png", i);
reed@android.com63e34c62009-10-06 21:19:18 +0000700 SkImageEncoder::EncodeFile(str.c_str(), bm, SkImageEncoder::kPNG_Type, 100);
701 }
reed@google.com81e3d7f2011-06-01 12:42:36 +0000702 this->setBGColor(BG_COLOR);
reed@android.com63e34c62009-10-06 21:19:18 +0000703 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000704
reed@android.com63e34c62009-10-06 21:19:18 +0000705protected:
706 // overrides from SkEventSink
707 virtual bool onQuery(SkEvent* evt) {
708 if (SampleCode::TitleQ(*evt)) {
709 SampleCode::TitleR(evt, "Slides");
710 return true;
711 }
712 return this->INHERITED::onQuery(evt);
713 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000714
reed@google.com81e3d7f2011-06-01 12:42:36 +0000715 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com7b185902012-03-12 21:13:48 +0000716 this->init();
reed@android.com63e34c62009-10-06 21:19:18 +0000717 gProc[fIndex](canvas);
718 }
719
sugoi@google.com9c55f802013-03-07 20:52:59 +0000720 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) SK_OVERRIDE {
reed@google.com7b185902012-03-12 21:13:48 +0000721 this->init();
reed@android.com63e34c62009-10-06 21:19:18 +0000722 fIndex = (fIndex + 1) % SK_ARRAY_COUNT(gProc);
723 this->inval(NULL);
724 return NULL;
725 }
726
727private:
reed@google.com81e3d7f2011-06-01 12:42:36 +0000728 typedef SampleView INHERITED;
reed@android.com63e34c62009-10-06 21:19:18 +0000729};
730
731//////////////////////////////////////////////////////////////////////////////
732
733static SkView* MyFactory() { return new SlideView; }
734static SkViewRegister reg(MyFactory);