blob: 42bf6778993bbc757563233eb033ef4c95dc7622 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkPoint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkScalar.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#include "include/core/SkSize.h"
16#include "include/core/SkString.h"
17#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/private/SkTArray.h"
19#include "include/utils/SkRandom.h"
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000020
21namespace 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.
26class PolygonsGM: public GM {
27public:
28 PolygonsGM() {}
29
30protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000031
mtklein36352bf2015-03-25 18:17:31 -070032 SkString onShortName() override {
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000033 return SkString("polygons");
34 }
35
mtklein36352bf2015-03-25 18:17:31 -070036 SkISize onISize() override {
reed@google.com7fa2a652014-01-27 13:42:58 +000037 int width = kNumPolygons * kCellSize + 40;
38 int height = (kNumJoins * kNumStrokeWidths + kNumExtraStyles) * kCellSize + 40;
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000039 return SkISize::Make(width, height);
40 }
41
42 // Construct all polygons
mtklein36352bf2015-03-25 18:17:31 -070043 void onOnceBeforeDraw() override {
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000044 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);
bungeman@google.com09800bc2013-11-27 16:08:37 +000058 p4[i].set(20 * SkScalarCos(angle) + 20, 20 * SkScalarSin(angle) + 20);
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000059 }
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
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
scroggof9d61012014-12-15 12:54:51 -080094 static void SetColorAndAlpha(SkPaint* paint, SkRandom* rand) {
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000095 SkColor color = rand->nextU();
96 color |= 0xff000000;
97 paint->setColor(color);
98 if (40 == paint->getStrokeWidth()) {
99 paint->setAlpha(0xA0);
100 }
101 }
102
mtklein36352bf2015-03-25 18:17:31 -0700103 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000104 // 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)
mtkleindbfd7ab2016-09-01 11:24:54 -0700107 constexpr int kStrokeWidths[] = {0, 10, 40};
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000108 SkASSERT(kNumStrokeWidths == SK_ARRAY_COUNT(kStrokeWidths));
109
mtkleindbfd7ab2016-09-01 11:24:54 -0700110 constexpr SkPaint::Join kJoins[] = {
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000111 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
scroggof9d61012014-12-15 12:54:51 -0800119 SkRandom rand;
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000120 // 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
mtkleindbfd7ab2016-09-01 11:24:54 -0700140 constexpr SkPaint::Style kStyles[] = {
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000141 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
160private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700161 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;
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000166
167 SkTArray<SkPath> fPolygons;
168 typedef GM INHERITED;
169};
170
171//////////////////////////////////////////////////////////////////////////////
172
173DEF_GM(return new PolygonsGM;)
174
175}