blob: 90307d8bbcfade4e76bf3e7daf1d1d600fd46c49 [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 Reed06d7c9d2020-08-26 12:56:51 -040012#include "include/core/SkPathBuilder.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"
Kevin Lubickdc6cc022023-01-13 11:24:27 -050018#include "include/private/base/SkTArray.h"
Kevin Lubick0d4d1142023-02-13 09:13:10 -050019#include "src/base/SkRandom.h"
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000020
Herb Derbyec96c212023-03-06 10:31:22 -050021using namespace skia_private;
22
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000023namespace skiagm {
24
25// This GM tests a grab-bag of convex and concave polygons. They are triangles,
26// trapezoid, diamond, polygons with lots of edges, several concave polygons...
27// But rectangles are excluded.
28class PolygonsGM: public GM {
29public:
30 PolygonsGM() {}
31
32protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000033 SkString getName() const override { return SkString("polygons"); }
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000034
Leandro Lovisolo8f023882023-08-15 21:13:52 +000035 SkISize getISize() override {
reed@google.com7fa2a652014-01-27 13:42:58 +000036 int width = kNumPolygons * kCellSize + 40;
37 int height = (kNumJoins * kNumStrokeWidths + kNumExtraStyles) * kCellSize + 40;
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000038 return SkISize::Make(width, height);
39 }
40
41 // Construct all polygons
mtklein36352bf2015-03-25 18:17:31 -070042 void onOnceBeforeDraw() override {
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000043 SkPoint p0[] = {{0, 0}, {60, 0}, {90, 40}}; // triangle
44 SkPoint p1[] = {{0, 0}, {0, 40}, {60, 40}, {40, 0}}; // trapezoid
45 SkPoint p2[] = {{0, 0}, {40, 40}, {80, 40}, {40, 0}}; // diamond
46 SkPoint p3[] = {{10, 0}, {50, 0}, {60, 10}, {60, 30}, {50, 40},
47 {10, 40}, {0, 30}, {0, 10}}; // octagon
48 SkPoint p4[32]; // circle-like polygons with 32-edges.
49 SkPoint p5[] = {{0, 0}, {20, 20}, {0, 40}, {60, 20}}; // concave polygon with 4 edges
50 SkPoint p6[] = {{0, 40}, {0, 30}, {15, 30}, {15, 20}, {30, 20},
51 {30, 10}, {45, 10}, {45, 0}, {60, 0}, {60, 40}}; // stairs-like polygon
52 SkPoint p7[] = {{0, 20}, {20, 20}, {30, 0}, {40, 20}, {60, 20},
53 {45, 30}, {55, 50}, {30, 40}, {5, 50}, {15, 30}}; // five-point stars
54
Herb Derbyc37b3862022-06-21 09:49:17 -040055 for (size_t i = 0; i < std::size(p4); ++i) {
56 SkScalar angle = 2 * SK_ScalarPI * i / std::size(p4);
bungeman@google.com09800bc2013-11-27 16:08:37 +000057 p4[i].set(20 * SkScalarCos(angle) + 20, 20 * SkScalarSin(angle) + 20);
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000058 }
59
60 struct Polygons {
61 SkPoint* fPoints;
62 size_t fPointNum;
63 } pgs[] = {
Herb Derbyc37b3862022-06-21 09:49:17 -040064 { p0, std::size(p0) },
65 { p1, std::size(p1) },
66 { p2, std::size(p2) },
67 { p3, std::size(p3) },
68 { p4, std::size(p4) },
69 { p5, std::size(p5) },
70 { p6, std::size(p6) },
71 { p7, std::size(p7) }
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000072 };
73
Herb Derbyc37b3862022-06-21 09:49:17 -040074 SkASSERT(std::size(pgs) == kNumPolygons);
75 for (size_t pgIndex = 0; pgIndex < std::size(pgs); ++pgIndex) {
Mike Reed06d7c9d2020-08-26 12:56:51 -040076 SkPathBuilder b;
77 b.moveTo(pgs[pgIndex].fPoints[0].fX,
78 pgs[pgIndex].fPoints[0].fY);
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000079 for (size_t ptIndex = 1; ptIndex < pgs[pgIndex].fPointNum; ++ptIndex) {
Mike Reed06d7c9d2020-08-26 12:56:51 -040080 b.lineTo(pgs[pgIndex].fPoints[ptIndex].fX,
81 pgs[pgIndex].fPoints[ptIndex].fY);
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000082 }
Mike Reed06d7c9d2020-08-26 12:56:51 -040083 b.close();
84 fPolygons.push_back(b.detach());
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000085 }
86 }
87
88 // Set the location for the current test on the canvas
89 static void SetLocation(SkCanvas* canvas, int counter, int lineNum) {
90 SkScalar x = SK_Scalar1 * kCellSize * (counter % lineNum) + 30 + SK_Scalar1 / 4;
91 SkScalar y = SK_Scalar1 * kCellSize * (counter / lineNum) + 30 + 3 * SK_Scalar1 / 4;
92 canvas->translate(x, y);
93 }
94
scroggof9d61012014-12-15 12:54:51 -080095 static void SetColorAndAlpha(SkPaint* paint, SkRandom* rand) {
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +000096 SkColor color = rand->nextU();
97 color |= 0xff000000;
98 paint->setColor(color);
99 if (40 == paint->getStrokeWidth()) {
100 paint->setAlpha(0xA0);
101 }
102 }
103
mtklein36352bf2015-03-25 18:17:31 -0700104 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000105 // Stroke widths are:
106 // 0(may use hairline rendering), 10(common case for stroke-style)
107 // 40(>= geometry width/height, make the contour filled in fact)
mtkleindbfd7ab2016-09-01 11:24:54 -0700108 constexpr int kStrokeWidths[] = {0, 10, 40};
Herb Derbyc37b3862022-06-21 09:49:17 -0400109 SkASSERT(kNumStrokeWidths == std::size(kStrokeWidths));
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000110
mtkleindbfd7ab2016-09-01 11:24:54 -0700111 constexpr SkPaint::Join kJoins[] = {
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000112 SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
113 };
Herb Derbyc37b3862022-06-21 09:49:17 -0400114 SkASSERT(kNumJoins == std::size(kJoins));
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000115
116 int counter = 0;
117 SkPaint paint;
118 paint.setAntiAlias(true);
119
scroggof9d61012014-12-15 12:54:51 -0800120 SkRandom rand;
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000121 // For stroke style painter
122 paint.setStyle(SkPaint::kStroke_Style);
123 for (int join = 0; join < kNumJoins; ++join) {
124 for (int width = 0; width < kNumStrokeWidths; ++width) {
Herb Derbyffacce52022-11-09 10:51:34 -0500125 for (int i = 0; i < fPolygons.size(); ++i) {
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000126 canvas->save();
Herb Derbyffacce52022-11-09 10:51:34 -0500127 SetLocation(canvas, counter, fPolygons.size());
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000128
129 SetColorAndAlpha(&paint, &rand);
130 paint.setStrokeJoin(kJoins[join]);
131 paint.setStrokeWidth(SkIntToScalar(kStrokeWidths[width]));
132
133 canvas->drawPath(fPolygons[i], paint);
134 canvas->restore();
135 ++counter;
136 }
137 }
138 }
139
140 // For stroke-and-fill style painter and fill style painter
mtkleindbfd7ab2016-09-01 11:24:54 -0700141 constexpr SkPaint::Style kStyles[] = {
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000142 SkPaint::kStrokeAndFill_Style, SkPaint::kFill_Style
143 };
Herb Derbyc37b3862022-06-21 09:49:17 -0400144 SkASSERT(kNumExtraStyles == std::size(kStyles));
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000145
146 paint.setStrokeJoin(SkPaint::kMiter_Join);
147 paint.setStrokeWidth(SkIntToScalar(20));
148 for (int style = 0; style < kNumExtraStyles; ++style) {
149 paint.setStyle(kStyles[style]);
Herb Derbyffacce52022-11-09 10:51:34 -0500150 for (int i = 0; i < fPolygons.size(); ++i) {
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000151 canvas->save();
Herb Derbyffacce52022-11-09 10:51:34 -0500152 SetLocation(canvas, counter, fPolygons.size());
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000153 SetColorAndAlpha(&paint, &rand);
154 canvas->drawPath(fPolygons[i], paint);
155 canvas->restore();
156 ++counter;
157 }
158 }
159 }
160
161private:
Brian Salomon9fa47cc2021-10-08 18:48:26 -0400162 inline static constexpr int kNumPolygons = 8;
163 inline static constexpr int kCellSize = 100;
164 inline static constexpr int kNumExtraStyles = 2;
165 inline static constexpr int kNumStrokeWidths = 3;
166 inline static constexpr int kNumJoins = 3;
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000167
Herb Derbyec96c212023-03-06 10:31:22 -0500168 TArray<SkPath> fPolygons;
John Stiles7571f9e2020-09-02 22:42:33 -0400169 using INHERITED = GM;
commit-bot@chromium.orgc09df2c2013-11-27 01:24:53 +0000170};
171
172//////////////////////////////////////////////////////////////////////////////
173
174DEF_GM(return new PolygonsGM;)
175
Michael Ludwig70fae352021-04-15 12:42:23 -0400176// see crbug.com/1197461
177DEF_SIMPLE_GM(conjoined_polygons, canvas, 400, 400) {
178 SkPathBuilder b;
179 b.moveTo(0.f, 120.f);
180 b.lineTo(0.f, 0.f);
181 b.lineTo(50.f, 330.f);
182 b.lineTo(90.f, 0.f);
183 b.lineTo(340.f, 0.f);
184 b.lineTo(90.f, 330.f);
185 b.lineTo(50.f, 330.f);
186 b.close();
187
188 SkPaint paint;
189 paint.setAntiAlias(true);
190 canvas->drawPath(b.detach(), paint);
191}
192
John Stilesa6841be2020-08-06 14:11:56 -0400193} // namespace skiagm