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