commit-bot@chromium.org | c09df2c | 2013-11-27 01:24:53 +0000 | [diff] [blame] | 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" |
bungeman@google.com | 09800bc | 2013-11-27 16:08:37 +0000 | [diff] [blame] | 12 | #include "SkScalar.h" |
commit-bot@chromium.org | c09df2c | 2013-11-27 01:24:53 +0000 | [diff] [blame] | 13 | #include "SkTArray.h" |
| 14 | |
| 15 | namespace 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. |
| 20 | class PolygonsGM: public GM { |
| 21 | public: |
| 22 | PolygonsGM() {} |
| 23 | |
| 24 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 25 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 26 | SkString onShortName() override { |
commit-bot@chromium.org | c09df2c | 2013-11-27 01:24:53 +0000 | [diff] [blame] | 27 | return SkString("polygons"); |
| 28 | } |
| 29 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 30 | SkISize onISize() override { |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 31 | int width = kNumPolygons * kCellSize + 40; |
| 32 | int height = (kNumJoins * kNumStrokeWidths + kNumExtraStyles) * kCellSize + 40; |
commit-bot@chromium.org | c09df2c | 2013-11-27 01:24:53 +0000 | [diff] [blame] | 33 | return SkISize::Make(width, height); |
| 34 | } |
| 35 | |
| 36 | // Construct all polygons |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 37 | void onOnceBeforeDraw() override { |
commit-bot@chromium.org | c09df2c | 2013-11-27 01:24:53 +0000 | [diff] [blame] | 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); |
bungeman@google.com | 09800bc | 2013-11-27 16:08:37 +0000 | [diff] [blame] | 52 | p4[i].set(20 * SkScalarCos(angle) + 20, 20 * SkScalarSin(angle) + 20); |
commit-bot@chromium.org | c09df2c | 2013-11-27 01:24:53 +0000 | [diff] [blame] | 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 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 88 | static void SetColorAndAlpha(SkPaint* paint, SkRandom* rand) { |
commit-bot@chromium.org | c09df2c | 2013-11-27 01:24:53 +0000 | [diff] [blame] | 89 | SkColor color = rand->nextU(); |
| 90 | color |= 0xff000000; |
| 91 | paint->setColor(color); |
| 92 | if (40 == paint->getStrokeWidth()) { |
| 93 | paint->setAlpha(0xA0); |
| 94 | } |
| 95 | } |
| 96 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 97 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | c09df2c | 2013-11-27 01:24:53 +0000 | [diff] [blame] | 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) |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 101 | constexpr int kStrokeWidths[] = {0, 10, 40}; |
commit-bot@chromium.org | c09df2c | 2013-11-27 01:24:53 +0000 | [diff] [blame] | 102 | SkASSERT(kNumStrokeWidths == SK_ARRAY_COUNT(kStrokeWidths)); |
| 103 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 104 | constexpr SkPaint::Join kJoins[] = { |
commit-bot@chromium.org | c09df2c | 2013-11-27 01:24:53 +0000 | [diff] [blame] | 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 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 113 | SkRandom rand; |
commit-bot@chromium.org | c09df2c | 2013-11-27 01:24:53 +0000 | [diff] [blame] | 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 |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 134 | constexpr SkPaint::Style kStyles[] = { |
commit-bot@chromium.org | c09df2c | 2013-11-27 01:24:53 +0000 | [diff] [blame] | 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 | |
| 154 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 155 | 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.org | c09df2c | 2013-11-27 01:24:53 +0000 | [diff] [blame] | 160 | |
| 161 | SkTArray<SkPath> fPolygons; |
| 162 | typedef GM INHERITED; |
| 163 | }; |
| 164 | |
| 165 | ////////////////////////////////////////////////////////////////////////////// |
| 166 | |
| 167 | DEF_GM(return new PolygonsGM;) |
| 168 | |
| 169 | } |