• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPoint.h"
13 #include "include/core/SkScalar.h"
14 #include "include/core/SkSize.h"
15 #include "include/core/SkString.h"
16 #include "include/utils/SkRandom.h"
17 
18 #include <stddef.h>
19 
20 namespace skiagm {
21 
22 class PointsGM : public GM {
23 public:
PointsGM()24     PointsGM() {}
25 
26 protected:
27 
onShortName()28     SkString onShortName() override {
29         return SkString("points");
30     }
31 
onISize()32     SkISize onISize() override {
33         return SkISize::Make(640, 490);
34     }
35 
fill_pts(SkPoint pts[],size_t n,SkRandom * rand)36     static void fill_pts(SkPoint pts[], size_t n, SkRandom* rand) {
37         for (size_t i = 0; i < n; i++) {
38             // Compute these independently and store in variables, rather
39             // than in the parameter-passing expression, to get consistent
40             // evaluation order across compilers.
41             SkScalar y = rand->nextUScalar1() * 480;
42             SkScalar x = rand->nextUScalar1() * 640;
43             pts[i].set(x, y);
44         }
45     }
46 
onDraw(SkCanvas * canvas)47     void onDraw(SkCanvas* canvas) override {
48         canvas->translate(SK_Scalar1, SK_Scalar1);
49 
50         SkRandom rand;
51         SkPaint  p0, p1, p2, p3;
52         const size_t n = 99;
53 
54         p0.setColor(SK_ColorRED);
55         p1.setColor(SK_ColorGREEN);
56         p2.setColor(SK_ColorBLUE);
57         p3.setColor(SK_ColorWHITE);
58 
59         p0.setStrokeWidth(SkIntToScalar(4));
60         p2.setStrokeCap(SkPaint::kRound_Cap);
61         p2.setStrokeWidth(SkIntToScalar(6));
62 
63         SkPoint* pts = new SkPoint[n];
64         fill_pts(pts, n, &rand);
65 
66         canvas->drawPoints(SkCanvas::kPolygon_PointMode, n, pts, p0);
67         canvas->drawPoints(SkCanvas::kLines_PointMode, n, pts, p1);
68         canvas->drawPoints(SkCanvas::kPoints_PointMode, n, pts, p2);
69         canvas->drawPoints(SkCanvas::kPoints_PointMode, n, pts, p3);
70 
71         delete[] pts;
72     }
73 
74 private:
75     using INHERITED = GM;
76 };
77 DEF_GM( return new PointsGM; )
78 }  // namespace skiagm
79 
80 #include "include/core/SkMaskFilter.h"
81 
82 DEF_SIMPLE_GM(points_maskfilter, canvas, 512, 256) {
83     constexpr int N = 30;
84     SkPoint pts[N];
85 
86     SkRandom rand;
87     for (SkPoint& p : pts) {
88         p.fX = rand.nextF() * 220 + 18;
89         p.fY = rand.nextF() * 220 + 18;
90     }
91 
92     auto mf = SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 6);
93     const SkPaint::Cap caps[] = { SkPaint::kSquare_Cap, SkPaint::kRound_Cap };
94 
95     SkPaint paint;
96     paint.setAntiAlias(true);
97     paint.setStroke(true);
98     paint.setStrokeWidth(10);
99 
100     for (auto cap : caps) {
101         paint.setStrokeCap(cap);
102 
103         paint.setMaskFilter(mf);
104         paint.setColor(SK_ColorBLACK);
105         canvas->drawPoints(SkCanvas::kPoints_PointMode, N, pts, paint);
106 
107         paint.setMaskFilter(nullptr);
108         paint.setColor(SK_ColorRED);
109         canvas->drawPoints(SkCanvas::kPoints_PointMode, N, pts, paint);
110 
111         canvas->translate(256, 0);
112     }
113 }
114