Debian Codegraph @

tools/viewer/SlidesSlide.cpp

raw
1 /*
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 */
7 #include "include/core/SkBitmap.h"
8 #include "include/core/SkCanvas.h"
9 #include "include/core/SkPaint.h"
10 #include "include/core/SkPathBuilder.h"
11 #include "include/core/SkVertices.h"
12 #include "include/effects/SkGradient.h"
13 #include "include/private/base/SkFloatingPoint.h"
14 #include "src/core/SkBlurMask.h"
15 #include "src/core/SkReadBuffer.h"
16 #include "src/core/SkWriteBuffer.h"
17 #include "tools/EncodeUtils.h"
18 #include "tools/viewer/ClickHandlerSlide.h"
19
20 #define BG_COLOR 0xFFDDDDDD
21
22 typedef void (*SlideProc)(SkCanvas*);
23
24 ///////////////////////////////////////////////////////////////////////////////
25
26 #include "include/effects/Sk1DPathEffect.h"
27 #include "include/effects/Sk2DPathEffect.h"
28 #include "include/effects/SkCornerPathEffect.h"
29 #include "include/effects/SkDashPathEffect.h"
30 #include "include/effects/SkDiscretePathEffect.h"
31
32 static void compose_pe(SkPaint* paint) {
33 SkPathEffect* pe = paint->getPathEffect();
34 sk_sp<SkPathEffect> corner = SkCornerPathEffect::Make(25);
35 sk_sp<SkPathEffect> compose;
36 if (pe) {
37 compose = SkPathEffect::MakeCompose(sk_ref_sp(pe), corner);
38 } else {
39 compose = corner;
40 }
41 paint->setPathEffect(compose);
42 }
43
44 static void hair_pe(SkPaint* paint) {
45 paint->setStrokeWidth(0);
46 }
47
48 static void hair2_pe(SkPaint* paint) {
49 paint->setStrokeWidth(0);
50 compose_pe(paint);
51 }
52
53 static void stroke_pe(SkPaint* paint) {
54 paint->setStrokeWidth(12);
55 compose_pe(paint);
56 }
57
58 static void dash_pe(SkPaint* paint) {
59 SkScalar inter[] = { 20, 10, 10, 10 };
60 paint->setStrokeWidth(12);
61 paint->setPathEffect(SkDashPathEffect::Make(inter, 0));
62 compose_pe(paint);
63 }
64
65 static const int gXY[] = {
66 4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
67 };
68
69 static void one_d_pe(SkPaint* paint) {
70 SkPathBuilder builder;
71 builder.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
72 for (unsigned i = 2; i < std::size(gXY); i += 2)
73 builder.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
74 builder.close();
75 builder.offset(SkIntToScalar(-6), 0);
76 builder.transform(SkMatrix::Scale(1.5f, 1.5f));
77 SkPath path = builder.detach();
78
79 paint->setPathEffect(SkPath1DPathEffect::Make(path, SkIntToScalar(21), 0,
80 SkPath1DPathEffect::kRotate_Style));
81 compose_pe(paint);
82 }
83
84 typedef void (*PE_Proc)(SkPaint*);
85 static const PE_Proc gPE[] = { hair_pe, hair2_pe, stroke_pe, dash_pe, one_d_pe };
86
87 static void fill_pe(SkPaint* paint) {
88 paint->setStyle(SkPaint::kFill_Style);
89 paint->setPathEffect(nullptr);
90 }
91
92 static void discrete_pe(SkPaint* paint) {
93 paint->setPathEffect(SkDiscretePathEffect::Make(10, 4));
94 }
95
96 static sk_sp<SkPathEffect> MakeTileEffect() {
97 SkMatrix m;
98 m.setScale(SkIntToScalar(12), SkIntToScalar(12));
99
100 SkPath path = SkPath::Circle(0, 0, 5);
101
102 return SkPath2DPathEffect::Make(m, path);
103 }
104
105 static void tile_pe(SkPaint* paint) {
106 paint->setPathEffect(MakeTileEffect());
107 }
108
109 static const PE_Proc gPE2[] = { fill_pe, discrete_pe, tile_pe };
110
111 static void patheffect_slide(SkCanvas* canvas) {
112 SkPaint paint;
113 paint.setAntiAlias(true);
114 paint.setStyle(SkPaint::kStroke_Style);
115
116 SkPath path = SkPathBuilder()
117 .moveTo(20, 20)
118 .lineTo(70, 120)
119 .lineTo(120, 30)
120 .lineTo(170, 80)
121 .lineTo(240, 50)
122 .detach();
123
124 size_t i;
125 canvas->save();
126 for (i = 0; i < std::size(gPE); i++) {
127 gPE[i](&paint);
128 canvas->drawPath(path, paint);
129 canvas->translate(0, 75);
130 }
131 canvas->restore();
132
133 SkRect r = { 0, 0, 250, 120 };
134 path = SkPathBuilder()
135 .addOval(r, SkPathDirection::kCW)
136 .addRect(r.makeInset(50, 50), SkPathDirection::kCCW)
137 .detach();
138
139 canvas->translate(320, 20);
140 for (i = 0; i < std::size(gPE2); i++) {
141 gPE2[i](&paint);
142 canvas->drawPath(path, paint);
143 canvas->translate(0, 160);
144 }
145 }
146
147 ///////////////////////////////////////////////////////////////////////////////
148
149 #include "include/effects/SkGradient.h"
150
151 struct GradData {
152 size_t fCount;
153 const SkColor4f* fColors;
154 const float* fPos;
155
156 SkGradient grad(SkTileMode tm) const {
157 SkSpan<const float> pos;
158 if (fPos) {
159 pos = {fPos, fCount};
160 }
161 return {{{fColors, fCount}, pos, tm}, {}};
162 }
163 };
164
165 static const SkColor4f gColors[] = {
166 SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kWhite, SkColors::kBlack
167 };
168 static const SkScalar gPos0[] = { 0, SK_Scalar1 };
169 static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
170 static const SkScalar gPos2[] = {
171 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1
172 };
173
174 static const GradData gGradData[] = {
175 { 2, gColors, nullptr },
176 { 2, gColors, gPos0 },
177 { 2, gColors, gPos1 },
178 { 5, gColors, nullptr },
179 { 5, gColors, gPos2 }
180 };
181
182 static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkTileMode tm) {
183 return SkShaders::LinearGradient(pts, data.grad(tm));
184 }
185
186 static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkTileMode tm) {
187 SkPoint center;
188 center.set(sk_float_midpoint(pts[0].fX, pts[1].fX),
189 sk_float_midpoint(pts[0].fY, pts[1].fY));
190 return SkShaders::RadialGradient(center, center.fX, data.grad(tm));
191 }
192
193 static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkTileMode tm) {
194 SkPoint center;
195 center.set(sk_float_midpoint(pts[0].fX, pts[1].fX),
196 sk_float_midpoint(pts[0].fY, pts[1].fY));
197 return SkShaders::SweepGradient(center, data.grad(tm));
198 }
199
200 static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, SkTileMode tm) {
201 SkPoint center0, center1;
202 center0.set(sk_float_midpoint(pts[0].fX, pts[1].fX),
203 sk_float_midpoint(pts[0].fY, pts[1].fY));
204 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
205 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
206 return SkShaders::TwoPointConicalGradient(center1, (pts[1].fX - pts[0].fX) / 7,
207 center0, (pts[1].fX - pts[0].fX) / 2,
208 data.grad(tm));
209 }
210
211 typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData&, SkTileMode);
212 static const GradMaker gGradMakers[] = {
213 MakeLinear, MakeRadial, MakeSweep, Make2Conical
214 };
215
216 static void gradient_slide(SkCanvas* canvas) {
217 SkPoint pts[2] = {
218 { 0, 0 },
219 { SkIntToScalar(100), SkIntToScalar(100) }
220 };
221 SkTileMode tm = SkTileMode::kClamp;
222 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
223 SkPaint paint;
224 paint.setAntiAlias(true);
225 paint.setDither(true);
226
227 canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
228 for (size_t i = 0; i < std::size(gGradData); i++) {
229 canvas->save();
230 for (size_t j = 0; j < std::size(gGradMakers); j++) {
231 paint.setShader(gGradMakers[j](pts, gGradData[i], tm));
232 canvas->drawRect(r, paint);
233 canvas->translate(0, SkIntToScalar(120));
234 }
235 canvas->restore();
236 canvas->translate(SkIntToScalar(120), 0);
237 }
238 }
239
240 ///////////////////////////////////////////////////////////////////////////////
241
242 #include "include/core/SkStream.h"
243 #include "src/base/SkRandom.h"
244 #include "src/core/SkOSFile.h"
245 #include "tools/DecodeUtils.h"
246
247 static sk_sp<SkShader> make_shader0(SkIPoint* size) {
248 SkBitmap bm;
249 ToolUtils::GetResourceAsBitmap("images/baby_tux.png", &bm);
250 size->set(bm.width(), bm.height());
251 return bm.makeShader(SkSamplingOptions(SkFilterMode::kLinear));
252 }
253
254 static sk_sp<SkShader> make_shader1(const SkIPoint& size) {
255 SkPoint pts[] = { { 0, 0 },
256 { SkIntToScalar(size.fX), SkIntToScalar(size.fY) } };
257 SkColor4f colors[] = { SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kRed };
258 return SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kMirror}, {}});
259 }
260
261 class Rec {
262 public:
263 SkVertices::VertexMode fMode;
264 int fCount;
265 SkPoint* fVerts;
266 SkPoint* fTexs;
267
268 Rec() : fCount(0), fVerts(nullptr), fTexs(nullptr) {}
269 ~Rec() { delete[] fVerts; delete[] fTexs; }
270 };
271
272 static void make_tris(Rec* rec) {
273 int n = 10;
274 SkRandom rand;
275
276 rec->fMode = SkVertices::kTriangles_VertexMode;
277 rec->fCount = n * 3;
278 rec->fVerts = new SkPoint[rec->fCount];
279
280 for (int i = 0; i < n; i++) {
281 SkPoint* v = &rec->fVerts[i*3];
282 for (int j = 0; j < 3; j++) {
283 v[j].set(rand.nextUScalar1() * 250, rand.nextUScalar1() * 250);
284 }
285 }
286 }
287
288 static void make_fan(Rec* rec, int texWidth, int texHeight) {
289 const SkScalar tx = SkIntToScalar(texWidth);
290 const SkScalar ty = SkIntToScalar(texHeight);
291 const int n = 24;
292
293 rec->fMode = SkVertices::kTriangleFan_VertexMode;
294 rec->fCount = n + 2;
295 rec->fVerts = new SkPoint[rec->fCount];
296 rec->fTexs = new SkPoint[rec->fCount];
297
298 SkPoint* v = rec->fVerts;
299 SkPoint* t = rec->fTexs;
300
301 v[0].set(0, 0);
302 t[0].set(0, 0);
303 for (int i = 0; i < n; i++) {
304 SkScalar r = SK_ScalarPI * 2 * i / n,
305 sin = SkScalarSin(r),
306 cos = SkScalarCos(r);
307 v[i+1].set(cos, sin);
308 t[i+1].set(i*tx/n, ty);
309 }
310 v[n+1] = v[1];
311 t[n+1].set(tx, ty);
312
313 SkMatrix m;
314 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
315 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
316 m.mapPoints({v, (size_t)rec->fCount});
317 }
318
319 static void make_strip(Rec* rec, int texWidth, int texHeight) {
320 const SkScalar tx = SkIntToScalar(texWidth);
321 const SkScalar ty = SkIntToScalar(texHeight);
322 const int n = 24;
323
324 rec->fMode = SkVertices::kTriangleStrip_VertexMode;
325 rec->fCount = 2 * (n + 1);
326 rec->fVerts = new SkPoint[rec->fCount];
327 rec->fTexs = new SkPoint[rec->fCount];
328
329 SkPoint* v = rec->fVerts;
330 SkPoint* t = rec->fTexs;
331
332 for (int i = 0; i < n; i++) {
333 SkScalar r = SK_ScalarPI * 2 * i / n,
334 sin = SkScalarSin(r),
335 cos = SkScalarCos(r);
336 v[i*2 + 0].set(cos/2, sin/2);
337 v[i*2 + 1].set(cos, sin);
338
339 t[i*2 + 0].set(tx * i / n, ty);
340 t[i*2 + 1].set(tx * i / n, 0);
341 }
342 v[2*n + 0] = v[0];
343 v[2*n + 1] = v[1];
344
345 t[2*n + 0].set(tx, ty);
346 t[2*n + 1].set(tx, 0);
347
348 SkMatrix m;
349 m.setScale(SkIntToScalar(100), SkIntToScalar(100));
350 m.postTranslate(SkIntToScalar(110), SkIntToScalar(110));
351 m.mapPoints({v, (size_t)rec->fCount});
352 }
353
354 static void mesh_slide(SkCanvas* canvas) {
355 Rec fRecs[3];
356 SkIPoint size;
357
358 auto fShader0 = make_shader0(&size);
359 auto fShader1 = make_shader1(size);
360
361 make_strip(&fRecs[0], size.fX, size.fY);
362 make_fan(&fRecs[1], size.fX, size.fY);
363 make_tris(&fRecs[2]);
364
365 SkPaint paint;
366 paint.setDither(true);
367
368 for (size_t i = 0; i < std::size(fRecs); i++) {
369 auto verts = SkVertices::MakeCopy(fRecs[i].fMode, fRecs[i].fCount,
370 fRecs[i].fVerts, fRecs[i].fTexs, nullptr);
371 canvas->save();
372
373 paint.setShader(nullptr);
374 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
375
376 canvas->translate(SkIntToScalar(210), 0);
377
378 paint.setShader(fShader0);
379 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
380
381 canvas->translate(SkIntToScalar(210), 0);
382
383 paint.setShader(fShader1);
384 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
385 canvas->restore();
386
387 canvas->translate(0, SkIntToScalar(250));
388 }
389 }
390
391 ///////////////////////////////////////////////////////////////////////////////
392
393 #include "include/core/SkTypeface.h"
394
395 ///////////////////////////////////////////////////////////////////////////////
396
397
398 static const SlideProc gProc[] = {
399 patheffect_slide,
400 gradient_slide,
401 mesh_slide,
402 };
403
404 class SlidesSlide : public ClickHandlerSlide {
405 int fIndex;
406
407 public:
408 SlidesSlide() { fName = "Slides"; }
409
410 void load(SkScalar w, SkScalar h) override {
411 fIndex = 0;
412
413 SkBitmap bm;
414 bm.allocN32Pixels(1024, 768);
415 SkCanvas canvas(bm);
416 SkScalar s = SkIntToScalar(1024) / 640;
417 canvas.scale(s, s);
418 for (size_t i = 0; i < std::size(gProc); i++) {
419 canvas.save();
420 canvas.drawColor(BG_COLOR);
421 gProc[i](&canvas);
422 canvas.restore();
423 SkString str;
424 str.printf("/skimages/slide_%zu.png", i);
425 ToolUtils::EncodeImageToPngFile(str.c_str(), bm);
426 }
427 }
428
429 void draw(SkCanvas* canvas) override {
430 gProc[fIndex](canvas);
431 }
432
433 protected:
434 Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override {
435 fIndex = (fIndex + 1) % std::size(gProc);
436 return nullptr;
437 }
438
439 bool onClick(ClickHandlerSlide::Click *) override { return false; }
440 };
441
442 //////////////////////////////////////////////////////////////////////////////
443
444 DEF_SLIDE( return new SlidesSlide(); )