blob: 6bc9a28fd3716615dac573f1c0ee461068de3325 [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"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurMask.h"
10#include "SkBlurMaskFilter.h"
reed@android.com63e34c62009-10-06 21:19:18 +000011#include "SkCanvas.h"
12#include "SkDevice.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000013#include "SkReadBuffer.h"
14#include "SkWriteBuffer.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000015#include "SkGradientShader.h"
16#include "SkLayerRasterizer.h"
reed@android.com63e34c62009-10-06 21:19:18 +000017#include "SkPaint.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000018#include "SkView.h"
reed@android.com63e34c62009-10-06 21:19:18 +000019
reed@android.com8af96022009-10-12 17:02:22 +000020#define BG_COLOR 0xFFDDDDDD
21
reed@android.com63e34c62009-10-06 21:19:18 +000022typedef void (*SlideProc)(SkCanvas*);
23
24///////////////////////////////////////////////////////////////////////////////
25
26#include "Sk1DPathEffect.h"
27#include "Sk2DPathEffect.h"
28#include "SkCornerPathEffect.h"
29#include "SkDashPathEffect.h"
30#include "SkDiscretePathEffect.h"
31
32static void compose_pe(SkPaint* paint) {
33 SkPathEffect* pe = paint->getPathEffect();
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +000034 SkPathEffect* corner = SkCornerPathEffect::Create(25);
reed@android.com63e34c62009-10-06 21:19:18 +000035 SkPathEffect* compose;
36 if (pe) {
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +000037 compose = SkComposePathEffect::Create(pe, corner);
reed@android.com63e34c62009-10-06 21:19:18 +000038 corner->unref();
39 } else {
40 compose = corner;
41 }
42 paint->setPathEffect(compose)->unref();
43}
44
45static void hair_pe(SkPaint* paint) {
46 paint->setStrokeWidth(0);
47}
48
49static void hair2_pe(SkPaint* paint) {
50 paint->setStrokeWidth(0);
51 compose_pe(paint);
52}
53
54static void stroke_pe(SkPaint* paint) {
55 paint->setStrokeWidth(12);
56 compose_pe(paint);
57}
58
59static void dash_pe(SkPaint* paint) {
60 SkScalar inter[] = { 20, 10, 10, 10 };
61 paint->setStrokeWidth(12);
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +000062 paint->setPathEffect(SkDashPathEffect::Create(inter, SK_ARRAY_COUNT(inter),
63 0))->unref();
reed@android.com63e34c62009-10-06 21:19:18 +000064 compose_pe(paint);
65}
66
67static const int gXY[] = {
684, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
69};
70
71static void scale(SkPath* path, SkScalar scale) {
72 SkMatrix m;
73 m.setScale(scale, scale);
74 path->transform(m);
75}
76
77static void one_d_pe(SkPaint* paint) {
78 SkPath path;
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]));
82 path.close();
83 path.offset(SkIntToScalar(-6), 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000084 scale(&path, 1.5f);
rmistry@google.comae933ce2012-08-23 18:19:56 +000085
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +000086 paint->setPathEffect(SkPath1DPathEffect::Create(path, SkIntToScalar(21), 0,
87 SkPath1DPathEffect::kRotate_Style))->unref();
reed@android.com63e34c62009-10-06 21:19:18 +000088 compose_pe(paint);
89}
90
91typedef void (*PE_Proc)(SkPaint*);
92static const PE_Proc gPE[] = { hair_pe, hair2_pe, stroke_pe, dash_pe, one_d_pe };
93
94static void fill_pe(SkPaint* paint) {
95 paint->setStyle(SkPaint::kFill_Style);
96 paint->setPathEffect(NULL);
97}
98
99static void discrete_pe(SkPaint* paint) {
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000100 paint->setPathEffect(SkDiscretePathEffect::Create(10, 4))->unref();
reed@android.com63e34c62009-10-06 21:19:18 +0000101}
102
reed@google.com43e9f202011-08-09 19:01:50 +0000103static SkPathEffect* MakeTileEffect() {
104 SkMatrix m;
105 m.setScale(SkIntToScalar(12), SkIntToScalar(12));
reed@android.com63e34c62009-10-06 21:19:18 +0000106
reed@google.com43e9f202011-08-09 19:01:50 +0000107 SkPath path;
108 path.addCircle(0, 0, SkIntToScalar(5));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000109
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000110 return SkPath2DPathEffect::Create(m, path);
reed@google.com43e9f202011-08-09 19:01:50 +0000111}
reed@android.com63e34c62009-10-06 21:19:18 +0000112
113static void tile_pe(SkPaint* paint) {
reed@google.com43e9f202011-08-09 19:01:50 +0000114 paint->setPathEffect(MakeTileEffect())->unref();
reed@android.com63e34c62009-10-06 21:19:18 +0000115}
116
117static const PE_Proc gPE2[] = { fill_pe, discrete_pe, tile_pe };
118
119static void patheffect_slide(SkCanvas* canvas) {
120 SkPaint paint;
121 paint.setAntiAlias(true);
122 paint.setStyle(SkPaint::kStroke_Style);
123
124 SkPath path;
125 path.moveTo(20, 20);
126 path.lineTo(70, 120);
127 path.lineTo(120, 30);
128 path.lineTo(170, 80);
129 path.lineTo(240, 50);
130
131 size_t i;
132 canvas->save();
133 for (i = 0; i < SK_ARRAY_COUNT(gPE); i++) {
134 gPE[i](&paint);
135 canvas->drawPath(path, paint);
136 canvas->translate(0, 75);
137 }
138 canvas->restore();
139
140 path.reset();
141 SkRect r = { 0, 0, 250, 120 };
142 path.addOval(r, SkPath::kCW_Direction);
143 r.inset(50, 50);
144 path.addRect(r, SkPath::kCCW_Direction);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000145
reed@android.com63e34c62009-10-06 21:19:18 +0000146 canvas->translate(320, 20);
147 for (i = 0; i < SK_ARRAY_COUNT(gPE2); i++) {
148 gPE2[i](&paint);
149 canvas->drawPath(path, paint);
150 canvas->translate(0, 160);
151 }
152}
153
154///////////////////////////////////////////////////////////////////////////////
155
156#include "SkGradientShader.h"
157
158struct GradData {
159 int fCount;
160 const SkColor* fColors;
161 const SkScalar* fPos;
162};
163
164static const SkColor gColors[] = {
165SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
166};
167static const SkScalar gPos0[] = { 0, SK_Scalar1 };
168static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
169static const SkScalar gPos2[] = {
1700, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1
171};
172
173static const GradData gGradData[] = {
174{ 2, gColors, NULL },
175{ 2, gColors, gPos0 },
176{ 2, gColors, gPos1 },
177{ 5, gColors, NULL },
178{ 5, gColors, gPos2 }
179};
180
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000181static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
182 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, data.fCount, tm);
reed@android.com63e34c62009-10-06 21:19:18 +0000183}
184
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000185static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
reed@android.com63e34c62009-10-06 21:19:18 +0000186 SkPoint center;
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,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000190 data.fPos, data.fCount, tm);
reed@android.com63e34c62009-10-06 21:19:18 +0000191}
192
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000193static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
reed@android.com63e34c62009-10-06 21:19:18 +0000194 SkPoint center;
195 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
196 SkScalarAve(pts[0].fY, pts[1].fY));
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000197 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount);
reed@android.com63e34c62009-10-06 21:19:18 +0000198}
199
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000200static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
reed@android.com63e34c62009-10-06 21:19:18 +0000201 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,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000209 data.fColors, data.fPos, data.fCount, tm);
reed@android.com63e34c62009-10-06 21:19:18 +0000210}
211
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000212typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm);
reed@android.com63e34c62009-10-06 21:19:18 +0000213static const GradMaker gGradMakers[] = {
214 MakeLinear, MakeRadial, MakeSweep, Make2Radial
215};
216
217static void gradient_slide(SkCanvas* canvas) {
218 SkPoint pts[2] = {
219 { 0, 0 },
220 { SkIntToScalar(100), SkIntToScalar(100) }
221 };
222 SkShader::TileMode tm = SkShader::kClamp_TileMode;
223 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
224 SkPaint paint;
225 paint.setAntiAlias(true);
226 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000227
reed@android.com63e34c62009-10-06 21:19:18 +0000228 canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
229 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
230 canvas->save();
231 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000232 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm);
reed@android.com63e34c62009-10-06 21:19:18 +0000233 paint.setShader(shader);
234 canvas->drawRect(r, paint);
235 shader->unref();
236 canvas->translate(0, SkIntToScalar(120));
237 }
238 canvas->restore();
239 canvas->translate(SkIntToScalar(120), 0);
240 }
241}
242
243///////////////////////////////////////////////////////////////////////////////
244
245#include "SkPathMeasure.h"
246
247static SkScalar getpathlen(const SkPath& path) {
248 SkPathMeasure meas(path, false);
249 return meas.getLength();
250}
251
252static void textonpath_slide(SkCanvas* canvas) {
253 const char* text = "Displacement";
254 size_t len =strlen(text);
255 SkPath path;
256 path.moveTo(100, 300);
257 path.quadTo(300, 100, 500, 300);
258 path.offset(0, -100);
259
260 SkPaint paint;
261 paint.setAntiAlias(true);
262 paint.setTextSize(40);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000263
reed@android.com63e34c62009-10-06 21:19:18 +0000264 paint.setStyle(SkPaint::kStroke_Style);
265 canvas->drawPath(path, paint);
266 paint.setStyle(SkPaint::kFill_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000267
reed@android.com63e34c62009-10-06 21:19:18 +0000268 SkScalar x = 50;
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,
rmistry@google.comae933ce2012-08-23 18:19:56 +0000274 x + 60, 0, paint);
reed@android.com63e34c62009-10-06 21:19:18 +0000275 paint.setColor(SK_ColorBLUE);
276 canvas->drawTextOnPathHV(text, len, path,
277 x + 120, -paint.getTextSize()*2/3, paint);
278
279 path.offset(0, 200);
280 paint.setTextAlign(SkPaint::kRight_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000281
reed@android.com63e34c62009-10-06 21:19:18 +0000282 text = "Matrices";
283 len = strlen(text);
284 SkScalar pathLen = getpathlen(path);
285 SkMatrix matrix;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000286
reed@android.com63e34c62009-10-06 21:19:18 +0000287 paint.setColor(SK_ColorBLACK);
288 paint.setStyle(SkPaint::kStroke_Style);
289 canvas->drawPath(path, paint);
290 paint.setStyle(SkPaint::kFill_Style);
291
292 paint.setTextSize(50);
293 canvas->drawTextOnPath(text, len, path, NULL, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000294
reed@android.com63e34c62009-10-06 21:19:18 +0000295 paint.setColor(SK_ColorRED);
296 matrix.setScale(-SK_Scalar1, SK_Scalar1);
297 matrix.postTranslate(pathLen, 0);
298 canvas->drawTextOnPath(text, len, path, &matrix, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000299
reed@android.com63e34c62009-10-06 21:19:18 +0000300 paint.setColor(SK_ColorBLUE);
301 matrix.setScale(SK_Scalar1, -SK_Scalar1);
302 canvas->drawTextOnPath(text, len, path, &matrix, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000303
reed@android.com63e34c62009-10-06 21:19:18 +0000304 paint.setColor(0xFF008800);
305 matrix.setScale(-SK_Scalar1, -SK_Scalar1);
306 matrix.postTranslate(pathLen, 0);
307 canvas->drawTextOnPath(text, len, path, &matrix, paint);
308}
309
310///////////////////////////////////////////////////////////////////////////////
311
312#include "SkImageDecoder.h"
313#include "SkOSFile.h"
314#include "SkRandom.h"
315#include "SkStream.h"
reed@android.com63e34c62009-10-06 21:19:18 +0000316
317static SkShader* make_shader0(SkIPoint* size) {
318 SkBitmap bm;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000319
reed@android.com63e34c62009-10-06 21:19:18 +0000320 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);
324}
325
326static SkShader* make_shader1(const SkIPoint& size) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000327 SkPoint pts[] = { { 0, 0 },
328 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
reed@android.com63e34c62009-10-06 21:19:18 +0000329 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
330 return SkGradientShader::CreateLinear(pts, colors, NULL,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000331 SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode);
reed@android.com63e34c62009-10-06 21:19:18 +0000332}
333
reed@android.comf2b98d62010-12-20 18:26:13 +0000334class Rec {
335public:
reed@android.com63e34c62009-10-06 21:19:18 +0000336 SkCanvas::VertexMode fMode;
337 int fCount;
338 SkPoint* fVerts;
339 SkPoint* fTexs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000340
reed@android.com63e34c62009-10-06 21:19:18 +0000341 Rec() : fCount(0), fVerts(NULL), fTexs(NULL) {}
342 ~Rec() { delete[] fVerts; delete[] fTexs; }
343};
344
caryclark@google.com02939ce2012-06-06 12:09:51 +0000345static void make_tris(Rec* rec) {
reed@android.com63e34c62009-10-06 21:19:18 +0000346 int n = 10;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000347 SkRandom rand;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000348
reed@android.com63e34c62009-10-06 21:19:18 +0000349 rec->fMode = SkCanvas::kTriangles_VertexMode;
350 rec->fCount = n * 3;
351 rec->fVerts = new SkPoint[rec->fCount];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000352
reed@android.com63e34c62009-10-06 21:19:18 +0000353 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);
357 }
358 }
359}
360
caryclark@google.com02939ce2012-06-06 12:09:51 +0000361static void make_fan(Rec* rec, int texWidth, int texHeight) {
reed@android.com63e34c62009-10-06 21:19:18 +0000362 const SkScalar tx = SkIntToScalar(texWidth);
363 const SkScalar ty = SkIntToScalar(texHeight);
364 const int n = 24;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000365
reed@android.com63e34c62009-10-06 21:19:18 +0000366 rec->fMode = SkCanvas::kTriangleFan_VertexMode;
367 rec->fCount = n + 2;
368 rec->fVerts = new SkPoint[rec->fCount];
369 rec->fTexs = new SkPoint[rec->fCount];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000370
reed@android.com63e34c62009-10-06 21:19:18 +0000371 SkPoint* v = rec->fVerts;
372 SkPoint* t = rec->fTexs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000373
reed@android.com63e34c62009-10-06 21:19:18 +0000374 v[0].set(0, 0);
375 t[0].set(0, 0);
376 for (int i = 0; i < n; i++) {
377 SkScalar cos;
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);
381 }
382 v[n+1] = v[1];
383 t[n+1].set(tx, ty);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000384
reed@android.com63e34c62009-10-06 21:19:18 +0000385 SkMatrix m;
386 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
387 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
388 m.mapPoints(v, rec->fCount);
389}
390
caryclark@google.com02939ce2012-06-06 12:09:51 +0000391static void make_strip(Rec* rec, int texWidth, int texHeight) {
reed@android.com63e34c62009-10-06 21:19:18 +0000392 const SkScalar tx = SkIntToScalar(texWidth);
393 const SkScalar ty = SkIntToScalar(texHeight);
394 const int n = 24;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000395
reed@android.com63e34c62009-10-06 21:19:18 +0000396 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];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000400
reed@android.com63e34c62009-10-06 21:19:18 +0000401 SkPoint* v = rec->fVerts;
402 SkPoint* t = rec->fTexs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000403
reed@android.com63e34c62009-10-06 21:19:18 +0000404 for (int i = 0; i < n; i++) {
405 SkScalar cos;
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);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000409
reed@android.com63e34c62009-10-06 21:19:18 +0000410 t[i*2 + 0].set(tx * i / n, ty);
411 t[i*2 + 1].set(tx * i / n, 0);
412 }
413 v[2*n + 0] = v[0];
414 v[2*n + 1] = v[1];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000415
reed@android.com63e34c62009-10-06 21:19:18 +0000416 t[2*n + 0].set(tx, ty);
417 t[2*n + 1].set(tx, 0);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000418
reed@android.com63e34c62009-10-06 21:19:18 +0000419 SkMatrix m;
420 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
421 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
422 m.mapPoints(v, rec->fCount);
423}
424
425static void mesh_slide(SkCanvas* canvas) {
426 Rec fRecs[3];
427 SkIPoint size;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000428
reed@android.com63e34c62009-10-06 21:19:18 +0000429 SkShader* fShader0 = make_shader0(&size);
430 SkShader* fShader1 = make_shader1(size);
reed@android.comf2b98d62010-12-20 18:26:13 +0000431
432 SkAutoUnref aur0(fShader0);
433 SkAutoUnref aur1(fShader1);
434
reed@android.com63e34c62009-10-06 21:19:18 +0000435 make_strip(&fRecs[0], size.fX, size.fY);
436 make_fan(&fRecs[1], size.fX, size.fY);
437 make_tris(&fRecs[2]);
438
reed@android.com63e34c62009-10-06 21:19:18 +0000439 SkPaint paint;
440 paint.setDither(true);
reed@google.com44699382013-10-31 17:28:30 +0000441 paint.setFilterLevel(SkPaint::kLow_FilterLevel);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000442
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000443 for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) {
reed@android.com63e34c62009-10-06 21:19:18 +0000444 canvas->save();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000445
reed@android.com63e34c62009-10-06 21:19:18 +0000446 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000450
reed@android.com63e34c62009-10-06 21:19:18 +0000451 canvas->translate(SkIntToScalar(210), 0);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000452
reed@android.com63e34c62009-10-06 21:19:18 +0000453 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);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000457
reed@android.com63e34c62009-10-06 21:19:18 +0000458 canvas->translate(SkIntToScalar(210), 0);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000459
reed@android.com63e34c62009-10-06 21:19:18 +0000460 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);
464 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000465
reed@android.com63e34c62009-10-06 21:19:18 +0000466 canvas->translate(0, SkIntToScalar(250));
467 }
468}
469
470///////////////////////////////////////////////////////////////////////////////
471
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000472static void r0(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000473{
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000474 p.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +0000475 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3))))->unref();
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000476 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000477
reed@android.com8af96022009-10-12 17:02:22 +0000478 p.setMaskFilter(NULL);
479 p.setStyle(SkPaint::kStroke_Style);
480 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000481 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000482
reed@android.com8af96022009-10-12 17:02:22 +0000483 p.setAlpha(0x11);
484 p.setStyle(SkPaint::kFill_Style);
485 p.setXfermodeMode(SkXfermode::kSrc_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000486 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000487}
488
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000489static void r1(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000490{
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000491 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000492
reed@android.com8af96022009-10-12 17:02:22 +0000493 p.setAlpha(0x40);
494 p.setXfermodeMode(SkXfermode::kSrc_Mode);
495 p.setStyle(SkPaint::kStroke_Style);
496 p.setStrokeWidth(SK_Scalar1*2);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000497 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000498}
499
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000500static void r2(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000501{
502 p.setStyle(SkPaint::kStrokeAndFill_Style);
503 p.setStrokeWidth(SK_Scalar1*4);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000504 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000505
reed@android.com8af96022009-10-12 17:02:22 +0000506 p.setStyle(SkPaint::kStroke_Style);
507 p.setStrokeWidth(SK_Scalar1*3/2);
508 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000509 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000510}
511
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000512static void r3(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000513{
514 p.setStyle(SkPaint::kStroke_Style);
515 p.setStrokeWidth(SK_Scalar1*3);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000516 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000517
reed@android.com8af96022009-10-12 17:02:22 +0000518 p.setAlpha(0x20);
519 p.setStyle(SkPaint::kFill_Style);
520 p.setXfermodeMode(SkXfermode::kSrc_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000521 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000522}
523
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000524static void r4(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000525{
526 p.setAlpha(0x60);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000527 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000528
reed@android.com8af96022009-10-12 17:02:22 +0000529 p.setAlpha(0xFF);
530 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000531 rastBuilder->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000532
reed@android.com8af96022009-10-12 17:02:22 +0000533 p.setXfermode(NULL);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000534 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000535}
536
537#include "SkDiscretePathEffect.h"
538
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000539static void r5(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000540{
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000541 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000542
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000543 p.setPathEffect(SkDiscretePathEffect::Create(SK_Scalar1*4, SK_Scalar1*3))->unref();
reed@android.com8af96022009-10-12 17:02:22 +0000544 p.setXfermodeMode(SkXfermode::kSrcOut_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000545 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000546}
547
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000548static void r6(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000549{
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000550 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000551
reed@android.com8af96022009-10-12 17:02:22 +0000552 p.setAntiAlias(false);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000553 SkLayerRasterizer::Builder rastBuilder2;
554 r5(&rastBuilder2, p);
555 p.setRasterizer(rastBuilder2.detachRasterizer())->unref();
reed@android.com8af96022009-10-12 17:02:22 +0000556 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000557 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000558}
559
560#include "Sk2DPathEffect.h"
561
reed@google.com18dc4772011-08-09 18:47:40 +0000562static SkPathEffect* MakeDotEffect(SkScalar radius, const SkMatrix& matrix) {
563 SkPath path;
564 path.addCircle(0, 0, radius);
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000565 return SkPath2DPathEffect::Create(matrix, path);
reed@google.com18dc4772011-08-09 18:47:40 +0000566}
reed@android.com8af96022009-10-12 17:02:22 +0000567
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000568static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000569{
570 SkMatrix lattice;
571 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
572 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reed@google.com18dc4772011-08-09 18:47:40 +0000573 p.setPathEffect(MakeDotEffect(SK_Scalar1*4, lattice))->unref();
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000574 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000575}
576
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000577static void r8(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000578{
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000579 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000580
reed@android.com8af96022009-10-12 17:02:22 +0000581 SkMatrix lattice;
582 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
583 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reed@google.com18dc4772011-08-09 18:47:40 +0000584 p.setPathEffect(MakeDotEffect(SK_Scalar1*2, lattice))->unref();
reed@android.com8af96022009-10-12 17:02:22 +0000585 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000586 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000587
reed@android.com8af96022009-10-12 17:02:22 +0000588 p.setPathEffect(NULL);
589 p.setXfermode(NULL);
590 p.setStyle(SkPaint::kStroke_Style);
591 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000592 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000593}
594
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000595static void r9(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000596{
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000597 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000598
reed@android.com8af96022009-10-12 17:02:22 +0000599 SkMatrix lattice;
600 lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
601 lattice.postRotate(SkIntToScalar(30), 0, 0);
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000602 p.setPathEffect(SkLine2DPathEffect::Create(SK_Scalar1*2, lattice))->unref();
reed@android.com8af96022009-10-12 17:02:22 +0000603 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000604 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000605
reed@android.com8af96022009-10-12 17:02:22 +0000606 p.setPathEffect(NULL);
607 p.setXfermode(NULL);
608 p.setStyle(SkPaint::kStroke_Style);
609 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000610 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000611}
612
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000613typedef void (*raster_proc)(SkLayerRasterizer::Builder*, SkPaint&);
reed@android.com8af96022009-10-12 17:02:22 +0000614
615static const raster_proc gRastProcs[] = {
616 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
617};
618
rmistry@google.comae933ce2012-08-23 18:19:56 +0000619static void apply_shader(SkPaint* paint, int index) {
reed@android.com8af96022009-10-12 17:02:22 +0000620 raster_proc proc = gRastProcs[index];
621 SkPaint p;
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000622 SkLayerRasterizer::Builder rastBuilder;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000623
reed@android.com8af96022009-10-12 17:02:22 +0000624 p.setAntiAlias(true);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000625 proc(&rastBuilder, p);
626 paint->setRasterizer(rastBuilder.detachRasterizer())->unref();
reed@android.com8af96022009-10-12 17:02:22 +0000627 paint->setColor(SK_ColorBLUE);
628}
629
630#include "SkTypeface.h"
631
632static void texteffect_slide(SkCanvas* canvas) {
633 const char* str = "Google";
634 size_t len = strlen(str);
635 SkScalar x = 20;
636 SkScalar y = 80;
637 SkPaint paint;
638 paint.setTypeface(SkTypeface::CreateFromName("Georgia", SkTypeface::kItalic));
639 paint.setTextSize(75);
640 paint.setAntiAlias(true);
641 paint.setColor(SK_ColorBLUE);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000642 for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) {
reed@google.com7fa2a652014-01-27 13:42:58 +0000643 apply_shader(&paint, (int)i);
reed@android.com8af96022009-10-12 17:02:22 +0000644 canvas->drawText(str, len, x, y, paint);
645 y += 80;
646 if (i == 4) {
647 x += 320;
648 y = 80;
649 }
650 }
651}
652
653///////////////////////////////////////////////////////////////////////////////
654
reed@android.com63e34c62009-10-06 21:19:18 +0000655#include "SkImageEncoder.h"
656
657static const SlideProc gProc[] = {
658 patheffect_slide,
659 gradient_slide,
660 textonpath_slide,
reed@android.com8af96022009-10-12 17:02:22 +0000661 mesh_slide,
662 texteffect_slide
reed@android.com63e34c62009-10-06 21:19:18 +0000663};
664
reed@google.com81e3d7f2011-06-01 12:42:36 +0000665class SlideView : public SampleView {
reed@android.com63e34c62009-10-06 21:19:18 +0000666 int fIndex;
reed@google.com7b185902012-03-12 21:13:48 +0000667 bool fOnce;
reed@android.com63e34c62009-10-06 21:19:18 +0000668public:
669 SlideView() {
reed@google.com7b185902012-03-12 21:13:48 +0000670 fOnce = false;
671 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000672
reed@google.com7b185902012-03-12 21:13:48 +0000673 void init() {
674 if (fOnce) {
675 return;
676 }
677 fOnce = true;
678
reed@android.com63e34c62009-10-06 21:19:18 +0000679 fIndex = 0;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000680
reed@android.com63e34c62009-10-06 21:19:18 +0000681 SkBitmap bm;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000682 bm.allocN32Pixels(1024, 768);
reed@android.com63e34c62009-10-06 21:19:18 +0000683 SkCanvas canvas(bm);
684 SkScalar s = SkIntToScalar(1024) / 640;
685 canvas.scale(s, s);
686 for (size_t i = 0; i < SK_ARRAY_COUNT(gProc); i++) {
687 canvas.save();
reed@android.com8af96022009-10-12 17:02:22 +0000688 canvas.drawColor(BG_COLOR);
reed@android.com63e34c62009-10-06 21:19:18 +0000689 gProc[i](&canvas);
690 canvas.restore();
691 SkString str;
senorblanco@chromium.org3a67a662012-07-09 18:22:08 +0000692 str.printf("/skimages/slide_" SK_SIZE_T_SPECIFIER ".png", i);
reed@android.com63e34c62009-10-06 21:19:18 +0000693 SkImageEncoder::EncodeFile(str.c_str(), bm, SkImageEncoder::kPNG_Type, 100);
694 }
reed@google.com81e3d7f2011-06-01 12:42:36 +0000695 this->setBGColor(BG_COLOR);
reed@android.com63e34c62009-10-06 21:19:18 +0000696 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000697
reed@android.com63e34c62009-10-06 21:19:18 +0000698protected:
699 // overrides from SkEventSink
700 virtual bool onQuery(SkEvent* evt) {
701 if (SampleCode::TitleQ(*evt)) {
702 SampleCode::TitleR(evt, "Slides");
703 return true;
704 }
705 return this->INHERITED::onQuery(evt);
706 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000707
reed@google.com81e3d7f2011-06-01 12:42:36 +0000708 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com7b185902012-03-12 21:13:48 +0000709 this->init();
reed@android.com63e34c62009-10-06 21:19:18 +0000710 gProc[fIndex](canvas);
711 }
712
sugoi@google.com9c55f802013-03-07 20:52:59 +0000713 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) SK_OVERRIDE {
reed@google.com7b185902012-03-12 21:13:48 +0000714 this->init();
reed@android.com63e34c62009-10-06 21:19:18 +0000715 fIndex = (fIndex + 1) % SK_ARRAY_COUNT(gProc);
716 this->inval(NULL);
717 return NULL;
718 }
719
720private:
reed@google.com81e3d7f2011-06-01 12:42:36 +0000721 typedef SampleView INHERITED;
reed@android.com63e34c62009-10-06 21:19:18 +0000722};
723
724//////////////////////////////////////////////////////////////////////////////
725
726static SkView* MyFactory() { return new SlideView; }
727static SkViewRegister reg(MyFactory);