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    virtual uint32_t onGetFlags() const SK_OVERRIDE {
26        return kSkipTiled_Flag;
27    }
28
29    virtual SkString onShortName() SK_OVERRIDE {
30        return SkString("polygons");
31    }
32
33    virtual SkISize onISize() SK_OVERRIDE {
34        int width = kNumPolygons * kCellSize + 40;
35        int height = (kNumJoins * kNumStrokeWidths + kNumExtraStyles) * kCellSize + 40;
36        return SkISize::Make(width, height);
37    }
38
39    // Construct all polygons
40    virtual void onOnceBeforeDraw() SK_OVERRIDE {
41        SkPoint p0[] = {{0, 0}, {60, 0}, {90, 40}};  // triangle
42        SkPoint p1[] = {{0, 0}, {0, 40}, {60, 40}, {40, 0}};  // trapezoid
43        SkPoint p2[] = {{0, 0}, {40, 40}, {80, 40}, {40, 0}};  // diamond
44        SkPoint p3[] = {{10, 0}, {50, 0}, {60, 10}, {60, 30}, {50, 40},
45                        {10, 40}, {0, 30}, {0, 10}};  // octagon
46        SkPoint p4[32];  // circle-like polygons with 32-edges.
47        SkPoint p5[] = {{0, 0}, {20, 20}, {0, 40}, {60, 20}};  // concave polygon with 4 edges
48        SkPoint p6[] = {{0, 40}, {0, 30}, {15, 30}, {15, 20}, {30, 20},
49                        {30, 10}, {45, 10}, {45, 0}, {60, 0}, {60, 40}};  // stairs-like polygon
50        SkPoint p7[] = {{0, 20}, {20, 20}, {30, 0}, {40, 20}, {60, 20},
51                        {45, 30}, {55, 50}, {30, 40}, {5, 50}, {15, 30}};  // five-point stars
52
53        for (size_t i = 0; i < SK_ARRAY_COUNT(p4); ++i) {
54            SkScalar angle = 2 * SK_ScalarPI * i / SK_ARRAY_COUNT(p4);
55            p4[i].set(20 * SkScalarCos(angle) + 20, 20 * SkScalarSin(angle) + 20);
56        }
57
58        struct Polygons {
59            SkPoint* fPoints;
60            size_t fPointNum;
61        } pgs[] = {
62            { p0, SK_ARRAY_COUNT(p0) },
63            { p1, SK_ARRAY_COUNT(p1) },
64            { p2, SK_ARRAY_COUNT(p2) },
65            { p3, SK_ARRAY_COUNT(p3) },
66            { p4, SK_ARRAY_COUNT(p4) },
67            { p5, SK_ARRAY_COUNT(p5) },
68            { p6, SK_ARRAY_COUNT(p6) },
69            { p7, SK_ARRAY_COUNT(p7) }
70        };
71
72        SkASSERT(SK_ARRAY_COUNT(pgs) == kNumPolygons);
73        for (size_t pgIndex = 0; pgIndex < SK_ARRAY_COUNT(pgs); ++pgIndex) {
74            fPolygons.push_back().moveTo(pgs[pgIndex].fPoints[0].fX,
75                                         pgs[pgIndex].fPoints[0].fY);
76            for (size_t ptIndex = 1; ptIndex < pgs[pgIndex].fPointNum; ++ptIndex) {
77                fPolygons.back().lineTo(pgs[pgIndex].fPoints[ptIndex].fX,
78                                        pgs[pgIndex].fPoints[ptIndex].fY);
79            }
80            fPolygons.back().close();
81        }
82    }
83
84    // Set the location for the current test on the canvas
85    static void SetLocation(SkCanvas* canvas, int counter, int lineNum) {
86        SkScalar x = SK_Scalar1 * kCellSize * (counter % lineNum) + 30 + SK_Scalar1 / 4;
87        SkScalar y = SK_Scalar1 * kCellSize * (counter / lineNum) + 30 + 3 * SK_Scalar1 / 4;
88        canvas->translate(x, y);
89    }
90
91    static void SetColorAndAlpha(SkPaint* paint, SkLCGRandom* rand) {
92        SkColor color = rand->nextU();
93        color |= 0xff000000;
94        paint->setColor(color);
95        if (40 == paint->getStrokeWidth()) {
96            paint->setAlpha(0xA0);
97        }
98    }
99
100    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
101        // Stroke widths are:
102        // 0(may use hairline rendering), 10(common case for stroke-style)
103        // 40(>= geometry width/height, make the contour filled in fact)
104        static const int kStrokeWidths[] = {0, 10, 40};
105        SkASSERT(kNumStrokeWidths == SK_ARRAY_COUNT(kStrokeWidths));
106
107        static const SkPaint::Join kJoins[] = {
108            SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
109        };
110        SkASSERT(kNumJoins == SK_ARRAY_COUNT(kJoins));
111
112        int counter = 0;
113        SkPaint paint;
114        paint.setAntiAlias(true);
115
116        SkLCGRandom rand;
117        // For stroke style painter
118        paint.setStyle(SkPaint::kStroke_Style);
119        for (int join = 0; join < kNumJoins; ++join) {
120            for (int width = 0; width < kNumStrokeWidths; ++width) {
121                for (int i = 0; i < fPolygons.count(); ++i) {
122                    canvas->save();
123                    SetLocation(canvas, counter, fPolygons.count());
124
125                    SetColorAndAlpha(&paint, &rand);
126                    paint.setStrokeJoin(kJoins[join]);
127                    paint.setStrokeWidth(SkIntToScalar(kStrokeWidths[width]));
128
129                    canvas->drawPath(fPolygons[i], paint);
130                    canvas->restore();
131                    ++counter;
132                }
133            }
134        }
135
136        // For stroke-and-fill style painter and fill style painter
137        static const SkPaint::Style kStyles[] = {
138            SkPaint::kStrokeAndFill_Style, SkPaint::kFill_Style
139        };
140        SkASSERT(kNumExtraStyles == SK_ARRAY_COUNT(kStyles));
141
142        paint.setStrokeJoin(SkPaint::kMiter_Join);
143        paint.setStrokeWidth(SkIntToScalar(20));
144        for (int style = 0; style < kNumExtraStyles; ++style) {
145            paint.setStyle(kStyles[style]);
146            for (int i = 0; i < fPolygons.count(); ++i) {
147                canvas->save();
148                SetLocation(canvas, counter, fPolygons.count());
149                SetColorAndAlpha(&paint, &rand);
150                canvas->drawPath(fPolygons[i], paint);
151                canvas->restore();
152                ++counter;
153            }
154        }
155    }
156
157private:
158    static const int kNumPolygons = 8;
159    static const int kCellSize = 100;
160    static const int kNumExtraStyles = 2;
161    static const int kNumStrokeWidths = 3;
162    static const int kNumJoins = 3;
163
164    SkTArray<SkPath> fPolygons;
165    typedef GM INHERITED;
166};
167
168//////////////////////////////////////////////////////////////////////////////
169
170DEF_GM(return new PolygonsGM;)
171
172}
173