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