blob: 48979a411ac26208d12b9747da5ad04eafbccd29 [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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "include/core/SkCanvas.h"
8#include "include/core/SkPaint.h"
9#include "include/core/SkVertices.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/effects/SkGradientShader.h"
11#include "samplecode/Sample.h"
12#include "src/core/SkBlurMask.h"
13#include "src/core/SkReadBuffer.h"
14#include "src/core/SkWriteBuffer.h"
reed@android.com63e34c62009-10-06 21:19:18 +000015
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "tools/ToolUtils.h"
Hal Canarydb683012016-11-23 08:55:18 -070017
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/effects/Sk1DPathEffect.h"
25#include "include/effects/Sk2DPathEffect.h"
26#include "include/effects/SkCornerPathEffect.h"
27#include "include/effects/SkDashPathEffect.h"
28#include "include/effects/SkDiscretePathEffect.h"
reed@android.com63e34c62009-10-06 21:19:18 +000029
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) {
Mike Reeda07741a2017-02-25 22:34:32 -050035 compose = SkPathEffect::MakeCompose(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 };
Mike Reed30bc5272019-11-22 18:34:02 +0000138 path.addOval(r, SkPathDirection::kCW);
reed@android.com63e34c62009-10-06 21:19:18 +0000139 r.inset(50, 50);
Mike Reed30bc5272019-11-22 18:34:02 +0000140 path.addRect(r, SkPathDirection::kCCW);
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500152#include "include/effects/SkGradientShader.h"
reed@android.com63e34c62009-10-06 21:19:18 +0000153
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
Mike Reedfae8fce2019-04-03 10:27:45 -0400177static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkTileMode tm) {
reed8a21c9f2016-03-08 18:50:00 -0800178 return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm);
reed@android.com63e34c62009-10-06 21:19:18 +0000179}
180
Mike Reedfae8fce2019-04-03 10:27:45 -0400181static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkTileMode 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
Mike Reedfae8fce2019-04-03 10:27:45 -0400189static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkTileMode 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
Mike Reedfae8fce2019-04-03 10:27:45 -0400196static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, SkTileMode 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
Mike Reedfae8fce2019-04-03 10:27:45 -0400207typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData&, SkTileMode);
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 };
Mike Reedfae8fce2019-04-03 10:27:45 -0400217 SkTileMode tm = SkTileMode::kClamp;
reed@android.com63e34c62009-10-06 21:19:18 +0000218 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500238#include "include/core/SkStream.h"
239#include "include/utils/SkRandom.h"
240#include "samplecode/DecodeFile.h"
241#include "src/core/SkOSFile.h"
reed@android.com63e34c62009-10-06 21:19:18 +0000242
reed8a21c9f2016-03-08 18:50:00 -0800243static sk_sp<SkShader> make_shader0(SkIPoint* size) {
reed@android.com63e34c62009-10-06 21:19:18 +0000244 SkBitmap bm;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000245
msarettd15750c2016-03-18 15:48:49 -0700246 decode_file("/skimages/logo.gif", &bm);
reed@android.com63e34c62009-10-06 21:19:18 +0000247 size->set(bm.width(), bm.height());
Mike Reed50acf8f2019-04-08 13:20:23 -0400248 return bm.makeShader();
reed@android.com63e34c62009-10-06 21:19:18 +0000249}
250
reed8a21c9f2016-03-08 18:50:00 -0800251static sk_sp<SkShader> make_shader1(const SkIPoint& size) {
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000252 SkPoint pts[] = { { 0, 0 },
253 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
reed@android.com63e34c62009-10-06 21:19:18 +0000254 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
reed8a21c9f2016-03-08 18:50:00 -0800255 return SkGradientShader::MakeLinear(pts, colors, nullptr,
Mike Reedfae8fce2019-04-03 10:27:45 -0400256 SK_ARRAY_COUNT(colors), SkTileMode::kMirror);
reed@android.com63e34c62009-10-06 21:19:18 +0000257}
258
reed@android.comf2b98d62010-12-20 18:26:13 +0000259class Rec {
260public:
Mike Reed887cdf12017-04-03 11:11:09 -0400261 SkVertices::VertexMode fMode;
reed@android.com63e34c62009-10-06 21:19:18 +0000262 int fCount;
263 SkPoint* fVerts;
264 SkPoint* fTexs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000265
halcanary96fcdcc2015-08-27 07:41:13 -0700266 Rec() : fCount(0), fVerts(nullptr), fTexs(nullptr) {}
reed@android.com63e34c62009-10-06 21:19:18 +0000267 ~Rec() { delete[] fVerts; delete[] fTexs; }
268};
269
caryclark@google.com02939ce2012-06-06 12:09:51 +0000270static void make_tris(Rec* rec) {
reed@android.com63e34c62009-10-06 21:19:18 +0000271 int n = 10;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000272 SkRandom rand;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000273
Mike Reed887cdf12017-04-03 11:11:09 -0400274 rec->fMode = SkVertices::kTriangles_VertexMode;
reed@android.com63e34c62009-10-06 21:19:18 +0000275 rec->fCount = n * 3;
276 rec->fVerts = new SkPoint[rec->fCount];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000277
reed@android.com63e34c62009-10-06 21:19:18 +0000278 for (int i = 0; i < n; i++) {
279 SkPoint* v = &rec->fVerts[i*3];
280 for (int j = 0; j < 3; j++) {
281 v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250);
282 }
283 }
284}
285
caryclark@google.com02939ce2012-06-06 12:09:51 +0000286static void make_fan(Rec* rec, int texWidth, int texHeight) {
reed@android.com63e34c62009-10-06 21:19:18 +0000287 const SkScalar tx = SkIntToScalar(texWidth);
288 const SkScalar ty = SkIntToScalar(texHeight);
289 const int n = 24;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000290
Mike Reed887cdf12017-04-03 11:11:09 -0400291 rec->fMode = SkVertices::kTriangleFan_VertexMode;
reed@android.com63e34c62009-10-06 21:19:18 +0000292 rec->fCount = n + 2;
293 rec->fVerts = new SkPoint[rec->fCount];
294 rec->fTexs = new SkPoint[rec->fCount];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000295
reed@android.com63e34c62009-10-06 21:19:18 +0000296 SkPoint* v = rec->fVerts;
297 SkPoint* t = rec->fTexs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000298
reed@android.com63e34c62009-10-06 21:19:18 +0000299 v[0].set(0, 0);
300 t[0].set(0, 0);
301 for (int i = 0; i < n; i++) {
Brian Osman4428f2c2019-04-02 10:59:28 -0400302 SkScalar r = SK_ScalarPI * 2 * i / n,
303 sin = SkScalarSin(r),
304 cos = SkScalarCos(r);
reed@android.com63e34c62009-10-06 21:19:18 +0000305 v[i+1].set(cos, sin);
306 t[i+1].set(i*tx/n, ty);
307 }
308 v[n+1] = v[1];
309 t[n+1].set(tx, ty);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000310
reed@android.com63e34c62009-10-06 21:19:18 +0000311 SkMatrix m;
312 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
313 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
314 m.mapPoints(v, rec->fCount);
315}
316
caryclark@google.com02939ce2012-06-06 12:09:51 +0000317static void make_strip(Rec* rec, int texWidth, int texHeight) {
reed@android.com63e34c62009-10-06 21:19:18 +0000318 const SkScalar tx = SkIntToScalar(texWidth);
319 const SkScalar ty = SkIntToScalar(texHeight);
320 const int n = 24;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000321
Mike Reed887cdf12017-04-03 11:11:09 -0400322 rec->fMode = SkVertices::kTriangleStrip_VertexMode;
reed@android.com63e34c62009-10-06 21:19:18 +0000323 rec->fCount = 2 * (n + 1);
324 rec->fVerts = new SkPoint[rec->fCount];
325 rec->fTexs = new SkPoint[rec->fCount];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000326
reed@android.com63e34c62009-10-06 21:19:18 +0000327 SkPoint* v = rec->fVerts;
328 SkPoint* t = rec->fTexs;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000329
reed@android.com63e34c62009-10-06 21:19:18 +0000330 for (int i = 0; i < n; i++) {
Brian Osman4428f2c2019-04-02 10:59:28 -0400331 SkScalar r = SK_ScalarPI * 2 * i / n,
332 sin = SkScalarSin(r),
333 cos = SkScalarCos(r);
reed@android.com63e34c62009-10-06 21:19:18 +0000334 v[i*2 + 0].set(cos/2, sin/2);
335 v[i*2 + 1].set(cos, sin);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000336
reed@android.com63e34c62009-10-06 21:19:18 +0000337 t[i*2 + 0].set(tx * i / n, ty);
338 t[i*2 + 1].set(tx * i / n, 0);
339 }
340 v[2*n + 0] = v[0];
341 v[2*n + 1] = v[1];
rmistry@google.comae933ce2012-08-23 18:19:56 +0000342
reed@android.com63e34c62009-10-06 21:19:18 +0000343 t[2*n + 0].set(tx, ty);
344 t[2*n + 1].set(tx, 0);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000345
reed@android.com63e34c62009-10-06 21:19:18 +0000346 SkMatrix m;
347 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
348 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
349 m.mapPoints(v, rec->fCount);
350}
351
352static void mesh_slide(SkCanvas* canvas) {
353 Rec fRecs[3];
354 SkIPoint size;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000355
reed8a21c9f2016-03-08 18:50:00 -0800356 auto fShader0 = make_shader0(&size);
357 auto fShader1 = make_shader1(size);
reed@android.comf2b98d62010-12-20 18:26:13 +0000358
reed@android.com63e34c62009-10-06 21:19:18 +0000359 make_strip(&fRecs[0], size.fX, size.fY);
360 make_fan(&fRecs[1], size.fX, size.fY);
361 make_tris(&fRecs[2]);
362
reed@android.com63e34c62009-10-06 21:19:18 +0000363 SkPaint paint;
364 paint.setDither(true);
reed93a12152015-03-16 10:08:34 -0700365 paint.setFilterQuality(kLow_SkFilterQuality);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000366
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000367 for (size_t i = 0; i < SK_ARRAY_COUNT(fRecs); i++) {
Mike Reed887cdf12017-04-03 11:11:09 -0400368 auto verts = SkVertices::MakeCopy(fRecs[i].fMode, fRecs[i].fCount,
369 fRecs[i].fVerts, fRecs[i].fTexs, nullptr);
reed@android.com63e34c62009-10-06 21:19:18 +0000370 canvas->save();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000371
halcanary96fcdcc2015-08-27 07:41:13 -0700372 paint.setShader(nullptr);
Mike Reed887cdf12017-04-03 11:11:09 -0400373 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000374
reed@android.com63e34c62009-10-06 21:19:18 +0000375 canvas->translate(SkIntToScalar(210), 0);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000376
reed@android.com63e34c62009-10-06 21:19:18 +0000377 paint.setShader(fShader0);
Mike Reed887cdf12017-04-03 11:11:09 -0400378 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000379
reed@android.com63e34c62009-10-06 21:19:18 +0000380 canvas->translate(SkIntToScalar(210), 0);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000381
reed@android.com63e34c62009-10-06 21:19:18 +0000382 paint.setShader(fShader1);
Mike Reed887cdf12017-04-03 11:11:09 -0400383 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
reed@android.com63e34c62009-10-06 21:19:18 +0000384 canvas->restore();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000385
reed@android.com63e34c62009-10-06 21:19:18 +0000386 canvas->translate(0, SkIntToScalar(250));
387 }
388}
389
390///////////////////////////////////////////////////////////////////////////////
391
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500392#include "include/core/SkTypeface.h"
reed@android.com8af96022009-10-12 17:02:22 +0000393
reed@android.com8af96022009-10-12 17:02:22 +0000394///////////////////////////////////////////////////////////////////////////////
395
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500396#include "include/core/SkImageEncoder.h"
reed@android.com63e34c62009-10-06 21:19:18 +0000397
398static const SlideProc gProc[] = {
399 patheffect_slide,
400 gradient_slide,
reed@android.com8af96022009-10-12 17:02:22 +0000401 mesh_slide,
reed@android.com63e34c62009-10-06 21:19:18 +0000402};
403
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400404class SlideView : public Sample {
reed@android.com63e34c62009-10-06 21:19:18 +0000405 int fIndex;
reed@google.com7b185902012-03-12 21:13:48 +0000406 bool fOnce;
reed@android.com63e34c62009-10-06 21:19:18 +0000407public:
408 SlideView() {
reed@google.com7b185902012-03-12 21:13:48 +0000409 fOnce = false;
410 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000411
reed@google.com7b185902012-03-12 21:13:48 +0000412 void init() {
413 if (fOnce) {
414 return;
415 }
416 fOnce = true;
417
reed@android.com63e34c62009-10-06 21:19:18 +0000418 fIndex = 0;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000419
reed@android.com63e34c62009-10-06 21:19:18 +0000420 SkBitmap bm;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000421 bm.allocN32Pixels(1024, 768);
reed@android.com63e34c62009-10-06 21:19:18 +0000422 SkCanvas canvas(bm);
423 SkScalar s = SkIntToScalar(1024) / 640;
424 canvas.scale(s, s);
425 for (size_t i = 0; i < SK_ARRAY_COUNT(gProc); i++) {
426 canvas.save();
reed@android.com8af96022009-10-12 17:02:22 +0000427 canvas.drawColor(BG_COLOR);
reed@android.com63e34c62009-10-06 21:19:18 +0000428 gProc[i](&canvas);
429 canvas.restore();
430 SkString str;
John Stiles70941592020-05-04 23:17:21 -0400431 str.printf("/skimages/slide_%zu.png", i);
Mike Kleinea3f0142019-03-20 11:12:10 -0500432 ToolUtils::EncodeImageToFile(str.c_str(), bm, SkEncodedImageFormat::kPNG, 100);
reed@android.com63e34c62009-10-06 21:19:18 +0000433 }
reed@google.com81e3d7f2011-06-01 12:42:36 +0000434 this->setBGColor(BG_COLOR);
reed@android.com63e34c62009-10-06 21:19:18 +0000435 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000436
reed@android.com63e34c62009-10-06 21:19:18 +0000437protected:
Hal Canary8a027312019-07-03 10:55:44 -0400438 SkString name() override { return SkString("Slides"); }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000439
mtklein36352bf2015-03-25 18:17:31 -0700440 void onDrawContent(SkCanvas* canvas) override {
reed@google.com7b185902012-03-12 21:13:48 +0000441 this->init();
reed@android.com63e34c62009-10-06 21:19:18 +0000442 gProc[fIndex](canvas);
443 }
444
Hal Canaryb1f411a2019-08-29 10:39:22 -0400445 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override {
reed@google.com7b185902012-03-12 21:13:48 +0000446 this->init();
reed@android.com63e34c62009-10-06 21:19:18 +0000447 fIndex = (fIndex + 1) % SK_ARRAY_COUNT(gProc);
halcanary96fcdcc2015-08-27 07:41:13 -0700448 return nullptr;
reed@android.com63e34c62009-10-06 21:19:18 +0000449 }
450
451private:
John Stiles7571f9e2020-09-02 22:42:33 -0400452 using INHERITED = Sample;
reed@android.com63e34c62009-10-06 21:19:18 +0000453};
454
455//////////////////////////////////////////////////////////////////////////////
456
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400457DEF_SAMPLE( return new SlideView(); )