blob: 00fe336a6fa2a69f69d7473330ea7d9e08f3636a [file] [log] [blame]
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +00001/*
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.h"
9#include "SkCanvas.h"
10#include "SkPath.h"
11#include "SkRandom.h"
bungeman@google.com09800bc2013-11-27 16:08:37 +000012#include "SkScalar.h"
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000013#include "SkTArray.h"
14
15namespace skiagm {
16
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.
20class PolygonsGM: public GM {
21public:
22 PolygonsGM() {}
23
24protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000025
mtklein36352bf2015-03-25 18:17:31 -070026 SkString onShortName() override {
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000027 return SkString("polygons");
28 }
29
mtklein36352bf2015-03-25 18:17:31 -070030 SkISize onISize() override {
reed@google.com7fa2a652014-01-27 13:42:58 +000031 int width = kNumPolygons * kCellSize + 40;
32 int height = (kNumJoins * kNumStrokeWidths + kNumExtraStyles) * kCellSize + 40;
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000033 return SkISize::Make(width, height);
34 }
35
36 // Construct all polygons
mtklein36352bf2015-03-25 18:17:31 -070037 void onOnceBeforeDraw() override {
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000038 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
49
50 for (size_t i = 0; i < SK_ARRAY_COUNT(p4); ++i) {
51 SkScalar angle = 2 * SK_ScalarPI * i / SK_ARRAY_COUNT(p4);
bungeman@google.com09800bc2013-11-27 16:08:37 +000052 p4[i].set(20 * SkScalarCos(angle) + 20, 20 * SkScalarSin(angle) + 20);
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000053 }
54
55 struct Polygons {
56 SkPoint* fPoints;
57 size_t fPointNum;
58 } pgs[] = {
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) }
67 };
68
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);
76 }
77 fPolygons.back().close();
78 }
79 }
80
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);
86 }
87
scroggof9d61012014-12-15 12:54:51 -080088 static void SetColorAndAlpha(SkPaint* paint, SkRandom* rand) {
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000089 SkColor color = rand->nextU();
90 color |= 0xff000000;
91 paint->setColor(color);
92 if (40 == paint->getStrokeWidth()) {
93 paint->setAlpha(0xA0);
94 }
95 }
96
mtklein36352bf2015-03-25 18:17:31 -070097 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000098 // Stroke widths are:
99 // 0(may use hairline rendering), 10(common case for stroke-style)
100 // 40(>= geometry width/height, make the contour filled in fact)
mtkleindbfd7ab2016-09-01 11:24:54 -0700101 constexpr int kStrokeWidths[] = {0, 10, 40};
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000102 SkASSERT(kNumStrokeWidths == SK_ARRAY_COUNT(kStrokeWidths));
103
mtkleindbfd7ab2016-09-01 11:24:54 -0700104 constexpr SkPaint::Join kJoins[] = {
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000105 SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
106 };
107 SkASSERT(kNumJoins == SK_ARRAY_COUNT(kJoins));
108
109 int counter = 0;
110 SkPaint paint;
111 paint.setAntiAlias(true);
112
scroggof9d61012014-12-15 12:54:51 -0800113 SkRandom rand;
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000114 // 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) {
119 canvas->save();
120 SetLocation(canvas, counter, fPolygons.count());
121
122 SetColorAndAlpha(&paint, &rand);
123 paint.setStrokeJoin(kJoins[join]);
124 paint.setStrokeWidth(SkIntToScalar(kStrokeWidths[width]));
125
126 canvas->drawPath(fPolygons[i], paint);
127 canvas->restore();
128 ++counter;
129 }
130 }
131 }
132
133 // For stroke-and-fill style painter and fill style painter
mtkleindbfd7ab2016-09-01 11:24:54 -0700134 constexpr SkPaint::Style kStyles[] = {
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000135 SkPaint::kStrokeAndFill_Style, SkPaint::kFill_Style
136 };
137 SkASSERT(kNumExtraStyles == SK_ARRAY_COUNT(kStyles));
138
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) {
144 canvas->save();
145 SetLocation(canvas, counter, fPolygons.count());
146 SetColorAndAlpha(&paint, &rand);
147 canvas->drawPath(fPolygons[i], paint);
148 canvas->restore();
149 ++counter;
150 }
151 }
152 }
153
154private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700155 static constexpr int kNumPolygons = 8;
156 static constexpr int kCellSize = 100;
157 static constexpr int kNumExtraStyles = 2;
158 static constexpr int kNumStrokeWidths = 3;
159 static constexpr int kNumJoins = 3;
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000160
161 SkTArray<SkPath> fPolygons;
162 typedef GM INHERITED;
163};
164
165//////////////////////////////////////////////////////////////////////////////
166
167DEF_GM(return new PolygonsGM;)
168
169}