blob: 5858d2674e87a989166a5613f594ed677fbf4c88 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@android.com63e34c62009-10-06 21:19:18 +00007#include "SampleCode.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00008#include "SkBlurMask.h"
9#include "SkBlurMaskFilter.h"
reed@android.com63e34c62009-10-06 21:19:18 +000010#include "SkCanvas.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000011#include "SkReadBuffer.h"
12#include "SkWriteBuffer.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000013#include "SkGradientShader.h"
14#include "SkLayerRasterizer.h"
reed@android.com63e34c62009-10-06 21:19:18 +000015#include "SkPaint.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000016#include "SkView.h"
reed@android.com63e34c62009-10-06 21:19:18 +000017
reed@android.com8af96022009-10-12 17:02:22 +000018#define BG_COLOR 0xFFDDDDDD
19
reed@android.com63e34c62009-10-06 21:19:18 +000020typedef void (*SlideProc)(SkCanvas*);
21
22///////////////////////////////////////////////////////////////////////////////
23
24#include "Sk1DPathEffect.h"
25#include "Sk2DPathEffect.h"
26#include "SkCornerPathEffect.h"
27#include "SkDashPathEffect.h"
28#include "SkDiscretePathEffect.h"
29
30static void compose_pe(SkPaint* paint) {
31 SkPathEffect* pe = paint->getPathEffect();
reeda4393342016-03-18 11:22:57 -070032 sk_sp<SkPathEffect> corner = SkCornerPathEffect::Make(25);
33 sk_sp<SkPathEffect> compose;
reed@android.com63e34c62009-10-06 21:19:18 +000034 if (pe) {
reeda4393342016-03-18 11:22:57 -070035 compose = SkComposePathEffect::Make(sk_ref_sp(pe), corner);
reed@android.com63e34c62009-10-06 21:19:18 +000036 } else {
37 compose = corner;
38 }
reeda4393342016-03-18 11:22:57 -070039 paint->setPathEffect(compose);
reed@android.com63e34c62009-10-06 21:19:18 +000040}
41
42static void hair_pe(SkPaint* paint) {
43 paint->setStrokeWidth(0);
44}
45
46static void hair2_pe(SkPaint* paint) {
47 paint->setStrokeWidth(0);
48 compose_pe(paint);
49}
50
51static void stroke_pe(SkPaint* paint) {
52 paint->setStrokeWidth(12);
53 compose_pe(paint);
54}
55
56static void dash_pe(SkPaint* paint) {
57 SkScalar inter[] = { 20, 10, 10, 10 };
58 paint->setStrokeWidth(12);
reeda4393342016-03-18 11:22:57 -070059 paint->setPathEffect(SkDashPathEffect::Make(inter, SK_ARRAY_COUNT(inter), 0));
reed@android.com63e34c62009-10-06 21:19:18 +000060 compose_pe(paint);
61}
62
63static const int gXY[] = {
644, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
65};
66
67static void scale(SkPath* path, SkScalar scale) {
68 SkMatrix m;
69 m.setScale(scale, scale);
70 path->transform(m);
71}
72
73static void one_d_pe(SkPaint* paint) {
74 SkPath path;
75 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
76 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
77 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
78 path.close();
79 path.offset(SkIntToScalar(-6), 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000080 scale(&path, 1.5f);
rmistry@google.comae933ce2012-08-23 18:19:56 +000081
reeda4393342016-03-18 11:22:57 -070082 paint->setPathEffect(SkPath1DPathEffect::Make(path, SkIntToScalar(21), 0,
83 SkPath1DPathEffect::kRotate_Style));
reed@android.com63e34c62009-10-06 21:19:18 +000084 compose_pe(paint);
85}
86
87typedef void (*PE_Proc)(SkPaint*);
88static const PE_Proc gPE[] = { hair_pe, hair2_pe, stroke_pe, dash_pe, one_d_pe };
89
90static void fill_pe(SkPaint* paint) {
91 paint->setStyle(SkPaint::kFill_Style);
halcanary96fcdcc2015-08-27 07:41:13 -070092 paint->setPathEffect(nullptr);
reed@android.com63e34c62009-10-06 21:19:18 +000093}
94
95static void discrete_pe(SkPaint* paint) {
reeda4393342016-03-18 11:22:57 -070096 paint->setPathEffect(SkDiscretePathEffect::Make(10, 4));
reed@android.com63e34c62009-10-06 21:19:18 +000097}
98
reeda4393342016-03-18 11:22:57 -070099static sk_sp<SkPathEffect> MakeTileEffect() {
reed@google.com43e9f202011-08-09 19:01:50 +0000100 SkMatrix m;
101 m.setScale(SkIntToScalar(12), SkIntToScalar(12));
reed@android.com63e34c62009-10-06 21:19:18 +0000102
reed@google.com43e9f202011-08-09 19:01:50 +0000103 SkPath path;
104 path.addCircle(0, 0, SkIntToScalar(5));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000105
reeda4393342016-03-18 11:22:57 -0700106 return SkPath2DPathEffect::Make(m, path);
reed@google.com43e9f202011-08-09 19:01:50 +0000107}
reed@android.com63e34c62009-10-06 21:19:18 +0000108
109static void tile_pe(SkPaint* paint) {
reeda4393342016-03-18 11:22:57 -0700110 paint->setPathEffect(MakeTileEffect());
reed@android.com63e34c62009-10-06 21:19:18 +0000111}
112
113static const PE_Proc gPE2[] = { fill_pe, discrete_pe, tile_pe };
114
115static void patheffect_slide(SkCanvas* canvas) {
116 SkPaint paint;
117 paint.setAntiAlias(true);
118 paint.setStyle(SkPaint::kStroke_Style);
119
120 SkPath path;
121 path.moveTo(20, 20);
122 path.lineTo(70, 120);
123 path.lineTo(120, 30);
124 path.lineTo(170, 80);
125 path.lineTo(240, 50);
126
127 size_t i;
128 canvas->save();
129 for (i = 0; i < SK_ARRAY_COUNT(gPE); i++) {
130 gPE[i](&paint);
131 canvas->drawPath(path, paint);
132 canvas->translate(0, 75);
133 }
134 canvas->restore();
135
136 path.reset();
137 SkRect r = { 0, 0, 250, 120 };
138 path.addOval(r, SkPath::kCW_Direction);
139 r.inset(50, 50);
140 path.addRect(r, SkPath::kCCW_Direction);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000141
reed@android.com63e34c62009-10-06 21:19:18 +0000142 canvas->translate(320, 20);
143 for (i = 0; i < SK_ARRAY_COUNT(gPE2); i++) {
144 gPE2[i](&paint);
145 canvas->drawPath(path, paint);
146 canvas->translate(0, 160);
147 }
148}
149
150///////////////////////////////////////////////////////////////////////////////
151
152#include "SkGradientShader.h"
153
154struct GradData {
155 int fCount;
156 const SkColor* fColors;
157 const SkScalar* fPos;
158};
159
160static const SkColor gColors[] = {
161SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
162};
163static const SkScalar gPos0[] = { 0, SK_Scalar1 };
164static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
165static const SkScalar gPos2[] = {
1660, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1
167};
168
169static const GradData gGradData[] = {
halcanary96fcdcc2015-08-27 07:41:13 -0700170{ 2, gColors, nullptr },
reed@android.com63e34c62009-10-06 21:19:18 +0000171{ 2, gColors, gPos0 },
172{ 2, gColors, gPos1 },
halcanary96fcdcc2015-08-27 07:41:13 -0700173{ 5, gColors, nullptr },
reed@android.com63e34c62009-10-06 21:19:18 +0000174{ 5, gColors, gPos2 }
175};
176
reed8a21c9f2016-03-08 18:50:00 -0800177static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
178 return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm);
reed@android.com63e34c62009-10-06 21:19:18 +0000179}
180
reed8a21c9f2016-03-08 18:50:00 -0800181static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
reed@android.com63e34c62009-10-06 21:19:18 +0000182 SkPoint center;
183 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
184 SkScalarAve(pts[0].fY, pts[1].fY));
reed8a21c9f2016-03-08 18:50:00 -0800185 return SkGradientShader::MakeRadial(center, center.fX, data.fColors,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000186 data.fPos, data.fCount, tm);
reed@android.com63e34c62009-10-06 21:19:18 +0000187}
188
reed8a21c9f2016-03-08 18:50:00 -0800189static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
reed@android.com63e34c62009-10-06 21:19:18 +0000190 SkPoint center;
191 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
192 SkScalarAve(pts[0].fY, pts[1].fY));
reed8a21c9f2016-03-08 18:50:00 -0800193 return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount);
reed@android.com63e34c62009-10-06 21:19:18 +0000194}
195
reed8a21c9f2016-03-08 18:50:00 -0800196static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
reed@android.com63e34c62009-10-06 21:19:18 +0000197 SkPoint center0, center1;
198 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
199 SkScalarAve(pts[0].fY, pts[1].fY));
200 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
201 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
reed8a21c9f2016-03-08 18:50:00 -0800202 return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7,
reed@android.com63e34c62009-10-06 21:19:18 +0000203 center0, (pts[1].fX - pts[0].fX) / 2,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000204 data.fColors, data.fPos, data.fCount, tm);
reed@android.com63e34c62009-10-06 21:19:18 +0000205}
206
reed8a21c9f2016-03-08 18:50:00 -0800207typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData&, SkShader::TileMode);
reed@android.com63e34c62009-10-06 21:19:18 +0000208static const GradMaker gGradMakers[] = {
reed71a6cbf2015-05-04 08:32:51 -0700209 MakeLinear, MakeRadial, MakeSweep, Make2Conical
reed@android.com63e34c62009-10-06 21:19:18 +0000210};
211
212static void gradient_slide(SkCanvas* canvas) {
213 SkPoint pts[2] = {
214 { 0, 0 },
215 { SkIntToScalar(100), SkIntToScalar(100) }
216 };
217 SkShader::TileMode tm = SkShader::kClamp_TileMode;
218 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
219 SkPaint paint;
220 paint.setAntiAlias(true);
221 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000222
reed@android.com63e34c62009-10-06 21:19:18 +0000223 canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
224 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
225 canvas->save();
226 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
reed8a21c9f2016-03-08 18:50:00 -0800227 paint.setShader(gGradMakers[j](pts, gGradData[i], tm));
reed@android.com63e34c62009-10-06 21:19:18 +0000228 canvas->drawRect(r, paint);
reed@android.com63e34c62009-10-06 21:19:18 +0000229 canvas->translate(0, SkIntToScalar(120));
230 }
231 canvas->restore();
232 canvas->translate(SkIntToScalar(120), 0);
233 }
234}
235
236///////////////////////////////////////////////////////////////////////////////
237
238#include "SkPathMeasure.h"
239
240static SkScalar getpathlen(const SkPath& path) {
241 SkPathMeasure meas(path, false);
242 return meas.getLength();
243}
244
245static void textonpath_slide(SkCanvas* canvas) {
246 const char* text = "Displacement";
247 size_t len =strlen(text);
248 SkPath path;
249 path.moveTo(100, 300);
250 path.quadTo(300, 100, 500, 300);
251 path.offset(0, -100);
252
253 SkPaint paint;
254 paint.setAntiAlias(true);
255 paint.setTextSize(40);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000256
reed@android.com63e34c62009-10-06 21:19:18 +0000257 paint.setStyle(SkPaint::kStroke_Style);
258 canvas->drawPath(path, paint);
259 paint.setStyle(SkPaint::kFill_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000260
reed@android.com63e34c62009-10-06 21:19:18 +0000261 SkScalar x = 50;
262 paint.setColor(0xFF008800);
263 canvas->drawTextOnPathHV(text, len, path,
264 x, paint.getTextSize()*2/3, paint);
265 paint.setColor(SK_ColorRED);
266 canvas->drawTextOnPathHV(text, len, path,
rmistry@google.comae933ce2012-08-23 18:19:56 +0000267 x + 60, 0, paint);
reed@android.com63e34c62009-10-06 21:19:18 +0000268 paint.setColor(SK_ColorBLUE);
269 canvas->drawTextOnPathHV(text, len, path,
270 x + 120, -paint.getTextSize()*2/3, paint);
271
272 path.offset(0, 200);
273 paint.setTextAlign(SkPaint::kRight_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000274
reed@android.com63e34c62009-10-06 21:19:18 +0000275 text = "Matrices";
276 len = strlen(text);
277 SkScalar pathLen = getpathlen(path);
278 SkMatrix matrix;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000279
reed@android.com63e34c62009-10-06 21:19:18 +0000280 paint.setColor(SK_ColorBLACK);
281 paint.setStyle(SkPaint::kStroke_Style);
282 canvas->drawPath(path, paint);
283 paint.setStyle(SkPaint::kFill_Style);
284
285 paint.setTextSize(50);
halcanary96fcdcc2015-08-27 07:41:13 -0700286 canvas->drawTextOnPath(text, len, path, nullptr, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000287
reed@android.com63e34c62009-10-06 21:19:18 +0000288 paint.setColor(SK_ColorRED);
289 matrix.setScale(-SK_Scalar1, SK_Scalar1);
290 matrix.postTranslate(pathLen, 0);
291 canvas->drawTextOnPath(text, len, path, &matrix, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000292
reed@android.com63e34c62009-10-06 21:19:18 +0000293 paint.setColor(SK_ColorBLUE);
294 matrix.setScale(SK_Scalar1, -SK_Scalar1);
295 canvas->drawTextOnPath(text, len, path, &matrix, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000296
reed@android.com63e34c62009-10-06 21:19:18 +0000297 paint.setColor(0xFF008800);
298 matrix.setScale(-SK_Scalar1, -SK_Scalar1);
299 matrix.postTranslate(pathLen, 0);
300 canvas->drawTextOnPath(text, len, path, &matrix, paint);
301}
302
303///////////////////////////////////////////////////////////////////////////////
304
msarettd15750c2016-03-18 15:48:49 -0700305#include "DecodeFile.h"
reed@android.com63e34c62009-10-06 21:19:18 +0000306#include "SkOSFile.h"
307#include "SkRandom.h"
308#include "SkStream.h"
reed@android.com63e34c62009-10-06 21:19:18 +0000309
reed8a21c9f2016-03-08 18:50:00 -0800310static sk_sp<SkShader> make_shader0(SkIPoint* size) {
reed@android.com63e34c62009-10-06 21:19:18 +0000311 SkBitmap bm;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000312
msarettd15750c2016-03-18 15:48:49 -0700313 decode_file("/skimages/logo.gif", &bm);
reed@android.com63e34c62009-10-06 21:19:18 +0000314 size->set(bm.width(), bm.height());
reed8a21c9f2016-03-08 18:50:00 -0800315 return SkShader::MakeBitmapShader(bm, SkShader::kClamp_TileMode,
reed@android.com63e34c62009-10-06 21:19:18 +0000316 SkShader::kClamp_TileMode);
317}
318
reed8a21c9f2016-03-08 18:50:00 -0800319static sk_sp<SkShader> make_shader1(const SkIPoint& size) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000320 SkPoint pts[] = { { 0, 0 },
321 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
reed@android.com63e34c62009-10-06 21:19:18 +0000322 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
reed8a21c9f2016-03-08 18:50:00 -0800323 return SkGradientShader::MakeLinear(pts, colors, nullptr,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000324 SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode);
reed@android.com63e34c62009-10-06 21:19:18 +0000325}
326
reed@android.comf2b98d62010-12-20 18:26:13 +0000327class Rec {
328public:
reed@android.com63e34c62009-10-06 21:19:18 +0000329 SkCanvas::VertexMode fMode;
330 int fCount;
331 SkPoint* fVerts;
332 SkPoint* fTexs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000333
halcanary96fcdcc2015-08-27 07:41:13 -0700334 Rec() : fCount(0), fVerts(nullptr), fTexs(nullptr) {}
reed@android.com63e34c62009-10-06 21:19:18 +0000335 ~Rec() { delete[] fVerts; delete[] fTexs; }
336};
337
caryclark@google.com02939ce2012-06-06 12:09:51 +0000338static void make_tris(Rec* rec) {
reed@android.com63e34c62009-10-06 21:19:18 +0000339 int n = 10;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000340 SkRandom rand;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000341
reed@android.com63e34c62009-10-06 21:19:18 +0000342 rec->fMode = SkCanvas::kTriangles_VertexMode;
343 rec->fCount = n * 3;
344 rec->fVerts = new SkPoint[rec->fCount];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000345
reed@android.com63e34c62009-10-06 21:19:18 +0000346 for (int i = 0; i < n; i++) {
347 SkPoint* v = &rec->fVerts[i*3];
348 for (int j = 0; j < 3; j++) {
349 v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250);
350 }
351 }
352}
353
caryclark@google.com02939ce2012-06-06 12:09:51 +0000354static void make_fan(Rec* rec, int texWidth, int texHeight) {
reed@android.com63e34c62009-10-06 21:19:18 +0000355 const SkScalar tx = SkIntToScalar(texWidth);
356 const SkScalar ty = SkIntToScalar(texHeight);
357 const int n = 24;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000358
reed@android.com63e34c62009-10-06 21:19:18 +0000359 rec->fMode = SkCanvas::kTriangleFan_VertexMode;
360 rec->fCount = n + 2;
361 rec->fVerts = new SkPoint[rec->fCount];
362 rec->fTexs = new SkPoint[rec->fCount];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000363
reed@android.com63e34c62009-10-06 21:19:18 +0000364 SkPoint* v = rec->fVerts;
365 SkPoint* t = rec->fTexs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000366
reed@android.com63e34c62009-10-06 21:19:18 +0000367 v[0].set(0, 0);
368 t[0].set(0, 0);
369 for (int i = 0; i < n; i++) {
370 SkScalar cos;
371 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
372 v[i+1].set(cos, sin);
373 t[i+1].set(i*tx/n, ty);
374 }
375 v[n+1] = v[1];
376 t[n+1].set(tx, ty);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000377
reed@android.com63e34c62009-10-06 21:19:18 +0000378 SkMatrix m;
379 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
380 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
381 m.mapPoints(v, rec->fCount);
382}
383
caryclark@google.com02939ce2012-06-06 12:09:51 +0000384static void make_strip(Rec* rec, int texWidth, int texHeight) {
reed@android.com63e34c62009-10-06 21:19:18 +0000385 const SkScalar tx = SkIntToScalar(texWidth);
386 const SkScalar ty = SkIntToScalar(texHeight);
387 const int n = 24;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000388
reed@android.com63e34c62009-10-06 21:19:18 +0000389 rec->fMode = SkCanvas::kTriangleStrip_VertexMode;
390 rec->fCount = 2 * (n + 1);
391 rec->fVerts = new SkPoint[rec->fCount];
392 rec->fTexs = new SkPoint[rec->fCount];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000393
reed@android.com63e34c62009-10-06 21:19:18 +0000394 SkPoint* v = rec->fVerts;
395 SkPoint* t = rec->fTexs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000396
reed@android.com63e34c62009-10-06 21:19:18 +0000397 for (int i = 0; i < n; i++) {
398 SkScalar cos;
399 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
400 v[i*2 + 0].set(cos/2, sin/2);
401 v[i*2 + 1].set(cos, sin);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000402
reed@android.com63e34c62009-10-06 21:19:18 +0000403 t[i*2 + 0].set(tx * i / n, ty);
404 t[i*2 + 1].set(tx * i / n, 0);
405 }
406 v[2*n + 0] = v[0];
407 v[2*n + 1] = v[1];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000408
reed@android.com63e34c62009-10-06 21:19:18 +0000409 t[2*n + 0].set(tx, ty);
410 t[2*n + 1].set(tx, 0);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000411
reed@android.com63e34c62009-10-06 21:19:18 +0000412 SkMatrix m;
413 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
414 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
415 m.mapPoints(v, rec->fCount);
416}
417
418static void mesh_slide(SkCanvas* canvas) {
419 Rec fRecs[3];
420 SkIPoint size;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000421
reed8a21c9f2016-03-08 18:50:00 -0800422 auto fShader0 = make_shader0(&size);
423 auto fShader1 = make_shader1(size);
reed@android.comf2b98d62010-12-20 18:26:13 +0000424
reed@android.com63e34c62009-10-06 21:19:18 +0000425 make_strip(&fRecs[0], size.fX, size.fY);
426 make_fan(&fRecs[1], size.fX, size.fY);
427 make_tris(&fRecs[2]);
428
reed@android.com63e34c62009-10-06 21:19:18 +0000429 SkPaint paint;
430 paint.setDither(true);
reed93a12152015-03-16 10:08:34 -0700431 paint.setFilterQuality(kLow_SkFilterQuality);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000432
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000433 for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) {
reed@android.com63e34c62009-10-06 21:19:18 +0000434 canvas->save();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000435
halcanary96fcdcc2015-08-27 07:41:13 -0700436 paint.setShader(nullptr);
reed@android.com63e34c62009-10-06 21:19:18 +0000437 canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount,
438 fRecs[i].fVerts, fRecs[i].fTexs,
halcanary96fcdcc2015-08-27 07:41:13 -0700439 nullptr, nullptr, nullptr, 0, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000440
reed@android.com63e34c62009-10-06 21:19:18 +0000441 canvas->translate(SkIntToScalar(210), 0);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000442
reed@android.com63e34c62009-10-06 21:19:18 +0000443 paint.setShader(fShader0);
444 canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount,
445 fRecs[i].fVerts, fRecs[i].fTexs,
halcanary96fcdcc2015-08-27 07:41:13 -0700446 nullptr, nullptr, nullptr, 0, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000447
reed@android.com63e34c62009-10-06 21:19:18 +0000448 canvas->translate(SkIntToScalar(210), 0);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000449
reed@android.com63e34c62009-10-06 21:19:18 +0000450 paint.setShader(fShader1);
451 canvas->drawVertices(fRecs[i].fMode, fRecs[i].fCount,
452 fRecs[i].fVerts, fRecs[i].fTexs,
halcanary96fcdcc2015-08-27 07:41:13 -0700453 nullptr, nullptr, nullptr, 0, paint);
reed@android.com63e34c62009-10-06 21:19:18 +0000454 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000455
reed@android.com63e34c62009-10-06 21:19:18 +0000456 canvas->translate(0, SkIntToScalar(250));
457 }
458}
459
460///////////////////////////////////////////////////////////////////////////////
461
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000462static void r0(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000463{
reedefdfd512016-04-04 10:02:58 -0700464 p.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
465 SkBlurMask::ConvertRadiusToSigma(3)));
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000466 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000467
halcanary96fcdcc2015-08-27 07:41:13 -0700468 p.setMaskFilter(nullptr);
reed@android.com8af96022009-10-12 17:02:22 +0000469 p.setStyle(SkPaint::kStroke_Style);
470 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000471 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000472
reed@android.com8af96022009-10-12 17:02:22 +0000473 p.setAlpha(0x11);
474 p.setStyle(SkPaint::kFill_Style);
475 p.setXfermodeMode(SkXfermode::kSrc_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000476 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000477}
478
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000479static void r1(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000480{
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(0x40);
484 p.setXfermodeMode(SkXfermode::kSrc_Mode);
485 p.setStyle(SkPaint::kStroke_Style);
486 p.setStrokeWidth(SK_Scalar1*2);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000487 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000488}
489
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000490static void r2(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000491{
492 p.setStyle(SkPaint::kStrokeAndFill_Style);
493 p.setStrokeWidth(SK_Scalar1*4);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000494 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000495
reed@android.com8af96022009-10-12 17:02:22 +0000496 p.setStyle(SkPaint::kStroke_Style);
497 p.setStrokeWidth(SK_Scalar1*3/2);
498 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000499 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000500}
501
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000502static void r3(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000503{
504 p.setStyle(SkPaint::kStroke_Style);
505 p.setStrokeWidth(SK_Scalar1*3);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000506 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000507
reed@android.com8af96022009-10-12 17:02:22 +0000508 p.setAlpha(0x20);
509 p.setStyle(SkPaint::kFill_Style);
510 p.setXfermodeMode(SkXfermode::kSrc_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000511 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000512}
513
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000514static void r4(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000515{
516 p.setAlpha(0x60);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000517 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000518
reed@android.com8af96022009-10-12 17:02:22 +0000519 p.setAlpha(0xFF);
520 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000521 rastBuilder->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000522
halcanary96fcdcc2015-08-27 07:41:13 -0700523 p.setXfermode(nullptr);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000524 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000525}
526
527#include "SkDiscretePathEffect.h"
528
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000529static void r5(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000530{
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000531 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000532
reeda4393342016-03-18 11:22:57 -0700533 p.setPathEffect(SkDiscretePathEffect::Make(SK_Scalar1*4, SK_Scalar1*3));
reed@android.com8af96022009-10-12 17:02:22 +0000534 p.setXfermodeMode(SkXfermode::kSrcOut_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000535 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000536}
537
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000538static void r6(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000539{
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000540 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000541
reed@android.com8af96022009-10-12 17:02:22 +0000542 p.setAntiAlias(false);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000543 SkLayerRasterizer::Builder rastBuilder2;
544 r5(&rastBuilder2, p);
reed7b380d02016-03-21 13:25:16 -0700545 p.setRasterizer(rastBuilder2.detach());
reed@android.com8af96022009-10-12 17:02:22 +0000546 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000547 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000548}
549
550#include "Sk2DPathEffect.h"
551
reeda4393342016-03-18 11:22:57 -0700552static sk_sp<SkPathEffect> MakeDotEffect(SkScalar radius, const SkMatrix& matrix) {
reed@google.com18dc4772011-08-09 18:47:40 +0000553 SkPath path;
554 path.addCircle(0, 0, radius);
reeda4393342016-03-18 11:22:57 -0700555 return SkPath2DPathEffect::Make(matrix, path);
reed@google.com18dc4772011-08-09 18:47:40 +0000556}
reed@android.com8af96022009-10-12 17:02:22 +0000557
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000558static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000559{
560 SkMatrix lattice;
561 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
562 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reeda4393342016-03-18 11:22:57 -0700563 p.setPathEffect(MakeDotEffect(SK_Scalar1*4, lattice));
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000564 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000565}
566
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000567static void r8(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000568{
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000569 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000570
reed@android.com8af96022009-10-12 17:02:22 +0000571 SkMatrix lattice;
572 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
573 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reeda4393342016-03-18 11:22:57 -0700574 p.setPathEffect(MakeDotEffect(SK_Scalar1*2, lattice));
reed@android.com8af96022009-10-12 17:02:22 +0000575 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000576 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000577
halcanary96fcdcc2015-08-27 07:41:13 -0700578 p.setPathEffect(nullptr);
579 p.setXfermode(nullptr);
reed@android.com8af96022009-10-12 17:02:22 +0000580 p.setStyle(SkPaint::kStroke_Style);
581 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000582 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000583}
584
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000585static void r9(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000586{
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000587 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000588
reed@android.com8af96022009-10-12 17:02:22 +0000589 SkMatrix lattice;
590 lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
591 lattice.postRotate(SkIntToScalar(30), 0, 0);
reeda4393342016-03-18 11:22:57 -0700592 p.setPathEffect(SkLine2DPathEffect::Make(SK_Scalar1*2, lattice));
reed@android.com8af96022009-10-12 17:02:22 +0000593 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000594 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000595
halcanary96fcdcc2015-08-27 07:41:13 -0700596 p.setPathEffect(nullptr);
597 p.setXfermode(nullptr);
reed@android.com8af96022009-10-12 17:02:22 +0000598 p.setStyle(SkPaint::kStroke_Style);
599 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000600 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000601}
602
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000603typedef void (*raster_proc)(SkLayerRasterizer::Builder*, SkPaint&);
reed@android.com8af96022009-10-12 17:02:22 +0000604
605static const raster_proc gRastProcs[] = {
606 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
607};
608
rmistry@google.comae933ce2012-08-23 18:19:56 +0000609static void apply_shader(SkPaint* paint, int index) {
reed@android.com8af96022009-10-12 17:02:22 +0000610 raster_proc proc = gRastProcs[index];
611 SkPaint p;
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000612 SkLayerRasterizer::Builder rastBuilder;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000613
reed@android.com8af96022009-10-12 17:02:22 +0000614 p.setAntiAlias(true);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000615 proc(&rastBuilder, p);
reed7b380d02016-03-21 13:25:16 -0700616 paint->setRasterizer(rastBuilder.detach());
reed@android.com8af96022009-10-12 17:02:22 +0000617 paint->setColor(SK_ColorBLUE);
618}
619
620#include "SkTypeface.h"
621
622static void texteffect_slide(SkCanvas* canvas) {
623 const char* str = "Google";
624 size_t len = strlen(str);
625 SkScalar x = 20;
626 SkScalar y = 80;
627 SkPaint paint;
mbocee6a9912016-05-31 11:42:36 -0700628 paint.setTypeface(SkTypeface::MakeFromName("Georgia",
629 SkFontStyle::FromOldStyle(SkTypeface::kItalic)));
reed@android.com8af96022009-10-12 17:02:22 +0000630 paint.setTextSize(75);
631 paint.setAntiAlias(true);
632 paint.setColor(SK_ColorBLUE);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000633 for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) {
reed@google.com7fa2a652014-01-27 13:42:58 +0000634 apply_shader(&paint, (int)i);
reed@android.com8af96022009-10-12 17:02:22 +0000635 canvas->drawText(str, len, x, y, paint);
636 y += 80;
637 if (i == 4) {
638 x += 320;
639 y = 80;
640 }
641 }
642}
643
644///////////////////////////////////////////////////////////////////////////////
645
reed@android.com63e34c62009-10-06 21:19:18 +0000646#include "SkImageEncoder.h"
647
648static const SlideProc gProc[] = {
649 patheffect_slide,
650 gradient_slide,
651 textonpath_slide,
reed@android.com8af96022009-10-12 17:02:22 +0000652 mesh_slide,
653 texteffect_slide
reed@android.com63e34c62009-10-06 21:19:18 +0000654};
655
reed@google.com81e3d7f2011-06-01 12:42:36 +0000656class SlideView : public SampleView {
reed@android.com63e34c62009-10-06 21:19:18 +0000657 int fIndex;
reed@google.com7b185902012-03-12 21:13:48 +0000658 bool fOnce;
reed@android.com63e34c62009-10-06 21:19:18 +0000659public:
660 SlideView() {
reed@google.com7b185902012-03-12 21:13:48 +0000661 fOnce = false;
662 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000663
reed@google.com7b185902012-03-12 21:13:48 +0000664 void init() {
665 if (fOnce) {
666 return;
667 }
668 fOnce = true;
669
reed@android.com63e34c62009-10-06 21:19:18 +0000670 fIndex = 0;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000671
reed@android.com63e34c62009-10-06 21:19:18 +0000672 SkBitmap bm;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000673 bm.allocN32Pixels(1024, 768);
reed@android.com63e34c62009-10-06 21:19:18 +0000674 SkCanvas canvas(bm);
675 SkScalar s = SkIntToScalar(1024) / 640;
676 canvas.scale(s, s);
677 for (size_t i = 0; i < SK_ARRAY_COUNT(gProc); i++) {
678 canvas.save();
reed@android.com8af96022009-10-12 17:02:22 +0000679 canvas.drawColor(BG_COLOR);
reed@android.com63e34c62009-10-06 21:19:18 +0000680 gProc[i](&canvas);
681 canvas.restore();
682 SkString str;
senorblanco@chromium.org3a67a662012-07-09 18:22:08 +0000683 str.printf("/skimages/slide_" SK_SIZE_T_SPECIFIER ".png", i);
reed@android.com63e34c62009-10-06 21:19:18 +0000684 SkImageEncoder::EncodeFile(str.c_str(), bm, SkImageEncoder::kPNG_Type, 100);
685 }
reed@google.com81e3d7f2011-06-01 12:42:36 +0000686 this->setBGColor(BG_COLOR);
reed@android.com63e34c62009-10-06 21:19:18 +0000687 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000688
reed@android.com63e34c62009-10-06 21:19:18 +0000689protected:
690 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -0700691 bool onQuery(SkEvent* evt) override {
reed@android.com63e34c62009-10-06 21:19:18 +0000692 if (SampleCode::TitleQ(*evt)) {
693 SampleCode::TitleR(evt, "Slides");
694 return true;
695 }
696 return this->INHERITED::onQuery(evt);
697 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000698
mtklein36352bf2015-03-25 18:17:31 -0700699 void onDrawContent(SkCanvas* canvas) override {
reed@google.com7b185902012-03-12 21:13:48 +0000700 this->init();
reed@android.com63e34c62009-10-06 21:19:18 +0000701 gProc[fIndex](canvas);
702 }
703
mtklein36352bf2015-03-25 18:17:31 -0700704 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override {
reed@google.com7b185902012-03-12 21:13:48 +0000705 this->init();
reed@android.com63e34c62009-10-06 21:19:18 +0000706 fIndex = (fIndex + 1) % SK_ARRAY_COUNT(gProc);
halcanary96fcdcc2015-08-27 07:41:13 -0700707 this->inval(nullptr);
708 return nullptr;
reed@android.com63e34c62009-10-06 21:19:18 +0000709 }
710
711private:
reed@google.com81e3d7f2011-06-01 12:42:36 +0000712 typedef SampleView INHERITED;
reed@android.com63e34c62009-10-06 21:19:18 +0000713};
714
715//////////////////////////////////////////////////////////////////////////////
716
717static SkView* MyFactory() { return new SlideView; }
718static SkViewRegister reg(MyFactory);