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