blob: 998896c9ecd69b3bd3552adf4cef65f8c87bf591 [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"
reed@android.com63e34c62009-10-06 21:19:18 +000014#include "SkPaint.h"
Mike Reed887cdf12017-04-03 11:11:09 -040015#include "SkVertices.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000016#include "SkView.h"
reed@android.com63e34c62009-10-06 21:19:18 +000017
Hal Canarydb683012016-11-23 08:55:18 -070018#include "sk_tool_utils.h"
19
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();
reeda4393342016-03-18 11:22:57 -070034 sk_sp<SkPathEffect> corner = SkCornerPathEffect::Make(25);
35 sk_sp<SkPathEffect> compose;
reed@android.com63e34c62009-10-06 21:19:18 +000036 if (pe) {
Mike Reeda07741a2017-02-25 22:34:32 -050037 compose = SkPathEffect::MakeCompose(sk_ref_sp(pe), corner);
reed@android.com63e34c62009-10-06 21:19:18 +000038 } else {
39 compose = corner;
40 }
reeda4393342016-03-18 11:22:57 -070041 paint->setPathEffect(compose);
reed@android.com63e34c62009-10-06 21:19:18 +000042}
43
44static void hair_pe(SkPaint* paint) {
45 paint->setStrokeWidth(0);
46}
47
48static void hair2_pe(SkPaint* paint) {
49 paint->setStrokeWidth(0);
50 compose_pe(paint);
51}
52
53static void stroke_pe(SkPaint* paint) {
54 paint->setStrokeWidth(12);
55 compose_pe(paint);
56}
57
58static void dash_pe(SkPaint* paint) {
59 SkScalar inter[] = { 20, 10, 10, 10 };
60 paint->setStrokeWidth(12);
reeda4393342016-03-18 11:22:57 -070061 paint->setPathEffect(SkDashPathEffect::Make(inter, SK_ARRAY_COUNT(inter), 0));
reed@android.com63e34c62009-10-06 21:19:18 +000062 compose_pe(paint);
63}
64
65static const int gXY[] = {
664, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
67};
68
69static void scale(SkPath* path, SkScalar scale) {
70 SkMatrix m;
71 m.setScale(scale, scale);
72 path->transform(m);
73}
74
75static void one_d_pe(SkPaint* paint) {
76 SkPath path;
77 path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
78 for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
79 path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
80 path.close();
81 path.offset(SkIntToScalar(-6), 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000082 scale(&path, 1.5f);
rmistry@google.comae933ce2012-08-23 18:19:56 +000083
reeda4393342016-03-18 11:22:57 -070084 paint->setPathEffect(SkPath1DPathEffect::Make(path, SkIntToScalar(21), 0,
85 SkPath1DPathEffect::kRotate_Style));
reed@android.com63e34c62009-10-06 21:19:18 +000086 compose_pe(paint);
87}
88
89typedef void (*PE_Proc)(SkPaint*);
90static const PE_Proc gPE[] = { hair_pe, hair2_pe, stroke_pe, dash_pe, one_d_pe };
91
92static void fill_pe(SkPaint* paint) {
93 paint->setStyle(SkPaint::kFill_Style);
halcanary96fcdcc2015-08-27 07:41:13 -070094 paint->setPathEffect(nullptr);
reed@android.com63e34c62009-10-06 21:19:18 +000095}
96
97static void discrete_pe(SkPaint* paint) {
reeda4393342016-03-18 11:22:57 -070098 paint->setPathEffect(SkDiscretePathEffect::Make(10, 4));
reed@android.com63e34c62009-10-06 21:19:18 +000099}
100
reeda4393342016-03-18 11:22:57 -0700101static sk_sp<SkPathEffect> MakeTileEffect() {
reed@google.com43e9f202011-08-09 19:01:50 +0000102 SkMatrix m;
103 m.setScale(SkIntToScalar(12), SkIntToScalar(12));
reed@android.com63e34c62009-10-06 21:19:18 +0000104
reed@google.com43e9f202011-08-09 19:01:50 +0000105 SkPath path;
106 path.addCircle(0, 0, SkIntToScalar(5));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000107
reeda4393342016-03-18 11:22:57 -0700108 return SkPath2DPathEffect::Make(m, path);
reed@google.com43e9f202011-08-09 19:01:50 +0000109}
reed@android.com63e34c62009-10-06 21:19:18 +0000110
111static void tile_pe(SkPaint* paint) {
reeda4393342016-03-18 11:22:57 -0700112 paint->setPathEffect(MakeTileEffect());
reed@android.com63e34c62009-10-06 21:19:18 +0000113}
114
115static const PE_Proc gPE2[] = { fill_pe, discrete_pe, tile_pe };
116
117static void patheffect_slide(SkCanvas* canvas) {
118 SkPaint paint;
119 paint.setAntiAlias(true);
120 paint.setStyle(SkPaint::kStroke_Style);
121
122 SkPath path;
123 path.moveTo(20, 20);
124 path.lineTo(70, 120);
125 path.lineTo(120, 30);
126 path.lineTo(170, 80);
127 path.lineTo(240, 50);
128
129 size_t i;
130 canvas->save();
131 for (i = 0; i < SK_ARRAY_COUNT(gPE); i++) {
132 gPE[i](&paint);
133 canvas->drawPath(path, paint);
134 canvas->translate(0, 75);
135 }
136 canvas->restore();
137
138 path.reset();
139 SkRect r = { 0, 0, 250, 120 };
140 path.addOval(r, SkPath::kCW_Direction);
141 r.inset(50, 50);
142 path.addRect(r, SkPath::kCCW_Direction);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000143
reed@android.com63e34c62009-10-06 21:19:18 +0000144 canvas->translate(320, 20);
145 for (i = 0; i < SK_ARRAY_COUNT(gPE2); i++) {
146 gPE2[i](&paint);
147 canvas->drawPath(path, paint);
148 canvas->translate(0, 160);
149 }
150}
151
152///////////////////////////////////////////////////////////////////////////////
153
154#include "SkGradientShader.h"
155
156struct GradData {
157 int fCount;
158 const SkColor* fColors;
159 const SkScalar* fPos;
160};
161
162static const SkColor gColors[] = {
163SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
164};
165static const SkScalar gPos0[] = { 0, SK_Scalar1 };
166static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
167static const SkScalar gPos2[] = {
1680, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1
169};
170
171static const GradData gGradData[] = {
halcanary96fcdcc2015-08-27 07:41:13 -0700172{ 2, gColors, nullptr },
reed@android.com63e34c62009-10-06 21:19:18 +0000173{ 2, gColors, gPos0 },
174{ 2, gColors, gPos1 },
halcanary96fcdcc2015-08-27 07:41:13 -0700175{ 5, gColors, nullptr },
reed@android.com63e34c62009-10-06 21:19:18 +0000176{ 5, gColors, gPos2 }
177};
178
reed8a21c9f2016-03-08 18:50:00 -0800179static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
180 return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm);
reed@android.com63e34c62009-10-06 21:19:18 +0000181}
182
reed8a21c9f2016-03-08 18:50:00 -0800183static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
reed@android.com63e34c62009-10-06 21:19:18 +0000184 SkPoint center;
185 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
186 SkScalarAve(pts[0].fY, pts[1].fY));
reed8a21c9f2016-03-08 18:50:00 -0800187 return SkGradientShader::MakeRadial(center, center.fX, data.fColors,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000188 data.fPos, data.fCount, tm);
reed@android.com63e34c62009-10-06 21:19:18 +0000189}
190
reed8a21c9f2016-03-08 18:50:00 -0800191static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
reed@android.com63e34c62009-10-06 21:19:18 +0000192 SkPoint center;
193 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
194 SkScalarAve(pts[0].fY, pts[1].fY));
reed8a21c9f2016-03-08 18:50:00 -0800195 return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount);
reed@android.com63e34c62009-10-06 21:19:18 +0000196}
197
reed8a21c9f2016-03-08 18:50:00 -0800198static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
reed@android.com63e34c62009-10-06 21:19:18 +0000199 SkPoint center0, center1;
200 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
201 SkScalarAve(pts[0].fY, pts[1].fY));
202 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
203 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
reed8a21c9f2016-03-08 18:50:00 -0800204 return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7,
reed@android.com63e34c62009-10-06 21:19:18 +0000205 center0, (pts[1].fX - pts[0].fX) / 2,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000206 data.fColors, data.fPos, data.fCount, tm);
reed@android.com63e34c62009-10-06 21:19:18 +0000207}
208
reed8a21c9f2016-03-08 18:50:00 -0800209typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData&, SkShader::TileMode);
reed@android.com63e34c62009-10-06 21:19:18 +0000210static const GradMaker gGradMakers[] = {
reed71a6cbf2015-05-04 08:32:51 -0700211 MakeLinear, MakeRadial, MakeSweep, Make2Conical
reed@android.com63e34c62009-10-06 21:19:18 +0000212};
213
214static void gradient_slide(SkCanvas* canvas) {
215 SkPoint pts[2] = {
216 { 0, 0 },
217 { SkIntToScalar(100), SkIntToScalar(100) }
218 };
219 SkShader::TileMode tm = SkShader::kClamp_TileMode;
220 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
221 SkPaint paint;
222 paint.setAntiAlias(true);
223 paint.setDither(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000224
reed@android.com63e34c62009-10-06 21:19:18 +0000225 canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
226 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
227 canvas->save();
228 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
reed8a21c9f2016-03-08 18:50:00 -0800229 paint.setShader(gGradMakers[j](pts, gGradData[i], tm));
reed@android.com63e34c62009-10-06 21:19:18 +0000230 canvas->drawRect(r, paint);
reed@android.com63e34c62009-10-06 21:19:18 +0000231 canvas->translate(0, SkIntToScalar(120));
232 }
233 canvas->restore();
234 canvas->translate(SkIntToScalar(120), 0);
235 }
236}
237
238///////////////////////////////////////////////////////////////////////////////
239
240#include "SkPathMeasure.h"
241
242static SkScalar getpathlen(const SkPath& path) {
243 SkPathMeasure meas(path, false);
244 return meas.getLength();
245}
246
247static void textonpath_slide(SkCanvas* canvas) {
248 const char* text = "Displacement";
249 size_t len =strlen(text);
250 SkPath path;
251 path.moveTo(100, 300);
252 path.quadTo(300, 100, 500, 300);
253 path.offset(0, -100);
254
255 SkPaint paint;
256 paint.setAntiAlias(true);
257 paint.setTextSize(40);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000258
reed@android.com63e34c62009-10-06 21:19:18 +0000259 paint.setStyle(SkPaint::kStroke_Style);
260 canvas->drawPath(path, paint);
261 paint.setStyle(SkPaint::kFill_Style);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000262
reed@android.com63e34c62009-10-06 21:19:18 +0000263 SkScalar x = 50;
264 paint.setColor(0xFF008800);
265 canvas->drawTextOnPathHV(text, len, path,
266 x, paint.getTextSize()*2/3, paint);
267 paint.setColor(SK_ColorRED);
268 canvas->drawTextOnPathHV(text, len, path,
rmistry@google.comae933ce2012-08-23 18:19:56 +0000269 x + 60, 0, paint);
reed@android.com63e34c62009-10-06 21:19:18 +0000270 paint.setColor(SK_ColorBLUE);
271 canvas->drawTextOnPathHV(text, len, path,
272 x + 120, -paint.getTextSize()*2/3, paint);
273
274 path.offset(0, 200);
275 paint.setTextAlign(SkPaint::kRight_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000276
reed@android.com63e34c62009-10-06 21:19:18 +0000277 text = "Matrices";
278 len = strlen(text);
279 SkScalar pathLen = getpathlen(path);
280 SkMatrix matrix;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000281
reed@android.com63e34c62009-10-06 21:19:18 +0000282 paint.setColor(SK_ColorBLACK);
283 paint.setStyle(SkPaint::kStroke_Style);
284 canvas->drawPath(path, paint);
285 paint.setStyle(SkPaint::kFill_Style);
286
287 paint.setTextSize(50);
halcanary96fcdcc2015-08-27 07:41:13 -0700288 canvas->drawTextOnPath(text, len, path, nullptr, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000289
reed@android.com63e34c62009-10-06 21:19:18 +0000290 paint.setColor(SK_ColorRED);
291 matrix.setScale(-SK_Scalar1, SK_Scalar1);
292 matrix.postTranslate(pathLen, 0);
293 canvas->drawTextOnPath(text, len, path, &matrix, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000294
reed@android.com63e34c62009-10-06 21:19:18 +0000295 paint.setColor(SK_ColorBLUE);
296 matrix.setScale(SK_Scalar1, -SK_Scalar1);
297 canvas->drawTextOnPath(text, len, path, &matrix, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000298
reed@android.com63e34c62009-10-06 21:19:18 +0000299 paint.setColor(0xFF008800);
300 matrix.setScale(-SK_Scalar1, -SK_Scalar1);
301 matrix.postTranslate(pathLen, 0);
302 canvas->drawTextOnPath(text, len, path, &matrix, paint);
303}
304
305///////////////////////////////////////////////////////////////////////////////
306
msarettd15750c2016-03-18 15:48:49 -0700307#include "DecodeFile.h"
reed@android.com63e34c62009-10-06 21:19:18 +0000308#include "SkOSFile.h"
309#include "SkRandom.h"
310#include "SkStream.h"
reed@android.com63e34c62009-10-06 21:19:18 +0000311
reed8a21c9f2016-03-08 18:50:00 -0800312static sk_sp<SkShader> make_shader0(SkIPoint* size) {
reed@android.com63e34c62009-10-06 21:19:18 +0000313 SkBitmap bm;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000314
msarettd15750c2016-03-18 15:48:49 -0700315 decode_file("/skimages/logo.gif", &bm);
reed@android.com63e34c62009-10-06 21:19:18 +0000316 size->set(bm.width(), bm.height());
reed8a21c9f2016-03-08 18:50:00 -0800317 return SkShader::MakeBitmapShader(bm, SkShader::kClamp_TileMode,
reed@android.com63e34c62009-10-06 21:19:18 +0000318 SkShader::kClamp_TileMode);
319}
320
reed8a21c9f2016-03-08 18:50:00 -0800321static sk_sp<SkShader> make_shader1(const SkIPoint& size) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000322 SkPoint pts[] = { { 0, 0 },
323 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
reed@android.com63e34c62009-10-06 21:19:18 +0000324 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
reed8a21c9f2016-03-08 18:50:00 -0800325 return SkGradientShader::MakeLinear(pts, colors, nullptr,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000326 SK_ARRAY_COUNT(colors), SkShader::kMirror_TileMode);
reed@android.com63e34c62009-10-06 21:19:18 +0000327}
328
reed@android.comf2b98d62010-12-20 18:26:13 +0000329class Rec {
330public:
Mike Reed887cdf12017-04-03 11:11:09 -0400331 SkVertices::VertexMode fMode;
reed@android.com63e34c62009-10-06 21:19:18 +0000332 int fCount;
333 SkPoint* fVerts;
334 SkPoint* fTexs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000335
halcanary96fcdcc2015-08-27 07:41:13 -0700336 Rec() : fCount(0), fVerts(nullptr), fTexs(nullptr) {}
reed@android.com63e34c62009-10-06 21:19:18 +0000337 ~Rec() { delete[] fVerts; delete[] fTexs; }
338};
339
caryclark@google.com02939ce2012-06-06 12:09:51 +0000340static void make_tris(Rec* rec) {
reed@android.com63e34c62009-10-06 21:19:18 +0000341 int n = 10;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000342 SkRandom rand;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000343
Mike Reed887cdf12017-04-03 11:11:09 -0400344 rec->fMode = SkVertices::kTriangles_VertexMode;
reed@android.com63e34c62009-10-06 21:19:18 +0000345 rec->fCount = n * 3;
346 rec->fVerts = new SkPoint[rec->fCount];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000347
reed@android.com63e34c62009-10-06 21:19:18 +0000348 for (int i = 0; i < n; i++) {
349 SkPoint* v = &rec->fVerts[i*3];
350 for (int j = 0; j < 3; j++) {
351 v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250);
352 }
353 }
354}
355
caryclark@google.com02939ce2012-06-06 12:09:51 +0000356static void make_fan(Rec* rec, int texWidth, int texHeight) {
reed@android.com63e34c62009-10-06 21:19:18 +0000357 const SkScalar tx = SkIntToScalar(texWidth);
358 const SkScalar ty = SkIntToScalar(texHeight);
359 const int n = 24;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000360
Mike Reed887cdf12017-04-03 11:11:09 -0400361 rec->fMode = SkVertices::kTriangleFan_VertexMode;
reed@android.com63e34c62009-10-06 21:19:18 +0000362 rec->fCount = n + 2;
363 rec->fVerts = new SkPoint[rec->fCount];
364 rec->fTexs = new SkPoint[rec->fCount];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000365
reed@android.com63e34c62009-10-06 21:19:18 +0000366 SkPoint* v = rec->fVerts;
367 SkPoint* t = rec->fTexs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000368
reed@android.com63e34c62009-10-06 21:19:18 +0000369 v[0].set(0, 0);
370 t[0].set(0, 0);
371 for (int i = 0; i < n; i++) {
372 SkScalar cos;
373 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
374 v[i+1].set(cos, sin);
375 t[i+1].set(i*tx/n, ty);
376 }
377 v[n+1] = v[1];
378 t[n+1].set(tx, ty);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000379
reed@android.com63e34c62009-10-06 21:19:18 +0000380 SkMatrix m;
381 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
382 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
383 m.mapPoints(v, rec->fCount);
384}
385
caryclark@google.com02939ce2012-06-06 12:09:51 +0000386static void make_strip(Rec* rec, int texWidth, int texHeight) {
reed@android.com63e34c62009-10-06 21:19:18 +0000387 const SkScalar tx = SkIntToScalar(texWidth);
388 const SkScalar ty = SkIntToScalar(texHeight);
389 const int n = 24;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000390
Mike Reed887cdf12017-04-03 11:11:09 -0400391 rec->fMode = SkVertices::kTriangleStrip_VertexMode;
reed@android.com63e34c62009-10-06 21:19:18 +0000392 rec->fCount = 2 * (n + 1);
393 rec->fVerts = new SkPoint[rec->fCount];
394 rec->fTexs = new SkPoint[rec->fCount];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000395
reed@android.com63e34c62009-10-06 21:19:18 +0000396 SkPoint* v = rec->fVerts;
397 SkPoint* t = rec->fTexs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000398
reed@android.com63e34c62009-10-06 21:19:18 +0000399 for (int i = 0; i < n; i++) {
400 SkScalar cos;
401 SkScalar sin = SkScalarSinCos(SK_ScalarPI * 2 * i / n, &cos);
402 v[i*2 + 0].set(cos/2, sin/2);
403 v[i*2 + 1].set(cos, sin);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000404
reed@android.com63e34c62009-10-06 21:19:18 +0000405 t[i*2 + 0].set(tx * i / n, ty);
406 t[i*2 + 1].set(tx * i / n, 0);
407 }
408 v[2*n + 0] = v[0];
409 v[2*n + 1] = v[1];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000410
reed@android.com63e34c62009-10-06 21:19:18 +0000411 t[2*n + 0].set(tx, ty);
412 t[2*n + 1].set(tx, 0);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000413
reed@android.com63e34c62009-10-06 21:19:18 +0000414 SkMatrix m;
415 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
416 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
417 m.mapPoints(v, rec->fCount);
418}
419
420static void mesh_slide(SkCanvas* canvas) {
421 Rec fRecs[3];
422 SkIPoint size;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000423
reed8a21c9f2016-03-08 18:50:00 -0800424 auto fShader0 = make_shader0(&size);
425 auto fShader1 = make_shader1(size);
reed@android.comf2b98d62010-12-20 18:26:13 +0000426
reed@android.com63e34c62009-10-06 21:19:18 +0000427 make_strip(&fRecs[0], size.fX, size.fY);
428 make_fan(&fRecs[1], size.fX, size.fY);
429 make_tris(&fRecs[2]);
430
reed@android.com63e34c62009-10-06 21:19:18 +0000431 SkPaint paint;
432 paint.setDither(true);
reed93a12152015-03-16 10:08:34 -0700433 paint.setFilterQuality(kLow_SkFilterQuality);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000434
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000435 for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) {
Mike Reed887cdf12017-04-03 11:11:09 -0400436 auto verts = SkVertices::MakeCopy(fRecs[i].fMode, fRecs[i].fCount,
437 fRecs[i].fVerts, fRecs[i].fTexs, nullptr);
reed@android.com63e34c62009-10-06 21:19:18 +0000438 canvas->save();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000439
halcanary96fcdcc2015-08-27 07:41:13 -0700440 paint.setShader(nullptr);
Mike Reed887cdf12017-04-03 11:11:09 -0400441 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000442
reed@android.com63e34c62009-10-06 21:19:18 +0000443 canvas->translate(SkIntToScalar(210), 0);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000444
reed@android.com63e34c62009-10-06 21:19:18 +0000445 paint.setShader(fShader0);
Mike Reed887cdf12017-04-03 11:11:09 -0400446 canvas->drawVertices(verts, SkBlendMode::kModulate, 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);
Mike Reed887cdf12017-04-03 11:11:09 -0400451 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
reed@android.com63e34c62009-10-06 21:19:18 +0000452 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000453
reed@android.com63e34c62009-10-06 21:19:18 +0000454 canvas->translate(0, SkIntToScalar(250));
455 }
456}
457
458///////////////////////////////////////////////////////////////////////////////
459
reed@android.com8af96022009-10-12 17:02:22 +0000460#include "SkTypeface.h"
461
reed@android.com8af96022009-10-12 17:02:22 +0000462///////////////////////////////////////////////////////////////////////////////
463
reed@android.com63e34c62009-10-06 21:19:18 +0000464#include "SkImageEncoder.h"
465
466static const SlideProc gProc[] = {
467 patheffect_slide,
468 gradient_slide,
469 textonpath_slide,
reed@android.com8af96022009-10-12 17:02:22 +0000470 mesh_slide,
reed@android.com63e34c62009-10-06 21:19:18 +0000471};
472
reed@google.com81e3d7f2011-06-01 12:42:36 +0000473class SlideView : public SampleView {
reed@android.com63e34c62009-10-06 21:19:18 +0000474 int fIndex;
reed@google.com7b185902012-03-12 21:13:48 +0000475 bool fOnce;
reed@android.com63e34c62009-10-06 21:19:18 +0000476public:
477 SlideView() {
reed@google.com7b185902012-03-12 21:13:48 +0000478 fOnce = false;
479 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000480
reed@google.com7b185902012-03-12 21:13:48 +0000481 void init() {
482 if (fOnce) {
483 return;
484 }
485 fOnce = true;
486
reed@android.com63e34c62009-10-06 21:19:18 +0000487 fIndex = 0;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000488
reed@android.com63e34c62009-10-06 21:19:18 +0000489 SkBitmap bm;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000490 bm.allocN32Pixels(1024, 768);
reed@android.com63e34c62009-10-06 21:19:18 +0000491 SkCanvas canvas(bm);
492 SkScalar s = SkIntToScalar(1024) / 640;
493 canvas.scale(s, s);
494 for (size_t i = 0; i < SK_ARRAY_COUNT(gProc); i++) {
495 canvas.save();
reed@android.com8af96022009-10-12 17:02:22 +0000496 canvas.drawColor(BG_COLOR);
reed@android.com63e34c62009-10-06 21:19:18 +0000497 gProc[i](&canvas);
498 canvas.restore();
499 SkString str;
senorblanco@chromium.org3a67a662012-07-09 18:22:08 +0000500 str.printf("/skimages/slide_" SK_SIZE_T_SPECIFIER ".png", i);
Hal Canarydb683012016-11-23 08:55:18 -0700501 sk_tool_utils::EncodeImageToFile(str.c_str(), bm, SkEncodedImageFormat::kPNG, 100);
reed@android.com63e34c62009-10-06 21:19:18 +0000502 }
reed@google.com81e3d7f2011-06-01 12:42:36 +0000503 this->setBGColor(BG_COLOR);
reed@android.com63e34c62009-10-06 21:19:18 +0000504 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000505
reed@android.com63e34c62009-10-06 21:19:18 +0000506protected:
507 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -0700508 bool onQuery(SkEvent* evt) override {
reed@android.com63e34c62009-10-06 21:19:18 +0000509 if (SampleCode::TitleQ(*evt)) {
510 SampleCode::TitleR(evt, "Slides");
511 return true;
512 }
513 return this->INHERITED::onQuery(evt);
514 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000515
mtklein36352bf2015-03-25 18:17:31 -0700516 void onDrawContent(SkCanvas* canvas) override {
reed@google.com7b185902012-03-12 21:13:48 +0000517 this->init();
reed@android.com63e34c62009-10-06 21:19:18 +0000518 gProc[fIndex](canvas);
519 }
520
mtklein36352bf2015-03-25 18:17:31 -0700521 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override {
reed@google.com7b185902012-03-12 21:13:48 +0000522 this->init();
reed@android.com63e34c62009-10-06 21:19:18 +0000523 fIndex = (fIndex + 1) % SK_ARRAY_COUNT(gProc);
halcanary96fcdcc2015-08-27 07:41:13 -0700524 return nullptr;
reed@android.com63e34c62009-10-06 21:19:18 +0000525 }
526
527private:
reed@google.com81e3d7f2011-06-01 12:42:36 +0000528 typedef SampleView INHERITED;
reed@android.com63e34c62009-10-06 21:19:18 +0000529};
530
531//////////////////////////////////////////////////////////////////////////////
532
533static SkView* MyFactory() { return new SlideView; }
534static SkViewRegister reg(MyFactory);