Chromium Code Reviews| 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); | |
|
robertphillips
2013/11/22 13:27:04
These two don't seem to be used after the followin
| |
| 68 size_t pgIndex, ptIndex; | |
| 69 for (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 (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_Scal ar1 / 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 paint->setColor(color); | |
| 90 if (color | 0x01) { | |
| 91 paint->setAlpha(color >> 24); | |
| 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 for (int style = 0; style < kNumExtraStyles; ++style) { | |
| 139 paint.setStyle(kStyles[style]); | |
|
robertphillips
2013/11/22 13:27:04
The "setStrokeWidth" call could be pulled out of t
| |
| 140 paint.setStrokeWidth(SkIntToScalar(20)); | |
| 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 |