• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 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/SkPath.h"
13 #include "include/core/SkPoint.h"
14 #include "include/core/SkScalar.h"
15 #include "include/core/SkSize.h"
16 #include "include/core/SkString.h"
17 #include "include/core/SkTypes.h"
18 #include "include/private/SkTArray.h"
19 #include "include/utils/SkRandom.h"
20 
21 namespace skiagm {
22 
23 // This GM tests a grab-bag of convex and concave polygons. They are triangles,
24 // trapezoid, diamond, polygons with lots of edges, several concave polygons...
25 // But rectangles are excluded.
26 class PolygonsGM: public GM {
27 public:
PolygonsGM()28     PolygonsGM() {}
29 
30 protected:
31 
onShortName()32     SkString onShortName() override {
33         return SkString("polygons");
34     }
35 
onISize()36     SkISize onISize() override {
37         int width = kNumPolygons * kCellSize + 40;
38         int height = (kNumJoins * kNumStrokeWidths + kNumExtraStyles) * kCellSize + 40;
39         return SkISize::Make(width, height);
40     }
41 
42     // Construct all polygons
onOnceBeforeDraw()43     void onOnceBeforeDraw() override {
44         SkPoint p0[] = {{0, 0}, {60, 0}, {90, 40}};  // triangle
45         SkPoint p1[] = {{0, 0}, {0, 40}, {60, 40}, {40, 0}};  // trapezoid
46         SkPoint p2[] = {{0, 0}, {40, 40}, {80, 40}, {40, 0}};  // diamond
47         SkPoint p3[] = {{10, 0}, {50, 0}, {60, 10}, {60, 30}, {50, 40},
48                         {10, 40}, {0, 30}, {0, 10}};  // octagon
49         SkPoint p4[32];  // circle-like polygons with 32-edges.
50         SkPoint p5[] = {{0, 0}, {20, 20}, {0, 40}, {60, 20}};  // concave polygon with 4 edges
51         SkPoint p6[] = {{0, 40}, {0, 30}, {15, 30}, {15, 20}, {30, 20},
52                         {30, 10}, {45, 10}, {45, 0}, {60, 0}, {60, 40}};  // stairs-like polygon
53         SkPoint p7[] = {{0, 20}, {20, 20}, {30, 0}, {40, 20}, {60, 20},
54                         {45, 30}, {55, 50}, {30, 40}, {5, 50}, {15, 30}};  // five-point stars
55 
56         for (size_t i = 0; i < SK_ARRAY_COUNT(p4); ++i) {
57             SkScalar angle = 2 * SK_ScalarPI * i / SK_ARRAY_COUNT(p4);
58             p4[i].set(20 * SkScalarCos(angle) + 20, 20 * SkScalarSin(angle) + 20);
59         }
60 
61         struct Polygons {
62             SkPoint* fPoints;
63             size_t fPointNum;
64         } pgs[] = {
65             { p0, SK_ARRAY_COUNT(p0) },
66             { p1, SK_ARRAY_COUNT(p1) },
67             { p2, SK_ARRAY_COUNT(p2) },
68             { p3, SK_ARRAY_COUNT(p3) },
69             { p4, SK_ARRAY_COUNT(p4) },
70             { p5, SK_ARRAY_COUNT(p5) },
71             { p6, SK_ARRAY_COUNT(p6) },
72             { p7, SK_ARRAY_COUNT(p7) }
73         };
74 
75         SkASSERT(SK_ARRAY_COUNT(pgs) == kNumPolygons);
76         for (size_t pgIndex = 0; pgIndex < SK_ARRAY_COUNT(pgs); ++pgIndex) {
77             fPolygons.push_back().moveTo(pgs[pgIndex].fPoints[0].fX,
78                                          pgs[pgIndex].fPoints[0].fY);
79             for (size_t ptIndex = 1; ptIndex < pgs[pgIndex].fPointNum; ++ptIndex) {
80                 fPolygons.back().lineTo(pgs[pgIndex].fPoints[ptIndex].fX,
81                                         pgs[pgIndex].fPoints[ptIndex].fY);
82             }
83             fPolygons.back().close();
84         }
85     }
86 
87     // Set the location for the current test on the canvas
SetLocation(SkCanvas * canvas,int counter,int lineNum)88     static void SetLocation(SkCanvas* canvas, int counter, int lineNum) {
89         SkScalar x = SK_Scalar1 * kCellSize * (counter % lineNum) + 30 + SK_Scalar1 / 4;
90         SkScalar y = SK_Scalar1 * kCellSize * (counter / lineNum) + 30 + 3 * SK_Scalar1 / 4;
91         canvas->translate(x, y);
92     }
93 
SetColorAndAlpha(SkPaint * paint,SkRandom * rand)94     static void SetColorAndAlpha(SkPaint* paint, SkRandom* rand) {
95         SkColor color = rand->nextU();
96         color |= 0xff000000;
97         paint->setColor(color);
98         if (40 == paint->getStrokeWidth()) {
99             paint->setAlpha(0xA0);
100         }
101     }
102 
onDraw(SkCanvas * canvas)103     void onDraw(SkCanvas* canvas) override {
104         // Stroke widths are:
105         // 0(may use hairline rendering), 10(common case for stroke-style)
106         // 40(>= geometry width/height, make the contour filled in fact)
107         constexpr int kStrokeWidths[] = {0, 10, 40};
108         SkASSERT(kNumStrokeWidths == SK_ARRAY_COUNT(kStrokeWidths));
109 
110         constexpr SkPaint::Join kJoins[] = {
111             SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
112         };
113         SkASSERT(kNumJoins == SK_ARRAY_COUNT(kJoins));
114 
115         int counter = 0;
116         SkPaint paint;
117         paint.setAntiAlias(true);
118 
119         SkRandom rand;
120         // For stroke style painter
121         paint.setStyle(SkPaint::kStroke_Style);
122         for (int join = 0; join < kNumJoins; ++join) {
123             for (int width = 0; width < kNumStrokeWidths; ++width) {
124                 for (int i = 0; i < fPolygons.count(); ++i) {
125                     canvas->save();
126                     SetLocation(canvas, counter, fPolygons.count());
127 
128                     SetColorAndAlpha(&paint, &rand);
129                     paint.setStrokeJoin(kJoins[join]);
130                     paint.setStrokeWidth(SkIntToScalar(kStrokeWidths[width]));
131 
132                     canvas->drawPath(fPolygons[i], paint);
133                     canvas->restore();
134                     ++counter;
135                 }
136             }
137         }
138 
139         // For stroke-and-fill style painter and fill style painter
140         constexpr SkPaint::Style kStyles[] = {
141             SkPaint::kStrokeAndFill_Style, SkPaint::kFill_Style
142         };
143         SkASSERT(kNumExtraStyles == SK_ARRAY_COUNT(kStyles));
144 
145         paint.setStrokeJoin(SkPaint::kMiter_Join);
146         paint.setStrokeWidth(SkIntToScalar(20));
147         for (int style = 0; style < kNumExtraStyles; ++style) {
148             paint.setStyle(kStyles[style]);
149             for (int i = 0; i < fPolygons.count(); ++i) {
150                 canvas->save();
151                 SetLocation(canvas, counter, fPolygons.count());
152                 SetColorAndAlpha(&paint, &rand);
153                 canvas->drawPath(fPolygons[i], paint);
154                 canvas->restore();
155                 ++counter;
156             }
157         }
158     }
159 
160 private:
161     static constexpr int kNumPolygons = 8;
162     static constexpr int kCellSize = 100;
163     static constexpr int kNumExtraStyles = 2;
164     static constexpr int kNumStrokeWidths = 3;
165     static constexpr int kNumJoins = 3;
166 
167     SkTArray<SkPath> fPolygons;
168     typedef GM INHERITED;
169 };
170 
171 //////////////////////////////////////////////////////////////////////////////
172 
173 DEF_GM(return new PolygonsGM;)
174 
175 }
176