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.h"
9#include "SkCanvas.h"
10#include "SkPath.h"
11#include "SkRandom.h"
12#include "SkScalar.h"
13#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:
25
26    SkString onShortName() override {
27        return SkString("polygons");
28    }
29
30    SkISize onISize() override {
31        int width = kNumPolygons * kCellSize + 40;
32        int height = (kNumJoins * kNumStrokeWidths + kNumExtraStyles) * kCellSize + 40;
33        return SkISize::Make(width, height);
34    }
35
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
49
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);
53        }
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
88    static void SetColorAndAlpha(SkPaint* paint, SkRandom* rand) {
89        SkColor color = rand->nextU();
90        color |= 0xff000000;
91        paint->setColor(color);
92        if (40 == paint->getStrokeWidth()) {
93            paint->setAlpha(0xA0);
94        }
95    }
96
97    void onDraw(SkCanvas* canvas) override {
98        // 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)
101        static const int kStrokeWidths[] = {0, 10, 40};
102        SkASSERT(kNumStrokeWidths == SK_ARRAY_COUNT(kStrokeWidths));
103
104        static const SkPaint::Join kJoins[] = {
105            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
113        SkRandom rand;
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) {
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
134        static const SkPaint::Style kStyles[] = {
135            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:
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;
160
161    SkTArray<SkPath> fPolygons;
162    typedef GM INHERITED;
163};
164
165//////////////////////////////////////////////////////////////////////////////
166
167DEF_GM(return new PolygonsGM;)
168
169}
170