2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
17 // This GM tests a grab-bag of convex and concave polygons. They are triangles,
18 // trapezoid, diamond, polygons with lots of edges, several concave polygons...
19 // But rectangles are excluded.
20 class PolygonsGM: public GM {
26 SkString onShortName() override {
27 return SkString("polygons");
30 SkISize onISize() override {
31 int width = kNumPolygons * kCellSize + 40;
32 int height = (kNumJoins * kNumStrokeWidths + kNumExtraStyles) * kCellSize + 40;
33 return SkISize::Make(width, height);
36 // Construct all polygons
37 void onOnceBeforeDraw() override {
38 SkPoint p0[] = {{0, 0}, {60, 0}, {90, 40}}; // triangle
39 SkPoint p1[] = {{0, 0}, {0, 40}, {60, 40}, {40, 0}}; // trapezoid
40 SkPoint p2[] = {{0, 0}, {40, 40}, {80, 40}, {40, 0}}; // diamond
41 SkPoint p3[] = {{10, 0}, {50, 0}, {60, 10}, {60, 30}, {50, 40},
42 {10, 40}, {0, 30}, {0, 10}}; // octagon
43 SkPoint p4[32]; // circle-like polygons with 32-edges.
44 SkPoint p5[] = {{0, 0}, {20, 20}, {0, 40}, {60, 20}}; // concave polygon with 4 edges
45 SkPoint p6[] = {{0, 40}, {0, 30}, {15, 30}, {15, 20}, {30, 20},
46 {30, 10}, {45, 10}, {45, 0}, {60, 0}, {60, 40}}; // stairs-like polygon
47 SkPoint p7[] = {{0, 20}, {20, 20}, {30, 0}, {40, 20}, {60, 20},
48 {45, 30}, {55, 50}, {30, 40}, {5, 50}, {15, 30}}; // five-point stars
50 for (size_t i = 0; i < SK_ARRAY_COUNT(p4); ++i) {
51 SkScalar angle = 2 * SK_ScalarPI * i / SK_ARRAY_COUNT(p4);
52 p4[i].set(20 * SkScalarCos(angle) + 20, 20 * SkScalarSin(angle) + 20);
59 { p0, SK_ARRAY_COUNT(p0) },
60 { p1, SK_ARRAY_COUNT(p1) },
61 { p2, SK_ARRAY_COUNT(p2) },
62 { p3, SK_ARRAY_COUNT(p3) },
63 { p4, SK_ARRAY_COUNT(p4) },
64 { p5, SK_ARRAY_COUNT(p5) },
65 { p6, SK_ARRAY_COUNT(p6) },
66 { p7, SK_ARRAY_COUNT(p7) }
69 SkASSERT(SK_ARRAY_COUNT(pgs) == kNumPolygons);
70 for (size_t pgIndex = 0; pgIndex < SK_ARRAY_COUNT(pgs); ++pgIndex) {
71 fPolygons.push_back().moveTo(pgs[pgIndex].fPoints[0].fX,
72 pgs[pgIndex].fPoints[0].fY);
73 for (size_t ptIndex = 1; ptIndex < pgs[pgIndex].fPointNum; ++ptIndex) {
74 fPolygons.back().lineTo(pgs[pgIndex].fPoints[ptIndex].fX,
75 pgs[pgIndex].fPoints[ptIndex].fY);
77 fPolygons.back().close();
81 // Set the location for the current test on the canvas
82 static void SetLocation(SkCanvas* canvas, int counter, int lineNum) {
83 SkScalar x = SK_Scalar1 * kCellSize * (counter % lineNum) + 30 + SK_Scalar1 / 4;
84 SkScalar y = SK_Scalar1 * kCellSize * (counter / lineNum) + 30 + 3 * SK_Scalar1 / 4;
85 canvas->translate(x, y);
88 static void SetColorAndAlpha(SkPaint* paint, SkRandom* rand) {
89 SkColor color = rand->nextU();
91 paint->setColor(color);
92 if (40 == paint->getStrokeWidth()) {
93 paint->setAlpha(0xA0);
97 void onDraw(SkCanvas* canvas) override {
99 // 0(may use hairline rendering), 10(common case for stroke-style)
100 // 40(>= geometry width/height, make the contour filled in fact)
101 static const int kStrokeWidths[] = {0, 10, 40};
102 SkASSERT(kNumStrokeWidths == SK_ARRAY_COUNT(kStrokeWidths));
104 static const SkPaint::Join kJoins[] = {
105 SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
107 SkASSERT(kNumJoins == SK_ARRAY_COUNT(kJoins));
111 paint.setAntiAlias(true);
114 // For stroke style painter
115 paint.setStyle(SkPaint::kStroke_Style);
116 for (int join = 0; join < kNumJoins; ++join) {
117 for (int width = 0; width < kNumStrokeWidths; ++width) {
118 for (int i = 0; i < fPolygons.count(); ++i) {
120 SetLocation(canvas, counter, fPolygons.count());
122 SetColorAndAlpha(&paint, &rand);
123 paint.setStrokeJoin(kJoins[join]);
124 paint.setStrokeWidth(SkIntToScalar(kStrokeWidths[width]));
126 canvas->drawPath(fPolygons[i], paint);
133 // For stroke-and-fill style painter and fill style painter
134 static const SkPaint::Style kStyles[] = {
135 SkPaint::kStrokeAndFill_Style, SkPaint::kFill_Style
137 SkASSERT(kNumExtraStyles == SK_ARRAY_COUNT(kStyles));
139 paint.setStrokeJoin(SkPaint::kMiter_Join);
140 paint.setStrokeWidth(SkIntToScalar(20));
141 for (int style = 0; style < kNumExtraStyles; ++style) {
142 paint.setStyle(kStyles[style]);
143 for (int i = 0; i < fPolygons.count(); ++i) {
145 SetLocation(canvas, counter, fPolygons.count());
146 SetColorAndAlpha(&paint, &rand);
147 canvas->drawPath(fPolygons[i], paint);
155 static const int kNumPolygons = 8;
156 static const int kCellSize = 100;
157 static const int kNumExtraStyles = 2;
158 static const int kNumStrokeWidths = 3;
159 static const int kNumJoins = 3;
161 SkTArray<SkPath> fPolygons;
162 typedef GM INHERITED;
165 //////////////////////////////////////////////////////////////////////////////
167 DEF_GM(return new PolygonsGM;)