blob: b8cf3800e0ade011410f854af579b95fe3efe843 [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"
Mike Reed887cdf12017-04-03 11:11:09 -040016#include "SkVertices.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000017#include "SkView.h"
reed@android.com63e34c62009-10-06 21:19:18 +000018
Hal Canarydb683012016-11-23 08:55:18 -070019#include "sk_tool_utils.h"
20
reed@android.com8af96022009-10-12 17:02:22 +000021#define BG_COLOR 0xFFDDDDDD
22
reed@android.com63e34c62009-10-06 21:19:18 +000023typedef void (*SlideProc)(SkCanvas*);
24
25///////////////////////////////////////////////////////////////////////////////
26
27#include "Sk1DPathEffect.h"
28#include "Sk2DPathEffect.h"
29#include "SkCornerPathEffect.h"
30#include "SkDashPathEffect.h"
31#include "SkDiscretePathEffect.h"
32
33static void compose_pe(SkPaint* paint) {
34 SkPathEffect* pe = paint->getPathEffect();
reeda4393342016-03-18 11:22:57 -070035 sk_sp<SkPathEffect> corner = SkCornerPathEffect::Make(25);
36 sk_sp<SkPathEffect> compose;
reed@android.com63e34c62009-10-06 21:19:18 +000037 if (pe) {
Mike Reeda07741a2017-02-25 22:34:32 -050038 compose = SkPathEffect::MakeCompose(sk_ref_sp(pe), corner);
reed@android.com63e34c62009-10-06 21:19:18 +000039 } else {
40 compose = corner;
41 }
reeda4393342016-03-18 11:22:57 -070042 paint->setPathEffect(compose);
reed@android.com63e34c62009-10-06 21:19:18 +000043}
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);
reeda4393342016-03-18 11:22:57 -070062 paint->setPathEffect(SkDashPathEffect::Make(inter, SK_ARRAY_COUNT(inter), 0));
reed@android.com63e34c62009-10-06 21:19:18 +000063 compose_pe(paint);
64}
65
66static const int gXY[] = {
674, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
68};
69
70static void scale(SkPath* path, SkScalar scale) {
71 SkMatrix m;
72 m.setScale(scale, scale);
73 path->transform(m);
74}
75
76static void one_d_pe(SkPaint* paint) {
77 SkPath path;
78 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
79 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
80 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
81 path.close();
82 path.offset(SkIntToScalar(-6), 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000083 scale(&path, 1.5f);
rmistry@google.comae933ce2012-08-23 18:19:56 +000084
reeda4393342016-03-18 11:22:57 -070085 paint->setPathEffect(SkPath1DPathEffect::Make(path, SkIntToScalar(21), 0,
86 SkPath1DPathEffect::kRotate_Style));
reed@android.com63e34c62009-10-06 21:19:18 +000087 compose_pe(paint);
88}
89
90typedef void (*PE_Proc)(SkPaint*);
91static const PE_Proc gPE[] = { hair_pe, hair2_pe, stroke_pe, dash_pe, one_d_pe };
92
93static void fill_pe(SkPaint* paint) {
94 paint->setStyle(SkPaint::kFill_Style);
halcanary96fcdcc2015-08-27 07:41:13 -070095 paint->setPathEffect(nullptr);
reed@android.com63e34c62009-10-06 21:19:18 +000096}
97
98static void discrete_pe(SkPaint* paint) {
reeda4393342016-03-18 11:22:57 -070099 paint->setPathEffect(SkDiscretePathEffect::Make(10, 4));
reed@android.com63e34c62009-10-06 21:19:18 +0000100}
101
reeda4393342016-03-18 11:22:57 -0700102static sk_sp<SkPathEffect> MakeTileEffect() {
reed@google.com43e9f202011-08-09 19:01:50 +0000103 SkMatrix m;
104 m.setScale(SkIntToScalar(12), SkIntToScalar(12));
reed@android.com63e34c62009-10-06 21:19:18 +0000105
reed@google.com43e9f202011-08-09 19:01:50 +0000106 SkPath path;
107 path.addCircle(0, 0, SkIntToScalar(5));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000108
reeda4393342016-03-18 11:22:57 -0700109 return SkPath2DPathEffect::Make(m, path);
reed@google.com43e9f202011-08-09 19:01:50 +0000110}
reed@android.com63e34c62009-10-06 21:19:18 +0000111
112static void tile_pe(SkPaint* paint) {
reeda4393342016-03-18 11:22:57 -0700113 paint->setPathEffect(MakeTileEffect());
reed@android.com63e34c62009-10-06 21:19:18 +0000114}
115
116static const PE_Proc gPE2[] = { fill_pe, discrete_pe, tile_pe };
117
118static void patheffect_slide(SkCanvas* canvas) {
119 SkPaint paint;
120 paint.setAntiAlias(true);
121 paint.setStyle(SkPaint::kStroke_Style);
122
123 SkPath path;
124 path.moveTo(20, 20);
125 path.lineTo(70, 120);
126 path.lineTo(120, 30);
127 path.lineTo(170, 80);
128 path.lineTo(240, 50);
129
130 size_t i;
131 canvas->save();
132 for (i = 0; i < SK_ARRAY_COUNT(gPE); i++) {
133 gPE[i](&paint);
134 canvas->drawPath(path, paint);
135 canvas->translate(0, 75);
136 }
137 canvas->restore();
138
139 path.reset();
140 SkRect r = { 0, 0, 250, 120 };
141 path.addOval(r, SkPath::kCW_Direction);
142 r.inset(50, 50);
143 path.addRect(r, SkPath::kCCW_Direction);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000144
reed@android.com63e34c62009-10-06 21:19:18 +0000145 canvas->translate(320, 20);
146 for (i = 0; i < SK_ARRAY_COUNT(gPE2); i++) {
147 gPE2[i](&paint);
148 canvas->drawPath(path, paint);
149 canvas->translate(0, 160);
150 }
151}
152
153///////////////////////////////////////////////////////////////////////////////
154
155#include "SkGradientShader.h"
156
157struct GradData {
158 int fCount;
159 const SkColor* fColors;
160 const SkScalar* fPos;
161};
162
163static const SkColor gColors[] = {
164SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
165};
166static const SkScalar gPos0[] = { 0, SK_Scalar1 };
167static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
168static const SkScalar gPos2[] = {
1690, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1
170};
171
172static const GradData gGradData[] = {
halcanary96fcdcc2015-08-27 07:41:13 -0700173{ 2, gColors, nullptr },
reed@android.com63e34c62009-10-06 21:19:18 +0000174{ 2, gColors, gPos0 },
175{ 2, gColors, gPos1 },
halcanary96fcdcc2015-08-27 07:41:13 -0700176{ 5, gColors, nullptr },
reed@android.com63e34c62009-10-06 21:19:18 +0000177{ 5, gColors, gPos2 }
178};
179
reed8a21c9f2016-03-08 18:50:00 -0800180static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
181 return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm);
reed@android.com63e34c62009-10-06 21:19:18 +0000182}
183
reed8a21c9f2016-03-08 18:50:00 -0800184static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
reed@android.com63e34c62009-10-06 21:19:18 +0000185 SkPoint center;
186 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
187 SkScalarAve(pts[0].fY, pts[1].fY));
reed8a21c9f2016-03-08 18:50:00 -0800188 return SkGradientShader::MakeRadial(center, center.fX, data.fColors,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000189 data.fPos, data.fCount, tm);
reed@android.com63e34c62009-10-06 21:19:18 +0000190}
191
reed8a21c9f2016-03-08 18:50:00 -0800192static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
reed@android.com63e34c62009-10-06 21:19:18 +0000193 SkPoint center;
194 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
195 SkScalarAve(pts[0].fY, pts[1].fY));
reed8a21c9f2016-03-08 18:50:00 -0800196 return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount);
reed@android.com63e34c62009-10-06 21:19:18 +0000197}
198
reed8a21c9f2016-03-08 18:50:00 -0800199static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
reed@android.com63e34c62009-10-06 21:19:18 +0000200 SkPoint center0, center1;
201 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
202 SkScalarAve(pts[0].fY, pts[1].fY));
203 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
204 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
reed8a21c9f2016-03-08 18:50:00 -0800205 return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7,
reed@android.com63e34c62009-10-06 21:19:18 +0000206 center0, (pts[1].fX - pts[0].fX) / 2,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000207 data.fColors, data.fPos, data.fCount, tm);
reed@android.com63e34c62009-10-06 21:19:18 +0000208}
209
reed8a21c9f2016-03-08 18:50:00 -0800210typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData&, SkShader::TileMode);
reed@android.com63e34c62009-10-06 21:19:18 +0000211static const GradMaker gGradMakers[] = {
reed71a6cbf2015-05-04 08:32:51 -0700212 MakeLinear, MakeRadial, MakeSweep, Make2Conical
reed@android.com63e34c62009-10-06 21:19:18 +0000213};
214
215static void gradient_slide(SkCanvas* canvas) {
216 SkPoint pts[2] = {
217 { 0, 0 },
218 { SkIntToScalar(100), SkIntToScalar(100) }
219 };
220 SkShader::TileMode tm = SkShader::kClamp_TileMode;
221 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
222 SkPaint paint;
223 paint.setAntiAlias(true);
224 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000225
reed@android.com63e34c62009-10-06 21:19:18 +0000226 canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
227 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
228 canvas->save();
229 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
reed8a21c9f2016-03-08 18:50:00 -0800230 paint.setShader(gGradMakers[j](pts, gGradData[i], tm));
reed@android.com63e34c62009-10-06 21:19:18 +0000231 canvas->drawRect(r, paint);
reed@android.com63e34c62009-10-06 21:19:18 +0000232 canvas->translate(0, SkIntToScalar(120));
233 }
234 canvas->restore();
235 canvas->translate(SkIntToScalar(120), 0);
236 }
237}
238
239///////////////////////////////////////////////////////////////////////////////
240
241#include "SkPathMeasure.h"
242
243static SkScalar getpathlen(const SkPath& path) {
244 SkPathMeasure meas(path, false);
245 return meas.getLength();
246}
247
248static void textonpath_slide(SkCanvas* canvas) {
249 const char* text = "Displacement";
250 size_t len =strlen(text);
251 SkPath path;
252 path.moveTo(100, 300);
253 path.quadTo(300, 100, 500, 300);
254 path.offset(0, -100);
255
256 SkPaint paint;
257 paint.setAntiAlias(true);
258 paint.setTextSize(40);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000259
reed@android.com63e34c62009-10-06 21:19:18 +0000260 paint.setStyle(SkPaint::kStroke_Style);
261 canvas->drawPath(path, paint);
262 paint.setStyle(SkPaint::kFill_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000263
reed@android.com63e34c62009-10-06 21:19:18 +0000264 SkScalar x = 50;
265 paint.setColor(0xFF008800);
266 canvas->drawTextOnPathHV(text, len, path,
267 x, paint.getTextSize()*2/3, paint);
268 paint.setColor(SK_ColorRED);
269 canvas->drawTextOnPathHV(text, len, path,
rmistry@google.comae933ce2012-08-23 18:19:56 +0000270 x + 60, 0, paint);
reed@android.com63e34c62009-10-06 21:19:18 +0000271 paint.setColor(SK_ColorBLUE);
272 canvas->drawTextOnPathHV(text, len, path,
273 x + 120, -paint.getTextSize()*2/3, paint);
274
275 path.offset(0, 200);
276 paint.setTextAlign(SkPaint::kRight_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000277
reed@android.com63e34c62009-10-06 21:19:18 +0000278 text = "Matrices";
279 len = strlen(text);
280 SkScalar pathLen = getpathlen(path);
281 SkMatrix matrix;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000282
reed@android.com63e34c62009-10-06 21:19:18 +0000283 paint.setColor(SK_ColorBLACK);
284 paint.setStyle(SkPaint::kStroke_Style);
285 canvas->drawPath(path, paint);
286 paint.setStyle(SkPaint::kFill_Style);
287
288 paint.setTextSize(50);
halcanary96fcdcc2015-08-27 07:41:13 -0700289 canvas->drawTextOnPath(text, len, path, nullptr, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000290
reed@android.com63e34c62009-10-06 21:19:18 +0000291 paint.setColor(SK_ColorRED);
292 matrix.setScale(-SK_Scalar1, SK_Scalar1);
293 matrix.postTranslate(pathLen, 0);
294 canvas->drawTextOnPath(text, len, path, &matrix, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000295
reed@android.com63e34c62009-10-06 21:19:18 +0000296 paint.setColor(SK_ColorBLUE);
297 matrix.setScale(SK_Scalar1, -SK_Scalar1);
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(0xFF008800);
301 matrix.setScale(-SK_Scalar1, -SK_Scalar1);
302 matrix.postTranslate(pathLen, 0);
303 canvas->drawTextOnPath(text, len, path, &matrix, paint);
304}
305
306///////////////////////////////////////////////////////////////////////////////
307
msarettd15750c2016-03-18 15:48:49 -0700308#include "DecodeFile.h"
reed@android.com63e34c62009-10-06 21:19:18 +0000309#include "SkOSFile.h"
310#include "SkRandom.h"
311#include "SkStream.h"
reed@android.com63e34c62009-10-06 21:19:18 +0000312
reed8a21c9f2016-03-08 18:50:00 -0800313static sk_sp<SkShader> make_shader0(SkIPoint* size) {
reed@android.com63e34c62009-10-06 21:19:18 +0000314 SkBitmap bm;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000315
msarettd15750c2016-03-18 15:48:49 -0700316 decode_file("/skimages/logo.gif", &bm);
reed@android.com63e34c62009-10-06 21:19:18 +0000317 size->set(bm.width(), bm.height());
reed8a21c9f2016-03-08 18:50:00 -0800318 return SkShader::MakeBitmapShader(bm, SkShader::kClamp_TileMode,
reed@android.com63e34c62009-10-06 21:19:18 +0000319 SkShader::kClamp_TileMode);
320}
321
reed8a21c9f2016-03-08 18:50:00 -0800322static sk_sp<SkShader> make_shader1(const SkIPoint& size) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000323 SkPoint pts[] = { { 0, 0 },
324 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
reed@android.com63e34c62009-10-06 21:19:18 +0000325 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
reed8a21c9f2016-03-08 18:50:00 -0800326 return SkGradientShader::MakeLinear(pts, colors, nullptr,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000327 SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode);
reed@android.com63e34c62009-10-06 21:19:18 +0000328}
329
reed@android.comf2b98d62010-12-20 18:26:13 +0000330class Rec {
331public:
Mike Reed887cdf12017-04-03 11:11:09 -0400332 SkVertices::VertexMode fMode;
reed@android.com63e34c62009-10-06 21:19:18 +0000333 int fCount;
334 SkPoint* fVerts;
335 SkPoint* fTexs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000336
halcanary96fcdcc2015-08-27 07:41:13 -0700337 Rec() : fCount(0), fVerts(nullptr), fTexs(nullptr) {}
reed@android.com63e34c62009-10-06 21:19:18 +0000338 ~Rec() { delete[] fVerts; delete[] fTexs; }
339};
340
caryclark@google.com02939ce2012-06-06 12:09:51 +0000341static void make_tris(Rec* rec) {
reed@android.com63e34c62009-10-06 21:19:18 +0000342 int n = 10;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000343 SkRandom rand;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000344
Mike Reed887cdf12017-04-03 11:11:09 -0400345 rec->fMode = SkVertices::kTriangles_VertexMode;
reed@android.com63e34c62009-10-06 21:19:18 +0000346 rec->fCount = n * 3;
347 rec->fVerts = new SkPoint[rec->fCount];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000348
reed@android.com63e34c62009-10-06 21:19:18 +0000349 for (int i = 0; i < n; i++) {
350 SkPoint* v = &rec->fVerts[i*3];
351 for (int j = 0; j < 3; j++) {
352 v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250);
353 }
354 }
355}
356
caryclark@google.com02939ce2012-06-06 12:09:51 +0000357static void make_fan(Rec* rec, int texWidth, int texHeight) {
reed@android.com63e34c62009-10-06 21:19:18 +0000358 const SkScalar tx = SkIntToScalar(texWidth);
359 const SkScalar ty = SkIntToScalar(texHeight);
360 const int n = 24;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000361
Mike Reed887cdf12017-04-03 11:11:09 -0400362 rec->fMode = SkVertices::kTriangleFan_VertexMode;
reed@android.com63e34c62009-10-06 21:19:18 +0000363 rec->fCount = n + 2;
364 rec->fVerts = new SkPoint[rec->fCount];
365 rec->fTexs = new SkPoint[rec->fCount];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000366
reed@android.com63e34c62009-10-06 21:19:18 +0000367 SkPoint* v = rec->fVerts;
368 SkPoint* t = rec->fTexs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000369
reed@android.com63e34c62009-10-06 21:19:18 +0000370 v[0].set(0, 0);
371 t[0].set(0, 0);
372 for (int i = 0; i < n; i++) {
373 SkScalar cos;
374 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
375 v[i+1].set(cos, sin);
376 t[i+1].set(i*tx/n, ty);
377 }
378 v[n+1] = v[1];
379 t[n+1].set(tx, ty);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000380
reed@android.com63e34c62009-10-06 21:19:18 +0000381 SkMatrix m;
382 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
383 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
384 m.mapPoints(v, rec->fCount);
385}
386
caryclark@google.com02939ce2012-06-06 12:09:51 +0000387static void make_strip(Rec* rec, int texWidth, int texHeight) {
reed@android.com63e34c62009-10-06 21:19:18 +0000388 const SkScalar tx = SkIntToScalar(texWidth);
389 const SkScalar ty = SkIntToScalar(texHeight);
390 const int n = 24;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000391
Mike Reed887cdf12017-04-03 11:11:09 -0400392 rec->fMode = SkVertices::kTriangleStrip_VertexMode;
reed@android.com63e34c62009-10-06 21:19:18 +0000393 rec->fCount = 2 * (n + 1);
394 rec->fVerts = new SkPoint[rec->fCount];
395 rec->fTexs = new SkPoint[rec->fCount];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000396
reed@android.com63e34c62009-10-06 21:19:18 +0000397 SkPoint* v = rec->fVerts;
398 SkPoint* t = rec->fTexs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000399
reed@android.com63e34c62009-10-06 21:19:18 +0000400 for (int i = 0; i < n; i++) {
401 SkScalar cos;
402 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
403 v[i*2 + 0].set(cos/2, sin/2);
404 v[i*2 + 1].set(cos, sin);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000405
reed@android.com63e34c62009-10-06 21:19:18 +0000406 t[i*2 + 0].set(tx * i / n, ty);
407 t[i*2 + 1].set(tx * i / n, 0);
408 }
409 v[2*n + 0] = v[0];
410 v[2*n + 1] = v[1];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000411
reed@android.com63e34c62009-10-06 21:19:18 +0000412 t[2*n + 0].set(tx, ty);
413 t[2*n + 1].set(tx, 0);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000414
reed@android.com63e34c62009-10-06 21:19:18 +0000415 SkMatrix m;
416 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
417 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
418 m.mapPoints(v, rec->fCount);
419}
420
421static void mesh_slide(SkCanvas* canvas) {
422 Rec fRecs[3];
423 SkIPoint size;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000424
reed8a21c9f2016-03-08 18:50:00 -0800425 auto fShader0 = make_shader0(&size);
426 auto fShader1 = make_shader1(size);
reed@android.comf2b98d62010-12-20 18:26:13 +0000427
reed@android.com63e34c62009-10-06 21:19:18 +0000428 make_strip(&fRecs[0], size.fX, size.fY);
429 make_fan(&fRecs[1], size.fX, size.fY);
430 make_tris(&fRecs[2]);
431
reed@android.com63e34c62009-10-06 21:19:18 +0000432 SkPaint paint;
433 paint.setDither(true);
reed93a12152015-03-16 10:08:34 -0700434 paint.setFilterQuality(kLow_SkFilterQuality);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000435
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000436 for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) {
Mike Reed887cdf12017-04-03 11:11:09 -0400437 auto verts = SkVertices::MakeCopy(fRecs[i].fMode, fRecs[i].fCount,
438 fRecs[i].fVerts, fRecs[i].fTexs, nullptr);
reed@android.com63e34c62009-10-06 21:19:18 +0000439 canvas->save();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000440
halcanary96fcdcc2015-08-27 07:41:13 -0700441 paint.setShader(nullptr);
Mike Reed887cdf12017-04-03 11:11:09 -0400442 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000443
reed@android.com63e34c62009-10-06 21:19:18 +0000444 canvas->translate(SkIntToScalar(210), 0);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000445
reed@android.com63e34c62009-10-06 21:19:18 +0000446 paint.setShader(fShader0);
Mike Reed887cdf12017-04-03 11:11:09 -0400447 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000448
reed@android.com63e34c62009-10-06 21:19:18 +0000449 canvas->translate(SkIntToScalar(210), 0);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000450
reed@android.com63e34c62009-10-06 21:19:18 +0000451 paint.setShader(fShader1);
Mike Reed887cdf12017-04-03 11:11:09 -0400452 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
reed@android.com63e34c62009-10-06 21:19:18 +0000453 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000454
reed@android.com63e34c62009-10-06 21:19:18 +0000455 canvas->translate(0, SkIntToScalar(250));
456 }
457}
458
459///////////////////////////////////////////////////////////////////////////////
460
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000461static void r0(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000462{
reedefdfd512016-04-04 10:02:58 -0700463 p.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
464 SkBlurMask::ConvertRadiusToSigma(3)));
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000465 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000466
halcanary96fcdcc2015-08-27 07:41:13 -0700467 p.setMaskFilter(nullptr);
reed@android.com8af96022009-10-12 17:02:22 +0000468 p.setStyle(SkPaint::kStroke_Style);
469 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000470 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000471
reed@android.com8af96022009-10-12 17:02:22 +0000472 p.setAlpha(0x11);
473 p.setStyle(SkPaint::kFill_Style);
reed374772b2016-10-05 17:33:02 -0700474 p.setBlendMode(SkBlendMode::kSrc);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000475 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000476}
477
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000478static void r1(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000479{
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000480 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000481
reed@android.com8af96022009-10-12 17:02:22 +0000482 p.setAlpha(0x40);
reed374772b2016-10-05 17:33:02 -0700483 p.setBlendMode(SkBlendMode::kSrc);
reed@android.com8af96022009-10-12 17:02:22 +0000484 p.setStyle(SkPaint::kStroke_Style);
485 p.setStrokeWidth(SK_Scalar1*2);
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 r2(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000490{
491 p.setStyle(SkPaint::kStrokeAndFill_Style);
492 p.setStrokeWidth(SK_Scalar1*4);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000493 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000494
reed@android.com8af96022009-10-12 17:02:22 +0000495 p.setStyle(SkPaint::kStroke_Style);
496 p.setStrokeWidth(SK_Scalar1*3/2);
reed374772b2016-10-05 17:33:02 -0700497 p.setBlendMode(SkBlendMode::kClear);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000498 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000499}
500
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000501static void r3(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000502{
503 p.setStyle(SkPaint::kStroke_Style);
504 p.setStrokeWidth(SK_Scalar1*3);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000505 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000506
reed@android.com8af96022009-10-12 17:02:22 +0000507 p.setAlpha(0x20);
508 p.setStyle(SkPaint::kFill_Style);
reed374772b2016-10-05 17:33:02 -0700509 p.setBlendMode(SkBlendMode::kSrc);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000510 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000511}
512
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000513static void r4(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000514{
515 p.setAlpha(0x60);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000516 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000517
reed@android.com8af96022009-10-12 17:02:22 +0000518 p.setAlpha(0xFF);
reed374772b2016-10-05 17:33:02 -0700519 p.setBlendMode(SkBlendMode::kClear);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000520 rastBuilder->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000521
reed374772b2016-10-05 17:33:02 -0700522 p.setBlendMode(SkBlendMode::kSrcOver);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000523 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000524}
525
526#include "SkDiscretePathEffect.h"
527
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000528static void r5(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000529{
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000530 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000531
reeda4393342016-03-18 11:22:57 -0700532 p.setPathEffect(SkDiscretePathEffect::Make(SK_Scalar1*4, SK_Scalar1*3));
reed374772b2016-10-05 17:33:02 -0700533 p.setBlendMode(SkBlendMode::kSrcOut);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000534 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000535}
536
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000537static void r6(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000538{
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000539 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000540
reed@android.com8af96022009-10-12 17:02:22 +0000541 p.setAntiAlias(false);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000542 SkLayerRasterizer::Builder rastBuilder2;
543 r5(&rastBuilder2, p);
reed7b380d02016-03-21 13:25:16 -0700544 p.setRasterizer(rastBuilder2.detach());
reed374772b2016-10-05 17:33:02 -0700545 p.setBlendMode(SkBlendMode::kClear);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000546 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000547}
548
549#include "Sk2DPathEffect.h"
550
reeda4393342016-03-18 11:22:57 -0700551static sk_sp<SkPathEffect> MakeDotEffect(SkScalar radius, const SkMatrix& matrix) {
reed@google.com18dc4772011-08-09 18:47:40 +0000552 SkPath path;
553 path.addCircle(0, 0, radius);
reeda4393342016-03-18 11:22:57 -0700554 return SkPath2DPathEffect::Make(matrix, path);
reed@google.com18dc4772011-08-09 18:47:40 +0000555}
reed@android.com8af96022009-10-12 17:02:22 +0000556
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000557static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000558{
559 SkMatrix lattice;
560 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
561 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reeda4393342016-03-18 11:22:57 -0700562 p.setPathEffect(MakeDotEffect(SK_Scalar1*4, lattice));
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000563 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000564}
565
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000566static void r8(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000567{
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000568 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000569
reed@android.com8af96022009-10-12 17:02:22 +0000570 SkMatrix lattice;
571 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
572 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reeda4393342016-03-18 11:22:57 -0700573 p.setPathEffect(MakeDotEffect(SK_Scalar1*2, lattice));
reed374772b2016-10-05 17:33:02 -0700574 p.setBlendMode(SkBlendMode::kClear);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000575 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000576
halcanary96fcdcc2015-08-27 07:41:13 -0700577 p.setPathEffect(nullptr);
reed374772b2016-10-05 17:33:02 -0700578 p.setBlendMode(SkBlendMode::kSrcOver);
reed@android.com8af96022009-10-12 17:02:22 +0000579 p.setStyle(SkPaint::kStroke_Style);
580 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000581 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000582}
583
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000584static void r9(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p)
reed@android.com8af96022009-10-12 17:02:22 +0000585{
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 SkMatrix lattice;
589 lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
590 lattice.postRotate(SkIntToScalar(30), 0, 0);
reeda4393342016-03-18 11:22:57 -0700591 p.setPathEffect(SkLine2DPathEffect::Make(SK_Scalar1*2, lattice));
reed374772b2016-10-05 17:33:02 -0700592 p.setBlendMode(SkBlendMode::kClear);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000593 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000594
halcanary96fcdcc2015-08-27 07:41:13 -0700595 p.setPathEffect(nullptr);
reed374772b2016-10-05 17:33:02 -0700596 p.setBlendMode(SkBlendMode::kSrcOver);
reed@android.com8af96022009-10-12 17:02:22 +0000597 p.setStyle(SkPaint::kStroke_Style);
598 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000599 rastBuilder->addLayer(p);
reed@android.com8af96022009-10-12 17:02:22 +0000600}
601
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000602typedef void (*raster_proc)(SkLayerRasterizer::Builder*, SkPaint&);
reed@android.com8af96022009-10-12 17:02:22 +0000603
604static const raster_proc gRastProcs[] = {
605 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
606};
607
rmistry@google.comae933ce2012-08-23 18:19:56 +0000608static void apply_shader(SkPaint* paint, int index) {
reed@android.com8af96022009-10-12 17:02:22 +0000609 raster_proc proc = gRastProcs[index];
610 SkPaint p;
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000611 SkLayerRasterizer::Builder rastBuilder;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000612
reed@android.com8af96022009-10-12 17:02:22 +0000613 p.setAntiAlias(true);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000614 proc(&rastBuilder, p);
reed7b380d02016-03-21 13:25:16 -0700615 paint->setRasterizer(rastBuilder.detach());
reed@android.com8af96022009-10-12 17:02:22 +0000616 paint->setColor(SK_ColorBLUE);
617}
618
619#include "SkTypeface.h"
620
621static void texteffect_slide(SkCanvas* canvas) {
622 const char* str = "Google";
623 size_t len = strlen(str);
624 SkScalar x = 20;
625 SkScalar y = 80;
626 SkPaint paint;
mbocee6a9912016-05-31 11:42:36 -0700627 paint.setTypeface(SkTypeface::MakeFromName("Georgia",
628 SkFontStyle::FromOldStyle(SkTypeface::kItalic)));
reed@android.com8af96022009-10-12 17:02:22 +0000629 paint.setTextSize(75);
630 paint.setAntiAlias(true);
631 paint.setColor(SK_ColorBLUE);
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000632 for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) {
reed@google.com7fa2a652014-01-27 13:42:58 +0000633 apply_shader(&paint, (int)i);
reed@android.com8af96022009-10-12 17:02:22 +0000634 canvas->drawText(str, len, x, y, paint);
635 y += 80;
636 if (i == 4) {
637 x += 320;
638 y = 80;
639 }
640 }
641}
642
643///////////////////////////////////////////////////////////////////////////////
644
reed@android.com63e34c62009-10-06 21:19:18 +0000645#include "SkImageEncoder.h"
646
647static const SlideProc gProc[] = {
648 patheffect_slide,
649 gradient_slide,
650 textonpath_slide,
reed@android.com8af96022009-10-12 17:02:22 +0000651 mesh_slide,
652 texteffect_slide
reed@android.com63e34c62009-10-06 21:19:18 +0000653};
654
reed@google.com81e3d7f2011-06-01 12:42:36 +0000655class SlideView : public SampleView {
reed@android.com63e34c62009-10-06 21:19:18 +0000656 int fIndex;
reed@google.com7b185902012-03-12 21:13:48 +0000657 bool fOnce;
reed@android.com63e34c62009-10-06 21:19:18 +0000658public:
659 SlideView() {
reed@google.com7b185902012-03-12 21:13:48 +0000660 fOnce = false;
661 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000662
reed@google.com7b185902012-03-12 21:13:48 +0000663 void init() {
664 if (fOnce) {
665 return;
666 }
667 fOnce = true;
668
reed@android.com63e34c62009-10-06 21:19:18 +0000669 fIndex = 0;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000670
reed@android.com63e34c62009-10-06 21:19:18 +0000671 SkBitmap bm;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000672 bm.allocN32Pixels(1024, 768);
reed@android.com63e34c62009-10-06 21:19:18 +0000673 SkCanvas canvas(bm);
674 SkScalar s = SkIntToScalar(1024) / 640;
675 canvas.scale(s, s);
676 for (size_t i = 0; i < SK_ARRAY_COUNT(gProc); i++) {
677 canvas.save();
reed@android.com8af96022009-10-12 17:02:22 +0000678 canvas.drawColor(BG_COLOR);
reed@android.com63e34c62009-10-06 21:19:18 +0000679 gProc[i](&canvas);
680 canvas.restore();
681 SkString str;
senorblanco@chromium.org3a67a662012-07-09 18:22:08 +0000682 str.printf("/skimages/slide_" SK_SIZE_T_SPECIFIER ".png", i);
Hal Canarydb683012016-11-23 08:55:18 -0700683 sk_tool_utils::EncodeImageToFile(str.c_str(), bm, SkEncodedImageFormat::kPNG, 100);
reed@android.com63e34c62009-10-06 21:19:18 +0000684 }
reed@google.com81e3d7f2011-06-01 12:42:36 +0000685 this->setBGColor(BG_COLOR);
reed@android.com63e34c62009-10-06 21:19:18 +0000686 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000687
reed@android.com63e34c62009-10-06 21:19:18 +0000688protected:
689 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -0700690 bool onQuery(SkEvent* evt) override {
reed@android.com63e34c62009-10-06 21:19:18 +0000691 if (SampleCode::TitleQ(*evt)) {
692 SampleCode::TitleR(evt, "Slides");
693 return true;
694 }
695 return this->INHERITED::onQuery(evt);
696 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000697
mtklein36352bf2015-03-25 18:17:31 -0700698 void onDrawContent(SkCanvas* canvas) override {
reed@google.com7b185902012-03-12 21:13:48 +0000699 this->init();
reed@android.com63e34c62009-10-06 21:19:18 +0000700 gProc[fIndex](canvas);
701 }
702
mtklein36352bf2015-03-25 18:17:31 -0700703 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override {
reed@google.com7b185902012-03-12 21:13:48 +0000704 this->init();
reed@android.com63e34c62009-10-06 21:19:18 +0000705 fIndex = (fIndex + 1) % SK_ARRAY_COUNT(gProc);
halcanary96fcdcc2015-08-27 07:41:13 -0700706 this->inval(nullptr);
707 return nullptr;
reed@android.com63e34c62009-10-06 21:19:18 +0000708 }
709
710private:
reed@google.com81e3d7f2011-06-01 12:42:36 +0000711 typedef SampleView INHERITED;
reed@android.com63e34c62009-10-06 21:19:18 +0000712};
713
714//////////////////////////////////////////////////////////////////////////////
715
716static SkView* MyFactory() { return new SlideView; }
717static SkViewRegister reg(MyFactory);